diff --git a/process/process.go b/process/process.go index 0ca26c2109..3a145107c0 100644 --- a/process/process.go +++ b/process/process.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "errors" - "runtime" "sort" "sync" "time" @@ -262,9 +261,11 @@ 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) + delta := now.Sub(p.lastCPUTime).Seconds() + if delta == 0 { + return 0, nil + } + ret := (cpuTimes.Total() - p.lastCPUTimes.Total()) * 100 / delta p.lastCPUTimes = cpuTimes p.lastCPUTime = now return ret, nil @@ -305,15 +306,6 @@ func (p *Process) CreateTimeWithContext(ctx context.Context) (int64, error) { return p.createTime, err } -func calculatePercent(t1, t2 *cpu.TimesStat, delta float64, numcpu int) float64 { - if delta == 0 { - return 0 - } - delta_proc := t2.Total() - t1.Total() - overall_percent := ((delta_proc / delta) * 100) * float64(numcpu) - return overall_percent -} - // MemoryPercent returns how many percent of the total RAM this process uses func (p *Process) MemoryPercent() (float32, error) { return p.MemoryPercentWithContext(context.Background())