perf(gpu/watch): stripe GUARDS mutex 64 ways by 4 MiB address region #287

Manually merged
github merged 1 commit from gh-pr/287/head into gh-pr/287/base 2026-12-01 20:26:26 +00:00
Owner

@⁠Androtim on GitHub · 2026-12-01 14:43 UTC · original
Androtim:perf/watch-striped-guards-64 → main · +945 −343 · 1 files · 1 commits · merged 2026-12-01 15:20 UTC

Replaces the single global GUARDS mutex and global OPENED queue in crates/victory-gpu/src/watch.rs with 64 cache-line-padded address-striped shards (default-on, kill-switch VICTORY_GUARD_SHARDS=0), removing the 35–52 ms/frame cross-thread contention wall between guard(), publish(), opened_for_writing(), and land workers.

Summary of Changes (crates/victory-gpu/src/watch.rs)

  • 64-way 4 MiB block striping (GuardShard): Maps each 64 KiB block via (block >> 6) & 63 so every stripe aligns exactly with one 64-bit word of Blocks. Each #[repr(align(64))] GuardShard owns its own Guards state, per-shard opened settle queue, to_settle atomic, and diagnostics holder.
  • Per-shard + serialized cross-shard unlock settle queues: Same-shard open_unlocked faults queue and settle strictly on their own shard; rare multi-stripe aliased faults spill to CROSS_OPENED / settle_cross() (serialized cleanly via CROSS_SETTLE before any shard lock).
  • Deadlock-free ascending ShardSet locking: Multi-shard operations lock shards in strictly ascending 0..64 order and use single-snapshot snapshot_open_aliases / open_resolved_with so concurrent inplace view mappings cannot add an un-locked shard between mask discovery and opening.
  • Per-word non-blocking guard(): Locks one 4 MiB shard at a time in Pass 1 (clean check + absent collection) and Pass 3 (per-word dirty protect + inline same-shard alias cover; rare cross-shard aliases clear clean immediately and resolve atomically in ascending order).
  • Unit tests: Added sharded_guards_do_not_block_distinct_stripes and cross_stripe_alias_settle_and_open_preserve_invariants.

Verification

  • cargo +1.98.1-x86_64-pc-windows-gnu xtask policy: 317 files ok
  • cargo +1.98.1-x86_64-pc-windows-gnu test -p victory-gpu --locked watch:: -- --test-threads=1: 55/55 PASS
  • cargo +1.98.1-x86_64-pc-windows-gnu test -p victory-gpu --locked --features diagnostics lock_holders_attributes_contended_guards_waits -- --test-threads=1: 1/1 PASS
  • Dual workspace clippy --workspace --all-targets --locked -- -D warnings (default + --features diagnostics): PASS (0 warnings)

Real-GPU Verification (RTX 4060 Ti 8 GB / Series X -PlayParity, 1440p + DLSS Quality, FG off, RT off)

Plain --release 60s A/B (NO --features diagnostics, Draw-Matched Outdoor 6.5k–7.8k Draws)

Metric main (6.7k–7.0k draws) This PR r1 (6.5k–6.9k) This PR r2 (7.4k–7.8k)
Median FPS / Frame Time 10.9 fps (91.5 ms) 17.35 fps (+59.2%) (57.6 ms) 14.3 fps (+31.2%) (70.0 ms)
Preparing / refreshes 38.4 ms (10.8–16.9 ms) 28.2 ms (8.1–10.3 ms) 26.8 ms (8.3–9.2 ms)
land guards (13k spike) 12.79–16.36 ms (e4310834) 5.63–7.09 ms 6.18–15.90 ms
Texture Queue (final_waiting) 10,459 1,315 5,580
VRAM Max / Over / Crashes 5,928 MB / 0 / 0 6,069 MB / 0 / 0 (room_false=0%) 5,991 MB / 0 / 0 (room_false=10%)

90s --features diagnostics Lock Telemetry & Visual Inspection

  • GUARDS contention (waits for the guards' lock per frame): dropped ~94%, from 127–138 waits / 35.00–52.19 ms on main down to 44–48 waits / 1.82–3.22 ms!
  • Visual & Stability: 24/24 lit PNGs (luma = 48.91,
    ear_black = 0.83%, edge_energy = 5.31, magenta = 0.0%, ailed = 0), crashes = 0, over = 0 MB. Visually inspected world PNGs ( rame-2785.png, rame-2833.png).
**@⁠Androtim** on GitHub · 2026-12-01 14:43 UTC · [original](https://github.com/Androtim/MiniVI/pull/287) `Androtim:perf/watch-striped-guards-64` → `main` · +945 −343 · 1 files · 1 commits · merged 2026-12-01 15:20 UTC Replaces the single global `GUARDS` mutex and global `OPENED` queue in `crates/victory-gpu/src/watch.rs` with **64 cache-line-padded address-striped shards** (default-on, kill-switch `VICTORY_GUARD_SHARDS=0`), removing the 35–52 ms/frame cross-thread contention wall between `guard()`, `publish()`, `opened_for_writing()`, and `land` workers. ### Summary of Changes (`crates/victory-gpu/src/watch.rs`) - **64-way 4 MiB block striping (`GuardShard`):** Maps each 64 KiB block via `(block >> 6) & 63` so every stripe aligns exactly with one 64-bit word of `Blocks`. Each `#[repr(align(64))] GuardShard` owns its own `Guards` state, per-shard `opened` settle queue, `to_settle` atomic, and diagnostics `holder`. - **Per-shard + serialized cross-shard unlock settle queues:** Same-shard `open_unlocked` faults queue and settle strictly on their own shard; rare multi-stripe aliased faults spill to `CROSS_OPENED` / `settle_cross()` (serialized cleanly via `CROSS_SETTLE` before any shard lock). - **Deadlock-free ascending `ShardSet` locking:** Multi-shard operations lock shards in strictly ascending `0..64` order and use single-snapshot `snapshot_open_aliases` / `open_resolved_with` so concurrent `inplace` view mappings cannot add an un-locked shard between mask discovery and opening. - **Per-word non-blocking `guard()`:** Locks one 4 MiB shard at a time in Pass 1 (clean check + absent collection) and Pass 3 (per-word dirty protect + inline same-shard alias cover; rare cross-shard aliases clear `clean` immediately and resolve atomically in ascending order). - **Unit tests:** Added `sharded_guards_do_not_block_distinct_stripes` and `cross_stripe_alias_settle_and_open_preserve_invariants`. ### Verification - `cargo +1.98.1-x86_64-pc-windows-gnu xtask policy`: **317 files ok** - `cargo +1.98.1-x86_64-pc-windows-gnu test -p victory-gpu --locked watch:: -- --test-threads=1`: **55/55 PASS** - `cargo +1.98.1-x86_64-pc-windows-gnu test -p victory-gpu --locked --features diagnostics lock_holders_attributes_contended_guards_waits -- --test-threads=1`: **1/1 PASS** - Dual workspace `clippy --workspace --all-targets --locked -- -D warnings` (default + `--features diagnostics`): **PASS (0 warnings)** ### Real-GPU Verification (RTX 4060 Ti 8 GB / Series X `-PlayParity`, 1440p + DLSS Quality, FG off, RT off) #### Plain `--release` 60s A/B (NO `--features diagnostics`, Draw-Matched Outdoor `6.5k–7.8k` Draws) | Metric | `main` (`6.7k–7.0k` draws) | This PR `r1` (`6.5k–6.9k`) | This PR `r2` (`7.4k–7.8k`) | |---|---:|---:|---:| | **Median FPS / Frame Time** | 10.9 fps (91.5 ms) | **17.35 fps (+59.2%)** (57.6 ms) | **14.3 fps (+31.2%)** (70.0 ms) | | **Preparing / `refreshes`** | 38.4 ms (10.8–16.9 ms) | **28.2 ms** (**8.1–10.3 ms**) | **26.8 ms** (**8.3–9.2 ms**) | | **`land` guards (`13k` spike)** | 12.79–16.36 ms (`e4310834`) | **5.63–7.09 ms** | 6.18–15.90 ms | | **Texture Queue (`final_waiting`)** | 10,459 | **1,315** | **5,580** | | **VRAM Max / Over / Crashes** | 5,928 MB / 0 / 0 | **6,069 MB / 0 / 0** (`room_false=0%`) | **5,991 MB / 0 / 0** (`room_false=10%`) | #### 90s `--features diagnostics` Lock Telemetry & Visual Inspection - **`GUARDS` contention (`waits for the guards' lock per frame`):** dropped **~94%**, from 127–138 waits / **35.00–52.19 ms** on `main` down to 44–48 waits / **1.82–3.22 ms**! - **Visual & Stability:** 24/24 lit PNGs (luma = 48.91, ear_black = 0.83%, edge_energy = 5.31, magenta = 0.0%, ailed = 0), crashes = 0, over = 0 MB. Visually inspected world PNGs ( rame-2785.png, rame-2833.png).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-01 20:26:22 +00:00
github manually merged commit 78606152f0 into gh-pr/287/base 2026-12-01 20:26:26 +00:00
Author
Owner

@⁠Androtim on GitHub · 2026-12-01 15:07 UTC · original

Automated triage for the maintainers (not an approval).

Verdict: needs-gpu-test · CI (check): passing

Review Summary (crates/victory-gpu/src/watch.rs)

Logic, concurrency invariants, and scoping look solid, and CI is passing. Because this changes the core CPU→GPU dirty-page guard and fault-settling path under multi-threaded rendering load, this warrants a local real-GPU smoke test before merging.

What Was Verified

  • Sharding & kill-switch (GuardShard, GUARD_SHARDS_TABLE, guard_stripe, range_mask): 64 cache-line-aligned shards map (block >> 6) & 63 to align 1:1 with Blocks u64 words (4 MiB per stripe). VICTORY_GUARD_SHARDS=0 cleanly collapses all operations onto shard 0.
  • Deadlock freedom & lock hierarchy: Multi-shard operations (ShardSet::lock_raw_mask, lock_shards, lock_shards_timed) lock shards in strictly ascending 0..64 order. settle_cross() is only called with no shard locks held, serializes via CROSS_SETTLE, and releases CROSS_OPENED before acquiring shard locks. CROSS_OPENED and per-shard opened are strictly leaf locks, and to_settle / CROSS_TO_SETTLE resets occur inside their respective mutexes, avoiding lost-wakeup races.
  • Cross-stripe alias coherency (snapshot_open_aliases, open_resolved_with, cover_in_shard, cross_covers):
    • Opening paths snapshot inplace::pages_of once per range under the current ShardSet and retry with the union mask if any alias lies outside the locked set, preventing concurrent mappings from introducing unlocked shards mid-open.
    • guard() Pass 3 keeps cross-shard aliased blocks out of g.clean until cross_covers acquires the union ShardSet, protects all aliases, and re-checks protected, !BESIDE, and !unsettled before marking clean.
    • publish() re-verifies g.clean / g.held per shard, so Pass 1's per-shard clean scan cannot cause a concurrently opened block to be falsely published.
  • Tests: Existing watch:: tests are cleanly migrated to shard helpers, and sharded_guards_do_not_block_distinct_stripes + cross_stripe_alias_settle_and_open_preserve_invariants cover distinct-stripe concurrency and cross-stripe settle invariants.

Minor Non-Blocking Note

  • In guard() Pass 1 (for s in BitsOf(range_mask(first, last))), the inner for w in w_first..w_last iterates all words for every shard in the mask and skips mismatches via if guard_stripe(w * 64) != s { continue; }. Stepping w by GUARD_SHARDS from the first word matching s would avoid redundant iterations on wide ranges.

(Independent automated triage review — not formal maintainer approval.)


Generated with Antigravity CLI (agy).

**@⁠Androtim** on GitHub · 2026-12-01 15:07 UTC · [original](https://github.com/Androtim/MiniVI/pull/287#issuecomment-7315372778) <!-- minivi-triage v1 head=78606152f08b0222619d78cf3ff7bdfc11b67ca9 verdict=needs-gpu-test ci=passing at=2026-12-01T15:07:37Z --> Automated triage for the maintainers (not an approval). **Verdict:** `needs-gpu-test` · **CI (`check`):** `passing` ### Review Summary (`crates/victory-gpu/src/watch.rs`) Logic, concurrency invariants, and scoping look solid, and CI is passing. Because this changes the core CPU→GPU dirty-page guard and fault-settling path under multi-threaded rendering load, this warrants a local real-GPU smoke test before merging. #### What Was Verified - **Sharding & kill-switch (`GuardShard`, `GUARD_SHARDS_TABLE`, `guard_stripe`, `range_mask`):** 64 cache-line-aligned shards map `(block >> 6) & 63` to align 1:1 with `Blocks` `u64` words (4 MiB per stripe). `VICTORY_GUARD_SHARDS=0` cleanly collapses all operations onto shard `0`. - **Deadlock freedom & lock hierarchy:** Multi-shard operations (`ShardSet::lock_raw_mask`, `lock_shards`, `lock_shards_timed`) lock shards in strictly ascending `0..64` order. `settle_cross()` is only called with no shard locks held, serializes via `CROSS_SETTLE`, and releases `CROSS_OPENED` before acquiring shard locks. `CROSS_OPENED` and per-shard `opened` are strictly leaf locks, and `to_settle` / `CROSS_TO_SETTLE` resets occur inside their respective mutexes, avoiding lost-wakeup races. - **Cross-stripe alias coherency (`snapshot_open_aliases`, `open_resolved_with`, `cover_in_shard`, `cross_covers`):** - Opening paths snapshot `inplace::pages_of` once per range under the current `ShardSet` and retry with the union mask if any alias lies outside the locked set, preventing concurrent mappings from introducing unlocked shards mid-open. - `guard()` Pass 3 keeps cross-shard aliased blocks out of `g.clean` until `cross_covers` acquires the union `ShardSet`, protects all aliases, and re-checks `protected`, `!BESIDE`, and `!unsettled` before marking clean. - `publish()` re-verifies `g.clean` / `g.held` per shard, so Pass 1's per-shard clean scan cannot cause a concurrently opened block to be falsely published. - **Tests:** Existing `watch::` tests are cleanly migrated to shard helpers, and `sharded_guards_do_not_block_distinct_stripes` + `cross_stripe_alias_settle_and_open_preserve_invariants` cover distinct-stripe concurrency and cross-stripe settle invariants. #### Minor Non-Blocking Note - In `guard()` Pass 1 (`for s in BitsOf(range_mask(first, last))`), the inner `for w in w_first..w_last` iterates all words for every shard in the mask and skips mismatches via `if guard_stripe(w * 64) != s { continue; }`. Stepping `w` by `GUARD_SHARDS` from the first word matching `s` would avoid redundant iterations on wide ranges. *(Independent automated triage review — not formal maintainer approval.)* --- _Generated with Antigravity CLI (`agy`)._
Author
Owner

@⁠Androtim on GitHub · 2026-12-01 15:20 UTC · original

Architect & Strict-Gate Review (78606152, CI passing, minivi-triage concurrency/deadlock/alias-coherency review verified, zero file overlap with #286):

Landmark Pillar #2 structural win — default-on 64-way address-striped GUARDS (VICTORY_GUARD_SHARDS=0 kill-switch only, zero opt-in toggles):

  • GUARDS contention (90s diag): -94% (35.00–52.19 ms -> 1.82–3.22 ms/frame)
  • Draw-matched Plain --release 60s A/B (6.5k–7.8k draws vs 10.9 fps / 38.4 ms prep / 10,459 waiting):
    • Median FPS: 17.35 fps (+59.2%) (r1, 6.5k–6.9k draws) and 14.3 fps (+31.2%) (r2, 7.4k–7.8k draws)
    • Preparing: 28.2 ms (-10.2 ms) (r1) and 26.8 ms (-11.6 ms) (r2)
    • Texture Queue (final_waiting): 1,315 (-87%) / 5,580 (-47%)
    • VRAM & Stability: over = 0 MB on both runs (6,069 / 5,991 MB), 0 crashes/DRED, 55/55 watch:: tests pass
  • Visuals: 90s --features diagnostics late in-world PNGs directly verified crisp and clean (lit=24/24, near_black=0.83%, magenta=0.0%).

Squash-merging into main.

**@⁠Androtim** on GitHub · 2026-12-01 15:20 UTC · [original](https://github.com/Androtim/MiniVI/pull/287#issuecomment-7315744005) Architect & Strict-Gate Review (`78606152`, CI passing, `minivi-triage` concurrency/deadlock/alias-coherency review verified, zero file overlap with `#286`): Landmark Pillar #2 structural win — default-on 64-way address-striped `GUARDS` (`VICTORY_GUARD_SHARDS=0` kill-switch only, zero opt-in toggles): - **`GUARDS` contention (90s diag):** **`-94%`** (`35.00–52.19 ms` -> **`1.82–3.22 ms/frame`**) - **Draw-matched Plain `--release` 60s A/B (`6.5k–7.8k` draws vs `10.9 fps` / `38.4 ms` prep / `10,459` waiting):** - **Median FPS:** **`17.35 fps (+59.2%)`** (`r1`, `6.5k–6.9k` draws) and **`14.3 fps (+31.2%)`** (`r2`, `7.4k–7.8k` draws) - **Preparing:** **`28.2 ms (-10.2 ms)`** (`r1`) and **`26.8 ms (-11.6 ms)`** (`r2`) - **Texture Queue (`final_waiting`):** **`1,315` (`-87%`)** / **`5,580` (`-47%`)** - **VRAM & Stability:** `over = 0 MB` on both runs (`6,069 / 5,991 MB`), `0` crashes/DRED, `55/55` `watch::` tests pass - **Visuals:** 90s `--features diagnostics` late in-world PNGs directly verified crisp and clean (`lit=24/24`, `near_black=0.83%`, `magenta=0.0%`). Squash-merging into `main`.
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Androtim--MiniVI!287
No description provided.