aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / argus / tests / test_argus_ring.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 | * test_argus_ring.c -- ARGUS lane B: SPSC ring tests. |
| 19 | * init/argument errors, watermark (saturation) points per class, drop |
| 20 | * reporting, and a two-thread run with a deliberately slow consumer: |
| 21 | * attempts == popped + sum(refused) + critical_overflow, FIFO per class, |
| 22 | * and every refusal reported exactly once by argus_ring_drain_drops. |
| 23 | * Usage: test_argus_ring [events] (default 10,000,000; must exceed 1024) |
| 24 | * The SPSC run starts with a held consumer, so refusal/backpressure is guaranteed |
| 25 | * by construction (not by thread timing) in every build, sanitized or not. |
| 26 | */ |
| 27 | #include "argus_abi.h" |
| 28 | #include <pthread.h> |
| 29 | #include <stdatomic.h> |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | |
| 34 | /* The ring enforces only the class floor of argus_event_min_class(kind). No v1 kind |
| 35 | * may be INFORMATIONAL, so the INFORMATIONAL watermark is exercised with a kind |
| 36 | * value that is not in v1 (no floor at the ring; the core would reject it). */ |
| 37 | #define NOFLOOR_KIND 0x7FFFu |
| 38 | static uint16_t kind_for(uint8_t cls) |
| 39 | { |
| 40 | return cls == 1 ? ARGUS_EV_FORGED_CAPABILITY : cls == 2 ? ARGUS_EV_CAPABILITY_DENIED |
| 41 | : cls == 3 ? ARGUS_EV_CAPABILITY_USED : NOFLOOR_KIND; |
| 42 | } |
| 43 | |
| 44 | static int failures; |
| 45 | #define CHECK(cond) do { if (!(cond)) { failures++; fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); } } while (0) |
| 46 | |
| 47 | static ArgusEvent mk(uint8_t cls, uint64_t seq, uint64_t resource) |
| 48 | { |
| 49 | ArgusEvent e; |
| 50 | memset(&e, 0, sizeof e); |
| 51 | e.version = ARGUS_ABI_VERSION; |
| 52 | e.class_ = cls; |
| 53 | e.kind = kind_for(cls); |
| 54 | e.outcome = ARGUS_OUTCOME_OK; |
| 55 | e.flags = ARGUS_FLAG_SYNTHETIC; |
| 56 | e.sequence = seq; |
| 57 | e.resource = resource; |
| 58 | return e; |
| 59 | } |
| 60 | |
| 61 | static ArgusRing *new_ring(uint32_t cap, void **mem_out) |
| 62 | { |
| 63 | size_t fp = argus_ring_footprint(cap); |
| 64 | void *mem = NULL; |
| 65 | if (fp == 0 || posix_memalign(&mem, 64, fp) != 0) { fprintf(stderr, "alloc failed\n"); exit(2); } |
| 66 | ArgusRing *r = NULL; |
| 67 | if (argus_ring_init(&r, mem, fp, cap) != ARGUS_OK) { fprintf(stderr, "init failed\n"); exit(2); } |
| 68 | *mem_out = mem; |
| 69 | return r; |
| 70 | } |
| 71 | |
| 72 | static void test_init(void) |
| 73 | { |
| 74 | CHECK(argus_ring_footprint(0) == 0); |
| 75 | CHECK(argus_ring_footprint(2) == 0); |
| 76 | CHECK(argus_ring_footprint(12) == 0); |
| 77 | CHECK(argus_ring_footprint(4) >= 4 * sizeof(ArgusEvent)); |
| 78 | CHECK(argus_ring_footprint(1024) - argus_ring_footprint(512) == 512 * sizeof(ArgusEvent)); |
| 79 | size_t fp = argus_ring_footprint(16); |
| 80 | void *mem = NULL; |
| 81 | CHECK(posix_memalign(&mem, 64, fp + 64) == 0); |
| 82 | ArgusRing *r = NULL; |
| 83 | CHECK(argus_ring_init(NULL, mem, fp, 16) == ARGUS_ERR_ARG); |
| 84 | CHECK(argus_ring_init(&r, NULL, fp, 16) == ARGUS_ERR_ARG); |
| 85 | CHECK(argus_ring_init(&r, mem, fp - 1, 16) == ARGUS_ERR_ARG); |
| 86 | CHECK(argus_ring_init(&r, mem, fp, 15) == ARGUS_ERR_ARG); |
| 87 | CHECK(argus_ring_init(&r, (char *)mem + 8, fp, 16) == ARGUS_ERR_ARG); |
| 88 | CHECK(argus_ring_init(&r, mem, fp, 16) == ARGUS_OK); |
| 89 | ArgusEvent e = mk(ARGUS_CLASS_AUDIT, 1, 0), o; |
| 90 | CHECK(argus_ring_pop(r, &o) == ARGUS_ERR_STATE); |
| 91 | CHECK(argus_ring_pop_batch(r, &o, 1) == 0); |
| 92 | CHECK(argus_ring_push(NULL, &e) == ARGUS_ERR_ARG); |
| 93 | CHECK(argus_ring_push(r, NULL) == ARGUS_ERR_ARG); |
| 94 | e.class_ = 0; CHECK(argus_ring_push(r, &e) == ARGUS_ERR_MALFORMED); |
| 95 | e.class_ = 5; CHECK(argus_ring_push(r, &e) == ARGUS_ERR_MALFORMED); |
| 96 | e.class_ = ARGUS_CLASS_AUDIT; |
| 97 | CHECK(argus_ring_push(r, &e) == ARGUS_OK); |
| 98 | CHECK(argus_ring_pop(r, &o) == ARGUS_OK && memcmp(&o, &e, sizeof o) == 0); |
| 99 | ArgusRingStats s; |
| 100 | argus_ring_stats(r, &s); |
| 101 | CHECK(s.pushed == 1 && s.popped == 1 && s.depth == 0 && s.capacity == 16); |
| 102 | CHECK(s.refused[0] == 2 && s.refused[1] == 0 && s.refused[2] == 0 && s.refused[3] == 0 && s.refused[4] == 0); |
| 103 | free(mem); |
| 104 | printf("init/args: OK\n"); |
| 105 | } |
| 106 | |
| 107 | /* Class weaker than the kind floor: MALFORMED, stored nowhere, counted in refused[0] |
| 108 | * only (not a drop, never drained). Every kind value x every class byte. */ |
| 109 | static void test_class_floor(void) |
| 110 | { |
| 111 | void *mem; ArgusRing *r = new_ring(1024, &mem); |
| 112 | uint64_t attempts = 0, ok = 0, bad = 0; |
| 113 | for (unsigned k = 0; k < 65536; k++) { |
| 114 | unsigned fl = argus_event_min_class((uint16_t)k); |
| 115 | for (unsigned c = 0; c < 256; c++) { |
| 116 | ArgusEvent e = mk((uint8_t)c, 1, 0), o; |
| 117 | e.kind = (uint16_t)k; |
| 118 | int want = (c >= 1 && c <= 4 && (fl == 0 || c <= fl) && k != 90) ? ARGUS_OK : ARGUS_ERR_MALFORMED; /* v1.2: 90 refused */ |
| 119 | int rc = argus_ring_push(r, &e); |
| 120 | CHECK(rc == want); |
| 121 | attempts++; |
| 122 | if (rc == ARGUS_OK) { ok++; CHECK(argus_ring_pop(r, &o) == ARGUS_OK); } else bad++; |
| 123 | } |
| 124 | } |
| 125 | ArgusRingStats s; |
| 126 | argus_ring_stats(r, &s); |
| 127 | CHECK(s.refused[0] == bad && s.pushed == ok && s.popped == ok); |
| 128 | CHECK(s.refused[1] + s.refused[2] + s.refused[3] + s.refused[4] == 0 && s.critical_overflow == 0); |
| 129 | CHECK(attempts == s.pushed + s.refused[0]); |
| 130 | ArgusEvent d[8]; uint64_t seq = 1; |
| 131 | CHECK(argus_ring_drain_drops(r, d, 8, &seq) == 0); /* malformed is not telemetry loss */ |
| 132 | /* The hostile cases by name. */ |
| 133 | ArgusEvent e = mk(ARGUS_CLASS_INFORMATIONAL, 1, 0); e.kind = ARGUS_EV_CAPABILITY_REVOKED; |
| 134 | CHECK(argus_ring_push(r, &e) == ARGUS_ERR_MALFORMED); |
| 135 | e.class_ = ARGUS_CLASS_SECURITY; CHECK(argus_ring_push(r, &e) == ARGUS_ERR_MALFORMED); |
| 136 | e.class_ = ARGUS_CLASS_CRITICAL; CHECK(argus_ring_push(r, &e) == ARGUS_OK); |
| 137 | e.kind = ARGUS_EV_PROVIDER_USED; e.class_ = ARGUS_CLASS_INFORMATIONAL; CHECK(argus_ring_push(r, &e) == ARGUS_ERR_MALFORMED); |
| 138 | e.class_ = ARGUS_CLASS_AUDIT; CHECK(argus_ring_push(r, &e) == ARGUS_OK); |
| 139 | /* v1.2 (ARGUS1_SPEC 3): kind 90 is refused at every class, CONSUMER flag or not; 91-93 are |
| 140 | * CRITICAL-floor transport. None of this is telemetry loss. */ |
| 141 | { |
| 142 | ArgusRingStats s0, s1; argus_ring_stats(r, &s0); |
| 143 | ArgusEvent p = mk(ARGUS_CLASS_CRITICAL, 1, 0), o; |
| 144 | while (argus_ring_pop(r, &o) == ARGUS_OK) { } /* the named cases above left events queued */ p.kind = ARGUS_EV_CONTAINMENT_PROPOSED; |
| 145 | CHECK(argus_ring_push(r, &p) == ARGUS_ERR_MALFORMED); |
| 146 | p.flags = ARGUS_FLAG_CONSUMER; CHECK(argus_ring_push(r, &p) == ARGUS_ERR_MALFORMED); |
| 147 | argus_ring_stats(r, &s1); |
| 148 | CHECK(s1.refused[0] == s0.refused[0] + 2 && s1.pushed == s0.pushed); |
| 149 | for (uint16_t k = ARGUS_EV_CONTAINMENT_DECIDED; k <= ARGUS_EV_AUTHORITY_ESCALATED; k++) { |
| 150 | ArgusEvent q = mk(ARGUS_CLASS_CRITICAL, 1, 0); q.kind = k; |
| 151 | CHECK(argus_ring_push(r, &q) == ARGUS_OK && argus_ring_pop(r, &o) == ARGUS_OK && o.kind == k); |
| 152 | q.class_ = ARGUS_CLASS_SECURITY; CHECK(argus_ring_push(r, &q) == ARGUS_ERR_MALFORMED); |
| 153 | } |
| 154 | CHECK(argus_ring_drain_drops(r, d, 8, &seq) == 0); |
| 155 | } |
| 156 | { /* a full ring never drops 91-93 silently: sticky critical_overflow */ |
| 157 | void *m2; ArgusRing *r2 = new_ring(16, &m2); |
| 158 | ArgusEvent q = mk(ARGUS_CLASS_CRITICAL, 1, 0); q.kind = ARGUS_EV_CONTAINMENT_EXECUTED; |
| 159 | int rc = ARGUS_OK; unsigned pushed = 0; |
| 160 | while ((rc = argus_ring_push(r2, &q)) == ARGUS_OK && pushed < 64) pushed++; |
| 161 | ArgusRingStats s2; argus_ring_stats(r2, &s2); |
| 162 | CHECK(rc == ARGUS_ERR_FULL && s2.critical_overflow == 1 && s2.refused[0] == 0); |
| 163 | free(m2); |
| 164 | } |
| 165 | free(mem); |
| 166 | printf("class floor: %llu pushes (every kind x every class byte), %llu MALFORMED counted in refused[0], " |
| 167 | "none drained\n", (unsigned long long)attempts, (unsigned long long)bad); |
| 168 | } |
| 169 | |
| 170 | /* For each capacity and class: the depth at which the class is first refused, |
| 171 | * measured both alone and on top of CRITICAL fill, equals the saturation point. */ |
| 172 | static void test_watermarks(void) |
| 173 | { |
| 174 | static const uint32_t caps[] = { 4, 8, 16, 64, 1024 }; |
| 175 | static const char *names[] = { "", "CRITICAL", "SECURITY", "AUDIT", "INFORMATIONAL" }; |
| 176 | for (size_t ci = 0; ci < sizeof caps / sizeof caps[0]; ci++) { |
| 177 | uint32_t cap = caps[ci]; |
| 178 | for (uint8_t cls = 1; cls <= ARGUS_CLASS_MAX; cls++) { |
| 179 | uint32_t sat = argus_ring_saturation_point(cap, cls); |
| 180 | /* ceil(cap * {100,90,75,50}%) written in twentieths */ |
| 181 | uint64_t expect = ((uint64_t)cap * (cls == 1 ? 20 : cls == 2 ? 18 : cls == 3 ? 15 : 10) + 19) / 20; |
| 182 | CHECK(sat == expect); |
| 183 | /* alone */ |
| 184 | void *mem; ArgusRing *r = new_ring(cap, &mem); |
| 185 | uint32_t depth = 0; |
| 186 | while (argus_ring_push(r, &(ArgusEvent){ .version = 1, .class_ = cls, .kind = NOFLOOR_KIND, .outcome = 1, .sequence = 1 }) == ARGUS_OK) |
| 187 | depth++; |
| 188 | CHECK(depth == sat); |
| 189 | free(mem); |
| 190 | /* on top of d CRITICAL events */ |
| 191 | for (uint32_t d = 0; d <= cap; d++) { |
| 192 | r = new_ring(cap, &mem); |
| 193 | for (uint32_t k = 0; k < d; k++) CHECK(argus_ring_push(r, &(ArgusEvent){ .version = 1, .class_ = 1, .kind = NOFLOOR_KIND, .outcome = 1, .sequence = 1 }) == (k < cap ? ARGUS_OK : ARGUS_ERR_FULL)); |
| 194 | int rc = argus_ring_push(r, &(ArgusEvent){ .version = 1, .class_ = cls, .kind = NOFLOOR_KIND, .outcome = 1, .sequence = 1 }); |
| 195 | CHECK(rc == (d < sat ? ARGUS_OK : ARGUS_ERR_FULL)); |
| 196 | free(mem); |
| 197 | } |
| 198 | if (cap == 1024) printf(" cap 1024 %-13s refused from depth %4u (%.1f%%)\n", names[cls], sat, 100.0 * sat / cap); |
| 199 | } |
| 200 | } |
| 201 | printf("watermarks: OK for capacities 4, 8, 16, 64, 1024\n"); |
| 202 | } |
| 203 | |
| 204 | static void test_drain_drops(void) |
| 205 | { |
| 206 | void *mem; ArgusRing *r = new_ring(8, &mem); |
| 207 | ArgusRingStats s; |
| 208 | ArgusEvent out[8]; |
| 209 | uint64_t seq = 0; |
| 210 | CHECK(argus_ring_drain_drops(r, out, 8, &seq) == 0); |
| 211 | /* Fill to 4 with SECURITY: INFO refused (limit 4). */ |
| 212 | for (int i = 0; i < 4; i++) CHECK(argus_ring_push(r, &(ArgusEvent){ .version = 1, .class_ = 2, .kind = NOFLOOR_KIND, .outcome = 1, .sequence = 1 }) == ARGUS_OK); |
| 213 | for (int i = 0; i < 3; i++) CHECK(argus_ring_push(r, &(ArgusEvent){ .version = 1, .class_ = 4, .kind = NOFLOOR_KIND, .outcome = 1, .sequence = 1 }) == ARGUS_ERR_FULL); |
| 214 | for (int i = 0; i < 2; i++) CHECK(argus_ring_push(r, &(ArgusEvent){ .version = 1, .class_ = 2, .kind = NOFLOOR_KIND, .outcome = 1, .sequence = 1 }) == ARGUS_OK); |
| 215 | /* depth 6 = AUDIT limit (6) */ |
| 216 | CHECK(argus_ring_push(r, &(ArgusEvent){ .version = 1, .class_ = 3, .kind = NOFLOOR_KIND, .outcome = 1, .sequence = 1 }) == ARGUS_ERR_FULL); |
| 217 | argus_ring_stats(r, &s); |
| 218 | CHECK(s.refused[4] == 3 && s.refused[3] == 1 && s.refused[2] == 0 && s.critical_overflow == 0 && s.depth == 6); |
| 219 | |
| 220 | CHECK(argus_ring_drain_drops(r, out, 8, NULL) == 0); |
| 221 | CHECK(argus_ring_drain_drops(r, out, 1, &seq) == 1); /* max honoured: AUDIT first (higher class), INFO stays pending */ |
| 222 | CHECK(seq == 2 && out[0].sequence == 1); |
| 223 | CHECK(out[0].kind == ARGUS_EV_TELEMETRY_DROPPED && out[0].class_ == ARGUS_CLASS_CRITICAL); /* floor of TELEMETRY_DROPPED */ |
| 224 | CHECK(out[0].flags == ARGUS_FLAG_CONSUMER && out[0].object_id == ARGUS_CLASS_AUDIT && out[0].resource == 1 && out[0].cap_id == ARGUS_CAP_NONE); |
| 225 | CHECK(argus_event_validate(&out[0]) == ARGUS_OK); |
| 226 | CHECK(argus_ring_drain_drops(r, out, 8, &seq) == 1); |
| 227 | CHECK(out[0].object_id == ARGUS_CLASS_INFORMATIONAL && out[0].resource == 3 && out[0].sequence == 2); |
| 228 | CHECK(argus_ring_drain_drops(r, out, 8, &seq) == 0); /* reset after report */ |
| 229 | argus_ring_stats(r, &s); |
| 230 | CHECK(s.refused[4] == 3 && s.refused[3] == 1); /* cumulative stats unchanged */ |
| 231 | |
| 232 | /* Fill to full with CRITICAL, overflow twice. */ |
| 233 | CHECK(argus_ring_push(r, &(ArgusEvent){ .version = 1, .class_ = 1, .kind = NOFLOOR_KIND, .outcome = 1, .sequence = 1 }) == ARGUS_OK); |
| 234 | CHECK(argus_ring_push(r, &(ArgusEvent){ .version = 1, .class_ = 1, .kind = NOFLOOR_KIND, .outcome = 1, .sequence = 1 }) == ARGUS_OK); |
| 235 | CHECK(argus_ring_push(r, &(ArgusEvent){ .version = 1, .class_ = 1, .kind = NOFLOOR_KIND, .outcome = 1, .sequence = 1 }) == ARGUS_ERR_FULL); |
| 236 | CHECK(argus_ring_push(r, &(ArgusEvent){ .version = 1, .class_ = 1, .kind = NOFLOOR_KIND, .outcome = 1, .sequence = 1 }) == ARGUS_ERR_FULL); |
| 237 | CHECK(argus_ring_push(r, &(ArgusEvent){ .version = 1, .class_ = 2, .kind = NOFLOOR_KIND, .outcome = 1, .sequence = 1 }) == ARGUS_ERR_FULL); |
| 238 | argus_ring_stats(r, &s); |
| 239 | CHECK(s.critical_overflow == 2 && s.refused[1] == 0 && s.refused[2] == 1 && s.depth == 8); |
| 240 | CHECK(argus_ring_drain_drops(r, out, 8, &seq) == 2); |
| 241 | CHECK(out[0].object_id == ARGUS_CLASS_CRITICAL && out[0].resource == 2 && out[0].class_ == ARGUS_CLASS_CRITICAL); |
| 242 | CHECK(out[1].object_id == ARGUS_CLASS_SECURITY && out[1].resource == 1 && out[1].class_ == ARGUS_CLASS_CRITICAL); |
| 243 | /* Drop reports are always CRITICAL class; stats keep the overflow (sticky). */ |
| 244 | CHECK(argus_ring_push(r, &(ArgusEvent){ .version = 1, .class_ = 4, .kind = NOFLOOR_KIND, .outcome = 1, .sequence = 1 }) == ARGUS_ERR_FULL); |
| 245 | CHECK(argus_ring_drain_drops(r, out, 8, &seq) == 1); |
| 246 | CHECK(out[0].class_ == ARGUS_CLASS_CRITICAL && out[0].object_id == ARGUS_CLASS_INFORMATIONAL); |
| 247 | argus_ring_stats(r, &s); |
| 248 | CHECK(s.critical_overflow == 2); |
| 249 | free(mem); |
| 250 | printf("drain_drops: OK (max honoured, reset after report, always CRITICAL, sticky critical overflow)\n"); |
| 251 | } |
| 252 | |
| 253 | /* ---- two-thread SPSC run ---- */ |
| 254 | #define SPSC_CAP 1024u |
| 255 | typedef struct { |
| 256 | ArgusRing *r; |
| 257 | uint64_t events; |
| 258 | _Atomic int producer_done; |
| 259 | _Atomic int consumer_released; /* the consumer does not pop until the producer sets this */ |
| 260 | /* Phase 0 (held consumer): the producer offers SPSC_CAP INFORMATIONAL events while |
| 261 | * nothing pops, so the outcome does not depend on thread scheduling. */ |
| 262 | uint64_t hold_accepted, hold_refused, hold_first_refusal; |
| 263 | ArgusRingStats hold_stats; /* snapshot taken while the consumer is still held */ |
| 264 | /* producer results */ |
| 265 | uint64_t attempts, accepted[5], refused_rc[5]; |
| 266 | /* consumer results */ |
| 267 | uint64_t popped, fifo_errors, content_errors, reported[5], drop_events, last_seq; |
| 268 | } Spsc; |
| 269 | |
| 270 | static uint64_t xs(uint64_t *s) { uint64_t x = *s; x ^= x << 13; x ^= x >> 7; x ^= x << 17; return *s = x; } |
| 271 | |
| 272 | static void *producer(void *arg) |
| 273 | { |
| 274 | Spsc *t = arg; |
| 275 | uint64_t rs = 0x1234567887654321ull, next[5] = {0}; |
| 276 | t->hold_first_refusal = UINT64_MAX; |
| 277 | for (uint64_t i = 0; i < t->events; i++) { |
| 278 | int hold = i < SPSC_CAP; /* phase 0: consumer held, INFORMATIONAL only */ |
| 279 | uint64_t roll = hold ? 99 : xs(&rs) % 100; |
| 280 | uint8_t cls = roll < 5 ? 1 : roll < 30 ? 2 : roll < 60 ? 3 : 4; |
| 281 | ArgusEvent e = mk(cls, i + 1, ++next[cls]); /* resource = per-class attempt number */ |
| 282 | e.object_id = (uint32_t)(i * 2654435761u); |
| 283 | int rc = argus_ring_push(t->r, &e); |
| 284 | t->attempts++; |
| 285 | if (rc == ARGUS_OK) t->accepted[cls]++; |
| 286 | else if (rc == ARGUS_ERR_FULL) t->refused_rc[cls]++; |
| 287 | else { fprintf(stderr, "FAIL push rc %d\n", rc); exit(1); } |
| 288 | if (hold) { |
| 289 | if (rc == ARGUS_OK) t->hold_accepted++; |
| 290 | else { t->hold_refused++; if (t->hold_first_refusal == UINT64_MAX) t->hold_first_refusal = i; } |
| 291 | } |
| 292 | if (i + 1 == SPSC_CAP) { /* end of phase 0: look, then let the consumer go */ |
| 293 | argus_ring_stats(t->r, &t->hold_stats); |
| 294 | atomic_store_explicit(&t->consumer_released, 1, memory_order_release); |
| 295 | } |
| 296 | } |
| 297 | atomic_store_explicit(&t->producer_done, 1, memory_order_release); |
| 298 | return NULL; |
| 299 | } |
| 300 | |
| 301 | static void consume_drops(Spsc *t, uint64_t *drop_seq) |
| 302 | { |
| 303 | ArgusEvent d[4]; |
| 304 | size_t n = argus_ring_drain_drops(t->r, d, 4, drop_seq); |
| 305 | for (size_t i = 0; i < n; i++) { |
| 306 | if (d[i].kind != ARGUS_EV_TELEMETRY_DROPPED || d[i].object_id < 1 || d[i].object_id > 4 || |
| 307 | argus_event_validate(&d[i]) != ARGUS_OK) |
| 308 | t->content_errors++; |
| 309 | else |
| 310 | t->reported[d[i].object_id] += d[i].resource; |
| 311 | t->drop_events++; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | static void *consumer(void *arg) |
| 316 | { |
| 317 | Spsc *t = arg; |
| 318 | uint64_t last_res[5] = {0}, last_seq = 0, drop_seq = 1; |
| 319 | volatile uint64_t sink = 0; |
| 320 | while (!atomic_load_explicit(&t->consumer_released, memory_order_acquire)) { /* held */ } |
| 321 | for (;;) { |
| 322 | ArgusEvent e; |
| 323 | int done = atomic_load_explicit(&t->producer_done, memory_order_acquire); |
| 324 | if (argus_ring_pop(t->r, &e) == ARGUS_OK) { |
| 325 | t->popped++; |
| 326 | uint8_t c = e.class_; |
| 327 | if (c < 1 || c > 4 || e.object_id != (uint32_t)((e.sequence - 1) * 2654435761u)) t->content_errors++; |
| 328 | else { |
| 329 | if (e.resource <= last_res[c]) t->fifo_errors++; |
| 330 | last_res[c] = e.resource; |
| 331 | if (e.sequence <= last_seq) t->fifo_errors++; |
| 332 | last_seq = e.sequence; |
| 333 | } |
| 334 | for (int k = 0; k < 40; k++) sink += (uint64_t)k * e.sequence; /* slow consumer */ |
| 335 | if ((t->popped & 1023) == 0) consume_drops(t, &drop_seq); |
| 336 | } else if (done) { |
| 337 | break; /* done was read before the failed pop: ring is truly drained */ |
| 338 | } |
| 339 | } |
| 340 | consume_drops(t, &drop_seq); |
| 341 | consume_drops(t, &drop_seq); |
| 342 | t->last_seq = last_seq; |
| 343 | (void)sink; |
| 344 | return NULL; |
| 345 | } |
| 346 | |
| 347 | static void test_spsc(uint64_t events) |
| 348 | { |
| 349 | if (events <= SPSC_CAP) { fprintf(stderr, "events must exceed %u\n", SPSC_CAP); exit(2); } |
| 350 | Spsc t; |
| 351 | memset(&t, 0, sizeof t); |
| 352 | void *mem; |
| 353 | t.r = new_ring(SPSC_CAP, &mem); |
| 354 | t.events = events; |
| 355 | atomic_init(&t.producer_done, 0); |
| 356 | atomic_init(&t.consumer_released, 0); |
| 357 | pthread_t pc, cc; |
| 358 | pthread_create(&cc, NULL, consumer, &t); |
| 359 | pthread_create(&pc, NULL, producer, &t); |
| 360 | pthread_join(pc, NULL); |
| 361 | pthread_join(cc, NULL); |
| 362 | |
| 363 | ArgusRingStats s; |
| 364 | argus_ring_stats(t.r, &s); |
| 365 | uint64_t sum_refused = s.refused[1] + s.refused[2] + s.refused[3] + s.refused[4]; |
| 366 | uint64_t accepted = t.accepted[1] + t.accepted[2] + t.accepted[3] + t.accepted[4]; |
| 367 | printf("spsc: %llu attempts, %llu popped, refused SEC %llu AUDIT %llu INFO %llu, critical_overflow %llu, " |
| 368 | "%llu drop events\n", |
| 369 | (unsigned long long)t.attempts, (unsigned long long)t.popped, (unsigned long long)s.refused[2], |
| 370 | (unsigned long long)s.refused[3], (unsigned long long)s.refused[4], |
| 371 | (unsigned long long)s.critical_overflow, (unsigned long long)t.drop_events); |
| 372 | printf(" accepted per class CRIT %llu SEC %llu AUDIT %llu INFO %llu\n", |
| 373 | (unsigned long long)t.accepted[1], (unsigned long long)t.accepted[2], |
| 374 | (unsigned long long)t.accepted[3], (unsigned long long)t.accepted[4]); |
| 375 | CHECK(t.attempts == events); |
| 376 | CHECK(t.attempts == t.popped + sum_refused + s.critical_overflow); /* the transport identity */ |
| 377 | CHECK(s.pushed == accepted && s.popped == t.popped && s.depth == 0); |
| 378 | CHECK(s.refused[1] == 0); |
| 379 | CHECK(s.critical_overflow == t.refused_rc[1]); |
| 380 | for (int c = 2; c <= 4; c++) CHECK(s.refused[c] == t.refused_rc[c]); |
| 381 | for (int c = 1; c <= 4; c++) CHECK(t.reported[c] == t.refused_rc[c]); /* every refusal reported exactly once */ |
| 382 | CHECK(t.fifo_errors == 0); |
| 383 | CHECK(t.content_errors == 0); |
| 384 | /* Deterministic backpressure (consumer held, ring offered SPSC_CAP INFORMATIONAL events, nothing popped): |
| 385 | * exactly the INFORMATIONAL watermark is accepted, the rest is refused and counted, the first refusal is the |
| 386 | * event right at the watermark, and the ring is left exactly at the watermark. */ |
| 387 | uint32_t sat = argus_ring_saturation_point(SPSC_CAP, ARGUS_CLASS_INFORMATIONAL); |
| 388 | CHECK(sat > 0 && sat < SPSC_CAP); |
| 389 | CHECK(t.hold_accepted == sat); |
| 390 | CHECK(t.hold_refused == SPSC_CAP - sat); |
| 391 | CHECK(t.hold_first_refusal == sat); |
| 392 | CHECK(t.hold_stats.pushed == sat && t.hold_stats.popped == 0 && t.hold_stats.depth == sat); |
| 393 | CHECK(t.hold_stats.refused[4] == SPSC_CAP - sat && t.hold_stats.refused[1] + t.hold_stats.refused[2] + t.hold_stats.refused[3] == 0); |
| 394 | CHECK(t.hold_stats.critical_overflow == 0); |
| 395 | CHECK(sum_refused >= SPSC_CAP - sat); |
| 396 | free(mem); |
| 397 | printf("spsc: identity attempts == popped + sum(refused) + critical_overflow holds; FIFO per class OK; " |
| 398 | "all refusals reported by drain_drops\n"); |
| 399 | } |
| 400 | |
| 401 | int main(int argc, char **argv) |
| 402 | { |
| 403 | uint64_t events = argc > 1 ? strtoull(argv[1], NULL, 10) : 10000000ull; |
| 404 | test_init(); |
| 405 | test_class_floor(); |
| 406 | test_watermarks(); |
| 407 | test_drain_drops(); |
| 408 | test_spsc(events); |
| 409 | if (failures) { printf("test_argus_ring: FAIL (%d)\n", failures); return 1; } |
| 410 | printf("test_argus_ring: PASS\n"); |
| 411 | return 0; |
| 412 | } |