hle: apu: decode Opus on dedicated worker threads (VICTORY_APU_ASYNC, port from ReVIctory) #455

Open
github-bot wants to merge 2 commits from github-bot/gh-455 into main AGit
Owner

@⁠lucamzanon on GitHub · 2026-12-04 11:32 UTC · original
lucamzanon:xmem/9-apu-async → main · +610 −76 · 3 files · 2 commits · open

Summary

Ports ReTerryDavis/ReVIctory commit 71de60b2 (PR #37, original author Jonas G. Morsch, Co-authored-by preserved in commit) to VIctory-xmem, offloading Opus audio decoding off the title's threads onto a dedicated worker pool (crates/victory-hle/src/modules/apu.rs only).

Settings

  • VICTORY_APU_ASYNC: on by default (0 restores inline synchronous decoding).
  • VICTORY_APU_THREADS: worker count (default 4).

Hardening added during the port

  1. Per-stream result ordering: sends/executes the job while still holding the queue lock (instead of releasing before send), eliminating a tiny window where two threads queueing on the same stream could execute commands out of result order. Added threads_queueing_on_one_stream_run_in_result_order covering both async and sync fallback modes.
  2. Graceful thread-spawn fallback: if spawning a decoder worker fails, logs a warning and falls back to inline decoding instead of panicking inside XApuConnect.

Verification (RX 9070 XT / Ryzen 7 5700X, Linux RADV)

  • Unit tests: all 4 apu unit tests executed under Wine against the cross-compiled x86_64-pc-windows-msvc test binary: 20/20 passes across VICTORY_APU_THREADS=1, 4, and 16.
  • Build / lint: cargo check and cargo test --no-run (x86_64-pc-windows-msvc) pass with 0 warnings; cargo clippy --no-deps --all-targets -p victory-hle reports 0 findings in apu.rs.
  • In-game sanity check: moving Boat 4m (VICTORY_BENCH=rt, 4K FSR 4 Quality, no FG): 0 crashes, warm run 27.10 fps / 9.70 1% low, clean audio playback.
**@⁠lucamzanon** on GitHub · 2026-12-04 11:32 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/455) `lucamzanon:xmem/9-apu-async` → `main` · +610 −76 · 3 files · 2 commits · open ## Summary Ports `ReTerryDavis/ReVIctory` commit `71de60b2` (PR #37, original author Jonas G. Morsch, `Co-authored-by` preserved in commit) to `VIctory-xmem`, offloading Opus audio decoding off the title's threads onto a dedicated worker pool (`crates/victory-hle/src/modules/apu.rs` only). ### Settings - `VICTORY_APU_ASYNC`: on by default (`0` restores inline synchronous decoding). - `VICTORY_APU_THREADS`: worker count (default `4`). ### Hardening added during the port 1. **Per-stream result ordering:** sends/executes the job while still holding the queue lock (instead of releasing before send), eliminating a tiny window where two threads queueing on the same stream could execute commands out of result order. Added `threads_queueing_on_one_stream_run_in_result_order` covering both async and sync fallback modes. 2. **Graceful thread-spawn fallback:** if spawning a decoder worker fails, logs a warning and falls back to inline decoding instead of panicking inside `XApuConnect`. ## Verification (RX 9070 XT / Ryzen 7 5700X, Linux RADV) - **Unit tests:** all 4 `apu` unit tests executed under Wine against the cross-compiled `x86_64-pc-windows-msvc` test binary: **20/20 passes** across `VICTORY_APU_THREADS=1`, `4`, and `16`. - **Build / lint:** `cargo check` and `cargo test --no-run` (`x86_64-pc-windows-msvc`) pass with 0 warnings; `cargo clippy --no-deps --all-targets -p victory-hle` reports **0 findings in `apu.rs`**. - **In-game sanity check:** moving Boat `4m` (`VICTORY_BENCH=rt`, 4K FSR 4 Quality, no FG): **0 crashes**, warm run `27.10 fps` / `9.70` 1% low, clean audio playback.
Port of ReTerryDavis/ReVIctory 71de60b2 onto cd797a6.

The console's decoder works beside the title's CPU; decoding inside
XApuEnqueueCommand was a third of the title's audio thread (xperf, GTA6
cutscene), the one core that stayed full. Results stay pending until
decoded, in command order; a stream's commands run on one thread in
order; disconnect waits for the commands in flight (to five seconds,
then keeps the connection's memory rather than free it under them).
Test: same results and samples as decoding at once.

Two departures from the reference, from reading it for thread safety:

- A command is sent, or run, with the queue lock still held; the
  reference let the lock go first. No decoder thread takes that lock,
  so nobody waits on it, and two threads queueing on one stream at
  once can no longer have a command run ahead of one whose result
  comes first, which decoding when queued never allowed. Test: the
  other thread's result tells what the decoder met, in result order.
- A decoder thread that cannot start is done without (a warning)
  instead of a panic in XApuConnect; with none at all, commands run
  when queued.

Co-authored-by: Jonas G. Morsch <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-04 11:36:18 +00:00
Author
Owner

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

Best of the three APU async PRs and the only one with real concurrency tests — take this, and the other two (one worker per connection, both adding PENDING_RESULTS) become duplicates in its favour. Three things before it ships.

  1. A missed wakeup. The event is only set by a command that itself carried SIGNAL when it runs, so a title polling it can be woken to an unfinished front slot: job A (no SIGNAL) still decoding on worker A when job B (SIGNAL) finishes on worker B, the title wakes, XApuGetNextResult returns PENDING_RESULTS because the front is not done, and when A finishes nothing signals (crates/victory-hle/src/modules/apu.rs:308-316 vs :575-576). Signal whenever the front slot becomes done — one relaxed load.
  2. On by default with the tests excluded. apu.rs:208 enables it for every user while cfg!(test) keeps the tests on the sync path, so a decode-order regression lands silently in the default configuration. Default it off for one release, or run the tests with it on.
  3. Switches nobody can find. VICTORY_APU_ASYNC and VICTORY_APU_THREADS exist only in the module doc and in asynchronous()/threads() (apu.rs:9-10, :202-225). Nothing in docs/SETTINGS.md, nothing in run.conf.default, so per README.md:125 the F1 menu never offers them.

What is right: worker-per-index with stream % workers.len() affinity keeps per-stream order without serialising across streams; Slot { done: AtomicBool, result: Mutex<ApuResult> } with the dequeue side popping only once the front is done is the correct shape; and XApuDisconnect spinning 5 s on in_flight and then leaking rather than handing a worker freed memory is the honest choice.

40 behind, apu.rs untouched on main since the merge base. The one thing this PR does not carry from the older one is the README.md note.

**@⁠Luxuride** on GitHub · 2026-12-04 12:20 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/455#issuecomment-7411298046) Best of the three APU async PRs and the only one with real concurrency tests — take this, and the other two (one worker per connection, both adding `PENDING_RESULTS`) become duplicates in its favour. Three things before it ships. 1. **A missed wakeup.** The event is only set by a command that itself carried SIGNAL when it *runs*, so a title polling it can be woken to an unfinished front slot: job A (no SIGNAL) still decoding on worker A when job B (SIGNAL) finishes on worker B, the title wakes, `XApuGetNextResult` returns `PENDING_RESULTS` because the front is not done, and when A finishes nothing signals (`crates/victory-hle/src/modules/apu.rs:308-316` vs `:575-576`). Signal whenever the **front** slot becomes done — one relaxed load. 2. **On by default with the tests excluded.** `apu.rs:208` enables it for every user while `cfg!(test)` keeps the tests on the sync path, so a decode-order regression lands silently in the default configuration. Default it off for one release, or run the tests with it on. 3. **Switches nobody can find.** `VICTORY_APU_ASYNC` and `VICTORY_APU_THREADS` exist only in the module doc and in `asynchronous()`/`threads()` (`apu.rs:9-10`, `:202-225`). Nothing in `docs/SETTINGS.md`, nothing in `run.conf.default`, so per `README.md:125` the F1 menu never offers them. What is right: worker-per-index with `stream % workers.len()` affinity keeps per-stream order without serialising across streams; `Slot { done: AtomicBool, result: Mutex<ApuResult> }` with the dequeue side popping only once the front is done is the correct shape; and `XApuDisconnect` spinning 5 s on `in_flight` and then leaking rather than handing a worker freed memory is the honest choice. 40 behind, `apu.rs` untouched on `main` since the merge base. The one thing this PR does not carry from the older one is the `README.md` note.
github-bot force-pushed github-bot/gh-455 from 9090ac803e to bca57f8f88 2026-12-04 13:03:59 +00:00 Compare
Author
Owner

@⁠lucamzanon on GitHub · 2026-12-04 12:57 UTC · original

Great catch on the missed wakeup @⁠Luxuride — thank you!

Rebased onto main (0fd7614) and addressed all three items in bca57f8:

  1. Missed wakeup fixed: tracked front_seq: AtomicU64 on Shared (incremented on XApuDequeueResult pop) with SeqCst ordering on Slot::done and front_seq; run() now signals shared.event when signal || seq == shared.front_seq.load(Ordering::SeqCst). Added deterministic regression test front_completion_signals_event_after_later_signal_job (holds stream 0's decoder lock while stream 1's SIGNAL job completes and wakes the auto-reset event, verifies PENDING_RESULTS, then releases stream 0 and asserts the event signals again).
  2. Tests now run async by default: removed !cfg!(test) from asynchronous() and updated the unit tests to dequeue via a bounded poll helper. Verified all 5 modules::apu::tests pass under Wine with both default (VICTORY_APU_ASYNC=1) and VICTORY_APU_ASYNC=0.
  3. Documented switches: added VICTORY_APU_ASYNC and VICTORY_APU_THREADS to docs/SETTINGS.md and run.conf.default (F1 menu).

0 clippy warnings in apu.rs.

**@⁠lucamzanon** on GitHub · 2026-12-04 12:57 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/455#issuecomment-7412153490) Great catch on the missed wakeup @⁠Luxuride — thank you! Rebased onto `main` (`0fd7614`) and addressed all three items in `bca57f8`: 1. **Missed wakeup fixed**: tracked `front_seq: AtomicU64` on `Shared` (incremented on `XApuDequeueResult` pop) with `SeqCst` ordering on `Slot::done` and `front_seq`; `run()` now signals `shared.event` when `signal || seq == shared.front_seq.load(Ordering::SeqCst)`. Added deterministic regression test `front_completion_signals_event_after_later_signal_job` (holds stream 0's decoder lock while stream 1's `SIGNAL` job completes and wakes the auto-reset event, verifies `PENDING_RESULTS`, then releases stream 0 and asserts the event signals again). 2. **Tests now run async by default**: removed `!cfg!(test)` from `asynchronous()` and updated the unit tests to dequeue via a bounded poll helper. Verified all 5 `modules::apu::tests` pass under Wine with both default (`VICTORY_APU_ASYNC=1`) and `VICTORY_APU_ASYNC=0`. 3. **Documented switches**: added `VICTORY_APU_ASYNC` and `VICTORY_APU_THREADS` to `docs/SETTINGS.md` and `run.conf.default` (F1 menu). 0 clippy warnings in `apu.rs`.
Author
Owner

@⁠Kal0s-design on GitHub · 2026-12-04 14:04 UTC · original

Tested bca57f8 natively on Windows 11 (MSVC, not Wine), CPU only, no game run.

cargo test -p victory-hle --lib -- --test-threads=2 with VICTORY_NATIVE_INPUT=0 gives 163 passed, 1 ignored with VICTORY_APU_ASYNC unset, =0 and =1. The 5 apu tests also pass with VICTORY_APU_THREADS=1 and =16.

I also wrote a local stress test that I'm not pushing. It uses 8 stereo streams with 300 packets each and a different tone per stream. Submits come in random bursts across streams, and results are drained as they become ready. Every result is checked three ways. Global order has to match submit order, per-stream order has to match, and the output bytes have to equal a reference decode done in order on a fresh decoder, read right when the result is dequeued. I ran it 5 times each for ASYNC=0/1 x THREADS=1/4/16 and all 30 runs passed.

What I checked in apu.rs

  • Default. It is on unless set to 0 (apu.rs:207-211, also in SETTINGS.md and run.conf.default). With 0 the command runs inline under the queue lock (apu.rs:520), so it is close to main but not identical. The event is now set when signal || is_front (apu.rs:316-317), while main only sets it for commands with SIGNAL (main apu.rs:316). With ASYNC=0 a NOTIFY without SIGNAL on an empty queue now signals the event; I confirmed that with a small local test, in both modes. It's probably harmless (a spurious wake on an auto-reset event), but 0 is not quite "as before" the way the module doc says (apu.rs:14-15).
  • Output before done. The worker writes the PCM in process (apu.rs:556-569), stores the result, then does done.store(SeqCst) (apu.rs:314-315). Dequeue and GetResultCount only look at a slot after done.load(SeqCst) (apu.rs:176, 586, 613). So the title can't see a result before its bytes are written. The stress test agrees.
  • Title buffer lifetime, the same point raised on #175. Like #175, this copies only the descriptor at enqueue (apu.rs:478). The worker reads input and writes output later (apu.rs:538, 556), and the packet bytes are not copied. That is fine for a title that keeps the buffer until it dequeues the result, as on the console. A title that reuses the buffer earlier gets its new bytes decoded. I checked that with a local test. It holds the stream's decoder lock, enqueues packet A, writes packet B over the same buffer, then releases the lock. With ASYNC=0 the output matches a reference decode of A. With ASYNC=1 it doesn't (no error returned, just different samples). The difference from #175 is that this PR has the escape hatch the reviewer asked for there (ASYNC=0). The SAFETY note at apu.rs:203-204 assumes the pointers are inside the connection memory, but nothing checks that.
  • Per-stream decoder state. Each stream has its own Mutex<Option<OpusDecoder>> (apu.rs:151, 281), and a stream always goes to worker stream % n. The send happens while the queue lock is held (apu.rs:504-517), and mpsc is FIFO, so one stream's packets never decode concurrently or out of order. Results across streams come back in command order because only a done front slot is popped (apu.rs:586).
  • Bounds. The channels are unbounded, but jobs are capped by the per-stream QUEUE_FULL check (apu.rs:453), so a connection has at most streams x commands_per_stream jobs in flight. Thread count is 4 by default, clamped to 1..16 (apu.rs:222-227).
  • Shutdown. The pool is global and never joined, which is fine. Disconnect spins with yield_now on in_flight for up to 5 s and then leaks the memory and event rather than freeing them under a worker (apu.rs:419-426). #175 joins its own per-connection thread instead.
  • Panics, from reading only (not reproduced, and I didn't find a way to make the decoder panic). Nothing catches a panic in run. If one happens, that worker thread dies, its slot never becomes done, and in_flight never drops. Jobs still queued to it are lost, and later sends fail and run inline (apu.rs:514-515). The connection then returns PENDING_RESULTS forever, and disconnect leaks after 5 s. This is a small note, not a blocker.
  • Windows. The only new Win32 call is SetThreadPriority(THREAD_PRIORITY_HIGHEST) on each worker (apu.rs:260-266). There's no affinity. With the default of 4, that's four threads above the title's normal ones, which is worth knowing if anyone sees stutter.

The logic looks right to me. The open points are small. The sync path isn't byte-identical to main because of the extra event signal, the buffer lifetime is the same as #175 (with an off switch), and a worker panic stalls the connection.

AI-assisted: yes

**@⁠Kal0s-design** on GitHub · 2026-12-04 14:04 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/455#issuecomment-7413787936) Tested bca57f8 natively on Windows 11 (MSVC, not Wine), CPU only, no game run. `cargo test -p victory-hle --lib -- --test-threads=2` with VICTORY_NATIVE_INPUT=0 gives 163 passed, 1 ignored with VICTORY_APU_ASYNC unset, =0 and =1. The 5 apu tests also pass with VICTORY_APU_THREADS=1 and =16. I also wrote a local stress test that I'm not pushing. It uses 8 stereo streams with 300 packets each and a different tone per stream. Submits come in random bursts across streams, and results are drained as they become ready. Every result is checked three ways. Global order has to match submit order, per-stream order has to match, and the output bytes have to equal a reference decode done in order on a fresh decoder, read right when the result is dequeued. I ran it 5 times each for ASYNC=0/1 x THREADS=1/4/16 and all 30 runs passed. What I checked in apu.rs - Default. It is on unless set to `0` (apu.rs:207-211, also in SETTINGS.md and run.conf.default). With `0` the command runs inline under the queue lock (apu.rs:520), so it is close to main but not identical. The event is now set when `signal || is_front` (apu.rs:316-317), while main only sets it for commands with SIGNAL (main apu.rs:316). With ASYNC=0 a NOTIFY without SIGNAL on an empty queue now signals the event; I confirmed that with a small local test, in both modes. It's probably harmless (a spurious wake on an auto-reset event), but `0` is not quite "as before" the way the module doc says (apu.rs:14-15). - Output before done. The worker writes the PCM in `process` (apu.rs:556-569), stores the result, then does `done.store(SeqCst)` (apu.rs:314-315). Dequeue and GetResultCount only look at a slot after `done.load(SeqCst)` (apu.rs:176, 586, 613). So the title can't see a result before its bytes are written. The stress test agrees. - Title buffer lifetime, the same point raised on #175. Like #175, this copies only the descriptor at enqueue (apu.rs:478). The worker reads `input` and writes `output` later (apu.rs:538, 556), and the packet bytes are not copied. That is fine for a title that keeps the buffer until it dequeues the result, as on the console. A title that reuses the buffer earlier gets its new bytes decoded. I checked that with a local test. It holds the stream's decoder lock, enqueues packet A, writes packet B over the same buffer, then releases the lock. With ASYNC=0 the output matches a reference decode of A. With ASYNC=1 it doesn't (no error returned, just different samples). The difference from #175 is that this PR has the escape hatch the reviewer asked for there (ASYNC=0). The SAFETY note at apu.rs:203-204 assumes the pointers are inside the connection memory, but nothing checks that. - Per-stream decoder state. Each stream has its own `Mutex<Option<OpusDecoder>>` (apu.rs:151, 281), and a stream always goes to worker `stream % n`. The send happens while the queue lock is held (apu.rs:504-517), and mpsc is FIFO, so one stream's packets never decode concurrently or out of order. Results across streams come back in command order because only a done front slot is popped (apu.rs:586). - Bounds. The channels are unbounded, but jobs are capped by the per-stream QUEUE_FULL check (apu.rs:453), so a connection has at most streams x commands_per_stream jobs in flight. Thread count is 4 by default, clamped to 1..16 (apu.rs:222-227). - Shutdown. The pool is global and never joined, which is fine. Disconnect spins with yield_now on `in_flight` for up to 5 s and then leaks the memory and event rather than freeing them under a worker (apu.rs:419-426). #175 joins its own per-connection thread instead. - Panics, from reading only (not reproduced, and I didn't find a way to make the decoder panic). Nothing catches a panic in `run`. If one happens, that worker thread dies, its slot never becomes done, and `in_flight` never drops. Jobs still queued to it are lost, and later sends fail and run inline (apu.rs:514-515). The connection then returns PENDING_RESULTS forever, and disconnect leaks after 5 s. This is a small note, not a blocker. - Windows. The only new Win32 call is SetThreadPriority(THREAD_PRIORITY_HIGHEST) on each worker (apu.rs:260-266). There's no affinity. With the default of 4, that's four threads above the title's normal ones, which is worth knowing if anyone sees stutter. The logic looks right to me. The open points are small. The sync path isn't byte-identical to main because of the extra event signal, the buffer lifetime is the same as #175 (with an off switch), and a worker panic stalls the connection. AI-assisted: yes
Author
Owner

@⁠lucamzanon on GitHub · 2026-12-05 04:31 UTC · original

Thanks @⁠Kal0s-design for the 30-run Win11 MSVC stress test across ASYNC=0/1 x THREADS=1/4/16!

Good catch on ASYNC=0 firing the extra is_front SetEvent for non-SIGNAL commands on an empty queue — since inline ASYNC=0 cannot complete out of order, gating the front-slot wake on asynchronous (signal || (asynchronous && is_front)) would keep VICTORY_APU_ASYNC=0 100% identical to main. Happy to push that 1-line tweak if maintainers prefer.

**@⁠lucamzanon** on GitHub · 2026-12-05 04:31 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/455#issuecomment-7431893495) Thanks @⁠Kal0s-design for the 30-run Win11 MSVC stress test across `ASYNC=0/1 x THREADS=1/4/16`! Good catch on `ASYNC=0` firing the extra `is_front` `SetEvent` for non-`SIGNAL` commands on an empty queue — since inline `ASYNC=0` cannot complete out of order, gating the front-slot wake on `asynchronous` (`signal || (asynchronous && is_front)`) would keep `VICTORY_APU_ASYNC=0` 100% identical to `main`. Happy to push that 1-line tweak if maintainers prefer.
Author
Owner

@⁠hugodasschlossgespenst on GitHub · 2026-12-05 17:08 UTC · original

Measurements on a 6-core Zen 1: #455 helps here, but only once GameMode stops pinning the game

TL;DR

  • With #455, the busiest title thread drops from ~78 % to ~46 % of a core. The four victory-apu workers share ~27 % of a core, waiting very little. The fps doesn't drop (unlike the −12 % in #155); in the one run so far median fps went from 29.5 to 34.9.
  • The first round of measurements showed nothing, because GameMode had pinned the whole game to 3 of 12 CPUs (bug report: https://github.com/FeralInteractive/gamemode/issues/582). That halved the fps and hid any effect of where Opus is decoded. If you measure through gamemoderun, check the affinity first.
  • The data is still thin: 2–3 minutes per variant in the open world once unpinned. Longer runs will follow in this thread.

Setup

CPU Ryzen 5 1600, 6 cores / 12 threads, Zen 1, 2 × 8 MB L3; fixed ~3.4 GHz, no cpufreq driver
GPU RX 7900 XTX, ReBAR on
RAM 2 × DDR4-2933
OS Fedora 45, kernel 7.3.6
Wine GE-Proton11-16, NTSync
VIctory 0fd7614; pr455 = only apu.rs swapped for bca57f8 (4 workers, HIGHEST)
Settings Series S profile, 1920×1080, lowest detail; the 60 fps cap stays on while measuring

How it was measured:

  • Frame times: from VICTORY_PRESENT_TIMING=1.
  • Per-thread CPU use and wait time: sampled once a second from /proc/<pid>/task/*/schedstat, the same method as in #149/#155.
  • What counts: only play in the open world, on the same save and the same walk-then-drive loop. Left out: the first 90 s, and every 5-s window where ≥ 90 % of frames sit at the cap (menus, pause, map).

The affinity problem

In the first runs, all 148 threads of the game process were limited to CPUs {1, 4, 8}.

  • Neither VIctory, Wine nor the title did this. Wine's own thread list shows affinity 0xfff for the threads. The pinning comes from outside, via sched_setaffinity.
  • The source is GameMode 1.8.2. Its pin_cores option is on by default. On a CPU with uniform L3 and no cpufreq driver it builds the core set from uninitialised memory. It also re-applies the set every few seconds: resetting it by hand took 34,503 resets in one session.
  • Fix: with [cpu] pin_cores=no in ~/.config/gamemode.ini, all threads may use all 12 CPUs again. A 40-minute session with this setting had no GameMode pinning at all.

Same build (main), pinned vs not pinned:

pinned (4 runs, 18 min) unpinned (2 runs, 3.6 min)
Median fps 15.5 29.5
Average fps 11.8 21.9
1 % low 1.2 3.3
Frames ≥ 50 ms per minute 451 324
CPU used by the game's threads ~2.9 cores ~7.9 cores
Busiest title thread 25–38 % of a core 75–80 %

While pinned, about 150 threads share 3 CPUs. Every thread mostly waits, so where Opus is decoded can't make a difference. The pinned #455 run confirms this (table below).

The combination that gives ~30 fps here, and how to check it on your machine

The game now holds a steady ~30 fps in the open world on this machine, up from ~15. Three things work together:

1. The CPU: Ryzen 5 1600 (Zen 1, 6 cores / 12 threads, no cpufreq driver)

  • It is what triggers the GameMode bug. Without a cpufreq driver, /sys/devices/system/cpu/cpu*/cpufreq/ does not exist. With uniform L3, GameMode then picks its core set from uninitialised memory. A CPU with a cpufreq driver or with non-uniform L3 (X3D) takes a different path.
  • 6 cores are enough once the game may use all of them. Unpinned, the game uses ~8 of the 12 logical CPUs, and the busiest thread is 75–80 % busy, not waiting for a core.
  • Check:
    • ls /sys/devices/system/cpu/cpu0/cpufreq/ (missing = affected if GameMode pins)
    • cat /sys/devices/system/cpu/cpu*/cache/index3/size | sort -u (one line = uniform L3)

2. A "game mode" guard script around run.sh that shuts down the rest of the desktop
A small wrapper (not part of VIctory) that runs while the game runs:

  • Stops background services and masks them for the session (systemctl mask --runtime): indexers, sync clients, assistants, input methods, GNOME background services. It closes the browser and sync apps.
  • Pushes idle memory into zram (zstd) with memory.reclaim before the start, so the game has as much RAM as possible. Then it sets swappiness back to the system's value, so the game's own pages aren't swapped out.
  • Releases the kdump crash-kernel reserve (256 MB).
  • Restores everything when the game exits.

Careful when building something like this: re-masking units in a loop makes systemd reload its whole configuration on every call. My first version did that every 15 s and cost more than a CPU core (2,745 reloads in 40 min). Mask once, in one call, and only units that aren't masked yet.

All measured runs, pinned and unpinned, were made with this guard on, so its own effect is not measured separately. What it does: on this machine, everything except the game stays below ~0.5 CPU core while the game runs.

3. GameMode (gamemoderun), with core pinning switched off

~/.config/gamemode.ini:

[general]
renice=10                      ; game threads at nice -10
inhibit_screensaver=1

[gpu]
apply_gpu_optimisations=accept-responsibility
gpu_device=0
amd_performance_level=high     ; GPU at full clocks while the game runs

[cpu]
pin_cores=no                   ; the important line: the default pins to random CPUs here
park_cores=no
  • Without pin_cores=no, the game ran on 3 CPUs at half the frame rate.
  • With it, there was no GameMode pinning in a 40-minute session.

Check on your machine while the game runs:

# the game is the victory.exe process with the most threads (its name shows as "main")
pid=$(ps -eo pid=,nlwp=,args= | grep -i '[v]ictory\.exe' | sort -k2 -n | tail -1 | awk '{print $1}')
grep -h Cpus_allowed_list /proc/$pid/task/*/status | sort | uniq -c   # all threads should show 0-11 (all CPUs)
journalctl --user -b | grep -c 'gamemoded.*Pinning process\.\.\.$'   # should be 0

If the first command shows a short list like 1,4,8 for every thread, GameMode is pinning the game. Set pin_cores=no, or run without gamemoderun.

#455 vs main

main, pinned #455, pinned main, unpinned #455, unpinned
Runs (time in the world) 4 (18 min) 1 (6.3 min) 2 (3.6 min) 1 (2.1 min)
Median fps 15.5 14.0 29.5 34.9
Average fps 11.8 11.9 21.9 23.5
1 % low 1.2 1.5 3.3 2.1
Frames ≥ 50 ms per minute 451 542 324 277
Busiest title thread 18–38 % 25 % 75–80 % 46 %
victory-apu-0..3 – 4 × ~4 % (wait ~19 %) – 4 × ~6.7 % (wait ~7 %)

Reading:

  • Unpinned, #455 does what #137 asks for. It moves about a third of the busiest title thread's load onto the workers, and the workers rarely wait for a core.
  • 6 cores were not too few for the extra threads at HIGHEST. There was no loss like the −12 % from #155. Median fps rose 18 % and frames ≥ 50 ms per minute fell 15 %.
  • The 1 % low was lower, though, and the unpinned runs are short. The fps gain is a trend for now, not a result.

Next (to be posted here):

  • main and #455 unpinned, ≥ 5 min in the world each, alternating
  • #455 with the workers at NORMAL priority, and #175, unpinned
**@⁠hugodasschlossgespenst** on GitHub · 2026-12-05 17:08 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/455#issuecomment-7441884186) ## Measurements on a 6-core Zen 1: #455 helps here, but only once GameMode stops pinning the game TL;DR - **With #455, the busiest title thread drops from ~78 % to ~46 % of a core.** The four `victory-apu` workers share ~27 % of a core, waiting very little. The fps doesn't drop (unlike the −12 % in #155); in the one run so far median fps went from 29.5 to 34.9. - **The first round of measurements showed nothing, because GameMode had pinned the whole game to 3 of 12 CPUs** (bug report: https://github.com/FeralInteractive/gamemode/issues/582). That halved the fps and hid any effect of where Opus is decoded. If you measure through `gamemoderun`, check the affinity first. - **The data is still thin:** 2–3 minutes per variant in the open world once unpinned. Longer runs will follow in this thread. ### Setup | | | |---|---| | CPU | Ryzen 5 1600, 6 cores / 12 threads, Zen 1, 2 × 8 MB L3; fixed ~3.4 GHz, no cpufreq driver | | GPU | RX 7900 XTX, ReBAR on | | RAM | 2 × DDR4-2933 | | OS | Fedora 45, kernel 7.3.6 | | Wine | GE-Proton11-16, NTSync | | VIctory | 0fd7614; `pr455` = only `apu.rs` swapped for bca57f8 (4 workers, HIGHEST) | | Settings | Series S profile, 1920×1080, lowest detail; the 60 fps cap stays on while measuring | How it was measured: - **Frame times:** from `VICTORY_PRESENT_TIMING=1`. - **Per-thread CPU use and wait time:** sampled once a second from `/proc/<pid>/task/*/schedstat`, the same method as in #149/#155. - **What counts:** only play in the open world, on the same save and the same walk-then-drive loop. Left out: the first 90 s, and every 5-s window where ≥ 90 % of frames sit at the cap (menus, pause, map). ### The affinity problem In the first runs, **all 148 threads of the game process were limited to CPUs {1, 4, 8}**. - **Neither VIctory, Wine nor the title did this.** Wine's own thread list shows `affinity 0xfff` for the threads. The pinning comes from outside, via `sched_setaffinity`. - **The source is GameMode 1.8.2.** Its `pin_cores` option is on by default. On a CPU with uniform L3 and no cpufreq driver it builds the core set from uninitialised memory. It also re-applies the set every few seconds: resetting it by hand took 34,503 resets in one session. - **Fix:** with `[cpu] pin_cores=no` in `~/.config/gamemode.ini`, all threads may use all 12 CPUs again. A 40-minute session with this setting had no GameMode pinning at all. Same build (`main`), pinned vs not pinned: | | pinned (4 runs, 18 min) | unpinned (2 runs, 3.6 min) | |---|---|---| | Median fps | 15.5 | 29.5 | | Average fps | 11.8 | 21.9 | | 1 % low | 1.2 | 3.3 | | Frames ≥ 50 ms per minute | 451 | 324 | | CPU used by the game's threads | ~2.9 cores | ~7.9 cores | | Busiest title thread | 25–38 % of a core | 75–80 % | While pinned, about 150 threads share 3 CPUs. Every thread mostly waits, so where Opus is decoded can't make a difference. The pinned #455 run confirms this (table below). ### The combination that gives ~30 fps here, and how to check it on your machine The game now holds a steady ~30 fps in the open world on this machine, up from ~15. Three things work together: **1. The CPU: Ryzen 5 1600 (Zen 1, 6 cores / 12 threads, no cpufreq driver)** - **It is what triggers the GameMode bug.** Without a cpufreq driver, `/sys/devices/system/cpu/cpu*/cpufreq/` does not exist. With uniform L3, GameMode then picks its core set from uninitialised memory. A CPU with a cpufreq driver or with non-uniform L3 (X3D) takes a different path. - **6 cores are enough once the game may use all of them.** Unpinned, the game uses ~8 of the 12 logical CPUs, and the busiest thread is 75–80 % busy, not waiting for a core. - **Check:** - `ls /sys/devices/system/cpu/cpu0/cpufreq/` (missing = affected if GameMode pins) - `cat /sys/devices/system/cpu/cpu*/cache/index3/size | sort -u` (one line = uniform L3) **2. A "game mode" guard script around `run.sh` that shuts down the rest of the desktop** A small wrapper (not part of VIctory) that runs while the game runs: - Stops background services and masks them for the session (`systemctl mask --runtime`): indexers, sync clients, assistants, input methods, GNOME background services. It closes the browser and sync apps. - Pushes idle memory into zram (zstd) with `memory.reclaim` before the start, so the game has as much RAM as possible. Then it sets swappiness back to the system's value, so the game's own pages aren't swapped out. - Releases the kdump crash-kernel reserve (256 MB). - Restores everything when the game exits. Careful when building something like this: re-masking units in a loop makes systemd reload its whole configuration on every call. My first version did that every 15 s and cost more than a CPU core (2,745 reloads in 40 min). Mask once, in one call, and only units that aren't masked yet. All measured runs, pinned and unpinned, were made with this guard on, so its own effect is not measured separately. What it does: on this machine, everything except the game stays below ~0.5 CPU core while the game runs. **3. GameMode (`gamemoderun`), with core pinning switched off** `~/.config/gamemode.ini`: ```ini [general] renice=10 ; game threads at nice -10 inhibit_screensaver=1 [gpu] apply_gpu_optimisations=accept-responsibility gpu_device=0 amd_performance_level=high ; GPU at full clocks while the game runs [cpu] pin_cores=no ; the important line: the default pins to random CPUs here park_cores=no ``` - Without `pin_cores=no`, the game ran on 3 CPUs at half the frame rate. - With it, there was no GameMode pinning in a 40-minute session. **Check on your machine while the game runs:** ```sh # the game is the victory.exe process with the most threads (its name shows as "main") pid=$(ps -eo pid=,nlwp=,args= | grep -i '[v]ictory\.exe' | sort -k2 -n | tail -1 | awk '{print $1}') grep -h Cpus_allowed_list /proc/$pid/task/*/status | sort | uniq -c # all threads should show 0-11 (all CPUs) journalctl --user -b | grep -c 'gamemoded.*Pinning process\.\.\.$' # should be 0 ``` If the first command shows a short list like `1,4,8` for every thread, GameMode is pinning the game. Set `pin_cores=no`, or run without `gamemoderun`. ### #455 vs main | | main, pinned | #455, pinned | main, unpinned | #455, unpinned | |---|---|---|---|---| | Runs (time in the world) | 4 (18 min) | 1 (6.3 min) | 2 (3.6 min) | 1 (2.1 min) | | Median fps | 15.5 | 14.0 | 29.5 | **34.9** | | Average fps | 11.8 | 11.9 | 21.9 | **23.5** | | 1 % low | 1.2 | 1.5 | 3.3 | 2.1 | | Frames ≥ 50 ms per minute | 451 | 542 | 324 | **277** | | Busiest title thread | 18–38 % | 25 % | 75–80 % | **46 %** | | `victory-apu-0..3` | – | 4 × ~4 % (wait ~19 %) | – | 4 × ~6.7 % (wait ~7 %) | Reading: - **Unpinned, #455 does what #137 asks for.** It moves about a third of the busiest title thread's load onto the workers, and the workers rarely wait for a core. - **6 cores were not too few for the extra threads at HIGHEST.** There was no loss like the −12 % from #155. Median fps rose 18 % and frames ≥ 50 ms per minute fell 15 %. - **The 1 % low was lower, though,** and the unpinned runs are short. The fps gain is a trend for now, not a result. Next (to be posted here): - [ ] main and #455 unpinned, ≥ 5 min in the world each, alternating - [ ] `#455` with the workers at NORMAL priority, and #175, unpinned
This pull request has changes conflicting with the target branch.
  • crates/victory-hle/src/modules/apu.rs
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/455/head:github-bot/gh-455'
git switch 'github-bot/gh-455'

Merge

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