1
0
mirror of https://github.com/golang/go synced 2024-11-17 04:04:46 -07:00

runtime: fix flake in TestCgoPprofThread

If the test's main goroutine receives a SIGPROF while creating the
C-owned thread for the test, that sample will appear in the resulting
profile. The root end of that stack will show a set of Go functions. The
leaf end will be the C functions returned by the SetCgoTraceback
handler, which will confuse the test runner.

Add a label to the main goroutine while it calls in to C, so all profile
samples that triggered the SetCgoTraceback handler are either correct,
or can easily be excluded from the test's analysis. (The labels will not
apply to the resulting C-owned thread, which does not use goroutines.)

Fixes #43174

Change-Id: Ica3100ca0f191dcf91b30b0084e8541c5a25689f
Reviewed-on: https://go-review.googlesource.com/c/go/+/370135
Reviewed-by: Michael Pratt <mpratt@google.com>
Trust: Michael Pratt <mpratt@google.com>
Run-TryBot: Michael Pratt <mpratt@google.com>
Trust: Michael Knyszek <mknyszek@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Rhys Hiltner 2021-12-07 13:32:24 -08:00 committed by Michael Pratt
parent 3042ba34db
commit 46db6aa157
2 changed files with 10 additions and 2 deletions

View File

@ -314,7 +314,7 @@ func testCgoPprof(t *testing.T, buildArg, runArg, top, bottom string) {
defer os.Remove(fn)
for try := 0; try < 2; try++ {
cmd := testenv.CleanCmdEnv(exec.Command(testenv.GoToolPath(t), "tool", "pprof", "-traces"))
cmd := testenv.CleanCmdEnv(exec.Command(testenv.GoToolPath(t), "tool", "pprof", "-tagignore=ignore", "-traces"))
// Check that pprof works both with and without explicit executable on command line.
if try == 0 {
cmd.Args = append(cmd.Args, exe, fn)

View File

@ -64,6 +64,7 @@ void runCPUHogThread(void) {
import "C"
import (
"context"
"fmt"
"os"
"runtime"
@ -98,7 +99,14 @@ func pprofThread() {
os.Exit(2)
}
C.runCPUHogThread()
// This goroutine may receive a profiling signal while creating the C-owned
// thread. If it does, the SetCgoTraceback handler will make the leaf end of
// the stack look almost (but not exactly) like the stacks the test case is
// trying to find. Attach a profiler label so the test can filter out those
// confusing samples.
pprof.Do(context.Background(), pprof.Labels("ignore", "ignore"), func(ctx context.Context) {
C.runCPUHogThread()
})
time.Sleep(1*time.Second)