Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/tests/runtime/r15_measure.c # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / tests / runtime / r15_measure.c` 409 lines ยท 15710 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 /* r15_measure.c -- see r15_measure.h. Observation only. */ 18 #ifndef _GNU_SOURCE 19 #define _GNU_SOURCE 20 #endif 21 #include "r15_measure.h" 22 23 #include 24 #include 25 #include 26 #include 27 #include 28 #include 29 #include 30 #include 31 #include 32 #include 33 #include 34 #include 35 36 const char *const r15_pmu_event_name[R15_PMU_EVENTS] = { 37 "cpu_cycles", "inst_retired", "bus_access", "ll_cache_miss_rd", 38 "l2d_cache_refill", "mem_access"}; 39 static const uint64_t k_pmu_event[R15_PMU_EVENTS] = {0x11, 0x08, 0x19, 0x37, 0x17, 0x13}; 40 41 uint64_t r15_now_ns(void) { 42 struct timespec ts; 43 clock_gettime(CLOCK_MONOTONIC, &ts); 44 return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; 45 } 46 47 uint64_t r15_thread_cpu_ns(pthread_t t) { 48 clockid_t c; 49 struct timespec ts; 50 if (pthread_getcpuclockid(t, &c) != 0 || clock_gettime(c, &ts) != 0) return 0; 51 return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; 52 } 53 54 static int read_line(const char *path, char *buf, size_t n) { 55 FILE *f = fopen(path, "r"); 56 if (!f) return -1; 57 char *ok = fgets(buf, (int)n, f); 58 fclose(f); 59 if (!ok) return -1; 60 buf[strcspn(buf, "\n")] = 0; 61 return 0; 62 } 63 64 /* ---- PMU ------------------------------------------------------------------ */ 65 66 int r15_pmu_open(R15Pmu *p, char *why, size_t n) { 67 memset(p, 0, sizeof *p); 68 for (int i = 0; i < R15_PMU_MAX; i++) 69 for (int e = 0; e < R15_PMU_EVENTS; e++) p->fd[i][e] = -1; 70 DIR *d = opendir("/sys/bus/event_source/devices"); 71 if (!d) { snprintf(why, n, "no event_source devices"); return -1; } 72 struct dirent *de; 73 char names[R15_PMU_MAX][32]; 74 int found = 0; 75 while ((de = readdir(d)) && found < R15_PMU_MAX) 76 if (strncmp(de->d_name, "armv8_pmuv3_", 12) == 0) 77 snprintf(names[found++], 32, "%.31s", de->d_name); 78 closedir(d); 79 if (found == 0) { snprintf(why, n, "no armv8_pmuv3 PMU"); return -1; } 80 /* stable order: _0 then _1 */ 81 if (found == 2 && strcmp(names[0], names[1]) > 0) { 82 char t[32]; 83 memcpy(t, names[0], 32); memcpy(names[0], names[1], 32); memcpy(names[1], t, 32); 84 } 85 for (int i = 0; i < found; i++) { 86 char path[256], buf[128]; 87 memcpy(p->name[i], names[i], sizeof p->name[i]); 88 snprintf(path, sizeof path, "/sys/bus/event_source/devices/%s/type", names[i]); 89 if (read_line(path, buf, sizeof buf) != 0) { snprintf(why, n, "%s: no type", names[i]); return -1; } 90 p->type[i] = (uint32_t)strtoul(buf, NULL, 10); 91 snprintf(path, sizeof path, "/sys/bus/event_source/devices/%s/cpus", names[i]); 92 if (read_line(path, p->cpus[i], sizeof p->cpus[i]) != 0) p->cpus[i][0] = 0; 93 for (int e = 0; e < R15_PMU_EVENTS; e++) { 94 struct perf_event_attr a; 95 memset(&a, 0, sizeof a); 96 a.size = sizeof a; 97 a.type = p->type[i]; 98 a.config = k_pmu_event[e]; 99 a.disabled = 1; 100 a.inherit = 1; 101 a.exclude_hv = 1; 102 a.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING; 103 int fd = (int)syscall(SYS_perf_event_open, &a, 0, -1, -1, PERF_FLAG_FD_CLOEXEC); 104 if (fd < 0) { 105 snprintf(why, n, "perf_event_open %s %s: %s", names[i], r15_pmu_event_name[e], 106 strerror(errno)); 107 r15_pmu_close(p); 108 return -1; 109 } 110 p->fd[i][e] = fd; 111 } 112 } 113 p->n_pmu = found; 114 return 0; 115 } 116 117 void r15_pmu_start(R15Pmu *p) { 118 for (int i = 0; i < p->n_pmu; i++) 119 for (int e = 0; e < R15_PMU_EVENTS; e++) { 120 ioctl(p->fd[i][e], PERF_EVENT_IOC_RESET, 0); 121 ioctl(p->fd[i][e], PERF_EVENT_IOC_ENABLE, 0); 122 } 123 } 124 125 void r15_pmu_stop(R15Pmu *p, R15PmuRead *out) { 126 memset(out, 0, sizeof *out); 127 for (int i = 0; i < p->n_pmu; i++) 128 for (int e = 0; e < R15_PMU_EVENTS; e++) ioctl(p->fd[i][e], PERF_EVENT_IOC_DISABLE, 0); 129 out->ok = p->n_pmu > 0; 130 for (int i = 0; i < p->n_pmu; i++) 131 for (int e = 0; e < R15_PMU_EVENTS; e++) { 132 uint64_t v[3]; 133 if (read(p->fd[i][e], v, sizeof v) != (ssize_t)sizeof v) { out->ok = 0; continue; } 134 out->value[i][e] = v[0]; 135 out->enabled[i][e] = v[1]; 136 out->running[i][e] = v[2]; 137 out->sum[e] += v[0]; 138 } 139 /* C1 item 8: running(A)+running(X) must equal enabled within 0.1%. */ 140 for (int e = 0; e < R15_PMU_EVENTS && p->n_pmu > 0; e++) { 141 uint64_t run = 0, en = out->enabled[0][e]; 142 for (int i = 0; i < p->n_pmu; i++) run += out->running[i][e]; 143 uint64_t diff = run > en ? run - en : en - run; 144 if (en && diff * 1000u > en) out->multiplexed = 1; 145 } 146 } 147 148 void r15_pmu_close(R15Pmu *p) { 149 for (int i = 0; i < R15_PMU_MAX; i++) 150 for (int e = 0; e < R15_PMU_EVENTS; e++) 151 if (p->fd[i][e] >= 0) { close(p->fd[i][e]); p->fd[i][e] = -1; } 152 p->n_pmu = 0; 153 } 154 155 /* ---- energy --------------------------------------------------------------- */ 156 157 static const char *const k_elabel[R15_SPBM_ENERGY] = {"pkg", "cpu_e", "cpu_p", "gpc_unverified", "gpm"}; 158 static const char *const k_plabel[R15_SPBM_POWER] = {"sys_total", "soc_pkg", "cpu_e", "cpu_p", "gpu"}; 159 160 static int read_u64(const char *dir, const char *kind, int i, const char *suffix, uint64_t *v) { 161 char path[256], buf[64], *end; 162 snprintf(path, sizeof path, "%s/%s%d_%s", dir, kind, i, suffix); 163 int fd = open(path, O_RDONLY | O_CLOEXEC); 164 if (fd < 0) return -1; 165 ssize_t got = read(fd, buf, sizeof buf - 1); 166 close(fd); 167 if (got <= 0) return -1; 168 buf[got] = 0; 169 errno = 0; 170 *v = strtoull(buf, &end, 10); 171 return errno || end == buf || buf[0] == '-' ? -1 : 0; 172 } 173 174 int r15_energy_open(R15Energy *e, char *why, size_t n) { 175 memset(e, 0, sizeof *e); 176 pthread_mutex_init(&e->mu, NULL); 177 DIR *d = opendir("/sys/class/hwmon"); 178 if (d) { 179 struct dirent *de; 180 while ((de = readdir(d))) { 181 char path[256], buf[64]; 182 snprintf(path, sizeof path, "/sys/class/hwmon/%.200s/name", de->d_name); 183 if (read_line(path, buf, sizeof buf) == 0 && strcmp(buf, "aien_spbm") == 0) { 184 snprintf(e->hwmon, sizeof e->hwmon, "/sys/class/hwmon/%.100s", de->d_name); 185 break; 186 } 187 } 188 closedir(d); 189 } 190 if (e->hwmon[0]) { 191 for (int i = 0; i < R15_SPBM_ENERGY; i++) { 192 char path[256], buf[64]; 193 snprintf(path, sizeof path, "%s/energy%d_label", e->hwmon, i + 1); 194 if (read_line(path, buf, sizeof buf) != 0 || strcmp(buf, k_elabel[i]) != 0) { 195 snprintf(why, n, "SPBM energy%d label is not %s", i + 1, k_elabel[i]); 196 e->hwmon[0] = 0; 197 break; 198 } 199 } 200 for (int i = 0; i < R15_SPBM_POWER && e->hwmon[0]; i++) { 201 char path[256], buf[64]; 202 snprintf(path, sizeof path, "%s/power%d_label", e->hwmon, i + 1); 203 if (read_line(path, buf, sizeof buf) != 0 || strcmp(buf, k_plabel[i]) != 0) { 204 snprintf(why, n, "SPBM power%d label is not %s", i + 1, k_plabel[i]); 205 e->hwmon[0] = 0; 206 } 207 } 208 } else { 209 snprintf(why, n, "no aien_spbm hwmon: package energy unavailable"); 210 } 211 e->nvml_lib = dlopen("libnvidia-ml.so.1", RTLD_NOW | RTLD_LOCAL); 212 if (e->nvml_lib) { 213 int (*init)(void) = (int (*)(void))dlsym(e->nvml_lib, "nvmlInit_v2"); 214 int (*handle)(unsigned int, void **) = 215 (int (*)(unsigned int, void **))dlsym(e->nvml_lib, "nvmlDeviceGetHandleByIndex_v2"); 216 e->nvml_energy = (int (*)(void *, unsigned long long *)) 217 dlsym(e->nvml_lib, "nvmlDeviceGetTotalEnergyConsumption"); 218 e->nvml_power = (int (*)(void *, unsigned int *))dlsym(e->nvml_lib, "nvmlDeviceGetPowerUsage"); 219 e->nvml_shutdown = (int (*)(void))dlsym(e->nvml_lib, "nvmlShutdown"); 220 if (!init || !handle || !e->nvml_energy || !e->nvml_power || init() != 0 || 221 handle(0, &e->nvml_dev) != 0) 222 e->nvml_dev = NULL; 223 } 224 return e->hwmon[0] ? 0 : -1; 225 } 226 227 int r15_energy_read(R15Energy *e, R15EnergySample *s) { 228 memset(s, 0, sizeof *s); 229 uint64_t t0 = r15_now_ns(); 230 if (e->hwmon[0]) { 231 s->spbm_ok = 1; 232 for (int i = 0; i < R15_SPBM_ENERGY; i++) { 233 if (read_u64(e->hwmon, "energy", i + 1, "input", &s->energy_uj[i]) != 0 || 234 read_u64(e->hwmon, "energy", i + 1, "overflow_raw", &s->overflow[i]) != 0) 235 s->spbm_ok = 0; 236 } 237 for (int i = 0; i < R15_SPBM_POWER; i++) 238 if (read_u64(e->hwmon, "power", i + 1, "input", &s->power_uw[i]) != 0) s->spbm_ok = 0; 239 } 240 if (e->nvml_dev) { 241 unsigned long long mj = 0; 242 unsigned int mw = 0; 243 s->nvml_ok = e->nvml_energy(e->nvml_dev, &mj) == 0 && e->nvml_power(e->nvml_dev, &mw) == 0; 244 s->nvml_mj = mj; 245 s->nvml_mw = mw; 246 } 247 s->t_ns = r15_now_ns(); 248 s->read_span_ns = s->t_ns - t0; 249 return s->spbm_ok ? 0 : -1; 250 } 251 252 static void *sampler_main(void *arg) { 253 R15Energy *e = arg; 254 uint64_t next = r15_now_ns(); 255 while (!__atomic_load_n(&e->stop, __ATOMIC_ACQUIRE)) { 256 R15EnergySample s; 257 r15_energy_read(e, &s); 258 pthread_mutex_lock(&e->mu); 259 e->ring[e->n % e->cap] = s; 260 e->n++; 261 pthread_mutex_unlock(&e->mu); 262 next += 100000000ull; /* 10 Hz */ 263 uint64_t now = r15_now_ns(); 264 if (next > now) { 265 struct timespec ts = {(time_t)((next - now) / 1000000000ull), 266 (long)((next - now) % 1000000000ull)}; 267 nanosleep(&ts, NULL); 268 } else { 269 next = now; 270 } 271 } 272 return NULL; 273 } 274 275 int r15_energy_start_sampler(R15Energy *e, uint64_t cap) { 276 e->ring = calloc(cap, sizeof *e->ring); 277 if (!e->ring) return -1; 278 e->cap = cap; 279 e->n = 0; 280 e->stop = 0; 281 if (pthread_create(&e->thread, NULL, sampler_main, e) != 0) return -1; 282 e->running = 1; 283 return 0; 284 } 285 286 uint64_t r15_energy_samples(R15Energy *e, uint64_t t0, uint64_t t1, R15EnergySample *out, 287 uint64_t max) { 288 uint64_t got = 0; 289 pthread_mutex_lock(&e->mu); 290 uint64_t first = e->n > e->cap ? e->n - e->cap : 0; 291 for (uint64_t i = first; i < e->n && got < max; i++) { 292 const R15EnergySample *s = &e->ring[i % e->cap]; 293 if (s->t_ns >= t0 && s->t_ns <= t1) out[got++] = *s; 294 } 295 pthread_mutex_unlock(&e->mu); 296 return got; 297 } 298 299 void r15_energy_close(R15Energy *e) { 300 if (e->running) { 301 __atomic_store_n(&e->stop, 1, __ATOMIC_RELEASE); 302 pthread_join(e->thread, NULL); 303 e->running = 0; 304 } 305 free(e->ring); 306 e->ring = NULL; 307 if (e->nvml_dev && e->nvml_shutdown) e->nvml_shutdown(); 308 if (e->nvml_lib) dlclose(e->nvml_lib); 309 e->nvml_lib = e->nvml_dev = NULL; 310 } 311 312 /* ---- histogram ------------------------------------------------------------ */ 313 314 uint32_t r15_hist_bucket(uint64_t v) { 315 if (v < 64) return (uint32_t)v; 316 uint32_t e = 63u - (uint32_t)__builtin_clzll(v); /* 6..63 */ 317 uint32_t sub = (uint32_t)(v >> (e - 6)) & 63u; 318 return 64u + (e - 6u) * R15_HIST_SUB + sub; 319 } 320 321 uint64_t r15_hist_lower(uint32_t b) { 322 if (b < 64) return b; 323 uint32_t e = (b - 64u) / R15_HIST_SUB + 6u, sub = (b - 64u) % R15_HIST_SUB; 324 return (uint64_t)(64u + sub) << (e - 6u); 325 } 326 327 void r15_hist_add(R15Hist *h, uint64_t v) { 328 h->count[r15_hist_bucket(v)]++; 329 if (h->n == 0 || v < h->min) h->min = v; 330 if (v > h->max) h->max = v; 331 h->n++; 332 h->sum += v; 333 } 334 335 /* {"n":..,"min":..,"max":..,"sum":..,"b":[[lower,count],...]} */ 336 void r15_hist_json(FILE *f, const R15Hist *h) { 337 fprintf(f, "{\"n\":%" PRIu64 ",\"min\":%" PRIu64 ",\"max\":%" PRIu64 ",\"sum\":%" PRIu64 ",\"b\":[", 338 h->n, h->min, h->max, h->sum); 339 int first = 1; 340 for (uint32_t b = 0; b < R15_HIST_BUCKETS; b++) 341 if (h->count[b]) { 342 fprintf(f, "%s[%" PRIu64 ",%" PRIu64 "]", first ? "" : ",", r15_hist_lower(b), h->count[b]); 343 first = 0; 344 } 345 fputs("]}", f); 346 } 347 348 /* ---- evidence ------------------------------------------------------------- */ 349 350 int r15_out_open(R15Out *o, const char *path, const char *run, const char *level, 351 const char *config, int round, int trial) { 352 memset(o, 0, sizeof *o); 353 o->f = fopen(path, "wx"); /* never overwrite raw evidence */ 354 if (!o->f) return -1; 355 snprintf(o->run, sizeof o->run, "%s", run); 356 snprintf(o->level, sizeof o->level, "%s", level); 357 snprintf(o->config, sizeof o->config, "%s", config); 358 o->round = round; 359 o->trial = trial; 360 return 0; 361 } 362 363 void r15_rec_begin(R15Out *o, const char *kind) { 364 fprintf(o->f, "{\"run\":\"%s\",\"level\":\"%s\",\"config\":\"%s\",\"round\":%d,\"trial\":%d," 365 "\"kind\":\"%s\",\"t_ns\":%" PRIu64, 366 o->run, o->level, o->config, o->round, o->trial, kind, r15_now_ns()); 367 } 368 369 void r15_rec_end(R15Out *o) { 370 fputs("}\n", o->f); 371 fflush(o->f); 372 } 373 374 void r15_json_energy(FILE *f, const char *key, const R15EnergySample *s) { 375 fprintf(f, ",\"%s\":{\"t_ns\":%" PRIu64 ",\"span_ns\":%" PRIu64 ",\"spbm_ok\":%d,\"energy_uj\":[", 376 key, s->t_ns, s->read_span_ns, s->spbm_ok); 377 for (int i = 0; i < R15_SPBM_ENERGY; i++) fprintf(f, "%s%" PRIu64, i ? "," : "", s->energy_uj[i]); 378 fputs("],\"overflow\":[", f); 379 for (int i = 0; i < R15_SPBM_ENERGY; i++) fprintf(f, "%s%" PRIu64, i ? "," : "", s->overflow[i]); 380 fputs("],\"power_uw\":[", f); 381 for (int i = 0; i < R15_SPBM_POWER; i++) fprintf(f, "%s%" PRIu64, i ? "," : "", s->power_uw[i]); 382 fprintf(f, "],\"nvml_ok\":%d,\"nvml_mj\":%" PRIu64 ",\"nvml_mw\":%u}", s->nvml_ok, s->nvml_mj, 383 s->nvml_mw); 384 } 385 386 void r15_json_pmu(FILE *f, const char *key, const R15Pmu *p, const R15PmuRead *r) { 387 fprintf(f, ",\"%s\":{\"ok\":%d,\"multiplexed\":%d,\"sum\":{", key, r->ok, r->multiplexed); 388 for (int e = 0; e < R15_PMU_EVENTS; e++) 389 fprintf(f, "%s\"%s\":%" PRIu64, e ? "," : "", r15_pmu_event_name[e], r->sum[e]); 390 fputs("},\"pmu\":[", f); 391 for (int i = 0; i < p->n_pmu; i++) { 392 fprintf(f, "%s{\"name\":\"%s\",\"type\":%u,\"cpus\":\"%s\",\"value\":[", i ? "," : "", 393 p->name[i], p->type[i], p->cpus[i]); 394 for (int e = 0; e < R15_PMU_EVENTS; e++) fprintf(f, "%s%" PRIu64, e ? "," : "", r->value[i][e]); 395 fputs("],\"enabled\":[", f); 396 for (int e = 0; e < R15_PMU_EVENTS; e++) fprintf(f, "%s%" PRIu64, e ? "," : "", r->enabled[i][e]); 397 fputs("],\"running\":[", f); 398 for (int e = 0; e < R15_PMU_EVENTS; e++) fprintf(f, "%s%" PRIu64, e ? "," : "", r->running[i][e]); 399 fputs("]}", f); 400 } 401 fputs("]}", f); 402 } 403 404 int r15_out_close(R15Out *o) { 405 if (!o->f) return 0; 406 int rc = fclose(o->f); 407 o->f = NULL; 408 return rc; 409 }