#!/usr/bin/env bash
# run.sh - run the matvec benchmark on the ODROID-HC1 and save results.
# Usage: ./run.sh [runs]        (default 21 runs per variant, median reported)
# Writes results-<date>.csv and results-<date>.txt next to this script.
# It does NOT change your CPU governor; it only suggests one.
set -u
cd "$(dirname "$0")"
RUNS="${1:-21}"
STAMP="$(date +%Y%m%d-%H%M%S)"
CSV="results-$STAMP.csv"
TXT="results-$STAMP.txt"

[ -x ./bench ] || make || { echo "build failed" >&2; exit 1; }

{
echo "=== asmbench results $STAMP ==="
echo "--- system info ---"
uname -a
echo "gcc: $(gcc --version | head -1)"
lscpu 2>/dev/null | grep -E 'Model name|Architecture|CPU\(s\)|On-line|Thread|Core|Max MHz|Min MHz' || true
for c in 0 4; do
  d=/sys/devices/system/cpu/cpu$c/cpufreq
  [ -r $d/scaling_governor ] && echo "cpu$c governor: $(cat $d/scaling_governor)  cur_khz: $(cat $d/scaling_cur_freq 2>/dev/null)  max_khz: $(cat $d/scaling_max_freq 2>/dev/null)"
done
echo "free -m:"; free -m
echo
echo "NOTE: for steadier numbers, consider the 'performance' governor, e.g."
echo "  echo performance | sudo tee /sys/devices/system/cpu/cpu4/cpufreq/scaling_governor"
echo "(and cpu0 for the A7 run). This script does not change it. Stop llama.cpp and other"
echo "heavy jobs first. Note the temperature/throttling if the board is hot."
echo

# One thread pinned to the big cores (A15, CPUs 4-7); the kernel picks one of 4-7.
echo "##### A15 big cores: taskset -c 4-7 (single thread) #####"
taskset -c 4-7 ./bench -n "$RUNS" -o "$CSV" -l "A15_cpu4-7" || { echo "BENCH FAILED"; exit 2; }
echo
# One thread pinned to a little core (A7, CPU 0).
echo "##### A7 little core: taskset -c 0 (single thread) #####"
taskset -c 0 ./bench -n "$RUNS" -o "$CSV" -l "A7_cpu0" || { echo "BENCH FAILED"; exit 2; }
} 2>&1 | tee "$TXT"

echo
echo "Saved: $CSV and $TXT"
