Audio: the XMA decoder runs at time-critical priority #1509

Open
pwu2775-dev wants to merge 1 commit from upstream-pr/1509 into main
pwu2775-dev commented 2026-11-28 07:20:05 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by pwu2775-dev on 2026-11-28T07:20:05Z.


Problem

The game's audio crackles, and the crackle grows over a run: clean at the start, broken later in the world.

Each enabled XMA voice writes decoded samples into an output ring in title memory that the title reads through ADVANCE_XMA_READ (command 18). The ring is shallow — the title asks for 1 to 31 blocks of 256 bytes, 2.7 ms each at 48 kHz mono, so 21 to 83 ms of audio in all. The frame timeline stalls far longer than that under memory pressure: 150 to 260 ms a frame measured on the affected run, whose private memory grew past 38 GiB on a 32 GiB machine (hard faults at 10,000–26,000 pages/s).

At normal priority the single victory XMA decoder thread then waits behind the thirty-odd frame threads (victory-list-0..7, victory-wide, the title's own workers) for its slice, a ring runs dry in between, and the title's mixer reads a partial block — the game plays that as a crackle. It grows as the run consumes more memory because the stalls grow with it.

Live measurement on the affected run (thread priority raised externally on the running process, RTX 4070 / Ryzen 5 7500F / 32 GB):

  • before: window showed 4–6 fps, 156–263 ms/frame, crackle audible;
  • with the decoder at time-critical: same scene load, crackle eased at once (a partial relief — the rings stay shallow and memory pressure remains).

Fix

The decoding thread raises itself to THREAD_PRIORITY_TIME_CRITICAL when it starts (windows-sys is already a dependency; no Cargo change). One line of behavior, with the reason documented at the function:

  • the thread feeds the title's time-critical mixer, so it belongs above the frame's threads; ahead_of_other_programs raised the whole process for a similar reason, while the pipeline helpers may go below (VICTORY_HELPER_PRIORITY) because a frame feeds them — this thread is the other direction of that trade.
  • failure to set the priority logs a warning instead of staying silent.

Validation

  • cargo test -p victory-hle --locked --lib: 171 passed, 2 ignored (audio module's 8 tests all pass).
  • cargo clippy -p victory-hle --all-targets: no new warnings (the crate's existing ones are in dstorage.rs/xmem.rs, untouched).
  • cargo build --release --locked -p victory: success.

Known limits

  • The rings stay shallow: a stall longer than the ring depth can still starve a voice, and decode-ahead beyond the title's read pointer is not possible (the ring is the title's memory; writing past its read pointer would corrupt what it holds). The deeper fix would need the title's own buffering contract to change.
  • The full run with the built binary has not yet been repeated end to end on this machine; the live priority change on the affected run is the measurement cited above.
<!-- greyfinch-sync:fxceless/victory-fork:issue:5537809151 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1509). Originally posted by [pwu2775-dev](https://github.com/pwu2775-dev) on 2026-11-28T07:20:05Z. --- ## Problem The game's audio crackles, and the crackle grows over a run: clean at the start, broken later in the world. Each enabled XMA voice writes decoded samples into an output ring in title memory that the title reads through `ADVANCE_XMA_READ` (command 18). The ring is shallow — the title asks for 1 to 31 blocks of 256 bytes, 2.7 ms each at 48 kHz mono, so **21 to 83 ms of audio in all**. The frame timeline stalls far longer than that under memory pressure: 150 to 260 ms a frame measured on the affected run, whose private memory grew past 38 GiB on a 32 GiB machine (hard faults at 10,000–26,000 pages/s). At normal priority the single `victory XMA decoder` thread then waits behind the thirty-odd frame threads (`victory-list-0..7`, `victory-wide`, the title's own workers) for its slice, a ring runs dry in between, and the title's mixer reads a partial block — the game plays that as a crackle. It grows as the run consumes more memory because the stalls grow with it. Live measurement on the affected run (thread priority raised externally on the running process, RTX 4070 / Ryzen 5 7500F / 32 GB): - before: window showed 4–6 fps, 156–263 ms/frame, crackle audible; - with the decoder at time-critical: same scene load, crackle eased at once (a partial relief — the rings stay shallow and memory pressure remains). ## Fix The decoding thread raises itself to `THREAD_PRIORITY_TIME_CRITICAL` when it starts (`windows-sys` is already a dependency; no Cargo change). One line of behavior, with the reason documented at the function: - the thread feeds the title's time-critical mixer, so it belongs above the frame's threads; `ahead_of_other_programs` raised the whole process for a similar reason, while the pipeline helpers may go below (`VICTORY_HELPER_PRIORITY`) because a frame feeds them — this thread is the other direction of that trade. - failure to set the priority logs a warning instead of staying silent. ## Validation - `cargo test -p victory-hle --locked --lib`: 171 passed, 2 ignored (audio module's 8 tests all pass). - `cargo clippy -p victory-hle --all-targets`: no new warnings (the crate's existing ones are in `dstorage.rs`/`xmem.rs`, untouched). - `cargo build --release --locked -p victory`: success. ## Known limits - The rings stay shallow: a stall longer than the ring depth can still starve a voice, and decode-ahead beyond the title's read pointer is not possible (the ring is the title's memory; writing past its read pointer would corrupt what it holds). The deeper fix would need the title's own buffering contract to change. - The full run with the built binary has not yet been repeated end to end on this machine; the live priority change on the affected run is the measurement cited above.
pwu2775-dev commented 2026-11-29 07:08:38 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by pwu2775-dev on 2026-11-29T07:08:38Z.


Live measurement on the machine that showed the crackle (RTX 4070 / Ryzen 5 7500F / 32 GB), this build with the fix active, second session in the same area:

  • The decoder thread runs at time-critical as intended (observed priority 15 on the running process).
  • With the run past 31 GiB of private memory on a 32 GiB machine, the frame stalls still exceeded the ring depth (112-122 ms measured, rings hold at most 83 ms): the crackle persisted, though the thread was scheduled at once.
  • The stall's driver this time was paging, not scheduling: hard faults at up to 74,754 pages/s while free RAM sat under 1 GiB. Raising the process working-set floor externally (18 GiB min) and trimming the other processes cut the fault rate to 6,000-20,000 pages/s within half a minute.

So the priority fix removes the scheduling delay, and on lighter scenes that is the whole difference; under this run's memory pressure the remaining stalls are page waits, which a thread priority cannot skip. The two practical conclusions for the PR: the crackle tracks the hard-fault rate rather than the frame rate alone, and a deeper output path (decode-ahead into memory we own, handed to the ring under the title's read pointer) would be the follow-up that helps where this one tops out.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7252288446 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1509#issuecomment-7252288446). Originally posted by [pwu2775-dev](https://github.com/pwu2775-dev) on 2026-11-29T07:08:38Z. --- Live measurement on the machine that showed the crackle (RTX 4070 / Ryzen 5 7500F / 32 GB), this build with the fix active, second session in the same area: - The decoder thread runs at time-critical as intended (observed priority 15 on the running process). - With the run past 31 GiB of private memory on a 32 GiB machine, the frame stalls still exceeded the ring depth (112-122 ms measured, rings hold at most 83 ms): the crackle persisted, though the thread was scheduled at once. - The stall's driver this time was paging, not scheduling: hard faults at up to 74,754 pages/s while free RAM sat under 1 GiB. Raising the process working-set floor externally (18 GiB min) and trimming the other processes cut the fault rate to 6,000-20,000 pages/s within half a minute. So the priority fix removes the scheduling delay, and on lighter scenes that is the whole difference; under this run's memory pressure the remaining stalls are page waits, which a thread priority cannot skip. The two practical conclusions for the PR: the crackle tracks the hard-fault rate rather than the frame rate alone, and a deeper output path (decode-ahead into memory we own, handed to the ring under the title's read pointer) would be the follow-up that helps where this one tops out.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1509:upstream-pr/1509'
git switch 'upstream-pr/1509'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/1509'
git switch 'upstream-pr/1509'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1509'
git switch 'upstream-pr/1509'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1509'
git switch 'main'
git merge --squash 'upstream-pr/1509'
git switch 'main'
git merge --ff-only 'upstream-pr/1509'
git switch 'main'
git merge 'upstream-pr/1509'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
fxceless/victory-fork!1509
No description provided.