Skip to content

Commit

Permalink
runtime/pprof: compare all samples vs rusage in TestCPUProfileMultith…
Browse files Browse the repository at this point in the history
…readMagnitude

TestCPUProfileMultithreadMagnitude compares pprof results vs OS rusage
to verify that pprof is capturing all CPU usage. Presently it compares
the sum of cpuHog1 samples vs rusage. However, background usage from the
scheduler, GC, etc can cause additional CPU usage causing test failures
if rusage is too far off from the cpuHog1 samples.

That said, this test doesn't actually need to care about cpuHog1
samples. It simply cares that pprof samples match rusage, not what the
breakdown of usage was. As a result, we can compare the sum of _all_
pprof samples vs rusage, which should implicitly include any background
CPU usage.

Fixes golang#50097.

Change-Id: I649a18de5b3dcf58b62be5962fa508d14cd4dc79
Reviewed-on: https://go-review.googlesource.com/c/go/+/379535
Trust: Michael Pratt <mpratt@google.com>
Run-TryBot: Michael Pratt <mpratt@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
  • Loading branch information
prattmic authored and jproberts committed Jun 21, 2022
1 parent 9469843 commit 5b7a11c
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/runtime/pprof/pprof_test.go
Expand Up @@ -154,14 +154,6 @@ func TestCPUProfileMultithreadMagnitude(t *testing.T) {
maxDiff = 0.40
}

// This test compares the process's total CPU time against the CPU
// profiler's view of time spent in direct execution of user code.
// Background work, especially from the garbage collector, adds noise to
// that measurement. Disable automatic triggering of the GC, and then
// request a complete GC cycle (up through sweep termination).
defer debug.SetGCPercent(debug.SetGCPercent(-1))
runtime.GC()

compare := func(a, b time.Duration, maxDiff float64) error {
if a <= 0 || b <= 0 {
return fmt.Errorf("Expected both time reports to be positive")
Expand Down Expand Up @@ -221,11 +213,14 @@ func TestCPUProfileMultithreadMagnitude(t *testing.T) {
}
}

// cpuHog1 called above is the primary source of CPU
// load, but there may be some background work by the
// runtime. Since the OS rusage measurement will
// include all work done by the process, also compare
// against all samples in our profile.
var value time.Duration
for _, sample := range p.Sample {
if stackContains("runtime/pprof.cpuHog1", uintptr(sample.Value[0]), sample.Location, sample.Label) {
value += time.Duration(sample.Value[1]) * time.Nanosecond
}
value += time.Duration(sample.Value[1]) * time.Nanosecond
}

t.Logf("compare %s vs %s", cpuTime, value)
Expand Down

0 comments on commit 5b7a11c

Please sign in to comment.