Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 120 additions & 9 deletions client/conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/prctl.h>
#include <sys/socket.h>
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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++)
Expand All @@ -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);
Expand All @@ -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;
}

Expand Down
100 changes: 100 additions & 0 deletions client/ftrace.c
Original file line number Diff line number Diff line change
@@ -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 <pid> <events>", each event "<code><us since the epoch>,<a>,<b>", with the epoch printed in the first line
* "FT0 <pid> epoch_ns=<CLOCK_MONOTONIC 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 <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

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); }
6 changes: 6 additions & 0 deletions client/vkb_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) */
Expand Down
Loading