Merge pull request #426 from disq/cpu-NaN-fix

Fix NaN percentage if process was created too soon
pull/431/head
shirou 8 years ago committed by GitHub
commit fcba942e03

@ -207,12 +207,16 @@ 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()
if totalTime <= 0 {
return 0, nil
}
return (100 * (cpu.Total()) / float64(time.Now().Unix()-(crt_time/1000))), nil
return 100 * cput.Total() / totalTime, nil
}

Loading…
Cancel
Save