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/omega-mv/blob/main/tests/runtime/rx_r15_instr.c # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / tests / runtime / rx_r15_instr.c` 401 lines · 18165 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 * rx_r15_instr.c -- R15 instrumentation checks (spec/r15-performance-proof.md 19 * §6 and clarification C1). Host only. Every counter the R15 harness reduces 20 * is checked here against a case whose true value is known in advance: 21 * 22 * propagation fanout n: inspected, matched, wake attempts, wakes accepted 23 * and ready insertions are exact, and a subscriber on another 24 * field is inspected but not matched; 25 * scheduler wall and thread-CPU time are both recorded, separately; 26 * timing a buffer that overflows says so; 27 * bytes each ring path counts one 128-byte descriptor per copy; 28 * R9 I/O a store counts only its own writes and syncs; 29 * SEQ idle a pulse over a plan that misses a registered reaction 30 * never declares the world quiescent. 31 * 32 * Exit status is the number of failed checks (0 = pass). 33 */ 34 #include "runtime/rx_caproot.h" 35 #include "runtime/rx_generation.h" 36 #include "runtime/rx_seq_reference.h" 37 #include "runtime/rx_world.h" 38 39 #include 40 #include 41 #include 42 #include 43 #include 44 45 static int g_checks, g_failures; 46 47 #define CHECK(cond, ...) do { \ 48 g_checks++; \ 49 if (!(cond)) { \ 50 g_failures++; \ 51 fprintf(stderr, " FAIL %s:%d ", __FILE__, __LINE__); \ 52 fprintf(stderr, __VA_ARGS__); \ 53 fputc('\n', stderr); \ 54 } \ 55 } while (0) 56 57 enum { SUBJ_WORK = 2, SUBJ_EXTERNAL = 100, ISSUER = 3 }; 58 enum { RES_SRC = 0x50, RES_OUT = 0x51 }; 59 60 typedef struct { 61 RxCapRoot root; 62 RxCapAdmin admin; 63 RxWorld w; 64 } Env; 65 66 static int env_start(Env *e, uint32_t workers) { 67 if (rx_caproot_start(&e->root, &e->admin) != RX_CAP_OK) return -1; 68 if (rx_world_init(&e->w, &e->root, workers, 1u << 16) != RX_OK) { 69 rx_caproot_stop(&e->root, &e->admin); 70 return -1; 71 } 72 e->w.external_subject = SUBJ_EXTERNAL; 73 return 0; 74 } 75 76 static void env_stop(Env *e) { 77 rx_world_destroy(&e->w); 78 rx_caproot_stop(&e->root, &e->admin); 79 } 80 81 static RxCapRef mint(Env *e, uint32_t subject, uint64_t resource, uint32_t rights) { 82 RxCapMint m; 83 memset(&m, 0, sizeof(m)); 84 m.issuer = ISSUER; 85 m.subject = subject; 86 m.resource = resource; 87 m.rights = rights; 88 m.parent = (RxCapRef){ UINT32_MAX, 0 }; 89 m.authority = rx_capadmin_office(&e->admin); 90 RxCapRef r = { UINT32_MAX, 0 }; 91 rx_capadmin_mint(&e->admin, &m, &r); 92 return r; 93 } 94 95 static RxObjRef mkobj(Env *e, uint64_t resource) { 96 uint64_t init[RX_MAX_FIELDS] = { 0 }; 97 RxObjRef r = { UINT32_MAX, 0 }; 98 rx_world_create(&e->w, 1, RX_PERSIST_RESIDENT, resource, init, &r); 99 return r; 100 } 101 102 /* Publishes nothing: the only propagation wave is the stimulus's. */ 103 static int fn_quiet(RxCtx *c) { (void)c; return 0; } 104 105 static uint32_t add_listener(Env *e, RxObjRef src, uint64_t mask, RxCapRef rd) { 106 RxReactionDesc d; 107 memset(&d, 0, sizeof(d)); 108 d.name = "r15.listener"; 109 d.faculty = RX_FACULTY_OMEGA; 110 d.subject = SUBJ_WORK; 111 d.priority = RX_PRIO_FOREGROUND; 112 d.fn = fn_quiet; 113 d.triggers[d.n_triggers++] = (RxDep){ src, mask }; 114 d.caps[d.n_caps++] = (RxCapNeed){ rd, RES_SRC, RX_RIGHT_READ }; 115 uint32_t id = UINT32_MAX; 116 CHECK(rx_world_add_reaction(&e->w, &d, &id) == RX_OK, "add listener"); 117 return id; 118 } 119 120 static void t_propagation_and_scheduler(uint32_t fanout) { 121 Env e; 122 CHECK(env_start(&e, 1) == 0, "setup fanout %u", fanout); 123 RxObjRef src = mkobj(&e, RES_SRC); 124 RxCapRef ext = mint(&e, SUBJ_EXTERNAL, RES_SRC, RX_RIGHT_WRITE); 125 RxCapRef rd = mint(&e, SUBJ_WORK, RES_SRC, RX_RIGHT_READ); 126 for (uint32_t i = 0; i < fanout; i++) add_listener(&e, src, RX_FIELD(0), rd); 127 add_listener(&e, src, RX_FIELD(1), rd); /* inspected, never matched */ 128 RxTiming buf[512]; 129 rx_world_set_timing(&e.w, buf, 512); 130 RxMutation m = { src, 0, 7 }; 131 CHECK(rx_world_publish_external(&e.w, ext, &m, 1) > 0, "stimulus"); 132 CHECK(rx_world_wait_quiescent(&e.w, 5000) == RX_OK, "quiet"); 133 pthread_mutex_lock(&e.w.mu); 134 RxPropWave p = e.w.last_prop; 135 RxStats s = e.w.stats; 136 pthread_mutex_unlock(&e.w.mu); 137 CHECK(p.inspected == fanout + 1u, "fanout %u: inspected %llu", fanout, 138 (unsigned long long)p.inspected); 139 CHECK(p.matched == fanout, "fanout %u: matched %llu", fanout, (unsigned long long)p.matched); 140 CHECK(p.wake_attempts == fanout, "fanout %u: wake attempts %llu", fanout, 141 (unsigned long long)p.wake_attempts); 142 CHECK(p.wakes_accepted == fanout, "fanout %u: accepted %llu", fanout, 143 (unsigned long long)p.wakes_accepted); 144 CHECK(p.ready_inserts == fanout, "fanout %u: ready inserts %llu", fanout, 145 (unsigned long long)p.ready_inserts); 146 CHECK(p.coalesced == 0 && p.deferred == 0 && p.suppressed == 0, 147 "fanout %u: unexpected coalesced/deferred/suppressed", fanout); 148 CHECK(p.wall_ns > 0 && p.cpu_ns > 0, "fanout %u: wave time not recorded", fanout); 149 uint64_t stored = 0, attempted = 0; 150 CHECK(rx_world_timing_status(&e.w, &stored, &attempted) == RX_OK && stored == fanout && 151 attempted == fanout, "fanout %u: timing stored %llu attempted %llu", fanout, 152 (unsigned long long)stored, (unsigned long long)attempted); 153 uint64_t cpu = 0, wall = 0; 154 int missing = 0; 155 for (uint64_t i = 0; i < stored; i++) { 156 if (!buf[i].t_demand || !buf[i].t_ready || !buf[i].t_run || !buf[i].sched_ns || 157 !buf[i].sched_cpu_ns) missing++; 158 cpu += buf[i].sched_cpu_ns; 159 wall += buf[i].sched_ns; 160 } 161 CHECK(missing == 0, "fanout %u: %d samples lack timestamps or scheduler time", fanout, missing); 162 CHECK(s.sched_wall_ns > 0 && s.sched_cpu_ns > 0, "world scheduler totals missing"); 163 /* Both clocks count nanoseconds on the same thread; CPU time cannot run 164 * meaningfully ahead of wall time. 1 us per sample allows for the two 165 * clocks' separate reads. */ 166 CHECK(cpu <= wall + stored * 1000u, "fanout %u: scheduler CPU %llu > wall %llu", fanout, 167 (unsigned long long)cpu, (unsigned long long)wall); 168 rx_world_set_timing(&e.w, NULL, 0); 169 env_stop(&e); 170 } 171 172 /* One slot: every matched dependent is woken, but only one is placed on the 173 * ready ring in the wave. Wake attempts are not ready insertions. */ 174 static void t_blocked_wave(void) { 175 Env e; 176 CHECK(env_start(&e, 1) == 0, "setup"); 177 RxResourceBudget b = e.w.budget; 178 b.slots = 1; 179 rx_world_set_resources(&e.w, &b); 180 RxObjRef src = mkobj(&e, RES_SRC); 181 RxCapRef ext = mint(&e, SUBJ_EXTERNAL, RES_SRC, RX_RIGHT_WRITE); 182 RxCapRef rd = mint(&e, SUBJ_WORK, RES_SRC, RX_RIGHT_READ); 183 for (int i = 0; i < 8; i++) add_listener(&e, src, RX_FIELD(0), rd); 184 RxTiming buf[64]; 185 rx_world_set_timing(&e.w, buf, 64); 186 RxMutation m = { src, 0, 3 }; 187 CHECK(rx_world_publish_external(&e.w, ext, &m, 1) > 0, "stimulus"); 188 pthread_mutex_lock(&e.w.mu); 189 RxPropWave p = e.w.last_prop; 190 pthread_mutex_unlock(&e.w.mu); 191 CHECK(p.matched == 8 && p.wake_attempts == 8 && p.wakes_accepted == 8, 192 "blocked wave: matched %llu attempts %llu accepted %llu", 193 (unsigned long long)p.matched, (unsigned long long)p.wake_attempts, 194 (unsigned long long)p.wakes_accepted); 195 CHECK(p.ready_inserts == 1, "blocked wave: %llu ready insertions with one slot", 196 (unsigned long long)p.ready_inserts); 197 CHECK(rx_world_wait_quiescent(&e.w, 5000) == RX_OK, "quiet"); 198 CHECK(e.w.stats.ready_inserts == 8, "all eight eventually ran (%llu)", 199 (unsigned long long)e.w.stats.ready_inserts); 200 rx_world_set_timing(&e.w, NULL, 0); 201 env_stop(&e); 202 } 203 204 static void t_timing_overflow(void) { 205 Env e; 206 CHECK(env_start(&e, 1) == 0, "setup"); 207 RxObjRef src = mkobj(&e, RES_SRC); 208 RxCapRef ext = mint(&e, SUBJ_EXTERNAL, RES_SRC, RX_RIGHT_WRITE); 209 RxCapRef rd = mint(&e, SUBJ_WORK, RES_SRC, RX_RIGHT_READ); 210 for (int i = 0; i < 10; i++) add_listener(&e, src, RX_FIELD(0), rd); 211 RxTiming buf[4]; 212 rx_world_set_timing(&e.w, buf, 4); 213 RxMutation m = { src, 0, 1 }; 214 rx_world_publish_external(&e.w, ext, &m, 1); 215 rx_world_wait_quiescent(&e.w, 5000); 216 uint64_t stored = 0, attempted = 0; 217 CHECK(rx_world_timing_status(&e.w, &stored, &attempted) == RX_ERR_FULL, 218 "an overflowing timing buffer was reported complete"); 219 CHECK(stored == 4 && attempted == 10, "stored %llu attempted %llu", 220 (unsigned long long)stored, (unsigned long long)attempted); 221 rx_world_set_timing(&e.w, NULL, 0); 222 env_stop(&e); 223 } 224 225 static void t_ring_bytes(void) { 226 Env e; 227 CHECK(env_start(&e, 1) == 0, "setup"); 228 RxObjRef obj = mkobj(&e, RES_SRC); 229 CHECK(rx_world_attach_physical(&e.w, obj) == RX_OK, "attach"); 230 RxCapRef cap = mint(&e, SUBJ_EXTERNAL, RES_SRC, RX_RIGHT_READ | RX_RIGHT_WRITE); 231 CHECK(rx_world_bind_capability(&e.w, obj, cap) == RX_OK, "bind"); 232 RxStats s0 = e.w.stats; 233 RxMutation m = { obj, 0, 5 }; 234 CHECK(rx_world_publish_external(&e.w, cap, &m, 1) > 0, "stimulus"); 235 CHECK(rx_world_wait_quiescent(&e.w, 3000) == RX_OK, "quiet"); 236 RxStats s1 = e.w.stats; 237 CHECK(s1.c2g_write_bytes - s0.c2g_write_bytes == sizeof(OmegaSharedWorldDesc), 238 "publication descriptor write not counted once (%llu)", 239 (unsigned long long)(s1.c2g_write_bytes - s0.c2g_write_bytes)); 240 CHECK(s1.proj_bytes > s0.proj_bytes, "projection write not counted"); 241 CHECK(s1.crumb_bytes - s0.crumb_bytes == sizeof(RxCrumb), "stimulus crumb not counted once"); 242 uint32_t fault = 0; 243 CHECK(rx_world_take_publication(&e.w, NULL, &fault) == RX_OK, "take publication"); 244 CHECK(e.w.stats.c2g_read_bytes - s1.c2g_read_bytes == sizeof(OmegaSharedWorldDesc), 245 "publication descriptor read not counted once"); 246 RxStats s2 = e.w.stats; 247 CHECK(rx_world_relocate_physical(&e.w, obj) == RX_OK, "relocate"); 248 CHECK(e.w.stats.window_move_bytes - s2.window_move_bytes == 2u * RX_OBJECT_WINDOW, 249 "window relocation not counted"); 250 env_stop(&e); 251 } 252 253 /* A long episode must not die of a full causal log: the long-episode 254 * capacity takes more records than the old 2^18 R14 log, records never 255 * move, and they verify. A deliberately small log still refuses a 256 * publication it cannot record (no action without evidence). */ 257 static void t_long_log(void) { 258 RxCapRoot root; 259 RxCapAdmin admin; 260 static RxWorld w; 261 CHECK(rx_caproot_start(&root, &admin) == RX_CAP_OK, "authority"); 262 CHECK(rx_world_init(&w, &root, 1, RX_CRUMBS_LONG_EPISODE) == RX_OK, "long-episode world"); 263 w.external_subject = SUBJ_EXTERNAL; 264 uint64_t init[RX_MAX_FIELDS] = {0}; 265 RxObjRef src = {UINT32_MAX, 0}; 266 rx_world_create(&w, 1, RX_PERSIST_RESIDENT, RES_SRC, init, &src); 267 RxCapMint mm; 268 memset(&mm, 0, sizeof mm); 269 mm.issuer = ISSUER; mm.subject = SUBJ_EXTERNAL; mm.resource = RES_SRC; 270 mm.rights = RX_RIGHT_WRITE; mm.parent = (RxCapRef){UINT32_MAX, 0}; 271 mm.authority = rx_capadmin_office(&admin); 272 RxCapRef ext = {UINT32_MAX, 0}; 273 rx_capadmin_mint(&admin, &mm, &ext); 274 RxMutation m = {src, 0, 1}; 275 CHECK(rx_world_publish_external(&w, ext, &m, 1) > 0, "first record"); 276 const RxCrumb *first = rx_world_crumb(&w, 1); 277 const uint64_t n = 600000; 278 int refused = 0; 279 for (uint64_t i = 2; i <= n; i++) { 280 m.value = i; 281 if (rx_world_publish_external(&w, ext, &m, 1) <= 0) { refused = 1; break; } 282 } 283 CHECK(!refused && w.stats.crumb_overflow == 0 && w.n_crumbs >= n, 284 "long log refused after %llu records", (unsigned long long)w.n_crumbs); 285 CHECK(rx_world_crumb(&w, 1) == first, "a record moved while the log grew"); 286 uint64_t checked = 0; 287 CHECK(rx_world_verify_crumbs(&w, &checked) == 0 && checked == w.n_crumbs, 288 "long log does not verify"); 289 rx_world_destroy(&w); 290 291 static RxWorld small; 292 CHECK(rx_world_init(&small, &root, 1, 16) == RX_OK, "small world"); 293 small.external_subject = SUBJ_EXTERNAL; 294 RxObjRef s2 = {UINT32_MAX, 0}; 295 rx_world_create(&small, 1, RX_PERSIST_RESIDENT, RES_SRC, init, &s2); 296 RxMutation m2 = {s2, 0, 1}; 297 int64_t last = 1; 298 for (int i = 0; i < 40 && last > 0; i++) { m2.value = (uint64_t)i + 2; last = rx_world_publish_external(&small, ext, &m2, 1); } 299 CHECK(last <= 0 && small.stats.crumb_overflow > 0, "a full log did not refuse the publication"); 300 rx_world_destroy(&small); 301 rx_caproot_stop(&root, &admin); 302 } 303 304 static int mkstore(char *dir, size_t n, const char *tag) { 305 snprintf(dir, n, "/tmp/rx_r15_instr_%ld_%s", (long)getpid(), tag); 306 return mkdir(dir, 0700); 307 } 308 309 static void t_store_io_scoped(void) { 310 char a_dir[128], b_dir[128]; 311 CHECK(mkstore(a_dir, sizeof a_dir, "a") == 0 && mkstore(b_dir, sizeof b_dir, "b") == 0, 312 "store dirs"); 313 uint64_t pb0 = 0, ps0 = 0; 314 rx_gen_io_counters(&pb0, &ps0); 315 RxGenStore *a = NULL, *b = NULL; 316 CHECK(rx_gen_open(a_dir, &a) == RX_GEN_OK, "open a"); 317 uint64_t ab = 0, as = 0; 318 CHECK(rx_gen_store_io(a, &ab, &as) == RX_GEN_OK, "store a io"); 319 CHECK(ab > 0 && as > 0, "opening a store wrote nothing (%llu bytes, %llu syncs)", 320 (unsigned long long)ab, (unsigned long long)as); 321 CHECK(rx_gen_open(b_dir, &b) == RX_GEN_OK, "open b"); 322 uint64_t ab2 = 0, as2 = 0, bb = 0, bs = 0, pb1 = 0, ps1 = 0; 323 rx_gen_store_io(a, &ab2, &as2); 324 rx_gen_store_io(b, &bb, &bs); 325 rx_gen_io_counters(&pb1, &ps1); 326 CHECK(ab2 == ab && as2 == as, "store b's writes were charged to store a"); 327 CHECK(pb1 - pb0 == ab + bb && ps1 - ps0 == as + bs, 328 "process total differs from the sum of the two stores"); 329 rx_gen_close(a); 330 rx_gen_close(b); 331 char cmd[300]; 332 snprintf(cmd, sizeof cmd, "rm -rf '%s' '%s'", a_dir, b_dir); 333 if (system(cmd) != 0) fprintf(stderr, "note: could not remove %s\n", a_dir); 334 } 335 336 /* ---- SEQ: a stale plan cannot declare the world quiescent ---------------- 337 * The SEQ orchestrator rebuilds its plan when n_reactions changes, but it 338 * checks that outside the pulse. A reaction registered and stimulated in 339 * between is not in the plan the next pulse walks; that pulse found nothing 340 * ready and used to declare the world idle, so rx_world_wait_quiescent 341 * returned before the new reaction ever ran (the R15 SEQ setup failure at 342 * stage 5, where living.experiment.ask is added and then awaited). */ 343 static int seq_validate(const void *ctx, RxCapRef ref, uint32_t subject, uint64_t resource, 344 uint32_t rights, RxCapEntry *out) { 345 return rx_caproot_validate(ctx, ref, subject, resource, rights, out); 346 } 347 348 static int seq_inspect(const void *ctx, RxCapRef ref, RxCapEntry *out) { 349 return rx_caproot_inspect(ctx, ref, out); 350 } 351 352 static void t_seq_stale_plan(void) { 353 Env e; 354 memset(&e, 0, sizeof e); 355 CHECK(rx_caproot_start(&e.root, &e.admin) == RX_CAP_OK, "authority"); 356 CHECK(rx_world_init_sequential_reference(&e.w, &e.root, seq_validate, seq_inspect, 357 1u << 16) == RX_OK, "sequential world"); 358 e.w.external_subject = SUBJ_EXTERNAL; 359 RxObjRef a_src = mkobj(&e, RES_SRC), b_src = mkobj(&e, RES_SRC); 360 RxCapRef ext = mint(&e, SUBJ_EXTERNAL, RES_SRC, RX_RIGHT_WRITE); 361 RxCapRef rd = mint(&e, SUBJ_WORK, RES_SRC, RX_RIGHT_READ); 362 uint32_t a = add_listener(&e, a_src, RX_FIELD(0), rd); 363 uint32_t order[2] = { a, UINT32_MAX }; 364 RxSeqPlan plan = { order, 1, NULL, NULL, 0 }; 365 uint32_t ran = 99; 366 CHECK(rx_seq_pulse(&e.w, &plan, &ran) == RX_OK && ran == 0, "first pulse ran %u", ran); 367 CHECK(e.w.seq_idle_start == e.w.seq_pulse_started, "a full plan with nothing ready is idle"); 368 369 /* Registered and stimulated after the plan was built. */ 370 uint32_t b = add_listener(&e, b_src, RX_FIELD(0), rd); 371 RxMutation m = { b_src, 0, 7 }; 372 CHECK(rx_world_publish_external(&e.w, ext, &m, 1) > 0, "stimulus"); 373 CHECK(rx_seq_pulse(&e.w, &plan, &ran) == RX_OK && ran == 0, "stale pulse ran %u", ran); 374 CHECK(e.w.seq_idle_start != e.w.seq_pulse_started, 375 "a pulse that missed reaction %u declared the world quiescent", b); 376 377 order[1] = b; 378 plan.n = 2; 379 uint64_t before = e.w.reactions[b].activations; 380 CHECK(rx_seq_pulse(&e.w, &plan, &ran) == RX_OK && ran == 1, "fresh pulse ran %u", ran); 381 CHECK(e.w.reactions[b].activations == before + 1, "the late reaction did not run"); 382 CHECK(rx_seq_pulse(&e.w, &plan, &ran) == RX_OK && ran == 0 && 383 e.w.seq_idle_start == e.w.seq_pulse_started, "the full plan is idle again"); 384 CHECK(rx_world_wait_quiescent(&e.w, 100) == RX_ERR_TIMEOUT, 385 "quiescence still needs a pulse that began after the wait"); 386 env_stop(&e); 387 } 388 389 int main(void) { 390 static const uint32_t fanouts[] = { 1, 2, 4, 8, 16, 32, 64, 128, 256 }; 391 for (size_t i = 0; i < sizeof fanouts / sizeof fanouts[0]; i++) 392 t_propagation_and_scheduler(fanouts[i]); 393 t_blocked_wave(); 394 t_timing_overflow(); 395 t_ring_bytes(); 396 t_store_io_scoped(); 397 t_long_log(); 398 t_seq_stale_plan(); 399 printf("R15 instrumentation: %d checks, %d failures\n", g_checks, g_failures); 400 return g_failures ? 1 : 0; 401 }