]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.16] runtime: remove adjustTimers counter
authorIan Lance Taylor <iant@golang.org>
Sun, 25 Jul 2021 23:26:13 +0000 (16:26 -0700)
committerIan Lance Taylor <iant@golang.org>
Fri, 30 Jul 2021 20:45:25 +0000 (20:45 +0000)
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
For #47332

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>
(cherry picked from commit bfbb288574841f2db2499a580d7bf985a5df4556)
Reviewed-on: https://go-review.googlesource.com/c/go/+/338649

src/runtime/proc.go
src/runtime/runtime2.go
src/runtime/time.go

index cf9770587aa2c2f817a563f615bc527c0259527c..3ace2b329bb76a06e9758123d89a12d1fe93fc57 100644 (file)
@@ -4751,7 +4751,6 @@ func (pp *p) destroy() {
                moveTimers(plocal, pp.timers)
                pp.timers = nil
                pp.numTimers = 0
-               pp.adjustTimers = 0
                pp.deletedTimers = 0
                atomic.Store64(&pp.timer0When, 0)
                unlock(&pp.timersLock)
index dd375da1936c5dd743b63ab17bb1dcca2688fbae..e9825327d105dfb373870b2f3fb97207a7a52789 100644 (file)
@@ -699,12 +699,6 @@ type p struct {
        // Modified using atomic instructions.
        numTimers uint32
 
-       // Number of timerModifiedEarlier timers on P's heap.
-       // This should only be modified while holding timersLock,
-       // or while the timer status is in a transient state
-       // such as timerModifying.
-       adjustTimers uint32
-
        // Number of timerDeleted timers in P's heap.
        // Modified using atomic instructions.
        deletedTimers uint32
index 7b84d2af571515e6e37d442ec57fbeba2a06321b..666b2423164162fd96d1beed5ba6544fe0f6fe71 100644 (file)
@@ -333,7 +333,6 @@ func deltimer(t *timer) bool {
                                // Must fetch t.pp before setting status
                                // to timerDeleted.
                                tpp := t.pp.ptr()
-                               atomic.Xadd(&tpp.adjustTimers, -1)
                                if !atomic.Cas(&t.status, timerModifying, timerDeleted) {
                                        badTimer()
                                }
@@ -510,20 +509,9 @@ loop:
 
                tpp := t.pp.ptr()
 
-               // Update the adjustTimers field.  Subtract one if we
-               // are removing a timerModifiedEarlier, add one if we
-               // are adding a timerModifiedEarlier.
-               adjust := int32(0)
-               if status == timerModifiedEarlier {
-                       adjust--
-               }
                if newStatus == timerModifiedEarlier {
-                       adjust++
                        updateTimerModifiedEarliest(tpp, when)
                }
-               if adjust != 0 {
-                       atomic.Xadd(&tpp.adjustTimers, adjust)
-               }
 
                // Set the new status of the timer.
                if !atomic.Cas(&t.status, timerModifying, newStatus) {
@@ -591,9 +579,6 @@ func cleantimers(pp *p) {
                        // Move t to the right position.
                        dodeltimer0(pp)
                        doaddtimer(pp, t)
-                       if s == timerModifiedEarlier {
-                               atomic.Xadd(&pp.adjustTimers, -1)
-                       }
                        if !atomic.Cas(&t.status, timerMoving, timerWaiting) {
                                badTimer()
                        }
@@ -664,32 +649,23 @@ func moveTimers(pp *p, timers []*timer) {
 // it also moves timers that have been modified to run later,
 // and removes deleted timers. The caller must have locked the timers for pp.
 func adjusttimers(pp *p, now int64) {
-       if atomic.Load(&pp.adjustTimers) == 0 {
-               if verifyTimers {
-                       verifyTimerHeap(pp)
-               }
-               return
-       }
-
        // If we haven't yet reached the time of the first timerModifiedEarlier
        // timer, don't do anything. This speeds up programs that adjust
        // a lot of timers back and forth if the timers rarely expire.
        // We'll postpone looking through all the adjusted timers until
        // one would actually expire.
-       if first := atomic.Load64(&pp.timerModifiedEarliest); first != 0 {
-               if int64(first) > now {
-                       if verifyTimers {
-                               verifyTimerHeap(pp)
-                       }
-                       return
+       first := atomic.Load64(&pp.timerModifiedEarliest)
+       if first == 0 || int64(first) > now {
+               if verifyTimers {
+                       verifyTimerHeap(pp)
                }
-
-               // We are going to clear all timerModifiedEarlier timers.
-               atomic.Store64(&pp.timerModifiedEarliest, 0)
+               return
        }
 
+       // We are going to clear all timerModifiedEarlier timers.
+       atomic.Store64(&pp.timerModifiedEarliest, 0)
+
        var moved []*timer
-loop:
        for i := 0; i < len(pp.timers); i++ {
                t := pp.timers[i]
                if t.pp.ptr() != pp {
@@ -716,11 +692,6 @@ loop:
                                // loop to skip some other timer.
                                dodeltimer(pp, i)
                                moved = append(moved, t)
-                               if s == timerModifiedEarlier {
-                                       if n := atomic.Xadd(&pp.adjustTimers, -1); int32(n) <= 0 {
-                                               break loop
-                                       }
-                               }
                                // Look at this heap position again.
                                i--
                        }
@@ -819,9 +790,6 @@ func runtimer(pp *p, now int64) int64 {
                        t.when = t.nextwhen
                        dodeltimer0(pp)
                        doaddtimer(pp, t)
-                       if s == timerModifiedEarlier {
-                               atomic.Xadd(&pp.adjustTimers, -1)
-                       }
                        if !atomic.Cas(&t.status, timerMoving, timerWaiting) {
                                badTimer()
                        }
@@ -916,7 +884,6 @@ func clearDeletedTimers(pp *p) {
        atomic.Store64(&pp.timerModifiedEarliest, 0)
 
        cdel := int32(0)
-       cearlier := int32(0)
        to := 0
        changedHeap := false
        timers := pp.timers
@@ -941,9 +908,6 @@ nextTimer:
                                        if !atomic.Cas(&t.status, timerMoving, timerWaiting) {
                                                badTimer()
                                        }
-                                       if s == timerModifiedEarlier {
-                                               cearlier++
-                                       }
                                        continue nextTimer
                                }
                        case timerDeleted:
@@ -980,7 +944,6 @@ nextTimer:
 
        atomic.Xadd(&pp.deletedTimers, -cdel)
        atomic.Xadd(&pp.numTimers, -cdel)
-       atomic.Xadd(&pp.adjustTimers, -cearlier)
 
        timers = timers[:to]
        pp.timers = timers