From: Austin Clements Date: Tue, 18 Apr 2017 20:41:19 +0000 (-0400) Subject: runtime/pprof: fix period information X-Git-Tag: go1.9beta1~590 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=0c0c94a9dcf90fbb48343d5a02787758d64e306d;p=gostls13.git runtime/pprof: fix period information The period recorded in CPU profiles is in nanoseconds, but was being computed incorrectly as hz * 1000. As a result, many absolute times displayed by pprof were incorrect. Fix this by computing the period correctly. Change-Id: I6fadd6d8ad3e57f31e8cc7a25a24fcaec510d8d4 Reviewed-on: https://go-review.googlesource.com/40995 Run-TryBot: Austin Clements TryBot-Result: Gobot Gobot Reviewed-by: Michael Hudson-Doyle Reviewed-by: Russ Cox --- diff --git a/src/runtime/pprof/proto.go b/src/runtime/pprof/proto.go index 0f74e119b6..923fa2188a 100644 --- a/src/runtime/pprof/proto.go +++ b/src/runtime/pprof/proto.go @@ -270,7 +270,9 @@ func (b *profileBuilder) addCPUData(data []uint64, tags []unsafe.Pointer) error if data[0] != 3 || data[2] == 0 { return fmt.Errorf("malformed profile") } - b.period = int64(data[2]) * 1000 + // data[2] is sampling rate in Hz. Convert to sampling + // period in nanoseconds. + b.period = 1e9 / int64(data[2]) b.havePeriod = true data = data[3:] } diff --git a/src/runtime/pprof/proto_test.go b/src/runtime/pprof/proto_test.go index 2f10419147..59c1080e7b 100644 --- a/src/runtime/pprof/proto_test.go +++ b/src/runtime/pprof/proto_test.go @@ -40,7 +40,7 @@ func TestConvertCPUProfileEmpty(t *testing.T) { // A test server with mock cpu profile data. var buf bytes.Buffer - b := []uint64{3, 0, 2000} // empty profile with 2ms sample period + b := []uint64{3, 0, 500} // empty profile at 500 Hz (2ms sample period) p, err := translateCPUProfile(b) if err != nil { t.Fatalf("translateCPUProfile: %v", err) @@ -103,7 +103,7 @@ func TestConvertCPUProfile(t *testing.T) { addr1, addr2, map1, map2 := testPCs(t) b := []uint64{ - 3, 0, 2000, // periodMs = 2000 + 3, 0, 500, // hz = 500 5, 0, 10, uint64(addr1), uint64(addr1 + 2), // 10 samples in addr1 5, 0, 40, uint64(addr2), uint64(addr2 + 2), // 40 samples in addr2 5, 0, 10, uint64(addr1), uint64(addr1 + 2), // 10 samples in addr1