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(
|
cs.UserMode = calculatePercent(
|
||||||
s.CPUStats.CPUUsage.UsageInUsermode, s.PreCPUStats.CPUUsage.UsageInUsermode,
|
s.CPUStats.CPUUsage.UsageInUsermode, s.PreCPUStats.CPUUsage.UsageInUsermode,
|
||||||
s.CPUStats.CPUUsage.TotalUsage, s.PreCPUStats.CPUUsage.TotalUsage, cores)
|
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.resourceUsageLock.Lock()
|
||||||
h.resourceUsage = &cstructs.TaskResourceUsage{
|
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 {
|
func calculatePercent(newSample, oldSample, newTotal, oldTotal uint64, cores int) float64 {
|
||||||
numerator := newSample - oldSample
|
numerator := newSample - oldSample
|
||||||
denom := newTotal - oldTotal
|
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
|
// TicksConsumed calculates the total ticks consumes by the process across all
|
||||||
// cpu cores
|
// cpu cores
|
||||||
func (c *CpuStats) TicksConsumed(percent float64) float64 {
|
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 {
|
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
|
overall_percent := (vDelta / float64(timeDelta)) * 100.0
|
||||||
return overall_percent
|
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].System = system
|
||||||
cs[idx].User = user
|
cs[idx].User = user
|
||||||
cs[idx].Total = total
|
cs[idx].Total = total
|
||||||
ticksConsumed += (total / 100) * h.getClkSpeed()
|
ticksConsumed += (total / 100) * shelpers.TotalTicksAvailable()
|
||||||
}
|
}
|
||||||
hs.CPU = cs
|
hs.CPU = cs
|
||||||
hs.CPUTicksConsumed = ticksConsumed
|
hs.CPUTicksConsumed = ticksConsumed
|
||||||
|
@ -132,16 +132,6 @@ func (h *HostStatsCollector) Collect() (*HostStats, error) {
|
||||||
return hs, nil
|
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
|
// HostCpuStatsCalculator calculates cpu usage percentages
|
||||||
type HostCpuStatsCalculator struct {
|
type HostCpuStatsCalculator struct {
|
||||||
prevIdle float64
|
prevIdle float64
|
||||||
|
|
|
@ -2,19 +2,29 @@ package stats
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/shirou/gopsutil/cpu"
|
"github.com/shirou/gopsutil/cpu"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
clkSpeed float64
|
||||||
|
ticksLock sync.Mutex
|
||||||
)
|
)
|
||||||
|
|
||||||
// TotalTicksAvailable calculates the total frequency available across all cores
|
// TotalTicksAvailable calculates the total frequency available across all cores
|
||||||
func TotalTicksAvailable() (float64, error) {
|
func TotalTicksAvailable() float64 {
|
||||||
var clkSpeed float64
|
ticksLock.Lock()
|
||||||
var cpuInfo []cpu.InfoStat
|
defer ticksLock.Unlock()
|
||||||
var err error
|
if clkSpeed == 0.0 {
|
||||||
|
var cpuInfo []cpu.InfoStat
|
||||||
|
var err error
|
||||||
|
|
||||||
if cpuInfo, err = cpu.Info(); err != nil {
|
var totalTicks float64
|
||||||
return 0.0, err
|
if cpuInfo, err = cpu.Info(); err == nil {
|
||||||
|
for _, cpu := range cpuInfo {
|
||||||
|
totalTicks += cpu.Mhz
|
||||||
|
}
|
||||||
|
clkSpeed = totalTicks
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for _, cpu := range cpuInfo {
|
return clkSpeed
|
||||||
clkSpeed += cpu.Mhz
|
|
||||||
}
|
|
||||||
return clkSpeed, nil
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue