omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tools / argus_replay.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * argus_replay.c -- replay a raw ARGUS runtime stream (128-byte records, as |
| 19 | * written by rx_argus RX_ARGUS_STREAM) into a fresh argus_core, twice, and |
| 20 | * print per-kind counts, findings and the determinism check. |
| 21 | * |
| 22 | * argus_replay FILE [-v] -v also prints every finding's triggering event |
| 23 | */ |
| 24 | #include "argus_abi.h" |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <time.h> |
| 30 | |
| 31 | static _Alignas(64) uint8_t core_mem[1 << 20]; |
| 32 | |
| 33 | typedef struct { |
| 34 | uint64_t by_kind[ARGUS_EV_KIND_MAX + 1], by_code[ARGUS_F_MAX + 1]; |
| 35 | uint64_t events, decode_fail, findings; |
| 36 | uint8_t state[ARGUS_DIGEST_LEN]; |
| 37 | ArgusCoreHealth h; |
| 38 | uint64_t ingest_ns; |
| 39 | } Replay; |
| 40 | |
| 41 | static void print_event(const char *tag, const ArgusEvent *e) { |
| 42 | printf("%s seq=%llu kind=%u class=%u outcome=%u code=%d principal=%u cap=%u/%llu " |
| 43 | "obj=%u res=%llu world=%llu flags=%u tick=%llu\n", |
| 44 | tag, (unsigned long long)e->sequence, e->kind, e->class_, e->outcome, e->code, |
| 45 | e->principal, e->cap_id, (unsigned long long)e->cap_generation, e->object_id, |
| 46 | (unsigned long long)e->resource, (unsigned long long)e->world_generation, e->flags, |
| 47 | (unsigned long long)e->tick); |
| 48 | } |
| 49 | |
| 50 | static int replay(const uint8_t *buf, size_t n, Replay *r, int verbose) { |
| 51 | memset(r, 0, sizeof *r); |
| 52 | ArgusCore *core; |
| 53 | if (argus_core_footprint() > sizeof core_mem || |
| 54 | argus_core_init(&core, core_mem, sizeof core_mem) != ARGUS_OK) |
| 55 | return -1; |
| 56 | for (size_t i = 0; i + ARGUS_EVENT_SIZE <= n; i += ARGUS_EVENT_SIZE) { |
| 57 | ArgusEvent ev; |
| 58 | if (argus_event_decode(buf + i, &ev) != ARGUS_OK) { |
| 59 | r->decode_fail++; |
| 60 | continue; |
| 61 | } |
| 62 | r->events++; |
| 63 | if (ev.kind <= ARGUS_EV_KIND_MAX) r->by_kind[ev.kind]++; |
| 64 | ArgusFinding f[32]; |
| 65 | size_t nf = 0; |
| 66 | struct timespec t0, t1; |
| 67 | clock_gettime(CLOCK_MONOTONIC, &t0); |
| 68 | argus_core_ingest(core, &ev, f, 32, &nf); |
| 69 | clock_gettime(CLOCK_MONOTONIC, &t1); |
| 70 | r->ingest_ns += (uint64_t)(t1.tv_sec - t0.tv_sec) * 1000000000ull + (uint64_t)(t1.tv_nsec - t0.tv_nsec); |
| 71 | for (size_t j = 0; j < nf; j++) { |
| 72 | r->findings++; |
| 73 | if (f[j].code <= ARGUS_F_MAX) r->by_code[f[j].code]++; |
| 74 | if (verbose) { |
| 75 | printf("finding code=%u sev=%u seq=%llu prior=%llu | ", f[j].code, f[j].severity, |
| 76 | (unsigned long long)f[j].sequence, (unsigned long long)f[j].prior_sequence); |
| 77 | print_event("event", &ev); |
| 78 | } |
| 79 | } |
| 80 | if (verbose > 1) print_event(" ", &ev); |
| 81 | } |
| 82 | argus_core_state_digest(core, r->state); |
| 83 | argus_core_health(core, &r->h); |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | int main(int argc, char **argv) { |
| 88 | if (argc < 2) { |
| 89 | fprintf(stderr, "usage: %s FILE [-v|-vv]\n", argv[0]); |
| 90 | return 2; |
| 91 | } |
| 92 | int verbose = argc > 2 ? (strcmp(argv[2], "-vv") == 0 ? 2 : 1) : 0; |
| 93 | FILE *f = fopen(argv[1], "rb"); |
| 94 | if (!f) { perror(argv[1]); return 2; } |
| 95 | fseek(f, 0, SEEK_END); |
| 96 | long len = ftell(f); |
| 97 | fseek(f, 0, SEEK_SET); |
| 98 | uint8_t *buf = malloc(len > 0 ? (size_t)len : 1); |
| 99 | if (!buf || fread(buf, 1, (size_t)len, f) != (size_t)len) { fclose(f); return 2; } |
| 100 | fclose(f); |
| 101 | Replay a, b; |
| 102 | if (replay(buf, (size_t)len, &a, verbose) || replay(buf, (size_t)len, &b, 0)) return 2; |
| 103 | printf("records %ld (%s), decoded %llu, decode failures %llu\n", len / ARGUS_EVENT_SIZE, |
| 104 | len % ARGUS_EVENT_SIZE ? "TRAILING BYTES" : "whole", (unsigned long long)a.events, |
| 105 | (unsigned long long)a.decode_fail); |
| 106 | for (unsigned k = 0; k <= ARGUS_EV_KIND_MAX; k++) |
| 107 | if (a.by_kind[k]) printf(" kind %u: %llu\n", k, (unsigned long long)a.by_kind[k]); |
| 108 | printf("core ingest: %.0f ns/event mean (second pass)\n", b.events ? (double)b.ingest_ns / (double)b.events : 0.0); |
| 109 | printf("findings %llu\n", (unsigned long long)a.findings); |
| 110 | for (unsigned c = 0; c <= ARGUS_F_MAX; c++) |
| 111 | if (a.by_code[c]) printf(" code %u: %llu\n", c, (unsigned long long)a.by_code[c]); |
| 112 | printf("core: received %llu rejected %llu not_applied %llu incidents %llu\n", |
| 113 | (unsigned long long)a.h.events_received, (unsigned long long)a.h.events_rejected, |
| 114 | (unsigned long long)a.h.events_not_applied, (unsigned long long)a.h.incidents_open); |
| 115 | int same = memcmp(a.state, b.state, ARGUS_DIGEST_LEN) == 0 && |
| 116 | memcmp(a.h.chain, b.h.chain, ARGUS_DIGEST_LEN) == 0 && a.findings == b.findings; |
| 117 | printf("state digest "); |
| 118 | for (unsigned i = 0; i < ARGUS_DIGEST_LEN; i++) printf("%02x", a.state[i]); |
| 119 | printf("\ndeterministic replay: %s\n", same ? "yes" : "NO"); |
| 120 | free(buf); |
| 121 | return same ? 0 : 1; |
| 122 | } |