Skip to content

Commit e46c8e0

Browse files
mknyszekgopherbot
authored andcommitted
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 <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Michael Knyszek <[email protected]>
1 parent b877f04 commit e46c8e0

24 files changed

+722
-215
lines changed

src/cmd/internal/objabi/funcid.go

+22-21
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,28 @@ import (
1010
)
1111

1212
var funcIDs = map[string]abi.FuncID{
13-
"abort": abi.FuncID_abort,
14-
"asmcgocall": abi.FuncID_asmcgocall,
15-
"asyncPreempt": abi.FuncID_asyncPreempt,
16-
"cgocallback": abi.FuncID_cgocallback,
17-
"corostart": abi.FuncID_corostart,
18-
"debugCallV2": abi.FuncID_debugCallV2,
19-
"gcBgMarkWorker": abi.FuncID_gcBgMarkWorker,
20-
"rt0_go": abi.FuncID_rt0_go,
21-
"goexit": abi.FuncID_goexit,
22-
"gogo": abi.FuncID_gogo,
23-
"gopanic": abi.FuncID_gopanic,
24-
"handleAsyncEvent": abi.FuncID_handleAsyncEvent,
25-
"main": abi.FuncID_runtime_main,
26-
"mcall": abi.FuncID_mcall,
27-
"morestack": abi.FuncID_morestack,
28-
"mstart": abi.FuncID_mstart,
29-
"panicwrap": abi.FuncID_panicwrap,
30-
"runFinalizersAndCleanups": abi.FuncID_runFinalizersAndCleanups,
31-
"sigpanic": abi.FuncID_sigpanic,
32-
"systemstack_switch": abi.FuncID_systemstack_switch,
33-
"systemstack": abi.FuncID_systemstack,
13+
"abort": abi.FuncID_abort,
14+
"asmcgocall": abi.FuncID_asmcgocall,
15+
"asyncPreempt": abi.FuncID_asyncPreempt,
16+
"cgocallback": abi.FuncID_cgocallback,
17+
"corostart": abi.FuncID_corostart,
18+
"debugCallV2": abi.FuncID_debugCallV2,
19+
"gcBgMarkWorker": abi.FuncID_gcBgMarkWorker,
20+
"rt0_go": abi.FuncID_rt0_go,
21+
"goexit": abi.FuncID_goexit,
22+
"gogo": abi.FuncID_gogo,
23+
"gopanic": abi.FuncID_gopanic,
24+
"handleAsyncEvent": abi.FuncID_handleAsyncEvent,
25+
"main": abi.FuncID_runtime_main,
26+
"mcall": abi.FuncID_mcall,
27+
"morestack": abi.FuncID_morestack,
28+
"mstart": abi.FuncID_mstart,
29+
"panicwrap": abi.FuncID_panicwrap,
30+
"runFinalizers": abi.FuncID_runFinalizers,
31+
"runCleanups": abi.FuncID_runCleanups,
32+
"sigpanic": abi.FuncID_sigpanic,
33+
"systemstack_switch": abi.FuncID_systemstack_switch,
34+
"systemstack": abi.FuncID_systemstack,
3435

3536
// Don't show in call stack but otherwise not special.
3637
"deferreturn": abi.FuncIDWrapper,

src/internal/abi/symtab.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ const (
5656
FuncID_mstart
5757
FuncID_panicwrap
5858
FuncID_rt0_go
59-
FuncID_runFinalizersAndCleanups
6059
FuncID_runtime_main
60+
FuncID_runFinalizers
61+
FuncID_runCleanups
6162
FuncID_sigpanic
6263
FuncID_systemstack
6364
FuncID_systemstack_switch

src/runtime/abi_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ func TestFinalizerRegisterABI(t *testing.T) {
6666
runtime.GC()
6767
runtime.GC()
6868

69+
// Make sure the finalizer goroutine is running.
70+
runtime.SetFinalizer(new(TintPointer), func(_ *TintPointer) {})
71+
6972
// fing will only pick the new IntRegArgs up if it's currently
7073
// sleeping and wakes up, so wait for it to go to sleep.
7174
success := false

src/runtime/crash_test.go

+75-69
Original file line numberDiff line numberDiff line change
@@ -1102,79 +1102,85 @@ func TestNetpollWaiters(t *testing.T) {
11021102
}
11031103
}
11041104

1105-
// The runtime.runFinalizersAndCleanups frame should appear in panics, even if
1106-
// runtime frames are normally hidden (GOTRACEBACK=all).
1107-
func TestFinalizerDeadlockPanic(t *testing.T) {
1105+
func TestFinalizerOrCleanupDeadlock(t *testing.T) {
11081106
t.Parallel()
1109-
output := runTestProg(t, "testprog", "FinalizerDeadlock", "GOTRACEBACK=all", "GO_TEST_FINALIZER_DEADLOCK=panic")
11101107

1111-
want := "runtime.runFinalizersAndCleanups()"
1112-
if !strings.Contains(output, want) {
1113-
t.Errorf("output does not contain %q:\n%s", want, output)
1114-
}
1115-
}
1116-
1117-
// The runtime.runFinalizersAndCleanups frame should appear in runtime.Stack,
1118-
// even though runtime frames are normally hidden.
1119-
func TestFinalizerDeadlockStack(t *testing.T) {
1120-
t.Parallel()
1121-
output := runTestProg(t, "testprog", "FinalizerDeadlock", "GO_TEST_FINALIZER_DEADLOCK=stack")
1122-
1123-
want := "runtime.runFinalizersAndCleanups()"
1124-
if !strings.Contains(output, want) {
1125-
t.Errorf("output does not contain %q:\n%s", want, output)
1126-
}
1127-
}
1128-
1129-
// The runtime.runFinalizersAndCleanups frame should appear in goroutine
1130-
// profiles.
1131-
func TestFinalizerDeadlockPprofProto(t *testing.T) {
1132-
t.Parallel()
1133-
output := runTestProg(t, "testprog", "FinalizerDeadlock", "GO_TEST_FINALIZER_DEADLOCK=pprof_proto")
1108+
for _, useCleanup := range []bool{false, true} {
1109+
progName := "Finalizer"
1110+
want := "runtime.runFinalizers"
1111+
if useCleanup {
1112+
progName = "Cleanup"
1113+
want = "runtime.runCleanups"
1114+
}
11341115

1135-
p, err := profile.Parse(strings.NewReader(output))
1136-
if err != nil {
1137-
// Logging the binary proto data is not very nice, but it might
1138-
// be a text error message instead.
1139-
t.Logf("Output: %s", output)
1140-
t.Fatalf("Error parsing proto output: %v", err)
1141-
}
1142-
1143-
want := "runtime.runFinalizersAndCleanups"
1144-
for _, s := range p.Sample {
1145-
for _, loc := range s.Location {
1146-
for _, line := range loc.Line {
1147-
if line.Function.Name == want {
1148-
// Done!
1149-
return
1116+
// The runtime.runFinalizers/runtime.runCleanups frame should appear in panics, even if
1117+
// runtime frames are normally hidden (GOTRACEBACK=all).
1118+
t.Run("Panic", func(t *testing.T) {
1119+
t.Parallel()
1120+
output := runTestProg(t, "testprog", progName+"Deadlock", "GOTRACEBACK=all", "GO_TEST_FINALIZER_DEADLOCK=panic")
1121+
want := want + "()"
1122+
if !strings.Contains(output, want) {
1123+
t.Errorf("output does not contain %q:\n%s", want, output)
1124+
}
1125+
})
1126+
1127+
// The runtime.runFinalizers/runtime.Cleanups frame should appear in runtime.Stack,
1128+
// even though runtime frames are normally hidden.
1129+
t.Run("Stack", func(t *testing.T) {
1130+
t.Parallel()
1131+
output := runTestProg(t, "testprog", progName+"Deadlock", "GO_TEST_FINALIZER_DEADLOCK=stack")
1132+
want := want + "()"
1133+
if !strings.Contains(output, want) {
1134+
t.Errorf("output does not contain %q:\n%s", want, output)
1135+
}
1136+
})
1137+
1138+
// The runtime.runFinalizers/runtime.Cleanups frame should appear in goroutine
1139+
// profiles.
1140+
t.Run("PprofProto", func(t *testing.T) {
1141+
t.Parallel()
1142+
output := runTestProg(t, "testprog", progName+"Deadlock", "GO_TEST_FINALIZER_DEADLOCK=pprof_proto")
1143+
1144+
p, err := profile.Parse(strings.NewReader(output))
1145+
if err != nil {
1146+
// Logging the binary proto data is not very nice, but it might
1147+
// be a text error message instead.
1148+
t.Logf("Output: %s", output)
1149+
t.Fatalf("Error parsing proto output: %v", err)
1150+
}
1151+
for _, s := range p.Sample {
1152+
for _, loc := range s.Location {
1153+
for _, line := range loc.Line {
1154+
if line.Function.Name == want {
1155+
// Done!
1156+
return
1157+
}
1158+
}
11501159
}
11511160
}
1152-
}
1153-
}
1154-
1155-
t.Errorf("Profile does not contain %q:\n%s", want, p)
1156-
}
1157-
1158-
// The runtime.runFinalizersAndCleanups frame should appear in goroutine
1159-
// profiles (debug=1).
1160-
func TestFinalizerDeadlockPprofDebug1(t *testing.T) {
1161-
t.Parallel()
1162-
output := runTestProg(t, "testprog", "FinalizerDeadlock", "GO_TEST_FINALIZER_DEADLOCK=pprof_debug1")
1163-
1164-
want := "runtime.runFinalizersAndCleanups+"
1165-
if !strings.Contains(output, want) {
1166-
t.Errorf("output does not contain %q:\n%s", want, output)
1167-
}
1168-
}
1169-
1170-
// The runtime.runFinalizersAndCleanups frame should appear in goroutine
1171-
// profiles (debug=2).
1172-
func TestFinalizerDeadlockPprofDebug2(t *testing.T) {
1173-
t.Parallel()
1174-
output := runTestProg(t, "testprog", "FinalizerDeadlock", "GO_TEST_FINALIZER_DEADLOCK=pprof_debug2")
1175-
1176-
want := "runtime.runFinalizersAndCleanups()"
1177-
if !strings.Contains(output, want) {
1178-
t.Errorf("output does not contain %q:\n%s", want, output)
1161+
t.Errorf("Profile does not contain %q:\n%s", want, p)
1162+
})
1163+
1164+
// The runtime.runFinalizers/runtime.runCleanups frame should appear in goroutine
1165+
// profiles (debug=1).
1166+
t.Run("PprofDebug1", func(t *testing.T) {
1167+
t.Parallel()
1168+
output := runTestProg(t, "testprog", progName+"Deadlock", "GO_TEST_FINALIZER_DEADLOCK=pprof_debug1")
1169+
want := want + "+"
1170+
if !strings.Contains(output, want) {
1171+
t.Errorf("output does not contain %q:\n%s", want, output)
1172+
}
1173+
})
1174+
1175+
// The runtime.runFinalizers/runtime.runCleanups frame should appear in goroutine
1176+
// profiles (debug=2).
1177+
t.Run("PprofDebug2", func(t *testing.T) {
1178+
t.Parallel()
1179+
output := runTestProg(t, "testprog", progName+"Deadlock", "GO_TEST_FINALIZER_DEADLOCK=pprof_debug2")
1180+
want := want + "()"
1181+
if !strings.Contains(output, want) {
1182+
t.Errorf("output does not contain %q:\n%s", want, output)
1183+
}
1184+
})
11791185
}
11801186
}

src/runtime/export_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -1798,6 +1798,10 @@ func BlockUntilEmptyFinalizerQueue(timeout int64) bool {
17981798
return blockUntilEmptyFinalizerQueue(timeout)
17991799
}
18001800

1801+
func BlockUntilEmptyCleanupQueue(timeout int64) bool {
1802+
return gcCleanups.blockUntilEmpty(timeout)
1803+
}
1804+
18011805
func FrameStartLine(f *Frame) int {
18021806
return f.startLine
18031807
}

0 commit comments

Comments
 (0)