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
omega-mv / tests / runtime / rx_branch_reuse.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 | * rx_branch_reuse.c -- OMEGA_BRANCH_STATE_REUSE gate. |
| 19 | * |
| 20 | * 100+ J-Space branches share a large common semantic prefix. For each of |
| 21 | * three realizers with different cost shapes, the same branch program runs |
| 22 | * twice, each in its own child process so memory is measured from a clean |
| 23 | * start: |
| 24 | * |
| 25 | * independent: every branch recomputes its whole state from the seed and |
| 26 | * keeps it (no sharing of any kind); |
| 27 | * shared: one prefix, branches fork by reference, copy on write, |
| 28 | * semantic reuse across branches, FORGE placement policy under |
| 29 | * memory pressure. |
| 30 | * |
| 31 | * Measured per run: memory (bytes held and resident-set growth), compute |
| 32 | * (derive calls and processor time), latency (all branches ready, per-branch |
| 33 | * ready), energy (GB10 package and CPU-cluster counters) and branch creation |
| 34 | * time. Correctness: every shared branch's bytes equal the independent run's, |
| 35 | * the shared ancestor never changes, semantic identity does not move with |
| 36 | * placement or with the choice of representation. |
| 37 | * |
| 38 | * Usage: rx_branch_reuse [branches] [prefix_units] [diverge_units] |
| 39 | */ |
| 40 | #include "runtime/rx_jspace.h" |
| 41 | #include "omega_evidence.h" |
| 42 | #include "sha256.h" |
| 43 | |
| 44 | #include <dirent.h> |
| 45 | #include <math.h> |
| 46 | #include <sched.h> |
| 47 | #include <stdio.h> |
| 48 | #include <stdlib.h> |
| 49 | #include <string.h> |
| 50 | #include <sys/mman.h> |
| 51 | #include <sys/wait.h> |
| 52 | #include <time.h> |
| 53 | #include <unistd.h> |
| 54 | |
| 55 | #define U(x) ((unsigned long long)(x)) |
| 56 | #define MAX_BRANCHES 1024u |
| 57 | #define UNIT_BYTES 65536u |
| 58 | #define EDITS 2u |
| 59 | #define EDIT_LEN 256u |
| 60 | #define ENERGY_WINDOW_NS 2000000000ull /* the GB10 counters tick every 100 ms */ |
| 61 | |
| 62 | static unsigned g_branches = 128, g_prefix = 512, g_diverge = 16; |
| 63 | /* Branch k and k + g_choices take the same steps (96 of 128 by default). */ |
| 64 | static unsigned g_choices = 96; |
| 65 | static int g_fail; |
| 66 | static int g_energy_not_run; /* counters unreadable: energy leg not run, never a pass */ |
| 67 | |
| 68 | #define CHECK(c, ...) do { if (!(c)) { g_fail++; printf(" FAIL: " __VA_ARGS__); printf("\n"); } } while (0) |
| 69 | |
| 70 | /* ---- realizers --------------------------------------------------------------- */ |
| 71 | |
| 72 | static uint64_t splitmix(uint64_t *x) { |
| 73 | uint64_t z = (*x += 0x9e3779b97f4a7c15ull); |
| 74 | z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ull; |
| 75 | z = (z ^ (z >> 27)) * 0x94d049bb133111ebull; |
| 76 | return z ^ (z >> 31); |
| 77 | } |
| 78 | |
| 79 | static void fill_seed(uint64_t seed, uint8_t *out, size_t n) { |
| 80 | uint64_t x = seed; |
| 81 | for (size_t i = 0; i + 8 <= n; i += 8) { uint64_t v = splitmix(&x); memcpy(out + i, &v, 8); } |
| 82 | } |
| 83 | |
| 84 | /* Native latent-state checkpoint: an integer recurrence over the whole state. */ |
| 85 | static void derive_latent(const uint8_t *prev, uint64_t token, uint8_t *out, size_t n) { |
| 86 | if (!prev) { fill_seed(token, out, n); return; } |
| 87 | const uint32_t *p = (const uint32_t *)prev; |
| 88 | uint32_t *o = (uint32_t *)out; |
| 89 | size_t w = n / 4, m = w - 1; |
| 90 | uint32_t t = (uint32_t)(token * 0x9e3779b1u) ^ (uint32_t)(token >> 32); |
| 91 | for (size_t j = 0; j < w; j++) { |
| 92 | uint32_t v = p[j] ^ t; |
| 93 | for (int r = 0; r < 6; r++) { |
| 94 | v = v * 0x85ebca6bu + p[(j * 31u + 7u + (unsigned)r) & m]; |
| 95 | v = (v << 13) | (v >> 19); |
| 96 | v ^= p[(j + 1u + (unsigned)r) & m]; |
| 97 | } |
| 98 | o[j] = v; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /* Neural activation checkpoint: one dense 128x128 layer applied to 128 rows, |
| 103 | * softsign activation, bias from the token. */ |
| 104 | #define DIM 128 |
| 105 | static float g_w[DIM][DIM]; |
| 106 | |
| 107 | static void init_weights(void) { |
| 108 | uint64_t x = 0x5eed; |
| 109 | for (int i = 0; i < DIM; i++) |
| 110 | for (int k = 0; k < DIM; k++) |
| 111 | g_w[i][k] = ((float)(splitmix(&x) >> 40) / (float)(1u << 24) - 0.5f) * 0.25f; |
| 112 | } |
| 113 | |
| 114 | static void derive_activation(const uint8_t *prev, uint64_t token, uint8_t *out, size_t n) { |
| 115 | float *o = (float *)out; |
| 116 | if (!prev) { |
| 117 | uint64_t x = token; |
| 118 | for (size_t i = 0; i < n / 4; i++) o[i] = (float)(splitmix(&x) >> 40) / (float)(1u << 24) - 0.5f; |
| 119 | return; |
| 120 | } |
| 121 | const float *p = (const float *)prev; |
| 122 | uint64_t x = token; |
| 123 | float bias[DIM]; |
| 124 | for (int i = 0; i < DIM; i++) bias[i] = (float)(splitmix(&x) >> 40) / (float)(1u << 24) * 0.1f; |
| 125 | for (int r = 0; r < DIM; r++) { |
| 126 | const float *in = p + r * DIM; |
| 127 | for (int i = 0; i < DIM; i++) { |
| 128 | float acc = bias[i]; |
| 129 | for (int k = 0; k < DIM; k++) acc += g_w[i][k] * in[k]; |
| 130 | o[r * DIM + i] = acc / (1.0f + fabsf(acc)); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /* Attention/KV state: a window of 128 rows of 512 bytes; each step drops the |
| 136 | * oldest row and appends one computed from the token and the newest row. */ |
| 137 | #define KV_ROW 512u |
| 138 | static void derive_kv(const uint8_t *prev, uint64_t token, uint8_t *out, size_t n) { |
| 139 | if (!prev) { fill_seed(token, out, n); return; } |
| 140 | memcpy(out, prev + KV_ROW, n - KV_ROW); |
| 141 | const uint8_t *last = prev + n - KV_ROW; |
| 142 | uint8_t *row = out + n - KV_ROW; |
| 143 | uint64_t x = token; |
| 144 | for (unsigned i = 0; i < KV_ROW; i += 8) { |
| 145 | uint64_t a, k = splitmix(&x); |
| 146 | memcpy(&a, last + i, 8); |
| 147 | a = (a ^ k) * 0xff51afd7ed558ccdull; |
| 148 | memcpy(row + i, &a, 8); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /* World projection: 128 objects of 512 bytes. A step re-derives only the 8 |
| 153 | * objects its token touches (a sparse update), each through 256 mixing rounds; |
| 154 | * the rest carry over unchanged. */ |
| 155 | #define WORLD_OBJ 512u |
| 156 | static void derive_world(const uint8_t *prev, uint64_t token, uint8_t *out, size_t n) { |
| 157 | if (!prev) { fill_seed(token, out, n); return; } |
| 158 | memcpy(out, prev, n); |
| 159 | uint64_t x = token; |
| 160 | unsigned objs = (unsigned)(n / WORLD_OBJ); |
| 161 | for (int u = 0; u < 8; u++) { |
| 162 | unsigned obj = (unsigned)(splitmix(&x) % objs); |
| 163 | const uint64_t *p = (const uint64_t *)(prev + (size_t)obj * WORLD_OBJ); |
| 164 | uint64_t *o = (uint64_t *)(out + (size_t)obj * WORLD_OBJ); |
| 165 | for (unsigned w = 0; w < WORLD_OBJ / 8; w++) { |
| 166 | uint64_t v = p[w] ^ x; |
| 167 | for (unsigned r = 0; r < 256; r++) { |
| 168 | v += p[(w + r) & (WORLD_OBJ / 8 - 1)]; |
| 169 | v ^= v >> 29; v *= 0xbf58476d1ce4e5b9ull; |
| 170 | } |
| 171 | o[w] = v; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | static const JsRealizer g_realizers[] = { |
| 177 | { JS_REAL_LATENT_CHECKPOINT, "latent_checkpoint", UNIT_BYTES, derive_latent }, |
| 178 | { JS_REAL_ACTIVATION_CHECKPOINT, "activation_checkpoint", UNIT_BYTES, derive_activation }, |
| 179 | { JS_REAL_KV_STATE, "kv_state", UNIT_BYTES, derive_kv }, |
| 180 | { JS_REAL_WORLD_PROJECTION, "world_projection", UNIT_BYTES, derive_world }, |
| 181 | }; |
| 182 | #define N_REAL (sizeof g_realizers / sizeof g_realizers[0]) |
| 183 | |
| 184 | /* ---- the branch program (identical for both runs) ------------------------------- */ |
| 185 | |
| 186 | #define ROOT_SEED 0xa1e5a1e5ull |
| 187 | static uint64_t prefix_token(unsigned i) { return 1000u + i; } |
| 188 | static unsigned choice_of(unsigned k) { return k % g_choices; } |
| 189 | static uint64_t diverge_token(unsigned k, unsigned j) { return (uint64_t)(choice_of(k) + 1) * 1000003ull + j; } |
| 190 | static void edit_of(unsigned k, unsigned e, uint32_t *idx, uint32_t *off, uint8_t patch[EDIT_LEN]) { |
| 191 | unsigned c = choice_of(k); |
| 192 | *idx = (c * 37u + e * 101u) % g_prefix; |
| 193 | *off = ((c * 13u + e * 7u) % (UNIT_BYTES / EDIT_LEN)) * EDIT_LEN; |
| 194 | fill_seed(0xed17ull * (c + 1) + e, patch, EDIT_LEN); |
| 195 | } |
| 196 | |
| 197 | /* ---- measurement ------------------------------------------------------------------ */ |
| 198 | |
| 199 | static uint64_t now_ns(void) { |
| 200 | struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); |
| 201 | return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; |
| 202 | } |
| 203 | static uint64_t cpu_ns(void) { |
| 204 | struct timespec ts; clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); |
| 205 | return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; |
| 206 | } |
| 207 | static uint64_t rss_bytes(void) { |
| 208 | unsigned long long size = 0, res = 0; |
| 209 | FILE *f = fopen("/proc/self/statm", "r"); |
| 210 | if (f) { if (fscanf(f, "%llu %llu", &size, &res) != 2) res = 0; fclose(f); } |
| 211 | return res * (uint64_t)sysconf(_SC_PAGESIZE); |
| 212 | } |
| 213 | |
| 214 | /* GB10 energy counters (hwmon "aien_spbm", microjoules): pkg, cpu_e, cpu_p. */ |
| 215 | static char g_hwmon[320]; |
| 216 | static void find_hwmon(void) { |
| 217 | DIR *d = opendir("/sys/class/hwmon"); |
| 218 | struct dirent *e; |
| 219 | while (d && (e = readdir(d))) { |
| 220 | char p[512], name[64] = ""; |
| 221 | snprintf(p, sizeof p, "/sys/class/hwmon/%s/name", e->d_name); |
| 222 | FILE *f = fopen(p, "r"); |
| 223 | if (!f) continue; |
| 224 | if (fgets(name, sizeof name, f) && !strncmp(name, "aien_spbm", 9)) |
| 225 | snprintf(g_hwmon, sizeof g_hwmon, "/sys/class/hwmon/%s", e->d_name); |
| 226 | fclose(f); |
| 227 | } |
| 228 | if (d) closedir(d); |
| 229 | } |
| 230 | static bool read_energy(uint64_t out[3]) { |
| 231 | if (!g_hwmon[0]) return false; |
| 232 | for (int i = 0; i < 3; i++) { |
| 233 | char p[340]; |
| 234 | snprintf(p, sizeof p, "%.300s/energy%d_input", g_hwmon, i + 1); |
| 235 | FILE *f = fopen(p, "r"); |
| 236 | unsigned long long v = 0; |
| 237 | if (!f) return false; |
| 238 | int ok = fscanf(f, "%llu", &v) == 1; |
| 239 | fclose(f); |
| 240 | if (!ok) return false; |
| 241 | out[i] = v; |
| 242 | } |
| 243 | return true; |
| 244 | } |
| 245 | |
| 246 | static int pin_performance_core(void) { |
| 247 | for (int cpu = 63; cpu >= 0; cpu--) { |
| 248 | char p[128]; |
| 249 | snprintf(p, sizeof p, "/sys/devices/system/cpu/cpu%d/regs/identification/midr_el1", cpu); |
| 250 | FILE *f = fopen(p, "r"); |
| 251 | unsigned long long midr = 0; |
| 252 | if (!f) continue; |
| 253 | int ok = fscanf(f, "%llx", &midr) == 1; |
| 254 | fclose(f); |
| 255 | if (ok && ((midr >> 4) & 0xfffu) == 0xd85u) { /* Cortex-X925 */ |
| 256 | cpu_set_t set; CPU_ZERO(&set); CPU_SET(cpu, &set); |
| 257 | if (sched_setaffinity(0, sizeof set, &set) == 0) return cpu; |
| 258 | } |
| 259 | } |
| 260 | return -1; |
| 261 | } |
| 262 | |
| 263 | /* ---- per-run results (shared with the parent through an anonymous mapping) ----- */ |
| 264 | |
| 265 | typedef struct { |
| 266 | int ok; |
| 267 | uint64_t wall_ns, cpu_ns, derives; |
| 268 | uint64_t held_bytes; /* bytes of state held when all branches are ready */ |
| 269 | uint64_t rss_growth; |
| 270 | bool energy_ok; |
| 271 | uint64_t energy_uj[3]; /* per construction: pkg, cpu_e, cpu_p */ |
| 272 | uint64_t energy_reps, energy_window_ns; |
| 273 | uint64_t first_ready_ns; /* start to first branch ready */ |
| 274 | double branch_ready_mean_ns, branch_ready_p99_ns; |
| 275 | double create_median_ns, create_p99_ns, create_max_ns; |
| 276 | uint8_t digest[MAX_BRANCHES][32]; |
| 277 | uint8_t semid[MAX_BRANCHES][32]; |
| 278 | /* shared-run only */ |
| 279 | uint64_t actions[JS_ACT_COUNT]; |
| 280 | uint64_t live_reals, bytes_copied, reuse_pairs_shared; |
| 281 | int ancestor_intact, frozen_refused, cow_isolated, reuse_ok, pressure_ok, placement_cycle_ok; |
| 282 | JsPolicyReport policy; |
| 283 | uint64_t budget_bytes, resident_after_policy, spilled_after_policy; |
| 284 | uint32_t placement_after[6]; |
| 285 | JsCosts costs; |
| 286 | char fail_msg[256]; |
| 287 | } RunResult; |
| 288 | |
| 289 | static int cmp_double(const void *a, const void *b) { |
| 290 | double x = *(const double *)a, y = *(const double *)b; |
| 291 | return (x > y) - (x < y); |
| 292 | } |
| 293 | static double pct(double *v, unsigned n, double q) { |
| 294 | qsort(v, n, sizeof *v, cmp_double); |
| 295 | unsigned i = (unsigned)(q * (n - 1) + 0.5); |
| 296 | return v[i]; |
| 297 | } |
| 298 | |
| 299 | /* Wait for the next counter tick so a window starts and ends on one. */ |
| 300 | static bool energy_edge(uint64_t e[3]) { |
| 301 | uint64_t a[3], deadline = now_ns() + 500000000ull; |
| 302 | if (!read_energy(a)) return false; |
| 303 | do { if (!read_energy(e)) return false; } while (e[0] == a[0] && now_ns() < deadline); |
| 304 | return e[0] != a[0]; |
| 305 | } |
| 306 | |
| 307 | static void begin(RunResult *R, uint64_t *t0, uint64_t *c0, uint64_t *r0, uint64_t e0[3]) { |
| 308 | R->energy_ok = energy_edge(e0); |
| 309 | *r0 = rss_bytes(); |
| 310 | *c0 = cpu_ns(); |
| 311 | *t0 = now_ns(); |
| 312 | } |
| 313 | static void end(RunResult *R, uint64_t t0, uint64_t c0, uint64_t r0, const uint64_t e0[3]) { |
| 314 | uint64_t e1[3]; |
| 315 | R->wall_ns = now_ns() - t0; |
| 316 | R->cpu_ns = cpu_ns() - c0; |
| 317 | uint64_t r1 = rss_bytes(); |
| 318 | R->rss_growth = r1 > r0 ? r1 - r0 : 0; |
| 319 | if (R->energy_ok && energy_edge(e1)) { |
| 320 | for (int i = 0; i < 3; i++) { |
| 321 | if (e1[i] < e0[i]) R->energy_ok = false; |
| 322 | R->energy_uj[i] = e1[i] - e0[i]; |
| 323 | } |
| 324 | R->energy_reps = 1; |
| 325 | R->energy_window_ns = now_ns() - t0; |
| 326 | } else R->energy_ok = false; |
| 327 | } |
| 328 | |
| 329 | /* A run shorter than the window is too short for 100 ms counters: repeat the |
| 330 | * whole construction (and teardown) until the window is long enough, starting |
| 331 | * and ending on a tick, and report energy per construction. */ |
| 332 | static void energy_by_repetition(RunResult *R, void (*rep)(void *), void *ctx) { |
| 333 | if (!R->energy_ok || R->energy_window_ns >= ENERGY_WINDOW_NS) return; |
| 334 | uint64_t e0[3], e1[3], e_at[3]; |
| 335 | if (!energy_edge(e0)) { R->energy_ok = false; return; } |
| 336 | uint64_t t0 = now_ns(), reps = 0; |
| 337 | bool armed = false; |
| 338 | for (;;) { |
| 339 | rep(ctx); |
| 340 | reps++; |
| 341 | if (!armed && now_ns() - t0 >= ENERGY_WINDOW_NS) { |
| 342 | if (!read_energy(e_at)) { R->energy_ok = false; return; } |
| 343 | armed = true; |
| 344 | } else if (armed) { |
| 345 | if (!read_energy(e1)) { R->energy_ok = false; return; } |
| 346 | if (e1[0] != e_at[0]) break; /* a tick landed during this construction */ |
| 347 | } |
| 348 | } |
| 349 | R->energy_window_ns = now_ns() - t0; |
| 350 | R->energy_reps = reps; |
| 351 | for (int i = 0; i < 3; i++) R->energy_uj[i] = (e1[i] - e0[i]) / reps; |
| 352 | } |
| 353 | |
| 354 | /* ---- independent recomputation ---------------------------------------------------- */ |
| 355 | |
| 356 | static void fail(RunResult *R, const char *m) { |
| 357 | if (!R->fail_msg[0]) snprintf(R->fail_msg, sizeof R->fail_msg, "%s", m); |
| 358 | } |
| 359 | |
| 360 | /* Branch k's whole state, computed from the seed. */ |
| 361 | static uint8_t *indep_branch(const JsRealizer *rz, unsigned k) { |
| 362 | unsigned units = g_prefix + 1 + g_diverge; |
| 363 | uint8_t *s = malloc((size_t)units * UNIT_BYTES); |
| 364 | if (!s) return NULL; |
| 365 | rz->derive(NULL, ROOT_SEED, s, UNIT_BYTES); |
| 366 | for (unsigned i = 0; i < g_prefix; i++) |
| 367 | rz->derive(s + (size_t)i * UNIT_BYTES, prefix_token(i), s + (size_t)(i + 1) * UNIT_BYTES, UNIT_BYTES); |
| 368 | for (unsigned j = 0; j < g_diverge; j++) { |
| 369 | unsigned i = g_prefix + j; |
| 370 | rz->derive(s + (size_t)i * UNIT_BYTES, diverge_token(k, j), s + (size_t)(i + 1) * UNIT_BYTES, UNIT_BYTES); |
| 371 | } |
| 372 | for (unsigned e = 0; e < EDITS; e++) { |
| 373 | uint32_t idx, off; uint8_t patch[EDIT_LEN]; |
| 374 | edit_of(k, e, &idx, &off, patch); |
| 375 | memcpy(s + (size_t)(idx + 1) * UNIT_BYTES + off, patch, EDIT_LEN); /* unit idx+1: after the root */ |
| 376 | } |
| 377 | return s; |
| 378 | } |
| 379 | |
| 380 | /* One full construction and teardown, for energy repetition. */ |
| 381 | static void indep_rep(void *ctx) { |
| 382 | const JsRealizer *rz = ctx; |
| 383 | uint8_t **state = calloc(g_branches, sizeof *state); |
| 384 | for (unsigned k = 0; state && k < g_branches; k++) state[k] = indep_branch(rz, k); |
| 385 | for (unsigned k = 0; state && k < g_branches; k++) free(state[k]); |
| 386 | free(state); |
| 387 | } |
| 388 | |
| 389 | static void run_independent(const JsRealizer *rz, RunResult *R) { |
| 390 | unsigned units = g_prefix + 1 + g_diverge; |
| 391 | uint8_t **state = calloc(g_branches, sizeof *state); |
| 392 | double *ready = malloc(g_branches * sizeof *ready); |
| 393 | uint64_t t0, c0, r0, e0[3]; |
| 394 | begin(R, &t0, &c0, &r0, e0); |
| 395 | for (unsigned k = 0; k < g_branches; k++) { |
| 396 | uint64_t bt = now_ns(); |
| 397 | /* Creating an independent branch is creating its whole state. */ |
| 398 | state[k] = indep_branch(rz, k); |
| 399 | if (!state[k]) { fail(R, "out of memory"); return; } |
| 400 | R->derives += 1 + g_prefix + g_diverge; |
| 401 | ready[k] = (double)(now_ns() - bt); |
| 402 | if (k == 0) R->first_ready_ns = now_ns() - t0; |
| 403 | } |
| 404 | end(R, t0, c0, r0, e0); |
| 405 | R->held_bytes = (uint64_t)g_branches * units * UNIT_BYTES; |
| 406 | double sum = 0; |
| 407 | for (unsigned k = 0; k < g_branches; k++) sum += ready[k]; |
| 408 | R->branch_ready_mean_ns = sum / g_branches; |
| 409 | R->branch_ready_p99_ns = pct(ready, g_branches, 0.99); |
| 410 | R->create_median_ns = pct(ready, g_branches, 0.5); |
| 411 | R->create_p99_ns = pct(ready, g_branches, 0.99); |
| 412 | R->create_max_ns = ready[g_branches - 1]; |
| 413 | for (unsigned k = 0; k < g_branches; k++) { |
| 414 | sha256_hash(state[k], (size_t)units * UNIT_BYTES, R->digest[k]); |
| 415 | free(state[k]); |
| 416 | } |
| 417 | free(state); free(ready); |
| 418 | energy_by_repetition(R, indep_rep, (void *)rz); |
| 419 | R->ok = 1; |
| 420 | } |
| 421 | |
| 422 | /* ---- shared-state realization ------------------------------------------------------- */ |
| 423 | |
| 424 | /* Root, common prefix, then every branch: fork, private steps, edits. */ |
| 425 | static int shared_construct(JsSpace *S, const JsRealizer *rz, uint32_t *root, uint32_t *ids, |
| 426 | double *create, double *ready, uint64_t t0, uint64_t *first_ready) { |
| 427 | int rc = js_branch_root(S, rz, ROOT_SEED, root); |
| 428 | for (unsigned i = 0; i < g_prefix && rc == JS_OK; i++) rc = js_branch_derive(S, *root, prefix_token(i)); |
| 429 | for (unsigned k = 0; k < g_branches && rc == JS_OK; k++) { |
| 430 | uint64_t bt = now_ns(); |
| 431 | rc = js_branch_fork(S, *root, &ids[k]); |
| 432 | if (create) create[k] = (double)(now_ns() - bt); |
| 433 | for (unsigned j = 0; j < g_diverge && rc == JS_OK; j++) rc = js_branch_derive(S, ids[k], diverge_token(k, j)); |
| 434 | for (unsigned e = 0; e < EDITS && rc == JS_OK; e++) { |
| 435 | uint32_t idx, off; uint8_t patch[EDIT_LEN]; |
| 436 | edit_of(k, e, &idx, &off, patch); |
| 437 | rc = js_branch_edit(S, ids[k], idx + 1, off, patch, EDIT_LEN); |
| 438 | } |
| 439 | if (ready) ready[k] = (double)(now_ns() - bt); |
| 440 | if (k == 0 && first_ready) *first_ready = now_ns() - t0; |
| 441 | } |
| 442 | return rc; |
| 443 | } |
| 444 | |
| 445 | typedef struct { const JsRealizer *rz; const char *spill; JsCosts costs; } SharedRepCtx; |
| 446 | |
| 447 | static void shared_rep(void *p) { |
| 448 | SharedRepCtx *c = p; |
| 449 | JsSpace *S = calloc(1, sizeof *S); |
| 450 | uint32_t root, *ids = malloc(g_branches * sizeof *ids); |
| 451 | if (S && ids && js_space_init(S, c->spill) == JS_OK) { |
| 452 | S->costs = c->costs; |
| 453 | shared_construct(S, c->rz, &root, ids, NULL, NULL, now_ns(), NULL); |
| 454 | js_space_destroy(S); |
| 455 | } |
| 456 | free(S); free(ids); |
| 457 | } |
| 458 | |
| 459 | static void run_shared(const JsRealizer *rz, RunResult *R, const char *spill) { |
| 460 | JsSpace *S = calloc(1, sizeof *S); |
| 461 | if (!S || js_space_init(S, spill) != JS_OK) { fail(R, "space init"); return; } |
| 462 | if (js_calibrate(S, rz) != JS_OK) { fail(R, "calibrate"); js_space_destroy(S); free(S); return; } |
| 463 | R->costs = S->costs; |
| 464 | uint64_t derives0 = S->stats.derives; |
| 465 | memset(S->stats.actions, 0, sizeof S->stats.actions); |
| 466 | uint32_t *ids = malloc(g_branches * sizeof *ids); |
| 467 | double *create = malloc(g_branches * sizeof *create), *ready = malloc(g_branches * sizeof *ready); |
| 468 | uint32_t root; |
| 469 | uint64_t t0, c0, r0, e0[3]; |
| 470 | |
| 471 | begin(R, &t0, &c0, &r0, e0); |
| 472 | int rc = shared_construct(S, rz, &root, ids, create, ready, t0, &R->first_ready_ns); |
| 473 | end(R, t0, c0, r0, e0); |
| 474 | if (rc != JS_OK) { fail(R, "branch construction"); goto out; } |
| 475 | R->derives = S->stats.derives - derives0; |
| 476 | R->held_bytes = S->stats.peak_resident_bytes; |
| 477 | { |
| 478 | double sum = 0; |
| 479 | for (unsigned k = 0; k < g_branches; k++) sum += ready[k]; |
| 480 | R->branch_ready_mean_ns = sum / g_branches; |
| 481 | R->branch_ready_p99_ns = pct(ready, g_branches, 0.99); |
| 482 | R->create_max_ns = 0; |
| 483 | for (unsigned k = 0; k < g_branches; k++) if (create[k] > R->create_max_ns) R->create_max_ns = create[k]; |
| 484 | R->create_median_ns = pct(create, g_branches, 0.5); |
| 485 | R->create_p99_ns = pct(create, g_branches, 0.99); |
| 486 | } |
| 487 | memcpy(R->actions, S->stats.actions, sizeof R->actions); |
| 488 | R->live_reals = S->stats.live_reals; |
| 489 | R->bytes_copied = S->stats.bytes_copied; |
| 490 | |
| 491 | /* --- correctness, outside the measured window --- */ |
| 492 | uint8_t anc_digest[32], anc_digest2[32]; |
| 493 | JsSemId anc_sem = js_branch_semantic_id(S->branches[root]); |
| 494 | if (js_branch_content_digest(S, root, anc_digest) != JS_OK) { fail(R, "ancestor digest"); goto out; } |
| 495 | |
| 496 | /* The shared ancestor: every unit still holds the prefix realizations in |
| 497 | * order, its identity and bytes match a fresh prefix, and it refuses writes. */ |
| 498 | { |
| 499 | JsSpace *F = calloc(1, sizeof *F); |
| 500 | uint32_t fr; uint8_t fresh[32]; |
| 501 | int ok = F && js_space_init(F, spill) == JS_OK && js_branch_root(F, rz, ROOT_SEED, &fr) == JS_OK; |
| 502 | for (unsigned i = 0; ok && i < g_prefix; i++) ok = js_branch_derive(F, fr, prefix_token(i)) == JS_OK; |
| 503 | ok = ok && js_branch_content_digest(F, fr, fresh) == JS_OK; |
| 504 | JsSemId fsem = ok ? js_branch_semantic_id(F->branches[fr]) : anc_sem; |
| 505 | R->ancestor_intact = ok && !memcmp(fresh, anc_digest, 32) && !memcmp(fsem.b, anc_sem.b, 32); |
| 506 | if (F) { js_space_destroy(F); free(F); } |
| 507 | } |
| 508 | { |
| 509 | uint8_t patch[EDIT_LEN] = { 1 }; |
| 510 | int a = js_branch_edit(S, root, 5, 0, patch, EDIT_LEN); |
| 511 | int b = js_branch_derive(S, root, 42); |
| 512 | js_branch_content_digest(S, root, anc_digest2); |
| 513 | R->frozen_refused = a == JS_ERR_FROZEN && b == JS_ERR_FROZEN && !memcmp(anc_digest, anc_digest2, 32); |
| 514 | } |
| 515 | /* Copy on write: an edited unit is private to the branches that made that |
| 516 | * edit; every other branch still holds the ancestor's realization. */ |
| 517 | { |
| 518 | int iso = 1; |
| 519 | JsBranch *rb = S->branches[root]; |
| 520 | for (unsigned k = 0; k < g_branches; k++) { |
| 521 | JsBranch *b = S->branches[ids[k]]; |
| 522 | for (unsigned i = 0; i < g_prefix + 1; i++) { |
| 523 | bool edited = false; |
| 524 | for (unsigned e = 0; e < EDITS; e++) { |
| 525 | uint32_t idx, off; uint8_t patch[EDIT_LEN]; |
| 526 | edit_of(k, e, &idx, &off, patch); |
| 527 | if (idx + 1 == i) edited = true; |
| 528 | } |
| 529 | if (edited == (b->units[i] == rb->units[i])) iso = 0; |
| 530 | } |
| 531 | } |
| 532 | R->cow_isolated = iso; |
| 533 | } |
| 534 | /* Semantic reuse: branches k and k + g_choices took the same steps, so they hold |
| 535 | * the same realizations; branches that differ share none past the fork. */ |
| 536 | { |
| 537 | int ok = 1; |
| 538 | for (unsigned k = 0; k + g_choices < g_branches; k++) { |
| 539 | JsBranch *a = S->branches[ids[k]], *b = S->branches[ids[k + g_choices]]; |
| 540 | for (unsigned i = 0; i < a->n_units; i++) if (a->units[i] != b->units[i]) ok = 0; |
| 541 | R->reuse_pairs_shared++; |
| 542 | } |
| 543 | if (g_branches > 1) { |
| 544 | JsBranch *a = S->branches[ids[0]], *b = S->branches[ids[1]]; |
| 545 | if (a->units[a->n_units - 1] == b->units[b->n_units - 1]) ok = 0; |
| 546 | } |
| 547 | R->reuse_ok = ok && R->reuse_pairs_shared > 0; |
| 548 | } |
| 549 | for (unsigned k = 0; k < g_branches; k++) { |
| 550 | JsSemId sid = js_branch_semantic_id(S->branches[ids[k]]); |
| 551 | memcpy(R->semid[k], sid.b, 32); |
| 552 | } |
| 553 | |
| 554 | /* --- memory pressure: FORGE decides per realization, then everything is |
| 555 | * read back through whatever placement it chose --- */ |
| 556 | R->budget_bytes = S->stats.resident_bytes / 4; |
| 557 | if (js_forge_enforce(S, R->budget_bytes, &R->policy) != JS_OK) { fail(R, "policy enforce"); goto out; } |
| 558 | R->resident_after_policy = S->stats.resident_bytes; |
| 559 | R->spilled_after_policy = S->stats.spilled_bytes; |
| 560 | for (uint32_t k = 0; k < JS_INDEX_BUCKETS; k++) |
| 561 | for (JsReal *r = S->index[k]; r; r = r->index_next) |
| 562 | if (r->placement < 6) R->placement_after[r->placement]++; |
| 563 | { |
| 564 | int ok = R->resident_after_policy <= R->budget_bytes; |
| 565 | for (unsigned k = 0; k < g_branches; k++) { |
| 566 | JsSemId sid = js_branch_semantic_id(S->branches[ids[k]]); |
| 567 | if (memcmp(sid.b, R->semid[k], 32)) ok = 0; |
| 568 | if (js_branch_content_digest(S, ids[k], R->digest[k]) != JS_OK) ok = 0; |
| 569 | } |
| 570 | if (js_branch_content_digest(S, root, anc_digest2) != JS_OK || memcmp(anc_digest, anc_digest2, 32)) ok = 0; |
| 571 | if (S->stats.corrupt_restores) ok = 0; |
| 572 | R->pressure_ok = ok; |
| 573 | /* Actions of construction, the policy and the reads that followed. |
| 574 | * The forced placement cycle below is a correctness probe and is not |
| 575 | * counted toward the gate. */ |
| 576 | memcpy(R->actions, S->stats.actions, sizeof R->actions); |
| 577 | } |
| 578 | /* One branch's every unit walked through every placement, checked each step. */ |
| 579 | { |
| 580 | JsBranch *b = S->branches[ids[g_branches - 1]]; |
| 581 | uint8_t *want = malloc(UNIT_BYTES), *got = malloc(UNIT_BYTES); |
| 582 | JsSemId before = js_branch_semantic_id(b); |
| 583 | int ok = want && got; |
| 584 | for (unsigned i = 0; ok && i < b->n_units; i++) { |
| 585 | JsReal *r = b->units[i]; |
| 586 | ok = js_branch_read(S, ids[g_branches - 1], i, want) == JS_OK; |
| 587 | int (*ops[])(JsSpace *, JsReal *) = { js_real_restore, js_real_move, js_real_compress, |
| 588 | js_real_spill, js_real_evict, js_real_restore }; |
| 589 | for (unsigned o = 0; ok && o < sizeof ops / sizeof ops[0]; o++) { |
| 590 | if (ops[o] == js_real_move && r->placement != JS_PLACE_HOT) continue; |
| 591 | ok = ops[o](S, r) == JS_OK && |
| 592 | js_branch_read(S, ids[g_branches - 1], i, got) == JS_OK && |
| 593 | !memcmp(want, got, UNIT_BYTES); |
| 594 | } |
| 595 | } |
| 596 | JsSemId after = js_branch_semantic_id(b); |
| 597 | R->placement_cycle_ok = ok && !memcmp(before.b, after.b, 32); |
| 598 | free(want); free(got); |
| 599 | } |
| 600 | R->ok = 1; |
| 601 | out: |
| 602 | js_space_destroy(S); |
| 603 | if (R->ok) { |
| 604 | SharedRepCtx c = { rz, spill, R->costs }; |
| 605 | energy_by_repetition(R, shared_rep, &c); |
| 606 | } |
| 607 | free(S); free(ids); free(create); free(ready); |
| 608 | } |
| 609 | |
| 610 | /* ---- driver ------------------------------------------------------------------------ */ |
| 611 | |
| 612 | static RunResult *run_child(void (*fn)(const JsRealizer *, RunResult *, const char *), |
| 613 | const JsRealizer *rz, const char *spill) { |
| 614 | RunResult *R = mmap(NULL, sizeof *R, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); |
| 615 | if (R == MAP_FAILED) return NULL; |
| 616 | memset(R, 0, sizeof *R); |
| 617 | fflush(stdout); |
| 618 | pid_t pid = fork(); |
| 619 | if (pid == 0) { |
| 620 | pin_performance_core(); |
| 621 | fn(rz, R, spill); |
| 622 | _exit(0); |
| 623 | } |
| 624 | int st = 0; |
| 625 | waitpid(pid, &st, 0); |
| 626 | if (!WIFEXITED(st) || WEXITSTATUS(st)) R->ok = 0; |
| 627 | return R; |
| 628 | } |
| 629 | static void indep_adapter(const JsRealizer *rz, RunResult *R, const char *spill) { |
| 630 | (void)spill; run_independent(rz, R); |
| 631 | } |
| 632 | |
| 633 | typedef struct { |
| 634 | const JsRealizer *rz; |
| 635 | RunResult *ind, *sh; |
| 636 | int digests_match, beats_memory, beats_compute, beats_latency, beats_energy, correct; |
| 637 | } RealizerOutcome; |
| 638 | |
| 639 | static double ms(double ns) { return ns / 1e6; } |
| 640 | static double mib(uint64_t b) { return (double)b / 1048576.0; } |
| 641 | static double joules(uint64_t uj) { return (double)uj / 1e6; } |
| 642 | |
| 643 | static void hex(const uint8_t *b, char *out) { for (int i = 0; i < 32; i++) sprintf(out + 2 * i, "%02x", b[i]); } |
| 644 | |
| 645 | int main(int argc, char **argv) { |
| 646 | if (argc > 1) g_branches = (unsigned)atoi(argv[1]); |
| 647 | if (argc > 2) g_prefix = (unsigned)atoi(argv[2]); |
| 648 | if (argc > 3) g_diverge = (unsigned)atoi(argv[3]); |
| 649 | if (g_branches < 2 || g_branches > MAX_BRANCHES || g_prefix < 8 || !g_diverge) { |
| 650 | fprintf(stderr, "usage: %s [branches 2..%u] [prefix >=8] [diverge >=1]\n", argv[0], MAX_BRANCHES); |
| 651 | return 2; |
| 652 | } |
| 653 | g_choices = g_branches - g_branches / 4; |
| 654 | init_weights(); |
| 655 | find_hwmon(); |
| 656 | char spill[512]; |
| 657 | if (omega_evidence_path("BRANCH_REUSE/spill.bin", spill, sizeof spill) != 0) return 2; |
| 658 | |
| 659 | printf("OMEGA branch-state reuse: %u branches, %u-unit common prefix, %u private units + %u edits each," |
| 660 | " %u-byte units (prefix %.0f MiB)\n", g_branches, g_prefix, g_diverge, EDITS, UNIT_BYTES, |
| 661 | mib((uint64_t)(g_prefix + 1) * UNIT_BYTES)); |
| 662 | printf("energy counters: %s\n", g_hwmon[0] ? g_hwmon : "NOT FOUND"); |
| 663 | |
| 664 | RealizerOutcome out[N_REAL]; |
| 665 | for (unsigned t = 0; t < N_REAL; t++) { |
| 666 | const JsRealizer *rz = &g_realizers[t]; |
| 667 | RealizerOutcome *o = &out[t]; |
| 668 | memset(o, 0, sizeof *o); |
| 669 | o->rz = rz; |
| 670 | printf("\n[%s]\n", rz->name); |
| 671 | o->ind = run_child(indep_adapter, rz, spill); |
| 672 | o->sh = run_child(run_shared, rz, spill); |
| 673 | RunResult *I = o->ind, *S = o->sh; |
| 674 | CHECK(I && I->ok, "%s: independent run did not complete", rz->name); |
| 675 | CHECK(S && S->ok, "%s: shared run did not complete (%s)", rz->name, S ? S->fail_msg : "?"); |
| 676 | if (!I || !S || !I->ok || !S->ok) continue; |
| 677 | o->digests_match = 1; |
| 678 | for (unsigned k = 0; k < g_branches; k++) if (memcmp(I->digest[k], S->digest[k], 32)) o->digests_match = 0; |
| 679 | CHECK(o->digests_match, "%s: a shared branch's bytes differ from its independent recomputation", rz->name); |
| 680 | CHECK(S->ancestor_intact, "%s: shared ancestor changed", rz->name); |
| 681 | CHECK(S->frozen_refused, "%s: a write to the frozen ancestor was not refused", rz->name); |
| 682 | CHECK(S->cow_isolated, "%s: copy-on-write isolation broken", rz->name); |
| 683 | CHECK(S->reuse_ok, "%s: semantic reuse did not share identical branches", rz->name); |
| 684 | CHECK(S->pressure_ok, "%s: identity or bytes changed under the placement policy", rz->name); |
| 685 | CHECK(S->placement_cycle_ok, "%s: a placement cycle changed bytes or identity", rz->name); |
| 686 | o->correct = o->digests_match && S->ancestor_intact && S->frozen_refused && S->cow_isolated && |
| 687 | S->reuse_ok && S->pressure_ok && S->placement_cycle_ok; |
| 688 | o->beats_memory = S->held_bytes < I->held_bytes && S->rss_growth < I->rss_growth; |
| 689 | o->beats_compute = S->derives < I->derives && S->cpu_ns < I->cpu_ns; |
| 690 | o->beats_latency = S->wall_ns < I->wall_ns && S->branch_ready_mean_ns < I->branch_ready_mean_ns; |
| 691 | o->beats_energy = I->energy_ok && S->energy_ok && S->energy_uj[0] < I->energy_uj[0]; |
| 692 | CHECK(o->beats_memory, "%s: shared realization did not use less memory", rz->name); |
| 693 | CHECK(o->beats_compute, "%s: shared realization did not use less compute", rz->name); |
| 694 | CHECK(o->beats_latency, "%s: shared realization was not faster", rz->name); |
| 695 | if (!(I->energy_ok && S->energy_ok)) { |
| 696 | /* Label only: memory, compute, latency and correctness stay hard checks. */ |
| 697 | g_energy_not_run++; |
| 698 | printf(" NOT_RUN: %s: energy leg (counters unavailable)\n", rz->name); |
| 699 | } else |
| 700 | CHECK(o->beats_energy, "%s: energy not measured lower (counters ok)", rz->name); |
| 701 | |
| 702 | printf(" measured costs: derive %.1f us/unit, copy %.3f ns/B, compress %.3f ns/B (delta ratio %.3f)," |
| 703 | " spill w/r %.3f/%.3f ns/B, retain %.3f ns/B\n", |
| 704 | S->costs.derive_ns / 1e3, S->costs.copy_ns_per_byte, S->costs.compress_ns_per_byte, |
| 705 | S->costs.compress_ratio, S->costs.spill_write_ns_per_byte, S->costs.spill_read_ns_per_byte, |
| 706 | S->costs.retain_ns_per_byte); |
| 707 | printf(" %-22s %14s %14s %9s\n", "", "independent", "shared", "ratio"); |
| 708 | printf(" %-22s %14.1f %14.1f %8.1fx\n", "memory held (MiB)", mib(I->held_bytes), mib(S->held_bytes), |
| 709 | (double)I->held_bytes / (double)S->held_bytes); |
| 710 | printf(" %-22s %14.1f %14.1f %8.1fx\n", "RSS growth (MiB)", mib(I->rss_growth), mib(S->rss_growth), |
| 711 | (double)I->rss_growth / (double)(S->rss_growth ? S->rss_growth : 1)); |
| 712 | printf(" %-22s %14llu %14llu %8.1fx\n", "derive calls", U(I->derives), U(S->derives), |
| 713 | (double)I->derives / (double)S->derives); |
| 714 | printf(" %-22s %14.1f %14.1f %8.1fx\n", "CPU time (ms)", ms(I->cpu_ns), ms(S->cpu_ns), |
| 715 | (double)I->cpu_ns / (double)S->cpu_ns); |
| 716 | printf(" %-22s %14.1f %14.1f %8.1fx\n", "all branches ready (ms)", ms(I->wall_ns), ms(S->wall_ns), |
| 717 | (double)I->wall_ns / (double)S->wall_ns); |
| 718 | printf(" %-22s %14.1f %14.1f\n", "first branch ready (ms)", ms(I->first_ready_ns), ms(S->first_ready_ns)); |
| 719 | printf(" %-22s %14.3f %14.3f %8.1fx\n", "per-branch ready (ms)", ms(I->branch_ready_mean_ns), |
| 720 | ms(S->branch_ready_mean_ns), I->branch_ready_mean_ns / S->branch_ready_mean_ns); |
| 721 | printf(" %-22s %14.1f %14.4f\n", "branch create med (ms)", ms(I->create_median_ns), ms(S->create_median_ns)); |
| 722 | printf(" %-22s %14.1f %14.4f\n", "branch create p99 (ms)", ms(I->create_p99_ns), ms(S->create_p99_ns)); |
| 723 | if (I->energy_ok && S->energy_ok) { |
| 724 | printf(" %-22s %14.2f %14.2f %8.1fx\n", "energy pkg (J)", joules(I->energy_uj[0]), |
| 725 | joules(S->energy_uj[0]), (double)I->energy_uj[0] / (double)S->energy_uj[0]); |
| 726 | printf(" %-22s %14.2f %14.2f\n", "energy cpu_p (J)", joules(I->energy_uj[2]), joules(S->energy_uj[2])); |
| 727 | printf(" %-22s %14llu %14llu\n", "energy constructions", U(I->energy_reps), U(S->energy_reps)); |
| 728 | } |
| 729 | printf(" actions:"); |
| 730 | for (int a = 0; a < JS_ACT_COUNT; a++) printf(" %s=%llu", js_action_name((JsAction)a), U(S->actions[a])); |
| 731 | printf("\n policy under %.1f MiB budget: considered %llu, kept %llu, move %llu, compress %llu," |
| 732 | " spill %llu, evict %llu; resident after %.1f MiB, spilled %.1f MiB\n", |
| 733 | mib(S->budget_bytes), U(S->policy.considered), U(S->policy.kept), |
| 734 | U(S->policy.chosen[JS_ACT_MOVE]), U(S->policy.chosen[JS_ACT_COMPRESS]), |
| 735 | U(S->policy.chosen[JS_ACT_SPILL]), U(S->policy.chosen[JS_ACT_EVICT]), |
| 736 | mib(S->resident_after_policy), mib(S->spilled_after_policy)); |
| 737 | printf(" correctness: bytes %s, ancestor %s, frozen %s, copy-on-write %s, reuse %s, pressure %s," |
| 738 | " placement cycle %s\n", |
| 739 | o->digests_match ? "ok" : "FAIL", S->ancestor_intact ? "ok" : "FAIL", |
| 740 | S->frozen_refused ? "ok" : "FAIL", S->cow_isolated ? "ok" : "FAIL", |
| 741 | S->reuse_ok ? "ok" : "FAIL", S->pressure_ok ? "ok" : "FAIL", |
| 742 | S->placement_cycle_ok ? "ok" : "FAIL"); |
| 743 | } |
| 744 | |
| 745 | /* Representation independence: the same branch program gives the same |
| 746 | * semantic identities under every realizer, and different bytes. */ |
| 747 | int rep_indep = 1; |
| 748 | for (unsigned t = 1; t < N_REAL; t++) { |
| 749 | if (!out[0].sh || !out[t].sh || !out[0].sh->ok || !out[t].sh->ok) { rep_indep = 0; continue; } |
| 750 | for (unsigned k = 0; k < g_branches; k++) { |
| 751 | if (memcmp(out[0].sh->semid[k], out[t].sh->semid[k], 32)) rep_indep = 0; |
| 752 | if (!memcmp(out[0].sh->digest[k], out[t].sh->digest[k], 32)) rep_indep = 0; |
| 753 | } |
| 754 | } |
| 755 | CHECK(rep_indep, "semantic identity depends on the realization"); |
| 756 | /* All eight policy actions must have happened somewhere. */ |
| 757 | uint64_t act_total[JS_ACT_COUNT] = { 0 }; |
| 758 | for (unsigned t = 0; t < N_REAL; t++) |
| 759 | if (out[t].sh && out[t].sh->ok) |
| 760 | for (int a = 0; a < JS_ACT_COUNT; a++) act_total[a] += out[t].sh->actions[a]; |
| 761 | int all_actions = 1; |
| 762 | for (int a = 0; a < JS_ACT_COUNT; a++) if (!act_total[a]) all_actions = 0; |
| 763 | CHECK(all_actions, "not every policy action was exercised"); |
| 764 | CHECK(g_branches >= 100, "fewer than 100 branches"); |
| 765 | |
| 766 | int pass = g_fail == 0; |
| 767 | char commit[41] = "unknown", physics[64] = "unknown", path[512]; |
| 768 | if (!omega_evidence_run_commit(commit)) strcpy(commit, "unknown"); |
| 769 | if (!omega_evidence_physics_commit(physics, sizeof physics)) strcpy(physics, "unknown"); |
| 770 | int dirty = omega_evidence_tree_dirty(); |
| 771 | const char *gate = !pass ? "FAIL" : dirty ? "PASS_UNBOUND_DIRTY_TREE" |
| 772 | : g_energy_not_run ? "PASS_ENERGY_NOT_RUN" : "PASS"; |
| 773 | |
| 774 | strcpy(path, "(not written)"); |
| 775 | { |
| 776 | /* Built in memory, then stored under its content address, so a later |
| 777 | * run can never overwrite this receipt. */ |
| 778 | char *rbuf = NULL; |
| 779 | size_t rlen = 0; |
| 780 | FILE *f = open_memstream(&rbuf, &rlen); |
| 781 | if (f) { |
| 782 | fprintf(f, "{\n \"gate\": \"OMEGA_BRANCH_STATE_REUSE\",\n \"result\": \"%s\",\n" |
| 783 | " \"run_id\": \"%s\",\n \"commit\": \"%s\",\n \"tree_dirty\": %s,\n" |
| 784 | " \"physics_commit\": \"%s\",\n \"energy_counters\": \"%s\",\n" |
| 785 | " \"branches\": %u,\n \"prefix_units\": %u,\n \"diverge_units\": %u,\n" |
| 786 | " \"edits_per_branch\": %u,\n \"unit_bytes\": %u,\n" |
| 787 | " \"representation_independent_identity\": %s,\n \"all_actions_exercised\": %s,\n" |
| 788 | " \"realizers\": [\n", |
| 789 | gate, omega_evidence_run_id(), commit, dirty ? "true" : "false", physics, |
| 790 | g_hwmon[0] ? g_hwmon : "none", g_branches, g_prefix, g_diverge, EDITS, UNIT_BYTES, |
| 791 | rep_indep ? "true" : "false", all_actions ? "true" : "false"); |
| 792 | for (unsigned t = 0; t < N_REAL; t++) { |
| 793 | RunResult *I = out[t].ind, *S = out[t].sh; |
| 794 | if (!I || !S) continue; |
| 795 | char h[65]; |
| 796 | hex(S->semid[0], h); |
| 797 | fprintf(f, " {\"realizer\": \"%s\", \"type\": \"%s\", \"correct\": %s," |
| 798 | " \"bytes_equal_independent\": %s, \"ancestor_intact\": %s, \"frozen_refused\": %s," |
| 799 | " \"cow_isolated\": %s, \"reuse_ok\": %s, \"pressure_ok\": %s, \"placement_cycle_ok\": %s,\n" |
| 800 | " \"branch0_semantic_id\": \"%s\",\n" |
| 801 | " \"independent\": {\"held_bytes\": %llu, \"rss_growth\": %llu, \"derives\": %llu," |
| 802 | " \"cpu_ns\": %llu, \"wall_ns\": %llu, \"first_ready_ns\": %llu, \"branch_ready_mean_ns\": %.0f," |
| 803 | " \"branch_create_median_ns\": %.0f, \"branch_create_p99_ns\": %.0f," |
| 804 | " \"energy_ok\": %s, \"energy_uj_per_construction\": {\"pkg\": %llu, \"cpu_e\": %llu, \"cpu_p\": %llu}," |
| 805 | " \"energy_constructions\": %llu, \"energy_window_ns\": %llu},\n" |
| 806 | " \"shared\": {\"held_bytes\": %llu, \"rss_growth\": %llu, \"derives\": %llu," |
| 807 | " \"cpu_ns\": %llu, \"wall_ns\": %llu, \"first_ready_ns\": %llu, \"branch_ready_mean_ns\": %.0f," |
| 808 | " \"branch_create_median_ns\": %.0f, \"branch_create_p99_ns\": %.0f, \"branch_create_max_ns\": %.0f," |
| 809 | " \"energy_ok\": %s, \"energy_uj_per_construction\": {\"pkg\": %llu, \"cpu_e\": %llu, \"cpu_p\": %llu}," |
| 810 | " \"energy_constructions\": %llu, \"energy_window_ns\": %llu, \"live_realizations\": %llu, \"bytes_copied\": %llu},\n" |
| 811 | " \"measured_costs\": {\"derive_ns\": %.1f, \"copy_ns_per_byte\": %.4f," |
| 812 | " \"compress_ns_per_byte\": %.4f, \"decompress_ns_per_byte\": %.4f, \"compress_ratio\": %.4f," |
| 813 | " \"spill_write_ns_per_byte\": %.4f, \"spill_read_ns_per_byte\": %.4f," |
| 814 | " \"retain_ns_per_byte\": %.4f},\n \"actions\": {", |
| 815 | out[t].rz->name, js_real_type_name(out[t].rz->type), out[t].correct ? "true" : "false", |
| 816 | out[t].digests_match ? "true" : "false", S->ancestor_intact ? "true" : "false", |
| 817 | S->frozen_refused ? "true" : "false", S->cow_isolated ? "true" : "false", |
| 818 | S->reuse_ok ? "true" : "false", S->pressure_ok ? "true" : "false", |
| 819 | S->placement_cycle_ok ? "true" : "false", h, |
| 820 | U(I->held_bytes), U(I->rss_growth), U(I->derives), U(I->cpu_ns), U(I->wall_ns), |
| 821 | U(I->first_ready_ns), I->branch_ready_mean_ns, I->create_median_ns, I->create_p99_ns, |
| 822 | I->energy_ok ? "true" : "false", U(I->energy_uj[0]), U(I->energy_uj[1]), U(I->energy_uj[2]), |
| 823 | U(I->energy_reps), U(I->energy_window_ns), |
| 824 | U(S->held_bytes), U(S->rss_growth), U(S->derives), U(S->cpu_ns), U(S->wall_ns), |
| 825 | U(S->first_ready_ns), S->branch_ready_mean_ns, S->create_median_ns, S->create_p99_ns, |
| 826 | S->create_max_ns, S->energy_ok ? "true" : "false", U(S->energy_uj[0]), U(S->energy_uj[1]), |
| 827 | U(S->energy_uj[2]), U(S->energy_reps), U(S->energy_window_ns), |
| 828 | U(S->live_reals), U(S->bytes_copied), |
| 829 | S->costs.derive_ns, S->costs.copy_ns_per_byte, S->costs.compress_ns_per_byte, |
| 830 | S->costs.decompress_ns_per_byte, S->costs.compress_ratio, S->costs.spill_write_ns_per_byte, |
| 831 | S->costs.spill_read_ns_per_byte, S->costs.retain_ns_per_byte); |
| 832 | for (int a = 0; a < JS_ACT_COUNT; a++) |
| 833 | fprintf(f, "%s\"%s\": %llu", a ? ", " : "", js_action_name((JsAction)a), U(S->actions[a])); |
| 834 | fprintf(f, "},\n \"policy\": {\"budget_bytes\": %llu, \"considered\": %llu, \"kept\": %llu," |
| 835 | " \"move\": %llu, \"compress\": %llu, \"spill\": %llu, \"evict\": %llu," |
| 836 | " \"resident_after\": %llu, \"spilled_after\": %llu," |
| 837 | " \"placements_after\": {\"hot\": %u, \"cold\": %u, \"compressed\": %u, \"spilled\": %u, \"evicted\": %u}}}%s\n", |
| 838 | U(S->budget_bytes), U(S->policy.considered), U(S->policy.kept), |
| 839 | U(S->policy.chosen[JS_ACT_MOVE]), U(S->policy.chosen[JS_ACT_COMPRESS]), |
| 840 | U(S->policy.chosen[JS_ACT_SPILL]), U(S->policy.chosen[JS_ACT_EVICT]), |
| 841 | U(S->resident_after_policy), U(S->spilled_after_policy), |
| 842 | S->placement_after[JS_PLACE_HOT], S->placement_after[JS_PLACE_COLD], |
| 843 | S->placement_after[JS_PLACE_COMPRESSED], S->placement_after[JS_PLACE_SPILLED], |
| 844 | S->placement_after[JS_PLACE_EVICTED], t + 1 < N_REAL ? "," : ""); |
| 845 | } |
| 846 | fprintf(f, " ],\n \"failures\": %d\n}\n", g_fail); |
| 847 | if (fclose(f) != 0 || |
| 848 | omega_evidence_write_digest("BRANCH_REUSE/rx_branch_reuse_receipt", "json", rbuf, |
| 849 | rlen, path, sizeof path) < 0) |
| 850 | strcpy(path, "(not written)"); |
| 851 | } |
| 852 | free(rbuf); |
| 853 | } |
| 854 | printf("\nrepresentation-independent identity: %s; all eight actions exercised: %s\n", |
| 855 | rep_indep ? "yes" : "NO", all_actions ? "yes" : "NO"); |
| 856 | if (pass && !dirty && !g_energy_not_run) printf("OMEGA_BRANCH_STATE_REUSE_PASS\n"); |
| 857 | printf("gate: OMEGA_BRANCH_STATE_REUSE=%s\nreceipt: %s\n", gate, path); |
| 858 | return pass ? 0 : 1; |
| 859 | } |