Skip to content

Commit 0bae74e

Browse files
committed
runtime: wake idle Ps when enqueuing GC work
If the scheduler has no user work and there's no GC work visible, it puts the P to sleep (or blocks on the network). However, if we later enqueue more GC work, there's currently nothing that specifically wakes up the scheduler to let it start an idle GC worker. As a result, we can underutilize the CPU during GC if Ps have been put to sleep. Fix this by making GC wake idle Ps when work buffers are put on the full list. We already have a hook to do this, since we use this to preempt a random P if we need more dedicated workers. We expand this hook to instead wake an idle P if there is one. The logic we use for this is identical to the logic used to wake an idle P when we ready a goroutine. To make this really sound, we also fix the scheduler to re-check the idle GC worker condition after releasing its P. This closes a race where 1) the scheduler checks for idle work and finds none, 2) new work is enqueued but there are no idle Ps so none are woken, and 3) the scheduler releases its P. There is one subtlety here. Currently we call enlistWorker directly from putfull, but the gcWork is in an inconsistent state in the places that call putfull. This isn't a problem right now because nothing that enlistWorker does touches the gcWork, but with the added call to wakep, it's possible to get a recursive call into the gcWork (specifically, while write barriers are disallowed, this can do an allocation, which can dispose a gcWork, which can put a workbuf). To handle this, we lift the enlistWorker calls up a layer and delay them until the gcWork is in a consistent state. Fixes #14179. Change-Id: Ia2467a52e54c9688c3c1752e1fc00f5b37bbfeeb Reviewed-on: https://go-review.googlesource.com/32434 Run-TryBot: Austin Clements <austin@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Dmitry Vyukov <dvyukov@google.com>
1 parent 49ea920 commit 0bae74e

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

src/runtime/mgc.go

+8
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,14 @@ func (c *gcControllerState) endCycle() {
624624
//
625625
//go:nowritebarrier
626626
func (c *gcControllerState) enlistWorker() {
627+
// If there are idle Ps, wake one so it will run an idle worker.
628+
if atomic.Load(&sched.npidle) != 0 && atomic.Load(&sched.nmspinning) == 0 {
629+
wakep()
630+
return
631+
}
632+
633+
// There are no idle Ps. If we need more dedicated workers,
634+
// try to preempt a running P so it will switch to a worker.
627635
if c.dedicatedMarkWorkersNeeded <= 0 {
628636
return
629637
}

src/runtime/mgcwork.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ func (w *gcWork) init() {
9999
// obj must point to the beginning of a heap object or an oblet.
100100
//go:nowritebarrier
101101
func (w *gcWork) put(obj uintptr) {
102+
flushed := false
102103
wbuf := w.wbuf1.ptr()
103104
if wbuf == nil {
104105
w.init()
@@ -111,11 +112,20 @@ func (w *gcWork) put(obj uintptr) {
111112
putfull(wbuf)
112113
wbuf = getempty()
113114
w.wbuf1 = wbufptrOf(wbuf)
115+
flushed = true
114116
}
115117
}
116118

117119
wbuf.obj[wbuf.nobj] = obj
118120
wbuf.nobj++
121+
122+
// If we put a buffer on full, let the GC controller know so
123+
// it can encourage more workers to run. We delay this until
124+
// the end of put so that w is in a consistent state, since
125+
// enlistWorker may itself manipulate w.
126+
if flushed && gcphase == _GCmark {
127+
gcController.enlistWorker()
128+
}
119129
}
120130

121131
// putFast does a put and returns true if it can be done quickly
@@ -263,6 +273,12 @@ func (w *gcWork) balance() {
263273
w.wbuf2 = wbufptrOf(getempty())
264274
} else if wbuf := w.wbuf1.ptr(); wbuf.nobj > 4 {
265275
w.wbuf1 = wbufptrOf(handoff(wbuf))
276+
} else {
277+
return
278+
}
279+
// We flushed a buffer to the full list, so wake a worker.
280+
if gcphase == _GCmark {
281+
gcController.enlistWorker()
266282
}
267283
}
268284

@@ -337,12 +353,6 @@ func putempty(b *workbuf) {
337353
func putfull(b *workbuf) {
338354
b.checknonempty()
339355
lfstackpush(&work.full, &b.node)
340-
341-
// We just made more work available. Let the GC controller
342-
// know so it can encourage more workers to run.
343-
if gcphase == _GCmark {
344-
gcController.enlistWorker()
345-
}
346356
}
347357

348358
// trygetfull tries to get a full or partially empty workbuffer.

src/runtime/proc.go

+20
Original file line numberDiff line numberDiff line change
@@ -2023,6 +2023,26 @@ stop:
20232023
}
20242024
}
20252025

2026+
// Check for idle-priority GC work again.
2027+
if gcBlackenEnabled != 0 && gcMarkWorkAvailable(nil) {
2028+
lock(&sched.lock)
2029+
_p_ = pidleget()
2030+
if _p_ != nil && _p_.gcBgMarkWorker == 0 {
2031+
pidleput(_p_)
2032+
_p_ = nil
2033+
}
2034+
unlock(&sched.lock)
2035+
if _p_ != nil {
2036+
acquirep(_p_)
2037+
if wasSpinning {
2038+
_g_.m.spinning = true
2039+
atomic.Xadd(&sched.nmspinning, 1)
2040+
}
2041+
// Go back to idle GC check.
2042+
goto stop
2043+
}
2044+
}
2045+
20262046
// poll network
20272047
if netpollinited() && atomic.Xchg64(&sched.lastpoll, 0) != 0 {
20282048
if _g_.m.p != 0 {

0 commit comments

Comments
 (0)