Skip to content
Myr

windows_timer.rs

This file

Path

src/timer/windows_timer.rs

126 lines · 4.8 KiB · Rust

Last changed by

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

History of this file

Take it

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

Remove the TestExporterDavid Peterbefore adoption9a46bcc

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

Remove the TestExporterDavid Peterbefore adoption9a46bcc

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

Remove the TestExporterDavid Peterbefore adoption9a46bcc

line 5why, one step back
5

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

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

Remove the TestExporterDavid Peterbefore adoption9a46bcc

line 13why, one step back
13

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

lines 14–17why, one step back
14#[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 back
18

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

line 19why, one step back
19#[cfg(not(feature = "windows_process_extensions_main_thread_handle"))]

Cleanup Windows CPUTimerClemens Wasserbefore adoption7db6178

line 20why, one step back
20use once_cell::sync::Lazy;

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

lines 21–28why, one step back
21#[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 back
29

Remove the TestExporterDavid Peterbefore adoption9a46bcc

lines 30–33why, one step back
30use crate::util::units::Second;
3132const HUNDRED_NS_PER_MS: i64 = 10;33

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

line 34why, one step back
34#[cfg(not(feature = "windows_process_extensions_main_thread_handle"))]

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 35–38why, one step back
35#[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 safe

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

lines 39–40why, one step back
39 let ntdll = unsafe { GetModuleHandleW(w!("ntdll.dll")) };
40 assert!(ntdll != 0, "GetModuleHandleW failed");

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 41–42why, one step back
41
42 // SAFETY: The ntdll handle is valid

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

line 43why, one step back
43 let nt_resume_process = unsafe { GetProcAddress(ntdll, s!("NtResumeProcess")) };

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 44–45why, one step back
44
45 // SAFETY: We transmute to the correct function signature

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

line 46why, one step back
46 unsafe { mem::transmute(nt_resume_process.unwrap()) }

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

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

Remove the TestExporterDavid Peterbefore adoption9a46bcc

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

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

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

Remove the TestExporterDavid Peterbefore adoption9a46bcc

lines 51–53why, one step back
51}
5253impl CPUTimer {

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

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

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

line 55why, one step back
55 let child_handle = child.as_raw_handle() as HANDLE;

Fix winapi type definition changeClemens Wasserbefore adoptionccda6c4

line 56why, one step back
56

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 57–58why, one step back
57 // SAFETY: Creating a new job object is safe
58 let job_object = unsafe { CreateJobObjectW(ptr::null_mut(), ptr::null_mut()) };

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

line 59why, one step back
59 assert!(job_object != 0, "CreateJobObjectW failed");

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 60–61why, one step back
60
61 // SAFETY: The job object handle is valid

Fix winapi type definition changeClemens Wasserbefore adoptionccda6c4

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

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 63–64why, one step back
63 assert!(ret != 0, "AssignProcessToJobObject failed");
64

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

line 65why, one step back
65 #[cfg(feature = "windows_process_extensions_main_thread_handle")]

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 66–67why, one step back
66 {
67 // SAFETY: The main thread handle is valid

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

lines 68–69why, one step back
68 let ret = unsafe { ResumeThread(child.main_thread_handle().as_raw_handle() as HANDLE) };
69 assert!(ret != u32::MAX, "ResumeThread failed");

Remove the TestExporterDavid Peterbefore adoption9a46bcc

line 70why, one step back
70 }

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

line 71why, one step back
71

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

line 72why, one step back
72 #[cfg(not(feature = "windows_process_extensions_main_thread_handle"))]

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 73–78why, one step back
73 {
74 // Since we can't get the main thread handle on stable rust, we use75 // the undocumented but widely known `NtResumeProcess` function to76 // resume a process by it's handle.7778 // SAFETY: The process handle is valid

Fix winapi type definition changeClemens Wasserbefore adoptionccda6c4

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

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 80–83why, one step back
80 assert!(ret == STATUS_SUCCESS, "NtResumeProcess failed");
81 }8283 Self { job_object }

Remove the TestExporterDavid Peterbefore adoption9a46bcc

lines 84–86why, one step back
84 }
8586 pub fn stop(&self) -> (Second, Second) {

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

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

Remove the TestExporterDavid Peterbefore adoption9a46bcc

line 89why, one step back
89

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

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

Shrink unsafe blockcactterbefore adoption2e8ce30

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

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 92–97why, one step back
92 QueryInformationJobObject(
93 self.job_object,94 JobObjectBasicAccountingInformation,95 job_object_info.as_mut_ptr().cast(),96 mem::size_of::<JOBOBJECT_BASIC_ACCOUNTING_INFORMATION>() as u32,97 ptr::null_mut(),

Shrink unsafe blockcactterbefore adoption2e8ce30

lines 98–99why, one step back
98 )
99 };

Remove the TestExporterDavid Peterbefore adoption9a46bcc

lines 100–101why, one step back
100
101 if res != 0 {

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 102–104why, one step back
102 // SAFETY: The job object info got correctly initialized
103 let job_object_info = unsafe { job_object_info.assume_init() };104

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

line 105why, one step back
105 // The `TotalUserTime` is "The total amount of user-mode execution time for

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

line 106why, one step back
106 // all active processes associated with the job, as well as all terminated processes no

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

lines 107–108why, one step back
107 // longer associated with the job, in 100-nanosecond ticks."
108 let user: i64 = job_object_info.TotalUserTime / HUNDRED_NS_PER_MS;

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

line 109why, one step back
109

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

line 110why, one step back
110 // The `TotalKernelTime` is "The total amount of kernel-mode execution time

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

line 111why, one step back
111 // for all active processes associated with the job, as well as all terminated

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

lines 112–113why, one step back
112 // processes no longer associated with the job, in 100-nanosecond ticks."
113 let kernel: i64 = job_object_info.TotalKernelTime / HUNDRED_NS_PER_MS;

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

line 114why, one step back
114 (user as f64 * 1e-6, kernel as f64 * 1e-6)

Remove the TestExporterDavid Peterbefore adoption9a46bcc

line 115why, one step back
115 } else {

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

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

Remove the TestExporterDavid Peterbefore adoption9a46bcc

line 117why, one step back
117 }

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 118–119why, one step back
118 }
119}

Remove the TestExporterDavid Peterbefore adoption9a46bcc

line 120why, one step back
120

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

line 121why, one step back
121impl Drop for CPUTimer {

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

line 122why, one step back
122 fn drop(&mut self) {

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 123–124why, one step back
123 // SAFETY: A valid job object got created in `start_suspended_process`
124 unsafe { CloseHandle(self.job_object) };

Remove the TestExporterDavid Peterbefore adoption9a46bcc

lines 125–126why, one step back
125 }
126}