Skip to content
pi-c Artifact [c54da25e26]
Login

Artifact [c54da25e26]

Artifact c54da25e26875a2bd6e480f3f346d8f07774d7522618746a0c65a9c1fd1d57d5:


/* Informational native microfixture for S0.
 *
 *   native --startup                      print a fixed checksum.
 *   native --work N [--deadline MILLIS]   bounded loop over an empty state
 *                                         transition and an empty effect
 *                                         record, folding a checksum.
 *
 * Synthetic workload only: nothing here models SQLite, commit, fsync, product
 * code, or any I/O. The checksum is this fixture's workload identity and makes
 * elision observable for these operations; it is not a general barrier that
 * keeps an optimizer from eliminating arbitrary code. Do not read a result
 * here as a product, capacity, or speed claim.
 *
 * With --deadline the fixture enforces its own wall-clock bound, so it stops
 * even if the sampler that launched it is killed and cannot reap the group.
 * The deadline is checked every CHECK_INTERVAL iterations to keep the check
 * off the hot path.
 */
#define _POSIX_C_SOURCE 200809L

#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define MAX_ITERATIONS 100000000ULL
#define MAX_DEADLINE 1800ULL /* seconds */
#define CHECK_INTERVAL 65536ULL
#define WORK_DEADLINE_EXCEEDED 3

struct state {
  uint64_t revision, transitions;
};

struct effect {
  uint64_t kind, payload;
};

static uint64_t mix(uint64_t h, uint64_t v) {
  return h ^ (v + 0x9e3779b97f4a7c15ULL + (h << 6) + (h >> 2));
}

/* Digits only: strtoull would accept a sign and saturate on overflow. */
static int parse_iterations(const char *text, uint64_t *out) {
  char *end = NULL;
  unsigned long long value;

  if (!text || *text < '0' || *text > '9') {
    return 0;
  }
  errno = 0;
  value = strtoull(text, &end, 10);
  if (errno != 0 || !end || *end != '\0' || value == 0 ||
      value > MAX_ITERATIONS) {
    return 0;
  }
  *out = (uint64_t)value;
  return 1;
}

static int transition_apply(struct state *s) { /* no allocation, no I/O */
  s->revision += 1;
  s->transitions += 1;
  return 0;
}

static int effect_record(struct effect *e, uint64_t kind, uint64_t payload) {
  e->kind = kind; /* intent only, no syscall */
  e->payload = payload;
  return 0;
}

/* Returning nonzero marks a deadline overrun; the partial result is discarded
 * by the caller, so no checksum is printed for an interrupted run. */
static int run_work(uint64_t iterations, double limit_ms, uint64_t *checksum) {
  struct state s = {0, 0};
  struct effect e = {0, 0};
  struct timespec started;
  uint64_t h = 1469598103934665603ULL;
  int deadline = limit_ms > 0.0;

  if (deadline && clock_gettime(CLOCK_MONOTONIC, &started) != 0) {
    return 1;
  }
  for (uint64_t i = 0; i < iterations; i++) {
    uint64_t revision;
    if ((i % CHECK_INTERVAL) == 0 && deadline) {
      struct timespec now;
      double elapsed;
      if (clock_gettime(CLOCK_MONOTONIC, &now) != 0) {
        return 1;
      }
      elapsed = (double)(now.tv_sec - started.tv_sec) * 1000.0 +
                (double)(now.tv_nsec - started.tv_nsec) / 1000000.0;
      if (elapsed >= limit_ms) {
        return WORK_DEADLINE_EXCEEDED;
      }
    }
    if (transition_apply(&s) || effect_record(&e, i & 1u, s.revision)) {
      return 1;
    }
    revision = s.revision;
    h = mix(h, revision ^ e.payload ^ e.kind);
  }
  *checksum = mix(h, s.transitions);
  return 0;
}

/* Sub-second granularity so a test can observe the bound directly; the value
 * is parsed as milliseconds with no sign, no whitespace, and no overflow. */
static int parse_deadline(const char *text, double *limit_ms) {
  char *end = NULL;
  unsigned long long value;

  *limit_ms = 0.0;
  if (!text) {
    return 1;
  }
  if (*text < '0' || *text > '9') {
    return 0;
  }
  errno = 0;
  value = strtoull(text, &end, 10);
  if (errno != 0 || !end || *end != '\0' || value == 0 ||
      value > MAX_DEADLINE * 1000ULL) {
    return 0;
  }
  *limit_ms = (double)value;
  return 1;
}

int main(int argc, char **argv) {
  uint64_t checksum = 0;
  double limit_ms = 0.0;

  if (argc == 2 && strcmp(argv[1], "--startup") == 0) {
    if (run_work(1, 0.0, &checksum) != 0) {
      fprintf(stderr, "startup work failed\n");
      return 1;
    }
    printf("startup ok checksum=%llu\n", (unsigned long long)checksum);
    return 0;
  }
  if ((argc == 3 || argc == 5) && strcmp(argv[1], "--work") == 0) {
    uint64_t n = 0;
    if (!parse_iterations(argv[2], &n)) {
      fprintf(stderr, "iterations must be 1..%llu\n",
              (unsigned long long)MAX_ITERATIONS);
      return 2;
    }
    if (argc == 5 && (strcmp(argv[3], "--deadline") != 0 ||
                      !parse_deadline(argv[4], &limit_ms))) {
      fprintf(stderr, "deadline must be --deadline 1..%llu milliseconds\n",
              (unsigned long long)MAX_DEADLINE * 1000ULL);
      return 2;
    }
    if (run_work(n, limit_ms, &checksum) != 0) {
      fprintf(stderr, "work did not complete within its bounds\n");
      return WORK_DEADLINE_EXCEEDED;
    }
    printf("work iterations=%llu checksum=%llu\n", (unsigned long long)n,
           (unsigned long long)checksum);
    return 0;
  }
  fprintf(stderr, "usage: %s --startup | --work ITERATIONS [--deadline S]\n",
          argv[0]);
  return 2;
}