GPU timeline (NX2NAVI_TRACE=perf): every 60 frames, what the GPU's idle time was spent waiting for.

The perf line's "idle with it queued" was the gap before each replay part handed over before the previous one ended:
13-17 ms a frame on an RX 580 in Kokiri Forest on 0.7.1, unexplained. Uploads and presents were not timed, a held
submission (submit batching) counted as handed over while it still waited in vk_sync, and with cuts and pre-pass
command buffers a submission closed more than the four timestamp pairs ts_unsent had room for: the rest went to the
completion queue without a submit time and out of the GPU's order. Now every timed piece of GPU work (replay parts,
mirror prologues, pre-pass command buffers, upload batches, presents) is noted with when it was handed to vk_sync, when
it really went to the queue and its GPU start and end, and every 60 frames two lines split the idle time: starved
(nothing in the queue, and how much of that a held submission waited in the batching) or with work in the queue, by
the kind of piece that started after the gap, with the average delay from hand-over to start on an idle GPU per kind.
ts_unsent has room for every command buffer of a submission, and the pairs are read in queue order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Tobim6
2026-11-12 16:22:42 +01:00
parent 78429f4933
commit 6de2be9ed5
4 changed files with 178 additions and 11 deletions

View File

@@ -140,6 +140,41 @@ static VkImageView g_sc_view[MAX_IMAGES]; // colour attachment views for the m
static VkSemaphore g_render_done[MAX_IMAGES];
static VkCommandPool g_pool;
static VkCommandBuffer g_cb[FRAMES];
// The GPU timeline (NX2NAVI_TRACE=perf, vk.h vk_gtl_note): a timestamp pair per present slot around the present's command
// buffer, read once the slot's fence is waited for again. Its gap before it shows a wait for the acquired image (the
// submission waits on the acquire semaphore) as idle time with work queued before presents.
static VkQueryPool pts_pool;
static int pts_state; // 0 not set up, 1 on, -1 off
static uint8_t pts_on[FRAMES]; // the slot's pair was written by its last present
static uint64_t pts_sub[FRAMES]; // when that present went to the queue
static void pts_begin(VkCtx *c, VkCommandBuffer cb, unsigned slot) {
pts_on[slot] = 0;
if (!(rt_trace & RT_TRACE_PERF) || pts_state < 0) return;
if (!pts_state) {
VkQueryPoolCreateInfo qi = {.sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO, .queryType = VK_QUERY_TYPE_TIMESTAMP,
.queryCount = 2 * FRAMES};
pts_state = c->props.limits.timestampComputeAndGraphics &&
vkCreateQueryPool(c->device, &qi, NULL, &pts_pool) == VK_SUCCESS ? 1 : -1;
if (pts_state < 0) return;
}
vkCmdResetQueryPool(cb, pts_pool, 2 * slot, 2);
vkCmdWriteTimestamp(cb, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, pts_pool, 2 * slot);
pts_on[slot] = 1;
}
static void pts_end(VkCommandBuffer cb, unsigned slot) {
if (pts_on[slot]) vkCmdWriteTimestamp(cb, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, pts_pool, 2 * slot + 1);
}
static void pts_read(VkCtx *c, unsigned slot) { // the slot's fence was waited for: its last present is done
if (!pts_on[slot]) return;
pts_on[slot] = 0;
uint64_t t[2];
if (vkGetQueryPoolResults(c->device, pts_pool, 2 * slot, 2, sizeof t, t, sizeof t[0], VK_QUERY_RESULT_64_BIT) == VK_SUCCESS)
vk_gtl_note(GTL_PRESENT, pts_sub[slot], t[0], t[1]);
}
static VkFence g_fence[FRAMES];
static VkSemaphore g_acquire[FRAMES];
static unsigned g_frame;
@@ -1373,6 +1408,7 @@ static void present_image(VkImage src, VkImageLayout layout, VkFormat fmt, int w
rt_log("gfx: present fence wait timed out");
goto out;
}
pts_read(c, slot); // the GPU timeline (vk_gtl_note): this slot's last present is done
uint32_t idx = 0;
// NGX frame generation: the frame is composed into ng.bb and the presenter shows it (ng_show); no acquire here
const int ngp = ng_ready();
@@ -1457,6 +1493,7 @@ static void present_image(VkImage src, VkImageLayout layout, VkFormat fmt, int w
VkCommandBufferBeginInfo bi = {.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT};
vkBeginCommandBuffer(cb, &bi);
pts_begin(c, cb, slot);
uint64_t crumb = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED);
vk_crumb(cb, VK_CRUMB_PRESENT, crumb);
VkImage dst = ngp ? ng.bb.img : g_sc_img[idx];
@@ -1549,6 +1586,7 @@ static void present_image(VkImage src, VkImageLayout layout, VkFormat fmt, int w
const int via_ng = ng_count >= 0;
if (!via_ng) sync_barrier(cb, dst, last, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, last_stage, last_write, VK_PIPELINE_STAGE_2_NONE, 0);
vk_crumb(cb, VK_CRUMB_PRESENT_END, crumb);
pts_end(cb, slot);
vkEndCommandBuffer(cb);
VkSemaphoreSubmitInfo wait = {.sType = VK_STRUCTURE_TYPE_SEMAPHORE_SUBMIT_INFO, .semaphore = g_acquire[slot],
@@ -1572,8 +1610,10 @@ static void present_image(VkImage src, VkImageLayout layout, VkFormat fmt, int w
if (ng_n > 2) ng_wait_signaled(ng_n - 2);
}
vk_queue_lock();
pts_sub[slot] = plat_now_ns();
r = vkQueueSubmit2(c->gfx_queue, 1, &si, g_fence[slot]);
vk_queue_unlock();
if (r != VK_SUCCESS) pts_on[slot] = 0;
if (r == VK_SUCCESS && via_ng) { // the presenter shows the generated frames, then this one
ng.n = ng_n;
ng_hand(idx, ng_count, ng_set, ng_n);