// the testing utility package main import ( "flag" "fmt" "os" "runtime" ) import ( _ "archive/tar" _ "archive/zip" _ "bufio" _ "bytes" _ "compress/bzip2" _ "compress/flate" _ "compress/gzip" _ "compress/lzw" _ "compress/zlib" _ "container/heap" _ "container/list" _ "container/ring" _ "crypto" _ "crypto/aes" _ "crypto/cipher" _ "crypto/des" _ "crypto/dsa" _ "crypto/ecdsa" _ "crypto/elliptic" _ "crypto/hmac" _ "crypto/md5" _ "crypto/rand" _ "crypto/rc4" _ "crypto/rsa" _ "crypto/sha1" _ "crypto/sha256" _ "crypto/sha512" _ "crypto/subtle" _ "crypto/tls" _ "crypto/x509" _ "crypto/x509/pkix" _ "database/sql" _ "database/sql/driver" _ "debug/dwarf" _ "debug/elf" _ "debug/gosym" _ "debug/macho" _ "debug/pe" _ "encoding/ascii85" _ "encoding/asn1" _ "encoding/base32" _ "encoding/base64" _ "encoding/binary" _ "encoding/csv" _ "encoding/gob" _ "encoding/hex" _ "encoding/json" _ "encoding/pem" _ "encoding/xml" _ "errors" _ "exp/ebnf" _ "exp/html" _ "exp/html/atom" _ "exp/inotify" _ "exp/locale/collate" _ "exp/locale/collate/build" _ "exp/norm" _ "exp/proxy" _ "exp/types" _ "exp/utf8string" _ "expvar" _ "flag" _ "fmt" _ "go/ast" _ "go/build" _ "go/doc" _ "go/parser" _ "go/printer" _ "go/scanner" _ "go/token" _ "hash" _ "hash/adler32" _ "hash/crc32" _ "hash/crc64" _ "hash/fnv" _ "html" _ "html/template" _ "image" _ "image/color" _ "image/draw" _ "image/gif" _ "image/jpeg" _ "image/png" _ "index/suffixarray" _ "io" _ "io/ioutil" _ "log" _ "log/syslog" _ "math" _ "math/big" _ "math/cmplx" _ "math/rand" _ "mime" _ "mime/multipart" _ "net" _ "net/http" _ "net/http/cgi" _ "net/http/fcgi" _ "net/http/httputil" _ "net/mail" _ "net/rpc" _ "net/rpc/jsonrpc" _ "net/smtp" _ "net/textproto" _ "net/url" _ "old/netchan" _ "os" _ "os/exec" _ "os/signal" _ "os/user" _ "path" _ "path/filepath" _ "reflect" _ "regexp" _ "regexp/syntax" _ "runtime" _ "runtime/cgo" _ "runtime/debug" _ "runtime/pprof" _ "sort" _ "strconv" _ "strings" _ "sync" _ "sync/atomic" _ "syscall" _ "testing/iotest" _ "text/scanner" _ "text/tabwriter" _ "text/template" _ "text/template/parse" _ "time" _ "unicode" _ "unicode/utf16" _ "unicode/utf8" _ "unsafe" ) func main() { flagSize := flag.Int("size", 0, "alloc block size in bytes (must be a power of 2)") flagMem := flag.Int("mem", 0, "total mem to allocate in MB") flag.Parse() if *flagSize <= 0 || *flagSize&(*flagSize-1) != 0 || *flagMem <= 0 { flag.PrintDefaults() os.Exit(1) } sz := *flagSize cnt := *flagMem * (1 << 20) / sz var memStats runtime.MemStats runtime.GC() runtime.ReadMemStats(&memStats) a0 := memStats.Alloc for i := 0; i < cnt; i++ { p := make([]byte, sz) func(p []byte) {}(p) } runtime.GC() runtime.ReadMemStats(&memStats) a1 := memStats.Alloc fmt.Printf("%.2f%% pinned (%d MiB)\n", 100*float64(a1-a0)/float64(cnt)/float64(sz), a1>>20) }