1
0
mirror of https://github.com/golang/go synced 2024-11-23 01:40:03 -07:00

runtime: check global runq during "delicate dance"

When a thread transitions to spinning to non-spinning it must recheck
all sources of work because other threads may submit new work but skip
wakep because they see a spinning thread.

However, since the beginning of time (CL 7314062) we do not check the
global run queue, only the local per-P run queues.

The global run queue is checked just above the spinning checks while
dropping the P. I am unsure what the purpose of this check is. It
appears to simply be opportunistic since sched.lock is already held
there in order to drop the P. It is not sufficient to synchronize with
threads adding work because it occurs before decrementing
sched.nmspinning, which is what threads us to decide to wake a thread.

Resolve this by adding an explicit global run queue check alongside the
local per-P run queue checks.

Almost nothing happens between dropped sched.lock after dropping the P
and relocking sched.lock: just clearing mp.spinning and decrementing
sched.nmspinning. Thus it may be better to just hold sched.lock for this
entire period, but this is a larger change that I would prefer to avoid
in the freeze and backports.

For #55160.

Change-Id: Ifd88b5a4c561c063cedcfcfe1dd8ae04202d9666
Reviewed-on: https://go-review.googlesource.com/c/go/+/501975
Run-TryBot: Michael Pratt <mpratt@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Auto-Submit: Michael Pratt <mpratt@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Michael Pratt 2023-06-07 11:38:26 -04:00 committed by Gopher Robot
parent 884aa71f51
commit dd5db4df56

View File

@ -84,7 +84,7 @@ var modinfo string
// semi-persistent CPU underutilization.
//
// The general pattern for submission is:
// 1. Submit work to the local run queue, timer heap, or GC state.
// 1. Submit work to the local or global run queue, timer heap, or GC state.
// 2. #StoreLoad-style memory barrier.
// 3. Check sched.nmspinning.
//
@ -3093,7 +3093,7 @@ top:
//
// This applies to the following sources of work:
//
// * Goroutines added to a per-P run queue.
// * Goroutines added to the global or a per-P run queue.
// * New/modified-earlier timers on a per-P timer heap.
// * Idle-priority GC work (barring golang.org/issue/19112).
//
@ -3135,7 +3135,24 @@ top:
//
// See https://go.dev/issue/43997.
// Check all runqueues once again.
// Check global and P runqueues again.
lock(&sched.lock)
if sched.runqsize != 0 {
pp, _ := pidlegetSpinning(0)
if pp != nil {
gp := globrunqget(pp, 0)
if gp == nil {
throw("global runq empty with non-zero runqsize")
}
unlock(&sched.lock)
acquirep(pp)
mp.becomeSpinning()
return gp, false, false
}
}
unlock(&sched.lock)
pp := checkRunqsNoP(allpSnapshot, idlepMaskSnapshot)
if pp != nil {
acquirep(pp)