diff options
| author | Michael Anthony Knyszek <mknyszek@google.com> | 2025-02-19 16:33:21 +0000 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2025-05-08 11:10:33 -0700 |
| commit | e46c8e0558d287fcffde75bb458419288e71db62 (patch) | |
| tree | 65e50347788fcc8d21bfdcf6663f6b8d030e5c22 /src/runtime/mfinal.go | |
| parent | b877f04eea44820481e3a33f93eb55e90ff1754d (diff) | |
| download | go-e46c8e0558d287fcffde75bb458419288e71db62.tar.xz | |
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 <mpratt@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Michael Knyszek <mknyszek@google.com>
Diffstat (limited to 'src/runtime/mfinal.go')
| -rw-r--r-- | src/runtime/mfinal.go | 26 |
1 files changed, 4 insertions, 22 deletions
diff --git a/src/runtime/mfinal.go b/src/runtime/mfinal.go index 9add92557c..4a0e110373 100644 --- a/src/runtime/mfinal.go +++ b/src/runtime/mfinal.go @@ -17,7 +17,7 @@ import ( const finBlockSize = 4 * 1024 -// finBlock is an block of finalizers/cleanups to be executed. finBlocks +// finBlock is an block of finalizers to be executed. finBlocks // are arranged in a linked list for the finalizer queue. // // finBlock is allocated from non-GC'd memory, so any heap pointers @@ -165,7 +165,7 @@ func wakefing() *g { func createfing() { // start the finalizer goroutine exactly once if fingStatus.Load() == fingUninitialized && fingStatus.CompareAndSwap(fingUninitialized, fingCreated) { - go runFinalizersAndCleanups() + go runFinalizers() } } @@ -177,8 +177,8 @@ func finalizercommit(gp *g, lock unsafe.Pointer) bool { return true } -// This is the goroutine that runs all of the finalizers and cleanups. -func runFinalizersAndCleanups() { +// This is the goroutine that runs all of the finalizers. +func runFinalizers() { var ( frame unsafe.Pointer framecap uintptr @@ -207,22 +207,6 @@ func runFinalizersAndCleanups() { for i := fb.cnt; i > 0; i-- { f := &fb.fin[i-1] - // arg will only be nil when a cleanup has been queued. - if f.arg == nil { - var cleanup func() - fn := unsafe.Pointer(f.fn) - cleanup = *(*func())(unsafe.Pointer(&fn)) - fingStatus.Or(fingRunningFinalizer) - cleanup() - fingStatus.And(^fingRunningFinalizer) - - f.fn = nil - f.arg = nil - f.ot = nil - atomic.Store(&fb.cnt, i-1) - continue - } - var regs abi.RegArgs // The args may be passed in registers or on stack. Even for // the register case, we still need the spill slots. @@ -241,8 +225,6 @@ func runFinalizersAndCleanups() { frame = mallocgc(framesz, nil, true) framecap = framesz } - // cleanups also have a nil fint. Cleanups should have been processed before - // reaching this point. if f.fint == nil { throw("missing type in finalizer") } |
