pool: use a 250 ms quarantine for STAGING pieces #279

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

@⁠deyvideguilherme on GitHub · 2026-12-03 01:43 UTC · original
deyvideguilherme:fix/staging-rest-ms → main · +136 −2 · 1 files · 1 commits · closed without merging

Summary

Separates the post-return quarantine duration for pool::STAGING (STAGING_REST = 250 ms) from pool::TEXTURES (REST = 5000 ms) in crates/victory-gpu/src/pool.rs.

Problem

Pool::give() previously quarantined returned upload-heap slices in STAGING for the same REST = 5000 ms used by placed VRAM textures in TEXTURES. During sustained texture streaming bursts on a 16 GiB RAM host, holding already-completed staging slices in resting for 5 seconds prevented slice reuse and overflow block reclamation. In baseline testing, pool::STAGING grew up to 1280–1920 MiB (20–30 × 64 MiB blocks in GTT/system RAM), contributing to elevated GTT occupancy and lowering available system RAM below 1000 MiB during peak streaming.

Rationale

TEXTURES and STAGING have different post-return lifetimes:

  • TEXTURES (REST = 5000 ms, unchanged): placed VRAM heaps may still be referenced indirectly by bindless descriptors or pending submissions, so keeping the 5000 ms quarantine preserves existing behavior.
  • STAGING (STAGING_REST = 250 ms): upload-heap slices are only used as copy sources and are dropped after their upload copies have completed. Parameterizing Pool::with_rest(most, rest_ms) keeps TEXTURES at 5000 ms while allowing STAGING blocks to be reused and excess blocks above STAGING_KEPT to be reclaimed sooner.

Synchronization Audit

StagingPiece is owned exclusively by Staging._piece (crates/victory-gpu/src/texture.rs) and is returned to STAGING only in Drop for StagingPiece:

  • Graphics queue path (HostList::defer_texture): HostList::deferred().copies retains a strong Arc<Staging> until HostList::reset() runs after the corresponding flight fence completes.
  • Copy queue path (texture::ahead): each Job retains a strong Arc<Staging> and waits for fence completion via SetEventOnCompletion before finish(jobs) drops the job batch.
  • Global staging map (texture::let_go_idle): only evicts entries when Arc::strong_count == 1.
  • Empty 64 MiB upload blocks: blocks released by reclaim() continue to go through crate::host::retire(block) before destruction.

Because Drop for StagingPiece runs after fence completion on both upload paths, REST = 5000 ms is not the primary synchronization mechanism for upload copies; STAGING_REST = 250 ms retains a conservative delay before slice reuse and block reclaim.

Verification

Unit Tests & Build

  • pool::tests: 11 passed; 0 failed, including two new deterministic tests covering STAGING_REST vs REST boundary behavior, overflow reclaim, and multi-threaded non-overlapping borrow/return.
  • Release build (./build.sh): completed with 0 warnings and 0 errors.

A/B Gameplay Sessions

All three sessions were recorded on the same host and configuration (main baseline fc79d68 vs two separate confirmation sessions with STAGING_REST = 250 ms). All runs exited cleanly with exit code 0 and 0 runtime ERROR log entries.

Metric Baseline (fc79d68, REST = 5000 ms) Session 1 (STAGING_REST = 250 ms) Session 2 (STAGING_REST = 250 ms)
Duration total / active (VRAM > 4 GiB) 57.3 min / 38.2 min 11.6 min / 11.1 min 6.9 min / 6.4 min
pool::STAGING p50 / p90 / p99 / max (MiB) 704 / 1280 / 1728 / 1920 320 / 384 / 384 / 512 320 / 320 / 479 / 512
GTT used (active window) p50 / p90 / p99 / max (MiB) 7802 / 8351 / 8782 / 9062 7410 / 7553 / 7658 / 7719 7354 / 7519 / 7616 / 7663
Available RAM min / p10 / p50 (MiB) 594.6 / 1342.0 / 2763.0 1644.1 / 2068.6 / 2377.0 2348.1 / 2514.9 / 2711.4
Samples with available RAM < 1000 / < 1800 MiB 2.78% / 28.03% 0.00% / 1.36% 0.00% / 0.00%
Copy prep p99 / max (ms) 6.13 / 7.12 4.15 / 5.57 4.49 / 4.59
Ahead uploads completed n/a 340,000 (6947 MiB) 230,000 (5539 MiB)
Post-exit GTT / available RAM (MiB) n/a (capture stopped mid-teardown) 130 / 11880 124 / 11753

Test Environment

  • CPU: AMD Ryzen 7 5700X (8C/16T)
  • GPU: AMD Radeon RX 9060 XT 16 GiB (RDNA 4, ReBAR enabled, Mesa RADV 26.3.1, vkd3d-proton 3.2.0)
  • Memory: 16 GiB RAM (15906 MiB) + 15.5 GiB zram
  • OS / Runtime: CachyOS Linux (7.3.6-3-cachyos), amdgpu.gttsize=12288, Proton Experimental (ntsync)

Limitations & Residual Risks

  • Single hardware/OS profile: validated on one Linux/RADV/vkd3d-proton 16 GiB RAM system; native Windows D3D12 drivers and other GPU architectures were not tested.
  • Session length and manual gameplay variance: the two confirmation runs are shorter than the baseline and follow manual routes, so scene-dependent metrics (such as raw FPS or long-frame rates) are not directly comparable and no FPS improvement is claimed here.
  • Residual fragmentation: during active gameplay pool::STAGING still settles around 320–384 MiB (5–6 × 64 MiB blocks) due to longer-lived referenced staging entries; this change only addresses unnecessary quarantine retention after pieces are returned.
  • Constant choice (250 ms): chosen as a conservative margin below 5000 ms rather than 0 ms; optimal tuning across all drivers and workloads was not explored.
**@⁠deyvideguilherme** on GitHub · 2026-12-03 01:43 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/279) `deyvideguilherme:fix/staging-rest-ms` → `main` · +136 −2 · 1 files · 1 commits · closed without merging ## Summary Separates the post-return quarantine duration for `pool::STAGING` (`STAGING_REST = 250 ms`) from `pool::TEXTURES` (`REST = 5000 ms`) in `crates/victory-gpu/src/pool.rs`. ### Problem `Pool::give()` previously quarantined returned upload-heap slices in `STAGING` for the same `REST = 5000 ms` used by placed VRAM textures in `TEXTURES`. During sustained texture streaming bursts on a 16 GiB RAM host, holding already-completed staging slices in `resting` for 5 seconds prevented slice reuse and overflow block reclamation. In baseline testing, `pool::STAGING` grew up to 1280–1920 MiB (20–30 × 64 MiB blocks in GTT/system RAM), contributing to elevated GTT occupancy and lowering available system RAM below 1000 MiB during peak streaming. ### Rationale `TEXTURES` and `STAGING` have different post-return lifetimes: - **`TEXTURES` (`REST = 5000 ms`, unchanged):** placed VRAM heaps may still be referenced indirectly by bindless descriptors or pending submissions, so keeping the 5000 ms quarantine preserves existing behavior. - **`STAGING` (`STAGING_REST = 250 ms`):** upload-heap slices are only used as copy sources and are dropped after their upload copies have completed. Parameterizing `Pool::with_rest(most, rest_ms)` keeps `TEXTURES` at `5000 ms` while allowing `STAGING` blocks to be reused and excess blocks above `STAGING_KEPT` to be reclaimed sooner. ### Synchronization Audit `StagingPiece` is owned exclusively by `Staging._piece` (`crates/victory-gpu/src/texture.rs`) and is returned to `STAGING` only in `Drop for StagingPiece`: - **Graphics queue path (`HostList::defer_texture`):** `HostList::deferred().copies` retains a strong `Arc<Staging>` until `HostList::reset()` runs after the corresponding flight fence completes. - **Copy queue path (`texture::ahead`):** each `Job` retains a strong `Arc<Staging>` and waits for fence completion via `SetEventOnCompletion` before `finish(jobs)` drops the job batch. - **Global staging map (`texture::let_go_idle`):** only evicts entries when `Arc::strong_count == 1`. - **Empty 64 MiB upload blocks:** blocks released by `reclaim()` continue to go through `crate::host::retire(block)` before destruction. Because `Drop for StagingPiece` runs after fence completion on both upload paths, `REST = 5000 ms` is not the primary synchronization mechanism for upload copies; `STAGING_REST = 250 ms` retains a conservative delay before slice reuse and block reclaim. ### Verification #### Unit Tests & Build - `pool::tests`: `11 passed; 0 failed`, including two new deterministic tests covering `STAGING_REST` vs `REST` boundary behavior, overflow reclaim, and multi-threaded non-overlapping borrow/return. - Release build (`./build.sh`): completed with 0 warnings and 0 errors. #### A/B Gameplay Sessions All three sessions were recorded on the same host and configuration (`main` baseline `fc79d68` vs two separate confirmation sessions with `STAGING_REST = 250 ms`). All runs exited cleanly with exit code 0 and 0 runtime `ERROR` log entries. | Metric | Baseline (`fc79d68`, `REST = 5000 ms`) | Session 1 (`STAGING_REST = 250 ms`) | Session 2 (`STAGING_REST = 250 ms`) | | --- | --- | --- | --- | | Duration total / active (VRAM > 4 GiB) | 57.3 min / 38.2 min | 11.6 min / 11.1 min | 6.9 min / 6.4 min | | `pool::STAGING` p50 / p90 / p99 / max (MiB) | 704 / 1280 / 1728 / 1920 | 320 / 384 / 384 / 512 | 320 / 320 / 479 / 512 | | GTT used (active window) p50 / p90 / p99 / max (MiB) | 7802 / 8351 / 8782 / 9062 | 7410 / 7553 / 7658 / 7719 | 7354 / 7519 / 7616 / 7663 | | Available RAM min / p10 / p50 (MiB) | 594.6 / 1342.0 / 2763.0 | 1644.1 / 2068.6 / 2377.0 | 2348.1 / 2514.9 / 2711.4 | | Samples with available RAM < 1000 / < 1800 MiB | 2.78% / 28.03% | 0.00% / 1.36% | 0.00% / 0.00% | | Copy prep p99 / max (ms) | 6.13 / 7.12 | 4.15 / 5.57 | 4.49 / 4.59 | | Ahead uploads completed | n/a | 340,000 (6947 MiB) | 230,000 (5539 MiB) | | Post-exit GTT / available RAM (MiB) | n/a (capture stopped mid-teardown) | 130 / 11880 | 124 / 11753 | ### Test Environment - **CPU:** AMD Ryzen 7 5700X (8C/16T) - **GPU:** AMD Radeon RX 9060 XT 16 GiB (RDNA 4, ReBAR enabled, Mesa RADV 26.3.1, vkd3d-proton 3.2.0) - **Memory:** 16 GiB RAM (`15906 MiB`) + 15.5 GiB zram - **OS / Runtime:** CachyOS Linux (`7.3.6-3-cachyos`), `amdgpu.gttsize=12288`, Proton Experimental (`ntsync`) ### Limitations & Residual Risks - **Single hardware/OS profile:** validated on one Linux/RADV/vkd3d-proton 16 GiB RAM system; native Windows D3D12 drivers and other GPU architectures were not tested. - **Session length and manual gameplay variance:** the two confirmation runs are shorter than the baseline and follow manual routes, so scene-dependent metrics (such as raw FPS or long-frame rates) are not directly comparable and no FPS improvement is claimed here. - **Residual fragmentation:** during active gameplay `pool::STAGING` still settles around 320–384 MiB (5–6 × 64 MiB blocks) due to longer-lived referenced staging entries; this change only addresses unnecessary quarantine retention after pieces are returned. - **Constant choice (`250 ms`):** chosen as a conservative margin below `5000 ms` rather than `0 ms`; optimal tuning across all drivers and workloads was not explored.
Pool::give quarantined returned STAGING upload-heap pieces for the same REST = 5000 ms used by TEXTURES. Unlike placed VRAM textures, StagingPiece is dropped only after the corresponding upload copy has completed (fence completion on the graphics or copy queue).

Parameterize Pool::with_rest(most, rest_ms) so TEXTURES keeps REST = 5000 ms unchanged while STAGING uses STAGING_REST = 250 ms, allowing upload-heap blocks to be reused and reclaimed sooner during streaming bursts.

Co-Authored-By: Antigravity <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-03 01:47:38 +00:00
Author
Owner

@⁠Kal0s-design on GitHub · 2026-12-03 19:17 UTC · original

Tested efe81ba on Windows 11 with a native MSVC build and toolchain 1.98.1. These are unit tests only and nothing was run in game.

I went through the synchronization audit on the PR head and it matches the code as far as I can see. The piece only lives inside Staging (texture.rs:49, made at texture.rs:2511), so it goes back to the pool when the last Arc is dropped. Uploads deferred with defer_texture (host.rs:5995) stay in deferred().copies until reset() (host.rs:4694), and nothing removes steps from there earlier. Late flights are recycled only once the relay sees GetCompletedValue() reach their fence (host.rs:3124-3125), and the relay polls again after a failed wait instead of landing anything (host.rs:3154-3156). The sync path waits on the event before returning (host.rs:4277-4287). Ahead jobs keep their Arc until finish(jobs) after the wait (texture/ahead.rs:266-269, 287-291). let_go_idle checks strong_count with the table lock held (texture.rs:940-953). Every clone taken out of the table happens under that same lock (texture.rs:2466, 2300), and I found no Weak, so the count can't go up between the check and the removal. forget() and unstage() (texture.rs:1089, 2066) remove from the table unconditionally, but a list that recorded a copy holds its own Arc, so the same argument covers them. Blocks that reclaim() drops go through host::retire (pool.rs:63, 629), which waits for every submission begun so far plus BURY_LAG (host.rs:362-366, 392). A removed device ends the run through fail().

There's one gap, and it isn't new in this PR. In two places a failed fence wait gets logged and the code carries on after the copy may already have been sent. In host.rs:4277-4290, if Signal or SetEventOnCompletion fails it warns "host submission not waited for" and continues. fail() only runs if the device was removed, so otherwise the caller resets the lists and can drop the last Arc while the copy is still running. texture/ahead.rs:266-269 does the same thing. An error from Signal or SetEventOnCompletion after ExecuteCommandLists (line 287) only warns, and then finish(jobs) drops the jobs. Main has the same code (host.rs:4724 there), and it needs a D3D12 call to fail first. The REST comment (pool.rs:76-79) says the 5 s was meant to let in-flight work and deferred lists drain, and those two paths are exactly where the new STAGING_REST comment's assumption that copies have completed doesn't hold. Making them fatal, or keeping the lists until a later wait succeeds, would close it. I don't think it blocks this PR.

cargo test -p victory-gpu --lib pool:: passes 11 of 11 in debug and in release. I ran the two new tests 20 times on the debug binary and 20 times on the release one and all 40 runs passed. Neither test uses sleeps or the wall clock. Time is a now value passed in, or an atomic counter in the threaded test, so Windows timer granularity doesn't affect the 249/250 boundary. One thing about staging_rest_concurrent_borrow_and_return_never_overlaps is that it only checks that live pieces never overlap. I changed its pool to with_rest(STAGING_KEPT, 0) locally and it still passed, so it doesn't cover the quarantine. The other new test does cover the boundary. Both build their own Pool, so the STAGING static at pool.rs:493 is only checked by reading it.

The full run was cargo test -p victory-gpu --lib -- --test-threads=4 with --skip descriptor::tests::views_die_with_their_resource, because that test crashes on main too. The PR head gives 312 passed, 4 failed and 23 ignored. Main at 00aa4d3 gives 338 passed, 5 failed and 23 ignored. The totals differ because the branch is 156 commits behind main. The same four tests fail on both, two indirect::tests::host_list_* and two pipeline::host::tests::*. Main's fifth failure, watch::tests::the_rest_of_the_one_asking_has_a_number_of_its_own, passes on the branch. The branch merges into main cleanly, and pool.rs, texture.rs and texture/ahead.rs haven't changed on main since the branch point.

cargo clippy -p victory-gpu --all-targets shows no warnings in the changed lines. The only warning in pool.rs is too_many_arguments on take_piece, which was already on main.

AI-assisted: yes

**@⁠Kal0s-design** on GitHub · 2026-12-03 19:17 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/279#issuecomment-7389454144) Tested efe81ba on Windows 11 with a native MSVC build and toolchain 1.98.1. These are unit tests only and nothing was run in game. I went through the synchronization audit on the PR head and it matches the code as far as I can see. The piece only lives inside Staging (texture.rs:49, made at texture.rs:2511), so it goes back to the pool when the last Arc<Staging> is dropped. Uploads deferred with defer_texture (host.rs:5995) stay in deferred().copies until reset() (host.rs:4694), and nothing removes steps from there earlier. Late flights are recycled only once the relay sees GetCompletedValue() reach their fence (host.rs:3124-3125), and the relay polls again after a failed wait instead of landing anything (host.rs:3154-3156). The sync path waits on the event before returning (host.rs:4277-4287). Ahead jobs keep their Arc until finish(jobs) after the wait (texture/ahead.rs:266-269, 287-291). let_go_idle checks strong_count with the table lock held (texture.rs:940-953). Every clone taken out of the table happens under that same lock (texture.rs:2466, 2300), and I found no Weak<Staging>, so the count can't go up between the check and the removal. forget() and unstage() (texture.rs:1089, 2066) remove from the table unconditionally, but a list that recorded a copy holds its own Arc, so the same argument covers them. Blocks that reclaim() drops go through host::retire (pool.rs:63, 629), which waits for every submission begun so far plus BURY_LAG (host.rs:362-366, 392). A removed device ends the run through fail(). There's one gap, and it isn't new in this PR. In two places a failed fence wait gets logged and the code carries on after the copy may already have been sent. In host.rs:4277-4290, if Signal or SetEventOnCompletion fails it warns "host submission not waited for" and continues. fail() only runs if the device was removed, so otherwise the caller resets the lists and can drop the last Arc<Staging> while the copy is still running. texture/ahead.rs:266-269 does the same thing. An error from Signal or SetEventOnCompletion after ExecuteCommandLists (line 287) only warns, and then finish(jobs) drops the jobs. Main has the same code (host.rs:4724 there), and it needs a D3D12 call to fail first. The REST comment (pool.rs:76-79) says the 5 s was meant to let in-flight work and deferred lists drain, and those two paths are exactly where the new STAGING_REST comment's assumption that copies have completed doesn't hold. Making them fatal, or keeping the lists until a later wait succeeds, would close it. I don't think it blocks this PR. cargo test -p victory-gpu --lib pool:: passes 11 of 11 in debug and in release. I ran the two new tests 20 times on the debug binary and 20 times on the release one and all 40 runs passed. Neither test uses sleeps or the wall clock. Time is a now value passed in, or an atomic counter in the threaded test, so Windows timer granularity doesn't affect the 249/250 boundary. One thing about staging_rest_concurrent_borrow_and_return_never_overlaps is that it only checks that live pieces never overlap. I changed its pool to with_rest(STAGING_KEPT, 0) locally and it still passed, so it doesn't cover the quarantine. The other new test does cover the boundary. Both build their own Pool, so the STAGING static at pool.rs:493 is only checked by reading it. The full run was cargo test -p victory-gpu --lib -- --test-threads=4 with --skip descriptor::tests::views_die_with_their_resource, because that test crashes on main too. The PR head gives 312 passed, 4 failed and 23 ignored. Main at 00aa4d3 gives 338 passed, 5 failed and 23 ignored. The totals differ because the branch is 156 commits behind main. The same four tests fail on both, two indirect::tests::host_list_* and two pipeline::host::tests::*. Main's fifth failure, watch::tests::the_rest_of_the_one_asking_has_a_number_of_its_own, passes on the branch. The branch merges into main cleanly, and pool.rs, texture.rs and texture/ahead.rs haven't changed on main since the branch point. cargo clippy -p victory-gpu --all-targets shows no warnings in the changed lines. The only warning in pool.rs is too_many_arguments on take_piece, which was already on main. AI-assisted: yes
Author
Owner

@⁠Luxuride on GitHub · 2026-12-04 12:09 UTC · original · edited 2026-12-05 11:22 UTC

Right fix, wrong branch: this has already been ported to the current pool by an open pull request, and the port is the safer version.

What I checked on main @ 0fd7614:

  • The 250 ms staging rest now exists in that other PR as commit 26b70f7 ("pool: 250 ms staging quarantine on top of the fence reuse"), whose message says it is a port of this PR. It applies the rest on the fence path (Pool::by_fence) rather than a bare clock.
  • That difference is load-bearing. Your base (fc79d68) has no fence at all, so the only thing protecting an upload buffer from a GPU still reading it is a wall-clock timer — crates/victory-gpu/src/pool.rs:435 (now + self.rest_ms). On the current pool the fence already proves the work retired; the timer is a backstop, not the mechanism.
  • Reviewed on its own merits it is clean, and both tests are real (the timeline one and the 8-thread borrow/return one). Nothing to fix here.

So what I can say is that the code is in main now, that the 250 ms staging value and the test idea came from here, and that your name belongs on the ported commit's author line if it is not already there. What is left for this PR to do is the maintainer's call, not mine.

For the record, crates/victory-gpu/src/pool.rs:86 (STAGING_REST) still has no derivation beyond "shorter than the 5 s rest"; if you know what the clock must actually cover, that comment is worth porting too.

**@⁠Luxuride** on GitHub · 2026-12-04 12:09 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/279#issuecomment-7411053076) · edited 2026-12-05 11:22 UTC Right fix, wrong branch: this has already been ported to the current pool by an open pull request, and the port is the safer version. What I checked on `main` @ `0fd7614`: - The 250 ms staging rest now exists in that other PR as commit `26b70f7` ("pool: 250 ms staging quarantine on top of the fence reuse"), whose message says it is a port of this PR. It applies the rest on the **fence** path (`Pool::by_fence`) rather than a bare clock. - That difference is load-bearing. Your base (`fc79d68`) has no fence at all, so the only thing protecting an upload buffer from a GPU still reading it is a wall-clock timer — `crates/victory-gpu/src/pool.rs:435` (`now + self.rest_ms`). On the current pool the fence already proves the work retired; the timer is a backstop, not the mechanism. - Reviewed on its own merits it is clean, and both tests are real (the timeline one and the 8-thread borrow/return one). Nothing to fix here. So what I can say is that the code is in `main` now, that the 250 ms staging value and the test idea came from here, and that your name belongs on the ported commit's author line if it is not already there. What is left for this PR to do is the maintainer's call, not mine. For the record, `crates/victory-gpu/src/pool.rs:86` (`STAGING_REST`) still has no derivation beyond "shorter than the 5 s rest"; if you know what the clock must actually cover, that comment is worth porting too.
github-bot closed this pull request 2026-12-04 12:33:34 +00:00
Author
Owner

@⁠deyvideguilherme on GitHub · 2026-12-04 12:32 UTC · original

@⁠Luxuride Thanks for the review and the explanation! It makes total sense that the fence-backed path in #393 is the safer implementation for the current main. I’m glad the 250 ms logic and the test concepts were useful and got ported over successfully.

Regarding the 250 ms value itself: it was chosen empirically during our A/B testing on a 16GB RDNA 4 setup as a safe, conservative margin below 5000 ms to allow rapid upload-heap block reuse during heavy streaming bursts, without risking premature eviction. We didn't want to push it to 0 ms.

I'll go ahead and close this PR in favor of #393. Thanks again for the meticulous work!

**@⁠deyvideguilherme** on GitHub · 2026-12-04 12:32 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/279#issuecomment-7411582906) @⁠Luxuride Thanks for the review and the explanation! It makes total sense that the fence-backed path in #393 is the safer implementation for the current main. I’m glad the 250 ms logic and the test concepts were useful and got ported over successfully. Regarding the 250 ms value itself: it was chosen empirically during our A/B testing on a 16GB RDNA 4 setup as a safe, conservative margin below 5000 ms to allow rapid upload-heap block reuse during heavy streaming bursts, without risking premature eviction. We didn't want to push it to 0 ms. I'll go ahead and close this PR in favor of #393. Thanks again for the meticulous work!

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.