Skip to content

Commit 9b90491

Browse files
runtime: steal timers from running P's
Previously we did not steal timers from running P's, because that P should be responsible for running its own timers. However, if the P is running a CPU-bound G, this can cause measurable delays in running ready timers. Also, in CL 214185 we avoided taking the timer lock of a P with no ready timers, which reduces the chances of timer lock contention. So, if we can't find any ready timers on sleeping P's, try stealing them from running P's. Fixes #38860 Change-Id: I0bf1d5dc56258838bdacccbf89493524e23d7fed Reviewed-on: https://go-review.googlesource.com/c/go/+/232199 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Austin Clements <[email protected]> Reviewed-by: Michael Knyszek <[email protected]>
1 parent 9bbe899 commit 9b90491

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/runtime/proc.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -2231,11 +2231,14 @@ top:
22312231
// Consider stealing timers from p2.
22322232
// This call to checkTimers is the only place where
22332233
// we hold a lock on a different P's timers.
2234-
// Lock contention can be a problem here, so avoid
2235-
// grabbing the lock if p2 is running and not marked
2236-
// for preemption. If p2 is running and not being
2237-
// preempted we assume it will handle its own timers.
2238-
if i > 2 && shouldStealTimers(p2) {
2234+
// Lock contention can be a problem here, so
2235+
// initially avoid grabbing the lock if p2 is running
2236+
// and is not marked for preemption. If p2 is running
2237+
// and not being preempted we assume it will handle its
2238+
// own timers.
2239+
// If we're still looking for work after checking all
2240+
// the P's, then go ahead and steal from an active P.
2241+
if i > 2 || (i > 1 && shouldStealTimers(p2)) {
22392242
tnow, w, ran := checkTimers(p2, now)
22402243
now = tnow
22412244
if w != 0 && (pollUntil == 0 || w < pollUntil) {

0 commit comments

Comments
 (0)