Skip to content

LordOS — a free, fast and secure GNU/Linux distribution based on Debian, with its own desktop environment: display server and Wayland compositor in C, a React UI engine on QuickJS, and private native apps.

Shared measurement library server/src/bench (frame times with p50/p99 and missed refresh budget, presentation and vblank accounting, input latency, GL renderer kind, JSON strings, atomic state report; glscene.c, the GLES2 3D scene for any context) used by lordos-testbench and, from 0.8.6, LordBench; build/bench-test (22 checks) found and fixed vblank skips not counted by time when the first frame was at 0

Federico Pereiracbbd0a4ea33f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a52fba2..9bd11ac 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -32,7 +32,9 @@ of the matching [GitHub release](https://github.com/lordbasex/LordOS/releases),
outside: frame timing and when frames really reach the screen, every key of the keyboard, the
mouse and wheel, and the Wayland protocols the system offers. G draws a 3D scene and says whether a
real graphics card or the processor renders it; V plays generated video at 24, 30 or 60 frames a second;
- L locks the pointer; its last report appears in System Information → Checks.
+ L locks the pointer; its last report appears in System Information → Checks. Its frame times give the
+ average, the 99th percentile and the frames that missed the display's refresh budget. Keys LordOS keeps
+ for itself (Print Screen, the media keys, ❖ on its own) are outlined in purple.
- **Monitor → Network shows ping (ICMP), raw IP and packet sockets** (the last two can see or forge
traffic: the row says so, with the protocol or ethertype and the interface), under the app that
opened them, and the remote
diff --git a/docs/BENCHMARK.md b/docs/BENCHMARK.md
index bf110b7..e2085e6 100644
--- a/docs/BENCHMARK.md
+++ b/docs/BENCHMARK.md
@@ -32,6 +32,14 @@ server/src/bench/ LordBench workloads (library)
- **`lordos-sysreport`** is an ordinary user command in `/usr/bin` that the user or a test script
runs; `lord-server` never runs it. It writes the same JSON as the app, so reports can be made on
a machine without a session (a headless Pi, a VM over SSH, CI).
+- **Shared with the Test Bench.** `lordos-testbench` (0.8.5, `src/testbench`, a Wayland client that checks
+ the display server from the outside) and LordBench keep their own names and jobs, but the measurement
+ pieces live once, in `server/src/bench/`, and both link them: `bench.c` (the clock; frame times with
+ average, p50, p99 and frames that missed the refresh budget; presentation accounting with vblanks skipped
+ and zero-copy frames; input-to-screen latency; the GL renderer's kind; JSON strings; the report written
+ atomically to `~/.local/state/lordos/<name>.json`) and `glscene.c` (the fixed 3D scene, drawn with GLES2
+ in whatever context is current: the bench's wayland-egl window, LordBench's offscreen context). Tested by
+ `build/bench-test`. LordBench's workloads go next to them.
- **`lord-bench`** is the helper the app starts for workloads, launched like `lord-doc` and
`lord-media`: fixed path (`<lord-server dir>/lord-bench`), no arguments, minimal environment,
channel on fd 3, `sandbox_apply_helper()` before any work; it gets only the render node and a
diff --git a/docs/PAPER-VALIDATION.md b/docs/PAPER-VALIDATION.md
index a305910..3625595 100644
--- a/docs/PAPER-VALIDATION.md
+++ b/docs/PAPER-VALIDATION.md
@@ -201,6 +201,11 @@ Linux app.
Keys LordOS's compositor keeps (`wl.c`, `wl_key`: Print Screen, the media keys, ❖ on its own) are outlined in
purple on the bench's keyboard, so a key that never lights isn't mistaken for a broken one.
+**Shared code (2026-10-26).** The measurement pieces moved to `server/src/bench/`, shared with LordBench
+(0.8.6): frame times now give p50, p99 and frames that missed the refresh budget (`frameMsP50`,
+`frameMsP99`, `missedBudget` in the report); `build/bench-test` (22 checks) found that vblanks skipped were
+never counted from presentation times when the first frame was at time 0, fixed.
+
**Still to build:** pointer confine, gamepads, and a run with the real monitor and GPU (presentation, latency,
zero-copy).
diff --git a/server/mk/testbench.mk b/server/mk/testbench.mk
index 2f07cb1..a837a0c 100644
--- a/server/mk/testbench.mk
+++ b/server/mk/testbench.mk
@@ -1,12 +1,21 @@
# lordos-testbench (src/testbench, docs/PAPER-VALIDATION.md): the visual test bench, a plain Wayland
# client (test card, frame and presentation timing, every key, the pointer, the compositor's globals).
-# libwayland-client and libxkbcommon only; it reads no files.
-TESTBENCH_SRC := src/testbench/testbench.c src/testbench/checks.c src/testbench/gl3d.c src/testbench/card.c src/testbench/draw.c
+# libwayland-client and libxkbcommon only; it reads no files. The measurement pieces (src/bench: frame
+# times, presentation, latency, the GL renderer's kind, the 3D scene, the report file) are shared with
+# LordBench (0.8.6, docs/BENCHMARK.md) and tested on their own (build/bench-test).
+BENCH_SRC := src/bench/bench.c
+TESTBENCH_SRC := src/testbench/testbench.c src/testbench/checks.c src/testbench/gl3d.c src/testbench/card.c src/testbench/draw.c $(BENCH_SRC)
all: build/lordos-testbench
# the 3D view needs EGL, GLESv2 and wayland-egl (Mesa); without them the bench builds and says so
TB_GL := $(shell pkg-config --exists egl glesv2 wayland-egl 2>/dev/null && echo 1)
TB_GL_FLAGS := $(if $(TB_GL),-DTB_GL $(shell pkg-config --cflags egl glesv2 wayland-egl))
+TESTBENCH_SRC += $(if $(TB_GL),src/bench/glscene.c)
TB_GL_LIBS := $(if $(TB_GL),$(shell pkg-config --libs egl glesv2 wayland-egl))
-build/lordos-testbench: $(TESTBENCH_SRC) $(wildcard src/testbench/*.h) $(CLIENT_GEN)
+build/lordos-testbench: $(TESTBENCH_SRC) $(wildcard src/testbench/*.h src/bench/*.h) $(CLIENT_GEN)
@mkdir -p build
$(CC) $(CPPFLAGS) $(CFLAGS) $(TB_GL_FLAGS) -Ibuild/gen $(WARN) -std=c11 -o $@ $(TESTBENCH_SRC) $(filter %.c,$(CLIENT_GEN)) -lwayland-client -lxkbcommon -lm $(TB_GL_LIBS)
+test: build/bench-test
+build/bench-test: tests/bench_test.c $(BENCH_SRC) src/bench/bench.h
+ @mkdir -p build
+ $(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/bench_test.c $(BENCH_SRC) -lm
+ ./$@ || { rm -f $@; exit 1; }
diff --git a/server/src/bench/bench.c b/server/src/bench/bench.c
new file mode 100644
index 0000000..ded3a5b
--- /dev/null
+++ b/server/src/bench/bench.c
@@ -0,0 +1,109 @@
+/* bench.c — see bench.h. */
+#define _POSIX_C_SOURCE 200809L
+#include "bench.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <time.h>
+#include <unistd.h>
+
+double bench_now_ms(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec * 1e3 + t.tv_nsec / 1e6; }
+
+void bench_frames_add(BenchFrames *f, double t_ms) {
+ if (f->frames) f->iv[f->n < BENCH_HIST ? f->n++ : (int)(f->frames % BENCH_HIST)] = t_ms - f->last;
+ else f->first = t_ms;
+ f->last = t_ms;
+ f->frames++;
+}
+
+double bench_frames_fps(const BenchFrames *f) {
+ double span = f->last - f->first;
+ return span > 0 && f->frames > 1 ? (f->frames - 1) * 1000.0 / span : 0;
+}
+
+static int cmp_d(const void *a, const void *b) { double x = *(const double *)a, y = *(const double *)b; return (x > y) - (x < y); }
+
+void bench_frames_stats(const BenchFrames *f, BenchStats *out) {
+ memset(out, 0, sizeof *out);
+ if (!f->n) return;
+ double s[BENCH_HIST], sum = 0;
+ memcpy(s, f->iv, f->n * sizeof *s);
+ for (int i = 0; i < f->n; i++) sum += s[i];
+ qsort(s, f->n, sizeof *s, cmp_d);
+ out->avg = sum / f->n;
+ out->max = s[f->n - 1];
+ out->p50 = s[(f->n - 1) / 2]; /* nearest rank */
+ int r = (99 * f->n + 99) / 100; /* ceil(0.99 n) */
+ out->p99 = s[(r < 1 ? 1 : r) - 1];
+}
+
+int bench_frames_missed(const BenchFrames *f, double budget_ms) {
+ int m = 0;
+ if (budget_ms <= 0) return 0;
+ for (int i = 0; i < f->n; i++) if (f->iv[i] > budget_ms * 1.5) m++;
+ return m;
+}
+
+void bench_present(BenchPresent *p, double t_ms, uint64_t msc, uint32_t refresh_ns, int zero_copy) {
+ p->presented++;
+ if (refresh_ns) p->refresh_ns = refresh_ns;
+ if (zero_copy) p->zero_copy++;
+ if (p->last_msc && msc > p->last_msc + 1) p->vblank_skips += msc - p->last_msc - 1;
+ else if (!msc && p->presented > 1 && p->refresh_ns) { /* no counter: count from the times */
+ double gap = (t_ms - p->last_ms) / (p->refresh_ns / 1e6);
+ if (gap > 1.5) p->vblank_skips += (unsigned long)(gap + 0.5) - 1;
+ }
+ p->last_msc = msc;
+ p->last_ms = t_ms;
+}
+
+double bench_refresh_hz(const BenchPresent *p) { return p->refresh_ns ? 1e9 / p->refresh_ns : 0; }
+
+void bench_latency_add(BenchLatency *l, double ms) {
+ if (ms < 0 || ms >= 2000) { l->lost++; return; }
+ l->last = ms;
+ l->sum += ms;
+ l->n++;
+}
+
+double bench_latency_avg(const BenchLatency *l) { return l->n ? l->sum / l->n : 0; }
+
+const char *bench_gl_kind(const char *r) {
+ if (!r || !*r) return "";
+ if (strstr(r, "llvmpipe") || strstr(r, "softpipe") || strstr(r, "swrast") || strstr(r, "SwiftShader")) return "SOFTWARE (CPU)";
+ if (strstr(r, "virgl") || strstr(r, "VirGL") || strstr(r, "SVGA") || strstr(r, "virtio")) return "VIRTUAL GPU (HOST)";
+ return "HARDWARE GPU";
+}
+
+void bench_json_str(FILE *o, const char *s) {
+ fputc('"', o);
+ for (const unsigned char *q = (const unsigned char *)(s ? s : ""); *q; q++) {
+ if (*q == '"' || *q == '\\') fprintf(o, "\\%c", *q);
+ else if (*q < 0x20) fprintf(o, "\\u%04x", *q);
+ else fputc(*q, o);
+ }
+ fputc('"', o);
+}
+
+int bench_save(const char *name, void (*write)(FILE *)) {
+ if (!name || !*name || strlen(name) > 32 || strspn(name, "abcdefghijklmnopqrstuvwxyz0123456789-") != strlen(name)) return -1;
+ const char *st = getenv("XDG_STATE_HOME"), *home = getenv("HOME");
+ char dir[512], tmp[600], path[600];
+ if (st && st[0] == '/') snprintf(dir, sizeof dir, "%s/lordos", st);
+ else if (home && home[0] == '/') {
+ snprintf(dir, sizeof dir, "%s/.local", home);
+ mkdir(dir, 0700);
+ snprintf(dir, sizeof dir, "%s/.local/state", home);
+ mkdir(dir, 0700);
+ snprintf(dir, sizeof dir, "%s/.local/state/lordos", home);
+ } else return -1;
+ mkdir(dir, 0700);
+ snprintf(tmp, sizeof tmp, "%s/.%s.json.tmp", dir, name);
+ snprintf(path, sizeof path, "%s/%s.json", dir, name);
+ FILE *f = fopen(tmp, "we");
+ if (!f) return -1;
+ write(f);
+ if (fclose(f) != 0) { unlink(tmp); return -1; }
+ return rename(tmp, path);
+}
diff --git a/server/src/bench/bench.h b/server/src/bench/bench.h
new file mode 100644
index 0000000..94d0fe1
--- /dev/null
+++ b/server/src/bench/bench.h
@@ -0,0 +1,54 @@
+/* bench.h — measurement pieces shared by lordos-testbench (src/testbench) and LordBench (0.8.6,
+ * docs/BENCHMARK.md): the clock, frame-time statistics against a refresh budget, presentation
+ * accounting, input-to-screen latency, the GL renderer's kind and the reports' state file. Pure C with
+ * libc and libm only: no Wayland, no GL (the GL scene is glscene.h). */
+#ifndef LORD_BENCH_H
+#define LORD_BENCH_H
+
+#include <stdint.h>
+#include <stdio.h>
+
+double bench_now_ms(void); /* CLOCK_MONOTONIC in milliseconds */
+
+/* ---- frame times: the interval between consecutive frames, the last BENCH_HIST of them ---- */
+#define BENCH_HIST 240
+typedef struct {
+ double iv[BENCH_HIST];
+ int n; /* intervals stored (≤ BENCH_HIST) */
+ unsigned long frames;
+ double first, last; /* the first and last frame's time, ms */
+} BenchFrames;
+typedef struct { double avg, max, p50, p99; } BenchStats;
+
+void bench_frames_add(BenchFrames *f, double t_ms);
+double bench_frames_fps(const BenchFrames *f);
+void bench_frames_stats(const BenchFrames *f, BenchStats *out); /* all 0 without intervals */
+/* intervals that missed the budget (longer than 1.5 × budget_ms), e.g. 16.67 at 60 Hz */
+int bench_frames_missed(const BenchFrames *f, double budget_ms);
+
+/* ---- presentation: what wp_presentation (or a page flip) reports per frame ---- */
+typedef struct {
+ unsigned long presented, discarded, vblank_skips, zero_copy;
+ uint32_t refresh_ns;
+ double last_ms;
+ uint64_t last_msc;
+} BenchPresent;
+/* a frame shown at t_ms on vblank msc (0 when the display has no counter); refresh_ns 0 if unknown */
+void bench_present(BenchPresent *p, double t_ms, uint64_t msc, uint32_t refresh_ns, int zero_copy);
+double bench_refresh_hz(const BenchPresent *p);
+
+/* ---- input to screen ---- */
+typedef struct { double last, sum; unsigned long n, lost; } BenchLatency;
+void bench_latency_add(BenchLatency *l, double ms); /* < 0 or ≥ 2000 ms counts as lost */
+double bench_latency_avg(const BenchLatency *l);
+
+/* ---- the GL renderer: "HARDWARE GPU", "VIRTUAL GPU (HOST)" or "SOFTWARE (CPU)" from GL_RENDERER ---- */
+const char *bench_gl_kind(const char *renderer);
+
+/* ---- reports ---- */
+void bench_json_str(FILE *o, const char *s); /* a JSON string, quoted and escaped */
+/* writes $XDG_STATE_HOME/lordos/<name>.json (or ~/.local/state/lordos/<name>.json) whole, renamed into
+ * place; name is [a-z0-9-]; 0 or -1 */
+int bench_save(const char *name, void (*write)(FILE *));
+
+#endif
diff --git a/server/src/bench/glscene.c b/server/src/bench/glscene.c
new file mode 100644
index 0000000..3d988ff
--- /dev/null
+++ b/server/src/bench/glscene.c
@@ -0,0 +1,101 @@
+/* glscene.c — see glscene.h. */
+#include "glscene.h"
+#include "bench.h"
+
+#include <GLES2/gl2.h>
+#include <math.h>
+#include <stdio.h>
+#include <string.h>
+
+static GLuint prog;
+static GLint u_mvp, a_pos, a_col;
+
+static GLuint shader(GLenum type, const char *src) {
+ GLuint s = glCreateShader(type);
+ glShaderSource(s, 1, &src, NULL);
+ glCompileShader(s);
+ GLint ok = 0;
+ glGetShaderiv(s, GL_COMPILE_STATUS, &ok);
+ if (!ok) { glDeleteShader(s); return 0; }
+ return s;
+}
+
+int bench_scene_init(BenchGlInfo *info) {
+ memset(info, 0, sizeof *info);
+ snprintf(info->renderer, sizeof info->renderer, "%.90s", (const char *)glGetString(GL_RENDERER));
+ snprintf(info->vendor, sizeof info->vendor, "%.60s", (const char *)glGetString(GL_VENDOR));
+ snprintf(info->version, sizeof info->version, "%.60s", (const char *)glGetString(GL_VERSION));
+ snprintf(info->kind, sizeof info->kind, "%s", bench_gl_kind(info->renderer));
+ static const char *vs = "uniform mat4 mvp; attribute vec3 pos; attribute vec3 col; varying vec3 c;"
+ "void main(){ c = col; gl_Position = mvp * vec4(pos, 1.0); }";
+ static const char *fs = "precision mediump float; varying vec3 c; void main(){ gl_FragColor = vec4(c, 1.0); }";
+ GLuint v = shader(GL_VERTEX_SHADER, vs), f = shader(GL_FRAGMENT_SHADER, fs);
+ if (!v || !f) return -1;
+ prog = glCreateProgram();
+ glAttachShader(prog, v);
+ glAttachShader(prog, f);
+ glLinkProgram(prog);
+ glDeleteShader(v);
+ glDeleteShader(f);
+ u_mvp = glGetUniformLocation(prog, "mvp");
+ a_pos = glGetAttribLocation(prog, "pos");
+ a_col = glGetAttribLocation(prog, "col");
+ glEnable(GL_DEPTH_TEST);
+ return 0;
+}
+
+static void mul(float *o, const float *a, const float *b) {
+ float r[16];
+ for (int i = 0; i < 4; i++)
+ for (int j = 0; j < 4; j++) {
+ r[j * 4 + i] = 0;
+ for (int k = 0; k < 4; k++) r[j * 4 + i] += a[k * 4 + i] * b[j * 4 + k];
+ }
+ memcpy(o, r, sizeof r);
+}
+
+double bench_scene_draw(int w, int h, double t_ms) {
+ if (!prog) return 0;
+ /* a cube, two triangles per face, a colour per face */
+ static const float P[8][3] = { { -1, -1, -1 }, { 1, -1, -1 }, { 1, 1, -1 }, { -1, 1, -1 }, { -1, -1, 1 }, { 1, -1, 1 }, { 1, 1, 1 }, { -1, 1, 1 } };
+ static const int F[6][4] = { { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 0, 1, 5, 4 }, { 2, 3, 7, 6 }, { 1, 2, 6, 5 }, { 0, 3, 7, 4 } };
+ static const float C[6][3] = { { .95f, .25f, .25f }, { .25f, .85f, .35f }, { .25f, .45f, .95f }, { .95f, .8f, .2f }, { .75f, .35f, .95f }, { .2f, .85f, .9f } };
+ float v[36 * 3], c[36 * 3];
+ int k = 0;
+ for (int f = 0; f < 6; f++) {
+ static const int tri[6] = { 0, 1, 2, 0, 2, 3 };
+ for (int i = 0; i < 6; i++, k++) {
+ memcpy(v + k * 3, P[F[f][tri[i]]], 12);
+ memcpy(c + k * 3, C[f], 12);
+ }
+ }
+ double a = t_ms / 1000.0;
+ float ca = (float)cos(a), sa = (float)sin(a), cb = (float)cos(a * 0.7), sb = (float)sin(a * 0.7);
+ float ry[16] = { ca, 0, -sa, 0, 0, 1, 0, 0, sa, 0, ca, 0, 0, 0, 0, 1 };
+ float rx[16] = { 1, 0, 0, 0, 0, cb, sb, 0, 0, -sb, cb, 0, 0, 0, 0, 1 };
+ float asp = h ? (float)w / h : 1, fn = 1.0f / (float)tan(0.5), zn = 1, zf = 20;
+ float pr[16] = { fn / asp, 0, 0, 0, 0, fn, 0, 0, 0, 0, (zf + zn) / (zn - zf), -1, 0, 0, 2 * zf * zn / (zn - zf), 0 };
+ float tr[16] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, -5, 1 };
+ float m[16], mvp[16];
+ mul(m, rx, ry);
+ mul(m, tr, m);
+ mul(mvp, pr, m);
+ double t0 = bench_now_ms();
+ glViewport(0, 0, w, h);
+ glClearColor(0.07f, 0.07f, 0.1f, 1);
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+ glUseProgram(prog);
+ glUniformMatrix4fv(u_mvp, 1, GL_FALSE, mvp);
+ glVertexAttribPointer((GLuint)a_pos, 3, GL_FLOAT, GL_FALSE, 0, v);
+ glVertexAttribPointer((GLuint)a_col, 3, GL_FLOAT, GL_FALSE, 0, c);
+ glEnableVertexAttribArray((GLuint)a_pos);
+ glEnableVertexAttribArray((GLuint)a_col);
+ glDrawArrays(GL_TRIANGLES, 0, 36);
+ glFinish(); /* the GPU's time for this frame */
+ return bench_now_ms() - t0;
+}
+
+void bench_scene_free(void) {
+ if (prog) glDeleteProgram(prog);
+ prog = 0;
+}
diff --git a/server/src/bench/glscene.h b/server/src/bench/glscene.h
new file mode 100644
index 0000000..8d2e03e
--- /dev/null
+++ b/server/src/bench/glscene.h
@@ -0,0 +1,18 @@
+/* glscene.h — a fixed 3D scene (a rotating cube, a colour per face) drawn with OpenGL ES 2 in whatever
+ * context is current: lordos-testbench's window (wayland-egl) or LordBench's offscreen context. Needs
+ * GLESv2 only; the caller owns EGL. */
+#ifndef LORD_BENCH_GLSCENE_H
+#define LORD_BENCH_GLSCENE_H
+
+typedef struct {
+ char renderer[96], vendor[64], version[64];
+ char kind[32]; /* bench_gl_kind(renderer) */
+} BenchGlInfo;
+
+/* Compiles the scene's program in the current context and fills info; 0, or -1 if the shaders fail. */
+int bench_scene_init(BenchGlInfo *info);
+/* Draws the scene at time t_ms into a w × h viewport and waits for the GPU (glFinish); returns that time, ms. */
+double bench_scene_draw(int w, int h, double t_ms);
+void bench_scene_free(void);
+
+#endif
diff --git a/server/src/testbench/gl3d.c b/server/src/testbench/gl3d.c
index 4c451e2..f59eab4 100644
--- a/server/src/testbench/gl3d.c
+++ b/server/src/testbench/gl3d.c
@@ -1,18 +1,16 @@
-/* gl3d.c — lordos-testbench's 3D view: a rotating cube drawn with EGL and OpenGL ES 2 on a subsurface,
- * through Mesa's Wayland platform, the way a game or a GPU app draws. It shows which renderer did the
+/* gl3d.c — lordos-testbench's 3D view: the shared scene (src/bench/glscene.c, also LordBench's) drawn
+ * with EGL and OpenGL ES 2 on a subsurface, through Mesa's Wayland platform, the way a game or a GPU app draws. It shows which renderer did the
* work (a GPU's driver, or a software one such as llvmpipe), how long the GPU takes per frame, and it
* sends its buffers through the compositor's dmabuf path (Mesa uses linux-dmabuf with a GPU, wl_shm with
* a software renderer). Built only when EGL, GLESv2 and wayland-egl are there (TB_GL). */
#include "gl3d.h"
#ifdef TB_GL
+#include "../bench/glscene.h"
#include <EGL/egl.h>
#include <EGL/eglext.h>
-#include <GLES2/gl2.h>
-#include <math.h>
#include <stdio.h>
#include <string.h>
-#include <time.h>
#include <wayland-egl.h>
static EGLDisplay edpy = EGL_NO_DISPLAY;
@@ -21,31 +19,9 @@ static EGLSurface esurf = EGL_NO_SURFACE;
static struct wl_egl_window *ewin;
static struct wl_surface *gsurf;
static struct wl_subsurface *gsub;
-static GLuint prog;
-static GLint u_mvp, a_pos, a_col;
static int gw, gh, on;
Gl3dInfo gl3d;
-static double ms(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec * 1e3 + t.tv_nsec / 1e6; }
-
-static GLuint shader(GLenum type, const char *src) {
- GLuint s = glCreateShader(type);
- glShaderSource(s, 1, &src, NULL);
- glCompileShader(s);
- GLint ok = 0;
- glGetShaderiv(s, GL_COMPILE_STATUS, &ok);
- return ok ? s : 0;
-}
-
-static void classify(void) {
- const char *r = gl3d.renderer;
- if (strstr(r, "llvmpipe") || strstr(r, "softpipe") || strstr(r, "swrast") || strstr(r, "SwiftShader"))
- snprintf(gl3d.kind, sizeof gl3d.kind, "SOFTWARE (CPU)");
- else if (strstr(r, "virgl") || strstr(r, "VirGL") || strstr(r, "SVGA") || strstr(r, "virtio"))
- snprintf(gl3d.kind, sizeof gl3d.kind, "VIRTUAL GPU (HOST)");
- else snprintf(gl3d.kind, sizeof gl3d.kind, "HARDWARE GPU");
-}
-
int gl3d_start(struct wl_display *dpy, struct wl_compositor *comp, struct wl_subcompositor *sc, struct wl_surface *parent, int x, int y, int w, int h) {
if (on) { gl3d_place(x, y, w, h); return 0; }
memset(&gl3d, 0, sizeof gl3d);
@@ -75,23 +51,13 @@ int gl3d_start(struct wl_display *dpy, struct wl_compositor *comp, struct wl_sub
esurf = eglCreateWindowSurface(edpy, cfg, (EGLNativeWindowType)ewin, NULL);
if (esurf == EGL_NO_SURFACE || !eglMakeCurrent(edpy, esurf, esurf, ectx)) { snprintf(gl3d.error, sizeof gl3d.error, "NO EGL WINDOW SURFACE"); return -1; }
eglSwapInterval(edpy, 0); /* the bench's own frame callback paces the frames */
- snprintf(gl3d.renderer, sizeof gl3d.renderer, "%.90s", (const char *)glGetString(GL_RENDERER));
- snprintf(gl3d.vendor, sizeof gl3d.vendor, "%.60s", (const char *)glGetString(GL_VENDOR));
- snprintf(gl3d.version, sizeof gl3d.version, "%.60s", (const char *)glGetString(GL_VERSION));
- classify();
- static const char *vs = "uniform mat4 mvp; attribute vec3 pos; attribute vec3 col; varying vec3 c;"
- "void main(){ c = col; gl_Position = mvp * vec4(pos, 1.0); }";
- static const char *fs = "precision mediump float; varying vec3 c; void main(){ gl_FragColor = vec4(c, 1.0); }";
- GLuint v = shader(GL_VERTEX_SHADER, vs), f = shader(GL_FRAGMENT_SHADER, fs);
- if (!v || !f) { snprintf(gl3d.error, sizeof gl3d.error, "SHADERS DID NOT COMPILE"); return -1; }
- prog = glCreateProgram();
- glAttachShader(prog, v);
- glAttachShader(prog, f);
- glLinkProgram(prog);
- u_mvp = glGetUniformLocation(prog, "mvp");
- a_pos = glGetAttribLocation(prog, "pos");
- a_col = glGetAttribLocation(prog, "col");
- glEnable(GL_DEPTH_TEST);
+ BenchGlInfo gi;
+ int bad = bench_scene_init(&gi);
+ snprintf(gl3d.renderer, sizeof gl3d.renderer, "%s", gi.renderer);
+ snprintf(gl3d.vendor, sizeof gl3d.vendor, "%s", gi.vendor);
+ snprintf(gl3d.version, sizeof gl3d.version, "%s", gi.version);
+ snprintf(gl3d.kind, sizeof gl3d.kind, "%s", gi.kind);
+ if (bad) { snprintf(gl3d.error, sizeof gl3d.error, "SHADERS DID NOT COMPILE"); return -1; }
on = 1;
return 0;
}
@@ -102,57 +68,11 @@ void gl3d_place(int x, int y, int w, int h) {
if (w != gw || h != gh) { wl_egl_window_resize(ewin, w, h, 0, 0); gw = w; gh = h; }
}
-static void mul(float *o, const float *a, const float *b) {
- float r[16];
- for (int i = 0; i < 4; i++)
- for (int j = 0; j < 4; j++) {
- r[j * 4 + i] = 0;
- for (int k = 0; k < 4; k++) r[j * 4 + i] += a[k * 4 + i] * b[j * 4 + k];
- }
- memcpy(o, r, sizeof r);
-}
-
struct wl_surface *gl3d_surface(void) { return on ? gsurf : NULL; }
void gl3d_frame(double t_ms) {
if (!on) return;
- /* a cube, two triangles per face, a colour per face */
- static const float P[8][3] = { { -1, -1, -1 }, { 1, -1, -1 }, { 1, 1, -1 }, { -1, 1, -1 }, { -1, -1, 1 }, { 1, -1, 1 }, { 1, 1, 1 }, { -1, 1, 1 } };
- static const int F[6][4] = { { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 0, 1, 5, 4 }, { 2, 3, 7, 6 }, { 1, 2, 6, 5 }, { 0, 3, 7, 4 } };
- static const float C[6][3] = { { .95f, .25f, .25f }, { .25f, .85f, .35f }, { .25f, .45f, .95f }, { .95f, .8f, .2f }, { .75f, .35f, .95f }, { .2f, .85f, .9f } };
- float v[36 * 3], c[36 * 3];
- int k = 0;
- for (int f = 0; f < 6; f++) {
- static const int tri[6] = { 0, 1, 2, 0, 2, 3 };
- for (int i = 0; i < 6; i++, k++) {
- memcpy(v + k * 3, P[F[f][tri[i]]], 12);
- memcpy(c + k * 3, C[f], 12);
- }
- }
- double a = t_ms / 1000.0;
- float ca = (float)cos(a), sa = (float)sin(a), cb = (float)cos(a * 0.7), sb = (float)sin(a * 0.7);
- float ry[16] = { ca, 0, -sa, 0, 0, 1, 0, 0, sa, 0, ca, 0, 0, 0, 0, 1 };
- float rx[16] = { 1, 0, 0, 0, 0, cb, sb, 0, 0, -sb, cb, 0, 0, 0, 0, 1 };
- float asp = gh ? (float)gw / gh : 1, fn = 1.0f / (float)tan(0.5), zn = 1, zf = 20;
- float pr[16] = { fn / asp, 0, 0, 0, 0, fn, 0, 0, 0, 0, (zf + zn) / (zn - zf), -1, 0, 0, 2 * zf * zn / (zn - zf), 0 };
- float tr[16] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, -5, 1 };
- float m[16], mvp[16];
- mul(m, rx, ry);
- mul(m, tr, m);
- mul(mvp, pr, m);
- double t0 = ms();
- glViewport(0, 0, gw, gh);
- glClearColor(0.07f, 0.07f, 0.1f, 1);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glUseProgram(prog);
- glUniformMatrix4fv(u_mvp, 1, GL_FALSE, mvp);
- glVertexAttribPointer((GLuint)a_pos, 3, GL_FLOAT, GL_FALSE, 0, v);
- glVertexAttribPointer((GLuint)a_col, 3, GL_FLOAT, GL_FALSE, 0, c);
- glEnableVertexAttribArray((GLuint)a_pos);
- glEnableVertexAttribArray((GLuint)a_col);
- glDrawArrays(GL_TRIANGLES, 0, 36);
- glFinish(); /* the GPU's time for this frame */
- double dt = ms() - t0;
+ double dt = bench_scene_draw(gw, gh, t_ms);
gl3d.gpu_ms = gl3d.frames ? gl3d.gpu_ms * 0.9 + dt * 0.1 : dt;
gl3d.frames++;
eglSwapBuffers(edpy, esurf);
@@ -160,6 +80,7 @@ void gl3d_frame(double t_ms) {
void gl3d_stop(void) {
if (!on) return;
+ bench_scene_free();
eglMakeCurrent(edpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
eglDestroySurface(edpy, esurf);
wl_egl_window_destroy(ewin);
diff --git a/server/src/testbench/testbench.c b/server/src/testbench/testbench.c
index 3200591..deaceef 100644
--- a/server/src/testbench/testbench.c
+++ b/server/src/testbench/testbench.c
@@ -25,6 +25,7 @@
#include "draw.h"
#include "tb.h"
#include "gl3d.h"
+#include "../bench/bench.h"
#include <errno.h>
#include <fcntl.h>
@@ -97,16 +98,11 @@ static Card card;
static int card_x, card_y;
/* ---- measurements ---- */
-static double now_ms(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec * 1e3 + t.tv_nsec / 1e6; }
-static unsigned long frames, no_buffer;
-static double t_first, t_last;
-#define HIST 240
-static double intervals[HIST];
-static int nint;
-static unsigned long presented, discarded, vblank_skips, zero_copy;
-static uint32_t refresh_ns, clock_id;
-static double last_present_ms;
-static uint64_t last_msc;
+#define now_ms bench_now_ms
+static BenchFrames ft; /* frame times (src/bench, shared with LordBench) */
+static BenchPresent pr;
+static unsigned long no_buffer;
+static uint32_t clock_id;
/* ---- input ---- */
static struct xkb_context *xkb;
@@ -123,8 +119,7 @@ static int rerun_checks, paused;
/* input to screen: the time of the last key or button, kept until the frame showing it is presented */
static uint32_t lat_ev;
static int lat_pending;
-static double lat_last = -1, lat_sum;
-static unsigned long lat_n, lat_lost;
+static BenchLatency lat = { .last = -1 };
static int clock_mono = 1; /* wp_presentation's clock is CLOCK_MONOTONIC (the event times' base) */
/* generated video: content at 24/30/60 fps, and how many refreshes each content frame stayed */
static int video_fps; /* 0 off */
@@ -266,35 +261,33 @@ static void draw_panel(Surf *s, int x0) {
/* timing */
d_text(s, x, y, 1, C_HEAD, "TIMING");
y += 14;
- double span = t_last - t_first;
- double fps = span > 0 && frames > 1 ? (frames - 1) * 1000.0 / span : 0;
- double sum = 0, mx = 0;
- for (int i = 0; i < nint; i++) { sum += intervals[i]; if (intervals[i] > mx) mx = intervals[i]; }
- snprintf(v, sizeof v, "%.1f", fps);
+ BenchStats st;
+ bench_frames_stats(&ft, &st);
+ snprintf(v, sizeof v, "%.1f", bench_frames_fps(&ft));
stat_line(s, x, &y, "FRAMES/S", v, C_GOOD);
- snprintf(v, sizeof v, "%.2f MS AVG %.2f MAX", nint ? sum / nint : 0, mx);
- stat_line(s, x, &y, "FRAME TIME", v, mx > (refresh_ns ? refresh_ns / 1e6 * 1.5 : 25) ? C_WARN : C_TEXT);
- snprintf(v, sizeof v, "%lu", frames);
+ snprintf(v, sizeof v, "%.2f MS AVG %.2f P99 %.2f MAX", st.avg, st.p99, st.max);
+ stat_line(s, x, &y, "FRAME TIME", v, st.p99 > (pr.refresh_ns ? pr.refresh_ns / 1e6 * 1.5 : 25) ? C_WARN : C_TEXT);
+ snprintf(v, sizeof v, "%lu", ft.frames);
stat_line(s, x, &y, "FRAMES", v, C_TEXT);
if (presentation) {
- snprintf(v, sizeof v, "%lu SHOWN %lu DISCARDED", presented, discarded);
- stat_line(s, x, &y, "PRESENTED", v, discarded ? C_WARN : C_GOOD);
- if (refresh_ns) snprintf(v, sizeof v, "%.2f HZ", 1e9 / refresh_ns);
+ snprintf(v, sizeof v, "%lu SHOWN %lu DISCARDED", pr.presented, pr.discarded);
+ stat_line(s, x, &y, "PRESENTED", v, pr.discarded ? C_WARN : C_GOOD);
+ if (pr.refresh_ns) snprintf(v, sizeof v, "%.2f HZ", bench_refresh_hz(&pr));
else if (out_refresh_mhz) snprintf(v, sizeof v, "%.2f HZ (OUTPUT MODE; NONE IN FEEDBACK)", out_refresh_mhz / 1000.0);
else snprintf(v, sizeof v, "UNKNOWN");
stat_line(s, x, &y, "REFRESH", v, C_TEXT);
- snprintf(v, sizeof v, "%lu", vblank_skips);
- stat_line(s, x, &y, "VBLANKS SKIPPED", v, vblank_skips ? C_WARN : C_GOOD);
- snprintf(v, sizeof v, "%lu", zero_copy);
+ snprintf(v, sizeof v, "%lu", pr.vblank_skips);
+ stat_line(s, x, &y, "VBLANKS SKIPPED", v, pr.vblank_skips ? C_WARN : C_GOOD);
+ snprintf(v, sizeof v, "%lu", pr.zero_copy);
stat_line(s, x, &y, "ZERO-COPY", v, C_TEXT);
} else stat_line(s, x, &y, "PRESENTED", "NO WP_PRESENTATION", C_WARN);
snprintf(v, sizeof v, "%lu", no_buffer);
stat_line(s, x, &y, "NO FREE BUFFER", v, no_buffer ? C_WARN : C_TEXT);
snprintf(v, sizeof v, "%d X %d", win_w, win_h);
stat_line(s, x, &y, "WINDOW", v, C_TEXT);
- if (lat_n) snprintf(v, sizeof v, "%.1f MS LAST %.1f AVG (%lu)", lat_last, lat_sum / lat_n, lat_n);
- else snprintf(v, sizeof v, lat_lost ? "NOT MEASURABLE (%lu FRAMES NOT PRESENTED)" : "PRESS A KEY OR CLICK", lat_lost);
- stat_line(s, x, &y, "INPUT TO SCREEN", v, lat_n ? (lat_sum / lat_n > 50 ? C_WARN : C_GOOD) : C_DIM);
+ if (lat.n) snprintf(v, sizeof v, "%.1f MS LAST %.1f AVG (%lu)", lat.last, bench_latency_avg(&lat), lat.n);
+ else snprintf(v, sizeof v, lat.lost ? "NOT MEASURABLE (%lu FRAMES NOT PRESENTED)" : "PRESS A KEY OR CLICK", lat.lost);
+ stat_line(s, x, &y, "INPUT TO SCREEN", v, lat.n ? (bench_latency_avg(&lat) > 50 ? C_WARN : C_GOOD) : C_DIM);
y += 8;
/* graphics: the 3D view (G) */
d_text(s, x, y, 1, C_HEAD, "GRAPHICS (G: 3D V: VIDEO L: LOCK POINTER)");
@@ -404,9 +397,8 @@ static void draw_pad(Surf *s) {
d_circle(s, fx, fy + off, fr, LAMP(KEY_Z)); d_text_c(s, fx + 1, fy + off, 1, C_TEXT, "B");
AT(0.40, 0.36, mx, my); d_circle(s, mx, my, u / 2 + 1, LAMP(KEY_RIGHTSHIFT)); d_text_c(s, mx + 1, my, 1, C_TEXT, "-");
AT(0.60, 0.36, px, py); d_circle(s, px, py, u / 2 + 1, LAMP(KEY_ENTER)); d_text_c(s, px + 1, py, 1, C_TEXT, "+");
- double span = t_last - t_first;
char t[48];
- snprintf(t, sizeof t, "%.1f FPS", span > 0 && frames > 1 ? (frames - 1) * 1000.0 / span : 0.0);
+ snprintf(t, sizeof t, "%.1f FPS", bench_frames_fps(&ft));
AT(0.5, 0.82, tx, ty);
d_text_c(s, tx, ty, u / 6 > 1 ? u / 6 : 1, C_GOOD, t);
#undef AT
@@ -434,10 +426,10 @@ static void draw(Buf *b) {
int sc = c.h / 10 > 1 ? c.h / 10 : 1;
d_text_c(&s, card_x + c.x + c.w / 2, card_y + c.y + c.h / 2 - sc, sc, TB_RGB(235, 235, 235), clk);
int steps = 60, cell = c.w / steps;
- d_fill(&s, card_x + c.x + (int)(frames % steps) * cell, card_y + c.y + c.h - 4, cell > 2 ? cell - 1 : 2, 3, C_LAMP_ON);
+ d_fill(&s, card_x + c.x + (int)(ft.frames % steps) * cell, card_y + c.y + c.h - 4, cell > 2 ? cell - 1 : 2, 3, C_LAMP_ON);
draw_pad(&s);
if (video_fps) { /* the generated video: a square that steps once per content frame */
- long idx = (long)(t_last * video_fps / 1000.0);
+ long idx = (long)(ft.last * video_fps / 1000.0);
int track = card.w - 40, pos = (int)(idx % 60) * track / 60;
d_fill(&s, card_x + 20, card_y + card.h - 26, track, 14, TB_RGB(20, 20, 20));
d_fill(&s, card_x + 20 + pos, card_y + card.h - 26, track / 60 + 2, 14, TB_RGB(255, 255, 255));
@@ -461,21 +453,11 @@ static void fb_sync(void *d, struct wp_presentation_feedback *f, struct wl_outpu
static void fb_presented(void *d, struct wp_presentation_feedback *f, 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)d;
- presented++;
- if (refresh) refresh_ns = refresh;
- if (flags & WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY) zero_copy++;
- uint64_t msc = ((uint64_t)seq_hi << 32) | seq_lo;
- double t = (((uint64_t)sec_hi << 32) | sec_lo) * 1e3 + nsec / 1e6;
- if (last_msc && msc > last_msc + 1) vblank_skips += msc - last_msc - 1;
- else if (!msc && last_present_ms > 0 && refresh_ns) { /* no counter: count from the times */
- double gap = (t - last_present_ms) / (refresh_ns / 1e6);
- if (gap > 1.5) vblank_skips += (unsigned long)(gap + 0.5) - 1;
- }
- last_msc = msc;
- last_present_ms = t;
+ bench_present(&pr, (((uint64_t)sec_hi << 32) | sec_lo) * 1e3 + nsec / 1e6, ((uint64_t)seq_hi << 32) | seq_lo, refresh,
+ !!(flags & WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY));
wp_presentation_feedback_destroy(f);
}
-static void fb_discarded(void *d, struct wp_presentation_feedback *f) { (void)d; discarded++; wp_presentation_feedback_destroy(f); }
+static void fb_discarded(void *d, struct wp_presentation_feedback *f) { (void)d; pr.discarded++; wp_presentation_feedback_destroy(f); }
static const struct wp_presentation_feedback_listener fb_listener = { fb_sync, fb_presented, fb_discarded };
/* the frame that shows an input: its presentation time minus the event's time */
@@ -485,10 +467,10 @@ static void lat_presented(void *d, struct wp_presentation_feedback *f, uint32_t
uint32_t ev = (uint32_t)(uintptr_t)d;
uint64_t t = ((((uint64_t)sec_hi << 32) | sec_lo) * 1000ull) + nsec / 1000000u;
double l = (double)(uint32_t)((uint32_t)t - ev);
- if (clock_mono && l < 2000) { lat_last = l; lat_sum += l; lat_n++; } else lat_lost++;
+ bench_latency_add(&lat, clock_mono ? l : -1);
wp_presentation_feedback_destroy(f);
}
-static void lat_discarded(void *d, struct wp_presentation_feedback *f) { (void)d; lat_lost++; wp_presentation_feedback_destroy(f); }
+static void lat_discarded(void *d, struct wp_presentation_feedback *f) { (void)d; lat.lost++; wp_presentation_feedback_destroy(f); }
static const struct wp_presentation_feedback_listener lat_listener = { fb_sync, lat_presented, lat_discarded };
static void g_presented_cb(void *d, struct wp_presentation_feedback *f, uint32_t a, uint32_t b, uint32_t c, uint32_t r, uint32_t e, uint32_t g, uint32_t flags) {
(void)d; (void)a; (void)b; (void)c; (void)r; (void)e; (void)g;
@@ -513,13 +495,9 @@ static void redraw(void) {
if (!b) { no_buffer++; }
}
double t = now_ms();
- if (frames) {
- intervals[nint < HIST ? nint++ : (int)(frames % HIST)] = t - t_last;
- } else t_first = t;
- t_last = t;
if (b) {
+ bench_frames_add(&ft, t);
draw(b);
- frames++;
}
if (video_fps) { /* how many refreshes each content frame stays */
long idx = (long)(t * video_fps / 1000.0);
@@ -728,47 +706,38 @@ static const struct wp_presentation_listener pres_listener = { pres_clock };
/* ---- the --auto report ---- */
static void report(FILE *o) {
- double span = t_last - t_first, sum = 0, mx = 0;
- for (int i = 0; i < nint; i++) { sum += intervals[i]; if (intervals[i] > mx) mx = intervals[i]; }
+ BenchStats fs;
+ bench_frames_stats(&ft, &fs);
+ double hz = bench_refresh_hz(&pr) > 0 ? bench_refresh_hz(&pr) : out_refresh_mhz / 1000.0;
fprintf(o, "{\"tool\":\"lordos-testbench\",\"v\":1,\"at\":%lld,\"window\":[%d,%d],\"frames\":%lu,\"fps\":%.2f,\"frameMsAvg\":%.3f,\"frameMsMax\":%.3f,"
+ "\"frameMsP50\":%.3f,\"frameMsP99\":%.3f,\"missedBudget\":%d,"
"\"noFreeBuffer\":%lu,\"presentation\":%s,\"presented\":%lu,\"discarded\":%lu,\"refreshHz\":%.3f,\"vblankSkips\":%lu,\"zeroCopy\":%lu,"
"\"keyboard\":%s,\"pointer\":%s,\"repeat\":[%d,%d],\"globals\":[",
- (long long)time(NULL) * 1000, win_w, win_h, frames, span > 0 && frames > 1 ? (frames - 1) * 1000.0 / span : 0.0, nint ? sum / nint : 0, mx,
- no_buffer, presentation ? "true" : "false", presented, discarded, refresh_ns ? 1e9 / refresh_ns : 0, vblank_skips, zero_copy,
+ (long long)time(NULL) * 1000, win_w, win_h, ft.frames, bench_frames_fps(&ft), fs.avg, fs.max,
+ fs.p50, fs.p99, hz > 0 ? bench_frames_missed(&ft, 1000.0 / hz) : 0,
+ no_buffer, presentation ? "true" : "false", pr.presented, pr.discarded, bench_refresh_hz(&pr), pr.vblank_skips, pr.zero_copy,
keyboard ? "true" : "false", pointer ? "true" : "false", repeat_rate, repeat_delay);
for (int i = 0; i < nglobals; i++) fprintf(o, "%s{\"interface\":\"%s\",\"version\":%u}", i ? "," : "", globals[i].iface, globals[i].version);
static const char *st[4] = { "untested", "pass", "fail", "info" };
- fprintf(o, "],\"outputRefreshHz\":%.3f,\"latency\":{\"n\":%lu,\"avgMs\":%.2f,\"notPresented\":%lu},", out_refresh_mhz / 1000.0, lat_n, lat_n ? lat_sum / lat_n : 0, lat_lost);
- fprintf(o, "\"gl\":{\"renderer\":\"%s\",\"vendor\":\"%s\",\"version\":\"%s\",\"kind\":\"%s\",\"error\":\"%s\",\"drawMs\":%.3f,\"frames\":%lu,\"presented\":%lu,\"zeroCopy\":%lu},\"checks\":[",
- gl3d.renderer, gl3d.vendor, gl3d.version, gl3d.kind, gl3d.error, gl3d.gpu_ms, gl3d.frames, g_presented, g_zero);
+ fprintf(o, "],\"outputRefreshHz\":%.3f,\"latency\":{\"n\":%lu,\"avgMs\":%.2f,\"notPresented\":%lu},", out_refresh_mhz / 1000.0, lat.n, bench_latency_avg(&lat), lat.lost);
+ fprintf(o, "\"gl\":{\"renderer\":");
+ bench_json_str(o, gl3d.renderer);
+ fprintf(o, ",\"vendor\":");
+ bench_json_str(o, gl3d.vendor);
+ fprintf(o, ",\"version\":");
+ bench_json_str(o, gl3d.version);
+ fprintf(o, ",\"kind\":\"%s\",\"error\":\"%s\",\"drawMs\":%.3f,\"frames\":%lu,\"presented\":%lu,\"zeroCopy\":%lu},\"checks\":[",
+ gl3d.kind, gl3d.error, gl3d.gpu_ms, gl3d.frames, g_presented, g_zero);
for (int i = 0; i < nchecks; i++) {
- fprintf(o, "%s{\"id\":\"%s\",\"state\":\"%s\",\"detail\":\"", i ? "," : "", checks[i].id, st[checks[i].state & 3]);
- for (const char *q = checks[i].detail; *q; q++) if (*q != '"' && *q != '\\') fputc(*q, o);
- fprintf(o, "\"}");
+ fprintf(o, "%s{\"id\":\"%s\",\"state\":\"%s\",\"detail\":", i ? "," : "", checks[i].id, st[checks[i].state & 3]);
+ bench_json_str(o, checks[i].detail);
+ fprintf(o, "}");
}
fprintf(o, "]}\n");
}
-/* The last report, for System Information's Checks: $XDG_STATE_HOME/lordos/testbench.json (or
- * ~/.local/state/lordos/testbench.json), written whole and renamed into place. */
-static void save_report(void) {
- const char *st = getenv("XDG_STATE_HOME"), *home = getenv("HOME");
- char dir[512], tmp[600], path[600];
- if (st && st[0] == '/') snprintf(dir, sizeof dir, "%s/lordos", st);
- else if (home && home[0] == '/') snprintf(dir, sizeof dir, "%s/.local/state/lordos", home);
- else return;
- char parent[512];
- snprintf(parent, sizeof parent, "%s", dir);
- char *sl = strrchr(parent, '/');
- if (sl) { *sl = 0; mkdir(parent, 0700); }
- mkdir(dir, 0700);
- snprintf(tmp, sizeof tmp, "%s/.testbench.json.tmp", dir);
- snprintf(path, sizeof path, "%s/testbench.json", dir);
- FILE *f = fopen(tmp, "we");
- if (!f) return;
- report(f);
- if (fclose(f) == 0) rename(tmp, path); else unlink(tmp);
-}
+/* The last report, for System Information's Checks: ~/.local/state/lordos/testbench.json (bench_save). */
+static void save_report(void) { bench_save("testbench", report); }
int main(int argc, char **argv) {
double auto_s = 0;
@@ -809,18 +778,18 @@ int main(int argc, char **argv) {
double start = now_ms();
int ran = 0;
while (!closed && wl_display_dispatch(dpy) != -1) {
- if ((!ran && frames > 2) || rerun_checks) {
+ if ((!ran && ft.frames > 2) || rerun_checks) {
ran = 1;
rerun_checks = 0;
if (checks_run() < 0) { save_report(); break; } /* the compositor ended the connection */
save_report();
if (auto_gl && subcomp && !gl3d_on()) gl3d_start(dpy, comp, subcomp, surf, card_x, card_y, card.w, card.h);
}
- if (auto_s > 0 && frames && now_ms() - start > auto_s * 1000) break;
+ if (auto_s > 0 && ft.frames && now_ms() - start > auto_s * 1000) break;
}
if (wl_display_get_error(dpy) && !checks_done) fprintf(stderr, "lordos-testbench: the connection ended (error %d)\n", wl_display_get_error(dpy));
if (auto_s > 0) report(stdout);
- if (frames) save_report();
+ if (ft.frames) save_report();
for (int i = 0; i < 2; i++) buf_free(&bufs[i]);
card_free(&card);
wl_display_disconnect(dpy);
diff --git a/server/tests/bench_test.c b/server/tests/bench_test.c
new file mode 100644
index 0000000..c5441d2
--- /dev/null
+++ b/server/tests/bench_test.c
@@ -0,0 +1,102 @@
+/* bench_test.c — the measurement pieces shared by lordos-testbench and LordBench (src/bench/bench.c). */
+#define _XOPEN_SOURCE 700
+#define _DARWIN_C_SOURCE
+#include "../src/bench/bench.h"
+
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+static int fails, oks;
+#define CHECK(c, ...) do { if (c) oks++; else { fails++; printf("FAIL %s:%d: ", __FILE__, __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0)
+static int near(double a, double b) { return fabs(a - b) < 1e-6; }
+
+static void w_hello(FILE *o) { fputs("{\"x\":1}\n", o); }
+
+int main(void) {
+ /* frame times: 100 frames at 16 ms with one 50 ms stall */
+ BenchFrames f;
+ memset(&f, 0, sizeof f);
+ double t = 1000;
+ for (int i = 0; i < 100; i++) { bench_frames_add(&f, t); t += i == 50 ? 50 : 16; }
+ BenchStats s;
+ bench_frames_stats(&f, &s);
+ CHECK(f.frames == 100 && f.n == 99, "frames %lu n %d", f.frames, f.n);
+ CHECK(near(s.max, 50) && near(s.p50, 16) && near(s.p99, 50), "max %f p50 %f p99 %f", s.max, s.p50, s.p99); /* 1 in 99: the p99 */
+ CHECK(near(s.avg, (98 * 16 + 50) / 99.0), "avg %f", s.avg);
+ CHECK(bench_frames_missed(&f, 1000.0 / 60) == 1, "missed at 60 Hz");
+ CHECK(bench_frames_missed(&f, 1000.0 / 144) == 99, "missed at 144 Hz %d", bench_frames_missed(&f, 1000.0 / 144));
+ CHECK(near(bench_frames_fps(&f), 99 * 1000.0 / (98 * 16 + 50)), "fps %f", bench_frames_fps(&f));
+ /* the ring keeps the last BENCH_HIST intervals; p99 with two stalls in 240 */
+ memset(&f, 0, sizeof f);
+ for (int i = 0; i <= 1000; i++) bench_frames_add(&f, i * 10.0 + (i > 990 ? 30 : 0));
+ bench_frames_stats(&f, &s);
+ CHECK(f.n == BENCH_HIST && near(s.max, 40) && near(s.p50, 10), "ring n %d max %f p50 %f", f.n, s.max, s.p50);
+ /* 1 stall in 199 intervals stays out of the p99 (nearest rank 198) */
+ memset(&f, 0, sizeof f);
+ for (int i = 0; i < 200; i++) bench_frames_add(&f, i * 16.0 + (i >= 100 ? 34 : 0));
+ bench_frames_stats(&f, &s);
+ CHECK(f.n == 199 && near(s.p99, 16) && near(s.max, 50), "p99 199: %f", s.p99);
+ BenchFrames e;
+ memset(&e, 0, sizeof e);
+ bench_frames_stats(&e, &s);
+ CHECK(s.avg == 0 && s.p99 == 0 && bench_frames_fps(&e) == 0 && bench_frames_missed(&e, 16) == 0, "empty");
+
+ /* presentation: vblank counter, and without one the times */
+ BenchPresent p;
+ memset(&p, 0, sizeof p);
+ bench_present(&p, 0, 100, 16666667, 0);
+ bench_present(&p, 16.7, 101, 0, 1);
+ bench_present(&p, 66.7, 104, 0, 0);
+ CHECK(p.presented == 3 && p.vblank_skips == 2 && p.zero_copy == 1 && fabs(bench_refresh_hz(&p) - 60) < 0.01, "msc skips %lu", p.vblank_skips);
+ memset(&p, 0, sizeof p);
+ bench_present(&p, 0, 0, 16666667, 0);
+ bench_present(&p, 50, 0, 0, 0); /* 3 refreshes later: 2 skipped */
+ bench_present(&p, 66.7, 0, 0, 0);
+ CHECK(p.vblank_skips == 2, "time skips %lu", p.vblank_skips);
+
+ /* latency */
+ BenchLatency l = { .last = -1 };
+ bench_latency_add(&l, 20);
+ bench_latency_add(&l, 30);
+ bench_latency_add(&l, -1);
+ bench_latency_add(&l, 5000);
+ CHECK(l.n == 2 && l.lost == 2 && near(bench_latency_avg(&l), 25) && near(l.last, 30), "latency");
+
+ /* the GL renderer's kind */
+ CHECK(!strcmp(bench_gl_kind("llvmpipe (LLVM 19.1.7, 256 bits)"), "SOFTWARE (CPU)"), "llvmpipe");
+ CHECK(!strcmp(bench_gl_kind("virgl (AMD Radeon)"), "VIRTUAL GPU (HOST)"), "virgl");
+ CHECK(!strcmp(bench_gl_kind("AMD Radeon RX 580 (radeonsi, polaris10)"), "HARDWARE GPU"), "amd");
+ CHECK(!strcmp(bench_gl_kind("V3D 7.1.7.0"), "HARDWARE GPU"), "pi 5");
+ CHECK(!strcmp(bench_gl_kind(""), "") && !strcmp(bench_gl_kind(NULL), ""), "empty renderer");
+
+ /* JSON strings */
+ char *buf = NULL;
+ size_t len = 0;
+ FILE *m = open_memstream(&buf, &len);
+ bench_json_str(m, "a\"b\\c\n\x01");
+ fclose(m);
+ CHECK(!strcmp(buf, "\"a\\\"b\\\\c\\u000a\\u0001\""), "json %s", buf);
+ free(buf);
+
+ /* the state file: written whole under $XDG_STATE_HOME/lordos, bad names refused */
+ char dir[] = "/tmp/bench-test-XXXXXX";
+ CHECK(mkdtemp(dir) != NULL, "mkdtemp");
+ setenv("XDG_STATE_HOME", dir, 1);
+ CHECK(bench_save("testbench", w_hello) == 0, "save");
+ char path[128], got[32] = "";
+ snprintf(path, sizeof path, "%s/lordos/testbench.json", dir);
+ FILE *r = fopen(path, "r");
+ CHECK(r && fgets(got, sizeof got, r) && !strcmp(got, "{\"x\":1}\n"), "saved content");
+ if (r) fclose(r);
+ CHECK(bench_save("../x", w_hello) < 0 && bench_save("A", w_hello) < 0 && bench_save("", w_hello) < 0, "bad names");
+ unlink(path);
+ snprintf(path, sizeof path, "%s/lordos", dir);
+ rmdir(path);
+ rmdir(dir);
+
+ printf("bench-test: %d ok, %d failed\n", oks, fails);
+ return fails ? 1 : 0;
+}