Initial windows timing support
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 92a0d4c · Browse files · 4 files·+50 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+50 added−2 removed
- other:+50 added−2 removed
Provenance
- Commit
42635fb- Tree
d85b54c- Author
Steve Pentland <stevepentland@gmail.com>- Committer
sharkdp <davidpeter@web.de>- Parent
92a0d4c
4 files changed+50 added−2 removed
modified:src/hyperfine/timer/cputimer.rs+43 added-2 removed
43 unmodified lines44 45 /// Read CPU execution times (dummy for now)46 #[cfg(windows)]47 fn get_cpu_times() -> CPUTimes {48+ use winapi::um::processthreadsapi::{GetCurrentProcess, GetProcessTimes};49+ use winapi::shared::minwindef::FILETIME;50+51+ // Winapi reports times as per 100 nanosecond52+ const HUNDRED_NS_PER_MS: i64 = 10;53+ let mut _ctime = FILETIME {54+ dwLowDateTime: 0,55+ dwHighDateTime: 0,56+ };57+ let mut _etime = FILETIME {58+ dwLowDateTime: 0,59+ dwHighDateTime: 0,60+ };61+ let mut kernel_time = FILETIME {62+ dwLowDateTime: 0,63+ dwHighDateTime: 0,64+ };65+ let mut user_time = FILETIME {66+ dwLowDateTime: 0,67+ dwHighDateTime: 0,68+ };69+70+ let (user_usec, system_usec) = unsafe {71+ let handle = GetCurrentProcess();72+ let res = GetProcessTimes(73+ handle,74+ &mut _ctime,75+ &mut _etime,76+ &mut kernel_time,77+ &mut user_time,78+ );79+ // Extract times as laid out here: https://support.microsoft.com/en-us/help/188768/info-working-with-the-filetime-structure80+ if res != 0 {81+ let user: i64 = (((user_time.dwHighDateTime as i64) << 32) + user_time.dwLowDateTime as i64) / HUNDRED_NS_PER_MS;82+ let kernel: i64 = (((kernel_time.dwHighDateTime as i64) << 32) + kernel_time.dwLowDateTime as i64) / HUNDRED_NS_PER_MS;83+ (user, kernel)84+ } else {85+ (0, 0)86+ }87+ };88+89 CPUTimes {49- user_usec: 0,50- system_usec: 0,90+ user_usec,91+ system_usec,92 }93 }94 95 /// Compute the time intervals in between two `CPUTimes` snapshots52 unmodified linesmodified:src/main.rs+3 added
6 unmodified lines7 8 #[cfg(not(target_os = "windows"))]9 extern crate libc;10 11+#[cfg(target_os = "windows")]12+extern crate winapi;13+14 #[cfg(test)]15 #[macro_use]16 extern crate approx;17 150 unmodified linesmodified:Cargo.lock+1 added
102 unmodified lines103 "colored 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)",104 "indicatif 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",105 "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)",106 "statistical 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",107+ "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",108 ]109 110 [[package]]111 name = "indicatif"353 unmodified linesmodified:Cargo.toml+3 added
16 unmodified lines17181920212223242526273 unmodified lines16 unmodified lines
[target.'cfg(not(target_os = "windows"))'.dependencies]libc = "0.2"
[target.'cfg(target_os = "windows")'.dependencies]winapi = { version = "0.3.4", features = [ "processthreadsapi", "minwindef", "winnt"] }
[dependencies.clap]version = "2"default-features = falsefeatures = ["suggestions", "color", "wrap_help"]3 unmodified lines