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/tests/bench_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 / tests / bench_argus_ring.c` 223 lines ยท 8994 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 * bench_argus_ring.c -- ARGUS lane B: ring saturation benchmark. 19 * Prints: bytes per event, ring footprint, the depth at which each class 20 * starts being refused (argus_ring_saturation_point, confirmed empirically), 21 * push latency p50/p99 (uncontended and with a live consumer), and pops/s. 22 * Timing (test code only; the ring itself makes no system calls): 23 * clock_gettime_nsec_np(CLOCK_UPTIME_RAW) on macOS, CLOCK_MONOTONIC_RAW 24 * elsewhere. Timer granularity (tens of ns on Apple silicon) is too coarse 25 * for one push, so each latency sample is one batch of BATCH pushes timed 26 * together and divided by BATCH; p50/p99 are over those batch samples. The 27 * timer pair cost is printed and already amortised over the batch. 28 */ 29 #include "argus_abi.h" 30 #include 31 #include 32 #include 33 #include 34 #include 35 #include 36 37 38 #define CAP 4096u 39 #define SAMPLES 200000u 40 #define BATCH 32u 41 42 static uint64_t now_ns(void) 43 { 44 #ifdef __APPLE__ 45 return clock_gettime_nsec_np(CLOCK_UPTIME_RAW); 46 #else 47 struct timespec ts; 48 clock_gettime(CLOCK_MONOTONIC_RAW, &ts); 49 return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; 50 #endif 51 } 52 53 static int cmp_dbl(const void *a, const void *b) 54 { 55 double x = *(const double *)a, y = *(const double *)b; 56 return (x > y) - (x < y); 57 } 58 59 static int cmp_u32(const void *a, const void *b) 60 { 61 uint32_t x = *(const uint32_t *)a, y = *(const uint32_t *)b; 62 return (x > y) - (x < y); 63 } 64 65 static ArgusRing *new_ring(uint32_t cap, void **mem) 66 { 67 size_t fp = argus_ring_footprint(cap); 68 ArgusRing *r = NULL; 69 if (posix_memalign(mem, 64, fp) != 0 || argus_ring_init(&r, *mem, fp, cap) != ARGUS_OK) { 70 fprintf(stderr, "ring init failed\n"); 71 exit(2); 72 } 73 return r; 74 } 75 76 /* A kind whose class floor admits cls (0x7FFF: no v1 kind, so no floor at the ring; 77 * no v1 kind may be INFORMATIONAL, the watermark is kept for future kinds). */ 78 static uint16_t kind_for(uint8_t cls) 79 { 80 return cls == 1 ? ARGUS_EV_FORGED_CAPABILITY : cls == 2 ? ARGUS_EV_CAPABILITY_DENIED 81 : cls == 3 ? ARGUS_EV_CAPABILITY_USED : 0x7FFF; 82 } 83 84 static ArgusEvent ev(uint8_t cls, uint64_t seq) 85 { 86 ArgusEvent e; 87 memset(&e, 0, sizeof e); 88 e.version = 1; e.class_ = cls; e.kind = kind_for(cls); e.outcome = 1; e.sequence = seq; 89 return e; 90 } 91 92 static void report(const char *name, double *s, size_t n) 93 { 94 qsort(s, n, sizeof *s, cmp_dbl); 95 printf(" %-40s p50 %6.2f ns p99 %6.2f ns p99.9 %7.2f ns (per push, %u-push batches)\n", 96 name, s[n / 2], s[(n * 99) / 100], s[(n * 999) / 1000], BATCH); 97 } 98 99 typedef struct { ArgusRing *r; _Atomic int stop; _Atomic uint64_t popped; } Cons; 100 101 static void *consumer(void *arg) 102 { 103 Cons *c = arg; 104 ArgusEvent buf[64]; 105 for (;;) { 106 size_t n = argus_ring_pop_batch(c->r, buf, 64); 107 atomic_fetch_add_explicit(&c->popped, n, memory_order_relaxed); 108 if (n == 0 && atomic_load_explicit(&c->stop, memory_order_acquire)) { 109 atomic_fetch_add_explicit(&c->popped, argus_ring_pop_batch(c->r, buf, 64), memory_order_relaxed); 110 if (argus_ring_pop_batch(c->r, buf, 1) == 0) break; 111 atomic_fetch_add_explicit(&c->popped, 1, memory_order_relaxed); 112 } 113 } 114 return NULL; 115 } 116 117 int main(void) 118 { 119 static const char *names[] = { "", "CRITICAL", "SECURITY", "AUDIT", "INFORMATIONAL" }; 120 uint32_t *t = malloc(SAMPLES * sizeof *t); 121 double *s = malloc(SAMPLES * sizeof *s); 122 if (!s || !t) return 2; 123 void *mem; 124 125 printf("bench_argus_ring (provisional; host-dependent)\n"); 126 printf(" bytes per event %u (sizeof ArgusEvent %zu)\n", ARGUS_EVENT_SIZE, sizeof(ArgusEvent)); 127 printf(" ring footprint: cap 1024 = %zu B, cap %u = %zu B, cap 65536 = %zu B (header %zu B)\n", 128 argus_ring_footprint(1024), CAP, argus_ring_footprint(CAP), argus_ring_footprint(65536), 129 argus_ring_footprint(1024) - 1024 * sizeof(ArgusEvent)); 130 131 /* Saturation points: formula vs measured (fill one class alone until refused). */ 132 printf(" saturation (cap %u): depth at which each class is first refused\n", CAP); 133 for (uint8_t cls = 1; cls <= 4; cls++) { 134 ArgusRing *r = new_ring(CAP, &mem); 135 uint32_t d = 0; 136 ArgusEvent e = ev(cls, 1); 137 while (argus_ring_push(r, &e) == ARGUS_OK) d++; 138 uint32_t sat = argus_ring_saturation_point(CAP, cls); 139 printf(" %-13s point %5u (%5.1f%% fill), measured %5u %s\n", names[cls], sat, 100.0 * sat / CAP, d, 140 d == sat ? "match" : "MISMATCH"); 141 free(mem); 142 if (d != sat) return 1; 143 } 144 145 /* Timer cost. */ 146 for (uint32_t i = 0; i < SAMPLES; i++) { uint64_t a = now_ns(); t[i] = (uint32_t)(now_ns() - a); } 147 qsort(t, SAMPLES, sizeof *t, cmp_u32); 148 printf(" timer pair cost p50 %u ns (amortised over %u pushes per sample)\n", t[SAMPLES / 2], BATCH); 149 150 /* Uncontended push: single thread, push a half ring then pop it back. */ 151 { 152 ArgusRing *r = new_ring(CAP, &mem); 153 static ArgusEvent buf[CAP / 2]; 154 uint64_t seq = 1; 155 for (uint32_t k = 0; k < SAMPLES; k++) { 156 ArgusEvent e[BATCH]; 157 for (uint32_t j = 0; j < BATCH; j++) e[j] = ev(ARGUS_CLASS_SECURITY, seq++); 158 int bad = 0; 159 uint64_t a = now_ns(); 160 for (uint32_t j = 0; j < BATCH; j++) bad |= argus_ring_push(r, &e[j]); 161 s[k] = (double)(now_ns() - a) / BATCH; 162 if (bad) { fprintf(stderr, "unexpected refusal\n"); return 1; } 163 if ((k % ((CAP / 2) / BATCH)) == ((CAP / 2) / BATCH) - 1) argus_ring_pop_batch(r, buf, CAP / 2); 164 } 165 report("push, single thread (SECURITY)", s, SAMPLES); 166 /* Bulk: no timer per push. */ 167 uint64_t n = 20000000ull, t0 = now_ns(); 168 for (uint64_t seq = 1; seq <= n; seq++) { 169 ArgusEvent e = ev(ARGUS_CLASS_SECURITY, seq); 170 argus_ring_push(r, &e); 171 if ((seq % (CAP / 2)) == 0) argus_ring_pop_batch(r, buf, CAP / 2); 172 } 173 uint64_t t1 = now_ns(); 174 printf(" push+pop_batch, single thread bulk: %.2f ns per event (%llu events)\n", 175 (double)(t1 - t0) / (double)n, (unsigned long long)n); 176 free(mem); 177 } 178 179 /* Two threads: producer pushes as fast as it can, consumer pops in batches. */ 180 { 181 Cons c; 182 memset(&c, 0, sizeof c); 183 c.r = new_ring(CAP, &mem); 184 atomic_init(&c.stop, 0); 185 pthread_t th; 186 pthread_create(&th, NULL, consumer, &c); 187 uint64_t accepted = 0, refused = 0, seq = 1; 188 for (uint32_t k = 0; k < SAMPLES; k++) { 189 ArgusEvent e[BATCH]; 190 int rc[BATCH]; 191 for (uint32_t j = 0; j < BATCH; j++) e[j] = ev(ARGUS_CLASS_CRITICAL, seq++); 192 uint64_t a = now_ns(); 193 for (uint32_t j = 0; j < BATCH; j++) rc[j] = argus_ring_push(c.r, &e[j]); 194 s[k] = (double)(now_ns() - a) / BATCH; 195 for (uint32_t j = 0; j < BATCH; j++) { if (rc[j] == ARGUS_OK) accepted++; else refused++; } 196 } 197 report("push, live consumer thread (CRITICAL)", s, SAMPLES); 198 uint64_t n = 20000000ull, p0 = atomic_load(&c.popped), t0 = now_ns(); 199 for (uint64_t i = 0; i < n; i++, seq++) { 200 ArgusEvent e = ev(ARGUS_CLASS_CRITICAL, seq); 201 if (argus_ring_push(c.r, &e) == ARGUS_OK) accepted++; else refused++; 202 } 203 uint64_t t1 = now_ns(), p1 = atomic_load(&c.popped); 204 atomic_store_explicit(&c.stop, 1, memory_order_release); 205 pthread_join(th, NULL); 206 ArgusRingStats st; 207 argus_ring_stats(c.r, &st); 208 double secs = (double)(t1 - t0) / 1e9; 209 printf(" two threads bulk: %.2f ns per push attempt, %.1f M pops/s, accepted %llu, refused (full) %llu, " 210 "popped total %llu\n", 211 (double)(t1 - t0) / (double)n, (double)(p1 - p0) / secs / 1e6, (unsigned long long)accepted, 212 (unsigned long long)refused, (unsigned long long)atomic_load(&c.popped)); 213 if (st.pushed != accepted || st.popped != atomic_load(&c.popped) || st.critical_overflow != refused) { 214 fprintf(stderr, "stats mismatch\n"); 215 return 1; 216 } 217 free(mem); 218 } 219 free(s); 220 free(t); 221 printf("bench_argus_ring: done\n"); 222 return 0; 223 }