diff --git a/client/conn.c b/client/conn.c index 8ffcd36..66e8914 100644 --- a/client/conn.c +++ b/client/conn.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -159,6 +160,22 @@ static uint64_t process_token; * included - what each side pays a syscall, a header and an ordering step for. */ static int stat_period = -1; static uint64_t stat_msgs, stat_wire, stat_msgs_total, stat_wire_total; +/* VKBRIDGE_STATS, time blocked in round trips (vkb_call_exec): summed over the calling threads, the + * longest single round trip, and per command. Only counted while STATS is on. */ +static uint64_t stat_blocked_ns, stat_blocked_max_ns, stat_blocked_cmd_ns[VKB_CMD_COUNT]; +static uint64_t stat_blocked_total_ns, stat_blocked_total_max_ns; +static void stat_blocked_add(uint32_t cmd, uint64_t ns) +{ + __atomic_add_fetch(&stat_blocked_ns, ns, __ATOMIC_RELAXED); + __atomic_add_fetch(&stat_blocked_total_ns, ns, __ATOMIC_RELAXED); + __atomic_add_fetch(&stat_blocked_cmd_ns[cmd], ns, __ATOMIC_RELAXED); + uint64_t m = __atomic_load_n(&stat_blocked_max_ns, __ATOMIC_RELAXED); + while (ns > m && !__atomic_compare_exchange_n(&stat_blocked_max_ns, &m, ns, 1, __ATOMIC_RELAXED, __ATOMIC_RELAXED)) { + } + m = __atomic_load_n(&stat_blocked_total_max_ns, __ATOMIC_RELAXED); + while (ns > m && !__atomic_compare_exchange_n(&stat_blocked_total_max_ns, &m, ns, 1, __ATOMIC_RELAXED, __ATOMIC_RELAXED)) { + } +} static void stat_wire_add(uint64_t msgs, uint64_t bytes) { if (stat_period <= 0) return; @@ -167,6 +184,52 @@ static void stat_wire_add(uint64_t msgs, uint64_t bytes) __atomic_add_fetch(&stat_msgs_total, msgs, __ATOMIC_RELAXED); __atomic_add_fetch(&stat_wire_total, bytes, __ATOMIC_RELAXED); } +/* VKBRIDGE_STATS_SPLIT (default on, only with VKBRIDGE_STATS; 0 = off): each round trip asks the + * server for its timestamps (VKB_F_TIMES) and the interval line is followed by "split" lines that + * divide a command's time into the server's run (for vkWaitForFences: from the wait being issued to + * the fence signalled, the driver's own wake-up included), the wait for earlier async work, the + * transport (request and reply on the socket, thread wake-ups) and the client's own work. */ +enum { SPL_RUN, SPL_BACKLOG, SPL_TRANSPORT, SPL_CLIENT, SPL_N }; +typedef struct { + uint64_t n, sum[SPL_N], max[SPL_N]; +} stat_split; +static stat_split stat_spl[VKB_CMD_COUNT]; +static pthread_mutex_t stat_spl_lock = PTHREAD_MUTEX_INITIALIZER; +typedef struct { + uint64_t t_send, t_recv, s_start, s_end; + uint32_t order_us; + int valid; +} vkb_times; +static __thread vkb_times tls_tm; +static int stat_split_on(void) +{ + static int v = -1; + if (v < 0) { + const char *e = getenv("VKBRIDGE_STATS_SPLIT"); + v = !(e && !strcmp(e, "0")); + } + return v; +} +static void stat_split_add(uint32_t cmd, uint64_t t0, uint64_t t1) +{ + const vkb_times *m = &tls_tm; + if (!m->valid || m->s_end < m->s_start || m->t_recv < m->s_end || m->s_start < m->t_send) return; + uint64_t backlog = (uint64_t)m->order_us * 1000; + uint64_t run = m->s_end - m->s_start; + uint64_t srv_recv = m->s_start > backlog ? m->s_start - backlog : m->s_start; + uint64_t tr = (srv_recv > m->t_send ? srv_recv - m->t_send : 0) + (m->t_recv - m->s_end); + uint64_t cl = (m->t_send - t0) + (t1 - m->t_recv); + uint64_t v[SPL_N] = {run, backlog, tr, cl}; + pthread_mutex_lock(&stat_spl_lock); + stat_split *a = &stat_spl[cmd]; + a->n++; + for (int i = 0; i < SPL_N; i++) { + a->sum[i] += v[i]; + if (v[i] > a->max[i]) a->max[i] = v[i]; + } + pthread_mutex_unlock(&stat_spl_lock); +} + /* The last asynchronous request's sequence number in this process (see vkb_msg_hdr). */ static uint64_t async_seq; static uint32_t fork_epoch; @@ -676,12 +739,16 @@ int vkb_call_exec(vkb_call *c) wait_period = wait_explicit ? atoi(v) : WAIT_STATS_DEFAULT; } int hitch = __atomic_load_n(&vkb_hitch_active, __ATOMIC_RELAXED); - if ((!slow_ms && wait_period <= 0 && !hitch) || (c->flags & VKB_F_NOREPLY)) return exec(c); + int stats = __atomic_load_n(&stat_period, __ATOMIC_RELAXED) > 0; + if ((!slow_ms && wait_period <= 0 && !hitch && !stats) || (c->flags & VKB_F_NOREPLY)) return exec(c); uint64_t t0 = now_ns(); + tls_tm.valid = 0; int r = exec(c); uint64_t t1 = now_ns(), ns = t1 - t0, us = ns / 1000; + if (stats && r && tls_tm.valid) stat_split_add(c->cmd, t0, t1); if (hitch && r) vkb_hitch_rt(c->cmd, ns); /* the per-hitch sums (hitch.c) */ if (wait_period > 0) wait_note(c->cmd, ns, t1); + if (stats) stat_blocked_add(c->cmd, ns); if (slow_ms && us >= (uint64_t)slow_ms * 1000) { struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); @@ -711,6 +778,7 @@ static int exec(vkb_call *c) } vkb_msg_hdr h = {VKB_MAGIC, (uint32_t)c->e.len, c->cmd, c->table, (uint32_t)c->e.nfds, c->flags & VKB_F_NOREPLY, 0, 0}; vkb_stats_count(c->cmd, (c->flags & VKB_F_NOREPLY) != 0, c->e.len); + if (!(c->flags & VKB_F_NOREPLY) && __atomic_load_n(&stat_period, __ATOMIC_RELAXED) > 0 && stat_split_on()) h.flags |= VKB_F_TIMES; pthread_mutex_lock(&aq_lock); if ((c->flags & VKB_F_NOREPLY) && !c->e.nfds && vkb_async_framed()) { /* Appended to the open vkbBatch (or opening it) under the lock that orders the seqs. Requests @@ -814,6 +882,7 @@ static int exec(vkb_call *c) h.barrier = c->flags & VKB_CALL_POLL ? 0 : async_seq; } pthread_mutex_unlock(&aq_lock); + if (h.flags & VKB_F_TIMES) tls_tm.t_send = now_ns(); if (qr < 0 || vkb_send_msg(conn->sock, &h, c->e.buf, c->e.fds, c->e.nfds) < 0) { VKB_ERR("%s: lost the bridge server (%s)", vkb_cmd_names[c->cmd], strerror(errno)); conn->dead = 1; @@ -834,6 +903,11 @@ static int exec(vkb_call *c) c->conn = NULL; return 0; } + if (h.flags & VKB_F_TIMES) { + tls_tm.t_recv = now_ns(); + tls_tm.valid = (rh.flags & VKB_F_TIMES) != 0; + tls_tm.s_start = rh.seq, tls_tm.s_end = rh.barrier, tls_tm.order_us = rh.table; + } for (uint64_t o = __atomic_load_n(&synced_seq, __ATOMIC_RELAXED); o < h.barrier && !__atomic_compare_exchange_n(&synced_seq, &o, h.barrier, 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);) {} if (rh.cmd != VKB_ST_OK) { @@ -1012,9 +1086,10 @@ static void stats_exit(void) s += stat_total_sync[i]; a += stat_total_async[i]; } - vkb_log(VKB_LOG_ERROR, "stats at exit: %llu round trips, %llu async, %llu KB encoded; %llu messages, %llu KB on the wire", + vkb_log(VKB_LOG_ERROR, "stats at exit: %llu round trips, %llu async, %llu KB encoded; %llu messages, %llu KB on the wire; blocked %llu ms, max %.1f ms", (unsigned long long)s, (unsigned long long)a, (unsigned long long)(stat_total_bytes >> 10), - (unsigned long long)stat_msgs_total, (unsigned long long)(stat_wire_total >> 10)); + (unsigned long long)stat_msgs_total, (unsigned long long)(stat_wire_total >> 10), + (unsigned long long)(stat_blocked_total_ns / 1000000), stat_blocked_total_max_ns / 1e6); uint64_t refused = stat_total_refused + __atomic_exchange_n(&vkb_stat_refused, 0, __ATOMIC_RELAXED); uint64_t full = stat_total_full + __atomic_exchange_n(&vkb_stat_pool_full, 0, __ATOMIC_RELAXED); if (refused || full) @@ -1026,6 +1101,35 @@ static void stats_exit(void) (unsigned long long)stat_total_async[i]); } +/* One line per command whose round trips took 20 ms (VKBRIDGE_STATS_SPLIT_MS) or more in the interval (at most 6, longest + * first): count, then avg/max of each part in ms (see VKBRIDGE_STATS_SPLIT above). */ +static void stat_split_print(void) +{ + stat_split snap[VKB_CMD_COUNT]; + pthread_mutex_lock(&stat_spl_lock); + memcpy(snap, stat_spl, sizeof(snap)); + memset(stat_spl, 0, sizeof(stat_spl)); + pthread_mutex_unlock(&stat_spl_lock); + const char *me = getenv("VKBRIDGE_STATS_SPLIT_MS"); + uint64_t min_ns = (me && *me ? strtoull(me, NULL, 10) : 20ull) * 1000000ull; + for (int k = 0; k < 6; k++) { + uint64_t best = 0; + uint32_t bi = 0; + for (uint32_t i = 0; i < VKB_CMD_COUNT; i++) { + uint64_t tot = snap[i].sum[0] + snap[i].sum[1] + snap[i].sum[2] + snap[i].sum[3]; + if (snap[i].n && tot > best) best = tot, bi = i; + } + if (best < min_ns) break; + const stat_split *a = &snap[bi]; + double n = (double)a->n; + vkb_log(VKB_LOG_ERROR, + " split %-28s %5llu calls; ms avg/max: server-run %.2f/%.2f, async-backlog %.2f/%.2f, transport+wake %.2f/%.2f, client %.2f/%.2f", + vkb_cmd_names[bi], (unsigned long long)a->n, a->sum[0] / n / 1e6, a->max[0] / 1e6, a->sum[1] / n / 1e6, a->max[1] / 1e6, + a->sum[2] / n / 1e6, a->max[2] / 1e6, a->sum[3] / n / 1e6, a->max[3] / 1e6); + snap[bi].n = 0; + } +} + void vkb_stats_count(uint32_t cmd, int async, size_t bytes) { if (stat_period < 0) { @@ -1056,10 +1160,13 @@ void vkb_stats_count(uint32_t cmd, int async, size_t bytes) tot_s += stat_sync[i]; tot_a += stat_async[i]; } - vkb_log(VKB_LOG_ERROR, "stats over %us: %u round trips, %u async, %llu KB encoded; %llu messages, %llu KB on the wire", period, - tot_s, tot_a, (unsigned long long)(stat_bytes >> 10), + uint64_t blocked = __atomic_exchange_n(&stat_blocked_ns, 0, __ATOMIC_RELAXED); + uint64_t blocked_max = __atomic_exchange_n(&stat_blocked_max_ns, 0, __ATOMIC_RELAXED); + vkb_log(VKB_LOG_ERROR, "stats over %us: %u round trips, %u async, %llu KB encoded; %llu messages, %llu KB on the wire; blocked %llu ms, max %.1f ms", + period, tot_s, tot_a, (unsigned long long)(stat_bytes >> 10), (unsigned long long)__atomic_exchange_n(&stat_msgs, 0, __ATOMIC_RELAXED), - (unsigned long long)(__atomic_exchange_n(&stat_wire, 0, __ATOMIC_RELAXED) >> 10)); + (unsigned long long)(__atomic_exchange_n(&stat_wire, 0, __ATOMIC_RELAXED) >> 10), + (unsigned long long)(blocked / 1000000), blocked_max / 1e6); for (int k = 0; k < 8; k++) { uint32_t best = 0, bi = 0; for (uint32_t i = 0; i < VKB_CMD_COUNT; i++) @@ -1068,14 +1175,17 @@ void vkb_stats_count(uint32_t cmd, int async, size_t bytes) bi = i; } if (!best) break; - vkb_log(VKB_LOG_ERROR, " %-40s %6u sync %6u async %7llu KB", vkb_cmd_names[bi], stat_sync[bi], stat_async[bi], - (unsigned long long)(stat_cmd_bytes[bi] >> 10)); + vkb_log(VKB_LOG_ERROR, " %-40s %6u sync %6u async %7llu KB, blocked %llu ms", vkb_cmd_names[bi], stat_sync[bi], stat_async[bi], + (unsigned long long)(stat_cmd_bytes[bi] >> 10), + (unsigned long long)(__atomic_exchange_n(&stat_blocked_cmd_ns[bi], 0, __ATOMIC_RELAXED) / 1000000)); stat_sync[bi] = stat_async[bi] = 0; } /* Round trips are what cost: the rest of them too, from one a second. */ for (uint32_t i = 0; i < VKB_CMD_COUNT; i++) if (stat_sync[i] >= (uint32_t)period) - vkb_log(VKB_LOG_ERROR, " %-40s %6u sync %6u async", vkb_cmd_names[i], stat_sync[i], stat_async[i]); + vkb_log(VKB_LOG_ERROR, " %-40s %6u sync %6u async, blocked %llu ms", vkb_cmd_names[i], stat_sync[i], stat_async[i], + (unsigned long long)(__atomic_exchange_n(&stat_blocked_cmd_ns[i], 0, __ATOMIC_RELAXED) / 1000000)); + stat_split_print(); uint32_t refused = __atomic_exchange_n(&vkb_stat_refused, 0, __ATOMIC_RELAXED); if (refused) vkb_log(VKB_LOG_ERROR, " descriptor set allocations refused: %u", refused); uint32_t full = __atomic_exchange_n(&vkb_stat_pool_full, 0, __ATOMIC_RELAXED); @@ -1084,6 +1194,7 @@ void vkb_stats_count(uint32_t cmd, int async, size_t bytes) memset(stat_sync, 0, sizeof(stat_sync)); memset(stat_async, 0, sizeof(stat_async)); memset(stat_cmd_bytes, 0, sizeof(stat_cmd_bytes)); + memset(stat_blocked_cmd_ns, 0, sizeof(stat_blocked_cmd_ns)); stat_bytes = 0; } diff --git a/client/ftrace.c b/client/ftrace.c new file mode 100644 index 0000000..56600fb --- /dev/null +++ b/client/ftrace.c @@ -0,0 +1,100 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright (C) 2026 ASEbrahim and MaliBridge contributors + * + * VKBRIDGE_FRAME_TRACE=1: a per-frame event trace of the presenting path, for finding where frames are lost (default off: one + * load of vkb_ft_on at each site). Events go into a ring (one atomic add and four stores each) and a thread writes them out every + * 250 ms as log lines "FT ", each event ",,", with the epoch printed in the first line + * "FT0 epoch_ns=" (the clock of the latency log, VKBRIDGE_LATLOG). The lines are always logged (as the + * wait statistics are), so they are in the guest process's log. Codes: see the sites (wsi.c, wsi_wayland.c); the + * code letters are listed in the comment above vkb_ft_event. + */ +#define _GNU_SOURCE +#include "vkb_client.h" + +#include +#include +#include +#include +#include +#include + +int vkb_ft_on; + +#define RINGN (1u << 16) +typedef struct ft_ev { + uint64_t ns; + int64_t b; + int32_t a; + uint32_t code; + uint32_t seq; /* written last: index + 1 */ +} ft_ev; + +static ft_ev ring[RINGN]; +static uint64_t wr, rd, epoch; +static pthread_once_t once = PTHREAD_ONCE_INIT; + +static uint64_t now_ns(void) +{ + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; +} + +/* Codes. wsi.c: a acquire entered, d/D acquire fence wait start/end, A acquired, f no free image, Q/q present entered/left, w/W frame-queue + * wait start/end. wsi_wayland.c: m swapchain created, P present thread took a frame, g its fence wait ended, b/c/M wl_present: callback + * outstanding, commit started, commit sent; F frame callback, L buffer release, S presentation feedback (seq, ns), X frame discarded. */ +void vkb_ft_event(char code, int32_t a, int64_t b) +{ + uint64_t i = __atomic_fetch_add(&wr, 1, __ATOMIC_RELAXED); + ft_ev *e = &ring[i % RINGN]; + e->ns = now_ns(); + e->a = a; + e->b = b; + e->code = (uint32_t)code; + __atomic_store_n(&e->seq, (uint32_t)(i + 1), __ATOMIC_RELEASE); +} + +static void *writer(void *arg) +{ + (void)arg; + pthread_setname_np(pthread_self(), "vkb-ftrace"); + char line[1900]; + for (;;) { + struct timespec ts = {0, 250000000}; + nanosleep(&ts, NULL); + uint64_t end = __atomic_load_n(&wr, __ATOMIC_ACQUIRE); + if (end - rd > RINGN - 64) { + VKB_INFO_ALWAYS("FT lost %llu events", (unsigned long long)(end - rd - (RINGN - 64))); + rd = end - (RINGN - 64); + } + size_t n = 0; + while (rd < end) { + ft_ev *e = &ring[rd % RINGN]; + if (__atomic_load_n(&e->seq, __ATOMIC_ACQUIRE) != (uint32_t)(rd + 1)) break; /* not complete yet: next time */ + if (n > sizeof(line) - 64) { + VKB_INFO_ALWAYS("FT %d%s", (int)getpid(), line); + n = 0; + } + n += (size_t)snprintf(line + n, sizeof(line) - n, " %c%lld,%d,%lld", (char)e->code, (long long)((e->ns - epoch) / 1000), e->a, + (long long)e->b); + rd++; + } + if (n) VKB_INFO_ALWAYS("FT %d%s", (int)getpid(), line); + } + return NULL; +} + +static void start(void) +{ + const char *e = getenv("VKBRIDGE_FRAME_TRACE"); + if (!e || !*e || !strcmp(e, "0")) return; + epoch = now_ns(); + VKB_INFO_ALWAYS("FT0 %d epoch_ns=%llu", (int)getpid(), (unsigned long long)epoch); + pthread_t t; + if (pthread_create(&t, NULL, writer, NULL) == 0) { + pthread_detach(t); + __atomic_store_n(&vkb_ft_on, 1, __ATOMIC_RELEASE); + } +} + +void vkb_ft_init(void) { pthread_once(&once, start); } diff --git a/client/vkb_client.h b/client/vkb_client.h index ad4f0c1..b4534da 100644 --- a/client/vkb_client.h +++ b/client/vkb_client.h @@ -300,6 +300,12 @@ void vkb_log_info_always(const char *fmt, ...) __attribute__((format(printf, 1, /* hitch.c: the per-hitch line's client half (VKBRIDGE_HITCH_MS). vkb_hitch_active: 0 while off. */ extern int vkb_hitch_active; +uint32_t frame_queue_value(void); /* wsi.c: VKBRIDGE_FRAME_QUEUE as in effect */ +extern int vkb_ft_on; /* ftrace.c: VKBRIDGE_FRAME_TRACE */ +void vkb_ft_init(void); +void vkb_ft_event(char code, int32_t a, int64_t b); +#define VKB_FT(c, a, b) do { if (__builtin_expect(__atomic_load_n(&vkb_ft_on, __ATOMIC_RELAXED), 0)) vkb_ft_event((c), (int32_t)(a), (int64_t)(b)); } while (0) +#define VKB_INFO_ALWAYS(...) vkb_log_info_always(__VA_ARGS__) extern __thread int vkb_hitch_in_present; /* inside the swapchain's own wait (wsi.c) */ uint64_t vkb_hitch_now(void); void vkb_hitch_server_ms(uint32_t ms); /* vkbQueryServer's hitch_ms (physdev.c) */ diff --git a/client/wsi.c b/client/wsi.c index 9b897d7..494b9d6 100644 --- a/client/wsi.c +++ b/client/wsi.c @@ -1105,16 +1105,20 @@ static VkResult acquire(VkDevice device, VkSwapchainKHR swapchain, uint64_t time if (sc->retired) return VK_ERROR_OUT_OF_DATE_KHR; if (sc->status != VK_SUCCESS) return sc->status; uint64_t deadline = timeout == UINT64_MAX ? UINT64_MAX : now_ns() + timeout; + VKB_FT('a', 0, timeout == UINT64_MAX ? -1 : (int64_t)timeout); for (;;) { pthread_mutex_lock(&sc->lock); for (uint32_t i = 0; i < sc->count; i++) { if (!sc->images[i].busy && !sc->images[i].acquired) { sc->images[i].acquired = 1; pthread_mutex_unlock(&sc->lock); + VKB_FT('d', i, 0); if (sc->ahb) wait_display_release(&sc->images[i]); + VKB_FT('D', i, 0); VkResult r = signal_acquire(vkb_dev(device), semaphore, fence); if (r != VK_SUCCESS) return r; *pImageIndex = i; + VKB_FT('A', i, 0); return sc->status; } } @@ -1125,6 +1129,7 @@ static VkResult acquire(VkDevice device, VkSwapchainKHR swapchain, uint64_t time /* a late copy present holds an image until shown: wait for it, not for the window system */ if (late_wait(sc, deadline)) continue; uint64_t ht0 = vkb_hitch_active ? now_ns() : 0; + VKB_FT('f', 0, 0); /* no free image: waiting for a release */ VkResult r = sc->backend->wait_free(sc, deadline == UINT64_MAX ? UINT64_MAX : deadline - now); if (ht0) vkb_hitch_waited(VKB_HITCH_W_PRESENT, VKB_CMD_COUNT, now_ns() - ht0); if (r < 0) { @@ -1235,6 +1240,8 @@ static uint32_t frame_queue(void) return n; } +uint32_t frame_queue_value(void) { return frame_queue(); } + VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR *pPresentInfo) { const VkPresentRegionsKHR *regions = NULL; @@ -1256,6 +1263,7 @@ VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkQueuePresentKHR(VkQueue queue, const VkP for (uint32_t i = 0; i < pPresentInfo->swapchainCount; i++) { vkb_swapchain *sc = chain(pPresentInfo->pSwapchains[i]); uint32_t index = pPresentInfo->pImageIndices[i]; + VKB_FT('Q', index, pPresentInfo->waitSemaphoreCount); VkResult r = sc->status; if (index >= sc->count) r = VK_ERROR_OUT_OF_DATE_KHR; uint32_t nwait = i == 0 ? pPresentInfo->waitSemaphoreCount : 0; @@ -1285,7 +1293,9 @@ VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkQueuePresentKHR(VkQueue queue, const VkP uint32_t fq = r >= 0 && sc->backend->wait_queue ? frame_queue() : 0; if (fq) { uint64_t ht0 = vkb_hitch_active ? now_ns() : 0; + VKB_FT('w', 0, fq); sc->backend->wait_queue(sc, fq - 1); + VKB_FT('W', 0, 0); if (ht0) vkb_hitch_waited(VKB_HITCH_W_PRESENT, VKB_CMD_COUNT, now_ns() - ht0); } goto done; @@ -1318,6 +1328,7 @@ VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkQueuePresentKHR(VkQueue queue, const VkP vkb_lat_presented(sc->lat_n, r >= 0); sc->lat_n = 0; done: + VKB_FT('q', index, r); if (r < 0) { pthread_mutex_lock(&sc->lock); if (index < sc->count) sc->images[index].busy = 0; diff --git a/client/wsi_wayland.c b/client/wsi_wayland.c index e43880a..3df815a 100644 --- a/client/wsi_wayland.c +++ b/client/wsi_wayland.c @@ -331,7 +331,8 @@ static void fb_sync_output(void *data, struct wl_proxy *fb, struct wl_proxy *out static void fb_presented(void *data, struct wl_proxy *fb, uint32_t sec_hi, uint32_t sec_lo, uint32_t nsec, uint32_t refresh, uint32_t seq_hi, uint32_t seq_lo, uint32_t flags) { - (void)refresh, (void)seq_hi, (void)seq_lo, (void)flags; + (void)refresh, (void)seq_hi, (void)flags; + VKB_FT('S', seq_lo, (((uint64_t)sec_hi << 32) | sec_lo) * 1000000000ull + nsec); wl_state *s = data; uint64_t pres = (((uint64_t)sec_hi << 32) | sec_lo) * 1000000000ull + nsec, id; uint32_t n = fb_forget(s, fb, &id); @@ -345,6 +346,7 @@ static void fb_discarded(void *data, struct wl_proxy *fb) { wl_state *s = data; uint64_t id; + VKB_FT('X', 0, 0); vkb_lat_flag(fb_forget(s, fb, &id), VKB_LAT_F_DISCARDED); vkb_wsi_pw_shown(s->sc, id); } @@ -415,6 +417,7 @@ static void buffer_release(void *data, struct wl_proxy *buffer) { (void)buffer; vkb_swap_image *img = data; + VKB_FT('L', img - img->chain->images, 0); pthread_mutex_lock(&img->chain->lock); img->busy = 0; pthread_mutex_unlock(&img->chain->lock); @@ -432,8 +435,8 @@ static const void *shm_buffer_listener[] = {(void *)shm_buffer_release}; static void frame_done(void *data, struct wl_proxy *cb, uint32_t t) { - (void)t; wl_state *s = data; + VKB_FT('F', t, 0); if (s->frame == cb) s->frame = NULL; wl.proxy_destroy(cb); } @@ -510,9 +513,11 @@ static void *present_thread(void *arg) } pthread_mutex_unlock(&sc->lock); if (have) { + VKB_FT('P', index, s->len); /* A slow frame (a game loading) is waited for; only a real loss or a frame past the limit * (VKBRIDGE_PRESENT_TIMEOUT_S) ends the swapchain (wsi.c) */ VkResult r = vkb_wsi_wait_frame(sc, s->fences[index]); + VKB_FT('g', index, r); if (r >= 0) vkb_lat_stamp(lat, VKB_LAT_DONE, 0); pthread_mutex_lock(&sc->lock); s->rendering--; /* a present waiting for the frame queue (wl_wait_queue) may go on */ @@ -573,6 +578,11 @@ static VkResult wl_present_async(vkb_swapchain *sc, VkQueue queue, uint32_t inde VkFenceCreateInfo fci = {VK_STRUCTURE_TYPE_FENCE_CREATE_INFO}; for (uint32_t i = 0; i < sc->count; i++) if (vkb_ep_vkCreateFence(device, &fci, NULL, &s->fences[i]) != VK_SUCCESS) return VK_ERROR_OUT_OF_HOST_MEMORY; + vkb_ft_init(); + if (vkb_ft_on) + VKB_INFO_ALWAYS("swapchain %ux%u: present mode %d, %u images, format %d, VKBRIDGE_FRAME_QUEUE %u", sc->info.imageExtent.width, + sc->info.imageExtent.height, (int)sc->info.presentMode, sc->count, (int)sc->info.imageFormat, frame_queue_value()); + VKB_FT('m', sc->info.presentMode, sc->count); pthread_cond_init(&s->changed, NULL); if (pthread_create(&s->thread, NULL, present_thread, sc) != 0) return VK_ERROR_OUT_OF_HOST_MEMORY; s->thread_on = 1; @@ -965,6 +975,7 @@ static VkResult wl_present(vkb_swapchain *sc, uint32_t index, const VkPresentReg if (sc->info.presentMode == VK_PRESENT_MODE_FIFO_KHR || sc->info.presentMode == VK_PRESENT_MODE_FIFO_RELAXED_KHR) { struct timespec t0; clock_gettime(CLOCK_MONOTONIC, &t0); + VKB_FT('b', s->frame != NULL, 0); /* the previous frame's callback: is it still outstanding? */ while (s->frame) { if (dispatch_timeout(s, 20000000ull) < 0) return VK_ERROR_SURFACE_LOST_KHR; struct timespec t1; @@ -973,6 +984,7 @@ static VkResult wl_present(vkb_swapchain *sc, uint32_t index, const VkPresentReg } } struct wl_proxy *buffer; + VKB_FT('c', index, 0); if (!sc->copy_present) { buffer = s->buffers[index]; } else { @@ -1027,6 +1039,7 @@ static VkResult wl_present(vkb_swapchain *sc, uint32_t index, const VkPresentReg } wl.proxy_marshal(s->surface_wrapper, WL_SURFACE_COMMIT); wl.display_flush(s->display); + VKB_FT('M', index, 0); if (wl.display_dispatch_queue_pending(s->display, s->queue) < 0) return VK_ERROR_SURFACE_LOST_KHR; return VK_SUCCESS; } diff --git a/common/vkb_wire.h b/common/vkb_wire.h index c28ea3b..78f7b07 100644 --- a/common/vkb_wire.h +++ b/common/vkb_wire.h @@ -136,6 +136,11 @@ typedef struct vkb_msg_hdr { } vkb_msg_hdr; #define VKB_MAGIC 0x32424b56u /* "VKB2" */ #define VKB_F_NOREPLY 1u +/* Request flag (VKBRIDGE_STATS on the client): the reply says when the server received the request + * and ran it. Reply header, flags = VKB_F_TIMES when present: seq = CLOCK_MONOTONIC ns the request + * began to run (after the wait for earlier async work), barrier = ns it was done, table = us it + * waited for earlier async work. One kernel, one clock. An older server answers with zeros. */ +#define VKB_F_TIMES 2u /* Reply status codes (vkb_msg_hdr.cmd in replies). */ #define VKB_ST_OK 0u diff --git a/server/server_conn.c b/server/server_conn.c index 5e0bd3b..ba8b63d 100644 --- a/server/server_conn.c +++ b/server/server_conn.c @@ -1686,7 +1686,7 @@ static void conn_loop(void *arg) if (srv_recv(conn, &st->h, &payload, st->fds, &nfds) < 0) break; vkb_msg_hdr h = st->h; tls_proc = conn->proc; /* set by its hello */ - uint64_t wait_ns = 0; + uint64_t wait_ns = 0, tm_recv = (h.flags & VKB_F_TIMES) ? mono_ns() : 0, tm_start = 0; if (conn->proc && h.cmd < VKB_CMD_COUNT && h.cmd != VKB_CMD_vkbHello) { uint64_t t0 = st_period ? mono_ns() : 0; int oms = vkb_syncfd_order_ms(h.table, h.cmd); /* server_syncfd.c: a sync-fd export's deadline */ @@ -1697,6 +1697,7 @@ static void conn_loop(void *arg) } if (st_period) wait_ns = mono_ns() - t0; } + if (tm_recv) tm_start = mono_ns(); if (h.cmd == VKB_CMD_vkbBatch && (h.flags & VKB_F_NOREPLY) && !nfds && conn->proc) { st->busy = 2; run_batch(conn, &st->c, payload, h.size, wait_ns); @@ -1712,6 +1713,13 @@ static void conn_loop(void *arg) if (!(h.flags & VKB_F_NOREPLY)) { vkb_msg_hdr rh = {VKB_MAGIC, st->c.status == VKB_ST_OK ? (uint32_t)st->c.r.len : 0, st->c.status, 0, st->c.status == VKB_ST_OK ? (uint32_t)st->c.r.nfds : 0, 0, 0, 0}; + if (tm_recv) { + uint64_t us = (tm_start - tm_recv) / 1000; + rh.flags = VKB_F_TIMES; + rh.seq = tm_start; + rh.barrier = mono_ns(); + rh.table = us > 0xffffffffu ? 0xffffffffu : (uint32_t)us; + } if (st->c.r.oom) { rh.cmd = VKB_ST_BAD_MESSAGE; rh.size = 0;