hle: apu: decode Opus on dedicated worker threads (VICTORY_APU_ASYNC, port from ReVIctory) #455
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/lobomfz--VIctory-xmem!455
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-455"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@lucamzanon on GitHub · 2026-12-04 11:32 UTC · original
lucamzanon:xmem/9-apu-async→main· +610 −76 · 3 files · 2 commits · openSummary
Ports
ReTerryDavis/ReVIctorycommit71de60b2(PR #37, original author Jonas G. Morsch,Co-authored-bypreserved in commit) toVIctory-xmem, offloading Opus audio decoding off the title's threads onto a dedicated worker pool (crates/victory-hle/src/modules/apu.rsonly).Settings
VICTORY_APU_ASYNC: on by default (0restores inline synchronous decoding).VICTORY_APU_THREADS: worker count (default4).Hardening added during the port
threads_queueing_on_one_stream_run_in_result_ordercovering both async and sync fallback modes.XApuConnect.Verification (RX 9070 XT / Ryzen 7 5700X, Linux RADV)
apuunit tests executed under Wine against the cross-compiledx86_64-pc-windows-msvctest binary: 20/20 passes acrossVICTORY_APU_THREADS=1,4, and16.cargo checkandcargo test --no-run(x86_64-pc-windows-msvc) pass with 0 warnings;cargo clippy --no-deps --all-targets -p victory-hlereports 0 findings inapu.rs.4m(VICTORY_BENCH=rt, 4K FSR 4 Quality, no FG): 0 crashes, warm run27.10 fps/9.701% low, clean audio playback.@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.XApuGetNextResultreturnsPENDING_RESULTSbecause the front is not done, and when A finishes nothing signals (crates/victory-hle/src/modules/apu.rs:308-316vs:575-576). Signal whenever the front slot becomes done — one relaxed load.apu.rs:208enables it for every user whilecfg!(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.VICTORY_APU_ASYNCandVICTORY_APU_THREADSexist only in the module doc and inasynchronous()/threads()(apu.rs:9-10,:202-225). Nothing indocs/SETTINGS.md, nothing inrun.conf.default, so perREADME.md:125the 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; andXApuDisconnectspinning 5 s onin_flightand then leaking rather than handing a worker freed memory is the honest choice.40 behind,
apu.rsuntouched onmainsince the merge base. The one thing this PR does not carry from the older one is theREADME.mdnote.9090ac803etobca57f8f88@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 inbca57f8:front_seq: AtomicU64onShared(incremented onXApuDequeueResultpop) withSeqCstordering onSlot::doneandfront_seq;run()now signalsshared.eventwhensignal || seq == shared.front_seq.load(Ordering::SeqCst). Added deterministic regression testfront_completion_signals_event_after_later_signal_job(holds stream 0's decoder lock while stream 1'sSIGNALjob completes and wakes the auto-reset event, verifiesPENDING_RESULTS, then releases stream 0 and asserts the event signals again).!cfg!(test)fromasynchronous()and updated the unit tests to dequeue via a bounded poll helper. Verified all 5modules::apu::testspass under Wine with both default (VICTORY_APU_ASYNC=1) andVICTORY_APU_ASYNC=0.VICTORY_APU_ASYNCandVICTORY_APU_THREADStodocs/SETTINGS.mdandrun.conf.default(F1 menu).0 clippy warnings in
apu.rs.@Kal0s-design on GitHub · 2026-12-04 14:04 UTC · original
Tested
bca57f8natively on Windows 11 (MSVC, not Wine), CPU only, no game run.cargo test -p victory-hle --lib -- --test-threads=2with 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
0(apu.rs:207-211, also in SETTINGS.md and run.conf.default). With0the command runs inline under the queue lock (apu.rs:520), so it is close to main but not identical. The event is now set whensignal || 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), but0is not quite "as before" the way the module doc says (apu.rs:14-15).process(apu.rs:556-569), stores the result, then doesdone.store(SeqCst)(apu.rs:314-315). Dequeue and GetResultCount only look at a slot afterdone.load(SeqCst)(apu.rs:176, 586, 613). So the title can't see a result before its bytes are written. The stress test agrees.inputand writesoutputlater (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.Mutex<Option<OpusDecoder>>(apu.rs:151, 281), and a stream always goes to workerstream % 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).in_flightfor 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.run. If one happens, that worker thread dies, its slot never becomes done, andin_flightnever 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.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
@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=0firing the extrais_frontSetEventfor non-SIGNALcommands on an empty queue — since inlineASYNC=0cannot complete out of order, gating the front-slot wake onasynchronous(signal || (asynchronous && is_front)) would keepVICTORY_APU_ASYNC=0100% identical tomain. Happy to push that 1-line tweak if maintainers prefer.@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
victory-apuworkers 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.gamemoderun, check the affinity first.Setup
0fd7614;pr455= onlyapu.rsswapped forbca57f8(4 workers, HIGHEST)How it was measured:
VICTORY_PRESENT_TIMING=1./proc/<pid>/task/*/schedstat, the same method as in #149/#155.The affinity problem
In the first runs, all 148 threads of the game process were limited to CPUs {1, 4, 8}.
affinity 0xffffor the threads. The pinning comes from outside, viasched_setaffinity.pin_coresoption 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.[cpu] pin_cores=noin~/.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: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)
/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.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.shthat shuts down the rest of the desktopA small wrapper (not part of VIctory) that runs while the game runs:
systemctl mask --runtime): indexers, sync clients, assistants, input methods, GNOME background services. It closes the browser and sync apps.memory.reclaimbefore 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.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:pin_cores=no, the game ran on 3 CPUs at half the frame rate.Check on your machine while the game runs:
If the first command shows a short list like
1,4,8for every thread, GameMode is pinning the game. Setpin_cores=no, or run withoutgamemoderun.#455 vs main
victory-apu-0..3Reading:
Next (to be posted here):
#455with the workers at NORMAL priority, and #175, unpinnedView 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.Merge
Merge the changes and update on Forgejo.