#! /bin/csh -f 
# Last edited on 2011-06-06 21:27:19 by stolfi

set usage = "$0 [ -log | -lin ] [ -cpus N ] ARCH.MACHINE"

set path = ( /bin /usr/bin /usr/local/bin )

set datfile = "/tmp/$$.dat"
set tmpfile = "/tmp/$$.plt"
set cpufile = "/tmp/$$.cpu"
set reffile = "/tmp/$$.ref"

set scaletype = "nologscale"
set scaletag = "lin"
#
set xmin = -2
set xmax = 50
set xtics = '( 1, " " 2, 4, 8, " " 10, 16, " " 20, 24, 32, 48 )'
#
set ymin = -5
set ymax = 850
set ytics = '( 0, 100, 200, 300, 400, 500, 600, 700, 800 )'
#
touch ${cpufile}
set plotcpus = ""

touch ${reffile}
set plotref = ""

while ( ( $#argv > 0 ) && ( "/$1" =~ /-* ) ) 
  if ( ( $#argv >= 1 ) && ( "/$1" == "/-log" ) ) then
    set scaletype = "logscale" 
    set scaletag = "log" 
    set xmin = "0.5"; set xmax = "127"
    set xtics = '( 1, 2, 4, 8, 16, 32, 64 )'
    set ymin = "0.1"; set ymax = "4999"; 
    set ytics = '( 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000 )'
    cat <<EOF > ${reffile}
0.7071  7.071
90.51   0.0552

0.7071  70.71
90.51   0.552

0.7071  707.1
90.51   5.52
EOF
    set plotref = '"'"${reffile}"'" using 1:2 title "" with lines lt 0, '
    shift
  else if ( ( $#argv >= 1 ) && ( "/$1" == "/-lin" ) ) then
    shift
  else if ( ( $#argv >= 2 ) && ( "/$1" == "/-cpus" ) ) then
    set cpus = "$2";
    echo "${cpus} ${ymin}" > ${cpufile}
    echo "${cpus} 700" >> ${cpufile}
    set plotcpus = '"'"${cpufile}"'" using 1:2 title "" with lines lt 2, '
    shift; shift
  else 
    echo "bad option "'"'"$1"'"'
    echo "usage: ${usage}"; exit 1
  endif
end

if ( $#argv != 1 ) then
  echo "usage: ${usage}"; exit 1
endif

set arcmac = $1; shift

set inpfile = "out/${arcmac}"
set ppmfile = "plt/${arcmac}-${scaletag}.ppm"
set giffile = "plt/${arcmac}-${scaletag}.gif"

if ( -r ${ppmfile} ) then 
  /bin/mv ${ppmfile} ${ppmfile}~
endif

cat ${inpfile} \
  | egrep '^tt[.][a-z]+.*totcpu *$' \
  | sort -b +4 -5n +1 -2n +5 -6n \
  > ${datfile}

cat ${datfile} \
  | gawk \
    ' \
      BEGIN { ttbest = -1; nthprev = 1000; } \
      function oops(msg){ print msg > "/dev/stderr"; exit 1; } \
      // { \
        wset = $2; trn = $5; nth = $6; \
        mops = $7; mb = $9; \
        te = $11 + 0.000001; tt = $13 + 0.000001; \
        if (nth <= nthprev) { \
          printf "\n\n"; \
          if (nth != 1) { oops(("missing mono case:" nthprev " " nth)); }\
          temono = te; \
        }\
        nthprev = nth; \
        if (ttbest == -1) { ttbest = tt; } \
        printf "%4d %4d %8d  %s %2d  %7.1f %5.1f  %7.1f %5.3f\n", \
          mb, mops, wset, (trn == 0 ? "C" : "S"), nth, \
          te, temono/te, tt, ttbest/tt; \
      } \
    ' \
  > ${tmpfile}
  
gnuplot <<EOF
set size 0.4,1
set terminal pbm small color
set output "${ppmfile}"
# set terminal x11
#
set xlabel "threads"
set xrange [${xmin}:${xmax}]
set ${scaletype} x 
set xtics ${xtics}
#
set ylabel "elapsed time (sec)"
set yrange [${ymin}:${ymax}]
set ${scaletype} y 
set ytics ${ytics}
#
set title "${arcmac}"
set pointsize 1.5
plot ${plotcpus} ${plotref} \
  "${tmpfile}" index 2 using 5:6 title "scattered 6.7K" with linespoints linetype 3, \
  "${tmpfile}" index 3 using 5:6 title "scattered 160K" with linespoints linetype 9, \
  "${tmpfile}" index 0 using 5:6 title "  compact 6.7K" with linespoints linetype 1, \
  "${tmpfile}" index 1 using 5:6 title "  compact 160K" with linespoints linetype 7
# pause 300
quit
EOF

if ( ( -r ${ppmfile} ) && ( ! ( -z ${ppmfile} ) ) ) then
  cat ${ppmfile} | ppmtogif > ${giffile}
  # display ${giffile}
endif
/bin/rm -f ${tmpfile} ${datfile} ${cpufile} ${reffile} ${ppmfile}
