Skip to content

Commit

Permalink
Only report CPU percentage per period if period is > 0
Browse files Browse the repository at this point in the history
The change in #154 lead to the CPU usage being reported for a 0s
duration:

cpu usage (0s):	NaN%

Fix this by only reporting CPU usage per period if the user specified a
positive duration.
  • Loading branch information
tklauser committed Jul 8, 2022
1 parent c14dce9 commit 9f82804
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,10 @@ func processInfo(pid int, period time.Duration) {
if v, err := p.CPUPercent(); err == nil {
fmt.Printf("cpu usage:\t%.3f%%\n", v)
}
if v, err := cpuPercentWithinTime(p, period); err == nil {
fmt.Printf("cpu usage (%v):\t%.3f%%\n", period, v)
if period > 0 {
if v, err := cpuPercentWithinTime(p, period); err == nil {
fmt.Printf("cpu usage (%v):\t%.3f%%\n", period, v)
}
}
if v, err := p.Username(); err == nil {
fmt.Printf("username:\t%v\n", v)
Expand Down

0 comments on commit 9f82804

Please sign in to comment.