From 85293b50fd8b1b6d16e6d16889e405916073d81b Mon Sep 17 00:00:00 2001 From: David Peter Date: Mon, 5 Oct 2026 11:15:01 +0200 Subject: [PATCH] Preserve 100-nanosecond precision in Windows CPU times --- src/quantity/mod.rs | 4 +++- src/timer/windows_timer.rs | 15 +++++---------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/quantity/mod.rs b/src/quantity/mod.rs index ac63722..84987aa 100644 --- a/src/quantity/mod.rs +++ b/src/quantity/mod.rs @@ -4,7 +4,9 @@ pub use uom::num_traits::Zero; pub use uom::si::f64::{Information, Ratio, Time}; pub use uom::si::information::byte; pub use uom::si::ratio::ratio; -pub use uom::si::time::{microsecond, nanosecond, second}; +#[cfg(any(not(windows), test))] +pub use uom::si::time::microsecond; +pub use uom::si::time::{nanosecond, second}; pub use units::TimeUnit; diff --git a/src/timer/windows_timer.rs b/src/timer/windows_timer.rs index 2c29c8c..1797c67 100644 --- a/src/timer/windows_timer.rs +++ b/src/timer/windows_timer.rs @@ -27,9 +27,7 @@ use windows_sys::{ }, }; -use crate::quantity::{microsecond, Information, Time, Zero}; - -const HUNDRED_NS_PER_MS: i64 = 10; +use crate::quantity::{nanosecond, Information, Time, Zero}; #[cfg(not(feature = "windows_process_extensions_main_thread_handle"))] #[allow(non_upper_case_globals)] @@ -108,17 +106,14 @@ impl CPUTimer { // The `TotalUserTime` is "The total amount of user-mode execution time for // all active processes associated with the job, as well as all terminated processes no // longer associated with the job, in 100-nanosecond ticks." - let user: i64 = job_object_info.TotalUserTime / HUNDRED_NS_PER_MS; + let user_time = Time::new::((job_object_info.TotalUserTime as f64) * 100.0); // The `TotalKernelTime` is "The total amount of kernel-mode execution time // for all active processes associated with the job, as well as all terminated // processes no longer associated with the job, in 100-nanosecond ticks." - let kernel: i64 = job_object_info.TotalKernelTime / HUNDRED_NS_PER_MS; - ( - Time::new::(user as f64), - Time::new::(kernel as f64), - Information::zero(), - ) + let system_time = + Time::new::((job_object_info.TotalKernelTime as f64) * 100.0); + (user_time, system_time, Information::zero()) } else { (Time::zero(), Time::zero(), Information::zero()) }