Skip to content
Myr

Restructure timer module, split timer types

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Steve Pentland · committed by sharkdp · parent 5da47cd · Browse files · 7 files+207 added−175 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

7 files+207 added−175 removed

  • other:+207 added−175 removed

7 files+207 added−175 removed

deleted:src/hyperfine/timer/cputimer.rs-146 removed
1use hyperfine::internal::Second;2use hyperfine::timer::Timer;34#[cfg(windows)]5use std::os::windows::io::RawHandle;67use std::mem;89#[derive(Debug, Copy, Clone)]10struct CPUTimes {11 /// Total amount of time spent executing in user mode12 user_usec: i64,1314 /// Total amount of time spent executing in kernel mode15 system_usec: i64,16}1718#[derive(Debug, Copy, Clone)]19struct CPUInterval {20 /// Total amount of time spent executing in user mode21 pub user: Second,2223 /// Total amount of time spent executing in kernel mode24 pub system: Second,25}2627/// Read CPU execution times ('user' and 'system')28#[cfg(not(windows))]29fn get_cpu_times() -> CPUTimes {30 use libc::{getrusage, rusage, RUSAGE_CHILDREN};3132 let result: rusage = unsafe {33 let mut buf = mem::zeroed();34 let success = getrusage(RUSAGE_CHILDREN, &mut buf);35 assert_eq!(0, success);36 buf37 };3839 const MICROSEC_PER_SEC: i64 = 1000 * 1000;4041 CPUTimes {42 user_usec: i64::from(result.ru_utime.tv_sec) * MICROSEC_PER_SEC43 + i64::from(result.ru_utime.tv_usec),44 system_usec: i64::from(result.ru_stime.tv_sec) * MICROSEC_PER_SEC45 + i64::from(result.ru_stime.tv_usec),46 }47}4849/// Read CPU execution times (dummy for now)50#[cfg(windows)]51fn get_cpu_times() -> CPUTimes {52 CPUTimes {53 user_usec : 0, 54 system_usec: 0,55 }56}5758#[cfg(windows)]59pub fn get_process_times(handle: RawHandle) -> (i64, i64) {60 use winapi::um::processthreadsapi::GetProcessTimes;61 use winapi::um::winnt::HANDLE;6263 // Winapi reports times as per 100 nanosecond64 const HUNDRED_NS_PER_MS: i64 = 10;6566 let (user_usec, system_usec) = unsafe {67 let mut _ctime = mem::zeroed();68 let mut _etime = mem::zeroed();69 let mut kernel_time = mem::zeroed();70 let mut user_time = mem::zeroed();71 let res = GetProcessTimes(72 handle as HANDLE,73 &mut _ctime,74 &mut _etime,75 &mut kernel_time,76 &mut user_time,77 );7879 // GetProcessTimes will exit with non-zero if success as per: https://msdn.microsoft.com/en-us/library/windows/desktop/ms683223(v=vs.85).aspx80 if res != 0 {81 // Extract times as laid out here: https://support.microsoft.com/en-us/help/188768/info-working-with-the-filetime-structure82 // Both user_time and kernel_time are spans that the proces spent in either. 83 let user: i64 = (((user_time.dwHighDateTime as i64) << 32) + user_time.dwLowDateTime as i64) / HUNDRED_NS_PER_MS;84 let kernel: i64 = (((kernel_time.dwHighDateTime as i64) << 32) + kernel_time.dwLowDateTime as i64) / HUNDRED_NS_PER_MS;85 (user, kernel)86 } else {87 (0, 0)88 }89 };9091 (user_usec,system_usec)92}9394/// Compute the time intervals in between two `CPUTimes` snapshots95fn cpu_time_interval(start: &CPUTimes, end: &CPUTimes) -> CPUInterval {96 CPUInterval {97 user: ((end.user_usec - start.user_usec) as f64) * 1e-6,98 system: ((end.system_usec - start.system_usec) as f64) * 1e-6,99 }100}101102/// A timer that measures system and user time103pub struct CPUTimer {104 start_cpu: CPUTimes,105}106107impl Timer for CPUTimer {108 type Result = (Second, Second);109110 fn start() -> Self {111 CPUTimer {112 start_cpu: get_cpu_times(),113 }114 }115116 fn stop(&self) -> Self::Result {117 let end_cpu = get_cpu_times();118 let cpu_interval = cpu_time_interval(&self.start_cpu, &end_cpu);119 (cpu_interval.user, cpu_interval.system)120 }121}122123#[test]124fn test_cpu_time_interval() {125 let t_a = CPUTimes {126 user_usec: 12345,127 system_usec: 54321,128 };129130 let t_b = CPUTimes {131 user_usec: 20000,132 system_usec: 70000,133 };134135 let t_zero = cpu_time_interval(&t_a, &t_a);136 assert_eq!(0.0, t_zero.user);137 assert_eq!(0.0, t_zero.system);138139 let t_ab = cpu_time_interval(&t_a, &t_b);140 assert_relative_eq!(0.007655, t_ab.user);141 assert_relative_eq!(0.015679, t_ab.system);142143 let t_ba = cpu_time_interval(&t_b, &t_a);144 assert_relative_eq!(-0.007655, t_ba.user);145 assert_relative_eq!(-0.015679, t_ba.system);146}
added:src/hyperfine/timer/internal.rs+19 added
1use hyperfine::internal::Second;23#[derive(Debug, Copy, Clone)]4pub struct CPUTimes {5 /// Total amount of time spent executing in user mode6 pub user_usec: i64,78 /// Total amount of time spent executing in kernel mode9 pub system_usec: i64,10}1112#[derive(Debug, Copy, Clone)]13pub struct CPUInterval {14 /// Total amount of time spent executing in user mode15 pub user: Second,1617 /// Total amount of time spent executing in kernel mode18 pub system: Second,19}
modified:src/hyperfine/timer/mod.rs+8 added-1 removed
1mod internal;23pub mod wallclocktimer;2pub mod cputimer;45#[cfg(not(windows))]6pub mod unix_timer;78#[cfg(windows)]9pub mod windows_timer;1011/// Defines a general timer with start/stop functionality.12pub trait Timer {13 type Result;14
3 unmodified lines
added:src/hyperfine/timer/unix_timer.rs+79 added
1#![cfg(not(windows))]23use super::internal::{CPUInterval, CPUTimes}4use hyperfine::timer::Timer;567pub struct UnixCPUTimer {8 start_cpu: CPUTimes,9}1011impl Timer for UnixCPUTimer {12 type Result = (Second, Second);1314 fn start() -> Self {15 CPUTimer {16 start_cpu: get_cpu_times(),17 }18 }1920 fn stop(&self) -> Self::Result {21 let end_cpu = get_cpu_times();22 let cpu_interval = cpu_time_interval(&self.start_cpu, &end_cpu);23 (cpu_interval.user, cpu_interval.system)24 }25}2627/// Read CPU execution times ('user' and 'system')28fn get_cpu_times() -> CPUTimes {29 use libc::{getrusage, rusage, RUSAGE_CHILDREN};3031 let result: rusage = unsafe {32 let mut buf = mem::zeroed();33 let success = getrusage(RUSAGE_CHILDREN, &mut buf);34 assert_eq!(0, success);35 buf36 };3738 const MICROSEC_PER_SEC: i64 = 1000 * 1000;3940 CPUTimes {41 user_usec: i64::from(result.ru_utime.tv_sec) * MICROSEC_PER_SEC42 + i64::from(result.ru_utime.tv_usec),43 system_usec: i64::from(result.ru_stime.tv_sec) * MICROSEC_PER_SEC44 + i64::from(result.ru_stime.tv_usec),45 }46}4748/// Compute the time intervals in between two `CPUTimes` snapshots49fn cpu_time_interval(start: &CPUTimes, end: &CPUTimes) -> CPUInterval {50 CPUInterval {51 user: ((end.user_usec - start.user_usec) as f64) * 1e-6,52 system: ((end.system_usec - start.system_usec) as f64) * 1e-6,53 }54}5556#[test]57fn test_cpu_time_interval() {58 let t_a = CPUTimes {59 user_usec: 12345,60 system_usec: 54321,61 };6263 let t_b = CPUTimes {64 user_usec: 20000,65 system_usec: 70000,66 };6768 let t_zero = cpu_time_interval(&t_a, &t_a);69 assert_eq!(0.0, t_zero.user);70 assert_eq!(0.0, t_zero.system);7172 let t_ab = cpu_time_interval(&t_a, &t_b);73 assert_relative_eq!(0.007655, t_ab.user);74 assert_relative_eq!(0.015679, t_ab.system);7576 let t_ba = cpu_time_interval(&t_b, &t_a);77 assert_relative_eq!(-0.007655, t_ba.user);78 assert_relative_eq!(-0.015679, t_ba.system);79}
added:src/hyperfine/timer/windows_timer.rs+68 added
1#![cfg(windows)]23use super::internal::CPUTimes;4use hyperfine::internal::Second;56use winapi::um::processthreadsapi::GetProcessTimes;7use winapi::um::winnt::HANDLE;89use std::mem;10use std::os::windows::io::RawHandle;1112const HUNDRED_NS_PER_MS: i64 = 10;1314pub trait WindowsTimer {15 type Result;1617 fn start(handle: RawHandle) -> Self;18 fn stop(&self) -> Self::Result;19}2021pub struct WindowsCPUTimer {22 handle: RawHandle,23}2425impl WindowsTimer for WindowsCPUTimer {26 type Result = (Second, Second);2728 fn start(handle: RawHandle) -> Self {29 WindowsCPUTimer { handle }30 }3132 fn stop(&self) -> Self::Result {33 let times = get_cpu_times(self.handle);34 (times.user_usec as f64 * 1e-6, times.system_usec as f64 * 1e-6)35 }36}3738/// Read CPU execution times (dummy for now)39fn get_cpu_times(handle: RawHandle) -> CPUTimes {40 let (user_usec, system_usec) = unsafe {41 let mut _ctime = mem::zeroed();42 let mut _etime = mem::zeroed();43 let mut kernel_time = mem::zeroed();44 let mut user_time = mem::zeroed();45 let res = GetProcessTimes(46 handle as HANDLE,47 &mut _ctime,48 &mut _etime,49 &mut kernel_time,50 &mut user_time,51 );5253 // GetProcessTimes will exit with non-zero if success as per: https://msdn.microsoft.com/en-us/library/windows/desktop/ms683223(v=vs.85).aspx54 if res != 0 {55 // Extract times as laid out here: https://support.microsoft.com/en-us/help/188768/info-working-with-the-filetime-structure56 // Both user_time and kernel_time are spans that the proces spent in either. 57 let user: i64 = (((user_time.dwHighDateTime as i64) << 32) + user_time.dwLowDateTime as i64) / HUNDRED_NS_PER_MS;58 let kernel: i64 = (((kernel_time.dwHighDateTime as i64) << 32) + kernel_time.dwLowDateTime as i64) / HUNDRED_NS_PER_MS;59 (user, kernel)60 } else {61 (0, 0)62 }63 };6465 CPUTimes {66 user_usec, system_usec,67 }68}
modified:src/hyperfine/benchmark.rs+30 added-21 removed
41 unmodified lines
42 failure_action: CmdFailureAction,43 shell_spawning_time: Option<TimingResult>,44) -> io::Result<(TimingResult, bool)> {45 let wallclock_timer = WallClockTimer::start();46 // let cpu_timer = CPUTimer::start();4648 let (mut time_user, mut time_system, status) = execute_and_time(shell_cmd)?;49 // let status = run_shell_command(shell_cmd)?;47 let result = execute_and_time(shell_cmd)?;4849 let mut time_user = result.user_time;50 let mut time_system = result.system_time;5152 let mut time_real = wallclock_timer.stop();52 // let (mut time_user, mut time_system) = cpu_timer.stop();5354 if failure_action == CmdFailureAction::RaiseError && !status.success() {54 if failure_action == CmdFailureAction::RaiseError && !result.status.success() {55 return Err(io::Error::new(56 io::ErrorKind::Other,57 "Command terminated with non-zero exit code. \58 Use the '-i'/'--ignore-failure' option if you want to ignore this.",
12 unmodified lines
71 time_real,72 time_user,73 time_system,74 },75 status.success(),75 result.status.success(),76 ))77}7879/// Measure the average shell spawning time
213 unmodified lines
293294 Ok(())295}296297#[derive(Debug, Copy, Clone)]298struct ExecuteResult {299 user_time: f64,300 system_time: f64,301 status: ExitStatus,302}303304#[cfg(windows)]298fn execute_and_time(command: &str) -> io::Result<(f64, f64, ExitStatus)> {299 use hyperfine::timer::cputimer::get_process_times;305fn execute_and_time(command: &str) -> io::Result<ExecuteResult> {306 use hyperfine::timer::windows_timer::{WindowsCPUTimer, WindowsTimer};307 use std::os::windows::io::AsRawHandle;308309 let mut child = run_shell_command(command)?;310 let timer = WindowsCPUTimer::start(child.as_raw_handle());311 let status = child.wait()?;312304 let result = child.wait()?;305 let handle = child.as_raw_handle();306307 let (user_usec, system_usec) = get_process_times(handle);308309 // Temp, bypassing timer and the stop method310 let user = user_usec as f64 * 1e-6;311 let system = system_usec as f64 * 1e-6;312313 Ok((user, system, result))313 let (user_time, system_time) = timer.stop();314 Ok(ExecuteResult {315 user_time,316 system_time,317 status,318 })319}320321#[cfg(not(windows))]317fn execute_and_time(command: &str) -> io::Result<(f64, f64, ExitStatus)> {322fn execute_and_time(command: &str) -> io::Result<ExecuteResult> {323 use hyperfine::timer::cputimer::CPUTimer;324325 let cpu_timer = CPUTimer::start();326327 let status = run_shell_command(command)?;328324 let (time_user, time_system) = cpu_timer.stop();329 let (user_time, system_time) = cpu_timer.stop();330326 Ok((time_user, time_system, status))331 Ok(ExecuteResult {332 user_time,333 system_time,334 status,335 })336}
modified:src/hyperfine/shell.rs+3 added-7 removed
1use std;2use std::io;3use std::process::{Command, Stdio};44#[cfg(not(windows))]5use std::process::ExitStatus;57#[cfg(windows)]8use std::process::Child;96/// Run a standard shell command7#[cfg(not(windows))]12pub fn run_shell_command(command: &str) -> io::Result<ExitStatus> {8pub fn run_shell_command(command: &str) -> io::Result<std::process::ExitStatus> {9 Command::new("sh")10 .arg("-c")11 .arg(command)12 .stdin(Stdio::null())
3 unmodified lines
16}1718/// Run a Windows shell command using cmd.exe19#[cfg(windows)]24pub fn run_shell_command(command: &str) -> io::Result<Child> {20pub fn run_shell_command(command: &str) -> io::Result<std::process::Child> {21 Command::new("cmd")22 .arg("/C")23 .arg(command)24 .stdin(Stdio::null())
4 unmodified lines