Skip to content
Myr

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

4 files+49 added−38 removed

modified:src/timer/mod.rs+5 added-2 removed
11 unmodified lines
12use std::fs::File;13#[cfg(target_os = "linux")]14use std::os::unix::io::AsRawFd;1516#[cfg(target_os = "windows")]17use windows_sys::Win32::System::Threading::CREATE_SUSPENDED;1819use crate::util::units::Second;20use wall_clock_timer::WallClockTimer;2122use std::io::Read;
61 unmodified lines
84 #[cfg(windows)]85 {86 use std::os::windows::process::CommandExt;8785 // 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 }9192 let wallclock_timer = WallClockTimer::start();93 let mut child = command.spawn()?;
24 unmodified lines
modified:src/timer/windows_timer.rs+36 added-34 removed
1 unmodified line
2#![warn(unsafe_op_in_unsafe_fn)]34use std::{mem, os::windows::io::AsRawHandle, process, ptr};56use 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 },6use windows_sys::Win32::{7 Foundation::{CloseHandle, HANDLE},8 System::JobObjects::{9 AssignProcessToJobObject, CreateJobObjectW, JobObjectBasicAccountingInformation,10 QueryInformationJobObject, JOBOBJECT_BASIC_ACCOUNTING_INFORMATION,11 },12};1318#[cfg(windows_process_extensions_main_thread_handle)]19use winapi::shared::minwindef::DWORD;14#[cfg(feature = "windows_process_extensions_main_thread_handle")]15use std::os::windows::process::ChildExt;16#[cfg(feature = "windows_process_extensions_main_thread_handle")]17use windows_sys::Win32::System::Threading::ResumeThread;1821#[cfg(not(windows_process_extensions_main_thread_handle))]19#[cfg(not(feature = "windows_process_extensions_main_thread_handle"))]20use once_cell::sync::Lazy;21#[cfg(not(feature = "windows_process_extensions_main_thread_handle"))]22use windows_sys::{23 s, w,24 Win32::{25 Foundation::{NTSTATUS, STATUS_SUCCESS},26 System::LibraryLoader::{GetModuleHandleW, GetProcAddress},27 },28};2930use crate::util::units::Second;3132const HUNDRED_NS_PER_MS: i64 = 10;3328#[cfg(not(windows_process_extensions_main_thread_handle))]34#[cfg(not(feature = "windows_process_extensions_main_thread_handle"))]35#[allow(non_upper_case_globals)]36static 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");4142 // 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")) };4445 // SAFETY: We transmute to the correct function signature42 unsafe { mem::transmute(nt_resume_process) }46 unsafe { mem::transmute(nt_resume_process.unwrap()) }47 });4849pub struct CPUTimer {50 job_object: HANDLE,51}5253impl 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;5657 // 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");6061 // SAFETY: The job object handle is valid62 let ret = unsafe { AssignProcessToJobObject(job_object, child_handle) };63 assert!(ret != 0, "AssignProcessToJobObject failed");6461 #[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 }7168 #[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 lines
101 if res != 0 {102 // SAFETY: The job object info got correctly initialized103 let job_object_info = unsafe { job_object_info.assume_init() };104101 // 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;109106 // 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}120121impl 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 lines
433 "shell-words",434 "statistical",435 "tempfile",436 "thiserror",437 "winapi",437 "windows-sys 0.45.0",438]439440[[package]]441name = "indicatif"
864 unmodified lines
modified:Cargo.toml+7 added-1 removed