Skip to content

startup: opt-in table lock so resource and heap lookups stop queueing on note_lock - #3391

Open
lukaslw-lab wants to merge 1 commit into
R0Y00:mainfrom
lukaslw-lab:runtime/table-lock
Open

lukaslw-lab wants to merge 1 commit into
R0Y00:mainfrom
lukaslw-lab:runtime/table-lock

Conversation

@lukaslw-lab

Copy link
Copy Markdown
Contributor

Change

Fixes #3390 (resource and heap lookups queue on note_lock behind every view creation and descriptor copy).

X2W_D3D12_TABLE_LOCK=1 (opt-in): the resource table (resource_infos), the slot 133 placement index and the
descriptor heap table (heap_infos) get their own reader-writer lock, table_lock.

  • Why: note_lock guards the view records and descriptor shadows as well as these tables. View creation and
    descriptor copies take it exclusively on every call (about 1.8·10^5 per second late in a startup run). Four lookups
    read only the tables: resource_info, placed_buffer_range, memory_srv_destination, resolve_title_table
    (about 5·10^5 shared takes per second). Measured with local lock counters, they spent ~0.42 threads waiting on
    note_lock, out of ~1.18 threads waiting in total.
  • With the switch the four lookups take only table_lock shared. Every table writer (note_resource, note_heap,
    retire_placement, publish_placed_buffer) takes note_lock and then table_lock, both exclusive, so code that
    already holds note_lock still sees fixed tables and needs no change. The lock order is the same everywhere.
  • The fields the lookups read do not change after an entry is added, except a placement's kind, which only
    retire_placement (a writer) changes. resource_info keeps returning a pointer into the map as before (entries are
    never removed).
  • publish_placement (placement_publish.h) takes note_lock itself; table_lock is taken in its record getter,
    which it calls first with note_lock held, and released after it returns. The header is unchanged.
  • Without the switch nothing changes: the lookups take note_lock shared and the writers take no second lock.
  • Fixture-only exports x2w_d3d12_table_lock_probe and x2w_d3d12_table_lock_hold; switch row and section "D3D12 table lock" in
    docs/startup-runtime.md.
  • Why opt-in: it changes the locking of tables every draw path reads. Same reasoning as startup: opt-in release of upload ring chunks at a fenced submit #2949, startup: opt-in alias binding for table buffer views over aliased placed buffers #2977, startup: opt-in skip of the graphics root SRV copy that no draw reads #3028.

Verification

Windows 10 22H2, i7-4790K, RTX 3080 Ti (NVIDIA 32.0.16.1047) and WARP; release; commit 008720e4 on main d418f70e.

ctest --preset release --no-tests=error -R '^startup_table_lock_' --output-on-failure
  • New startup_table_lock_{warp,hardware}:
    • the four lookups for a committed texture (slot 76) and a shader-visible heap return while the probe holds
      note_lock exclusive, with the expected answers;
    • a heap creation waits for table_lock while x2w_d3d12_table_lock_hold holds it shared for 2 s (measured
      2000 ms);
    • four threads repeat the lookups while 16 heaps and 64 textures are created (23k–261k lookups), and every new
      entry is found afterwards.
  • Negative control startup_table_lock_control_{warp,hardware} (switch unset): the lookups must wait for note_lock
    and the heap creation must return at once (measured 0 ms); every other expectation is the same.
  • Mutant (local): table writers skip table_lock → startup_table_lock_{warp,hardware} fail with a heap creation did not wait for table_lock. The race part alone did not catch it (five passes out of five), so it is described as a
    smoke test.
  • This head, release: 4/4 startup_table_lock_*, no skips.
  • Wider families (startup_view_, placed_, resource_, address_buffer_srv_, table_alias_, memory_srv,
    formatted_view, direct_index_, stream_draw_, copied_vertex_srv_, frame_copy_, descriptor_), switch unset
    and set, with the new fixture: 114/114 each (one capability skip in both, startup_view_heap_warp).
  • tests/docs_links_check.py: passed.

Startup run of the main executable (#870), this head, release, warm shader cache, scripted input, the merged switches
(#2949, #2977, #3028, #3313) in every run, 250 s, four pairs (order off/on/on/off, then on/off/off/on). Means of four
runs per cell (three where one run did not reach 200k), range in brackets:

25k loaded blocks 100–125k 125–150k 150–175k 175–200k
fps, unset 13.9 (12.0–15.3) 12.4 (11.0–13.6) 9.8 (8.8–10.7) 9.2 (8.7–9.6)
fps, set 13.6 (13.1–14.0) 12.8 (11.7–13.7) 10.2 (9.2–11.1) 10.2 (9.8–10.6)
title CPU ms per frame, unset 332 380 478 506
title CPU ms per frame, set 331 370 439 456
  • Before 150k there is no difference. At 150–175k CPU per frame is 8 % lower, but fps overlap. At 175–200k every set
    run is faster than every unset run (+11 % fps, −10 % CPU per frame). The lock waits grow as the load goes on, so the
    gain shows only late.
  • No device removal in the eight runs; commit the same.
  • On a local tree with startup: opt-in guarded write of the view token in note_view #3097 and startup: opt-in content view tokens so view records can be freed #3377 merged (fewer view records), two pairs gave a larger gain: both set runs faster
    than both unset runs in every block from 100k to 200k, fps +6 to +21 %, CPU per frame −8 to −19 %. That tree is not
    this head; the numbers are given for context only.

Lock counters (local measurement build on the tree with #3097 and #3377, one run each, ~100k–175k blocks):

unset set
threads waiting on the four lookups' lock 0.42 0.03
threads waiting on locks in total 1.18 0.77
wait per view creation / descriptor copy (µs) 4.1 / 3.5 3.5 / 3.0

What remains is view creation and descriptor copies waiting on each other (~0.65 threads). Not covered: other
adapters, hosts with more cores, Wine.

AI-assisted: the change, tests and this description were written with Claude; I ran the startup comparison.

🤖 Generated with Claude Code

… on note_lock

X2W_D3D12_TABLE_LOCK=1 gives resource_infos, the slot 133 placement index and heap_infos their own
lock. resource_info, placed_buffer_range, memory_srv_destination and resolve_title_table read only those
tables and take table_lock shared; every table writer takes note_lock, then table_lock. Without the
switch nothing changes.

startup_table_lock checks that the lookups return while note_lock is held exclusive, with the expected
answers, that a heap creation waits for table_lock, and that lookups racing heap and texture creation find
every new entry; the control variants run without the switch and must see the opposite waits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

startup: resource and heap lookups queue on note_lock behind every view creation and descriptor copy

1 participant