unix_timer.rs
This file
Path
src/timer/unix_timer.rs
106 lines · 2.8 KiB · Rust
Last changed by
Initial work on tracking memory usageDavid Peterbefore adoption6556b2b
History of this fileTake it
The walk stopped after 200 commits: lines older than 0fe17cf point at it.
Bump clap from 3.2.16 to 3.2.20dependabot[bot]before adoption0fe17cf
lines 1–2why, one step back1#![cfg(not(windows))]2Initial work on tracking memory usageDavid Peterbefore adoption6556b2b
line 3why, one step back3use std::convert::TryFrom;Bump clap from 3.2.16 to 3.2.20dependabot[bot]before adoption0fe17cf
lines 4–28why, one step back4use std::mem;56use crate::timer::CPUTimes;7use crate::util::units::Second;89#[derive(Debug, Copy, Clone)]10pub struct CPUInterval {11 /// Total amount of time spent executing in user mode12 pub user: Second,1314 /// Total amount of time spent executing in kernel mode15 pub system: Second,16}1718pub struct CPUTimer {19 start_cpu: CPUTimes,20}2122impl CPUTimer {23 pub fn start() -> Self {24 CPUTimer {25 start_cpu: get_cpu_times(),26 }27 }28Initial work on tracking memory usageDavid Peterbefore adoption6556b2b
line 29why, one step back29 pub fn stop(&self) -> (Second, Second, u64) {Bump clap from 3.2.16 to 3.2.20dependabot[bot]before adoption0fe17cf
lines 30–31why, one step back30 let end_cpu = get_cpu_times();31 let cpu_interval = cpu_time_interval(&self.start_cpu, &end_cpu);Initial work on tracking memory usageDavid Peterbefore adoption6556b2b
lines 32–36why, one step back32 (33 cpu_interval.user,34 cpu_interval.system,35 end_cpu.memory_usage_byte,36 )Bump clap from 3.2.16 to 3.2.20dependabot[bot]before adoption0fe17cf
lines 37–52why, one step back37 }38}3940/// Read CPU execution times ('user' and 'system')41fn get_cpu_times() -> CPUTimes {42 use libc::{getrusage, rusage, RUSAGE_CHILDREN};4344 let result: rusage = unsafe {45 let mut buf = mem::zeroed();46 let success = getrusage(RUSAGE_CHILDREN, &mut buf);47 assert_eq!(0, success);48 buf49 };5051 const MICROSEC_PER_SEC: i64 = 1000 * 1000;52Initial work on tracking memory usageDavid Peterbefore adoption6556b2b
lines 53–59why, one step back53 // Linux and *BSD return the value in KibiBytes, Darwin flavors in bytes54 let max_rss_byte = if cfg!(target_os = "macos") || cfg!(target_os = "ios") {55 result.ru_maxrss56 } else {57 result.ru_maxrss * 102458 };59Bump clap from 3.2.16 to 3.2.20dependabot[bot]before adoption0fe17cf
lines 60–65why, one step back60 #[allow(clippy::useless_conversion)]61 CPUTimes {62 user_usec: i64::from(result.ru_utime.tv_sec) * MICROSEC_PER_SEC63 + i64::from(result.ru_utime.tv_usec),64 system_usec: i64::from(result.ru_stime.tv_sec) * MICROSEC_PER_SEC65 + i64::from(result.ru_stime.tv_usec),Initial work on tracking memory usageDavid Peterbefore adoption6556b2b
line 66why, one step back66 memory_usage_byte: u64::try_from(max_rss_byte).unwrap_or(0),Bump clap from 3.2.16 to 3.2.20dependabot[bot]before adoption0fe17cf
lines 67–85why, one step back67 }68}6970/// Compute the time intervals in between two `CPUTimes` snapshots71fn cpu_time_interval(start: &CPUTimes, end: &CPUTimes) -> CPUInterval {72 CPUInterval {73 user: ((end.user_usec - start.user_usec) as f64) * 1e-6,74 system: ((end.system_usec - start.system_usec) as f64) * 1e-6,75 }76}7778#[cfg(test)]79use approx::assert_relative_eq;8081#[test]82fn test_cpu_time_interval() {83 let t_a = CPUTimes {84 user_usec: 12345,85 system_usec: 54321,Initial work on tracking memory usageDavid Peterbefore adoption6556b2b
line 86why, one step back86 memory_usage_byte: 0,Bump clap from 3.2.16 to 3.2.20dependabot[bot]before adoption0fe17cf
lines 87–91why, one step back87 };8889 let t_b = CPUTimes {90 user_usec: 20000,91 system_usec: 70000,Initial work on tracking memory usageDavid Peterbefore adoption6556b2b
line 92why, one step back92 memory_usage_byte: 0,Bump clap from 3.2.16 to 3.2.20dependabot[bot]before adoption0fe17cf
lines 93–106why, one step back93 };9495 let t_zero = cpu_time_interval(&t_a, &t_a);96 assert!(t_zero.user.abs() < f64::EPSILON);97 assert!(t_zero.system.abs() < f64::EPSILON);9899 let t_ab = cpu_time_interval(&t_a, &t_b);100 assert_relative_eq!(0.007655, t_ab.user);101 assert_relative_eq!(0.015679, t_ab.system);102103 let t_ba = cpu_time_interval(&t_b, &t_a);104 assert_relative_eq!(-0.007655, t_ba.user);105 assert_relative_eq!(-0.015679, t_ba.system);106}