#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;
}