Skip to content

perf: per-frame input-to-present latency probe on metal - #5105

Merged
jakildev merged 3 commits into
masterfrom
claude/4998-input-latency-probe
Nov 17, 2026
Merged

jakildev merged 3 commits into
masterfrom
claude/4998-input-latency-probe

Conversation

@jakildev

Copy link
Copy Markdown
Owner

Summary

  • Frame-loop probe (engine/render/include/irreden/render/input_latency.hpp): InputLatencyAccumulator keeps the last OS-event poll, opens one record per RENDER (frame id from a World-owned counter, cursor-sample time, loop-start time, frames in flight at frame start), and closes it when the device reports that frame's end, including an end that arrives frames later. A frame with no poll of its own keeps the most recent earlier poll. Records stay parallel to the report's frame series, reserved once in enableFrameTiming. Disabled, no method reads a clock.
  • Device side (RenderDevice): setPresentingFrame(id), a never-blocking pollFrameCompletion(), framesInFlight() and frameEndEvent(), backed by a FrameCompletionQueue that defaults to NOT_MEASURED. The OpenGL device is untouched and reports "not measured". Metal stamps the submission at commit and reports the command buffer's GPUEndTime, converted from host time to steady_clock with an offset read between two host-clock reads (half their gap bounds the mapping error). A mid-frame finish() does not touch the queue, so frame ids never follow submissionSerial().
  • Report (--- Input latency ---): end event (gpu-complete / not measured), steady-frame count and sample count, mean / p99 / max of input_to_present_ms, cursor_to_present_ms, frame_start_to_submit_ms (CPU) and submit_to_present_ms (GPU), max frames in flight at frame start, the end-observed lag and clock-mapping bound, and a per-frame --- Input to present --- series (- = no end). Works in Release.
  • Positive control: the input_latency_test_delay_ms WorldConfig key (default 0) sleeps between a frame's last poll and RENDER; creations/demos/shape_debug/configs/perf/input-latency-delay-20ms.lua sets it to 20.
  • Tooling: compare_perf_runs.parse_report reads the section into input_latency (None when absent; measured false for not measured). repeat_profile.py adds per-run mean/p99 rows and a per-run coverage table, and admits IRShapeDebug's default scene (--sdf-voxel-scene off, no SDF workload controls, no SDF witnesses) plus the delay preset. perf_grid/configs/perf/profiling-off.lua is the default IRPerfGrid scene with stage profiling off.
  • Baseline: docs/perf/input-latency/README.md holds three scenes × three interleaved Release rounds, with each row's between-run spread as its noise bound, plus the positive-control pairs and the parent-vs-head command-buffer reports.

Test plan

  • fleet-build --target IrredenEngineTest, then fleet-run IrredenEngineTest --gtest_filter='InputLatency*': [ PASSED ] 15 tests (accumulator pairing, a late end, an earlier poll kept, no reading before any poll, a skipped end, an invalid end, the disabled path reading no clock, queue order and in-flight count, the base device reporting not measured, a blocking-present device leaving 0 in flight, and the report section's steady-frame statistics, not-measured and no-sample forms).
  • Full fleet-run IrredenEngineTest (Debug, macOS Metal): [ PASSED ] 3848 tests, 3 skipped (platform skips: two buffered-I/O failure injections and FogCrossSectionTest.SkippedOnNonOpenGLBackend).
  • python3 -m unittest discover -p 'test_*.py' in scripts/perf (174 tests OK) and in scripts/perf/tests (7 OK); ruff check scripts/: All checks passed.
  • fleet-build --target format-changed: no change to the committed tree.
  • fleet-pr-overlap --base master: VERDICT: overlap (exit 1), rows quoted in Notes for reviewer.
  • caffeinate -d -i -u python3 scripts/render-verify.py --target IRShapeDebug --no-build: 20 of 148 checks FAIL (10 skipped — references pending). Every failing row is master's, and every failing pass renders pixel-identically on master. A master 345dbbf25 Debug tree built in the same session renders the same set of PNG SHA-256s as this head for each pass with a red row: default 28/28, sdf_baked_surface_parented 6/6, sdf_baked_pose_root 6/6, sdf_baked_pose_parented 6/6, glitch_standard_off 28/28, glitch_subject_standard_off 28/28, and lod_scene (one SHA-256 across three head and three master runs).

Acceptance evidence

Criterion Check run Observed
1. Report carries mean, p99, max over the steady frames, the per-frame series, max frames in flight, the end event; sample count equals steady frame count fleet-run IrredenEngineTest --gtest_filter='InputLatency*' (15 PASSED); fleet-build --target IRPerfGrid, then fleet-run IRPerfGrid --auto-profile 120 --no-overlay (Debug, QUIET=guarded, paced at 120 Hz) End event: gpu-complete, Steady frames: 90 (first 30 of 120 excluded) samples=90, input_to_present_ms: mean=11.512 p99=18.528 max=18.528, Frames in flight at frame start: max=0, and a 120-value (0 without an end) --- Input to present (ms, in order; - = no end) --- series. Report committed at docs/perf/input-latency/report-fields/irperfgrid-debug-120.txt.
2. Positive control: a 20 ms test-only delay between the last poll and RENDER raises the reported mean by 20 ms within 2 ms; three interleaved pairs, light scene, Debug, quoting both means repeat_profile.py --target IRShapeDebug --repeats 1 -- --auto-profile 300 --yaw 0 --zoom 1 --sdf-voxel-scene off --config-preset configs/perf/<sdf-off | input-latency-delay-20ms>.lua, three pairs interleaved, every run QUIET=guarded (see Plan departures on the benchmark lease) Without → with delay: pair 1 10.206 → 28.824 (rise 18.618), pair 2 10.007 → 28.503 (18.496), pair 3 9.608 → 28.722 (19.114) ms. All three within 2 ms of 20. 225/225 samples per run. Reports in docs/perf/input-latency/positive-control/.
3. Metal reports max 0 frames in flight at frame start, distinguished from "not measured"; OpenGL reading is #5001's Every Metal report above; InputLatencyRenderDevice.ADeviceThatTimesNoFrameEndReportsNotMeasured, InputLatencyReport.ADeviceWithoutFrameEndsIsNotMeasuredRatherThanZero All 9 baseline runs, the 6 control runs and the criterion-1 run read Frames in flight at frame start: max=0 under End event: gpu-complete. A device that times no frame end (the OpenGL device, untouched here) writes End event: not measured / Frames in flight at frame start: not measured and no statistic row. The OpenGL reading belongs to #5001 per Plan correction A1.
4. Baseline in docs/perf/input-latency/: Release, stage profiling off, three interleaved rounds of light / million / CPU-bound (probe CPU > GPU); mean and p99, frame mean, updates per frame, power, host load, between-run spread as the noise bound; quiet-host verdict repeat_profile.py --release --repeats 1 per cell with the per-scene arguments in the doc's Scenes table; rounds a → b → c, three times, Release 9 of 9 runs QUIET=guarded, RESULT=CLEAN, AC power, 225/225 samples. input_to_present_ms mean (spread): light 10.344 (1.098), million 26.250 (0.118), CPU-bound 15.753 (0.140); p99 (spread): 16.099 (1.700), 26.989 (0.361), 23.507 (2.119). CPU-bound scene (IRPerfGrid default scene, --worker-threads 0) reads CPU 9.61 > GPU 4.01 ms; million reads 8.51 / 19.14. Load average 5–12 is stated per row. The pacer fix 227cadbec is in the tree, and the doc says so.
5. No added GPU wait or submission: command buffers per valid frame unchanged against the parent commit; disabled path takes no timestamp fleet-run --build-dir <tree> IRPerfGrid --auto-profile 120 --no-overlay and the million control at 120 frames, Release, on this head and on a parent (345dbbf25) tree; InputLatencyAccumulator.DisabledTakesNoTimestampAndRecordsNothing, InputLatencyFrameCompletionQueue.DisabledOrUnmeasuredReadsNoClock Parent and head both Coverage: supported=1 attempted=120 valid=120 invalid=0 commandBuffers=120 at both fixtures. The disabled-path tests count 0 clock reads. Reports in docs/perf/input-latency/command-buffers/.

Plan departures

  • Metal ends at GPU completion in a visible window too. The plan's end-event decision prefers a presentation time where the backend reports one. The drawable's presented time arrives later, on a Metal-owned thread, so it needs storage that outlives the device, and no unattended run has a drawable to exercise it (every fleet run is offscreen). Metal therefore reports gpu-complete in both window modes, and the report names that end event. FrameEndEvent::PRESENTED and the late-arrival path in the accumulator are ready for a device that adds it.
  • Benchmark lease. Criteria 2 and 4 say "under ir-acquire benchmark". An outer ir-acquire benchmark -- … hold makes every nested ir-run --auto-profile inherit the lease, so it opens no guarded window and prints no QUIET= line. Each run instead took its own guarded window through fleet-run, and every manifest records quiet: guarded.
  • Light scene through repeat_profile.py. Its IRShapeDebug path accepted only the SDF fixtures. It now also admits --sdf-voxel-scene off, IRShapeDebug's default scene with an explicit --yaw 0 --zoom 1 (the defaults), and the delay preset.
  • CPU-bound scene. It is IRPerfGrid's default scene (64³ per-cell wave) with --worker-threads 0, through a new configs/perf/profiling-off.lua preset.
  • Extra row. frame_start_to_submit_ms is the frame's CPU time. It is the probe-side witness that scene (c) is CPU-bound ("the probe's own CPU time per frame exceeds its GPU time").
  • OpenGL. Per Plan correction A1, no file in the OpenGL-only source set is edited.

Notes for reviewer

  • Open-PR overlap (fleet-pr-overlap --base master, VERDICT: overlap). Every row is a conflict render: size per-axis stores from content height (#4960) #5101 (stacked on render: reject hidden overflow-lane faces before sort, relight and draw (#4954) #5084, forked before master moved) already has with master:
    #5101 engine/profile/src/profile_report.cpp base-conflict (conflicts with master 345dbbf25 alone)
    #5101 scripts/perf/compare_perf_runs.py base-conflict (conflicts with master 345dbbf25 alone)
    #5101 scripts/perf/test_profile_parser.py base-conflict (conflicts with master 345dbbf25 alone)
    
    The two follow-up commits moved this PR's scripts/perf/README.md section and its parser regexes clear of engine: stop paying per-update work for resident entities that cannot change (#5009) #5073's and render: size per-axis stores from content height (#4960) #5101's hunks. On all three paths, a -c merge.conflictStyle=merge trial merge against render: size per-axis stores from content height (#4960) #5101 gives the same conflict blocks from the merge-base as from this head, and git merge-file replays this branch onto the merge-base result with exit 0.
  • What the light scene's mean contains. At 120 Hz frames and 60 Hz updates, every other frame has no poll of its own and keeps the previous frame's, so input_to_present alternates by about one frame (8.3 ms). This is real latency, surfaced as the plan asked. The delayed control arm always polls, which is why its rise is 18.5–19.1 ms rather than exactly 20. The doc spells this out.
  • Display sleep. A first render-verify pass died with RESULT=NO-DISPLAY when the display slept, and some CPU-bound scouting runs before that read the 60 Hz pacer fallback. Those scouting runs only picked the scene. Every committed run was taken under caffeinate -d -i -u with 120 Hz pacing confirmed in Debug before and after.
  • Captures. No captures attached: the change has no visual effect (engine/render/CLAUDE.md, "Changes without a visual effect need no captures"). Render-verify, in the test plan, is the unchanged-output check.

Closes #4998

🤖 Generated with Claude Code

jakildev and others added 3 commits November 16, 2026 03:29
Every rendered frame now gets a frame id and steady-clock stamps for
its last OS-event poll, RENDER's cursor sample, its loop-iteration
start and its last commit. The render device reports when the frame
ended through a non-blocking query on RenderDevice that defaults to
"not measured", so the OpenGL device compiles unchanged and reports no
reading. Metal reports the command buffer's GPU end, moved from host
time onto the steady clock with a bounded mapping error.

The --auto-profile report gains an Input latency section: mean, p99
and max of input_to_present, cursor_to_present, frame_start_to_submit
and submit_to_present over the steady frames, frames in flight at
frame start, the clock mapping bound, and the per-frame series. The
probe rides on frame timing and reads no clock when it is off.
input_latency_test_delay_ms is the positive control's test-only sleep
between the last poll and RENDER.

repeat_profile.py parses and summarizes the section and admits
IRShapeDebug's default scene, which the serialized baseline in
docs/perf/input-latency/ uses as its light scene.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Keeps the new scripts/perf/README.md section clear of the lines another
open PR inserts beside the frame decomposition section.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Moves the input latency parser patterns up beside the steady frame
pattern, out of the lines an open stacked PR rewrites next to the
device counter patterns. No behaviour change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jakildev jakildev added fleet:author-claude Implementation last authored by Claude; route review to Codex fleet:authored-on-macos PR was authored and smoke-tested on macOS Metal backend labels Nov 16, 2026
@github-actions

Copy link
Copy Markdown

Perf gate

Head host slug: linux-x86_64-xeon-6975p-c-03-0-vga

Perf gate — seeding new baseline

No baseline at /home/runner/work/_temp/perf-baseline/docs/perf/baseline_latest/linux-x86_64-xeon-6975p-c-03-0-vga/. The next master push from this host will seed one. Gate is informational this PR.

@jakildev jakildev left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review — perf: per-frame input-to-present latency probe on metal

Verdict: approve

The frame-loop/device pairing is coherent: frame identity is independent of submission serials, the Metal path timestamps the existing final commit/wait without adding GPU work, late completions remain non-blocking, and the disabled/OpenGL-default paths read no clock and report not measured. I found no correctness, lifetime, synchronization, or hot-path allocation defect in the reviewed diff.

Praise

  • InputLatencyAccumulator keeps report rows parallel with rendered frames while pairing completions by stable frame id; the late, skipped, invalid, and no-poll cases are explicit and covered.
  • The Metal clock conversion records both observation lag and a bounded host-to-steady mapping error instead of treating CPU observation time as GPU completion.
  • The committed baseline includes the positive control, three scene classes, parent/head submission counts, and enough run metadata to make later overlap comparisons meaningful.

Acceptance (issue #4998)

Criterion Grade Evidence
1. Report fields, ordered series, end kind, frames in flight, and full steady-frame coverage Met writeInputLatency emits the required fields; the committed 120-frame report records 90 steady frames and 90 samples. The focused C++ suite covers pairing and report forms.
2. Three interleaved 20 ms positive-control pairs Met The rises are 18.618, 18.496, and 19.114 ms, all within 2 ms of 20, with 225/225 samples. Each manifest records a guarded quiet window.
3. Metal reports measured zero in flight; OpenGL is distinct Met Metal reports gpu-complete and max 0; base-device/report tests distinguish not measured from zero. Plan correction A1 assigns the OpenGL implementation to #5001.
4. Three Release scene classes across three interleaved rounds with noise bounds Met Nine guarded, clean manifests cover light, GPU-bound million, and CPU-bound fixtures; the baseline gives means, p99s, frame/update context, host load/power, and between-run spreads.
5. No added wait/submission; disabled path reads no clock Met Parent/head reports retain 120 command buffers for 120 valid frames in both fixtures. The implementation instruments the existing Metal commit/wait, and disabled-path tests assert zero clock reads.

Validation

  • Local reviewed-head build: fleet-jobs: 20261116T122938Z-build-2f5f64 succeeded exit=0 for IrredenEngineTest.
  • Local reviewed-head C++ tests: fleet-run IrredenEngineTest --gtest_filter='InputLatency*' — 15 passed.
  • Local parser/summary tests: python3 -m unittest test_profile_parser.py test_repeat_profile.py test_shape_profile.py — 119 passed.
  • Live CI: perf-gate, fleet-tests, format, headers, lint, and other completed checks pass. render-harness-tests is red on the existing scripts/verify_common.py import of fleet; the latest master run fails the same dependency-contract assertions, so this is inherited rather than introduced by #5105. linux-build was still pending when reviewed.

Test plan before merge

  • Let the pending linux-build finish; do not treat it as green until it completes.

🤖 Reviewed by Codex GPT-5 (review-pr skill)

Opus recheck not required (reviewed at opus class).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fleet:approved Fleet reviewer approved — ready for human merge fleet:author-claude Implementation last authored by Claude; route review to Codex fleet:authored-on-macos PR was authored and smoke-tested on macOS Metal backend fleet:needs-windows-smoke PR needs windows-debug build + smoke-run validation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: per-frame input-to-present latency probe on both render backends

1 participant