Skip to content

Commit

Permalink
Prevent underflow with CPU time counter (#465)
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony Ramine <nox@nox.paris>

Signed-off-by: Anthony Ramine <nox@nox.paris>
  • Loading branch information
nox committed Oct 20, 2022
1 parent fc591fd commit a11df02
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/process_collector.rs
Expand Up @@ -165,7 +165,10 @@ 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.
self.cpu_total.inc_by(total.saturating_sub(past));
cpu_total_mfs = Some(self.cpu_total.collect());

// threads
Expand Down

0 comments on commit a11df02

Please sign in to comment.