interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / tests / runtime / rx_dual_jspace_tap_bench.c

193 lines · 6741 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * rx_dual_jspace_tap_bench.c -- overhead of the DUAL-3a observation tap at
19 * decision site "jspace.residency". Public API only.
20 *
21 * Built twice by mk/dual_jspace_tap.mk: against the base commit's rx_jspace.c
22 * (-DDUAL_TAP_BENCH_BASE: observer-off leg only, the pre-change timing) and
23 * against the current one (observer off, then a recording observer that
24 * encodes and keeps every record). Same workload, fixed costs, trials
25 * interleaved; medians reported. Thresholds are pre-registered in
26 * evidence/DUAL/3a-jspace-tap/receipt.md, not here.
27 *
28 * Workload per round: make every unit resident again (untimed), then two timed
29 * js_forge_enforce calls: hot-arena SOFT case (budget 3/2 of residency) and
30 * over-total CAPACITY case (budget 1/2 of residency).
31 */
32#include "runtime/rx_jspace.h"
33
34#include <inttypes.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38#include <time.h>
39#include <unistd.h>
40
41#define UNIT 4096u
42#define FANOUT 256u
43#define DEPTH 4u
44#define ROUNDS 40
45#define TRIALS 7
46
47static uint64_t splitmix(uint64_t *x) {
48 uint64_t z = (*x += 0x9e3779b97f4a7c15ull);
49 z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ull;
50 z = (z ^ (z >> 27)) * 0x94d049bb133111ebull;
51 return z ^ (z >> 31);
52}
53
54static void derive_sparse(const uint8_t *prev, uint64_t token, uint8_t *out, size_t n) {
55 uint64_t x = token;
56 if (!prev) {
57 for (size_t i = 0; i + 8 <= n; i += 8) { uint64_t v = splitmix(&x); memcpy(out + i, &v, 8); }
58 return;
59 }
60 memcpy(out, prev, n);
61 for (size_t i = (size_t)(token % 8) * 8; i + 8 <= n; i += 64) {
62 uint64_t v = splitmix(&x);
63 memcpy(out + i, &v, 8);
64 }
65}
66
67static const JsRealizer RZ = { JS_REAL_LATENT_CHECKPOINT, "sparse", UNIT, derive_sparse };
68
69static void costs_plain(JsSpace *s) {
70 JsCosts c = { 0 };
71 c.derive_ns = (double)UNIT;
72 c.copy_ns_per_byte = 0.125;
73 c.move_ns_per_byte = 0.125;
74 c.compress_ns_per_byte = 0.5;
75 c.decompress_ns_per_byte = 0.5;
76 c.compress_ratio = 0.25;
77 c.spill_write_ns_per_byte = 2.0;
78 c.spill_read_ns_per_byte = 4.0;
79 c.retain_ns_per_byte = 1.0 / 1024.0;
80 s->costs = c;
81}
82
83static uint64_t now_ns(void) {
84 struct timespec ts;
85 clock_gettime(CLOCK_MONOTONIC, &ts);
86 return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec;
87}
88
89#ifndef DUAL_TAP_BENCH_BASE
90typedef struct { uint64_t records, bytes; uint8_t xor_digest[32]; } Sink;
91static void sink(const JsDualObservation *o, const void *ctx) {
92 Sink *k = (Sink *)ctx;
93 uint8_t enc[JS_DUAL_OBS_BYTES];
94 k->bytes += js_dual_observation_encode(o, enc);
95 for (int i = 0; i < 32; i++) k->xor_digest[i] ^= o->digest[i];
96 k->records++;
97}
98#endif
99
100typedef struct { uint64_t wall_ns, decisions, records, bytes; } Trial;
101
102static Trial trial(int observe) {
103 Trial t = { 0, 0, 0, 0 };
104 JsSpace s;
105 char path[96];
106 snprintf(path, sizeof path, "/tmp/jstap_bench_%ld", (long)getpid());
107 if (js_space_init(&s, path) != JS_OK) { fprintf(stderr, "init failed\n"); exit(2); }
108 costs_plain(&s);
109 uint32_t root;
110 js_branch_root(&s, &RZ, 7, &root);
111 for (unsigned i = 0; i < DEPTH; i++) js_branch_derive(&s, root, 100 + i);
112 for (unsigned k = 0; k < FANOUT; k++) {
113 uint32_t c;
114 js_branch_fork(&s, root, &c);
115 js_branch_derive(&s, c, 1000 + k);
116 }
117#ifndef DUAL_TAP_BENCH_BASE
118 Sink snk;
119 memset(&snk, 0, sizeof snk);
120 if (observe) js_dual_set_observer(&s, sink, &snk);
121#else
122 (void)observe;
123#endif
124 for (int round = 0; round < ROUNDS; round++) {
125 for (uint32_t b = 0; b < JS_MAX_BRANCHES; b++) {
126 JsBranch *br = s.branches[b];
127 if (!br) continue;
128 for (uint32_t i = 0; i < br->n_units; i++) js_real_restore(&s, br->units[i]);
129 }
130 JsPolicyReport rep;
131 uint64_t res = s.stats.resident_bytes;
132 memset(&rep, 0, sizeof rep);
133 uint64_t t0 = now_ns();
134 js_forge_enforce(&s, res * 3 / 2, &rep);
135 uint64_t t1 = now_ns();
136 t.wall_ns += t1 - t0;
137 t.decisions += rep.considered;
138 res = s.stats.resident_bytes;
139 memset(&rep, 0, sizeof rep);
140 t0 = now_ns();
141 js_forge_enforce(&s, res / 2, &rep);
142 t1 = now_ns();
143 t.wall_ns += t1 - t0;
144 t.decisions += rep.considered;
145 }
146#ifndef DUAL_TAP_BENCH_BASE
147 t.records = snk.records;
148 t.bytes = snk.bytes;
149#endif
150 js_space_destroy(&s);
151 return t;
152}
153
154static int cmp_u64(const void *a, const void *b) {
155 uint64_t x = *(const uint64_t *)a, y = *(const uint64_t *)b;
156 return (x > y) - (x < y);
157}
158
159static uint64_t median(uint64_t *v, int n) { qsort(v, n, sizeof *v, cmp_u64); return v[n / 2]; }
160
161int main(void) {
162#ifdef DUAL_TAP_BENCH_BASE
163 const char *build = "base";
164#else
165 const char *build = "tap";
166#endif
167 uint64_t off[TRIALS], on[TRIALS];
168 Trial last_off = { 0, 0, 0, 0 }, last_on = { 0, 0, 0, 0 };
169 for (int i = 0; i < TRIALS; i++) {
170 last_off = trial(0); off[i] = last_off.wall_ns;
171#ifndef DUAL_TAP_BENCH_BASE
172 last_on = trial(1); on[i] = last_on.wall_ns;
173#else
174 on[i] = 0;
175#endif
176 }
177 uint64_t m_off = median(off, TRIALS);
178#ifdef DUAL_TAP_BENCH_BASE
179 (void)on; (void)last_on;
180#endif
181 printf("bench[%s] observer=NULL median_wall_ns=%" PRIu64 " decisions=%" PRIu64 " records=0 bytes=0 "
182 "(rounds %d, trials %d, fanout %u, unit %u)\n", build, m_off, last_off.decisions, ROUNDS, TRIALS, FANOUT, UNIT);
183#ifndef DUAL_TAP_BENCH_BASE
184 uint64_t m_on = median(on, TRIALS);
185 printf("bench[%s] observer=record median_wall_ns=%" PRIu64 " decisions=%" PRIu64 " records=%" PRIu64 " bytes=%" PRIu64 "\n",
186 build, m_on, last_on.decisions, last_on.records, last_on.bytes);
187 double per_record = last_on.records ? ((double)m_on - (double)m_off) / (double)last_on.records : 0;
188 printf("bench[%s] ratio_on_off=%.3f added_ns_per_record=%.1f records_equal_decisions=%s\n",
189 build, m_off ? (double)m_on / (double)m_off : 0, per_record,
190 last_on.records == last_on.decisions ? "yes" : "NO");
191#endif
192 return 0;
193}