xcl run: XCL_THREAD_SAMPLE samples every thread's CPU time and stacks #3232
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3232"
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 ivall/xcl. Originally posted by CharlyRien on 2026-11-28T23:27:39Z.
A diagnostic, off unless set:
XCL_THREAD_SAMPLE=<from s>,<period ms>,<count>samples where every thread of the process spends its CPU, without a debugger, ETW or administrator rights. We used it to find which XCL threads and functions take the time while a title runs slowly; ETW and VTune both need elevation, andXCL_THREAD_DUMP_ATgives one snapshot, not where the time goes.From log time
<from>, every<period>ms, a thread of its own reads for every other thread:QueryThreadCycleTime: the CPU it used, exact);GetThreadContextand that copy, with no lock and no allocation while it is suspended.The frames are unwound afterwards from the copy (
RtlVirtualUnwind): RIP and up to 7 return addresses. A thread whose cycle time did not move and that waits in ntdll / win32u is not stopped again (written as "unchanged"). Everything goes to a binary file (XCL_THREAD_SAMPLE_FILE, elsexcl-thread-sample.binin the temp folder) with the modules and the thread names.xcl.loggets a start line and the totals.scripts/thread_sample.pyreads the file.What is in it
First commit, the sampler:
crates/xcl/src/thread_sample.rs(new): the switch, the sampling thread, the file format (documented at the top of the file), the unwinding over the copy.RtlVirtualUnwindstep,frame_extentbounds what the step can read from the function'sUNWIND_INFO: pushes, allocations, machine frame, the farthest save, chained and indirect entries, version 2 epilogue descriptors, and the return address. Unknown codes are refused. That span must lie in the copy, from RSP and, with a frame register, fromFrameReg - 16 × FrameOffset. Otherwise the walk ends with aTRUNCATED(1) frame.RSP & !15in a 16-aligned buffer, and the CONTEXT is 16-aligned.RtlVirtualUnwindrestores XMM saves with aligned moves, so a misaligned copy faults inside ntdll. A misaligned one gives RIP alone.host.rs:thread_sample::start_from_env()at start;lib.rs: the module;eh.rs:thread_nameispub(crate);README.md: the switch.UNWIND_INFOs, chained and indirect entries, refusals);PAGE_NOACCESSpage stops with[RIP, TRUNCATED], and from the whole stack unwinds into its caller;Second and third commits, the reader (
scripts/thread_sample.py, Python 3, standard library only):title.exe/title-dll<k>plus a page offset, never by name or path.--log <xcl.log>(a run withXCL_ACTIVITY=1) gives ms per frame from its presents.--selftestwrites a synthetic file over the real ntdll and checks the shares, the names, the waits, and that no title name is printed.+1,690 / −1 in 6 files: 1,071 lines for the sampler (its tests among them), 605 for the reader, 15 elsewhere. Over the 400-line target: the sampler and its unwinding only make sense together, and the reader is a separate commit. Nothing runs unless
XCL_THREAD_SAMPLEis set.Measured / Read / Deduced
XCL_THREAD_SAMPLE(12,000 samples, 209 threads): 183,848 stops, 82 µs each on average, the longest 264 ms; the sampler used 2.2 ms per sample. These are that version's own totals line.RtlVirtualUnwind. In one of them, the frame being unwound was a 146 KiB title frame, while the copy is 16 KiB. That is whatframe_extentnow bounds. Separately, a unit test died in ntdll on a frame that saves xmm6, from the alignment above. Both are covered by the tests in this PR; neither fix has had a title run yet.mainsince) and #2923 (open) copy each thread's stack and unwind over the copy forXCL_THREAD_DUMP_AT(one dump), #2923 with a bound likeframe_extent(itsframe_reach). This PR samples continuously and adds the cycle times; the two could share the unwinding later.Tested
8eb9a99onmain7a649b0(rebased 29/11, no conflict; #253 is merged onmainsince the last push, itseh.rsstack copy forXCL_THREAD_DUMP_ATsits next to this PR'sthread_namechange without touching it). RTX 3090 24 GB (Resizable BAR off), driver 616.56, Ryzen 9 5950X, 32 GB RAM, Windows 11 22631; Rust 1.100.0, release tests; Python 3.12.10.XCL_ADAPTER=warp;XCL_REQUIRE_GPUunset; nothing here uses the GPU):cargo test --release -p xcl --lib -- thread_sample eh:: shaders::pipeline_library::tests shaders::warm_library::tests --test-threads=4: 21 passed, 7 failed: the 5thread_sampletests and 12eh::tests passed (with #253's stack copy now onmain). The 7 failures are 6shaders::pipeline_library::testsandshaders::warm_library::tests::cold_publish_makes_an_unchanged_corpus_reusable:CreateComputePipelineStatereturns E_INVALIDARG on this machine's WARP, which has no SM 6.6 (the other 6 then fail on the poisoned lock or the same error). That is a local artefact of a WARP run here, not the CI failure (an entry count, 3 against 2).main7a649b0: the same 7 failures with the same errors, nothing else failed (408 passed, 7 failed, 19 ignored for the union of the filters of our five PRs).python -I scripts/thread_sample.py --selftest: SELFTEST PASSED, 12 checks, the no-end-record ones among them (without xcl.exe's PDB next to it, so its PDB checks were skipped).main7a649b0(no new one).XCL_REQUIRE_GPU=mesh):cargo test --release -p xcl --lib -- thread_sample:: --test-threads=1: 5 passed, 0 failed, 0 ignored: the 5thread_sampletests, the sampling of this process among them.d5b5c66. Onmain7a649b0main's own CI fails 8 tests, all inhle::d3d12x::title_textures::tests(run 46261846646): a red Build and test on this head with those 8 ismain's.Regression run
Same machine as under Tested, same title, 60 s each (the
scripts/smoke.ps1default), both throughmaind52ff48'sscripts/smoke.ps1,XCL_PREPARE=0, the same scripted A presses (XCL_PRESS_A_AT),XCL_THREAD_SAMPLEunset. Each run started from its own copy of the same shader cache, made by anothermaincommit, so cold for both. The head is onmain7a649b0; themainrun is ofmaind52ff48(the newestmaintoday, 244 commits later), not of the head's base: the check asks for a commit ofmain.maind52ff48:this head
8eb9a99(main7a649b0+ this PR's three commits):main's 517 (110.3 %; the check wants at least 80 %), peak commit 15,629 MB against 15,968 MB (97.9 %; at most 125 %). Last three 2 s slices: head 61 60 61,main61 60 61 presents. 60 s covers the start of the title, not a long stay in its world.maind52ff48, so the difference between the two runs includes those commits, not only this PR.Not tested
XCL_THREAD_SAMPLEset (the runs above were of the earlier version).AI use
AI use: the sampler, its fixes, the reader, the rebase, the test and smoke runs and this description were done with Claude Code (Claude Opus 5.5), checked and posted by me.
🤖 Generated with Claude Code
Imported from ivall/xcl. Originally posted by andrewDoing on 2026-11-28T23:32:20Z.
On
a8cd5921, a capture without a complete final E record reports a busy thread as 0.0% CPU. The sample rows can still contain all their cycle deltas. That matters when inspecting a still-running capture or a run that exited before finalization.I measured
QueryThreadCycleTime, wall time (QPC), TSC andGetThreadTimesfor three bounded 1.5 s spinner controls on a Windows Ryzen 7 5700X3D. TSC calibrated to 3.000 GHz; cycles/TSC gave 99.0–99.4% of a core and kernel+user thread time gave about 99.0%. Three sleeping controls gave about 0.001% by cycles.For an exact reader comparison, I put the first measured spinner and sleeper cycle deltas into the same four synthetic S records covering 1.5 s. Those records were identical in all three files. Running this PR's unchanged
scripts/thread_sample.pygave:The reader labels the latter two captures truncated, but
cpu(g)converts the missing final calibration into a numeric zero for every group. Reporting raw cycles and “CPU percentage unavailable” would retain the useful evidence. An explicitly approximate fallback could also use valid separated sample TSC/uptime pairs. No title process or stack unwinding was involved in this reader control.AI assistance: OpenAI Codex ran the bounded Windows measurements, constructed the files and drafted this report.
Imported from ivall/xcl. Originally posted by CharlyRien on 2026-11-28T23:58:37Z.
@andrewDoing thanks, you are right: the reader took the TSC rate from the end record only, so a capture without it showed 0.0 % for every thread.
Fixed in
d5b5c66(reader only,scripts/thread_sample.py):--selftestnow runs the same synthetic file without its end record and expects the same ~91 % for the busy thread. It passed here (RTX 3090 machine, Ryzen 9 5950X, Windows 11 22631; Python only, no title).AI use: Claude Code with Claude Opus 5.5 made the fix, ran the self-test and wrote this reply.
Imported from ivall/xcl. Originally posted by CharlyRien on 2026-11-29T13:57:39Z.
Rebased on
main7a649b0; head8eb9a99(no conflict). Measured, CPU only (cargo test --release -p xcl --lib -- thread_sample eh::+ the library tests): 21 passed, 7 failed; the 5thread_sampleand 12eh::tests passed (#253 now onmain), andpython -I scripts/thread_sample.py --selftestpassed. The 7 failures are the 6shaders::pipeline_library::testsandwarm_library::tests::cold_publish_makes_an_unchanged_corpus_reusable, the same 7 with the same errors onmain7a649b0here: E_INVALIDARG fromCreateComputePipelineStateon this machine's WARP (no SM 6.6). Deduced: a local artefact of WARP here, not the CI failure mode. No title run.Read, CI: the 4
pipeline_library/warm_libraryfailures of our previous CI runs weremain's own on227d2cb(run 46201186646) and passed onmainsincefa20459;main7a649b0now fails 8hle::d3d12x::title_textures::testsin CI (run 46261846646), so a red Build and test with those 8 ismain's. "PR description" stays red only for thexcl-smokepair of the new head, which needs a title run (not made: CPU only this time).Description refreshed (Tested, Regression run, AI use).
Stack: RTX 3090 24 GB (Resizable BAR off), driver 616.56, Ryzen 9 5950X, 32 GB RAM, Windows 11 22631; Rust 1.100.0, release tests,
XCL_ADAPTER=warp,XCL_REQUIRE_GPUunset.AI use: rebase, test runs and this comment done with Claude Code (Claude Opus 5.5), checked by me.
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T09:46:13Z.
Triage (#1607) – head
8eb9a99No open or merged PR adds a CPU-time/stack sampler. The closest is #2923 (
XCL_THREAD_DUMP_ATunwinds a copy of each thread's stack), which is a one-shot dump ineh.rsand shares no code withthread_sample.rs.In
thread_sample.rs:run, the target thread is suspended only forGetThreadContextand a stack copy into a preallocated buffer (no lock or heap while it is suspended), andwalkchecks each unwind step against the copy before callingRtlVirtualUnwind. No problems found in the diff. At +1712 lines (about 1070 in Rust), it is well over the 800-line size guideline.Recommendation: keep open.
AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T10:12:56Z.
merge-check
base:
493dbd3dd0head:
8eb9a992f8merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2905 passed, 0 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: none
review: read the whole diff (+1712/-1: 684 lines of sampler, 386 of tests, a 627-line Python reader). With
XCL_THREAD_SAMPLEunset nothing runs: one environment read (host.rs~263), no thread, hook, allocation or symbol loading. Findings: (1) Size and duplication. "The sampler and its unwinding only make sense together" doesn't cover the reader, which is plainly separate. The unwinder (thread_sample.rs~201-354,walk/frame_extent) is a second, stricter copy of main'seh.rscopy_stack/unwind(~872-931) and of open #2923'sframe_reach. It should replaceeh::unwindas a shared helper rather than sit beside it. A split: the shared copy-and-unwind helper with its tests (agreed with #2923); the sampler, its test and README;Scripts:the reader with its self-test asscripts/test_thread_sample.py. (2) The crash fix hasn't run in a title. The description reports 4 of 85 sampler runs dying inRtlVirtualUnwind, and neither fix has had a title run with the sampler on. A fault on the sampler thread ends the title, since nothing catches it. (3) The stack is copied with a plain memory copy (~581) whereeh.rsusesReadProcessMemory, which fails cleanly. A fault would hit while the other thread is suspended. Low risk after theVirtualQuerycheck, butcopy_stackshould be reused. (4) Accuracy: a thread last seen waiting in ntdll that used under 1M cycles (~45, ~553) is marked unchanged for up to 20 samples, so short bursts are charged to the old wait stack.counthas no upper bound (~70), and the thread list refreshes every 100 samples (~514), so short-lived threads are missed. (5) The live test (~989) suspends every thread of the test binary, other tests included, and asserts timing (spin_stopped >= 140), so it may flake under load. The reader's--selftestisn't run by any harness. (6) Minor:XCL_THREAD_SAMPLEis missing fromlegacy_warm_settings' ignore list (shaders.rs~68). Fine: while a thread is suspended the sampler only callsGetThreadContext,VirtualQueryand the copy, with buffers allocated beforehand and everything else done afterResumeThread, the same rule aseh.rs(#106), so no heap or loader deadlock.frame_extentbounds its reads (machine frames, chained entries, v2 epilogues) and has tests. dbghelp is used only in the offline reader, and there are no new dependencies.performance: no smoke run (the default path doesn't reach this change); not measured. The sampler is off unless
XCL_THREAD_SAMPLEis set, so no smoke run; the live sampling test passed here under the full suite's load.verdict: not yet: 1,712 lines with the reader plainly separable, and an unwinder that duplicates
eh.rs/ #2923 instead of replacing them; split it (shared helper, sampler, reader) and run a title with the sampler on to show theRtlVirtualUnwindcrash fix holdshardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5
AI-written and posted by Foniass without a human check of each point.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.