Artifact 176ceb0a2238ae0576c0998a1f7adc72776b34ee8068b2f4a6b487071ebb8535:
- File
src/bench.c
— part of check-in
[2f274ce15b]
at
2026-11-12 22:28:09
on branch trunk
— Phase 2: the store, a working copy that is never half written
The store is a working copy of a wardrobe, kept through libfossil's C API with suit's own file writer: every file is written beside itself and renamed, so a reader sees the old file or the new one.
suit store open, status and update, suit commit and suit back. An update refuses when there are local changes, runs as a dry run first, and changes nothing if any file would conflict.
Proven here: the store is byte-identical to fossil's own checkout of a 600-file wardrobe at each version; a reader hashing files during a slowed update saw only old or new contents; an update that fails at any call leaves the old bytes and no temporary file. 100% of functions, lines, regions and branches in all ten source files.
Vendored, unedited: libfossil trunk f2983dcd78, SQLite 3.55.0, zlib 1.3.1. The release binary links only the system library.
Speed: with everything linked, suit costs 0.82 ms over an empty program quiet and up to 1.38 ms under load, which is over budget. The timer's entry point is now suit-run, a 35 KB program built from the no-change path alone, at 0.40 ms quiet and about 0.5 ms loaded. It runs suit only when something changed. The budget test is on suit-run.
Known limits, recorded in docs/decisions.md: libfossil's checkout writes symlinks as regular files, so wardrobes may not hold them; suit back undoes an update and not a check-in; and a store left half updated by a double failure has no repair command yet.
Not yet run: Fil-C and the Linux build of this phase. (user: dmestas, size: 9496) [annotate] [blame] [check-ins using]
#include "bench.h" #include "platform.h" #include <errno.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/resource.h> #include <sys/wait.h> #include <unistd.h> constexpr int ROUNDS_DEFAULT = 600; constexpr int ROUNDS_MAX = 10000; constexpr int WARMUP_ROUNDS = 10; constexpr long BUDGET_TIME_US = 1000; constexpr long BUDGET_RESIDENT_KIB = 1024; constexpr size_t SELF_PATH_MAX = 4096; enum { PROG_TRUE, PROG_EMPTY, PROG_SUIT, PROG_COUNT }; typedef struct { uint64_t ns[ROUNDS_MAX]; uint64_t resident[ROUNDS_MAX]; } Series; static Series series[PROG_COUNT]; static int64_t diff_ns[2][ROUNDS_MAX]; // suit minus empty, suit minus true static int64_t diff_resident[2][ROUNDS_MAX]; // Spawn PATH, wait, and record wall time and peak resident size. static bool spawn_once(const char *path, char *const argv[], uint64_t *ns, uint64_t *resident) { uint64_t start, stop; pid_t pid; if (!sys_now_ns(&start)) { say(STDERR_FILENO, "suit bench: cannot read the clock\n"); return false; } int rc = sys_spawn(&pid, path, argv); if (rc != 0) { say(STDERR_FILENO, "suit bench: cannot spawn %s: %s\n", path, strerror(rc)); return false; } int status; struct rusage ru; while (sys_wait4(pid, &status, &ru) < 0) { if (errno == EINTR) continue; say(STDERR_FILENO, "suit bench: wait4 failed: %s\n", strerror(errno)); return false; } if (!sys_now_ns(&stop)) { say(STDERR_FILENO, "suit bench: cannot read the clock\n"); return false; } *ns = stop - start; *resident = platform_peak_resident(&ru); if (WIFEXITED(status) && WEXITSTATUS(status) == 0) return true; if (WIFEXITED(status)) say(STDERR_FILENO, "suit bench: %s exited with status %d (a stamp is needed " "for the run to succeed)\n", path, WEXITSTATUS(status)); else say(STDERR_FILENO, "suit bench: %s was killed by signal %d\n", path, WTERMSIG(status)); return false; } static int cmp_u64(const void *pa, const void *pb) { uint64_t a = *(const uint64_t *)pa; uint64_t b = *(const uint64_t *)pb; return (a > b) - (a < b); } static int cmp_i64(const void *pa, const void *pb) { int64_t a = *(const int64_t *)pa; int64_t b = *(const int64_t *)pb; return (a > b) - (a < b); } typedef struct { uint64_t median, p95, max; } Stats; // Sorts V. Nearest-rank percentile. static Stats summarize(uint64_t *v, int n) { ensure(n > 0); qsort(v, (size_t)n, sizeof v[0], cmp_u64); int rank = (n * 95 + 99) / 100; // ceil(0.95 n) return (Stats){.median = v[n / 2], .p95 = v[rank - 1], .max = v[n - 1]}; } static int64_t median_i64(int64_t *v, int n) { qsort(v, (size_t)n, sizeof v[0], cmp_i64); return v[n / 2]; } static bool parse_int(const char *s, long lo, long hi, long *out) { char *end; errno = 0; long v = strtol(s, &end, 10); if (end == s || errno != 0 || *end != '\0' || v < lo || v > hi) return false; *out = v; return true; } typedef struct { int rounds; const char *empty; long time_us; long resident_kib; } Options; static bool parse_args(int argc, char **argv, Options *o) { o->rounds = ROUNDS_DEFAULT; o->empty = nullptr; o->time_us = BUDGET_TIME_US; o->resident_kib = BUDGET_RESIDENT_KIB; long v; for (int i = 0; i < argc; i += 2) { if (i + 1 >= argc) return false; const char *opt = argv[i]; const char *arg = argv[i + 1]; if (strcmp(opt, "-e") == 0) { o->empty = arg; } else if (strcmp(opt, "-n") == 0) { if (!parse_int(arg, 1, ROUNDS_MAX, &v)) return false; o->rounds = (int)v; } else if (strcmp(opt, "-t") == 0) { if (!parse_int(arg, -1000000, 1000000, &o->time_us)) return false; } else if (strcmp(opt, "-m") == 0) { if (!parse_int(arg, -1048576, 1048576, &o->resident_kib)) return false; } else { return false; } } return true; } static double ms(int64_t ns) { return (double)ns / 1e6; } static double kib(int64_t bytes) { return (double)bytes / 1024.0; } static void print_row(const char *name, Stats t, Stats r) { say(STDOUT_FILENO, "%-28s %9.3f %9.3f %9.3f %8.1f %8.1f %8.1f\n", name, ms((int64_t)t.median), ms((int64_t)t.p95), ms((int64_t)t.max), kib((int64_t)r.median), kib((int64_t)r.p95), kib((int64_t)r.max)); } static void print_over(const char *name, Stats a, Stats b, Stats ra, Stats rb) { say(STDOUT_FILENO, "%-28s %9.3f %9.3f %9.3f %8.1f %8.1f %8.1f\n", name, ms((int64_t)a.median - (int64_t)b.median), ms((int64_t)a.p95 - (int64_t)b.p95), ms((int64_t)a.max - (int64_t)b.max), kib((int64_t)ra.median - (int64_t)rb.median), kib((int64_t)ra.p95 - (int64_t)rb.p95), kib((int64_t)ra.max - (int64_t)rb.max)); } // Run every program WARMUP_ROUNDS times, then ROUNDS times with the order // rotated each round so no program always runs first. static bool measure(const Options *o, const char *const path[PROG_COUNT], char *const *args[PROG_COUNT]) { uint64_t ns, resident; for (int i = 0; i < WARMUP_ROUNDS; i++) for (int p = 0; p < PROG_COUNT; p++) if (!spawn_once(path[p], args[p], &ns, &resident)) return false; for (int i = 0; i < o->rounds; i++) { for (int k = 0; k < PROG_COUNT; k++) { int p = (i + k) % PROG_COUNT; if (!spawn_once(path[p], args[p], &series[p].ns[i], &series[p].resident[i])) return false; } } return true; } int bench_main(int argc, char **argv) { Options o; char self[SELF_PATH_MAX]; char empty_buf[SELF_PATH_MAX + 8]; char front[SELF_PATH_MAX + 16]; if (!parse_args(argc, argv, &o)) { say(STDERR_FILENO, "usage: suit bench [-n N] [-e EMPTY_PROGRAM] [-t MICROSECONDS] " "[-m KIB] (N from 1 to %d)\n", ROUNDS_MAX); return 2; } if (!platform_self_path(self, sizeof self)) { say(STDERR_FILENO, "suit bench: cannot find the path of this program\n"); return 1; } // The empty program and suit-run sit next to this one. const char *slash = strrchr(self, '/'); ensure(slash != nullptr); size_t dir = (size_t)(slash - self) + 1; memcpy(front, self, dir); memcpy(front + dir, "suit-run", sizeof "suit-run"); if (o.empty == nullptr) { memcpy(empty_buf, self, dir); memcpy(empty_buf + dir, "empty", sizeof "empty"); o.empty = empty_buf; } char *true_argv[] = {(char *)"true", nullptr}; char *empty_argv[] = {(char *)"empty", nullptr}; char *suit_argv[] = {(char *)"suit-run", nullptr}; const char *path[PROG_COUNT] = {PLATFORM_TRUE_PATH, o.empty, front}; char *const *args[PROG_COUNT] = {true_argv, empty_argv, suit_argv}; if (!measure(&o, path, args)) return 1; // Per-round differences, before the series are sorted. int rounds = o.rounds; for (int i = 0; i < rounds; i++) { int64_t s = (int64_t)series[PROG_SUIT].ns[i]; diff_ns[0][i] = s - (int64_t)series[PROG_EMPTY].ns[i]; diff_ns[1][i] = s - (int64_t)series[PROG_TRUE].ns[i]; int64_t r = (int64_t)series[PROG_SUIT].resident[i]; diff_resident[0][i] = r - (int64_t)series[PROG_EMPTY].resident[i]; diff_resident[1][i] = r - (int64_t)series[PROG_TRUE].resident[i]; } int64_t per_round_ns[2], per_round_rss[2]; for (int d = 0; d < 2; d++) { per_round_ns[d] = median_i64(diff_ns[d], rounds); per_round_rss[d] = median_i64(diff_resident[d], rounds); } Stats t[PROG_COUNT], r[PROG_COUNT]; for (int p = 0; p < PROG_COUNT; p++) { t[p] = summarize(series[p].ns, rounds); r[p] = summarize(series[p].resident, rounds); } say(STDOUT_FILENO, "suit bench: %d rounds of three programs, order rotated each round\n", rounds); say(STDOUT_FILENO, "%-28s %9s %9s %9s %8s %8s %8s\n", "", "time ms", "", "", "resident KiB", "", ""); say(STDOUT_FILENO, "%-28s %9s %9s %9s %8s %8s %8s\n", "", "median", "p95", "max", "median", "p95", "max"); print_row("/usr/bin/true (info only)", t[PROG_TRUE], r[PROG_TRUE]); print_row("empty", t[PROG_EMPTY], r[PROG_EMPTY]); print_row("suit-run", t[PROG_SUIT], r[PROG_SUIT]); print_over("suit-run over empty", t[PROG_SUIT], t[PROG_EMPTY], r[PROG_SUIT], r[PROG_EMPTY]); print_over("suit-run over /usr/bin/true", t[PROG_SUIT], t[PROG_TRUE], r[PROG_SUIT], r[PROG_TRUE]); say(STDOUT_FILENO, "median of per-round differences, suit-run over empty: %7.3f ms %8.1f KiB\n", ms(per_round_ns[0]), kib(per_round_rss[0])); say(STDOUT_FILENO, "median of per-round differences, suit-run over /usr/bin/true: %7.3f ms %8.1f KiB\n", ms(per_round_ns[1]), kib(per_round_rss[1])); bool time_ok = per_round_ns[0] <= (int64_t)o.time_us * 1000; bool rss_ok = per_round_rss[0] <= (int64_t)o.resident_kib * 1024; say(STDOUT_FILENO, "budget: time, per-round median over empty %.3f ms, limit %.3f ms: %s\n", ms(per_round_ns[0]), (double)o.time_us / 1e3, time_ok ? "ok" : "OVER"); say(STDOUT_FILENO, "budget: resident, per-round median over empty %.1f KiB, limit %ld KiB: %s\n", kib(per_round_rss[0]), o.resident_kib, rss_ok ? "ok" : "OVER"); return time_ok && rss_ok ? 0 : 1; }