WIP: fix(profiling): the Windows all-thread sampler stamps each sample with its own time under HOS_CHAIN #1044

Draft
Amateurwizrd wants to merge 1 commit from Amateurwizrd/Longshot:fix/windows-sampler-at into main
Contributor

Type: fix · Risk: low · Tested on: Windows 11, RTX 5070 Ti · Needs: none

Why

On Windows, scripts/chain.py's frame critical path, read together with an all-thread sample of the same run
(HOS_CHAIN + HOS_SAMPLE_ALL), reports the game's job threads as mostly "waiting below the game" (a host wait the
path does not see) while they are on the path. Those waits are not there: the Windows sampler gives every sample of a
sweep the one millisecond the sweep started at, so chain.py lays samples 0.6-3 ms away from stretches of the path that
last 50-100 µs. Anyone hunting the renderer's share of a Windows frame with these tools is sent after time that does
not exist. The Linux sampler (host/posix/prof.c) has written each sample's own time since HOS_CHAIN came in
(57ed3aa8); the Windows one never did. No issue covers it (#383, the sampler's unguarded stack walk, is
another bug).

How to reproduce

Seen on: Windows 11 26200, RTX 5070 Ti (driver 621.01), Ryzen 7 7800X3D
Settings and switches: HOS_OUTPUT_RES=640x360 HOS_DYNRES=0 HOS_FPS_MAX=1000 RX_VK_FRAMEGEN=0, plus
HOS_PROFILE=5 HOS_SAMPLE_ALL=<run>/all.txt HOS_CHAIN=<run>/chain.txt with both .go files present for 5-8 s
Steps: Hyrule Field (child), the camera turning at full speed (right stick held right); then
python -I scripts/chain.py <run>/chain.txt <run>/all.txt
Expected: "samples on the path" shows the threads on the path working, as on Linux
Actual: on main, 26-85 % of each thread's samples on the path are "waiting" (main thread 26 %, the job workers
59-86 %)

What changed

With HOS_CHAIN set, the Windows all-thread sampler writes # at <ns> before each sample, the time its thread was
stopped, which scripts/chain.py already reads. The same line host/posix/prof.c writes on Linux.

  • profiling: sample_all_thread() in host/mkw_glue.c takes hos_now_ns() after GetThreadContext() and writes it
    before the sample's line. Without HOS_CHAIN nothing changes.

Measurements

Verdict: the chain's samples now land on the path: "waiting below the game" on the path falls from 26-86 % to
0-1.5 % per thread; the path itself reads the same (8.61 and 8.80 ms a frame, two runs of different length).

Hardware: Windows 11 26200, RTX 5070 Ti 16 GB (driver 621.01), Ryzen 7 7800X3D, 32 GB
Scene: Hyrule Field (child, night), camera turning at full speed; 640x360 output, Resolution 100 %, no frame
generation, uncapped
Method: one HOS_CHAIN + HOS_SAMPLE_ALL run on each side (8 s on main, 5 s here: main's 8 s run filled the chain's
2,097,152-record cap), python -I scripts/chain.py chain.txt all.txt 25, the "samples on the path" block. A
correctness check of a diagnostic, not a speed measurement.
How to reproduce: as in How to reproduce above, on each build

samples on the path, "waiting" share Before: main @ cefce0d1 After: this PR @ 65923bab
sead::MainThread 26.0 % 0.5 %
BaseProcExecutor0 61.4 % 0.0 %
ModuleSystemWorker1 59.3 % 1.0 %
ModuleSystemWorker5 67.9 % 1.5 %
BaseProcExecutor5 85.5 % 0.0 %
Presentation_Thread 80.0 % 0.0 %
the path, ms a frame 8.61 (7.44 running) 8.80 (7.68 running)
Before (main @ cefce0d1, 8 s):
2097152 records, 50 threads; 719 frames presented by Presentation_Thread, 8.61 ms a frame (116.2 fps)
     22.2%  sead::MainThread              74.0% working,  26.0% waiting
     19.0%  BaseProcExecutor0             38.6% working,  61.4% waiting
After (this PR, 5 s):
1654096 records, 50 threads; 569 frames presented by Presentation_Thread, 8.80 ms a frame (113.7 fps)
     19.3%  sead::MainThread              99.5% working,   0.5% waiting
     18.1%  BaseProcExecutor0            100.0% working,   0.0% waiting

The base build was main @ cefce0d1; this branch is on main @ 6fb13fe5. Nothing between them touches the sampler or
scripts/chain.py (git diff cefce0d1 6fb13fe5 -- host/mkw_glue.c scripts/chain.py is empty).

Flags and settings

None: the line is written only under the existing HOS_CHAIN.

Testing

Tested: the run above in the game on Windows (RTX 5070 Ti); its all.txt holds 107,732 # at lines and chain.py
reads them. Game-free checks that read host/mkw_glue.c or the profiling scripts pass: test_commit_limit,
test_crash_record, test_input_background, test_intel_compiler_gate, test_warm_bounded, test_hle_stats, test_chain,
test_prof_clock, test_prof_sampling, test_prof_symbols. python -I -B tests/run_all.py (Windows, llvm-mingw): 583
passed, 26 failed, 50 skipped. 25 of the failures fail on main @ 6fb13fe5 too: test_auto_handheld,
test_bug_report_defaults, test_crash_dialog, test_crt_options, test_fastfp_fma, test_fpx_sticky,
test_fsr_uniform_ownership, test_gpu_hang_watch, test_guest_imported, test_import_reach, test_linux_preflight,
test_make_game_cache, test_missing_nup, test_pads_after_menu, test_parallel_init_order, test_pending_guest_import,
test_portable_builder_home, test_portable_builder_startup, test_profile_stream, test_replay_write_order,
test_run_all_changed, test_spawn_emit, test_tested_configurations, test_vk_occlusion_queries, test_walk_key. The 26th,
test_longshot_ctl, does not read the diff: it failed in the full run and once alone, then passed alone (and on main).
Not tested: Linux: the changed function is in the Windows half of host/mkw_glue.c; Linux uses
host/posix/prof.c, unchanged.
Must be tested: None.

Risks

What could break: an all-thread sample with HOS_CHAIN on grows by one short line a sample. Nothing else runs it.
Turning it off: revert the commit.

Checklist

Everyone

  • One topic.
  • The title is a Conventional Commit (type(scope): description) of the status line's type.
  • Why gives the reason plainly and links the issues that fit (the tracker searched).
  • Measurements has a verdict, or one line saying why there is nothing to measure.
  • Testing names what was tested, what wasn't, and what must still be tested, where and how.
  • Rebased on current main (not rebased: merges clean into main @ bbe421f5).
  • python -I -B tests/run_all.py passes (any failure that also fails on main is named).
  • No game files, dumps, build output or generated files.
  • third_party/ untouched (a new folder with a PROVENANCE.md instead).
  • The license is OK: GPL-3.0-or-later, nothing incompatible and nothing decompiled.

When it applies

  • AI assistance recorded with Assisted-by: / Co-Authored-By: trailers.
  • Host code builds on both Windows (llvm-mingw) and Linux, with UTF-8 paths.
**Type:** fix · **Risk:** low · **Tested on:** Windows 11, RTX 5070 Ti · **Needs:** none ## Why On Windows, `scripts/chain.py`'s frame critical path, read together with an all-thread sample of the same run (`HOS_CHAIN` + `HOS_SAMPLE_ALL`), reports the game's job threads as mostly "waiting below the game" (a host wait the path does not see) while they are on the path. Those waits are not there: the Windows sampler gives every sample of a sweep the one millisecond the sweep started at, so chain.py lays samples 0.6-3 ms away from stretches of the path that last 50-100 µs. Anyone hunting the renderer's share of a Windows frame with these tools is sent after time that does not exist. The Linux sampler (`host/posix/prof.c`) has written each sample's own time since HOS_CHAIN came in (`57ed3aa8`); the Windows one never did. No issue covers it (#383, the sampler's unguarded stack walk, is another bug). ## How to reproduce **Seen on:** Windows 11 26200, RTX 5070 Ti (driver 621.01), Ryzen 7 7800X3D **Settings and switches:** `HOS_OUTPUT_RES=640x360 HOS_DYNRES=0 HOS_FPS_MAX=1000 RX_VK_FRAMEGEN=0`, plus `HOS_PROFILE=5 HOS_SAMPLE_ALL=<run>/all.txt HOS_CHAIN=<run>/chain.txt` with both `.go` files present for 5-8 s **Steps:** Hyrule Field (child), the camera turning at full speed (right stick held right); then `python -I scripts/chain.py <run>/chain.txt <run>/all.txt` **Expected:** "samples on the path" shows the threads on the path working, as on Linux **Actual:** on main, 26-85 % of each thread's samples on the path are "waiting" (main thread 26 %, the job workers 59-86 %) ## What changed With `HOS_CHAIN` set, the Windows all-thread sampler writes `# at <ns>` before each sample, the time its thread was stopped, which `scripts/chain.py` already reads. The same line `host/posix/prof.c` writes on Linux. - profiling: `sample_all_thread()` in `host/mkw_glue.c` takes `hos_now_ns()` after `GetThreadContext()` and writes it before the sample's line. Without `HOS_CHAIN` nothing changes. ## Measurements **Verdict:** the chain's samples now land on the path: "waiting below the game" on the path falls from 26-86 % to 0-1.5 % per thread; the path itself reads the same (8.61 and 8.80 ms a frame, two runs of different length). Hardware: Windows 11 26200, RTX 5070 Ti 16 GB (driver 621.01), Ryzen 7 7800X3D, 32 GB Scene: Hyrule Field (child, night), camera turning at full speed; 640x360 output, Resolution 100 %, no frame generation, uncapped Method: one `HOS_CHAIN` + `HOS_SAMPLE_ALL` run on each side (8 s on main, 5 s here: main's 8 s run filled the chain's 2,097,152-record cap), `python -I scripts/chain.py chain.txt all.txt 25`, the "samples on the path" block. A correctness check of a diagnostic, not a speed measurement. How to reproduce: as in How to reproduce above, on each build | samples on the path, "waiting" share | Before: main @ cefce0d1 | After: this PR @ 65923bab | |---|---|---| | sead::MainThread | 26.0 % | 0.5 % | | BaseProcExecutor0 | 61.4 % | 0.0 % | | ModuleSystemWorker1 | 59.3 % | 1.0 % | | ModuleSystemWorker5 | 67.9 % | 1.5 % | | BaseProcExecutor5 | 85.5 % | 0.0 % | | Presentation_Thread | 80.0 % | 0.0 % | | the path, ms a frame | 8.61 (7.44 running) | 8.80 (7.68 running) | ``` Before (main @ cefce0d1, 8 s): 2097152 records, 50 threads; 719 frames presented by Presentation_Thread, 8.61 ms a frame (116.2 fps) 22.2% sead::MainThread 74.0% working, 26.0% waiting 19.0% BaseProcExecutor0 38.6% working, 61.4% waiting After (this PR, 5 s): 1654096 records, 50 threads; 569 frames presented by Presentation_Thread, 8.80 ms a frame (113.7 fps) 19.3% sead::MainThread 99.5% working, 0.5% waiting 18.1% BaseProcExecutor0 100.0% working, 0.0% waiting ``` The base build was main @ cefce0d1; this branch is on main @ 6fb13fe5. Nothing between them touches the sampler or `scripts/chain.py` (`git diff cefce0d1 6fb13fe5 -- host/mkw_glue.c scripts/chain.py` is empty). ### Flags and settings None: the line is written only under the existing `HOS_CHAIN`. ## Testing **Tested:** the run above in the game on Windows (RTX 5070 Ti); its `all.txt` holds 107,732 `# at` lines and chain.py reads them. Game-free checks that read `host/mkw_glue.c` or the profiling scripts pass: test_commit_limit, test_crash_record, test_input_background, test_intel_compiler_gate, test_warm_bounded, test_hle_stats, test_chain, test_prof_clock, test_prof_sampling, test_prof_symbols. `python -I -B tests/run_all.py` (Windows, llvm-mingw): 583 passed, 26 failed, 50 skipped. 25 of the failures fail on main @ 6fb13fe5 too: test_auto_handheld, test_bug_report_defaults, test_crash_dialog, test_crt_options, test_fastfp_fma, test_fpx_sticky, test_fsr_uniform_ownership, test_gpu_hang_watch, test_guest_imported, test_import_reach, test_linux_preflight, test_make_game_cache, test_missing_nup, test_pads_after_menu, test_parallel_init_order, test_pending_guest_import, test_portable_builder_home, test_portable_builder_startup, test_profile_stream, test_replay_write_order, test_run_all_changed, test_spawn_emit, test_tested_configurations, test_vk_occlusion_queries, test_walk_key. The 26th, test_longshot_ctl, does not read the diff: it failed in the full run and once alone, then passed alone (and on main). **Not tested:** Linux: the changed function is in the Windows half of `host/mkw_glue.c`; Linux uses `host/posix/prof.c`, unchanged. **Must be tested:** None. ## Risks **What could break:** an all-thread sample with `HOS_CHAIN` on grows by one short line a sample. Nothing else runs it. **Turning it off:** revert the commit. ## Checklist ### Everyone - [x] One topic. - [x] The title is a Conventional Commit (`type(scope): description`) of the status line's type. - [x] Why gives the reason plainly and links the issues that fit (the tracker searched). - [x] Measurements has a verdict, or one line saying why there is nothing to measure. - [x] Testing names what was tested, what wasn't, and what must still be tested, where and how. - [x] Rebased on current `main` (not rebased: merges clean into main @ bbe421f5). - [x] `python -I -B tests/run_all.py` passes (any failure that also fails on `main` is named). - [x] No game files, dumps, build output or generated files. - [x] `third_party/` untouched (a new folder with a `PROVENANCE.md` instead). - [x] The license is OK: GPL-3.0-or-later, nothing incompatible and nothing decompiled. ### When it applies - [x] AI assistance recorded with `Assisted-by:` / `Co-Authored-By:` trailers. - [ ] Host code builds on both Windows (llvm-mingw) and Linux, with UTF-8 paths.
fix(profiling): the Windows all-thread sampler stamps each sample with its own time under HOS_CHAIN
Some checks are pending
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
Guide screenshots / Guide images and local UI source review (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Website / Website on Cloudflare (pull_request_target) Has been skipped
PR description / Description follows the template (pull_request_target) Successful in 3s
65923babf6
- With HOS_CHAIN, a "# at <ns>" line before each sample, taken when its thread was stopped, as host/posix/prof.c does.
- One sweep of ~90 threads takes a millisecond or more; every sample carried the sweep's one millisecond, so
  scripts/chain.py laid them 0.6-3 ms off its 50-100 us stretches and charged waits to the path that were not on it.

Assisted-by: Claude
Author
Contributor

Tested in game: Windows 11, RTX 5070 Ti: Hyrule Field at 640x360 with HOS_CHAIN + HOS_SAMPLE_ALL; chain.py's samples on the path read 0-1.5 % waiting (main: 26-86 %)

Tested in game: Windows 11, RTX 5070 Ti: Hyrule Field at 640x360 with HOS_CHAIN + HOS_SAMPLE_ALL; chain.py's samples on the path read 0-1.5 % waiting (main: 26-86 %)
Some checks are pending
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
Guide screenshots / Guide images and local UI source review (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Website / Website on Cloudflare (pull_request_target) Has been skipped
PR description / Description follows the template (pull_request_target) Successful in 3s
This pull request is marked as a work in progress.
Some workflows are waiting to be reviewed.
This branch is out-of-date with the base branch
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'fix/windows-sampler-at:Amateurwizrd-fix/windows-sampler-at'
git switch 'Amateurwizrd-fix/windows-sampler-at'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
MagicBeanDev/Longshot!1044
No description provided.