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/tools/r15_machine_state.sh # 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 / tools / r15_machine_state.sh` 292 lines · 17429 bytes Raw Blame History 1 #!/bin/bash 2 # SPDX-License-Identifier: Apache-2.0 3 # Copyright 2026 Drake Stapleton (AIEN / aien-dev) 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 # r15_machine_state.sh -- machine physical state for R15 runs 18 # (spec/r15-performance-proof.md §18, clarification C3 of 2026-09-28, 19 # before any qualification data). Observability only: nothing here is a gate, and no trial is 20 # dropped or rerun because of it. 21 # 22 # tools/r15_machine_state.sh preflight exit 0 if the X925 sustains 23 # its clock, 3 if it does not 24 # tools/r15_machine_state.sh energy-preflight exit 0 if the SPBM package-energy 25 # reader gives valid samples, 5 if not 26 # tools/r15_machine_state.sh start begin 1 s sampling 27 # tools/r15_machine_state.sh mark 28 # tools/r15_machine_state.sh stop end sampling, write summary (+ kernel-xid.txt) 29 # tools/r15_machine_state.sh xid-scan Xid lines from the kernel log 30 # 31 # The operating system's reported frequency does not show the sustained X925 32 # clock cut seen on 2026-09-28 (scaling_cur_freq stayed at 3.9 GHz while the 33 # core ran at 3.0-3.5 GHz), so the effective clock comes from each core's 34 # cycle counter (perf, system-wide, read from outside the measured processes) 35 # divided by the core's busy time from /proc/stat. 36 set -u 37 CUT_GHZ=3.75 # the preflight fails below this sustained median 38 39 # preflight_ghz : effective GHz per second while the core was fully 40 # busy (1 s intervals); exit 0 if the median of seconds 13-20 reaches CUT_GHZ, 41 # 3 below it, 4 with no samples. Counts only the CPU PMU cycles event: newer 42 # kernels also list SMMU PMUs (smmuv3_pmcg_*/cycles/) in the same interval, 43 # which are not core clocks. 44 preflight_ghz() { 45 awk -F, -v cut="$CUT_GHZ" ' 46 $5 == "cycles" || $5 ~ /^armv8_pmuv3(_[0-9]+)?\/cycles\/$/ { n++; g = $3 / ($1 - last) / 1e9; last = $1; if (n > 12 && n <= 20) s[++m] = g; all = all sprintf(" %.3f", g) } 47 END { 48 if (m == 0) { print "preflight: no samples"; exit 4 } 49 asort(s); med = (m % 2) ? s[(m + 1) / 2] : (s[m / 2] + s[m / 2 + 1]) / 2 50 printf "per-second GHz:%s\nmedian seconds 13-20: %.3f GHz (cut-off %.2f)\n", all, med, cut 51 exit (med < cut) ? 3 : 0 52 }' "$1" 53 } 54 55 # preflight-parse : the preflight verdict on a recorded file (tests). 56 if [ "${1:-}" = preflight-parse ]; then preflight_ghz "${2:?perf csv}"; exit $?; fi 57 58 59 # energy_preflight: is the spec §7/§17 C2 package-energy reader present and 60 # returning valid samples? The binding source is the owner-key signed 61 # read-only SPBM reader (hwmon name "aien_spbm", energy1 labelled "pkg", the 62 # same match tests/runtime/r15_measure.c uses). Spec §7: "If package energy 63 # cannot be read by a physical hardware source at qualification time, metric 64 # 17 is incomplete and R15 cannot PASS. GPU-domain energy alone is not 65 # accepted", and §17 says GPM and NVML are "never substituted". So no other 66 # source is tried here. Exit 0 = reader valid; 5 = INSTRUMENT_UNAVAILABLE 67 # (reason=absent or reason=invalid). That is a blocked run, not a measured 68 # FAIL: no trial is started, no receipt is written. 69 # Env (tests): R15_HWMON_ROOT (default /sys/class/hwmon), R15_ENERGY_PREFLIGHT_SLEEP (0.1 s). 70 # The reader contract is read from tests/runtime/r15_measure.c itself (its 71 # k_elabel / k_plabel tables), so the preflight cannot drift from what 72 # r15_energy_open / r15_energy_read require: all 5 energy and 5 power labels 73 # exact; every energyN_input, energyN_overflow_raw and powerN_input readable 74 # as a non-negative integer. Beyond that the preflight also requires every 75 # overflow_raw to be 0 and package energy to advance over three samples. 76 # Counter headroom: spec §17 "A window is refused if any overflow flag is set, 77 # any counter decreases, or headroom is below the window length at a 600 W 78 # bound (sample.c)". The per-window refusal lives in research/m15/spbm/sample.c; 79 # the R15 harness and reducer reject overflow flags and decreases (energy_ok). 80 # This preflight adds the headroom check for R15_ENERGY_HEADROOM_SECONDS 81 # (default 300 s, longer than any single measured process) at 600 W. 82 R15_MEASURE_C=$(cd "$(dirname "$0")/.." && pwd)/tests/runtime/r15_measure.c 83 energy_preflight() { 84 local root=${R15_HWMON_ROOT:-/sys/class/hwmon} nap=${R15_ENERGY_PREFLIGHT_SLEEP:-0.1} 85 local hs=${R15_ENERGY_HEADROOM_SECONDS:-300} 86 local run_s=${R15_PLANNED_RUN_SECONDS:-2400} run_w=${R15_RUN_BOUND_WATTS:-100} min_w=${R15_ENERGY_MIN_WATTS:-10} 87 local up_file=${R15_UPTIME_FILE:-/proc/uptime} up up_s 88 declare -a last=() 89 local h="" d s v prev="" first="" n=0 i lab want 90 local el pl 91 el=$(sed -n 's/^static const char \*const k_elabel\[[A-Z_0-9]*\] = {\(.*\)};/\1/p' "$R15_MEASURE_C" | tr -d '" ') 92 pl=$(sed -n 's/^static const char \*const k_plabel\[[A-Z_0-9]*\] = {\(.*\)};/\1/p' "$R15_MEASURE_C" | tr -d '" ') 93 if [ "$(printf "%s" "$el" | awk -F, "{print NF}")" != 5 ] || [ "$(printf "%s" "$pl" | awk -F, "{print NF}")" != 5 ]; then 94 echo "INSTRUMENT_UNAVAILABLE reason=invalid: cannot read the 5+5 label tables from $R15_MEASURE_C" 95 return 5 96 fi 97 for d in "$root"/hwmon*; do 98 [ "$(cat "$d/name" 2>/dev/null)" = aien_spbm ] && { h=$d; break; } 99 done 100 if [ -z "$h" ]; then 101 echo "INSTRUMENT_UNAVAILABLE reason=absent: no hwmon named aien_spbm (the signed SPBM reader is not loaded); package energy cannot be read, R15 cannot PASS (spec §7)" 102 return 5 103 fi 104 for i in 1 2 3 4 5; do 105 want=$(printf '%s' "$el" | cut -d, -f$i); lab=$(cat "$h/energy${i}_label" 2>/dev/null) 106 [ "$lab" = "$want" ] || { echo "INSTRUMENT_UNAVAILABLE reason=invalid: energy${i}_label is '$lab', not $want"; return 5; } 107 want=$(printf '%s' "$pl" | cut -d, -f$i); lab=$(cat "$h/power${i}_label" 2>/dev/null) 108 [ "$lab" = "$want" ] || { echo "INSTRUMENT_UNAVAILABLE reason=invalid: power${i}_label is '$lab', not $want"; return 5; } 109 done 110 up=$(cut -d" " -f1 "$up_file" 2>/dev/null); up_s=${up%%.*} 111 case "$up_s" in ''|*[!0-9]*) echo "INSTRUMENT_UNAVAILABLE reason=invalid: cannot read uptime from $up_file, so a wrapped or reset counter cannot be ruled out"; return 5;; esac 112 for s in 1 2 3; do 113 for i in 1 2 3 4 5; do 114 v=$(cat "$h/energy${i}_overflow_raw" 2>/dev/null) 115 case "$v" in 0) ;; '') echo "INSTRUMENT_UNAVAILABLE reason=invalid: energy${i}_overflow_raw unreadable"; return 5;; 116 *) echo "INSTRUMENT_UNAVAILABLE reason=invalid: energy${i}_overflow_raw is '$v' (must be 0)"; return 5;; esac 117 v=$(cat "$h/power${i}_input" 2>/dev/null) 118 case "$v" in ''|*[!0-9]*) echo "INSTRUMENT_UNAVAILABLE reason=invalid: power${i}_input unreadable or not a number ('$v')"; return 5;; esac 119 v=$(cat "$h/energy${i}_input" 2>/dev/null) 120 case "$v" in ''|*[!0-9]*) echo "INSTRUMENT_UNAVAILABLE reason=invalid: energy${i}_input sample $s unreadable or not a number ('$v')"; return 5;; esac 121 if [ "$i" = 1 ]; then 122 if [ "$v" -le 0 ]; then echo "INSTRUMENT_UNAVAILABLE reason=invalid: package sample $s reads $v uJ (a running counter is above zero)"; return 5; fi 123 if [ -n "$prev" ] && [ "$v" -lt "$prev" ]; then echo "INSTRUMENT_UNAVAILABLE reason=invalid: package counter went backwards ($prev -> $v uJ)"; return 5; fi 124 [ -z "$first" ] && first=$v 125 prev=$v; n=$((n + 1)) 126 fi 127 # every channel is a monotonic counter: a decrease on any of them (not only package) invalidates the reader 128 if [ -n "${last[$i]:-}" ] && [ "$v" -lt "${last[$i]}" ]; then echo "INSTRUMENT_UNAVAILABLE reason=invalid: energy${i} counter went backwards (${last[$i]} -> $v uJ)"; return 5; fi 129 last[$i]=$v 130 # a counter that counts from boot cannot sit below a floor power times the uptime unless it wrapped or was reset 131 if [ "$i" = 1 ] && [ "$v" -lt $((up_s * min_w * `1000000`)) ]; then 132 echo "INSTRUMENT_UNAVAILABLE reason=invalid: package counter $v uJ is below ${min_w} W x ${up_s} s of uptime: the counter wrapped or was reset (spec §17 does not unwrap)"; return 5 133 fi 134 # headroom to the 32-bit mJ wrap (as uJ) at a 600 W bound over the window 135 if [ $((`4294967295` * 1000 - v)) -lt $((hs * `600000000`)) ]; then 136 echo "INSTRUMENT_UNAVAILABLE reason=invalid: energy${i} has less than ${hs} s of headroom before the counter wraps at 600 W (spec §17)"; return 5 137 fi 138 # and enough for the whole planned run, so the counter cannot wrap between trials 139 if [ $((`4294967295` * 1000 - v)) -lt $((run_s * run_w * `1000000`)) ]; then 140 echo "INSTRUMENT_UNAVAILABLE reason=invalid: energy${i} has less headroom than the planned run (${run_s} s at ${run_w} W) before the counter wraps"; return 5 141 fi 142 done 143 [ "$s" -lt 3 ] && sleep "$nap" 144 done 145 if [ "$prev" -le "$first" ]; then 146 echo "INSTRUMENT_UNAVAILABLE reason=invalid: package counter did not advance over $n samples ($first uJ)" 147 return 5 148 fi 149 echo "energy preflight ok: $h pkg $first -> $prev uJ over $n samples, 5 energy + 5 power labels verified" 150 return 0 151 } 152 if [ "${1:-}" = energy-preflight ]; then energy_preflight; exit $?; fi 153 154 # xid-scan : NVRM Xid lines from the kernel log in 155 # that span. Window 2 of CAND-1 had a GPU fault (Xid 109, CTX SWITCH TIMEOUT, 156 # 08:08:16 CDT) that the runner's xid-count.txt reported as 0, because it used 157 # `dmesg`, which this account may not read; the failed read was hidden and 158 # counted as "none". journalctl -k is readable here. A failed read is reported 159 # as XID_LOG_UNREADABLE (exit 4), never as zero. Observability only. 160 # journalctl answers "-- No entries --" with exit 0 to an account without journal 161 # access, so a readable-looking empty window is not proof of no Xid: the kernel 162 # log of this boot must itself read non-empty first (it never is, on a real boot). 163 # R15_KLOG_FILE= reads a saved log instead of journalctl (tests). 164 # R15_KLOG_PROBE_FILE= stands in for that boot-log probe (tests). 165 xid_scan() { 166 local out rc 167 if [ -z "${R15_KLOG_FILE:-}" ] || [ -n "${R15_KLOG_PROBE_FILE+x}" ]; then 168 local probe 169 if [ -n "${R15_KLOG_PROBE_FILE+x}" ]; then probe=$(cat "$R15_KLOG_PROBE_FILE" 2>/dev/null) 170 else probe=$(journalctl -k -b --no-pager -q -n 1 -o cat 2>/dev/null); fi 171 if [ -z "$probe" ]; then 172 echo "XID_LOG_UNREADABLE: the kernel log of this boot reads empty (no journal access?)"; return 4 173 fi 174 fi 175 if [ -n "${R15_KLOG_FILE:-}" ]; then out=$(cat "$R15_KLOG_FILE" 2>&1); rc=$? 176 else out=$(journalctl -k --no-pager -o short-iso --since "@$1" --until "@$2" 2>&1); rc=$?; fi 177 if [ $rc -ne 0 ]; then echo "XID_LOG_UNREADABLE: $(printf '%s' "$out" | head -1)"; return 4; fi 178 printf '%s\n' "$out" | grep -i 'NVRM: Xid' 179 echo "xid lines: $(printf '%s\n' "$out" | grep -ci 'NVRM: Xid')" 180 return 0 181 } 182 if [ "${1:-}" = xid-scan ]; then xid_scan "${2:?since epoch}" "${3:?until epoch}"; exit $?; fi 183 184 CMD=${1:?preflight, start, mark or stop} 185 OUT=${2:?out dir} 186 mkdir -p "$OUT" 187 X925=7 # the preflight core (a Cortex-X925) 188 SAMPLER_CPU=${R15_STATE_CPU:-0} 189 190 spbm_energy() { 191 for h in /sys/class/hwmon/hwmon*; do 192 case "$(cat "$h/name" 2>/dev/null)" in aien_spbm*) cat "$h/energy1_input" 2>/dev/null; return;; esac 193 done 194 echo -1 195 } 196 197 # busy jiffies per cpu, "cpuN busy" lines 198 busy() { awk '/^cpu[0-9]/{b=$2+$3+$4+$7+$8; print $1, b}' /proc/stat; } 199 200 case "$CMD" in 201 preflight) 202 # 20 s of sustained load on one X925 core while perf counts its cycles. 203 F=$OUT/preflight.txt 204 { echo "== preflight $(date -u +%FT%TZ): 20 s sustained on cpu $X925"; busy; } > "$F" 205 sudo -n perf stat -a -A -C "$X925" -e cycles -I 1000 -x, -o "$OUT/preflight-perf.csv" -- \ 206 taskset -c "$X925" timeout 20 sha256sum /dev/zero >/dev/null 2>&1 207 busy >> "$F" 208 preflight_ghz "$OUT/preflight-perf.csv" >> "$F" 209 rc=$? 210 [ $rc = 0 ] && echo "result: X925 sustains its clock" >> "$F" || echo "result: MACHINE-STATE FAILURE (rc $rc)" >> "$F" 211 exit $rc 212 ;; 213 start) 214 date -u +%FT%TZ > "$OUT/machine-state-start.txt" 215 date +%s > "$OUT/.start.epoch" 216 for c in /sys/devices/system/cpu/cpu[0-9]*; do 217 echo "${c##*/} $(cat "$c/cpufreq/scaling_governor" 2>/dev/null) max=$(cat "$c/cpufreq/cpuinfo_max_freq" 2>/dev/null)" 218 done >> "$OUT/machine-state-start.txt" 219 # perf's workload ends by itself when stop drops .perf.stop (cap 24 h). A 220 # bare `sleep 86400` outlived pkill of perf (root, reparented to init) and 221 # kept the caller's quietlock hold alive for a day (2026-10-05 13:35Z). 222 rm -f "$OUT/.perf.stop" 223 sudo -n perf stat -a -A -e cycles -I 1000 -x, -o "$OUT/machine-perf.csv" -- \ 224 sh -c 'i=0; while [ ! -e "$1/.perf.stop" ] && [ $i -lt 86400 ]; do sleep 1; i=$((i + 1)); done' \ 225 r15-sampler "$OUT" >/dev/null 2>&1 & 226 echo $! > "$OUT/.perf.pid" 227 ( 228 n=0 229 while :; do 230 t=$(date +%s.%N) 231 b=$(busy | awk '{printf "%s:%s ", $1, $2}') 232 th=$(cat /sys/class/thermal/thermal_zone*/temp 2>/dev/null | tr '\n' ' ') 233 g="" 234 if [ $((n % 5)) = 0 ]; then 235 g=$(nvidia-smi --query-gpu=clocks.sm,power.draw,utilization.gpu,temperature.gpu,clocks_event_reasons.active --format=csv,noheader,nounits 2>/dev/null | tr -d ' ') 236 fi 237 printf '{"t":%s,"spbm_uj":%s,"loadavg":"%s","busy":"%s","thermal_mc":"%s","gpu":"%s"}\n' \ 238 "$t" "$(spbm_energy)" "$(cut -d' ' -f1-3 /proc/loadavg)" "$b" "$th" "$g" 239 n=$((n + 1)) 240 sleep 1 241 done 242 ) >> "$OUT/machine-state.ndjson" 2>/dev/null & 243 echo $! > "$OUT/.sampler.pid" 244 taskset -pc "$SAMPLER_CPU" "$(cat "$OUT/.sampler.pid")" >/dev/null 2>&1 245 ;; 246 mark) 247 echo "$(date +%s.%N) ${3:-}" >> "$OUT/machine-state-marks.txt" 248 ;; 249 stop) 250 # Let perf's workload end on its own so perf exits and flushes its last 251 # interval; the pkill below is only the fallback. 252 touch "$OUT/.perf.stop" 253 if [ -f "$OUT/.perf.pid" ]; then 254 w=0; while [ -d "/proc/$(cat "$OUT/.perf.pid")" ] && [ $w -lt 5 ]; do sleep 1; w=$((w + 1)); done 255 fi 256 for p in .sampler.pid .perf.pid; do 257 [ -f "$OUT/$p" ] || continue 258 pid=$(cat "$OUT/$p") 259 pkill -P "$pid" 2>/dev/null 260 kill "$pid" 2>/dev/null 261 sudo -n pkill -f "perf stat -a -A -e cycles -I 1000 -x, -o $OUT/machine-perf.csv" 2>/dev/null 262 rm -f "$OUT/$p" 263 done 264 # Kernel GPU faults (Xid) during the run, from the readable kernel log. 265 # The scan's own verdict is kept: an unreadable log or a missing start time is 266 # written into kernel-xid.txt and warned about, never left as an empty file. 267 if [ -f "$OUT/.start.epoch" ]; then 268 xid_scan "$(cat "$OUT/.start.epoch")" "$(date +%s)" > "$OUT/kernel-xid.txt" 2>&1; xrc=$? 269 else 270 echo "XID_SCAN_NOT_RUN: $OUT/.start.epoch is missing" > "$OUT/kernel-xid.txt"; xrc=5 271 fi 272 echo "xid-scan exit $xrc" >> "$OUT/kernel-xid.txt" 273 [ $xrc = 0 ] || echo "r15_machine_state: warning: kernel Xid scan did not complete (exit $xrc), see $OUT/kernel-xid.txt" >&2 274 # Summary (informative; the raw files are authoritative): per-second 275 # cycles of each X925 core (cpus 5-9, 15-19) as GHz. A fully busy second 276 # reads as the effective clock; a partly idle second reads lower, so the 277 # busy jiffies in machine-state.ndjson decide individual seconds. 278 awk -F, '$5 == "cycles" || $5 ~ /^armv8_pmuv3(_[0-9]+)?\/cycles\/$/ { split($2, c, "CPU"); cpu = c[2] + 0 279 if ((cpu >= 5 && cpu <= 9) || (cpu >= 15 && cpu <= 19)) { g = $3 / 1e9; if (g >= 1.0) v[++n] = g } } 280 END { 281 if (!n) { print "X925: no second above 1 GHz of cycles"; exit } 282 asort(v) 283 printf "X925 seconds with >= 1 G cycles: %d; p10 %.3f, median %.3f, max %.3f GHz\n", n, v[int(n * 0.1) + 1], v[int(n / 2) + 1], v[n] 284 }' "$OUT/machine-perf.csv" > "$OUT/machine-state-summary.txt" 2>/dev/null 285 awk '{ if (match($0, /"t":[0-9.]+/)) t = substr($0, RSTART + 4, RLENGTH - 4) 286 if (match($0, /"spbm_uj":-?[0-9]+/)) e = substr($0, RSTART + 10, RLENGTH - 10) + 0 287 if (e >= 0) { if (t0 == "") { t0 = t; e0 = e } t1 = t; e1 = e } } 288 END { if (t0 != "" && t1 > t0) printf "package energy %.1f J over %.0f s, mean %.2f W\n", (e1 - e0) / 1e6, t1 - t0, (e1 - e0) / 1e6 / (t1 - t0) 289 else print "package energy: SPBM reader not loaded" }' "$OUT/machine-state.ndjson" >> "$OUT/machine-state-summary.txt" 290 ;; 291 *) echo "unknown command $CMD" >&2; exit 64;; 292 esac