#!/bin/bash
#
# Creates a time scatterplot of scheduling activity, plotting events
# per time along with the scheduler's view of the state of the
# network.  Events are per link, and you specify either one or two.
# The input file is the trace output from the global scheduler
# (schedsvr).  The output is in jgraph format.

if [ $# != 2 -a $# != 3 ]; then
  echo "usage: $0 <tracefile> <link> [link2]"
  exit 1
fi

INFILE=$1
LINK1=$2
LINK2=$3

# graph preamble

echo "newgraph"

echo "legend defaults x 3 y 250"

echo "xaxis min 0 max 120 size 2.5 label : time (s)"
echo "yaxis min 0 max 310 size 2.5 label : bandwidth (KB/s)"

# b/w reports

for link in $LINK1 $LINK2; do
  echo "newcurve"
  echo "  label : $link reported b/w"
  echo "  linetype none"
  echo "  marktype circle"
  if [ "$LINK1" = "$link" ]; then
    echo "  fill 0"
  else
    echo "  fill 1"
  fi
  echo -n "pts "
  egrep -e 'BWREPORT|BWUPDATE' $INFILE | grep $link | awk '{ tm=$1; bw=$5 / 1000.0; printf("%0.2f %d ",tm,bw); }'

  echo

# b/w estimate

  echo "newcurve"
  echo "  label : $link assumed b/w"
  echo "  linetype none"
  echo "  marktype box"
  if [ "$LINK1" = "$link" ]; then
    echo "  fill 0"
  else
    echo "  fill 1"
  fi
  echo -n "pts "
  egrep -e 'BWREPORT|BWUPDATE|BWCREEP' $INFILE | grep $link | awk '{ tm=$1; bw=$4 / 1000.0; printf("%0.2f %d ",tm,bw); }'

  echo
done

# reconfigs


grep RECONFIG $INFILE | awk '{ if (last==0) { ofs=10; last=1; } else { ofs=-10; last=0; } tm=$1; printf("newcurve\nmarktype text y %d : %s \ncolor 0 0 1\npts %0.2f 20\n",ofs,$3,tm); printf("newcurve\nmarktype cross\ncolor 0 0 1\npts %0.2f 20\n",tm); }'

echo

