aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/time.go
AgeCommit message (Collapse)Author
2022-07-25[release-branch.go1.17] runtime: clear timerModifiedEarliest when last timer ↵Michael Pratt
is deleted timerModifiedEarliest contains the lowest possible expiration for a modified earlier timer, which may be earlier than timer0When because we haven't yet updated the heap. Note "may", as the modified earlier timer that set timerModifiedEarliest may have since been modified later or deleted. We can clear timerModifiedEarliest when the last timer is deleted because by definition there must not be any modified earlier timers. Why does this matter? checkTimersNoP claims that there is work to do if timerModifiedEarliest has passed, causing findRunnable to loop back around to checkTimers. But the code to clean up timerModifiedEarliest in checkTimers (i.e., the call to adjusttimers) is conditional behind a check that len(pp.timers) > 0. Without clearing timerModifiedEarliest, a spinning M that would otherwise go to sleep will busy loop in findRunnable until some other work is available. Note that changing the condition on the call to adjusttimers would also be a valid fix. I took this approach because it feels a bit cleaner to clean up timerModifiedEarliest as soon as it is known to be irrelevant. For #51654. Fixes #53846. Change-Id: I3f3787c67781cac7ce87939c5706cef8db927dd5 Reviewed-on: https://go-review.googlesource.com/c/go/+/417434 Auto-Submit: Michael Pratt <mpratt@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Michael Pratt <mpratt@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> (cherry picked from commit c006b7ac2765252f397dec40fef610a3c17d956d) Reviewed-on: https://go-review.googlesource.com/c/go/+/417476
2021-09-15[release-branch.go1.17] runtime: in adjustTimers back up as far as necessaryIan Lance Taylor
When the adjustTimers function removed a timer it assumed it was sufficient to continue the heap traversal at that position. However, in some cases a timer will be moved to an earlier position in the heap. If that timer is timerModifiedEarlier, that can leave timerModifiedEarliest not correctly representing the earlier such timer. Fix the problem by restarting the heap traversal at the earliest changed position. For #47762 Fixes #47859 Change-Id: I152bbe62793ee40a680baf49967bcb89b1f94764 Reviewed-on: https://go-review.googlesource.com/c/go/+/343882 Trust: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com> (cherry picked from commit 2da3375e9b4980e368a8641f54cc53c4af4d1a12) Reviewed-on: https://go-review.googlesource.com/c/go/+/350001
2021-07-26runtime: remove adjustTimers counterIan Lance Taylor
In CL 336432 we changed adjusttimers so that it no longer cleared timerModifiedEarliest if there were no timersModifiedEarlier timers. This caused some Google internal tests to time out, presumably due to the increased contention on timersLock. We can avoid that by simply not skipping the loop in adjusttimers, which lets us safely clear timerModifiedEarliest. And if we don't skip the loop, then there isn't much reason to keep the count of timerModifiedEarlier timers at all. So remove it. The effect will be that for programs that create some timerModifiedEarlier timers and then remove them all, the program will do an occasional additional loop over all the timers. And, programs that have some timerModifiedEarlier timers will always loop over all the timers, without the quicker exit when they have all been seen. But the loops should not occur all that often, due to timerModifiedEarliest. For #47329 Change-Id: I7b244c1244d97b169a3c7fbc8f8a8b115731ddee Reviewed-on: https://go-review.googlesource.com/c/go/+/337309 Trust: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Michael Pratt <mpratt@google.com>
2021-07-22runtime: don't clear timerModifiedEarliest if adjustTimers is 0Ian Lance Taylor
This avoids a race when a new timerModifiedEarlier timer is created by a different goroutine. Fixes #47329 Change-Id: I6f6c87b4a9b5491b201c725c10bc98e23e0ed9d1 Reviewed-on: https://go-review.googlesource.com/c/go/+/336432 Trust: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2021-03-10runtime, time: disable preemption in addtimerMichael Pratt
The timerpMask optimization updates a mask of Ps (potentially) containing timers in pidleget / pidleput. For correctness, it depends on the assumption that new timers can only be added to a P's own heap. addtimer violates this assumption if it is preempted after computing pp. That G may then run on a different P, but adding a timer to the original P's heap. Avoid this by disabling preemption while pp is in use. Other uses of doaddtimer should be OK: * moveTimers: always moves to the current P's heap * modtimer, cleantimers, addAdjustedTimers, runtimer: does not add net new timers to the heap while locked Fixes #44868 Change-Id: I4a5d080865e854931d0a3a09a51ca36879101d72 Reviewed-on: https://go-review.googlesource.com/c/go/+/300610 Trust: Michael Pratt <mpratt@google.com> Run-TryBot: Michael Pratt <mpratt@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org>
2021-01-20runtime: don't adjust timer pp field in timerWaiting statusIan Lance Taylor
Before this CL, the following sequence was possible: * GC scavenger starts and sets up scavenge.timer * GC calls readyForScavenger, but sysmon is sleeping * program calls runtime.GOMAXPROCS to shrink number of processors * procresize destroys a P, the one that scavenge.timer is on * (*pp).destroy calls moveTimers, which gets to the scavenger timer * scavenger timer is timerWaiting, and moveTimers clears t.pp * sysmon wakes up and calls wakeScavenger * wakeScavengers calls stopTimer on scavenger.timer, still timerWaiting * stopTimer calls deltimer which loads t.pp, which is still nil * stopTimer tries to increment deletedTimers on nil t.pp, and crashes The point of vulnerability is the time that t.pp is set to nil by moveTimers and the time that t.pp is set to non-nil by moveTimers, which is a few instructions at most. So it's not likely and in particular is quite unlikely on x86. But with a more relaxed memory model the area of vulnerability can be somewhat larger. This appears to tbe the cause of two builder failures in a few months on linux-mips. This CL fixes the problem by making moveTimers change the status from timerWaiting to timerMoving while t.pp is clear. That will cause deltimer to wait until the status is back to timerWaiting, at which point t.pp has been set again. Fixes #43712 Change-Id: I66838319ecfbf15be66c1fac88d9bd40e2295852 Reviewed-on: https://go-review.googlesource.com/c/go/+/284775 Trust: Ian Lance Taylor <iant@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Michael Pratt <mpratt@google.com>
2020-12-03runtime, time: strictly enforce when, period constraintsMichael Pratt
timer.when must always be positive. addtimer and modtimer already check that it is non-negative; we expand it to include zero. Also upgrade from pinning bad values to throwing, as these values shouldn't be possible to pass (except as below). timeSleep may overflow timer.nextwhen. This would previously have been pinned by resetForSleep, now we fix it manually. runOneTimer may overflow timer.when when adding timer.period. Detect this and pin to maxWhen. addtimer is now too strict to allow TestOverflowRuntimeTimer to test an overflowed timer. Such a timer should not be possible; to help guard against accidental inclusion siftup / siftdown will check timers as it goes. This has been replaced with tests for period and sleep overflows. Change-Id: I17f9739e27ebcb20d87945c635050316fb8e9226 Reviewed-on: https://go-review.googlesource.com/c/go/+/274853 Trust: Michael Pratt <mpratt@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-12-03time, runtime: don't set timer when = 0Michael Pratt
timer when == 0, in the context of timer0When and timerModifiedEarliest, is a sentinel value meaning there are no timers on the heap. TestCheckRuntimeTimerOverflow reaching into the runtime to set a timer to when = 0 when it is otherwise not possible breaks this invariant. After golang.org/cl/258303, we will no longer detect and run this timer, thus blocking any other timers lower on the heap from running. This manifests as random timers failing to fire in other tests. The need to set this overflowed timer to when = 0 is gone with the old timer proc implementation, so we can simply remove it. Fixes #42424 Change-Id: Iea32100136ad8ec1bedfa77b1e7d9ed868812838 Reviewed-on: https://go-review.googlesource.com/c/go/+/274632 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Michael Pratt <mpratt@google.com> TryBot-Result: Go Bot <gobot@golang.org> Trust: Michael Pratt <mpratt@google.com>
2020-10-30runtime: simplify nobarrierWakeTimeIan Lance Taylor
Also use the simplified nobarrierWakeTime in findrunnable, as it no longer needs the current time. Change-Id: I77b125d6a184dde0aeb517fc068164c274f0a046 Reviewed-on: https://go-review.googlesource.com/c/go/+/266304 Trust: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Michael Pratt <mpratt@google.com>
2020-10-27runtime: don't always adjust timersIan Lance Taylor
Some programs have a lot of timers that they adjust both forward and backward in time. This can cause a large number of timerModifiedEarlier timers. In practice these timers are used for I/O deadlines and are rarely reached. The effect is that the runtime spends a lot of time in adjusttimers making sure that there are no timerModifiedEarlier timers, but the effort is wasted because none of the adjusted timers are near the top of the timer heap anyhow. Avoid much of this extra work by keeping track of the earliest known timerModifiedEarlier timer. This lets us skip adjusttimers if we know that none of the timers will be ready to run anyhow. We will still eventually run it, when we reach the deadline of the earliest known timerModifiedEarlier, although in practice that timer has likely been removed. When we do run adjusttimers, we will reset all of the timerModifiedEarlier timers, and clear our notion of when we need to run adjusttimers again. This effect should be to significantly reduce the number of times we walk through the timer list in adjusttimers. Fixes #41699 Change-Id: I38eb2be611fb34e3017bb33d0a9ed40d75fb414f Reviewed-on: https://go-review.googlesource.com/c/go/+/258303 Trust: Ian Lance Taylor <iant@golang.org> Trust: Emmanuel Odeke <emmanuel@orijtech.com> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2020-08-17runtime: update comment: modTimer is called by Timer.Resetzero.xu
Change-Id: I97d0d1343d41b603a68388e496411fb040dc6d66 GitHub-Last-Rev: d11177ad249bd844dd9e7e355eea28596d0b1fa8 GitHub-Pull-Request: golang/go#38625 Reviewed-on: https://go-review.googlesource.com/c/go/+/229767 Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
2020-04-06time, runtime: only call resetTimer from (*Timer).ResetIan Lance Taylor
Previously we stopped the timer and then reset it. With the current timer implementation that is no longer required. Change-Id: Ie7aba61ad53ce835f6fcd0b6bce7fe0a15b10e24 Reviewed-on: https://go-review.googlesource.com/c/go/+/227180 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2020-03-25runtime: prevent preemption while timer is in timerModifyingMichael Anthony Knyszek
Currently if a goroutine is preempted while owning a timer in the timerModifying state, it could self-deadlock. When the goroutine is preempted and calls into the scheduler, it could call checkTimers. If checkTimers encounters the timerModifying timer and calls runtimer on it, then runtimer will spin, waiting for that timer to leave the timerModifying state, which it never will. So far we got lucky that for the most part that there were no preemption points while timerModifying is happening, however CL 221077 seems to have introduced one, leading to sporadic self-deadlocks. This change disables preemption explicitly while a goroutines holds a timer in timerModifying. Since only checkTimers (and thus runtimer) is called from the scheduler, this is sufficient to prevent preemption-based self-deadlocks. Fixes #38070. Updates #37894. Change-Id: Idbfac310889c92773023733ff7e2ff87e9896f0c Reviewed-on: https://go-review.googlesource.com/c/go/+/225497 Run-TryBot: Michael Knyszek <mknyszek@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-03-24runtime: don't call wakeNetPoller during timerModifyingIan Lance Taylor
Reduce the length of time that other timer functions can see timerModifying. In particular avoid system calls. Fixes #38023 Change-Id: I1b61229c668e6085d9ee6dca9488a90055386c36 Reviewed-on: https://go-review.googlesource.com/c/go/+/224902 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2020-03-12runtime: leave cleantimers early if G is being preemptedIan Lance Taylor
The cleantimers can run for a while in some unlikely cases. If the GC is trying to preempt the G, it is forced to wait as the G is holding timersLock. To avoid introducing a GC delay, return from cleantimers if the G has a preemption request. Fixes #37779 Change-Id: Id9a567f991e26668e2292eefc39e2edc56efa4e0 Reviewed-on: https://go-review.googlesource.com/c/go/+/223122 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Cherry Zhang <cherryyz@google.com>
2020-02-27runtime: don't panic on racy use of timersIan Lance Taylor
If we see a racy use of timers, as in concurrent calls to Timer.Reset, do the operations in an unpredictable order, rather than crashing. Fixes #37400 Change-Id: Idbac295df2dfd551b6d762909d5040fc532c1b34 Reviewed-on: https://go-review.googlesource.com/c/go/+/221077 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2020-02-24time: add Ticker.ResetChangkun Ou
This CL implements Ticker.Reset method in time package. Benchmark: name time/op TickerReset-12 6.41µs ±10% TickerResetNaive-12 95.7µs ±12% Fixes #33184 Change-Id: I4cbd31796efa012b2a297bb342158f11a4a31fef Reviewed-on: https://go-review.googlesource.com/c/go/+/220424 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-02-23Revert "time: add Ticker.Reset"Andrew Bonventre
This reverts CL 217362 (6e5652bebede2d53484a872f6d1dfeb498b0b50c.) Reason for revert: Causing failures on arm64 bots. See #33184 for more info Change-Id: I72ba40047e4138767d95aaa68842893c3508c52f Reviewed-on: https://go-review.googlesource.com/c/go/+/220638 Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-02-23time: add Ticker.ResetChangkun Ou
This CL implements Ticker.Reset method in time package. Benchmark: name time/op TickerReset-12 6.41µs ±10% TickerResetNaive-12 95.7µs ±12% Fixes #33184 Change-Id: I12c651f81e452541bcbbc748b45f038aae1f8dae Reviewed-on: https://go-review.googlesource.com/c/go/+/217362 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-01-24runtime: update deltimer commentsDraven
Change-Id: I5f4c21bf650b9825ebd98330ac9faa7371a562be GitHub-Last-Rev: 4a2e9aabe91935001300619b6f58c67f05e9f3c7 GitHub-Pull-Request: golang/go#36728 Reviewed-on: https://go-review.googlesource.com/c/go/+/216223 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-01-22runtime: don't skip checkTimers if we would clear deleted timersIan Lance Taylor
The timers code used to have a problem: if code started and stopped a lot of timers, as would happen with, for example, lots of calls to context.WithTimeout, then it would steadily use memory holding timers that had stopped but not been removed from the timer heap. That problem was fixed by CL 214299, which would remove all deleted timers whenever they got to be more than 1/4 of the total number of timers on the heap. The timers code had a different problem: if there were some idle P's, the running P's would have lock contention trying to steal their timers. That problem was fixed by CL 214185, which only acquired the timer lock if the next timer was ready to run or there were some timers to adjust. Unfortunately, CL 214185 partially undid 214299, in that we could now accumulate an increasing number of deleted timers while there were no timers ready to run. This CL restores the 214299 behavior, by checking whether there are lots of deleted timers without acquiring the lock. This is a performance issue to consider for the 1.14 release. Change-Id: I13c980efdcc2a46eb84882750c39e3f7c5b2e7c3 Reviewed-on: https://go-review.googlesource.com/c/go/+/215722 Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Austin Clements <austin@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
2020-01-14runtime: keep P's first timer when in new atomically accessed fieldIan Lance Taylor
This reduces lock contention when only a few P's are running and checking for whether they need to run timers on the sleeping P's. Without this change the running P's would get lock contention while looking at the sleeping P's timers. With this change a single atomic load suffices to determine whether there are any ready timers. Change-Id: Ie843782bd56df49867a01ecf19c47498ec827452 Reviewed-on: https://go-review.googlesource.com/c/go/+/214185 Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: David Chase <drchase@google.com>
2020-01-10runtime: don't let P's timer heap get clogged with deleted timersIan Lance Taylor
Whenever more than 1/4 of the timers on a P's heap are deleted, remove them from the heap. Change-Id: Iff63ed3d04e6f33ffc5c834f77f645c52c007e52 Reviewed-on: https://go-review.googlesource.com/c/go/+/214299 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2020-01-10runtime: don't skip timer when adjustTimers sees a modified timerIan Lance Taylor
When adjustTimers sees a timerModifiedEarlier or timerModifiedLater, it removes it from the heap, leaving a new timer at that position in the heap. We were accidentally skipping that new timer in our loop. In some unlikely cases this could cause adjustTimers to look at more timers than necessary. Change-Id: Ic71e54c175ab7d86a7fa46f1497aca71ed1c43cc Reviewed-on: https://go-review.googlesource.com/c/go/+/214338 Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-12-02runtime: use current P's race context in timer codeIan Lance Taylor
We were using the race context of the P that held the timer, but since we unlock the P's timers while executing a timer that could lead to a race on the race context itself. Updates #6239 Updates #27707 Fixes #35906 Change-Id: I5f9d5f52d8e28dffb88c3327301071b16ed1a913 Reviewed-on: https://go-review.googlesource.com/c/go/+/209580 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2019-11-19runtime, time: remove old timer codeIan Lance Taylor
Updates #6239 Updates #27707 Change-Id: I65e6471829c9de4677d3ac78ef6cd7aa0a1fc4cb Reviewed-on: https://go-review.googlesource.com/c/go/+/171884 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
2019-11-19runtime: release timersLock while running timerIan Lance Taylor
Dan Scales pointed out a theoretical deadlock in the runtime. The timer code runs timer functions while holding the timers lock for a P. The scavenger queues up a timer function that calls wakeScavenger, which acquires the scavenger lock. The scavengeSleep function acquires the scavenger lock, then calls resetTimer which can call addInitializedTimer which acquires the timers lock for the current P. So there is a potential deadlock, in that the scavenger lock and the timers lock for some P may both be acquired in different order. It's not clear to me whether this deadlock can ever actually occur. Issue 35532 describes another possible deadlock. The pollSetDeadline function acquires pd.lock for some poll descriptor, and in some cases calls resettimer which can in some cases acquire the timers lock for the current P. The timer code runs timer functions while holding the timers lock for a P. The timer function for poll descriptors winds up in netpolldeadlineimpl which acquires pd.lock. So again there is a potential deadlock, in that the pd lock for some poll descriptor and the timers lock for some P may both be acquired in different order. I think this can happen if we change the deadline for a network connection exactly as the former deadline expires. Looking at the code, I don't see any reason why we have to hold the timers lock while running a timer function. This CL implements that change. Updates #6239 Updates #27707 Fixes #35532 Change-Id: I17792f5a0120e01ea07cf1b2de8434d5c10704dd Reviewed-on: https://go-review.googlesource.com/c/go/+/207348 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2019-11-13runtime: acquire timersLocks around moveTimersIan Lance Taylor
In the discussion of CL 171828 we decided that it was not necessary to acquire timersLock around the call to moveTimers, because the world is stopped. However, that is not correct, as sysmon runs even when the world is stopped, and it calls timeSleepUntil which looks through the timers. timeSleepUntil acquires timersLock, but that doesn't help if moveTimers is running at the same time. Updates #6239 Updates #27707 Updates #35462 Change-Id: I346c5bde594c4aff9955ae430b37c2b6fc71567f Reviewed-on: https://go-review.googlesource.com/c/go/+/206938 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2019-11-06runtime: don't hold scheduler lock when calling timeSleepUntilIan Lance Taylor
Otherwise, we can get into a deadlock: sysmon takes the scheduler lock and calls timeSleepUntil which takes each P's timer lock. Simultaneously, some P calls runtimer (holding the P's own timer lock) which wakes up the scavenger, calling goready, calling wakep, calling startm, getting the scheduler lock. Now the sysmon thread is holding the scheduler lock and trying to get a P's timer lock, while some other thread running on that P is holding the P's timer lock and trying to get the scheduler lock. So change sysmon to call timeSleepUntil without holding the scheduler lock, and change timeSleepUntil to use allpLock, which is only held for limited periods of time and should never compete with timer locks. This hopefully Fixes #35375 At least it should fix the linux-arm64-packet builder problems, which occurred more reliably as that system has GOMAXPROCS == 96, giving a lot more scope for this deadlock. Change-Id: I7a7917daf7a4882e0b27ca416e4f6300cfaaa774 Reviewed-on: https://go-review.googlesource.com/c/go/+/205558 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Cherry Zhang <cherryyz@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2019-11-05runtime: keep adjusted timers in timerMoving status until movedIan Lance Taylor
Before this CL adjustTimers left timers being moved in an inconsistent state: status timerWaiting but not on a P. Simplify the code by leaving the timers in timerMoving status until they are actually moved. Other functions (deltimer, modtimer) will wait until the move is complete before changing anything on the timer. This does leave timers in timerMoving state for longer, but still not all that long. Fixes #35367 Change-Id: I31851002fb4053bd6914139125b4c82a68bf6fb2 Reviewed-on: https://go-review.googlesource.com/c/go/+/205418 Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2019-11-04runtime: wake netpoller when dropping P, don't sleep too long in sysmonIan Lance Taylor
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>
2019-11-01runtime: use atomic.Cas to change timerRemoved to timerWaitingIan Lance Taylor
If multiple goroutines call time.(*Timer).Reset then the timer will go from timerWaiting to timerDeleted to timerModifying to timerModifiedLater. The timer can be on a different P, meaning that simultaneously cleantimers could change it from timerDeleted to timerRemoving to timerRemoved. If Reset sees timerRemoved, it was doing an atomic.Store of timerWaiting, meaning that it did not necessarily see the other values set in the timer, so the timer could appear to be in an inconsistent state. Use atomic.Cas to avoid that possibility. Updates #6239 Updates #27707 Fixes #35272 Change-Id: I1d59a13dc4f2ff4af110fc6e032c8c9d59cfc270 Reviewed-on: https://go-review.googlesource.com/c/go/+/204717 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2019-11-01runtime: use correct state machine in addAdjustedTimersIan Lance Taylor
The addAdjustedTimers function was a late addition, and it got some of the state machine wrong, leading to failures like https://storage.googleapis.com/go-build-log/930576b6/windows-amd64-2016_53d0319e.log Updates #6239 Updates #27707 Change-Id: I9e94e563b4698ff3035ce609055ca292b9cab3df Reviewed-on: https://go-review.googlesource.com/c/go/+/204280 Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2019-10-30runtime: switch to using new timer codeIan Lance Taylor
No big changes in the runtime package benchmarks. Changes in the time package benchmarks: name old time/op new time/op delta AfterFunc-12 1.57ms ± 1% 0.07ms ± 1% -95.42% (p=0.000 n=10+8) After-12 1.63ms ± 3% 0.11ms ± 1% -93.54% (p=0.000 n=9+10) Stop-12 78.3µs ± 3% 73.6µs ± 3% -6.01% (p=0.000 n=9+10) SimultaneousAfterFunc-12 138µs ± 1% 111µs ± 1% -19.57% (p=0.000 n=10+9) StartStop-12 28.7µs ± 1% 31.5µs ± 5% +9.64% (p=0.000 n=10+7) Reset-12 6.78µs ± 1% 4.24µs ± 7% -37.45% (p=0.000 n=9+10) Sleep-12 183µs ± 1% 125µs ± 1% -31.67% (p=0.000 n=10+9) Ticker-12 5.40ms ± 2% 0.03ms ± 1% -99.43% (p=0.000 n=10+10) Sub-12 114ns ± 1% 113ns ± 3% ~ (p=0.069 n=9+10) Now-12 37.2ns ± 1% 36.8ns ± 3% ~ (p=0.287 n=8+8) NowUnixNano-12 38.1ns ± 2% 37.4ns ± 3% -1.87% (p=0.020 n=10+9) Format-12 252ns ± 2% 195ns ± 3% -22.61% (p=0.000 n=9+10) FormatNow-12 234ns ± 1% 177ns ± 2% -24.34% (p=0.000 n=10+10) MarshalJSON-12 320ns ± 2% 250ns ± 0% -21.94% (p=0.000 n=8+8) MarshalText-12 320ns ± 2% 245ns ± 2% -23.30% (p=0.000 n=9+10) Parse-12 206ns ± 2% 208ns ± 4% ~ (p=0.084 n=10+10) ParseDuration-12 89.1ns ± 1% 86.6ns ± 3% -2.78% (p=0.000 n=10+10) Hour-12 4.43ns ± 2% 4.46ns ± 1% ~ (p=0.324 n=10+8) Second-12 4.47ns ± 1% 4.40ns ± 3% ~ (p=0.145 n=9+10) Year-12 14.6ns ± 1% 14.7ns ± 2% ~ (p=0.112 n=9+9) Day-12 20.1ns ± 3% 20.2ns ± 1% ~ (p=0.404 n=10+9) Updates #6239 Updates #27707 Change-Id: I51e25a90f941574f1a9cf83a22e84ac8c678537d Reviewed-on: https://go-review.googlesource.com/c/go/+/171883 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
2019-10-23runtime: add race detector support for new timersIan Lance Taylor
Since the new timers run on g0, which does not have a race context, we add a race context field to the P, and use that for timer functions. This works since all timer functions are in the standard library. Updates #27707 Change-Id: I8a5b727b4ddc8ca6fc60eb6d6f5e9819245e395b Reviewed-on: https://go-review.googlesource.com/c/go/+/171882 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Keith Randall <khr@golang.org>
2019-10-23runtime: implement timeSleepUntil for new timersIan Lance Taylor
Updates #27707 Change-Id: Id4b37594511895f404ee3c09a85263b2b35f835d Reviewed-on: https://go-review.googlesource.com/c/go/+/171881 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
2019-10-23runtime: update timejump function for new timersIan Lance Taylor
Since timers are now on a P, rather than having a G running timerproc, timejump changes to return a P rather than a G. Updates #27707 Change-Id: I3d05af2d664409a0fd906e709fdecbbcbe00b9a7 Reviewed-on: https://go-review.googlesource.com/c/go/+/171880 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2019-10-22runtime: implement time.Sleep for new timersIan Lance Taylor
Updates #27707 Change-Id: I51da8a04ec12ba1efa435e86e3a15d4d13c96c45 Reviewed-on: https://go-review.googlesource.com/c/go/+/171879 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2019-10-22runtime: add new runtimer functionIan Lance Taylor
Updates #27707 Change-Id: I1e65effb708911c727d126c51e0f50fe219f42ff Reviewed-on: https://go-review.googlesource.com/c/go/+/171878 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2019-10-22runtime: add new adjusttimers functionIan Lance Taylor
The adjusttimers function is where we check the adjustTimers field in the P struct to see if we need to resort the heap. We walk forward in the heap and find and resort timers that have been modified, until we find all the timers that were modified to run earlier. Along the way we remove deleted timers. Updates #27707 Change-Id: I1cba7fe77b8112b7e9a9dba80b5dfb08fcc7c568 Reviewed-on: https://go-review.googlesource.com/c/go/+/171877 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2019-10-22runtime: implement new movetimers functionIan Lance Taylor
Updates #27707 Change-Id: Idda31d0065064a81c570e291ef588d020871997d Reviewed-on: https://go-review.googlesource.com/c/go/+/171836 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2019-10-22runtime: add new cleantimers functionIan Lance Taylor
Also add a skeleton of the runOneTimer function. Updates #27707 Change-Id: Ic6a0279354a57295f823093704b7e152ce5d769d Reviewed-on: https://go-review.googlesource.com/c/go/+/171835 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2019-10-22runtime: add new dodeltimer and dodeltimer0 functionsIan Lance Taylor
The dodeltimer function removes a timer from a heap. The dodeltimer0 function removes the first timer from a heap; in the old timer code this common special case was inlined in the timerproc function. Updates #27707 Change-Id: I1b7c0af46866abb4bffa8aa4d8e7143f9ae8f402 Reviewed-on: https://go-review.googlesource.com/c/go/+/171834 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
2019-10-22runtime: add new resettimer functionIan Lance Taylor
Updates #27707 Change-Id: I02f97ec7869ec8a3fb2dfc94cff246badc7ea0fa Reviewed-on: https://go-review.googlesource.com/c/go/+/171833 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2019-10-22runtime: add new modtimer functionIan Lance Taylor
This adds a new field to P, adjustTimers, that tells the P that one of its existing timers was modified to be earlier, and that it therefore needs to resort them. Updates #27707 Change-Id: I4c5f5b51ed116f1d898d3f87cdddfa1b552337f8 Reviewed-on: https://go-review.googlesource.com/c/go/+/171832 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2019-10-22runtime: add new deltimer functionIan Lance Taylor
Updates #27707 Change-Id: I720e8af9e183c75abcb63ccc30466734c8dba74f Reviewed-on: https://go-review.googlesource.com/c/go/+/171831 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2019-10-22runtime: add new addtimer functionIan Lance Taylor
When we add a timer, make sure that the network poller is initialized, since we will use it if we have to wait for the timer to be ready. Updates #27707 Change-Id: I0637fe646bade2cc5ce50b745712292aa9c445b1 Reviewed-on: https://go-review.googlesource.com/c/go/+/171830 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2019-10-21runtime, syscall, time: prepare for adding timers to P'sIan Lance Taylor
Add new fields to runtime.timer, and adjust the various timer functions in preparation for adding timers to P's. This continues to use the old timer code. Updates #6239 Updates #27707 Change-Id: I9adb3814f657e083ec5e22736c4b5b52b77b6a3f Reviewed-on: https://go-review.googlesource.com/c/go/+/171829 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2019-10-21runtime: handle timers on P's in procresize/(*pp).destroyIan Lance Taylor
Updates #6239 Updates #27707 Change-Id: I52cab8bf3dc8c552463725fc1d9e4e6b12230b03 Reviewed-on: https://go-review.googlesource.com/c/go/+/171828 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2019-10-21runtime: initial scheduler changes for timers on P'sIan Lance Taylor
Add support to the main scheduler loop for handling timers on P's. This is not used yet, as timers are not yet put on P's. Updates #6239 Updates #27707 Change-Id: I6a359df408629f333a9232142ce19e8be8496dae Reviewed-on: https://go-review.googlesource.com/c/go/+/171826 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>