Skip to content

Commit

Permalink
Update gopsutil code
Browse files Browse the repository at this point in the history
Latest gosutil includes two backward incompatible changes:

First, it removed unused Stolen field in
shirou/gopsutil@cae8efc#diff-d9747e2da342bdb995f6389533ad1a3d
.

Second, it updated the Windows cpu stats calculation to be inline with
other platforms, where it returns absolate stats rather than
percentages.  See shirou/gopsutil#611.
  • Loading branch information
Mahmood Ali authored and greut committed Mar 15, 2020
1 parent f80cbe8 commit 4e4c387
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 89 deletions.
29 changes: 29 additions & 0 deletions client/stats/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

shelpers "github.com/hashicorp/nomad/helper/stats"
"github.com/shirou/gopsutil/cpu"
)

// CpuStats calculates cpu usage percentage
Expand Down Expand Up @@ -59,3 +60,31 @@ func (c *CpuStats) calculatePercent(t1, t2 float64, timeDelta int64) float64 {
overall_percent := (vDelta / float64(timeDelta)) * 100.0
return overall_percent
}

func (h *HostStatsCollector) collectCPUStats() (cpus []*CPUStats, totalTicks float64, err error) {

ticksConsumed := 0.0
cpuStats, err := cpu.Times(true)
if err != nil {
return nil, 0.0, err
}
cs := make([]*CPUStats, len(cpuStats))
for idx, cpuStat := range cpuStats {
percentCalculator, ok := h.statsCalculator[cpuStat.CPU]
if !ok {
percentCalculator = NewHostCpuStatsCalculator()
h.statsCalculator[cpuStat.CPU] = percentCalculator
}
idle, user, system, total := percentCalculator.Calculate(cpuStat)
cs[idx] = &CPUStats{
CPU: cpuStat.CPU,
User: user,
System: system,
Idle: idle,
Total: total,
}
ticksConsumed += (total / 100.0) * (shelpers.TotalTicksAvailable() / float64(len(cpuStats)))
}

return cs, ticksConsumed, nil
}
36 changes: 0 additions & 36 deletions client/stats/cpu_unix.go

This file was deleted.

53 changes: 0 additions & 53 deletions client/stats/cpu_windows.go

This file was deleted.

0 comments on commit 4e4c387

Please sign in to comment.