Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/argus/argus_ring.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / argus / argus_ring.c` 251 lines ยท 11685 bytes Raw Blame History 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_ring.c -- ARGUS single-producer / single-consumer bounded event ring. 19 * 20 * MALFORMED PUSHES 21 * argus_ring_push returns ARGUS_ERR_MALFORMED, stores nothing and adds 1 to 22 * refused[0] (ArgusRingStats: index 0 = malformed pushes, indices 1..4 = 23 * capacity refusals per class) when class_ is outside 1..ARGUS_CLASS_MAX or 24 * is weaker than argus_event_min_class(kind). A kind the ring does not know 25 * (min class 0) has no floor here: the ring is transport, the core runs the 26 * full argus_event_validate. v1.2 (spec section 3): kind 90 CONTAINMENT_PROPOSED is 27 * always refused MALFORMED here; only the consumer synthesizes proposals and ingests 28 * them directly. Kinds 91-93 are CRITICAL, so a full ring counts them in the sticky 29 * critical_overflow (never dropped silently), like every CRITICAL kind. 30 * Malformed pushes are not telemetry loss: they are 31 * never drained as TELEMETRY_DROPPED. Conservation still holds: 32 * attempts = accepted + refused[0..4] + critical_overflow. 33 * 34 * One producer thread calls argus_ring_push. One consumer thread calls 35 * argus_ring_pop, argus_ring_pop_batch and argus_ring_drain_drops. 36 * argus_ring_stats may be called from either. The ring lives entirely in 37 * caller-provided memory (argus_ring_footprint bytes, 64-byte aligned); it 38 * never allocates, never locks, never makes a system call, and never blocks. 39 * Synchronisation is two C11 atomic indices (head written only by the 40 * producer with release, tail written only by the consumer with release), 41 * each on its own cache line, plus per-class atomic refusal counters. 42 * 43 * WHAT HAPPENS WHEN THE CONSUMER FALLS BEHIND 44 * Push never waits. As the ring fills, lower classes are refused first so 45 * that room is always kept for the events that matter most: 46 * INFORMATIONAL refused once depth >= 50% of capacity 47 * AUDIT refused once depth >= 75% 48 * SECURITY refused once depth >= 90% (rounded up to a whole slot) 49 * CRITICAL may use 100%; refused only when the ring is truly full 50 * A refused push returns ARGUS_ERR_FULL and the event is NOT stored; the 51 * producer keeps running. Nothing is ever lost silently: 52 * - a refused SECURITY/AUDIT/INFORMATIONAL event adds 1 to refused[class] 53 * (cumulative, in argus_ring_stats) and to a pending per-class count; 54 * - a refused CRITICAL event adds 1 to critical_overflow (cumulative and 55 * sticky: it is never reset, it marks a permanent evidence gap) and to a 56 * pending critical count. refused[CRITICAL] therefore always stays 0, 57 * and for every attempt: attempts = accepted + sum(refused) + critical_overflow. 58 * The consumer calls argus_ring_drain_drops to turn the pending counts into 59 * ARGUS_EV_TELEMETRY_DROPPED events (flag ARGUS_FLAG_CONSUMER, 60 * object_id = the class that lost events, resource = how many, outcome 61 * ERROR, code ARGUS_ERR_FULL, cap_id ARGUS_CAP_NONE, stream 0), one per class with a nonzero pending count, 62 * CRITICAL first. It resets exactly the pending counts it reported 63 * (atomic exchange, so refusals racing with the drain are never lost; they 64 * show up in the next drain). Every synthesized event has class CRITICAL, 65 * the minimum class for TELEMETRY_DROPPED (argus_event_min_class): an 66 * evidence gap is always critical telemetry; the lost class is object_id. 67 * The synthesized events are handed to the consumer directly; they do not 68 * go through the ring, so reporting a loss can never itself be refused. 69 * Accepted events are delivered in exact push order (FIFO overall, hence 70 * also FIFO within each class). 71 */ 72 #include "argus_abi.h" 73 #include 74 75 #define ARGUS_RING_MIN_CAPACITY 4u 76 #define ARGUS_RING_MAX_CAPACITY (1u << 30) 77 #define ARGUS_RING_ALIGN 64u 78 79 struct ArgusRing { 80 /* producer cache line */ 81 _Alignas(64) _Atomic uint64_t head; /* next slot to write == events accepted */ 82 uint64_t tail_cache; /* producer's last view of tail */ 83 /* consumer cache line */ 84 _Alignas(64) _Atomic uint64_t tail; /* next slot to read == events popped */ 85 uint64_t head_cache; /* consumer's last view of head */ 86 /* counters (producer increments, consumer exchanges pending) */ 87 _Alignas(64) _Atomic uint64_t refused[ARGUS_CLASS_MAX + 1]; 88 _Atomic uint64_t critical_overflow; 89 _Atomic uint64_t pending[ARGUS_CLASS_MAX + 1]; /* index CRITICAL = pending overflow */ 90 /* immutable after init */ 91 _Alignas(64) uint32_t capacity, mask; 92 uint32_t limit[ARGUS_CLASS_MAX + 1]; /* push accepted iff depth < limit[class] */ 93 _Alignas(64) ArgusEvent slots[]; 94 }; 95 96 /* Depth at which a class starts being refused (see top of file). */ 97 static uint32_t class_limit(uint32_t cap, unsigned cls) 98 { 99 uint64_t c = cap; 100 switch (cls) { 101 case ARGUS_CLASS_CRITICAL: return cap; 102 case ARGUS_CLASS_SECURITY: return (uint32_t)((c * 9u + 9u) / 10u); /* ceil(0.9 cap) */ 103 case ARGUS_CLASS_AUDIT: return (uint32_t)((c * 3u + 3u) / 4u); /* ceil(0.75 cap) */ 104 case ARGUS_CLASS_INFORMATIONAL: return (uint32_t)((c + 1u) / 2u); /* ceil(0.5 cap) */ 105 default: return 0; 106 } 107 } 108 109 static int capacity_ok(uint32_t cap) 110 { 111 return cap >= ARGUS_RING_MIN_CAPACITY && cap <= ARGUS_RING_MAX_CAPACITY && (cap & (cap - 1u)) == 0; 112 } 113 114 /* The ring depth at which pushes of `class_` start being refused, 0 if invalid. */ 115 uint32_t argus_ring_saturation_point(uint32_t capacity_pow2, uint8_t class_) 116 { 117 if (!capacity_ok(capacity_pow2)) return 0; 118 return class_limit(capacity_pow2, class_); 119 } 120 121 size_t argus_ring_footprint(uint32_t capacity_pow2) 122 { 123 if (!capacity_ok(capacity_pow2)) return 0; 124 size_t slots = (size_t)capacity_pow2; 125 if (slots > (SIZE_MAX - sizeof(struct ArgusRing)) / sizeof(ArgusEvent)) return 0; 126 return sizeof(struct ArgusRing) + slots * sizeof(ArgusEvent); 127 } 128 129 int argus_ring_init(ArgusRing **ring, void *memory, size_t bytes, uint32_t capacity_pow2) 130 { 131 if (!ring || !memory) return ARGUS_ERR_ARG; 132 size_t need = argus_ring_footprint(capacity_pow2); 133 if (need == 0 || bytes < need) return ARGUS_ERR_ARG; 134 if (((uintptr_t)memory & (ARGUS_RING_ALIGN - 1u)) != 0) return ARGUS_ERR_ARG; 135 struct ArgusRing *r = (struct ArgusRing *)memory; 136 atomic_init(&r->head, 0); 137 atomic_init(&r->tail, 0); 138 r->tail_cache = 0; 139 r->head_cache = 0; 140 for (unsigned c = 0; c <= ARGUS_CLASS_MAX; c++) { 141 atomic_init(&r->refused[c], 0); 142 atomic_init(&r->pending[c], 0); 143 r->limit[c] = class_limit(capacity_pow2, c); 144 } 145 atomic_init(&r->critical_overflow, 0); 146 r->capacity = capacity_pow2; 147 r->mask = capacity_pow2 - 1u; 148 *ring = r; 149 return ARGUS_OK; 150 } 151 152 static void bump(_Atomic uint64_t *a) { atomic_fetch_add_explicit(a, 1, memory_order_relaxed); } 153 154 int argus_ring_push(ArgusRing *r, const ArgusEvent *ev) 155 { 156 if (!r || !ev) return ARGUS_ERR_ARG; 157 unsigned cls = ev->class_; 158 unsigned min = argus_event_min_class(ev->kind); 159 if (cls < ARGUS_CLASS_CRITICAL || cls > ARGUS_CLASS_MAX || (min != 0 && cls > min) || 160 ev->kind == ARGUS_EV_CONTAINMENT_PROPOSED) { /* v1.2: only the consumer makes 90 */ 161 bump(&r->refused[0]); /* index 0 = malformed (see top of file); not a drop, never drained */ 162 return ARGUS_ERR_MALFORMED; 163 } 164 uint64_t h = atomic_load_explicit(&r->head, memory_order_relaxed); 165 uint64_t limit = r->limit[cls]; 166 if (h - r->tail_cache >= limit) { 167 /* Cached view says too full: refresh from the consumer before refusing. */ 168 r->tail_cache = atomic_load_explicit(&r->tail, memory_order_acquire); 169 if (h - r->tail_cache >= limit) { 170 if (cls == ARGUS_CLASS_CRITICAL) bump(&r->critical_overflow); 171 else bump(&r->refused[cls]); 172 bump(&r->pending[cls]); 173 return ARGUS_ERR_FULL; 174 } 175 } 176 r->slots[h & r->mask] = *ev; 177 atomic_store_explicit(&r->head, h + 1, memory_order_release); 178 return ARGUS_OK; 179 } 180 181 size_t argus_ring_pop_batch(ArgusRing *r, ArgusEvent *out, size_t max) 182 { 183 if (!r || !out || max == 0) return 0; 184 uint64_t t = atomic_load_explicit(&r->tail, memory_order_relaxed); 185 uint64_t avail = r->head_cache - t; 186 if (avail < max) { 187 r->head_cache = atomic_load_explicit(&r->head, memory_order_acquire); 188 avail = r->head_cache - t; 189 } 190 if (avail == 0) return 0; 191 size_t n = avail < max ? (size_t)avail : max; 192 for (size_t i = 0; i < n; i++) out[i] = r->slots[(t + i) & r->mask]; 193 atomic_store_explicit(&r->tail, t + n, memory_order_release); 194 return n; 195 } 196 197 int argus_ring_pop(ArgusRing *r, ArgusEvent *out) 198 { 199 if (!r || !out) return ARGUS_ERR_ARG; 200 return argus_ring_pop_batch(r, out, 1) == 1 ? ARGUS_OK : ARGUS_ERR_STATE; 201 } 202 203 void argus_ring_stats(const ArgusRing *ring, ArgusRingStats *out) 204 { 205 if (!out) return; 206 struct ArgusRing *r = (struct ArgusRing *)ring; /* atomic loads need non-const in C11 */ 207 for (unsigned c = 0; c <= ARGUS_CLASS_MAX; c++) out->refused[c] = 0; 208 out->pushed = out->popped = out->critical_overflow = 0; 209 out->depth = out->capacity = 0; 210 if (!r) return; 211 uint64_t t = atomic_load_explicit(&r->tail, memory_order_acquire); 212 uint64_t h = atomic_load_explicit(&r->head, memory_order_acquire); 213 out->pushed = h; 214 out->popped = t; 215 for (unsigned c = 0; c <= ARGUS_CLASS_MAX; c++) 216 out->refused[c] = atomic_load_explicit(&r->refused[c], memory_order_relaxed); 217 out->critical_overflow = atomic_load_explicit(&r->critical_overflow, memory_order_relaxed); 218 uint64_t d = h >= t ? h - t : 0; 219 out->depth = (uint32_t)(d > r->capacity ? r->capacity : d); 220 out->capacity = r->capacity; 221 } 222 223 size_t argus_ring_drain_drops(ArgusRing *r, ArgusEvent *out, size_t max, uint64_t *next_sequence) 224 { 225 static const uint8_t order[ARGUS_CLASS_MAX] = { 226 ARGUS_CLASS_CRITICAL, ARGUS_CLASS_SECURITY, ARGUS_CLASS_AUDIT, ARGUS_CLASS_INFORMATIONAL 227 }; 228 if (!r || !out || !next_sequence) return 0; 229 size_t n = 0; 230 for (unsigned i = 0; i < ARGUS_CLASS_MAX && n < max; i++) { 231 unsigned cls = order[i]; 232 if (atomic_load_explicit(&r->pending[cls], memory_order_relaxed) == 0) continue; 233 uint64_t count = atomic_exchange_explicit(&r->pending[cls], 0, memory_order_relaxed); 234 if (count == 0) continue; 235 if (*next_sequence == 0) *next_sequence = 1; 236 ArgusEvent *e = &out[n++]; 237 *e = (ArgusEvent){0}; 238 e->version = ARGUS_ABI_VERSION; 239 e->class_ = ARGUS_CLASS_CRITICAL; 240 e->kind = ARGUS_EV_TELEMETRY_DROPPED; 241 e->effect_class = ARGUS_EFFECT_NONE; 242 e->outcome = ARGUS_OUTCOME_ERROR; 243 e->flags = ARGUS_FLAG_CONSUMER; 244 e->sequence = (*next_sequence)++; 245 e->code = ARGUS_ERR_FULL; 246 e->cap_id = ARGUS_CAP_NONE; /* v1.1: cap_id 0 is a real slot */ 247 e->object_id = cls; 248 e->resource = count; 249 } 250 return n; 251 }