]> Cypherpunks.ru repositories - gostls13.git/commitdiff
runtime: add UpdateMemStats, use in tests
authorRuss Cox <rsc@golang.org>
Fri, 22 Jul 2011 04:55:01 +0000 (00:55 -0400)
committerRuss Cox <rsc@golang.org>
Fri, 22 Jul 2011 04:55:01 +0000 (00:55 -0400)
Drops mallocrep1.go back to a reasonable
amount of time.  (154 -> 0.8 seconds on my Mac)

Fixes #2085.

R=golang-dev, dvyukov, r
CC=golang-dev
https://golang.org/cl/4811045

src/pkg/runtime/mem.go
src/pkg/runtime/mgc0.c
test/gc2.go
test/malloc1.go
test/mallocrand.go
test/mallocrep.go
test/mallocrep1.go

index c3316d44c0df835000504348afeea50bd0bdf3e7..93d155a7f829b6d564b9e67214a71a972d1fa266 100644 (file)
@@ -62,8 +62,13 @@ func init() {
 }
 
 // MemStats holds statistics about the memory system.
-// The statistics are only approximate, as they are not interlocked on update.
+// 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()
+
 // GC runs a garbage collection.
 func GC()
index bc373d890904eb45cd15e68573adabefa7c84540..6325aadc67273ded5dc618554096c82bd57c3ac4 100644 (file)
@@ -663,6 +663,22 @@ runtime·gc(int32 force)
                runtime·gc(1);
 }
 
+void
+runtime·UpdateMemStats(void)
+{
+       // Have to acquire gcsema to stop the world,
+       // because stoptheworld can only be used by
+       // one goroutine at a time, and there might be
+       // a pending garbage collection already calling it.
+       runtime·semacquire(&gcsema);
+       m->gcing = 1;
+       runtime·stoptheworld();
+       cachestats();
+       m->gcing = 0;
+       runtime·semrelease(&gcsema);
+       runtime·starttheworld();
+}
+
 static void
 runfinq(void)
 {
index c5c6cbe4bb6ac417638bc10ed740ece9a54c497e..c54d807df724ff0d0f254fd30568fff6b091b8fe 100644 (file)
@@ -32,7 +32,8 @@ func main() {
                        }
                }
        }
-       
+
+       runtime.UpdateMemStats()
        obj := runtime.MemStats.HeapObjects - st.HeapObjects
        if obj > N/5 {
                fmt.Println("too many objects left:", obj)
index 146976467b01338cf4da81f4f6e4fea44f00df68..61f1797c75d43338c60ade64229959ec41099ce0 100644 (file)
@@ -18,6 +18,7 @@ var chatty = flag.Bool("v", false, "chatty")
 
 func main() {
        runtime.Free(runtime.Alloc(1))
+       runtime.UpdateMemStats()
        if *chatty {
                fmt.Printf("%+v %v\n", runtime.MemStats, uint64(0))
        }
index e6b422e2244afaa5607d569e4e1e509a1e0e88df..f014b441b2b66c85ada5b756c62069e0eb435350 100644 (file)
@@ -21,6 +21,7 @@ var footprint uint64
 var allocated uint64
 
 func bigger() {
+       runtime.UpdateMemStats()
        if f := runtime.MemStats.Sys; footprint < f {
                footprint = f
                if *chatty {
index 43233b7b74b042234704c862bc4d2afbc5bb19aa..9f47e52e2b60a80776ac1070ac1ef5f54e37a89f 100644 (file)
@@ -18,6 +18,7 @@ var chatty = flag.Bool("v", false, "chatty")
 var oldsys uint64
 
 func bigger() {
+       runtime.UpdateMemStats()
        if st := runtime.MemStats; oldsys < st.Sys {
                oldsys = st.Sys
                if *chatty {
@@ -31,7 +32,7 @@ func bigger() {
 }
 
 func main() {
-       runtime.GC()               // clean up garbage from init
+       runtime.GC()               // clean up garbage from init
        runtime.MemProfileRate = 0 // disable profiler
        runtime.MemStats.Alloc = 0 // ignore stacks
        flag.Parse()
@@ -45,9 +46,10 @@ func main() {
                                panic("fail")
                        }
                        b := runtime.Alloc(uintptr(j))
+                       runtime.UpdateMemStats()
                        during := runtime.MemStats.Alloc
                        runtime.Free(b)
-                       runtime.GC()
+                       runtime.UpdateMemStats()
                        if a := runtime.MemStats.Alloc; a != 0 {
                                println("allocated ", j, ": wrong stats: during=", during, " after=", a, " (want 0)")
                                panic("fail")
index 079ae94226084935c3216bfb3f7368795005f886..0b1479900e69805222f4bfad0e84516df669ea3b 100644 (file)
@@ -42,6 +42,7 @@ func AllocAndFree(size, count int) {
        if *chatty {
                fmt.Printf("size=%d count=%d ...\n", size, count)
        }
+       runtime.UpdateMemStats()
        n1 := stats.Alloc
        for i := 0; i < count; i++ {
                b[i] = runtime.Alloc(uintptr(size))
@@ -50,17 +51,18 @@ func AllocAndFree(size, count int) {
                        println("lookup failed: got", base, n, "for", b[i])
                        panic("fail")
                }
-               if runtime.MemStats.Sys > 1e9 {
+               runtime.UpdateMemStats()
+               if stats.Sys > 1e9 {
                        println("too much memory allocated")
                        panic("fail")
                }
        }
+       runtime.UpdateMemStats()
        n2 := stats.Alloc
        if *chatty {
                fmt.Printf("size=%d count=%d stats=%+v\n", size, count, *stats)
        }
        n3 := stats.Alloc
-       runtime.GC()
        for j := 0; j < count; j++ {
                i := j
                if *reverse {
@@ -73,7 +75,7 @@ func AllocAndFree(size, count int) {
                        panic("fail")
                }
                runtime.Free(b[i])
-               runtime.GC()
+               runtime.UpdateMemStats()
                if stats.Alloc != uint64(alloc-n) {
                        println("free alloc got", stats.Alloc, "expected", alloc-n, "after free of", n)
                        panic("fail")
@@ -83,6 +85,7 @@ func AllocAndFree(size, count int) {
                        panic("fail")
                }
        }
+       runtime.UpdateMemStats()
        n4 := stats.Alloc
 
        if *chatty {