From c508cddfe89da179e8ab91e510b69a2cf8a38991 Mon Sep 17 00:00:00 2001 From: Mahmood Ali Date: Mon, 27 Jan 2020 13:36:38 -0500 Subject: [PATCH] Update gopsutil code Latest gosutil includes two backward incompatible changes: First, it removed unused Stolen field in https://github.com/shirou/gopsutil/commit/cae8efcffa765c9534442328d385bdd883faf5df#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 https://github.com/shirou/gopsutil/pull/611. --- client/stats/cpu.go | 73 ++++++++++++------------------------- client/stats/cpu_unix.go | 36 ------------------ client/stats/cpu_windows.go | 53 --------------------------- client/stats/host.go | 2 +- 4 files changed, 24 insertions(+), 140 deletions(-) delete mode 100644 client/stats/cpu_unix.go delete mode 100644 client/stats/cpu_windows.go diff --git a/client/stats/cpu.go b/client/stats/cpu.go index 47a9ee3cc63..a84716c04ac 100644 --- a/client/stats/cpu.go +++ b/client/stats/cpu.go @@ -1,61 +1,34 @@ package stats import ( - "runtime" - "time" - shelpers "github.com/hashicorp/nomad/helper/stats" + "github.com/shirou/gopsutil/cpu" ) -// CpuStats calculates cpu usage percentage -type CpuStats struct { - prevCpuTime float64 - prevTime time.Time - - totalCpus int -} - -// NewCpuStats returns a cpu stats calculator -func NewCpuStats() *CpuStats { - numCpus := runtime.NumCPU() - cpuStats := &CpuStats{ - totalCpus: numCpus, - } - return cpuStats -} - -// Percent calculates the cpu usage percentage based on the current cpu usage -// and the previous cpu usage where usage is given as time in nanoseconds spend -// in the cpu -func (c *CpuStats) Percent(cpuTime float64) float64 { - now := time.Now() +func (h *HostStatsCollector) collectCPUStats() (cpus []*CPUStats, totalTicks float64, err error) { - if c.prevCpuTime == 0.0 { - // invoked first time - c.prevCpuTime = cpuTime - c.prevTime = now - return 0.0 + ticksConsumed := 0.0 + cpuStats, err := cpu.Times(true) + if err != nil { + return nil, 0.0, err } - - timeDelta := now.Sub(c.prevTime).Nanoseconds() - ret := c.calculatePercent(c.prevCpuTime, cpuTime, timeDelta) - c.prevCpuTime = cpuTime - c.prevTime = now - return ret -} - -// TicksConsumed calculates the total ticks consumes by the process across all -// cpu cores -func (c *CpuStats) TicksConsumed(percent float64) float64 { - return (percent / 100) * shelpers.TotalTicksAvailable() / float64(c.totalCpus) -} - -func (c *CpuStats) calculatePercent(t1, t2 float64, timeDelta int64) float64 { - vDelta := t2 - t1 - if timeDelta <= 0 || vDelta <= 0.0 { - return 0.0 + 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))) } - overall_percent := (vDelta / float64(timeDelta)) * 100.0 - return overall_percent + return cs, ticksConsumed, nil } diff --git a/client/stats/cpu_unix.go b/client/stats/cpu_unix.go deleted file mode 100644 index 3416cd4dab8..00000000000 --- a/client/stats/cpu_unix.go +++ /dev/null @@ -1,36 +0,0 @@ -// +build !windows - -package stats - -import ( - shelpers "github.com/hashicorp/nomad/helper/stats" - "github.com/shirou/gopsutil/cpu" -) - -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 -} diff --git a/client/stats/cpu_windows.go b/client/stats/cpu_windows.go deleted file mode 100644 index 64b59c09397..00000000000 --- a/client/stats/cpu_windows.go +++ /dev/null @@ -1,53 +0,0 @@ -// +build windows - -package stats - -import ( - "fmt" - - shelpers "github.com/hashicorp/nomad/helper/stats" - "github.com/shirou/gopsutil/cpu" -) - -func (h *HostStatsCollector) collectCPUStats() (cpus []*CPUStats, totalTicks float64, err error) { - // Get the per cpu stats - cpuStats, err := cpu.Times(true) - if err != nil { - return nil, 0.0, err - } - - cs := make([]*CPUStats, len(cpuStats)) - for idx, cpuStat := range cpuStats { - - // On windows they are already in percent - cs[idx] = &CPUStats{ - CPU: cpuStat.CPU, - User: cpuStat.User, - System: cpuStat.System, - Idle: cpuStat.Idle, - Total: cpuStat.Total(), - } - } - - // Get the number of ticks - allCpu, err := cpu.Times(false) - if err != nil { - return nil, 0.0, err - } - if len(allCpu) != 1 { - return nil, 0.0, fmt.Errorf("unexpected number of cpus (%d)", len(allCpu)) - } - - // We use the calculator because when retrieving against all cpus it is - // returned as ticks. - all := allCpu[0] - percentCalculator, ok := h.statsCalculator[all.CPU] - if !ok { - percentCalculator = NewHostCpuStatsCalculator() - h.statsCalculator[all.CPU] = percentCalculator - } - _, _, _, total := percentCalculator.Calculate(all) - ticks := (total / 100) * shelpers.TotalTicksAvailable() - - return cs, ticks, nil -} diff --git a/client/stats/host.go b/client/stats/host.go index a434c9421fa..4cc11545b3a 100644 --- a/client/stats/host.go +++ b/client/stats/host.go @@ -275,7 +275,7 @@ func (h *HostCpuStatsCalculator) Calculate(times cpu.TimesStat) (idle float64, u currentSystem := times.System currentTotal := times.Total() currentBusy := times.User + times.System + times.Nice + times.Iowait + times.Irq + - times.Softirq + times.Steal + times.Guest + times.GuestNice + times.Stolen + times.Softirq + times.Steal + times.Guest + times.GuestNice deltaTotal := currentTotal - h.prevTotal idle = ((currentIdle - h.prevIdle) / deltaTotal) * 100