-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
committed
Jan 27, 2020
1 parent
98c48ce
commit c508cdd
Showing
4 changed files
with
24 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters