Skip to content
Myr

windows_timer.rs

This file

Path

src/timer/windows_timer.rs

134 lines · 5.1 KiB · Rust

Last changed by

Merge pull request #975 from sharkdp/codex/unified-windows-timerDavid Peterbefore adoptionefd57b3

History of this file

Take it

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

Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd

lines 1–3why, one step back
1#![cfg(windows)]
2#![warn(unsafe_op_in_unsafe_fn)]3

Merge pull request #975 from sharkdp/codex/unified-windows-timerDavid Peterbefore adoptionefd57b3

lines 4–5why, one step back
4use std::process::{self, Child, ExitStatus};
5use std::{mem, os::windows::io::AsRawHandle, ptr};

Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd

line 6why, one step back
6

Merge pull request #975 from sharkdp/codex/unified-windows-timerDavid Peterbefore adoptionefd57b3

lines 7–8why, one step back
7use anyhow::Result;
8

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

lines 9–13why, one step back
9use windows_sys::Win32::{
10 Foundation::{CloseHandle, HANDLE},11 System::JobObjects::{12 AssignProcessToJobObject, CreateJobObjectW, JobObjectBasicAccountingInformation,13 QueryInformationJobObject, JOBOBJECT_BASIC_ACCOUNTING_INFORMATION,

Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd

lines 14–16why, one step back
14 },
15};16

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

lines 17–20why, one step back
17#[cfg(feature = "windows_process_extensions_main_thread_handle")]
18use std::os::windows::process::ChildExt;19#[cfg(feature = "windows_process_extensions_main_thread_handle")]20use windows_sys::Win32::System::Threading::ResumeThread;

Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd

line 21why, one step back
21

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

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

Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd

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

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

lines 24–31why, one step back
24#[cfg(not(feature = "windows_process_extensions_main_thread_handle"))]
25use windows_sys::{26 s, w,27 Win32::{28 Foundation::{NTSTATUS, STATUS_SUCCESS},29 System::LibraryLoader::{GetModuleHandleW, GetProcAddress},30 },31};

Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd

line 32why, one step back
32

Merge pull request #964 from sharkdp/codex/windows-timer-precisionDavid Peterbefore adoption1c1be47

line 33why, one step back
33use crate::quantity::{nanosecond, Information, Time, Zero};

Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd

line 34why, one step back
34

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

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

Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd

lines 36–39why, one step back
36#[allow(non_upper_case_globals)]
37static NtResumeProcess: Lazy<unsafe extern "system" fn(ProcessHandle: HANDLE) -> NTSTATUS> =38 Lazy::new(|| {39 // SAFETY: Getting the module handle for ntdll.dll is safe

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

line 40why, one step back
40 let ntdll = unsafe { GetModuleHandleW(w!("ntdll.dll")) };

Update dependenciesDavid Peterbefore adoption2efc633

line 41why, one step back
41 assert!(ntdll != std::ptr::null_mut(), "GetModuleHandleW failed");

Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd

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

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

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

Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd

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

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

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

Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd

lines 48–55why, one step back
48 });
4950pub struct CPUTimer {51 job_object: HANDLE,52}5354impl CPUTimer {55 pub unsafe fn start_suspended_process(child: &process::Child) -> Self {

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

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

Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd

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

Update dependenciesDavid Peterbefore adoption2efc633

lines 60–63why, one step back
60 assert!(
61 job_object != std::ptr::null_mut(),62 "CreateJobObjectW failed"63 );

Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd

lines 64–68why, one step back
64
65 // SAFETY: The job object handle is valid66 let ret = unsafe { AssignProcessToJobObject(job_object, child_handle) };67 assert!(ret != 0, "AssignProcessToJobObject failed");68

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

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

Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd

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

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

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

Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd

lines 74–75why, one step back
74 }
75

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

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

Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd

lines 77–89why, one step back
77 {
78 // Since we can't get the main thread handle on stable rust, we use79 // the undocumented but widely known `NtResumeProcess` function to80 // resume a process by it's handle.8182 // SAFETY: The process handle is valid83 let ret = unsafe { NtResumeProcess(child_handle) };84 assert!(ret == STATUS_SUCCESS, "NtResumeProcess failed");85 }8687 Self { job_object }88 }89

Merge pull request #975 from sharkdp/codex/unified-windows-timerDavid Peterbefore adoptionefd57b3

lines 90–92why, one step back
90 pub fn stop(&self, mut child: Child) -> Result<(Time, Time, Information, ExitStatus)> {
91 let status = child.wait()?;92

Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd

lines 93–110why, one step back
93 let mut job_object_info =
94 mem::MaybeUninit::<JOBOBJECT_BASIC_ACCOUNTING_INFORMATION>::uninit();9596 // SAFETY: A valid job object got created in `start_suspended_process`97 let res = unsafe {98 QueryInformationJobObject(99 self.job_object,100 JobObjectBasicAccountingInformation,101 job_object_info.as_mut_ptr().cast(),102 mem::size_of::<JOBOBJECT_BASIC_ACCOUNTING_INFORMATION>() as u32,103 ptr::null_mut(),104 )105 };106107 if res != 0 {108 // SAFETY: The job object info got correctly initialized109 let job_object_info = unsafe { job_object_info.assume_init() };110

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

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

Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd

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

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

line 113why, one step back
113 // longer associated with the job, in 100-nanosecond ticks."

Merge pull request #964 from sharkdp/codex/windows-timer-precisionDavid Peterbefore adoption1c1be47

line 114why, one step back
114 let user_time = Time::new::<nanosecond>((job_object_info.TotalUserTime as f64) * 100.0);

Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd

line 115why, one step back
115

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

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

Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd

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

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

line 118why, one step back
118 // processes no longer associated with the job, in 100-nanosecond ticks."

Merge pull request #964 from sharkdp/codex/windows-timer-precisionDavid Peterbefore adoption1c1be47

lines 119–120why, one step back
119 let system_time =
120 Time::new::<nanosecond>((job_object_info.TotalKernelTime as f64) * 100.0);

Merge pull request #975 from sharkdp/codex/unified-windows-timerDavid Peterbefore adoptionefd57b3

lines 121–122why, one step back
121
122 Ok((user_time, system_time, Information::zero(), status))

Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd

line 123why, one step back
123 } else {

Merge pull request #975 from sharkdp/codex/unified-windows-timerDavid Peterbefore adoptionefd57b3

line 124why, one step back
124 Ok((Time::zero(), Time::zero(), Information::zero(), status))

Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd

lines 125–129why, one step back
125 }
126 }127}128129impl Drop for CPUTimer {

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

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

Use plt.get_cmap instead of plt.colormapsNicolas van Kempenbefore adoption7684efd

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