#!/bin/sh
# DF-5642 RUN A (baseline, stock kernel): unguarded child re-creation.
#
# EXPECTED (buggy, sys/bus/smbus/smbus.c:94-97 + 105-115):
#   - after kldload smb: /dev/smb nodes minted for the initial child set
#   - each CYCLE (device_detach + device_probe_and_attach of smbus0 --
#     the production module-teardown/re-registration choreography):
#       nchildren grows by 1 (line-94 generic child) + |respond| (probe
#       children), the previous set survives detached and re-attaches,
#       the new set gets REMAPPED units (devclass_alloc_unit) -> extra
#       /dev/smbN nodes appear and probe children lose the 0x400
#       forced-slave encoding.
set -e
cd /root/df5642

echo "=== load the REAL smbus chain under the fake controller ==="
kldload /root/df5642/df5642_rig.ko 2>/dev/null || true
sleep 0.3
echo "--- now kldload the real smb.ko (children attach, /dev minted) ---"
# smb driver compiled into guest kernel
sleep 0.3
dmesg | grep '\[df5642\]' | tail -20

echo; echo "=== initial state ==="
./df5642ctl stat
ls /dev/smb* 2>/dev/null | sort | tr '\n' ' '; echo
echo "node_count=$(ls /dev/smb* 2>/dev/null | wc -l)"

echo; echo "=== 3 detach+re-attach cycles (production APIs) ==="
./df5642ctl cycle 3
ls /dev/smb* 2>/dev/null | sort | tr '\n' ' '; echo
echo "node_count=$(ls /dev/smb* 2>/dev/null | wc -l)"

echo; echo "=== 3 more cycles (unbounded) ==="
./df5642ctl cycle 3
ls /dev/smb* 2>/dev/null | sort | tr '\n' ' '; echo
echo "node_count=$(ls /dev/smb* 2>/dev/null | wc -l)"

echo; echo "=== memory growth (M_BUS) ==="
vmstat -m 2>/dev/null | grep -E '^bus|Item' || true
echo "RUN-A-DONE"
