#!/bin/csh -f
#
# gantt - plot a gantt chart for the given elogs
#
# This picks out the key events - read, demand, hit, prefetch, etc.
# Then it plots them in a sortof gantt chart 
#
# usage: gantt from to logfile.elog...
#  only intervals [from..to] are plotted. See pickint for details.

set P=`procdir`

if ($#argv < 3) then
	echo usage: gantt from to logfile.elog...
	exit 1
endif

set TMP=/tmp/gantt$$
set plot=$TMP/plot
onintr cleanup
mkdir $TMP

set from=$1
set to=$2
shift;shift

set files=$TMP/{start,end,read,pref,demand.start,demand.end,hit.start,hit.end,mistake}

foreach logfile ($*)
    elogdump -stp =2 =3 =4 =5 =6 =10 =12 =15 =16 =23 $logfile:r \
    	   | pickint $from $to \
        | (cd $TMP; Awk -f $P/gantt.awk)

    # find out how many points there are per file, and the time range
    wc -l $files | cols 1 | stats max > $TMP/max
    set samples=`cat $TMP/max`
    set xrange="`datarange $files`"

    echo set title \"$logfile:r $from-$to\" > $plot
    echo set noclip >> $plot
    echo set data style points >> $plot
    echo set samples $samples >> $plot
    echo set xrange "$xrange" >> $plot

    # we do the chart in 5-processor plots
    set nodes=`elognodes -n $logfile:r `
    set ymin=6
    set ymax=59

    while ($nodes > 0)
    	   echo "set yrange [${ymin}:$ymax]" >> $plot
        echo -n "plot " >> $plot
        foreach i (start end read pref demand.start demand.end hit.start hit.end mistake)
            if (-e $TMP/$i) then
            	echo -n \"$i\", >> $plot
            else
            	echo -n 0, >> $plot
            endif
        end
        echo 0 >> $plot
        
    	   @ ymin = $ymin + 50
    	   @ ymax = $ymax + 50
    	   @ nodes = $nodes - 5
    end

    (cd $TMP; lasergnu -l -f plot)

    rm -f $TMP/*
end

cleanup:
rm -rf $TMP
