Skip to content

Commit b089033

Browse files
committed
Measure per-taskgroup actual CPU time in addition to scheduler ticks
1 parent 61dc69d commit b089033

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

src/runtime/proc.go

+11
Original file line numberDiff line numberDiff line change
@@ -2537,6 +2537,8 @@ func execute(gp *g, inheritTime bool) {
25372537
_g_.m.p.ptr().schedtick++
25382538
}
25392539
// BEGIN - CockroachDB tweaks
2540+
// Accounting for the new G, the one we're scheduling into.
2541+
gp.lastSchedTime = nanotime()
25402542
if gp.taskGroupCtx != nil {
25412543
atomic.Xadd64(&gp.taskGroupCtx.schedtick, 1)
25422544
}
@@ -3185,6 +3187,15 @@ top:
31853187
func dropg() {
31863188
_g_ := getg()
31873189

3190+
// BEGIN - CockroachDB tweaks
3191+
// Accounting for the past G, the one we're scheduling away from.
3192+
if _g_.m.curg != nil && _g_.m.curg.taskGroupCtx != nil {
3193+
endTickTime := nanotime()
3194+
lastTime := _g_.m.curg.lastSchedTime
3195+
atomic.Xadd64(&_g_.m.curg.taskGroupCtx.nanos, endTickTime-lastTime)
3196+
}
3197+
// END - CockroachDB tweaks
3198+
31883199
setMNoWB(&_g_.m.curg.m, nil)
31893200
setGNoWB(&_g_.m.curg, nil)
31903201
}

src/runtime/runtime2.go

+4
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,10 @@ type g struct {
474474
// BEGIN - CockroachDB tweaks
475475
taskGroupId int64 // inherited goroutine task group ID. Typically set from the task group root's own goroutine ID via SetGoroutineGroupID(GetGID()).
476476
taskGroupCtx *t // inherited task group context.
477+
478+
// lastSchedTime is the nanotime of the last time a goroutine
479+
// was scheduled into a P.
480+
lastSchedTime int64
477481
// END - CockroachDB tweaks
478482

479483
// Per-G GC state

src/runtime/task_group.go

+8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import "runtime/internal/atomic"
1010

1111
type t struct {
1212
schedtick uint64 // incremented atomically on every scheduler call
13+
nanos uint64 // cumulative slices of CPU time used by the task group, in nanoseconds
1314
}
1415

1516
// defaulTaskGroupCtx is used for top level goroutines without a task group yet.
@@ -35,3 +36,10 @@ func GetInternalTaskGroupSchedTicks(taskGroup InternalTaskGroup) uint64 {
3536
tg := (*t)(taskGroup)
3637
return atomic.Load64(&tg.schedtick)
3738
}
39+
40+
// GetInternalTaskGroupNanos retrieves the number of CPU nanoseconds used by
41+
// all goroutines in the given task group.
42+
func GetInternalTaskGroupNanos(taskGroup InternalTaskGroup) uint64 {
43+
tg := (*t)(taskGroup)
44+
return atomic.Load64(&tg.nanos)
45+
}

0 commit comments

Comments
 (0)