aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/mgcwork.go
diff options
context:
space:
mode:
authorAustin Clements <austin@google.com>2016-10-30 20:43:53 -0400
committerAustin Clements <austin@google.com>2016-11-20 22:44:22 +0000
commit0bae74e8c9b5fab3baf61bde0169f4aa5e287bdc (patch)
tree362c7a5b8b11fd0069075dbda6400dd4706bf62e /src/runtime/mgcwork.go
parent49ea9207b6512c2400de11bc097d974bb527ba63 (diff)
downloadgo-0bae74e8c9b5fab3baf61bde0169f4aa5e287bdc.tar.gz
go-0bae74e8c9b5fab3baf61bde0169f4aa5e287bdc.zip
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>
Diffstat (limited to 'src/runtime/mgcwork.go')
-rw-r--r--src/runtime/mgcwork.go22
1 files changed, 16 insertions, 6 deletions
diff --git a/src/runtime/mgcwork.go b/src/runtime/mgcwork.go
index 699982e01d..5eb05a767c 100644
--- a/src/runtime/mgcwork.go
+++ b/src/runtime/mgcwork.go
@@ -99,6 +99,7 @@ func (w *gcWork) init() {
// obj must point to the beginning of a heap object or an oblet.
//go:nowritebarrier
func (w *gcWork) put(obj uintptr) {
+ flushed := false
wbuf := w.wbuf1.ptr()
if wbuf == nil {
w.init()
@@ -111,11 +112,20 @@ func (w *gcWork) put(obj uintptr) {
putfull(wbuf)
wbuf = getempty()
w.wbuf1 = wbufptrOf(wbuf)
+ flushed = true
}
}
wbuf.obj[wbuf.nobj] = obj
wbuf.nobj++
+
+ // If we put a buffer on full, let the GC controller know so
+ // it can encourage more workers to run. We delay this until
+ // the end of put so that w is in a consistent state, since
+ // enlistWorker may itself manipulate w.
+ if flushed && gcphase == _GCmark {
+ gcController.enlistWorker()
+ }
}
// putFast does a put and returns true if it can be done quickly
@@ -263,6 +273,12 @@ func (w *gcWork) balance() {
w.wbuf2 = wbufptrOf(getempty())
} else if wbuf := w.wbuf1.ptr(); wbuf.nobj > 4 {
w.wbuf1 = wbufptrOf(handoff(wbuf))
+ } else {
+ return
+ }
+ // We flushed a buffer to the full list, so wake a worker.
+ if gcphase == _GCmark {
+ gcController.enlistWorker()
}
}
@@ -337,12 +353,6 @@ func putempty(b *workbuf) {
func putfull(b *workbuf) {
b.checknonempty()
lfstackpush(&work.full, &b.node)
-
- // We just made more work available. Let the GC controller
- // know so it can encourage more workers to run.
- if gcphase == _GCmark {
- gcController.enlistWorker()
- }
}
// trygetfull tries to get a full or partially empty workbuffer.