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_semantic_comm.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 | * OMEGA_SEMANTIC_COMMUNICATION -- swarms of 32, 100 and 500 cognitive |
| 19 | * workers with partially overlapping tasks, served three ways from the same |
| 20 | * World at the same moment: |
| 21 | * |
| 22 | * B full-state broadcast: every live object, every field, its version and |
| 23 | * its writer crumb, to every worker, every round |
| 24 | * P semantic projection: what each worker's InformationNeed selects, fresh |
| 25 | * every round |
| 26 | * D projection with delta delivery: the first projection, then only |
| 27 | * changed fields, new evidence, invalidations, new conclusions and |
| 28 | * changed derived values |
| 29 | * |
| 30 | * Each worker then performs its operation from what it received. The answer |
| 31 | * is compared with a reference computed straight from the World under the |
| 32 | * worker's own authority (an evaluator written apart from rx_sem_project). |
| 33 | * Every required (object, field, evidence) the worker lacks or holds stale is |
| 34 | * a missing-required-information failure. Every field a worker holds that its |
| 35 | * principal may not read is a leak. |
| 36 | * |
| 37 | * The World moves between rounds: field writes, retired and replaced |
| 38 | * objects, new conclusions, a revoked and later re-granted capability. The |
| 39 | * test plays the outside: it holds the Aienux admin to give principals the |
| 40 | * authority they start with, to revoke and to re-grant. Projection is given |
| 41 | * the world and the receiver's capability table only. |
| 42 | */ |
| 43 | #include "runtime/aienos_cap.h" |
| 44 | #include "runtime/rx_semcomm.h" |
| 45 | #include "runtime/rx_world.h" |
| 46 | #include "omega_evidence.h" |
| 47 | #include "sha256.h" |
| 48 | |
| 49 | #include <dirent.h> |
| 50 | #include <sched.h> |
| 51 | #include <signal.h> |
| 52 | #include <stdio.h> |
| 53 | #include <stdlib.h> |
| 54 | #include <string.h> |
| 55 | #include <sys/utsname.h> |
| 56 | #include <time.h> |
| 57 | #include <unistd.h> |
| 58 | |
| 59 | enum { SUBJ_EXTERNAL = 100, ISSUER = 3, SUBJ_TEAM0 = 200 }; |
| 60 | |
| 61 | #define N_TEAMS 16u |
| 62 | #define N_DOMAINS 12u |
| 63 | #define N_INITIAL 180u |
| 64 | #define N_ROUNDS 24u |
| 65 | #define MAX_CONCL 40u |
| 66 | #define RES_DOM(d) (0xB000000ull + (d)) |
| 67 | #define RES_TRIGGER 0xB0000F0ull |
| 68 | #define REVOKE_ROUND 12u |
| 69 | #define REGRANT_ROUND 18u |
| 70 | #define REVOKE_TEAM 3u |
| 71 | |
| 72 | enum { TY_SENSOR = 0x51, TY_TRACK, TY_ASSET, TY_PLAN, TY_RISK, TY_CONCL }; |
| 73 | static const uint32_t ALL_TYPES[6] = { TY_SENSOR, TY_TRACK, TY_ASSET, TY_PLAN, TY_RISK, TY_CONCL }; |
| 74 | #define UNC_FIELD 7u |
| 75 | #define RISK_PRIVATE (RX_FIELD(4) | RX_FIELD(5)) |
| 76 | |
| 77 | enum { M_B = 0, M_P, M_D, M_COUNT }; |
| 78 | static const char *MODE_NAME[M_COUNT] = { "full_state_broadcast", "semantic_projection", |
| 79 | "semantic_projection_delta" }; |
| 80 | |
| 81 | static int g_checks; |
| 82 | static int g_fail; |
| 83 | |
| 84 | #define CHECK(cond, ...) do { \ |
| 85 | g_checks++; \ |
| 86 | if (!(cond)) { \ |
| 87 | g_fail++; \ |
| 88 | fprintf(stderr, " FAIL %s:%d ", __FILE__, __LINE__); \ |
| 89 | fprintf(stderr, __VA_ARGS__); \ |
| 90 | fputc('\n', stderr); \ |
| 91 | } \ |
| 92 | } while (0) |
| 93 | |
| 94 | static uint64_t now_ns(void) { |
| 95 | struct timespec ts; |
| 96 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 97 | return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; |
| 98 | } |
| 99 | |
| 100 | static uint64_t cpu_ns(void) { |
| 101 | struct timespec ts; |
| 102 | clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts); |
| 103 | return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; |
| 104 | } |
| 105 | |
| 106 | typedef struct { uint64_t s; } Rng; |
| 107 | static uint64_t rnd(Rng *r) { |
| 108 | uint64_t z = (r->s += 0x9E3779B97F4A7C15ull); |
| 109 | z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ull; |
| 110 | z = (z ^ (z >> 27)) * 0x94D049BB133111EBull; |
| 111 | return z ^ (z >> 31); |
| 112 | } |
| 113 | |
| 114 | static uint64_t mix(uint32_t id, uint32_t f, uint64_t v) { |
| 115 | return (v * 0x9E3779B97F4A7C15ull) ^ (((uint64_t)id << 8) | f); |
| 116 | } |
| 117 | |
| 118 | static RxSemSchema g_schema; |
| 119 | |
| 120 | static void schema_init(void) { |
| 121 | memset(&g_schema, 0, sizeof g_schema); |
| 122 | g_schema.n = 6; |
| 123 | g_schema.t[0] = (RxSemType){ TY_SENSOR, 0, (int32_t)UNC_FIELD, false }; |
| 124 | g_schema.t[1] = (RxSemType){ TY_TRACK, 0, (int32_t)UNC_FIELD, false }; |
| 125 | g_schema.t[2] = (RxSemType){ TY_ASSET, 0, -1, false }; |
| 126 | g_schema.t[3] = (RxSemType){ TY_PLAN, 0, -1, false }; |
| 127 | g_schema.t[4] = (RxSemType){ TY_RISK, RISK_PRIVATE, -1, false }; |
| 128 | g_schema.t[5] = (RxSemType){ TY_CONCL, 0, (int32_t)UNC_FIELD, true }; |
| 129 | } |
| 130 | |
| 131 | static const RxSemType *stype(uint32_t type) { |
| 132 | for (uint32_t i = 0; i < g_schema.n; i++) |
| 133 | if (g_schema.t[i].type == type) return &g_schema.t[i]; |
| 134 | return NULL; |
| 135 | } |
| 136 | |
| 137 | /* ---- metrics ---- */ |
| 138 | |
| 139 | typedef struct { |
| 140 | uint64_t bytes, messages, records; |
| 141 | uint64_t sender_cpu_ns, receiver_cpu_ns; |
| 142 | uint64_t round_wall_ns[N_ROUNDS]; |
| 143 | uint64_t receiver_mem_peak, sender_mem_peak; |
| 144 | uint64_t task_ok, task_total, missing, leaks, validations; |
| 145 | } ModeM; |
| 146 | |
| 147 | typedef struct { |
| 148 | uint32_t agents; |
| 149 | ModeM m[M_COUNT]; |
| 150 | uint64_t denied_by_authority, excluded_uncertain; |
| 151 | uint64_t equiv_checked, equiv_mismatch; |
| 152 | uint64_t det_checked, det_mismatch; |
| 153 | uint64_t quiet_checked, quiet_nonempty; |
| 154 | uint64_t inval_reason[5]; |
| 155 | uint64_t delta_changed_fields, delta_new_evidence, delta_new_conclusions, delta_derived; |
| 156 | uint64_t conclusions_received; |
| 157 | uint64_t revocation_invalidations; |
| 158 | uint64_t revoke_expected, revoke_delivered; |
| 159 | /* energy (µJ per scenario run; 0 = sensor unavailable) */ |
| 160 | int energy_ok; |
| 161 | uint64_t e_runs[3], e_pkg[3], e_cpu[3], e_ns[3]; /* none, B, D */ |
| 162 | } ScaleM; |
| 163 | |
| 164 | static struct { |
| 165 | ScaleM scale[3]; |
| 166 | uint64_t control_missing, control_agents; |
| 167 | uint32_t write_attempts, write_denied, write_bypass, write_positive; |
| 168 | char cpus[64]; |
| 169 | char energy_source[160]; |
| 170 | uint64_t idle_pkg_uw, idle_cpu_uw; |
| 171 | } R; |
| 172 | |
| 173 | /* ---- environment ---- */ |
| 174 | |
| 175 | typedef struct { |
| 176 | AienosCapAdmin *admin; |
| 177 | AienosCapView *view; |
| 178 | RxWorld w; |
| 179 | RxCapRef ext[N_DOMAINS]; |
| 180 | RxCapRef ext_trigger; |
| 181 | RxSemCaps team[N_TEAMS]; |
| 182 | RxCapRef revoked; |
| 183 | uint32_t n_live; |
| 184 | RxObjRef live[RX_MAX_OBJECTS]; |
| 185 | uint32_t n_concl; |
| 186 | uint64_t base_crumb; |
| 187 | } Env; |
| 188 | |
| 189 | static RxCapRef mint(Env *e, uint32_t subject, uint64_t resource, uint32_t rights) { |
| 190 | AienosCapRef office; |
| 191 | aienos_cap_office(e->admin, &office); |
| 192 | AienosCapMint m = { ISSUER, subject, resource, rights, 0, { UINT32_MAX, 0 }, office }; |
| 193 | AienosCapRef r = { UINT32_MAX, 0 }; |
| 194 | if (aienos_cap_mint(e->admin, &m, &r) != 0) r = (AienosCapRef){ UINT32_MAX, 0 }; |
| 195 | return (RxCapRef){ r.cap_id, r.generation }; |
| 196 | } |
| 197 | |
| 198 | static void hold(RxSemCaps *c, RxCapRef ref, uint64_t res, uint32_t rights) { |
| 199 | if (c->n >= SC_MAX_CAPS) return; |
| 200 | c->cap[c->n].ref = ref; |
| 201 | c->cap[c->n].resource = res; |
| 202 | c->cap[c->n].rights = rights; |
| 203 | c->n++; |
| 204 | } |
| 205 | |
| 206 | static uint32_t team_domain(uint32_t t, uint32_t k) { |
| 207 | static const uint32_t off[4] = { 0, 1, 3, 7 }; |
| 208 | return (t + off[k]) % N_DOMAINS; |
| 209 | } |
| 210 | |
| 211 | static void fields_for(Rng *r, uint64_t f[RX_MAX_FIELDS]) { |
| 212 | for (uint32_t i = 0; i < RX_MAX_FIELDS; i++) f[i] = rnd(r) % 1000000u; |
| 213 | } |
| 214 | |
| 215 | static int env_start(Env *e) { |
| 216 | memset(e, 0, sizeof *e); |
| 217 | if (aienos_cap_start(&e->admin, &e->view) != 0) return -1; |
| 218 | if (rx_world_init_native(&e->w, e->view, 1, 1u << 16) != RX_OK) return -1; |
| 219 | e->w.external_subject = SUBJ_EXTERNAL; |
| 220 | Rng r = { 0x5EEDu }; |
| 221 | for (uint32_t i = 0; i < N_INITIAL; i++) { |
| 222 | uint64_t f[RX_MAX_FIELDS]; |
| 223 | fields_for(&r, f); |
| 224 | uint32_t dom = (i / 15u) % N_DOMAINS; |
| 225 | if (rx_world_create(&e->w, ALL_TYPES[i % 5u], RX_PERSIST_RESIDENT, RES_DOM(dom), f, |
| 226 | &e->live[e->n_live]) != RX_OK) |
| 227 | return -1; |
| 228 | e->n_live++; |
| 229 | } |
| 230 | for (uint32_t d = 0; d < N_DOMAINS; d++) e->ext[d] = mint(e, SUBJ_EXTERNAL, RES_DOM(d), RX_RIGHT_WRITE); |
| 231 | for (uint32_t t = 0; t < N_TEAMS; t++) { |
| 232 | e->team[t].subject = SUBJ_TEAM0 + t; |
| 233 | for (uint32_t k = 0; k < 4; k++) { |
| 234 | uint64_t res = RES_DOM(team_domain(t, k)); |
| 235 | RxCapRef c = mint(e, SUBJ_TEAM0 + t, res, RX_RIGHT_READ); |
| 236 | hold(&e->team[t], c, res, RX_RIGHT_READ); |
| 237 | if (t == REVOKE_TEAM && k == 0) e->revoked = c; |
| 238 | } |
| 239 | if (t % 2 == 0) { |
| 240 | uint64_t pres = rx_sem_private_resource(RES_DOM(team_domain(t, 0))); |
| 241 | hold(&e->team[t], mint(e, SUBJ_TEAM0 + t, pres, RX_RIGHT_READ), pres, RX_RIGHT_READ); |
| 242 | } |
| 243 | } |
| 244 | e->base_crumb = e->w.n_crumbs; |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | static void env_stop(Env *e) { |
| 249 | rx_world_wait_quiescent(&e->w, 30000); |
| 250 | rx_world_destroy(&e->w); |
| 251 | aienos_cap_stop(e->admin, e->view); |
| 252 | } |
| 253 | |
| 254 | static uint32_t domain_of(Env *e, RxObjRef ref) { |
| 255 | return (uint32_t)(e->w.objects[ref.id].resource - RES_DOM(0)); |
| 256 | } |
| 257 | |
| 258 | /* The World moves. The same sequence for every swarm size. */ |
| 259 | static void mutate(Env *e, uint32_t round, int verify) { |
| 260 | Rng r = { 0xC0FFEEull + round * 0x1000193ull }; |
| 261 | for (uint32_t k = 0; k < 12; k++) { |
| 262 | RxObjRef ref = e->live[rnd(&r) % e->n_live]; |
| 263 | uint32_t nf = 1 + (uint32_t)(rnd(&r) % 3u); |
| 264 | RxMutation m[3]; |
| 265 | for (uint32_t j = 0; j < nf; j++) |
| 266 | m[j] = (RxMutation){ ref, (uint32_t)(rnd(&r) % RX_MAX_FIELDS), rnd(&r) % 1000000u }; |
| 267 | int64_t rc = rx_world_publish_external(&e->w, e->ext[domain_of(e, ref)], m, nf); |
| 268 | if (verify) CHECK(rc > 0, "outside publication accepted (%lld)", (long long)rc); |
| 269 | else if (rc <= 0) { fprintf(stderr, " FAIL outside publication refused in a measurement run\n"); g_fail++; } |
| 270 | } |
| 271 | if (round % 4 == 3) { |
| 272 | uint32_t idx = (uint32_t)(rnd(&r) % e->n_live); |
| 273 | RxObject o; |
| 274 | if (rx_world_read(&e->w, e->live[idx], &o) == RX_OK && o.type != TY_CONCL) { |
| 275 | uint64_t f[RX_MAX_FIELDS]; |
| 276 | fields_for(&r, f); |
| 277 | CHECK(rx_world_retire(&e->w, e->live[idx]) == RX_OK, "retire"); |
| 278 | CHECK(rx_world_create(&e->w, o.type, RX_PERSIST_RESIDENT, o.resource, f, |
| 279 | &e->live[idx]) == RX_OK, "replace"); |
| 280 | } |
| 281 | } |
| 282 | if (round % 3 == 2 && e->n_concl < MAX_CONCL) { |
| 283 | uint64_t f[RX_MAX_FIELDS]; |
| 284 | fields_for(&r, f); |
| 285 | uint32_t dom = (uint32_t)(rnd(&r) % N_DOMAINS); |
| 286 | if (rx_world_create(&e->w, TY_CONCL, RX_PERSIST_RESIDENT, RES_DOM(dom), f, |
| 287 | &e->live[e->n_live]) == RX_OK) { |
| 288 | e->n_live++; |
| 289 | e->n_concl++; |
| 290 | } |
| 291 | } |
| 292 | if (round == REVOKE_ROUND) { |
| 293 | AienosCapRef office; |
| 294 | aienos_cap_office(e->admin, &office); |
| 295 | CHECK(aienos_cap_revoke(e->admin, office, |
| 296 | (AienosCapRef){ e->revoked.cap_id, e->revoked.generation }) == 0, |
| 297 | "revoke team %u READ on its home domain", REVOKE_TEAM); |
| 298 | } |
| 299 | if (round == REGRANT_ROUND) { |
| 300 | uint64_t res = RES_DOM(team_domain(REVOKE_TEAM, 0)); |
| 301 | hold(&e->team[REVOKE_TEAM], mint(e, SUBJ_TEAM0 + REVOKE_TEAM, res, RX_RIGHT_READ), res, |
| 302 | RX_RIGHT_READ); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | /* ---- workers ---- */ |
| 307 | |
| 308 | typedef struct { |
| 309 | uint32_t team; |
| 310 | RxInformationNeed need; |
| 311 | RxSemView v[M_COUNT]; |
| 312 | RxSemCursor cur; |
| 313 | RxSemBuf msg; |
| 314 | uint64_t result; |
| 315 | RxObjRef arg; |
| 316 | uint32_t n_inputs; |
| 317 | } Agent; |
| 318 | |
| 319 | static void make_need(Env *e, Agent *a, uint32_t i) { |
| 320 | Rng r = { 0xA6E47ull ^ ((uint64_t)i * 0x9E3779B97F4A7C15ull) }; |
| 321 | a->team = i % N_TEAMS; |
| 322 | RxInformationNeed *n = &a->need; |
| 323 | memset(n, 0, sizeof *n); |
| 324 | n->receiver = SUBJ_TEAM0 + a->team; |
| 325 | if (i % 10u == 9u) { |
| 326 | /* A coordinator: every type, every object, every data field, with |
| 327 | * writer evidence. Broad needs keep the comparison honest. */ |
| 328 | n->operation.kind = SC_OP_INSPECT; |
| 329 | n->operation.fields = 0x7Fu; |
| 330 | n->evidence_requirement = SC_EV_WRITER; |
| 331 | return; |
| 332 | } |
| 333 | uint32_t x = (uint32_t)(rnd(&r) % 10u); |
| 334 | if (x < 5) { |
| 335 | n->operation.kind = SC_OP_INSPECT; |
| 336 | uint32_t k = 2 + (uint32_t)(rnd(&r) % 3u); |
| 337 | for (uint32_t j = 0; j < k; j++) n->operation.fields |= RX_FIELD(rnd(&r) % 7u); |
| 338 | } else { |
| 339 | n->operation.kind = x < 8 ? SC_OP_SUM : SC_OP_MAX; |
| 340 | n->operation.field = (uint32_t)(rnd(&r) % 7u); |
| 341 | } |
| 342 | n->n_types = 1 + (uint32_t)(rnd(&r) % 2u); |
| 343 | n->required_types[0] = ALL_TYPES[rnd(&r) % 6u]; |
| 344 | if (n->n_types == 2) { |
| 345 | do n->required_types[1] = ALL_TYPES[rnd(&r) % 6u]; |
| 346 | while (n->required_types[1] == n->required_types[0]); |
| 347 | } |
| 348 | if (rnd(&r) % 10u < 7) { |
| 349 | /* Task groups: workers in a group look at neighbouring windows. */ |
| 350 | uint32_t g = (uint32_t)(rnd(&r) % 24u); |
| 351 | uint32_t start = (g * 29u + (uint32_t)(rnd(&r) % 8u)) % N_INITIAL; |
| 352 | uint32_t width = 12 + (uint32_t)(rnd(&r) % 37u); |
| 353 | for (uint32_t j = 0; j < width && n->n_objects < SC_MAX_RELEVANT; j++) |
| 354 | n->relevant_objects[n->n_objects++] = e->live[(start + j) % N_INITIAL]; |
| 355 | } |
| 356 | if (rnd(&r) % 10u < 2) n->relevant_time_range.from_crumb = e->base_crumb + 40 + rnd(&r) % 240u; |
| 357 | uint32_t ev = (uint32_t)(rnd(&r) % 20u); |
| 358 | n->evidence_requirement = ev < 10 ? SC_EV_NONE : ev < 17 ? SC_EV_WRITER : SC_EV_DIGEST; |
| 359 | if (rnd(&r) % 10u < 4) n->uncertainty_requirement.max_ppm = 300000 + (uint32_t)(rnd(&r) % 600000u); |
| 360 | n->uncertainty_requirement.deliver = rnd(&r) % 10u < 3; |
| 361 | } |
| 362 | |
| 363 | /* ---- reference evaluation ---- |
| 364 | * Applies a need to a view-shaped source under the principal's authority. |
| 365 | * The truth runs it on a copy taken straight from the World; the broadcast |
| 366 | * worker runs it on what it decoded. It shares no code with rx_sem_project. */ |
| 367 | |
| 368 | typedef struct { |
| 369 | uint16_t id; |
| 370 | uint8_t f; |
| 371 | bool value, ev; |
| 372 | } Req; |
| 373 | |
| 374 | typedef struct { |
| 375 | uint64_t result; |
| 376 | RxObjRef arg; |
| 377 | uint32_t n_inputs; |
| 378 | uint32_t n_req; |
| 379 | Req req[RX_MAX_OBJECTS * RX_MAX_FIELDS]; |
| 380 | uint32_t denied, uncertain; |
| 381 | } Eval; |
| 382 | |
| 383 | static bool authorized(Env *e, const RxSemCaps *c, uint64_t res, uint64_t *validations) { |
| 384 | for (uint32_t i = 0; i < c->n; i++) { |
| 385 | if (c->cap[i].resource != res) continue; |
| 386 | (*validations)++; |
| 387 | if (rx_world_validate_cap(&e->w, c->cap[i].ref, c->subject, res, RX_RIGHT_READ, NULL) == |
| 388 | RX_CAP_OK) |
| 389 | return true; |
| 390 | } |
| 391 | return false; |
| 392 | } |
| 393 | |
| 394 | static void evaluate(Env *e, const RxSemView *v, const Agent *a, Eval *out, uint64_t *validations) { |
| 395 | const RxInformationNeed *n = &a->need; |
| 396 | const RxSemCaps *caps = &e->team[a->team]; |
| 397 | memset(out, 0, offsetof(Eval, req)); |
| 398 | out->denied = out->uncertain = 0; |
| 399 | const bool derived = n->operation.kind != SC_OP_INSPECT; |
| 400 | bool first = true; |
| 401 | for (uint32_t id = 0; id < RX_MAX_OBJECTS; id++) { |
| 402 | if (!v->present[id]) continue; |
| 403 | bool tw = n->n_types == 0; |
| 404 | for (uint32_t i = 0; i < n->n_types; i++) tw |= n->required_types[i] == v->type[id]; |
| 405 | if (!tw) continue; |
| 406 | bool ow = n->n_objects == 0; |
| 407 | for (uint32_t i = 0; i < n->n_objects; i++) |
| 408 | ow |= n->relevant_objects[i].id == id && n->relevant_objects[i].generation == v->gen[id]; |
| 409 | if (!ow) continue; |
| 410 | uint64_t want = derived ? RX_FIELD(n->operation.field) : n->operation.fields; |
| 411 | uint64_t sel = 0; |
| 412 | for (uint32_t f = 0; f < RX_MAX_FIELDS; f++) { |
| 413 | uint64_t c = v->crumb[id][f]; |
| 414 | bool t = c >= n->relevant_time_range.from_crumb && |
| 415 | (n->relevant_time_range.to_crumb == 0 || c <= n->relevant_time_range.to_crumb); |
| 416 | if ((want & RX_FIELD(f)) && t) sel |= RX_FIELD(f); |
| 417 | } |
| 418 | if (!sel) continue; |
| 419 | if (!authorized(e, caps, v->resource[id], validations)) { out->denied++; continue; } |
| 420 | const RxSemType *T = stype(v->type[id]); |
| 421 | if (T && T->uncertainty_field >= 0 && n->uncertainty_requirement.max_ppm && |
| 422 | v->value[id][T->uncertainty_field] > n->uncertainty_requirement.max_ppm) { |
| 423 | out->uncertain++; |
| 424 | continue; |
| 425 | } |
| 426 | uint64_t unc = (T && T->uncertainty_field >= 0 && n->uncertainty_requirement.deliver) |
| 427 | ? RX_FIELD((uint32_t)T->uncertainty_field) : 0; |
| 428 | uint64_t priv = T ? T->private_mask : 0; |
| 429 | if (((sel | unc) & priv) && |
| 430 | !authorized(e, caps, rx_sem_private_resource(v->resource[id]), validations)) { |
| 431 | sel &= ~priv; |
| 432 | unc &= ~priv; |
| 433 | if (!sel) continue; |
| 434 | } |
| 435 | for (uint32_t f = 0; f < RX_MAX_FIELDS; f++) { |
| 436 | uint64_t b = RX_FIELD(f); |
| 437 | if (!((sel | unc) & b)) continue; |
| 438 | Req *q = &out->req[out->n_req++]; |
| 439 | q->id = (uint16_t)id; |
| 440 | q->f = (uint8_t)f; |
| 441 | q->value = derived ? (unc & b) != 0 : true; |
| 442 | q->ev = n->evidence_requirement != SC_EV_NONE; |
| 443 | if (!derived && (sel & b)) out->result += mix(id, f, v->value[id][f]); |
| 444 | } |
| 445 | if (derived) { |
| 446 | uint64_t x = v->value[id][n->operation.field]; |
| 447 | if (n->operation.kind == SC_OP_SUM) out->result += x; |
| 448 | else if (first || x > out->result) { out->result = x; out->arg = (RxObjRef){ id, v->gen[id] }; } |
| 449 | first = false; |
| 450 | out->n_inputs++; |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | /* Truth source: the World now, including crumb digests. */ |
| 456 | static RxSemView *g_truth; |
| 457 | |
| 458 | static void snapshot_truth(Env *e) { |
| 459 | if (!g_truth->digest) g_truth->digest = calloc(RX_MAX_OBJECTS, sizeof *g_truth->digest); |
| 460 | pthread_mutex_lock(&e->w.mu); |
| 461 | for (uint32_t id = 0; id < RX_MAX_OBJECTS; id++) { |
| 462 | const RxObject *o = &e->w.objects[id]; |
| 463 | g_truth->present[id] = o->live; |
| 464 | if (!o->live) continue; |
| 465 | g_truth->gen[id] = o->generation; |
| 466 | g_truth->type[id] = o->type; |
| 467 | g_truth->resource[id] = o->resource; |
| 468 | g_truth->mask[id] = g_truth->ev_mask[id] = RX_ALL_FIELDS; |
| 469 | for (uint32_t f = 0; f < RX_MAX_FIELDS; f++) { |
| 470 | g_truth->value[id][f] = o->field[f]; |
| 471 | g_truth->version[id][f] = o->field_version[f]; |
| 472 | g_truth->crumb[id][f] = o->field_writer[f]; |
| 473 | const RxCrumb *c = rx_world_crumb(&e->w, o->field_writer[f]); |
| 474 | if (c) memcpy(g_truth->digest[id][f], c->digest, 32); |
| 475 | else memset(g_truth->digest[id][f], 0, 32); |
| 476 | } |
| 477 | } |
| 478 | pthread_mutex_unlock(&e->w.mu); |
| 479 | } |
| 480 | |
| 481 | /* Required information the worker lacks or holds stale. */ |
| 482 | static uint32_t missing_in(const RxSemView *v, const Eval *t, uint32_t ev_mode, bool check_digest) { |
| 483 | uint32_t miss = 0; |
| 484 | for (uint32_t i = 0; i < t->n_req; i++) { |
| 485 | const Req *q = &t->req[i]; |
| 486 | uint32_t id = q->id, f = q->f; |
| 487 | if (!v->present[id] || v->gen[id] != g_truth->gen[id]) { miss++; continue; } |
| 488 | if (q->value && (!(v->mask[id] & RX_FIELD(f)) || v->value[id][f] != g_truth->value[id][f] || |
| 489 | v->version[id][f] != g_truth->version[id][f])) { miss++; continue; } |
| 490 | if (q->ev) { |
| 491 | if (!(v->ev_mask[id] & RX_FIELD(f)) || v->crumb[id][f] != g_truth->crumb[id][f]) { miss++; continue; } |
| 492 | if (check_digest && ev_mode == SC_EV_DIGEST && |
| 493 | (!v->digest || memcmp(v->digest[id][f], g_truth->digest[id][f], 32) != 0)) { miss++; continue; } |
| 494 | } |
| 495 | } |
| 496 | return miss; |
| 497 | } |
| 498 | |
| 499 | /* Fields a worker holds that its principal may not read. */ |
| 500 | static uint32_t leaks_in(Env *e, const RxSemView *v, const Agent *a) { |
| 501 | uint32_t leaks = 0; |
| 502 | uint64_t scratch = 0; |
| 503 | const RxSemCaps *caps = &e->team[a->team]; |
| 504 | for (uint32_t id = 0; id < RX_MAX_OBJECTS; id++) { |
| 505 | if (!v->present[id]) continue; |
| 506 | uint64_t held = v->mask[id]; |
| 507 | if (!authorized(e, caps, v->resource[id], &scratch)) { |
| 508 | leaks += (uint32_t)__builtin_popcountll(held); |
| 509 | continue; |
| 510 | } |
| 511 | const RxSemType *T = stype(v->type[id]); |
| 512 | if (T && (held & T->private_mask) && |
| 513 | !authorized(e, caps, rx_sem_private_resource(v->resource[id]), &scratch)) |
| 514 | leaks += (uint32_t)__builtin_popcountll(held & T->private_mask); |
| 515 | } |
| 516 | return leaks; |
| 517 | } |
| 518 | |
| 519 | /* The worker's operation, from what it holds. */ |
| 520 | static void act_projection(Agent *a, const RxSemView *v) { |
| 521 | a->result = 0; |
| 522 | a->arg = (RxObjRef){ 0, 0 }; |
| 523 | a->n_inputs = 0; |
| 524 | if (a->need.operation.kind != SC_OP_INSPECT) { |
| 525 | if (v->n_derived) { |
| 526 | a->result = v->derived[0].value; |
| 527 | a->arg = v->derived[0].arg; |
| 528 | a->n_inputs = v->derived[0].n_inputs; |
| 529 | } |
| 530 | return; |
| 531 | } |
| 532 | for (uint32_t id = 0; id < RX_MAX_OBJECTS; id++) { |
| 533 | if (!v->present[id]) continue; |
| 534 | uint64_t m = v->mask[id] & a->need.operation.fields; |
| 535 | for (uint32_t f = 0; f < RX_MAX_FIELDS; f++) |
| 536 | if (m & RX_FIELD(f)) a->result += mix(id, f, v->value[id][f]); |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | static bool answer_ok(const Agent *a, const Eval *t) { |
| 541 | if (a->result != t->result) return false; |
| 542 | if (a->need.operation.kind == SC_OP_INSPECT) return true; |
| 543 | if (a->n_inputs != t->n_inputs) return false; |
| 544 | if (a->need.operation.kind == SC_OP_MAX && t->n_inputs && |
| 545 | (a->arg.id != t->arg.id || a->arg.generation != t->arg.generation)) |
| 546 | return false; |
| 547 | return true; |
| 548 | } |
| 549 | |
| 550 | typedef struct { Env *e; Agent *a; ScaleM *S; } ReasonCtx; |
| 551 | |
| 552 | static uint32_t reason_why(ReasonCtx *c, RxObjRef gone) { |
| 553 | Env *e = c->e; |
| 554 | const RxObject *o = &e->w.objects[gone.id]; |
| 555 | if (!o->live || o->generation != gone.generation) return SC_INV_RETIRED; |
| 556 | uint64_t scratch = 0; |
| 557 | if (!authorized(e, &e->team[c->a->team], o->resource, &scratch)) return SC_INV_AUTHORITY; |
| 558 | const RxSemType *T = stype(o->type); |
| 559 | uint32_t max = c->a->need.uncertainty_requirement.max_ppm; |
| 560 | if (T && T->uncertainty_field >= 0 && max && o->field[T->uncertainty_field] > max) |
| 561 | return SC_INV_UNCERTAIN; |
| 562 | return SC_INV_SCOPE; |
| 563 | } |
| 564 | |
| 565 | static uint32_t reason_of(void *ctx, RxObjRef gone) { |
| 566 | ReasonCtx *c = ctx; |
| 567 | uint32_t why = reason_why(c, gone); |
| 568 | if (c->S && why < 5) c->S->inval_reason[why]++; |
| 569 | return why; |
| 570 | } |
| 571 | |
| 572 | /* ---- one scenario ---- */ |
| 573 | |
| 574 | static Eval *g_eval; |
| 575 | static RxSemanticProjection *g_proj; |
| 576 | |
| 577 | static void run_scale(uint32_t n_agents, uint32_t mode_mask, int verify, ScaleM *S) { |
| 578 | Env *e = calloc(1, sizeof *e); |
| 579 | Agent *ag = calloc(n_agents, sizeof *ag); |
| 580 | RxSemBuf bcast = { 0 }; |
| 581 | CHECK(e && ag && env_start(e) == 0, "environment for %u workers", n_agents); |
| 582 | if (!e || !ag) exit(2); |
| 583 | for (uint32_t i = 0; i < n_agents; i++) { |
| 584 | make_need(e, &ag[i], i); |
| 585 | for (uint32_t m = 0; m < M_COUNT; m++) rx_sem_view_init(&ag[i].v[m]); |
| 586 | } |
| 587 | uint64_t seq = 0; |
| 588 | uint32_t *inv_before = calloc(n_agents, sizeof *inv_before); |
| 589 | bool *held_revoked = calloc(n_agents, sizeof *held_revoked); |
| 590 | const uint64_t revoked_res = RES_DOM(team_domain(REVOKE_TEAM, 0)); |
| 591 | for (uint32_t round = 0; round < N_ROUNDS; round++) { |
| 592 | if (verify && round == REVOKE_ROUND) { |
| 593 | /* Who holds something the revocation takes away. */ |
| 594 | for (uint32_t i = 0; i < n_agents; i++) { |
| 595 | const RxSemView *v = &ag[i].v[M_D]; |
| 596 | inv_before[i] = v->invalidations_seen; |
| 597 | held_revoked[i] = false; |
| 598 | if (ag[i].team != REVOKE_TEAM) continue; |
| 599 | for (uint32_t id = 0; id < RX_MAX_OBJECTS; id++) |
| 600 | held_revoked[i] |= v->present[id] && v->resource[id] == revoked_res; |
| 601 | } |
| 602 | } |
| 603 | mutate(e, round, verify); |
| 604 | seq++; |
| 605 | if (verify) snapshot_truth(e); |
| 606 | for (uint32_t m = 0; m < M_COUNT; m++) { |
| 607 | if (!(mode_mask & (1u << m))) continue; |
| 608 | ModeM *M = &S->m[m]; |
| 609 | uint64_t w0 = now_ns(), c0 = cpu_ns(); |
| 610 | /* Sender. */ |
| 611 | uint64_t sent_mem = 0; |
| 612 | if (m == M_B) { |
| 613 | rx_sem_encode_full_state(&e->w, 0, seq, &bcast); |
| 614 | M->bytes += bcast.len * n_agents; |
| 615 | M->messages += n_agents; |
| 616 | sent_mem = bcast.len; |
| 617 | } else { |
| 618 | for (uint32_t i = 0; i < n_agents; i++) { |
| 619 | Agent *a = &ag[i]; |
| 620 | rx_sem_project(&e->w, &g_schema, &a->need, &e->team[a->team], g_proj); |
| 621 | M->validations += g_proj->validations; |
| 622 | if (m == M_P) { |
| 623 | rx_sem_encode_projection(g_proj, a->need.receiver, seq, &a->msg); |
| 624 | } else { |
| 625 | ReasonCtx rc = { e, a, S }; |
| 626 | RxSemDeltaStats st; |
| 627 | rx_sem_delta(&a->cur, g_proj, a->need.receiver, seq, reason_of, &rc, &a->msg, &st); |
| 628 | if (round > 0) { |
| 629 | S->delta_changed_fields += st.changed_fields; |
| 630 | S->delta_new_evidence += st.new_evidence; |
| 631 | S->delta_new_conclusions += st.new_conclusions; |
| 632 | S->delta_derived += st.derived_changed; |
| 633 | } |
| 634 | sent_mem += rx_sem_cursor_bytes(&a->cur); |
| 635 | } |
| 636 | if (verify && m == M_P && round == 0) { |
| 637 | S->denied_by_authority += g_proj->withheld_authority; |
| 638 | S->excluded_uncertain += g_proj->excluded_uncertain; |
| 639 | } |
| 640 | M->bytes += a->msg.len; |
| 641 | M->messages++; |
| 642 | if (m == M_P && a->msg.len > sent_mem) sent_mem = a->msg.len; |
| 643 | } |
| 644 | } |
| 645 | uint64_t c1 = cpu_ns(); |
| 646 | /* Receivers. */ |
| 647 | uint64_t held = 0; |
| 648 | for (uint32_t i = 0; i < n_agents; i++) { |
| 649 | Agent *a = &ag[i]; |
| 650 | const RxSemBuf *msg = m == M_B ? &bcast : &a->msg; |
| 651 | int rc = rx_sem_view_apply(&a->v[m], msg->buf, msg->len); |
| 652 | if (verify) CHECK(rc == SC_OK, "decode %s worker %u round %u", MODE_NAME[m], i, round); |
| 653 | if (m == M_B) { |
| 654 | evaluate(e, &a->v[m], a, g_eval, &M->validations); |
| 655 | a->result = g_eval->result; |
| 656 | a->arg = g_eval->arg; |
| 657 | a->n_inputs = g_eval->n_inputs; |
| 658 | } else { |
| 659 | act_projection(a, &a->v[m]); |
| 660 | } |
| 661 | held += rx_sem_view_bytes(&a->v[m]); |
| 662 | } |
| 663 | uint64_t c2 = cpu_ns(), w1 = now_ns(); |
| 664 | M->sender_cpu_ns += c1 - c0; |
| 665 | M->receiver_cpu_ns += c2 - c1; |
| 666 | M->round_wall_ns[round] = w1 - w0; |
| 667 | if (held > M->receiver_mem_peak) M->receiver_mem_peak = held; |
| 668 | if (sent_mem > M->sender_mem_peak) M->sender_mem_peak = sent_mem; |
| 669 | |
| 670 | if (!verify) continue; |
| 671 | /* Judge every worker against the World. */ |
| 672 | for (uint32_t i = 0; i < n_agents; i++) { |
| 673 | Agent *a = &ag[i]; |
| 674 | uint64_t scratch = 0; |
| 675 | evaluate(e, g_truth, a, g_eval, &scratch); /* the judge; leaves the worker's answer alone */ |
| 676 | uint32_t miss = missing_in(&a->v[m], g_eval, a->need.evidence_requirement, m != M_B); |
| 677 | bool derived_ok = true; |
| 678 | if (m != M_B && a->need.operation.kind != SC_OP_INSPECT) derived_ok = answer_ok(a, g_eval); |
| 679 | if (!derived_ok) miss++; |
| 680 | M->missing += miss; |
| 681 | M->leaks += leaks_in(e, &a->v[m], a); |
| 682 | M->task_total++; |
| 683 | if (miss == 0 && answer_ok(a, g_eval)) M->task_ok++; |
| 684 | } |
| 685 | } |
| 686 | if (!verify) continue; |
| 687 | /* A view rebuilt from deltas holds exactly the fresh projection. */ |
| 688 | if ((mode_mask & (1u << M_P)) && (mode_mask & (1u << M_D))) { |
| 689 | for (uint32_t i = 0; i < n_agents; i++) { |
| 690 | uint8_t dp[32], dd[32]; |
| 691 | rx_sem_view_digest(&ag[i].v[M_P], dp); |
| 692 | rx_sem_view_digest(&ag[i].v[M_D], dd); |
| 693 | S->equiv_checked++; |
| 694 | if (memcmp(dp, dd, 32) != 0) S->equiv_mismatch++; |
| 695 | } |
| 696 | } |
| 697 | if (round == REVOKE_ROUND && (mode_mask & (1u << M_D))) { |
| 698 | for (uint32_t i = 0; i < n_agents; i++) { |
| 699 | if (ag[i].team == REVOKE_TEAM) |
| 700 | S->revocation_invalidations += ag[i].v[M_D].invalidations_seen - inv_before[i]; |
| 701 | if (!held_revoked[i]) continue; |
| 702 | S->revoke_expected++; |
| 703 | bool still = false; |
| 704 | for (uint32_t id = 0; id < RX_MAX_OBJECTS; id++) |
| 705 | still |= ag[i].v[M_D].present[id] && ag[i].v[M_D].resource[id] == revoked_res; |
| 706 | if (!still && ag[i].v[M_D].invalidations_seen > inv_before[i]) S->revoke_delivered++; |
| 707 | } |
| 708 | } |
| 709 | /* Same need, same World: identical bytes, whatever order the capabilities are held in. */ |
| 710 | if (round == 0 || round == N_ROUNDS / 2) { |
| 711 | RxSemBuf b1 = { 0 }, b2 = { 0 }; |
| 712 | for (uint32_t i = 0; i < n_agents; i++) { |
| 713 | Agent *a = &ag[i]; |
| 714 | RxSemCaps rev = e->team[a->team]; |
| 715 | for (uint32_t k = 0; k < rev.n / 2; k++) { |
| 716 | __typeof__(rev.cap[0]) t = rev.cap[k]; |
| 717 | rev.cap[k] = rev.cap[rev.n - 1 - k]; |
| 718 | rev.cap[rev.n - 1 - k] = t; |
| 719 | } |
| 720 | rx_sem_project(&e->w, &g_schema, &a->need, &e->team[a->team], g_proj); |
| 721 | rx_sem_encode_projection(g_proj, a->need.receiver, seq, &b1); |
| 722 | rx_sem_project(&e->w, &g_schema, &a->need, &rev, g_proj); |
| 723 | rx_sem_encode_projection(g_proj, a->need.receiver, seq, &b2); |
| 724 | S->det_checked++; |
| 725 | if (b1.len != b2.len || memcmp(b1.buf, b2.buf, b1.len) != 0) S->det_mismatch++; |
| 726 | } |
| 727 | rx_sem_buf_free(&b1); |
| 728 | rx_sem_buf_free(&b2); |
| 729 | } |
| 730 | } |
| 731 | if (verify) { |
| 732 | /* Nothing moved: every delta is an empty header. */ |
| 733 | for (uint32_t i = 0; i < n_agents; i++) { |
| 734 | Agent *a = &ag[i]; |
| 735 | rx_sem_project(&e->w, &g_schema, &a->need, &e->team[a->team], g_proj); |
| 736 | RxSemDeltaStats st; |
| 737 | rx_sem_delta(&a->cur, g_proj, a->need.receiver, seq + 1, NULL, NULL, &a->msg, &st); |
| 738 | S->quiet_checked++; |
| 739 | if (a->msg.len != 21) S->quiet_nonempty++; |
| 740 | S->conclusions_received += a->v[M_D].conclusions_seen; |
| 741 | } |
| 742 | } |
| 743 | free(inv_before); |
| 744 | free(held_revoked); |
| 745 | for (uint32_t i = 0; i < n_agents; i++) { |
| 746 | for (uint32_t m = 0; m < M_COUNT; m++) rx_sem_view_free(&ag[i].v[m]); |
| 747 | rx_sem_buf_free(&ag[i].msg); |
| 748 | } |
| 749 | rx_sem_buf_free(&bcast); |
| 750 | env_stop(e); |
| 751 | free(ag); |
| 752 | free(e); |
| 753 | } |
| 754 | |
| 755 | /* ---- negative control: an under-declared need must show up as missing ---- */ |
| 756 | |
| 757 | static void t_control(void) { |
| 758 | Env *e = calloc(1, sizeof *e); |
| 759 | CHECK(e && env_start(e) == 0, "control environment"); |
| 760 | mutate(e, 0, 1); |
| 761 | snapshot_truth(e); |
| 762 | RxSemView *v = calloc(1, sizeof *v); |
| 763 | RxSemBuf b = { 0 }; |
| 764 | for (uint32_t i = 0; i < 64; i++) { |
| 765 | Agent a; |
| 766 | memset(&a, 0, sizeof a); |
| 767 | make_need(e, &a, i); |
| 768 | if (a.need.n_types != 2) continue; |
| 769 | uint64_t scratch = 0; |
| 770 | evaluate(e, g_truth, &a, g_eval, &scratch); |
| 771 | if (g_eval->n_req == 0) continue; |
| 772 | uint32_t second = a.need.required_types[1]; |
| 773 | bool second_used = false; |
| 774 | for (uint32_t k = 0; k < g_eval->n_req; k++) |
| 775 | second_used |= g_truth->type[g_eval->req[k].id] == second; |
| 776 | if (!second_used) continue; |
| 777 | RxInformationNeed under = a.need; |
| 778 | under.n_types = 1; /* forgot one type it needs */ |
| 779 | rx_sem_project(&e->w, &g_schema, &under, &e->team[a.team], g_proj); |
| 780 | rx_sem_encode_projection(g_proj, under.receiver, 1, &b); |
| 781 | rx_sem_view_init(v); |
| 782 | rx_sem_view_apply(v, b.buf, b.len); |
| 783 | R.control_missing += missing_in(v, g_eval, a.need.evidence_requirement, true); |
| 784 | R.control_agents++; |
| 785 | rx_sem_view_free(v); |
| 786 | } |
| 787 | CHECK(R.control_agents > 0 && R.control_missing > 0, |
| 788 | "an under-declared need is caught as missing information (%llu over %llu workers)", |
| 789 | (unsigned long long)R.control_missing, (unsigned long long)R.control_agents); |
| 790 | rx_sem_buf_free(&b); |
| 791 | free(v); |
| 792 | env_stop(e); |
| 793 | free(e); |
| 794 | } |
| 795 | |
| 796 | /* ---- receiving a projection gives no write authority ---- */ |
| 797 | |
| 798 | static int rx_write_fn(RxCtx *ctx) { |
| 799 | const RxObjRef *target = ctx->user; |
| 800 | ctx->out[0] = (RxMutation){ *target, 0, 424242 }; |
| 801 | ctx->n_out = 1; |
| 802 | return 0; |
| 803 | } |
| 804 | |
| 805 | static void t_write_authority(void) { |
| 806 | Env *e = calloc(1, sizeof *e); |
| 807 | CHECK(e && env_start(e) == 0, "authority environment"); |
| 808 | const uint32_t team = 1, writer_team = 2; |
| 809 | const uint32_t dom = team_domain(team, 0); |
| 810 | uint64_t zero[RX_MAX_FIELDS] = { 0 }; |
| 811 | RxObjRef trig; |
| 812 | CHECK(rx_world_create(&e->w, 0xF0, RX_PERSIST_RESIDENT, RES_TRIGGER, zero, &trig) == RX_OK, "trigger"); |
| 813 | RxCapRef ext_trig = mint(e, SUBJ_EXTERNAL, RES_TRIGGER, RX_RIGHT_WRITE); |
| 814 | RxCapRef team_trig = mint(e, SUBJ_TEAM0 + team, RES_TRIGGER, RX_RIGHT_READ); |
| 815 | RxCapRef writer_trig = mint(e, SUBJ_TEAM0 + writer_team, RES_TRIGGER, RX_RIGHT_READ); |
| 816 | |
| 817 | /* The worker receives a projection of its home domain. */ |
| 818 | Agent a; |
| 819 | memset(&a, 0, sizeof a); |
| 820 | a.team = team; |
| 821 | a.need.receiver = SUBJ_TEAM0 + team; |
| 822 | a.need.operation.kind = SC_OP_INSPECT; |
| 823 | a.need.operation.fields = RX_FIELD(0); |
| 824 | rx_sem_project(&e->w, &g_schema, &a.need, &e->team[team], g_proj); |
| 825 | RxObjRef target = { UINT32_MAX, 0 }; |
| 826 | for (uint32_t k = 0; k < g_proj->n_objects; k++) |
| 827 | if (g_proj->object_refs[k].resource == RES_DOM(dom)) { target = g_proj->object_refs[k].ref; break; } |
| 828 | CHECK(target.id != UINT32_MAX, "projection names an object in the worker's domain"); |
| 829 | RxObject before; |
| 830 | rx_world_read(&e->w, target, &before); |
| 831 | RxCapRef read_cap = e->team[team].cap[0].ref; |
| 832 | |
| 833 | /* 1. Publish with what it holds. */ |
| 834 | R.write_attempts++; |
| 835 | RxMutation m = { target, 0, 999 }; |
| 836 | int64_t rc = rx_world_publish_external(&e->w, read_cap, &m, 1); |
| 837 | if (rc < 0) R.write_denied++; |
| 838 | |
| 839 | /* 2. A reaction that claims WRITE on the READ capability it holds. */ |
| 840 | RxReactionDesc d; |
| 841 | memset(&d, 0, sizeof d); |
| 842 | d.name = "projection-holder-writes"; |
| 843 | d.faculty = RX_FACULTY_AIEN; |
| 844 | d.subject = SUBJ_TEAM0 + team; |
| 845 | d.priority = RX_PRIO_FOREGROUND; |
| 846 | d.n_triggers = 1; |
| 847 | d.triggers[0] = (RxDep){ trig, RX_FIELD(0) }; |
| 848 | d.n_writes = 1; |
| 849 | d.writes[0] = (RxDep){ target, RX_FIELD(0) }; |
| 850 | d.n_caps = 2; |
| 851 | d.caps[0] = (RxCapNeed){ team_trig, RES_TRIGGER, RX_RIGHT_READ }; |
| 852 | d.caps[1] = (RxCapNeed){ read_cap, RES_DOM(dom), RX_RIGHT_WRITE }; |
| 853 | d.fn = rx_write_fn; |
| 854 | d.user = ⌖ |
| 855 | uint32_t rid; |
| 856 | R.write_attempts++; |
| 857 | CHECK(rx_world_add_reaction(&e->w, &d, &rid) == RX_OK, "reaction registered (authority is checked when it runs)"); |
| 858 | uint64_t blocked0 = e->w.stats.blocked_authority; |
| 859 | RxMutation tm = { trig, 0, 1 }; |
| 860 | CHECK(rx_world_publish_external(&e->w, ext_trig, &tm, 1) > 0, "trigger"); |
| 861 | rx_world_wait_quiescent(&e->w, 30000); |
| 862 | if (e->w.stats.blocked_authority > blocked0) R.write_denied++; |
| 863 | |
| 864 | /* 3. The same reaction declared honestly, with READ only, is refused outright. */ |
| 865 | RxReactionDesc d3 = d; |
| 866 | d3.caps[1].rights = RX_RIGHT_READ; |
| 867 | uint32_t rid3; |
| 868 | R.write_attempts++; |
| 869 | if (rx_world_add_reaction(&e->w, &d3, &rid3) != RX_OK) R.write_denied++; |
| 870 | |
| 871 | RxObject after; |
| 872 | rx_world_read(&e->w, target, &after); |
| 873 | if (after.field[0] != before.field[0]) R.write_bypass++; |
| 874 | CHECK(R.write_denied == R.write_attempts && R.write_bypass == 0, |
| 875 | "holding a projection gives no write (%u of %u denied, %u bypasses)", R.write_denied, |
| 876 | R.write_attempts, R.write_bypass); |
| 877 | |
| 878 | /* Positive control: a principal that really holds WRITE does write. */ |
| 879 | RxCapRef wcap = mint(e, SUBJ_TEAM0 + writer_team, RES_DOM(dom), RX_RIGHT_WRITE); |
| 880 | RxReactionDesc d4 = d; |
| 881 | d4.name = "real-writer"; |
| 882 | d4.subject = SUBJ_TEAM0 + writer_team; |
| 883 | d4.caps[0] = (RxCapNeed){ writer_trig, RES_TRIGGER, RX_RIGHT_READ }; |
| 884 | d4.caps[1] = (RxCapNeed){ wcap, RES_DOM(dom), RX_RIGHT_WRITE }; |
| 885 | uint32_t rid4; |
| 886 | CHECK(rx_world_add_reaction(&e->w, &d4, &rid4) == RX_OK, "writer registered"); |
| 887 | tm.value = 2; |
| 888 | CHECK(rx_world_publish_external(&e->w, ext_trig, &tm, 1) > 0, "trigger again"); |
| 889 | rx_world_wait_quiescent(&e->w, 30000); |
| 890 | rx_world_read(&e->w, target, &after); |
| 891 | R.write_positive = after.field[0] == 424242; |
| 892 | CHECK(R.write_positive, "a principal holding WRITE commits (control)"); |
| 893 | env_stop(e); |
| 894 | free(e); |
| 895 | } |
| 896 | |
| 897 | /* ---- energy ---- */ |
| 898 | |
| 899 | static char g_hwmon[96]; |
| 900 | |
| 901 | static int find_energy(void) { |
| 902 | DIR *dir = opendir("/sys/class/hwmon"); |
| 903 | if (!dir) return 0; |
| 904 | struct dirent *de; |
| 905 | int ok = 0; |
| 906 | while ((de = readdir(dir))) { |
| 907 | if (de->d_name[0] == '.') continue; |
| 908 | char path[512], name[64] = { 0 }; |
| 909 | snprintf(path, sizeof path, "/sys/class/hwmon/%.64s/name", de->d_name); |
| 910 | FILE *fp = fopen(path, "r"); |
| 911 | if (!fp) continue; |
| 912 | if (fgets(name, sizeof name, fp) && strncmp(name, "aien_spbm", 9) == 0) { |
| 913 | snprintf(g_hwmon, sizeof g_hwmon, "/sys/class/hwmon/%.64s", de->d_name); |
| 914 | ok = 1; |
| 915 | } |
| 916 | fclose(fp); |
| 917 | if (ok) break; |
| 918 | } |
| 919 | closedir(dir); |
| 920 | return ok; |
| 921 | } |
| 922 | |
| 923 | static uint64_t read_energy(const char *label) { |
| 924 | for (int i = 1; i <= 8; i++) { |
| 925 | char path[512], buf[64] = { 0 }; |
| 926 | snprintf(path, sizeof path, "%s/energy%d_label", g_hwmon, i); |
| 927 | FILE *fp = fopen(path, "r"); |
| 928 | if (!fp) continue; |
| 929 | int match = fgets(buf, sizeof buf, fp) && strncmp(buf, label, strlen(label)) == 0 && |
| 930 | (buf[strlen(label)] == '\n' || buf[strlen(label)] == 0); |
| 931 | fclose(fp); |
| 932 | if (!match) continue; |
| 933 | snprintf(path, sizeof path, "%s/energy%d_input", g_hwmon, i); |
| 934 | fp = fopen(path, "r"); |
| 935 | unsigned long long v = 0; |
| 936 | if (fp) { if (fscanf(fp, "%llu", &v) != 1) v = 0; fclose(fp); } |
| 937 | return v; |
| 938 | } |
| 939 | return 0; |
| 940 | } |
| 941 | |
| 942 | static int cmp_u64(const void *a, const void *b) { |
| 943 | uint64_t x = *(const uint64_t *)a, y = *(const uint64_t *)b; |
| 944 | return x < y ? -1 : x > y; |
| 945 | } |
| 946 | |
| 947 | /* Scenario runs per block until at least `min_ns` passed. Modes interleave |
| 948 | * (none, B, D, then D, B, none, ...) so drift does not favour one. */ |
| 949 | static void measure_energy(uint32_t n_agents, ScaleM *S) { |
| 950 | const int reps = 4; |
| 951 | const uint64_t min_ns = 1500000000ull; |
| 952 | uint64_t pkg[3][8], cpu[3][8], ns[3][8], runs[3][8]; |
| 953 | static ScaleM scratch; |
| 954 | for (int rep = 0; rep < reps; rep++) { |
| 955 | for (int j = 0; j < 3; j++) { |
| 956 | int k = (rep % 2 == 0) ? j : 2 - j; /* 0 none, 1 B, 2 D */ |
| 957 | uint32_t mask = k == 0 ? 0 : k == 1 ? (1u << M_B) : (1u << M_D); |
| 958 | uint64_t p0 = read_energy("pkg"), c0 = read_energy("cpu_p"), t0 = now_ns(); |
| 959 | uint64_t n = 0; |
| 960 | do { |
| 961 | memset(&scratch, 0, sizeof scratch); |
| 962 | run_scale(n_agents, mask, 0, &scratch); |
| 963 | n++; |
| 964 | } while (now_ns() - t0 < min_ns); |
| 965 | uint64_t t1 = now_ns(), p1 = read_energy("pkg"), c1 = read_energy("cpu_p"); |
| 966 | pkg[k][rep] = (p1 - p0) / n; |
| 967 | cpu[k][rep] = (c1 - c0) / n; |
| 968 | ns[k][rep] = (t1 - t0) / n; |
| 969 | runs[k][rep] = n; |
| 970 | } |
| 971 | } |
| 972 | for (int k = 0; k < 3; k++) { |
| 973 | qsort(pkg[k], reps, sizeof(uint64_t), cmp_u64); |
| 974 | qsort(cpu[k], reps, sizeof(uint64_t), cmp_u64); |
| 975 | qsort(ns[k], reps, sizeof(uint64_t), cmp_u64); |
| 976 | S->e_pkg[k] = (pkg[k][1] + pkg[k][2]) / 2; |
| 977 | S->e_cpu[k] = (cpu[k][1] + cpu[k][2]) / 2; |
| 978 | S->e_ns[k] = (ns[k][1] + ns[k][2]) / 2; |
| 979 | S->e_runs[k] = 0; |
| 980 | for (int r = 0; r < reps; r++) S->e_runs[k] += runs[k][r]; |
| 981 | } |
| 982 | S->energy_ok = 1; |
| 983 | } |
| 984 | |
| 985 | static void idle_power(void) { |
| 986 | uint64_t p0 = read_energy("pkg"), c0 = read_energy("cpu_p"), t0 = now_ns(); |
| 987 | struct timespec ts = { 1, 0 }; |
| 988 | nanosleep(&ts, NULL); |
| 989 | uint64_t t1 = now_ns(), p1 = read_energy("pkg"), c1 = read_energy("cpu_p"); |
| 990 | double s = (double)(t1 - t0) / 1e9; |
| 991 | R.idle_pkg_uw = (uint64_t)((double)(p1 - p0) / s); |
| 992 | R.idle_cpu_uw = (uint64_t)((double)(c1 - c0) / s); |
| 993 | } |
| 994 | |
| 995 | /* ---- placement and receipt ---- */ |
| 996 | |
| 997 | static void place(void) { |
| 998 | cpu_set_t set; |
| 999 | CPU_ZERO(&set); |
| 1000 | int count = 0; |
| 1001 | char *p = R.cpus; |
| 1002 | long n = sysconf(_SC_NPROCESSORS_CONF); |
| 1003 | for (long c = 0; c < n; c++) { |
| 1004 | char path[128]; |
| 1005 | snprintf(path, sizeof path, "/sys/devices/system/cpu/cpu%ld/regs/identification/midr_el1", c); |
| 1006 | FILE *fp = fopen(path, "r"); |
| 1007 | if (!fp) continue; |
| 1008 | unsigned long long midr = 0; |
| 1009 | int ok = fscanf(fp, "%llx", &midr) == 1; |
| 1010 | fclose(fp); |
| 1011 | if (ok && ((midr >> 4) & 0xfffu) == 0xd85u) { |
| 1012 | CPU_SET(c, &set); |
| 1013 | count++; |
| 1014 | if (p < R.cpus + sizeof R.cpus - 5) p += sprintf(p, "%s%ld", count > 1 ? "," : "", c); |
| 1015 | } |
| 1016 | } |
| 1017 | if (count == 0 || sched_setaffinity(0, sizeof set, &set) != 0) { |
| 1018 | snprintf(R.cpus, sizeof R.cpus, "unpinned"); |
| 1019 | return; |
| 1020 | } |
| 1021 | printf("[*] placed on %d Cortex-X925 cores: %s\n", count, R.cpus); |
| 1022 | } |
| 1023 | |
| 1024 | static void binary_digest(char out[65]) { |
| 1025 | strcpy(out, "unavailable"); |
| 1026 | FILE *fp = fopen("/proc/self/exe", "rb"); |
| 1027 | if (!fp) return; |
| 1028 | sha256_ctx c; |
| 1029 | sha256_init(&c); |
| 1030 | uint8_t buf[65536]; |
| 1031 | size_t n; |
| 1032 | while ((n = fread(buf, 1, sizeof buf, fp)) > 0) sha256_update(&c, buf, n); |
| 1033 | fclose(fp); |
| 1034 | uint8_t d[32]; |
| 1035 | sha256_final(&c, d); |
| 1036 | for (int i = 0; i < 32; i++) sprintf(out + i * 2, "%02x", d[i]); |
| 1037 | } |
| 1038 | |
| 1039 | static uint64_t median_wall(const ModeM *M) { |
| 1040 | uint64_t v[N_ROUNDS]; |
| 1041 | memcpy(v, M->round_wall_ns, sizeof v); |
| 1042 | qsort(v, N_ROUNDS, sizeof(uint64_t), cmp_u64); |
| 1043 | return v[N_ROUNDS / 2]; |
| 1044 | } |
| 1045 | |
| 1046 | static uint64_t max_wall(const ModeM *M) { |
| 1047 | uint64_t x = 0; |
| 1048 | for (uint32_t i = 0; i < N_ROUNDS; i++) if (M->round_wall_ns[i] > x) x = M->round_wall_ns[i]; |
| 1049 | return x; |
| 1050 | } |
| 1051 | |
| 1052 | static int scale_pass(const ScaleM *S) { |
| 1053 | const ModeM *B = &S->m[M_B], *P = &S->m[M_P], *D = &S->m[M_D]; |
| 1054 | return P->bytes < B->bytes && D->bytes < P->bytes && P->missing == 0 && D->missing == 0 && |
| 1055 | B->missing == 0 && P->leaks == 0 && D->leaks == 0 && D->task_ok == D->task_total && |
| 1056 | P->task_ok == P->task_total && B->task_ok == B->task_total && |
| 1057 | S->equiv_mismatch == 0 && S->det_mismatch == 0 && S->quiet_nonempty == 0; |
| 1058 | } |
| 1059 | |
| 1060 | static void write_receipt(int gate) { |
| 1061 | char path[512]; |
| 1062 | if (omega_evidence_path("SEMANTIC_COMM/rx_semantic_comm_receipt.json", path, sizeof path) != 0) return; |
| 1063 | FILE *fp = fopen(path, "w"); |
| 1064 | if (!fp) return; |
| 1065 | char commit[41]; |
| 1066 | memset(commit, 0, sizeof commit); |
| 1067 | if (!omega_evidence_run_commit(commit)) memcpy(commit, "unknown", 8); |
| 1068 | const char *candidate = getenv("OMEGA_CANDIDATE_COMMIT"); |
| 1069 | int bound = candidate && candidate[0] && strcmp(candidate, commit) == 0 && |
| 1070 | !omega_evidence_tree_dirty(); |
| 1071 | const char *aienos = getenv("AIENOS_COMMIT"); |
| 1072 | char digest[65]; |
| 1073 | binary_digest(digest); |
| 1074 | struct utsname u; |
| 1075 | memset(&u, 0, sizeof u); |
| 1076 | uname(&u); |
| 1077 | fprintf(fp, "{\n \"schema\": \"OMEGA_SEMANTIC_COMMUNICATION_V1\",\n"); |
| 1078 | fprintf(fp, " \"run_id\": \"%s\",\n", omega_evidence_run_id()); |
| 1079 | fprintf(fp, " \"candidate_commit\": %s%s%s,\n", candidate ? "\"" : "", candidate ? candidate : "null", |
| 1080 | candidate ? "\"" : ""); |
| 1081 | fprintf(fp, " \"candidate_bound\": %s,\n", bound ? "true" : "false"); |
| 1082 | fprintf(fp, " \"run_commit\": \"%s\",\n \"tree_dirty\": %s,\n", commit, |
| 1083 | omega_evidence_tree_dirty() ? "true" : "false"); |
| 1084 | fprintf(fp, " \"aienos_commit\": %s%s%s,\n", aienos ? "\"" : "", aienos ? aienos : "null", |
| 1085 | aienos ? "\"" : ""); |
| 1086 | fprintf(fp, " \"checks\": %d,\n \"failures\": %d,\n", g_checks, g_fail); |
| 1087 | fprintf(fp, " \"test_binary_sha256\": \"%s\",\n", digest); |
| 1088 | fprintf(fp, " \"host\": {\"sysname\": \"%s\", \"release\": \"%s\", \"machine\": \"%s\", \"cpus\": \"%s\"},\n", |
| 1089 | u.sysname, u.release, u.machine, R.cpus); |
| 1090 | fprintf(fp, " \"scope\": \"host processor; native AIENOS authority library; resident reaction world; " |
| 1091 | "one sender thread; delivery is in-process decode of real encoded bytes\",\n"); |
| 1092 | fprintf(fp, " \"baseline\": \"full_state_broadcast = every live object, every field, its version and its " |
| 1093 | "writer crumb id, to every worker every round (no crumb digests, which favours the baseline)\",\n"); |
| 1094 | fprintf(fp, " \"world\": {\"initial_objects\": %u, \"types\": 6, \"domains\": %u, \"teams\": %u, " |
| 1095 | "\"rounds\": %u, \"writes_per_round\": 12, \"retire_replace_every\": 4, " |
| 1096 | "\"new_conclusion_every\": 3, \"revoke_round\": %u, \"regrant_round\": %u},\n", |
| 1097 | N_INITIAL, N_DOMAINS, N_TEAMS, N_ROUNDS, REVOKE_ROUND, REGRANT_ROUND); |
| 1098 | fprintf(fp, " \"scales\": [\n"); |
| 1099 | for (int s = 0; s < 3; s++) { |
| 1100 | const ScaleM *S = &R.scale[s]; |
| 1101 | fprintf(fp, " {\"agents\": %u, \"pass\": %s,\n", S->agents, scale_pass(S) ? "true" : "false"); |
| 1102 | for (int m = 0; m < M_COUNT; m++) { |
| 1103 | const ModeM *M = &S->m[m]; |
| 1104 | fprintf(fp, |
| 1105 | " \"%s\": {\"bytes\": %llu, \"messages\": %llu, \"sender_cpu_ns\": %llu, " |
| 1106 | "\"receiver_cpu_ns\": %llu, \"round_latency_ns_median\": %llu, \"round_latency_ns_max\": %llu, " |
| 1107 | "\"receiver_memory_peak_bytes\": %llu, \"sender_memory_peak_bytes\": %llu, " |
| 1108 | "\"authority_validations\": %llu, \"task_success\": %llu, \"tasks\": %llu, " |
| 1109 | "\"missing_required_information\": %llu, \"fields_delivered_without_authority\": %llu},\n", |
| 1110 | MODE_NAME[m], (unsigned long long)M->bytes, (unsigned long long)M->messages, |
| 1111 | (unsigned long long)M->sender_cpu_ns, (unsigned long long)M->receiver_cpu_ns, |
| 1112 | (unsigned long long)median_wall(M), (unsigned long long)max_wall(M), |
| 1113 | (unsigned long long)M->receiver_mem_peak, (unsigned long long)M->sender_mem_peak, |
| 1114 | (unsigned long long)M->validations, (unsigned long long)M->task_ok, |
| 1115 | (unsigned long long)M->task_total, (unsigned long long)M->missing, |
| 1116 | (unsigned long long)M->leaks); |
| 1117 | } |
| 1118 | fprintf(fp, |
| 1119 | " \"bytes_ratio_broadcast_over_delta\": %.2f, \"bytes_ratio_broadcast_over_projection\": %.2f,\n" |
| 1120 | " \"withheld_by_authority_round0\": %llu, \"excluded_by_uncertainty_round0\": %llu,\n" |
| 1121 | " \"delta\": {\"changed_fields\": %llu, \"new_evidence\": %llu, \"new_conclusions\": %llu, " |
| 1122 | "\"derived_changed\": %llu, \"conclusions_received\": %llu, \"revoked_team_invalidations\": %llu, " |
| 1123 | "\"revocation_expected_workers\": %llu, \"revocation_told_workers\": %llu},\n" |
| 1124 | " \"invalidation_reasons\": {\"retired\": %llu, \"authority\": %llu, \"uncertain\": %llu, \"scope\": %llu},\n" |
| 1125 | " \"equivalence\": {\"checked\": %llu, \"mismatches\": %llu}, " |
| 1126 | "\"determinism\": {\"checked\": %llu, \"mismatches\": %llu}, " |
| 1127 | "\"quiet_round\": {\"checked\": %llu, \"nonempty\": %llu},\n", |
| 1128 | (double)S->m[M_B].bytes / (double)(S->m[M_D].bytes ? S->m[M_D].bytes : 1), |
| 1129 | (double)S->m[M_B].bytes / (double)(S->m[M_P].bytes ? S->m[M_P].bytes : 1), |
| 1130 | (unsigned long long)S->denied_by_authority, (unsigned long long)S->excluded_uncertain, |
| 1131 | (unsigned long long)S->delta_changed_fields, (unsigned long long)S->delta_new_evidence, |
| 1132 | (unsigned long long)S->delta_new_conclusions, (unsigned long long)S->delta_derived, |
| 1133 | (unsigned long long)S->conclusions_received, (unsigned long long)S->revocation_invalidations, |
| 1134 | (unsigned long long)S->revoke_expected, (unsigned long long)S->revoke_delivered, |
| 1135 | (unsigned long long)S->inval_reason[SC_INV_RETIRED], (unsigned long long)S->inval_reason[SC_INV_AUTHORITY], |
| 1136 | (unsigned long long)S->inval_reason[SC_INV_UNCERTAIN], (unsigned long long)S->inval_reason[SC_INV_SCOPE], |
| 1137 | (unsigned long long)S->equiv_checked, (unsigned long long)S->equiv_mismatch, |
| 1138 | (unsigned long long)S->det_checked, (unsigned long long)S->det_mismatch, |
| 1139 | (unsigned long long)S->quiet_checked, (unsigned long long)S->quiet_nonempty); |
| 1140 | if (S->energy_ok) |
| 1141 | fprintf(fp, |
| 1142 | " \"energy_uJ_per_scenario_run\": {\"source\": \"%s\", \"method\": \"median of 4 interleaved " |
| 1143 | "blocks, each >=1.5 s; whole scenario incl. world setup; raw, nothing subtracted\",\n" |
| 1144 | " \"world_only_no_messages\": {\"pkg\": %llu, \"cpu_p\": %llu, \"wall_ns\": %llu, \"runs\": %llu},\n" |
| 1145 | " \"full_state_broadcast\": {\"pkg\": %llu, \"cpu_p\": %llu, \"wall_ns\": %llu, \"runs\": %llu},\n" |
| 1146 | " \"semantic_projection_delta\": {\"pkg\": %llu, \"cpu_p\": %llu, \"wall_ns\": %llu, \"runs\": %llu}}}%s\n", |
| 1147 | R.energy_source, (unsigned long long)S->e_pkg[0], (unsigned long long)S->e_cpu[0], |
| 1148 | (unsigned long long)S->e_ns[0], (unsigned long long)S->e_runs[0], |
| 1149 | (unsigned long long)S->e_pkg[1], (unsigned long long)S->e_cpu[1], |
| 1150 | (unsigned long long)S->e_ns[1], (unsigned long long)S->e_runs[1], |
| 1151 | (unsigned long long)S->e_pkg[2], (unsigned long long)S->e_cpu[2], |
| 1152 | (unsigned long long)S->e_ns[2], (unsigned long long)S->e_runs[2], s < 2 ? "," : ""); |
| 1153 | else |
| 1154 | fprintf(fp, " \"energy_uJ_per_scenario_run\": \"unavailable: no aien_spbm energy sensor on this host\"}%s\n", |
| 1155 | s < 2 ? "," : ""); |
| 1156 | } |
| 1157 | fprintf(fp, " ],\n"); |
| 1158 | fprintf(fp, " \"idle_power_uW\": {\"pkg\": %llu, \"cpu_p\": %llu},\n", (unsigned long long)R.idle_pkg_uw, |
| 1159 | (unsigned long long)R.idle_cpu_uw); |
| 1160 | fprintf(fp, " \"negative_control\": {\"under_declared_workers\": %llu, \"missing_detected\": %llu},\n", |
| 1161 | (unsigned long long)R.control_agents, (unsigned long long)R.control_missing); |
| 1162 | fprintf(fp, " \"write_authority\": {\"attempts_with_projection_only\": %u, \"denied\": %u, \"bypasses\": %u, " |
| 1163 | "\"real_writer_control_committed\": %s},\n", |
| 1164 | R.write_attempts, R.write_denied, R.write_bypass, R.write_positive ? "true" : "false"); |
| 1165 | fprintf(fp, " \"gates\": {\n \"OMEGA_SEMANTIC_COMMUNICATION_PASS\": \"%s\",\n", gate ? "PASS" : "FAIL"); |
| 1166 | fprintf(fp, " \"not_claimed\": [\"transport over a network or between processes (delivery is in-process decode)\", " |
| 1167 | "\"graphics-processor senders or receivers\", \"AIENOS kernel (the authority runs as a host library)\", " |
| 1168 | "\"learned or inferred needs (needs are declared)\", \"uncertainty estimation (Omega carries the producer's declared value)\", " |
| 1169 | "\"worlds above 256 objects\", \"energy as a gate condition (it is measured and reported only)\"]\n"); |
| 1170 | fprintf(fp, " }\n}\n"); |
| 1171 | fclose(fp); |
| 1172 | printf("receipt: %s\n", path); |
| 1173 | } |
| 1174 | |
| 1175 | int main(void) { |
| 1176 | signal(SIGPIPE, SIG_IGN); |
| 1177 | place(); |
| 1178 | schema_init(); |
| 1179 | g_truth = calloc(1, sizeof *g_truth); |
| 1180 | g_eval = calloc(1, sizeof *g_eval); |
| 1181 | g_proj = calloc(1, sizeof *g_proj); |
| 1182 | if (!g_truth || !g_eval || !g_proj) return 2; |
| 1183 | |
| 1184 | t_write_authority(); |
| 1185 | t_control(); |
| 1186 | |
| 1187 | static const uint32_t scales[3] = { 32, 100, 500 }; |
| 1188 | for (int s = 0; s < 3; s++) { |
| 1189 | ScaleM *S = &R.scale[s]; |
| 1190 | S->agents = scales[s]; |
| 1191 | run_scale(scales[s], (1u << M_B) | (1u << M_P) | (1u << M_D), 1, S); |
| 1192 | const ModeM *B = &S->m[M_B], *P = &S->m[M_P], *D = &S->m[M_D]; |
| 1193 | printf("[%3u workers] bytes B %llu P %llu D %llu (B/D %.1fx) success B %llu/%llu P %llu/%llu D %llu/%llu " |
| 1194 | "missing P %llu D %llu leaks P %llu D %llu (B %llu)\n", |
| 1195 | scales[s], (unsigned long long)B->bytes, (unsigned long long)P->bytes, |
| 1196 | (unsigned long long)D->bytes, (double)B->bytes / (double)(D->bytes ? D->bytes : 1), |
| 1197 | (unsigned long long)B->task_ok, (unsigned long long)B->task_total, |
| 1198 | (unsigned long long)P->task_ok, (unsigned long long)P->task_total, |
| 1199 | (unsigned long long)D->task_ok, (unsigned long long)D->task_total, |
| 1200 | (unsigned long long)P->missing, (unsigned long long)D->missing, |
| 1201 | (unsigned long long)P->leaks, (unsigned long long)D->leaks, (unsigned long long)B->leaks); |
| 1202 | printf(" cpu send/recv ms B %.1f/%.1f P %.1f/%.1f D %.1f/%.1f round median us B %.0f P %.0f D %.0f " |
| 1203 | "recv mem KiB B %llu P %llu D %llu\n", |
| 1204 | B->sender_cpu_ns / 1e6, B->receiver_cpu_ns / 1e6, P->sender_cpu_ns / 1e6, |
| 1205 | P->receiver_cpu_ns / 1e6, D->sender_cpu_ns / 1e6, D->receiver_cpu_ns / 1e6, |
| 1206 | median_wall(B) / 1e3, median_wall(P) / 1e3, median_wall(D) / 1e3, |
| 1207 | (unsigned long long)B->receiver_mem_peak / 1024, (unsigned long long)P->receiver_mem_peak / 1024, |
| 1208 | (unsigned long long)D->receiver_mem_peak / 1024); |
| 1209 | CHECK(scale_pass(S), "%u workers: pruning keeps every required fact, leaks nothing, sends less", scales[s]); |
| 1210 | CHECK(S->revoke_delivered == S->revoke_expected, |
| 1211 | "%u workers: every worker holding revoked state is told (%llu of %llu)", scales[s], |
| 1212 | (unsigned long long)S->revoke_delivered, (unsigned long long)S->revoke_expected); |
| 1213 | } |
| 1214 | |
| 1215 | if (!getenv("SEMCOMM_NO_ENERGY") && find_energy()) { |
| 1216 | snprintf(R.energy_source, sizeof R.energy_source, "%s (pkg, cpu_p; microjoules)", g_hwmon); |
| 1217 | idle_power(); |
| 1218 | for (int s = 0; s < 3; s++) { |
| 1219 | measure_energy(R.scale[s].agents, &R.scale[s]); |
| 1220 | const ScaleM *S = &R.scale[s]; |
| 1221 | printf("[%3u workers] energy per run uJ pkg/cpu_p: world-only %llu/%llu B %llu/%llu D %llu/%llu\n", |
| 1222 | S->agents, (unsigned long long)S->e_pkg[0], (unsigned long long)S->e_cpu[0], |
| 1223 | (unsigned long long)S->e_pkg[1], (unsigned long long)S->e_cpu[1], |
| 1224 | (unsigned long long)S->e_pkg[2], (unsigned long long)S->e_cpu[2]); |
| 1225 | } |
| 1226 | } else { |
| 1227 | printf("[*] energy sensor unavailable; energy not measured\n"); |
| 1228 | } |
| 1229 | |
| 1230 | int gate = g_fail == 0 && R.write_bypass == 0 && R.control_missing > 0; |
| 1231 | uint64_t revoke_seen = 0; |
| 1232 | for (int s = 0; s < 3; s++) { |
| 1233 | gate = gate && scale_pass(&R.scale[s]); |
| 1234 | revoke_seen += R.scale[s].revoke_expected; |
| 1235 | } |
| 1236 | CHECK(revoke_seen > 0, "revocation of held state was exercised (%llu workers)", (unsigned long long)revoke_seen); |
| 1237 | gate = gate && revoke_seen > 0; |
| 1238 | printf("checks %d failures %d OMEGA_SEMANTIC_COMMUNICATION_PASS: %s\n", g_checks, g_fail, |
| 1239 | gate ? "PASS" : "FAIL"); |
| 1240 | write_receipt(gate); |
| 1241 | return gate ? 0 : 1; |
| 1242 | } |