From: Rhys Hiltner Date: Thu, 16 Nov 2023 17:44:21 +0000 (-0800) Subject: runtime: disable automatic GC for STW metric tests X-Git-Tag: go1.22rc1~289 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=3e8000393e1b8e4cd0f8bb448bf4655920ff6fa4;p=gostls13.git runtime: disable automatic GC for STW metric tests A follow-up to https://go.dev/cl/534161 -- calls to runtime/trace.Start and Stop synchronize with the GC, waiting for any in-progress mark phase to complete. Disable automatic GCs to quiet the system, so we can observe only the test's intentional pauses. Change-Id: I6f8106c42528f9bda9afec1c151119783bbc78dc Reviewed-on: https://go-review.googlesource.com/c/go/+/543075 Run-TryBot: Rhys Hiltner TryBot-Result: Gopher Robot Reviewed-by: Michael Pratt Auto-Submit: Rhys Hiltner Reviewed-by: Bryan Mills LUCI-TryBot-Result: Go LUCI --- diff --git a/src/runtime/metrics_test.go b/src/runtime/metrics_test.go index d7f21f1825..1e82897381 100644 --- a/src/runtime/metrics_test.go +++ b/src/runtime/metrics_test.go @@ -780,8 +780,6 @@ func TestCPUMetricsSleep(t *testing.T) { // Call f() and verify that the correct STW metrics increment. If isGC is true, // fn triggers a GC STW. Otherwise, fn triggers an other STW. func testSchedPauseMetrics(t *testing.T, fn func(t *testing.T), isGC bool) { - t.Helper() - m := []metrics.Sample{ {Name: "/sched/pauses/stopping/gc:seconds"}, {Name: "/sched/pauses/stopping/other:seconds"}, @@ -848,7 +846,7 @@ func testSchedPauseMetrics(t *testing.T, fn func(t *testing.T), isGC bool) { } func TestSchedPauseMetrics(t *testing.T) { - tests := []struct{ + tests := []struct { name string isGC bool fn func(t *testing.T) @@ -856,13 +854,13 @@ func TestSchedPauseMetrics(t *testing.T) { { name: "runtime.GC", isGC: true, - fn: func(t *testing.T) { + fn: func(t *testing.T) { runtime.GC() }, }, { name: "runtime.GOMAXPROCS", - fn: func(t *testing.T) { + fn: func(t *testing.T) { if runtime.GOARCH == "wasm" { t.Skip("GOMAXPROCS >1 not supported on wasm") } @@ -870,33 +868,33 @@ func TestSchedPauseMetrics(t *testing.T) { n := runtime.GOMAXPROCS(0) defer runtime.GOMAXPROCS(n) - runtime.GOMAXPROCS(n+1) + runtime.GOMAXPROCS(n + 1) }, }, { name: "runtime.GoroutineProfile", - fn: func(t *testing.T) { + fn: func(t *testing.T) { var s [1]runtime.StackRecord runtime.GoroutineProfile(s[:]) }, }, { name: "runtime.ReadMemStats", - fn: func(t *testing.T) { + fn: func(t *testing.T) { var mstats runtime.MemStats runtime.ReadMemStats(&mstats) }, }, { name: "runtime.Stack", - fn: func(t *testing.T) { + fn: func(t *testing.T) { var b [64]byte runtime.Stack(b[:], true) }, }, { name: "runtime/debug.WriteHeapDump", - fn: func(t *testing.T) { + fn: func(t *testing.T) { if runtime.GOOS == "js" { t.Skip("WriteHeapDump not supported on js") } @@ -912,7 +910,7 @@ func TestSchedPauseMetrics(t *testing.T) { }, { name: "runtime/trace.Start", - fn: func(t *testing.T) { + fn: func(t *testing.T) { if trace.IsEnabled() { t.Skip("tracing already enabled") } @@ -926,6 +924,15 @@ func TestSchedPauseMetrics(t *testing.T) { }, } + // These tests count STW pauses, classified based on whether they're related + // to the GC or not. Disable automatic GC cycles during the test so we don't + // have an incidental GC pause when we're trying to observe only + // non-GC-related pauses. This is especially important for the + // runtime/trace.Start test, since (as of this writing) that will block + // until any active GC mark phase completes. + defer debug.SetGCPercent(debug.SetGCPercent(-1)) + runtime.GC() + for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { testSchedPauseMetrics(t, tc.fn, tc.isGC)