windows_timer.rs
This file
Path
src/timer/windows_timer.rs
131 lines · 5.0 KiB · Rust
Last changed by
Merge pull request #977 from sharkdp/codex/fix-clippy-warningsDavid Peterbefore adoption79aea88
History of this fileTake it
The walk stopped after 200 commits: lines older than 7684efd point at it.
Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd
lines 1–3why, one step back1#![cfg(windows)]2#![warn(unsafe_op_in_unsafe_fn)]3Merge pull request #975 from sharkdp/codex/unified-windows-timerDavid Peterbefore adoptionefd57b3
lines 4–5why, one step back4use std::process::{self, Child, ExitStatus};5use std::{mem, os::windows::io::AsRawHandle, ptr};Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd
line 6why, one step back6Merge pull request #975 from sharkdp/codex/unified-windows-timerDavid Peterbefore adoptionefd57b3
lines 7–8why, one step back7use anyhow::Result;8Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
lines 9–13why, one step back9use windows_sys::Win32::{10 Foundation::{CloseHandle, HANDLE},11 System::JobObjects::{12 AssignProcessToJobObject, CreateJobObjectW, JobObjectBasicAccountingInformation,13 QueryInformationJobObject, JOBOBJECT_BASIC_ACCOUNTING_INFORMATION,Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd
lines 14–16why, one step back14 },15};16Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
lines 17–20why, one step back17#[cfg(feature = "windows_process_extensions_main_thread_handle")]18use std::os::windows::process::ChildExt;19#[cfg(feature = "windows_process_extensions_main_thread_handle")]20use windows_sys::Win32::System::Threading::ResumeThread;Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd
line 21why, one step back21Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 22why, one step back22#[cfg(not(feature = "windows_process_extensions_main_thread_handle"))]Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd
line 23why, one step back23use once_cell::sync::Lazy;Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
lines 24–31why, one step back24#[cfg(not(feature = "windows_process_extensions_main_thread_handle"))]25use windows_sys::{26 s, w,27 Win32::{28 Foundation::{NTSTATUS, STATUS_SUCCESS},29 System::LibraryLoader::{GetModuleHandleW, GetProcAddress},30 },31};Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd
line 32why, one step back32Merge pull request #964 from sharkdp/codex/windows-timer-precisionDavid Peterbefore adoption1c1be47
line 33why, one step back33use crate::quantity::{nanosecond, Information, Time, Zero};Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd
line 34why, one step back34Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 35why, one step back35#[cfg(not(feature = "windows_process_extensions_main_thread_handle"))]Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd
line 36why, one step back36#[allow(non_upper_case_globals)]Merge pull request #977 from sharkdp/codex/fix-clippy-warningsDavid Peterbefore adoption79aea88
line 37why, one step back37static NtResumeProcess: Lazy<unsafe extern "system" fn(process_handle: HANDLE) -> NTSTATUS> =Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd
lines 38–39why, one step back38 Lazy::new(|| {39 // SAFETY: Getting the module handle for ntdll.dll is safeUse windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 40why, one step back40 let ntdll = unsafe { GetModuleHandleW(w!("ntdll.dll")) };Merge pull request #977 from sharkdp/codex/fix-clippy-warningsDavid Peterbefore adoption79aea88
line 41why, one step back41 assert!(!ntdll.is_null(), "GetModuleHandleW failed");Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd
lines 42–43why, one step back4243 // SAFETY: The ntdll handle is validUse windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 44why, one step back44 let nt_resume_process = unsafe { GetProcAddress(ntdll, s!("NtResumeProcess")) };Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd
lines 45–46why, one step back4546 // SAFETY: We transmute to the correct function signatureUse windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 47why, one step back47 unsafe { mem::transmute(nt_resume_process.unwrap()) }Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd
lines 48–55why, one step back48 });4950pub struct CPUTimer {51 job_object: HANDLE,52}5354impl CPUTimer {55 pub unsafe fn start_suspended_process(child: &process::Child) -> Self {Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 56why, one step back56 let child_handle = child.as_raw_handle() as HANDLE;Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd
lines 57–59why, one step back5758 // SAFETY: Creating a new job object is safe59 let job_object = unsafe { CreateJobObjectW(ptr::null_mut(), ptr::null_mut()) };Merge pull request #977 from sharkdp/codex/fix-clippy-warningsDavid Peterbefore adoption79aea88
line 60why, one step back60 assert!(!job_object.is_null(), "CreateJobObjectW failed");Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd
lines 61–65why, one step back6162 // SAFETY: The job object handle is valid63 let ret = unsafe { AssignProcessToJobObject(job_object, child_handle) };64 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")]Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd
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");Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd
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"))]Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd
lines 74–86why, 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 valid80 let ret = unsafe { NtResumeProcess(child_handle) };81 assert!(ret == STATUS_SUCCESS, "NtResumeProcess failed");82 }8384 Self { job_object }85 }86Merge pull request #975 from sharkdp/codex/unified-windows-timerDavid Peterbefore adoptionefd57b3
lines 87–89why, one step back87 pub fn stop(&self, mut child: Child) -> Result<(Time, Time, Information, ExitStatus)> {88 let status = child.wait()?;89Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd
lines 90–107why, one step back90 let mut job_object_info =91 mem::MaybeUninit::<JOBOBJECT_BASIC_ACCOUNTING_INFORMATION>::uninit();9293 // SAFETY: A valid job object got created in `start_suspended_process`94 let res = unsafe {95 QueryInformationJobObject(96 self.job_object,97 JobObjectBasicAccountingInformation,98 job_object_info.as_mut_ptr().cast(),99 mem::size_of::<JOBOBJECT_BASIC_ACCOUNTING_INFORMATION>() as u32,100 ptr::null_mut(),101 )102 };103104 if res != 0 {105 // SAFETY: The job object info got correctly initialized106 let job_object_info = unsafe { job_object_info.assume_init() };107Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 108why, one step back108 // The `TotalUserTime` is "The total amount of user-mode execution time forUse plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd
line 109why, one step back109 // all active processes associated with the job, as well as all terminated processes noUse windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 110why, one step back110 // longer associated with the job, in 100-nanosecond ticks."Merge pull request #964 from sharkdp/codex/windows-timer-precisionDavid Peterbefore adoption1c1be47
line 111why, one step back111 let user_time = Time::new::<nanosecond>((job_object_info.TotalUserTime as f64) * 100.0);Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd
line 112why, one step back112Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 113why, one step back113 // The `TotalKernelTime` is "The total amount of kernel-mode execution timeUse plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd
line 114why, one step back114 // for all active processes associated with the job, as well as all terminatedUse windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 115why, one step back115 // processes no longer associated with the job, in 100-nanosecond ticks."Merge pull request #964 from sharkdp/codex/windows-timer-precisionDavid Peterbefore adoption1c1be47
lines 116–117why, one step back116 let system_time =117 Time::new::<nanosecond>((job_object_info.TotalKernelTime as f64) * 100.0);Merge pull request #975 from sharkdp/codex/unified-windows-timerDavid Peterbefore adoptionefd57b3
lines 118–119why, one step back118119 Ok((user_time, system_time, Information::zero(), status))Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd
line 120why, one step back120 } else {Merge pull request #975 from sharkdp/codex/unified-windows-timerDavid Peterbefore adoptionefd57b3
line 121why, one step back121 Ok((Time::zero(), Time::zero(), Information::zero(), status))Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd
lines 122–126why, one step back122 }123 }124}125126impl Drop for CPUTimer {Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 127why, one step back127 fn drop(&mut self) {Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd
lines 128–131why, one step back128 // SAFETY: A valid job object got created in `start_suspended_process`129 unsafe { CloseHandle(self.job_object) };130 }131}