interplane.Get Omega
Read in your language

aien-architecture-mv

Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories.

Brought by drakestapleton

branch main Branches Tags History

aien-architecture-mv / tools / ops / quiet-measure

127 lines · 7334 bytes Raw Blame History
1#!/bin/sh
2# 2026-10-20: commands are pinned to the X925 cores; old copy quiet-measure.bak-20261020.
3# quiet-measure CMD... Run a timing measurement with the box as quiet as possible.
4# L217: if CMD starts with "flock LOCKFILE ...", take LOCKFILE first while holding nothing,
5# then raise the want flag, pass the gate, and take /tmp/aien-quiet.lock exclusive.
6# Waiting for the GPU must not freeze every build. Any other CMD still raises the want
7# flag first, passes the gate /tmp/aien-quiet.gate (held by a window at the front of the
8# line; fail open after 25 min; L322), then takes the quiet lock exclusive.
9# Plain flock alone starves measurements: new shared holders keep arriving.
10# Inside a measurement window (SPARK_WINDOW set by spark-window run, and that
11# window is the open one with a live holder) the lock is already held
12# exclusive for us, so CMD runs directly instead of waiting on our own lock.
13# L405: a measurement holding the lock past 25 min no longer stops builds; they run beside it and it warns.
14# Fast cores for timing (Cortex-X925: 5-9,15-19); builds live on the A725 cores. Builds running at start are logged.
15case "${1-}" in
16 ""|-h|--help|help)
17 sed -n '2,/^[^#]/{/^#/s/^# \{0,1\}//p}' "$0"
18 exit 0 ;;
19 -*)
20 echo "$(basename "$0"): unknown option '$1' (the first argument must be the command to run; try --help)" >&2
21 exit 2 ;;
22esac
23L=$HOME/.local/state/quiet; mkdir -p "$L"
24printf '%s builds_running=%s %s\n' "$(date -u +%FT%TZ)" "$(ls /tmp/aien-build.active.d 2>/dev/null | wc -l)" "$*" >> "$L/measure-overlap.log"
25PIN="taskset -c 5-9,15-19"
26if [ -n "${SPARK_WINDOW:-}" ]; then
27 WR=${SPARK_WINDOWS_ROOT:-$HOME/yardsticks/windows}
28 if [ "$(cat "$WR/OPEN" 2>/dev/null)" = "$SPARK_WINDOW" ] && [ -f "$WR/$SPARK_WINDOW/held" ] &&
29 kill -0 "$(cat "$WR/$SPARK_WINDOW/holder.pid" 2>/dev/null)" 2>/dev/null; then
30 exec $PIN "$@"
31 fi
32 echo "quiet-measure: window $SPARK_WINDOW is not open and held; taking the quiet lock the normal way" >&2
33fi
34W=${SPARK_QUIET_WANT:-/tmp/aien-quiet.want.d}; Q=${SPARK_QUIET_LOCK:-/tmp/aien-quiet.lock}; GATE=${SPARK_QUIET_GATE:-/tmp/aien-quiet.gate}
35mkdir -p "$W" 2>/dev/null; chmod 1777 "$W" 2>/dev/null
36# L217: chip first, quiet lock second. GPU timings are run as "quiet-measure flock /tmp/aien-gb10.lock CMD".
37# The old order took the quiet lock EXCLUSIVE and then queued for the chip inside it, so every build on the
38# box waited behind a measurement that was itself only waiting for the GPU (2026-10-20: 65+ min, 26 builds;
39# again 21:20Z and 21:30Z). For CMD = flock [-x|-e|-s|-n|-o|-w SECS|--] LOCKFILE CMD... we now take LOCKFILE
40# first while holding nothing else (no want flag, no quiet lock), then raise the flag, pass the gate and drain
41# the quiet lock with the chip in hand: the same order as spark-window. Two locks must never wait on each
42# other, so while draining we let the chip go and start over when a process queued on the chip has the quiet
43# lock open (an old-order measurement, a build whose CMD takes the chip) or when the drain passes
44# SPARK_DRAIN_MAX s (default 300, as spark-window). With the chip in hand the gate is also waited for at most
45# SPARK_DRAIN_MAX s, then passed (fail open). LOCKFILE stays held while CMD runs (fd 9, passed to CMD
46# like flock(1) does; with -o CMD does not get it, the lock is still held for CMD's life). Other flock forms
47# (-c, combined flags, -E, a directory) and LOCKFILE = the quiet lock itself keep the old path below.
48rl_parse() {
49 [ "${1-}" = flock ] || return 1
50 shift; RN=1; rmode=-x; rnb=; rwait=; rclose=
51 while [ $# -gt 0 ]; do
52 case $1 in
53 -x|-e|--exclusive) rmode=-x ;;
54 -s|--shared) rmode=-s ;;
55 -n|--nb|--nonblock) rnb=1 ;;
56 -o|--close) rclose=1 ;;
57 -w|--wait|--timeout) [ $# -ge 2 ] || return 1
58 case $2 in ''|*[!0-9.]*) return 1 ;; esac
59 rwait=$2; shift; RN=$((RN + 1)) ;;
60 --) shift; RN=$((RN + 1)); break ;;
61 -*) return 1 ;;
62 *) break ;;
63 esac
64 shift; RN=$((RN + 1))
65 done
66 [ $# -ge 2 ] || return 1
67 case $2 in -c|--command) return 1 ;; esac
68 [ -d "$1" ] && return 1
69 RL=$1; RN=$((RN + 1))
70 [ -e "$RL" ] || : >> "$RL" 2>/dev/null || return 1
71 [ -r "$RL" ] || return 1
72 [ "$RL" -ef "$Q" ] && return 1
73 return 0
74}
75# chip_waiter_holds_quiet: some process queued on LOCKFILE has the quiet lock file open (spark-window's guard)
76chip_waiter_holds_quiet() {
77 cino=$(stat -c %i "$RL" 2>/dev/null) || return 1
78 qpath=$(readlink -f "$Q" 2>/dev/null) || return 1
79 for p in $(awk -v i="$cino" '$2=="->" { split($7, a, ":"); if (a[3] == i) print $6 }' /proc/locks 2>/dev/null); do
80 for fd in /proc/$p/fd/*; do [ "$(readlink "$fd" 2>/dev/null)" = "$qpath" ] && return 0; done
81 done
82 return 1
83}
84if rl_parse "$@"; then
85 all="$*"; shift "$RN"
86 # a TERM or INT ends us: a measurement killed while queued for the chip must not run CMD once it gets it
87 f="$W/$$"; trap 'rm -f "$f"' EXIT; trap 'exit 130' INT; trap 'exit 143' TERM
88 [ -e "$GATE" ] || { : > "$GATE"; chmod 666 "$GATE"; } 2>/dev/null
89 [ -e "$Q" ] || { : > "$Q"; chmod 666 "$Q"; } 2>/dev/null
90 rf=$rmode; [ -n "$rnb" ] && rf="$rf -n"; [ -n "$rwait" ] && rf="$rf -w $rwait"
91 exec 9<"$RL"
92 DRAIN=${SPARK_DRAIN_MAX:-300}; tries=0
93 while :; do
94 tries=$((tries + 1))
95 # 1. LOCKFILE, holding nothing else: builds keep running while we queue for the chip
96 flock $rf 9 || { echo "quiet-measure: could not take $RL (flock $rf)" >&2; exit 1; }
97 # 2. only now say a measurement is waiting, and pass the gate: with the chip in hand at most DRAIN s, then fail open
98 echo "$(date -u +%FT%TZ) $all" > "$f"
99 flock -s -w "$DRAIN" "$GATE" true 2>/dev/null
100 # 3. drain the quiet lock with the chip in hand, bounded, with the deadlock guard
101 exec 8<"$Q"; ds=$(date +%s); got=0; why="did not drain in ${DRAIN}s"
102 while [ $(( $(date +%s) - ds )) -lt "$DRAIN" ]; do
103 flock -x -w 5 8 && { got=1; break; }
104 if chip_waiter_holds_quiet; then why="a process queued on $RL holds the quiet lock open"; break; fi
105 done
106 [ "$got" = 1 ] && break
107 exec 8<&-; rm -f "$f"; flock -u 9
108 printf '%s pid=%s try=%s let %s go: %s | %s\n' "$(date -u +%FT%TZ)" "$$" "$tries" "$RL" "$why" "$all" | cut -c1-300 >> "$L/measure-yields.log" 2>/dev/null
109 echo "quiet-measure: let $RL go and queued again: the quiet lock $why (try $tries)" >&2
110 [ -n "$rnb" ] && { echo "quiet-measure: -n given, not retrying" >&2; exit 1; }
111 sleep 1
112 done
113 tsf="$W/.$$.held"; date -u +%FT%TZ > "$tsf"
114 if [ -n "$rclose" ]; then $PIN "$@" 9<&-; else $PIN "$@"; fi; rc=$?
115else
116 f="$W/$$"; echo "$(date -u +%FT%TZ) $*" > "$f"
117 trap 'rm -f "$f"' EXIT INT TERM
118 [ -e "$GATE" ] || { : > "$GATE"; chmod 666 "$GATE"; } 2>/dev/null
119 flock -s -w "${SPARK_GATE_WAIT:-1500}" "$GATE" true 2>/dev/null # fail open
120 tsf="$W/.$$.held"
121 flock -x "$Q" sh -c 'date -u +%FT%TZ > "$0"; exec "$@"' "$tsf" $PIN "$@"; rc=$?
122fi
123# L405: correctness jobs stop waiting after their cap (25 min) and run beside us; say so, so the numbers are labelled.
124ts=$(cat "$tsf" 2>/dev/null); rm -f "$tsf"
125n=$( [ -n "$ts" ] && awk -v t="$ts" '$1 >= t' "$L/intrusions.log" 2>/dev/null | wc -l || echo 0)
126[ "${n:-0}" -gt 0 ] && echo "quiet-measure: $n correctness job(s) ran beside this measurement after waiting their cap; label its numbers 'shared machine' (L405)" >&2
127exit $rc