windows_timer.rs
This file
Path
src/timer/windows_timer.rs
122 lines · 4.7 KiB · Rust
Last changed by
Cleanup Windows CPUTimerClemens Wasserbefore adoption7db6178
History of this fileTake it
The walk stopped after 200 commits: lines older than d3321dc point at it.
Bump predicates from 2.1.0 to 2.1.1dependabot[bot]before adoptiond3321dc
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)]Bump predicates from 2.1.0 to 2.1.1dependabot[bot]before adoptiond3321dc
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};Bump predicates from 2.1.0 to 2.1.1dependabot[bot]before adoptiond3321dc
line 5why, one step back5Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 6–16why, one step back6use 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 },15 },16};Bump predicates from 2.1.0 to 2.1.1dependabot[bot]before adoptiond3321dc
line 17why, one step back17Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 18–20why, one step back18#[cfg(windows_process_extensions_main_thread_handle)]19use winapi::shared::minwindef::DWORD;20Cleanup Windows CPUTimerClemens Wasserbefore adoption7db6178
lines 21–23why, one step back21#[cfg(not(windows_process_extensions_main_thread_handle))]22use once_cell::sync::Lazy;23Move a few modules to utilDavid Peterbefore adoption5c634c9
line 24why, one step back24use crate::util::units::Second;Bump predicates from 2.1.0 to 2.1.1dependabot[bot]before adoptiond3321dc
lines 25–27why, one step back2526const HUNDRED_NS_PER_MS: i64 = 10;27Cleanup Windows CPUTimerClemens Wasserbefore adoption7db6178
line 28why, one step back28#[cfg(not(windows_process_extensions_main_thread_handle))]Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 29–44why, one step back29#[allow(non_upper_case_globals)]30static NtResumeProcess: Lazy<unsafe extern "system" fn(ProcessHandle: HANDLE) -> NTSTATUS> =31 Lazy::new(|| {32 // 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");3536 // 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");4041 // SAFETY: We transmute to the correct function signature42 unsafe { mem::transmute(nt_resume_process) }43 });44Refactor timer moduleDavid Peterbefore adoptiondefafdf
line 45why, one step back45pub struct CPUTimer {Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
line 46why, one step back46 job_object: HANDLE,Bump predicates from 2.1.0 to 2.1.1dependabot[bot]before adoptiond3321dc
lines 47–48why, one step back47}48Refactor timer moduleDavid Peterbefore adoptiondefafdf
line 49why, one step back49impl CPUTimer {Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 50–62why, one step back50 pub unsafe fn start_suspended_process(child: &process::Child) -> Self {51 // SAFETY: Creating a new job object is safe52 let job_object = unsafe { CreateJobObjectW(ptr::null_mut(), ptr::null_mut()) };53 assert!(!job_object.is_null(), "CreateJobObjectW failed");5455 // SAFETY: The job object handle is valid56 let ret = unsafe { AssignProcessToJobObject(job_object, child.as_raw_handle()) };57 assert!(ret != 0, "AssignProcessToJobObject failed");5859 #[cfg(windows_process_extensions_main_thread_handle)]60 {61 // SAFETY: The main thread handle is valid62 let ret = unsafe { ResumeThread(child.main_thread_handle().as_raw_handle()) };Cleanup Windows CPUTimerClemens Wasserbefore adoption7db6178
line 63why, one step back63 assert!(ret != -1 as DWORD, "ResumeThread failed");Bump predicates from 2.1.0 to 2.1.1dependabot[bot]before adoptiond3321dc
line 64why, one step back64 }Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 65–77why, one step back6566 #[cfg(not(windows_process_extensions_main_thread_handle))]67 {68 // Since we can't get the main thread handle on stable rust, we use69 // the undocumented but widely known `NtResumeProcess` function to70 // resume a process by it's handle.7172 // SAFETY: The process handle is valid73 let ret = unsafe { NtResumeProcess(child.as_raw_handle()) };74 assert!(ret == STATUS_SUCCESS, "NtResumeProcess failed");75 }7677 Self { job_object }Bump predicates from 2.1.0 to 2.1.1dependabot[bot]before adoptiond3321dc
lines 78–79why, one step back78 }79Refactor timer moduleDavid Peterbefore adoptiondefafdf
line 80why, one step back80 pub fn stop(&self) -> (Second, Second) {Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 81–82why, one step back81 let mut job_object_info =82 mem::MaybeUninit::<JOBOBJECT_BASIC_ACCOUNTING_INFORMATION>::uninit();Bump predicates from 2.1.0 to 2.1.1dependabot[bot]before adoptiond3321dc
line 83why, one step back83Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
line 84why, one step back84 // SAFETY: A valid job object got created in `start_suspended_process`Shrink unsafe blockcactterbefore adoption2e8ce30
line 85why, one step back85 let res = unsafe {Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 86–91why, one step back86 QueryInformationJobObject(87 self.job_object,88 JobObjectBasicAccountingInformation,89 job_object_info.as_mut_ptr().cast(),90 mem::size_of::<JOBOBJECT_BASIC_ACCOUNTING_INFORMATION>() as u32,91 ptr::null_mut(),Shrink unsafe blockcactterbefore adoption2e8ce30
lines 92–93why, one step back92 )93 };Bump predicates from 2.1.0 to 2.1.1dependabot[bot]before adoptiond3321dc
lines 94–95why, one step back9495 if res != 0 {Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 96–110why, one step back96 // SAFETY: The job object info got correctly initialized97 let job_object_info = unsafe { job_object_info.assume_init() };9899 // SAFETY: The `TotalUserTime` is "The total amount of user-mode execution time for100 // all active processes associated with the job, as well as all terminated processes no101 // longer associated with the job, in 100-nanosecond ticks." and is safe to extract102 let user: i64 = unsafe { job_object_info.TotalUserTime.QuadPart() } / HUNDRED_NS_PER_MS;103104 // SAFETY: The `TotalKernelTime` is "The total amount of kernel-mode execution time105 // for all active processes associated with the job, as well as all terminated106 // processes no longer associated with the job, in 100-nanosecond ticks." and is safe107 // to extract108 let kernel: i64 =109 unsafe { job_object_info.TotalKernelTime.QuadPart() } / HUNDRED_NS_PER_MS;110 (user as f64 * 1e-6, kernel as f64 * 1e-6)Bump predicates from 2.1.0 to 2.1.1dependabot[bot]before adoptiond3321dc
line 111why, one step back111 } else {Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
line 112why, one step back112 (0.0, 0.0)Bump predicates from 2.1.0 to 2.1.1dependabot[bot]before adoptiond3321dc
line 113why, one step back113 }Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 114–115why, one step back114 }115}Bump predicates from 2.1.0 to 2.1.1dependabot[bot]before adoptiond3321dc
line 116why, one step back116Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 117–120why, one step back117impl Drop for CPUTimer {118 fn drop(self: &mut Self) {119 // SAFETY: A valid job object got created in `start_suspended_process`120 unsafe { CloseHandle(self.job_object) };Bump predicates from 2.1.0 to 2.1.1dependabot[bot]before adoptiond3321dc
lines 121–122why, one step back121 }122}