Files
hyperfine/src/timer/windows_timer.rs
T

131 lines
5.1 KiB
Rust
Raw Normal View History

2018-02-05 18:22:59 -05:00
#![cfg(windows)]
2022-08-08 16:37:39 +02:00
#![warn(unsafe_op_in_unsafe_fn)]
2018-02-05 18:22:59 -05:00
2026-10-05 13:51:06 +02:00
use std::process::{self, Child, ExitStatus};
use std::{mem, os::windows::io::AsRawHandle, ptr};
use anyhow::Result;
2018-02-05 18:22:59 -05:00
2023-04-17 22:12:48 +02:00
use windows_sys::Win32::{
Foundation::{CloseHandle, HANDLE},
System::JobObjects::{
AssignProcessToJobObject, CreateJobObjectW, JobObjectBasicAccountingInformation,
QueryInformationJobObject, JOBOBJECT_BASIC_ACCOUNTING_INFORMATION,
2022-08-08 16:37:39 +02:00
},
};
2023-04-17 22:12:48 +02:00
#[cfg(feature = "windows_process_extensions_main_thread_handle")]
use std::os::windows::process::ChildExt;
#[cfg(feature = "windows_process_extensions_main_thread_handle")]
use windows_sys::Win32::System::Threading::ResumeThread;
2021-08-24 21:33:48 +02:00
2023-04-17 22:12:48 +02:00
#[cfg(not(feature = "windows_process_extensions_main_thread_handle"))]
2022-09-06 22:43:51 +02:00
use once_cell::sync::Lazy;
2023-04-17 22:12:48 +02:00
#[cfg(not(feature = "windows_process_extensions_main_thread_handle"))]
use windows_sys::{
s, w,
Win32::{
Foundation::{NTSTATUS, STATUS_SUCCESS},
System::LibraryLoader::{GetModuleHandleW, GetProcAddress},
},
};
2022-09-06 22:43:51 +02:00
2026-10-05 14:17:40 +02:00
use crate::benchmark::quantity::{Information, InformationQuantity, Time, TimeQuantity};
2018-02-05 18:22:59 -05:00
2023-04-17 22:12:48 +02:00
#[cfg(not(feature = "windows_process_extensions_main_thread_handle"))]
2022-08-08 16:37:39 +02:00
#[allow(non_upper_case_globals)]
2026-10-05 14:07:39 +02:00
static NtResumeProcess: Lazy<unsafe extern "system" fn(process_handle: HANDLE) -> NTSTATUS> =
2022-08-08 16:37:39 +02:00
Lazy::new(|| {
// SAFETY: Getting the module handle for ntdll.dll is safe
2023-04-17 22:12:48 +02:00
let ntdll = unsafe { GetModuleHandleW(w!("ntdll.dll")) };
2026-10-05 14:07:39 +02:00
assert!(!ntdll.is_null(), "GetModuleHandleW failed");
2022-08-08 16:37:39 +02:00
// SAFETY: The ntdll handle is valid
2023-04-17 22:12:48 +02:00
let nt_resume_process = unsafe { GetProcAddress(ntdll, s!("NtResumeProcess")) };
2022-08-08 16:37:39 +02:00
// SAFETY: We transmute to the correct function signature
2023-04-17 22:12:48 +02:00
unsafe { mem::transmute(nt_resume_process.unwrap()) }
2022-08-08 16:37:39 +02:00
});
2022-02-22 10:29:58 +01:00
pub struct CPUTimer {
2022-08-08 16:37:39 +02:00
job_object: HANDLE,
2018-02-05 18:22:59 -05:00
}
2022-02-22 10:29:58 +01:00
impl CPUTimer {
2022-08-08 16:37:39 +02:00
pub unsafe fn start_suspended_process(child: &process::Child) -> Self {
2023-04-17 22:12:48 +02:00
let child_handle = child.as_raw_handle() as HANDLE;
2023-04-14 18:56:24 +02:00
2022-08-08 16:37:39 +02:00
// SAFETY: Creating a new job object is safe
let job_object = unsafe { CreateJobObjectW(ptr::null_mut(), ptr::null_mut()) };
2026-10-05 14:07:39 +02:00
assert!(!job_object.is_null(), "CreateJobObjectW failed");
2022-08-08 16:37:39 +02:00
// SAFETY: The job object handle is valid
2023-04-14 18:56:24 +02:00
let ret = unsafe { AssignProcessToJobObject(job_object, child_handle) };
2022-08-08 16:37:39 +02:00
assert!(ret != 0, "AssignProcessToJobObject failed");
2023-04-17 22:12:48 +02:00
#[cfg(feature = "windows_process_extensions_main_thread_handle")]
2022-08-08 16:37:39 +02:00
{
// SAFETY: The main thread handle is valid
2023-04-17 22:12:48 +02:00
let ret = unsafe { ResumeThread(child.main_thread_handle().as_raw_handle() as HANDLE) };
assert!(ret != u32::MAX, "ResumeThread failed");
}
2022-08-08 16:37:39 +02:00
2023-04-17 22:12:48 +02:00
#[cfg(not(feature = "windows_process_extensions_main_thread_handle"))]
2022-08-08 16:37:39 +02:00
{
// Since we can't get the main thread handle on stable rust, we use
// the undocumented but widely known `NtResumeProcess` function to
// resume a process by it's handle.
// SAFETY: The process handle is valid
2023-04-14 18:56:24 +02:00
let ret = unsafe { NtResumeProcess(child_handle) };
2022-08-08 16:37:39 +02:00
assert!(ret == STATUS_SUCCESS, "NtResumeProcess failed");
}
Self { job_object }
}
2026-10-05 14:17:40 +02:00
pub fn stop(&self, mut child: Child) -> Result<(ExitStatus, Time, Time, Information)> {
2026-10-05 13:51:06 +02:00
let status = child.wait()?;
2022-08-08 16:37:39 +02:00
let mut job_object_info =
mem::MaybeUninit::<JOBOBJECT_BASIC_ACCOUNTING_INFORMATION>::uninit();
2018-02-05 18:22:59 -05:00
2022-08-08 16:37:39 +02:00
// SAFETY: A valid job object got created in `start_suspended_process`
2022-07-21 22:47:40 +08:00
let res = unsafe {
2022-08-08 16:37:39 +02:00
QueryInformationJobObject(
self.job_object,
JobObjectBasicAccountingInformation,
job_object_info.as_mut_ptr().cast(),
mem::size_of::<JOBOBJECT_BASIC_ACCOUNTING_INFORMATION>() as u32,
ptr::null_mut(),
2022-07-21 22:47:40 +08:00
)
};
2018-02-05 18:22:59 -05:00
if res != 0 {
2022-08-08 16:37:39 +02:00
// SAFETY: The job object info got correctly initialized
let job_object_info = unsafe { job_object_info.assume_init() };
2018-02-05 18:22:59 -05:00
2023-04-17 22:12:48 +02:00
// The `TotalUserTime` is "The total amount of user-mode execution time for
2022-08-08 16:37:39 +02:00
// all active processes associated with the job, as well as all terminated processes no
2023-04-17 22:12:48 +02:00
// longer associated with the job, in 100-nanosecond ticks."
2026-10-05 14:17:40 +02:00
let user_time = Time::from_nanoseconds((job_object_info.TotalUserTime as f64) * 100.0);
2022-08-08 16:37:39 +02:00
2023-04-17 22:12:48 +02:00
// The `TotalKernelTime` is "The total amount of kernel-mode execution time
2022-08-08 16:37:39 +02:00
// for all active processes associated with the job, as well as all terminated
2023-04-17 22:12:48 +02:00
// processes no longer associated with the job, in 100-nanosecond ticks."
2026-10-05 14:17:40 +02:00
let system_time =
Time::from_nanoseconds((job_object_info.TotalKernelTime as f64) * 100.0);
Ok((status, user_time, system_time, Information::zero()))
2022-08-08 16:37:39 +02:00
} else {
2026-10-05 14:17:40 +02:00
Ok((status, Time::zero(), Time::zero(), Information::zero()))
2022-08-08 16:37:39 +02:00
}
}
}
impl Drop for CPUTimer {
2023-04-17 22:12:48 +02:00
fn drop(&mut self) {
2022-08-08 16:37:39 +02:00
// SAFETY: A valid job object got created in `start_suspended_process`
unsafe { CloseHandle(self.job_object) };
2018-02-05 18:22:59 -05:00
}
}