Skip to content

Commit

Permalink
Prevent underflow with CPU time counter
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony Ramine <nox@nox.paris>
  • Loading branch information
nox committed Oct 20, 2022
1 parent fc591fd commit f1d1524
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/process_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,12 @@ impl Collector for ProcessCollector {
// cpu
let total = (stat.utime + stat.stime) / *CLK_TCK as u64;
let past = self.cpu_total.get();
self.cpu_total.inc_by(total - past);
// If two threads are collecting metrics at the same time,
// the cpu_total counter may have already been updated,
// and the subtraction may underflow.
if let Some(diff) = total.checked_sub(past) {
self.cpu_total.inc_by(diff);
}
cpu_total_mfs = Some(self.cpu_total.collect());

// threads
Expand Down

0 comments on commit f1d1524

Please sign in to comment.