~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/zlay

atproto relay in zig · mirror of https://tangled.org/zat.dev/zlay

0
0
175 lines · 6.5 KB Raw History
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#ifndef __APPLE__
#include <sys/syscall.h>
#endif

enum { count = 512, bytes = 160 };
struct sums {
    uint64_t create, write, file_sync, publish, dir_sync, close, cleanup;
};

static void check(int rc) {
    if (rc < 0) { perror("publication probe"); exit(1); }
}

static uint64_t now(void) {
#ifdef __APPLE__
    return clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
#else
    struct timespec time;
    check(clock_gettime(CLOCK_MONOTONIC, &time));
    return (uint64_t)time.tv_sec * 1000000000 + time.tv_nsec;
#endif
}

static int compare(const void *a, const void *b) {
    uint64_t left = *(const uint64_t *)a, right = *(const uint64_t *)b;
    return (left > right) - (left < right);
}

static void payload(unsigned char *out, unsigned n) {
    memset(out, 0, bytes);
    for (unsigned i = 0; i < 4; ++i) out[i] = (n >> (8 * i)) & 255;
}

static void write_all(int fd, const unsigned char *data) {
    size_t offset = 0;
    while (offset < bytes) {
        ssize_t written = write(fd, data + offset, bytes - offset);
        check((int)written);
        if (!written) abort();
        offset += written;
    }
}

static void verify(int fd, unsigned n, off_t offset) {
    unsigned char actual[bytes], expected[bytes];
    size_t at = 0;
    while (at < bytes) {
        ssize_t received = pread(fd, actual + at, bytes - at, offset + at);
        check((int)received);
        if (!received) abort();
        at += received;
    }
    payload(expected, n);
    if (memcmp(expected, actual, bytes)) abort();
}

static int publish_exclusive(const char *source, const char *target) {
#ifdef __APPLE__
    return renamex_np(source, target, RENAME_EXCL);
#else
    return (int)syscall(SYS_renameat2, AT_FDCWD, source, AT_FDCWD, target, 1);
#endif
}

static void run(const char *root, const char *mode) {
    char path[2048], file[2048], temp[2048];
    int length = snprintf(path, sizeof path, "%s/%s", root, mode);
    if (length < 0 || (size_t)length >= sizeof path) abort();
    check(mkdir(path, 0700));
    int dir = open(path, O_RDONLY);
    check(dir);
    int object = strncmp(mode, "append", 6) != 0;
    int rename = !strncmp(mode, "rename", 6);
    int file_sync = strstr(mode, "file") || strstr(mode, "both");
    int dir_sync = strstr(mode, "parent") || strstr(mode, "both");
    unsigned window = !strcmp(mode, "append-sync64") ? 64 :
                      !strcmp(mode, "append-sync1") ? 1 : 0;
    int append = -1;
    if (!object) {
        snprintf(file, sizeof file, "%s/wal", path);
        append = open(file, O_RDWR | O_CREAT | O_EXCL | O_APPEND, 0600);
        check(append);
        check(fsync(dir));
    }
    struct sums sum = {0};
    uint64_t samples[count], all_start = now();
    for (unsigned n = 0; n < count; ++n) {
        unsigned char data[bytes];
        payload(data, n);
        uint64_t start = now(), before = start;
        int fd = append;
        if (object) {
            snprintf(file, sizeof file, "%s/%08u", path, n);
            snprintf(temp, sizeof temp, "%s/%08u.tmp", path, n);
            fd = open(temp, O_RDWR | O_CREAT | O_EXCL, 0600);
            check(fd);
        }
        uint64_t after = now(); sum.create += after - before; before = after;
        write_all(fd, data);
        after = now(); sum.write += after - before; before = after;
        if (file_sync || (window && (n + 1) % window == 0)) check(fsync(fd));
        after = now(); sum.file_sync += after - before; before = after;
        if (object) check(close(fd));
        after = now(); sum.close += after - before; before = after;
        if (object) check(rename ? publish_exclusive(temp, file) : link(temp, file));
        after = now(); sum.publish += after - before; before = after;
        if (dir_sync) check(fsync(dir));
        after = now(); sum.dir_sync += after - before; before = after;
        if (object && !rename) check(unlink(temp));
        after = now(); sum.cleanup += after - before;
        samples[n] = after - start;
    }
    uint64_t elapsed = now() - all_start;
    if (!object) check(close(append));
    qsort(samples, count, sizeof *samples, compare);
    printf("%s,%u,%llu,%llu,%llu,%llu,%llu,%llu,%llu,%llu,%llu,%llu\n",
           mode, count, (unsigned long long)elapsed,
           (unsigned long long)samples[255], (unsigned long long)samples[486],
           (unsigned long long)sum.create, (unsigned long long)sum.write,
           (unsigned long long)sum.file_sync, (unsigned long long)sum.close,
           (unsigned long long)sum.publish, (unsigned long long)sum.dir_sync,
           (unsigned long long)sum.cleanup);
    for (unsigned n = 0; n < count; ++n) {
        if (object) snprintf(file, sizeof file, "%s/%08u", path, n);
        int fd = open(file, O_RDONLY);
        check(fd);
        verify(fd, n, object ? 0 : (off_t)n * bytes);
        check(close(fd));
        if (object) check(unlink(file));
    }
    if (!object) check(unlink(file));
    check(close(dir));
    check(rmdir(path));
}

static void check_collision(const char *root) {
    char source[2048], target[2048];
    snprintf(source, sizeof source, "%s/source", root);
    snprintf(target, sizeof target, "%s/target", root);
    unsigned char data[bytes];
    int a = open(source, O_RDWR | O_CREAT | O_EXCL, 0600);
    int b = open(target, O_RDWR | O_CREAT | O_EXCL, 0600);
    check(a); check(b);
    payload(data, 1); write_all(a, data);
    payload(data, 2); write_all(b, data);
    if (link(source, target) != -1 || errno != EEXIST) abort();
    if (publish_exclusive(source, target) != -1 || errno != EEXIST) abort();
    verify(a, 1, 0); verify(b, 2, 0);
    check(close(a)); check(close(b));
    check(unlink(source)); check(unlink(target));
}

int main(int argc, char **argv) {
    if (argc != 2 && argc != 3) return 2;
    check(mkdir(argv[1], 0700));
    check_collision(argv[1]);
    puts("mode,records,elapsed_ns,p50_ns,p95_ns,create_ns,write_ns,file_sync_ns,close_ns,publish_ns,parent_sync_ns,cleanup_ns");
    const char *modes[] = {"append-buffered", "append-sync1", "append-sync64",
                          "object-buffered", "object-file", "object-parent", "object-both",
                          "rename-buffered", "rename-both"};
    size_t total = sizeof modes / sizeof *modes;
    size_t offset = argc == 3 ? strtoul(argv[2], NULL, 10) % total : 0;
    for (size_t n = 0; n < total; ++n) run(argv[1], modes[(n + offset) % total]);
    check(rmdir(argv[1]));
}