mirror of
https://github.com/sharkdp/hyperfine.git
synced 2026-11-01 08:50:19 +00:00
Use windows-sys and fix feature flag
This commit is contained in:
1 parent
77e7b6cd65
commit
d2fb7d4c4c
4 files changed
+49
-38
No files matched your search
Generated
+1
-1
@@ -434,7 +434,7 @@ dependencies = [
|
|||||||
"statistical",
|
"statistical",
|
||||||
"tempfile",
|
"tempfile",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
"winapi",
|
"windows-sys 0.45.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
+7
-1
@@ -34,7 +34,13 @@ anyhow = "1.0"
|
|||||||
libc = "0.2"
|
libc = "0.2"
|
||||||
|
|
||||||
[target.'cfg(windows)'.dependencies]
|
[target.'cfg(windows)'.dependencies]
|
||||||
winapi = { version = "0.3", features = ["processthreadsapi", "minwindef", "winnt", "jobapi2", "tlhelp32", "handleapi"] }
|
windows-sys = { version = "0.45", features = [
|
||||||
|
"Win32_Foundation",
|
||||||
|
"Win32_Security",
|
||||||
|
"Win32_System_JobObjects",
|
||||||
|
"Win32_System_LibraryLoader",
|
||||||
|
"Win32_System_Threading",
|
||||||
|
] }
|
||||||
|
|
||||||
[target.'cfg(all(windows, not(windows_process_extensions_main_thread_handle)))'.dependencies]
|
[target.'cfg(all(windows, not(windows_process_extensions_main_thread_handle)))'.dependencies]
|
||||||
once_cell = "1.17"
|
once_cell = "1.17"
|
||||||
|
|||||||
+5
-2
@@ -13,6 +13,9 @@ use std::fs::File;
|
|||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
use std::os::unix::io::AsRawFd;
|
use std::os::unix::io::AsRawFd;
|
||||||
|
|
||||||
|
#[cfg(target_os = "windows")]
|
||||||
|
use windows_sys::Win32::System::Threading::CREATE_SUSPENDED;
|
||||||
|
|
||||||
use crate::util::units::Second;
|
use crate::util::units::Second;
|
||||||
use wall_clock_timer::WallClockTimer;
|
use wall_clock_timer::WallClockTimer;
|
||||||
|
|
||||||
@@ -82,8 +85,8 @@ pub fn execute_and_measure(mut command: Command) -> Result<TimerResult> {
|
|||||||
{
|
{
|
||||||
use std::os::windows::process::CommandExt;
|
use std::os::windows::process::CommandExt;
|
||||||
|
|
||||||
// Create a suspended process
|
// Create the process in a suspended state so that we don't miss any cpu time between process creation and `CPUTimer` start.
|
||||||
command.creation_flags(4);
|
command.creation_flags(CREATE_SUSPENDED);
|
||||||
}
|
}
|
||||||
|
|
||||||
let wallclock_timer = WallClockTimer::start();
|
let wallclock_timer = WallClockTimer::start();
|
||||||
|
|||||||
+36
-34
@@ -3,43 +3,47 @@
|
|||||||
|
|
||||||
use std::{mem, os::windows::io::AsRawHandle, process, ptr};
|
use std::{mem, os::windows::io::AsRawHandle, process, ptr};
|
||||||
|
|
||||||
use winapi::{
|
use windows_sys::Win32::{
|
||||||
shared::{ntdef::NTSTATUS, ntstatus::STATUS_SUCCESS},
|
Foundation::{CloseHandle, HANDLE},
|
||||||
um::{
|
System::JobObjects::{
|
||||||
handleapi::CloseHandle,
|
AssignProcessToJobObject, CreateJobObjectW, JobObjectBasicAccountingInformation,
|
||||||
jobapi2::{AssignProcessToJobObject, CreateJobObjectW, QueryInformationJobObject},
|
QueryInformationJobObject, JOBOBJECT_BASIC_ACCOUNTING_INFORMATION,
|
||||||
libloaderapi::{GetModuleHandleA, GetProcAddress},
|
|
||||||
winnt::{
|
|
||||||
JobObjectBasicAccountingInformation, HANDLE, JOBOBJECT_BASIC_ACCOUNTING_INFORMATION,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(windows_process_extensions_main_thread_handle)]
|
#[cfg(feature = "windows_process_extensions_main_thread_handle")]
|
||||||
use winapi::shared::minwindef::DWORD;
|
use std::os::windows::process::ChildExt;
|
||||||
|
#[cfg(feature = "windows_process_extensions_main_thread_handle")]
|
||||||
|
use windows_sys::Win32::System::Threading::ResumeThread;
|
||||||
|
|
||||||
#[cfg(not(windows_process_extensions_main_thread_handle))]
|
#[cfg(not(feature = "windows_process_extensions_main_thread_handle"))]
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
|
#[cfg(not(feature = "windows_process_extensions_main_thread_handle"))]
|
||||||
|
use windows_sys::{
|
||||||
|
s, w,
|
||||||
|
Win32::{
|
||||||
|
Foundation::{NTSTATUS, STATUS_SUCCESS},
|
||||||
|
System::LibraryLoader::{GetModuleHandleW, GetProcAddress},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
use crate::util::units::Second;
|
use crate::util::units::Second;
|
||||||
|
|
||||||
const HUNDRED_NS_PER_MS: i64 = 10;
|
const HUNDRED_NS_PER_MS: i64 = 10;
|
||||||
|
|
||||||
#[cfg(not(windows_process_extensions_main_thread_handle))]
|
#[cfg(not(feature = "windows_process_extensions_main_thread_handle"))]
|
||||||
#[allow(non_upper_case_globals)]
|
#[allow(non_upper_case_globals)]
|
||||||
static NtResumeProcess: Lazy<unsafe extern "system" fn(ProcessHandle: HANDLE) -> NTSTATUS> =
|
static NtResumeProcess: Lazy<unsafe extern "system" fn(ProcessHandle: HANDLE) -> NTSTATUS> =
|
||||||
Lazy::new(|| {
|
Lazy::new(|| {
|
||||||
// SAFETY: Getting the module handle for ntdll.dll is safe
|
// SAFETY: Getting the module handle for ntdll.dll is safe
|
||||||
let ntdll = unsafe { GetModuleHandleA(b"ntdll.dll\0".as_ptr().cast()) };
|
let ntdll = unsafe { GetModuleHandleW(w!("ntdll.dll")) };
|
||||||
assert!(!ntdll.is_null(), "GetModuleHandleA failed");
|
assert!(ntdll != 0, "GetModuleHandleW failed");
|
||||||
|
|
||||||
// SAFETY: The ntdll handle is valid
|
// SAFETY: The ntdll handle is valid
|
||||||
let nt_resume_process =
|
let nt_resume_process = unsafe { GetProcAddress(ntdll, s!("NtResumeProcess")) };
|
||||||
unsafe { GetProcAddress(ntdll, b"NtResumeProcess\0".as_ptr().cast()) };
|
|
||||||
assert!(!nt_resume_process.is_null(), "GetProcAddress failed");
|
|
||||||
|
|
||||||
// SAFETY: We transmute to the correct function signature
|
// SAFETY: We transmute to the correct function signature
|
||||||
unsafe { mem::transmute(nt_resume_process) }
|
unsafe { mem::transmute(nt_resume_process.unwrap()) }
|
||||||
});
|
});
|
||||||
|
|
||||||
pub struct CPUTimer {
|
pub struct CPUTimer {
|
||||||
@@ -48,24 +52,24 @@ pub struct CPUTimer {
|
|||||||
|
|
||||||
impl CPUTimer {
|
impl CPUTimer {
|
||||||
pub unsafe fn start_suspended_process(child: &process::Child) -> Self {
|
pub unsafe fn start_suspended_process(child: &process::Child) -> Self {
|
||||||
let child_handle = child.as_raw_handle().cast();
|
let child_handle = child.as_raw_handle() as HANDLE;
|
||||||
|
|
||||||
// SAFETY: Creating a new job object is safe
|
// SAFETY: Creating a new job object is safe
|
||||||
let job_object = unsafe { CreateJobObjectW(ptr::null_mut(), ptr::null_mut()) };
|
let job_object = unsafe { CreateJobObjectW(ptr::null_mut(), ptr::null_mut()) };
|
||||||
assert!(!job_object.is_null(), "CreateJobObjectW failed");
|
assert!(job_object != 0, "CreateJobObjectW failed");
|
||||||
|
|
||||||
// SAFETY: The job object handle is valid
|
// SAFETY: The job object handle is valid
|
||||||
let ret = unsafe { AssignProcessToJobObject(job_object, child_handle) };
|
let ret = unsafe { AssignProcessToJobObject(job_object, child_handle) };
|
||||||
assert!(ret != 0, "AssignProcessToJobObject failed");
|
assert!(ret != 0, "AssignProcessToJobObject failed");
|
||||||
|
|
||||||
#[cfg(windows_process_extensions_main_thread_handle)]
|
#[cfg(feature = "windows_process_extensions_main_thread_handle")]
|
||||||
{
|
{
|
||||||
// SAFETY: The main thread handle is valid
|
// SAFETY: The main thread handle is valid
|
||||||
let ret = unsafe { ResumeThread(child.main_thread_handle().as_raw_handle()) };
|
let ret = unsafe { ResumeThread(child.main_thread_handle().as_raw_handle() as HANDLE) };
|
||||||
assert!(ret != -1 as DWORD, "ResumeThread failed");
|
assert!(ret != u32::MAX, "ResumeThread failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(windows_process_extensions_main_thread_handle))]
|
#[cfg(not(feature = "windows_process_extensions_main_thread_handle"))]
|
||||||
{
|
{
|
||||||
// Since we can't get the main thread handle on stable rust, we use
|
// Since we can't get the main thread handle on stable rust, we use
|
||||||
// the undocumented but widely known `NtResumeProcess` function to
|
// the undocumented but widely known `NtResumeProcess` function to
|
||||||
@@ -98,17 +102,15 @@ impl CPUTimer {
|
|||||||
// SAFETY: The job object info got correctly initialized
|
// SAFETY: The job object info got correctly initialized
|
||||||
let job_object_info = unsafe { job_object_info.assume_init() };
|
let job_object_info = unsafe { job_object_info.assume_init() };
|
||||||
|
|
||||||
// SAFETY: The `TotalUserTime` is "The total amount of user-mode execution time for
|
// 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
|
// 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
|
// longer associated with the job, in 100-nanosecond ticks."
|
||||||
let user: i64 = unsafe { job_object_info.TotalUserTime.QuadPart() } / HUNDRED_NS_PER_MS;
|
let user: i64 = job_object_info.TotalUserTime / HUNDRED_NS_PER_MS;
|
||||||
|
|
||||||
// SAFETY: The `TotalKernelTime` is "The total amount of kernel-mode execution time
|
// The `TotalKernelTime` is "The total amount of kernel-mode execution time
|
||||||
// for all active processes associated with the job, as well as all terminated
|
// 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
|
// processes no longer associated with the job, in 100-nanosecond ticks."
|
||||||
// to extract
|
let kernel: i64 = job_object_info.TotalKernelTime / HUNDRED_NS_PER_MS;
|
||||||
let kernel: i64 =
|
|
||||||
unsafe { job_object_info.TotalKernelTime.QuadPart() } / HUNDRED_NS_PER_MS;
|
|
||||||
(user as f64 * 1e-6, kernel as f64 * 1e-6)
|
(user as f64 * 1e-6, kernel as f64 * 1e-6)
|
||||||
} else {
|
} else {
|
||||||
(0.0, 0.0)
|
(0.0, 0.0)
|
||||||
@@ -117,7 +119,7 @@ impl CPUTimer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for CPUTimer {
|
impl Drop for CPUTimer {
|
||||||
fn drop(self: &mut Self) {
|
fn drop(&mut self) {
|
||||||
// SAFETY: A valid job object got created in `start_suspended_process`
|
// SAFETY: A valid job object got created in `start_suspended_process`
|
||||||
unsafe { CloseHandle(self.job_object) };
|
unsafe { CloseHandle(self.job_object) };
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user