Skip to content
Myr

windows_timer.rs

This file

Path

src/timer/windows_timer.rs

128 lines · 4.9 KiB · Rust

Last changed by

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

History of this file

Take it

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

Update CHANGELOGDavid Peterbefore adoption5924ac3

lines 1–5why, one step back
1#![cfg(windows)]
2#![warn(unsafe_op_in_unsafe_fn)]34use std::{mem, os::windows::io::AsRawHandle, process, ptr};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,

Update CHANGELOGDavid Peterbefore adoption5924ac3

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

Update CHANGELOGDavid Peterbefore adoption5924ac3

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

Update CHANGELOGDavid Peterbefore adoption5924ac3

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

Update CHANGELOGDavid Peterbefore adoption5924ac3

line 29why, one step back
29

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

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

Update CHANGELOGDavid Peterbefore adoption5924ac3

line 31why, one step back
31

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

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

Update CHANGELOGDavid Peterbefore adoption5924ac3

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

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

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

Update dependenciesDavid Peterbefore adoption2efc633

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

Update CHANGELOGDavid Peterbefore adoption5924ac3

lines 39–40why, one step back
39
40 // SAFETY: The ntdll handle is valid

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

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

Update CHANGELOGDavid Peterbefore adoption5924ac3

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

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

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

Update CHANGELOGDavid Peterbefore adoption5924ac3

lines 45–52why, one step back
45 });
4647pub struct CPUTimer {48 job_object: HANDLE,49}5051impl CPUTimer {52 pub unsafe fn start_suspended_process(child: &process::Child) -> Self {

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

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

Fix winapi type definition changeClemens Wasserbefore adoptionccda6c4

line 54why, one step back
54

Update CHANGELOGDavid Peterbefore adoption5924ac3

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

Update dependenciesDavid Peterbefore adoption2efc633

lines 57–60why, one step back
57 assert!(
58 job_object != std::ptr::null_mut(),59 "CreateJobObjectW failed"60 );

Update CHANGELOGDavid Peterbefore adoption5924ac3

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

Fix winapi type definition changeClemens Wasserbefore adoptionccda6c4

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

Update CHANGELOGDavid Peterbefore adoption5924ac3

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

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

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

Update CHANGELOGDavid Peterbefore adoption5924ac3

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

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

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

Update CHANGELOGDavid Peterbefore adoption5924ac3

lines 71–72why, one step back
71 }
72

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

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

Update CHANGELOGDavid Peterbefore adoption5924ac3

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

Fix winapi type definition changeClemens Wasserbefore adoptionccda6c4

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

Update CHANGELOGDavid Peterbefore adoption5924ac3

lines 81–86why, one step back
81 assert!(ret == STATUS_SUCCESS, "NtResumeProcess failed");
82 }8384 Self { job_object }85 }86

Merge pull request #961 from sharkdp/codex/uom-quantitiesDavid Peterbefore adoption975a25c

line 87why, one step back
87 pub fn stop(&self) -> (Time, Time, Information) {

Update CHANGELOGDavid Peterbefore adoption5924ac3

lines 88–105why, one step back
88 let mut job_object_info =
89 mem::MaybeUninit::<JOBOBJECT_BASIC_ACCOUNTING_INFORMATION>::uninit();9091 // SAFETY: A valid job object got created in `start_suspended_process`92 let res = unsafe {93 QueryInformationJobObject(94 self.job_object,95 JobObjectBasicAccountingInformation,96 job_object_info.as_mut_ptr().cast(),97 mem::size_of::<JOBOBJECT_BASIC_ACCOUNTING_INFORMATION>() as u32,98 ptr::null_mut(),99 )100 };101102 if res != 0 {103 // SAFETY: The job object info got correctly initialized104 let job_object_info = unsafe { job_object_info.assume_init() };105

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

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

Update CHANGELOGDavid Peterbefore adoption5924ac3

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

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

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

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

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

Update CHANGELOGDavid Peterbefore adoption5924ac3

line 110why, one step back
110

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

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

Update CHANGELOGDavid Peterbefore adoption5924ac3

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

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

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

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

lines 114–116why, one step back
114 let system_time =
115 Time::new::<nanosecond>((job_object_info.TotalKernelTime as f64) * 100.0);116 (user_time, system_time, Information::zero())

Update CHANGELOGDavid Peterbefore adoption5924ac3

line 117why, one step back
117 } else {

Merge pull request #961 from sharkdp/codex/uom-quantitiesDavid Peterbefore adoption975a25c

line 118why, one step back
118 (Time::zero(), Time::zero(), Information::zero())

Update CHANGELOGDavid Peterbefore adoption5924ac3

lines 119–123why, one step back
119 }
120 }121}122123impl Drop for CPUTimer {

Use windows-sys and fix feature flagClemens Wasserbefore adoptiond2fb7d4

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

Update CHANGELOGDavid Peterbefore adoption5924ac3

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