#!/bin/sh
# CVS: $Id: plot,v 1.28 2006/02/03 18:27:19 pascal Exp $
# Tag: $name$
# Info: $CVSROOT/COPYRIGHT

#if [$1 =~ -h* || $#argv < 2 || $#argv >= 5]; then
if [ $# -le 3 ]; then  
  echo " "
  echo "Usage: ./plot <log-file-name> <plotName> <init/all> <latency/nolatency> [start-cycle] [end-cycle]"
  echo ""
  echo "<log-file-name>, the log file to be used"
  echo "<plotName>, name of the output plot.  If anything other then 'screen'"
  echo "is given then the plot will go to a png image file named by the plotName"
  echo "you give.  Specifying 'screen' will send the plot to the screen."
  echo "<init>, If you set the third parameter to 'init' it will plot the " 
  echo "following events: waiting for lock, setting time, done preprocessing"
  echo "If you set it to 'all' it will plot the following events: waiting for lock,"
  echo "done pre-processing, planning, post-processing, printing database,"
  echo "sending messages."
  echo "<latency>, if you set the fourth parameter to 'latency' it will plot "
  echo "the latency"
  echo "[start-cycle], optional, start point for the graph "
  echo "[end-cycle], optional, end point for the graph "
  echo ""
  echo "Example: "
  echo "./plot log plot_name2 init latency"
  echo "./plot log plot_name1 all latency"
  echo "./plot log screen all nolatency"
  echo "./plot log screen all nolatency 100 200"
  echo
  exit 1
fi

callPath=${0%/*}

useRange=0
if [ "$5" != "" ] && [ "$6" != "" ]; then
  useRange=1
  start=$5
  end=$6
fi

plot=$3
log_file=$1

plotName=$2
displayPlot=0;
if [ $2 == 'screen' ]; then
  displayPlot=1;
fi

if [ ! -e $log_file ]; then
  echo " "
  echo 'No log file for '$1' agent.'
  echo " "
  exit
fi

temp_data=$log_file-plot.data
gplot_cmd_file=$log_file-plot.cmds
gplot_loop_file=$log_file-plot.loop

width=1
lstyle="lines"
temp_data0=$temp_data
if [ "$4" = "nolatency" ]; then
  latencyString=""
else
  latencyString="'"$temp_data0"'"' using 1:9 title "latency" w '$lstyle' lw '$width', '
fi

show_graph=""

# There can be multiple planners in the log, we only look at the one running in thread by the 
# following name
thread="Controller"

realtime_sampleone=`awk '/Started loading the model/ {printf "%f", $4}' $log_file`
simtime_sampleone=`awk '/Started loading the model/ {printf "%f", $5}' $log_file`
realtime_sampletwo=`awk '/startup-done/ {printf "%f", $4}' $log_file`
simtime_sampletwo=`awk '/startup-done/ {printf "%f", $5}' $log_file`

deltarealtime=$(echo "scale=9;$realtime_sampletwo-$realtime_sampleone" | bc)
deltasimtime=$(echo "scale=9;$simtime_sampletwo-$simtime_sampleone" | bc)

if [ $deltasimtime = 0 ]; then
  timescale=1
else
  timescale=$(echo "scale=9;$deltarealtime/$deltasimtime" | bc)
fi

latency=`awk '/Latency/ {printf "%f ", $3}' $log_file` 
latencyscaled=$(echo "scale=9;$latency * $timescale" | bc)
scale_factor=$latencyscaled 

rm -f $temp_data 
rm -f $gplot_cmd_file
rm -f $gplot_loop_file

touch $temp_data
#  This is for plot init
#  Parse file for the information we need for gnuplot
   awk '/Start of cycle/ { Start=$4; gStart=Start; do { gStart=gStart-1; } while (gStart > 1); } /Setting reboot/ { Setting=$4-Start; } /start updating/ { startu=$4-Start; } /finished updating/ { finishedu=$4-Start; } /found consistent plan/ { found=$4-Start; } /finished post-process/ { finishedp=$4-Start; } /finished reactive-planning/ { finishedr=$4-Start; } /Cancelling reboot/ { cancelling=$4-Start; printf("%f %f %f %f %f %f %f %f %f\n", $6, Setting, startu, finishedu, found, finishedp, finishedr, cancelling, scale); }' scale=$scale_factor $log_file > $temp_data

# filename=`pwd | awk '{ nr = split($0, temp, "/"); print temp[nr] }'`
if [ `grep "Start of cycle" $log_file | wc -l` -eq 0 ]; then
 	echo "Could not find data string 'Start of cycle'. "
	exit 1
fi
 
if [ `cat $temp_data | wc -l` -eq 0 ]; then
	echo "Could not find data in log file "$log_file".  Data plot file size is 0."
	exit 1
fi

title="Execution time of reactive planner cycles"

width=1
lstyle="lines"

touch $gplot_cmd_file
echo 'set ylabel "[Seconds]"' > $gplot_cmd_file

if [ $useRange = 1 ]; then
  echo 'set xrange ['$start':'$end']' >> $gplot_cmd_file
fi

echo 'set xlabel "Time [seconds]"' >> $gplot_cmd_file
echo 'set title "'$title'\n Latency '$latency'\n"' >> $gplot_cmd_file

if [ 0 -eq $displayPlot ]; then
  echo 'set terminal png' >> $gplot_cmd_file
  echo 'set out "'$plotName'"' >> $gplot_cmd_file
fi

if [ "$plot" = "init" ]; then
  echo 'plot '$latencyString' "'$temp_data0'" using 1:4 title "done pre-processing" w '$lstyle' lw '$width', "'$temp_data0'" using 1:3 title "setting time" w '$lstyle' lw '$width', "'$temp_data0'" using 1:2 title "waiting for lock" w '$lstyle' lw '$width  >> $gplot_cmd_file

else

  echo 'plot '$latencyString'"'$temp_data0'" using 1:8 title "sending messages" w '$lstyle' lw '$width', '"'"$temp_data0"'"' using 1:7 title "printing database" w '$lstyle' lw '$width', '"'"$temp_data0"'"' using 1:6 title "post-processing" w '$lstyle' lw '$width', '"'"$temp_data0"'"' using 1:5 title "planning" w '$lstyle' lw '$width', '"'"$temp_data0"'"' using 1:4 title "done pre-processing" w '$lstyle' lw '$width', '"'"$temp_data0"'"' using 1:2 title "waiting for lock" w '$lstyle' lw '$width >> $gplot_cmd_file
 
fi

gnuplot -geometry -0-0 -persist $gplot_cmd_file &

rm -f $temp_data 
rm -f $gplot_cmd_file
rm -f $gplot_loop_file
