Repository navigation
startup: opt-in table lock so resource and heap lookups stop queueing on note_lock - #3391
Conversation
… 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>
KelboLive
left a comment
There was a problem hiding this comment.
LGTM — locked down and reviewed.
Splits note_lock into a separate table_lock for resource_infos, placement index, and heap_infos. Four lookups now take table_lock shared instead of note_lock shared (opt-in via X2W_D3D12_TABLE_LOCK=1). Lock ordering consistent: writers acquire note_lock→table_lock exclusive; lookups hold only table_lock shared. Never-held note_lock code can't see stale tables since nothing removes table entries.
Strengths: real measured gains (8% CPU at 150k+, 11% FPS at 175k+), lazy opt-in via env var, mutant test for writer table_lock skip, 114 regression tests with both switch states, excellent documentation with fixture breakdown.
One note: resource_infos never removes entries (confirmed from usage), so reader-writer model is safe even as the map grows.
Ready to land.
KelboLive
left a comment
There was a problem hiding this comment.
LGTM — locked down and reviewed.
Splits note_lock into a separate table_lock for resource_infos, placement index, and heap_infos. Four lookups now take table_lock shared instead of note_lock shared (opt-in via X2W_D3D12_TABLE_LOCK=1). Lock ordering consistent: writers acquire note_lock→table_lock exclusive; lookups hold only table_lock shared. Never-held note_lock code can't see stale tables since nothing removes table entries.
Strengths: real measured gains (8% CPU at 150k+, 11% FPS at 175k+), lazy opt-in via env var, mutant test for writer table_lock skip, 114 regression tests with both switch states, excellent documentation with fixture breakdown.
One note: resource_infos never removes entries (confirmed from usage), so reader-writer model is safe even as the map grows.
Ready to land.
KelboLive
left a comment
There was a problem hiding this comment.
LGTM — locked down and reviewed.
Splits note_lock into a separate table_lock for resource_infos, placement index, and heap_infos. Four lookups now take table_lock shared instead of note_lock shared (opt-in via X2W_D3D12_TABLE_LOCK=1). Lock ordering consistent: writers acquire note_lock→table_lock exclusive; lookups hold only table_lock shared. Never-held note_lock code can't see stale tables since nothing removes table entries.
Strengths: real measured gains (8% CPU at 150k+, 11% FPS at 175k+), lazy opt-in via env var, mutant test for writer table_lock skip, 114 regression tests with both switch states, excellent documentation with fixture breakdown.
One note: resource_infos never removes entries (confirmed from usage), so reader-writer model is safe even as the map grows.
Ready to land.
Leyukaka
left a comment
There was a problem hiding this comment.
Reviewed head 008720e4 by reading the diff and the head sources; nothing was built or run here. The locking is correct. Two gaps in the test.
- Merge:
git merge-tree --write-tree origin/main 008720e4exits 0 (main212d4b5f). - Writers: every change to
resource_infos, the placement index (by_start,max_width) andheap_infosis in four functions, each takingnote_lockexclusive and thentable_lock:note_resource,d3d12_capture.inl:385-386:x2w_startup::ExclusiveNoteLock guard(note_lock);thenTableWriteLock tables;retire_placement,:395and:397:AcquireSRWLockExclusive(¬e_lock);thenTableWriteLock tables;note_heap,:452and:454: same pair.publish_placed_buffer,d3d12_xs_device.cpp:5766: the getter[&tables]() -> auto& { tables.emplace(); return resource_infos(); }is the first call afterExclusiveNoteLock guard(lock);(placement_publish.h:34,:36), beforetry_emplace,by_start.emplaceand themax_widthupdate.
- Two other writers hold
note_lockonly and touch nothing the four lookups read:retire_drop_heap_viewswritesh.shadow[i] = 0;(d3d12_xs_device.cpp:6063) and no otherHeapInfofield; the retire drop callsresource_address_index().erase(address, drop.wrapper);(:6316). None of the four lookups readsshadowor the address index. - Order:
note_lock->table_lockin all four writers. The lookup bodies (d3d12_capture.inl:419-441,:445-447,d3d12_memory_srv.inl:129-139,d3d12_draw.inl:3630-3638) take no other lock and call no other lookup. TheTableWriteLockscopes call onlystore_indexed,index_resource, container operations andVirtualAlloc, so no lookup runs under the write lock. - Switch unset:
TableWriteLockdoes nothing (if (on_) AcquireSRWLockExclusive(&table_lock);) andTableReadLocktakesnote_lockshared (table_lock_on() ? &table_lock : ¬e_lock), as main does. - Test gap 1: part 4 (
tests/startup_table_lock.cpp:201-217) times one heap creation, so it coversnote_heaponly. WithTableWriteLockremoved fromnote_resource,retire_placementorpublish_placed_buffer, every check still passes, by reading: part 3 is described in the file as "A smoke test: a writer that skipped table_lock passed it five times out of five." Timing a committed texture creation (slot 76, which reachesnote_resource) againsttable_lockthe same way would covernote_resource: expect >= 1000 ms, control < 1000 ms. It needs its own hold, or a second thread during the same hold, because the heap creation already waits out the 2 s. - Test gap 2: the fixture creates no slot 133 placed buffer, so
expect(!held.placed_owner, ...)(tests/startup_table_lock.cpp:163) also passes ifplaced_buffer_rangealways returns an empty result. - CI: pull requests run
cmake --build --preset prandctest --preset pr(windows.yml:490,:493).startup_table_lockis not in theprbuild targets (CMakePresets.json:28-38) andstartup_table_lock_*does not match theprtest filter (:51), so CI neither builds nor runs the new tests. - Overlap with #3377:
git merge-tree --write-tree 008720e4 <#3377 head 603228b9>exits 1 with content conflicts ind3d12_capture.inl(one hunk, both add a block after theviews_notedline) anddocs/startup-runtime.md(two hunks: a row in the switch list and a section). Each side only adds text there; keeping both resolves them.
…r, runs in the pr preset Part 4 times note_heap, note_resource (slot 76 texture), publish_placed_buffer (slot 133 buffer) and retire_placement (its final Release), each under its own table_lock hold. The placed buffer is found at its address and last byte, and not after its Release. The pr preset builds startup_table_lock and runs its warp tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks. Both gaps closed in
Mutants: dropping the lock from Release: |
Leyukaka
left a comment
There was a problem hiding this comment.
Re-reviewed head bf18c942 (7fa07f67 plus a merge of main 212d4b5f) by reading the diff and the head sources; nothing was built or run here. Both test gaps are closed. A rebase onto main is needed: eab7180c conflicts in CMakePresets.json only; keep both sides.
- Writers: part 4 (
tests/startup_table_lock.cpp:205-261) runs each writer under its own 2 s shared hold oftable_lockand expects >= 1000 ms with the switch, < 1000 ms in the control.TableWriteLockis taken in four places only (d3d12_capture.inl:386,:397,:454,d3d12_xs_device.cpp:5765), and each timed call reaches exactly one of them:- slot 14 heap creation (
:232):note_heap; - slot 76 texture (
:234):note_resource; - slot 133 buffer (
:248):publish_placed_buffer. Thenote_resourcecall in slot 133 (d3d12_xs_device.cpp:5840) is on the texture branch only; - the buffer's last
Reset()(:258):release->retire_placement(d3d12_xs_device.cpp:1074).
- slot 14 heap creation (
- Slot 133: a 64 KiB buffer is created at a
VirtualAllocaddress.placed_owneris expected at its first byte (:253) and its last byte (:254), and not after the release (:261). An always-emptyplaced_buffer_rangefails:253. prpreset:startup_table_lockadded to the build targets (CMakePresets.json:37) andstartup_table_lock_(control_)?warpto the test filter (:51). No other preset change.- Merge commit:
bf18c942has the same tree asgit merge-tree --write-tree 7fa07f67 212d4b5f. - Conflict:
git merge-tree --write-tree origin/main bf18c942exits 1 with one file,CMakePresets.json, two hunks: theprbuild target list and theprtest filter, where #3429 appended its own targets and tests on the same lines.docs/startup-runtime.md,d3d12_draw.inlandd3d12_xs_device.cppmerge without conflict. Insrc, the only lock line #3429 adds is a sharednote_lockaroundview_records()inx2w_d3d12_token_texture; it adds notable_lockuse and no write to the three tables.
Keeps both PRs' pr preset entries (build targets and test filter). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Merged Release: |
Leyukaka
left a comment
There was a problem hiding this comment.
Re-reviewed head b2140b65 (merge of main eab7180c into bf18c942) by reading only, nothing built or run; this approval replaces the one on bf18c942.
git show --remerge-diff b2140b65 shows only the two CMakePresets.json conflict hunks, both resolved by keeping both sides (startup_table_lock appended after #3429's pr build targets, startup_table_lock_(control_)?warp placed after #3429's inline entries in the pr test filter); every other path is git's automatic merge, so the change against main is otherwise the one reviewed on bf18c942 (same 8 files). git merge-tree --write-tree of eab7180c and b2140b65 is clean, and the checks on b2140b65 show the Windows build and test, fixture and Python jobs passing, with the /O2 job skipped.
Keeps both PRs' additions in d3d12_capture.inl and docs/startup-runtime.md (content tokens first, then the table lock). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Merged Release: |
Leyukaka
left a comment
There was a problem hiding this comment.
Re-reviewed head a306c285 (merge of main 7dc168a3, the #3377 merge, into b2140b65) by reading only, nothing built or run; this approval replaces the one on b2140b65.
git show --remerge-diff a306c285 shows conflicts in two files, docs/startup-runtime.md (the switch row and the section) and src/startup_stubs/d3d12_capture.inl, each resolved by keeping both sides with the text unchanged: #3377's view-content-token text first, the table-lock text after it. git diff -U0 7dc168a3 a306c285 and git diff -U0 eab7180c b2140b65 are identical apart from index and hunk header lines (same 8 files, +454/-25), so the change against main is the one already reviewed. The source diff of #3377 (eab7180c..7dc168a3) has no line naming resource_infos, heap_infos, the placement index or table_lock.
git merge-tree --write-tree of main 740ffdf1 and a306c285 is clean, and the checks on a306c285 show the Windows build and test, fixture and Python jobs passing, with the /O2 job skipped.
…ches to the shared run presets X2W_D3D12_INLINE_DESCRIPTORS (R0Y00#3429), X2W_D3D12_VIEW_CONTENT_TOKENS (R0Y00#3377) and X2W_D3D12_TABLE_LOCK (R0Y00#3391) join base.env. The base why gives the 360 s A/B runs with native shaders only and with -Translate. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ches to the shared run presets X2W_D3D12_INLINE_DESCRIPTORS (R0Y00#3429), X2W_D3D12_VIEW_CONTENT_TOKENS (R0Y00#3377) and X2W_D3D12_TABLE_LOCK (R0Y00#3391) join base.env. The base why gives the 360 s A/B runs with native shaders only and with -Translate. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ches to the shared run presets (#3550) X2W_D3D12_INLINE_DESCRIPTORS (#3429), X2W_D3D12_VIEW_CONTENT_TOKENS (#3377) and X2W_D3D12_TABLE_LOCK (#3391) join base.env. The base why gives the 360 s A/B runs with native shaders only and with -Translate. Co-authored-by: lukaslw-lab <291379150+lukaslw-lab@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Change
Fixes #3390 (resource and heap lookups queue on
note_lockbehind every view creation and descriptor copy).X2W_D3D12_TABLE_LOCK=1(opt-in): the resource table (resource_infos), the slot 133 placement index and thedescriptor heap table (
heap_infos) get their own reader-writer lock,table_lock.note_lockguards the view records and descriptor shadows as well as these tables. View creation anddescriptor 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.table_lockshared. Every table writer (note_resource,note_heap,retire_placement,publish_placed_buffer) takesnote_lockand thentable_lock, both exclusive, so code thatalready holds
note_lockstill sees fixed tables and needs no change. The lock order is the same everywhere.retire_placement(a writer) changes.resource_infokeeps returning a pointer into the map as before (entries arenever removed).
publish_placement(placement_publish.h) takesnote_lockitself;table_lockis taken in its record getter,which it calls first with
note_lockheld, and released after it returns. The header is unchanged.note_lockshared and the writers take no second lock.x2w_d3d12_table_lock_probeandx2w_d3d12_table_lock_hold; switch row and section "D3D12 table lock" indocs/startup-runtime.md.Verification
Windows 10 22H2, i7-4790K, RTX 3080 Ti (NVIDIA 32.0.16.1047) and WARP; release; commit
008720e4onmaind418f70e.startup_table_lock_{warp,hardware}:note_lockexclusive, with the expected answers;table_lockwhilex2w_d3d12_table_lock_holdholds it shared for 2 s (measured2000 ms);
entry is found afterwards.
startup_table_lock_control_{warp,hardware}(switch unset): the lookups must wait fornote_lockand the heap creation must return at once (measured 0 ms); every other expectation is the same.
table_lock→startup_table_lock_{warp,hardware}fail witha 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 asmoke test.
startup_table_lock_*, no skips.startup_view_,placed_,resource_,address_buffer_srv_,table_alias_,memory_srv,formatted_view,direct_index_,stream_draw_,copied_vertex_srv_,frame_copy_,descriptor_), switch unsetand 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:
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.
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):
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