Time the hand-off to the present thread, the present call and other threads' acquire waits, and count the acquire's GPU wait once.

The perf lines could not see pacing. In the 0.10.0 fresh session (nx2navi.1.log, 7,974,927 bytes) all 666 windows read
"pacing 0%" and "bound: cpu" and all 70 slow-frame lines "pacing 0.0 drain 0.0", while 610 windows had a 16.x ms
interval at fps_limit 60 under the game's own limiter. The pacer's wait happened where perf does not count it:
vk_backend.c present_image's wait for the present thread to take the previous picture was untimed, perf_end drops
waits on any thread but the presenting and replay ones (a game may acquire its window image on another), and the
acquire's GPU wait was counted twice, as pacing around b->fence_wait and as a fence wait inside vk_sync_fence_wait
(F-7-04).

- host.h: PERF_HANDOFF, the presenting thread's wait to hand the present thread the next picture, timed in
  present_image from under pl (where pending is read) and kept in PresentJob.handoff_ns. perf_end_any counts a wait
  on any other guest thread into a sum that perf_present adds to the frame before ft_frame reads its parts.
- nvn_api_obj.c nvnWindowAcquireTexture: the pacer's sleep is timed with perf_end_any; the GPU wait counts only as
  the fence wait it is; one line names the threads that acquire and present once the first present is known.
- vk_backend.c present_main: the TRACE=present line adds the present call's time (vkQueuePresentKHR, under the queue
  lock, where an overlay's frame limiter sleeps) and the job's hand-off wait; with trace=perf, a line per 60 presents
  gives the present thread's blit and submit, paced wait, present call (avg, max), time past target and the hand-off.
- perf.c: "handoff" in the window line's shares and the slow-frame line, "hand-off" in the worst-frame lines and as a
  cause of its own for frames over 25 ms and the stutter map. The "bound:" rule stays as it is until play-test numbers
  exist.
- The cause list changes together: perf.c FT_C_* and ft_cause_name, stutter_map.c SM_CAUSES and cause_short,
  tests/stutter_hitch_check.c names[] (now sized from SM_CAUSES), and tools/stutter_map.py, whose cause and hitch
  patterns now take a hyphen. flight.c's frames.tsv gets a handoff_ms column (blocked_names checked against PERF_N),
  and tools/flight_view.py lists it.
- tools/perf_summary.py reads the hand-off share, the present-thread lines and the TRACE=present lines, and prints
  percentiles of the present interval, paced wait, time past target, present call and hand-off.

New tests/perf_summary_check.py makes lines with the runtime's format strings (checked against the C sources) and
checks the percentiles, the window averages and the hand-off share, and that older lines without these fields still
parse; tests/stutter_map_check.py gets a hand-off hitch and area cause, which the old patterns dropped. Diagnostics
only: no wait, pacing or presentation change. Not reproduced before the change (it needs a game run with trace=perf).
perf_summary_check, stutter_map_check, stutter_hitch_check, flight_flash_check and frame_step_check pass; every
runtime file compiles with the dev flags after the host.h change, and the changed files add no -Wall -Wextra warnings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-13 17:17:19 -05:00
parent 26b7cfd658
commit 0472734008
13 changed files with 304 additions and 56 deletions

View File

@@ -40,7 +40,8 @@
#define WEAK __attribute__((weak))
typedef struct { VkImage image; VkFormat format; int w, h, has_crop, crop[4]; uint64_t frame, at, queued, value; } PresentJob;
// handoff_ns: how long the presenting thread waited to hand this job over (present_image), for the present lines
typedef struct { VkImage image; VkFormat format; int w, h, has_crop, crop[4]; uint64_t frame, at, queued, value, handoff_ns; } PresentJob;
static PlatMutex pl = PLAT_MUTEX_INIT;
static PlatCond pcv = PLAT_COND_INIT; // job queued, job taken, job submitted
@@ -76,9 +77,31 @@ static void pace_shown(uint64_t interval_ns) {
pace_avg_ns = pace_avg_ns ? pace_avg_ns + (uint64_t)(((int64_t)interval_ns - (int64_t)pace_avg_ns) / 4) : interval_ns;
}
// NX2NAVI_TRACE=perf: the present thread's own window of PW_PRESENTS presents. The render thread's perf lines time the
// game's side and the hand-off; these say where the present thread's time went: the blit and its submission, the
// wait for the picture's time (pace_smooth), and the present call, which takes the queue lock and in which an overlay
// with a frame limiter sleeps. With the presenting thread's wait to hand each picture over, which that time causes.
#define PW_PRESENTS 60
typedef struct { uint64_t n, blit, wait, call, call_max, past, hand, hand_max; } PresentWin;
static void present_window(PresentWin *w, uint64_t frame, uint64_t blit, uint64_t wait, uint64_t call, uint64_t past,
uint64_t hand) {
w->n++, w->blit += blit, w->wait += wait, w->call += call, w->past += past, w->hand += hand;
if (call > w->call_max) w->call_max = call;
if (hand > w->hand_max) w->hand_max = hand;
if (w->n < PW_PRESENTS) return;
double n = (double)w->n;
rt_log("perf: present thread frame %llu: %llu presents; blit and submit avg %.2f ms, paced wait avg %.2f ms, present "
"call avg %.2f ms max %.2f ms, past target avg %.2f ms; the presenting thread waited to hand over avg %.2f ms "
"max %.2f ms", (unsigned long long)frame, (unsigned long long)w->n, w->blit / 1e6 / n, w->wait / 1e6 / n,
w->call / 1e6 / n, w->call_max / 1e6, w->past / 1e6 / n, w->hand / 1e6 / n, w->hand_max / 1e6);
memset(w, 0, sizeof *w);
}
static void present_main(void *arg) {
(void)arg;
uint64_t last = 0, last_queued = 0;
PresentWin win = {0};
plat_mutex_lock(&pl);
for (;;) {
while (!pending) plat_cond_wait(&pcv, &pl);
@@ -87,6 +110,8 @@ static void present_main(void *arg) {
busy = 1;
plat_cond_broadcast(&pcv);
plat_mutex_unlock(&pl);
int timed = (rt_trace & (RT_TRACE_PRESENT | RT_TRACE_PERF)) != 0;
uint64_t b0 = timed ? kern_now_ns() : 0;
if (flight_on || vk_dump_white) vk_thumbs_frame(j.frame); // NX2NAVI_DUMP_WHITE reads them too
vk_dlssg_present_frame(j.frame);
vk_lowlat_present_frame(j.frame); // the frame's number for the blit's submit and the present's markers
@@ -104,14 +129,18 @@ static void present_main(void *arg) {
uint64_t t1 = kern_now_ns();
if (last) pace_shown(t1 - last);
vk_present_flip();
uint64_t t2 = timed ? kern_now_ns() : 0;
perfmon_shown(t1); // the overlay's frame times: the pictures as shown
// NX2NAVI_TRACE=present: one line per present: the host present's interval, the interval of the game's present
// calls (when the host present went out before back-edge pacing), the wait and how far past its target it went
// calls (when the host present went out before back-edge pacing), the wait and how far past its target it went;
// then how long the present call took and how long the presenting thread waited to hand this picture over
if (rt_trace & RT_TRACE_PRESENT)
rt_log("present: frame %llu interval %.3f ms, game %.3f ms, waited %.3f ms, %.3f ms past target",
(unsigned long long)j.frame, last ? (t1 - last) / 1e6 : 0.0,
rt_log("present: frame %llu interval %.3f ms, game %.3f ms, waited %.3f ms, %.3f ms past target; present call "
"%.3f ms, hand-off %.3f ms", (unsigned long long)j.frame, last ? (t1 - last) / 1e6 : 0.0,
last_queued ? (j.queued - last_queued) / 1e6 : 0.0, (t1 - t0) / 1e6,
j.at && t1 > j.at ? (t1 - j.at) / 1e6 : 0.0);
j.at && t1 > j.at ? (t1 - j.at) / 1e6 : 0.0, (t2 - t1) / 1e6, j.handoff_ns / 1e6);
if (rt_trace & RT_TRACE_PERF)
present_window(&win, j.frame, t0 - b0, t1 - t0, t2 - t1, j.at && t1 > j.at ? t1 - j.at : 0, j.handoff_ns);
last = t1;
last_queued = j.queued;
vk_pipeline_autosave(); // starts the autosave thread once
@@ -258,11 +287,16 @@ static void present_image(const tex_t *t, const int crop[4], uint64_t at_ns, uin
if (!plat_thread_spawn(present_main, NULL, "vk present", 0)) rt_fatal("vk: cannot start the present thread");
present_thread = 1;
}
while (pending) plat_cond_wait(&pcv, &pl); // one present in waiting at most
// One present in waiting at most: the wait for the present thread to take the previous one is the hand-off
// (PERF_HANDOFF), timed from under pl, where pending is read.
uint64_t h0 = pending ? perf_begin() : 0;
while (pending) plat_cond_wait(&pcv, &pl);
j.handoff_ns = h0 ? kern_now_ns() - h0 : 0;
job = j;
pending = 1;
plat_cond_broadcast(&pcv);
plat_mutex_unlock(&pl);
perf_end(PERF_HANDOFF, h0);
if (rdoc_active()) { vk_present_drain(); rdoc_after_present(frame); }
}