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-lock-order / suite.sh

125 lines · 9164 bytes Raw Blame History
1#!/bin/bash
2# L217 model suite: suite.sh VARIANT_DIR (VARIANT_DIR holds quiet-measure + quiet-build to test)
3# Every lock, flag, gate, window dir and log is a model path under $ROOT/m-*; the live /tmp locks and
4# ~/.local/state/quiet are never touched (HOME is redirected). The scripts' timing pin (cores 5-9,15-19)
5# is rewritten to 16-18 in the model copies so nothing runs outside this lane's pool cores.
6# Prints one PASS/FAIL line per check; exit status = number of FAILs.
7V=$(cd "$1" && pwd); ROOT=${L217_ROOT:?set L217_ROOT to a scratch directory}; mkdir -p "$ROOT"
8fails=0; pass=0
9ok() { echo "PASS $*"; pass=$((pass + 1)); }
10bad() { echo "FAIL $*"; fails=$((fails + 1)); }
11chk() { if eval "$2"; then ok "$1"; else bad "$1 [$3]"; fi; }
12mk() { # mk NAME: fresh model dir, exports the model environment, sets M B G Q
13 M=$ROOT/m-$1; [ -d "$M" ] && mv "$M" "$ROOT/.old-$1-$$-$RANDOM"
14 mkdir -p "$M/home" "$M/bin" "$M/win" "$M/want.d"
15 for s in quiet-build quiet-measure; do sed 's/taskset -c 5-9,15-19/taskset -c 16-18/' "$V/$s" > "$M/bin/$s"; chmod +x "$M/bin/$s"; done
16 B=$M/bin; G=$M/gb10.lock; Q=$M/quiet.lock; : > "$G"; : > "$Q"; : > "$M/gate"
17 export HOME=$M/home SPARK_QUIET_LOCK=$Q SPARK_QUIET_WANT=$M/want.d SPARK_QUIET_GATE=$M/gate SPARK_WINDOWS_ROOT=$M/win
18 export SPARK_FLAG_WAIT=3 SPARK_BUILD_MAX_WAIT=8 SPARK_DRAIN_MAX=300
19 unset SPARK_WINDOW AIEN_GB10_LOCK_HELD
20}
21ms() { date +%s%3N; }
22can_shared() { flock -n -s "$1" true; } # is the lock free for a shared taker right now?
23can_excl() { flock -n -x "$1" true; }
24
25# A. The L217 bug: a measurement queued for a busy chip must not stop builds.
26mk A
27flock "$G" sleep 20 & H=$!; sleep 0.5 # chip busy longer than the build cap (8 s): the live case is hours
28$B/quiet-measure flock "$G" env AIEN_GB10_LOCK_HELD=1 sh -c 'echo "ran_ms=$(date +%s%3N) held=$AIEN_GB10_LOCK_HELD"' > $M/measure.out 2>$M/measure.err & MP=$!
29sleep 1.5
30can_shared "$Q"; q_free=$? # 0 = builds can take the quiet lock
31nflags=$(ls "$M/want.d" | grep -c '^[0-9]')
32t0=$(ms); $B/quiet-build sh -c 'echo BUILD-RAN' > $M/build.out 2>&1; t1=$(ms)
33el=$(( t1 - t0 ))
34wait $H; hend=$(ms); wait $MP; mrc=$?
35chk "A1 quiet lock free while the measurement queues for the chip" '[ $q_free = 0 ]' "quiet lock busy"
36chk "A2 no want flag raised while queued for the chip" '[ "$nflags" = 0 ]' "flags=$nflags"
37chk "A3 build ran at once (<=2000 ms) behind a measurement queued for the chip" '[ $el -le 2000 ] && grep -q BUILD-RAN $M/build.out' "elapsed_ms=$el"
38chk "A4 build took the lock, no bypass/intrusion" 'grep -q "how=lock" $HOME/.local/state/quiet/waits.log && [ ! -s $HOME/.local/state/quiet/intrusions.log ]' "$(cut -c1-80 $HOME/.local/state/quiet/waits.log 2>/dev/null | tr '\n' ' ')"
39r=$(sed -n 's/.*ran_ms=\([0-9]*\).*/\1/p' $M/measure.out)
40chk "A5 measurement ran after the chip holder left, rc 0, AIEN_GB10_LOCK_HELD=1" '[ "$mrc" = 0 ] && [ -n "$r" ] && [ "$r" -ge $((hend - 500)) ] && grep -q "held=1" $M/measure.out' "rc=$mrc out=$(cat $M/measure.out) err=$(head -c 200 $M/measure.err)"
41echo " A: build elapsed_ms=$el"
42
43# B. No overlap: a build started during a measurement waits until the measurement ends.
44mk B
45$B/quiet-measure flock "$G" sh -c 'echo "M_start $(date +%s%3N)"; sleep 3; echo "M_end $(date +%s%3N)"' > $M/o 2>&1 & MP=$!
46sleep 1
47SPARK_FLAG_WAIT=0 SPARK_BUILD_MAX_WAIT=30 $B/quiet-build sh -c 'echo "B_start $(date +%s%3N)"' >> $M/o 2>&1
48wait $MP
49me=$(awk '$1=="M_end"{print $2}' $M/o); bs=$(awk '$1=="B_start"{print $2}' $M/o)
50chk "B1 build waited for the running measurement (B_start >= M_end)" '[ -n "$me" ] && [ -n "$bs" ] && [ "$bs" -ge "$me" ]' "$(tr '\n' ' ' < $M/o)"
51chk "B2 no bypass" '[ ! -s $HOME/.local/state/quiet/intrusions.log ]' "intrusion logged"
52
53# C. CMD exit code kept; chip AND quiet lock held while CMD runs; both free afterwards.
54mk C
55$B/quiet-measure flock "$G" sh -c 'flock -n -x "$1" true && echo CHIP_FREE_INSIDE; flock -n -s "$2" true && echo QUIET_FREE_INSIDE; exit 7' x "$G" "$Q" > $M/o 2>&1; rc=$?
56chk "C1 CMD exit code preserved (7)" '[ $rc = 7 ]' "rc=$rc"
57chk "C2 chip lock held during CMD" '! grep -q CHIP_FREE_INSIDE $M/o' "$(cat $M/o)"
58chk "C3 quiet lock held exclusive during CMD" '! grep -q QUIET_FREE_INSIDE $M/o' "$(cat $M/o)"
59chk "C4 both locks free after" 'can_excl "$G" && can_excl "$Q"' "still held"
60chk "C5 no want flag left" '[ -z "$(ls -A $M/want.d)" ]' "$(ls -A $M/want.d)"
61
62# D. -o: CMD does not inherit the chip fd, the chip stays held for CMD's life.
63mk D
64$B/quiet-measure flock -o "$G" sh -c 'n=0; for f in /proc/$$/fd/*; do [ "$(readlink $f)" = "$1" ] && n=$((n+1)); done; echo "chipfds=$n"; flock -n -x "$1" true && echo CHIP_FREE_INSIDE; exit 0' x "$G" > $M/o 2>&1; rc=$?
65chk "D1 -o: chip fd not passed to CMD, chip held, rc 0" '[ $rc = 0 ] && grep -q chipfds=0 $M/o && ! grep -q CHIP_FREE_INSIDE $M/o' "rc=$rc $(tr '\n' ' ' < $M/o)"
66
67# E. Other forms.
68mk E
69flock -s "$G" sleep 3 & H=$!; sleep 0.3
70t0=$(ms); $B/quiet-measure flock -s "$G" true; rc=$?; el=$(( $(ms) - t0 ))
71chk "E1 -s beside another shared holder runs at once" '[ $rc = 0 ] && [ $el -le 1500 ]' "rc=$rc el=$el"
72wait $H
73flock -x "$G" sleep 4 & H=$!; sleep 0.3
74t0=$(ms); $B/quiet-measure flock -n "$G" echo SHOULD-NOT-RUN > $M/n.out 2>&1; rcn=$?; eln=$(( $(ms) - t0 ))
75t0=$(ms); $B/quiet-measure flock -w 1 "$G" echo SHOULD-NOT-RUN > $M/w.out 2>&1; rcw=$?; elw=$(( $(ms) - t0 ))
76wait $H
77chk "E2 -n on a busy chip: rc 1, quick, CMD not run" '[ $rcn = 1 ] && [ $eln -le 1500 ] && ! grep -q SHOULD-NOT-RUN $M/n.out' "rc=$rcn el=$eln"
78chk "E3 -w 1 on a busy chip: rc 1 after ~1 s, CMD not run" '[ $rcw = 1 ] && [ $elw -ge 900 ] && [ $elw -le 3000 ] && ! grep -q SHOULD-NOT-RUN $M/w.out' "rc=$rcw el=$elw"
79o=$($B/quiet-measure sh -c 'echo plain-ok' 2>&1); chk "E4 non-flock CMD (old path) works" '[ "$o" = plain-ok ]' "$o"
80o=$($B/quiet-measure flock "$G" -c 'echo dash-c-ok' 2>&1); chk "E5 flock FILE -c (old path) works" '[ "$o" = dash-c-ok ]' "$o"
81chk "E6 no want flag left" '[ -z "$(ls -A $M/want.d)" ]' "$(ls -A $M/want.d)"
82
83# F. Deadlock guard: a build holds the quiet lock (shared) and its CMD then queues for the chip that the
84# measurement holds while draining the quiet lock. Without the guard the two wait on each other.
85mk F
86SPARK_DRAIN_MAX=60
87$B/quiet-build sh -c 'sleep 2; flock "$1" sh -c "echo X_chip \$(date +%s%3N)"' x "$G" > $M/x.out 2>&1 & XP=$!
88sleep 0.5
89SPARK_DRAIN_MAX=60 timeout 25 $B/quiet-measure flock "$G" sh -c 'echo "M_ran $(date +%s%3N)"' > $M/m.out 2>$M/m.err & MP=$!
90wait $MP; mrc=$?; timeout 10 sh -c "while kill -0 $XP 2>/dev/null; do sleep 0.2; done"; wait $XP; xrc=$?
91xt=$(awk '$1=="X_chip"{print $2}' $M/x.out); mt=$(awk '$1=="M_ran"{print $2}' $M/m.out)
92chk "F1 both finish (no deadlock): measurement rc 0, build rc 0" '[ "$mrc" = 0 ] && [ "$xrc" = 0 ] && [ -n "$xt" ] && [ -n "$mt" ]' "mrc=$mrc xrc=$xrc x=$(cat $M/x.out) m=$(cat $M/m.out) err=$(head -c 200 $M/m.err)"
93chk "F2 the measurement let the chip go for the build (yield logged)" 'grep -q "holds the quiet lock open" $HOME/.local/state/quiet/measure-yields.log 2>/dev/null' "no yield line"
94unset SPARK_DRAIN_MAX
95
96# G. Drain cap: an unrelated exclusive holder of the quiet lock; the measurement lets the chip go after
97# SPARK_DRAIN_MAX s instead of sitting on the chip, then runs when the holder leaves.
98mk G
99flock -x "$Q" sleep 7 & H=$!; sleep 0.3
100SPARK_DRAIN_MAX=2 timeout 30 $B/quiet-measure flock "$G" sh -c 'echo M_ran' > $M/m.out 2>$M/m.err & MP=$!
101sleep 3.2; can_excl "$G"; chip_free_mid=$?
102wait $H; wait $MP; mrc=$?
103chk "G1 drain cap: yield logged ('did not drain')" 'grep -q "did not drain in 2s" $HOME/.local/state/quiet/measure-yields.log 2>/dev/null' "no yield line"
104chk "G2 measurement ran after the holder left (rc 0)" '[ "$mrc" = 0 ] && grep -q M_ran $M/m.out' "rc=$mrc err=$(head -c 200 $M/m.err)"
105echo " G: chip lock free sampled mid-wait: $([ $chip_free_mid = 0 ] && echo yes || echo no) (informational)"
106
107# H. A measurement killed (TERM to quiet-measure only) while queued for the chip never runs CMD once the
108# chip frees. The pre-fix trap only removed the flag and carried on.
109mk H
110flock "$G" sleep 4 & H=$!; sleep 0.3
111$B/quiet-measure flock "$G" sh -c 'echo SHOULD-NOT-RUN' > $M/o 2>&1 & MP=$!
112sleep 1; kill -TERM $MP
113wait $H; wait $MP; mrc=$?; sleep 0.3
114chk "H1 TERM while queued for the chip: CMD not run, rc 143, chip free, no flag" '! grep -q SHOULD-NOT-RUN $M/o && [ "$mrc" = 143 ] && can_excl "$G" && [ -z "$(ls -A $M/want.d)" ]' "rc=$mrc out=$(tr '\n' ' ' < $M/o) flags=$(ls -A $M/want.d)"
115
116# I. With the chip in hand the gate is waited for at most SPARK_DRAIN_MAX s, then passed (fail open): a gate
117# holder must not keep the chip idle for the old 25 min gate wait.
118mk I
119flock -x "$M/gate" sleep 8 & H=$!; sleep 0.3
120t0=$(ms); SPARK_DRAIN_MAX=2 timeout 20 $B/quiet-measure flock "$G" sh -c 'echo M_ran' > $M/o 2>&1; mrc=$?; el=$(( $(ms) - t0 ))
121wait $H
122chk "I1 gate held by another: with the chip, pass it after SPARK_DRAIN_MAX (2 s) and run, rc 0" '[ "$mrc" = 0 ] && grep -q M_ran $M/o && [ $el -ge 1500 ] && [ $el -le 5000 ]' "rc=$mrc el=$el"
123
124echo "SUMMARY variant=$V pass=$pass fail=$fails"
125exit $fails