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 / r15_measure.h
| 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 | * r15_measure.h -- R15 physical measurement: CPU PMU (spec §16 C1 item 8), |
| 19 | * SPBM package energy and NVML graphics energy (§17 C2), latency histograms, |
| 20 | * and the JSON Lines evidence writer (§12). |
| 21 | * |
| 22 | * Nothing here decides anything about the body; it only observes. |
| 23 | */ |
| 24 | #ifndef R15_MEASURE_H |
| 25 | #define R15_MEASURE_H |
| 26 | |
| 27 | #include <pthread.h> |
| 28 | #include <stdint.h> |
| 29 | #include <stdio.h> |
| 30 | |
| 31 | /* ---- PMU ------------------------------------------------------------------ |
| 32 | * Per task (pid 0, cpu -1), inherit, both CPU PMUs, six events, no groups, |
| 33 | * user + kernel, exclude_hv. Open from the main thread before any thread |
| 34 | * whose work is to be counted is created. Raw sums over the PMUs; never |
| 35 | * scaled. */ |
| 36 | #define R15_PMU_EVENTS 6 |
| 37 | #define R15_PMU_MAX 2 |
| 38 | extern const char *const r15_pmu_event_name[R15_PMU_EVENTS]; |
| 39 | |
| 40 | typedef struct { |
| 41 | int n_pmu; |
| 42 | uint32_t type[R15_PMU_MAX]; |
| 43 | char name[R15_PMU_MAX][32]; |
| 44 | char cpus[R15_PMU_MAX][64]; |
| 45 | int fd[R15_PMU_MAX][R15_PMU_EVENTS]; |
| 46 | } R15Pmu; |
| 47 | |
| 48 | typedef struct { |
| 49 | uint64_t value[R15_PMU_MAX][R15_PMU_EVENTS]; |
| 50 | uint64_t enabled[R15_PMU_MAX][R15_PMU_EVENTS]; |
| 51 | uint64_t running[R15_PMU_MAX][R15_PMU_EVENTS]; |
| 52 | uint64_t sum[R15_PMU_EVENTS]; /* raw sum over PMUs */ |
| 53 | int multiplexed; /* running(A)+running(X) != enabled beyond 0.1% */ |
| 54 | int ok; |
| 55 | } R15PmuRead; |
| 56 | |
| 57 | int r15_pmu_open(R15Pmu *p, char *why, size_t n); |
| 58 | void r15_pmu_start(R15Pmu *p); /* RESET + ENABLE */ |
| 59 | void r15_pmu_stop(R15Pmu *p, R15PmuRead *out); /* DISABLE + read */ |
| 60 | void r15_pmu_close(R15Pmu *p); |
| 61 | |
| 62 | /* ---- energy -------------------------------------------------------------- */ |
| 63 | #define R15_SPBM_ENERGY 5 /* pkg, cpu_e, cpu_p, gpc_unverified, gpm */ |
| 64 | #define R15_SPBM_POWER 5 /* sys_total, soc_pkg, cpu_e, cpu_p, gpu */ |
| 65 | |
| 66 | typedef struct { |
| 67 | uint64_t t_ns; /* CLOCK_MONOTONIC after the reads */ |
| 68 | uint64_t read_span_ns; |
| 69 | uint64_t energy_uj[R15_SPBM_ENERGY]; |
| 70 | uint64_t power_uw[R15_SPBM_POWER]; |
| 71 | uint64_t overflow[R15_SPBM_ENERGY]; |
| 72 | uint64_t nvml_mj; |
| 73 | uint32_t nvml_mw; |
| 74 | int spbm_ok, nvml_ok; |
| 75 | } R15EnergySample; |
| 76 | |
| 77 | typedef struct { |
| 78 | char hwmon[128]; /* empty: no SPBM reader (metric 17 unavailable) */ |
| 79 | void *nvml_lib, *nvml_dev; |
| 80 | int (*nvml_energy)(void *, unsigned long long *); |
| 81 | int (*nvml_power)(void *, unsigned int *); |
| 82 | int (*nvml_shutdown)(void); |
| 83 | /* 10 Hz sampler (spec §7): started before the PMU is opened, so its own |
| 84 | * reads are not counted as the body's work. */ |
| 85 | pthread_t thread; |
| 86 | int running; |
| 87 | volatile int stop; |
| 88 | pthread_mutex_t mu; |
| 89 | R15EnergySample *ring; |
| 90 | uint64_t cap, n; /* samples taken since start (may exceed cap) */ |
| 91 | } R15Energy; |
| 92 | |
| 93 | int r15_energy_open(R15Energy *e, char *why, size_t n); |
| 94 | int r15_energy_read(R15Energy *e, R15EnergySample *s); |
| 95 | int r15_energy_start_sampler(R15Energy *e, uint64_t cap); |
| 96 | /* Samples taken in [t0, t1]; returns count copied (<= max). */ |
| 97 | uint64_t r15_energy_samples(R15Energy *e, uint64_t t0, uint64_t t1, |
| 98 | R15EnergySample *out, uint64_t max); |
| 99 | void r15_energy_close(R15Energy *e); |
| 100 | |
| 101 | /* ---- histogram ------------------------------------------------------------ |
| 102 | * Log-linear buckets: exact below 64 ns, then 64 sub-buckets per octave |
| 103 | * (relative bucket width <= 1/64). Stored sparsely in evidence. */ |
| 104 | #define R15_HIST_SUB 64 |
| 105 | #define R15_HIST_BUCKETS (64 + 58 * R15_HIST_SUB) |
| 106 | typedef struct { |
| 107 | uint64_t count[R15_HIST_BUCKETS]; |
| 108 | uint64_t n, min, max, sum; |
| 109 | } R15Hist; |
| 110 | void r15_hist_add(R15Hist *h, uint64_t v); |
| 111 | uint64_t r15_hist_lower(uint32_t bucket); |
| 112 | uint32_t r15_hist_bucket(uint64_t v); |
| 113 | void r15_hist_json(FILE *f, const R15Hist *h); |
| 114 | |
| 115 | /* ---- evidence ------------------------------------------------------------- */ |
| 116 | typedef struct { |
| 117 | FILE *f; |
| 118 | char run[64], level[16], config[16]; |
| 119 | int round, trial; |
| 120 | } R15Out; |
| 121 | int r15_out_open(R15Out *o, const char *path, const char *run, const char *level, |
| 122 | const char *config, int round, int trial); |
| 123 | /* Starts a record with the common keys; the caller appends ",\"k\":v..." |
| 124 | * and ends it with r15_rec_end. */ |
| 125 | void r15_rec_begin(R15Out *o, const char *kind); |
| 126 | void r15_rec_end(R15Out *o); |
| 127 | void r15_json_energy(FILE *f, const char *key, const R15EnergySample *s); |
| 128 | void r15_json_pmu(FILE *f, const char *key, const R15Pmu *p, const R15PmuRead *r); |
| 129 | int r15_out_close(R15Out *o); |
| 130 | |
| 131 | uint64_t r15_now_ns(void); |
| 132 | uint64_t r15_thread_cpu_ns(pthread_t t); /* 0 if unavailable */ |
| 133 | |
| 134 | #endif |