Skip to content

kernelx: the finest system timer for the title (choppy audio, #260) - #300

Open
AAigars wants to merge 1 commit into
ivall:mainfrom
AAigars:kernelx-timer-resolution
Open

AAigars wants to merge 1 commit into
ivall:mainfrom
AAigars:kernelx-timer-resolution

Conversation

@AAigars

@AAigars AAigars commented Nov 23, 2026

Copy link
Copy Markdown

Fixes #260.

The choppy sound in the retail GTA VI is not the audio processor: the game mixes on the CPU and plays through Windows spatial audio (ISpatialAudioClient, two static objects, 48 kHz float), and that stream was fed on time but with sound in only 34 % of its 10 ms passes. The mixer sleeps between 5.33 ms chunks, and without a finer timer Windows wakes it on its 15.6 ms tick, so it made a third of real time (5.33 / 15.6 = 34 %). XCL now asks for the finest timer Windows offers (0.5 ms) at start, and opts out of the Windows 11 power throttling that drops it while the window is hidden. With it every pass has sound.

What is in it

  • kernelx::fine_timer (crates/xcl/src/hle/kernelx.rs): NtSetTimerResolution to the finest resolution NtQueryTimerResolution reports, SetProcessInformation(ProcessPowerThrottling, IGNORE_TIMER_RESOLUTION), one timer: log line; test fine_timer_makes_short_sleeps_short (20 × Windows Sleep(1), average under 5 ms: 15.4 ms without the fix, fails; passes with it)
  • called from host_main after hle::init (crates/xcl/src/host.rs)

Tested

  • NVIDIA GeForce RTX 4070, driver 32.0.16.2101; AMD Ryzen 7 9800X3D, 32 GB; Windows 11 Pro build 26200; Rust 1.96.1; release
  • cargo test --workspace --release -- --test-threads=4: 652 passed, 0 failed, 40 ignored (SKIPPED: gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware – no wave64)
  • GTA VI retail (RockstarGamesInc.28872443D20A7 1.0.2.1), XCL_NETWORK=0: the menu's sound is continuous instead of choppy. Measured with temporary logging on the game's spatial audio stream (not in this PR): before, sound in 172 of 500 passes per 5 s, evenly spread; after, 500 of 500 once the sound starts.

Regression run

main:

xcl-smoke v1
commit: 9ab7d763197c7f420bff10cd9bf1310f07e2a9aa
gpu: NVIDIA GeForce RTX 4070
title: GTA 6 XBOX
seconds: 60
exit: 0x0
frames shown: 1487 (PresentX 1487)
presents per 2 s (last 3): 60 61 59
device removals: 0
crashes: 0
peak commit: 14673 MB

PR:

xcl-smoke v1
commit: 9507d05496be7bd87ca769d95cdfec705a3bd6cf
gpu: NVIDIA GeForce RTX 4070
title: GTA 6 XBOX
seconds: 60
exit: 0x0
frames shown: 1482 (PresentX 1482)
presents per 2 s (last 3): 60 61 60
device removals: 0
crashes: 0
peak commit: 14667 MB

5 frames fewer over 60 s (0.3 %) at the same rate at the end; one run each, so within run-to-run noise.

Not tested

  • AMD GPUs; Windows 10 (where the power-throttling opt-out does not exist and the call fails harmlessly)
  • other titles (Minecraft, Unicorn Overlord, Fortnite, XDefiant)
  • debug build
  • power use with the finer timer (laptops)

AI use

  • Claude Code, Claude Opus 5.5: found the cause (temporary logging on the game's audio calls), wrote the code, the test and this description; I read the diff, ran the tests and the regression runs, and listened to the game before and after.

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 <noreply@anthropic.com>
@likecrank-dotcom

Copy link
Copy Markdown

Independent native Windows/NVIDIA validation of #300 at 9507d05496be7bd87ca769d95cdfec705a3bd6cf (clean checkout).

Tested

  • Ryzen 5 3600, RTX 5060 Ti (NVIDIA 621.14 / 32.0.16.2114), 32 GB RAM; Windows 11 Pro 10.0.26300; Rust 1.100.0 MSVC, MSVC 19.44, SDK 10.0.26100.0, DXC 1.9.2602.17. Debug build, an environment listed as not tested in the PR description.
  • cargo build --workspace: exit 0; one pre-existing fetch_update deprecation warning.
  • cargo test --workspace -- --test-threads=4 --nocapture: exit 0; 652 passed, 0 failed, 40 ignored over 17 result blocks (lib: 612 passed, 0 failed, 20 ignored). The synthetic-title end-to-end test passes.
  • fine_timer_makes_short_sleeps_short passes. Log: was 15.62 ms, now 0.50 ms; kept while the window is hidden: yes.

Separate before/after probe, using the actual PR function
A small local Rust executable depends on this checkout's xcl library. In one fresh process, it measures three batches of 20 calls to Windows Sleep(1), calls xcl::hle::kernelx::fine_timer() once, and measures three more batches. Instant measures each batch; no std::thread::sleep, no title is loaded. It was run after the workspace tests ended, using cargo run --offline --manifest-path <local-probe>/Cargo.toml.

batch mean Sleep(1), before mean Sleep(1), after
1 8.279205 ms 1.476785 ms
2 7.283230 ms 1.541250 ms
3 7.638460 ms 1.572365 ms

This reproduces a shorter wait after the timer request on this host. The before values are the measured 7-8 ms, not assumed to equal the queried 15.62 ms system resolution. This does not measure audio quality, mixer continuity, or game performance.

SKIPPED / Not tested
Four explicit skips: gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware (no wave64); test_title_draws_with_a_root_signature_under_the_debug_layer (no d3d12SDKLayers.dll); uploads_start_from_each_subresources_state (no D3D12 debug layer, 0x887A002D). No XCL_SAMPLE variables were set. No retail title/smoke blocks, no listening test, no verification with an occluded/minimized title window, no power-use measurement, AMD, Wine or Windows 10. The opt-out API's reported success is not an independent check of hidden-window behavior.

AI use
Codex wrote the local probe, executed the build/tests/probe and summarized the actual logs. Exact deployed model identifier is not exposed in this session. No runtime changes or game data published.

@mosin311aa

Copy link
Copy Markdown

Can confirm this fixes the choppy audio problem on my RTX 4070 Super system, no idea if this is the best solution though

@ethandepstein-del

ethandepstein-del commented Nov 23, 2026 •

Copy link
Copy Markdown

Independent static code & Win32 API audit of 9507d0549 (code inspection only — cargo test not run on macOS; no game files):

Summary: Clean and matches Windows API semantics

  • PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION (kernelx.rs:1293-1299): Setting ControlMask = PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION with StateMask = 0 matches Microsoft Learn for opting out of Windows 11's occluded/background timer-resolution throttling, and ignoring a FALSE return on Windows 10/Wine degrades gracefully.
  • NtQueryTimerResolution / NtSetTimerResolution (kernelx.rs:1272-1273, 1301-1307): Signatures ((u32, u8, *mut u32) -> i32) and parameter ordering (coarsest, finest, current) match ntdll, and the Windows kernel automatically releases per-process NtSetTimerResolution requests on process exit/crash.

Two small notes

  1. Closing keyword (Fixes #260): Since xapu answers Opus decode with silence (no sound), and the default 15.6 ms timer step starves the title's audio thread #260 was updated to track both the 15.6 ms timer tick and xapu returning silence for Opus packets (xapu: decode the title's Opus packets and play them on the PC (GTA VI's sound) #294), changing Fixes #260. to Part of #260. avoids auto-closing xapu answers Opus decode with silence (no sound), and the default 15.6 ms timer step starves the title's audio thread #260 before xapu: decode the title's Opus packets and play them on the PC (GTA VI's sound) #294 lands.
  2. Connection to xapu: decode the title's Opus packets and play them on the PC (GTA VI's sound) #294 (ISpatialAudioClient): Your finding that the title mixes on the CPU and outputs through ISpatialAudioClient directly answers xapu: decode the title's Opus packets and play them on the PC (GTA VI's sound) #294's open question ("Where the title's final mix goes is not found yet") — worth checking whether xapu: decode the title's Opus packets and play them on the PC (GTA VI's sound) #294's xaudio2::console_pcm direct playback double-plays decoded xapu audio on Windows once xapu writes real PCM into the title's buffer.

(AI-assisted — Gemini 4 Neon)


Update: Windows 11 / RTX 5080 test run on 9507d0549

Tested

  • Hardware / OS / toolchain: NVIDIA GeForce RTX 5080 (driver 617.42), AMD Ryzen 7 9800X3D, Windows 11 25H2 (build 26200.9711), rustc 1.100.0 (MSVC), debug build in a clean worktree at 9507d05496be7bd87ca769d95cdfec705a3bd6cf.
  • cargo build --workspace && cargo test --workspace -- --test-threads=4: 652 passed, 0 failed, 40 ignored (xcl lib: 612 passed, 0 failed, 20 ignored; all e2e/GPU suites passed).
  • SKIPPED GPU tests (2, wave32-only adapter): gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask: no wave64, gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware: no wave64.
  • hle::kernelx::tests::fine_timer_makes_short_sleeps_short passed.

Not tested

  • Retail title / scripts/smoke.ps1 run on this machine (test suite only).
  • AMD / Intel GPUs.

AI use: test run orchestrated and reported with Antigravity (Gemini 4 Neon) on the hardware listed above.

@ryosen0

ryosen0 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Data point on the intro slow stretch (our machine: RTX 3060 Ti 8 GB, Ryzen 7 9800X3D, driver 32.0.16.1742): the retail title's artwork intro runs at ~4.4-8.6 fps for roughly t=45-165 s, then a steady 30 fps. We measured the same dip with and without this PR + #294 in the stack (same graphics PRs otherwise, XCL_NETWORK=0, XCL_ACTIVITY=1):

So the dip is not the audio path on this hardware - noting it so nobody chases the timer / Opus work for it. In the no-audio run during the dip (t=90 s): 15252 MB committed by the process, video memory 6515 MB local + 4619 MB non-local on the 8 GB card, 3231 MB of target pages; by t=178 s it is 17071 MB / 7791 MB local. DXC compiles are all cache hits there (dxil_cache 25 hits, 0 misses). It looks like memory pressure and streaming, not compilation.

Separately, a 600 s run on an older stack (main 4017f8f + #300 + #294 + #273/+#208 + #283/+#252, before #283 was merged) reached the character loading screen and then crashed at t=395 s with 0xc0000005 (write at 0xc43440110, game+0x35a47bc) at ~26.6 GB of commit - past the loading screen, so probably unrelated to this PR.

AI use: GPT-6.2 Astra (API model selector) ran the builds/runs and wrote this report; the human contributor watched the runs.

@emirduran707

Copy link
Copy Markdown

First AMD run: the sound plays cleanly with this PR.

Setup: AMD Radeon RX 9060 XT 16 GB (RDNA4), driver 32.0.31044.16; Ryzen 5 5600, 32 GB; Windows 11 Pro 26300; release. Build: main a6f5f7a + this PR (9507d05) + #440 (be722a7, so the intro video runs at speed) + #435 (a94e151), merged locally. Same retail title as #260, XCL_NETWORK=0 XCL_ACTIVITY=1 XCL_FRAME_DUMP=60, A pressed by hand on the first menu, a person listening.

  • Log: timer: finest resolution for the title (was 1.00 ms, now 0.50 ms; kept while the window is hidden: yes).
  • Heard: the sound is continuous through the intro video and the loading screen (it was choppy on the same machine without this PR).
  • Measured from the xapu: … Opus packets decoded lines: 50 packets a second from the video on (960 frames each, so real time at 48 kHz), 100 a second once a second stream starts. Without this PR, on main + Textures: linear levels are detiled a row at a time #440 only, the same stretch decoded about 17 packets a second (~0.34 of real time).

Not tested / not attributed: the run ended at 161 s with 0xc0000005 in title code (+0x23cf683, write at address 0) while the world streamed in, at 27 GB of process commit, with no E_OUTOFMEMORY logged. With three PRs in the build and no longer baseline run on this machine I cannot tie it to any of them. NVIDIA, other titles.

AI use: Claude Code with Claude Opus 5.5 read the run's log and wrote this report from my run; the listening is mine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

xapu answers Opus decode with silence (no sound), and the default 15.6 ms timer step starves the title's audio thread

6 participants