Skip to content

Commit

Permalink
Measure per-taskgroup actual CPU time in addition to scheduler ticks
Browse files Browse the repository at this point in the history
  • Loading branch information
knz committed Feb 22, 2021
1 parent 61dc69d commit b089033
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/runtime/proc.go
Expand Up @@ -2537,6 +2537,8 @@ func execute(gp *g, inheritTime bool) {
_g_.m.p.ptr().schedtick++
}
// BEGIN - CockroachDB tweaks
// Accounting for the new G, the one we're scheduling into.
gp.lastSchedTime = nanotime()
if gp.taskGroupCtx != nil {
atomic.Xadd64(&gp.taskGroupCtx.schedtick, 1)
}
Expand Down Expand Up @@ -3185,6 +3187,15 @@ top:
func dropg() {
_g_ := getg()

// BEGIN - CockroachDB tweaks
// Accounting for the past G, the one we're scheduling away from.
if _g_.m.curg != nil && _g_.m.curg.taskGroupCtx != nil {
endTickTime := nanotime()
lastTime := _g_.m.curg.lastSchedTime
atomic.Xadd64(&_g_.m.curg.taskGroupCtx.nanos, endTickTime-lastTime)
}
// END - CockroachDB tweaks

setMNoWB(&_g_.m.curg.m, nil)
setGNoWB(&_g_.m.curg, nil)
}
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/runtime2.go
Expand Up @@ -474,6 +474,10 @@ type g struct {
// BEGIN - CockroachDB tweaks
taskGroupId int64 // inherited goroutine task group ID. Typically set from the task group root's own goroutine ID via SetGoroutineGroupID(GetGID()).
taskGroupCtx *t // inherited task group context.

// lastSchedTime is the nanotime of the last time a goroutine
// was scheduled into a P.
lastSchedTime int64
// END - CockroachDB tweaks

// Per-G GC state
Expand Down
8 changes: 8 additions & 0 deletions src/runtime/task_group.go
Expand Up @@ -10,6 +10,7 @@ import "runtime/internal/atomic"

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

// defaulTaskGroupCtx is used for top level goroutines without a task group yet.
Expand All @@ -35,3 +36,10 @@ func GetInternalTaskGroupSchedTicks(taskGroup InternalTaskGroup) uint64 {
tg := (*t)(taskGroup)
return atomic.Load64(&tg.schedtick)
}

// GetInternalTaskGroupNanos retrieves the number of CPU nanoseconds used by
// all goroutines in the given task group.
func GetInternalTaskGroupNanos(taskGroup InternalTaskGroup) uint64 {
tg := (*t)(taskGroup)
return atomic.Load64(&tg.nanos)
}

0 comments on commit b089033

Please sign in to comment.