Skip to content

Commit

Permalink
Fix user and kernel times on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
clemenswasser committed Aug 8, 2022
1 parent 2e8ce30 commit 8354ef6
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 3 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ version = "1.14.0"
edition = "2018"
build = "build.rs"

[features]
nightly = []

[dependencies]
colored = "2.0"
indicatif = "0.16"
Expand All @@ -29,7 +32,7 @@ anyhow = "1.0"
libc = "0.2"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["processthreadsapi", "minwindef", "winnt"] }
winapi = { version = "0.3", features = ["processthreadsapi", "minwindef", "winnt", "jobapi2"] }

[target.'cfg(target_os="linux")'.dependencies]
nix = { version = "0.24.2", features = ["zerocopy"] }
Expand Down
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#![cfg_attr(
all(windows, feature = "nightly"),
feature(windows_process_extensions_main_thread_handle)
)]

use std::env;

use benchmark::scheduler::Scheduler;
Expand Down
24 changes: 22 additions & 2 deletions src/timer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
mod wall_clock_timer;

#[cfg(windows)]
#[cfg(all(windows, feature = "nightly"))]
mod windows_timer_nightly;

#[cfg(all(windows, not(feature = "nightly")))]
mod windows_timer;

#[cfg(not(windows))]
Expand All @@ -21,6 +24,7 @@ use std::process::{ChildStdout, Command, ExitStatus};

use anyhow::Result;

#[cfg(any(not(windows), not(feature = "nightly")))]
#[derive(Debug, Copy, Clone)]
struct CPUTimes {
/// Total amount of time spent executing in user mode
Expand Down Expand Up @@ -79,10 +83,26 @@ pub fn execute_and_measure(mut command: Command) -> Result<TimerResult> {
#[cfg(not(windows))]
let cpu_timer = self::unix_timer::CPUTimer::start();

#[cfg(all(windows, feature = "nightly"))]
{
use std::os::windows::process::CommandExt;

// Create a suspended process
command.creation_flags(4);
}

let mut child = command.spawn()?;

#[cfg(windows)]
let cpu_timer = self::windows_timer::CPUTimer::start_for_process(&child);
let cpu_timer = {
// SAFETY: We created a suspended process
#[cfg(feature = "nightly")]
unsafe {
self::windows_timer_nightly::CPUTimer::start_suspended_process(&child)
}
#[cfg(not(feature = "nightly"))]
self::windows_timer::CPUTimer::start_for_process(&child)
};

if let Some(output) = child.stdout.take() {
// Handle CommandOutputPolicy::Pipe
Expand Down
84 changes: 84 additions & 0 deletions src/timer/windows_timer_nightly.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#![cfg(windows)]
#![warn(unsafe_op_in_unsafe_fn)]

use std::{
mem,
os::windows::{io::AsRawHandle, process::ChildExt},
process, ptr,
};

use winapi::um::{
handleapi::CloseHandle,
jobapi2::{AssignProcessToJobObject, CreateJobObjectW, QueryInformationJobObject},
processthreadsapi::ResumeThread,
winnt::{JobObjectBasicAccountingInformation, HANDLE, JOBOBJECT_BASIC_ACCOUNTING_INFORMATION},
};

use crate::util::units::Second;

const HUNDRED_NS_PER_MS: i64 = 10;

pub struct CPUTimer {
job_object: HANDLE,
}

impl CPUTimer {
pub unsafe fn start_suspended_process(child: &process::Child) -> Self {
// SAFETY: Creating a new job object is safe
let job_object = unsafe { CreateJobObjectW(ptr::null_mut(), ptr::null_mut()) };
assert!(!job_object.is_null(), "CreateJobObjectW failed");

// SAFETY: The job object handle is valid
let ret = unsafe { AssignProcessToJobObject(job_object, child.as_raw_handle()) };
assert!(ret != 0, "AssignProcessToJobObject failed");

// SAFETY: The main thread handle is valid
unsafe { ResumeThread(child.main_thread_handle().as_raw_handle()) };

Self { job_object }
}

pub fn stop(&self) -> (Second, Second) {
let mut job_object_info: JOBOBJECT_BASIC_ACCOUNTING_INFORMATION = unsafe { mem::zeroed() };

// SAFETY: A valid job object got created in `start_suspended_process`
let res = unsafe {
QueryInformationJobObject(
self.job_object,
JobObjectBasicAccountingInformation,
ptr::addr_of_mut!(job_object_info).cast(),
mem::size_of_val(&job_object_info) as u32,
ptr::null_mut(),
)
};

assert!(
job_object_info.ActiveProcesses == 0,
"There shouldn't be active child processes left after the benchmarked process exited"
);

if res != 0 {
// SAFETY: The `TotalUserTime` is "The total amount of user-mode execution time for
// all active processes associated with the job, as well as all terminated processes no
// longer associated with the job, in 100-nanosecond ticks." and is safe to extract
let user: i64 = unsafe { job_object_info.TotalUserTime.QuadPart() } / HUNDRED_NS_PER_MS;

// SAFETY: The `TotalKernelTime` is "The total amount of kernel-mode execution time
// for all active processes associated with the job, as well as all terminated
// processes no longer associated with the job, in 100-nanosecond ticks." and is safe
// to extract
let kernel: i64 =
unsafe { job_object_info.TotalKernelTime.QuadPart() } / HUNDRED_NS_PER_MS;
(user as f64 * 1e-6, kernel as f64 * 1e-6)
} else {
(0.0, 0.0)
}
}
}

impl Drop for CPUTimer {
fn drop(self: &mut Self) {
// SAFETY: A valid job object got created in `start_suspended_process`
unsafe { CloseHandle(self.job_object) };
}
}

0 comments on commit 8354ef6

Please sign in to comment.