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 / src / runtime / rx_argus.h
| 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 | * rx_argus.h -- the live ARGUS producer inside the reaction runtime (ABI v1.1). |
| 19 | * |
| 20 | * ARGUS (aien-dev/aienux native/argus, ABI argus_abi.h) observes authority |
| 21 | * decisions; it never authorizes. The runtime copies each decision the |
| 22 | * authority or AEGIS already made into ARGUS events. Nothing here |
| 23 | * re-validates a capability. |
| 24 | * |
| 25 | * Compile-time mode, RX_ARGUS: |
| 26 | * 0 (default) no code at all: every hook expands to nothing and this |
| 27 | * header does not include argus_abi.h. |
| 28 | * 1 emit only: events are built and pushed, no consumer thread. |
| 29 | * 2 emit + consumer thread (ingest into an argus_core, or discard). |
| 30 | * |
| 31 | * v1.1 hot-path rules ("emit on transition, count on use"): |
| 32 | * - Each producer thread owns one producer slot (up to RX_ARGUS_PRODUCERS): |
| 33 | * its own SPSC ring (RX_ARGUS_RING_CAPACITY), its own stream id (flag |
| 34 | * bits 2-15) and its own per-stream sequence. No push lock. |
| 35 | * - A successful validate updates the thread's 256-slot use table |
| 36 | * {count, min gen, max gen, principal} indexed by cap_id: a few stores, |
| 37 | * no lock, no clock, no ring access. The table is flushed as |
| 38 | * CAPABILITY_USE_SUMMARY events every ARGUS_USE_FLUSH_OPS uses, on the |
| 39 | * consumer's periodic flush request (next use after it), when the worker |
| 40 | * goes idle, at thread exit and at shutdown. A refused flush keeps the |
| 41 | * counts (the next summary carries the total). |
| 42 | * - Full events only for GRANTED/DENIED/REVOKED/WORLD_COMMITTED/JOINED and |
| 43 | * failed validates. The authority tick is read only for the transitions; |
| 44 | * a failed validate is a full USED/DENIED keyed like a use (tick 0, no |
| 45 | * stamp), so denials never force other threads to flush. |
| 46 | * - A thread flushes its whole table before any transition it emits |
| 47 | * (398cfb9: flush before GRANTED/REVOKED); for transitions announced on |
| 48 | * another thread the consumer's ordered merge gives the same order. |
| 49 | * |
| 50 | * Cross-ring order. One ring per thread loses the global push order that the |
| 51 | * old push lock gave. The consumer restores a causal order with one shared |
| 52 | * counter (the "stamp") that only transitions advance: |
| 53 | * - every transition takes a stamp s (atomic fetch_add) and gets order key |
| 54 | * 2s+1; the GRANTED stamp is taken before the new reference can reach |
| 55 | * anyone, the REVOKED stamp after the revoke took effect; |
| 56 | * - a use reads the stamp e BEFORE the authority validate (rx_argus_use_begin) |
| 57 | * and is keyed 2e: "after every transition stamped < e, before every |
| 58 | * transition stamped >= e"; a thread flushes its table whenever e changes, |
| 59 | * so one summary covers one stamp interval; |
| 60 | * - each thread publishes a lower bound of the keys it may still push; the |
| 61 | * consumer merges the rings by key and only ingests an event when no |
| 62 | * producer can still push a smaller key. A thread publishes "idle" when it |
| 63 | * parks (rx_argus_idle), after a transition and at exit. |
| 64 | * The order key travels in a side array next to each ring, not in the event. |
| 65 | */ |
| 66 | #ifndef RX_ARGUS_H |
| 67 | #define RX_ARGUS_H |
| 68 | |
| 69 | #ifndef RX_ARGUS |
| 70 | #define RX_ARGUS 0 |
| 71 | #endif |
| 72 | |
| 73 | #if RX_ARGUS |
| 74 | |
| 75 | #include "aienos_cap.h" |
| 76 | #include "argus_abi.h" |
| 77 | |
| 78 | #include <stdatomic.h> |
| 79 | #include <stddef.h> |
| 80 | #include <stdint.h> |
| 81 | |
| 82 | #define RX_ARGUS_PRODUCERS 32u |
| 83 | #define RX_ARGUS_RING_CAPACITY 1024u |
| 84 | #define RX_ARGUS_KEYS (2u * RX_ARGUS_RING_CAPACITY) |
| 85 | #define RX_ARGUS_USE_SLOTS 256u /* == ARGUS_CAP_MAX: indexed by cap_id */ |
| 86 | #define RX_ARGUS_FINDINGS_MAX 1024u |
| 87 | #define RX_ARGUS_FLUSH_NS 10000000ull /* consumer flush request period (10 ms) */ |
| 88 | #define RX_ARGUS_STALL_NS 2000000ull /* consumer stops waiting for a silent producer */ |
| 89 | #define RX_ARGUS_IDLE UINT64_MAX |
| 90 | |
| 91 | enum { RX_ARGUS_CONSUMER_OFF = 0, RX_ARGUS_CONSUMER_DISCARD = 1, RX_ARGUS_CONSUMER_INGEST = 2 }; |
| 92 | |
| 93 | typedef struct { |
| 94 | uint64_t count; |
| 95 | uint64_t min_gen, max_gen; |
| 96 | uint32_t principal; |
| 97 | uint32_t pad; |
| 98 | } RxArgusUse; |
| 99 | |
| 100 | typedef struct RxArgusProducer { |
| 101 | /* owner-only hot line(s) */ |
| 102 | uint64_t epoch; /* stamp that keys the pending uses; 0 = idle (next use re-publishes) */ |
| 103 | uint64_t ops, flush_at; /* uses since start; next flush threshold */ |
| 104 | RxArgusUse use[RX_ARGUS_USE_SLOTS]; |
| 105 | /* consumer -> producer: periodic flush request (own line) */ |
| 106 | _Alignas(128) _Atomic uint32_t flush_req; |
| 107 | /* producer -> consumer: lower bound of the stamp of anything still to come */ |
| 108 | _Alignas(128) _Atomic uint64_t pub; |
| 109 | } RxArgusProducer; |
| 110 | |
| 111 | extern _Thread_local RxArgusProducer *rx_argus_tls; |
| 112 | extern _Atomic uint64_t rx_argus_stamp; |
| 113 | |
| 114 | uint64_t rx_argus_use_begin_slow(uint64_t e); |
| 115 | void rx_argus_use_slow(uint64_t key, const AienosCapView *view, uint32_t subject, uint32_t cap_id, |
| 116 | uint64_t cap_generation, uint64_t resource, int code); |
| 117 | void rx_argus_flush_self(void); |
| 118 | |
| 119 | /* Before the authority validate: returns the use's order key. */ |
| 120 | static inline uint64_t rx_argus_use_begin(void) { |
| 121 | RxArgusProducer *t = rx_argus_tls; |
| 122 | uint64_t e = atomic_load_explicit(&rx_argus_stamp, memory_order_acquire); |
| 123 | if (__builtin_expect(!t || t->epoch != e, 0)) return rx_argus_use_begin_slow(e); |
| 124 | return e; |
| 125 | } |
| 126 | |
| 127 | /* After the authority validate: count a success, emit a failure. */ |
| 128 | static inline void rx_argus_use_end(uint64_t key, const AienosCapView *view, uint32_t subject, |
| 129 | uint32_t cap_id, uint64_t gen, uint64_t resource, int code) { |
| 130 | RxArgusProducer *t = rx_argus_tls; |
| 131 | if (__builtin_expect(code != 0 || cap_id >= RX_ARGUS_USE_SLOTS || !t || key != t->epoch, 0)) { |
| 132 | rx_argus_use_slow(key, view, subject, cap_id, gen, resource, code); |
| 133 | return; |
| 134 | } |
| 135 | RxArgusUse *u = &t->use[cap_id]; |
| 136 | if (__builtin_expect(u->principal != subject && u->count != 0, 0)) { |
| 137 | rx_argus_use_slow(key, view, subject, cap_id, gen, resource, code); |
| 138 | return; |
| 139 | } |
| 140 | u->principal = subject; |
| 141 | u->count++; |
| 142 | u->min_gen = gen < u->min_gen ? gen : u->min_gen; |
| 143 | u->max_gen = gen > u->max_gen ? gen : u->max_gen; |
| 144 | if (__builtin_expect(++t->ops >= t->flush_at || |
| 145 | atomic_load_explicit(&t->flush_req, memory_order_relaxed), 0)) |
| 146 | rx_argus_flush_self(); |
| 147 | } |
| 148 | |
| 149 | typedef struct { |
| 150 | /* producer side */ |
| 151 | uint64_t emitted, pushed, ring_refused, ring_malformed; |
| 152 | uint64_t emitted_by_kind[ARGUS_EV_KIND_MAX + 1]; |
| 153 | uint64_t uses_counted; /* successful validates folded into the use tables */ |
| 154 | uint64_t summaries_emitted; /* summary pushes attempted */ |
| 155 | uint64_t summary_refused; /* summary pushes refused (counts kept, retried) */ |
| 156 | uint64_t unregistered_uses; /* uses by a thread that found no free producer slot */ |
| 157 | uint64_t order_slow; /* use keyed off its thread's epoch (should be 0) */ |
| 158 | uint64_t mixed_keys; /* refused leftovers carried across a stamp change */ |
| 159 | uint32_t producers_claimed, producers_max_live; |
| 160 | /* consumer side */ |
| 161 | uint64_t received, synthesized, batches, stream_records, ingest_errors; |
| 162 | uint64_t received_by_kind[ARGUS_EV_KIND_MAX + 1]; |
| 163 | uint64_t findings_total, findings_by_code[ARGUS_F_MAX + 1], findings_kept; |
| 164 | uint64_t late_events; /* ingested with a key below one already ingested */ |
| 165 | uint64_t stall_breaks; /* consumer stopped waiting for a silent producer */ |
| 166 | uint64_t hold_ns_max; /* longest time the head event waited for the merge */ |
| 167 | uint32_t lag_max; /* deepest ring depth seen by the consumer */ |
| 168 | uint64_t authority_obs[9]; /* observer calls by AIENOS_CAP_OBS_* op */ |
| 169 | int consumer_mode; |
| 170 | size_t ring_bytes, core_bytes, findings_bytes, producer_bytes; |
| 171 | ArgusCoreHealth core; /* zero unless INGEST */ |
| 172 | } RxArgusStats; |
| 173 | |
| 174 | int rx_argus_active(void); |
| 175 | void rx_argus_shutdown(void); |
| 176 | void rx_argus_stats(RxArgusStats *out); |
| 177 | size_t rx_argus_findings(ArgusFinding *out, size_t max); |
| 178 | int rx_argus_write_summary(const char *path, const char *suite); |
| 179 | |
| 180 | /* Transitions. view may be NULL (tick 0). */ |
| 181 | void rx_argus_emit_cap_granted(const AienosCapView *view, uint32_t subject, uint32_t cap_id, |
| 182 | uint64_t cap_generation, uint64_t resource, uint32_t rights, |
| 183 | int code); |
| 184 | /* A policy or root refusal; cap_id ARGUS_CAP_NONE when no reference exists. */ |
| 185 | void rx_argus_emit_cap_denied(const AienosCapView *view, uint32_t subject, uint32_t cap_id, |
| 186 | uint64_t cap_generation, uint64_t resource, uint32_t rights, |
| 187 | int code); |
| 188 | void rx_argus_emit_cap_revoked(const AienosCapView *view, uint32_t subject, uint32_t cap_id, |
| 189 | uint64_t cap_generation, int code); |
| 190 | /* store_id: the RxGenStore identity (hash of its directory). */ |
| 191 | void rx_argus_emit_world_committed(uint32_t store_id, uint32_t subject, uint32_t cap_id, |
| 192 | uint64_t cap_generation, uint64_t world_generation, |
| 193 | const uint8_t digest[32], int code); |
| 194 | /* The worker is about to park: flush, publish idle. */ |
| 195 | void rx_argus_idle(void); |
| 196 | /* Non-zero when rx_argus_idle() has work on this thread (pending uses, a refused |
| 197 | * leftover, or a published bound to clear). A caller parking under its own lock |
| 198 | * drops that lock only when this says a flush is due (rx_world.c worker_main). */ |
| 199 | int rx_argus_idle_pending(void); |
| 200 | /* Stable u32 identity for a store directory (never 0). */ |
| 201 | uint32_t rx_argus_store_id(const char *dir); |
| 202 | |
| 203 | #define RX_ARGUS_EMIT(call) call |
| 204 | #define RX_ARGUS_USE_BEGIN() rx_argus_use_begin() |
| 205 | #define RX_ARGUS_USE_END(key, view, subj, cap, gen, res, code) \ |
| 206 | rx_argus_use_end((key), (view), (subj), (cap), (gen), (res), (code)) |
| 207 | /* Mint/revoke emits at the AEGIS root. Compiled out when the authority itself |
| 208 | * announces them (RX_ARGUS_AUTHORITY_OBSERVER) or the test-only link wrap does. */ |
| 209 | #if defined(RX_ARGUS_AUTHORITY_WRAP) || defined(RX_ARGUS_AUTHORITY_OBSERVER) |
| 210 | #define RX_ARGUS_EMIT_AUTH(call) ((void)0) |
| 211 | #else |
| 212 | #define RX_ARGUS_EMIT_AUTH(call) call |
| 213 | #endif |
| 214 | |
| 215 | #else /* RX_ARGUS == 0 */ |
| 216 | |
| 217 | #define RX_ARGUS_EMIT(call) ((void)0) |
| 218 | #define RX_ARGUS_EMIT_AUTH(call) ((void)0) |
| 219 | #define RX_ARGUS_USE_BEGIN() 0 |
| 220 | #define RX_ARGUS_USE_END(key, view, subj, cap, gen, res, code) ((void)(key)) |
| 221 | |
| 222 | #endif |
| 223 | #endif /* RX_ARGUS_H */ |