Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Add CPU in MHz to stats and emit nomad.client.allocs.cpu.total_mhz metric #11794

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type MemoryStats struct {
type CpuStats struct {
SystemMode float64
UserMode float64
TotalMHz float64
TotalTicks float64
ThrottledPeriods uint64
ThrottledTime uint64
Expand Down
2 changes: 2 additions & 0 deletions client/allocrunner/taskrunner/task_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -1432,6 +1432,8 @@ func (tr *TaskRunner) setGaugeForCPU(ru *cstructs.TaskResourceUsage) {
float32(ru.ResourceUsage.CpuStats.ThrottledTime), tr.baseLabels)
metrics.SetGaugeWithLabels([]string{"client", "allocs", "cpu", "throttled_periods"},
float32(ru.ResourceUsage.CpuStats.ThrottledPeriods), tr.baseLabels)
metrics.SetGaugeWithLabels([]string{"client", "allocs", "cpu", "total_mhz"},
float32(ru.ResourceUsage.CpuStats.TotalMHz), tr.baseLabels)
metrics.SetGaugeWithLabels([]string{"client", "allocs", "cpu", "total_ticks"},
float32(ru.ResourceUsage.CpuStats.TotalTicks), tr.baseLabels)
if allocatedCPU > 0 {
Expand Down
10 changes: 8 additions & 2 deletions client/stats/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,18 @@ func (c *CpuStats) Percent(cpuTime float64) float64 {
return ret
}

// TicksConsumed calculates the total ticks consumes by the process across all
// cpu cores
// TicksConsumed calculates the total ticks consumed by the process across all
// CPU cores
func (c *CpuStats) TicksConsumed(percent float64) float64 {
return (percent / 100) * shelpers.TotalTicksAvailable() / float64(c.totalCpus)
}

// MHzConsumed calculates the total MHz consumed by the process across all CPU
// cores
func (c *CpuStats) MHzConsumed(percent float64) float64 {
return (percent / 100) * shelpers.CPUMHzTotal() / float64(c.totalCpus)
}

func (c *CpuStats) calculatePercent(t1, t2 float64, timeDelta int64) float64 {
vDelta := t2 - t1
if timeDelta <= 0 || vDelta <= 0.0 {
Expand Down
2 changes: 2 additions & 0 deletions client/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ type CpuStats struct {
SystemMode float64
UserMode float64
TotalTicks float64
TotalMHz float64
ThrottledPeriods uint64
ThrottledTime uint64
Percent float64
Expand All @@ -250,6 +251,7 @@ func (cs *CpuStats) Add(other *CpuStats) {

cs.SystemMode += other.SystemMode
cs.UserMode += other.UserMode
cs.TotalMHz += other.TotalMHz
cs.TotalTicks += other.TotalTicks
cs.ThrottledPeriods += other.ThrottledPeriods
cs.ThrottledTime += other.ThrottledTime
Expand Down
1 change: 1 addition & 0 deletions drivers/docker/util/stats_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func DockerStatsToTaskResourceUsage(s *docker.Stats) *cstructs.TaskResourceUsage
cs.UserMode = CalculateCPUPercent(
s.CPUStats.CPUUsage.UsageInUsermode, s.PreCPUStats.CPUUsage.UsageInUsermode,
s.CPUStats.CPUUsage.TotalUsage, s.PreCPUStats.CPUUsage.TotalUsage, runtime.NumCPU())
cs.TotalMHz = (cs.Percent / 100) * stats.CPUMHzTotal() / float64(runtime.NumCPU())
cs.TotalTicks = (cs.Percent / 100) * stats.TotalTicksAvailable() / float64(runtime.NumCPU())

return &cstructs.TaskResourceUsage{
Expand Down
1 change: 1 addition & 0 deletions drivers/docker/util/stats_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func DockerStatsToTaskResourceUsage(s *docker.Stats) *cstructs.TaskResourceUsage
ThrottledPeriods: s.CPUStats.ThrottlingData.ThrottledPeriods,
ThrottledTime: s.CPUStats.ThrottlingData.ThrottledTime,
Percent: cpuPercent,
TotalMHz: (cpuPercent / 100) * stats.CPUMHzTotal() / float64(runtime.NumCPU()),
TotalTicks: (cpuPercent / 100) * stats.TotalTicksAvailable() / float64(runtime.NumCPU()),
Measured: DockerMeasuredCPUStats,
}
Expand Down
1 change: 1 addition & 0 deletions drivers/shared/executor/executor_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ func (l *LibcontainerExecutor) handleStats(ch chan *cstructs.TaskResourceUsage,
Percent: totalPercent,
ThrottledPeriods: stats.CpuStats.ThrottlingData.ThrottledPeriods,
ThrottledTime: stats.CpuStats.ThrottlingData.ThrottledTime,
TotalMHz: l.systemCpuStats.MHzConsumed(totalPercent),
TotalTicks: l.systemCpuStats.TicksConsumed(totalPercent),
Measured: ExecutorCgroupMeasuredCpuStats,
}
Expand Down
6 changes: 6 additions & 0 deletions helper/stats/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ func CPUMHzPerCore() float64 {
return cpuMhzPerCore
}

// CPUMHzTotal returns the total MHz across the number
// of available cores
func CPUMHzTotal() float64 {
return float64(cpuNumCores) * cpuMhzPerCore
}

// CPUModelName returns the model name of the CPU
func CPUModelName() string {
return cpuModelName
Expand Down
1 change: 1 addition & 0 deletions plugins/drivers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ func resourceUsageToProto(ru *ResourceUsage) *proto.TaskResourceUsage {
MeasuredFields: cpuUsageMeasuredFieldsToProto(ru.CpuStats.Measured),
SystemMode: ru.CpuStats.SystemMode,
UserMode: ru.CpuStats.UserMode,
TotalMHz: ru.CpuStats.TotalMHz,
TotalTicks: ru.CpuStats.TotalTicks,
ThrottledPeriods: ru.CpuStats.ThrottledPeriods,
ThrottledTime: ru.CpuStats.ThrottledTime,
Expand Down
1 change: 1 addition & 0 deletions plugins/drivers/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func TestResourceUsageRoundTrip(t *testing.T) {
CpuStats: &CpuStats{
SystemMode: 0,
UserMode: 0.9963907032120152,
TotalMHz: 4096.71,
TotalTicks: 21.920595295932515,
ThrottledPeriods: 2321,
ThrottledTime: 123,
Expand Down