#!/bin/bash
# run.sh <name> <duration_s> [fill]: start agent probe + 2 tenant apps, collect, tear down.
# Per-run tuning (slice drop-ins, sysctls, swap) is applied by the caller before this.
name=$1; dur=$2; fill=${3:-a5}; d=/opt/lab/out/$name; mkdir -p $d; cd /opt/lab
sync; echo 3 > /proc/sys/vm/drop_caches; sleep 2
systemctl reset-failed 'app*' agent.service 2>/dev/null
dmesg -C
systemd-run -q --unit=agent --slice=agent.slice ${AGENT_PROPS} /opt/lab/file-hot /opt/lab/agent.bin seq 10000
sleep 5
./collect.sh $d/collect.csv & cpid=$!
systemd-run -q --unit=app1 --slice=thrash.slice ${APP_PROPS} /opt/lab/app.sh ${HEAP:-250} $fill /opt/lab/hot1.bin skew 48 95
systemd-run -q --unit=app2 --slice=thrash.slice ${APP_PROPS} /opt/lab/app.sh ${HEAP:-250} $fill /opt/lab/hot2.bin skew 48 95
start=$(date +%s); echo start=$start heap=${HEAP:-250} fill=$fill > $d/meta
sleep $dur
for u in app1 app2 agent; do systemctl show $u -p ActiveState -p Result >> $d/meta; done
cat /sys/fs/cgroup/agent.slice/memory.events > $d/agent.memory.events 2>/dev/null
cat /sys/fs/cgroup/thrash.slice/io.stat > $d/thrash.io.stat 2>/dev/null
cat /sys/fs/cgroup/thrash.slice/memory.pressure /sys/fs/cgroup/thrash.slice/io.pressure > $d/thrash.pressure 2>/dev/null
[ -e /sys/block/zram0/mm_stat ] && cat /sys/block/zram0/mm_stat > $d/zram.mm_stat
kill $cpid
for u in app1 app2 agent; do journalctl -q -u $u --since @$start -o cat > $d/$u.log; systemctl stop $u 2>/dev/null; done
journalctl -q -u systemd-oomd --since @$start -o short-iso > $d/oomd.log
dmesg | grep -iE 'oom|killed' > $d/dmesg.oom
echo "== $name"; cat $d/meta
