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

Open
wbkepler wants to merge 2 commits from upstream-pr/275 into main
wbkepler commented 2026-11-24 01:58:50 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by wbkepler on 2026-11-24T01:58:50Z.


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)
<!-- greyfinch-sync:Marvisak/VIctory:issue:5464629237 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/275). Originally posted by [wbkepler](https://github.com/wbkepler) on 2026-11-24T01:58:50Z. --- ## 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)
ifBars commented 2026-11-24 06:55:08 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by ifBars on 2026-11-24T02:09:54Z.


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?

<!-- greyfinch-sync:Marvisak/VIctory:comment:7119819722 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/275#issuecomment-7119819722). Originally posted by [ifBars](https://github.com/ifBars) on 2026-11-24T02:09:54Z. --- 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?
wbkepler commented 2026-11-24 06:55:09 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by wbkepler on 2026-11-24T02:16:13Z.


@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).
<!-- greyfinch-sync:Marvisak/VIctory:comment:7119947275 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/275#issuecomment-7119947275). Originally posted by [wbkepler](https://github.com/wbkepler) on 2026-11-24T02:16:13Z. --- @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).
Marvisak commented 2026-11-25 01:07:45 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:25:12Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148490415 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/275#issuecomment-7148490415). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:25:12Z. --- <!-- 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)_
r4tedx commented 2026-11-25 08:06:39 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by r4tedx on 2026-11-25T07:29:32Z.


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).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7155918977 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/275#issuecomment-7155918977). Originally posted by [r4tedx](https://github.com/r4tedx) on 2026-11-25T07:29:32Z. --- 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).
andrewpozdnakov7-jpg commented 2026-11-28 04:55:11 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-28T04:55:11Z.


I checked the 65,536-key warmup limit at 96b2822. When the previous file is already full, the current membership set rejects every unseen key. With a generated full list and 256 new uses, it retained 0 of 256.

9d989f5 applies the #3668 ordering to the save snapshot on top of this exact head. It keeps all 256 new keys at the front, fills the remaining slots with earlier keys in their saved order and keeps the 65,536 limit. The nine pipeline library and worker tests passed, including the capacity and save-file tests. Strict library Clippy and the asset policy passed. All-target Clippy flags a missing safety comment in the base's adapter test in host.rs:267.

wbkepler can use the commit when rebasing the warmup and background-save changes.

Please retain the Git authors on cherry-pick or rebase, or add Co-authored-by: andrewpozdnakov7-jpg <[email protected]> if folding this change into another commit.

AI-assisted with OpenAI Codex.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7234340891 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/275#issuecomment-7234340891). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-28T04:55:11Z. --- I checked the 65,536-key warmup limit at `96b2822`. When the previous file is already full, the current membership set rejects every unseen key. With a generated full list and 256 new uses, it retained 0 of 256. [9d989f5](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/9d989f5fde7ed2cec4e83115906f08e9b859b116) applies the #3668 ordering to the save snapshot on top of this exact head. It keeps all 256 new keys at the front, fills the remaining slots with earlier keys in their saved order and keeps the 65,536 limit. The nine pipeline library and worker tests passed, including the capacity and save-file tests. Strict library Clippy and the asset policy passed. All-target Clippy flags a missing safety comment in the base's adapter test in `host.rs:267`. wbkepler can use the commit when rebasing the warmup and background-save changes. Please retain the Git authors on cherry-pick or rebase, or add `Co-authored-by: andrewpozdnakov7-jpg <[email protected]>` if folding this change into another commit. AI-assisted with OpenAI Codex.
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/275:upstream-pr/275'
git switch 'upstream-pr/275'

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/275'
git switch 'upstream-pr/275'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/275'
git switch 'upstream-pr/275'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/275'
git switch 'main'
git merge --squash 'upstream-pr/275'
git switch 'main'
git merge --ff-only 'upstream-pr/275'
git switch 'main'
git merge 'upstream-pr/275'
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
Marvisak/VIctory!275
No description provided.