in rd-agent/src/report.rs [297:374]
fn update(&mut self) -> Result<BTreeMap<String, UsageReport>> {
let mut reps = BTreeMap::new();
let now = Instant::now();
let (usages, cpu_total) = self.read_usages()?;
let dur = now.duration_since(self.at).as_secs_f64();
let zero_usage = Usage::default();
for (unit, cur) in usages.iter() {
let mut rep: UsageReport = Default::default();
let last = self.usages.get(unit).unwrap_or(&zero_usage);
let cpu_total_delta = cpu_total - self.cpu_total;
if cpu_total_delta > 0.0 {
rep.cpu_util = ((cur.cpu_busy - last.cpu_busy) / cpu_total_delta)
.min(1.0)
.max(0.0);
rep.cpu_sys = ((cur.cpu_sys - last.cpu_sys) / cpu_total_delta)
.min(1.0)
.max(0.0);
}
rep.cpu_usage = cur.cpu_busy;
rep.cpu_usage_sys = cur.cpu_sys;
rep.cpu_usage_base = cpu_total;
rep.mem_bytes = cur.mem_bytes;
rep.swap_bytes = cur.swap_bytes;
rep.swap_free = cur.swap_free;
rep.io_rbytes = cur.io_rbytes;
rep.io_wbytes = cur.io_wbytes;
if dur > 0.0 {
if cur.io_rbytes >= last.io_rbytes {
rep.io_rbps = ((cur.io_rbytes - last.io_rbytes) as f64 / dur).round() as u64;
}
if cur.io_wbytes >= last.io_wbytes {
rep.io_wbps = ((cur.io_wbytes - last.io_wbytes) as f64 / dur).round() as u64;
}
rep.io_util = (cur.io_usage - last.io_usage) as f64 / 1_000_000.0 / dur;
rep.io_usage = cur.io_usage as f64 / 1_000_000.0;
rep.cpu_stalls = cur.cpu_stalls;
rep.mem_stalls = cur.mem_stalls;
rep.io_stalls = cur.io_stalls;
rep.cpu_pressures = (
((cur.cpu_stalls.0 - last.cpu_stalls.0) / dur)
.min(1.0)
.max(0.0),
((cur.cpu_stalls.1 - last.cpu_stalls.1) / dur)
.min(1.0)
.max(0.0),
);
rep.mem_pressures = (
((cur.mem_stalls.0 - last.mem_stalls.0) / dur)
.min(1.0)
.max(0.0),
((cur.mem_stalls.1 - last.mem_stalls.1) / dur)
.min(1.0)
.max(0.0),
);
rep.io_pressures = (
((cur.io_stalls.0 - last.io_stalls.0) / dur)
.min(1.0)
.max(0.0),
((cur.io_stalls.1 - last.io_stalls.1) / dur)
.min(1.0)
.max(0.0),
);
}
reps.insert(unit.into(), rep);
}
self.at = now;
self.cpu_total = cpu_total;
self.usages = usages;
Ok(reps)
}