#include "bench.h"
#include "platform.h"
#include <assert.h>
#include <errno.h>
#include <spawn.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <time.h>
constexpr int ROUNDS_DEFAULT = 600;
constexpr int ROUNDS_MAX = 10000;
constexpr int WARMUP_ROUNDS = 10;
constexpr int64_t BUDGET_MEDIAN_NS = 1000000;
constexpr int64_t BUDGET_RESIDENT_BYTES = 1048576;
constexpr size_t SELF_PATH_MAX = 4096;
extern char **environ;
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];
static uint64_t now_ns(void)
{
struct timespec ts;
int rc = clock_gettime(CLOCK_MONOTONIC, &ts);
assert(rc == 0);
return (uint64_t)ts.tv_sec * 1000000000u + (uint64_t)ts.tv_nsec;
}
// 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)
{
pid_t pid;
uint64_t start = now_ns();
int rc = posix_spawn(&pid, path, nullptr, nullptr, argv, environ);
if (rc != 0) {
fprintf(stderr, "suit bench: cannot spawn %s: %s\n", path, strerror(rc));
return false;
}
int status;
struct rusage ru;
while (wait4(pid, &status, 0, &ru) < 0) {
if (errno == EINTR) continue;
fprintf(stderr, "suit bench: wait4 failed: %s\n", strerror(errno));
return false;
}
*ns = now_ns() - start;
*resident = platform_peak_resident(&ru);
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
fprintf(stderr, "suit bench: %s exited with status %d (a stamp is needed for "
"the run to succeed)\n", path, WIFEXITED(status) ? WEXITSTATUS(status) : -1);
return false;
}
return true;
}
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);
}
typedef struct {
uint64_t median, p95, max;
} Stats;
// Sorts V. Nearest-rank percentile.
static Stats summarize(uint64_t *v, int n)
{
assert(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 double ms(uint64_t ns)
{
return (double)ns / 1e6;
}
static double ms_diff(uint64_t a, uint64_t b)
{
return ((double)a - (double)b) / 1e6;
}
static double kib(uint64_t bytes)
{
return (double)bytes / 1024.0;
}
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);
}
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 int parse_args(int argc, char **argv, int *rounds, const char **empty)
{
*rounds = ROUNDS_DEFAULT;
for (int i = 0; i < argc; i += 2) {
if (i + 1 >= argc) return -1;
if (strcmp(argv[i], "-n") == 0) {
char *end;
errno = 0;
long v = strtol(argv[i + 1], &end, 10);
if (errno != 0 || *end != '\0' || argv[i + 1][0] == '\0' || v < 1 ||
v > ROUNDS_MAX)
return -1;
*rounds = (int)v;
} else if (strcmp(argv[i], "-e") == 0) {
*empty = argv[i + 1];
} else {
return -1;
}
}
return 0;
}
static bool default_empty_path(char *out, size_t cap, const char *self)
{
const char *slash = strrchr(self, '/');
size_t dir = slash != nullptr ? (size_t)(slash - self) + 1 : 0;
if (dir + sizeof "empty" > cap) return false;
memcpy(out, self, dir);
memcpy(out + dir, "empty", sizeof "empty");
return true;
}
static void print_row(const char *name, Stats t, Stats r)
{
printf("%-26s %9.3f %9.3f %9.3f %8.1f %8.1f %8.1f\n", name, ms(t.median),
ms(t.p95), ms(t.max), kib(r.median), kib(r.p95), kib(r.max));
}
static void print_over(const char *name, Stats a, Stats b, Stats ra, Stats rb)
{
printf("%-26s %9.3f %9.3f %9.3f %8.1f %8.1f %8.1f\n", name,
ms_diff(a.median, b.median), ms_diff(a.p95, b.p95), ms_diff(a.max, b.max),
((double)ra.median - (double)rb.median) / 1024.0,
((double)ra.p95 - (double)rb.p95) / 1024.0,
((double)ra.max - (double)rb.max) / 1024.0);
}
int bench_main(int argc, char **argv)
{
int rounds;
char self[SELF_PATH_MAX];
char empty_buf[SELF_PATH_MAX + 8];
const char *empty = nullptr;
if (parse_args(argc, argv, &rounds, &empty) != 0) {
fprintf(stderr, "usage: suit bench [-n N] [-e EMPTY_PROGRAM] (N from 1 to %d)\n",
ROUNDS_MAX);
return 2;
}
if (!platform_self_path(self, sizeof self)) {
fprintf(stderr, "suit bench: cannot find the path of this program\n");
return 1;
}
if (empty == nullptr) {
if (!default_empty_path(empty_buf, sizeof empty_buf, self)) {
fprintf(stderr, "suit bench: path of the empty program is too long\n");
return 1;
}
empty = empty_buf;
}
char *true_argv[] = {(char *)"true", nullptr};
char *empty_argv[] = {(char *)"empty", nullptr};
char *suit_argv[] = {(char *)"suit", (char *)"run", nullptr};
const char *path[PROG_COUNT] = {PLATFORM_TRUE_PATH, empty, self};
char *const *args[PROG_COUNT] = {true_argv, empty_argv, suit_argv};
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 1;
// Rotate the order each round so no program always runs first.
for (int i = 0; i < 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 1;
}
// Per-round differences, before the series are sorted.
for (int i = 0; i < rounds; i++) {
const 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];
const 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);
}
printf("suit bench: %d rounds of three programs, order rotated each round\n", rounds);
printf("%-26s %9s %9s %9s %8s %8s %8s\n", "", "time ms", "", "", "resident KiB", "", "");
printf("%-26s %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 over empty", t[PROG_SUIT], t[PROG_EMPTY], r[PROG_SUIT], r[PROG_EMPTY]);
print_over("suit over /usr/bin/true", t[PROG_SUIT], t[PROG_TRUE], r[PROG_SUIT],
r[PROG_TRUE]);
printf("median of per-round differences, suit over empty: %7.3f ms %8.1f KiB\n",
(double)per_round_ns[0] / 1e6, (double)per_round_rss[0] / 1024.0);
printf("median of per-round differences, suit over /usr/bin/true: %7.3f ms %8.1f KiB\n",
(double)per_round_ns[1] / 1e6, (double)per_round_rss[1] / 1024.0);
bool time_ok = per_round_ns[0] <= BUDGET_MEDIAN_NS;
bool rss_ok = per_round_rss[0] <= BUDGET_RESIDENT_BYTES;
printf("budget: time, per-round median over empty %.3f ms, limit %.3f ms: %s\n",
(double)per_round_ns[0] / 1e6, (double)BUDGET_MEDIAN_NS / 1e6,
time_ok ? "ok" : "OVER");
printf("budget: resident, per-round median over empty %.1f KiB, limit %.1f KiB: %s\n",
(double)per_round_rss[0] / 1024.0, (double)BUDGET_RESIDENT_BYTES / 1024.0,
rss_ok ? "ok" : "OVER");
return time_ok && rss_ok ? 0 : 1;
}