windows_timer.rs
This file
Path
src/timer/windows_timer.rs
126 lines · 4.8 KiB · Rust
Last changed by
Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
History of this fileTake it
The walk stopped after 200 commits: lines older than 62e82a9 point at it.
Update CHANGELOGDavid Peterbefore adoption62e82a9
line 1why, one step back1#![cfg(windows)]Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
line 2why, one step back2#![warn(unsafe_op_in_unsafe_fn)]Update CHANGELOGDavid Peterbefore adoption62e82a9
line 3why, one step back3Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
line 4why, one step back4use std::{mem, os::windows::io::AsRawHandle, process, ptr};Update CHANGELOGDavid Peterbefore adoption62e82a9
line 5why, one step back5Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
lines 6–10why, one step back6use windows_sys::Win32::{7 Foundation::{CloseHandle, HANDLE},8 System::JobObjects::{9 AssignProcessToJobObject, CreateJobObjectW, JobObjectBasicAccountingInformation,10 QueryInformationJobObject, JOBOBJECT_BASIC_ACCOUNTING_INFORMATION,Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 11–12why, one step back11 },12};Update CHANGELOGDavid Peterbefore adoption62e82a9
line 13why, one step back13Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
lines 14–17why, one step back14#[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;Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
line 18why, one step back18Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 19why, one step back19#[cfg(not(feature = "windows_process_extensions_main_thread_handle"))]Cleanup Windows CPUTimerClemens Wasserbefore adoption7db6178
line 20why, one step back20use once_cell::sync::Lazy;Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
lines 21–28why, one step back21#[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};Cleanup Windows CPUTimerClemens Wasserbefore adoption7db6178
line 29why, one step back29Update CHANGELOGDavid Peterbefore adoption62e82a9
lines 30–33why, one step back30use crate::util::units::Second;3132const HUNDRED_NS_PER_MS: i64 = 10;33Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 34why, one step back34#[cfg(not(feature = "windows_process_extensions_main_thread_handle"))]Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 35–38why, one step back35#[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 safeUse windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
lines 39–40why, one step back39 let ntdll = unsafe { GetModuleHandleW(w!("ntdll.dll")) };40 assert!(ntdll != 0, "GetModuleHandleW failed");Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 41–42why, one step back4142 // SAFETY: The ntdll handle is validUse windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 43why, one step back43 let nt_resume_process = unsafe { GetProcAddress(ntdll, s!("NtResumeProcess")) };Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 44–45why, one step back4445 // SAFETY: We transmute to the correct function signatureUse windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 46why, one step back46 unsafe { mem::transmute(nt_resume_process.unwrap()) }Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 47–48why, one step back47 });48Refactor timer moduleDavid Peterbefore adoptiondefafdf
line 49why, one step back49pub struct CPUTimer {Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
line 50why, one step back50 job_object: HANDLE,Update CHANGELOGDavid Peterbefore adoption62e82a9
lines 51–52why, one step back51}52Refactor timer moduleDavid Peterbefore adoptiondefafdf
line 53why, one step back53impl CPUTimer {Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
line 54why, one step back54 pub unsafe fn start_suspended_process(child: &process::Child) -> Self {Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 55why, one step back55 let child_handle = child.as_raw_handle() as HANDLE;Fix winapi type definition changeClemens Wasserbefore adoptionccda6c4
line 56why, one step back56Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 57–58why, one step back57 // SAFETY: Creating a new job object is safe58 let job_object = unsafe { CreateJobObjectW(ptr::null_mut(), ptr::null_mut()) };Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 59why, one step back59 assert!(job_object != 0, "CreateJobObjectW failed");Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 60–61why, one step back6061 // SAFETY: The job object handle is validFix winapi type definition changeClemens Wasserbefore adoptionccda6c4
line 62why, one step back62 let ret = unsafe { AssignProcessToJobObject(job_object, child_handle) };Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 63–64why, one step back63 assert!(ret != 0, "AssignProcessToJobObject failed");64Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 65why, one step back65 #[cfg(feature = "windows_process_extensions_main_thread_handle")]Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 66–67why, one step back66 {67 // SAFETY: The main thread handle is validUse windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
lines 68–69why, one step back68 let ret = unsafe { ResumeThread(child.main_thread_handle().as_raw_handle() as HANDLE) };69 assert!(ret != u32::MAX, "ResumeThread failed");Update CHANGELOGDavid Peterbefore adoption62e82a9
line 70why, one step back70 }Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
line 71why, one step back71Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 72why, one step back72 #[cfg(not(feature = "windows_process_extensions_main_thread_handle"))]Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 73–78why, one step back73 {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.7778 // SAFETY: The process handle is validFix winapi type definition changeClemens Wasserbefore adoptionccda6c4
line 79why, one step back79 let ret = unsafe { NtResumeProcess(child_handle) };Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 80–83why, one step back80 assert!(ret == STATUS_SUCCESS, "NtResumeProcess failed");81 }8283 Self { job_object }Update CHANGELOGDavid Peterbefore adoption62e82a9
lines 84–85why, one step back84 }85Refactor timer moduleDavid Peterbefore adoptiondefafdf
line 86why, one step back86 pub fn stop(&self) -> (Second, Second) {Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 87–88why, one step back87 let mut job_object_info =88 mem::MaybeUninit::<JOBOBJECT_BASIC_ACCOUNTING_INFORMATION>::uninit();Update CHANGELOGDavid Peterbefore adoption62e82a9
line 89why, one step back89Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
line 90why, one step back90 // SAFETY: A valid job object got created in `start_suspended_process`Shrink unsafe blockcactterbefore adoption2e8ce30
line 91why, one step back91 let res = unsafe {Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 92–97why, one step back92 QueryInformationJobObject(93 self.job_object,94 JobObjectBasicAccountingInformation,95 job_object_info.as_mut_ptr().cast(),96 mem::size_of::<JOBOBJECT_BASIC_ACCOUNTING_INFORMATION>() as u32,97 ptr::null_mut(),Shrink unsafe blockcactterbefore adoption2e8ce30
lines 98–99why, one step back98 )99 };Update CHANGELOGDavid Peterbefore adoption62e82a9
lines 100–101why, one step back100101 if res != 0 {Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 102–104why, one step back102 // SAFETY: The job object info got correctly initialized103 let job_object_info = unsafe { job_object_info.assume_init() };104Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 105why, one step back105 // The `TotalUserTime` is "The total amount of user-mode execution time forFix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
line 106why, one step back106 // all active processes associated with the job, as well as all terminated processes noUse windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
lines 107–108why, one step back107 // longer associated with the job, in 100-nanosecond ticks."108 let user: i64 = job_object_info.TotalUserTime / HUNDRED_NS_PER_MS;Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
line 109why, one step back109Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 110why, one step back110 // The `TotalKernelTime` is "The total amount of kernel-mode execution timeFix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
line 111why, one step back111 // for all active processes associated with the job, as well as all terminatedUse windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
lines 112–113why, one step back112 // processes no longer associated with the job, in 100-nanosecond ticks."113 let kernel: i64 = job_object_info.TotalKernelTime / HUNDRED_NS_PER_MS;Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
line 114why, one step back114 (user as f64 * 1e-6, kernel as f64 * 1e-6)Update CHANGELOGDavid Peterbefore adoption62e82a9
line 115why, one step back115 } else {Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
line 116why, one step back116 (0.0, 0.0)Update CHANGELOGDavid Peterbefore adoption62e82a9
line 117why, one step back117 }Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 118–119why, one step back118 }119}Update CHANGELOGDavid Peterbefore adoption62e82a9
line 120why, one step back120Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
line 121why, one step back121impl Drop for CPUTimer {Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 122why, one step back122 fn drop(&mut self) {Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 123–124why, one step back123 // SAFETY: A valid job object got created in `start_suspended_process`124 unsafe { CloseHandle(self.job_object) };Update CHANGELOGDavid Peterbefore adoption62e82a9
lines 125–126why, one step back125 }126}