Repository navigation
xcl run: XCL_THREAD_SAMPLE samples every thread's CPU time and stacks - #3232
CharlyRien wants to merge 3 commits into
Conversation
|
On I measured 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
The reader labels the latter two captures truncated, but AI assistance: OpenAI Codex ran the bounded Windows measurements, constructed the files and drafted this report. |
…end record
The TSC rate the cycle times are converted with came only from the end record ('E'),
so a capture still being written, or one from a run that ended before the sampler
wrote it, showed every thread at 0.0 % of a core although its samples hold all the
cycle deltas (andrewDoing on xcl-dev#3232). Without the end record the rate is now taken from
the first and last sample's TSC over their log uptime, said as approximate; with fewer
than two timed samples the shares are said to be unavailable and the cycles are shown.
--selftest: the synthetic samples carry real TSC values, and the same file without its
end record gives the same ~91 % for the busy thread.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@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
AI use: Claude Code with Claude Opus 5.5 made the fix, ran the self-test and wrote this reply. |
XCL_THREAD_SAMPLE=<from s>,<period ms>,<count> (+ XCL_THREAD_SAMPLE_FILE): from log time <from>, every <period> ms, a thread of its own reads for every other thread of the process its cycle time since the previous sample (QueryThreadCycleTime: the CPU it used, exact) and, when it ran, its registers and the top 16 KiB of its stack. The thread is stopped only for GetThreadContext and that copy: no lock and no allocation while it is suspended. Frames are unwound afterwards from the copy with 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 (its last stack is written as "unchanged"). Everything goes to a binary file with the modules and the thread names; the log gets a start line and the totals. Off unless set. ETW and VTune need elevation; this needs nothing. The walk never reads outside the copy: before each RtlVirtualUnwind, frame_extent bounds what the step can read from the function's UNWIND_INFO (pushes, allocations, machine frame, the farthest save, chained and indirect entries, version 2 epilogue descriptors, plus the return address; unknown codes are refused). That span must lie in the copy from RSP and, with a frame register, from FrameReg - 16 x FrameOffset; otherwise the walk ends with the frame TRUNCATED (1). The copy starts at RSP & !15 in a 16-aligned buffer and the CONTEXT is 16-aligned: RtlVirtualUnwind restores XMM saves with aligned moves (a misaligned copy is an access violation in ntdll); a misaligned one gives RIP alone. eh::thread_name is pub(crate) for the thread names. Tests (no GPU): the switch's parsing; the file read back record by record; the frame extent of every unwind code (synthetic UNWIND_INFOs, chained and indirect entries, refusals); a real 64 KiB frame walked from a 16 KiB copy that ends at a PAGE_NOACCESS page stops with [RIP, TRUNCATED], and from the whole stack unwinds into its caller; a sampling of this process that finds a spinning thread running and a waiting one unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Per thread group (the thread's name, else its start address): CPU as a share of one core from the exact cycle times, the samples split into user code, a system call at work and a wait, the share of each module, and for the busiest groups (and always the thread that submits command lists) the top functions inclusive and self and the ntdll / win32u leaves. xcl.exe is named through its PDB (dbghelp via ctypes), other DLLs through the export table of the file at the recorded path. The title's own images are printed only as title.exe / title-dll<k> plus a 4 KiB page offset, never by name or path. --log takes an xcl.log of a run with XCL_ACTIVITY=1 and gives ms per frame from its presents. The sampler's TRUNCATED frame (1) is printed as the callers past the stack copy. --selftest writes a synthetic file over the real ntdll (and xcl.exe with its PDB when XCL_EXE or target/release has one) and checks the shares, the names, the waits and that no title name is printed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…end record
The TSC rate the cycle times are converted with came only from the end record ('E'),
so a capture still being written, or one from a run that ended before the sampler
wrote it, showed every thread at 0.0 % of a core although its samples hold all the
cycle deltas (andrewDoing on xcl-dev#3232). Without the end record the rate is now taken from
the first and last sample's TSC over their log uptime, said as approximate; with fewer
than two timed samples the shares are said to be unavailable and the cycles are shown.
--selftest: the synthetic samples carry real TSC values, and the same file without its
end record gives the same ~91 % for the busy thread.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
d5b5c66 to
8eb9a99
Compare
|
Rebased on Read, CI: the 4 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, AI use: rebase, test runs and this comment done with Claude Code (Claude Opus 5.5), checked by me. |
|
No open or merged PR adds a CPU-time/stack sampler. The closest is #2923 ( AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human. |
|
merge-check AI-written and posted by Foniass without a human check of each point. |
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 xcl run: XCL_THREAD_DUMP_AT unwinds a copy of each thread's stack #2923 (open) copy each thread's stack and unwind over the copy forXCL_THREAD_DUMP_AT(one dump), xcl run: XCL_THREAD_DUMP_AT unwinds a copy of each thread's stack #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; xcl run: thread dumps unwind a copy of each stopped thread's stack #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 xcl run: thread dumps unwind a copy of each stopped thread's stack #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