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 / turing / ty_energy_window.c

364 lines · 14561 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/* ty_energy_window: measure ONE TY-5 window (docs/turing/TURING_YIELD_ENERGY_PROTOCOL_V0.md).
18 *
19 * The rounds, the idle wait and the pause between windows live in the shell
20 * driver (tools/ty_energy_run.sh); this tool measures one window per call and
21 * exits, so it holds no timed loop of its own.
22 *
23 * Meter: tests/runtime/r15_measure linked unchanged. r15_energy_open is NOT
24 * called (it loads NVML); meter_attach() below does the SPBM half of it: find
25 * the aien_spbm hwmon, check all ten channel labels, initialise the mutex.
26 * Every later use (r15_energy_read, the 10 Hz sampler, r15_energy_samples) is
27 * the frozen code, and the NVML pointers stay NULL, so it is never touched.
28 *
29 * usage:
30 * ty_energy_window --plan SEED ROUNDS print "round pos cond" lines
31 * ty_energy_window --out FILE --run ID --config S1 --cond AB --round R --pos K
32 * --secs 30 --workload BIN --a R1_plain:5,6 --b R2c_crumb:7,8
33 * [--m 8192 --n 16384 --seed-a 1 --seed-b 2 --settle-ms 2000 --tool-cpu 0]
34 */
35#ifndef _GNU_SOURCE
36#define _GNU_SOURCE
37#endif
38#include <dirent.h>
39#include <errno.h>
40#include <inttypes.h>
41#include <pthread.h>
42#include <sched.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
46#include <sys/wait.h>
47#include <time.h>
48#include <unistd.h>
49
50#include "r15_measure.h"
51
52#define TYE_NCPU 20
53#define TYE_NZONE 16
54#define TYE_MAXS 2048
55
56/* Same tables as r15_measure.c (static there). */
57static const char *const tye_elabel[R15_SPBM_ENERGY] = {"pkg", "cpu_e", "cpu_p", "gpc_unverified", "gpm"};
58static const char *const tye_plabel[R15_SPBM_POWER] = {"sys_total", "soc_pkg", "cpu_e", "cpu_p", "gpu"};
59
60static int line_of(const char *path, char *buf, size_t n) {
61 FILE *f = fopen(path, "r");
62 if (!f) return -1;
63 char *ok = fgets(buf, (int)n, f);
64 fclose(f);
65 if (!ok) return -1;
66 buf[strcspn(buf, "\n")] = 0;
67 return 0;
68}
69
70static int meter_attach(R15Energy *e, char *why, size_t n) {
71 memset(e, 0, sizeof *e);
72 pthread_mutex_init(&e->mu, NULL);
73 DIR *d = opendir("/sys/class/hwmon");
74 struct dirent *de;
75 char path[512], buf[64], dir[160] = "";
76 for (de = d ? readdir(d) : NULL; de; de = readdir(d)) {
77 snprintf(path, sizeof path, "/sys/class/hwmon/%.200s/name", de->d_name);
78 if (!dir[0] && line_of(path, buf, sizeof buf) == 0 && !strcmp(buf, "aien_spbm"))
79 snprintf(dir, sizeof dir, "/sys/class/hwmon/%.100s", de->d_name);
80 }
81 if (d) closedir(d);
82 if (!dir[0]) { snprintf(why, n, "no aien_spbm hwmon"); return -1; }
83 for (int i = 0; i < R15_SPBM_ENERGY; i++) {
84 snprintf(path, sizeof path, "%s/energy%d_label", dir, i + 1);
85 if (line_of(path, buf, sizeof buf) || strcmp(buf, tye_elabel[i])) {
86 snprintf(why, n, "energy%d label is not %s", i + 1, tye_elabel[i]);
87 return -1;
88 }
89 }
90 for (int i = 0; i < R15_SPBM_POWER; i++) {
91 snprintf(path, sizeof path, "%s/power%d_label", dir, i + 1);
92 if (line_of(path, buf, sizeof buf) || strcmp(buf, tye_plabel[i])) {
93 snprintf(why, n, "power%d label is not %s", i + 1, tye_plabel[i]);
94 return -1;
95 }
96 }
97 snprintf(e->hwmon, sizeof e->hwmon, "%s", dir);
98 return 0;
99}
100
101/* ---- machine snapshot ---- */
102typedef struct {
103 uint64_t busy[TYE_NCPU], total[TYE_NCPU];
104 uint64_t freq_khz[TYE_NCPU];
105 long temp_mc[TYE_NZONE];
106 int nzone;
107 char load[96];
108} snap;
109
110static void take_snap(snap *s) {
111 memset(s, 0, sizeof *s);
112 char line[512], path[160];
113 FILE *f = fopen("/proc/stat", "r");
114 for (; f && fgets(line, sizeof line, f);) {
115 int cpu;
116 unsigned long long v[8] = {0};
117 if (sscanf(line, "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu", &cpu, &v[0], &v[1], &v[2], &v[3],
118 &v[4], &v[5], &v[6], &v[7]) == 9 && cpu >= 0 && cpu < TYE_NCPU) {
119 s->busy[cpu] = v[0] + v[1] + v[2] + v[5] + v[6] + v[7];
120 s->total[cpu] = s->busy[cpu] + v[3] + v[4];
121 }
122 }
123 if (f) fclose(f);
124 line_of("/proc/loadavg", s->load, sizeof s->load);
125 for (int c = 0; c < TYE_NCPU; c++) {
126 snprintf(path, sizeof path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", c);
127 if (line_of(path, line, sizeof line) == 0) s->freq_khz[c] = strtoull(line, NULL, 10);
128 }
129 for (int z = 0; z < TYE_NZONE; z++) {
130 snprintf(path, sizeof path, "/sys/class/thermal/thermal_zone%d/temp", z);
131 if (line_of(path, line, sizeof line) != 0) break;
132 s->temp_mc[z] = strtol(line, NULL, 10);
133 s->nzone = z + 1;
134 }
135}
136
137static void json_snap(FILE *f, const char *key, const snap *s) {
138 fprintf(f, ",\"%s\":{\"loadavg\":\"%s\",\"freq_khz\":[", key, s->load);
139 for (int c = 0; c < TYE_NCPU; c++) fprintf(f, "%s%" PRIu64, c ? "," : "", s->freq_khz[c]);
140 fputs("],\"temp_mc\":[", f);
141 for (int z = 0; z < s->nzone; z++) fprintf(f, "%s%ld", z ? "," : "", s->temp_mc[z]);
142 fputs("],\"busy_jiffies\":[", f);
143 for (int c = 0; c < TYE_NCPU; c++) fprintf(f, "%s%" PRIu64, c ? "," : "", s->busy[c]);
144 fputs("],\"total_jiffies\":[", f);
145 for (int c = 0; c < TYE_NCPU; c++) fprintf(f, "%s%" PRIu64, c ? "," : "", s->total[c]);
146 fputs("]}", f);
147}
148
149/* ---- plan: seeded splitmix64 Fisher-Yates per round ---- */
150static uint64_t g_mix;
151static uint64_t mix_next(void) {
152 uint64_t z = (g_mix += 0x9E3779B97F4A7C15ull);
153 z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ull;
154 z = (z ^ (z >> 27)) * 0x94D049BB133111EBull;
155 return z ^ (z >> 31);
156}
157
158static int print_plan(uint64_t seed, int rounds) {
159 static const char *const cond[4] = {"IDLE", "A", "B", "AB"};
160 g_mix = seed;
161 for (int r = 0; r < rounds; r++) {
162 int o[4] = {0, 1, 2, 3};
163 for (int i = 3; i > 0; i--) {
164 int j = (int)(mix_next() % (uint64_t)(i + 1)), t = o[i];
165 o[i] = o[j];
166 o[j] = t;
167 }
168 for (int k = 0; k < 4; k++) printf("%d %d %s\n", r, k, cond[o[k]]);
169 }
170 return 0;
171}
172
173/* ---- workload processes ---- */
174typedef struct {
175 char tag[4];
176 char spec[64]; /* rz:cpus */
177 char rz[32], cpus[32];
178 pid_t pid;
179 int to_child;
180 FILE *from_child;
181 char ready[160];
182 char result[16384];
183 int status, ok;
184} part;
185
186static int spawn(part *p, const char *bin, const char *m, const char *n, const char *seed, unsigned secs) {
187 char *colon = strchr(p->spec, ':');
188 if (!colon) return -1;
189 snprintf(p->rz, sizeof p->rz, "%.*s", (int)(colon - p->spec), p->spec);
190 snprintf(p->cpus, sizeof p->cpus, "%s", colon + 1);
191 int in[2], out[2];
192 if (pipe(in) || pipe(out)) return -1;
193 char secs_s[16];
194 snprintf(secs_s, sizeof secs_s, "%u", secs);
195 p->pid = fork();
196 if (p->pid < 0) return -1;
197 if (p->pid == 0) {
198 dup2(in[0], 0);
199 dup2(out[1], 1);
200 close(in[1]);
201 close(out[0]);
202 execl(bin, bin, "--tag", p->tag, "--rz", p->rz, "--m", m, "--n", n, "--cpus", p->cpus, "--secs", secs_s,
203 "--seed", seed, (char *)NULL);
204 _exit(127);
205 }
206 close(in[0]);
207 close(out[1]);
208 p->to_child = in[1];
209 p->from_child = fdopen(out[0], "r");
210 return p->from_child ? 0 : -1;
211}
212
213static void pin_self(int cpu) {
214 cpu_set_t s;
215 CPU_ZERO(&s);
216 CPU_SET(cpu, &s);
217 sched_setaffinity(0, sizeof s, &s);
218}
219
220static void pause_ns(uint64_t ns) {
221 struct timespec ts = {(time_t)(ns / 1000000000ull), (long)(ns % 1000000000ull)};
222 nanosleep(&ts, NULL);
223}
224
225/* --shuffle SEED N: a seeded permutation of 0..N-1 (configuration order). */
226static int print_shuffle(uint64_t seed, int n) {
227 int o[64];
228 if (n < 1 || n > 64) return 64;
229 for (int i = 0; i < n; i++) o[i] = i;
230 g_mix = seed;
231 for (int i = n - 1; i > 0; i--) {
232 int j = (int)(mix_next() % (uint64_t)(i + 1)), t = o[i];
233 o[i] = o[j];
234 o[j] = t;
235 }
236 for (int i = 0; i < n; i++) printf("%d\n", o[i]);
237 return 0;
238}
239
240int main(int argc, char **argv) {
241 if (argc == 4 && !strcmp(argv[1], "--plan")) return print_plan(strtoull(argv[2], NULL, 0), atoi(argv[3]));
242 if (argc == 4 && !strcmp(argv[1], "--shuffle")) return print_shuffle(strtoull(argv[2], NULL, 0), atoi(argv[3]));
243 const char *out = NULL, *run = "run", *config = "S0", *cond = "IDLE", *bin = NULL, *sa = NULL, *sb = NULL;
244 const char *m = "8192", *n = "16384", *seed_a = "1", *seed_b = "2";
245 int round = 0, pos = 0, tool_cpu = 0;
246 unsigned secs = 30, settle_ms = 2000;
247 for (int i = 1; i + 1 < argc; i += 2) {
248 const char *k = argv[i], *v = argv[i + 1];
249 if (!strcmp(k, "--out")) out = v;
250 else if (!strcmp(k, "--run")) run = v;
251 else if (!strcmp(k, "--config")) config = v;
252 else if (!strcmp(k, "--cond")) cond = v;
253 else if (!strcmp(k, "--round")) round = atoi(v);
254 else if (!strcmp(k, "--pos")) pos = atoi(v);
255 else if (!strcmp(k, "--secs")) secs = (unsigned)strtoul(v, NULL, 10);
256 else if (!strcmp(k, "--workload")) bin = v;
257 else if (!strcmp(k, "--a")) sa = v;
258 else if (!strcmp(k, "--b")) sb = v;
259 else if (!strcmp(k, "--m")) m = v;
260 else if (!strcmp(k, "--n")) n = v;
261 else if (!strcmp(k, "--seed-a")) seed_a = v;
262 else if (!strcmp(k, "--seed-b")) seed_b = v;
263 else if (!strcmp(k, "--settle-ms")) settle_ms = (unsigned)strtoul(v, NULL, 10);
264 else if (!strcmp(k, "--tool-cpu")) tool_cpu = atoi(v);
265 else { fprintf(stderr, "ty_energy_window: unknown argument %s\n", k); return 64; }
266 }
267 if (!out || !bin || !sa || !sb || !secs) {
268 fprintf(stderr, "ty_energy_window: need --out --workload --a --b --secs\n");
269 return 64;
270 }
271 int want_a = strcmp(cond, "IDLE") && strchr(cond, 'A'), want_b = strcmp(cond, "IDLE") && strchr(cond, 'B');
272 part P[2];
273 memset(P, 0, sizeof P);
274 snprintf(P[0].tag, sizeof P[0].tag, "A");
275 snprintf(P[0].spec, sizeof P[0].spec, "%s", sa);
276 snprintf(P[1].tag, sizeof P[1].tag, "B");
277 snprintf(P[1].spec, sizeof P[1].spec, "%s", sb);
278 int np = 0;
279 part *act[2];
280 if (want_a) act[np++] = &P[0];
281 if (want_b) act[np++] = &P[1];
282
283 R15Out o;
284 if (r15_out_open(&o, out, run, cond, config, round, pos) != 0) {
285 fprintf(stderr, "ty_energy_window: cannot create %s (exists?)\n", out);
286 return 2;
287 }
288 pin_self(tool_cpu); /* the sampler thread inherits this: never on a measured core */
289 R15Energy e;
290 char why[256] = "";
291 int meter_ok = meter_attach(&e, why, sizeof why) == 0;
292 if (meter_ok) r15_energy_start_sampler(&e, TYE_MAXS);
293
294 int spawn_ok = 1;
295 for (int i = 0; i < np; i++) {
296 part *p = act[i];
297 if (spawn(p, bin, m, n, p == &P[0] ? seed_a : seed_b, secs) != 0 ||
298 !fgets(p->ready, sizeof p->ready, p->from_child) || strncmp(p->ready, "READY ", 6) != 0)
299 spawn_ok = 0;
300 p->ready[strcspn(p->ready, "\n")] = 0;
301 }
302 pause_ns((uint64_t)settle_ms * 1000000ull);
303 snap s0, s1;
304 take_snap(&s0);
305 char quiet[256] = "";
306 char qpath[512];
307 snprintf(qpath, sizeof qpath, "%s/workspace/.spark-quiet", getenv("HOME") ? getenv("HOME") : "");
308 int quiet_present = line_of(qpath, quiet, sizeof quiet) == 0;
309 for (char *c = quiet; *c; c++)
310 if (*c == '"' || *c == '\\' || (unsigned char)*c < 32) *c = ' ';
311
312 R15EnergySample e0, e1;
313 r15_energy_read(&e, &e0);
314 uint64_t t_go = r15_now_ns();
315 for (int i = 0; i < np; i++)
316 if (act[i]->to_child >= 0 && write(act[i]->to_child, "g", 1) != 1) spawn_ok = 0;
317 if (np == 0) pause_ns((uint64_t)secs * 1000000000ull);
318 for (int i = 0; i < np; i++) {
319 part *p = act[i];
320 if (!p->from_child || !fgets(p->result, sizeof p->result, p->from_child)) p->result[0] = 0;
321 p->result[strcspn(p->result, "\n")] = 0;
322 if (p->pid > 0) waitpid(p->pid, &p->status, 0);
323 p->ok = p->result[0] == '{' && WIFEXITED(p->status) && WEXITSTATUS(p->status) == 0;
324 }
325 r15_energy_read(&e, &e1);
326 take_snap(&s1);
327
328 static R15EnergySample smp[TYE_MAXS];
329 uint64_t ns = meter_ok ? r15_energy_samples(&e, e0.t_ns, e1.t_ns, smp, TYE_MAXS) : 0;
330 if (meter_ok) r15_energy_close(&e);
331
332 r15_rec_begin(&o, "ty_window");
333 FILE *f = o.f;
334 fprintf(f, ",\"meter_ok\":%d,\"meter_why\":\"%s\",\"spawn_ok\":%d,\"planned_ns\":%" PRIu64
335 ",\"settle_ns\":%" PRIu64 ",\"go_ns\":%" PRIu64 ",\"tool_cpu\":%d,\"m\":%s,\"n\":%s,"
336 "\"spec_a\":\"%s\",\"spec_b\":\"%s\",\"quiet_present\":%d,\"quiet\":\"%s\"",
337 meter_ok, why, spawn_ok, (uint64_t)secs * (uint64_t)1000000000u, (uint64_t)settle_ms * (uint64_t)1000000u, t_go,
338 tool_cpu, m, n, sa, sb, quiet_present, quiet);
339 r15_json_energy(f, "e0", &e0);
340 r15_json_energy(f, "e1", &e1);
341 json_snap(f, "s0", &s0);
342 json_snap(f, "s1", &s1);
343 fprintf(f, ",\"tel\":{\"n\":%" PRIu64 ",\"t_ns\":[", ns);
344 for (uint64_t i = 0; i < ns; i++) fprintf(f, "%s%" PRIu64, i ? "," : "", smp[i].t_ns);
345 fputs("],\"ok\":[", f);
346 for (uint64_t i = 0; i < ns; i++) fprintf(f, "%s%d", i ? "," : "", smp[i].spbm_ok);
347 for (int ch = 0; ch < 3; ch++) {
348 fprintf(f, "],\"%s\":[", tye_elabel[ch]);
349 for (uint64_t i = 0; i < ns; i++) fprintf(f, "%s%" PRIu64, i ? "," : "", smp[i].energy_uj[ch]);
350 }
351 fputs("]},\"participants\":[", f);
352 for (int i = 0; i < np; i++)
353 fprintf(f, "%s{\"ok\":%d,\"exit\":%d,\"ready\":\"%s\",\"result\":%s}", i ? "," : "", act[i]->ok,
354 WIFEXITED(act[i]->status) ? WEXITSTATUS(act[i]->status) : -1, act[i]->ready,
355 act[i]->result[0] == '{' ? act[i]->result : "null");
356 fputc(']', f);
357 r15_rec_end(&o);
358 int rc = r15_out_close(&o);
359 for (int i = 0; i < np; i++) {
360 if (act[i]->to_child > 0) close(act[i]->to_child);
361 if (act[i]->from_child) fclose(act[i]->from_child);
362 }
363 return rc == 0 && meter_ok && spawn_ok ? 0 : 1;
364}