Skip to content
Myr

unix_timer.rs

This file

Path

src/timer/unix_timer.rs

91 lines · 2.3 KiB · Rust

Last changed by

Move to timer moduleDavid Peterbefore adoption0ffc6d7

History of this file

Take it

Flat directory/module structureDavid Peterbefore adoption30c381f

lines 1–2why, one step back
1#![cfg(not(windows))]
2

Re-order importsDavid Peterbefore adoption9df6174

lines 3–4why, one step back
3use std::mem;
4

Move to timer moduleDavid Peterbefore adoption0ffc6d7

line 5why, one step back
5use crate::timer::CPUTimes;

Move a few modules to utilDavid Peterbefore adoption5c634c9

line 6why, one step back
6use crate::util::units::Second;

Move to timer moduleDavid Peterbefore adoption0ffc6d7

lines 7–15why, one step back
7
8#[derive(Debug, Copy, Clone)]9pub struct CPUInterval {10 /// Total amount of time spent executing in user mode11 pub user: Second,1213 /// Total amount of time spent executing in kernel mode14 pub system: Second,15}

Flat directory/module structureDavid Peterbefore adoption30c381f

line 16why, one step back
16

Refactor timer moduleDavid Peterbefore adoptiondefafdf

line 17why, one step back
17pub struct CPUTimer {

Flat directory/module structureDavid Peterbefore adoption30c381f

lines 18–20why, one step back
18 start_cpu: CPUTimes,
19}20

Refactor timer moduleDavid Peterbefore adoptiondefafdf

lines 21–22why, one step back
21impl CPUTimer {
22 pub fn start() -> Self {

Flat directory/module structureDavid Peterbefore adoption30c381f

lines 23–27why, one step back
23 CPUTimer {
24 start_cpu: get_cpu_times(),25 }26 }27

Refactor timer moduleDavid Peterbefore adoptiondefafdf

line 28why, one step back
28 pub fn stop(&self) -> (Second, Second) {

Flat directory/module structureDavid Peterbefore adoption30c381f

lines 29–91why, one step back
29 let end_cpu = get_cpu_times();
30 let cpu_interval = cpu_time_interval(&self.start_cpu, &end_cpu);31 (cpu_interval.user, cpu_interval.system)32 }33}3435/// Read CPU execution times ('user' and 'system')36fn get_cpu_times() -> CPUTimes {37 use libc::{getrusage, rusage, RUSAGE_CHILDREN};3839 let result: rusage = unsafe {40 let mut buf = mem::zeroed();41 let success = getrusage(RUSAGE_CHILDREN, &mut buf);42 assert_eq!(0, success);43 buf44 };4546 const MICROSEC_PER_SEC: i64 = 1000 * 1000;4748 #[allow(clippy::useless_conversion)]49 CPUTimes {50 user_usec: i64::from(result.ru_utime.tv_sec) * MICROSEC_PER_SEC51 + i64::from(result.ru_utime.tv_usec),52 system_usec: i64::from(result.ru_stime.tv_sec) * MICROSEC_PER_SEC53 + i64::from(result.ru_stime.tv_usec),54 }55}5657/// Compute the time intervals in between two `CPUTimes` snapshots58fn cpu_time_interval(start: &CPUTimes, end: &CPUTimes) -> CPUInterval {59 CPUInterval {60 user: ((end.user_usec - start.user_usec) as f64) * 1e-6,61 system: ((end.system_usec - start.system_usec) as f64) * 1e-6,62 }63}6465#[cfg(test)]66use approx::assert_relative_eq;6768#[test]69fn test_cpu_time_interval() {70 let t_a = CPUTimes {71 user_usec: 12345,72 system_usec: 54321,73 };7475 let t_b = CPUTimes {76 user_usec: 20000,77 system_usec: 70000,78 };7980 let t_zero = cpu_time_interval(&t_a, &t_a);81 assert!(t_zero.user.abs() < f64::EPSILON);82 assert!(t_zero.system.abs() < f64::EPSILON);8384 let t_ab = cpu_time_interval(&t_a, &t_b);85 assert_relative_eq!(0.007655, t_ab.user);86 assert_relative_eq!(0.015679, t_ab.system);8788 let t_ba = cpu_time_interval(&t_b, &t_a);89 assert_relative_eq!(-0.007655, t_ba.user);90 assert_relative_eq!(-0.015679, t_ba.system);91}