Initial working version with child proc HANDLE.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Still very ugly, and more work needs to be done to make it all pretty again. Additionally, cleaned up all the cfg attrs to shor version
authored by Steve Pentland · committed by sharkdp · parent 79c375a · Browse files · 5 files·+72 added−23 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+72 added−23 removed
- other:+72 added−23 removed
Provenance
- Commit
9753eed- Tree
f564003- Author
Steve Pentland <stevepentland@gmail.com>- Committer
sharkdp <davidpeter@web.de>- Parent
79c375a
5 files changed+72 added−23 removed
modified:src/hyperfine/timer/cputimer.rs+17 added-8 removed
1 use hyperfine::internal::Second;2 use hyperfine::timer::Timer;3 4+#[cfg(windows)]5+use std::os::windows::io::RawHandle;6+7 #[derive(Debug, Copy, Clone)]8 struct CPUTimes {9 /// Total amount of time spent executing in user mode10 user_usec: i64,11 unmodified lines22 pub system: Second,23 }24 25 /// Read CPU execution times ('user' and 'system')23-#[cfg(not(target_os = "windows"))]26+#[cfg(not(windows))]27 fn get_cpu_times() -> CPUTimes {28 use libc::{getrusage, rusage, RUSAGE_CHILDREN};29 use std::mem;30 16 unmodified lines47 48 /// Read CPU execution times (dummy for now)49 #[cfg(windows)]50 fn get_cpu_times() -> CPUTimes {48- use winapi::um::processthreadsapi::{GetCurrentProcess, GetProcessTimes};51+ CPUTimes {52+ user_usec : 0, 53+ system_usec: 0,54+ }55+}56+57+#[cfg(windows)]58+pub fn get_process_times(handle: RawHandle) -> (i64, i64) {59+ use winapi::um::processthreadsapi::GetProcessTimes;60 use winapi::shared::minwindef::FILETIME;61+ use winapi::um::winnt::HANDLE;62 63 // Winapi reports times as per 100 nanosecond64 const HUNDRED_NS_PER_MS: i64 = 10;65 14 unmodified lines80 dwHighDateTime: 0,81 };82 83 let (user_usec, system_usec) = unsafe {72- let handle = GetCurrentProcess();84 let res = GetProcessTimes(74- handle,85+ handle as HANDLE,86 &mut _ctime,87 &mut _etime,88 &mut kernel_time,89 &mut user_time,1 unmodified line91 92 // GetProcessTimes will exit with non-zero if success as per: https://msdn.microsoft.com/en-us/library/windows/desktop/ms683223(v=vs.85).aspx93 if res != 0 {94 // Extract times as laid out here: https://support.microsoft.com/en-us/help/188768/info-working-with-the-filetime-structure95+ // Both user_time and kernel_time are spans that the proces spent in either. 96 let user: i64 = (((user_time.dwHighDateTime as i64) << 32) + user_time.dwLowDateTime as i64) / HUNDRED_NS_PER_MS;97 let kernel: i64 = (((kernel_time.dwHighDateTime as i64) << 32) + kernel_time.dwLowDateTime as i64) / HUNDRED_NS_PER_MS;98 (user, kernel)99 } else {100 (0, 0)101 }102 };103 92- CPUTimes {93- user_usec,94- system_usec,95- }104+ (user_usec,system_usec)105 }106 107 /// Compute the time intervals in between two `CPUTimes` snapshots108 fn cpu_time_interval(start: &CPUTimes, end: &CPUTimes) -> CPUInterval {51 unmodified linesmodified:src/hyperfine/benchmark.rs+38 added-4 removed
1 use std::io;2+use std::process::ExitStatus;3 4 use colored::*;5 use statistical::{mean, standard_deviation};6 3 unmodified lines10 use hyperfine::format::{format_duration, format_duration_unit};11 use hyperfine::outlier_detection::{modified_zscores, OUTLIER_THRESHOLD};12 use hyperfine::timer::Timer;13 use hyperfine::timer::wallclocktimer::WallClockTimer;13-use hyperfine::timer::cputimer::CPUTimer;14+use hyperfine::timer::cputimer::get_process_times;15 use hyperfine::shell::run_shell_command;16 17+18 /// Results from timing a single shell command19 #[derive(Debug, Copy, Clone)]20 pub struct TimingResult {21 /// Wall clock time21 unmodified lines43 failure_action: CmdFailureAction,44 shell_spawning_time: Option<TimingResult>,45 ) -> io::Result<(TimingResult, bool)> {46 let wallclock_timer = WallClockTimer::start();45- let cpu_timer = CPUTimer::start();47+ // let cpu_timer = CPUTimer::start();48 49+ let (mut time_user, mut time_system, status) = execute_and_time(shell_cmd)?;47- let status = run_shell_command(shell_cmd)?;50+ // let status = run_shell_command(shell_cmd)?;51 52 let mut time_real = wallclock_timer.stop();50- let (mut time_user, mut time_system) = cpu_timer.stop();53+ // let (mut time_user, mut time_system) = cpu_timer.stop();54 55 if failure_action == CmdFailureAction::RaiseError && !status.success() {56 return Err(io::Error::new(57 io::ErrorKind::Other,235 unmodified lines293 println!(" ");294 295 Ok(())296 }297+298+#[cfg(windows)]299+fn execute_and_time(command: &str) -> io::Result<(f64, f64, ExitStatus)> {300+ use std::os::windows::io::AsRawHandle;301+302+ let mut child = run_shell_command(command)?;303+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))314+}315+316+#[cfg(not(windows))]317+fn execute_and_time(command: &str) -> io::Result<(f64, f64, ExitStatus)> {318+ use hyperfine::timer::cputimer::CPUTimer;319+320+ let cpu_timer = CPUTimer::start();321+322+ let status = run_shell_command(command)?;323+324+ let (mut time_user, mut time_system) = cpu_timer.stop();325+326+ Ok((time_user, time_system, status))327+}modified:src/hyperfine/shell.rs+11 added-5 removed
1 use std::io;2-use std::process::{Command, ExitStatus, Stdio};2+use std::process::{Command, Stdio};3 4+#[cfg(not(windows))]5+use std::process::ExitStatus;6+7+#[cfg(windows)]8+use std::process::Child;9+10 /// Run a standard shell command5-#[cfg(not(target_os = "windows"))]11+#[cfg(not(windows))]12 pub fn run_shell_command(command: &str) -> io::Result<ExitStatus> {13 Command::new("sh")14 .arg("-c")15 .arg(command)3 unmodified lines19 .status()20 }21 22 /// Run a Windows shell command using cmd.exe17-#[cfg(target_os = "windows")]18-pub fn run_shell_command(command: &str) -> io::Result<ExitStatus> {23+#[cfg(windows)]24+pub fn run_shell_command(command: &str) -> io::Result<Child> {25 Command::new("cmd")26 .arg("/C")27 .arg(command)28 .stdin(Stdio::null())29 .stdout(Stdio::null())30 .stderr(Stdio::null())25- .status()31+ .spawn()32 }modified:src/main.rs+3 added-3 removed
3 unmodified lines4 extern crate colored;5 extern crate indicatif;6 extern crate statistical;7 8-#[cfg(not(target_os = "windows"))]8+#[cfg(not(windows))]9 extern crate libc;10 11-#[cfg(target_os = "windows")]11+#[cfg(windows)]12 extern crate winapi;13 14 #[cfg(test)]15 #[macro_use]132 unmodified lines148 } else {149 OutputStyleOption::Basic150 },151 };152- 152+153 if options.output_style == OutputStyleOption::Basic {154 colored::control::set_override(false);155 }156 11 unmodified linesmodified:Cargo.toml+3 added-3 removed
13 unmodified lines141516171818192021222122232425264 unmodified lines13 unmodified linesindicatif = "0.9"statistical = "0.1"atty = "0.2.2"
[target.'cfg(not(target_os = "windows"))'.dependencies][target.'cfg(not(windows))'.dependencies]libc = "0.2"
[target.'cfg(target_os = "windows")'.dependencies]winapi = { version = "0.3.4", features = [ "processthreadsapi", "minwindef"] }[target.'cfg(windows)'.dependencies]winapi = { version = "0.3.4", features = ["processthreadsapi", "minwindef", "winnt"] }
[dependencies.clap]version = "2"default-features = false4 unmodified lines