Skip to content

XFER stats: The STATS window reads the StretchRect and ColorFill counters without the device lock - #205

Merged
jdrly merged 1 commit into
mainfrom
fix/194-xfer-stats-lock
Nov 14, 2026
Merged

jdrly merged 1 commit into
mainfrom
fix/194-xfer-stats-lock

Conversation

@jdrly

@jdrly jdrly commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

What changes

With D9MT_PROFILE, the StretchRect and ColorFill paths add to the XFER counters under the device lock. The STATS window took them without it: HitchFrameEnd runs after Present has released the lock. A StretchRect or ColorFill on another thread during the take could be lost or counted in the wrong window, and a 64-bit count could tear on i686. xfer::Stats::takeCounts now holds the lock the caller passes for both the read and the reset, and the device passes its own. The XFER line is formatted after the lock is released.

Closes #194

  • ADR: none

How to verify

scripts/run-native-tests.sh: xfer_stats_test now builds with ThreadSanitizer. It adds from two threads, each add under a mutex, while a third thread takes windows, and checks that every add lands in exactly one window.

Evidence

Proof. Head b676125, base c52fff5.

  • xfer_stats_test on jd-ai-server (x86_64 Linux, g++ -fsanitize=thread): OK on four runs, each taking 95,000 to 345,000 windows while the adders ran.
  • Controls:
    • With the lock dropped inside takeCounts (main's behaviour), ThreadSanitizer reports 4 data races and two count checks fail on lost adds (exit 66).
    • Against main's headers the test doesn't build.
  • Hosted Checks on b676125: green (run 43588817148). The macOS runner's clang built and ran xfer_stats_test with -fsanitize=thread: ok.
  • GPU gate: none. The change runs only with D9MT_PROFILE set, once a second, and touches nothing on a draw or present path (dispatcher's decision).

Performance (PERFORMANCE.md §1). Kind: a profile-only fix. With D9MT_PROFILE unset nothing changes: HitchFrameEnd isn't called. With it set, STATS takes the device lock once more per second.

Definition of Done

  • Clean x64/x86 builds: hosted Checks 43588817148.
  • Hash suite: not run. The code runs only with profiling on, outside any draw path (dispatcher's decision).
  • Regression test: fails on main (no locked take; with the lock dropped, data races and lost adds), passes here.
  • Native tests, the changed one under ThreadSanitizer.
  • Performance: none with profiling off.
  • AIR codegen id: no codegen input changed.
  • No new environment variable or option.
  • No new term, no changed decision.
  • Independent review: APPROVE, no findings.
  • UAT: not applicable; a profiling counter's thread safety, nothing a player sees.

Review

Codex (gpt-6.1-sol, reasoning high), one round at b676125: no findings. It checked these points:

  • The returned guard covers both the read and the reset.
  • Present and the snapshot block above release the device lock before this takes it, so there is no nesting.
  • The destructor's summary() needs no lock under the device's lifetime contract.

It ran the test under ThreadSanitizer itself.

  • Verdict: APPROVE

Risks and what I didn't do

Risk. With profiling on, STATS now waits for the device lock once a second. If another thread holds it for long, the STATS, RESTORE and XFER lines of that second come later. With profiling off, nothing changes.

How to revert. Revert the squash commit.

…under the device lock

Closes #194. ProfXfer adds under the device lock, but HitchFrameEnd runs after
Present released it and took the window without it: another thread's
StretchRect or ColorFill could be lost or land in the wrong window, and a 64-bit
count could tear on i686. xfer::Stats::takeCounts now holds the lock the caller
passes for the read and the reset; the line is formatted after. The native test
adds from two threads while a third takes windows, under ThreadSanitizer: with
the lock dropped it reports data races and loses adds.
@jdrly jdrly self-assigned this Nov 14, 2026
@jdrly
jdrly marked this pull request as ready for review November 14, 2026 11:40
@jdrly
jdrly merged commit 8713666 into main Nov 14, 2026
5 checks passed
@jdrly
jdrly deleted the fix/194-xfer-stats-lock branch November 14, 2026 11:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XFER stats: The STATS window reads the StretchRect and ColorFill counters without the device lock

1 participant