# !/bin/bash
duration="$1"

testtime=$(date +'%m_%d_%Y_%T')
start=$(date +%s)
current=$(date +%s)

title_time="Time"
title_ids=$(sensors | grep -A 0 'Package' | cut -c1-13 | tr '\n' ',')
title_cores=$(sensors | grep -A 0 'Core' | cut -c1-7 | tr '\n' ',')
title=${title_time},${title_ids},${title_cores}
echo $title >> /tmp/cpu_log_$testtime.csv

echo "Beginning temperature logging..."
while ((((current-start)) \< $duration));
    do sample_date=$(date +%T);
    sample_id_temps=$(sensors | grep -A 0 'Package' | cut -c17-20 | tr '\n' ',');
    sample_core_temps=$(sensors | grep -A 0 'Core' | cut -c17-20 | tr '\n' ',');
    sample=${sample_date},${sample_id_temps},${sample_core_temps}
    echo $sample >> /tmp/cpu_log_$testtime.csv
    nvidia-smi --query-gpu=timestamp,index,utilization.gpu,temperature.gpu,power.draw --format=csv >> /tmp/gpu_log_$testtime.csv
    sleep 1;
    current=$(date +%s);
done

python experimental/gma/compute_stress_test/parse_gpu_log.py --gpu_log /tmp/gpu_log_$testtime.csv
rm /tmp/gpu_log_$testtime.csv
