#!/usr/bin/env bash
# Interleaved decode A/B on rack-81 card 0 (A5500), one tree (5f2de30d2-based, the evidence boot tree) with exl3_moe_route_race.py overlaid:
# a = race_step unused (shared expert undeclared, as main) + L2 log; b = the fix + L2 log (ea00a25a3 file).
# a1 b1 a2 b2 under ONE card-0 lock; a1 vs a2 is the null. Clocks sampled through each arm (pstate, SM/app clocks).
E=/mnt/k8scache/agent-snapshots/exl3-flint-cells-20261112; R=/dev/shm/flintcells; mkdir -p $R/out
exec 9>/run/lock/arbi-gpu-0.lock; echo "$(date -Is) waiting card 0"; flock -w 43200 9 || exit 1; echo "$(date -Is) card 0 held"
for t in a1 b1 a2 b2; do
  nvidia-smi -i 0 --query-gpu=timestamp,pstate,clocks.sm,clocks.applications.graphics,clocks.max.sm,temperature.gpu,power.draw,utilization.gpu \
    --format=csv,noheader -lms 2000 > $R/out/clocks-$t.log 2>&1 & ck=$!
  ARM=$t TREE=$E/tree RACE=$E/ab/race/${t:0:1}.py HELD=1 bash $E/scripts/fc_ab_run.sh flint 0 8000 > $R/out/arm-$t.log 2>&1
  kill $ck; echo "$(date -Is) $t done: $(grep -E 'drive rc|FAILED|not ready' $R/out/rk81-flint-tp1-$t/run.log | tail -1)"
done
echo "$(date -Is) JOB_AB_DONE"
