Moving the clkspeed code to helper
This commit is contained in:
parent
59540c3e93
commit
7fb507e810
|
@ -1016,7 +1016,7 @@ func (h *DockerHandle) collectStats() {
|
|||
cs.UserMode = calculatePercent(
|
||||
s.CPUStats.CPUUsage.UsageInUsermode, s.PreCPUStats.CPUUsage.UsageInUsermode,
|
||||
s.CPUStats.CPUUsage.TotalUsage, s.PreCPUStats.CPUUsage.TotalUsage, cores)
|
||||
cs.TotalTicks = (cs.Percent / 100) * h.getClkSpeed()
|
||||
cs.TotalTicks = (cs.Percent / 100) * shelpers.TotalTicksAvailable()
|
||||
|
||||
h.resourceUsageLock.Lock()
|
||||
h.resourceUsage = &cstructs.TaskResourceUsage{
|
||||
|
@ -1034,16 +1034,6 @@ func (h *DockerHandle) collectStats() {
|
|||
}
|
||||
}
|
||||
|
||||
// getClkSpeed returns the total ticks available on a machine
|
||||
func (h *DockerHandle) getClkSpeed() float64 {
|
||||
if h.clkSpeed == 0.0 {
|
||||
if clkSpeed, err := shelpers.TotalTicksAvailable(); err == nil {
|
||||
h.clkSpeed = clkSpeed
|
||||
}
|
||||
}
|
||||
return h.clkSpeed
|
||||
}
|
||||
|
||||
func calculatePercent(newSample, oldSample, newTotal, oldTotal uint64, cores int) float64 {
|
||||
numerator := newSample - oldSample
|
||||
denom := newTotal - oldTotal
|
||||
|
|
|
@ -48,7 +48,7 @@ func (c *CpuStats) Percent(cpuTime float64) float64 {
|
|||
// TicksConsumed calculates the total ticks consumes by the process across all
|
||||
// cpu cores
|
||||
func (c *CpuStats) TicksConsumed(percent float64) float64 {
|
||||
return (percent / 100) * c.getClkSpeed()
|
||||
return (percent / 100) * shelpers.TotalTicksAvailable()
|
||||
}
|
||||
|
||||
func (c *CpuStats) calculatePercent(t1, t2 float64, timeDelta int64) float64 {
|
||||
|
@ -60,13 +60,3 @@ func (c *CpuStats) calculatePercent(t1, t2 float64, timeDelta int64) float64 {
|
|||
overall_percent := (vDelta / float64(timeDelta)) * 100.0
|
||||
return overall_percent
|
||||
}
|
||||
|
||||
// getClkSpeed returns the total ticks available on a machine
|
||||
func (c *CpuStats) getClkSpeed() float64 {
|
||||
if c.clkSpeed == 0.0 {
|
||||
if clkSpeed, err := shelpers.TotalTicksAvailable(); err == nil {
|
||||
c.clkSpeed = clkSpeed
|
||||
}
|
||||
}
|
||||
return c.clkSpeed
|
||||
}
|
||||
|
|
|
@ -101,7 +101,7 @@ func (h *HostStatsCollector) Collect() (*HostStats, error) {
|
|||
cs[idx].System = system
|
||||
cs[idx].User = user
|
||||
cs[idx].Total = total
|
||||
ticksConsumed += (total / 100) * h.getClkSpeed()
|
||||
ticksConsumed += (total / 100) * shelpers.TotalTicksAvailable()
|
||||
}
|
||||
hs.CPU = cs
|
||||
hs.CPUTicksConsumed = ticksConsumed
|
||||
|
@ -132,16 +132,6 @@ func (h *HostStatsCollector) Collect() (*HostStats, error) {
|
|||
return hs, nil
|
||||
}
|
||||
|
||||
// getClkSpeed returns the total ticks available on a machine
|
||||
func (h *HostStatsCollector) getClkSpeed() float64 {
|
||||
if h.clkSpeed == 0.0 {
|
||||
if clkSpeed, err := shelpers.TotalTicksAvailable(); err == nil {
|
||||
h.clkSpeed = clkSpeed
|
||||
}
|
||||
}
|
||||
return h.clkSpeed
|
||||
}
|
||||
|
||||
// HostCpuStatsCalculator calculates cpu usage percentages
|
||||
type HostCpuStatsCalculator struct {
|
||||
prevIdle float64
|
||||
|
|
|
@ -2,19 +2,29 @@ package stats
|
|||
|
||||
import (
|
||||
"github.com/shirou/gopsutil/cpu"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
clkSpeed float64
|
||||
ticksLock sync.Mutex
|
||||
)
|
||||
|
||||
// TotalTicksAvailable calculates the total frequency available across all cores
|
||||
func TotalTicksAvailable() (float64, error) {
|
||||
var clkSpeed float64
|
||||
var cpuInfo []cpu.InfoStat
|
||||
var err error
|
||||
func TotalTicksAvailable() float64 {
|
||||
ticksLock.Lock()
|
||||
defer ticksLock.Unlock()
|
||||
if clkSpeed == 0.0 {
|
||||
var cpuInfo []cpu.InfoStat
|
||||
var err error
|
||||
|
||||
if cpuInfo, err = cpu.Info(); err != nil {
|
||||
return 0.0, err
|
||||
var totalTicks float64
|
||||
if cpuInfo, err = cpu.Info(); err == nil {
|
||||
for _, cpu := range cpuInfo {
|
||||
totalTicks += cpu.Mhz
|
||||
}
|
||||
clkSpeed = totalTicks
|
||||
}
|
||||
}
|
||||
for _, cpu := range cpuInfo {
|
||||
clkSpeed += cpu.Mhz
|
||||
}
|
||||
return clkSpeed, nil
|
||||
return clkSpeed
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue