host, resource: spread the sweep's texture look over looks, make placed textures outside the table lock #355
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/lobomfz--VIctory-xmem!355
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-355"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@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 UTCReplaces #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, sosettle(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. WithVICTORY_ROOM_TIMING=1at 87 %, that was 20 ms at the median and 25–66 ms in busy scenes.3. List threads waited on
PLACED_TEXTURES.texture_atmade 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:retire, which waits for every submission already started or queued to finish;VICTORY_SWEEP_SETTLE=1keeps the old drain, for comparison.host:VICTORY_LONG_FRAMESsets 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 takesMAKING, 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 beforetake_idle, which checks again that it is idle.run.conf.defaultanddocs/SETTINGS.mdlist 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'smade, the reverse of the order inwant_level,take_idleandhosted. It now copies members out 256 at a time and drops the members' lock before taking anymade. The table of textures the caller holds keeps the textures alive:Drop for Resourceremoves a texture from both under that lock. This is the change Kal0s tested (3000 rounds, no hang).MAKINGre-entry (Luxuride's question). I traced everything that runs under it, and none of it reachestexture_at,placed_knownortexture_at_made:known()(PLACED_TEXTURES);hand_out_at→hand_out_resource. This makes no D3D12 resource: it boxes theResource, inserts intoTEXTURESandTARGETS_AT, and callsobject::hand_out/answer, which only add a reference. The host copy is made at first use, outsideMAKING;PLACED_TEXTURES,texture::placed_in_host_written(HOST_WRITTEN_AT),placed_inside_target(TARGETS_AT, a log set),Placed::retain(add ref) andtrim_placed_later(atry_sendto the collection thread, which takesPLACED_TEXTURESbut neverMAKING).These are all leaf locks, and none of the five
texture_atcallers (in_existing_heap,descriptor::host::placed_texture, three tests) holds any of them. Before this PR the same work ran underPLACED_TEXTURESitself, soMAKINGadds 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
!behindand!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:
@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:
VICTORY_ROOM_TIMING)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 --libpasses 338, with the same failures asmain(indirect::tests::host_list_*×2,watch::many_askers_tests::*×2,watch::tests::what_is_left_alone_is_no_known_hole) plus the flakythe_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 policypasses.🤖 Generated with Claude Code
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@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_lapnow keeps thesweep_members()guard (resource.rs:2221) while it locks each candidate'sr.host.made(resource.rs:2237).want_levelgoes the other way round. It locksmadeat resource.rs:1399, and when it replaces a partial copy it callssweep_members().remove(..)at resource.rs:1422. Before this commit the members guard was dropped before anymadewas taken, so the two never crossed. So if the submitting thread is in a lap and a title thread callswant_levelon an idle partial copy (for examplecopy_resourcein list.rs:954, or a placed view in descriptor/host.rs:5217), the title thread holdsmadeand waits for members, then the lap reaches that texture and waits formade, and both are stuck.I reproduced it with a local test that I'm not pushing. One thread loops
gather_lapwithtextures()held over a few hundred recently used members plus one idle partial copy. The other thread loopswant_level(t, 2)andhost_copy, backdatesused, then callswant_level(t, 0). The setup is forced (the test bumps the frame counter and backdatesused), 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 insidewant_level(t, 0). Then I changedgather_laplocally to collect the prefiltered addresses under the members lock, drop it, and only then lockmade. With that change the same test did 3000 rounds with no hang. That should be safe because the caller still holdstextures(), which is what keeps the pointers valid. Atry_lockonmadethat skips the entry would also do.4e6ec00looks right to me. Makers still take turns throughMAKING, the second lookup under it stops a double make, and the insert, theplacedflag andplaced_inside_targetstill happen under the table lock. I also ran a local stress test where 8 threads went through 48 keys withtexture_at,placed_knownandtexture_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(), thentake_idleundermade) before it's let go. The step rotation in2c8a659doesn't starve any step. One question, though.sweep_steps(true)skips the lap turn, so withVICTORY_SWEEP_SETTLE=1and 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 --librun (4 threads, skippingdescriptor::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 twoindirect::tests::host_list_*tests, the twopipeline::hostlibrary tests, and the flakywatch::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
@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_idle20 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:
crates/victory-gpu/src/resource.rs:2223re-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 staleatis never dereferenced, andtake_idlere-tests idleness under the lock afterwards. Good.sweep_steps' lap branch (crates/victory-gpu/src/host.rs:355-370) advanceslet_go_idlewithout touchingLAST[4], so step 4 can still fire from the rotation on the same submission and run a lap twice. Not corrupt, just wasted work; oneLAST[4].storefixes it.The one thing I would hold the PR for:
static MAKING: Mutex<()>(resource.rs:3850) is non-reentrant and is held acrosshand_out_at, i.e. across real D3D12 resource creation. If any creation path loops back intotexture_at— through descriptor sync, orin_existing_heapatresource.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_SETTLEandVICTORY_LONG_FRAMESare indocs/SETTINGS.md:151-152but notrun.conf.default. Thedocs/SETTINGS.mdconflict withmainis 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.
af4174273fto70209f710d@Kal0s-design on GitHub · 2026-12-04 19:08 UTC · original
Re-checked
70209f7on 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_lapnow copies the members out 256 at a time and lets go of that lock (resource.rs:2226) before it takes anymade(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, soDrop for Resourcetakes the table and the members while the lap andwant_levelrun. On70209f7it did 5 runs of 5,000 rounds with no hang. That exact file still hung 3 out of 3 onaf41742, 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
madeor the table any more, and nothing takes the table orLAPundermade. The other locks taken undermadeare the same as on main.texture_atlooks right. The second lookup underMAKINGstops a double make, and the insert, theplacedflag andPlaced::retainall happen beforePLACED_TEXTURESis 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_knownandtexture_at_madereturn 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). WhenNEXTis 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 setLAST[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=1brings 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 --libgave 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::hostlibrary loads x2 and the flakywatchone). Clippy (--all-targets, with and withoutdiagnostics) shows nothing new compared with main.AI-assisted: yes
@supermonkeypie on GitHub · 2026-12-04 23:26 UTC · original
Deadlock:
want_levelvs the sweep'sgather_lap(lock ordermade/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 ownwatchdog (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):
So:
want_leveltakes made -> members;gather_laptakes members -> made.want_levelis not the only path thattakes 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_lapdoes not block on a member'smade. Atexture whose lock is held is in use right now, so it is not idle anyway, and the next lap looks at it again.
(Alternative: copy the member addresses out under the members lock and take
madeafter releasing it; the table lockthe 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).@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'smade, againstwant_leveltakingmadethen the members). The current head70209f7already has the fix in25c698d. It's the alternative you mention:gather_lapcopies 256 members out, lets the members lock go, and only then takesmade(resource.rs:2226 and 2241 at70209f7). My stress test that hung 3 of 3 onaf41742ran clean on70209f7. So if your build had #355 from before25c698d, 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