Skip to content

Commit

Permalink
Prevent underflow with CPU time counter
Browse files Browse the repository at this point in the history
  • Loading branch information
nox committed Oct 18, 2022
1 parent fc591fd commit ccf534c
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 total < past {
self.cpu_total.inc_by(total - past);
}
cpu_total_mfs = Some(self.cpu_total.collect());

// threads
Expand Down

0 comments on commit ccf534c

Please sign in to comment.