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 / tools / ty_energy_run.sh

163 lines · 6382 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# TY-4/TY-5 timed run driver (docs/turing/TURING_YIELD_ENERGY_PROTOCOL_V0.md).
18#
19# ty_energy_run.sh all OUTDIR BINDIR stage 1 sweep, selection, stage 2
20# ty_energy_run.sh fixture OUTDIR BINDIR one short round (3 s windows) for test fixtures
21#
22# The rounds, the idle wait and the pause between windows are here, in shell,
23# on purpose: the C tools hold no timed loop (R16 loop inventory scope is C and
24# Rust). Waits for an idle machine, then creates its OWN quiet flag, and removes
25# it only if it is still its own. Never kills anything. Run it detached.
26set -u
27case "${1:-}" in all|fixture) MODE=$1; OUT=$2; BIN=$3 ;; *) echo "usage: $0 all|fixture OUTDIR BINDIR" >&2; exit 64 ;; esac
28# Serialize with the existing Spark hardware/qualification owner before any
29# timed window. No other job is interrupted and no new lock namespace is used.
30exec 9> "$HOME/workspace/.argus-bench.lock"
31flock 9 || exit 1
32QUIET=$HOME/workspace/.spark-quiet
33TAG="ty45-energy pid $$"
34WL=$BIN/ty_workload
35WIN=$BIN/ty_energy_window
36RED=$BIN/ty_energy_reduce
37S1_SEED=0x7459510001
38S2_SEED=0x7459520001
39
40log() { echo "$(date -u +%FT%TZ) $*"; }
41
42# Config table (protocol section 5): id A-spec B-spec
43cfg_line() {
44 case "$1" in
45 S1) echo "R1_plain:5 R2c_crumb:6" ;;
46 S2) echo "R1_plain:5 R2c_crumb:15" ;;
47 S3) echo "R1_plain:5,6 R2c_crumb:7,8" ;;
48 S4) echo "R1_plain:5,6 R2c_crumb:15,16" ;;
49 S5) echo "R1_plain:5,6,7 R2c_crumb:8,9" ;;
50 S6) echo "R1_plain:5,6,7,8,9 R2c_crumb:15,16,17,18,19" ;;
51 *) return 1 ;;
52 esac
53}
54
55busy_reason() {
56 l1=$(cut -d' ' -f1 /proc/loadavg)
57 if awk -v l="$l1" 'BEGIN{exit !(l >= 2)}'; then echo "load1=$l1"; return; fi
58 if [ -e "$QUIET" ] && ! grep -q "$TAG" "$QUIET" 2>/dev/null; then echo "quiet flag held: $(head -c 120 "$QUIET")"; return; fi
59 p=$(pgrep -a -x make 2>/dev/null | head -1)
60 [ -n "$p" ] && { echo "make running: $p"; return; }
61 p=$(pgrep -a -f 'bench_|_bench|qualify|rx_r1[0-9]_' 2>/dev/null | grep -v "ty_energy\|ty_workload\|pgrep" | head -1)
62 [ -n "$p" ] && { echo "bench running: $p"; return; }
63 echo ""
64}
65
66wait_idle() {
67 n=0
68 while :; do
69 why=$(busy_reason)
70 [ -z "$why" ] && return 0
71 [ $((n % 15)) -eq 0 ] && log "waiting for idle: $why"
72 n=$((n + 1))
73 sleep 20
74 done
75}
76
77take_flag() {
78 wait_idle
79 (set -C; printf '%s since %s: TY-5 timed energy windows, please hold heavy work\n' "$TAG" "$(date -u +%FT%TZ)" > "$QUIET") 2>/dev/null \
80 || { log "quiet flag appeared while taking it; waiting again"; take_flag; return; }
81 log "quiet flag taken"
82 sleep 10 # let anything that just checked the flag finish its current step
83}
84
85drop_flag() {
86 if [ -e "$QUIET" ] && grep -q "$TAG" "$QUIET" 2>/dev/null; then rm -f "$QUIET"; log "quiet flag removed"; fi
87}
88trap drop_flag EXIT
89trap 'drop_flag; exit 130' INT TERM
90
91manifest() {
92 {
93 echo "date_utc $(date -u +%FT%TZ)"
94 echo "git_head $(git rev-parse HEAD 2>/dev/null)"
95 echo "git_dirty $(git status --porcelain -- src tests tools mk 2>/dev/null | grep -c .)"
96 for b in "$WL" "$WIN" "$RED"; do echo "bin $(sha256sum "$b")"; done
97 echo "kernel $(uname -r)"
98 echo "stage1_seed $S1_SEED (config order: --shuffle seed; round order: --plan seed+k, k = position of the config in S1..S6)"
99 echo "stage2_seed $S2_SEED (contention case --plan seed, control case --plan seed+1)"
100 } > "$OUT/manifest.txt"
101}
102
103# run_config STAGEDIR STAGE CFG SEED ROUNDS SECS
104run_config() {
105 dir=$1; st=$2; cfg=$3; seed=$4; rounds=$5; secs=$6
106 set -- $(cfg_line "$cfg")
107 sa=$1; sb=$2
108 "$WIN" --plan "$seed" "$rounds" > "$dir/plan_$cfg.txt"
109 while read -r r p c; do
110 f=$(printf '%s/%s_%s_r%02d_p%d_%s.jsonl' "$dir" "$st" "$cfg" "$r" "$p" "$c")
111 "$WIN" --out "$f" --run "$(basename "$OUT")-$st" --config "$cfg" --cond "$c" --round "$r" --pos "$p" \
112 --secs "$secs" --workload "$WL" --a "$sa" --b "$sb" --tool-cpu 0 \
113 || log "window $f returned nonzero (kept; the reducer decides)"
114 grep -q "$TAG" "$QUIET" 2>/dev/null || log "WARNING: our quiet flag is gone during $f"
115 sleep 3
116 done < "$dir/plan_$cfg.txt"
117}
118
119sums() { (cd "$1" && sha256sum *.jsonl > SHA256SUMS); }
120
121mkdir -p "$OUT"
122manifest
123if [ "$MODE" = fixture ]; then
124 mkdir -p "$OUT/raw"
125 take_flag
126 run_config "$OUT/raw" fx S1 "$S1_SEED" 1 3
127 sums "$OUT/raw"
128 exit 0
129fi
130
131# ---- stage 1 ----
132mkdir -p "$OUT/raw/stage1"
133take_flag
134log "stage 1 start"
135"$WIN" --shuffle "$S1_SEED" 6 > "$OUT/raw/stage1/config_order.txt"
136while read -r k; do
137 cfg="S$((k + 1))"
138 log "stage 1 config $cfg"
139 run_config "$OUT/raw/stage1" s1 "$cfg" $((S1_SEED + k)) 3 10
140done < "$OUT/raw/stage1/config_order.txt"
141sums "$OUT/raw/stage1"
142drop_flag
143"$RED" --stage1 "$OUT/raw/stage1" "$OUT/stage1_summary.json" > "$OUT/stage1_report.txt" 2>&1
144sel=$(grep '^SELECT' "$OUT/stage1_report.txt")
145log "stage 1 done: $sel"
146cont=$(echo "$sel" | sed -n 's/.*contention=\([^ ]*\).*/\1/p')
147ctrl=$(echo "$sel" | sed -n 's/.*control=\([^ ]*\).*/\1/p')
148cfg_line "$cont" > /dev/null || { log "no contention config selected; stop"; exit 1; }
149
150# ---- stage 2 ----
151mkdir -p "$OUT/raw/stage2"
152take_flag
153log "stage 2 start: contention=$cont control=$ctrl"
154run_config "$OUT/raw/stage2" s2 "$cont" "$S2_SEED" 10 30
155if [ -n "$ctrl" ] && [ "$ctrl" != "$cont" ] && cfg_line "$ctrl" > /dev/null; then
156 run_config "$OUT/raw/stage2" s2 "$ctrl" $((S2_SEED + 1)) 10 30
157fi
158sums "$OUT/raw/stage2"
159drop_flag
160"$RED" --stage2 "$OUT/raw/stage2" "$OUT/stage2_summary.json" "$OUT/records.jsonl" > "$OUT/stage2_report.txt" 2>&1
161log "stage 2 done"
162grep '^CHECK\|^I \|^FIT' "$OUT/stage2_report.txt"
163log "finished"