Files
metal2vulkan/crates
Anees Iqbal 9027bd2aa3 rt: count scheduled-to-start under earlier GPU spans, GPU-end-to-handler steps and calibrations
The clean 5080 re-play's print needs three things the stage sums alone cannot give.

First, how much of each command buffer's span from scheduling to GPU start lies under other
command buffers' GPU spans, the runtime's own uploads and copy-ins included, against the part
under none of them. This is Metrics::scheduled_to_gpu_start_behind_nanos.

Second, its span from GPU end to handlers split at the completion thread's own steps. This is
Metrics::gpu_end_to_handler_steps_nanos, six steps:
- seen: GPU end to the thread seeing the completion;
- told: telling the submit thread;
- stamp read: reading the round's timestamps;
- calibration: a calibration, where the round runs one;
- earlier: the round's earlier retirements, their handlers included;
- own: its own retirement until its handlers begin.
The same steps are kept for the command buffers of 1,024 us or more (gpu_end_to_handler_tail
and its steps), so a 1-8 ms tail can be placed in a step.

Third, the calibrations and their calls' time. The counter that only the tests had becomes
Metrics::calibrations, beside calibration_nanos.

The replay backend reports all of these as executed counters.

Counting only; nothing changes form. There are no new waits, locks or allocations. What each
count costs:
- Completion thread, per round: two clock reads (the stamp read's start and end), and a third
  where it calibrates. The period check reuses the stamp read's end, so it no longer reads the
  clock itself.
- Per command buffer retired: one clock read where its retirement begins, then for one that
  counts stages:
  - one pass over a fixed ring of the latest 128 GPU spans, a stack array in the completion
    thread;
  - a sort, on the stack, of those that meet its span;
  - one relaxed atomic add for the behind part;
  - six for the steps, and seven more for one in the tail.
- Per upload or copy-in retired: the timestamps it already read go into that ring. There is no
  second query read.
- Per calibration: two clock reads and two relaxed atomic adds.

Retirement returns an upload's and a copy-in's two timestamps (times::retire_own). The
completion thread places them on the host clock by the calibration as it stands
(Calibration::at, which moves nothing), and the program's command buffers' spans as placed.

Tests:
- host tests for the ring's merged, clipped union and its oldest-first replacement;
- a host test for the round's steps adding up to the span;
- the device stage test now checks that the six steps add up to gpu_end_to_handler_nanos, that
  the behind part is at most scheduled_to_gpu_start_nanos, and that the device start
  calibrated;
- the replay stage test checks the same through the report's keys.
2026-11-20 20:09:37 -08:00
..