Skip to content

Commit a11df02

Browse files
authored
Prevent underflow with CPU time counter (#465)
Signed-off-by: Anthony Ramine <[email protected]> Signed-off-by: Anthony Ramine <[email protected]>
1 parent fc591fd commit a11df02

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/process_collector.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,10 @@ impl Collector for ProcessCollector {
165165
// cpu
166166
let total = (stat.utime + stat.stime) / *CLK_TCK as u64;
167167
let past = self.cpu_total.get();
168-
self.cpu_total.inc_by(total - past);
168+
// If two threads are collecting metrics at the same time,
169+
// the cpu_total counter may have already been updated,
170+
// and the subtraction may underflow.
171+
self.cpu_total.inc_by(total.saturating_sub(past));
169172
cpu_total_mfs = Some(self.cpu_total.collect());
170173

171174
// threads

0 commit comments

Comments
 (0)