Skip to content

Commit 0c0c94a

Browse files
committed
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 <austin@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Hudson-Doyle <michael.hudson@canonical.com> Reviewed-by: Russ Cox <rsc@golang.org>
1 parent f3f3f0d commit 0c0c94a

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

src/runtime/pprof/proto.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,9 @@ func (b *profileBuilder) addCPUData(data []uint64, tags []unsafe.Pointer) error
270270
if data[0] != 3 || data[2] == 0 {
271271
return fmt.Errorf("malformed profile")
272272
}
273-
b.period = int64(data[2]) * 1000
273+
// data[2] is sampling rate in Hz. Convert to sampling
274+
// period in nanoseconds.
275+
b.period = 1e9 / int64(data[2])
274276
b.havePeriod = true
275277
data = data[3:]
276278
}

src/runtime/pprof/proto_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestConvertCPUProfileEmpty(t *testing.T) {
4040
// A test server with mock cpu profile data.
4141
var buf bytes.Buffer
4242

43-
b := []uint64{3, 0, 2000} // empty profile with 2ms sample period
43+
b := []uint64{3, 0, 500} // empty profile at 500 Hz (2ms sample period)
4444
p, err := translateCPUProfile(b)
4545
if err != nil {
4646
t.Fatalf("translateCPUProfile: %v", err)
@@ -103,7 +103,7 @@ func TestConvertCPUProfile(t *testing.T) {
103103
addr1, addr2, map1, map2 := testPCs(t)
104104

105105
b := []uint64{
106-
3, 0, 2000, // periodMs = 2000
106+
3, 0, 500, // hz = 500
107107
5, 0, 10, uint64(addr1), uint64(addr1 + 2), // 10 samples in addr1
108108
5, 0, 40, uint64(addr2), uint64(addr2 + 2), // 40 samples in addr2
109109
5, 0, 10, uint64(addr1), uint64(addr1 + 2), // 10 samples in addr1

0 commit comments

Comments
 (0)