Refactor timer module
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Peter · committed by David Peter · parent f029a46 · Browse files · 6 files·+18 added−73 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+18 added−73 removed
- other:+18 added−73 removed
Provenance
- Commit
defafdf- Tree
e27d4c0- Author
David Peter <mail@david-peter.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
f029a46
6 files changed+18 added−73 removed
modified:src/benchmark/executor.rs-1 removed
3 unmodified lines4 use crate::options::{CmdFailureAction, CommandOutputPolicy, Options, OutputStyleOption, Shell};5 use crate::output::progress_bar::get_progress_bar;6 use crate::shell::execute_and_time;7 use crate::timer::wallclocktimer::WallClockTimer;8-use crate::timer::{TimerStart, TimerStop};8 use crate::util::units::Second;9 10 use super::timing_result::TimingResult;11 217 unmodified linesmodified:src/timer/mod.rs+2 added-20 removed
1-use std::process::Child;2-1 use crate::util::units::Second;2 3 pub mod wallclocktimer;4 5 #[cfg(windows)]8-mod windows_timer;9-#[cfg(windows)]10-pub use self::windows_timer::get_cpu_timer;6+pub mod windows_timer;7 8 #[cfg(not(windows))]13-mod unix_timer;14-#[cfg(not(windows))]15-pub use self::unix_timer::get_cpu_timer;9+pub mod unix_timer;10 17-/// Defines start functionality of a timer.18-pub trait TimerStart {19- fn start() -> Self;20- fn start_for_process(process: &Child) -> Self;21-}22-23-/// Defines stop functionality of a timer.24-pub trait TimerStop {25- type Result;26- fn stop(&self) -> Self::Result;27-}28-11 #[derive(Debug, Copy, Clone)]12 pub struct CPUTimes {13 /// Total amount of time spent executing in user mode14 pub user_usec: i64,13 unmodified linesmodified:src/timer/unix_timer.rs+5 added-18 removed
1 #![cfg(not(windows))]2 3 use std::mem;4-use std::process::Child;4 6-use crate::timer::{CPUInterval, CPUTimes, TimerStart, TimerStop};5+use crate::timer::{CPUInterval, CPUTimes};6 use crate::util::units::Second;7 9-pub fn get_cpu_timer() -> Box<dyn TimerStop<Result = (Second, Second)>> {10- Box::new(CPUTimer::start())11-}12-13-struct CPUTimer {8+pub struct CPUTimer {9 start_cpu: CPUTimes,10 }11 17-impl TimerStart for CPUTimer {18- fn start() -> Self {12+impl CPUTimer {13+ pub fn start() -> Self {14 CPUTimer {15 start_cpu: get_cpu_times(),16 }17 }18 24- fn start_for_process(_: &Child) -> Self {25- Self::start()26- }27-}28-29-impl TimerStop for CPUTimer {30- type Result = (Second, Second);31-32- fn stop(&self) -> Self::Result {19+ pub fn stop(&self) -> (Second, Second) {20 let end_cpu = get_cpu_times();21 let cpu_interval = cpu_time_interval(&self.start_cpu, &end_cpu);22 (cpu_interval.user, cpu_interval.system)23 }59 unmodified linesmodified:src/timer/wallclocktimer.rs+3 added-13 removed
1-use std::process::Child;1 use std::time::Instant;2 4-use crate::timer::{TimerStart, TimerStop};3 use crate::util::units::Second;4 5 pub struct WallClockTimer {6 start: Instant,7 }8 11-impl TimerStart for WallClockTimer {12- fn start() -> WallClockTimer {9+impl WallClockTimer {10+ pub fn start() -> WallClockTimer {11 WallClockTimer {12 start: Instant::now(),13 }14 }15 18- fn start_for_process(_: &Child) -> Self {19- Self::start()20- }21-}22-23-impl TimerStop for WallClockTimer {24- type Result = Second;25-26- fn stop(&self) -> Second {16+ pub fn stop(&self) -> Second {17 let duration = self.start.elapsed();18 duration.as_secs() as f64 + f64::from(duration.subsec_nanos()) * 1e-919 }20 }modified:src/timer/windows_timer.rs+6 added-18 removed
5 unmodified lines6 7 use winapi::um::processthreadsapi::GetProcessTimes;8 use winapi::um::winnt::HANDLE;9 10-use crate::timer::{CPUTimes, TimerStart, TimerStop};10+use crate::timer::CPUTimes;11 use crate::util::units::Second;12 13 const HUNDRED_NS_PER_MS: i64 = 10;14 15-pub fn get_cpu_timer(process: &Child) -> Box<dyn TimerStop<Result = (Second, Second)>> {16- Box::new(CPUTimer::start_for_process(process))17-}18-19-struct CPUTimer {15+pub struct CPUTimer {16 handle: RawHandle,17 }18 23-impl TimerStart for CPUTimer {24- fn start() -> Self {25- panic!()26- }27-28- fn start_for_process(process: &Child) -> Self {19+impl CPUTimer {20+ pub fn start_for_process(process: &Child) -> Self {21 CPUTimer {22 handle: process.as_raw_handle(),23 }24 }33-}25 35-impl TimerStop for CPUTimer {36- type Result = (Second, Second);37-38- fn stop(&self) -> Self::Result {26+ pub fn stop(&self) -> (Second, Second) {27 let times = get_cpu_times(self.handle);28 (29 times.user_usec as f64 * 1e-6,30 times.system_usec as f64 * 1e-6,18 unmodified lines49 50 // GetProcessTimes will exit with non-zero if success as per: https://msdn.microsoft.com/en-us/library/windows/desktop/ms683223(v=vs.85).aspx51 if res != 0 {52 // Extract times as laid out here: https://support.microsoft.com/en-us/help/188768/info-working-with-the-filetime-structure65- // Both user_time and kernel_time are spans that the proces spent in either.53+ // Both user_time and kernel_time are spans that the process spent in either.54 let user: i64 = (((user_time.dwHighDateTime as i64) << 32)55 + user_time.dwLowDateTime as i64)56 / HUNDRED_NS_PER_MS;57 let kernel: i64 = (((kernel_time.dwHighDateTime as i64) << 32)13 unmodified linesmodified:src/shell.rs+2 added-3 removed
1 use std::process::{ExitStatus, Stdio};2 3 use crate::options::Shell;4-use crate::timer::get_cpu_timer;4 5 use anyhow::{Context, Result};6 7 /// Used to indicate the result of running a command20 unmodified lines28 stderr: Stdio,29 command: &str,30 shell: &Shell,31 ) -> Result<ExecuteResult> {33- let cpu_timer = get_cpu_timer();32+ let cpu_timer = crate::timer::unix_timer::CPUTimer::start();33 34 let status = shell35 .command()36 .arg("-c")37 unmodified lines74 .stdout(stdout)75 .stderr(stderr)76 .spawn()77 .with_context(|| format!("Failed to run command '{}'", command))?;79- let cpu_timer = get_cpu_timer(&child);78+ let cpu_timer = crate::timer::windows_timer::CPUTimer::start_for_process(&child);79 let status = child.wait()?;80 81 let (user_time, system_time) = cpu_timer.stop();82 Ok(ExecuteResult {5 unmodified lines