Skip to content
Myr

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 file

Take it

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

Rename parse to from_strDavid Peterbefore adoptionf9e62e5

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)]

Rename parse to from_strDavid Peterbefore adoptionf9e62e5

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};

Rename parse to from_strDavid Peterbefore adoptionf9e62e5

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};

Rename parse to from_strDavid Peterbefore adoptionf9e62e5

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

Rename parse to from_strDavid Peterbefore adoptionf9e62e5

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,

Rename parse to from_strDavid Peterbefore adoptionf9e62e5

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

lines 50–62why, one step back
50 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 back
63 assert!(ret != -1 as DWORD, "ResumeThread failed");

Rename parse to from_strDavid Peterbefore adoptionf9e62e5

line 64why, one step back
64 }

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 65–77why, one step back
65
66 #[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 }

Rename parse to from_strDavid Peterbefore adoptionf9e62e5

lines 78–79why, one step back
78 }
79

Refactor timer moduleDavid Peterbefore adoptiondefafdf

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

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

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

Rename parse to from_strDavid Peterbefore adoptionf9e62e5

line 83why, one step back
83

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

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

Shrink unsafe blockcactterbefore adoption2e8ce30

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

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 86–91why, one step back
86 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 back
92 )
93 };

Rename parse to from_strDavid Peterbefore adoptionf9e62e5

lines 94–95why, one step back
94
95 if res != 0 {

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 96–110why, one step back
96 // SAFETY: The job object info got correctly initialized
97 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)

Rename parse to from_strDavid Peterbefore adoptionf9e62e5

line 111why, one step back
111 } else {

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

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

Rename parse to from_strDavid Peterbefore adoptionf9e62e5

line 113why, one step back
113 }

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 114–115why, one step back
114 }
115}

Rename parse to from_strDavid Peterbefore adoptionf9e62e5

line 116why, one step back
116

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 117–120why, one step back
117impl 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) };

Rename parse to from_strDavid Peterbefore adoptionf9e62e5

lines 121–122why, one step back
121 }
122}