Skip to content
Myr

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

6 files+18 added−73 removed

modified:src/benchmark/executor.rs-1 removed
3 unmodified lines
4use crate::options::{CmdFailureAction, CommandOutputPolicy, Options, OutputStyleOption, Shell};5use crate::output::progress_bar::get_progress_bar;6use crate::shell::execute_and_time;7use crate::timer::wallclocktimer::WallClockTimer;8use crate::timer::{TimerStart, TimerStop};8use crate::util::units::Second;910use super::timing_result::TimingResult;11
217 unmodified lines
modified:src/timer/mod.rs+2 added-20 removed
1use std::process::Child;21use crate::util::units::Second;23pub mod wallclocktimer;45#[cfg(windows)]8mod windows_timer;9#[cfg(windows)]10pub use self::windows_timer::get_cpu_timer;6pub mod windows_timer;78#[cfg(not(windows))]13mod unix_timer;14#[cfg(not(windows))]15pub use self::unix_timer::get_cpu_timer;9pub mod unix_timer;1017/// Defines start functionality of a timer.18pub trait TimerStart {19 fn start() -> Self;20 fn start_for_process(process: &Child) -> Self;21}2223/// Defines stop functionality of a timer.24pub trait TimerStop {25 type Result;26 fn stop(&self) -> Self::Result;27}2811#[derive(Debug, Copy, Clone)]12pub struct CPUTimes {13 /// Total amount of time spent executing in user mode14 pub user_usec: i64,
13 unmodified lines
modified:src/timer/unix_timer.rs+5 added-18 removed
1#![cfg(not(windows))]23use std::mem;4use std::process::Child;46use crate::timer::{CPUInterval, CPUTimes, TimerStart, TimerStop};5use crate::timer::{CPUInterval, CPUTimes};6use crate::util::units::Second;79pub fn get_cpu_timer() -> Box<dyn TimerStop<Result = (Second, Second)>> {10 Box::new(CPUTimer::start())11}1213struct CPUTimer {8pub struct CPUTimer {9 start_cpu: CPUTimes,10}1117impl TimerStart for CPUTimer {18 fn start() -> Self {12impl CPUTimer {13 pub fn start() -> Self {14 CPUTimer {15 start_cpu: get_cpu_times(),16 }17 }1824 fn start_for_process(_: &Child) -> Self {25 Self::start()26 }27}2829impl TimerStop for CPUTimer {30 type Result = (Second, Second);3132 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 lines
modified:src/timer/wallclocktimer.rs+3 added-13 removed
1use std::process::Child;1use std::time::Instant;24use crate::timer::{TimerStart, TimerStop};3use crate::util::units::Second;45pub struct WallClockTimer {6 start: Instant,7}811impl TimerStart for WallClockTimer {12 fn start() -> WallClockTimer {9impl WallClockTimer {10 pub fn start() -> WallClockTimer {11 WallClockTimer {12 start: Instant::now(),13 }14 }1518 fn start_for_process(_: &Child) -> Self {19 Self::start()20 }21}2223impl TimerStop for WallClockTimer {24 type Result = Second;2526 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 lines
67use winapi::um::processthreadsapi::GetProcessTimes;8use winapi::um::winnt::HANDLE;910use crate::timer::{CPUTimes, TimerStart, TimerStop};10use crate::timer::CPUTimes;11use crate::util::units::Second;1213const HUNDRED_NS_PER_MS: i64 = 10;1415pub fn get_cpu_timer(process: &Child) -> Box<dyn TimerStop<Result = (Second, Second)>> {16 Box::new(CPUTimer::start_for_process(process))17}1819struct CPUTimer {15pub struct CPUTimer {16 handle: RawHandle,17}1823impl TimerStart for CPUTimer {24 fn start() -> Self {25 panic!()26 }2728 fn start_for_process(process: &Child) -> Self {19impl CPUTimer {20 pub fn start_for_process(process: &Child) -> Self {21 CPUTimer {22 handle: process.as_raw_handle(),23 }24 }33}2535impl TimerStop for CPUTimer {36 type Result = (Second, Second);3738 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 lines
4950 // 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 lines
modified:src/shell.rs+2 added-3 removed
1use std::process::{ExitStatus, Stdio};23use crate::options::Shell;4use crate::timer::get_cpu_timer;45use anyhow::{Context, Result};67/// Used to indicate the result of running a command
20 unmodified lines
28 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();3334 let status = shell35 .command()36 .arg("-c")
37 unmodified lines
74 .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()?;8081 let (user_time, system_time) = cpu_timer.stop();82 Ok(ExecuteResult {
5 unmodified lines