diff options
author | Ian Lance Taylor <iant@golang.org> | 2019-11-01 12:38:10 -0700 |
---|---|---|
committer | Ian Lance Taylor <iant@golang.org> | 2019-11-04 21:37:08 +0000 |
commit | d80ab3e85a76581277f62761ae3c22817dab745d (patch) | |
tree | 3626681c30bfd668803b0fc2482235db831457e5 /src/runtime/time.go | |
parent | 5a7c571ea19a0d859222d6c92d108fdc68da9f13 (diff) | |
download | go-d80ab3e85a76581277f62761ae3c22817dab745d.tar.gz go-d80ab3e85a76581277f62761ae3c22817dab745d.zip |
runtime: wake netpoller when dropping P, don't sleep too long in sysmon
When dropping a P, if it has any timers, and if some thread is
sleeping in the netpoller, wake the netpoller to run the P's timers.
This mitigates races between the netpoller deciding how long to sleep
and a new timer being added.
In sysmon, if all P's are idle, check the timers to decide how long to sleep.
This avoids oversleeping if no thread is using the netpoller.
This can happen in particular if some threads use runtime.LockOSThread,
as those threads do not block in the netpoller.
Also, print the number of timers per P for GODEBUG=scheddetail=1.
Before this CL, TestLockedDeadlock2 would fail about 1% of the time.
With this CL, I ran it 150,000 times with no failures.
Updates #6239
Updates #27707
Fixes #35274
Fixes #35288
Change-Id: I7e5193e6c885e567f0b1ee023664aa3e2902fcd1
Reviewed-on: https://go-review.googlesource.com/c/go/+/204800
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Diffstat (limited to 'src/runtime/time.go')
-rw-r--r-- | src/runtime/time.go | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/runtime/time.go b/src/runtime/time.go index e6a24c5561..6ae5225c68 100644 --- a/src/runtime/time.go +++ b/src/runtime/time.go @@ -1024,6 +1024,27 @@ func addAdjustedTimers(pp *p, moved []*timer) { } } +// nobarrierWakeTime looks at P's timers and returns the time when we +// should wake up the netpoller. It returns 0 if there are no timers. +// This function is invoked when dropping a P, and must run without +// any write barriers. Therefore, if there are any timers that needs +// to be moved earlier, it conservatively returns the current time. +// The netpoller M will wake up and adjust timers before sleeping again. +//go:nowritebarrierrec +func nobarrierWakeTime(pp *p) int64 { + lock(&pp.timersLock) + ret := int64(0) + if len(pp.timers) > 0 { + if atomic.Load(&pp.adjustTimers) > 0 { + ret = nanotime() + } else { + ret = pp.timers[0].when + } + } + unlock(&pp.timersLock) + return ret +} + // runtimer examines the first timer in timers. If it is ready based on now, // it runs the timer and removes or updates it. // Returns 0 if it ran a timer, -1 if there are no more timers, or the time |