#!/bin/bash
# Sample GPU utilization every 5s, output CSV
# Usage: bash gpu_monitor.sh <output_file> [interval_s]
#   Runs until killed (Ctrl+C or kill)

OUT="${1:-/tmp/gpu_util.csv}"
INTERVAL="${2:-5}"

echo "timestamp,gpu,util_pct,mem_used_mb,mem_total_mb,power_w" > "$OUT"

while true; do
    TS=$(date +%s.%N)
    nvidia-smi --query-gpu=index,utilization.gpu,memory.used,memory.total,power.draw \
        --format=csv,noheader,nounits 2>/dev/null | while IFS=', ' read -r idx util mem_used mem_total power; do
        echo "$TS,$idx,$util,$mem_used,$mem_total,$power"
    done >> "$OUT"
    sleep "$INTERVAL"
done
