[pr-performance][RAYTRACING] Hardware RT: a page-mapping copy's unmapped pages leave the RT tables once, not a page at a time (DARNER_HW_RT_FREED_GATHERED) #92
Loading…
Reference in a new issue
No description provided.
Delete branch "f41/Darner:pr-rt-freed-gathered"
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?
Refs #60
Contributor role (optional)
What it changes
The problem
With hardware ray tracing on, driving gives short freezes of 100-550 ms every few seconds. It gets worse the longer you drive: before this change, 17% of seconds had a frame over 100 ms in the first two minutes and 44% after that. The average frame rate barely moves (median 39-41 ms), so the fps counter looks fine while it feels like it is "slowing down every second". That is what #60 describes, on the same kind of PC (RTX 5090, 9800X3D, HW RT). I can't confirm their logs from here, so this says
Refs.Where the time goes
I recorded
DARNER_TIMELINEsnapshots (7 × 60 frames) during a drive. In 25 of the 32 frames of 90 ms or more, the title'sCopyPageMappingsandXMemMapPhysicalPagesmade up 30% or more of the chain the present came out of, 58 ms ofCopyPageMappingsper hitch on average. In normal frames they took about 1.3 ms. Inside those calls the thread was not waiting (locks about 1 us, landing flights about 4 us). It was busy.I added temporary step timers to the page-mapping copy (not part of this PR). 97% of the time went to one step:
title_memory_unmapping, which callshwrt::title_memory_freedfor every view it unmaps. The page-mapping copies make one view per page, so that is once per page. Each call walks every table of the ray tracing under its write lock:as_map, the shown TLAS, the waiting requests, the LRU, and the decoded and validated index buffers. After a few minutes of driving those hold 11,000-19,000 structures and as many index buffers. That cost about 80 us per page, and copies of 400-7,000 pages took 20-114 ms on the title's thread.This explains the pattern: it only happens with RT on, it is worst while driving (the most streaming), and it grows over time (the tables grow).
The fix
DARNER_HW_RT_FREED_GATHERED(on unless0): inside a page-mapping copy, the unmapped ranges are collected, and each RT table is walked once for all of them at the end of the copy, before it returns to the title. Previously each table was walked once per page.hwrt.rs:title_memory_freed_many(ranges)joins the ranges and walks each table once, with a binary search per key.title_memory_freedcalls it with one range.MeshletCache::forgettakes a predicate instead of(lo, hi). A unit test checks that the joined ranges remove exactly what the ranges one at a time would.watch.rs:title_memory_unmappingadds its range to a thread-local list while a gathering scope is open, and the outermost scope hands the list tohwrtwhen it ends.title_memory_notes_gathered(DARNER_MAP_ONCE=1) and the newtitle_memory_freed_gatheredopen that scope.xmem.rs:map_aliases, the entry of every page-mapping copy, runs insidetitle_memory_freed_gathered, so it works with or withoutDARNER_MAP_ONCE.The ray tracing's tables end up the same as before once the copy returns. During the copy, entries for pages already unmapped stay a few ms longer than before, but the copy was already removing them one page at a time partway through, so nothing could rely on the old order. The locks are taken in the same order as before, from the same place, so no new lock nesting.
DARNER_HW_RT_FREED_GATHERED=0restores the walk per page.How it was tested
7a9d3fdonmain1a5286a. The game runs used the same change applied toe6245bf, together with #23'shwrt.rschange, a port of the long-frame logging from #7, a local DLSS 5 change, and the temporary step timers above. None of those is in this PR.7a9d3fd):cargo clippy --workspace --all-targets --locked -- -D warnings: cleancargo clippy --workspace --all-targets --all-features --locked -- -D warnings: cleancargo test --workspace --locked: stopped atdarner-shaderwith 2 failures,emit::waterfall_tests::sampled_marks_restore_early_depth_only_when_safe_in_pixel_shaders(DXIL validation: "Not all elements of output SV_Depth were written") andemit::wave64_tests::lds_shared_atomic_add_producer_executes_wave32_on_hardware(wave size 64, expected 32). Both fail the same way onmain1a5286a, and this PR does not touchdarner-shaderor anything it depends on. Every crate before it passed. 3,267 tests passed in all, including 554 indarner-shader.cargo test --workspace --locked --no-fail-fast --exclude darner-shader: 2,737 passed, 3 failed:host::refresh_tests::refresh_front_zero_arc_matches_and_runs_dueandhost::card_steps::tests::shared_scratch_eliminates_flight_settle_end_to_end(2 failures). Both passed in the first full run with the same test binary, and 3 of 3 times alone on this branch and onmain. They look timing-dependent under the parallel run.cargo test -p darner-gpu --lib hwrt: 24 passed, including the newfreed_ranges_joined_name_what_each_alone_names.cargo test -p darner-gpu --lib watch: 102 passed.diagbuild withDARNER_TIMELINEsnapshots,DARNER_LONG_FRAMES=1andDARNER_LATE_WAIT_LISTS=512(not the default 256). Each was a hand-driven city drive with traffic, 2.4-4.7 minutes.DARNER_MAP_ONCEnot set):CopyPageMappingsper such frameDARNER_MAP_ONCE=1, which is off by default and has the step timers:title_memory_unmappingtitle_memory_freedreturns early without RT), frame generation, Series S, AMD or Intel.DARNER_MAP_ONCEthe step timers do not cover that path, so I can't say where that time went.DARNER_MAP_ONCE=1, the slowest remaining copies spent their time intitle_memory_view(28-37 ms). Not looked at here.Checklist
main.docs/asset-policy.md.Checked for overlap
hwrt.rs, mine, BLAS inputs), #57 (hwrt.rs), #56 (xmem.rs), #50 (watch.rs). None changestitle_memory_freed,title_memory_unmapping,title_memory_notes_gatheredormap_aliases, but they may conflict textually.🤖 Generated with Claude Code
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.