#!/bin/sh
# DF-5780 run3: burst race + LIVE corruption watchdog.
# Shorter flood windows (fewer debug prints), watchdog kthread probing
# the 48-byte zone and the memo-walk latency DURING the race window.
#
# usage (as root, inside guest): sh run3.sh [cycles] [race_secs]
set -e
cd "$(dirname "$0")"

CYCLES=${1:-6}
RSECS=${2:-30}

kldload ./hci_race_harness.ko 2>/dev/null || true
sysctl hw.hcir_setup=1
sysctl net.bluetooth.hci.memo_expiry=1

install -m 755 race /tmp/race

c=0
while [ $c -lt $CYCLES ]; do
	c=$((c+1))
	echo "== cycle $c/$CYCLES: build list (flood 2s, quiet) =="
	sysctl hw.hcir_flood=1
	sleep 2
	sysctl hw.hcir_flood=0
	sleep 2			# every memo on the list is now EXPIRED
	sysctl hw.hcir_memos=1

	echo "== cycle $c: watchdog + burst + unprivileged racer ($RSECS s) =="
	sysctl hw.hcir_watchdog=1
	sysctl hw.hcir_debug=1
	( sysctl hw.hcir_burst=48 >/dev/null; sleep 2; \
	  sysctl hw.hcir_burst=48 >/dev/null; sleep 2; \
	  sysctl hw.hcir_burst=48 >/dev/null ) &
	su -m maxx -c "/tmp/race 6 $RSECS" || echo "(racer exited nonzero)"
	wait
	sysctl hw.hcir_debug=0
	sysctl hw.hcir_watchdog=0
	sysctl hw.hcir_memos=1 || true
done

echo "== run3 complete =="
dmesg | grep -E "\\*\\*\\*|watchdog totals" | tail -20
dmesg | tail -5
