#!/usr/bin/env bash
set -euo pipefail

OUT="${1:-/home/xsl/product/outputs/gpu-metrics.csv}"
SECONDS_TOTAL="${2:-3600}"
INTERVAL="${3:-5}"

mkdir -p "$(dirname "$OUT")"
echo "ts,index,name,util_gpu,util_mem,mem_used_mb,mem_total_mb,temp_c,power_w" > "$OUT"

end_ts=$(( $(date +%s) + SECONDS_TOTAL ))
while [[ "$(date +%s)" -lt "$end_ts" ]]; do
  now="$(date +%s)"
  nvidia-smi --query-gpu=index,name,utilization.gpu,utilization.memory,memory.used,memory.total,temperature.gpu,power.draw \
    --format=csv,noheader,nounits \
    | awk -v ts="$now" -F', ' '{print ts","$1","$2","$3","$4","$5","$6","$7","$8}' >> "$OUT"
  sleep "$INTERVAL"
done

echo "saved: $OUT"
