Skip to content
Myr

windows_timer.rs

This file

Path

src/timer/windows_timer.rs

124 lines · 4.7 KiB · Rust

Last changed by

Fix winapi type definition changeClemens Wasserbefore adoptionccda6c4

History of this file

Take it

The walk stopped after 200 commits: lines older than 62e82a9 point at it.

Update CHANGELOGDavid Peterbefore adoption62e82a9

line 1why, one step back
1#![cfg(windows)]

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

line 2why, one step back
2#![warn(unsafe_op_in_unsafe_fn)]

Update CHANGELOGDavid Peterbefore adoption62e82a9

line 3why, one step back
3

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

line 4why, one step back
4use std::{mem, os::windows::io::AsRawHandle, process, ptr};

Update CHANGELOGDavid Peterbefore adoption62e82a9

line 5why, one step back
5

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 6–16why, one step back
6use 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};

Update CHANGELOGDavid Peterbefore adoption62e82a9

line 17why, one step back
17

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 18–20why, one step back
18#[cfg(windows_process_extensions_main_thread_handle)]
19use winapi::shared::minwindef::DWORD;20

Cleanup Windows CPUTimerClemens Wasserbefore adoption7db6178

lines 21–23why, one step back
21#[cfg(not(windows_process_extensions_main_thread_handle))]
22use once_cell::sync::Lazy;23

Update CHANGELOGDavid Peterbefore adoption62e82a9

lines 24–27why, one step back
24use crate::util::units::Second;
2526const HUNDRED_NS_PER_MS: i64 = 10;27

Cleanup Windows CPUTimerClemens Wasserbefore adoption7db6178

line 28why, one step back
28#[cfg(not(windows_process_extensions_main_thread_handle))]

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 29–44why, one step back
29#[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 });44

Refactor timer moduleDavid Peterbefore adoptiondefafdf

line 45why, one step back
45pub struct CPUTimer {

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

line 46why, one step back
46 job_object: HANDLE,

Update CHANGELOGDavid Peterbefore adoption62e82a9

lines 47–48why, one step back
47}
48

Refactor timer moduleDavid Peterbefore adoptiondefafdf

line 49why, one step back
49impl CPUTimer {

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

line 50why, one step back
50 pub unsafe fn start_suspended_process(child: &process::Child) -> Self {

Fix winapi type definition changeClemens Wasserbefore adoptionccda6c4

lines 51–52why, one step back
51 let child_handle = child.as_raw_handle().cast();
52

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 53–57why, one step back
53 // SAFETY: Creating a new job object is safe
54 let job_object = unsafe { CreateJobObjectW(ptr::null_mut(), ptr::null_mut()) };55 assert!(!job_object.is_null(), "CreateJobObjectW failed");5657 // SAFETY: The job object handle is valid

Fix winapi type definition changeClemens Wasserbefore adoptionccda6c4

line 58why, one step back
58 let ret = unsafe { AssignProcessToJobObject(job_object, child_handle) };

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 59–64why, one step back
59 assert!(ret != 0, "AssignProcessToJobObject failed");
6061 #[cfg(windows_process_extensions_main_thread_handle)]62 {63 // SAFETY: The main thread handle is valid64 let ret = unsafe { ResumeThread(child.main_thread_handle().as_raw_handle()) };

Cleanup Windows CPUTimerClemens Wasserbefore adoption7db6178

line 65why, one step back
65 assert!(ret != -1 as DWORD, "ResumeThread failed");

Update CHANGELOGDavid Peterbefore adoption62e82a9

line 66why, one step back
66 }

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 67–74why, one step back
67
68 #[cfg(not(windows_process_extensions_main_thread_handle))]69 {70 // Since we can't get the main thread handle on stable rust, we use71 // the undocumented but widely known `NtResumeProcess` function to72 // resume a process by it's handle.7374 // SAFETY: The process handle is valid

Fix winapi type definition changeClemens Wasserbefore adoptionccda6c4

line 75why, one step back
75 let ret = unsafe { NtResumeProcess(child_handle) };

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 76–79why, one step back
76 assert!(ret == STATUS_SUCCESS, "NtResumeProcess failed");
77 }7879 Self { job_object }

Update CHANGELOGDavid Peterbefore adoption62e82a9

lines 80–81why, one step back
80 }
81

Refactor timer moduleDavid Peterbefore adoptiondefafdf

line 82why, one step back
82 pub fn stop(&self) -> (Second, Second) {

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 83–84why, one step back
83 let mut job_object_info =
84 mem::MaybeUninit::<JOBOBJECT_BASIC_ACCOUNTING_INFORMATION>::uninit();

Update CHANGELOGDavid Peterbefore adoption62e82a9

line 85why, one step back
85

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

line 86why, one step back
86 // SAFETY: A valid job object got created in `start_suspended_process`

Shrink unsafe blockcactterbefore adoption2e8ce30

line 87why, one step back
87 let res = unsafe {

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 88–93why, one step back
88 QueryInformationJobObject(
89 self.job_object,90 JobObjectBasicAccountingInformation,91 job_object_info.as_mut_ptr().cast(),92 mem::size_of::<JOBOBJECT_BASIC_ACCOUNTING_INFORMATION>() as u32,93 ptr::null_mut(),

Shrink unsafe blockcactterbefore adoption2e8ce30

lines 94–95why, one step back
94 )
95 };

Update CHANGELOGDavid Peterbefore adoption62e82a9

lines 96–97why, one step back
96
97 if res != 0 {

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 98–112why, one step back
98 // SAFETY: The job object info got correctly initialized
99 let job_object_info = unsafe { job_object_info.assume_init() };100101 // SAFETY: The `TotalUserTime` is "The total amount of user-mode execution time for102 // all active processes associated with the job, as well as all terminated processes no103 // longer associated with the job, in 100-nanosecond ticks." and is safe to extract104 let user: i64 = unsafe { job_object_info.TotalUserTime.QuadPart() } / HUNDRED_NS_PER_MS;105106 // SAFETY: The `TotalKernelTime` is "The total amount of kernel-mode execution time107 // for all active processes associated with the job, as well as all terminated108 // processes no longer associated with the job, in 100-nanosecond ticks." and is safe109 // to extract110 let kernel: i64 =111 unsafe { job_object_info.TotalKernelTime.QuadPart() } / HUNDRED_NS_PER_MS;112 (user as f64 * 1e-6, kernel as f64 * 1e-6)

Update CHANGELOGDavid Peterbefore adoption62e82a9

line 113why, one step back
113 } else {

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

line 114why, one step back
114 (0.0, 0.0)

Update CHANGELOGDavid Peterbefore adoption62e82a9

line 115why, one step back
115 }

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 116–117why, one step back
116 }
117}

Update CHANGELOGDavid Peterbefore adoption62e82a9

line 118why, one step back
118

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 119–122why, one step back
119impl Drop for CPUTimer {
120 fn drop(self: &mut Self) {121 // SAFETY: A valid job object got created in `start_suspended_process`122 unsafe { CloseHandle(self.job_object) };

Update CHANGELOGDavid Peterbefore adoption62e82a9

lines 123–124why, one step back
123 }
124}