profile: sample Wine threads without QueryThreadCycleTime #53

Open
github-bot wants to merge 1 commit from github-bot/gh-53 into win10-test AGit
Owner

@⁠zocram4cc on GitHub · 2026-11-25 23:31 UTC · original
zocram4cc:up/profile-wine → win10-test · +119 −20 · 2 files · 1 commits · open

Base: win10-test. beta has no profile.rs (checked at 78ad571). The profiler came in with f85101b (fxceless) on win10-test, so this PR targets win10-test.

Problem

Under Proton, VICTORY_PROFILE=1 records no samples: every look at every thread fails. Measured before the fix (see the commit message): 19,899 looks, 19,899 failed, 0 samples per thread. The red run below shows the same at test level: 904 looks per thread, 904 failed, 0.0 ms on a processor.

Root cause

  1. QueryThreadCycleTime is a stub in Wine. dlls/kernelbase/thread.c:444 (ValveSoftware/wine branch experimental_11.0) logs a FIXME once, calls SetLastError(ERROR_CALL_NOT_IMPLEMENTED) and returns FALSE. Sampler::look counted that failure as a missed look and dropped it, so every look was lost.
  2. Wine's PE exports have no size. dbghelp (dlls/dbghelp/symbol.c, symt_find_symref_at) gives a size-less symbol the length of its type, or 1 byte, so an export names only its own address. A frame inside an exported system function came out as ntdll.dll+0xea80, not as the function, so kernel waits were not recognised.

Fix

  • Sampler::new probes QueryThreadCycleTime once and keeps the result as exact.
  • Exact (Windows): unchanged. Same counter, same missed accounting, same per-look cap at the thread's weight.
  • Stub (Wine): cycles_of takes the thread's kernel and user time from GetThreadTimes and converts it to cycles at cycles_per_us. Wine computes that from /proc/<pid>/task/<tid>/stat in clock ticks (dlls/ntdll/unix/thread.c, get_thread_times, sysconf(_SC_CLK_TCK)).
  • Without the exact counter the per-look CPU share is not capped at the thread's weight, because a stand-in tick can span several looks. Sums over a run stay right.
  • Naming: when a frame's own address names nothing, the function's start is looked up (from its unwind data) and used only if an export starts exactly there.

Windows behaviour

Not run on Windows. From the code:

  • QueryThreadCycleTime succeeds there, so exact is true: cycles_of takes the exact branch (same call, same result as before) and the per-look cap applies as before. The stand-in code is not reached.
  • The naming fallback runs only when a frame's own address names nothing. On Windows the first lookup should already name the containing export, so names should not change. [deduction, not run]
  • One extra QueryThreadCycleTime call in Sampler::new.

Test

New: profile::tests::a_blocked_thread_is_sampled_in_a_kernel_wait. A thread spins and another waits on a lock, both followed for 1 s. The spinner must be sampled and mostly running; the blocked thread must be sampled and at least 95 % waiting in the Wide role.

  • Build: cargo xwin build --profile diag -p victory --features diagnostics --target x86_64-pc-windows-msvc passes (exit 0) on this branch.
  • Test binary: cargo xwin test --profile diag -p victory-gpu --features diagnostics --target x86_64-pc-windows-msvc --no-run (the profile tests' own doc uses --profile diag).
  • Run under Proton experimental-11.0-20261112: proton runinprefix <victory_gpu-*.exe> --exact profile::tests::a_blocked_thread_is_sampled_in_a_kernel_wait --test-threads 1. Keep the .pdb beside the test exe: the profiler names our own functions from it.

Red (fix reverted: profile.rs at the win10-test base, new test kept), exit 101. Legend lines omitted:

red output
ntsync: up and running.

running 1 test
test profile::tests::a_blocked_thread_is_sampled_in_a_kernel_wait ... FAILED

failures:

---- profile::tests::a_blocked_thread_is_sampled_in_a_kernel_wait stdout ----

thread 'profile::tests::a_blocked_thread_is_sampled_in_a_kernel_wait' (240) panicked at crates/victory-gpu/src/profile/tests.rs:121:28:
no Submit in
VIctory profile, process 216
sampled 1.0 s in 904 looks (1000 asked a second, 1.11 ms between looks, 0 late); 0 frames presented meanwhile (none: the tables are per second of sampling)
2 threads followed (2 still running); a thread looked at stood still under 0 us half the times, under 0 us 99 in 100, 0 us at longest, 0.0 us on average, 0 times; 0 call sites kept
a thread may stand still for looks 3.0 % of its time (VICTORY_PROFILE_BUDGET): one whose looks cost more is looked at every 2nd, 3rd... tick while it runs ("every" below); its samples stand for more time each, so the times stay right
the sampler's thread used 9.7 % of one processor
names of ours: from Z:\tmp\i19\red\victory_gpu-d0f9bd8e96167b37.pdb

THREADS
  role        thread  name, or where it started      ms a second on cpu % waiting %     looks not susp  failed     cut every  look us  mostly in (own time)
  submit         244  spins                                0.0      0.0       0.0       904        0     904       0     1      0.0
  wide           248  locks                                0.0      0.0       0.0       904        0     904       0     1      0.0

(this report took 114 ms to write; sampling stood still meanwhile)

note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    profile::tests::a_blocked_thread_is_sampled_in_a_kernel_wait

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 188 filtered out; finished in 1.19s

Green (this branch), exit 0:

ntsync: up and running.

running 1 test
test profile::tests::a_blocked_thread_is_sampled_in_a_kernel_wait ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 188 filtered out; finished in 17.97s

The 18 s is mostly Wine's dbghelp loading the 28 MB PDB on the first lookup [deduction: the same test without the PDB beside it finished in 1.2 s].

Sample report (green; profile::tests::the_sampler_finds_threads_and_writes_its_files, --nocapture, THREADS table as printed):

  role        thread  name, or where it started      ms a second on cpu % waiting %     looks not susp  failed     cut every  look us  mostly in (own time)
  pipelines      388  victory-pipelines-9               1000.0      0.0     100.0       302      301       0       0     2     86.5  ntdll!NtWaitForSingleObject (100 %)
  busy           392  at victory_gpu-d0f9bd...+0x51e450     166.5     95.0       0.0        51       31       0       0     2     79.6  victory_gpu-d0f9bd8e96167b37.exe+0x319d30 (100 %)

The pipeline thread's kernel wait is named; before the fix that thread had no samples at all. The busy thread's innermost frame is one of our functions, which stays unnamed (see known limits).

Whole profiler module (profile::tests, --nocapture): before, 6 passed, 4 failed, 1 ignored; with this change, 7 passed, 3 failed, 1 ignored. The four red failures are the new test and the three older tests below. Only the new test changes status.

Known limits

  • 10 ms ticks. Wine reports thread time in clock ticks (getconf CLK_TCK is 100 here: 10 ms). A thread that runs less than one tick between two looks reads as not having run. Per-look shares are coarse; sums over a run are right.
  • Clock cost. About 31.5 µs per stand-in read under Wine (our earlier Proton measurement, not repeated here). The sampler test at 500 Hz with two followed threads reports 78.6 % of one processor for the sampler's own thread. Under Wine use VICTORY_PROFILE_HZ=100 to 200 (default 1000). [deduction: at 100–200 Hz a look spans about one tick]
  • Three older tests still fail under Wine, on names of our own functions: time_goes_to_the_function_that_spent_it, deep_stacks_are_cut_not_lost, the_sampler_finds_threads_and_writes_its_files. With the PDB beside the exe, none of our profile_test_* free functions is named in the output (they show as victory_gpu-<hash>.exe+0x…), while closures in the same module are named. Green failures: inner 0 of …, profile_test_sleeps absent, profile_test_spin_inner absent from the folded stacks. Why Wine's dbghelp does not name these functions is not isolated here. They were already failing before this change, for another reason: with every look failing there were no samples to assert on (red: no Submit, missed looks, no PIPELINE WORKERS). They are not ignored in this PR.
  • Windows was not run. See above.
  • The victory-gpu test target does not compile on win10-test (d006c10). The test modules of indirect.rs (duplicate test functions and mod bench), descriptor/host.rs and pipeline/host.rs call APIs whose signatures changed or no longer exist: 42 compile errors. PR #37 (open, win10-test, "Repair indirect, overlap and depth-bounds GPU tests on win10-test") changes the same test files; this PR does not depend on it. For the runs above the three test modules were gated locally with #[cfg(any())]; that gate is not in this PR, and the branch builds as is.
  • fxceless/victory-fork#960 (open, by UwUCammie) addresses the same symptom with a broader change: it drops the CPU share when the counter is missing (every look takes the stack, "on cpu" reads 0), names system exports from the module's export table, and names inlined calls by source path. It reports time_goes_to_the_function_that_spent_it and the_sampler_finds_threads_and_writes_its_files passing under Wine 11.22. This PR keeps the CPU share on the GetThreadTimes stand-in, names only a function's start, and does not change inlined names. We did not run #960. Both edit profile.rs in the sampler and the naming code, so they will likely conflict.

Disclosure

AI-assisted: the investigation, the change and this description were produced with an AI coding assistant, and the builds and Proton runs above were run in that session. No game files, title data or rig settings are included; the runs execute the test binary only.

**@⁠zocram4cc** on GitHub · 2026-11-25 23:31 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/53) `zocram4cc:up/profile-wine` → `win10-test` · +119 −20 · 2 files · 1 commits · open **Base: `win10-test`.** `beta` has no `profile.rs` (checked at `78ad571`). The profiler came in with `f85101b` (fxceless) on win10-test, so this PR targets win10-test. ## Problem Under Proton, `VICTORY_PROFILE=1` records no samples: every look at every thread fails. Measured before the fix (see the commit message): 19,899 looks, 19,899 failed, 0 samples per thread. The red run below shows the same at test level: 904 looks per thread, 904 failed, 0.0 ms on a processor. ## Root cause 1. `QueryThreadCycleTime` is a stub in Wine. `dlls/kernelbase/thread.c:444` (ValveSoftware/wine branch `experimental_11.0`) logs a FIXME once, calls `SetLastError(ERROR_CALL_NOT_IMPLEMENTED)` and returns FALSE. `Sampler::look` counted that failure as a missed look and dropped it, so every look was lost. 2. Wine's PE exports have no size. dbghelp (`dlls/dbghelp/symbol.c`, `symt_find_symref_at`) gives a size-less symbol the length of its type, or 1 byte, so an export names only its own address. A frame inside an exported system function came out as `ntdll.dll+0xea80`, not as the function, so kernel waits were not recognised. ## Fix - `Sampler::new` probes `QueryThreadCycleTime` once and keeps the result as `exact`. - Exact (Windows): unchanged. Same counter, same `missed` accounting, same per-look cap at the thread's weight. - Stub (Wine): `cycles_of` takes the thread's kernel and user time from `GetThreadTimes` and converts it to cycles at `cycles_per_us`. Wine computes that from `/proc/<pid>/task/<tid>/stat` in clock ticks (`dlls/ntdll/unix/thread.c`, `get_thread_times`, `sysconf(_SC_CLK_TCK)`). - Without the exact counter the per-look CPU share is not capped at the thread's weight, because a stand-in tick can span several looks. Sums over a run stay right. - Naming: when a frame's own address names nothing, the function's start is looked up (from its unwind data) and used only if an export starts exactly there. ## Windows behaviour Not run on Windows. From the code: - `QueryThreadCycleTime` succeeds there, so `exact` is true: `cycles_of` takes the exact branch (same call, same result as before) and the per-look cap applies as before. The stand-in code is not reached. - The naming fallback runs only when a frame's own address names nothing. On Windows the first lookup should already name the containing export, so names should not change. [deduction, not run] - One extra `QueryThreadCycleTime` call in `Sampler::new`. ## Test New: `profile::tests::a_blocked_thread_is_sampled_in_a_kernel_wait`. A thread spins and another waits on a lock, both followed for 1 s. The spinner must be sampled and mostly running; the blocked thread must be sampled and at least 95 % waiting in the Wide role. - Build: `cargo xwin build --profile diag -p victory --features diagnostics --target x86_64-pc-windows-msvc` passes (exit 0) on this branch. - Test binary: `cargo xwin test --profile diag -p victory-gpu --features diagnostics --target x86_64-pc-windows-msvc --no-run` (the profile tests' own doc uses `--profile diag`). - Run under Proton `experimental-11.0-20261112`: `proton runinprefix <victory_gpu-*.exe> --exact profile::tests::a_blocked_thread_is_sampled_in_a_kernel_wait --test-threads 1`. Keep the `.pdb` beside the test exe: the profiler names our own functions from it. **Red** (fix reverted: `profile.rs` at the win10-test base, new test kept), exit 101. Legend lines omitted: <details><summary>red output</summary> ``` ntsync: up and running. running 1 test test profile::tests::a_blocked_thread_is_sampled_in_a_kernel_wait ... FAILED failures: ---- profile::tests::a_blocked_thread_is_sampled_in_a_kernel_wait stdout ---- thread 'profile::tests::a_blocked_thread_is_sampled_in_a_kernel_wait' (240) panicked at crates/victory-gpu/src/profile/tests.rs:121:28: no Submit in VIctory profile, process 216 sampled 1.0 s in 904 looks (1000 asked a second, 1.11 ms between looks, 0 late); 0 frames presented meanwhile (none: the tables are per second of sampling) 2 threads followed (2 still running); a thread looked at stood still under 0 us half the times, under 0 us 99 in 100, 0 us at longest, 0.0 us on average, 0 times; 0 call sites kept a thread may stand still for looks 3.0 % of its time (VICTORY_PROFILE_BUDGET): one whose looks cost more is looked at every 2nd, 3rd... tick while it runs ("every" below); its samples stand for more time each, so the times stay right the sampler's thread used 9.7 % of one processor names of ours: from Z:\tmp\i19\red\victory_gpu-d0f9bd8e96167b37.pdb THREADS role thread name, or where it started ms a second on cpu % waiting % looks not susp failed cut every look us mostly in (own time) submit 244 spins 0.0 0.0 0.0 904 0 904 0 1 0.0 wide 248 locks 0.0 0.0 0.0 904 0 904 0 1 0.0 (this report took 114 ms to write; sampling stood still meanwhile) note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace failures: profile::tests::a_blocked_thread_is_sampled_in_a_kernel_wait test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 188 filtered out; finished in 1.19s ``` </details> **Green** (this branch), exit 0: ``` ntsync: up and running. running 1 test test profile::tests::a_blocked_thread_is_sampled_in_a_kernel_wait ... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 188 filtered out; finished in 17.97s ``` The 18 s is mostly Wine's dbghelp loading the 28 MB PDB on the first lookup [deduction: the same test without the PDB beside it finished in 1.2 s]. **Sample report** (green; `profile::tests::the_sampler_finds_threads_and_writes_its_files`, `--nocapture`, THREADS table as printed): ``` role thread name, or where it started ms a second on cpu % waiting % looks not susp failed cut every look us mostly in (own time) pipelines 388 victory-pipelines-9 1000.0 0.0 100.0 302 301 0 0 2 86.5 ntdll!NtWaitForSingleObject (100 %) busy 392 at victory_gpu-d0f9bd...+0x51e450 166.5 95.0 0.0 51 31 0 0 2 79.6 victory_gpu-d0f9bd8e96167b37.exe+0x319d30 (100 %) ``` The pipeline thread's kernel wait is named; before the fix that thread had no samples at all. The busy thread's innermost frame is one of our functions, which stays unnamed (see known limits). **Whole profiler module** (`profile::tests`, `--nocapture`): before, 6 passed, 4 failed, 1 ignored; with this change, 7 passed, 3 failed, 1 ignored. The four red failures are the new test and the three older tests below. Only the new test changes status. ## Known limits - **10 ms ticks.** Wine reports thread time in clock ticks (`getconf CLK_TCK` is 100 here: 10 ms). A thread that runs less than one tick between two looks reads as not having run. Per-look shares are coarse; sums over a run are right. - **Clock cost.** About 31.5 µs per stand-in read under Wine (our earlier Proton measurement, not repeated here). The sampler test at 500 Hz with two followed threads reports 78.6 % of one processor for the sampler's own thread. Under Wine use `VICTORY_PROFILE_HZ=100` to `200` (default 1000). [deduction: at 100–200 Hz a look spans about one tick] - **Three older tests still fail under Wine, on names of our own functions:** `time_goes_to_the_function_that_spent_it`, `deep_stacks_are_cut_not_lost`, `the_sampler_finds_threads_and_writes_its_files`. With the PDB beside the exe, none of our `profile_test_*` free functions is named in the output (they show as `victory_gpu-<hash>.exe+0x…`), while closures in the same module are named. Green failures: `inner 0 of …`, `profile_test_sleeps` absent, `profile_test_spin_inner` absent from the folded stacks. Why Wine's dbghelp does not name these functions is not isolated here. They were already failing before this change, for another reason: with every look failing there were no samples to assert on (red: `no Submit`, `missed` looks, no `PIPELINE WORKERS`). They are not ignored in this PR. - **Windows was not run.** See above. - **The victory-gpu test target does not compile on win10-test (d006c10).** The test modules of `indirect.rs` (duplicate test functions and `mod bench`), `descriptor/host.rs` and `pipeline/host.rs` call APIs whose signatures changed or no longer exist: 42 compile errors. PR #37 (open, win10-test, "Repair indirect, overlap and depth-bounds GPU tests on win10-test") changes the same test files; this PR does not depend on it. For the runs above the three test modules were gated locally with `#[cfg(any())]`; that gate is not in this PR, and the branch builds as is. ## Related - fxceless/victory-fork#960 (open, by UwUCammie) addresses the same symptom with a broader change: it drops the CPU share when the counter is missing (every look takes the stack, "on cpu" reads 0), names system exports from the module's export table, and names inlined calls by source path. It reports `time_goes_to_the_function_that_spent_it` and `the_sampler_finds_threads_and_writes_its_files` passing under Wine 11.22. This PR keeps the CPU share on the `GetThreadTimes` stand-in, names only a function's start, and does not change inlined names. We did not run #960. Both edit `profile.rs` in the sampler and the naming code, so they will likely conflict. ## Disclosure AI-assisted: the investigation, the change and this description were produced with an AI coding assistant, and the builds and Proton runs above were run in that session. No game files, title data or rig settings are included; the runs execute the test binary only.
Under Wine every look of VICTORY_PROFILE failed before the stack was read.
QueryThreadCycleTime is a stub there (dlls/kernelbase/thread.c:444: FIXME,
SetLastError(ERROR_CALL_NOT_IMPLEMENTED), FALSE), and Sampler::look counted
its failure as a thread that had ended (profile.rs look). Measured under
Proton: 19899 looks, 19899 failed, 0 samples per thread.

Where the exact counter is missing (probed once in Sampler::new), cycles_of
takes a thread's kernel and user time from GetThreadTimes as cycles, so the
look logic is unchanged. Wine counts that time in 10 ms ticks (its
get_thread_times reads /proc/<pid>/task/<tid>/stat): a thread that runs less
than a tick between two looks reads as not having run, and a look's CPU
share is not capped at its weight on that path (the sums over a run stay
right). Windows keeps the exact counter and the cap.

Wine's exports carry no size (dlls/dbghelp/symbol.c, symt_find_symbol_at),
so SymFromAddrW names an export only at its own address. A frame inside
ZwWaitForSingleObject was reported as ntdll.dll+0xea80, not as a kernel wait.
When the frame's own address names nothing, the function's start is asked
for; on Windows that lookup adds no name.

Adds a_blocked_thread_is_sampled_in_a_kernel_wait: red on the previous code
(no Submit in the report), green with this change.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:53:46 +00:00
Author
Owner

@⁠PastiiJay on GitHub · 2026-11-25 23:34 UTC · original

Nice one @⁠zocram4cc, the profiler being blind under Proton (19,899 looks, 0 samples) explains a lot of the empty Linux reports. Falling back to GetThreadTimes only when QueryThreadCycleTime is a stub keeps Windows on the exact path, which is what we want.

We'll check the Windows side on our RX 9070 XT (same samples as before with VICTORY_PROFILE=1) and report here, since that's the one part you couldn't run.

**@⁠PastiiJay** on GitHub · 2026-11-25 23:34 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/53#issuecomment-7177743497) Nice one @⁠zocram4cc, the profiler being blind under Proton (19,899 looks, 0 samples) explains a lot of the empty Linux reports. Falling back to `GetThreadTimes` only when `QueryThreadCycleTime` is a stub keeps Windows on the exact path, which is what we want. We'll check the Windows side on our RX 9070 XT (same samples as before with `VICTORY_PROFILE=1`) and report here, since that's the one part you couldn't run.
Author
Owner

@⁠HandshakeHackin on GitHub · 2026-11-26 02:50 UTC · original

I ran the same two synthetic profiler tests on native Windows before and after d973b9d. Both passed on base d006c10 and on the PR (4/4 executions). The runner returned a nonzero QueryThreadCycleTime value. The tests distinguished spinning work from a thread blocked on a lock, and attributed CPU time, sleeps and waits to the expected call sites. Windows comparison

I compiled the unchanged profiler and its test file as an integration target because the full win10-test unit target still hits the unrelated descriptor test errors tracked in #37. This covers the native counter and attribution on a hosted Windows CPU. An in-game RX 9070 XT capture is still a separate check.

**@⁠HandshakeHackin** on GitHub · 2026-11-26 02:50 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/53#issuecomment-7181158748) I ran the same two synthetic profiler tests on native Windows before and after `d973b9d`. Both passed on base `d006c10` and on the PR (4/4 executions). The runner returned a nonzero `QueryThreadCycleTime` value. The tests distinguished spinning work from a thread blocked on a lock, and attributed CPU time, sleeps and waits to the expected call sites. [Windows comparison](https://github.com/HandshakeHackin/klbf-lab/actions/runs/45744472585/job/139657075436) I compiled the unchanged profiler and its test file as an integration target because the full `win10-test` unit target still hits the unrelated descriptor test errors tracked in #37. This covers the native counter and attribution on a hosted Windows CPU. An in-game RX 9070 XT capture is still a separate check.
Author
Owner

@⁠PastiiJay on GitHub · 2026-11-26 02:50 UTC · original

That covers most of the Windows check we'd promised @⁠zocram4cc on this one: exact counter path unchanged, attribution right on native Windows. What's left on our side is the in-game capture on the RX 9070 XT, and we'll post it here.

**@⁠PastiiJay** on GitHub · 2026-11-26 02:50 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/53#issuecomment-7181166521) That covers most of the Windows check we'd promised @⁠zocram4cc on this one: exact counter path unchanged, attribution right on native Windows. What's left on our side is the in-game capture on the RX 9070 XT, and we'll post it here.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/53/head:github-bot/gh-53'
git switch 'github-bot/gh-53'

Merge

Merge the changes and update on Forgejo.
git switch 'win10-test'
git merge --no-ff 'github-bot/gh-53'
git switch 'github-bot/gh-53'
git rebase 'win10-test'
git switch 'win10-test'
git merge --ff-only 'github-bot/gh-53'
git switch 'github-bot/gh-53'
git rebase 'win10-test'
git switch 'win10-test'
git merge --no-ff 'github-bot/gh-53'
git switch 'win10-test'
git merge --squash 'github-bot/gh-53'
git switch 'win10-test'
git merge --ff-only 'github-bot/gh-53'
git switch 'win10-test'
git merge 'github-bot/gh-53'
git push origin 'win10-test'
This discussion has been locked. Commenting is limited to contributors.
No description provided.