host, resource: spread the sweep's texture look over looks, make placed textures outside the table lock #355

Manually merged
github merged 9 commits from github-bot/gh-355 into gh-pr/355/base 2026-12-05 19:33:04 +00:00 AGit
Owner

@⁠OliWoly on GitHub · 2026-12-03 19:26 UTC · original
OliWoly:pr/texture-sweep-steps → main · +288 −56 · 4 files · 9 commits · merged 2026-12-05 19:20 UTC

Replaces #354 (its two commits are the first two here). Rebased on main (0fd7614).

Problems

1. Every 1.5 s the sweep drained the card when frame generation was on. With submissions sent ahead (late && flying()), the once-a-second sweep waited for a moment with nothing in the air. Since the present is sent ahead too, nothing provided one, so settle(WHY_SYNC) drained the card for it every 1.5 s. With FSR 4 frame generation at ~30 real fps, that was ~42 ms of GPU wait every 1.5 s, including a chain of 35 long frames exactly 1.5 s apart while standing still.

2. The sweep held the submitting thread for 25–47 ms at once, every second. A timeline of a busy scene (diagnostics build, VICTORY_TIMELINE) showed its five steps running back to back, and one of them, resource::let_go_idle, took nearly all of the time. With the card past 80 % of its budget, that step gathers every texture that might go (~52,000–79,000 in a busy scene, each with its own lock) and sorts them, with the table of textures locked the whole time. With VICTORY_ROOM_TIMING=1 at 87 %, that was 20 ms at the median and 25–66 ms in busy scenes.

3. List threads waited on PLACED_TEXTURES. texture_at made a texture's resource while holding the table's lock. In list calls of 5 ms or more, ~40 % of the time was spent waiting on locks, and this lock was the biggest share.

Changes

  • host: the sweep runs behind flights, without the drain. Everything it lets go of is already safe there:

    • shadow windows, host copies and the pools' blocks go through retire, which waits for every submission already started or queued to finish;
    • a staging buffer goes only when no list holds it.

    VICTORY_SWEEP_SETTLE=1 keeps the old drain, for comparison.

  • host: VICTORY_LONG_FRAMES sets how many "long frame" lines are written (default 50,000; was 5,000, which a 30 fps session reaches in ~20 min). This is its own commit.

  • host: the sweep's steps run one at a time, at least 200 ms apart, and each still runs once a second. After a drain made for the sweep (VICTORY_SWEEP_SETTLE=1), every step that is due runs at once. A step of 5 ms or more gets a log line, and each step gets a timeline span.

  • resource: a placed texture is made outside the table's lock. A lookup takes the lock only to look. A thread that has to make a texture takes MAKING, looks again, makes it unlocked, then puts it in the table, so makers take turns and no texture is made twice.

  • resource: the gathering for the card's memory is spread over several looks (gather_lap). Each look does at most 4 ms of it and resumes where the last one stopped. Once every member has been looked at, it sorts and lets go exactly as before. Anything an earlier look found is looked up in the table again before take_idle, which checks again that it is idle.

  • run.conf.default and docs/SETTINGS.md list both new settings, in a commit of their own.

Review fixes (since the first push)

  • Lock order in gather_lap (found by @⁠Kal0s-design). The lap held the members' lock while it took each texture's made, the reverse of the order in want_level, take_idle and hosted. It now copies members out 256 at a time and drops the members' lock before taking any made. The table of textures the caller holds keeps the textures alive: Drop for Resource removes a texture from both under that lock. This is the change Kal0s tested (3000 rounds, no hang).

  • MAKING re-entry (Luxuride's question). I traced everything that runs under it, and none of it reaches texture_at, placed_known or texture_at_made:

    • known() (PLACED_TEXTURES);
    • hand_out_at → hand_out_resource. This makes no D3D12 resource: it boxes the Resource, inserts into TEXTURES and TARGETS_AT, and calls object::hand_out/answer, which only add a reference. The host copy is made at first use, outside MAKING;
    • then PLACED_TEXTURES, texture::placed_in_host_written (HOST_WRITTEN_AT), placed_inside_target (TARGETS_AT, a log set), Placed::retain (add ref) and trim_placed_later (a try_send to the collection thread, which takes PLACED_TEXTURES but never MAKING).

    These are all leaf locks, and none of the five texture_at callers (in_existing_heap, descriptor::host::placed_texture, three tests) holds any of them. Before this PR the same work ran under PLACED_TEXTURES itself, so MAKING adds no new nesting.

  • A lap turn now updates LAST[4], so the rotation doesn't run the step again 200 ms later.

  • Lap with VICTORY_SWEEP_SETTLE=1 (Kal0s's question). That was not intended: one 4 ms slice per drain could leave textures unreleased for minutes. After a drain made for the sweep, an open lap is now finished at once (at most 64 slices), since the card is waiting anyway.

  • The !behind and !sweep_settles() branches are now one branch.

Results

All numbers come from one machine: Ryzen 7 5800X3D, RX 7900 XTX, RADV with this project's Mesa patch, Proton (cachyos), 2560x1440, FSR 4 quality and frame generation (VICTORY_FIDELITYFX_FRAMEGEN_DIRECT=1), VICTORY_UI_PLANE=3840x2160, RT on.

Sweep behind flights (from #354): ~28 min in the world against the first ~18 min of a run before:

before after
frames ≥ 75 ms 81 / min 47 / min
frames ≥ 100 ms 27 / min 13 / min
frames 1.5 s apart chains of up to 35 none

@⁠joaowtrdev measured the same direction on an RX 9070 XT at 4K (#354): ≥ 100 ms frames dropped from 64 to 44 per minute, and the 1% low went from 6.9 to 7.9 fps. @⁠fsrajner saw no change without frame generation at ~22 fps, which fits: the drain only happens with submissions sent ahead.

Steps and lap at 87–90 % of the card's budget:

before after
looks that end a lap (VICTORY_ROOM_TIMING) 20 ms median, 43–66 ms max 6.8 ms median, 32 ms max
sweep steps ≥ 5 ms 213 in 9 min, 29 ms mean 86 in 6 min, 8.5 ms mean

Overall fps in busy scenes is unchanged: they stay limited by per-draw work on the submit thread.

Tests (Linux, cross-built, run under Wine): cargo test -p victory-gpu --lib passes 338, with the same failures as main (indirect::tests::host_list_* ×2, watch::many_askers_tests::* ×2, watch::tests::what_is_left_alone_is_no_known_hole) plus the flaky the_rest_of_the_one_asking_has_a_number_of_its_own, which passes on its own. Clippy shows no warnings in the changed code. cargo xtask policy passes.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Bg4XQaVExsGA3ocyAed39H

**@⁠OliWoly** on GitHub · 2026-12-03 19:26 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/355) `OliWoly:pr/texture-sweep-steps` → `main` · +288 −56 · 4 files · 9 commits · merged 2026-12-05 19:20 UTC **Replaces #354** (its two commits are the first two here). Rebased on `main` (`0fd7614`). ## Problems **1. Every 1.5 s the sweep drained the card when frame generation was on.** With submissions sent ahead (`late && flying()`), the once-a-second sweep waited for a moment with nothing in the air. Since the present is sent ahead too, nothing provided one, so `settle(WHY_SYNC)` drained the card for it every 1.5 s. With FSR 4 frame generation at ~30 real fps, that was ~42 ms of GPU wait every 1.5 s, including a chain of 35 long frames exactly 1.5 s apart while standing still. **2. The sweep held the submitting thread for 25–47 ms at once, every second.** A timeline of a busy scene (diagnostics build, `VICTORY_TIMELINE`) showed its five steps running back to back, and one of them, `resource::let_go_idle`, took nearly all of the time. With the card past 80 % of its budget, that step gathers every texture that might go (~52,000–79,000 in a busy scene, each with its own lock) and sorts them, with the table of textures locked the whole time. With `VICTORY_ROOM_TIMING=1` at 87 %, that was 20 ms at the median and 25–66 ms in busy scenes. **3. List threads waited on `PLACED_TEXTURES`.** `texture_at` made a texture's resource while holding the table's lock. In list calls of 5 ms or more, ~40 % of the time was spent waiting on locks, and this lock was the biggest share. ## Changes - **`host`: the sweep runs behind flights, without the drain.** Everything it lets go of is already safe there: - shadow windows, host copies and the pools' blocks go through `retire`, which waits for every submission already started or queued to finish; - a staging buffer goes only when no list holds it. `VICTORY_SWEEP_SETTLE=1` keeps the old drain, for comparison. - **`host`: `VICTORY_LONG_FRAMES`** sets how many "long frame" lines are written (default 50,000; was 5,000, which a 30 fps session reaches in ~20 min). This is its own commit. - **`host`: the sweep's steps run one at a time, at least 200 ms apart**, and each still runs once a second. After a drain made for the sweep (`VICTORY_SWEEP_SETTLE=1`), every step that is due runs at once. A step of 5 ms or more gets a log line, and each step gets a timeline span. - **`resource`: a placed texture is made outside the table's lock.** A lookup takes the lock only to look. A thread that has to make a texture takes `MAKING`, looks again, makes it unlocked, then puts it in the table, so makers take turns and no texture is made twice. - **`resource`: the gathering for the card's memory is spread over several looks (`gather_lap`).** Each look does at most 4 ms of it and resumes where the last one stopped. Once every member has been looked at, it sorts and lets go exactly as before. Anything an earlier look found is looked up in the table again before `take_idle`, which checks again that it is idle. - `run.conf.default` and `docs/SETTINGS.md` list both new settings, in a commit of their own. ## Review fixes (since the first push) - **Lock order in `gather_lap`** (found by @⁠Kal0s-design). The lap held the members' lock while it took each texture's `made`, the reverse of the order in `want_level`, `take_idle` and `hosted`. It now copies members out 256 at a time and drops the members' lock before taking any `made`. The table of textures the caller holds keeps the textures alive: `Drop for Resource` removes a texture from both under that lock. This is the change Kal0s tested (3000 rounds, no hang). - **`MAKING` re-entry** (Luxuride's question). I traced everything that runs under it, and none of it reaches `texture_at`, `placed_known` or `texture_at_made`: - `known()` (`PLACED_TEXTURES`); - `hand_out_at` → `hand_out_resource`. This makes **no D3D12 resource**: it boxes the `Resource`, inserts into `TEXTURES` and `TARGETS_AT`, and calls `object::hand_out`/`answer`, which only add a reference. The host copy is made at first use, outside `MAKING`; - then `PLACED_TEXTURES`, `texture::placed_in_host_written` (`HOST_WRITTEN_AT`), `placed_inside_target` (`TARGETS_AT`, a log set), `Placed::retain` (add ref) and `trim_placed_later` (a `try_send` to the collection thread, which takes `PLACED_TEXTURES` but never `MAKING`). These are all leaf locks, and none of the five `texture_at` callers (`in_existing_heap`, `descriptor::host::placed_texture`, three tests) holds any of them. Before this PR the same work ran under `PLACED_TEXTURES` itself, so `MAKING` adds no new nesting. - **A lap turn now updates `LAST[4]`**, so the rotation doesn't run the step again 200 ms later. - **Lap with `VICTORY_SWEEP_SETTLE=1`** (Kal0s's question). That was not intended: one 4 ms slice per drain could leave textures unreleased for minutes. After a drain made for the sweep, an open lap is now finished at once (at most 64 slices), since the card is waiting anyway. - The `!behind` and `!sweep_settles()` branches are now one branch. ## Results All numbers come from one machine: Ryzen 7 5800X3D, RX 7900 XTX, RADV with this project's Mesa patch, Proton (cachyos), 2560x1440, FSR 4 quality and frame generation (`VICTORY_FIDELITYFX_FRAMEGEN_DIRECT=1`), `VICTORY_UI_PLANE=3840x2160`, RT on. **Sweep behind flights** (from #354): ~28 min in the world against the first ~18 min of a run before: | | before | after | |---|---|---| | frames ≥ 75 ms | 81 / min | **47 / min** | | frames ≥ 100 ms | 27 / min | **13 / min** | | frames 1.5 s apart | chains of up to 35 | **none** | @⁠joaowtrdev measured the same direction on an RX 9070 XT at 4K (#354): ≥ 100 ms frames dropped from 64 to 44 per minute, and the 1% low went from 6.9 to 7.9 fps. @⁠fsrajner saw no change without frame generation at ~22 fps, which fits: the drain only happens with submissions sent ahead. **Steps and lap** at 87–90 % of the card's budget: | | before | after | |---|---|---| | looks that end a lap (`VICTORY_ROOM_TIMING`) | 20 ms median, 43–66 ms max | **6.8 ms median, 32 ms max** | | sweep steps ≥ 5 ms | 213 in 9 min, 29 ms mean | **86 in 6 min, 8.5 ms mean** | Overall fps in busy scenes is unchanged: they stay limited by per-draw work on the submit thread. **Tests** (Linux, cross-built, run under Wine): `cargo test -p victory-gpu --lib` passes 338, with the same failures as `main` (`indirect::tests::host_list_*` ×2, `watch::many_askers_tests::*` ×2, `watch::tests::what_is_left_alone_is_no_known_hole`) plus the flaky `the_rest_of_the_one_asking_has_a_number_of_its_own`, which passes on its own. Clippy shows no warnings in the changed code. `cargo xtask policy` passes. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01Bg4XQaVExsGA3ocyAed39H
With submissions sent ahead (`late && flying()`), the once-a-second sweep
waited for a moment with nothing in the air, and since the present is sent
ahead too nothing brought one: every 1.5 s `settle(WHY_SYNC)` drained the
card for it. In a 30 fps run with FSR frame generation (RX 7900 XTX, RADV)
that was a frame of ~42 ms GPU wait every 1.5 s: 69 of 499 frames over
50 ms in 277 s in the world, a chain of 35 of them 1.5 s apart while
standing still.

All the sweep lets go of is already safe behind flights: shadow windows,
host copies and the pools' blocks go by `retire`, which waits for every
submission begun or in the line to finish, and a staging buffer goes only
when no list holds it (a list holds it until it is reset, after it ran).
So the sweep now runs behind flights as well, without the drain.
VICTORY_SWEEP_SETTLE=1 keeps the old way.

Afterwards (same settings, ~28 min in the world against the first ~18 of
the run before): frames of 75 ms or more 81 -> 47 a minute, of 100 ms or
more 27 -> 13, and no frames 1.5 s apart left. ~3 h of play with it, no
device loss.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Bg4XQaVExsGA3ocyAed39H
The "long frame" lines (frames of 50 ms or more, with what their time went
to) stopped at 5,000: at 30 fps in a busy scene about 20 minutes in, which
left the rest of a session without them. They now go up to 50,000 by
default, and VICTORY_LONG_FRAMES sets the number.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Bg4XQaVExsGA3ocyAed39H
The sweep's five steps (the pools' room, the pipeline library, host copies
of windows, staging buffers, host copies of textures) ran together once a
second, on the submitting thread before a submission: 25-47 ms at once in
a busy scene (timeline of a dec3 diagnostics build), one long frame every
second. Each step now runs once a second as before, but one at a time and
at least 200 ms after the step before, so a frame waits for one step at
most. After a drain made for the sweep (VICTORY_SWEEP_SETTLE=1) all the
steps due run at once, as before.

A step of 5 ms or more gets a line ("a long sweep step", at most 1,000)
and each step a timeline span. In a 20-minute run only the host copies of
textures (`resource::let_go_idle`) took that long: 532 times, 32 ms on
average, 68 ms at most; the other steps stayed under 5 ms.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Bg4XQaVExsGA3ocyAed39H
`texture_at` held PLACED_TEXTURES while it made the resource for a texture
placed in title memory. The list threads look textures up in that table
for their draws all the time, so while one made a texture the others
waited: in a timeline of a busy scene ~40% of the list calls of 5 ms or
more was waiting on locks, this one the most (texture_at, texture_at_made,
and lookups without a stack, on the same lock).

A lookup now takes the lock only to look. A thread that has to make a
texture takes the `MAKING` lock, looks again, makes it without the table's
lock and then puts it in the table, so makers take turns and none makes a
texture another just made (a twin would take the first one's place in
`TARGETS_AT`, and be let go after it).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Bg4XQaVExsGA3ocyAed39H
With the card past 80 % of its budget, `let_go_idle` gathered every
texture that might go (~52,000-79,000 in a busy scene: a lock of its own,
the host-written set's and two reference counts each), sorted them, and
only then let some go, all on the submitting thread with the table of
textures locked: 20 ms at the median, 25-66 ms in busy scenes, about every
other second (VICTORY_ROOM_TIMING=1). Many of those looks let nothing go.

The gathering now goes on over looks (`gather_lap`): at most 4 ms of it a
look, from where the last one stopped, and while a lap is open the sweep
gives the step every other turn (a fifth of a second apart), so a lap of
~60,000 takes about 1.5 s. Once every member was looked at, the look sorts
and lets go as before. What an earlier look found may have been freed
since: before `take_idle` each is looked up in the table again, and
`take_idle` itself looks again at whether it is idle and held by nothing
else. A lap is dropped when the card is no longer short.

In a run at 87-90 % of the budget: the looks that end a lap 6.8 ms at the
median (was 20 ms), 32 ms at most; looks of 5 ms or more 86 in 6 minutes
(213 in 9 before), 8.5 ms on average (29 ms before).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Bg4XQaVExsGA3ocyAed39H
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-03 19:28:32 +00:00
Author
Owner

@⁠Kal0s-design on GitHub · 2026-12-03 20:10 UTC · original

Tested natively on Windows 11 (MSVC, Rust 1.98.1), with the D3D12 host tests on one NVIDIA desktop card. I only looked at the three new commits (2c8a659, 4e6ec00, af41742), since the first two are #354.

I found one real problem, a lock order inversion in af41742. gather_lap now keeps the sweep_members() guard (resource.rs:2221) while it locks each candidate's r.host.made (resource.rs:2237). want_level goes the other way round. It locks made at resource.rs:1399, and when it replaces a partial copy it calls sweep_members().remove(..) at resource.rs:1422. Before this commit the members guard was dropped before any made was taken, so the two never crossed. So if the submitting thread is in a lap and a title thread calls want_level on an idle partial copy (for example copy_resource in list.rs:954, or a placed view in descriptor/host.rs:5217), the title thread holds made and waits for members, then the lap reaches that texture and waits for made, and both are stuck.

I reproduced it with a local test that I'm not pushing. One thread loops gather_lap with textures() held over a few hundred recently used members plus one idle partial copy. The other thread loops want_level(t, 2) and host_copy, backdates used, then calls want_level(t, 0). The setup is forced (the test bumps the frame counter and backdates used), so I haven't seen this in a real run. It hung within the first few rounds every time I ran it (5 runs), with both locks held and the second thread inside want_level(t, 0). Then I changed gather_lap locally to collect the prefiltered addresses under the members lock, drop it, and only then lock made. With that change the same test did 3000 rounds with no hang. That should be safe because the caller still holds textures(), which is what keeps the pointers valid. A try_lock on made that skips the entry would also do.

4e6ec00 looks right to me. Makers still take turns through MAKING, the second lookup under it stops a double make, and the insert, the placed flag and placed_inside_target still happen under the table lock. I also ran a local stress test where 8 threads went through 48 keys with texture_at, placed_known and texture_at_made. Every key ended up with exactly one resource, and nothing hung.

The rest of the lap looks fine. The cursor resumes where it stopped, and anything found in an earlier look is checked again (textures(), then take_idle under made) before it's let go. The step rotation in 2c8a659 doesn't starve any step. One question, though. sweep_steps(true) skips the lap turn, so with VICTORY_SWEEP_SETTLE=1 and every submission behind, a lap seems to move only one 4 ms slice per settle. Nothing is let go while a lap is open, so under pressure that could take a while. Is that intended?

The full cargo test -p victory-gpu --lib run (4 threads, skipping descriptor::tests::views_die_with_their_resource) gave 338 passed, 5 failed, 23 ignored. #354's head and main gave the same counts with the same five failures, all already there before this PR. Those are the two indirect::tests::host_list_* tests, the two pipeline::host library tests, and the flaky watch::tests::the_rest_of_the_one_asking_has_a_number_of_its_own, which passes on its own. Clippy (-p victory-gpu --all-targets) shows no new warnings compared with #354.

AI-assisted: yes

**@⁠Kal0s-design** on GitHub · 2026-12-03 20:10 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/355#issuecomment-7390828044) Tested natively on Windows 11 (MSVC, Rust 1.98.1), with the D3D12 host tests on one NVIDIA desktop card. I only looked at the three new commits (2c8a659, 4e6ec00, af41742), since the first two are #354. I found one real problem, a lock order inversion in af41742. `gather_lap` now keeps the `sweep_members()` guard (resource.rs:2221) while it locks each candidate's `r.host.made` (resource.rs:2237). `want_level` goes the other way round. It locks `made` at resource.rs:1399, and when it replaces a partial copy it calls `sweep_members().remove(..)` at resource.rs:1422. Before this commit the members guard was dropped before any `made` was taken, so the two never crossed. So if the submitting thread is in a lap and a title thread calls `want_level` on an idle partial copy (for example `copy_resource` in list.rs:954, or a placed view in descriptor/host.rs:5217), the title thread holds `made` and waits for members, then the lap reaches that texture and waits for `made`, and both are stuck. I reproduced it with a local test that I'm not pushing. One thread loops `gather_lap` with `textures()` held over a few hundred recently used members plus one idle partial copy. The other thread loops `want_level(t, 2)` and `host_copy`, backdates `used`, then calls `want_level(t, 0)`. The setup is forced (the test bumps the frame counter and backdates `used`), so I haven't seen this in a real run. It hung within the first few rounds every time I ran it (5 runs), with both locks held and the second thread inside `want_level(t, 0)`. Then I changed `gather_lap` locally to collect the prefiltered addresses under the members lock, drop it, and only then lock `made`. With that change the same test did 3000 rounds with no hang. That should be safe because the caller still holds `textures()`, which is what keeps the pointers valid. A `try_lock` on `made` that skips the entry would also do. 4e6ec00 looks right to me. Makers still take turns through `MAKING`, the second lookup under it stops a double make, and the insert, the `placed` flag and `placed_inside_target` still happen under the table lock. I also ran a local stress test where 8 threads went through 48 keys with `texture_at`, `placed_known` and `texture_at_made`. Every key ended up with exactly one resource, and nothing hung. The rest of the lap looks fine. The cursor resumes where it stopped, and anything found in an earlier look is checked again (`textures()`, then `take_idle` under `made`) before it's let go. The step rotation in 2c8a659 doesn't starve any step. One question, though. `sweep_steps(true)` skips the lap turn, so with `VICTORY_SWEEP_SETTLE=1` and every submission behind, a lap seems to move only one 4 ms slice per settle. Nothing is let go while a lap is open, so under pressure that could take a while. Is that intended? The full `cargo test -p victory-gpu --lib` run (4 threads, skipping `descriptor::tests::views_die_with_their_resource`) gave 338 passed, 5 failed, 23 ignored. #354's head and main gave the same counts with the same five failures, all already there before this PR. Those are the two `indirect::tests::host_list_*` tests, the two `pipeline::host` library tests, and the flaky `watch::tests::the_rest_of_the_one_asking_has_a_number_of_its_own`, which passes on its own. Clippy (`-p victory-gpu --all-targets`) shows no new warnings compared with #354. AI-assisted: yes
Author
Owner

@⁠Luxuride on GitHub · 2026-12-04 12:10 UTC · original

The best-engineered PR in this batch, and I could read it far enough to believe the measurements: let_go_idle 20 ms median → 6.8 ms, ≥ 5 ms looks 213 in 9 min → 86 in 6 min, the lock it was waiting on named, and the stale-address path defended rather than assumed.

Verdict: worth taking now, after one question is answered and one line of docs is fixed.

What I checked:

  • The lap is bounded properly: crates/victory-gpu/src/resource.rs:2223 re-checks the clock every 256 entries against a 4 ms budget, so a lap over ~100k textures finishes in a turn instead of blocking eviction for minutes. I checked that specifically because an unbounded lap inside eviction would be worse than the 20 ms stall it replaces.
  • all.contains(&i.at) (resource.rs:2639, :2650, :2675) is an address-only re-check, so a stale at is never dereferenced, and take_idle re-tests idleness under the lock afterwards. Good.
  • sweep_steps' lap branch (crates/victory-gpu/src/host.rs:355-370) advances let_go_idle without touching LAST[4], so step 4 can still fire from the rotation on the same submission and run a lap twice. Not corrupt, just wasted work; one LAST[4].store fixes it.

The one thing I would hold the PR for: static MAKING: Mutex<()> (resource.rs:3850) is non-reentrant and is held across hand_out_at, i.e. across real D3D12 resource creation. If any creation path loops back into texture_at — through descriptor sync, or in_existing_heap at resource.rs:3026 — the submitting thread deadlocks. I cannot compile this crate on Linux, so I cannot rule it out; please walk that call chain once and say in the PR that you did.

Docs: VICTORY_SWEEP_SETTLE and VICTORY_LONG_FRAMES are in docs/SETTINGS.md:151-152 but not run.conf.default. The docs/SETTINGS.md conflict with main is a two-line table addition four people are editing — splitting the docs row into its own commit would stop these from colliding.

Also: the earlier PR from this same branch is contained here verbatim, so this is the one to review.

**@⁠Luxuride** on GitHub · 2026-12-04 12:10 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/355#issuecomment-7411074559) The best-engineered PR in this batch, and I could read it far enough to believe the measurements: `let_go_idle` 20 ms median → 6.8 ms, ≥ 5 ms looks 213 in 9 min → 86 in 6 min, the lock it was waiting on named, and the stale-address path defended rather than assumed. Verdict: worth taking now, after one question is answered and one line of docs is fixed. What I checked: - The lap is bounded properly: `crates/victory-gpu/src/resource.rs:2223` re-checks the clock every 256 entries against a 4 ms budget, so a lap over ~100k textures finishes in a turn instead of blocking eviction for minutes. I checked that specifically because an unbounded lap inside eviction would be worse than the 20 ms stall it replaces. - `all.contains(&i.at)` (`resource.rs:2639`, `:2650`, `:2675`) is an address-only re-check, so a stale `at` is never dereferenced, and `take_idle` re-tests idleness under the lock afterwards. Good. - `sweep_steps`' lap branch (`crates/victory-gpu/src/host.rs:355-370`) advances `let_go_idle` without touching `LAST[4]`, so step 4 can still fire from the rotation on the same submission and run a lap twice. Not corrupt, just wasted work; one `LAST[4].store` fixes it. The one thing I would hold the PR for: `static MAKING: Mutex<()>` (`resource.rs:3850`) is non-reentrant and is held across `hand_out_at`, i.e. across real D3D12 resource creation. If any creation path loops back into `texture_at` — through descriptor sync, or `in_existing_heap` at `resource.rs:3026` — the submitting thread deadlocks. I cannot compile this crate on Linux, so I cannot rule it out; please walk that call chain once and say in the PR that you did. Docs: `VICTORY_SWEEP_SETTLE` and `VICTORY_LONG_FRAMES` are in `docs/SETTINGS.md:151-152` but not `run.conf.default`. The `docs/SETTINGS.md` conflict with `main` is a two-line table addition four people are editing — splitting the docs row into its own commit would stop these from colliding. Also: the earlier PR from this same branch is contained here verbatim, so this is the one to review.
github-bot force-pushed github-bot/gh-355 from af4174273f to 70209f710d 2026-12-04 12:44:28 +00:00 Compare
Author
Owner

@⁠Kal0s-design on GitHub · 2026-12-04 19:08 UTC · original

Re-checked 70209f7 on native Windows (one NVIDIA desktop card, didn't run the game). I compared it with current main (b2449c4), which is 19 commits past the base this was rebased on. It still merges cleanly.

The deadlock is gone. gather_lap now copies the members out 256 at a time and lets go of that lock (resource.rs:2226) before it takes any made (resource.rs:2241). I ran the same local test as last time with one change, a third thread that makes textures, gives them a host copy and drops them, so Drop for Resource takes the table and the members while the lap and want_level run. On 70209f7 it did 5 runs of 5,000 rounds with no hang. That exact file still hung 3 out of 3 on af41742, so it still catches the old bug. Three longer runs of 150,000 rounds (with a yield in the lap loop so the third thread gets the table more often) didn't hang either.

Going through the nested locks again, I found no cycle. Nothing takes the members before made or the table any more, and nothing takes the table or LAP under made. The other locks taken under made are the same as on main.

texture_at looks right. The second lookup under MAKING stops a double make, and the insert, the placed flag and Placed::retain all happen before PLACED_TEXTURES is let go, so the trim thread can't find the new entry with only one owner. The 8 threads by 48 keys test still ends with one resource per key. The only difference I could see is that while a texture is being made, placed_known and texture_at_made return None for it instead of waiting, which seems fine for lookups that never make anything.

One question on the host side. With a lap open, each lap turn sets LAST[4] to now (host.rs:367). When NEXT is on step 4, the rotation finds that step isn't due and returns (host.rs:395-396), so steps 0 to 3 wait until the lap closes. The same thing can happen after a settle drain once the 64-slice finish has set LAST[4] (host.rs:379). It only lasts as long as a lap, but skipping a step that isn't due instead of returning would avoid it.

Leaving both settings unset doesn't give main's behaviour, but the docs say so. The sweep runs behind flights with its steps 200 ms apart (host.rs:4595), and the long-frame cap is 50,000 (host.rs:10373). VICTORY_SWEEP_SETTLE=1 brings back the drain but keeps the spread-out steps, so it isn't quite "as before" if someone uses it to compare.

cargo test -p victory-gpu --lib gave 339 passed, 5 failed and 23 ignored on both this head and main, with the same five known failures (indirect::tests::host_list_* x2, pipeline::host library loads x2 and the flaky watch one). Clippy (--all-targets, with and without diagnostics) shows nothing new compared with main.

AI-assisted: yes

**@⁠Kal0s-design** on GitHub · 2026-12-04 19:08 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/355#issuecomment-7421431282) Re-checked 70209f7 on native Windows (one NVIDIA desktop card, didn't run the game). I compared it with current main (b2449c4), which is 19 commits past the base this was rebased on. It still merges cleanly. The deadlock is gone. `gather_lap` now copies the members out 256 at a time and lets go of that lock (resource.rs:2226) before it takes any `made` (resource.rs:2241). I ran the same local test as last time with one change, a third thread that makes textures, gives them a host copy and drops them, so `Drop for Resource` takes the table and the members while the lap and `want_level` run. On 70209f7 it did 5 runs of 5,000 rounds with no hang. That exact file still hung 3 out of 3 on af41742, so it still catches the old bug. Three longer runs of 150,000 rounds (with a yield in the lap loop so the third thread gets the table more often) didn't hang either. Going through the nested locks again, I found no cycle. Nothing takes the members before `made` or the table any more, and nothing takes the table or `LAP` under `made`. The other locks taken under `made` are the same as on main. `texture_at` looks right. The second lookup under `MAKING` stops a double make, and the insert, the `placed` flag and `Placed::retain` all happen before `PLACED_TEXTURES` is let go, so the trim thread can't find the new entry with only one owner. The 8 threads by 48 keys test still ends with one resource per key. The only difference I could see is that while a texture is being made, `placed_known` and `texture_at_made` return None for it instead of waiting, which seems fine for lookups that never make anything. One question on the host side. With a lap open, each lap turn sets `LAST[4]` to now (host.rs:367). When `NEXT` is on step 4, the rotation finds that step isn't due and returns (host.rs:395-396), so steps 0 to 3 wait until the lap closes. The same thing can happen after a settle drain once the 64-slice finish has set `LAST[4]` (host.rs:379). It only lasts as long as a lap, but skipping a step that isn't due instead of returning would avoid it. Leaving both settings unset doesn't give main's behaviour, but the docs say so. The sweep runs behind flights with its steps 200 ms apart (host.rs:4595), and the long-frame cap is 50,000 (host.rs:10373). `VICTORY_SWEEP_SETTLE=1` brings back the drain but keeps the spread-out steps, so it isn't quite "as before" if someone uses it to compare. `cargo test -p victory-gpu --lib` gave 339 passed, 5 failed and 23 ignored on both this head and main, with the same five known failures (`indirect::tests::host_list_*` x2, `pipeline::host` library loads x2 and the flaky `watch` one). Clippy (`--all-targets`, with and without `diagnostics`) shows nothing new compared with main. AI-assisted: yes
Author
Owner

@⁠supermonkeypie on GitHub · 2026-12-04 23:26 UTC · original

Deadlock: want_level vs the sweep's gather_lap (lock order made / sweep_members), hit with #355 (which now carries #354's sweep-behind-flights)

Setup. RX AI PRO R9700 (RDNA 4, GFX1201), Ryzen 9 9950X3D (16 threads given to the game's container), Arch in an LXC,
patched Proton Experimental 11.0-20261123b. xmem main at 7b38ade-era (3 Dec) plus #354 and #355 (and #313, #364, #368,
#369, #9). Series X profile, FSR 4 Quality 1440p, ray tracing on.

Symptom. 37 minutes into play the frame stopped: hang watch: no frame begun for 10 s, then 20 s, then the title's own
watchdog (write to 0x0 in the image) ended the run. No GPU fault in the kernel log. Before #354/#355 we had not seen this one.

The two threads at the hang (symbolised against the build's PDB):

victory-list-7
  futex::wait_on_address
  victory_gpu::resource::want_level            resource.rs:1422   sweep_members().remove(..)   <- waits for MEMBERS
  victory_gpu::descriptor::host::placed_texture host.rs:5327
  victory_gpu::draw::State::dispatch_now / dispatch
  (holds r.host.made of the texture, taken at the top of want_level)

victory-submit
  futex::wait_on_address
  victory_gpu::host::sweep_steps                                     -> resource::gather_lap
  victory_gpu::host::sweep
  victory_gpu::host::Host::send_late
  victory_gpu::queue::behind::Lane::step
  (gather_lap holds sweep_members() and calls r.host.made.lock() on each member)   <- waits for MADE

So: want_level takes made -> members; gather_lap takes members -> made. want_level is not the only path that
takes made first (the pool move-out around resource.rs:2401 and the ghost publish around 2487 do too). Before #354 the
sweep only ran with nothing in flight, so it never overlapped list translation and the inverted order could not meet;
with the sweep behind flights (and #355 spreading the look over laps) it does. Everything else in the dump is idle (list
threads 0-6, 8, 9, pipelines, dstorage workers, residency), and the title's main thread waits for the frame.

Fix we run (since 4 Dec: about 5 hours of play over two sessions with no hang; the first, 72 min, 41.5 fps average, 20.6 1% low): the look in gather_lap does not block on a member's made. A
texture whose lock is held is in use right now, so it is not idle anyway, and the next lap looks at it again.

--- a/crates/victory-gpu/src/resource.rs
+++ b/crates/victory-gpu/src/resource.rs
@@ fn gather_lap(idle_ms: u64, age: u64) -> Option<Vec<Idle>> {
-            let made = r.host.made.lock().unwrap_or_else(|e| e.into_inner());
+            // Not `lock`: other paths hold `made` and then take the members
+            // (`want_level`, the pool's move-out), and since the sweep runs
+            // behind flights (#354/#355) they meet here. A texture in use now is
+            // not idle: the next look sees it again.
+            let made = match r.host.made.try_lock() {
+                Ok(made) => made,
+                Err(std::sync::TryLockError::Poisoned(e)) => e.into_inner(),
+                Err(std::sync::TryLockError::WouldBlock) => continue,
+            };
             if !sole_idle(r, &made, idle_ms) {

(Alternative: copy the member addresses out under the members lock and take made after releasing it; the table lock
the caller holds keeps them alive.)

Numbers from the hang session, for the sweep change's case: 37 min at 38.8 fps average, 18.7 1% low, 13.1 0.1% low, 1.1 frames
over 100 ms per 30 s, which was among our best sessions on this card, so the sweep change is worth having with this fix. The patch still applies cleanly on #355's current head (70209f7).

**@⁠supermonkeypie** on GitHub · 2026-12-04 23:26 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/355#issuecomment-7427087852) ## Deadlock: `want_level` vs the sweep's `gather_lap` (lock order `made` / `sweep_members`), hit with #355 (which now carries #354's sweep-behind-flights) **Setup.** RX AI PRO R9700 (RDNA 4, GFX1201), Ryzen 9 9950X3D (16 threads given to the game's container), Arch in an LXC, patched Proton Experimental 11.0-20261123b. xmem main at `7b38ade`-era (3 Dec) plus #354 and #355 (and #313, #364, #368, #369, #9). Series X profile, FSR 4 Quality 1440p, ray tracing on. **Symptom.** 37 minutes into play the frame stopped: `hang watch: no frame begun for 10 s`, then 20 s, then the title's own watchdog (write to 0x0 in the image) ended the run. No GPU fault in the kernel log. Before #354/#355 we had not seen this one. **The two threads at the hang** (symbolised against the build's PDB): ``` victory-list-7 futex::wait_on_address victory_gpu::resource::want_level resource.rs:1422 sweep_members().remove(..) <- waits for MEMBERS victory_gpu::descriptor::host::placed_texture host.rs:5327 victory_gpu::draw::State::dispatch_now / dispatch (holds r.host.made of the texture, taken at the top of want_level) victory-submit futex::wait_on_address victory_gpu::host::sweep_steps -> resource::gather_lap victory_gpu::host::sweep victory_gpu::host::Host::send_late victory_gpu::queue::behind::Lane::step (gather_lap holds sweep_members() and calls r.host.made.lock() on each member) <- waits for MADE ``` So: `want_level` takes **made -> members**; `gather_lap` takes **members -> made**. `want_level` is not the only path that takes made first (the pool move-out around resource.rs:2401 and the ghost publish around 2487 do too). Before #354 the sweep only ran with nothing in flight, so it never overlapped list translation and the inverted order could not meet; with the sweep behind flights (and #355 spreading the look over laps) it does. Everything else in the dump is idle (list threads 0-6, 8, 9, pipelines, dstorage workers, residency), and the title's main thread waits for the frame. **Fix we run** (since 4 Dec: about 5 hours of play over two sessions with no hang; the first, 72 min, 41.5 fps average, 20.6 1% low): the look in `gather_lap` does not block on a member's `made`. A texture whose lock is held is in use right now, so it is not idle anyway, and the next lap looks at it again. ```diff --- a/crates/victory-gpu/src/resource.rs +++ b/crates/victory-gpu/src/resource.rs @@ fn gather_lap(idle_ms: u64, age: u64) -> Option<Vec<Idle>> { - let made = r.host.made.lock().unwrap_or_else(|e| e.into_inner()); + // Not `lock`: other paths hold `made` and then take the members + // (`want_level`, the pool's move-out), and since the sweep runs + // behind flights (#354/#355) they meet here. A texture in use now is + // not idle: the next look sees it again. + let made = match r.host.made.try_lock() { + Ok(made) => made, + Err(std::sync::TryLockError::Poisoned(e)) => e.into_inner(), + Err(std::sync::TryLockError::WouldBlock) => continue, + }; if !sole_idle(r, &made, idle_ms) { ``` (Alternative: copy the member addresses out under the members lock and take `made` after releasing it; the table lock the caller holds keeps them alive.) **Numbers from the hang session, for the sweep change's case:** 37 min at 38.8 fps average, 18.7 1% low, 13.1 0.1% low, 1.1 frames over 100 ms per 30 s, which was among our best sessions on this card, so the sweep change is worth having with this fix. The patch still applies cleanly on #355's current head (70209f7).
Author
Owner

@⁠Kal0s-design on GitHub · 2026-12-05 06:11 UTC · original

@⁠supermonkeypie the trace matches the deadlock I hit on af41742 (gather_lap holding the members lock while it takes a texture's made, against want_level taking made then the members). The current head 70209f7 already has the fix in 25c698d. It's the alternative you mention: gather_lap copies 256 members out, lets the members lock go, and only then takes made (resource.rs:2226 and 2241 at 70209f7). My stress test that hung 3 of 3 on af41742 ran clean on 70209f7. So if your build had #355 from before 25c698d, the head as it is now shouldn't need the try_lock. It wouldn't hurt either, though, since a texture whose lock is held isn't idle anyway.

AI-assisted: yes

**@⁠Kal0s-design** on GitHub · 2026-12-05 06:11 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/355#issuecomment-7433170069) @⁠supermonkeypie the trace matches the deadlock I hit on af41742 (gather_lap holding the members lock while it takes a texture's `made`, against `want_level` taking `made` then the members). The current head 70209f7 already has the fix in 25c698d. It's the alternative you mention: `gather_lap` copies 256 members out, lets the members lock go, and only then takes `made` (resource.rs:2226 and 2241 at 70209f7). My stress test that hung 3 of 3 on af41742 ran clean on 70209f7. So if your build had #355 from before 25c698d, the head as it is now shouldn't need the try_lock. It wouldn't hurt either, though, since a texture whose lock is held isn't idle anyway. AI-assisted: yes
github-bot changed target branch from main to gh-pr/355/base 2026-12-05 19:21:30 +00:00
github manually merged commit 70209f710d into gh-pr/355/base 2026-12-05 19:33:04 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.