Skip to content

Commit e5c3967

Browse files
Show correct CPU usage when the process is terminated early (#2445)
When the process is terminated its CPU time calculation could be incorrect since gopsutil sometimes returns CPU times that are lower than previously returned times. This was resulting in negative CPU time delta, which was then converted to uint32 percent resulting in overflowing of the negative number into large positive number and very large incorrect CPU percentages shown. This changes fixes the problem by ensuring the calculated CPU time delta is never negative.
1 parent e479242 commit e5c3967

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

testbed/testbed/child_process.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,10 @@ func (cp *ChildProcess) fetchCPUUsage() {
425425
// Calculate elapsed and process CPU time deltas in seconds
426426
deltaElapsedTime := now.Sub(cp.lastElapsedTime).Seconds()
427427
deltaCPUTime := times.Total() - cp.lastProcessTimes.Total()
428+
if deltaCPUTime < 0 {
429+
// We sometimes get negative difference when the process is terminated.
430+
deltaCPUTime = 0
431+
}
428432

429433
cp.lastProcessTimes = times
430434
cp.lastElapsedTime = now

0 commit comments

Comments
 (0)