diff --git a/cpu/cpu.go b/cpu/cpu.go index fb23a39c291af1bf2e5456cb16423d1896a4c805..d4925aae0a87dcfe4123150d5caf66e4dd68415a 100644 --- a/cpu/cpu.go +++ b/cpu/cpu.go @@ -63,6 +63,13 @@ func (c CPUTimesStat) String() string { return `{` + strings.Join(v, ",") + `}` } +// Total returns the total number of seconds in a CPUTimesStat +func (c CPUTimesStat) Total() float64 { + total := c.User + c.System + c.Nice + c.Iowait + c.Irq + c.Softirq + c.Steal + + c.Guest + c.GuestNice + c.Idle + c.Stolen + return total +} + func (c CPUInfoStat) String() string { s, _ := json.Marshal(c) return string(s) diff --git a/process/process.go b/process/process.go index 0a1c3fee5d39cfce655e03a204972ad5ec3f4b7d..4f63a2a9fd84b978f92e7ecba1e5055f87f2b924 100644 --- a/process/process.go +++ b/process/process.go @@ -141,13 +141,7 @@ func calculatePercent(t1, t2 *cpu.CPUTimesStat, delta float64, numcpu int) float if delta == 0 { return 0 } - delta_proc := totalCpuTime(t2) - totalCpuTime(t1) + delta_proc := t2.Total() - t1.Total() overall_percent := ((delta_proc / delta) * 100) * float64(numcpu) return overall_percent } - -func totalCpuTime(t *cpu.CPUTimesStat) float64 { - total := t.User + t.System + t.Nice + t.Iowait + t.Irq + t.Softirq + t.Steal + - t.Guest + t.GuestNice + t.Idle - return total -}