#!/bin/sh

if [ $# -ne 1 ] || [ \! -f $1 ]; then
	echo "usage: ${0##*/} <intel_gpu_top -l output file>"
	exit 1
fi

dat=$1
gnu=$dat.gnu
svg=$dat.svg

echo "GnuPlot'ing $dat..."

# Gnuplot settings
cat > $gnu << EOF
set terminal svg size 800, 3200
set output "$svg"

# margins for plots, not page
set tmargin 1
set bmargin 3
set lmargin 20

set multiplot layout 11, 1 title "intel-gpu-top output: $dat"

set grid
set tics out
set xtics nomirror
set style data lines

# show X-axis label only for bottom graph
unset xlabel

set yrange [ 0 : ]

set ylabel "GPU frequency (MHz)"
plot '$dat' using 1 title 'Request',\\
     '$dat' using 2 title 'Actual'

set ylabel "Power usage (W)"
plot '$dat' using 5 title 'Uncore'

set ylabel "System bandwidth (MiB/s)"
plot '$dat' using 6 title 'Reads',\\
     '$dat' using 7 title 'Writes'

set yrange [ 0 : 100 ]

set ylabel "RC6 (%)"
plot '$dat' using 4 title 'GPU'

set ylabel "Utilization (%)"
plot '$dat' using  8 title 'RCS/0',\\
     '$dat' using 11 title 'BCS/0',\\
     '$dat' using 14 title 'VCS/0',\\
     '$dat' using 17 title 'VCS/1',\\
     '$dat' using 20 title 'VECS/0'

set autoscale y
set yrange [ 0 : ]

set ylabel "/s"
plot '$dat' using 3 title 'Interrupts'

set ylabel "Engine events, se"
plot '$dat' using  9 title 'RCS/0',\\
     '$dat' using 12 title 'BCS/0',\\
     '$dat' using 15 title 'VCS/0',\\
     '$dat' using 18 title 'VCS/1',\\
     '$dat' using 21 title 'VECS/0'

set xlabel "Seconds"

set ylabel "Engine events, wa"
plot '$dat' using 10 title 'RCS/0',\\
     '$dat' using 13 title 'BCS/0',\\
     '$dat' using 16 title 'VCS/0',\\
     '$dat' using 19 title 'VCS/1',\\
     '$dat' using 22 title 'VECS/0'
EOF

gnuplot $gnu

if [ $? -ne 0 ]; then
	echo "ERROR: failed running 'gnuplot $gnu'!"
	exit 1
fi
rm $gnu

echo "View intel_gpu_top graph with: chromium $svg"
