run: the title's audio threads at real-time priority while it plays #149

Open
github-bot wants to merge 1 commit from github-bot/gh-149 into main AGit
Owner

@⁠Arvi23 on GitHub · 2026-12-02 01:52 UTC · original
Arvi23:audio-rt → main · +237 −0 · 3 files · 1 commits · open

What

tools/audio-rt.py, started by run.sh play in the background, moves the game's audio threads to SCHED_RR (priority 10, below PipeWire's own) for the session. VICTORY_AUDIO_RT=0 turns it off. Its log is local/run/audio-rt-<utc>.log.

Why

On a Ryzen 7 5800X3D (8 cores / 16 threads, CachyOS, scx_beerland), the sound crackled every time the frame rate dipped below about 25 fps in the open world. Per-thread sampling of /proc/<pid>/task/*/schedstat during play showed:

  • 10–11 of 16 logical cores busy all the time while driving; threads waited a combined 4–5 s per second for a core.
  • No memory pressure (PSI ~0, no memory.max hits), so not swapping.
  • The title's audio mixer (a time-critical thread Wine runs at nice -11, 550–650 wakeups/s on ~1% of a core) waited 126 ms per second for a core. Nice does not bound that wait under a saturated scheduler.

With the mixer and its helpers at SCHED_RR, the same thread waited 0.1 ms per second and the crackle was gone. Frame rate was unchanged: these threads use very little CPU.

What it promotes

Every second it looks at the game's threads (the process of um-play-<pid>.scope with the most threads) and promotes:

  • our XMA (and XApu, where it has its own thread) decoders, by name;
  • up to 6 threads at Windows time-critical priority (nice -11 or below) that wake 100+ times a second on under 15% of a core: on the tested build, the title's mixer and its feeders, and Wine's PipeWire timer (winepipewire_ti).

Safety

  • A promoted thread that comes to use over 40% of a core (90% for our decoders) goes back to SCHED_OTHER at once, for good. That matters where real-time throttling is off (sched_rt_runtime_us equal to the period), where a spinning real-time thread would keep its core. In testing, a title thread that went from 1% to 77% was put back within a second.
  • Without a real-time limit (ulimit -r below 10) it logs one line saying so and changes nothing.
  • It only reads /proc and calls sched_setscheduler on the game's own threads; it ends with the session (a menu restart starts a fresh one).

Caveats

  • Tested on a branch where XApu (Opus) decoding runs on a worker of its own (the idea of issue #137). On main that decoding runs on a title thread; if it is the mixer, that thread may use more than the 15% needed to be picked, and only the feeders and Wine's timer would be promoted. Logs from someone on main would settle it.
  • Needs python3 (skipped if missing).

🤖 Generated with Claude Code

**@⁠Arvi23** on GitHub · 2026-12-02 01:52 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/149) `Arvi23:audio-rt` → `main` · +237 −0 · 3 files · 1 commits · open ## What `tools/audio-rt.py`, started by `run.sh play` in the background, moves the game's audio threads to `SCHED_RR` (priority 10, below PipeWire's own) for the session. `VICTORY_AUDIO_RT=0` turns it off. Its log is `local/run/audio-rt-<utc>.log`. ## Why On a Ryzen 7 5800X3D (8 cores / 16 threads, CachyOS, `scx_beerland`), the sound crackled every time the frame rate dipped below about 25 fps in the open world. Per-thread sampling of `/proc/<pid>/task/*/schedstat` during play showed: - 10–11 of 16 logical cores busy all the time while driving; threads waited a combined 4–5 s per second for a core. - No memory pressure (PSI ~0, no `memory.max` hits), so not swapping. - The title's audio mixer (a time-critical thread Wine runs at nice -11, 550–650 wakeups/s on ~1% of a core) waited **126 ms per second** for a core. Nice does not bound that wait under a saturated scheduler. With the mixer and its helpers at `SCHED_RR`, the same thread waited **0.1 ms per second** and the crackle was gone. Frame rate was unchanged: these threads use very little CPU. ## What it promotes Every second it looks at the game's threads (the process of `um-play-<pid>.scope` with the most threads) and promotes: - our XMA (and XApu, where it has its own thread) decoders, by name; - up to 6 threads at Windows time-critical priority (nice -11 or below) that wake 100+ times a second on under 15% of a core: on the tested build, the title's mixer and its feeders, and Wine's PipeWire timer (`winepipewire_ti`). ## Safety - A promoted thread that comes to use over 40% of a core (90% for our decoders) goes back to `SCHED_OTHER` at once, for good. That matters where real-time throttling is off (`sched_rt_runtime_us` equal to the period), where a spinning real-time thread would keep its core. In testing, a title thread that went from 1% to 77% was put back within a second. - Without a real-time limit (`ulimit -r` below 10) it logs one line saying so and changes nothing. - It only reads `/proc` and calls `sched_setscheduler` on the game's own threads; it ends with the session (a menu restart starts a fresh one). ## Caveats - Tested on a branch where XApu (Opus) decoding runs on a worker of its own (the idea of issue #137). On `main` that decoding runs on a title thread; if it is the mixer, that thread may use more than the 15% needed to be picked, and only the feeders and Wine's timer would be promoted. Logs from someone on `main` would settle it. - Needs `python3` (skipped if missing). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
On an 8-core CPU (5800X3D) the title and our threads keep 10-11 of 16
logical cores busy in the open world. The title's audio mixer, a
time-critical thread Wine runs at nice -11, still waited 126 ms a second
for a core under scx_beerland, and the sound crackled with every frame dip
below about 25 fps. tools/audio-rt.py, started by run.sh for play, moves to
SCHED_RR 10 (below PipeWire's own threads) our XMA and XApu decoders and up
to 6 time-critical threads that wake 100+ times a second on little CPU (the
mixer, its feeders, Wine's PipeWire timer). The mixer then waited 0.1 ms a
second and the crackle was gone (play-20261202-033914).

A promoted thread that comes to use over 40% of a core (90% for our
decoders) is put back for good, since a spinning real-time thread keeps its
core where real-time throttling is off. Without a real-time limit
(ulimit -r) it says so and changes nothing. VICTORY_AUDIO_RT=0 turns it off.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-02 01:56:35 +00:00
Author
Owner

@⁠weneedbucks on GitHub · 2026-12-02 02:23 UTC · original · edited 2026-12-02 02:24 UTC

Your caveat mentions a branch where XApu decoding runs on a worker of its own (a similar idea to the one i filed in #137,). For context from my side (i7-10700K, RX 9060 XT, main f7f0360): I don't get crackle, but the Opus decoding on the title thread costs frame time. In my profile that thread ran at about 70%, roughly 29% of it in XApu/Opus. Would you be open to putting that worker branch up as a separate PR? I'm happy to test it here and post before/after fps.

**@⁠weneedbucks** on GitHub · 2026-12-02 02:23 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/149#issuecomment-7332781876) · edited 2026-12-02 02:24 UTC Your caveat mentions a branch where XApu decoding runs on a worker of its own (a similar idea to the one i filed in #137,). For context from my side (i7-10700K, RX 9060 XT, main f7f0360): I don't get crackle, but the Opus decoding on the title thread costs frame time. In my profile that thread ran at about 70%, roughly 29% of it in XApu/Opus. Would you be open to putting that worker branch up as a separate PR? I'm happy to test it here and post before/after fps.
github-bot force-pushed github-bot/gh-149 from c07227be17 to 8c862ac362 2026-12-02 02:38:58 +00:00 Compare
Author
Owner

@⁠Arvi23 on GitHub · 2026-12-02 02:34 UTC · original

Sure, it's up on its own as #155, rebased on current main. Decoding moves to a worker per connection, results still come back in the order they were queued, and VICTORY_APU_ASYNC=0 goes back to the old path. On my 5800X3D the worker sits at 28–33% of a core in busy scenes, but I haven't done an fps A/B, so your before/after would be really useful. (I've also rebased this PR onto main after #128.)

**@⁠Arvi23** on GitHub · 2026-12-02 02:34 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/149#issuecomment-7332976672) Sure, it's up on its own as #155, rebased on current main. Decoding moves to a worker per connection, results still come back in the order they were queued, and `VICTORY_APU_ASYNC=0` goes back to the old path. On my 5800X3D the worker sits at 28–33% of a core in busy scenes, but I haven't done an fps A/B, so your before/after would be really useful. (I've also rebased this PR onto main after #128.)
Author
Owner

@⁠hazre on GitHub · 2026-12-02 05:36 UTC · original

I am not sure if this is related to your fix, but I fixed the audio crackling with this patch alone.

From fecb77cf15ad6eeb2ec93d9f010196ed4bfb7c8d Mon Sep 17 00:00:00 2001
From: hazre <[email protected]>
Date: Tue, 1 Dec 2026 05:19:35 +0100
Subject: [PATCH] feat(xmem): VICTORY_DSP_PASS switches the convolution
 pass-through

---
 crates/victory-hle/src/modules/dsp.rs | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/crates/victory-hle/src/modules/dsp.rs b/crates/victory-hle/src/modules/dsp.rs
index e11c074..274455e 100644
--- a/crates/victory-hle/src/modules/dsp.rs
+++ b/crates/victory-hle/src/modules/dsp.rs
@@ -37,6 +37,15 @@ const NOT_FOR_CONVOLUTION: u32 = 4;
 const PASS_IR_FLOATS: u32 = 0x1_0000;
 const PASS_GAIN: f32 = 5.0;
 
+/// `VICTORY_DSP_PASS=0`: short convolutions return silence, as they did before
+/// upstream made them pass their input through (b69806a). With the pass on, a
+/// convolution the title uses as an effect return sends the dry signal to the
+/// output as well, amplified by PASS_GAIN, which doubles and distorts the mix.
+fn pass_allowed() -> bool {
+    static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
+    *ON.get_or_init(|| std::env::var("VICTORY_DSP_PASS").map_or(true, |v| v.trim() != "0"))
+}
+
 pub(crate) struct Dsp {
     max_streams: u32,
     active: Mutex<u32>,
@@ -160,7 +169,7 @@ pub(crate) extern "system" fn XDspActivate(
         desc.channels,
         desc.ir_floats,
     );
-    let passes = kind == CONVOLUTION && ir_floats <= PASS_IR_FLOATS;
+    let passes = kind == CONVOLUTION && ir_floats <= PASS_IR_FLOATS && pass_allowed();
     tracing::debug!(
         kind,
         options,
**@⁠hazre** on GitHub · 2026-12-02 05:36 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/149#issuecomment-7336107067) I am not sure if this is related to your fix, but I fixed the audio crackling with this patch alone. ```patch From fecb77cf15ad6eeb2ec93d9f010196ed4bfb7c8d Mon Sep 17 00:00:00 2001 From: hazre <[email protected]> Date: Tue, 1 Dec 2026 05:19:35 +0100 Subject: [PATCH] feat(xmem): VICTORY_DSP_PASS switches the convolution pass-through --- crates/victory-hle/src/modules/dsp.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/crates/victory-hle/src/modules/dsp.rs b/crates/victory-hle/src/modules/dsp.rs index e11c074..274455e 100644 --- a/crates/victory-hle/src/modules/dsp.rs +++ b/crates/victory-hle/src/modules/dsp.rs @@ -37,6 +37,15 @@ const NOT_FOR_CONVOLUTION: u32 = 4; const PASS_IR_FLOATS: u32 = 0x1_0000; const PASS_GAIN: f32 = 5.0; +/// `VICTORY_DSP_PASS=0`: short convolutions return silence, as they did before +/// upstream made them pass their input through (b69806a). With the pass on, a +/// convolution the title uses as an effect return sends the dry signal to the +/// output as well, amplified by PASS_GAIN, which doubles and distorts the mix. +fn pass_allowed() -> bool { + static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new(); + *ON.get_or_init(|| std::env::var("VICTORY_DSP_PASS").map_or(true, |v| v.trim() != "0")) +} + pub(crate) struct Dsp { max_streams: u32, active: Mutex<u32>, @@ -160,7 +169,7 @@ pub(crate) extern "system" fn XDspActivate( desc.channels, desc.ir_floats, ); - let passes = kind == CONVOLUTION && ir_floats <= PASS_IR_FLOATS; + let passes = kind == CONVOLUTION && ir_floats <= PASS_IR_FLOATS && pass_allowed(); tracing::debug!( kind, options, ```
Author
Owner

@⁠Arvi23 on GitHub · 2026-12-02 10:48 UTC · original

Thanks, that was it here too. On a 5800X3D / RX 9070 XT the title opened 8 short convolutions in 3 minutes of night driving (mono, 1024 frames, 40,960-float responses). With your switch at VICTORY_DSP_PASS=0 the crackle that was left is gone. It also let me drop VICTORY_AMBIENT_MOST=15, which had seemed to stop the crackle but turned the cars' headlights off at night.

So there were two different crackles on this machine: the mixer starving for a core on an 8-core CPU, which this PR's real-time priority fixes, and the 5x hard-clipped pass-through, which your patch fixes. Your switch seems worth a PR of its own. The pass-through clips anything above 0.2, and phone callers are the only thing it was meant for.

**@⁠Arvi23** on GitHub · 2026-12-02 10:48 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/149#issuecomment-7343019303) Thanks, that was it here too. On a 5800X3D / RX 9070 XT the title opened 8 short convolutions in 3 minutes of night driving (mono, 1024 frames, 40,960-float responses). With your switch at `VICTORY_DSP_PASS=0` the crackle that was left is gone. It also let me drop `VICTORY_AMBIENT_MOST=15`, which had seemed to stop the crackle but turned the cars' headlights off at night. So there were two different crackles on this machine: the mixer starving for a core on an 8-core CPU, which this PR's real-time priority fixes, and the 5x hard-clipped pass-through, which your patch fixes. Your switch seems worth a PR of its own. The pass-through clips anything above 0.2, and phone callers are the only thing it was meant for.
Author
Owner

@⁠Luxuride on GitHub · 2026-12-04 12:01 UTC · original

Worth having, but not as a silent default. As it stands VICTORY_AUDIO_RT is on by default and documented nowhere — no run.conf.default entry, no docs/SETTINGS.md row — so a user whose audio threads behave worse under SCHED_RR has no supported way to turn it off and the F1 menu cannot show it.

  • Demotion is sampled once a second (tools/audio-rt.py:126, :135, :146), so a promoted thread that starts spinning stays at priority 10 for up to a second with no RT bandwidth cap. On a kernel with RT throttling off, that starves the compositor for that second — a visible hitch traded for a small crackle. A CPU budget over a shorter window would be safer.
  • MAX_CPU_OTHERS/MAX_CPU_OURS (tools/audio-rt.py:45-46) pick threads by comm prefix and nice value, i.e. by Wine's internal naming, which is not a contract and changes between Wine versions. Keep the name-based part as the default and make the six-thread nice-based fallback opt-in.
  • The evidence is one hand-read run log with no harness in-tree, so a maintainer cannot re-measure it later.

Rebase (only README.md conflicts), add the two doc entries, tighten the demotion window, and put the before/after numbers in the body — then it is a 30-minute review.

**@⁠Luxuride** on GitHub · 2026-12-04 12:01 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/149#issuecomment-7410868139) Worth having, but not as a silent default. As it stands `VICTORY_AUDIO_RT` is on by default and documented nowhere — no `run.conf.default` entry, no `docs/SETTINGS.md` row — so a user whose audio threads behave worse under `SCHED_RR` has no supported way to turn it off and the F1 menu cannot show it. - Demotion is sampled once a second (`tools/audio-rt.py:126`, `:135`, `:146`), so a promoted thread that starts spinning stays at priority 10 for up to a second with no RT bandwidth cap. On a kernel with RT throttling off, that starves the compositor for that second — a visible hitch traded for a small crackle. A CPU budget over a shorter window would be safer. - `MAX_CPU_OTHERS`/`MAX_CPU_OURS` (`tools/audio-rt.py:45-46`) pick threads by comm prefix and nice value, i.e. by Wine's internal naming, which is not a contract and changes between Wine versions. Keep the name-based part as the default and make the six-thread nice-based fallback opt-in. - The evidence is one hand-read run log with no harness in-tree, so a maintainer cannot re-measure it later. Rebase (only `README.md` conflicts), add the two doc entries, tighten the demotion window, and put the before/after numbers in the body — then it is a 30-minute review.
This pull request has changes conflicting with the target branch.
  • README.md
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/149/head:github-bot/gh-149'
git switch 'github-bot/gh-149'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-149'
git switch 'github-bot/gh-149'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-149'
git switch 'github-bot/gh-149'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-149'
git switch 'main'
git merge --squash 'github-bot/gh-149'
git switch 'main'
git merge --ff-only 'github-bot/gh-149'
git switch 'main'
git merge 'github-bot/gh-149'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.