windows_timer.rs
This file
Path
src/timer/windows_timer.rs
128 lines · 4.9 KiB · Rust
Last changed by
Merge pull request #964 from sharkdp/codex/windows-timer-precisionDavid Peterbefore adoption1c1be47
History of this fileTake it
The walk stopped after 200 commits: lines older than 5924ac3 point at it.
Update CHANGELOGDavid Peterbefore adoption5924ac3
lines 1–5why, one step back1#![cfg(windows)]2#![warn(unsafe_op_in_unsafe_fn)]34use std::{mem, os::windows::io::AsRawHandle, process, ptr};5Use 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,Update CHANGELOGDavid Peterbefore adoption5924ac3
lines 11–13why, one step back11 },12};13Use 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;Update CHANGELOGDavid Peterbefore adoption5924ac3
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"))]Update CHANGELOGDavid Peterbefore adoption5924ac3
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};Update CHANGELOGDavid Peterbefore adoption5924ac3
line 29why, one step back29Merge pull request #964 from sharkdp/codex/windows-timer-precisionDavid Peterbefore adoption1c1be47
line 30why, one step back30use crate::quantity::{nanosecond, Information, Time, Zero};Update CHANGELOGDavid Peterbefore adoption5924ac3
line 31why, one step back31Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 32why, one step back32#[cfg(not(feature = "windows_process_extensions_main_thread_handle"))]Update CHANGELOGDavid Peterbefore adoption5924ac3
lines 33–36why, one step back33#[allow(non_upper_case_globals)]34static NtResumeProcess: Lazy<unsafe extern "system" fn(ProcessHandle: HANDLE) -> NTSTATUS> =35 Lazy::new(|| {36 // SAFETY: Getting the module handle for ntdll.dll is safeUse windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 37why, one step back37 let ntdll = unsafe { GetModuleHandleW(w!("ntdll.dll")) };Update dependenciesDavid Peterbefore adoption2efc633
line 38why, one step back38 assert!(ntdll != std::ptr::null_mut(), "GetModuleHandleW failed");Update CHANGELOGDavid Peterbefore adoption5924ac3
lines 39–40why, one step back3940 // SAFETY: The ntdll handle is validUse windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 41why, one step back41 let nt_resume_process = unsafe { GetProcAddress(ntdll, s!("NtResumeProcess")) };Update CHANGELOGDavid Peterbefore adoption5924ac3
lines 42–43why, one step back4243 // SAFETY: We transmute to the correct function signatureUse windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 44why, one step back44 unsafe { mem::transmute(nt_resume_process.unwrap()) }Update CHANGELOGDavid Peterbefore adoption5924ac3
lines 45–52why, one step back45 });4647pub struct CPUTimer {48 job_object: HANDLE,49}5051impl CPUTimer {52 pub unsafe fn start_suspended_process(child: &process::Child) -> Self {Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 53why, one step back53 let child_handle = child.as_raw_handle() as HANDLE;Fix winapi type definition changeClemens Wasserbefore adoptionccda6c4
line 54why, one step back54Update CHANGELOGDavid Peterbefore adoption5924ac3
lines 55–56why, one step back55 // SAFETY: Creating a new job object is safe56 let job_object = unsafe { CreateJobObjectW(ptr::null_mut(), ptr::null_mut()) };Update dependenciesDavid Peterbefore adoption2efc633
lines 57–60why, one step back57 assert!(58 job_object != std::ptr::null_mut(),59 "CreateJobObjectW failed"60 );Update CHANGELOGDavid Peterbefore adoption5924ac3
lines 61–62why, one step back6162 // SAFETY: The job object handle is validFix winapi type definition changeClemens Wasserbefore adoptionccda6c4
line 63why, one step back63 let ret = unsafe { AssignProcessToJobObject(job_object, child_handle) };Update CHANGELOGDavid Peterbefore adoption5924ac3
lines 64–65why, one step back64 assert!(ret != 0, "AssignProcessToJobObject failed");65Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 66why, one step back66 #[cfg(feature = "windows_process_extensions_main_thread_handle")]Update CHANGELOGDavid Peterbefore adoption5924ac3
lines 67–68why, one step back67 {68 // SAFETY: The main thread handle is validUse windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
lines 69–70why, one step back69 let ret = unsafe { ResumeThread(child.main_thread_handle().as_raw_handle() as HANDLE) };70 assert!(ret != u32::MAX, "ResumeThread failed");Update CHANGELOGDavid Peterbefore adoption5924ac3
lines 71–72why, one step back71 }72Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 73why, one step back73 #[cfg(not(feature = "windows_process_extensions_main_thread_handle"))]Update CHANGELOGDavid Peterbefore adoption5924ac3
lines 74–79why, one step back74 {75 // Since we can't get the main thread handle on stable rust, we use76 // the undocumented but widely known `NtResumeProcess` function to77 // resume a process by it's handle.7879 // SAFETY: The process handle is validFix winapi type definition changeClemens Wasserbefore adoptionccda6c4
line 80why, one step back80 let ret = unsafe { NtResumeProcess(child_handle) };Update CHANGELOGDavid Peterbefore adoption5924ac3
lines 81–86why, one step back81 assert!(ret == STATUS_SUCCESS, "NtResumeProcess failed");82 }8384 Self { job_object }85 }86Merge pull request #961 from sharkdp/codex/uom-quantitiesDavid Peterbefore adoption975a25c
line 87why, one step back87 pub fn stop(&self) -> (Time, Time, Information) {Update CHANGELOGDavid Peterbefore adoption5924ac3
lines 88–105why, one step back88 let mut job_object_info =89 mem::MaybeUninit::<JOBOBJECT_BASIC_ACCOUNTING_INFORMATION>::uninit();9091 // SAFETY: A valid job object got created in `start_suspended_process`92 let res = unsafe {93 QueryInformationJobObject(94 self.job_object,95 JobObjectBasicAccountingInformation,96 job_object_info.as_mut_ptr().cast(),97 mem::size_of::<JOBOBJECT_BASIC_ACCOUNTING_INFORMATION>() as u32,98 ptr::null_mut(),99 )100 };101102 if res != 0 {103 // SAFETY: The job object info got correctly initialized104 let job_object_info = unsafe { job_object_info.assume_init() };105Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 106why, one step back106 // The `TotalUserTime` is "The total amount of user-mode execution time forUpdate CHANGELOGDavid Peterbefore adoption5924ac3
line 107why, one step back107 // all active processes associated with the job, as well as all terminated processes noUse windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 108why, one step back108 // longer associated with the job, in 100-nanosecond ticks."Merge pull request #964 from sharkdp/codex/windows-timer-precisionDavid Peterbefore adoption1c1be47
line 109why, one step back109 let user_time = Time::new::<nanosecond>((job_object_info.TotalUserTime as f64) * 100.0);Update CHANGELOGDavid Peterbefore adoption5924ac3
line 110why, one step back110Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 111why, one step back111 // The `TotalKernelTime` is "The total amount of kernel-mode execution timeUpdate CHANGELOGDavid Peterbefore adoption5924ac3
line 112why, one step back112 // for all active processes associated with the job, as well as all terminatedUse windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 113why, one step back113 // processes no longer associated with the job, in 100-nanosecond ticks."Merge pull request #964 from sharkdp/codex/windows-timer-precisionDavid Peterbefore adoption1c1be47
lines 114–116why, one step back114 let system_time =115 Time::new::<nanosecond>((job_object_info.TotalKernelTime as f64) * 100.0);116 (user_time, system_time, Information::zero())Update CHANGELOGDavid Peterbefore adoption5924ac3
line 117why, one step back117 } else {Merge pull request #961 from sharkdp/codex/uom-quantitiesDavid Peterbefore adoption975a25c
line 118why, one step back118 (Time::zero(), Time::zero(), Information::zero())Update CHANGELOGDavid Peterbefore adoption5924ac3
lines 119–123why, one step back119 }120 }121}122123impl Drop for CPUTimer {Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 124why, one step back124 fn drop(&mut self) {Update CHANGELOGDavid Peterbefore adoption5924ac3
lines 125–128why, one step back125 // SAFETY: A valid job object got created in `start_suspended_process`126 unsafe { CloseHandle(self.job_object) };127 }128}