From e46c8e0558d287fcffde75bb458419288e71db62 Mon Sep 17 00:00:00 2001 From: Michael Anthony Knyszek Date: Wed, 19 Feb 2025 16:33:21 +0000 Subject: runtime: schedule cleanups across multiple goroutines This change splits the finalizer and cleanup queues and implements a new lock-free blocking queue for cleanups. The basic design is as follows: The cleanup queue is organized in fixed-sized blocks. Individual cleanup functions are queued, but only whole blocks are dequeued. Enqueuing cleanups places them in P-local cleanup blocks. These are flushed to the full list as they get full. Cleanups can only be enqueued by an active sweeper. Dequeuing cleanups always dequeues entire blocks from the full list. Cleanup blocks can be dequeued and executed at any time. The very last active sweeper in the sweep phase is responsible for flushing all local cleanup blocks to the full list. It can do this without any synchronization because the next GC can't start yet, so we can be very certain that nobody else will be accessing the local blocks. Cleanup blocks are stored off-heap because the need to be allocated by the sweeper, which is called from heap allocation paths. As a result, the GC treats cleanup blocks as roots, just like finalizer blocks. Flushes to the full list signal to the scheduler that cleanup goroutines should be awoken. Every time the scheduler goes to wake up a cleanup goroutine and there were more signals than goroutines to wake, it then forwards this signal to runtime.AddCleanup, so that it creates another goroutine the next time it is called, up to gomaxprocs goroutines. The signals here are a little convoluted, but exist because the sweeper and the scheduler cannot safely create new goroutines. For #71772. For #71825. Change-Id: Ie839fde2b67e1b79ac1426be0ea29a8d923a62cc Reviewed-on: https://go-review.googlesource.com/c/go/+/650697 Reviewed-by: Michael Pratt LUCI-TryBot-Result: Go LUCI Auto-Submit: Michael Knyszek --- src/runtime/pprof/pprof_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src/runtime/pprof') diff --git a/src/runtime/pprof/pprof_test.go b/src/runtime/pprof/pprof_test.go index 5477d9ed26..01d3b0aa4b 100644 --- a/src/runtime/pprof/pprof_test.go +++ b/src/runtime/pprof/pprof_test.go @@ -1577,8 +1577,8 @@ func TestGoroutineProfileConcurrency(t *testing.T) { return strings.Count(s, "\truntime/pprof.runtime_goroutineProfileWithLabels+") } - includesFinalizer := func(s string) bool { - return strings.Contains(s, "runtime.runFinalizersAndCleanups") + includesFinalizerOrCleanup := func(s string) bool { + return strings.Contains(s, "runtime.runFinalizers") || strings.Contains(s, "runtime.runCleanups") } // Concurrent calls to the goroutine profiler should not trigger data races @@ -1616,8 +1616,8 @@ func TestGoroutineProfileConcurrency(t *testing.T) { var w strings.Builder goroutineProf.WriteTo(&w, 1) prof := w.String() - if includesFinalizer(prof) { - t.Errorf("profile includes finalizer (but finalizer should be marked as system):\n%s", prof) + if includesFinalizerOrCleanup(prof) { + t.Errorf("profile includes finalizer or cleanup (but should be marked as system):\n%s", prof) } }) @@ -1648,7 +1648,7 @@ func TestGoroutineProfileConcurrency(t *testing.T) { var w strings.Builder goroutineProf.WriteTo(&w, 1) prof := w.String() - if !includesFinalizer(prof) { + if !includesFinalizerOrCleanup(prof) { t.Errorf("profile does not include finalizer (and it should be marked as user):\n%s", prof) } }) @@ -2065,7 +2065,7 @@ func TestLabelSystemstack(t *testing.T) { // which part of the function they are // at. mayBeLabeled = true - case "runtime.bgsweep", "runtime.bgscavenge", "runtime.forcegchelper", "runtime.gcBgMarkWorker", "runtime.runFinalizersAndCleanups", "runtime.sysmon": + case "runtime.bgsweep", "runtime.bgscavenge", "runtime.forcegchelper", "runtime.gcBgMarkWorker", "runtime.runFinalizers", "runtime.runCleanups", "runtime.sysmon": // Runtime system goroutines or threads // (such as those identified by // runtime.isSystemGoroutine). These -- cgit v1.3