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 / tests / bench_rx_argus.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 | * bench_rx_argus.c -- ARGUS_PERFORMANCE_GATE micro-benchmark (CPU only, no GPU). |
| 19 | * |
| 20 | * One op = one AEGIS policy evaluation (rx_aegis_evaluate, the pure decide |
| 21 | * core) + one authority validate on the reaction path (rx_world_validate_cap |
| 22 | * on a world bound to the native Aienux view, i.e. rx_native_bind's |
| 23 | * native_validate, which is where the ARGUS CAPABILITY_USED emit sits). |
| 24 | * Built identically for RX_ARGUS=0/1/2; the consumer mode for RX_ARGUS=2 is |
| 25 | * chosen at run time by RX_ARGUS_CONSUMER=discard|ingest (rx_argus.c). |
| 26 | * |
| 27 | * Phases: |
| 28 | * throughput N ops, one clock read around the whole loop -> ops/s |
| 29 | * latency S ops, each timed with CLOCK_MONOTONIC -> p50/p99 (includes |
| 30 | * the clock read overhead, reported separately) |
| 31 | * use update RX_ARGUS>=1: the hot-path ARGUS cost alone (use_begin + |
| 32 | * use_end of a successful validate), batches of 256 timed |
| 33 | * (the clock quantizes at 16 ns; op p50/p99 cannot resolve |
| 34 | * sub-16 ns differences, ops/s is the discriminating metric) |
| 35 | * |
| 36 | * bench_rx_argus [N] [S] |
| 37 | */ |
| 38 | #include "runtime/aienos_cap.h" |
| 39 | #include "runtime/rx_aegis.h" |
| 40 | #include "runtime/rx_argus.h" |
| 41 | #include "runtime/rx_world.h" |
| 42 | |
| 43 | #include <stdio.h> |
| 44 | #include <stdlib.h> |
| 45 | #include <string.h> |
| 46 | #include <time.h> |
| 47 | |
| 48 | #define SUBJ 7u |
| 49 | #define RES 0x5810100ull |
| 50 | #define BENCH_RIGHTS 0x1u |
| 51 | |
| 52 | static uint64_t now_ns(void) { |
| 53 | struct timespec ts; |
| 54 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 55 | return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; |
| 56 | } |
| 57 | |
| 58 | static int cmp_u64(const void *a, const void *b) { |
| 59 | uint64_t x = *(const uint64_t *)a, y = *(const uint64_t *)b; |
| 60 | return x < y ? -1 : x > y; |
| 61 | } |
| 62 | |
| 63 | static uint64_t pct(uint64_t *v, size_t n, double p) { |
| 64 | qsort(v, n, sizeof *v, cmp_u64); |
| 65 | size_t i = (size_t)(p * (double)(n - 1)); |
| 66 | return v[i]; |
| 67 | } |
| 68 | |
| 69 | static RxWorld g_world; |
| 70 | |
| 71 | int main(int argc, char **argv) { |
| 72 | size_t n_ops = argc > 1 ? strtoull(argv[1], NULL, 10) : 2000000u; |
| 73 | size_t n_lat = argc > 2 ? strtoull(argv[2], NULL, 10) : 200000u; |
| 74 | |
| 75 | AienosCapAdmin *admin; |
| 76 | AienosCapView *view; |
| 77 | if (aienos_cap_start(&admin, &view) != 0) { fprintf(stderr, "authority start\n"); return 1; } |
| 78 | AienosCapRef office, cap; |
| 79 | aienos_cap_office(admin, &office); |
| 80 | AienosCapMint m = { RX_AEGIS_SUBJ, SUBJ, RES, BENCH_RIGHTS, 0, { UINT32_MAX, 0 }, office }; |
| 81 | if (aienos_cap_mint(admin, &m, &cap) != 0) { fprintf(stderr, "mint\n"); return 1; } |
| 82 | /* The bench is the harness that minted: tell ARGUS, so the shadow knows the grant. */ |
| 83 | RX_ARGUS_EMIT(rx_argus_emit_cap_granted(view, SUBJ, cap.cap_id, (uint64_t)cap.generation, |
| 84 | RES, BENCH_RIGHTS, 0)); |
| 85 | if (rx_world_init_native(&g_world, view, 1, 1u << 12) != RX_OK) { |
| 86 | fprintf(stderr, "world init\n"); |
| 87 | return 1; |
| 88 | } |
| 89 | RxAegisPolicy pol; |
| 90 | memset(&pol, 0, sizeof pol); |
| 91 | pol.n_rules = 1; |
| 92 | pol.rules[0] = (RxAegisRule){ 1, SUBJ, RES, RES + 0xff, BENCH_RIGHTS, 0, 0 }; |
| 93 | RxCapRef ref = { cap.cap_id, cap.generation }; |
| 94 | |
| 95 | volatile uint64_t sink = 0; |
| 96 | /* warm-up */ |
| 97 | for (size_t i = 0; i < 100000; i++) { |
| 98 | uint64_t l; uint32_t why, na; |
| 99 | sink += rx_aegis_evaluate(&pol, SUBJ, RES + (i & 7), BENCH_RIGHTS, 0, &l, &why, &na); |
| 100 | sink += (uint64_t)rx_world_validate_cap(&g_world, ref, SUBJ, RES, BENCH_RIGHTS, NULL); |
| 101 | } |
| 102 | |
| 103 | /* throughput */ |
| 104 | uint64_t t0 = now_ns(); |
| 105 | for (size_t i = 0; i < n_ops; i++) { |
| 106 | uint64_t l; uint32_t why, na; |
| 107 | sink += rx_aegis_evaluate(&pol, SUBJ, RES + (i & 7), BENCH_RIGHTS, 0, &l, &why, &na); |
| 108 | sink += (uint64_t)rx_world_validate_cap(&g_world, ref, SUBJ, RES, BENCH_RIGHTS, NULL); |
| 109 | } |
| 110 | uint64_t t1 = now_ns(); |
| 111 | double ops_s = (double)n_ops * 1e9 / (double)(t1 - t0); |
| 112 | |
| 113 | /* latency */ |
| 114 | uint64_t *lat = malloc(n_lat * sizeof *lat); |
| 115 | uint64_t *clk = malloc(n_lat * sizeof *clk); |
| 116 | for (size_t i = 0; i < n_lat; i++) { |
| 117 | uint64_t a = now_ns(); |
| 118 | uint64_t b = now_ns(); |
| 119 | clk[i] = b - a; |
| 120 | } |
| 121 | for (size_t i = 0; i < n_lat; i++) { |
| 122 | uint64_t l; uint32_t why, na; |
| 123 | uint64_t a = now_ns(); |
| 124 | sink += rx_aegis_evaluate(&pol, SUBJ, RES + (i & 7), BENCH_RIGHTS, 0, &l, &why, &na); |
| 125 | sink += (uint64_t)rx_world_validate_cap(&g_world, ref, SUBJ, RES, BENCH_RIGHTS, NULL); |
| 126 | lat[i] = now_ns() - a; |
| 127 | } |
| 128 | uint64_t lat50 = pct(lat, n_lat, 0.50), lat99 = pct(lat, n_lat, 0.99); |
| 129 | uint64_t clk50 = pct(clk, n_lat, 0.50); |
| 130 | |
| 131 | printf("{ \"rx_argus\": %d, \"ops\": %zu, \"ops_per_s\": %.0f, \"op_p50_ns\": %llu, " |
| 132 | "\"op_p99_ns\": %llu, \"clock_read_p50_ns\": %llu", |
| 133 | RX_ARGUS, n_ops, ops_s, (unsigned long long)lat50, (unsigned long long)lat99, |
| 134 | (unsigned long long)clk50); |
| 135 | |
| 136 | #if RX_ARGUS |
| 137 | RxArgusStats before; |
| 138 | rx_argus_stats(&before); |
| 139 | /* The hot-path ARGUS cost alone: use_begin + use_end for a successful |
| 140 | * validate (use-table update, flush every ARGUS_USE_FLUSH_OPS included). |
| 141 | * The clock quantizes at 16 ns, so each sample times a batch of 256 |
| 142 | * calls and reports the per-call mean of the batch. */ |
| 143 | enum { UB = 256 }; |
| 144 | size_t n_ub = n_lat / UB ? n_lat / UB : 1; |
| 145 | for (size_t i = 0; i < n_ub; i++) { |
| 146 | uint64_t a = now_ns(); |
| 147 | for (unsigned j = 0; j < UB; j++) { |
| 148 | uint64_t k = rx_argus_use_begin(); |
| 149 | rx_argus_use_end(k, view, SUBJ, cap.cap_id, (uint64_t)cap.generation, RES, 0); |
| 150 | } |
| 151 | lat[i] = now_ns() - a; |
| 152 | } |
| 153 | double ub50 = (double)pct(lat, n_ub, 0.50) / UB, ub99 = (double)pct(lat, n_ub, 0.99) / UB; |
| 154 | RxArgusStats s; |
| 155 | rx_argus_stats(&s); |
| 156 | printf(", \"use_update_p50_ns\": %.2f, \"use_update_p99_ns\": %.2f, \"bytes_per_event\": %u", |
| 157 | ub50, ub99, ARGUS_EVENT_SIZE); |
| 158 | printf(", \"consumer_mode\": %d, \"ops_phase_emitted\": %llu, \"ops_phase_pushed\": %llu, " |
| 159 | "\"ops_phase_refused\": %llu, \"ops_phase_malformed\": %llu, \"total_emitted\": %llu, " |
| 160 | "\"total_refused\": %llu, \"summaries\": %llu, \"summary_refused\": %llu", |
| 161 | s.consumer_mode, (unsigned long long)before.emitted, (unsigned long long)before.pushed, |
| 162 | (unsigned long long)before.ring_refused, (unsigned long long)before.ring_malformed, |
| 163 | (unsigned long long)s.emitted, (unsigned long long)s.ring_refused, |
| 164 | (unsigned long long)s.summaries_emitted, (unsigned long long)s.summary_refused); |
| 165 | rx_argus_shutdown(); |
| 166 | rx_argus_stats(&s); |
| 167 | printf(", \"received\": %llu, \"uses_counted\": %llu, \"lag_max\": %u, \"late\": %llu, " |
| 168 | "\"stall_breaks\": %llu, \"findings\": %llu, \"producers\": %u, \"producer_slot_bytes\": %zu, " |
| 169 | "\"core_bytes\": %zu", |
| 170 | (unsigned long long)s.received, (unsigned long long)s.uses_counted, s.lag_max, |
| 171 | (unsigned long long)s.late_events, (unsigned long long)s.stall_breaks, |
| 172 | (unsigned long long)s.findings_total, s.producers_claimed, s.producer_bytes, s.core_bytes); |
| 173 | #endif |
| 174 | printf(", \"sink\": %llu }\n", (unsigned long long)(sink & 1)); |
| 175 | rx_world_destroy(&g_world); |
| 176 | aienos_cap_stop(admin, view); |
| 177 | free(lat); |
| 178 | free(clk); |
| 179 | return 0; |
| 180 | } |