From 9507d05496be7bd87ca769d95cdfec705a3bd6cf Mon Sep 17 00:00:00 2001 From: AAigars Date: Mon, 23 Nov 2026 22:36:37 +0000 Subject: [PATCH] kernelx: the finest system timer for the title (choppy audio, #260) Windows wakes a sleeping thread on its 15.6 ms tick unless the process asks for a finer timer. The retail game's audio mixer sleeps between 5.33 ms chunks, so it made one chunk per tick, a third of real time: measured on its spatial audio stream, sound in 34 % of the device's 10 ms passes and silence in the rest. With the finest timer (0.5 ms), every pass has sound. Windows 11 keeps it while the window is hidden only when the process opts out of power throttling. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/kernelx.rs | 46 +++++++++++++++++++++++++++++++++++ crates/xcl/src/host.rs | 1 + 2 files changed, 47 insertions(+) diff --git a/crates/xcl/src/hle/kernelx.rs b/crates/xcl/src/hle/kernelx.rs index 0b8f2a3fa..97d8542e2 100644 --- a/crates/xcl/src/hle/kernelx.rs +++ b/crates/xcl/src/hle/kernelx.rs @@ -1269,6 +1269,8 @@ struct OsVersionInfoW { #[link(name = "ntdll", kind = "raw-dylib")] extern "system" { fn RtlGetVersion(info: *mut OsVersionInfoW) -> i32; + fn NtQueryTimerResolution(coarsest: *mut u32, finest: *mut u32, current: *mut u32) -> i32; + fn NtSetTimerResolution(desired: u32, set: u8, current: *mut u32) -> i32; } pub fn host_os_version() -> (u32, u32, u32) { @@ -1280,6 +1282,38 @@ pub fn host_os_version() -> (u32, u32, u32) { } } +/// The finest timer Windows offers (0.5 ms) for the title: with the default 15.6 ms tick every +/// sleep rounds up (a retail game's audio mixer ran at a third of real time). Windows 11 keeps +/// it while the window is hidden only when told to. +pub fn fine_timer() { + use windows_sys::Win32::System::Threading::{ + GetCurrentProcess, ProcessPowerThrottling, SetProcessInformation, PROCESS_POWER_THROTTLING_CURRENT_VERSION, + PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION, PROCESS_POWER_THROTTLING_STATE, + }; + let state = PROCESS_POWER_THROTTLING_STATE { + Version: PROCESS_POWER_THROTTLING_CURRENT_VERSION, + ControlMask: PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION, + StateMask: 0, + }; + let size = std::mem::size_of::() as u32; + let kept = unsafe { SetProcessInformation(GetCurrentProcess(), ProcessPowerThrottling, &state as *const _ as *const c_void, size) } != 0; + // in 100 ns units; "finest" is the smallest period the system supports + let (mut coarsest, mut finest, mut was) = (0u32, 0u32, 0u32); + if unsafe { NtQueryTimerResolution(&mut coarsest, &mut finest, &mut was) } < 0 || finest == 0 { + crate::xlog_warn!("timer: NtQueryTimerResolution failed – Windows' default resolution"); + return; + } + let mut now = 0u32; + let status = unsafe { NtSetTimerResolution(finest, 1, &mut now) }; + crate::xlog_info!( + "timer: {} (was {:.2} ms, now {:.2} ms; kept while the window is hidden: {})", + if status >= 0 { "finest resolution for the title" } else { "raising the resolution failed" }, + was as f64 / 10_000.0, + now as f64 / 10_000.0, + if kept { "yes" } else { "no" } + ); +} + pub unsafe extern "system" fn GetSystemOSVersion(out: *mut SystemOsVersionInfo) { if out.is_null() { return; @@ -1621,6 +1655,18 @@ pub unsafe extern "system" fn DisableThreadLibraryCalls(h: usize) -> BOOL { mod tests { use super::*; + #[test] + fn fine_timer_makes_short_sleeps_short() { + // Windows' Sleep (std::thread::sleep has its own high-resolution timer) + fine_timer(); + let t = std::time::Instant::now(); + for _ in 0..20 { + unsafe { windows_sys::Win32::System::Threading::Sleep(1) }; + } + let per = t.elapsed() / 20; + assert!(per < std::time::Duration::from_millis(5), "Sleep(1) took {per:?}: the 15.6 ms tick"); + } + #[test] fn affinity_masks_outside_the_process_are_fitted() { // a mask from the PC's processors is the title's choice diff --git a/crates/xcl/src/host.rs b/crates/xcl/src/host.rs index 07d5bcfb5..a171f2346 100644 --- a/crates/xcl/src/host.rs +++ b/crates/xcl/src/host.rs @@ -105,6 +105,7 @@ pub fn host_main(boot_path: &Path) -> i32 { } crate::stubs::init_db(&boot.config.stub_returns); crate::hle::init(&boot.config); + crate::hle::kernelx::fine_timer(); crate::loader::set_cache_dir(boot.cache_dir()); let mut dirs = vec![boot.game_root.clone()]; if let Some(p) = boot.exe_path.parent() {