Host retirement: 64 cache-line-aligned shards with lock-free watermark skip (VICTORY_LOCK_TRIM) #433

Open
Pototoooo wants to merge 1 commit from upstream-pr/433 into main
Pototoooo commented 2026-12-02 09:35:18 +00:00 (Migrated from github.com)

Imported from readmet3xt-lab/foxhound. Originally posted by Pototoooo on 2026-12-02T09:35:18Z.


Summary

Completes RFC #413 patch 09 (part 2, MIT-0) after 618da3c (VICTORY_LOCK_TRIM in watch.rs) and release_dynamic_batch in descriptor.rs:

  • Extracts deferred host retirement (RETIRED, retire, retire_resource, retire_resources, bury, bury_lag, bury_timed) out of crates/victory-gpu/src/host.rs into crates/victory-gpu/src/host/retire.rs (also reducing host.rs by ~10.5 KiB).
  • Replaces the single global RETIRED: Mutex<VecDeque<(u64, Retired)>> with 64 cache-line-aligned (#[repr(align(64))]) per-thread shards (Shard) so concurrent thin-recording, staging, descriptor, and submission threads do not contend or false-share when retiring resources.
  • Tracks each shard's front submission fence in an AtomicU64 watermark updated under the shard lock on push-into-empty and on every drain in bury(), allowing bury() to skip empty or unready shards without taking any lock (VICTORY_LOCK_TRIM=0 disables the watermark skip and checks every shard under its lock, matching watch.rs).

Invariant check

drain_up_to(finished) drains strictly from the front (take_while(|(at, _)| *at <= finished)), so a shard can yield items if and only if its queue is non-empty and front.0 <= finished. Because oldest is updated to q.front().map_or(u64::MAX, |(at, _)| *at) under shard.queue's lock on push-into-empty and after every drain, oldest == u64::MAX || oldest > finished skips locking if and only if take_while would drain zero items.

Verification

  • New unit tests in host::retire::tests:
    • shard_watermark_tracks_front_through_pushes_and_drains (empty, ordered push, overtaking push, partial drain, full drain)
    • shard_alignment_prevents_false_sharing
    • bury_drains_across_multiple_threads_and_shards (8 concurrent producer threads across shards + lag boundary check + async drop)
  • cargo clippy --workspace --all-targets --locked -- -D warnings: clean
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: clean
  • cargo test -p victory-gpu -p victory-hle -p victory-core -p victory-pipeline -p victory-loader --locked --lib: all passing (victory-gpu 622, victory-hle 196, victory-loader 60, victory-core 13, victory-pipeline 7; 0 failed)
  • Note: xtask asset-policy check passes on host/retire.rs (17,434 bytes); host.rs shrinks from 717,220 to 706,735 bytes (pre-existing size over the 640 KiB limit on main is unrelated).

After procotos-art's RFC #413 patch 09, part 2 (MIT-0).

<!-- greyfinch-sync:readmet3xt-lab/foxhound:issue:5603726568 --> Imported from [readmet3xt-lab/foxhound](https://github.com/readmet3xt-lab/foxhound/pull/433). Originally posted by [Pototoooo](https://github.com/Pototoooo) on 2026-12-02T09:35:18Z. --- ## Summary Completes RFC #413 patch 09 (part 2, MIT-0) after `618da3c` (`VICTORY_LOCK_TRIM` in `watch.rs`) and `release_dynamic_batch` in `descriptor.rs`: - Extracts deferred host retirement (`RETIRED`, `retire`, `retire_resource`, `retire_resources`, `bury`, `bury_lag`, `bury_timed`) out of `crates/victory-gpu/src/host.rs` into `crates/victory-gpu/src/host/retire.rs` (also reducing `host.rs` by ~10.5 KiB). - Replaces the single global `RETIRED: Mutex<VecDeque<(u64, Retired)>>` with 64 cache-line-aligned (`#[repr(align(64))]`) per-thread shards (`Shard`) so concurrent thin-recording, staging, descriptor, and submission threads do not contend or false-share when retiring resources. - Tracks each shard's front submission fence in an `AtomicU64` watermark updated under the shard lock on push-into-empty and on every drain in `bury()`, allowing `bury()` to skip empty or unready shards without taking any lock (`VICTORY_LOCK_TRIM=0` disables the watermark skip and checks every shard under its lock, matching `watch.rs`). ## Invariant check `drain_up_to(finished)` drains strictly from the front (`take_while(|(at, _)| *at <= finished)`), so a shard can yield items if and only if its queue is non-empty and `front.0 <= finished`. Because `oldest` is updated to `q.front().map_or(u64::MAX, |(at, _)| *at)` under `shard.queue`'s lock on push-into-empty and after every drain, `oldest == u64::MAX || oldest > finished` skips locking if and only if `take_while` would drain zero items. ## Verification - New unit tests in `host::retire::tests`: - `shard_watermark_tracks_front_through_pushes_and_drains` (empty, ordered push, overtaking push, partial drain, full drain) - `shard_alignment_prevents_false_sharing` - `bury_drains_across_multiple_threads_and_shards` (8 concurrent producer threads across shards + lag boundary check + async drop) - `cargo clippy --workspace --all-targets --locked -- -D warnings`: clean - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: clean - `cargo test -p victory-gpu -p victory-hle -p victory-core -p victory-pipeline -p victory-loader --locked --lib`: all passing (`victory-gpu` 622, `victory-hle` 196, `victory-loader` 60, `victory-core` 13, `victory-pipeline` 7; 0 failed) - Note: `xtask` asset-policy check passes on `host/retire.rs` (`17,434` bytes); `host.rs` shrinks from `717,220` to `706,735` bytes (pre-existing size over the 640 KiB limit on `main` is unrelated). After procotos-art's RFC #413 patch 09, part 2 (MIT-0).
GValdezN commented 2026-12-02 16:51:03 +00:00 (Migrated from github.com)

Imported from readmet3xt-lab/foxhound. Originally posted by GValdezN on 2026-12-02T16:51:03Z.


Good refactor — extracting retirement into its own module makes the code much more navigable, and the per-shard watermark skip is the right pattern (same philosophy as our summary bitmap in #472 — check a cheap atomic before grabbing the lock).

The #[repr(align(64))] on each shard prevents false sharing, which is important since bury() iterates all 64 on the submitting thread. No conflict with our journal sharding or barriers work — different data structures, different files.

One thought: the watermark front_fence skip means bury() only locks shards whose front item might be ready. For the common case where GPU is 2-3 frames ahead, most shards will be skipped — nice.

<!-- greyfinch-sync:readmet3xt-lab/foxhound:comment:7352546355 --> Imported from [readmet3xt-lab/foxhound](https://github.com/readmet3xt-lab/foxhound/pull/433#issuecomment-7352546355). Originally posted by [GValdezN](https://github.com/GValdezN) on 2026-12-02T16:51:03Z. --- Good refactor — extracting retirement into its own module makes the code much more navigable, and the per-shard watermark skip is the right pattern (same philosophy as our summary bitmap in #472 — check a cheap atomic before grabbing the lock). The `#[repr(align(64))]` on each shard prevents false sharing, which is important since `bury()` iterates all 64 on the submitting thread. No conflict with our journal sharding or barriers work — different data structures, different files. One thought: the watermark `front_fence` skip means `bury()` only locks shards whose front item might be ready. For the common case where GPU is 2-3 frames ahead, most shards will be skipped — nice.
Pototoooo commented 2026-12-03 03:09:38 +00:00 (Migrated from github.com)

Imported from readmet3xt-lab/foxhound. Originally posted by Pototoooo on 2026-12-03T03:09:38Z.


Thanks for taking a look, @GValdezN! Agreed — the front_fence atomic watermark keeps bury() lock-free for shards whose front submission isn't ready yet, and isolating retirement into host/retire.rs keeps it cleanly decoupled from the journal and barrier paths.

<!-- greyfinch-sync:readmet3xt-lab/foxhound:comment:7366265619 --> Imported from [readmet3xt-lab/foxhound](https://github.com/readmet3xt-lab/foxhound/pull/433#issuecomment-7366265619). Originally posted by [Pototoooo](https://github.com/Pototoooo) on 2026-12-03T03:09:38Z. --- Thanks for taking a look, @GValdezN! Agreed — the `front_fence` atomic watermark keeps `bury()` lock-free for shards whose front submission isn't ready yet, and isolating retirement into `host/retire.rs` keeps it cleanly decoupled from the journal and barrier paths.
tillo88 commented 2026-12-03 03:44:41 +00:00 (Migrated from github.com)

Imported from readmet3xt-lab/foxhound. Originally posted by tillo88 on 2026-12-03T03:44:41Z.


Replay-bench measurement: the earlier ~3% slowdown did not repeat; no measurable difference on v0.8.3.3

Measured: PR head 4c227c3 merged onto release v0.8.3.3 (same code as main 7dbcdcb), 4 measured runs in two separate jobs (each job after a warm-up run that is not counted), against 8 runs of v0.8.3.3 without the PR, same route (~6.5 min). The PR's default (VICTORY_LOCK_TRIM not set) was used.

run fps CPU prep ms median ms p95 ms p99 ms frames > 100 ms
release (8 runs, mean; fps range 36.15-37.12) 36.60 19.57 28.45 39.70 47.35 14.9
PR #433 run 1 (job 1) 35.21 19.76 30.10 41.18 49.96 20
PR #433 run 2 (job 1) 35.51 19.88 29.35 39.62 46.87 15
PR #433 run 3 (job 2) 36.86 18.70 28.30 38.62 45.55 13
PR #433 run 4 (job 2) 36.47 19.77 28.60 38.52 45.08 10

The first job's two runs were both below the slowest release run (36.15 fps); the repeat job's two runs are inside the release's range. Mean over all 4 PR runs: 36.01 fps vs 36.60 (-1.6%), with 2 of 4 runs inside the release range. That is not enough to call a slowdown, so: no measurable difference on this route. The first job's lower numbers remain unexplained.

Crashes: none in the PR runs and none in the release runs (no host GPU failure, no device lost).

Config: RTX 5070 Ti 16 GB, Ryzen 7 9800X3D, 32 GB, Windows 11; default play-foxhound.ps1 settings (Series X profile, 1440p, DLSS balanced, no frame generation), fullscreen, no frame cap.

Measured unattended on a replay bench (same save, recorded controller input); numbers and text prepared with Claude.

<!-- greyfinch-sync:readmet3xt-lab/foxhound:comment:7366888689 --> Imported from [readmet3xt-lab/foxhound](https://github.com/readmet3xt-lab/foxhound/pull/433#issuecomment-7366888689). Originally posted by [tillo88](https://github.com/tillo88) on 2026-12-03T03:44:41Z. --- **Replay-bench measurement: the earlier ~3% slowdown did not repeat; no measurable difference on v0.8.3.3** Measured: PR head `4c227c3` merged onto release **v0.8.3.3** (same code as main `7dbcdcb`), 4 measured runs in two separate jobs (each job after a warm-up run that is not counted), against 8 runs of v0.8.3.3 without the PR, same route (~6.5 min). The PR's default (`VICTORY_LOCK_TRIM` not set) was used. | run | fps | CPU prep ms | median ms | p95 ms | p99 ms | frames > 100 ms | |---|---|---|---|---|---|---| | release (8 runs, mean; fps range 36.15-37.12) | 36.60 | 19.57 | 28.45 | 39.70 | 47.35 | 14.9 | | PR #433 run 1 (job 1) | 35.21 | 19.76 | 30.10 | 41.18 | 49.96 | 20 | | PR #433 run 2 (job 1) | 35.51 | 19.88 | 29.35 | 39.62 | 46.87 | 15 | | PR #433 run 3 (job 2) | 36.86 | 18.70 | 28.30 | 38.62 | 45.55 | 13 | | PR #433 run 4 (job 2) | 36.47 | 19.77 | 28.60 | 38.52 | 45.08 | 10 | The first job's two runs were both below the slowest release run (36.15 fps); the repeat job's two runs are inside the release's range. Mean over all 4 PR runs: 36.01 fps vs 36.60 (-1.6%), with 2 of 4 runs inside the release range. That is not enough to call a slowdown, so: **no measurable difference** on this route. The first job's lower numbers remain unexplained. Crashes: none in the PR runs and none in the release runs (no host GPU failure, no device lost). Config: RTX 5070 Ti 16 GB, Ryzen 7 9800X3D, 32 GB, Windows 11; default `play-foxhound.ps1` settings (Series X profile, 1440p, DLSS balanced, no frame generation), fullscreen, no frame cap. Measured unattended on a replay bench (same save, recorded controller input); numbers and text prepared with Claude.
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/433:upstream-pr/433'
git switch 'upstream-pr/433'

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/433'
git switch 'upstream-pr/433'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/433'
git switch 'upstream-pr/433'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/433'
git switch 'main'
git merge --squash 'upstream-pr/433'
git switch 'main'
git merge --ff-only 'upstream-pr/433'
git switch 'main'
git merge 'upstream-pr/433'
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
readmet3xt-lab/foxhound!433
No description provided.