Skip to content

Commit

Permalink
build: update patch with bug fix to runtime grunning
Browse files Browse the repository at this point in the history
In our previous version of this patch, we missed two entry points and
one exit point into the grunning state of a Goroutine. This led to
`grunning.Time()` being non-monotonic.

This new patch adds those missing integration points.

Fixes cockroachdb#95529.

Release note: None
  • Loading branch information
aadityasondhi committed Feb 20, 2024
1 parent 7214f53 commit 11fbe3d
Showing 1 changed file with 43 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,10 @@ index 26dcf0bd52..01b84268db 100644
error at each collection, summarizing the amount of memory collected and the
length of the pause. The format of this line is subject to change. Included in
diff --git a/src/runtime/malloc.go b/src/runtime/malloc.go
index b2026ad0dc..6db0ca52f9 100644
index 44479cc2be..a24ce64a24 100644
--- a/src/runtime/malloc.go
+++ b/src/runtime/malloc.go
@@ -1274,7 +1274,7 @@ func mallocgc(size uintptr, typ *_type, needzero bool) unsafe.Pointer {
@@ -1270,7 +1270,7 @@ func mallocgc(size uintptr, typ *_type, needzero bool) unsafe.Pointer {
// Returns the G for which the assist credit was accounted.
func deductAssistCredit(size uintptr) *g {
var assistG *g
Expand All @@ -272,10 +272,34 @@ index b2026ad0dc..6db0ca52f9 100644
assistG = getg()
if assistG.m.curg != nil {
diff --git a/src/runtime/proc.go b/src/runtime/proc.go
index afb33c1e8b..ccdaaab3e8 100644
index afb33c1e8b..390b99be13 100644
--- a/src/runtime/proc.go
+++ b/src/runtime/proc.go
@@ -1073,7 +1073,18 @@ func casgstatus(gp *g, oldval, newval uint32) {
@@ -1004,6 +1004,11 @@ func casfrom_Gscanstatus(gp *g, oldval, newval uint32) {
dumpgstatus(gp)
throw("casfrom_Gscanstatus: gp->status is not in scan state")
}
+ // We're transitioning into the running state, record the timestamp for
+ // subsequent use.
+ if newval == _Grunning {
+ gp.lastsched = nanotime()
+ }
releaseLockRank(lockRankGscan)
}

@@ -1019,6 +1024,11 @@ func castogscanstatus(gp *g, oldval, newval uint32) bool {
r := gp.atomicstatus.CompareAndSwap(oldval, newval)
if r {
acquireLockRank(lockRankGscan)
+ // We're transitioning out of running, record how long we were in the
+ // state.
+ if oldval == _Grunning {
+ gp.runningnanos += nanotime() - gp.lastsched
+ }
}
return r

@@ -1073,7 +1083,18 @@ func casgstatus(gp *g, oldval, newval uint32) {
}
}

Expand All @@ -294,23 +318,23 @@ index afb33c1e8b..ccdaaab3e8 100644
// Track every gTrackingPeriod time a goroutine transitions out of running.
if casgstatusAlwaysTrack || gp.trackingSeq%gTrackingPeriod == 0 {
gp.tracking = true
@@ -1094,7 +1105,6 @@ func casgstatus(gp *g, oldval, newval uint32) {
@@ -1094,7 +1115,6 @@ func casgstatus(gp *g, oldval, newval uint32) {
// We transitioned out of runnable, so measure how much
// time we spent in this state and add it to
// runnableTime.
- now := nanotime()
gp.runnableTime += now - gp.trackingStamp
gp.trackingStamp = 0
case _Gwaiting:
@@ -1107,7 +1117,6 @@ func casgstatus(gp *g, oldval, newval uint32) {
@@ -1107,7 +1127,6 @@ func casgstatus(gp *g, oldval, newval uint32) {
// a more representative estimate of the absolute value.
// gTrackingPeriod also represents an accurate sampling period
// because we can only enter this state from _Grunning.
- now := nanotime()
sched.totalMutexWaitTime.Add((now - gp.trackingStamp) * gTrackingPeriod)
gp.trackingStamp = 0
}
@@ -1118,12 +1127,10 @@ func casgstatus(gp *g, oldval, newval uint32) {
@@ -1118,12 +1137,10 @@ func casgstatus(gp *g, oldval, newval uint32) {
break
}
// Blocking on a lock. Write down the timestamp.
Expand All @@ -323,7 +347,17 @@ index afb33c1e8b..ccdaaab3e8 100644
gp.trackingStamp = now
case _Grunning:
// We're transitioning into running, so turn off
@@ -3646,6 +3653,14 @@ func dropg() {
@@ -1174,6 +1191,9 @@ func casGToPreemptScan(gp *g, old, new uint32) {
acquireLockRank(lockRankGscan)
for !gp.atomicstatus.CompareAndSwap(_Grunning, _Gscan|_Gpreempted) {
}
+ // We're transitioning out of running, record how long we were in the
+ // state.
+ gp.runningnanos += nanotime() - gp.lastsched
}

// casGFromPreempted attempts to transition gp from _Gpreempted to
@@ -3646,6 +3666,14 @@ func dropg() {
setGNoWB(&gp.m.curg, nil)
}

Expand All @@ -338,7 +372,7 @@ index afb33c1e8b..ccdaaab3e8 100644
// checkTimers runs any timers for the P that are ready.
// If now is not 0 it is the current time.
// It returns the passed time or the current time if now was passed as 0.
@@ -3880,6 +3895,8 @@ func goexit0(gp *g) {
@@ -3880,6 +3908,8 @@ func goexit0(gp *g) {
gp.param = nil
gp.labels = nil
gp.timer = nil
Expand Down

0 comments on commit 11fbe3d

Please sign in to comment.