]> Cypherpunks.ru repositories - gostls13.git/commitdiff
runtime: delete UpdateMemStats, replace with ReadMemStats(&stats).
authorRémy Oudompheng <oudomphe@phare.normalesup.org>
Mon, 6 Feb 2012 18:16:26 +0000 (19:16 +0100)
committerRémy Oudompheng <oudomphe@phare.normalesup.org>
Mon, 6 Feb 2012 18:16:26 +0000 (19:16 +0100)
Unexports runtime.MemStats and rename MemStatsType to MemStats.
The new accessor requires passing a pointer to a user-allocated
MemStats structure.

Fixes #2572.

R=bradfitz, rsc, bradfitz, gustavo
CC=golang-dev, remy
https://golang.org/cl/5616072

23 files changed:
src/cmd/godoc/godoc.go
src/pkg/encoding/gob/timing_test.go
src/pkg/expvar/expvar.go
src/pkg/fmt/fmt_test.go
src/pkg/net/rpc/server_test.go
src/pkg/reflect/all_test.go
src/pkg/runtime/gc_test.go
src/pkg/runtime/malloc.h
src/pkg/runtime/mem.go
src/pkg/runtime/mgc0.c
src/pkg/runtime/pprof/pprof.go
src/pkg/strconv/itoa_test.go
test/bench/garbage/parser.go
test/bench/garbage/stats.go
test/bench/garbage/tree2.go
test/chan/select2.go
test/closure.go
test/gc2.go
test/init1.go
test/malloc1.go
test/mallocrand.go
test/mallocrep.go
test/mallocrep1.go

index 34e058ab5c106744ad87c4817af1daed3f7e72a2..5745b28ba69fea070c4d3e9df9c21380e926db38 100644 (file)
@@ -1510,9 +1510,12 @@ func updateIndex() {
                log.Printf("index updated (%gs, %d bytes of source, %d files, %d lines, %d unique words, %d spots)",
                        secs, stats.Bytes, stats.Files, stats.Lines, stats.Words, stats.Spots)
        }
-       log.Printf("before GC: bytes = %d footprint = %d", runtime.MemStats.HeapAlloc, runtime.MemStats.Sys)
+       memstats := new(runtime.MemStats)
+       runtime.ReadMemStats(memstats)
+       log.Printf("before GC: bytes = %d footprint = %d", memstats.HeapAlloc, memstats.Sys)
        runtime.GC()
-       log.Printf("after  GC: bytes = %d footprint = %d", runtime.MemStats.HeapAlloc, runtime.MemStats.Sys)
+       runtime.ReadMemStats(memstats)
+       log.Printf("after  GC: bytes = %d footprint = %d", memstats.HeapAlloc, memstats.Sys)
 }
 
 func indexer() {
index 1017eb7f51d4100a8df8070360ef208b12b50c9a..b9371c42309eec79e858001e423ac106afcc1468 100644 (file)
@@ -53,8 +53,9 @@ func TestCountEncodeMallocs(t *testing.T) {
        var buf bytes.Buffer
        enc := NewEncoder(&buf)
        bench := &Bench{7, 3.2, "now is the time", []byte("for all good men")}
-       runtime.UpdateMemStats()
-       mallocs := 0 - runtime.MemStats.Mallocs
+       memstats := new(runtime.MemStats)
+       runtime.ReadMemStats(memstats)
+       mallocs := 0 - memstats.Mallocs
        const count = 1000
        for i := 0; i < count; i++ {
                err := enc.Encode(bench)
@@ -62,8 +63,8 @@ func TestCountEncodeMallocs(t *testing.T) {
                        t.Fatal("encode:", err)
                }
        }
-       runtime.UpdateMemStats()
-       mallocs += runtime.MemStats.Mallocs
+       runtime.ReadMemStats(memstats)
+       mallocs += memstats.Mallocs
        fmt.Printf("mallocs per encode of type Bench: %d\n", mallocs/count)
 }
 
@@ -79,8 +80,9 @@ func TestCountDecodeMallocs(t *testing.T) {
                }
        }
        dec := NewDecoder(&buf)
-       runtime.UpdateMemStats()
-       mallocs := 0 - runtime.MemStats.Mallocs
+       memstats := new(runtime.MemStats)
+       runtime.ReadMemStats(memstats)
+       mallocs := 0 - memstats.Mallocs
        for i := 0; i < count; i++ {
                *bench = Bench{}
                err := dec.Decode(&bench)
@@ -88,7 +90,7 @@ func TestCountDecodeMallocs(t *testing.T) {
                        t.Fatal("decode:", err)
                }
        }
-       runtime.UpdateMemStats()
-       mallocs += runtime.MemStats.Mallocs
+       runtime.ReadMemStats(memstats)
+       mallocs += memstats.Mallocs
        fmt.Printf("mallocs per decode of type Bench: %d\n", mallocs/count)
 }
index ee32eff9ea3bee7f5b51c640b3b5c4dc914c20a7..0ccfb34328d69a19b3e74e98b20583bbb811ccdd 100644 (file)
@@ -277,7 +277,9 @@ func cmdline() interface{} {
 }
 
 func memstats() interface{} {
-       return runtime.MemStats
+       stats := new(runtime.MemStats)
+       runtime.ReadMemStats(stats)
+       return *stats
 }
 
 func init() {
index cd0b0ce66be2e9f26e02065ace7fb7295ba79316..44dcae46cedfeb0e50014a5373a3e636673717a3 100644 (file)
@@ -538,13 +538,14 @@ var _ bytes.Buffer
 func TestCountMallocs(t *testing.T) {
        for _, mt := range mallocTest {
                const N = 100
-               runtime.UpdateMemStats()
-               mallocs := 0 - runtime.MemStats.Mallocs
+               memstats := new(runtime.MemStats)
+               runtime.ReadMemStats(memstats)
+               mallocs := 0 - memstats.Mallocs
                for i := 0; i < N; i++ {
                        mt.fn()
                }
-               runtime.UpdateMemStats()
-               mallocs += runtime.MemStats.Mallocs
+               runtime.ReadMemStats(memstats)
+               mallocs += memstats.Mallocs
                if mallocs/N > uint64(mt.count) {
                        t.Errorf("%s: expected %d mallocs, got %d", mt.desc, mt.count, mallocs/N)
                }
index b05c63c0563d747cf4d6be5dc4c33b8d67183025..8cfa033ccc3be0adf8a62e712a86115a9f41371e 100644 (file)
@@ -442,8 +442,9 @@ func countMallocs(dial func() (*Client, error), t *testing.T) uint64 {
        }
        args := &Args{7, 8}
        reply := new(Reply)
-       runtime.UpdateMemStats()
-       mallocs := 0 - runtime.MemStats.Mallocs
+       memstats := new(runtime.MemStats)
+       runtime.ReadMemStats(memstats)
+       mallocs := 0 - memstats.Mallocs
        const count = 100
        for i := 0; i < count; i++ {
                err := client.Call("Arith.Add", args, reply)
@@ -454,8 +455,8 @@ func countMallocs(dial func() (*Client, error), t *testing.T) uint64 {
                        t.Errorf("Add: expected %d got %d", reply.C, args.A+args.B)
                }
        }
-       runtime.UpdateMemStats()
-       mallocs += runtime.MemStats.Mallocs
+       runtime.ReadMemStats(memstats)
+       mallocs += memstats.Mallocs
        return mallocs / count
 }
 
index 8ae977912b3d0206610a95e75bad3cd7ada554c8..ad995875537653cb5e96f31a67f2c530d4da1b31 100644 (file)
@@ -1545,15 +1545,19 @@ func TestAddr(t *testing.T) {
 func noAlloc(t *testing.T, n int, f func(int)) {
        // once to prime everything
        f(-1)
-       runtime.MemStats.Mallocs = 0
+       memstats := new(runtime.MemStats)
+       runtime.ReadMemStats(memstats)
+       oldmallocs := memstats.Mallocs
 
        for j := 0; j < n; j++ {
                f(j)
        }
        // A few allocs may happen in the testing package when GOMAXPROCS > 1, so don't
        // require zero mallocs.
-       if runtime.MemStats.Mallocs > 5 {
-               t.Fatalf("%d mallocs after %d iterations", runtime.MemStats.Mallocs, n)
+       runtime.ReadMemStats(memstats)
+       mallocs := memstats.Mallocs - oldmallocs
+       if mallocs > 5 {
+               t.Fatalf("%d mallocs after %d iterations", mallocs, n)
        }
 }
 
index 00b3a04ce9d0f0fcd82af1f1db9ea6509d8007b7..739ebcba2ff81e1580959ac784471869734b3fdf 100644 (file)
@@ -10,20 +10,21 @@ import (
 )
 
 func TestGcSys(t *testing.T) {
+       memstats := new(runtime.MemStats)
        runtime.GC()
-       runtime.UpdateMemStats()
-       sys := runtime.MemStats.Sys
+       runtime.ReadMemStats(memstats)
+       sys := memstats.Sys
 
        for i := 0; i < 1000000; i++ {
                workthegc()
        }
 
        // Should only be using a few MB.
-       runtime.UpdateMemStats()
-       if sys > runtime.MemStats.Sys {
+       runtime.ReadMemStats(memstats)
+       if sys > memstats.Sys {
                sys = 0
        } else {
-               sys = runtime.MemStats.Sys - sys
+               sys = memstats.Sys - sys
        }
        t.Logf("used %d extra bytes", sys)
        if sys > 4<<20 {
index a85e1af8c701367b5560e4c0f5db89f6f862abfd..d79c86d1244e865782ada0d4f15761f61ad39900 100644 (file)
@@ -234,7 +234,7 @@ struct MStats
        } by_size[NumSizeClasses];
 };
 
-#define mstats runtime·MemStats       /* name shared with Go */
+#define mstats runtime·memStats       /* name shared with Go */
 extern MStats mstats;
 
 
index 93d155a7f829b6d564b9e67214a71a972d1fa266..3ad906ac276b684bd41bd918ebf42464700a7eff 100644 (file)
@@ -6,9 +6,9 @@ package runtime
 
 import "unsafe"
 
-type MemStatsType struct {
+// A MemStats records statistics about the memory allocator.
+type MemStats struct {
        // General statistics.
-       // Not locked during update; approximate.
        Alloc      uint64 // bytes allocated and still in use
        TotalAlloc uint64 // bytes allocated (even if freed)
        Sys        uint64 // bytes obtained from system (should be sum of XxxSys below)
@@ -43,7 +43,6 @@ type MemStatsType struct {
        DebugGC      bool
 
        // Per-size allocation statistics.
-       // Not locked during update; approximate.
        // 61 is NumSizeClasses in the C code.
        BySize [61]struct {
                Size    uint32
@@ -54,21 +53,17 @@ type MemStatsType struct {
 
 var sizeof_C_MStats uintptr // filled in by malloc.goc
 
+var memStats MemStats
+
 func init() {
-       if sizeof_C_MStats != unsafe.Sizeof(MemStats) {
-               println(sizeof_C_MStats, unsafe.Sizeof(MemStats))
+       if sizeof_C_MStats != unsafe.Sizeof(memStats) {
+               println(sizeof_C_MStats, unsafe.Sizeof(memStats))
                panic("MStats vs MemStatsType size mismatch")
        }
 }
 
-// MemStats holds statistics about the memory system.
-// The statistics may be out of date, as the information is
-// updated lazily from per-thread caches.
-// Use UpdateMemStats to bring the statistics up to date.
-var MemStats MemStatsType
-
-// UpdateMemStats brings MemStats up to date.
-func UpdateMemStats()
+// ReadMemStats populates m with memory allocator statistics.
+func ReadMemStats(m *MemStats)
 
 // GC runs a garbage collection.
 func GC()
index 78daa783621b337eb24fe426bac6a63e7089ccea..1b959286ac3d483482675c21885d620cc68ff1d4 100644 (file)
@@ -997,7 +997,7 @@ runtime·gc(int32 force)
 }
 
 void
-runtime·UpdateMemStats(void)
+runtime·ReadMemStats(MStats *stats)
 {
        // Have to acquire gcsema to stop the world,
        // because stoptheworld can only be used by
@@ -1007,6 +1007,7 @@ runtime·UpdateMemStats(void)
        m->gcing = 1;
        runtime·stoptheworld();
        cachestats();
+       *stats = mstats;
        m->gcing = 0;
        runtime·semrelease(&gcsema);
        runtime·starttheworld(false);
index d14bb141c4a8b4ae602eb4288de8b391abf7b3b5..a8e78e0ea754ad81ff933ad166178ff2a1a4b62b 100644 (file)
@@ -75,7 +75,8 @@ func WriteHeapProfile(w io.Writer) error {
 
        // Print memstats information too.
        // Pprof will ignore, but useful for people.
-       s := &runtime.MemStats
+       s := new(runtime.MemStats)
+       runtime.ReadMemStats(s)
        fmt.Fprintf(b, "\n# runtime.MemStats\n")
        fmt.Fprintf(b, "# Alloc = %d\n", s.Alloc)
        fmt.Fprintf(b, "# TotalAlloc = %d\n", s.TotalAlloc)
index d4b09a5d87d12845d824e06c1b68c93912f6df32..6687314d2f6bd9bb24ad7b3579bcc4877baf8853 100644 (file)
@@ -127,11 +127,12 @@ func TestUitoa(t *testing.T) {
 }
 
 func numAllocations(f func()) int {
-       runtime.UpdateMemStats()
-       n0 := runtime.MemStats.Mallocs
+       memstats := new(runtime.MemStats)
+       runtime.ReadMemStats(memstats)
+       n0 := memstats.Mallocs
        f()
-       runtime.UpdateMemStats()
-       return int(runtime.MemStats.Mallocs - n0)
+       runtime.ReadMemStats(memstats)
+       return int(memstats.Mallocs - n0)
 }
 
 var globalBuf [64]byte
index 9e15f6c0f4d41ee1dc0ee0f3aa3ea1ccb27767de..d66281b6bf49d6fbda1f391709b956260123792c 100644 (file)
@@ -73,7 +73,7 @@ func parseDir(dirpath string) map[string]*ast.Package {
 }
 
 func main() {
-       st := &runtime.MemStats
+       st := new(runtime.MemStats)
        packages = append(packages, packages...)
        packages = append(packages, packages...)
        n := flag.Int("n", 4, "iterations")
@@ -83,14 +83,17 @@ func main() {
 
        var lastParsed []map[string]*ast.Package
        var t0 time.Time
+       var numGC uint32
+       var pauseTotalNs uint64
        pkgroot := runtime.GOROOT() + "/src/pkg/"
        for pass := 0; pass < 2; pass++ {
                // Once the heap is grown to full size, reset counters.
                // This hides the start-up pauses, which are much smaller
                // than the normal pauses and would otherwise make
                // the average look much better than it actually is.
-               st.NumGC = 0
-               st.PauseTotalNs = 0
+               runtime.ReadMemStats(st)
+               numGC = st.NumGC
+               pauseTotalNs = st.PauseTotalNs
                t0 = time.Now()
 
                for i := 0; i < *n; i++ {
@@ -107,6 +110,9 @@ func main() {
        }
        t1 := time.Now()
 
+       runtime.ReadMemStats(st)
+       st.NumGC -= numGC
+       st.PauseTotalNs -= pauseTotalNs
        fmt.Printf("Alloc=%d/%d Heap=%d Mallocs=%d PauseTime=%.3f/%d = %.3f\n",
                st.Alloc, st.TotalAlloc,
                st.Sys,
@@ -142,9 +148,7 @@ var packages = []string{
        "container/list",
        "container/ring",
        "crypto/aes",
-       "crypto/blowfish",
        "crypto/hmac",
-       "crypto/md4",
        "crypto/md5",
        "crypto/rand",
        "crypto/rc4",
@@ -155,7 +159,6 @@ var packages = []string{
        "crypto/subtle",
        "crypto/tls",
        "crypto/x509",
-       "crypto/xtea",
        "debug/dwarf",
        "debug/macho",
        "debug/elf",
@@ -164,7 +167,6 @@ var packages = []string{
        "encoding/ascii85",
        "encoding/base64",
        "encoding/binary",
-       "encoding/git85",
        "encoding/hex",
        "encoding/pem",
        "os/exec",
@@ -193,8 +195,7 @@ var packages = []string{
        "mime",
        "net",
        "os",
-       "os/signal",
-       "patch",
+       "exp/signal",
        "path",
        "math/rand",
        "reflect",
@@ -219,6 +220,5 @@ var packages = []string{
        "unicode",
        "unicode/utf8",
        "unicode/utf16",
-       "websocket",
        "encoding/xml",
 }
index 985e7eaf5dc3fc23894d6da378189f528da4db93..cdcb32f9b65826845cad2d13355ea57e8fd8e7c2 100644 (file)
@@ -12,7 +12,8 @@ import (
 )
 
 func gcstats(name string, n int, t time.Duration) {
-       st := &runtime.MemStats
+       st := new(runtime.MemStats)
+       runtime.ReadMemStats(st)
        fmt.Printf("garbage.%sMem Alloc=%d/%d Heap=%d NextGC=%d Mallocs=%d\n", name, st.Alloc, st.TotalAlloc, st.Sys, st.NextGC, st.Mallocs)
        fmt.Printf("garbage.%s %d %d ns/op\n", name, n, t.Nanoseconds()/int64(n))
        fmt.Printf("garbage.%sLastPause 1 %d ns/op\n", name, st.PauseNs[(st.NumGC-1)%uint32(len(st.PauseNs))])
index 6d78f72c5b850becf59467755f3fdfb1e83507fe..3db0a0ba3c3a1939888b90b5366922f18007d25b 100644 (file)
@@ -30,6 +30,7 @@ var (
        heap        *Object
        calls       [20]int
        numobjects  int64
+       memstats    runtime.MemStats
 )
 
 func buildHeap() {
@@ -55,10 +56,10 @@ func buildTree(objsize, size float64, depth int) (*Object, float64) {
 
 func gc() {
        runtime.GC()
-       runtime.UpdateMemStats()
-       pause := runtime.MemStats.PauseTotalNs
-       inuse := runtime.MemStats.Alloc
-       free := runtime.MemStats.TotalAlloc - inuse
+       runtime.ReadMemStats(&memstats)
+       pause := memstats.PauseTotalNs
+       inuse := memstats.Alloc
+       free := memstats.TotalAlloc - inuse
        fmt.Printf("gc pause: %8.3f ms; collect: %8.0f MB; heapsize: %8.0f MB\n",
                float64(pause-lastPauseNs)/1e6,
                float64(free-lastFree)/1048576,
@@ -71,9 +72,9 @@ func main() {
        flag.Parse()
        buildHeap()
        runtime.GOMAXPROCS(*cpus)
-       runtime.UpdateMemStats()
-       lastPauseNs = runtime.MemStats.PauseTotalNs
-       lastFree = runtime.MemStats.TotalAlloc - runtime.MemStats.Alloc
+       runtime.ReadMemStats(&memstats)
+       lastPauseNs = memstats.PauseTotalNs
+       lastFree = memstats.TotalAlloc - memstats.Alloc
        if *cpuprofile != "" {
                f, err := os.Create(*cpuprofile)
                if err != nil {
index e24c51ed16c2b7b932d6b08b662644c2c80a3b61..2cbb86ec626c72390965b8e2db8405368608028b 100644 (file)
@@ -35,14 +35,17 @@ func main() {
        go sender(c, 100000)
        receiver(c, dummy, 100000)
        runtime.GC()
-       runtime.MemStats.Alloc = 0
+       memstats := new(runtime.MemStats)
+       runtime.ReadMemStats(memstats)
+       alloc := memstats.Alloc
 
        // second time shouldn't increase footprint by much
        go sender(c, 100000)
        receiver(c, dummy, 100000)
        runtime.GC()
+       runtime.ReadMemStats(memstats)
 
-       if runtime.MemStats.Alloc > 1e5 {
-               println("BUG: too much memory for 100,000 selects:", runtime.MemStats.Alloc)
+       if memstats.Alloc-alloc > 1e5 {
+               println("BUG: too much memory for 100,000 selects:", memstats.Alloc-alloc)
        }
 }
index 97da1dd23045d6d65393c40251e2fa0a73410782..c2248d68e95773a78d3e1d9b53edd17ad55da47a 100644 (file)
@@ -92,8 +92,9 @@ func main() {
        go h()
        check([]int{100, 200, 101, 201, 500, 101, 201, 500})
 
-       runtime.UpdateMemStats()
-        n0 := runtime.MemStats.Mallocs
+       memstats := new(runtime.MemStats)
+       runtime.ReadMemStats(memstats)
+       n0 := memstats.Mallocs
 
        x, y := newfunc(), newfunc()
        if x(1) != 1 || y(2) != 2 {
@@ -101,8 +102,8 @@ func main() {
                fail = true
        }
 
-       runtime.UpdateMemStats()
-        if n0 != runtime.MemStats.Mallocs {
+       runtime.ReadMemStats(memstats)
+       if n0 != memstats.Mallocs {
                println("newfunc allocated unexpectedly")
                fail = true
        }
@@ -110,7 +111,7 @@ func main() {
        ff(1)
 
        if fail {
-               panic("fail") 
+               panic("fail")
        }
 }
 
index c54d807df724ff0d0f254fd30568fff6b091b8fe..772f9810daaba053c60da480ede9e01b7874a531 100644 (file)
@@ -19,7 +19,9 @@ import (
 
 func main() {
        const N = 10000
-       st := runtime.MemStats
+       st := new(runtime.MemStats)
+       memstats := new(runtime.MemStats)
+       runtime.ReadMemStats(st)
        for i := 0; i < N; i++ {
                c := make(chan int, 10)
                _ = c
@@ -33,8 +35,8 @@ func main() {
                }
        }
 
-       runtime.UpdateMemStats()
-       obj := runtime.MemStats.HeapObjects - st.HeapObjects
+       runtime.ReadMemStats(memstats)
+       obj := memstats.HeapObjects - st.HeapObjects
        if obj > N/5 {
                fmt.Println("too many objects left:", obj)
                os.Exit(1)
index 9ce3c12ee68f9267f4d4be3709a7fc314d7ee4ed..56ef17249fde7c722c447d9077fc26138c3c1913 100644 (file)
@@ -16,10 +16,11 @@ func init() {
        c := make(chan int)
        go send(c)
        <-c
-       
-       const chunk = 1<<20
-       runtime.UpdateMemStats()
-       sys := runtime.MemStats.Sys     
+
+       const chunk = 1 << 20
+       memstats := new(runtime.MemStats)
+       runtime.ReadMemStats(memstats)
+       sys := memstats.Sys
        b := make([]byte, chunk)
        for i := range b {
                b[i] = byte(i%10 + '0')
@@ -28,8 +29,8 @@ func init() {
        for i := 0; i < 1000; i++ {
                x = []byte(s)
        }
-       runtime.UpdateMemStats()
-       sys1 := runtime.MemStats.Sys
+       runtime.ReadMemStats(memstats)
+       sys1 := memstats.Sys
        if sys1-sys > chunk*50 {
                println("allocated 1000 chunks of", chunk, "and used ", sys1-sys, "memory")
        }
@@ -41,4 +42,3 @@ func send(c chan int) {
 
 func main() {
 }
-
index 61f1797c75d43338c60ade64229959ec41099ce0..0f7f0b267a035ca1a1384f1beaac4dcec5ac1d00 100644 (file)
@@ -17,9 +17,10 @@ import (
 var chatty = flag.Bool("v", false, "chatty")
 
 func main() {
+       memstats := new(runtime.MemStats)
        runtime.Free(runtime.Alloc(1))
-       runtime.UpdateMemStats()
+       runtime.ReadMemStats(memstats)
        if *chatty {
-               fmt.Printf("%+v %v\n", runtime.MemStats, uint64(0))
+               fmt.Printf("%+v %v\n", memstats, uint64(0))
        }
 }
index 726e36799a82b104e54bd6d995958de49686cdff..69d07cec5d2f93b2941c8782abf0da0dcac804ee 100644 (file)
@@ -21,8 +21,9 @@ var footprint uint64
 var allocated uint64
 
 func bigger() {
-       runtime.UpdateMemStats()
-       if f := runtime.MemStats.Sys; footprint < f {
+       memstats := new(runtime.MemStats)
+       runtime.ReadMemStats(memstats)
+       if f := memstats.Sys; footprint < f {
                footprint = f
                if *chatty {
                        println("Footprint", footprint, " for ", allocated)
index cffcd1638fcecface8e3002b76fc6b978a73d5bc..4188da9b833519c1b8d46608567a81a616857336 100644 (file)
@@ -16,10 +16,12 @@ import (
 var chatty = flag.Bool("v", false, "chatty")
 
 var oldsys uint64
+var memstats runtime.MemStats
 
 func bigger() {
-       runtime.UpdateMemStats()
-       if st := runtime.MemStats; oldsys < st.Sys {
+       st := &memstats
+       runtime.ReadMemStats(st)
+       if oldsys < st.Sys {
                oldsys = st.Sys
                if *chatty {
                        println(st.Sys, " system bytes for ", st.Alloc, " Go bytes")
@@ -32,26 +34,26 @@ func bigger() {
 }
 
 func main() {
-       runtime.GC()               // clean up garbage from init
-       runtime.UpdateMemStats()   // first call can do some allocations
-       runtime.MemProfileRate = 0 // disable profiler
-       runtime.MemStats.Alloc = 0 // ignore stacks
+       runtime.GC()                    // clean up garbage from init
+       runtime.ReadMemStats(&memstats) // first call can do some allocations
+       runtime.MemProfileRate = 0      // disable profiler
+       stacks := memstats.Alloc        // ignore stacks
        flag.Parse()
        for i := 0; i < 1<<7; i++ {
                for j := 1; j <= 1<<22; j <<= 1 {
                        if i == 0 && *chatty {
                                println("First alloc:", j)
                        }
-                       if a := runtime.MemStats.Alloc; a != 0 {
+                       if a := memstats.Alloc - stacks; a != 0 {
                                println("no allocations but stats report", a, "bytes allocated")
                                panic("fail")
                        }
                        b := runtime.Alloc(uintptr(j))
-                       runtime.UpdateMemStats()
-                       during := runtime.MemStats.Alloc
+                       runtime.ReadMemStats(&memstats)
+                       during := memstats.Alloc - stacks
                        runtime.Free(b)
-                       runtime.UpdateMemStats()
-                       if a := runtime.MemStats.Alloc; a != 0 {
+                       runtime.ReadMemStats(&memstats)
+                       if a := memstats.Alloc - stacks; a != 0 {
                                println("allocated ", j, ": wrong stats: during=", during, " after=", a, " (want 0)")
                                panic("fail")
                        }
index 0b1479900e69805222f4bfad0e84516df669ea3b..f9d7286efd9d3a8aa59c64d701db484799ce598c 100644 (file)
@@ -20,7 +20,7 @@ var reverse = flag.Bool("r", false, "reverse")
 var longtest = flag.Bool("l", false, "long test")
 
 var b []*byte
-var stats = &runtime.MemStats
+var stats = new(runtime.MemStats)
 
 func OkAmount(size, n uintptr) bool {
        if n < size {
@@ -42,7 +42,7 @@ func AllocAndFree(size, count int) {
        if *chatty {
                fmt.Printf("size=%d count=%d ...\n", size, count)
        }
-       runtime.UpdateMemStats()
+       runtime.ReadMemStats(stats)
        n1 := stats.Alloc
        for i := 0; i < count; i++ {
                b[i] = runtime.Alloc(uintptr(size))
@@ -51,13 +51,13 @@ func AllocAndFree(size, count int) {
                        println("lookup failed: got", base, n, "for", b[i])
                        panic("fail")
                }
-               runtime.UpdateMemStats()
+               runtime.ReadMemStats(stats)
                if stats.Sys > 1e9 {
                        println("too much memory allocated")
                        panic("fail")
                }
        }
-       runtime.UpdateMemStats()
+       runtime.ReadMemStats(stats)
        n2 := stats.Alloc
        if *chatty {
                fmt.Printf("size=%d count=%d stats=%+v\n", size, count, *stats)
@@ -75,17 +75,17 @@ func AllocAndFree(size, count int) {
                        panic("fail")
                }
                runtime.Free(b[i])
-               runtime.UpdateMemStats()
+               runtime.ReadMemStats(stats)
                if stats.Alloc != uint64(alloc-n) {
                        println("free alloc got", stats.Alloc, "expected", alloc-n, "after free of", n)
                        panic("fail")
                }
-               if runtime.MemStats.Sys > 1e9 {
+               if stats.Sys > 1e9 {
                        println("too much memory allocated")
                        panic("fail")
                }
        }
-       runtime.UpdateMemStats()
+       runtime.ReadMemStats(stats)
        n4 := stats.Alloc
 
        if *chatty {