Skip to content

Commit

Permalink
shirou#755 fixes process.Percent return invalid value
Browse files Browse the repository at this point in the history
make it return percent of all cpu core usage which will bewteen 0 and 100
  • Loading branch information
lrita committed Sep 3, 2019
1 parent e4ec7b2 commit 9870316
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,18 @@ func (p *Process) PercentWithContext(ctx context.Context, interval time.Duration

numcpu := runtime.NumCPU()
delta := (now.Sub(p.lastCPUTime).Seconds()) * float64(numcpu)
ret := calculatePercent(p.lastCPUTimes, cpuTimes, delta, numcpu)
ret := calculatePercent(p.lastCPUTimes, cpuTimes, delta)
p.lastCPUTimes = cpuTimes
p.lastCPUTime = now
return ret, nil
}

func calculatePercent(t1, t2 *cpu.TimesStat, delta float64, numcpu int) float64 {
func calculatePercent(t1, t2 *cpu.TimesStat, delta float64) float64 {
if delta == 0 {
return 0
}
delta_proc := t2.Total() - t1.Total()
overall_percent := ((delta_proc / delta) * 100) * float64(numcpu)
overall_percent := ((delta_proc / delta) * 100)
return math.Min(100, math.Max(0, overall_percent))
}

Expand Down

0 comments on commit 9870316

Please sign in to comment.