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_perf.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_perf.c` 1416 lines · 57542 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_perf.c -- the R15 performance harness (spec/r15-performance-proof.md). 19 * 20 * One process = one observation unit. It measures and records; it decides 21 * nothing. Every number is written raw to a JSON Lines file (§12) and the 22 * reducer (tools/r15_reduce.c) computes every summary and gate from it. 23 * 24 * rx_r15_perf trial 25 * one W-EPISODE trial (§4): IDLE, BEFORE, ADAPT, AFTER windows. 26 * rx_r15_perf l1 27 * Level 1 substrate measures (§5, C1). W0/W1 = W-PROD with the timing 28 * buffer off/on (C1 item 6, instrumentation overhead). 29 * rx_r15_perf l2 30 * Level 2 CPU/GPU cooperation through the resident seat (§5). 31 * 32 * The configuration name recorded is RES-1-NODIGEST when this binary was 33 * built with -DRX_MEASURE_NO_CAUSAL_DIGEST (spec §3). Exit status 0 means the 34 * observation completed and its own correctness checks held; anything else 35 * is a failed trial (§10: it is recorded, never rerun away). 36 */ 37 #include "rx_r15_rig.h" 38 #include "r15_measure.h" 39 #include "r15_seat_diag.h" 40 #include "runtime/rx_generation.h" 41 42 #include 43 #include 44 #include 45 #include 46 #include 47 #include 48 #include 49 #include 50 51 enum { EXTERNAL = 100, ISSUER = 3, L1_SUBJ = 2 }; 52 53 #define WARM_NS 2000000000ull 54 #define WINDOW_NS 5000000000ull 55 #define IDLE_NS 5000000000ull 56 57 static R15Out g_out; 58 static R15Energy g_energy; 59 static R15Pmu g_pmu; 60 static int g_pmu_ok; 61 static char g_config[24]; 62 63 static void sleep_ns(uint64_t ns) { 64 struct timespec ts = {(time_t)(ns / 1000000000ull), (long)(ns % 1000000000ull)}; 65 while (nanosleep(&ts, &ts) != 0) {} 66 } 67 68 static void rec_str(const char *k, const char *v) { 69 fprintf(g_out.f, ",\"%s\":\"", k); 70 for (const char *p = v; p && *p; p++) { 71 if (*p == '"' || *p == '\\') fputc('\\', g_out.f); 72 if ((unsigned char)*p >= 0x20) fputc(*p, g_out.f); 73 } 74 fputc('"', g_out.f); 75 } 76 static void rec_u(const char *k, uint64_t v) { fprintf(g_out.f, ",\"%s\":%" PRIu64, k, v); } 77 static void rec_i(const char *k, int64_t v) { fprintf(g_out.f, ",\"%s\":%" PRId64, k, v); } 78 static void rec_arr(const char *k, const uint64_t *v, uint64_t n) { 79 fprintf(g_out.f, ",\"%s\":[", k); 80 for (uint64_t i = 0; i < n; i++) fprintf(g_out.f, "%s%" PRIu64, i ? "," : "", v[i]); 81 fputc(']', g_out.f); 82 } 83 84 /* ---- machine state (§9, §13) ------------------------------------------- */ 85 86 static int max_temp_mc(void) { 87 int hot = 0; 88 for (int z = 0; z < 64; z++) { 89 char p[96]; 90 snprintf(p, sizeof p, "/sys/class/thermal/thermal_zone%d/temp", z); 91 FILE *f = fopen(p, "r"); 92 if (!f) break; 93 int t = 0; 94 if (fscanf(f, "%d", &t) == 1 && t > hot) hot = t; 95 fclose(f); 96 } 97 return hot; 98 } 99 100 static void rec_machine(const char *when) { 101 r15_rec_begin(&g_out, "machine"); 102 rec_str("when", when); 103 fputs(",\"thermal_mc\":[", g_out.f); 104 for (int z = 0; z < 64; z++) { 105 char p[96]; 106 snprintf(p, sizeof p, "/sys/class/thermal/thermal_zone%d/temp", z); 107 FILE *f = fopen(p, "r"); 108 if (!f) break; 109 int t = 0; 110 if (fscanf(f, "%d", &t) != 1) t = -1; 111 fclose(f); 112 fprintf(g_out.f, "%s%d", z ? "," : "", t); 113 } 114 fputs("],\"cur_khz\":[", g_out.f); 115 for (int c = 0; c < 256; c++) { 116 char p[96]; 117 snprintf(p, sizeof p, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", c); 118 FILE *f = fopen(p, "r"); 119 if (!f) break; 120 long k = 0; 121 if (fscanf(f, "%ld", &k) != 1) k = -1; 122 fclose(f); 123 fprintf(g_out.f, "%s%ld", c ? "," : "", k); 124 } 125 fputc(']', g_out.f); 126 double la[3] = {0}; 127 if (getloadavg(la, 3) == 3) fprintf(g_out.f, ",\"loadavg\":[%.2f,%.2f,%.2f]", la[0], la[1], la[2]); 128 rec_i("causal_digest", rx_world_causal_digest_enabled); 129 r15_rec_end(&g_out); 130 } 131 132 /* §9: wait up to 120 s for every zone <= 55 C; run and flag otherwise. */ 133 static void thermal_wait(void) { 134 uint64_t t0 = r15_now_ns(); 135 int hot; 136 while ((hot = max_temp_mc()) > 55000 && r15_now_ns() - t0 < 120000000000ull) sleep_ns(1000000000ull); 137 r15_rec_begin(&g_out, "thermal_wait"); 138 rec_u("waited_ns", r15_now_ns() - t0); 139 rec_i("max_mc", hot); 140 rec_i("flag_hot", hot > 55000); 141 r15_rec_end(&g_out); 142 } 143 144 /* ---- GPU residency and R5 slot sampler (1 ms; §6.10, §6.11) ------------- */ 145 146 /* One accumulator per open window (windows may overlap), plus whole-process 147 * residency totals that are never reset. */ 148 typedef struct { 149 uint64_t samples, live, slot_sum, slot_max, inflight_sum, inflight_max, claims_max; 150 SeatDiag d; /* sampler lateness and not-live runs (r15_seat_diag.h); does not feed G15 */ 151 } SampSnap; 152 #define SAMP_ACC 4 153 154 typedef struct { 155 pthread_t t; 156 volatile int stop, armed; 157 RxWorld *volatile w; 158 volatile int silicon; 159 pthread_mutex_t mu; 160 uint64_t last, have_last; 161 SampSnap acc[SAMP_ACC]; 162 int active[SAMP_ACC]; 163 SampSnap total; 164 } Sampler; 165 static Sampler g_samp; 166 167 static void *sampler_main(void *arg) { 168 Sampler *s = arg; 169 while (!s->stop) { 170 struct timespec ts = {0, 1000000L}; 171 nanosleep(&ts, NULL); 172 if (!s->armed || !s->w) continue; 173 RxWorld *w = s->w; 174 uint64_t slots = __atomic_load_n(&w->used_slots, __ATOMIC_RELAXED); 175 uint64_t infl = __atomic_load_n(&w->in_flight, __ATOMIC_RELAXED); 176 uint64_t open = __atomic_load_n(&w->stats.resident_claims, __ATOMIC_RELAXED) - 177 __atomic_load_n(&w->stats.resident_closed, __ATOMIC_RELAXED); 178 uint64_t hb = 0; 179 if (s->silicon && w->coherent) 180 hb = __atomic_load_n((uint32_t *)(w->coherent + rx_world_off_heartbeat() + RX_SEAT_HB_LIVE), 181 __ATOMIC_ACQUIRE); 182 /* live = the seat's counter moved since the previous 1 ms sample */ 183 int live = s->silicon && s->have_last && hb != s->last; 184 int counted = s->have_last; 185 s->last = hb; 186 s->have_last = 1; 187 uint64_t t_now = r15_now_ns(); 188 if (!counted) continue; 189 pthread_mutex_lock(&s->mu); 190 for (int i = 0; i <= SAMP_ACC; i++) { 191 SampSnap *a = i == SAMP_ACC ? &s->total : &s->acc[i]; 192 if (i < SAMP_ACC && !s->active[i]) continue; 193 a->samples++; 194 a->live += (uint64_t)live; 195 seat_diag_sample(&a->d, t_now, live, (uint32_t)hb); 196 a->slot_sum += slots; 197 if (slots > a->slot_max) a->slot_max = slots; 198 a->inflight_sum += infl; 199 if (infl > a->inflight_max) a->inflight_max = infl; 200 if (open > a->claims_max) a->claims_max = open; 201 } 202 pthread_mutex_unlock(&s->mu); 203 } 204 return NULL; 205 } 206 207 static int samp_open(void) { 208 pthread_mutex_lock(&g_samp.mu); 209 int k = -1; 210 for (int i = 0; i < SAMP_ACC && k < 0; i++) 211 if (!g_samp.active[i]) { k = i; memset(&g_samp.acc[i], 0, sizeof g_samp.acc[i]); g_samp.active[i] = 1; } 212 pthread_mutex_unlock(&g_samp.mu); 213 return k; 214 } 215 216 static SampSnap samp_close(int k) { 217 SampSnap x; 218 memset(&x, 0, sizeof x); 219 if (k < 0) return x; 220 pthread_mutex_lock(&g_samp.mu); 221 x = g_samp.acc[k]; 222 g_samp.active[k] = 0; 223 pthread_mutex_unlock(&g_samp.mu); 224 return x; 225 } 226 227 static void rec_samp(const char *key, const SampSnap *x) { 228 fprintf(g_out.f, ",\"%s\":{\"silicon\":%d,\"intervals\":%" PRIu64 ",\"seat_live\":%" PRIu64 229 ",\"slot_sum\":%" PRIu64 ",\"slot_max\":%" PRIu64 ",\"inflight_sum\":%" PRIu64 230 ",\"inflight_max\":%" PRIu64 ",\"claims_max\":%" PRIu64 231 ",\"diag\":{\"late_2ms\":%" PRIu64 ",\"late_10ms\":%" PRIu64 ",\"max_gap_ns\":%" PRIu64 232 ",\"dead_runs\":%" PRIu64 ",\"dead_longest\":%" PRIu64 ",\"dead_at_end\":%" PRIu64 233 ",\"t_first\":%" PRIu64 ",\"t_last\":%" PRIu64 ",\"first_dead_t\":%" PRIu64 234 ",\"last_live_t\":%" PRIu64 ",\"stale_max_ns\":%" PRIu64 "}}", 235 key, g_samp.silicon, x->samples, x->live, x->slot_sum, x->slot_max, x->inflight_sum, 236 x->inflight_max, x->claims_max, x->d.late_2ms, x->d.late_10ms, x->d.max_gap_ns, x->d.dead_runs, 237 x->d.dead_longest, x->d.dead_cur, x->d.t_first, x->d.last_t, x->d.first_dead_t, x->d.last_live_t, 238 x->d.stale_max_ns); 239 } 240 241 static void rec_residency(void) { 242 pthread_mutex_lock(&g_samp.mu); 243 SampSnap t = g_samp.total; 244 pthread_mutex_unlock(&g_samp.mu); 245 r15_rec_begin(&g_out, "residency"); 246 rec_samp("total", &t); 247 r15_rec_end(&g_out); 248 } 249 250 /* Observers are created before the PMU is opened, so their own work is not 251 * counted as the body's (C1 item 8: inherit covers threads created later). */ 252 static void observers_start(void) { 253 char why[200] = ""; 254 int e = r15_energy_open(&g_energy, why, sizeof why); 255 r15_energy_start_sampler(&g_energy, 64000); 256 pthread_mutex_init(&g_samp.mu, NULL); 257 #ifdef R15_SILICON 258 g_samp.silicon = 1; 259 #endif 260 pthread_create(&g_samp.t, NULL, sampler_main, &g_samp); 261 char pwhy[200] = ""; 262 g_pmu_ok = r15_pmu_open(&g_pmu, pwhy, sizeof pwhy) == 0; 263 r15_rec_begin(&g_out, "observers"); 264 rec_i("spbm", e == 0); 265 rec_str("spbm_why", why); 266 rec_str("hwmon", g_energy.hwmon); 267 rec_i("nvml", g_energy.nvml_dev != NULL); 268 rec_i("pmu", g_pmu_ok); 269 rec_str("pmu_why", pwhy); 270 rec_i("silicon", g_samp.silicon); 271 FILE *f = fopen("/proc/sys/kernel/perf_event_paranoid", "r"); 272 int par = -99; 273 if (f) { if (fscanf(f, "%d", &par) != 1) par = -99; fclose(f); } 274 rec_i("perf_event_paranoid", par); 275 r15_rec_end(&g_out); 276 } 277 278 static void observers_stop(uint64_t t0) { 279 g_samp.stop = 1; 280 pthread_join(g_samp.t, NULL); 281 /* 10 Hz telemetry over the whole process (§7) */ 282 static R15EnergySample tel[64000]; 283 uint64_t n = r15_energy_samples(&g_energy, t0, UINT64_MAX, tel, 64000); 284 for (uint64_t i = 0; i < n; i++) { 285 r15_rec_begin(&g_out, "telemetry"); 286 r15_json_energy(g_out.f, "e", &tel[i]); 287 r15_rec_end(&g_out); 288 } 289 r15_energy_close(&g_energy); 290 if (g_pmu_ok) r15_pmu_close(&g_pmu); 291 } 292 293 /* ---- live windows ------------------------------------------------------- */ 294 295 #define STAT_FIELDS(X) \ 296 X(activations) X(externals) X(wakes) X(wakes_accepted) X(ready_inserts) \ 297 X(subscriptions_checked) X(subscriptions_matched) X(coalesced_wakes) X(commits) \ 298 X(invalidations) X(rejected) X(noops) X(failed) X(blocked_authority) X(blocked_resource) \ 299 X(quarantines) X(suppressed_wakes) X(deferred_wakes) X(snapshot_bytes) X(stage_bytes) \ 300 X(crumb_bytes) X(proj_bytes) X(c2g_write_bytes) X(c2g_read_bytes) X(g2c_write_bytes) \ 301 X(g2c_read_bytes) X(window_move_bytes) X(setup_copy_bytes) X(sched_wall_ns) \ 302 X(sched_cpu_ns) X(gpu_results_taken) X(gpu_polls_empty) X(gpu_host_waits) X(seq_pulses) \ 303 X(seq_polls) X(seq_runs) X(resident_claims) X(resident_closed) X(crumb_overflow) \ 304 X(illegal_transitions) X(desc_published) 305 306 typedef struct { 307 uint64_t t0, t1, served0, served1; 308 RxStats s0, s1; 309 struct rusage ru0, ru1; 310 R15EnergySample e0, e1; 311 R15PmuRead pmu; 312 int pmu_used; 313 uint64_t gio_b0, gio_s0, gio_b1, gio_s1, pio_b0, pio_s0, pio_b1, pio_s1; 314 char threads0[4096], threads1[4096]; 315 int acc; 316 SampSnap samp; 317 } Win; 318 319 static RxStats stats_of(RxWorld *w) { 320 pthread_mutex_lock(&w->mu); 321 RxStats s = w->stats; 322 pthread_mutex_unlock(&w->mu); 323 return s; 324 } 325 326 /* Per-thread CPU (utime+stime, clock ticks) and name, "tid:comm:ticks;..." */ 327 static void thread_cpu(char *buf, size_t n) { 328 buf[0] = 0; 329 size_t at = 0; 330 DIR *d = opendir("/proc/self/task"); 331 if (!d) return; 332 struct dirent *e; 333 while ((e = readdir(d)) && at + 64 < n) { 334 if (e->d_name[0] == '.') continue; 335 char p[128], line[512]; 336 snprintf(p, sizeof p, "/proc/self/task/%.20s/stat", e->d_name); 337 FILE *f = fopen(p, "r"); 338 if (!f) continue; 339 size_t got = fread(line, 1, sizeof line - 1, f); 340 fclose(f); 341 line[got] = 0; 342 char *l = strchr(line, '('), *r = strrchr(line, ')'); 343 if (!l || !r) continue; 344 *r = 0; 345 unsigned long ut = 0, st = 0; 346 if (sscanf(r + 2, "%*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu %lu", &ut, &st) != 2) continue; 347 for (char *c = l + 1; *c; c++) if (*c == ';' || *c == ':' || *c == '"' || *c == '\\') *c = '_'; 348 at += (size_t)snprintf(buf + at, n - at, "%s:%.15s:%lu;", e->d_name, l + 1, ut + st); 349 } 350 closedir(d); 351 } 352 353 static void win_begin(Win *x, RxWorld *w, R15Rig *r, int pmu) { 354 memset(x, 0, sizeof *x); 355 thread_cpu(x->threads0, sizeof x->threads0); 356 if (r) rx_gen_store_io(r->gen, &x->gio_b0, &x->gio_s0); 357 rx_gen_io_counters(&x->pio_b0, &x->pio_s0); 358 x->served0 = r ? __atomic_load_n(&r->served, __ATOMIC_RELAXED) : 0; 359 x->s0 = stats_of(w); 360 getrusage(RUSAGE_SELF, &x->ru0); 361 x->acc = samp_open(); 362 r15_energy_read(&g_energy, &x->e0); 363 x->pmu_used = pmu && g_pmu_ok; 364 x->t0 = r15_now_ns(); 365 if (x->pmu_used) r15_pmu_start(&g_pmu); 366 } 367 368 static void win_end(Win *x, RxWorld *w, R15Rig *r) { 369 if (x->pmu_used) r15_pmu_stop(&g_pmu, &x->pmu); 370 x->t1 = r15_now_ns(); 371 x->samp = samp_close(x->acc); 372 r15_energy_read(&g_energy, &x->e1); 373 getrusage(RUSAGE_SELF, &x->ru1); 374 x->s1 = stats_of(w); 375 x->served1 = r ? __atomic_load_n(&r->served, __ATOMIC_RELAXED) : 0; 376 rx_gen_io_counters(&x->pio_b1, &x->pio_s1); 377 if (r) rx_gen_store_io(r->gen, &x->gio_b1, &x->gio_s1); 378 thread_cpu(x->threads1, sizeof x->threads1); 379 } 380 381 static uint64_t tv_ns(struct timeval t) { return (uint64_t)t.tv_sec * 1000000000ull + (uint64_t)t.tv_usec * 1000ull; } 382 383 static void rec_window(const char *name, const Win *x) { 384 r15_rec_begin(&g_out, "window"); 385 rec_str("window", name); 386 rec_u("t0", x->t0); 387 rec_u("t1", x->t1); 388 rec_u("wall_ns", x->t1 - x->t0); 389 rec_u("ops", x->served1 - x->served0); 390 rec_u("cpu_user_ns", tv_ns(x->ru1.ru_utime) - tv_ns(x->ru0.ru_utime)); 391 rec_u("cpu_sys_ns", tv_ns(x->ru1.ru_stime) - tv_ns(x->ru0.ru_stime)); 392 fputs(",\"stats\":{", g_out.f); 393 int first = 1; 394 #define X(fld) fprintf(g_out.f, "%s\"" #fld "\":%" PRIu64, first ? "" : ",", x->s1.fld - x->s0.fld); first = 0; 395 STAT_FIELDS(X) 396 #undef X 397 fputc('}', g_out.f); 398 r15_json_energy(g_out.f, "e0", &x->e0); 399 r15_json_energy(g_out.f, "e1", &x->e1); 400 if (x->pmu_used) r15_json_pmu(g_out.f, "pmu", &g_pmu, &x->pmu); 401 rec_u("gen_store_bytes", x->gio_b1 - x->gio_b0); 402 rec_u("gen_store_syncs", x->gio_s1 - x->gio_s0); 403 rec_u("gen_process_bytes", x->pio_b1 - x->pio_b0); 404 rec_u("gen_process_syncs", x->pio_s1 - x->pio_s0); 405 rec_str("threads0", x->threads0); 406 rec_str("threads1", x->threads1); 407 rec_samp("sampler", &x->samp); 408 r15_rec_end(&g_out); 409 } 410 411 /* ---- crumb-derived windows (§5 L3, §6.2, §6.12-15) ---------------------- */ 412 413 typedef struct { 414 uint64_t t0, t1; 415 uint64_t act, kind[16], replays, serve_commits, externals, consequential; 416 uint64_t cost_ns; /* Σ(t_end - t_start), non-production activations */ 417 R15Hist lat; /* activation latency: t_start - cause t_end */ 418 } CWin; 419 420 static int writes_obj(const RxCrumb *k, uint32_t id) { 421 for (uint32_t i = 0; i < k->n_outputs; i++) if (k->outputs[i].obj.id == id) return 1; 422 return 0; 423 } 424 425 /* Crumbs whose activation started in [t0, t1]. cause_has_commit[e] = 1 when 426 * some reaction commit belongs to causal episode e. */ 427 static void cwin_scan(RxWorld *w, uint32_t r_serve, const uint8_t *ep_commit, uint64_t n, CWin *c) { 428 static uint64_t last_inv[RX_MAX_REACTIONS]; 429 memset(last_inv, 0, sizeof last_inv); 430 /* n: the crumb count ep_commit was sized for */ 431 for (uint64_t id = 1; id <= n; id++) { 432 const RxCrumb *k = rx_world_crumb(w, id); 433 if (!k) continue; 434 if (k->kind == RX_CRUMB_INVALIDATED && k->reaction < RX_MAX_REACTIONS) 435 last_inv[k->reaction] = k->wake_cause; 436 if (k->t_start_ns < c->t0 || k->t_start_ns > c->t1) continue; 437 if (k->kind == RX_CRUMB_EXTERNAL) { 438 c->externals++; 439 if (ep_commit[id]) c->consequential++; 440 continue; 441 } 442 if (k->reaction == UINT32_MAX || k->kind == RX_CRUMB_CREATE || k->kind == RX_CRUMB_RETIRE) continue; 443 c->act++; 444 if ((unsigned)k->kind < 16) c->kind[k->kind]++; 445 if (k->kind != RX_CRUMB_INVALIDATED && k->reaction < RX_MAX_REACTIONS && 446 last_inv[k->reaction] && last_inv[k->reaction] == k->wake_cause) { 447 c->replays++; 448 last_inv[k->reaction] = 0; 449 } 450 if (k->reaction == r_serve) { 451 if (k->kind == RX_CRUMB_COMMIT) c->serve_commits++; 452 } else { 453 c->cost_ns += k->t_end_ns - k->t_start_ns; 454 } 455 const RxCrumb *cause = k->wake_cause ? rx_world_crumb(w, k->wake_cause) : NULL; 456 if (cause && k->t_start_ns >= cause->t_end_ns) r15_hist_add(&c->lat, k->t_start_ns - cause->t_end_ns); 457 } 458 } 459 460 static void rec_cwin(const char *name, const CWin *c) { 461 r15_rec_begin(&g_out, "cwindow"); 462 rec_str("window", name); 463 rec_u("t0", c->t0); 464 rec_u("t1", c->t1); 465 rec_u("activations", c->act); 466 fprintf(g_out.f, ",\"kinds\":{\"commit\":%" PRIu64 ",\"invalidated\":%" PRIu64 467 ",\"blocked_authority\":%" PRIu64 ",\"rejected\":%" PRIu64 ",\"failed\":%" PRIu64 468 ",\"noop\":%" PRIu64 ",\"quarantine\":%" PRIu64 "}", 469 c->kind[RX_CRUMB_COMMIT], c->kind[RX_CRUMB_INVALIDATED], c->kind[RX_CRUMB_BLOCKED_AUTHORITY], 470 c->kind[RX_CRUMB_REJECTED], c->kind[RX_CRUMB_FAILED], c->kind[RX_CRUMB_NOOP], 471 c->kind[RX_CRUMB_QUARANTINE]); 472 rec_u("replays", c->replays); 473 rec_u("serve_commits", c->serve_commits); 474 rec_u("externals", c->externals); 475 rec_u("consequential", c->consequential); 476 rec_u("adapt_cost_ns", c->cost_ns); 477 fputs(",\"latency\":", g_out.f); 478 r15_hist_json(g_out.f, &c->lat); 479 r15_rec_end(&g_out); 480 } 481 482 /* ---- trial (§4) ----------------------------------------------------------- */ 483 484 typedef struct { Win idle, before, span, after; int idle_ok, before_ok, after_ok; uint64_t t_met; } TrialWins; 485 486 static int hook_idle(R15Hooks *h, R15Rig *r) { 487 TrialWins *t = h->ctx; 488 if (rx_world_wait_quiescent(&r->w, 10000) != RX_OK) return -1; 489 win_begin(&t->idle, &r->w, r, 1); 490 sleep_ns(IDLE_NS); 491 win_end(&t->idle, &r->w, r); 492 t->idle_ok = 1; 493 return 0; 494 } 495 496 static int hook_before(R15Hooks *h, R15Rig *r) { 497 TrialWins *t = h->ctx; 498 sleep_ns(WARM_NS); 499 win_begin(&t->before, &r->w, r, 1); 500 sleep_ns(WINDOW_NS); 501 win_end(&t->before, &r->w, r); 502 t->before_ok = 1; 503 win_begin(&t->span, &r->w, r, 0); /* goal .. end of AFTER (G12) */ 504 return 0; 505 } 506 507 static int hook_after(R15Hooks *h, R15Rig *r) { 508 TrialWins *t = h->ctx; 509 t->t_met = r15_now_ns(); /* goal MET observed on the in-force record */ 510 sleep_ns(WARM_NS); 511 win_begin(&t->after, &r->w, r, 1); 512 sleep_ns(WINDOW_NS); 513 win_end(&t->after, &r->w, r); 514 win_end(&t->span, &r->w, r); 515 t->after_ok = 1; 516 return 0; 517 } 518 519 /* First crumb after `from` by reaction `rx` (UINT32_MAX = any) that commits 520 * a write to object `obj` (UINT32_MAX = any). */ 521 static const RxCrumb *find_commit(RxWorld *w, uint64_t from, uint32_t rx, uint32_t obj) { 522 for (uint64_t id = from + 1; id <= w->n_crumbs; id++) { 523 const RxCrumb *k = rx_world_crumb(w, id); 524 if (!k || k->kind != RX_CRUMB_COMMIT) continue; 525 if (rx != UINT32_MAX && k->reaction != rx) continue; 526 if (obj != UINT32_MAX && !writes_obj(k, obj)) continue; 527 return k; 528 } 529 return NULL; 530 } 531 532 static uint32_t reaction_named(RxWorld *w, const char *name) { 533 for (uint32_t i = 0; i < w->n_reactions; i++) 534 if (w->reactions[i].desc.name && strcmp(w->reactions[i].desc.name, name) == 0) return i; 535 return UINT32_MAX; 536 } 537 538 static void rec_outcome(const R15Outcome *o, int rc) { 539 r15_rec_begin(&g_out, "episode"); 540 rec_i("rc", rc); 541 rec_i("ok", o->ok); 542 rec_str("why", o->why); 543 #define U(f) rec_u(#f, o->f); 544 U(target_ns) U(goal_seq) U(goal_regime) U(goal_status) U(goal_status_final) U(goal_class) U(plan_action) 545 U(plan_regime) U(plan_condition) U(plan_reason) U(plan_goal) U(search_epoch) 546 U(selection_epoch) U(selection_regime) U(selected_verdict) U(gen_before) U(gen_after) 547 U(lineage_before) U(lineage_after) U(inforce_epoch) U(inforce_regime) U(inforce_generation) 548 U(candidate_id) U(promotion_candidate) U(promotion_active) U(aegis_woken) U(root_woken) 549 U(gpu_claims) U(seat_commits) U(crumbs_checked) U(crumb_overflow) U(illegal) U(wrong) 550 U(unpromoted_use) U(lost_triggers) U(incumbent_ns) U(selected_ps) U(reference_ps) 551 U(inforce_ps) U(final_expected_ns) U(plan_seq) U(served) U(crumbs) 552 #undef U 553 rec_i("promote_result", o->promote_result); 554 rec_i("crumbs_verified", o->crumbs_verified); 555 rec_arr("selection_id", o->selection_id, 4); 556 rec_arr("inforce_id", o->inforce_id, 4); 557 rec_arr("evidence", o->evidence, 5); 558 rec_arr("belief", o->belief, 4); 559 rec_arr("slot_verdict", o->slot_verdict, RX_OMEGA_SLOTS); 560 rec_arr("slot_measure", o->slot_measure, RX_OMEGA_SLOTS); 561 rec_arr("slot_cps", o->slot_cps, RX_OMEGA_SLOTS); 562 rec_arr("slot_rps", o->slot_rps, RX_OMEGA_SLOTS); 563 r15_rec_end(&g_out); 564 } 565 566 /* Diagnostic only (never part of the raw evidence): with R15_BARRIER_TRACE 567 * set to a file name, write every crumb around the trial's generation barrier 568 * and the barrier's inner phases, one line each. */ 569 static void barrier_trace(RxWorld *w, const RxGenPhases *ph) { 570 const char *path = getenv("R15_BARRIER_TRACE"); 571 if (!path || !ph->enter_ns) return; 572 FILE *f = fopen(path, "a"); 573 if (!f) return; 574 uint64_t lo = ph->enter_ns - 20000000ull; 575 uint64_t hi = (ph->receipt_ns ? ph->receipt_ns : ph->enter_ns) + 20000000ull; 576 fprintf(f, "phase enter %" PRIu64 " barrier %" PRIu64 " verified %" PRIu64 " blobs %" PRIu64 577 " candidate %" PRIu64 " reachable %" PRIu64 " flip %" PRIu64 " flipped %" PRIu64 578 " receipt_file %" PRIu64 " event %" PRIu64 " receipt %" PRIu64 " result %d\n", 579 ph->enter_ns, ph->barrier_ns, ph->verified_ns, ph->blobs_ns, ph->candidate_ns, 580 ph->reachable_ns, ph->flip_ns, ph->flipped_ns, ph->receipt_file_ns, ph->event_ns, 581 ph->receipt_ns, ph->result); 582 for (uint64_t id = 1; id <= w->n_crumbs; id++) { 583 const RxCrumb *k = rx_world_crumb(w, id); 584 if (!k || k->t_end_ns < lo || k->t_start_ns > hi) continue; 585 const char *name = k->reaction < w->n_reactions ? w->reactions[k->reaction].desc.name 586 : "(outside)"; 587 fprintf(f, "crumb %" PRIu64 " kind %d worker %u start %" PRIu64 " end %" PRIu64 " %s\n", 588 k->id, (int)k->kind, k->worker, k->t_start_ns, k->t_end_ns, name); 589 } 590 fclose(f); 591 } 592 593 static int run_trial(R15Config cfg) { 594 thermal_wait(); 595 rec_machine("before"); 596 uint64_t t_proc = r15_now_ns(); 597 observers_start(); 598 static R15Rig rig; 599 R15Rig *r = &rig; 600 TrialWins tw; 601 memset(&tw, 0, sizeof tw); 602 R15Hooks hooks = {hook_idle, hook_before, hook_after, &tw}; 603 if (r15_start(r, cfg) != 0) { 604 r15_rec_begin(&g_out, "error"); 605 rec_str("what", "build"); 606 rec_i("stage", r->stage); 607 rec_str("stage_why", r->stage_why ? r->stage_why : ""); 608 r15_rec_end(&g_out); 609 observers_stop(t_proc); 610 return 2; 611 } 612 g_samp.w = &r->w; 613 g_samp.armed = 1; 614 r->hooks = &hooks; 615 R15Outcome out; 616 uint64_t t_ep = r15_now_ns(); 617 int rc = r15_episode(r, 0, &out); 618 uint64_t t_done = r15_now_ns(); 619 /* A failed episode returns with production still running: stop it and let 620 * the body go quiet so the crumb log no longer grows under the analysis. */ 621 r15_producer_stop(r); 622 rx_world_wait_quiescent(&r->w, 10000); 623 rec_outcome(&out, rc); 624 if (tw.idle_ok) rec_window("IDLE", &tw.idle); 625 if (tw.before_ok) rec_window("BEFORE", &tw.before); 626 if (tw.after_ok) { rec_window("AFTER", &tw.after); rec_window("ADAPT_AFTER", &tw.span); } 627 628 /* crumb-derived ADAPT (goal crumb .. promotion crumb) and L3 intervals */ 629 RxWorld *w = &r->w; 630 uint32_t r_serve = r->omega.r_serve; 631 const uint64_t n_crumbs = w->n_crumbs; /* one snapshot bounds every scan below */ 632 uint8_t *ep = calloc(n_crumbs + 2, 1); 633 for (uint64_t id = 1; ep && id <= n_crumbs; id++) { 634 const RxCrumb *k = rx_world_crumb(w, id); 635 if (k && k->kind == RX_CRUMB_COMMIT && k->reaction != UINT32_MAX && k->episode && 636 k->episode <= n_crumbs) 637 ep[k->episode] = 1; 638 } 639 const RxCrumb *goal = r->goal_crumb > 0 ? rx_world_crumb(w, (uint64_t)r->goal_crumb) : NULL; 640 uint64_t g = goal ? (uint64_t)r->goal_crumb : 0; 641 const RxCrumb *plan = goal ? find_commit(w, g, reaction_named(w, "aien.plan"), UINT32_MAX) : NULL; 642 const RxCrumb *sel = plan ? find_commit(w, plan->id, reaction_named(w, "omega.select"), UINT32_MAX) : NULL; 643 const RxCrumb *claim = NULL, *evid = NULL; 644 const RxCrumb *cand = goal ? find_commit(w, g, UINT32_MAX, r->living.o.candidate.id) : NULL; 645 const RxCrumb *promo = goal ? find_commit(w, g, UINT32_MAX, r->living.o.promotion.id) : NULL; 646 const RxCrumb *inforce = goal ? find_commit(w, g, UINT32_MAX, r->living.o.inforce.id) : NULL; 647 if (sel) 648 for (uint64_t id = sel->id + 1; id <= w->n_crumbs; id++) { 649 const RxCrumb *k = rx_world_crumb(w, id); 650 if (!k || k->reaction != r->living.r_seat) continue; 651 claim = k; 652 break; 653 } 654 if (claim && promo) 655 for (uint64_t id = claim->id; id < promo->id; id++) { 656 const RxCrumb *k = rx_world_crumb(w, id); 657 if (k && k->kind == RX_CRUMB_COMMIT && k->reaction == r->living.r_evidence && 658 writes_obj(k, r->living.o.evidence.id)) 659 evid = k; 660 } 661 r15_rec_begin(&g_out, "adapt"); 662 rec_u("t_episode_start", t_ep); 663 rec_u("t_episode_end", t_done); 664 rec_u("goal_t", goal ? goal->t_end_ns : 0); 665 rec_u("plan_t", plan ? plan->t_end_ns : 0); 666 rec_u("select_t", sel ? sel->t_end_ns : 0); 667 rec_u("claim_t", claim ? claim->t_start_ns : 0); 668 rec_u("evidence_t", evid ? evid->t_end_ns : 0); 669 rec_u("candidate_t", cand ? cand->t_end_ns : 0); 670 rec_u("promotion_t", promo ? promo->t_end_ns : 0); 671 rec_u("inforce_t", inforce ? inforce->t_end_ns : 0); 672 rec_u("met_t", tw.t_met); 673 RxGenPhases ph; 674 memset(&ph, 0, sizeof ph); 675 rx_gen_last_phases(r->gen, &ph); 676 rec_u("barrier_enter", ph.enter_ns); 677 rec_u("barrier_begin", ph.barrier_ns); 678 rec_u("barrier_flip", ph.flip_ns); 679 rec_u("barrier_receipt", ph.receipt_ns); 680 rec_i("barrier_result", ph.result); 681 uint64_t during = 0; 682 if (ph.barrier_ns && ph.receipt_ns) 683 for (uint64_t id = 1; id <= w->n_crumbs; id++) { 684 const RxCrumb *k = rx_world_crumb(w, id); 685 if (k && k->reaction == r_serve && k->kind == RX_CRUMB_COMMIT && 686 k->t_end_ns >= ph.barrier_ns && k->t_end_ns <= ph.receipt_ns) 687 during++; 688 } 689 rec_u("barrier_production_commits", during); 690 barrier_trace(w, &ph); 691 rec_u("selected_ps", out.selected_ps); 692 rec_u("reference_ps", out.reference_ps); 693 r15_rec_end(&g_out); 694 if (goal && promo && inforce && ep) { 695 static CWin adapt; 696 memset(&adapt, 0, sizeof adapt); 697 adapt.t0 = goal->t_end_ns; 698 adapt.t1 = promo->t_end_ns; 699 cwin_scan(w, r_serve, ep, n_crumbs, &adapt); 700 rec_cwin("ADAPT", &adapt); 701 static CWin cost; /* goal .. in-force (§5 L3) */ 702 memset(&cost, 0, sizeof cost); 703 cost.t0 = goal->t_end_ns; 704 cost.t1 = inforce->t_end_ns; 705 cwin_scan(w, r_serve, ep, n_crumbs, &cost); 706 rec_cwin("ADAPT_COST", &cost); 707 } 708 if (tw.before_ok && ep) { 709 static CWin b; 710 memset(&b, 0, sizeof b); 711 b.t0 = tw.before.t0; b.t1 = tw.before.t1; 712 cwin_scan(w, r_serve, ep, n_crumbs, &b); 713 rec_cwin("BEFORE", &b); 714 } 715 if (tw.after_ok && ep) { 716 static CWin a, s; 717 memset(&a, 0, sizeof a); 718 a.t0 = tw.after.t0; a.t1 = tw.after.t1; 719 cwin_scan(w, r_serve, ep, n_crumbs, &a); 720 rec_cwin("AFTER", &a); 721 memset(&s, 0, sizeof s); 722 s.t0 = tw.span.t0; s.t1 = tw.span.t1; 723 cwin_scan(w, r_serve, ep, n_crumbs, &s); 724 rec_cwin("ADAPT_AFTER", &s); 725 } 726 free(ep); 727 g_samp.armed = 0; 728 rec_residency(); 729 r15_stop(r); 730 observers_stop(t_proc); 731 rec_machine("after"); 732 int complete = rc == 0 && tw.idle_ok && tw.before_ok && tw.after_ok && goal && promo && inforce; 733 r15_rec_begin(&g_out, "result"); 734 rec_i("complete", complete); 735 { /* where the teardown spent its time (CAND-1 window 2: SEQ-06 and RES4-11 waited ~102 s here); reported only */ 736 char tb[768]; 737 if (r15_stage_json(&r->stop_clock, tb, sizeof tb) > 0) fputs(tb, g_out.f); 738 if (r->leave_valid) 739 fprintf(g_out.f, ",\"seat_leave\":{\"rc\":%d,\"marker\":%u,\"sem\":%u}", r->leave_rc, r->leave_marker, r->leave_sem); 740 } 741 r15_rec_end(&g_out); 742 return complete ? 0 : 1; 743 } 744 745 /* ---- Level 1 (§5, pinned X925 cpu 7 driver / cpu 8 worker) --------------- */ 746 747 #define L1_WARM 1000u 748 #define L1_N 10000u 749 750 typedef struct { 751 AienosCapAdmin *admin; 752 AienosCapView *view; 753 RxWorld w; 754 int seq; 755 pthread_t orch; 756 atomic_int stop, err; 757 uint32_t order[RX_MAX_REACTIONS]; 758 RxSeqPlan plan; 759 uint32_t plan_for; 760 } L1; 761 762 static int pin_cpu(int cpu) { 763 cpu_set_t s; 764 CPU_ZERO(&s); 765 CPU_SET(cpu, &s); 766 return sched_setaffinity(0, sizeof s, &s); 767 } 768 769 static RxCapRef l1_mint(L1 *e, uint32_t subject, uint64_t resource, uint32_t rights) { 770 AienosCapRef office, ref = {UINT32_MAX, 0}; 771 aienos_cap_office(e->admin, &office); 772 AienosCapMint m = {ISSUER, subject, resource, rights, 0, {UINT32_MAX, 0}, office}; 773 aienos_cap_mint(e->admin, &m, &ref); 774 return (RxCapRef){ref.cap_id, ref.generation}; 775 } 776 777 static RxObjRef l1_obj(L1 *e, uint64_t res) { 778 uint64_t z[RX_MAX_FIELDS] = {0}; 779 RxObjRef r = {UINT32_MAX, 0}; 780 rx_world_create(&e->w, 1, RX_PERSIST_RESIDENT, res, z, &r); 781 return r; 782 } 783 784 static void *l1_orch(void *arg) { 785 L1 *e = arg; 786 while (!atomic_load(&e->stop)) { 787 pthread_mutex_lock(&e->w.mu); 788 if (e->plan_for != e->w.n_reactions) { 789 for (uint32_t i = 0; i < e->w.n_reactions; i++) e->order[i] = i; 790 e->plan.order = e->order; 791 e->plan.n = e->w.n_reactions; 792 e->plan_for = e->w.n_reactions; 793 } 794 pthread_mutex_unlock(&e->w.mu); 795 uint32_t ran = 0; 796 if (rx_seq_pulse(&e->w, &e->plan, &ran) != RX_OK) { atomic_store(&e->err, 1); break; } 797 if (!ran) sched_yield(); 798 } 799 return NULL; 800 } 801 802 static int l1_start(L1 *e, int seq) { 803 memset(e, 0, sizeof *e); 804 e->seq = seq; 805 if (pin_cpu(8) != 0) return -1; /* workers / orchestrator inherit cpu 8 */ 806 if (aienos_cap_start(&e->admin, &e->view) != 0) return -1; 807 int rc = seq ? rx_world_init_native_sequential_reference(&e->w, e->view, 1u << 24) 808 : rx_world_init_native(&e->w, e->view, 1, 1u << 24); 809 if (rc != RX_OK) return -1; 810 e->w.external_subject = EXTERNAL; 811 RxResourceBudget b = {0}; 812 b.slots = RX_MAX_REACTIONS; /* admission never limits L1 fanout (B inserts up to 256) */ 813 b.memory_bytes = UINT64_MAX; 814 b.energy_budget = UINT64_MAX; 815 b.offered_locality = UINT32_MAX; 816 b.compute_mask = UINT32_MAX; 817 rx_world_set_resources(&e->w, &b); 818 if (seq) { 819 e->plan.gpu_timeout_ms = 5000; 820 if (pthread_create(&e->orch, NULL, l1_orch, e) != 0) return -1; 821 } 822 return pin_cpu(7); 823 } 824 825 static void l1_stop(L1 *e) { 826 if (e->seq) { atomic_store(&e->stop, 1); pthread_join(e->orch, NULL); } 827 rx_world_destroy(&e->w); 828 aienos_cap_stop(e->admin, e->view); 829 } 830 831 static int fn_quiet(RxCtx *c) { (void)c; return 0; } 832 833 typedef struct { RxObjRef in, out; } Copy; 834 static int fn_copy(RxCtx *c) { 835 Copy *k = c->user; 836 for (uint32_t i = 0; i < c->n_in; i++) 837 if (c->in[i].obj.id == k->in.id) 838 c->out[c->n_out++] = (RxMutation){k->out, 0, c->in[i].field[0]}; 839 return 0; 840 } 841 842 static volatile uint64_t g_spin_ns; 843 static int fn_spin(RxCtx *c) { 844 (void)c; 845 uint64_t until = r15_now_ns() + g_spin_ns; 846 while (r15_now_ns() < until) {} 847 return 0; 848 } 849 850 static uint32_t l1_reaction(L1 *e, const char *name, RxFn fn, void *user, uint32_t prio, 851 RxObjRef trig, RxCapRef rd, uint64_t res) { 852 RxReactionDesc d; 853 memset(&d, 0, sizeof d); 854 d.name = name; 855 d.faculty = RX_FACULTY_OMEGA; 856 d.subject = L1_SUBJ; 857 d.priority = prio; 858 d.fn = fn; 859 d.user = user; 860 d.n_triggers = 1; 861 d.triggers[0] = (RxDep){trig, RX_FIELD(0)}; 862 d.n_caps = 1; 863 d.caps[0] = (RxCapNeed){rd, res, RX_RIGHT_READ}; 864 uint32_t id = UINT32_MAX; 865 return rx_world_add_reaction(&e->w, &d, &id) == RX_OK ? id : UINT32_MAX; 866 } 867 868 static uint64_t acts_of(RxWorld *w, uint32_t rid) { 869 return __atomic_load_n(&w->reactions[rid].activations, __ATOMIC_ACQUIRE); 870 } 871 872 static int wait_acts(RxWorld *w, uint32_t rid, uint64_t want) { 873 uint64_t t = r15_now_ns(); 874 while (acts_of(w, rid) < want) { 875 if (r15_now_ns() - t > 5000000000ull) return -1; 876 } 877 return 0; 878 } 879 880 static void rec_raw(const char *measure, const char *field, const uint64_t *v, uint64_t n, 881 uint64_t extra_k, uint64_t extra_v) { 882 r15_rec_begin(&g_out, "l1"); 883 rec_str("measure", measure); 884 rec_str("field", field); 885 if (extra_k) rec_u("param", extra_v); 886 rec_u("n", n); 887 rec_arr("v", v, n); 888 r15_rec_end(&g_out); 889 } 890 891 static int timing_ok(RxWorld *w, const char *measure) { 892 uint64_t st = 0, at = 0; 893 int ok = rx_world_timing_status(w, &st, &at) == RX_OK; 894 r15_rec_begin(&g_out, "timing_status"); 895 rec_str("measure", measure); 896 rec_u("stored", st); 897 rec_u("attempted", at); 898 rec_i("ok", ok); 899 r15_rec_end(&g_out); 900 return ok; 901 } 902 903 /* A: external publication (EXTERNAL crumb t_end, inside the lock) -> run_one 904 * start of the one dependent. C (uncontended) from the same activations. */ 905 static int l1_a(L1 *e, int with_c) { 906 const uint64_t RES = 0x150; 907 RxObjRef src = l1_obj(e, RES); 908 RxCapRef ext = l1_mint(e, EXTERNAL, RES, RX_RIGHT_WRITE); 909 RxCapRef rd = l1_mint(e, L1_SUBJ, RES, RX_RIGHT_READ); 910 uint32_t rid = l1_reaction(e, "l1.a", fn_quiet, NULL, RX_PRIO_FOREGROUND, src, rd, RES); 911 if (rid == UINT32_MAX) return -1; 912 uint32_t total = L1_WARM + L1_N; 913 RxTiming *tb = calloc(total + 16, sizeof *tb); 914 uint64_t *lat = calloc(total, 8), *sch = calloc(total, 8), *scpu = calloc(total, 8), 915 *rr = calloc(total, 8), *dr = calloc(total, 8); 916 if (!tb || !lat || !sch || !scpu || !rr || !dr) return -1; 917 rx_world_set_timing(&e->w, tb, total + 16); 918 uint64_t base = acts_of(&e->w, rid); 919 for (uint32_t i = 0; i < total; i++) { 920 RxMutation m = {src, 0, i + 1}; 921 if (rx_world_publish_external(&e->w, ext, &m, 1) <= 0) return -1; 922 if (wait_acts(&e->w, rid, base + i + 1) != 0) return -1; 923 } 924 if (rx_world_wait_quiescent(&e->w, 5000) != RX_OK) return -1; 925 int ok = timing_ok(&e->w, with_c ? "C" : "A"); 926 uint64_t stored = 0, att = 0; 927 rx_world_timing_status(&e->w, &stored, &att); 928 uint64_t n = 0; 929 for (uint64_t j = 0; j < stored; j++) { 930 if (tb[j].reaction != rid) continue; 931 if (n >= total) break; 932 const RxCrumb *c = rx_world_crumb(&e->w, tb[j].cause); 933 lat[n] = c && tb[j].t_run >= c->t_end_ns ? tb[j].t_run - c->t_end_ns : UINT64_MAX; 934 sch[n] = tb[j].sched_ns; 935 scpu[n] = tb[j].sched_cpu_ns; 936 rr[n] = tb[j].t_run - tb[j].t_ready; 937 dr[n] = tb[j].t_ready - tb[j].t_demand; 938 n++; 939 } 940 rx_world_set_timing(&e->w, NULL, 0); 941 if (n != total) ok = 0; 942 if (!with_c) { 943 rec_raw("A", "latency_ns", lat + L1_WARM, n > L1_WARM ? n - L1_WARM : 0, 0, 0); 944 } else { 945 uint64_t m = n > L1_WARM ? n - L1_WARM : 0; 946 rec_raw("C_uncontended", "sched_wall_ns", sch + L1_WARM, m, 0, 0); 947 rec_raw("C_uncontended", "sched_cpu_ns", scpu + L1_WARM, m, 0, 0); 948 rec_raw("C_uncontended", "ready_to_run_ns", rr + L1_WARM, m, 0, 0); 949 rec_raw("C_uncontended", "demand_to_ready_ns", dr + L1_WARM, m, 0, 0); 950 } 951 free(tb); free(lat); free(sch); free(scpu); free(rr); free(dr); 952 return ok ? 0 : -1; 953 } 954 955 /* B: one committed field -> n dependents inserted (RES only). */ 956 static int l1_b(L1 *e) { 957 static const uint32_t fan[] = {1, 2, 4, 8, 16, 32, 64, 128, 256}; 958 RxTiming *tb = calloc(512, sizeof *tb); 959 uint64_t *wall = calloc(L1_N, 8), *cpu = calloc(L1_N, 8); 960 if (!tb || !wall || !cpu) return -1; 961 int bad = 0; 962 for (uint32_t f = 0; f < sizeof fan / sizeof fan[0]; f++) { 963 uint64_t RES = 0x160 + f; 964 RxObjRef src = l1_obj(e, RES); 965 RxCapRef ext = l1_mint(e, EXTERNAL, RES, RX_RIGHT_WRITE); 966 RxCapRef rd = l1_mint(e, L1_SUBJ, RES, RX_RIGHT_READ); 967 for (uint32_t i = 0; i < fan[f]; i++) 968 if (l1_reaction(e, "l1.b", fn_quiet, NULL, RX_PRIO_FOREGROUND, src, rd, RES) == UINT32_MAX) 969 return -1; 970 uint64_t failed = 0, n = 0; 971 for (uint32_t i = 0; i < L1_WARM + L1_N; i++) { 972 rx_world_set_timing(&e->w, tb, 512); 973 RxMutation m = {src, 0, i + 1}; 974 if (rx_world_publish_external(&e->w, ext, &m, 1) <= 0) return -1; 975 if (rx_world_wait_quiescent(&e->w, 5000) != RX_OK) return -1; 976 pthread_mutex_lock(&e->w.mu); 977 RxPropWave p = e->w.last_prop; 978 pthread_mutex_unlock(&e->w.mu); 979 if (i < L1_WARM) continue; 980 if (p.ready_inserts != fan[f]) { /* C1 item 2 */ 981 if (!failed) fprintf(stderr, "B fanout %u: inspected %llu matched %llu attempts %llu accepted %llu inserts %llu coalesced %llu deferred %llu suppressed %llu\n", fan[f], (unsigned long long)p.inspected, (unsigned long long)p.matched, (unsigned long long)p.wake_attempts, (unsigned long long)p.wakes_accepted, (unsigned long long)p.ready_inserts, (unsigned long long)p.coalesced, (unsigned long long)p.deferred, (unsigned long long)p.suppressed); 982 failed++; 983 continue; 984 } 985 wall[n] = p.wall_ns; 986 cpu[n] = p.cpu_ns; 987 n++; 988 } 989 rx_world_set_timing(&e->w, NULL, 0); 990 rec_raw("B", "wave_wall_ns", wall, n, 1, fan[f]); 991 rec_raw("B", "wave_cpu_ns", cpu, n, 1, fan[f]); 992 r15_rec_begin(&g_out, "l1_b_failed"); 993 rec_u("fanout", fan[f]); 994 rec_u("failed_samples", failed); 995 r15_rec_end(&g_out); 996 if (failed) bad = 1; 997 } 998 free(tb); free(wall); free(cpu); 999 return bad ? -1 : 0; 1000 } 1001 1002 /* C contended: resource-blocked (slots = 1, a holder releases) and priority 1003 * contention (7 classes, starvation bound 4). */ 1004 static int l1_c_contended(L1 *e) { 1005 RxResourceBudget b = {0}; 1006 b.memory_bytes = UINT64_MAX; 1007 b.energy_budget = UINT64_MAX; 1008 b.offered_locality = UINT32_MAX; 1009 b.compute_mask = UINT32_MAX; 1010 int ok = 1; 1011 const uint32_t N = 2000; /* stimuli per case; every activation recorded */ 1012 for (int kase = 0; kase < 2; kase++) { 1013 b.slots = kase == 0 ? 1 : 8; 1014 b.starvation_bound = kase == 0 ? 0 : 4; 1015 rx_world_set_resources(&e->w, &b); 1016 uint64_t RES = 0x170 + (uint64_t)kase; 1017 RxObjRef src = l1_obj(e, RES); 1018 RxCapRef ext = l1_mint(e, EXTERNAL, RES, RX_RIGHT_WRITE); 1019 RxCapRef rd = l1_mint(e, L1_SUBJ, RES, RX_RIGHT_READ); 1020 uint32_t nr = kase == 0 ? 2 : RX_PRIORITY_CLASSES; 1021 uint32_t ids[RX_PRIORITY_CLASSES]; 1022 g_spin_ns = 20000; 1023 for (uint32_t i = 0; i < nr; i++) 1024 if ((ids[i] = l1_reaction(e, kase == 0 ? "l1.c.blocked" : "l1.c.prio", fn_spin, NULL, 1025 kase == 0 ? RX_PRIO_FOREGROUND : i, src, rd, RES)) == UINT32_MAX) 1026 return -1; 1027 uint64_t cap = (uint64_t)N * nr + 64; 1028 RxTiming *tb = calloc(cap, sizeof *tb); 1029 uint64_t *dr = calloc(cap, 8), *rr = calloc(cap, 8), *sc = calloc(cap, 8), *sw = calloc(cap, 8), 1030 *cls = calloc(cap, 8); 1031 if (!tb || !dr || !rr || !sc || !sw || !cls) return -1; 1032 rx_world_set_timing(&e->w, tb, cap); 1033 for (uint32_t i = 0; i < N; i++) { 1034 RxMutation m = {src, 0, i + 1}; 1035 if (rx_world_publish_external(&e->w, ext, &m, 1) <= 0) return -1; 1036 if (rx_world_wait_quiescent(&e->w, 5000) != RX_OK) return -1; 1037 } 1038 if (!timing_ok(&e->w, kase == 0 ? "C_blocked" : "C_priority")) ok = 0; 1039 uint64_t st = 0, at = 0; 1040 rx_world_timing_status(&e->w, &st, &at); 1041 uint64_t n = 0; 1042 for (uint64_t j = 0; j < st; j++) { 1043 uint32_t k; 1044 for (k = 0; k < nr && ids[k] != tb[j].reaction; k++) {} 1045 if (k == nr) continue; 1046 dr[n] = tb[j].t_ready - tb[j].t_demand; 1047 rr[n] = tb[j].t_run - tb[j].t_ready; 1048 sc[n] = tb[j].sched_cpu_ns; 1049 sw[n] = tb[j].sched_ns; 1050 cls[n] = k; 1051 n++; 1052 } 1053 rx_world_set_timing(&e->w, NULL, 0); 1054 const char *m = kase == 0 ? "C_blocked" : "C_priority"; 1055 rec_raw(m, "demand_to_ready_ns", dr, n, 0, 0); 1056 rec_raw(m, "ready_to_run_ns", rr, n, 0, 0); 1057 rec_raw(m, "sched_cpu_ns", sc, n, 0, 0); 1058 rec_raw(m, "sched_wall_ns", sw, n, 0, 0); 1059 rec_raw(m, "class", cls, n, 0, 0); 1060 free(tb); free(dr); free(rr); free(sc); free(sw); free(cls); 1061 } 1062 g_spin_ns = 0; 1063 return ok ? 0 : -1; 1064 } 1065 1066 /* D: reaction return -> publication committed -> dependents visible, per 1067 * commit; E(activation): the whole activation of a reaction holding existing 1068 * grants (run_one -> visible). */ 1069 static int l1_d(L1 *e) { 1070 const uint64_t RI = 0x180, RO = 0x181; 1071 RxObjRef in = l1_obj(e, RI), outo = l1_obj(e, RO); 1072 RxCapRef ext = l1_mint(e, EXTERNAL, RI, RX_RIGHT_WRITE); 1073 RxCapRef rdi = l1_mint(e, L1_SUBJ, RI, RX_RIGHT_READ); 1074 RxCapRef rwo = l1_mint(e, L1_SUBJ, RO, RX_RIGHT_READ | RX_RIGHT_WRITE); 1075 static Copy k; 1076 k.in = in; k.out = outo; 1077 RxReactionDesc d; 1078 memset(&d, 0, sizeof d); 1079 d.name = "l1.d.copy"; 1080 d.faculty = RX_FACULTY_OMEGA; 1081 d.subject = L1_SUBJ; 1082 d.priority = RX_PRIO_FOREGROUND; 1083 d.fn = fn_copy; 1084 d.user = &k; 1085 d.n_triggers = 1; 1086 d.triggers[0] = (RxDep){in, RX_FIELD(0)}; 1087 d.n_writes = 1; 1088 d.writes[0] = (RxDep){outo, RX_FIELD(0)}; 1089 d.n_caps = 2; 1090 d.caps[0] = (RxCapNeed){rdi, RI, RX_RIGHT_READ}; 1091 d.caps[1] = (RxCapNeed){rwo, RO, RX_RIGHT_WRITE}; 1092 uint32_t rid; 1093 if (rx_world_add_reaction(&e->w, &d, &rid) != RX_OK) return -1; 1094 RxCapRef rdo = l1_mint(e, L1_SUBJ, RO, RX_RIGHT_READ); 1095 if (l1_reaction(e, "l1.d.listener", fn_quiet, NULL, RX_PRIO_FOREGROUND, outo, rdo, RO) == UINT32_MAX) 1096 return -1; 1097 uint32_t total = L1_WARM + L1_N; 1098 uint64_t cap = (uint64_t)total * 2 + 16; 1099 RxTiming *tb = calloc(cap, sizeof *tb); 1100 uint64_t *pub = calloc(total, 8), *act = calloc(total, 8); 1101 if (!tb || !pub || !act) return -1; 1102 rx_world_set_timing(&e->w, tb, cap); 1103 for (uint32_t i = 0; i < total; i++) { 1104 RxMutation m = {in, 0, i + 1}; 1105 if (rx_world_publish_external(&e->w, ext, &m, 1) <= 0) return -1; 1106 if (rx_world_wait_quiescent(&e->w, 5000) != RX_OK) return -1; 1107 } 1108 int ok = timing_ok(&e->w, "D"); 1109 uint64_t st = 0, at = 0, n = 0; 1110 rx_world_timing_status(&e->w, &st, &at); 1111 for (uint64_t j = 0; j < st && n < total; j++) { 1112 if (tb[j].reaction != rid || tb[j].outcome != RX_CRUMB_COMMIT) continue; 1113 pub[n] = tb[j].t_visible - tb[j].t_fn_end; 1114 act[n] = tb[j].t_visible - tb[j].t_run; 1115 n++; 1116 } 1117 rx_world_set_timing(&e->w, NULL, 0); 1118 if (n != total) ok = 0; 1119 uint64_t m = n > L1_WARM ? n - L1_WARM : 0; 1120 rec_raw("D", "publish_to_visible_ns", pub + L1_WARM, m, 0, 0); 1121 rec_raw("E_activation", "run_to_visible_ns", act + L1_WARM, m, 0, 0); 1122 free(tb); free(pub); free(act); 1123 return ok ? 0 : -1; 1124 } 1125 1126 /* E: rx_world_validate_cap on an existing valid grant, batches of 100. */ 1127 static int l1_e(L1 *e) { 1128 const uint64_t RES = 0x190; 1129 RxCapRef rd = l1_mint(e, L1_SUBJ, RES, RX_RIGHT_READ); 1130 uint64_t *v = calloc(L1_N, 8); 1131 if (!v) return -1; 1132 int ok = 1; 1133 for (uint32_t b = 0; b < L1_WARM + L1_N; b++) { 1134 uint64_t t0 = r15_now_ns(); 1135 for (int i = 0; i < 100; i++) { 1136 RxCapEntry ent; 1137 if (rx_world_validate_cap(&e->w, rd, L1_SUBJ, RES, RX_RIGHT_READ, &ent) != RX_CAP_OK) ok = 0; 1138 } 1139 uint64_t dt = r15_now_ns() - t0; 1140 if (b >= L1_WARM) v[b - L1_WARM] = dt; 1141 } 1142 rec_raw("E", "batch100_ns", v, L1_N, 0, 0); 1143 free(v); 1144 return ok ? 0 : -1; 1145 } 1146 1147 /* The same check the living promoter uses (rx_living.c native_promotion). */ 1148 static int native_promotion(void *ctx, uint32_t cap_id, uint64_t generation, uint32_t subject, 1149 uint64_t resource, uint32_t rights) { 1150 AienosCapEntry entry; 1151 return aienos_cap_validate(ctx, (AienosCapRef){cap_id, generation}, subject, resource, rights, 1152 &entry); 1153 } 1154 1155 /* G: R9 barrier on the living body while production keeps publishing. 1156 * W0/W1: W-PROD throughput with the timing buffer off/on. Both use the rig. */ 1157 static int l1_rig(R15Config cfg, const char *measure) { 1158 static R15Rig rig; 1159 R15Rig *r = &rig; 1160 if (r15_start(r, cfg) != 0) return -1; 1161 if (r15_move_class(R15_CLASS_X925) != 0 || r15_placement(r, R15_CLASS_X925) != 0) { 1162 r15_stop(r); 1163 return -1; 1164 } 1165 int ok = 1; 1166 if (strcmp(measure, "G") == 0) { 1167 if (r15_producer_start(r) != 0) { r15_stop(r); return -1; } 1168 sleep_ns(WARM_NS); 1169 RxObject slot; 1170 AienosCapEntry grant; 1171 if (rx_world_read(&r->w, r->aegis.o[0].slot[0], &slot) != RX_OK || 1172 aienos_cap_inspect(r->view, (AienosCapRef){(uint32_t)slot.field[0], 1173 slot.field[1]}, &grant) != 0) 1174 ok = 0; 1175 static uint8_t blob[4096]; 1176 for (int b = 0; b < 30 && ok; b++) { 1177 for (size_t i = 0; i < sizeof blob; i++) blob[i] = (uint8_t)(i * 31u + (unsigned)b); 1178 RxGenDraft d; 1179 memset(&d, 0, sizeof d); 1180 d.authority_epoch = grant.epoch; 1181 d.authority_generation = grant.generation; 1182 d.proofs_ok = 1; 1183 d.evidence = blob; d.evidence_len = 256; 1184 d.model = blob + 256; d.model_len = 128; 1185 d.realization = blob; d.realization_len = sizeof blob; 1186 d.config = blob + 512; d.config_len = 64; 1187 d.provenance = blob + 1024; d.provenance_len = 128; 1188 uint64_t id = 0, b0, s0, b1, s1, pb0, ps0, pb1, ps1; 1189 rx_gen_store_io(r->gen, &b0, &s0); 1190 rx_gen_io_counters(&pb0, &ps0); 1191 uint64_t prep = r15_now_ns(); 1192 int prc = rx_gen_propose_as(r->gen, RX_LIVING_PREPARE_SUBJ, 1193 rx_caller_find(&r->keys_living, RX_LIVING_PREPARE_SUBJ), &d, &id); 1194 RxPromotionRequest req = {id, RX_LIVING_PROMOTE_SUBJ, 1195 r->promoter.promotion_authority.cap_id, r->promoter.promotion_authority.generation, 1196 RX_GEN_RES_PROMOTION, RX_GEN_RIGHT_PROMOTE, {0, {0}}}; 1197 req.caller = *rx_caller_find(&r->keys_promoter, RX_LIVING_PROMOTE_SUBJ); 1198 uint64_t n0 = r->w.n_crumbs; 1199 int rc = prc == RX_GEN_OK 1200 ? rx_gen_promote(r->gen, &req, native_promotion, (void *)r->view, NULL, NULL, NULL, NULL) : prc; 1201 RxGenPhases ph; 1202 memset(&ph, 0, sizeof ph); 1203 rx_gen_last_phases(r->gen, &ph); 1204 sleep_ns(20000000ull); /* live work continues */ 1205 rx_gen_store_io(r->gen, &b1, &s1); 1206 rx_gen_io_counters(&pb1, &ps1); 1207 uint64_t during = 0, during_flip = 0, after = 0; 1208 uint64_t n1 = r->w.n_crumbs; 1209 for (uint64_t cid = n0 > 4096 ? n0 - 4096 : 1; cid <= n1; cid++) { 1210 const RxCrumb *k = rx_world_crumb(&r->w, cid); 1211 if (!k || k->reaction != r->omega.r_serve || k->kind != RX_CRUMB_COMMIT) continue; 1212 if (k->t_end_ns >= ph.barrier_ns && k->t_end_ns <= ph.receipt_ns) during++; 1213 if (k->t_end_ns >= ph.barrier_ns && k->t_end_ns <= ph.flip_ns) during_flip++; 1214 if (k->t_end_ns > ph.receipt_ns) after++; 1215 } 1216 r15_rec_begin(&g_out, "l1_g"); 1217 rec_i("barrier", b); 1218 rec_i("propose_rc", prc); 1219 rec_i("promote_rc", rc); 1220 rec_u("prepared", prep); 1221 rec_u("enter", ph.enter_ns); 1222 rec_u("begin", ph.barrier_ns); 1223 rec_u("flip", ph.flip_ns); 1224 rec_u("receipt", ph.receipt_ns); 1225 rec_u("production_commits_barrier", during); 1226 rec_u("production_commits_to_flip", during_flip); 1227 rec_u("production_commits_after", after); 1228 rec_u("store_bytes", b1 - b0); 1229 rec_u("store_syncs", s1 - s0); 1230 rec_u("process_bytes", pb1 - pb0); 1231 rec_u("process_syncs", ps1 - ps0); 1232 r15_rec_end(&g_out); 1233 if (rc != RX_GEN_OK) ok = 0; 1234 sleep_ns(100000000ull); 1235 } 1236 if (r15_producer_stop(r) != 0) ok = 0; 1237 } else { 1238 int timing = strcmp(measure, "W1") == 0; 1239 uint64_t cap = 8ull << 20; 1240 RxTiming *tb = timing ? calloc(cap, sizeof *tb) : NULL; 1241 if (timing && !tb) ok = 0; 1242 if (tb) rx_world_set_timing(&r->w, tb, cap); 1243 if (r15_producer_start(r) != 0) ok = 0; 1244 sleep_ns(WARM_NS); 1245 Win x; 1246 win_begin(&x, &r->w, r, 0); 1247 sleep_ns(WINDOW_NS); 1248 win_end(&x, &r->w, r); 1249 if (r15_producer_stop(r) != 0) ok = 0; 1250 rec_window(timing ? "W_TIMING_ON" : "W_TIMING_OFF", &x); 1251 if (tb) { 1252 timing_ok(&r->w, "W1"); 1253 rx_world_set_timing(&r->w, NULL, 0); 1254 } 1255 r15_rec_begin(&g_out, "w_correct"); 1256 rec_u("wrong", r->wrong); 1257 r15_rec_end(&g_out); 1258 if (r->wrong) ok = 0; 1259 rx_world_wait_quiescent(&r->w, 10000); 1260 free(tb); 1261 } 1262 r15_stop(r); 1263 return ok ? 0 : -1; 1264 } 1265 1266 static int run_l1(const char *measure, R15Config cfg) { 1267 rec_machine("before"); 1268 if (strcmp(measure, "G") == 0 || measure[0] == 'W') { 1269 int rc = l1_rig(cfg, measure); 1270 rec_machine("after"); 1271 return rc == 0 ? 0 : 1; 1272 } 1273 L1 e; 1274 if (l1_start(&e, cfg == R15_SEQ) != 0) return 2; 1275 int rc; 1276 switch (measure[0]) { 1277 case 'A': rc = l1_a(&e, 0); break; 1278 case 'B': rc = cfg == R15_SEQ ? -1 : l1_b(&e); break; 1279 case 'C': rc = l1_a(&e, 1); if (rc == 0) rc = l1_c_contended(&e); break; 1280 case 'D': rc = l1_d(&e); break; 1281 case 'E': rc = l1_e(&e); break; 1282 default: rc = -1; 1283 } 1284 if (e.seq && atomic_load(&e.err)) rc = -1; 1285 l1_stop(&e); 1286 rec_machine("after"); 1287 return rc == 0 ? 0 : 1; 1288 } 1289 1290 /* ---- Level 2: CPU <-> GPU through the resident seat ----------------------- */ 1291 1292 static int run_l2(R15Config cfg) { 1293 rec_machine("before"); 1294 uint64_t t_proc = r15_now_ns(); 1295 observers_start(); 1296 static R15Rig rig; 1297 R15Rig *r = &rig; 1298 if (r15_start(r, cfg) != 0) { observers_stop(t_proc); return 2; } 1299 g_samp.w = &r->w; 1300 g_samp.armed = 1; 1301 if (r15_move_class(R15_CLASS_X925) != 0) { r15_stop(r); observers_stop(t_proc); return 2; } 1302 RxCapRef ext = r15_mint(r, EXTERNAL, RX_LIVING_RES_BASE + RX_LIVING_RES_INPUT, RX_RIGHT_WRITE); 1303 RxWorld *w = &r->w; 1304 int ok = 1; 1305 const uint32_t CLAIMS = 256; 1306 Win x; 1307 win_begin(&x, w, r, 0); 1308 for (uint32_t i = 0; i < CLAIMS && ok; i++) { 1309 uint64_t a = 1000u + i * 7u, b = 3u + i * 3u, want = (uint32_t)(a + b); 1310 RxMutation m[2] = {{r->living.o.input, 0, a}, {r->living.o.input, 1, b}}; 1311 int64_t ec = rx_world_publish_external(w, ext, m, 2); 1312 if (ec <= 0) { ok = 0; break; } 1313 uint64_t t = r15_now_ns(); 1314 RxObject o; 1315 while (!(rx_world_read(w, r->living.o.output, &o) == RX_OK && o.field[0] == want)) { 1316 if (r15_now_ns() - t > 5000000000ull) { ok = 0; break; } 1317 } 1318 if (!ok || rx_world_wait_quiescent(w, 5000) != RX_OK) { ok = 0; break; } 1319 uint32_t pick = 0, done = 0, claim = 0; 1320 #ifdef R15_SILICON 1321 const uint8_t *hb = w->coherent + rx_world_off_heartbeat(); 1322 pick = __atomic_load_n((const uint32_t *)(hb + RX_SEAT_HB_T_PICK), __ATOMIC_ACQUIRE); 1323 done = __atomic_load_n((const uint32_t *)(hb + RX_SEAT_HB_T_DONE), __ATOMIC_ACQUIRE); 1324 claim = __atomic_load_n((const uint32_t *)(hb + RX_SEAT_HB_CLAIM), __ATOMIC_ACQUIRE); 1325 #endif 1326 const RxCrumb *ek = rx_world_crumb(w, (uint64_t)ec), *sk = NULL, *dk = NULL; 1327 for (uint64_t id = (uint64_t)ec + 1; id <= w->n_crumbs; id++) { 1328 const RxCrumb *k = rx_world_crumb(w, id); 1329 if (!k) continue; 1330 if (!sk && k->reaction == r->living.r_seat && k->kind == RX_CRUMB_COMMIT) sk = k; 1331 else if (sk && !dk && k->reaction == r->living.r_evidence) dk = k; 1332 } 1333 r15_rec_begin(&g_out, "l2_claim"); 1334 rec_u("i", i); 1335 rec_u("input_t", ek ? ek->t_end_ns : 0); 1336 rec_u("seat_start_t", sk ? sk->t_start_ns : 0); 1337 rec_u("seat_commit_t", sk ? sk->t_end_ns : 0); 1338 rec_u("dependent_start_t", dk ? dk->t_start_ns : 0); 1339 rec_u("chip_pick", pick); 1340 rec_u("chip_done", done); 1341 rec_u("chip_claim", claim); 1342 r15_rec_end(&g_out); 1343 if (!sk || !dk) ok = 0; 1344 } 1345 win_end(&x, w, r); 1346 rec_window("L2", &x); 1347 g_samp.armed = 0; 1348 rec_residency(); 1349 r15_stop(r); 1350 observers_stop(t_proc); 1351 rec_machine("after"); 1352 return ok ? 0 : 1; 1353 } 1354 1355 /* ---- main ------------------------------------------------------------------ */ 1356 1357 static int parse_cfg(const char *s, R15Config *c) { 1358 if (strcmp(s, "RES4") == 0) *c = R15_RES4; 1359 else if (strcmp(s, "RES1") == 0) *c = R15_RES1; 1360 else if (strcmp(s, "SEQ") == 0) *c = R15_SEQ; 1361 else return -1; 1362 return 0; 1363 } 1364 1365 static void usage(void) { 1366 fprintf(stderr, "usage: rx_r15_perf trial \n" 1367 " rx_r15_perf l1 \n" 1368 " rx_r15_perf l2 \n"); 1369 } 1370 1371 int main(int argc, char **argv) { 1372 if (argc < 6) { usage(); return 64; } 1373 R15Config cfg; 1374 const char *mode = argv[1], *measure = "", *run, *outp; 1375 int round; 1376 if (strcmp(mode, "l1") == 0) { 1377 if (argc != 7 || parse_cfg(argv[3], &cfg) != 0) { usage(); return 64; } 1378 measure = argv[2]; run = argv[4]; round = atoi(argv[5]); outp = argv[6]; 1379 } else { 1380 if (argc != 6 || parse_cfg(argv[2], &cfg) != 0) { usage(); return 64; } 1381 run = argv[3]; round = atoi(argv[4]); outp = argv[5]; 1382 } 1383 snprintf(g_config, sizeof g_config, "%s%s", r15_config_name(cfg), 1384 !rx_world_causal_digest_enabled ? "-NODIGEST" : ""); 1385 if (!rx_world_causal_digest_enabled && cfg != R15_RES1) { 1386 fprintf(stderr, "the NODIGEST build measures RES-1 only (spec §3)\n"); 1387 return 64; 1388 } 1389 char level[16]; 1390 snprintf(level, sizeof level, "%s%s%s", mode, measure[0] ? "-" : "", measure); 1391 if (r15_out_open(&g_out, outp, run, level, g_config, round, 0) != 0) { 1392 fprintf(stderr, "cannot create %s (raw evidence is never overwritten)\n", outp); 1393 return 65; 1394 } 1395 r15_rec_begin(&g_out, "start"); 1396 rec_str("mode", mode); 1397 rec_str("measure", measure); 1398 rec_i("pid", getpid()); 1399 #ifdef R15_SILICON 1400 rec_i("silicon", 1); 1401 #else 1402 rec_i("silicon", 0); 1403 #endif 1404 r15_rec_end(&g_out); 1405 int rc; 1406 if (strcmp(mode, "trial") == 0) rc = run_trial(cfg); 1407 else if (strcmp(mode, "l1") == 0) rc = run_l1(measure, cfg); 1408 else if (strcmp(mode, "l2") == 0) rc = run_l2(cfg); 1409 else { usage(); rc = 64; } 1410 r15_rec_begin(&g_out, "end"); 1411 rec_i("exit", rc); 1412 r15_rec_end(&g_out); 1413 r15_out_close(&g_out); 1414 fprintf(stderr, "%s %s %s round %d: %s\n", level, g_config, run, round, rc == 0 ? "ok" : "FAILED"); 1415 return rc; 1416 }