startup: opt-in table lock so resource and heap lookups stop queueing on note_lock #3391
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3391"
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?
Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-11-28T18:52:25Z.
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
Imported from R0Y00/Xbox2Windows. Originally posted by KelboLive on 2026-11-28T19:10:29Z.
Review: COMMENTED
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.
Imported from R0Y00/Xbox2Windows. Originally posted by KelboLive on 2026-11-28T20:29:36Z.
Review: APPROVED
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.
Imported from R0Y00/Xbox2Windows. Originally posted by KelboLive on 2026-11-28T21:56:59Z.
Review: APPROVED
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.
Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-29T07:04:59Z.
Review: COMMENTED
Reviewed head
008720e4by reading the diff and the head sources; nothing was built or run here. The locking is correct. Two gaps in the test.git merge-tree --write-tree origin/main 008720e4exits 0 (main212d4b5f).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.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.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.TableWriteLockdoes nothing (if (on_) AcquireSRWLockExclusive(&table_lock);) andTableReadLocktakesnote_lockshared (table_lock_on() ? &table_lock : ¬e_lock), as main does.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.expect(!held.placed_owner, ...)(tests/startup_table_lock.cpp:163) also passes ifplaced_buffer_rangealways returns an empty result.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.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.Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-11-29T07:36:42Z.
Thanks. Both gaps closed in
7fa07f67(merged withmaininbf18c942):table_lockhold (note_heap,note_resourcevia slot 76,publish_placed_bufferandretire_placementvia a slot 133 buffer): ~2000 ms with the switch, 0 ms in the control.prpreset builds and runsstartup_table_lock_(control_)?warp.Mutants: dropping the lock from
note_resource,retire_placementorpublish_placed_buffer, or an always-emptyplaced_buffer_range, each fails only its own check.Release:
startup_table_lock_*4/4; wider families 112/112 with the switch unset and set.Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-29T07:50:35Z.
Review: APPROVED
Re-reviewed head
bf18c942(7fa07f67plus a merge of main212d4b5f) 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:eab7180cconflicts inCMakePresets.jsononly; keep both sides.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::232):note_heap;:234):note_resource;:248):publish_placed_buffer. Thenote_resourcecall in slot 133 (d3d12_xs_device.cpp:5840) is on the texture branch only;Reset()(:258):release->retire_placement(d3d12_xs_device.cpp:1074).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.bf18c942has the same tree asgit merge-tree --write-tree 7fa07f67 212d4b5f.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.Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-11-29T07:57:33Z.
Merged
main(#3429) inb2140b65. The one conflict was theprpreset, where both PRs add their tests; bothkept, and the filter selects both sets.
Release:
startup_table_lock_*4/4; wider families 116/116 with the switch unset and set.Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-29T08:06:31Z.
Review: APPROVED
Re-reviewed head
b2140b65(merge of maineab7180cintobf18c942) by reading only, nothing built or run; this approval replaces the one onbf18c942.git show --remerge-diff b2140b65shows only the twoCMakePresets.jsonconflict hunks, both resolved by keeping both sides (startup_table_lockappended after #3429'sprbuild targets,startup_table_lock_(control_)?warpplaced after #3429's inline entries in theprtest filter); every other path is git's automatic merge, so the change against main is otherwise the one reviewed onbf18c942(same 8 files).git merge-tree --write-treeofeab7180candb2140b65is clean, and the checks onb2140b65show the Windows build and test, fixture and Python jobs passing, with the /O2 job skipped.Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-11-29T08:17:26Z.
Merged
main(#3377) ina306c285. The conflicts were the additions both PRs make ind3d12_capture.inlanddocs/startup-runtime.md(a switch row and a section each); both kept. The change againstmainis the same as onb2140b65apart from theprpreset lines next to #3429's.Release:
startup_table_lock_*4/4,startup_view_content*5/5; wider families 121/121 with the switch unset and set.Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-29T08:31:16Z.
Review: APPROVED
Re-reviewed head
a306c285(merge of main7dc168a3, the #3377 merge, intob2140b65) by reading only, nothing built or run; this approval replaces the one onb2140b65.git show --remerge-diff a306c285shows conflicts in two files,docs/startup-runtime.md(the switch row and the section) andsrc/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 a306c285andgit diff -U0 eab7180c b2140b65are identical apart fromindexand 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 namingresource_infos,heap_infos, the placement index ortable_lock.git merge-tree --write-treeof main740ffdf1anda306c285is clean, and the checks ona306c285show the Windows build and test, fixture and Python jobs passing, with the /O2 job skipped.