gpu: opt-in single-source GetWriteWatch collector thread (VICTORY_SUBMIT_PIPE=1) #4490

Closed
github-bot wants to merge 1 commit from github-bot/gh-4489 into main AGit
Owner

@⁠lyufeng000 on GitHub · 2026-11-30 21:10 UTC · original
lyufeng000:pr/watch-pipe → main · +1858 −30 · 5 files · 1 commits · closed without merging

Motivation

On Windows 11 (measured on RTX 4090 + Ryzen 9 9950X, 4K DLSS Quality steady state on e1251c31), write-watch collection accounts for ~5.05 ms/frame of CPU wall time across the critical path:

  • Host::submit (asking the write watch): ~2.80 ms/frame
  • descriptor::sync_heaps (victory-heap-sync, asking): ~2.25 ms/frame

With per-range ASKING locks (hold_pages()), victory-submit / victory-ask and victory-heap-sync (plus occasional helper-thread fallbacks) call GetWriteWatch(WRITE_WATCH_FLAG_RESET) concurrently on disjoint address ranges. Inside ntoskrnl, every resetting GetWriteWatch acquires process-wide AddressCreationLock / working-set / PFN locks and broadcasts cross-core TLB shootdown IPIs, causing severe kernel lock contention whenever multiple threads query simultaneously.

This PR introduces an opt-in, self-contained single-source collector thread (victory-watch-pipe) behind VICTORY_SUBMIT_PIPE=1 (off by default) that serialises all GetWriteWatch(WRITE_WATCH_FLAG_RESET) queries and overlaps submission write-watch collection with card_steps and sweep.


Architecture

New module: crates/victory-gpu/src/host/watch_pipe.rs

  • Single resident collector thread (victory-watch-pipe):
    • Lazily spawned only when VICTORY_SUBMIT_PIPE=1 (placed on fast cores via cores::keep_on_fast_cores with cores::Priority, matching victory-ask and victory-heap-sync).
    • Two-tier FIFO queue: high priority for submission askings (begin_ask_once, ask_once, ask_once_delta_reask) and normal priority for descriptor::sync_heaps (Tracked::take).
    • Concurrent request coalescing: consecutive SubmitAsk requests queued in high coalesce into a single normalized ask_once_direct kernel pass while delivering individual monotonic WatchSnapshot tokens to every waiter.
    • Shared thread-local caches: routing both Host::submit and victory-heap-sync through one thread also reuses a single watch::FOUND buffer and unifies watch::REFUSED across both callers.
    • Re-entrancy & panic isolation: thread-local IN_COLLECTOR executes nested calls inline without deadlock; catch_unwind in worker_loop ensures a panic never hangs waiting callers or kills the collector thread (SubmitAsk clears kept state and signals None for safe fallback; Task propagates the panic cleanly via resume_unwind).

Overlapped Epoch-Isolated Collection (WatchSnapshot + delta_ranges)

  1. Right after taking Host::submitted, Host::submit reuses batch.copy_spans(runs) (no extra list walk and no manifest dependency) to enqueue watch_pipe::begin_ask_once(ranges) asynchronously on victory-watch-pipe, overlapping kernel page walks with card_steps, late_blocked, and sweep.
  2. Right before ask_once_direct runs on the collector thread, it snapshots hi_ep = watch::current_write_epoch() (SeqCst) and binds a WatchSnapshot { epoch, hi_ep, settle_gen } to the WatchTicket.
  3. At the submission asking point, watch_pipe::finish_early_watch completes the ticket:
    • Fast path: if snap.is_current() (settle_gen unchanged), writes_since_epoch(snap.hi_ep) returns complete log coverage, and both uncovered = ticket.delta_ranges(&batch.ranges) and reask = intersecting_needed(&batch.ranges, &written) are empty, zero extra GetWriteWatch calls are made.
    • Incremental delta/re-ask path: if card_steps added an own list (uncovered) or an asynchronous landing / DirectStorage write / remap overlapped batch.ranges (reask, narrowed via exact two-pointer sub-range intersection), ask_once_delta_reask_direct carves only the overlapping pages from journal().ahead, runs a single coalesced pass over uncovered ∪ reask, and merges back into journal().ahead without discarding unaffected spans.
    • Fail-closed fallback: if flights were waited for, settle_gen advanced, WRITE_LOG overflowed (> 256 entries), or an unsized free (begin_write_epoch()) left an unlogged gap, finish_early_watch safely falls back to a full ask_once(&batch.ranges).

Process-Wide Kernel Serialization (watch.rs)

  • watch::ask_once and Tracked::take delegate through watch_pipe::{ask_once, take_tracked} when VICTORY_SUBMIT_PIPE=1, and execute ask_once_direct / take_direct inline when off.
  • watch::KERNEL_WATCH_LOCK wraps GetWriteWatch(WRITE_WATCH_FLAG_RESET) inside kernel_written only when watch_pipe::on(), serialising any residual helper-thread queries (texture::Staging::refresh_now, shadow.rs, memory.rs, ask_early) strictly after ASKING.

Opt-In Switches

Env Var Default Behaviour
VICTORY_SUBMIT_PIPE off (0 / unset) 1 enables victory-watch-pipe, KERNEL_WATCH_LOCK, overlapped early_watch, and SeqCst write-epoch tracking (via asked_for!, compatible with VICTORY_AB).
VICTORY_WATCH_PIPE_EPOCH on when VICTORY_SUBMIT_PIPE=1 0 disables WatchSnapshot / incremental delta_ranges while keeping single-source collector serialization. Always false when VICTORY_SUBMIT_PIPE is off.

When VICTORY_SUBMIT_PIPE is unset or "0":

  • No victory-watch-pipe thread is spawned (default_disabled_does_not_spawn_collector).
  • KERNEL_WATCH_LOCK, record_write_epoch, bump_settle_gen, and log_line are completely bypassed.
  • apps/victory/src/play.rs defaults are untouched; Host::submit executes the exact upstream e1251c31 path (including VICTORY_ASK_FIRST=1).

Correctness, Deadlock Safety & Compatibility Audit (e1251c31)

  1. Lock hierarchy (zero AB-BA cycles):
    • Host::submitted ≺ WatchPipe::state / Completion ≺ ASKING ≺ KERNEL_WATCH_LOCK / JOURNAL / GUARDS.
    • SHADER_HEAPS.read() (held by descriptor::sync_heaps around Tracked::take) ≺ WatchPipe::state / Completion; the collector thread never acquires SHADER_HEAPS, SYNC_ROUNDS, Host::submitted, or title_memory_reading.
    • WRITE_LOG is a strict leaf mutex; elsewhere / elsewhere_in (inplace::VIEWS) is resolved before acquiring WRITE_LOG.
    • rangelock::RangeLock (VICTORY_RANGE_LOCK=1) / MAPPING: no caller holds title_memory_reading* while waiting on WatchPipe.
  2. Compatibility with descriptor.rs (VICTORY_HEAP_SYNC_AT_SUBMIT=1, VICTORY_HEAP_SYNC_FIRST=1, VICTORY_SYNC_FINE=1):
    • All sync_heaps watch queries (written.take(...), including heap_hooks::reguard) route through Tracked::take → watch_pipe::take_tracked with zero changes to descriptor.rs.
    • When waited_for_flights or settled_for_sweep occurs in Host::submit, early_watch.cancel() awaits any in-flight collector pass and calls ask_once_end() before begin_sync_heaps() starts a fresh round, closing any window where victory-heap-sync could observe a stale pre-settle journal().ahead.
  3. Compatibility with PR #3762 (30911195) & WRITTEN_EPOCHS:
    • Both ask_once_direct and ask_once_delta_reask_direct maintain Journal::note (mirroring into lock-free WRITTEN_EPOCHS), strictly sorted non-touching j.ahead (merged(...)), and atomic KEPT (is_kept()) before Batch::refresh_front / Batch::flush_all run.
    • When VICTORY_SUBMIT_PIPE=1, pipe_early supersedes VICTORY_ASK_FIRST=1 so victory-ask and victory-watch-pipe never race.
  4. No missed pages (SeqCst post-write invariant):
    • Every write/remap path records its epoch after bytes land in title memory (gated by watch_pipe::on()): landed_direct, landed_beside, shadow::put_changed (RAII post-write guard), KernelWrite::drop (DirectStorage / ReadFile / fread), stream_written, title_memory_written, wrote, writing, title_memory_remapped, title_memory_unmapping, title_memory_allocated, and title_memory_freed (unsized len == 0 triggers bare begin_write_epoch() gap \to fail-closed full fallback).
    • Gap detection in record_write_epoch (ep != log.last_ep + 1 advances min_lost_ep) ensures later logged writes can never mask an earlier unlogged begin_write_epoch().

Files Changed

  • crates/victory-gpu/src/host/watch_pipe.rs (new): single-source collector thread, WatchTicket / WatchSnapshot, concurrent SubmitAsk coalescing, delta_ranges, exact sub-range intersection, finish_early_watch, diagnostics line, and 11 unit tests.
  • crates/victory-gpu/src/watch.rs: WRITING_EPOCH / WRITE_LOG with gap detection, KERNEL_WATCH_LOCK (gated on watch_pipe::on()), ask_once_direct, ask_once_delta_reask_direct, Tracked::take_direct, gated epoch hooks on write/remap/free entrypoints, and 2 unit tests.
  • crates/victory-gpu/src/host.rs: mod watch_pipe, overlapped early_watch start/cancel/finish in Host::submit, bump_settle_gen in settle / settle_stale / WHY_READ relay_wait / Batch::title_written, and watch_pipe::log_line.
  • crates/victory-gpu/src/shadow.rs: post-write RAII epoch guard in put_changed (gated on watch_pipe::on()).
  • crates/victory-gpu/src/queue/behind.rs: watch_pipe::ensure_started() on victory-submit startup (no-op when off).

Verification & CI Gates

Executed against --locked:

  • Targeted PR 2 unit tests (13 tests) — PASS (13 passed, 0 failed) in both default (VICTORY_SUBMIT_PIPE unset) and VICTORY_SUBMIT_PIPE=1 modes:
    • default_disabled_does_not_spawn_collector
    • collector_serializes_concurrent_callers_on_single_thread
    • reentrant_run_on_collector_does_not_deadlock
    • collector_survives_task_panic_and_propagates_to_caller
    • delta_ranges_subtracts_covered_and_merges_overlaps
    • intersecting_needed_returns_exact_overlapping_subranges
    • fifo_epoch_snapshot_and_settle_gen_invalidation
    • concurrent_submit_asks_coalesce_without_losing_dirty_pages
    • ask_once_delta_runs_on_collector_and_preserves_ahead
    • finish_early_watch_fast_path_reasks_overlap_and_handles_settle_or_cancel
    • concurrent_submit_and_heap_tracked_serialize_without_deadlock_or_missed_pages
    • write_log_tracks_ranges_gaps_and_overflow_falls_back_to_none
    • ask_once_delta_reask_direct_carves_refreshes_and_updates_page_epochs
  • victory-gpu full suite with VICTORY_SUBMIT_PIPE=1 + victory-hle a_read_by_range_waits_only_for_a_remapping_that_meets_it — all PR-related tests pass; zero regressions introduced.
  • cargo clippy --workspace --all-targets --locked & --all-features — 0 warnings in all touched/added files (watch_pipe.rs, watch.rs, host.rs new hunks, shadow.rs new hunk, queue/behind.rs). (Note: upstream base e1251c31 has pre-existing unrelated warnings in victory-shader/src/emit/lower.rs:114, copystat.rs:1470, shadow.rs:7206, host.rs:5092, xmem.rs:1933, pre-existing test failures in list::thin::tests::bindings_record_no_work, shader_cache::tests::every_switch_of_the_translator_is_in_the_key, split_lds_wave32_read_after_read_barrier_coalescing_and_parity, and a pre-existing xtask policy finding in crates/victory-shader/src/native.rs.)
  • cargo xtask policy — 0 violations on all added/modified files.
  • Release build: cargo build --release --locked -p victory — PASS.

Local Benchmark Results (Same-Binary Off vs On)

Measured on the same Release build (pr/watch-pipe on e1251c31), RTX 4090 + Ryzen 9 9950X, Windows 11, 4K DLSS Quality + FrameGen 2x, identical settings (including VICTORY_PLACED_COPIED=0), over two 13–14 minute free-play sessions:

  • VICTORY_SUBMIT_PIPE=0 (Off): 764 s steady / 17,483 frames, 0 errors
  • VICTORY_SUBMIT_PIPE=1 (On): 720 s steady / 16,181 frames, 0 errors

Excluding 1-second buckets that contain first-run shader/PSO compilations (PIPE=1 ran first and warmed the DXCache for PIPE=0: PIPE=0 600 clean s / 13,959 frames vs PIPE=1 466 clean s / 11,045 frames; comparable workload refreshes 8,759 vs 8,564, heap_pages 482.1 vs 472.6):

Metric (per frame, clean steady seconds) VICTORY_SUBMIT_PIPE=0 (mean / p90) VICTORY_SUBMIT_PIPE=1 (mean / p90) Delta
Host::submit: asking the write watch (sub_ask_ms) 3.02 / 3.40 ms 2.69 / 3.00 ms -10.9% / p90 -11.8%
Pure write-watch wait (sub_ask_ms - find_ask_ms) 2.58 / 2.90 ms 2.16 / 2.50 ms -16.3% / p90 -13.8%
descriptor::sync_heaps: asking (heap_ask_ms) 2.59 / 3.37 ms 2.99 / 3.75 ms +0.40 ms (includes normal-queue yield behind high submit asks)
descriptor::sync_heaps: read & compare (heap_cmp_ms) 2.72 / 3.67 ms 2.19 / 3.05 ms -19.6% / p90 -16.9% (less cross-core TLB shootdown contention)
descriptor::sync_heaps: total (heap_ms) 7.18 / 9.05 ms 6.67 / 8.18 ms -7.1% / p90 -9.5%
Host::submit: wait for heap sync (at_sub_wait) 0.26 / 0.45 ms 0.19 / 0.33 ms -24.7% / p90 -26.7%
Host::submit: total preparing (prep_ms) 28.76 / 34.70 ms 27.29 / 33.25 ms -5.1% / p90 -4.2%
Per-second median frame time (ft_med) / >50 ms share 44.41 ms / 16.05% 43.14 ms / 13.64% -2.9% / fewer >50 ms frames

Steady-state VICTORY_SUBMIT_PIPE=1 collector line averages (all 720 steady seconds): 22.10 submit asks/frame, 19.31 heap-sync takes/frame; early-watch 12.81 fast hits + 4.56 delta/reask vs 0.08 full fallbacks (>99.5% incremental/fast-path hit rate).

(Note: these are two full free-play runs rather than fixed-camera VICTORY_AB sampling. For strict fixed-camera A/B reproduction on diagnostics builds, run VICTORY_AB=20,VICTORY_SUBMIT_PIPE.)

Recommended extra matrix checks:

  • VICTORY_SUBMIT_PIPE=1 VICTORY_HEAP_SYNC_AT_SUBMIT=1 VICTORY_SYNC_FINE=1
  • VICTORY_SUBMIT_PIPE=1 VICTORY_HEAP_SYNC_FIRST=1 VICTORY_RANGE_LOCK=1
  • VICTORY_SUBMIT_PIPE=1 VICTORY_SUBMIT_SPLIT=1
  • Diagnostics live A/B: VICTORY_AB=20,VICTORY_SUBMIT_PIPE
**@⁠lyufeng000** on GitHub · 2026-11-30 21:10 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4489) `lyufeng000:pr/watch-pipe` → `main` · +1858 −30 · 5 files · 1 commits · closed without merging ## Motivation On Windows 11 (measured on RTX 4090 + Ryzen 9 9950X, 4K DLSS Quality steady state on `e1251c31`), write-watch collection accounts for **~5.05 ms/frame** of CPU wall time across the critical path: - `Host::submit` (`asking the write watch`): **~2.80 ms/frame** - `descriptor::sync_heaps` (`victory-heap-sync`, `asking`): **~2.25 ms/frame** With per-range `ASKING` locks (`hold_pages()`), `victory-submit` / `victory-ask` and `victory-heap-sync` (plus occasional helper-thread fallbacks) call `GetWriteWatch(WRITE_WATCH_FLAG_RESET)` concurrently on disjoint address ranges. Inside `ntoskrnl`, every resetting `GetWriteWatch` acquires process-wide `AddressCreationLock` / working-set / PFN locks and broadcasts cross-core TLB shootdown IPIs, causing severe kernel lock contention whenever multiple threads query simultaneously. This PR introduces an **opt-in**, self-contained single-source collector thread (`victory-watch-pipe`) behind `VICTORY_SUBMIT_PIPE=1` (off by default) that serialises all `GetWriteWatch(WRITE_WATCH_FLAG_RESET)` queries and overlaps submission write-watch collection with `card_steps` and `sweep`. --- ## Architecture ### New module: `crates/victory-gpu/src/host/watch_pipe.rs` - **Single resident collector thread (`victory-watch-pipe`)**: - Lazily spawned **only** when `VICTORY_SUBMIT_PIPE=1` (placed on fast cores via `cores::keep_on_fast_cores` with `cores::Priority`, matching `victory-ask` and `victory-heap-sync`). - Two-tier FIFO queue: `high` priority for submission askings (`begin_ask_once`, `ask_once`, `ask_once_delta_reask`) and `normal` priority for `descriptor::sync_heaps` (`Tracked::take`). - **Concurrent request coalescing**: consecutive `SubmitAsk` requests queued in `high` coalesce into a single normalized `ask_once_direct` kernel pass while delivering individual monotonic `WatchSnapshot` tokens to every waiter. - **Shared thread-local caches**: routing both `Host::submit` and `victory-heap-sync` through one thread also reuses a single `watch::FOUND` buffer and unifies `watch::REFUSED` across both callers. - **Re-entrancy & panic isolation**: thread-local `IN_COLLECTOR` executes nested calls inline without deadlock; `catch_unwind` in `worker_loop` ensures a panic never hangs waiting callers or kills the collector thread (`SubmitAsk` clears kept state and signals `None` for safe fallback; `Task` propagates the panic cleanly via `resume_unwind`). ### Overlapped Epoch-Isolated Collection (`WatchSnapshot` + `delta_ranges`) 1. Right after taking `Host::submitted`, `Host::submit` reuses `batch.copy_spans(runs)` (no extra list walk and no manifest dependency) to enqueue `watch_pipe::begin_ask_once(ranges)` asynchronously on `victory-watch-pipe`, overlapping kernel page walks with `card_steps`, `late_blocked`, and `sweep`. 2. Right before `ask_once_direct` runs on the collector thread, it snapshots `hi_ep = watch::current_write_epoch()` (`SeqCst`) and binds a `WatchSnapshot { epoch, hi_ep, settle_gen }` to the `WatchTicket`. 3. At the submission asking point, `watch_pipe::finish_early_watch` completes the ticket: - **Fast path**: if `snap.is_current()` (`settle_gen` unchanged), `writes_since_epoch(snap.hi_ep)` returns complete log coverage, and both `uncovered = ticket.delta_ranges(&batch.ranges)` and `reask = intersecting_needed(&batch.ranges, &written)` are empty, **zero** extra `GetWriteWatch` calls are made. - **Incremental delta/re-ask path**: if `card_steps` added an `own` list (`uncovered`) or an asynchronous landing / DirectStorage write / remap overlapped `batch.ranges` (`reask`, narrowed via exact two-pointer sub-range intersection), `ask_once_delta_reask_direct` carves only the overlapping pages from `journal().ahead`, runs a single coalesced pass over `uncovered ∪ reask`, and merges back into `journal().ahead` without discarding unaffected spans. - **Fail-closed fallback**: if flights were waited for, `settle_gen` advanced, `WRITE_LOG` overflowed (`> 256` entries), or an unsized free (`begin_write_epoch()`) left an unlogged gap, `finish_early_watch` safely falls back to a full `ask_once(&batch.ranges)`. ### Process-Wide Kernel Serialization (`watch.rs`) - `watch::ask_once` and `Tracked::take` delegate through `watch_pipe::{ask_once, take_tracked}` when `VICTORY_SUBMIT_PIPE=1`, and execute `ask_once_direct` / `take_direct` inline when off. - `watch::KERNEL_WATCH_LOCK` wraps `GetWriteWatch(WRITE_WATCH_FLAG_RESET)` inside `kernel_written` **only when `watch_pipe::on()`**, serialising any residual helper-thread queries (`texture::Staging::refresh_now`, `shadow.rs`, `memory.rs`, `ask_early`) strictly after `ASKING`. --- ## Opt-In Switches | Env Var | Default | Behaviour | | --- | :---: | --- | | `VICTORY_SUBMIT_PIPE` | off (`0` / unset) | `1` enables `victory-watch-pipe`, `KERNEL_WATCH_LOCK`, overlapped `early_watch`, and `SeqCst` write-epoch tracking (via `asked_for!`, compatible with `VICTORY_AB`). | | `VICTORY_WATCH_PIPE_EPOCH` | on when `VICTORY_SUBMIT_PIPE=1` | `0` disables `WatchSnapshot` / incremental `delta_ranges` while keeping single-source collector serialization. Always `false` when `VICTORY_SUBMIT_PIPE` is off. | When `VICTORY_SUBMIT_PIPE` is unset or `"0"`: - No `victory-watch-pipe` thread is spawned (`default_disabled_does_not_spawn_collector`). - `KERNEL_WATCH_LOCK`, `record_write_epoch`, `bump_settle_gen`, and `log_line` are completely bypassed. - `apps/victory/src/play.rs` defaults are untouched; `Host::submit` executes the exact upstream `e1251c31` path (including `VICTORY_ASK_FIRST=1`). --- ## Correctness, Deadlock Safety & Compatibility Audit (`e1251c31`) 1. **Lock hierarchy (zero AB-BA cycles)**: - `Host::submitted` ≺ `WatchPipe::state` / `Completion` ≺ `ASKING` ≺ `KERNEL_WATCH_LOCK` / `JOURNAL` / `GUARDS`. - `SHADER_HEAPS.read()` (held by `descriptor::sync_heaps` around `Tracked::take`) ≺ `WatchPipe::state` / `Completion`; the collector thread never acquires `SHADER_HEAPS`, `SYNC_ROUNDS`, `Host::submitted`, or `title_memory_reading`. - `WRITE_LOG` is a strict leaf mutex; `elsewhere` / `elsewhere_in` (`inplace::VIEWS`) is resolved before acquiring `WRITE_LOG`. - `rangelock::RangeLock` (`VICTORY_RANGE_LOCK=1`) / `MAPPING`: no caller holds `title_memory_reading*` while waiting on `WatchPipe`. 2. **Compatibility with `descriptor.rs` (`VICTORY_HEAP_SYNC_AT_SUBMIT=1`, `VICTORY_HEAP_SYNC_FIRST=1`, `VICTORY_SYNC_FINE=1`)**: - All `sync_heaps` watch queries (`written.take(...)`, including `heap_hooks::reguard`) route through `Tracked::take` → `watch_pipe::take_tracked` with zero changes to `descriptor.rs`. - When `waited_for_flights` or `settled_for_sweep` occurs in `Host::submit`, `early_watch.cancel()` awaits any in-flight collector pass and calls `ask_once_end()` **before** `begin_sync_heaps()` starts a fresh round, closing any window where `victory-heap-sync` could observe a stale pre-settle `journal().ahead`. 3. **Compatibility with PR #3762 (`30911195`) & `WRITTEN_EPOCHS`**: - Both `ask_once_direct` and `ask_once_delta_reask_direct` maintain `Journal::note` (mirroring into lock-free `WRITTEN_EPOCHS`), strictly sorted non-touching `j.ahead` (`merged(...)`), and atomic `KEPT` (`is_kept()`) before `Batch::refresh_front` / `Batch::flush_all` run. - When `VICTORY_SUBMIT_PIPE=1`, `pipe_early` supersedes `VICTORY_ASK_FIRST=1` so `victory-ask` and `victory-watch-pipe` never race. 4. **No missed pages (`SeqCst` post-write invariant)**: - Every write/remap path records its epoch **after** bytes land in title memory (gated by `watch_pipe::on()`): `landed_direct`, `landed_beside`, `shadow::put_changed` (RAII post-write guard), `KernelWrite::drop` (DirectStorage / `ReadFile` / `fread`), `stream_written`, `title_memory_written`, `wrote`, `writing`, `title_memory_remapped`, `title_memory_unmapping`, `title_memory_allocated`, and `title_memory_freed` (unsized `len == 0` triggers bare `begin_write_epoch()` gap $\to$ fail-closed full fallback). - Gap detection in `record_write_epoch` (`ep != log.last_ep + 1` advances `min_lost_ep`) ensures later logged writes can never mask an earlier unlogged `begin_write_epoch()`. --- ## Files Changed - `crates/victory-gpu/src/host/watch_pipe.rs` (new): single-source collector thread, `WatchTicket` / `WatchSnapshot`, concurrent `SubmitAsk` coalescing, `delta_ranges`, exact sub-range intersection, `finish_early_watch`, diagnostics line, and 11 unit tests. - `crates/victory-gpu/src/watch.rs`: `WRITING_EPOCH` / `WRITE_LOG` with gap detection, `KERNEL_WATCH_LOCK` (gated on `watch_pipe::on()`), `ask_once_direct`, `ask_once_delta_reask_direct`, `Tracked::take_direct`, gated epoch hooks on write/remap/free entrypoints, and 2 unit tests. - `crates/victory-gpu/src/host.rs`: `mod watch_pipe`, overlapped `early_watch` start/cancel/finish in `Host::submit`, `bump_settle_gen` in `settle` / `settle_stale` / `WHY_READ` `relay_wait` / `Batch::title_written`, and `watch_pipe::log_line`. - `crates/victory-gpu/src/shadow.rs`: post-write RAII epoch guard in `put_changed` (gated on `watch_pipe::on()`). - `crates/victory-gpu/src/queue/behind.rs`: `watch_pipe::ensure_started()` on `victory-submit` startup (no-op when off). --- ## Verification & CI Gates Executed against `--locked`: - **Targeted PR 2 unit tests (13 tests)** — `PASS (13 passed, 0 failed)` in both default (`VICTORY_SUBMIT_PIPE` unset) and `VICTORY_SUBMIT_PIPE=1` modes: - `default_disabled_does_not_spawn_collector` - `collector_serializes_concurrent_callers_on_single_thread` - `reentrant_run_on_collector_does_not_deadlock` - `collector_survives_task_panic_and_propagates_to_caller` - `delta_ranges_subtracts_covered_and_merges_overlaps` - `intersecting_needed_returns_exact_overlapping_subranges` - `fifo_epoch_snapshot_and_settle_gen_invalidation` - `concurrent_submit_asks_coalesce_without_losing_dirty_pages` - `ask_once_delta_runs_on_collector_and_preserves_ahead` - `finish_early_watch_fast_path_reasks_overlap_and_handles_settle_or_cancel` - `concurrent_submit_and_heap_tracked_serialize_without_deadlock_or_missed_pages` - `write_log_tracks_ranges_gaps_and_overflow_falls_back_to_none` - `ask_once_delta_reask_direct_carves_refreshes_and_updates_page_epochs` - **`victory-gpu` full suite with `VICTORY_SUBMIT_PIPE=1`** + `victory-hle` `a_read_by_range_waits_only_for_a_remapping_that_meets_it` — all PR-related tests pass; zero regressions introduced. - **`cargo clippy --workspace --all-targets --locked` & `--all-features`** — **0 warnings** in all touched/added files (`watch_pipe.rs`, `watch.rs`, `host.rs` new hunks, `shadow.rs` new hunk, `queue/behind.rs`). *(Note: upstream base `e1251c31` has pre-existing unrelated warnings in `victory-shader/src/emit/lower.rs:114`, `copystat.rs:1470`, `shadow.rs:7206`, `host.rs:5092`, `xmem.rs:1933`, pre-existing test failures in `list::thin::tests::bindings_record_no_work`, `shader_cache::tests::every_switch_of_the_translator_is_in_the_key`, `split_lds_wave32_read_after_read_barrier_coalescing_and_parity`, and a pre-existing `xtask policy` finding in `crates/victory-shader/src/native.rs`.)* - **`cargo xtask policy`** — **0 violations** on all added/modified files. - **Release build**: `cargo build --release --locked -p victory` — `PASS`. --- ## Local Benchmark Results (Same-Binary Off vs On) Measured on the same Release build (`pr/watch-pipe` on `e1251c31`), RTX 4090 + Ryzen 9 9950X, Windows 11, 4K DLSS Quality + FrameGen 2x, identical settings (including `VICTORY_PLACED_COPIED=0`), over two 13–14 minute free-play sessions: - `VICTORY_SUBMIT_PIPE=0` (Off): 764 s steady / 17,483 frames, **0 errors** - `VICTORY_SUBMIT_PIPE=1` (On): 720 s steady / 16,181 frames, **0 errors** Excluding 1-second buckets that contain first-run shader/PSO compilations (`PIPE=1` ran first and warmed the DXCache for `PIPE=0`: `PIPE=0` 600 clean s / 13,959 frames vs `PIPE=1` 466 clean s / 11,045 frames; comparable workload `refreshes` 8,759 vs 8,564, `heap_pages` 482.1 vs 472.6): | Metric (per frame, clean steady seconds) | `VICTORY_SUBMIT_PIPE=0` (mean / p90) | `VICTORY_SUBMIT_PIPE=1` (mean / p90) | Delta | | --- | :---: | :---: | :---: | | `Host::submit`: asking the write watch (`sub_ask_ms`) | 3.02 / 3.40 ms | **2.69 / 3.00 ms** | **-10.9% / p90 -11.8%** | | Pure write-watch wait (`sub_ask_ms - find_ask_ms`) | 2.58 / 2.90 ms | **2.16 / 2.50 ms** | **-16.3% / p90 -13.8%** | | `descriptor::sync_heaps`: asking (`heap_ask_ms`) | 2.59 / 3.37 ms | 2.99 / 3.75 ms | +0.40 ms (includes `normal`-queue yield behind `high` submit asks) | | `descriptor::sync_heaps`: read & compare (`heap_cmp_ms`) | 2.72 / 3.67 ms | **2.19 / 3.05 ms** | **-19.6% / p90 -16.9%** (less cross-core TLB shootdown contention) | | `descriptor::sync_heaps`: total (`heap_ms`) | 7.18 / 9.05 ms | **6.67 / 8.18 ms** | **-7.1% / p90 -9.5%** | | `Host::submit`: wait for heap sync (`at_sub_wait`) | 0.26 / 0.45 ms | **0.19 / 0.33 ms** | **-24.7% / p90 -26.7%** | | `Host::submit`: total preparing (`prep_ms`) | 28.76 / 34.70 ms | **27.29 / 33.25 ms** | **-5.1% / p90 -4.2%** | | Per-second median frame time (`ft_med`) / `>50 ms` share | 44.41 ms / 16.05% | **43.14 ms / 13.64%** | **-2.9%** / fewer >50 ms frames | Steady-state `VICTORY_SUBMIT_PIPE=1` collector line averages (all 720 steady seconds): `22.10` submit asks/frame, `19.31` heap-sync takes/frame; `early-watch` `12.81` fast hits + `4.56` delta/reask vs `0.08` full fallbacks (**>99.5% incremental/fast-path hit rate**). *(Note: these are two full free-play runs rather than fixed-camera `VICTORY_AB` sampling. For strict fixed-camera A/B reproduction on diagnostics builds, run `VICTORY_AB=20,VICTORY_SUBMIT_PIPE`.)* Recommended extra matrix checks: - `VICTORY_SUBMIT_PIPE=1 VICTORY_HEAP_SYNC_AT_SUBMIT=1 VICTORY_SYNC_FINE=1` - `VICTORY_SUBMIT_PIPE=1 VICTORY_HEAP_SYNC_FIRST=1 VICTORY_RANGE_LOCK=1` - `VICTORY_SUBMIT_PIPE=1 VICTORY_SUBMIT_SPLIT=1` - Diagnostics live A/B: `VICTORY_AB=20,VICTORY_SUBMIT_PIPE`
Introduce a self-contained single-source collector thread (`victory-watch-pipe`)
behind the opt-in `VICTORY_SUBMIT_PIPE=1` switch (off by default) to serialize
`GetWriteWatch(WRITE_WATCH_FLAG_RESET)` queries across `Host::submit` and
`descriptor::sync_heaps` (`victory-heap-sync`):

- Add `crates/victory-gpu/src/host/watch_pipe.rs` with two-tier priority scheduling
  (`high` submit asks, `normal` heap-sync takes), concurrent `SubmitAsk` coalescing,
  `WatchSnapshot` epoch isolation, exact `delta_ranges` / sub-range re-ask
  intersection, panic isolation, and 11 unit tests.
- Add `WRITING_EPOCH` / `WRITE_LOG` with gap detection and `KERNEL_WATCH_LOCK`
  (all gated by `watch_pipe::on()`), plus `ask_once_delta_reask_direct` preserving
  `WRITTEN_EPOCHS`, `KEPT`, and sorted `journal().ahead`.
- Overlap `early_watch` with `card_steps` / `sweep` in `Host::submit` when enabled,
  leaving the default (`VICTORY_SUBMIT_PIPE` unset / 0) path unchanged.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-30 21:16:40 +00:00
Author
Owner

@⁠IRennie74 on GitHub · 2026-11-30 22:38 UTC · original

Some notes relevant to 6-core CPUs.

  • On a Ryzen 5 5600X + RTX 3060 (Series S, DLSS Performance), the submitting thread is busy 44-54 ms of a 55-58 ms frame, and asking the write watch is 3-4 ms of it. Moving that off the critical path is worth measuring there. The CPU is also close to saturation, though: the title takes about 3 cores, list threads 1.7, and ours the rest. In our runs, VICTORY_SUBMIT_FRAMES=2 and SUBMIT_SPLIT gave nothing, because the work moved off the submitting thread came back as contention. So a 9950X result may not carry over. Your table already shows sync_heaps asking +0.4 ms. I can run VICTORY_AB=…,VICTORY_SUBMIT_PIPE on that PC.
  • record_write_epoch runs on every wrote / title_memory_written / _remapped / _unmapping / landing while the switch is on (watch.rs, the hooks listed in the description), including when no early ticket is in flight. Each call takes elsewhere() (the in-place views' lock) and then WRITE_LOG. Some of those callers are title threads during remaps. Logging only between begin_ask_once and finish_early_watch (an atomic count of tickets in flight) would remove that cost for the rest of the frame.
  • wrote() used to return immediately when no asking was kept. With the switch on, it now always logs first.
  • Interaction with #4396: both rewrite ask_once. ask_once_delta_reask_direct copies the old merge loop, so with VICTORY_ASK_BY_RES on it would still make merged askings across reservations. Whichever lands second has to route it through the same splitting.
  • Interaction with #4559 (the once-a-second sweep on its own thread): with VICTORY_SWEEP_OFF_THREAD=1, the submitting thread no longer settles for the sweep, so the settled_for_sweep cancellation never fires. The overlap "with sweep" from the description also disappears, which leaves only card_steps and the planning to hide the asking behind. The two should combine cleanly, but the gain from the overlap will be smaller than with the sweep inline.

The CI failure is main's (lower.rs:114, shadow.rs ranged, host.rs:5092), not this PR's.

**@⁠IRennie74** on GitHub · 2026-11-30 22:38 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4489#issuecomment-7294121727) Some notes relevant to 6-core CPUs. - On a Ryzen 5 5600X + RTX 3060 (Series S, DLSS Performance), the submitting thread is busy 44-54 ms of a 55-58 ms frame, and asking the write watch is 3-4 ms of it. Moving that off the critical path is worth measuring there. The CPU is also close to saturation, though: the title takes about 3 cores, list threads 1.7, and ours the rest. In our runs, `VICTORY_SUBMIT_FRAMES=2` and `SUBMIT_SPLIT` gave nothing, because the work moved off the submitting thread came back as contention. So a 9950X result may not carry over. Your table already shows `sync_heaps` asking +0.4 ms. I can run `VICTORY_AB=…,VICTORY_SUBMIT_PIPE` on that PC. - `record_write_epoch` runs on every `wrote` / `title_memory_written` / `_remapped` / `_unmapping` / landing while the switch is on (`watch.rs`, the hooks listed in the description), including when no early ticket is in flight. Each call takes `elsewhere()` (the in-place views' lock) and then `WRITE_LOG`. Some of those callers are title threads during remaps. Logging only between `begin_ask_once` and `finish_early_watch` (an atomic count of tickets in flight) would remove that cost for the rest of the frame. - `wrote()` used to return immediately when no asking was kept. With the switch on, it now always logs first. - Interaction with #4396: both rewrite `ask_once`. `ask_once_delta_reask_direct` copies the old merge loop, so with `VICTORY_ASK_BY_RES` on it would still make merged askings across reservations. Whichever lands second has to route it through the same splitting. - Interaction with #4559 (the once-a-second sweep on its own thread): with `VICTORY_SWEEP_OFF_THREAD=1`, the submitting thread no longer settles for the sweep, so the `settled_for_sweep` cancellation never fires. The overlap "with `sweep`" from the description also disappears, which leaves only `card_steps` and the planning to hide the asking behind. The two should combine cleanly, but the gain from the overlap will be smaller than with the sweep inline. The CI failure is main's (`lower.rs:114`, `shadow.rs` `ranged`, `host.rs:5092`), not this PR's.
github-bot force-pushed github-bot/gh-4489 from 5c0495331c to b31e5e386a 2026-12-01 11:53:35 +00:00 Compare
Author
Owner

@⁠lyufeng000 on GitHub · 2026-12-01 12:33 UTC · original

Hi @⁠IRennie74,

Thank you so much for taking the time to review this PR, look into the implementation details, and offer to test it on your setup.

I'm really sorry for not properly considering lower-core-count / more CPU-constrained systems—this was developed and tuned on a 9950X, and I overlooked that moving work onto an extra collector thread and adding lock overhead can cause contention and backfire when the CPU is already near saturation.

To be honest, I'm feeling a bit burnt out from coding lately, so I don't have any plans for further optimizations at the moment. Thanks again for the thoughtful feedback!

**@⁠lyufeng000** on GitHub · 2026-12-01 12:33 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4489#issuecomment-7311320344) Hi @⁠IRennie74, Thank you so much for taking the time to review this PR, look into the implementation details, and offer to test it on your setup. I'm really sorry for not properly considering lower-core-count / more CPU-constrained systems—this was developed and tuned on a 9950X, and I overlooked that moving work onto an extra collector thread and adding lock overhead can cause contention and backfire when the CPU is already near saturation. To be honest, I'm feeling a bit burnt out from coding lately, so I don't have any plans for further optimizations at the moment. Thanks again for the thoughtful feedback!
github-bot closed this pull request 2026-12-01 18:43:25 +00:00
Author
Owner

@⁠trustno1-coder on GitHub · 2026-12-01 18:39 UTC · original

Closing this as part of moving main to the texture and video-memory rework (v2026.12.01.01-test). A lot of the code changed, and this pull request no longer applies cleanly to it; it would need to be rewritten against the new main rather than rebased. Thank you for the work. If the change is still needed, please open a new pull request based on the current main.

**@⁠trustno1-coder** on GitHub · 2026-12-01 18:39 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4489#issuecomment-7321301884) Closing this as part of moving main to the texture and video-memory rework (v2026.12.01.01-test). A lot of the code changed, and this pull request no longer applies cleanly to it; it would need to be rewritten against the new main rather than rebased. Thank you for the work. If the change is still needed, please open a new pull request based on the current main.

Pull request closed

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/trustno1-coder--VIctory-NVIDIA!4490
No description provided.