Use windows-sys and fix feature flag
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Clemens Wasser · committed by David Peter · parent 77e7b6c · Browse files · 4 files·+49 added−38 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+49 added−38 removed
- other:+49 added−38 removed
Provenance
- Commit
d2fb7d4- Tree
d000f77- Author
Clemens Wasser <clemens.wasser@gmail.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
77e7b6c
4 files changed+49 added−38 removed
modified:src/timer/mod.rs+5 added-2 removed
11 unmodified lines12 use std::fs::File;13 #[cfg(target_os = "linux")]14 use std::os::unix::io::AsRawFd;15 16+#[cfg(target_os = "windows")]17+use windows_sys::Win32::System::Threading::CREATE_SUSPENDED;18+19 use crate::util::units::Second;20 use wall_clock_timer::WallClockTimer;21 22 use std::io::Read;61 unmodified lines84 #[cfg(windows)]85 {86 use std::os::windows::process::CommandExt;87 85- // Create a suspended process86- command.creation_flags(4);88+ // Create the process in a suspended state so that we don't miss any cpu time between process creation and `CPUTimer` start.89+ command.creation_flags(CREATE_SUSPENDED);90 }91 92 let wallclock_timer = WallClockTimer::start();93 let mut child = command.spawn()?;24 unmodified linesmodified:src/timer/windows_timer.rs+36 added-34 removed
1 unmodified line2 #![warn(unsafe_op_in_unsafe_fn)]3 4 use std::{mem, os::windows::io::AsRawHandle, process, ptr};5 6-use winapi::{7- shared::{ntdef::NTSTATUS, ntstatus::STATUS_SUCCESS},8- um::{9- handleapi::CloseHandle,10- jobapi2::{AssignProcessToJobObject, CreateJobObjectW, QueryInformationJobObject},11- libloaderapi::{GetModuleHandleA, GetProcAddress},12- winnt::{13- JobObjectBasicAccountingInformation, HANDLE, JOBOBJECT_BASIC_ACCOUNTING_INFORMATION,14- },6+use windows_sys::Win32::{7+ Foundation::{CloseHandle, HANDLE},8+ System::JobObjects::{9+ AssignProcessToJobObject, CreateJobObjectW, JobObjectBasicAccountingInformation,10+ QueryInformationJobObject, JOBOBJECT_BASIC_ACCOUNTING_INFORMATION,11 },12 };13 18-#[cfg(windows_process_extensions_main_thread_handle)]19-use winapi::shared::minwindef::DWORD;14+#[cfg(feature = "windows_process_extensions_main_thread_handle")]15+use std::os::windows::process::ChildExt;16+#[cfg(feature = "windows_process_extensions_main_thread_handle")]17+use windows_sys::Win32::System::Threading::ResumeThread;18 21-#[cfg(not(windows_process_extensions_main_thread_handle))]19+#[cfg(not(feature = "windows_process_extensions_main_thread_handle"))]20 use once_cell::sync::Lazy;21+#[cfg(not(feature = "windows_process_extensions_main_thread_handle"))]22+use windows_sys::{23+ s, w,24+ Win32::{25+ Foundation::{NTSTATUS, STATUS_SUCCESS},26+ System::LibraryLoader::{GetModuleHandleW, GetProcAddress},27+ },28+};29 30 use crate::util::units::Second;31 32 const HUNDRED_NS_PER_MS: i64 = 10;33 28-#[cfg(not(windows_process_extensions_main_thread_handle))]34+#[cfg(not(feature = "windows_process_extensions_main_thread_handle"))]35 #[allow(non_upper_case_globals)]36 static NtResumeProcess: Lazy<unsafe extern "system" fn(ProcessHandle: HANDLE) -> NTSTATUS> =37 Lazy::new(|| {38 // SAFETY: Getting the module handle for ntdll.dll is safe33- let ntdll = unsafe { GetModuleHandleA(b"ntdll.dll\0".as_ptr().cast()) };34- assert!(!ntdll.is_null(), "GetModuleHandleA failed");39+ let ntdll = unsafe { GetModuleHandleW(w!("ntdll.dll")) };40+ assert!(ntdll != 0, "GetModuleHandleW failed");41 42 // SAFETY: The ntdll handle is valid37- let nt_resume_process =38- unsafe { GetProcAddress(ntdll, b"NtResumeProcess\0".as_ptr().cast()) };39- assert!(!nt_resume_process.is_null(), "GetProcAddress failed");43+ let nt_resume_process = unsafe { GetProcAddress(ntdll, s!("NtResumeProcess")) };44 45 // SAFETY: We transmute to the correct function signature42- unsafe { mem::transmute(nt_resume_process) }46+ unsafe { mem::transmute(nt_resume_process.unwrap()) }47 });48 49 pub struct CPUTimer {50 job_object: HANDLE,51 }52 53 impl CPUTimer {54 pub unsafe fn start_suspended_process(child: &process::Child) -> Self {51- let child_handle = child.as_raw_handle().cast();55+ let child_handle = child.as_raw_handle() as HANDLE;56 57 // SAFETY: Creating a new job object is safe58 let job_object = unsafe { CreateJobObjectW(ptr::null_mut(), ptr::null_mut()) };55- assert!(!job_object.is_null(), "CreateJobObjectW failed");59+ assert!(job_object != 0, "CreateJobObjectW failed");60 61 // SAFETY: The job object handle is valid62 let ret = unsafe { AssignProcessToJobObject(job_object, child_handle) };63 assert!(ret != 0, "AssignProcessToJobObject failed");64 61- #[cfg(windows_process_extensions_main_thread_handle)]65+ #[cfg(feature = "windows_process_extensions_main_thread_handle")]66 {67 // SAFETY: The main thread handle is valid64- let ret = unsafe { ResumeThread(child.main_thread_handle().as_raw_handle()) };65- assert!(ret != -1 as DWORD, "ResumeThread failed");68+ let ret = unsafe { ResumeThread(child.main_thread_handle().as_raw_handle() as HANDLE) };69+ assert!(ret != u32::MAX, "ResumeThread failed");70 }71 68- #[cfg(not(windows_process_extensions_main_thread_handle))]72+ #[cfg(not(feature = "windows_process_extensions_main_thread_handle"))]73 {74 // Since we can't get the main thread handle on stable rust, we use75 // the undocumented but widely known `NtResumeProcess` function to76 // resume a process by it's handle.24 unmodified lines101 if res != 0 {102 // SAFETY: The job object info got correctly initialized103 let job_object_info = unsafe { job_object_info.assume_init() };104 101- // SAFETY: The `TotalUserTime` is "The total amount of user-mode execution time for105+ // The `TotalUserTime` is "The total amount of user-mode execution time for106 // all active processes associated with the job, as well as all terminated processes no103- // longer associated with the job, in 100-nanosecond ticks." and is safe to extract104- let user: i64 = unsafe { job_object_info.TotalUserTime.QuadPart() } / HUNDRED_NS_PER_MS;107+ // longer associated with the job, in 100-nanosecond ticks."108+ let user: i64 = job_object_info.TotalUserTime / HUNDRED_NS_PER_MS;109 106- // SAFETY: The `TotalKernelTime` is "The total amount of kernel-mode execution time110+ // The `TotalKernelTime` is "The total amount of kernel-mode execution time111 // for all active processes associated with the job, as well as all terminated108- // processes no longer associated with the job, in 100-nanosecond ticks." and is safe109- // to extract110- let kernel: i64 =111- unsafe { job_object_info.TotalKernelTime.QuadPart() } / HUNDRED_NS_PER_MS;112+ // processes no longer associated with the job, in 100-nanosecond ticks."113+ let kernel: i64 = job_object_info.TotalKernelTime / HUNDRED_NS_PER_MS;114 (user as f64 * 1e-6, kernel as f64 * 1e-6)115 } else {116 (0.0, 0.0)117 }118 }119 }120 121 impl Drop for CPUTimer {120- fn drop(self: &mut Self) {122+ fn drop(&mut self) {123 // SAFETY: A valid job object got created in `start_suspended_process`124 unsafe { CloseHandle(self.job_object) };125 }126 }modified:Cargo.lock+1 added-1 removed
432 unmodified lines433 "shell-words",434 "statistical",435 "tempfile",436 "thiserror",437- "winapi",437+ "windows-sys 0.45.0",438 ]439 440 [[package]]441 name = "indicatif"864 unmodified linesmodified:Cargo.toml+7 added-1 removed
32 unmodified lines3334353637373839404142434445464723 unmodified lines32 unmodified lines[target.'cfg(not(windows))'.dependencies]libc = "0.2"
[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]once_cell = "1.17"
23 unmodified lines