Skip to content
Myr

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

5 files+72 added−23 removed

modified:src/hyperfine/timer/cputimer.rs+17 added-8 removed
1use hyperfine::internal::Second;2use hyperfine::timer::Timer;34#[cfg(windows)]5use std::os::windows::io::RawHandle;67#[derive(Debug, Copy, Clone)]8struct CPUTimes {9 /// Total amount of time spent executing in user mode10 user_usec: i64,
11 unmodified lines
22 pub system: Second,23}2425/// Read CPU execution times ('user' and 'system')23#[cfg(not(target_os = "windows"))]26#[cfg(not(windows))]27fn get_cpu_times() -> CPUTimes {28 use libc::{getrusage, rusage, RUSAGE_CHILDREN};29 use std::mem;30
16 unmodified lines
4748/// Read CPU execution times (dummy for now)49#[cfg(windows)]50fn get_cpu_times() -> CPUTimes {48 use winapi::um::processthreadsapi::{GetCurrentProcess, GetProcessTimes};51 CPUTimes {52 user_usec : 0, 53 system_usec: 0,54 }55}5657#[cfg(windows)]58pub 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;6263 // Winapi reports times as per 100 nanosecond64 const HUNDRED_NS_PER_MS: i64 = 10;65
14 unmodified lines
80 dwHighDateTime: 0,81 };8283 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 line
9192 // 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 };10392 CPUTimes {93 user_usec,94 system_usec,95 }104 (user_usec,system_usec)105}106107/// Compute the time intervals in between two `CPUTimes` snapshots108fn cpu_time_interval(start: &CPUTimes, end: &CPUTimes) -> CPUInterval {
51 unmodified lines
modified:src/hyperfine/benchmark.rs+38 added-4 removed
1use std::io;2use std::process::ExitStatus;34use colored::*;5use statistical::{mean, standard_deviation};6
3 unmodified lines
10use hyperfine::format::{format_duration, format_duration_unit};11use hyperfine::outlier_detection::{modified_zscores, OUTLIER_THRESHOLD};12use hyperfine::timer::Timer;13use hyperfine::timer::wallclocktimer::WallClockTimer;13use hyperfine::timer::cputimer::CPUTimer;14use hyperfine::timer::cputimer::get_process_times;15use hyperfine::shell::run_shell_command;161718/// Results from timing a single shell command19#[derive(Debug, Copy, Clone)]20pub struct TimingResult {21 /// Wall clock time
21 unmodified lines
43 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();4849 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)?;5152 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();5455 if failure_action == CmdFailureAction::RaiseError && !status.success() {56 return Err(io::Error::new(57 io::ErrorKind::Other,
235 unmodified lines
293 println!(" ");294295 Ok(())296}297298#[cfg(windows)]299fn execute_and_time(command: &str) -> io::Result<(f64, f64, ExitStatus)> {300 use std::os::windows::io::AsRawHandle;301302 let mut child = run_shell_command(command)?;303304 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))314}315316#[cfg(not(windows))]317fn execute_and_time(command: &str) -> io::Result<(f64, f64, ExitStatus)> {318 use hyperfine::timer::cputimer::CPUTimer;319320 let cpu_timer = CPUTimer::start();321322 let status = run_shell_command(command)?;323324 let (mut time_user, mut time_system) = cpu_timer.stop();325326 Ok((time_user, time_system, status))327}
modified:src/hyperfine/shell.rs+11 added-5 removed
1use std::io;2use std::process::{Command, ExitStatus, Stdio};2use std::process::{Command, Stdio};34#[cfg(not(windows))]5use std::process::ExitStatus;67#[cfg(windows)]8use std::process::Child;910/// Run a standard shell command5#[cfg(not(target_os = "windows"))]11#[cfg(not(windows))]12pub fn run_shell_command(command: &str) -> io::Result<ExitStatus> {13 Command::new("sh")14 .arg("-c")15 .arg(command)
3 unmodified lines
19 .status()20}2122/// Run a Windows shell command using cmd.exe17#[cfg(target_os = "windows")]18pub fn run_shell_command(command: &str) -> io::Result<ExitStatus> {23#[cfg(windows)]24pub 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 lines
4extern crate colored;5extern crate indicatif;6extern crate statistical;78#[cfg(not(target_os = "windows"))]8#[cfg(not(windows))]9extern crate libc;1011#[cfg(target_os = "windows")]11#[cfg(windows)]12extern crate winapi;1314#[cfg(test)]15#[macro_use]
132 unmodified lines
148 } else {149 OutputStyleOption::Basic150 },151 };152 152153 if options.output_style == OutputStyleOption::Basic {154 colored::control::set_override(false);155 }156
11 unmodified lines
modified:Cargo.toml+3 added-3 removed