mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-20 05:16:01 +00:00
perf(vkbridge): async requests with ordering, batched descriptor sets, buffered server reads
- Ordered async protocol: vkb_msg_hdr seq/barrier, per-process ordering gate on the server; void commands and VkResult commands without outputs (submits, resets, binds, vkEndCommandBuffer replay) no longer wait for a reply. VKBRIDGE_SYNC=1 restores synchronous calls for debugging. - Client async queue batched across threads with a ~100us background flusher. - Descriptor sets allocated in batches per (pool, layout); the client keeps the pool's books (layout descriptor counts vs pool sizes) so batches ask only for what fits and a full pool answers OUT_OF_POOL_MEMORY without a round trip. Caches for a destroyed layout are dropped (its handle may be reused). - Server parses requests out of one receive buffer (descriptor FIFO for SCM_RIGHTS) instead of two reads per message. - Server logs async VkResult failures (generated vkb_cmd_result table) and per-command timing with VKBRIDGE_STATS=<s>. - tests/d3d11_bench.c: game-like D3D11 frame (1000 draws, Map DISCARD, rebinds). d3d11_bench 1000 draws on RADV: native 0.30 ms/frame; bridge 26.3 ms (sync) -> 1.43 ms. Host suite 14/14 in plain and Mali-imitation modes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
308b200350
commit
a4bd2eb271
14 files changed
+1247
-314
No files matched your search
@@ -11,6 +11,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static pthread_mutex_t tables_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
@@ -126,6 +127,8 @@ static vkb_srv_proc *proc_join(uint64_t token, int pid)
|
||||
p = calloc(1, sizeof(*p));
|
||||
p->token = token;
|
||||
p->pid = pid;
|
||||
pthread_mutex_init(&p->olock, NULL);
|
||||
pthread_cond_init(&p->ocond, NULL);
|
||||
p->next = procs;
|
||||
procs = p;
|
||||
VKB_INFO("client process %d connected", pid);
|
||||
@@ -148,10 +151,53 @@ static void proc_leave(vkb_srv_proc *p)
|
||||
if (last) {
|
||||
vkb_proc_cleanup(p);
|
||||
VKB_INFO("client process %d gone", p->pid);
|
||||
pthread_mutex_destroy(&p->olock);
|
||||
pthread_cond_destroy(&p->ocond);
|
||||
free(p);
|
||||
} else {
|
||||
pthread_mutex_lock(&p->olock);
|
||||
p->conn_closed = 1;
|
||||
pthread_cond_broadcast(&p->ocond);
|
||||
pthread_mutex_unlock(&p->olock);
|
||||
}
|
||||
}
|
||||
|
||||
/* Waits until every async request of the process up to `barrier` has run. */
|
||||
static void order_wait(vkb_srv_proc *p, uint64_t barrier)
|
||||
{
|
||||
if (!p || !barrier) return;
|
||||
pthread_mutex_lock(&p->olock);
|
||||
int waited = 0;
|
||||
while (p->watermark < barrier) {
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_REALTIME, &ts);
|
||||
ts.tv_sec += 1;
|
||||
if (pthread_cond_timedwait(&p->ocond, &p->olock, &ts) != 0 && ++waited >= 3 && p->conn_closed) {
|
||||
/* A connection of this process is gone with requests it never sent: go on. */
|
||||
VKB_WARN("process %d: async request %llu never arrived; continuing", p->pid, (unsigned long long)(p->watermark + 1));
|
||||
p->watermark = barrier;
|
||||
memset(p->done, 0, sizeof(p->done));
|
||||
break;
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&p->olock);
|
||||
}
|
||||
|
||||
static void order_done(vkb_srv_proc *p, uint64_t seq)
|
||||
{
|
||||
if (!p || !seq) return;
|
||||
pthread_mutex_lock(&p->olock);
|
||||
if (seq > p->watermark) {
|
||||
p->done[seq % VKB_SEQ_RING] = 1;
|
||||
while (p->done[(p->watermark + 1) % VKB_SEQ_RING]) {
|
||||
p->done[(p->watermark + 1) % VKB_SEQ_RING] = 0;
|
||||
p->watermark++;
|
||||
}
|
||||
pthread_cond_broadcast(&p->ocond);
|
||||
}
|
||||
pthread_mutex_unlock(&p->olock);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ command buffer replay */
|
||||
|
||||
static int is_stream_cmd(uint32_t id)
|
||||
@@ -238,8 +284,144 @@ void vkb_sv_vkbHello(vkb_srv_call *c)
|
||||
vkb_enc_u32(&c->r, 1);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ statistics */
|
||||
|
||||
/* VKBRIDGE_STATS=<seconds> on the server: time spent per command, waiting for order, idle. */
|
||||
static uint64_t st_ns[VKB_CMD_COUNT], st_n[VKB_CMD_COUNT], st_wait_ns, st_last;
|
||||
static int st_period = -1;
|
||||
|
||||
static uint64_t mono_ns(void)
|
||||
{
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec;
|
||||
}
|
||||
|
||||
static void stats_add(uint32_t cmd, uint64_t ns, uint64_t wait_ns)
|
||||
{
|
||||
if (st_period < 0) {
|
||||
const char *v = getenv("VKBRIDGE_STATS");
|
||||
st_period = v ? atoi(v) : 0;
|
||||
}
|
||||
if (!st_period) return;
|
||||
__atomic_add_fetch(&st_ns[cmd], ns, __ATOMIC_RELAXED);
|
||||
__atomic_add_fetch(&st_n[cmd], 1, __ATOMIC_RELAXED);
|
||||
__atomic_add_fetch(&st_wait_ns, wait_ns, __ATOMIC_RELAXED);
|
||||
uint64_t now = mono_ns(), last = __atomic_load_n(&st_last, __ATOMIC_RELAXED);
|
||||
if (!last) {
|
||||
__atomic_store_n(&st_last, now, __ATOMIC_RELAXED);
|
||||
return;
|
||||
}
|
||||
if (now - last < (uint64_t)st_period * 1000000000ull || !__atomic_compare_exchange_n(&st_last, &last, now, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED))
|
||||
return;
|
||||
uint64_t total = 0;
|
||||
for (uint32_t i = 0; i < VKB_CMD_COUNT; i++) total += st_ns[i];
|
||||
VKB_INFO("stats over %ds: %.1f ms in handlers, %.1f ms waiting for order", st_period, total / 1e6, st_wait_ns / 1e6);
|
||||
for (int k = 0; k < 8; k++) {
|
||||
uint64_t best = 0;
|
||||
uint32_t bi = 0;
|
||||
for (uint32_t i = 0; i < VKB_CMD_COUNT; i++)
|
||||
if (st_ns[i] > best) {
|
||||
best = st_ns[i];
|
||||
bi = i;
|
||||
}
|
||||
if (!best) break;
|
||||
VKB_INFO(" %-40s %8llu calls %9.2f ms (%.2f us each)", vkb_cmd_names[bi], (unsigned long long)st_n[bi], best / 1e6,
|
||||
best / 1e3 / (double)st_n[bi]);
|
||||
st_ns[bi] = 0;
|
||||
}
|
||||
memset(st_ns, 0, sizeof(st_ns));
|
||||
memset(st_n, 0, sizeof(st_n));
|
||||
st_wait_ns = 0;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ connection loop */
|
||||
|
||||
/* Reads the next request. Buffered: a burst of small asynchronous requests costs one recvmsg, not
|
||||
* two reads each. Descriptors arrive with the first bytes of their message, so they queue up in
|
||||
* order and each header takes its nfds from the front. The payload points into the buffer and
|
||||
* stays valid until the next call. */
|
||||
static int srv_recv(vkb_srv_conn *conn, vkb_msg_hdr *h, uint8_t **payload, int *fds, int *nfds)
|
||||
{
|
||||
*nfds = 0;
|
||||
if (conn->rpos == conn->rlen && conn->rcap > (16u << 20)) {
|
||||
free(conn->rb);
|
||||
conn->rb = NULL;
|
||||
conn->rcap = conn->rpos = conn->rlen = 0;
|
||||
}
|
||||
for (;;) {
|
||||
size_t avail = conn->rlen - conn->rpos;
|
||||
size_t need = sizeof(*h);
|
||||
if (avail >= sizeof(*h)) {
|
||||
memcpy(h, conn->rb + conn->rpos, sizeof(*h));
|
||||
if (h->magic != VKB_MAGIC) {
|
||||
VKB_ERR("transport: bad message magic 0x%08x", h->magic);
|
||||
return -1;
|
||||
}
|
||||
if (h->size > (1u << 30) || h->nfds > VKB_MAX_FDS) {
|
||||
VKB_ERR("transport: bad message (%u bytes, %u descriptors)", h->size, h->nfds);
|
||||
return -1;
|
||||
}
|
||||
need += h->size;
|
||||
if (avail >= need) {
|
||||
if ((int)h->nfds > conn->fdn) {
|
||||
VKB_ERR("transport: message arrived without its %u descriptors", h->nfds);
|
||||
return -1;
|
||||
}
|
||||
*nfds = (int)h->nfds;
|
||||
memcpy(fds, conn->fdq, sizeof(int) * h->nfds);
|
||||
conn->fdn -= (int)h->nfds;
|
||||
memmove(conn->fdq, conn->fdq + h->nfds, sizeof(int) * conn->fdn);
|
||||
*payload = conn->rb + conn->rpos + sizeof(*h);
|
||||
conn->rpos += need;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (conn->rpos) {
|
||||
memmove(conn->rb, conn->rb + conn->rpos, avail);
|
||||
conn->rpos = 0;
|
||||
conn->rlen = avail;
|
||||
}
|
||||
if (need < (256u << 10)) need = 256u << 10;
|
||||
if (need > conn->rcap) {
|
||||
size_t nc = conn->rcap ? conn->rcap : (256u << 10);
|
||||
while (nc < need) nc *= 2;
|
||||
uint8_t *nb = realloc(conn->rb, nc);
|
||||
if (!nb) return -1;
|
||||
conn->rb = nb;
|
||||
conn->rcap = nc;
|
||||
}
|
||||
union {
|
||||
char buf[CMSG_SPACE(sizeof(int) * VKB_MAX_FDS)];
|
||||
struct cmsghdr align;
|
||||
} cm;
|
||||
struct iovec iov = {conn->rb + conn->rlen, conn->rcap - conn->rlen};
|
||||
struct msghdr msg;
|
||||
memset(&msg, 0, sizeof(msg));
|
||||
msg.msg_iov = &iov;
|
||||
msg.msg_iovlen = 1;
|
||||
msg.msg_control = cm.buf;
|
||||
msg.msg_controllen = sizeof(cm.buf);
|
||||
ssize_t r;
|
||||
do {
|
||||
r = recvmsg(conn->sock, &msg, MSG_CMSG_CLOEXEC);
|
||||
} while (r < 0 && errno == EINTR);
|
||||
if (r <= 0) return -1;
|
||||
for (struct cmsghdr *cmh = CMSG_FIRSTHDR(&msg); cmh; cmh = CMSG_NXTHDR(&msg, cmh)) {
|
||||
if (cmh->cmsg_level != SOL_SOCKET || cmh->cmsg_type != SCM_RIGHTS) continue;
|
||||
int n = (int)((cmh->cmsg_len - CMSG_LEN(0)) / sizeof(int));
|
||||
for (int i = 0; i < n; i++) {
|
||||
int fd;
|
||||
memcpy(&fd, CMSG_DATA(cmh) + i * sizeof(int), sizeof(int));
|
||||
if (conn->fdn < (int)(sizeof(conn->fdq) / sizeof(conn->fdq[0]))) conn->fdq[conn->fdn++] = fd;
|
||||
else close(fd);
|
||||
}
|
||||
}
|
||||
if (msg.msg_flags & MSG_CTRUNC) VKB_WARN("transport: descriptors truncated");
|
||||
conn->rlen += (size_t)r;
|
||||
}
|
||||
}
|
||||
|
||||
static void *conn_main(void *arg)
|
||||
{
|
||||
vkb_srv_conn *conn = arg;
|
||||
@@ -249,9 +431,10 @@ static void *conn_main(void *arg)
|
||||
for (;;) {
|
||||
vkb_msg_hdr h;
|
||||
int nfds = 0;
|
||||
if (vkb_recv_msg(conn->sock, &h, &conn->inbuf, &conn->incap, fds, &nfds) < 0) break;
|
||||
uint8_t *payload;
|
||||
if (srv_recv(conn, &h, &payload, fds, &nfds) < 0) break;
|
||||
vkb_enc_reset(&c.r);
|
||||
vkb_dec_init(&c.d, conn->inbuf, h.size, &conn->arena, fds, nfds);
|
||||
vkb_dec_init(&c.d, payload, h.size, &conn->arena, fds, nfds);
|
||||
c.conn = conn;
|
||||
c.status = VKB_ST_OK;
|
||||
c.nclose = 0;
|
||||
@@ -275,18 +458,23 @@ static void *conn_main(void *arg)
|
||||
}
|
||||
}
|
||||
if (c.status == VKB_ST_OK) {
|
||||
uint64_t t0 = st_period ? mono_ns() : 0;
|
||||
order_wait(conn->proc, h.barrier);
|
||||
uint64_t t1 = st_period ? mono_ns() : 0;
|
||||
vkb_srv_set_current_table(c.table);
|
||||
if (vkb_verbose) VKB_DBG("-> %s", vkb_cmd_names[h.cmd]);
|
||||
vkb_srv_current_cmd = vkb_cmd_names[h.cmd];
|
||||
vkb_srv_handlers[h.cmd](&c);
|
||||
vkb_srv_current_cmd = NULL;
|
||||
vkb_srv_set_current_table(NULL);
|
||||
if (st_period) stats_add(h.cmd, mono_ns() - t1, t1 - t0);
|
||||
}
|
||||
}
|
||||
vkb_dec_close_untaken(&c.d);
|
||||
order_done(conn->proc, h.seq);
|
||||
if (!(h.flags & VKB_F_NOREPLY)) {
|
||||
vkb_msg_hdr rh = {VKB_MAGIC, c.status == VKB_ST_OK ? (uint32_t)c.r.len : 0, c.status, 0,
|
||||
c.status == VKB_ST_OK ? (uint32_t)c.r.nfds : 0, 0};
|
||||
c.status == VKB_ST_OK ? (uint32_t)c.r.nfds : 0, 0, 0, 0};
|
||||
if (c.r.oom) {
|
||||
rh.cmd = VKB_ST_BAD_MESSAGE;
|
||||
rh.size = 0;
|
||||
@@ -298,13 +486,19 @@ static void *conn_main(void *arg)
|
||||
}
|
||||
} else if (c.status != VKB_ST_OK) {
|
||||
VKB_ERR("asynchronous %s failed (status %u)", vkb_cmd_names[h.cmd], c.status);
|
||||
} else if (vkb_cmd_result[h.cmd] && c.r.len >= sizeof(int32_t)) {
|
||||
/* The client already told the app VK_SUCCESS; a failure can only be reported here. */
|
||||
int32_t vr;
|
||||
memcpy(&vr, c.r.buf, sizeof(vr));
|
||||
if (vr < 0) VKB_ERR("asynchronous %s returned %d", vkb_cmd_names[h.cmd], vr);
|
||||
}
|
||||
for (int i = 0; i < c.nclose; i++) close(c.close_after[i]);
|
||||
vkb_arena_reset(&conn->arena);
|
||||
}
|
||||
close(conn->sock);
|
||||
vkb_enc_free(&c.r);
|
||||
free(conn->inbuf);
|
||||
free(conn->rb);
|
||||
for (int i = 0; i < conn->fdn; i++) close(conn->fdq[i]);
|
||||
vkb_arena_free(&conn->arena);
|
||||
if (conn->proc) proc_leave(conn->proc);
|
||||
free(conn);
|
||||
@@ -313,6 +507,7 @@ static void *conn_main(void *arg)
|
||||
|
||||
void vkb_srv_serve(int lfd)
|
||||
{
|
||||
stats_add(0, 0, 0);
|
||||
for (;;) {
|
||||
int s = accept4(lfd, NULL, NULL, SOCK_CLOEXEC);
|
||||
if (s < 0) {
|
||||
|
||||
@@ -20,18 +20,29 @@
|
||||
#define VKB_MAX_PDS 16
|
||||
|
||||
/* A client process (all connections that said hello with the same token). */
|
||||
#define VKB_SEQ_RING 65536
|
||||
|
||||
typedef struct vkb_srv_proc {
|
||||
uint64_t token;
|
||||
int pid;
|
||||
int conns;
|
||||
struct vkb_srv_proc *next;
|
||||
/* Ordering of the process's asynchronous requests (vkb_msg_hdr.seq/barrier). */
|
||||
pthread_mutex_t olock;
|
||||
pthread_cond_t ocond;
|
||||
uint64_t watermark; /* every async seq <= this has run */
|
||||
uint8_t done[VKB_SEQ_RING]; /* seqs above the watermark that have run */
|
||||
int conn_closed; /* a connection ended (a seq may never arrive) */
|
||||
} vkb_srv_proc;
|
||||
|
||||
typedef struct vkb_srv_conn {
|
||||
int sock;
|
||||
vkb_srv_proc *proc;
|
||||
uint8_t *inbuf;
|
||||
size_t incap;
|
||||
/* Receive buffer: messages are parsed out of it in place (srv_recv). */
|
||||
uint8_t *rb;
|
||||
size_t rcap, rpos, rlen;
|
||||
int fdq[2 * VKB_MAX_FDS]; /* descriptors received ahead of their message's turn */
|
||||
int fdn;
|
||||
vkb_arena arena;
|
||||
pthread_t thread;
|
||||
} vkb_srv_conn;
|
||||
|
||||
Reference in new issue
Block a user