GPU: atomic adaptive PSO wait budget and non-blocking pipeline library saves #275

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

@⁠wbkepler on GitHub · 2026-11-24 01:58 UTC · original
wbkepler:gpu/pso-hitch-and-library-save → main · +629 −86 · 2 files · 2 commits · open

Summary

Eliminates mid-gameplay 500 ms PSO wait freezes and periodic 20-second pipeline-worker stalls while raising early-warmup capacity:

  • pipeline/make.rs:
    • Replaces non-atomic FRAME/WAITED_US accounting with a lock-free packed AtomicU64 FrameBudget (reserve + same-frame refund) so concurrent recording threads cannot collectively overshoot the frame wait budget or corrupt newer frames.
    • Moves from a single global Condvar to per-Pending wait_lock/wait_done wakeups.
    • Uses an adaptive default budget (500 ms for warmup frames < 60, 8 ms in steady state) with an explicit VICTORY_PSO_WAIT_MS (0..=5000 ms) override.
  • pipeline/library.rs:
    • Offloads periodic save(false) onto a dedicated low-priority background saver thread reusing its serialization buffer (pre-sized outside library.lock()).
    • Holds library.lock() strictly for ID3D12PipelineLibrary1::Serialize, skips rewriting host-pipelines.bin when only early-pipelines.txt changed, and performs unique-temp atomic write+rename with cleanup on error outside all locks (restoring dirty counters on failure).
    • Keeps shutdown save(true) synchronous via FIFO SaveAck, and raises EARLY_MAX from 8,192 to 65,536.
  • Adds 4 unit tests in victory-gpu covering concurrent 8-thread budget reservation/refund/frame isolation, VICTORY_PSO_WAIT_MS parsing/clamping and adaptive defaults, early-list recording/dedup up to 65,536, and atomic-write overwrite + temp-file cleanup on rename error.

Verification

  • cargo clippy --workspace --all-targets — passed (zero new warnings)
  • cargo clippy -p victory --features diagnostics --all-targets — passed
  • cargo test --workspace — passed
  • cargo xtask policy — passed (151 files ok)
**@⁠wbkepler** on GitHub · 2026-11-24 01:58 UTC · [original](https://github.com/Marvisak/VIctory/pull/275) `wbkepler:gpu/pso-hitch-and-library-save` → `main` · +629 −86 · 2 files · 2 commits · open ## Summary Eliminates mid-gameplay 500 ms PSO wait freezes and periodic 20-second pipeline-worker stalls while raising early-warmup capacity: - **`pipeline/make.rs`**: - Replaces non-atomic `FRAME`/`WAITED_US` accounting with a lock-free packed `AtomicU64` `FrameBudget` (reserve + same-frame refund) so concurrent recording threads cannot collectively overshoot the frame wait budget or corrupt newer frames. - Moves from a single global `Condvar` to per-`Pending` `wait_lock`/`wait_done` wakeups. - Uses an adaptive default budget (500 ms for warmup frames `< 60`, 8 ms in steady state) with an explicit `VICTORY_PSO_WAIT_MS` (`0..=5000` ms) override. - **`pipeline/library.rs`**: - Offloads periodic `save(false)` onto a dedicated low-priority background saver thread reusing its serialization buffer (pre-sized outside `library.lock()`). - Holds `library.lock()` strictly for `ID3D12PipelineLibrary1::Serialize`, skips rewriting `host-pipelines.bin` when only `early-pipelines.txt` changed, and performs unique-temp atomic write+rename with cleanup on error outside all locks (restoring dirty counters on failure). - Keeps shutdown `save(true)` synchronous via FIFO `SaveAck`, and raises `EARLY_MAX` from `8,192` to `65,536`. - Adds 4 unit tests in `victory-gpu` covering concurrent 8-thread budget reservation/refund/frame isolation, `VICTORY_PSO_WAIT_MS` parsing/clamping and adaptive defaults, early-list recording/dedup up to 65,536, and atomic-write overwrite + temp-file cleanup on rename error. ## Verification - `cargo clippy --workspace --all-targets` — passed (zero new warnings) - `cargo clippy -p victory --features diagnostics --all-targets` — passed - `cargo test --workspace` — passed - `cargo xtask policy` — passed (151 files ok)
Eliminate mid-gameplay 500 ms PSO wait freezes and periodic 20-second
pipeline-worker stalls while raising early-warmup capacity:

- pipeline/make: replace non-atomic FRAME/WAITED_US accounting with a
  lock-free packed AtomicU64 FrameBudget (reserve + same-frame refund) so
  concurrent recording threads cannot overshoot the frame wait budget or
  corrupt a subsequent frame's budget; move from a single global Condvar
  to per-Pending wakeups so unrelated background compiles do not wake
  waiting threads.
- pipeline/make: use an adaptive default budget (500 ms during the first
  60 warmup frames for boot/menu/one-shot init passes, 8 ms in steady
  state) with an explicit `VICTORY_PSO_WAIT_MS` (`0..=5000`) override.
- pipeline/library: offload periodic saves onto a dedicated low-priority
  background saver thread reusing its serialization buffer (pre-sized
  outside `library.lock()`), hold `library.lock()` only for `Serialize`,
  skip rewriting `host-pipelines.bin` when only the early list changed,
  perform atomic unique-temp write + rename with cleanup on error outside
  all locks, keep `save(true)` synchronous on shutdown, and raise
  `EARLY_MAX` from 8,192 to 65,536.
- tests: add unit tests for concurrent budget reservation/refund/frame
  isolation, `VICTORY_PSO_WAIT_MS` parsing/clamping and adaptive defaults,
  early-list recording/dedup above 8,192 up to 65,536, and atomic-write
  temp-file cleanup on rename failure.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:57:34 +00:00
Author
Owner

@⁠ifBars on GitHub · 2026-11-24 02:09 UTC · original

I compiled a deterministic CPU-only test against the FrameBudget implementation from f7730115. A recording thread can read frame 100, get descheduled, then enter reserve(100, ...) after another thread has already reserved frame 101. Because reserve resets the state on every differing tag, the stale reservation moves the tag back to 100. The next frame-101 caller then gets a second full budget.

This interleaving needs no timing-sensitive test:

let budget = FrameBudget::new();
let limit_us = 8_000;
assert_eq!(budget.reserve(101, limit_us), limit_us);
assert_eq!(
    (budget.reserve(100, limit_us), budget.reserve(101, limit_us)),
    (0, 0),
);

The current implementation returns (8000, 8000). The existing test exercises stale refunds. Could we also test stale reservations and have reserve return zero when the supplied tag precedes the active tag, accounting for the sentinel and wraparound?

**@⁠ifBars** on GitHub · 2026-11-24 02:09 UTC · [original](https://github.com/Marvisak/VIctory/pull/275#issuecomment-7119819722) I compiled a deterministic CPU-only test against the `FrameBudget` implementation from `f7730115`. A recording thread can read frame 100, get descheduled, then enter `reserve(100, ...)` after another thread has already reserved frame 101. Because `reserve` resets the state on every differing tag, the stale reservation moves the tag back to 100. The next frame-101 caller then gets a second full budget. This interleaving needs no timing-sensitive test: ```rust let budget = FrameBudget::new(); let limit_us = 8_000; assert_eq!(budget.reserve(101, limit_us), limit_us); assert_eq!( (budget.reserve(100, limit_us), budget.reserve(101, limit_us)), (0, 0), ); ``` The current implementation returns `(8000, 8000)`. The existing test exercises stale refunds. Could we also test stale reservations and have `reserve` return zero when the supplied tag precedes the active tag, accounting for the sentinel and wraparound?
Author
Owner

@⁠wbkepler on GitHub · 2026-11-24 02:16 UTC · original

@⁠ifBars Good catch — thanks for the deterministic repro!

Fixed in 96b2822:

  • FrameBudget::frame_tag now uses frame % NO_FRAME_TAG (keeping tags strictly in 0..NO_FRAME_TAG without colliding at NO_FRAME_TAG), and FrameBudget::reserve checks is_newer_tag(tag, cur_tag) via modular half-range distance (0 < diff < NO_FRAME_TAG / 2) before resetting the budget.
  • Stale tags preceding cur_tag (including across the NO_FRAME_TAG wraparound boundary) now return 0 without mutating the active frame's packed state.
  • Added stale_reservation_never_rewinds_active_frame_or_wraparound covering the reserve(101) -> reserve(100) / reserve(101) interleaving, partial-budget preservation, and NO_FRAME_TAG wraparound (cargo test -p victory-gpu --lib: 48 passed / 1 ignored, cargo clippy -p victory-gpu --all-targets, cargo xtask policy all passing).
**@⁠wbkepler** on GitHub · 2026-11-24 02:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/275#issuecomment-7119947275) @⁠ifBars Good catch — thanks for the deterministic repro! Fixed in `96b2822`: - `FrameBudget::frame_tag` now uses `frame % NO_FRAME_TAG` (keeping tags strictly in `0..NO_FRAME_TAG` without colliding at `NO_FRAME_TAG`), and `FrameBudget::reserve` checks `is_newer_tag(tag, cur_tag)` via modular half-range distance (`0 < diff < NO_FRAME_TAG / 2`) before resetting the budget. - Stale tags preceding `cur_tag` (including across the `NO_FRAME_TAG` wraparound boundary) now return `0` without mutating the active frame's packed state. - Added `stale_reservation_never_rewinds_active_frame_or_wraparound` covering the `reserve(101) -> reserve(100) / reserve(101)` interleaving, partial-budget preservation, and `NO_FRAME_TAG` wraparound (`cargo test -p victory-gpu --lib`: 48 passed / 1 ignored, `cargo clippy -p victory-gpu --all-targets`, `cargo xtask policy` all passing).
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:25 UTC · original

Automated triage for the maintainers.

Needs rebase. This PR does not merge cleanly into current main (ac2525d). Conflicting files: crates/victory-gpu/src/pipeline/library.rs, crates/victory-gpu/src/pipeline/make.rs.

The content has not been reviewed yet; it will be once the branch is rebased onto main.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 00:25 UTC · [original](https://github.com/Marvisak/VIctory/pull/275#issuecomment-7148490415) <!-- victory-triage v1 head=96b282274c7a34900436f859989d1f70c57ed5f0 verdict=needs-rebase at=2026-11-25T00:13:20Z --> Automated triage for the maintainers. **Needs rebase.** This PR does not merge cleanly into current `main` (ac2525d). Conflicting files: `crates/victory-gpu/src/pipeline/library.rs`, `crates/victory-gpu/src/pipeline/make.rs`. The content has not been reviewed yet; it will be once the branch is rebased onto `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠r4tedx on GitHub · 2026-11-25 07:29 UTC · original

A pointer and one observation, since this PR and #2111 change the same accounting in pipeline/make.rs.

What overlaps. On main, wait reads the frame's waited time before it waits and adds its own only afterwards, so recording threads that wait at the same moment each take the whole budget. Both PRs fix that. #2111 does only that (the waits of a frame end together, and the frame line prints the time draws waited for pipelines); it is on main 940d32f and merges cleanly. Its synthetic test, four threads on pipelines that are never made: the frame stands still 882 ms on main and 505 ms with the fix.

What is only here. The adaptive budget (500 ms for the first 60 frames, 8 ms after), VICTORY_PSO_WAIT_MS, a wake per pipeline instead of one for all, and the non-blocking library saves. This PR came first (24 Nov). It is on d2a4bae and conflicts with main. For the library half, #2129 now maps the file and stages the save.

One behavioural difference, from reading 96b2822. wait reserves all the budget that is left before it waits, and returns at once when it is granted nothing. So when two recording threads need a pipeline at the same moment, the first holds the whole budget and the second drops its draw immediately, even if its pipeline would be there a millisecond later; it can wait only after the first has refunded. In #2111 both wait until the frame's shared budget ends. I have not measured whether that matters in the title, and neither PR has a title run.

Suggestion. After a rebase, the parts unique to this PR sit on top of either accounting, so the two need not compete: this could keep the adaptive budget, the switch and the per-pipeline wake, and take the accounting from whichever the maintainers prefer. If they prefer this PR's accounting, #2111 can close. Either way a test with two threads waiting at once would pin the case above.

Nothing was run for this comment; I read the diff.

AI-assisted (Claude Code).

**@⁠r4tedx** on GitHub · 2026-11-25 07:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/275#issuecomment-7155918977) A pointer and one observation, since this PR and #2111 change the same accounting in `pipeline/make.rs`. **What overlaps.** On `main`, `wait` reads the frame's waited time before it waits and adds its own only afterwards, so recording threads that wait at the same moment each take the whole budget. Both PRs fix that. #2111 does only that (the waits of a frame end together, and the frame line prints the time draws waited for pipelines); it is on `main` `940d32f` and merges cleanly. Its synthetic test, four threads on pipelines that are never made: the frame stands still 882 ms on `main` and 505 ms with the fix. **What is only here.** The adaptive budget (500 ms for the first 60 frames, 8 ms after), `VICTORY_PSO_WAIT_MS`, a wake per pipeline instead of one for all, and the non-blocking library saves. This PR came first (24 Nov). It is on `d2a4bae` and conflicts with `main`. For the library half, #2129 now maps the file and stages the save. **One behavioural difference, from reading `96b2822`.** `wait` reserves all the budget that is left before it waits, and returns at once when it is granted nothing. So when two recording threads need a pipeline at the same moment, the first holds the whole budget and the second drops its draw immediately, even if its pipeline would be there a millisecond later; it can wait only after the first has refunded. In #2111 both wait until the frame's shared budget ends. I have not measured whether that matters in the title, and neither PR has a title run. **Suggestion.** After a rebase, the parts unique to this PR sit on top of either accounting, so the two need not compete: this could keep the adaptive budget, the switch and the per-pipeline wake, and take the accounting from whichever the maintainers prefer. If they prefer this PR's accounting, #2111 can close. Either way a test with two threads waiting at once would pin the case above. Nothing was run for this comment; I read the diff. AI-assisted (Claude Code).
This pull request has changes conflicting with the target branch.
  • crates/victory-gpu/src/pipeline/library.rs
  • crates/victory-gpu/src/pipeline/make.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/275/head:github-bot/gh-275'
git switch 'github-bot/gh-275'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-275'
git switch 'github-bot/gh-275'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-275'
git switch 'github-bot/gh-275'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-275'
git switch 'main'
git merge --squash 'github-bot/gh-275'
git switch 'main'
git merge --ff-only 'github-bot/gh-275'
git switch 'main'
git merge 'github-bot/gh-275'
git push origin '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/Marvisak--VIctory!275
No description provided.