Commit 301b127a authored by Russ Cox's avatar Russ Cox

runtime/pprof: read memstats earlier in profile handler

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/80739Reviewed-by: default avatarAustin Clements <austin@google.com>
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 9cc9f108
...@@ -509,6 +509,14 @@ func countHeap() int { ...@@ -509,6 +509,14 @@ func countHeap() int {
// writeHeap writes the current runtime heap profile to w. // writeHeap writes the current runtime heap profile to w.
func writeHeap(w io.Writer, debug int) error { 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)), // Find out how many records there are (MemProfile(nil, true)),
// allocate that many records, and get the data. // allocate that many records, and get the data.
// There's a race—more records might be added between // There's a race—more records might be added between
...@@ -571,8 +579,7 @@ func writeHeap(w io.Writer, debug int) error { ...@@ -571,8 +579,7 @@ func writeHeap(w io.Writer, debug int) error {
// Print memstats information too. // Print memstats information too.
// Pprof will ignore, but useful for people // Pprof will ignore, but useful for people
s := new(runtime.MemStats) s := memStats
runtime.ReadMemStats(s)
fmt.Fprintf(w, "\n# runtime.MemStats\n") fmt.Fprintf(w, "\n# runtime.MemStats\n")
fmt.Fprintf(w, "# Alloc = %d\n", s.Alloc) fmt.Fprintf(w, "# Alloc = %d\n", s.Alloc)
fmt.Fprintf(w, "# TotalAlloc = %d\n", s.TotalAlloc) fmt.Fprintf(w, "# TotalAlloc = %d\n", s.TotalAlloc)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment