Skip to content

Commit

Permalink
Merge pull request #17811 from klangman/fix-divbyzero-computeThreadCp…
Browse files Browse the repository at this point in the history
…uUtilOverLastNns

Prevent a divide by zero in computeThreadCpuUtilOverLastNns()
  • Loading branch information
dsouzai authored Jul 19, 2023
2 parents ac121a2 + fc05366 commit a2c8e15
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion runtime/compiler/env/CpuUtilization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,6 @@ int32_t CpuSelfThreadUtilization::computeThreadCpuUtilOverLastNns(int64_t validI
int64_t totalCPU = _cpuTimeDuringLastInterval;
int64_t totalTime = _lastIntervalLength;


// The interval between crtTimeNs and lastIntervalEndNs is not accounted for; if this interval
// is larger than the measurement period the thread might have gone to sleep and not
// had a chance to update its CPU utilization. Blindly assume a 0% duty cycle
Expand All @@ -334,6 +333,8 @@ int32_t CpuSelfThreadUtilization::computeThreadCpuUtilOverLastNns(int64_t validI
totalTime += _secondLastIntervalLength;
}
}
if (totalTime == 0)
return -1; // A race condition can defeat our attempts to avoid DivByZero, so we need to check here and bailout rather then cause an exception
return (int32_t)(100 * totalCPU / totalTime);
}
}
Expand Down

0 comments on commit a2c8e15

Please sign in to comment.