windows_timer.rs
This file
Path
src/timer/windows_timer.rs
129 lines · 4.8 KiB · Rust
Last changed by
Initial work on tracking memory usageDavid Peterbefore adoption6556b2b
History of this fileTake it
The walk stopped after 200 commits: lines older than 3013c9d point at it.
Bump clap from 3.2.8 to 3.2.16dependabot[bot]before adoption3013c9d
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 clap from 3.2.8 to 3.2.16dependabot[bot]before adoption3013c9d
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 clap from 3.2.8 to 3.2.16dependabot[bot]before adoption3013c9d
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};Bump clap from 3.2.8 to 3.2.16dependabot[bot]before adoption3013c9d
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 back29Bump clap from 3.2.8 to 3.2.16dependabot[bot]before adoption3013c9d
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
line 39why, one step back39 let ntdll = unsafe { GetModuleHandleW(w!("ntdll.dll")) };Update dependenciesDavid Peterbefore adoption2efc633
line 40why, one step back40 assert!(ntdll != std::ptr::null_mut(), "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 });48Bump clap from 3.2.8 to 3.2.16dependabot[bot]before adoption3013c9d
line 49why, one step back49pub struct CPUTimer {Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
line 50why, one step back50 job_object: HANDLE,Bump clap from 3.2.8 to 3.2.16dependabot[bot]before adoption3013c9d
lines 51–53why, one step back51}5253impl 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()) };Update dependenciesDavid Peterbefore adoption2efc633
lines 59–62why, one step back59 assert!(60 job_object != std::ptr::null_mut(),61 "CreateJobObjectW failed"62 );Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 63–64why, one step back6364 // SAFETY: The job object handle is validFix winapi type definition changeClemens Wasserbefore adoptionccda6c4
line 65why, one step back65 let ret = unsafe { AssignProcessToJobObject(job_object, child_handle) };Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 66–67why, one step back66 assert!(ret != 0, "AssignProcessToJobObject failed");67Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 68why, one step back68 #[cfg(feature = "windows_process_extensions_main_thread_handle")]Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 69–70why, one step back69 {70 // SAFETY: The main thread handle is validUse windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
lines 71–72why, one step back71 let ret = unsafe { ResumeThread(child.main_thread_handle().as_raw_handle() as HANDLE) };72 assert!(ret != u32::MAX, "ResumeThread failed");Bump clap from 3.2.8 to 3.2.16dependabot[bot]before adoption3013c9d
line 73why, one step back73 }Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
line 74why, one step back74Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 75why, one step back75 #[cfg(not(feature = "windows_process_extensions_main_thread_handle"))]Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 76–81why, one step back76 {77 // Since we can't get the main thread handle on stable rust, we use78 // the undocumented but widely known `NtResumeProcess` function to79 // resume a process by it's handle.8081 // SAFETY: The process handle is validFix winapi type definition changeClemens Wasserbefore adoptionccda6c4
line 82why, one step back82 let ret = unsafe { NtResumeProcess(child_handle) };Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 83–86why, one step back83 assert!(ret == STATUS_SUCCESS, "NtResumeProcess failed");84 }8586 Self { job_object }Bump clap from 3.2.8 to 3.2.16dependabot[bot]before adoption3013c9d
lines 87–88why, one step back87 }88Initial work on tracking memory usageDavid Peterbefore adoption6556b2b
line 89why, one step back89 pub fn stop(&self) -> (Second, Second, u64) {Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 90–91why, one step back90 let mut job_object_info =91 mem::MaybeUninit::<JOBOBJECT_BASIC_ACCOUNTING_INFORMATION>::uninit();Bump clap from 3.2.8 to 3.2.16dependabot[bot]before adoption3013c9d
line 92why, one step back92Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
line 93why, one step back93 // SAFETY: A valid job object got created in `start_suspended_process`Shrink unsafe blockcactterbefore adoption2e8ce30
line 94why, one step back94 let res = unsafe {Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 95–100why, one step back95 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(),Shrink unsafe blockcactterbefore adoption2e8ce30
lines 101–102why, one step back101 )102 };Bump clap from 3.2.8 to 3.2.16dependabot[bot]before adoption3013c9d
lines 103–104why, one step back103104 if res != 0 {Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 105–107why, one step back105 // 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 forFix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
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
lines 110–111why, one step back110 // longer associated with the job, in 100-nanosecond ticks."111 let user: i64 = job_object_info.TotalUserTime / HUNDRED_NS_PER_MS;Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
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 timeFix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
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
lines 115–116why, one step back115 // processes no longer associated with the job, in 100-nanosecond ticks."116 let kernel: i64 = job_object_info.TotalKernelTime / HUNDRED_NS_PER_MS;Initial work on tracking memory usageDavid Peterbefore adoption6556b2b
line 117why, one step back117 (user as f64 * 1e-6, kernel as f64 * 1e-6, 0)Bump clap from 3.2.8 to 3.2.16dependabot[bot]before adoption3013c9d
line 118why, one step back118 } else {Initial work on tracking memory usageDavid Peterbefore adoption6556b2b
line 119why, one step back119 (0.0, 0.0, 0)Bump clap from 3.2.8 to 3.2.16dependabot[bot]before adoption3013c9d
line 120why, one step back120 }Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 121–122why, one step back121 }122}Bump clap from 3.2.8 to 3.2.16dependabot[bot]before adoption3013c9d
line 123why, one step back123Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
line 124why, one step back124impl Drop for CPUTimer {Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4
line 125why, one step back125 fn drop(&mut self) {Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114
lines 126–127why, one step back126 // SAFETY: A valid job object got created in `start_suspended_process`127 unsafe { CloseHandle(self.job_object) };Bump clap from 3.2.8 to 3.2.16dependabot[bot]before adoption3013c9d
lines 128–129why, one step back128 }129}