Skip to content
Myr

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 file

Take it

1#![cfg(not(windows))]23use std::convert::TryFrom;4use 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    }2829    pub fn stop(&self) -> (Second, Second, u64) {30        let end_cpu = get_cpu_times();31        let cpu_interval = cpu_time_interval(&self.start_cpu, &end_cpu);32        (33            cpu_interval.user,34            cpu_interval.system,35            end_cpu.memory_usage_byte,36        )37    }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;5253    // 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    };5960    #[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),66        memory_usage_byte: u64::try_from(max_rss_byte).unwrap_or(0),67    }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,86        memory_usage_byte: 0,87    };8889    let t_b = CPUTimes {90        user_usec: 20000,91        system_usec: 70000,92        memory_usage_byte: 0,93    };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}