[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

Open
f41 wants to merge 1 commit from f41/Darner:pr-rt-freed-gathered into main
First-time contributor

Refs #60

Contributor role (optional)

  • Fluxer ID: 1577614931405045760

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_TIMELINE snapshots (7 × 60 frames) during a drive. In 25 of the 32 frames of 90 ms or more, the title's CopyPageMappings and XMemMapPhysicalPages made up 30% or more of the chain the present came out of, 58 ms of CopyPageMappings per 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 calls hwrt::title_memory_freed for 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.

slow page-mapping copy, mapped at once (DARNER_MAP_ONCE) ranges=1104 joined=1104 ok=true total_us=114115 locks_us=0 map_us=114115 pages=7111 steps_us=told_unmapping:105591,told_splitting:0,host_unmap:0,unmap_books:2891,section_window:1,placeholder_split:149,told_remapped:22,told_view:27,map_books:21,host_map:0,notes_handed:213
slow page-mapping copy, mapped at once (DARNER_MAP_ONCE) ranges=953 joined=953 ok=true total_us=76065 locks_us=1 map_us=76064 pages=1682 steps_us=told_unmapping:72421,told_splitting:0,host_unmap:0,unmap_books:365,section_window:33,placeholder_split:489,told_remapped:564,told_view:655,map_books:334,host_map:0,notes_handed:28

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 unless 0): 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_freed calls it with one range. MeshletCache::forget takes 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_unmapping adds its range to a thread-local list while a gathering scope is open, and the outermost scope hands the list to hwrt when it ends. title_memory_notes_gathered (DARNER_MAP_ONCE=1) and the new title_memory_freed_gathered open that scope.
  • xmem.rs: map_aliases, the entry of every page-mapping copy, runs inside title_memory_freed_gathered, so it works with or without DARNER_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=0 restores the walk per page.

How it was tested

  • Tested revision: this PR is 7a9d3fd on main 1a5286a. The game runs used the same change applied to e6245bf, together with #23's hwrt.rs change, 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.
  • Commands and results (on 7a9d3fd):
    • cargo clippy --workspace --all-targets --locked -- -D warnings: clean
    • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: clean
    • cargo test --workspace --locked: stopped at darner-shader with 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") and emit::wave64_tests::lds_shared_atomic_add_producer_executes_wave32_on_hardware (wave size 64, expected 32). Both fail the same way on main 1a5286a, and this PR does not touch darner-shader or anything it depends on. Every crate before it passed. 3,267 tests passed in all, including 554 in darner-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_due and host::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 on main. They look timing-dependent under the parallel run.
    • cargo test -p darner-gpu --lib hwrt: 24 passed, including the new freed_ranges_joined_name_what_each_alone_names. cargo test -p darner-gpu --lib watch: 102 passed.
  • Gameplay: RTX 5090 (32 GB, driver 621.14), 9800X3D, Series X profile, 1440p, DLSS Quality, frame generation off, hardware ray tracing, warm shader cache. All runs used the diag build with DARNER_TIMELINE snapshots, DARNER_LONG_FRAMES=1 and DARNER_LATE_WAIT_LISTS=512 (not the default 256). Each was a hand-driven city drive with traffic, 2.4-4.7 minutes.
  • Before and after, the default path (DARNER_MAP_ONCE not set):
while driving before this PR
seconds with a frame over 100 ms, first 2 min 17% 13%
seconds with a frame over 100 ms, after 2 min 44% 12%
frames over 100 ms, after 2 min 67 in 107 s 15 in 120 s
worst frame 552 ms 265 ms
timeline snapshots: frames of 90 ms or more / 420 32 5
CopyPageMappings per such frame 58 ms 1.9 ms
median frame 39-41 ms 37-41 ms
  • With DARNER_MAP_ONCE=1, which is off by default and has the step timers:
while driving before this PR
seconds with a frame over 100 ms, first 2 min 11% 4%
page-mapping copies over 20 ms / over 50 ms 194 / 23 3 / 0
slowest page-mapping copy 114 ms 39 ms
share of copy time in title_memory_unmapping 97% under 1%
  • Coverage gaps:
    • One card and one player's drives. These are not matched benchmarks: the routes were similar but driven by hand, and the "before" and "after" drives had different lengths.
    • Not tested with software RT, RT off (nothing changes there, as title_memory_freed returns early without RT), frame generation, Series S, AMD or Intel.
    • On the default path, a few page-mapping copies still took 50-91 ms (3 in 4.7 minutes). Without DARNER_MAP_ONCE the step timers do not cover that path, so I can't say where that time went.
    • With DARNER_MAP_ONCE=1, the slowest remaining copies spent their time in title_memory_view (28-37 ms). Not looked at here.
    • In the longest drive the card's memory went over its budget after about 4 minutes, and hitches rose again from then on (15 of the last 54 seconds). That is a separate problem, card memory growth (#19), not this one. #23 also raised the BLAS budget on 32 GB cards, and the runs used that.

Checklist

  • This PR contains one change based on main.
  • The diff follows docs/asset-policy.md.
  • I recorded this PR's dependencies below.
    • Depends on: none

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches.
  • Related PRs: none fixes this. Open PRs that touch the same files: #23 (hwrt.rs, mine, BLAS inputs), #57 (hwrt.rs), #56 (xmem.rs), #50 (watch.rs). None changes title_memory_freed, title_memory_unmapping, title_memory_notes_gathered or map_aliases, but they may conflict textually.

🤖 Generated with Claude Code

Refs #60 ## Contributor role (optional) - Fluxer ID: 1577614931405045760 ## 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_TIMELINE` snapshots (7 × 60 frames) during a drive. In 25 of the 32 frames of 90 ms or more, the title's `CopyPageMappings` and `XMemMapPhysicalPages` made up 30% or more of the chain the present came out of, 58 ms of `CopyPageMappings` per 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 calls `hwrt::title_memory_freed` for 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. ``` slow page-mapping copy, mapped at once (DARNER_MAP_ONCE) ranges=1104 joined=1104 ok=true total_us=114115 locks_us=0 map_us=114115 pages=7111 steps_us=told_unmapping:105591,told_splitting:0,host_unmap:0,unmap_books:2891,section_window:1,placeholder_split:149,told_remapped:22,told_view:27,map_books:21,host_map:0,notes_handed:213 slow page-mapping copy, mapped at once (DARNER_MAP_ONCE) ranges=953 joined=953 ok=true total_us=76065 locks_us=1 map_us=76064 pages=1682 steps_us=told_unmapping:72421,told_splitting:0,host_unmap:0,unmap_books:365,section_window:33,placeholder_split:489,told_remapped:564,told_view:655,map_books:334,host_map:0,notes_handed:28 ``` 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 unless `0`): 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_freed` calls it with one range. `MeshletCache::forget` takes 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_unmapping` adds its range to a thread-local list while a gathering scope is open, and the outermost scope hands the list to `hwrt` when it ends. `title_memory_notes_gathered` (`DARNER_MAP_ONCE=1`) and the new `title_memory_freed_gathered` open that scope. - `xmem.rs`: `map_aliases`, the entry of every page-mapping copy, runs inside `title_memory_freed_gathered`, so it works with or without `DARNER_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=0` restores the walk per page. ## How it was tested - Tested revision: this PR is `7a9d3fd` on `main` `1a5286a`. The game runs used the same change applied to `e6245bf`, together with #23's `hwrt.rs` change, 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. - Commands and results (on `7a9d3fd`): - `cargo clippy --workspace --all-targets --locked -- -D warnings`: clean - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: clean - `cargo test --workspace --locked`: stopped at `darner-shader` with 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") and `emit::wave64_tests::lds_shared_atomic_add_producer_executes_wave32_on_hardware` (wave size 64, expected 32). Both fail the same way on `main` `1a5286a`, and this PR does not touch `darner-shader` or anything it depends on. Every crate before it passed. 3,267 tests passed in all, including 554 in `darner-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_due` and `host::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 on `main`. They look timing-dependent under the parallel run. - `cargo test -p darner-gpu --lib hwrt`: 24 passed, including the new `freed_ranges_joined_name_what_each_alone_names`. `cargo test -p darner-gpu --lib watch`: 102 passed. - Gameplay: RTX 5090 (32 GB, driver 621.14), 9800X3D, Series X profile, 1440p, DLSS Quality, frame generation off, hardware ray tracing, warm shader cache. All runs used the `diag` build with `DARNER_TIMELINE` snapshots, `DARNER_LONG_FRAMES=1` and `DARNER_LATE_WAIT_LISTS=512` (not the default 256). Each was a hand-driven city drive with traffic, 2.4-4.7 minutes. - Before and after, the default path (`DARNER_MAP_ONCE` not set): | while driving | before | this PR | |---|---|---| | seconds with a frame over 100 ms, first 2 min | 17% | 13% | | seconds with a frame over 100 ms, after 2 min | 44% | 12% | | frames over 100 ms, after 2 min | 67 in 107 s | 15 in 120 s | | worst frame | 552 ms | 265 ms | | timeline snapshots: frames of 90 ms or more / 420 | 32 | 5 | | `CopyPageMappings` per such frame | 58 ms | 1.9 ms | | median frame | 39-41 ms | 37-41 ms | - With `DARNER_MAP_ONCE=1`, which is off by default and has the step timers: | while driving | before | this PR | |---|---|---| | seconds with a frame over 100 ms, first 2 min | 11% | 4% | | page-mapping copies over 20 ms / over 50 ms | 194 / 23 | 3 / 0 | | slowest page-mapping copy | 114 ms | 39 ms | | share of copy time in `title_memory_unmapping` | 97% | under 1% | - Coverage gaps: - One card and one player's drives. These are not matched benchmarks: the routes were similar but driven by hand, and the "before" and "after" drives had different lengths. - Not tested with software RT, RT off (nothing changes there, as `title_memory_freed` returns early without RT), frame generation, Series S, AMD or Intel. - On the default path, a few page-mapping copies still took 50-91 ms (3 in 4.7 minutes). Without `DARNER_MAP_ONCE` the step timers do not cover that path, so I can't say where that time went. - With `DARNER_MAP_ONCE=1`, the slowest remaining copies spent their time in `title_memory_view` (28-37 ms). Not looked at here. - In the longest drive the card's memory went over its budget after about 4 minutes, and hitches rose again from then on (15 of the last 54 seconds). That is a separate problem, card memory growth (#19), not this one. #23 also raised the BLAS budget on 32 GB cards, and the runs used that. ## Checklist - [x] This PR contains one change based on `main`. - [x] The diff follows `docs/asset-policy.md`. - [x] I recorded this PR's dependencies below. - Depends on: none ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. - Related PRs: none fixes this. Open PRs that touch the same files: #23 (`hwrt.rs`, mine, BLAS inputs), #57 (`hwrt.rs`), #56 (`xmem.rs`), #50 (`watch.rs`). None changes `title_memory_freed`, `title_memory_unmapping`, `title_memory_notes_gathered` or `map_aliases`, but they may conflict textually. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
title_memory_freed walked every table of the ray tracing (structures,
the LRU, waiting requests, decoded and validated index buffers) under
their write locks, and title_memory_unmapping calls it for every view
unmapped: a page at a time inside the runtime's page-mapping copies
(CopyPageMappingsBatchX). With hardware RT on, while driving, that was
97% of those copies' time, about 80 us a page, calls of 400-7,000 pages
taking 20-105 ms on the title's thread, and more the longer the drive as
the tables grow.

The ranges of one copy are now gathered (map_aliases, with or without
DARNER_MAP_ONCE) and each table is walked once for all of them at the
copy's end, before it returns to the title. DARNER_HW_RT_FREED_GATHERED=0
restores the walk a page at a time.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'pr-rt-freed-gathered:f41-pr-rt-freed-gathered'
git switch 'f41-pr-rt-freed-gathered'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'f41-pr-rt-freed-gathered'
git switch 'f41-pr-rt-freed-gathered'
git rebase 'main'
git switch 'main'
git merge --ff-only 'f41-pr-rt-freed-gathered'
git switch 'f41-pr-rt-freed-gathered'
git rebase 'main'
git switch 'main'
git merge --no-ff 'f41-pr-rt-freed-gathered'
git switch 'main'
git merge --squash 'f41-pr-rt-freed-gathered'
git switch 'main'
git merge --ff-only 'f41-pr-rt-freed-gathered'
git switch 'main'
git merge 'f41-pr-rt-freed-gathered'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
TrustNo1/Darner!92
No description provided.