From 696e1e0123aeee6b0f6de5ceeae0107e3f407911 Mon Sep 17 00:00:00 2001 From: Kemal Hadimli Date: Thu, 28 Sep 2017 14:34:12 +0300 Subject: [PATCH] Fix NaN percentage if process was created too soon --- process/process.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/process/process.go b/process/process.go index 8393049..504d7d4 100644 --- a/process/process.go +++ b/process/process.go @@ -207,12 +207,13 @@ func (p *Process) CPUPercent() (float64, error) { } - cpu, err := p.Times() + cput, err := p.Times() if err != nil { return 0, err } + created := time.Unix(0, crt_time * int64(time.Millisecond)) + totalTime := time.Since(created).Seconds() - return (100 * (cpu.Total()) / float64(time.Now().Unix()-(crt_time/1000))), nil + return (100 * (cput.Total() / totalTime)), nil } -