#!/bin/bash
# bench-gc-arena.sh — arena (meta-GC) vs naive sweep on the same
# transient-allocation workload. RSS is sampled by an external
# watcher. Both phases run in the same process so their stats
# counters reflect a single interpreter's behavior.
set -e
cd "$(dirname "$0")/.."

ulimit -v 524288
trap 'pkill -9 -u "$USER" -f "asm/lumbda" 2>/dev/null || true' EXIT

make -s -C asm all

echo "== asm (naive GC, with arena as meta fast path) =="
./asm/lumbda-gc < examples/bench-gc-arena.lsp &
pid=$!
peak=0
while kill -0 "$pid" 2>/dev/null; do
    rss=$(awk '/^VmRSS:/ {print $2}' /proc/$pid/status 2>/dev/null || echo 0)
    [ -n "$rss" ] && [ "$rss" -gt "$peak" ] && peak=$rss
    sleep 0.05
done
wait "$pid" || true
echo ""
echo "peak_rss_kb=$peak"

if pgrep -u "$USER" -f 'asm/lumbda' > /dev/null; then
    echo "STRAGGLER" >&2
    exit 1
fi
