#!/bin/bash

#
# visualize memory consumption over time
# as recorded by pmap / track_memory.sh
# script
#

logfile=$1

if [ ! -f "$logfile" ]; then
  echo "cannot find memory logfile: $1"
  echo
  echo "usage: show_memory.sh LOGFILE"
  echo
  echo "example: show_memory.sh mem.log.12345"
  exit
fi

title=$(head -n1 "$logfile")
timeout=$(head -n2 "$logfile" | tail -n1)
basetime=$(head -n3 "$logfile" | tail -n1 | awk '{print $1}')

title=${title/\# /}
timeout=${timeout/\# /}

if [[ "$2" == "smooth" ]]; then
  lineType="smooth bezier"
else
  lineType="w lines"
fi

read -r -d '' plot <<GNUPLOT
set title '$title' noenhanced;
set xlabel 'time in s';
set ylabel 'memory consumption in MB';
set key bottom right;
set grid;
plot \
  '$logfile' using (\$1 - $basetime):(\$2/1024) $lineType title 'RSS', \
  '$logfile' using (\$1 - $basetime):(\$3/1024) $lineType title 'Dirty', \
  '$logfile' using (\$1 - $basetime):(\$4/1024) $lineType title 'Heap', \
  '$logfile' using (\$1 - $basetime):(\$5/1024) $lineType title 'Stack' ;
  ;
GNUPLOT

gnuplot -p -e "$plot"
