Merge pull request #152 from walles/walles/total-cpu-time

Make a public function for computing total CPU time
This commit is contained in:
shirou 2016-02-12 00:07:32 +09:00
commit 70b7a99f9b
2 changed files with 8 additions and 7 deletions

View File

@ -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)

View File

@ -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
}