#!/bin/bash
# bench-gc-adaptive.sh — greedy vs adaptive meta-GC policy across
# friendly / hostile / mixed workloads. Each (workload × mode) pair
# runs in a fresh asm-gc process to isolate GC state.
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

run_phase() {
    local workload_name="$1"
    local workload="$2"
    local mode="$3"
    local mode_name="$4"
    echo "== $workload_name / $mode_name =="
    {
        echo "(define *workload* $workload)"
        echo "(define *mode* $mode)"
        cat examples/bench-gc-adaptive.lsp
    } | timeout 60 ./asm/lumbda-gc
    echo ""
}

run_phase "friendly" 0 0 "greedy"
run_phase "friendly" 0 1 "adaptive"
run_phase "hostile"  1 0 "greedy"
run_phase "hostile"  1 1 "adaptive"
run_phase "mixed"    2 0 "greedy"
run_phase "mixed"    2 1 "adaptive"

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