Skip to content

Commit

Permalink
Error out when the given duration is negative (#155)
Browse files Browse the repository at this point in the history
Validate processInfo period & log a fatal error in the case of negative duration.
  • Loading branch information
el10savio committed Nov 10, 2021
1 parent db6b6e9 commit e6cb030
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions main.go
Expand Up @@ -136,6 +136,9 @@ func processes() {
}

func processInfo(pid int, period time.Duration) {
if period < 0 {
log.Fatalf("Cannot determine CPU usage for negative duration %v", period)
}
p, err := process.NewProcess(int32(pid))
if err != nil {
log.Fatalf("Cannot read process info: %v", err)
Expand All @@ -152,10 +155,8 @@ func processInfo(pid int, period time.Duration) {
if v, err := p.CPUPercent(); err == nil {
fmt.Printf("cpu usage:\t%.3f%%\n", 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 := 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 e6cb030

Please sign in to comment.