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 / r15_energy_preflight_test.sh

101 lines · 7768 bytes Raw Blame History
1#!/bin/sh
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 energy-instrument preflight (tools/r15_machine_state.sh energy-preflight).
18# Spec ยง7: if package energy cannot be read by the physical SPBM source, R15
19# cannot PASS. The preflight must refuse EARLY with INSTRUMENT_UNAVAILABLE
20# (exit 5) whenever the reader would not satisfy r15_energy_open/r15_energy_read
21# (tests/runtime/r15_measure.c): all 5 energy + 5 power labels exact, every
22# energyN_input / energyN_overflow_raw / powerN_input readable, overflow 0,
23# package energy advancing, counter headroom. The preflight reads its label
24# tables from r15_measure.c, and this test builds its fixtures from the same
25# tables, so the two cannot drift. Fixtures are fake hwmon trees in a temp dir.
26cd "$(dirname "$0")/.." || exit 1
27T=tools/r15_machine_state.sh; fail=0; C=tests/runtime/r15_measure.c
28W=$(mktemp -d) || exit 1; trap 'rm -rf "$W"' EXIT
29export R15_ENERGY_PREFLIGHT_SLEEP=0
30# fixture uptime: 100 s, so a 10 W floor is 1 kJ (1,000,000,000 uJ); fixtures hold 5 kJ unless a case says otherwise
31printf "100.00 90.00\n" > "$W/uptime"; export R15_UPTIME_FILE="$W/uptime"
32EL=$(sed -n 's/^static const char \*const k_elabel\[[A-Z_0-9]*\] = {\(.*\)};/\1/p' $C | tr -d '" ' | tr , ' ')
33PL=$(sed -n 's/^static const char \*const k_plabel\[[A-Z_0-9]*\] = {\(.*\)};/\1/p' $C | tr -d '" ' | tr , ' ')
34[ "$(echo $EL | wc -w)" = 5 ] && [ "$(echo $PL | wc -w)" = 5 ] || { echo "[-] cannot read label tables from $C"; exit 1; }
35check() { # name expected-exit expected-text
36 out=$(R15_HWMON_ROOT="$W/$1" "$T" energy-preflight 2>&1); rc=$?
37 if [ "$rc" = "$2" ] && printf '%s\n' "$out" | grep -Fq -- "$3"; then echo "[+] $1: exit $rc"
38 else echo "[-] $1: exit $rc (want $2), output: $out"; fail=$((fail + 1)); fi
39}
40mk() { # case: a complete, valid-looking reader tree (static counters)
41 H=$W/$1/hwmon1; mkdir -p "$W/$1/hwmon0" "$H"; echo acpitz > "$W/$1/hwmon0/name"; echo aien_spbm > "$H/name"
42 i=1; for l in $EL; do echo "$l" > "$H/energy${i}_label"; echo 5000000000 > "$H/energy${i}_input"; echo 0 > "$H/energy${i}_overflow_raw"; i=$((i + 1)); done
43 i=1; for l in $PL; do echo "$l" > "$H/power${i}_label"; echo 20000000 > "$H/power${i}_input"; i=$((i + 1)); done
44}
45# tick <case> <channel> <start-uJ> <step-uJ>: a background writer moves hwmon1/energy<channel>_input by <step>
46# every 10 ms from <start> until untick <case>, so the counter keeps moving through the preflight's whole
47# sampling window however late the preflight starts. (A writer on a fixed schedule could finish before the
48# first sample under load; all three samples then saw one value and the valid case reported 'did not advance'.)
49put() { echo "$2" > "$1.tmp" && mv "$1.tmp" "$1"; } # atomic, so the preflight never reads a half-written file
50tick() { ( v=$3; while [ ! -e "$W/$1.untick" ]; do v=$((v + $4)); put "$W/$1/hwmon1/energy$2_input" $v; sleep 0.01; done ) & }
51untick() { : > "$W/$1.untick"; wait; }
52advance() { tick "$1" 1 "$2" 500000; } # advance <case> <start-uJ>: package counter rises 0.5 J per tick
53mkdir -p "$W/absent/hwmon0"; echo acpitz > "$W/absent/hwmon0/name"; check absent 5 "reason=absent"
54mkdir -p "$W/empty"; check empty 5 "reason=absent"
55mk substitute; echo nvml > "$W/substitute/hwmon1/name"; check substitute 5 "reason=absent"
56mk zero; echo 0 > "$W/zero/hwmon1/energy1_input"; check zero 5 "reads 0 uJ"
57mk unreadable; rm "$W/unreadable/hwmon1/energy1_input"; check unreadable 5 "energy1_input"
58mk garbage; echo -22 > "$W/garbage/hwmon1/energy1_input"; check garbage 5 "not a number"
59mk stuck; check stuck 5 "did not advance"
60mk wrongelabel; echo gpm > "$W/wrongelabel/hwmon1/energy1_label"; check wrongelabel 5 "energy1_label"
61mk wrongelabel4; echo x > "$W/wrongelabel4/hwmon1/energy4_label"; check wrongelabel4 5 "energy4_label"
62mk wrongplabel; echo x > "$W/wrongplabel/hwmon1/power3_label"; check wrongplabel 5 "power3_label"
63mk missingplabel; rm "$W/missingplabel/hwmon1/power5_label"; check missingplabel 5 "power5_label"
64mk noovf; rm "$W/noovf/hwmon1/energy2_overflow_raw"; check noovf 5 "energy2_overflow_raw unreadable"
65mk ovf1; echo 1 > "$W/ovf1/hwmon1/energy1_overflow_raw"; check ovf1 5 "energy1_overflow_raw is '1'"
66mk ovf5; echo 1 > "$W/ovf5/hwmon1/energy5_overflow_raw"; check ovf5 5 "energy5_overflow_raw is '1'"
67mk nopower; rm "$W/nopower/hwmon1/power2_input"; check nopower 5 "power2_input"
68mk badpower; echo abc > "$W/badpower/hwmon1/power4_input"; check badpower 5 "power4_input"
69mk noenergy3; rm "$W/noenergy3/hwmon1/energy3_input"; check noenergy3 5 "energy3_input"
70mk lowhead; echo 4294967000000000 > "$W/lowhead/hwmon1/energy3_input"; check lowhead 5 "headroom"
71# whole-run headroom: 200 kJ left passes the old 300 s x 600 W (180 kJ) window check but is
72# less than the planned run (2400 s x 100 W = 240 kJ), so a wrap could land mid-run
73mk shortrun; echo 4094967295000 > "$W/shortrun/hwmon1/energy1_input"
74advance shortrun 4094967295000
75R15_ENERGY_PREFLIGHT_SLEEP=0.08 check shortrun 5 "planned run"; untick shortrun
76# wrapped / reset counter: uptime 200000 s but only 1 kJ on the counter (floor 10 W x 200000 s = 2 MJ)
77mk wrapped; echo 1000000000 > "$W/wrapped/hwmon1/energy1_input"; printf "200000.00 1.00\n" > "$W/wrapped-uptime"
78advance wrapped 1000000000
79R15_UPTIME_FILE="$W/wrapped-uptime" R15_ENERGY_PREFLIGHT_SLEEP=0.08 check wrapped 5 "wrapped or was reset"; untick wrapped
80mk nouptime; advance nouptime 5000000000; R15_UPTIME_FILE="$W/does-not-exist" R15_ENERGY_PREFLIGHT_SLEEP=0.08 check nouptime 5 "uptime"; untick nouptime
81# a decrease on any channel (not only package) invalidates the reader
82mk decr3; echo 5000000000 > "$W/decr3/hwmon1/energy1_input"
83tick decr3 3 5000000000 -1000 # steadily falling cpu_p counter
84advance decr3 5000000000
85R15_ENERGY_PREFLIGHT_SLEEP=0.08 check decr3 5 "energy3 counter went backwards"
86untick decr3
87# valid: package counter advances between samples
88mk valid
89advance valid 5000100000
90R15_ENERGY_PREFLIGHT_SLEEP=0.08 check valid 0 "energy preflight ok"; untick valid
91# blocked run: receipt refuses (exit 4), writes nothing
92R=$W/blocked; mkdir -p "$R"; printf 'BLOCKED_INSTRUMENT\nINSTRUMENT_UNAVAILABLE reason=absent\n' > "$R/instrument-unavailable.txt"
93out=$(tools/r15_receipt.sh "$R" "$W/receipt-out" 2>&1); rc=$?
94if [ "$rc" = 4 ] && printf '%s' "$out" | grep -Fq BLOCKED_INSTRUMENT && [ -z "$(ls "$W/receipt-out" 2>/dev/null)" ]; then
95 echo "[+] receipt refuses blocked run: exit $rc, nothing written"
96else echo "[-] receipt on blocked run: exit $rc (want 4): $out"; fail=$((fail + 1)); fi
97# the qualify path seals a blocked directory with SHA256SUMS (same command as r15_qualify.sh)
98( cd "$R" && find . -type f ! -name SHA256SUMS -printf "%P\n" | LC_ALL=C sort | xargs sha256sum > SHA256SUMS )
99if ( cd "$R" && sha256sum -c SHA256SUMS >/dev/null 2>&1 ) && grep -q instrument-unavailable.txt "$R/SHA256SUMS"; then echo "[+] blocked dir sealed"; else echo "[-] seal"; fail=$((fail + 1)); fi
100grep -q 'printf "%P\\n" | LC_ALL=C sort | xargs sha256sum > SHA256SUMS' tools/r15_qualify.sh && echo "[+] qualify.sh uses the same seal command" || { echo "[-] qualify.sh seal missing"; fail=$((fail + 1)); }
101echo "failures $fail"; [ "$fail" = 0 ]