]> Cypherpunks repositories - gostls13.git/commitdiff
runtime/pprof: read memstats earlier in profile handler
authorRuss Cox <rsc@golang.org>
Wed, 29 Nov 2017 20:38:52 +0000 (15:38 -0500)
committerRuss Cox <rsc@golang.org>
Fri, 1 Dec 2017 00:23:05 +0000 (00:23 +0000)
Reading the mem stats before our own allocations
avoids cluttering memory stats with our recent garbage.

Fixes #20565.

Change-Id: I3b0046c8300dca83cea24013ffebc32b2ae7f742
Reviewed-on: https://go-review.googlesource.com/80739
Reviewed-by: Austin Clements <austin@google.com>
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/runtime/pprof/pprof.go

index f76dde28d444c12cb62a9f09abb68bce5883f23d..d3382a5589d8a8de258fcddf59c12d5f3130ee96 100644 (file)
@@ -509,6 +509,14 @@ func countHeap() int {
 
 // writeHeap writes the current runtime heap profile to w.
 func writeHeap(w io.Writer, debug int) error {
+       var memStats *runtime.MemStats
+       if debug != 0 {
+               // Read mem stats first, so that our other allocations
+               // do not appear in the statistics.
+               memStats = new(runtime.MemStats)
+               runtime.ReadMemStats(memStats)
+       }
+
        // Find out how many records there are (MemProfile(nil, true)),
        // allocate that many records, and get the data.
        // There's a race—more records might be added between
@@ -571,8 +579,7 @@ func writeHeap(w io.Writer, debug int) error {
 
        // Print memstats information too.
        // Pprof will ignore, but useful for people
-       s := new(runtime.MemStats)
-       runtime.ReadMemStats(s)
+       s := memStats
        fmt.Fprintf(w, "\n# runtime.MemStats\n")
        fmt.Fprintf(w, "# Alloc = %d\n", s.Alloc)
        fmt.Fprintf(w, "# TotalAlloc = %d\n", s.TotalAlloc)