Skip to content

Commit bfbb288

Browse files
runtime: remove adjustTimers counter
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 <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Michael Pratt <[email protected]>
1 parent 9c81fd5 commit bfbb288

File tree

3 files changed

+8
-52
lines changed

3 files changed

+8
-52
lines changed

src/runtime/proc.go

-1
Original file line numberDiff line numberDiff line change
@@ -4919,7 +4919,6 @@ func (pp *p) destroy() {
49194919
moveTimers(plocal, pp.timers)
49204920
pp.timers = nil
49214921
pp.numTimers = 0
4922-
pp.adjustTimers = 0
49234922
pp.deletedTimers = 0
49244923
atomic.Store64(&pp.timer0When, 0)
49254924
unlock(&pp.timersLock)

src/runtime/runtime2.go

-6
Original file line numberDiff line numberDiff line change
@@ -727,12 +727,6 @@ type p struct {
727727
// Modified using atomic instructions.
728728
numTimers uint32
729729

730-
// Number of timerModifiedEarlier timers on P's heap.
731-
// This should only be modified while holding timersLock,
732-
// or while the timer status is in a transient state
733-
// such as timerModifying.
734-
adjustTimers uint32
735-
736730
// Number of timerDeleted timers in P's heap.
737731
// Modified using atomic instructions.
738732
deletedTimers uint32

src/runtime/time.go

+8-45
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,6 @@ func deltimer(t *timer) bool {
333333
// Must fetch t.pp before setting status
334334
// to timerDeleted.
335335
tpp := t.pp.ptr()
336-
atomic.Xadd(&tpp.adjustTimers, -1)
337336
if !atomic.Cas(&t.status, timerModifying, timerDeleted) {
338337
badTimer()
339338
}
@@ -510,20 +509,9 @@ loop:
510509

511510
tpp := t.pp.ptr()
512511

513-
// Update the adjustTimers field. Subtract one if we
514-
// are removing a timerModifiedEarlier, add one if we
515-
// are adding a timerModifiedEarlier.
516-
adjust := int32(0)
517-
if status == timerModifiedEarlier {
518-
adjust--
519-
}
520512
if newStatus == timerModifiedEarlier {
521-
adjust++
522513
updateTimerModifiedEarliest(tpp, when)
523514
}
524-
if adjust != 0 {
525-
atomic.Xadd(&tpp.adjustTimers, adjust)
526-
}
527515

528516
// Set the new status of the timer.
529517
if !atomic.Cas(&t.status, timerModifying, newStatus) {
@@ -591,9 +579,6 @@ func cleantimers(pp *p) {
591579
// Move t to the right position.
592580
dodeltimer0(pp)
593581
doaddtimer(pp, t)
594-
if s == timerModifiedEarlier {
595-
atomic.Xadd(&pp.adjustTimers, -1)
596-
}
597582
if !atomic.Cas(&t.status, timerMoving, timerWaiting) {
598583
badTimer()
599584
}
@@ -664,32 +649,23 @@ func moveTimers(pp *p, timers []*timer) {
664649
// it also moves timers that have been modified to run later,
665650
// and removes deleted timers. The caller must have locked the timers for pp.
666651
func adjusttimers(pp *p, now int64) {
667-
if atomic.Load(&pp.adjustTimers) == 0 {
668-
if verifyTimers {
669-
verifyTimerHeap(pp)
670-
}
671-
return
672-
}
673-
674652
// If we haven't yet reached the time of the first timerModifiedEarlier
675653
// timer, don't do anything. This speeds up programs that adjust
676654
// a lot of timers back and forth if the timers rarely expire.
677655
// We'll postpone looking through all the adjusted timers until
678656
// one would actually expire.
679-
if first := atomic.Load64(&pp.timerModifiedEarliest); first != 0 {
680-
if int64(first) > now {
681-
if verifyTimers {
682-
verifyTimerHeap(pp)
683-
}
684-
return
657+
first := atomic.Load64(&pp.timerModifiedEarliest)
658+
if first == 0 || int64(first) > now {
659+
if verifyTimers {
660+
verifyTimerHeap(pp)
685661
}
686-
687-
// We are going to clear all timerModifiedEarlier timers.
688-
atomic.Store64(&pp.timerModifiedEarliest, 0)
662+
return
689663
}
690664

665+
// We are going to clear all timerModifiedEarlier timers.
666+
atomic.Store64(&pp.timerModifiedEarliest, 0)
667+
691668
var moved []*timer
692-
loop:
693669
for i := 0; i < len(pp.timers); i++ {
694670
t := pp.timers[i]
695671
if t.pp.ptr() != pp {
@@ -716,11 +692,6 @@ loop:
716692
// loop to skip some other timer.
717693
dodeltimer(pp, i)
718694
moved = append(moved, t)
719-
if s == timerModifiedEarlier {
720-
if n := atomic.Xadd(&pp.adjustTimers, -1); int32(n) <= 0 {
721-
break loop
722-
}
723-
}
724695
// Look at this heap position again.
725696
i--
726697
}
@@ -819,9 +790,6 @@ func runtimer(pp *p, now int64) int64 {
819790
t.when = t.nextwhen
820791
dodeltimer0(pp)
821792
doaddtimer(pp, t)
822-
if s == timerModifiedEarlier {
823-
atomic.Xadd(&pp.adjustTimers, -1)
824-
}
825793
if !atomic.Cas(&t.status, timerMoving, timerWaiting) {
826794
badTimer()
827795
}
@@ -916,7 +884,6 @@ func clearDeletedTimers(pp *p) {
916884
atomic.Store64(&pp.timerModifiedEarliest, 0)
917885

918886
cdel := int32(0)
919-
cearlier := int32(0)
920887
to := 0
921888
changedHeap := false
922889
timers := pp.timers
@@ -941,9 +908,6 @@ nextTimer:
941908
if !atomic.Cas(&t.status, timerMoving, timerWaiting) {
942909
badTimer()
943910
}
944-
if s == timerModifiedEarlier {
945-
cearlier++
946-
}
947911
continue nextTimer
948912
}
949913
case timerDeleted:
@@ -980,7 +944,6 @@ nextTimer:
980944

981945
atomic.Xadd(&pp.deletedTimers, -cdel)
982946
atomic.Xadd(&pp.numTimers, -cdel)
983-
atomic.Xadd(&pp.adjustTimers, -cearlier)
984947

985948
timers = timers[:to]
986949
pp.timers = timers

0 commit comments

Comments
 (0)