Skip to content

Commit 179d41f

Browse files
committed
runtime: tune P retake logic
When GOMAXPROCS>1 the last P in syscall is never retaken (because there are already idle P's -- npidle>0). This prevents sysmon thread from sleeping. On a darwin machine the program from issue 6673 constantly consumes ~0.2% CPU. With this change it stably consumes 0.0% CPU. Fixes #6673. R=golang-codereviews, r CC=bradfitz, golang-codereviews, iant, khr https://golang.org/cl/56990045
1 parent ea86752 commit 179d41f

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/pkg/runtime/proc.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2534,16 +2534,19 @@ retake(int64 now)
25342534
pd = &pdesc[i];
25352535
s = p->status;
25362536
if(s == Psyscall) {
2537-
// Retake P from syscall if it's there for more than 1 sysmon tick (20us).
2538-
// But only if there is other work to do.
2537+
// Retake P from syscall if it's there for more than 1 sysmon tick (at least 20us).
25392538
t = p->syscalltick;
25402539
if(pd->syscalltick != t) {
25412540
pd->syscalltick = t;
25422541
pd->syscallwhen = now;
25432542
continue;
25442543
}
2544+
// On the one hand we don't want to retake Ps if there is no other work to do,
2545+
// but on the other hand we want to retake them eventually
2546+
// because they can prevent the sysmon thread from deep sleep.
25452547
if(p->runqhead == p->runqtail &&
2546-
runtime·atomicload(&runtime·sched.nmspinning) + runtime·atomicload(&runtime·sched.npidle) > 0)
2548+
runtime·atomicload(&runtime·sched.nmspinning) + runtime·atomicload(&runtime·sched.npidle) > 0 &&
2549+
pd->syscallwhen + 10*1000*1000 > now)
25472550
continue;
25482551
// Need to decrement number of idle locked M's
25492552
// (pretending that one more is running) before the CAS.

0 commit comments

Comments
 (0)