Skip to content

Commit

Permalink
Merge pull request cockroachdb#104498 from kvoli/backport22.2-104153
Browse files Browse the repository at this point in the history
  • Loading branch information
kvoli authored Jun 7, 2023
2 parents c2da7a3 + 9a821c8 commit e405460
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions pkg/server/status/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,10 @@ func (rsr *RuntimeStatSampler) SampleEnvironment(
if err != nil {
log.Ops.Errorf(ctx, "unable to get cpu usage: %v", err)
}
cgroupCPU, _ := cgroups.GetCgroupCPU()
cpuShare := cgroupCPU.CPUShares()
cpuCapacity, err := getCPUCapacity()
if err != nil {
log.Ops.Errorf(ctx, "unable to get CPU capacity: %v", err)
}

fds := gosigar.ProcFDUsage{}
if err := fds.Get(pid); err != nil {
Expand Down Expand Up @@ -519,7 +521,7 @@ func (rsr *RuntimeStatSampler) SampleEnvironment(
stime := sysTimeMillis * 1e6
urate := float64(utime-rsr.last.utime) / dur
srate := float64(stime-rsr.last.stime) / dur
combinedNormalizedPerc := (srate + urate) / cpuShare
combinedNormalizedPerc := (srate + urate) / cpuCapacity
gcPauseRatio := float64(uint64(gc.PauseTotal)-rsr.last.gcPauseTime) / dur
runnableSum := goschedstats.CumulativeNormalizedRunnableGoroutines()
// The number of runnable goroutines per CPU is a count, but it can vary
Expand Down Expand Up @@ -712,3 +714,23 @@ func GetCPUTime(ctx context.Context) (userTimeMillis, sysTimeMillis int64, err e
}
return int64(cpuTime.User), int64(cpuTime.Sys), nil
}

// getCPUCapacity returns the number of logical CPU processors available for
// use by the process. The capacity accounts for cgroup constraints, GOMAXPROCS
// and the number of host processors.
func getCPUCapacity() (float64, error) {
numProcs := float64(runtime.GOMAXPROCS(0 /* read only */))
cgroupCPU, err := cgroups.GetCgroupCPU()
if err != nil {
// Return the GOMAXPROCS value if unable to read the cgroup settings, in
// practice this is not likely to occur.
return numProcs, err
}
cpuShare := cgroupCPU.CPUShares()
// Take the minimum of the CPU shares and the GOMAXPROCS value. The most CPU
// the process could use is the lesser of the two.
if cpuShare > numProcs {
return numProcs, nil
}
return cpuShare, nil
}

0 comments on commit e405460

Please sign in to comment.