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
Provenance
- Commit
1ce265e- Tree
eddbab6- Author
Steve Pentland <stevepentland@gmail.com>- Committer
sharkdp <davidpeter@web.de>- Parent
5da47cd
7 files changed+207 added−175 removed
deleted:src/hyperfine/timer/cputimer.rs-146 removed
1-use hyperfine::internal::Second;2-use hyperfine::timer::Timer;3-4-#[cfg(windows)]5-use std::os::windows::io::RawHandle;6-7-use std::mem;8-9-#[derive(Debug, Copy, Clone)]10-struct CPUTimes {11- /// Total amount of time spent executing in user mode12- user_usec: i64,13-14- /// Total amount of time spent executing in kernel mode15- system_usec: i64,16-}17-18-#[derive(Debug, Copy, Clone)]19-struct CPUInterval {20- /// Total amount of time spent executing in user mode21- pub user: Second,22-23- /// Total amount of time spent executing in kernel mode24- pub system: Second,25-}26-27-/// Read CPU execution times ('user' and 'system')28-#[cfg(not(windows))]29-fn get_cpu_times() -> CPUTimes {30- use libc::{getrusage, rusage, RUSAGE_CHILDREN};31-32- 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- };38-39- const MICROSEC_PER_SEC: i64 = 1000 * 1000;40-41- 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-}48-49-/// Read CPU execution times (dummy for now)50-#[cfg(windows)]51-fn get_cpu_times() -> CPUTimes {52- CPUTimes {53- user_usec : 0, 54- system_usec: 0,55- }56-}57-58-#[cfg(windows)]59-pub fn get_process_times(handle: RawHandle) -> (i64, i64) {60- use winapi::um::processthreadsapi::GetProcessTimes;61- use winapi::um::winnt::HANDLE;62-63- // Winapi reports times as per 100 nanosecond64- const HUNDRED_NS_PER_MS: i64 = 10;65-66- 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- );78-79- // 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- };90-91- (user_usec,system_usec)92-}93-94-/// Compute the time intervals in between two `CPUTimes` snapshots95-fn 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-}101-102-/// A timer that measures system and user time103-pub struct CPUTimer {104- start_cpu: CPUTimes,105-}106-107-impl Timer for CPUTimer {108- type Result = (Second, Second);109-110- fn start() -> Self {111- CPUTimer {112- start_cpu: get_cpu_times(),113- }114- }115-116- 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-}122-123-#[test]124-fn test_cpu_time_interval() {125- let t_a = CPUTimes {126- user_usec: 12345,127- system_usec: 54321,128- };129-130- let t_b = CPUTimes {131- user_usec: 20000,132- system_usec: 70000,133- };134-135- 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);138-139- 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);142-143- 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
1+use hyperfine::internal::Second;2+3+#[derive(Debug, Copy, Clone)]4+pub struct CPUTimes {5+ /// Total amount of time spent executing in user mode6+ pub user_usec: i64,7+8+ /// Total amount of time spent executing in kernel mode9+ pub system_usec: i64,10+}11+12+#[derive(Debug, Copy, Clone)]13+pub struct CPUInterval {14+ /// Total amount of time spent executing in user mode15+ pub user: Second,16+17+ /// Total amount of time spent executing in kernel mode18+ pub system: Second,19+}modified:src/hyperfine/timer/mod.rs+8 added-1 removed
1+mod internal;2+3 pub mod wallclocktimer;2-pub mod cputimer;4 5+#[cfg(not(windows))]6+pub mod unix_timer;7+8+#[cfg(windows)]9+pub mod windows_timer;10+11 /// Defines a general timer with start/stop functionality.12 pub trait Timer {13 type Result;14 3 unmodified linesadded:src/hyperfine/timer/unix_timer.rs+79 added
1+#![cfg(not(windows))]2+3+use super::internal::{CPUInterval, CPUTimes}4+use hyperfine::timer::Timer;5+6+7+pub struct UnixCPUTimer {8+ start_cpu: CPUTimes,9+}10+11+impl Timer for UnixCPUTimer {12+ type Result = (Second, Second);13+14+ fn start() -> Self {15+ CPUTimer {16+ start_cpu: get_cpu_times(),17+ }18+ }19+20+ 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+}26+27+/// Read CPU execution times ('user' and 'system')28+fn get_cpu_times() -> CPUTimes {29+ use libc::{getrusage, rusage, RUSAGE_CHILDREN};30+31+ 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+ };37+38+ const MICROSEC_PER_SEC: i64 = 1000 * 1000;39+40+ 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+}47+48+/// Compute the time intervals in between two `CPUTimes` snapshots49+fn 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+}55+56+#[test]57+fn test_cpu_time_interval() {58+ let t_a = CPUTimes {59+ user_usec: 12345,60+ system_usec: 54321,61+ };62+63+ let t_b = CPUTimes {64+ user_usec: 20000,65+ system_usec: 70000,66+ };67+68+ 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);71+72+ 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);75+76+ 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)]2+3+use super::internal::CPUTimes;4+use hyperfine::internal::Second;5+6+use winapi::um::processthreadsapi::GetProcessTimes;7+use winapi::um::winnt::HANDLE;8+9+use std::mem;10+use std::os::windows::io::RawHandle;11+12+const HUNDRED_NS_PER_MS: i64 = 10;13+14+pub trait WindowsTimer {15+ type Result;16+17+ fn start(handle: RawHandle) -> Self;18+ fn stop(&self) -> Self::Result;19+}20+21+pub struct WindowsCPUTimer {22+ handle: RawHandle,23+}24+25+impl WindowsTimer for WindowsCPUTimer {26+ type Result = (Second, Second);27+28+ fn start(handle: RawHandle) -> Self {29+ WindowsCPUTimer { handle }30+ }31+32+ 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+}37+38+/// Read CPU execution times (dummy for now)39+fn 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+ );52+53+ // 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+ };64+65+ CPUTimes {66+ user_usec, system_usec,67+ }68+}modified:src/hyperfine/benchmark.rs+30 added-21 removed
41 unmodified lines42 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();46 48- 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)?;48 49+ let mut time_user = result.user_time;50+ let mut time_system = result.system_time;51+52 let mut time_real = wallclock_timer.stop();52- // let (mut time_user, mut time_system) = cpu_timer.stop();53 54- 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 lines71 time_real,72 time_user,73 time_system,74 },75- status.success(),75+ result.status.success(),76 ))77 }78 79 /// Measure the average shell spawning time213 unmodified lines293 294 Ok(())295 }296 297+#[derive(Debug, Copy, Clone)]298+struct ExecuteResult {299+ user_time: f64,300+ system_time: f64,301+ status: ExitStatus,302+}303+304 #[cfg(windows)]298-fn execute_and_time(command: &str) -> io::Result<(f64, f64, ExitStatus)> {299- use hyperfine::timer::cputimer::get_process_times;305+fn execute_and_time(command: &str) -> io::Result<ExecuteResult> {306+ use hyperfine::timer::windows_timer::{WindowsCPUTimer, WindowsTimer};307 use std::os::windows::io::AsRawHandle;308 309 let mut child = run_shell_command(command)?;310+ let timer = WindowsCPUTimer::start(child.as_raw_handle());311+ let status = child.wait()?;312 304- let result = child.wait()?;305- let handle = child.as_raw_handle();306-307- let (user_usec, system_usec) = get_process_times(handle);308-309- // Temp, bypassing timer and the stop method310- let user = user_usec as f64 * 1e-6;311- let system = system_usec as f64 * 1e-6;312-313- 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 }320 321 #[cfg(not(windows))]317-fn execute_and_time(command: &str) -> io::Result<(f64, f64, ExitStatus)> {322+fn execute_and_time(command: &str) -> io::Result<ExecuteResult> {323 use hyperfine::timer::cputimer::CPUTimer;324 325 let cpu_timer = CPUTimer::start();326 327 let status = run_shell_command(command)?;328 324- let (time_user, time_system) = cpu_timer.stop();329+ let (user_time, system_time) = cpu_timer.stop();330 326- 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
1+use std;2 use std::io;3 use std::process::{Command, Stdio};4 4-#[cfg(not(windows))]5-use std::process::ExitStatus;5 7-#[cfg(windows)]8-use std::process::Child;9-6 /// Run a standard shell command7 #[cfg(not(windows))]12-pub fn run_shell_command(command: &str) -> io::Result<ExitStatus> {8+pub 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 lines16 }17 18 /// Run a Windows shell command using cmd.exe19 #[cfg(windows)]24-pub fn run_shell_command(command: &str) -> io::Result<Child> {20+pub 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