Skip to content

GPU: keep looked-up views across direct SRV refreshes - #3

Open
desplmfao wants to merge 3 commits into
aycow:fix/direct-indexed-buffer-coherencyfrom
desplmfao:perf/reservation-cache
Open

desplmfao wants to merge 3 commits into
aycow:fix/direct-indexed-buffer-coherencyfrom
desplmfao:perf/reservation-cache

Conversation

@desplmfao

@desplmfao desplmfao commented Nov 24, 2026 •

Copy link
Copy Markdown

Status: ready for review. The head (dbf9f2d) caches only views; private memory is asked about every time, so the committed=0 window found in 7e86c17 doesn't apply. In game it ran in seven sessions on NVIDIA/Proton (up to 7.5 minutes in the world, into the first prologue mission) with no crash attributable to it. The Xid 109 hang seen in some of those runs also occurs without this change. With the wineserver view-tree patch from Marvisak#340 the saving drops to about 20 ms of lookups per world frame (comments below); with a stock server it was 2.3 s → 0.9 s.

Stacked on #2's merge (a6367ba). Earlier context: matched on/off run on #2 and #2's description, section "Where the frame time goes".

Problem

After #2, direct SRV preparation is still about 2 s of a world frame under Proton, with sharing on or off (2,052 vs 2,391 ms in the on/off run, while write-watch went from 684 to 23 ms). With your VICTORY_PROFILE_SHARED_PREP split (docs/direct-srv-preparation-profile.md), most of it is reservation_groups_ms. A counter around the real QueryVirtualMemoryInformation calls (plus the VirtualQuery fallback from #1) puts that in watch::allocation: about 530k calls and 2.3 s per world frame. A mapping change anywhere bumps MAPPING_REVISION, so an earlier answer is rarely still live for its next use.

Change (dbf9f2d)

  • watch::allocation keeps the views it has looked up, across refreshes. Views of title physical memory are only mapped and unmapped through title_memory_remapped; record_mapping_change now removes the entries its range overlaps, and an entry no change touched is returned as live at the current revision.
  • Private memory is asked about every time, as before. The title can allocate and free it through paths that report nothing here: only VirtualAlloc/VirtualFree are hooked, and XMemAllocDefault uses the host heap. A stale extent or commit size there would group the wrong pages, or have is_empty_private → zero_reserved overwrite title data.
  • The lookup runs while the cache is held, so a change reported meanwhile waits and then removes what the lookup inserted.
  • Test view_lookups_are_cached_until_a_mapping_change: a view is asked about once; it is asked again after a remap over it but not after one elsewhere, and is still live after that; private memory is asked about every time.

How I got here (the earlier commits): a cache for one refresh pass (c21391c, 7e86c17) saves almost nothing, because each reservation comes up about once per pass. A cache of everything across passes cut the lookups to about 150, but Marvisak#185's grouped_watch_keeps_mixed_private_reservations_separate then fails after other tests: its fixture frees a reservation and allocates two at the same address without a report, and the stale extent groups across both. That is the unreported-change case, and it is why private memory is no longer cached. The one game run with that version also ended in the title's watchdog crash.

Measured

RTX 5080, proton-cachyos, Series X 1.0.2.1. One diagnostics binary of Marvisak#185 + #2 + your profiling branch (c53f8e6) + Marvisak#493 (+Marvisak#336), Marvisak#259, Marvisak#177, Marvisak#151, with VICTORY_PROFILE_SHARED_PREP=1, lookup counters, and a temporary VICTORY_NO_PASS_CACHE switch (the counters and switch are not in this PR). Medians of the first 17 world summary lines; one run each, some cut short, so these are rough.

per world frame no cache per-pass per-pass + lock views only (dbf9f2d)
frame time 6,333 ms 3,133 ms 4,300 ms 2,200 ms
submission preparation 5,980 ms 2,863 ms 4,025 ms 1,958 ms
direct SRV preparation 3,071 ms 2,344 ms 2,991 ms 2,090 ms
reservation_groups_ms 2,264 ms 1,993 ms 2,596 ms 591 ms
fallback_ms 532 ms 147 ms 345 ms 1,220 ms
system lookups 527,298 280,486 286,994 35,089
time in them 2,300 ms 1,762 ms 2,339 ms 887 ms
direct SRV ranges 792,795 808,639 830,010 844,396
GPU fault / crash none none none none (38 world lines)

The no-cache run's frame time is high next to earlier runs of this stack (2.7–2.9 s unprofiled), so the frame-time gain is more like 20 % than 65 %. Lookups drop by 93 %; the 35k left are private memory, at about 25 µs each. fallback_ms rose from about 0.5 to 1.2 s, and I don't know why yet; it may be time moving out of grouping, since direct SRV preparation fell overall.

Tests

On vkd3d-proton with VICTORY_PC_DXC set: watch:: 8, shadow:: 18 and coherency 12 pass. The full victory-gpu suite is 84 of 87; the same three (argument_clamp_builds, a_rejected_blob_gives_an_empty_library, buffer_data_reaches_the_host_texture) fail on a6367ba without this change. Merging review/cross-shadow-batching conflicts only on the ALLOCATION_QUERIES test counter; with that resolved, an earlier head passed shadow:: 22.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

desplmfao and others added 2 commits November 24, 2026 02:26
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@aycow

aycow commented Nov 24, 2026

Copy link
Copy Markdown
Owner

@desplmfao Thanks for isolating the reservation queries with the stage split. The locked per-pass result is close to the no-cache preparation result despite far fewer lookups. The cross-frame measurement leaves mapping validity and the watchdog unresolved, so I’ll leave the draft separate.

The narrower case to investigate is existing knowledge that an address is in a mapped view. It can rule out private-reservation batching without carrying a cached committed-byte count across frames. I’ll check whether the current guarded-view metadata gives a sufficiently specific proof and keep the existing per-shadow refresh as the fallback.

…every time

A refresh pass sees each reservation about once, so a cache that lasts
one pass saves almost nothing. Views are only mapped and unmapped
through title_memory_remapped, which drops the entries it overlaps, so
they can be kept across passes. Private memory can change through paths
that report nothing here (the title's heap, for one), so it is asked
about every time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@aycow

aycow commented Nov 24, 2026

Copy link
Copy Markdown
Owner

@desplmfao A focused review of 7e86c17 confirmed a commit-state window in the per-pass prototype. An empty private reservation was cached as committed=0. The native commit then made 65,536 bytes accessible and a CPU write stored a sentinel. Before the mapping notification, the cache hit still reported the old zero committed count as live. A fresh query reported the committed page, and the callback invalidated the cache.

That can affect the empty-reservation decision, and it is separate from the Proton watchdog observation. The next check is a negative-only mapped-view hint before cross-shadow grouping, with ordinary per-shadow refresh and live commit queries retained. I added the profile and draft status to Marvisak#185.

@aycow

aycow commented Nov 24, 2026

Copy link
Copy Markdown
Owner

@desplmfao I prototyped your observed-mapped-view suggestion as a negative hint on exact Marvisak#185 head a6367ba. Comparison branch

The hint uses known 64 KiB view starts to skip the private cross-shadow grouping probe. Each hinted span still goes through the ordinary per-shadow refresh. A same-VA remap from a mapped view to private memory cleared the hint. Injecting the old snapshot still refreshed the new private bytes. With two learned mapped bases and one private reservation, real QueryVirtualMemoryInformation calls went from 3 to 1, then 2 after the remap. This held on native Windows with the AMD, NVIDIA and WARP device paths. The test is host-side, and the counter excludes VirtualQuery and GetWriteWatch.

Reproduce with cargo test -p victory-gpu --features diagnostics mapped_view_hint_skips_only_cross_shadow_allocation_queries -- --nocapture --test-threads=1. The separate runtime switch is VICTORY_HINT_MAPPED_SRV_SPANS=1 on diagnostics builds. If your next matched Proton run has room for an extra toggle, could you report hinted mapped starts, reservation query counts/time, direct-SRV prep, and frame time over the same warm scene? That will show whether the skipped probes matter for your workload. The synthetic run establishes query counts and refreshed bytes. It has no Proton or CPU timing result.

@desplmfao desplmfao changed the title GPU: cache reservation lookups for direct SRV refreshes (unfinished, not for merge) GPU: keep looked-up views across direct SRV refreshes (draft) Nov 24, 2026
@desplmfao

Copy link
Copy Markdown
Author

Thanks for checking 7e86c17; that committed=0 window is the case I was worried about too. The current head dbf9f2d no longer caches private memory at all, so an empty reservation is asked about every time and there is no stored commit count for private memory to go stale. It keeps only views, which are mapped and unmapped through title_memory_remapped; record_mapping_change removes the entries a change overlaps.

The difference from your hint as I read 2cb05bc: the hint skips the lookup in refresh_across only for a range that starts at a known view start, and that range then goes through update_ready, which looks it up again there. dbf9f2d answers the view's lookup from the cache wherever it's asked. In the one Proton run of dbf9f2d (description, last column), system lookups went from 527,298 to 35,089 per world frame, reservation_groups_ms from 2,264 to 591 ms, with no fault or crash in 38 world lines; fallback_ms rose from 532 to 1,220 ms, and I haven't found out why yet.

I'll run your switch in a matched pair when the machine is free and report hinted mapped starts, reservation query count and time, direct SRV preparation and frame time over the same warm scene. I'll need to add a diagnostics counter for the hinted starts, since 2cb05bc counts them only in the test.

AI-assisted (Claude Code).

@desplmfao

desplmfao commented Nov 24, 2026 •

Copy link
Copy Markdown
Author

Matched pair for VICTORY_HINT_MAPPED_SRV_SPANS, as asked: one diagnostics binary of 2cb05bc plus Marvisak#493 (+Marvisak#336), Marvisak#259, Marvisak#177 and Marvisak#151 to reach the world, with two counters added for this run only: real allocation() calls with their time (including the Wine VirtualQuery fallback), and ranges that took the hint path. RTX 5080, proton-cachyos, Series X 1.0.2.1, story mode by VICTORY_KEYS, a fresh process each, hint run first. Window: world summary lines 12 to 32 of each run (the first minutes in the world are noisy), medians. No GPU fault or crash in either run. The dbf9f2d column is a separate binary and run from earlier, over the same window, for reference only.

per world frame hint on hint off dbf9f2d (other binary)
frame time 4,067 ms 4,433 ms 3,000 ms
submission preparation 3,870 ms 4,314 ms 2,897 ms
on the GPU 157 ms 141 ms 124 ms
submissions per frame 41 46 42
direct SRV preparation 3,811 ms 3,417 ms 2,514 ms
direct SRV ranges 735,116 791,038 732,749
hinted mapped starts 515,622 0 –
reservation queries 267,629 524,951 20,473
time in them 3,066 ms 2,729 ms 972 ms
write-watch questions 3,978 3,582 4,300

The hint takes about 70 % of the ranges and halves the reservation queries, but the time in them doesn't fall: the hinted ranges are looked up again in update_ready, and those lookups appear to be the expensive ones (about 11 µs each here vs about 5 µs without the hint). The frame-time difference is within run-to-run spread. One run each, so treat this as a direction rather than a result.

I'm pausing in-game runs until more of the open PRs have landed upstream, then I'll take a fresh baseline.

AI-assisted (Claude Code).

@desplmfao

desplmfao commented Nov 24, 2026 •

Copy link
Copy Markdown
Author

Probable root cause, found by someone else: ajsem1337 on Marvisak#340 profiled wineserver at 37 % of the title's CPU time, almost all in two loops in server/mapping.c (is_valid_view_addr() for map_view, find_mapped_view() for unmap_view) that walk process->views, a plain list holding thousands of the title's physical-page views. Their server patch adds an rb_tree keyed by view base. On an RX 7900 GRE with wine-cachyos 10.0 it took copies / write-watch from 1,748 to 73 ms and frame time from 3,615 to 1,100 ms per world frame.

It may also be why each QueryVirtualMemoryInformation / VirtualQuery here cost about 5–25 µs under Proton, but not directly: in Wine those queries for the current process are answered in ntdll under virtual_mutex without a server call (get_memory_region_info in dlls/ntdll/unix/virtual.c). If the title's map and unmap calls hold that lock while the server walks its list, the queries wait on it. I haven't verified that. If the server fix holds, this PR likely matters much less under Wine, and on Windows each call was already cheap. I'm building that patch against the wineserver of my proton-cachyos 11.0 (protocol 932) and will post a matched run here, with and without this PR, before saying more.

AI-assisted (Claude Code).

@desplmfao

desplmfao commented Nov 24, 2026 •

Copy link
Copy Markdown
Author

Measured with the wineserver view-tree patch from Marvisak#340, ported to proton-cachyos 11.0 (my confirmation there). AMD Ryzen 9 9950X3D, RTX 5080, 60 GB RAM, CachyOS. One diagnostics binary: this PR on Marvisak#185 merged with current main (#4) + Marvisak#493, Marvisak#259, Marvisak#177, Marvisak#151, Marvisak#178 (with VICTORY_DXC), VICTORY_SHADER_LOOP_LIMIT=256, with lookup counters and a temporary switch that turns this cache off. One run each, the cache-on run first.

per world frame stock server, no cache patched server, no cache, no loop limit patched, cache on patched, cache off
frame time, first 8 world lines 4,000 ms 983 ms 667 ms –
reservation queries – – 76,508 359,624
time in them – – 26 ms 46 ms
frame time, all world lines – – 750 ms (14 lines) 817 ms (54 lines)
draws per frame, median – – 2,270 584

With the patched server a lookup costs about 0.1–0.4 µs instead of 5–25 µs, so this cache saves about 20 ms per world frame of directly measured lookup time. The frame-time difference between the two runs is dominated by where the runs were: the cache-off run spent most of its time in light scenes. Grouped by draws per frame, the two runs disagree in direction (1,000–1,800 draws: 389 ms on vs 767 ms off; 1,800–2,600: 750 ms on vs 467 ms off), so I can't attribute frame time to the cache from these runs. Under Wine with the server fix, the gain I can stand behind is about 20 ms per frame. Without the server fix it was 2.3 s → 0.9 s of lookups (description).

The Xid 109 hang came in both the cache-on run and a patched-server run without this PR, so it isn't this change; the cache-off run happened not to hit it in 5 minutes. Next I'm testing whether skipping mesh 04b9be3756e8f41c and pixel 1af4a50c7fb40c97 (ajsem1337's hang suspects on Marvisak#340) avoids it.

AI-assisted (Claude Code).

@desplmfao
desplmfao marked this pull request as ready for review November 24, 2026 10:09
@desplmfao desplmfao changed the title GPU: keep looked-up views across direct SRV refreshes (draft) GPU: keep looked-up views across direct SRV refreshes Nov 24, 2026
desplmfao added a commit to desplmfao/VIctory that referenced this pull request Nov 24, 2026
desplmfao added a commit to desplmfao/VIctory that referenced this pull request Nov 24, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@aycow

aycow commented Nov 24, 2026

Copy link
Copy Markdown
Owner

@desplmfao Thanks for measuring both server configurations. I recorded the stock-server lookup comparison separately from the patched-server 46 to 26 ms result. The different draw counts leave the patched frame times inconclusive. On native Windows, the synthetic matched fixture at 68c8f20 still spent 88.180 ms in direct-SRV preparation per 12 dirty submissions with mixed allocations and 64.057 ms with all watched allocations. This changes allocation modes and leaves your cache disabled. If you have another warm Proton comparison, the most useful split would be lookups, range build, refresh CPU time and copied bytes, with a fixed frame window and draw count. I kept the view cache separate from Marvisak#185.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants