diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2016-03-18 16:34:11 +0100 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2016-05-03 10:06:32 +0000 |
| commit | fcd7c02c70a110c6f6dbac30ad4ac3eb435ac3fd (patch) | |
| tree | ecf652d701125b971eb5e05edde7c1887d4d81bf /src/runtime/proc.go | |
| parent | 499cd3371997bdb6e33377266754d20782ef134d (diff) | |
| download | go-fcd7c02c70a110c6f6dbac30ad4ac3eb435ac3fd.tar.xz | |
runtime: fix CPU underutilization
Runqempty is a critical predicate for scheduler. If runqempty spuriously
returns true, then scheduler can fail to schedule arbitrary number of
runnable goroutines on idle Ps for arbitrary long time. With the addition
of runnext runqempty predicate become broken (can spuriously return true).
Consider that runnext is not nil and the main array is empty. Runqempty
observes that the array is empty, then it is descheduled for some time.
Then queue owner pushes another element to the queue evicting runnext
into the array. Then queue owner pops runnext. Then runqempty resumes
and observes runnext is nil and returns true. But there were no point
in time when the queue was empty.
Fix runqempty predicate to not return true spuriously.
Change-Id: Ifb7d75a699101f3ff753c4ce7c983cf08befd31e
Reviewed-on: https://go-review.googlesource.com/20858
Reviewed-by: Austin Clements <austin@google.com>
Run-TryBot: Dmitry Vyukov <dvyukov@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/runtime/proc.go')
| -rw-r--r-- | src/runtime/proc.go | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/src/runtime/proc.go b/src/runtime/proc.go index ee732e3cf7..e03059080d 100644 --- a/src/runtime/proc.go +++ b/src/runtime/proc.go @@ -3921,9 +3921,20 @@ func pidleget() *p { } // runqempty returns true if _p_ has no Gs on its local run queue. -// Note that this test is generally racy. +// It never returns true spuriously. func runqempty(_p_ *p) bool { - return _p_.runqhead == _p_.runqtail && _p_.runnext == 0 + // Defend against a race where 1) _p_ has G1 in runqnext but runqhead == runqtail, + // 2) runqput on _p_ kicks G1 to the runq, 3) runqget on _p_ empties runqnext. + // Simply observing that runqhead == runqtail and then observing that runqnext == nil + // does not mean the queue is empty. + for { + head := atomic.Load(&_p_.runqhead) + tail := atomic.Load(&_p_.runqtail) + runnext := atomic.Loaduintptr((*uintptr)(unsafe.Pointer(&_p_.runnext))) + if tail == atomic.Load(&_p_.runqtail) { + return head == tail && runnext == 0 + } + } } // To shake out latent assumptions about scheduling order, |
