#!/usr/bin/env bash
# DV-5, the unit body OUTSIDE the sandbox, on benchbox (L-2's run_unit_lev2.sh form). Started by the lane as a
# transient user unit:
#
#   systemd-run --user --unit dv-speed-<dry|timed>-<arm> --collect -p MemoryMax=16G -p MemorySwapMax=0 \
#     -p RuntimeMaxSec=<900|1800> --setenv=DV_DIR=<scratch> bash <wt>/.../speed/unit_speed.sh <arm> <dry|timed>
#
# In order: (1) the card lock is read FIRST (the house rule, D-20260928-025: the body checks its OWN lock before
# anything reads or touches the card); (2) the card's posture of record, read on the host where
# nvidia-persistenced answers: alone (no compute process), at most 50 MiB, 300.00 W, persistence Enabled, or
# REFUSED; (3) the stop guard's heartbeat under 10 s old, and the UPS on line with a runtime of at least 300 s, or
# REFUSED; (4) the sandboxed arm under taskset (CPU arms: CPUs 0-7, one thread per core; GPU arms: CPUs 1-7, the
# house's GPU form); (5) the posture again after. The card's UUID is read here into DV_CARD for GPU arms only and
# is never printed or written.
set -uo pipefail
ARM=${1:?arm}; MODE=${2:?dry or timed}
: "${DV_DIR:?set DV_DIR}"
S=$DV_DIR/wt/bench/deciders-von-laya-2026-10-01/instrument/speed
P=$DV_DIR/receipts-speed/$MODE-$ARM-posture.txt
bash "$S/lock_gate.sh" >> "$P" 2>&1 || { echo "REFUSED: the dv-speed lock is not held" | tee -a "$P"; exit 3; }
U=$(nvidia-smi --query-gpu=uuid --format=csv,noheader)
[ "$(printf '%s\n' "$U" | grep -c .)" = 1 ] || { echo "REFUSED: not exactly one card on the bus" | tee -a "$P"; exit 3; }
posture() {
  printf '%s %s ' "$(date -u +%FT%TZ)" "$1"
  nvidia-smi --id="$U" --query-gpu=memory.used,power.limit,persistence_mode --format=csv,noheader,nounits | tr -d '\n'
  printf ', compute_apps=%s\n' "$(nvidia-smi --id="$U" --query-compute-apps=pid --format=csv,noheader | grep -c .)"
}
posture before >> "$P"
line=$(tail -n 1 "$P")
case "$line" in
  *", 300.00, Enabled, compute_apps=0") ;;
  *) echo "REFUSED: the card is not alone at its posture before the arm: $line" | tee -a "$P"; exit 3 ;;
esac
mem=$(printf '%s\n' "$line" | awk '{print $3}' | tr -d ,)
[ "$mem" -le 50 ] || { echo "REFUSED: $mem MiB used before the arm" | tee -a "$P"; exit 3; }
hb=$DV_DIR/guard/guard.heartbeat
age=$(( $(date +%s) - $(stat -c %Y "$hb" 2>/dev/null || echo 0) ))
[ "$age" -lt 10 ] || { echo "REFUSED: the stop guard's heartbeat is ${age} s old" | tee -a "$P"; exit 3; }
st=$(upsc pr1500@localhost ups.status 2>/dev/null); rt=$(upsc pr1500@localhost battery.runtime 2>/dev/null)
case " $st " in
  *" OL "*) ;;
  *) echo "REFUSED: the UPS reads '${st:-nothing}', not on line" | tee -a "$P"; exit 3 ;;
esac
case "$st" in *OB*|*LB*) echo "REFUSED: the UPS reads '$st'" | tee -a "$P"; exit 3 ;; esac
[ -n "$rt" ] && [ "${rt%.*}" -ge 300 ] || { echo "REFUSED: the UPS runtime reads '${rt:-nothing}' s" | tee -a "$P"; exit 3; }
echo "$(date -u +%FT%TZ) ups ${st} runtime ${rt} s; guard heartbeat ${age} s" >> "$P"
case "$ARM" in
  *-gpu) CPUS=1-7; export DV_CARD="$U" ;;
  *)     CPUS=0-7; unset DV_CARD ;;
esac
DV_DIR=$DV_DIR taskset -c "$CPUS" bash "$S/sandbox_speed.sh" "$ARM" bash "$S/serve_speed.sh" "$ARM" "$MODE"
rc=$?
sleep 2
posture after >> "$P"
echo "EXIT=$rc" >> "$P"
exit $rc
