Skip to content
Myr

windows_timer.rs

This file

Path

src/timer/windows_timer.rs

119 lines · 4.6 KiB · Rust

Last changed by

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

History of this file

Take it

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

Bump serde_json from 1.0.67 to 1.0.68dependabot[bot]before adoptionc6bc4e3

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

Bump serde_json from 1.0.67 to 1.0.68dependabot[bot]before adoptionc6bc4e3

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

Bump serde_json from 1.0.67 to 1.0.68dependabot[bot]before adoptionc6bc4e3

line 5why, one step back
5

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 6–17why, one step back
6use clap::__macro_refs::once_cell::sync::Lazy;
7use winapi::{8 shared::{ntdef::NTSTATUS, ntstatus::STATUS_SUCCESS},9 um::{10 handleapi::CloseHandle,11 jobapi2::{AssignProcessToJobObject, CreateJobObjectW, QueryInformationJobObject},12 libloaderapi::{GetModuleHandleA, GetProcAddress},13 winnt::{14 JobObjectBasicAccountingInformation, HANDLE, JOBOBJECT_BASIC_ACCOUNTING_INFORMATION,15 },16 },17};

Bump serde_json from 1.0.67 to 1.0.68dependabot[bot]before adoptionc6bc4e3

line 18why, one step back
18

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

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

Move a few modules to utilDavid Peterbefore adoption5c634c9

line 22why, one step back
22use crate::util::units::Second;

Bump serde_json from 1.0.67 to 1.0.68dependabot[bot]before adoptionc6bc4e3

lines 23–25why, one step back
23
24const HUNDRED_NS_PER_MS: i64 = 10;25

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 26–41why, one step back
26#[allow(non_upper_case_globals)]
27static NtResumeProcess: Lazy<unsafe extern "system" fn(ProcessHandle: HANDLE) -> NTSTATUS> =28 Lazy::new(|| {29 // SAFETY: Getting the module handle for ntdll.dll is safe30 let ntdll = unsafe { GetModuleHandleA(b"ntdll.dll\0".as_ptr().cast()) };31 assert!(!ntdll.is_null(), "GetModuleHandleA failed");3233 // SAFETY: The ntdll handle is valid34 let nt_resume_process =35 unsafe { GetProcAddress(ntdll, b"NtResumeProcess\0".as_ptr().cast()) };36 assert!(!nt_resume_process.is_null(), "GetProcAddress failed");3738 // SAFETY: We transmute to the correct function signature39 unsafe { mem::transmute(nt_resume_process) }40 });41

Refactor timer moduleDavid Peterbefore adoptiondefafdf

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

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

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

Bump serde_json from 1.0.67 to 1.0.68dependabot[bot]before adoptionc6bc4e3

lines 44–45why, one step back
44}
45

Refactor timer moduleDavid Peterbefore adoptiondefafdf

line 46why, one step back
46impl CPUTimer {

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 47–60why, one step back
47 pub unsafe fn start_suspended_process(child: &process::Child) -> Self {
48 // SAFETY: Creating a new job object is safe49 let job_object = unsafe { CreateJobObjectW(ptr::null_mut(), ptr::null_mut()) };50 assert!(!job_object.is_null(), "CreateJobObjectW failed");5152 // SAFETY: The job object handle is valid53 let ret = unsafe { AssignProcessToJobObject(job_object, child.as_raw_handle()) };54 assert!(ret != 0, "AssignProcessToJobObject failed");5556 #[cfg(windows_process_extensions_main_thread_handle)]57 {58 // SAFETY: The main thread handle is valid59 let ret = unsafe { ResumeThread(child.main_thread_handle().as_raw_handle()) };60 assert!(ret != -1 as DWORD, "NtResumeProcess failed");

Bump serde_json from 1.0.67 to 1.0.68dependabot[bot]before adoptionc6bc4e3

line 61why, one step back
61 }

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 62–74why, one step back
62
63 #[cfg(not(windows_process_extensions_main_thread_handle))]64 {65 // Since we can't get the main thread handle on stable rust, we use66 // the undocumented but widely known `NtResumeProcess` function to67 // resume a process by it's handle.6869 // SAFETY: The process handle is valid70 let ret = unsafe { NtResumeProcess(child.as_raw_handle()) };71 assert!(ret == STATUS_SUCCESS, "NtResumeProcess failed");72 }7374 Self { job_object }

Bump serde_json from 1.0.67 to 1.0.68dependabot[bot]before adoptionc6bc4e3

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

Refactor timer moduleDavid Peterbefore adoptiondefafdf

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

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

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

Bump serde_json from 1.0.67 to 1.0.68dependabot[bot]before adoptionc6bc4e3

line 80why, one step back
80

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

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

Shrink unsafe blockcactterbefore adoption2e8ce30

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

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 83–88why, one step back
83 QueryInformationJobObject(
84 self.job_object,85 JobObjectBasicAccountingInformation,86 job_object_info.as_mut_ptr().cast(),87 mem::size_of::<JOBOBJECT_BASIC_ACCOUNTING_INFORMATION>() as u32,88 ptr::null_mut(),

Shrink unsafe blockcactterbefore adoption2e8ce30

lines 89–90why, one step back
89 )
90 };

Bump serde_json from 1.0.67 to 1.0.68dependabot[bot]before adoptionc6bc4e3

lines 91–92why, one step back
91
92 if res != 0 {

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 93–107why, one step back
93 // SAFETY: The job object info got correctly initialized
94 let job_object_info = unsafe { job_object_info.assume_init() };9596 // SAFETY: The `TotalUserTime` is "The total amount of user-mode execution time for97 // all active processes associated with the job, as well as all terminated processes no98 // longer associated with the job, in 100-nanosecond ticks." and is safe to extract99 let user: i64 = unsafe { job_object_info.TotalUserTime.QuadPart() } / HUNDRED_NS_PER_MS;100101 // SAFETY: The `TotalKernelTime` is "The total amount of kernel-mode execution time102 // for all active processes associated with the job, as well as all terminated103 // processes no longer associated with the job, in 100-nanosecond ticks." and is safe104 // to extract105 let kernel: i64 =106 unsafe { job_object_info.TotalKernelTime.QuadPart() } / HUNDRED_NS_PER_MS;107 (user as f64 * 1e-6, kernel as f64 * 1e-6)

Bump serde_json from 1.0.67 to 1.0.68dependabot[bot]before adoptionc6bc4e3

line 108why, one step back
108 } else {

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

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

Bump serde_json from 1.0.67 to 1.0.68dependabot[bot]before adoptionc6bc4e3

line 110why, one step back
110 }

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

lines 111–112why, one step back
111 }
112}

Bump serde_json from 1.0.67 to 1.0.68dependabot[bot]before adoptionc6bc4e3

line 113why, one step back
113

Fix user and kernel times on WindowsClemens Wasserbefore adoptionac8c114

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

Bump serde_json from 1.0.67 to 1.0.68dependabot[bot]before adoptionc6bc4e3

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