]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: fix flake in TestCgoPprofThread
authorRhys Hiltner <rhys@justin.tv>
Tue, 7 Dec 2021 21:32:24 +0000 (13:32 -0800)
committerMichael Pratt <mpratt@google.com>
Wed, 8 Dec 2021 15:45:29 +0000 (15:45 +0000)
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>

src/runtime/crash_cgo_test.go
src/runtime/testdata/testprogcgo/threadpprof.go

index bfb260a143d480b64ccad66fd8f2e75e68953925..058eae1c09c8144f6d2245d3a06ff6fe17b78737 100644 (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)
index 4bc84d16d0c607fb6f18732584bf574581a9e3f1..e093f67e1e69d2bbe7c81b8d909e40c21cb3d77a 100644 (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)