#! /bin/sh
#################### set your defaults here ##############
FLAVOR="" # flavor of UNIX: if not determined by script: SysV or BSD
FULL_SUITE="dhry2 dhry2reg arithoh register short int long float double syscall pipe context1 spawn execl fstime C shell dc hanoi"
###############################################################################
#  The BYTE UNIX Benchmarks - Release 3
#          Module: Run   SID: 3.11 5/15/91 19:30:14
#          
###############################################################################
# Bug reports, patches, comments, suggestions should be sent to:
#
#	Ben Smith,        	Tom Yager at BYTE Magazine
#	ben@bytepb.byte.com   	tyager@bytepb.byte.com
# BIX:  bensmith          	tyager
#
###############################################################################
#  Modification Log:
# $Header: run,v 5.2 88/01/12 06:23:43 kenj Exp $
#     Ken McDonell, Computer Science, Monash University
#     August 1, 1983
# 3/89 - Ben Smith - BYTE: globalized many variables, modernized syntax
# 5/89 - commented and modernized. Removed workload items till they
#        have been modernized. Added database server test.
# 11/14/89 - Made modifications to reflect new version of fstime
#        and elimination of mem tests.
# 10/22/90 - Many tests have been flipped so that they run for
#        a specified length of time and loops are counted.
#  4/3/91 - Cleaned up and debugged several test parameters - Ben
#  4/9/91 - Added structure for creating index and determing flavor of UNIX
#  4/26/91 - Made changes and corrections suggested by Tin Le of Sony
#  5/15/91 - Removed db from distribution
#
###############################################################################
ID="@(#)Run:3.11 -- 5/15/91 19:30:14";
version="3.11"
#######################################################################
# General Purpose Benchmark
# based on the work by Ken McDonell, Computer Science, Monash University
#
#  You will need ...
#	awk cat cc chmod comm cp date dc df echo ed expr
#	kill ls make mkdir rm sed test time touch tty umask who
#       AND /usr/bin/time
#  to generate result indexes, you will also need ...
#       join
#
#
umask 022    # at least mortals can read root's files this way
#
if [ -z "$FLAVOR" ]
then
   # determine flavor of UNIX from number of lines generated by /bin/tim
   Fcount=`/usr/bin/time date 2>&1 | wc -l | sed 's/ //g'`
   case "$Fcount"
   in
	2) FLAVOR="BSD";;
	5) FLAVOR="SysV";;
	*) echo "Flavor of UNIX is not known."
	   echo "Please define FLAVOR in Run script"
	   exit;;
   esac
fi
#if SysV use 'uname -a' -- if BSD use 'hostname'
if [ $FLAVOR = "SysV" ]
then UNAME="uname -a"
else UNAME="hostname"
fi
export FLAVOR
# check that the required files are in the proper places
#if make check 
#        then :
#        else make all
#fi
#
#
# establish full paths to directories
PWD=`pwd`
HOMEDIR=${HOMEDIR-.}
cd $HOMEDIR
HOMEDIR=`pwd`
cd $PWD

BINDIR=${BINDIR-${HOMEDIR}/pgms}
cd $BINDIR
BINDIR=`pwd`
cd $PWD
# let's extend the path to this directory
PATH="${PATH}:${BINDIR}"

SCRPDIR=${SCRPDIR-${HOMEDIR}/pgms}
cd $SCRPDIR
SCRPDIR=`pwd`
cd $PWD

TMPDIR=${TMPDIR-${HOMEDIR}/tmp}
cd $TMPDIR
TMPDIR=`pwd`
cd $PWD

RESULTDIR=${RESULTDIR-${HOMEDIR}/results}
cd $RESULTDIR
RESULTDIR=`pwd`
cd $PWD

TIMEACCUM=${TIMEACCUM-${RESULTDIR}/times}

TESTDIR=${TESTDIR-${HOMEDIR}/testdir}
cd $TESTDIR
TESTDIR=`pwd`
cd $PWD

export BINDIR TMPDIR RESULTDIR PATH TESTDIR TIMEACCUM
#
cat ${BINDIR}/byte.logo # display banner
rm -f ${TIMEACCUM}      # clean out old time accumulation file
echo "kill -9 $$" > ${TMPDIR}/kill_run ; chmod u+x ${TMPDIR}/kill_run
#
arithmetic="arithoh register short int long float double"
system="syscall pipe context1 spawn execl fstime"
misc="C dc hanoi"
dhry="dhry2 dhry2reg" # dhrystone loops
load="shell" # cummulative load tests
index="double dhry2 execl fstime context1 shell"
#
args="" # the accumulator for the bench units to be run
runoption="N"
# generate list of bench programs
for word
do  # do level 1
    case $word
    in
  all)
      ;;
  arithmetic)
      args="$args $arithmetic"
      ;;
  dhry)
      args="$args $dhry"
      ;;
  load)
      args="$args $load"
      ;;
  misc)
      args="$args $misc"
      ;;
  speed)
      args="$args $arithmetic $system"
      ;;
  system)
      args="$args $system"
      ;;
  index)
      args="$args $index"
      ;;
  -q|-Q)
      runoption="Q" #quiet
      shift
      ;;
  -v|-V)
      runoption="V" #verbose
      shift
      ;;
  -d|-D)
      runoption="D" #debug
      shift
      ;;
  *)
      args="$args $word"
      ;;
    esac

done # end do level 1
#if no benchmark units have be specified, do them all 
# the - option of set implies no options; any list following
# becomes the line arguments (replacing any that may exist)
set - $args
if test $# -eq 0  #no arguments specified
   then
   set - $FULL_SUITE
fi

if test "$runoption" = 'D'
then
  set -x
  set -v
fi

date=`date`
TMPTIMES=${TMPDIR}/$$.tmp
LOGFILE=${RESULTDIR}/log
REPORTLOG=${RESULTDIR}/report
#add old log to accumulated log or move it
if test -w ${RESULTDIR}/log
then
   if test -w ${RESULTDIR}/log.accum
   then
      cat ${RESULTDIR}/log >> ${RESULTDIR}/log.accum
      rm ${RESULTDIR}/log
   else
      mv ${RESULTDIR}/log ${RESULTDIR}/log.accum
   fi
fi
echo "|  BYTE UNIX Benchmarks (Version $version)" >>$LOGFILE
echo "|  System --" `$UNAME` >>$LOGFILE
echo "|  Start Benchmark Run: `date`" >>$LOGFILE
echo "|  " `who | wc -l` "interactive users." >>$LOGFILE
#if not specified, do each bench 6 iterations
iter=${iterations-6}
if test $iter -eq 6
then
  longloop="1 2 3 4 5 6"
  shortloop="1 2 3"
else  # generate list of loop numbers
  short=`expr \( $iter + 1 \) / 2`
  longloop=""
  shortloop=""
  while test $iter -gt 0
  do # do level 1
       longloop="$iter $longloop"
       if test $iter -le $short
       then
         shortloop="$iter $shortloop"
       fi
       iter=`expr $iter - 1`
  done # end do level 1
fi #loop list genration
####################################################################
############## the major control loop ##############################
####################################################################
for bench # line argument processing
do # do level 1
    # set some default values
    prog=${BINDIR}/$bench  # the bench name is default program
    paramlist="#"          # a dummy parameter to make anything run
    testdir="${TESTDIR}"   # the directory in which to run the test
    prepcmd=""             # preparation command or script
    parammsg=""
    repeat="$longloop"
    stdout="$LOGFILE"
    stdin=""
    options=""
    logmsg=""
    cleanopt="-l $TMPTIMES"
    bgnumber=""
    trap "${SCRPDIR}/cleanup.sh -L $LOGFILE -a; exit" 1 2 3 15
    echo "" >>$LOGFILE
    ###################### select the bench specific values ##########
    case $bench
    in
  dhry2)
    options=${dhrytime-10}
    logmsg="Dhrystone 2 without register variables"
    ;;

  dhry2reg)
    options=${dhrytime-10}
    logmsg="Dhrystone 2 using register variables"
    ;;

  arithoh|register|short|int|long)
    options=${arithtime-10}
    logmsg="Arithmetic Test (type = $bench)"
    ;;

  float|double)
    options=${arithtime-10}
    logmsg="Arithmetic Test (type = $bench)"
    ;;

  syscall)
    options=${systime-10}
    logmsg="System Call Overhead Test"
    ;;

  context1)
    options=${systime-10}
    logmsg="Pipe-based Context Switching Test"
    ;;

  pipe)   
    options=${systime-10}
    logmsg="Pipe Throughput Test"
    ;;

  spawn)  
    options=${systime-10}
    logmsg="Process Creation Test"
    ;;

  execl)  
    options=${systime-10}
    logmsg="Execl Throughput Test"
    ;;

  fstime) 
    logmsg='Filesystem Throughput Test ($param second test)'
    where=${where-${TMPDIR}}
    cleanopt="-f $TMPTIMES"
    options='$param '"$where"
    paramlist=${seconds-"10 30"}
    parammsg='Test Time: $param secs'
    ;;

  C)  
    logmsg="C Compiler Test"
    prog="looper ${looper-60} cc cctest.c -o ${TMPDIR}/a.out"
    stdout=/dev/null
    repeat="$shortloop"
    cleanopt="-m $TMPTIMES"
    rm -f ${TESTDIR}/cctest.o ${TMPDIR}/a.out
    ;;

  shell)
    logmsg='Shell scripts ($param concurrent)'
    prog="looper ${looper-60} multi.sh"
    repeat="$shortloop"
    stdout=/dev/null
    paramlist=${background-"1 2 4 8 "}
    parammsg='$param concurrent background processes'
    bgnumber='$param'
    cleanopt="-m $TMPTIMES"
    ;;

  dc)  
    logmsg="Dc: sqrt(2) to 99 decimal places"
    prog="looper ${looper-60} \"dc < ${TESTDIR}/dc.dat\""
    stdin=
    stdout=/dev/null
    cleanopt="-m $TMPTIMES"
    ;;

  hanoi)  
    options=${systime-10}
    stdout=/dev/null
    logmsg="Recursion Test--Tower of Hanoi"
    parammsg='$param Disk Problem:'
    ;;

  *)   ${BINDIR}/cleanup.sh -L $LOGFILE -r "run: unknown benchmark \"$bench\" \n Known benchmarks are: \n $FULL_SUITE" -a
    exit 1
    ;;
esac
################################################################
###################### the main task ###########################
###################### run the bench ###########################
################################################################
# each of those variables are now used in a general way
#
    for param in $paramlist
    do   # level 2
      param=`echo $param | sed 's/_/ /g'` # be sure that spaces are used
      eval Lmsg='"'$logmsg'"'
      eval opt='"'$options'"'		 # evaluate any vars in options
      eval prep='"'$prepcmd'"'		 # evaluate any prep command
      eval bg='"'$bgnumber'"'		 # evaluate bgnumber string
      rm -f $TMPTIMES			 # remove any tmp files

      # if the test requires mulitple concurrent processes,
      # prepare the background process string (bgstr)
      # this is just a string of "+"s that will provides a
      # parameter count for a "for" loop
      bgstr=""
      if test "$bg" != ""
      then
	  count=`expr "$bg"`
          while test $count -gt 0
          do
	      bgstr="+ $bgstr"
	      count=`expr $count - 1`
	  done
      fi
      #
      echo "TEST|$Lmsg" >>$TMPTIMES
      echo "FLAVOR|${FLAVOR}" >>$TMPTIMES
      if [ "$runoption" != 'Q' ]
      then
	      echo ""
	      if [ $FLAVOR = "SysV" ]
		 then echo "$Lmsg \c"
		 else echo -n "$Lmsg "
	      fi
	
      fi
      for i in $repeat			 # loop for the specified number
      do  # do depth 3
          if [ "$runoption" != 'D' ]  # level 1
	  then
	    # regular Run - set logfile to go on signal
            trap "${BINDIR}/cleanup.sh -L $LOGFILE -i $i $cleanopt -a; exit" 1 2 3 15
	  else
            trap "exit" 1 2 3 15
	  fi #end level 1
	  # make an attempt to flush buffers
	  sync; sync; sleep 10
	  # display heartbeat
          if [ "$runoption" != 'Q' ]
	  then
	      if [ $FLAVOR = "SysV" ]
	         then echo " $i\c"      # display repeat number
	         else echo -n " $i"
	      fi
	  fi
	  pwd=`pwd`                     # remember where we are
	  cd $testdir                   # move to the test directory
	  if [ "$runoption" = "V" ]
	  then
		echo
		echo "BENCH COMMAND TO BE EXECUTED:"
		echo "$prog $opt"
	  fi

	  # execute any prepratory command string
	  if [ -n "$prep" ]
	  then
	    $prep 2>&1 >>$stdout 
	  fi
	  ############ THE BENCH IS TIMED ##############
          if test "$stdin" = ""  
          then # without redirected stdin
             /usr/bin/time $prog $opt $bgstr 2>>$TMPTIMES >>$stdout
          else # with redirected stdin
             /usr/bin/time $prog $opt $bgstr <$stdin 2>>$TMPTIMES >>$stdout
          fi 
	  ###############################################
	  cd $pwd                    # move back home
          status=$?                  # save the result code
          if test $status != 0 # must have been an error
          then
           if test -f $TMPTIMES # is there an error file ?
           then
                cp $TMPTIMES ${TMPDIR}/save.$bench.$param
            ${SCRPDIR}/cleanup.sh -L $LOGFILE -i $i $cleanopt -r \
	       "run: bench=$bench param=$param fatalstatus=$status" -a
           else
            ${SCRPDIR}/cleanup.sh -L $LOGFILE -r \
	       "run: bench=$bench param=$param fatalstatus=$status" -a
           fi 
           exit # leave the script if there are errors
          fi # end  level 1
      done # end do depth 3 - repeat of bench
      if [ "$runoption" != 'D' ]
      then
        ${SCRPDIR}/cleanup.sh -L $LOGFILE $cleanopt # finalize this bench
						 # with these options
						 # & calculate results
      fi
    done # end do depth 2 - end of all options for this bench

    ########### some specific cleanup routines ##############
    case $bench
    in
      C)
      rm -f ${TESTDIR}/cctest.o ${TMPDIR}/a.out
      ;;

      fstime)
      sync; sleep 20
      ;;
    esac
    if [ "$runoption" != 'Q' ]
    then
       echo ""
    fi
done # end do level 1  - all benchmarks requested
########## a few last items for the end ##############
echo "" >>$LOGFILE
echo "End Benchmark Run: `date`" >>$LOGFILE
echo " " `who | wc -l` "interactive users." >>$LOGFILE
# create the report
${SCRPDIR}/report.sh $LOGFILE > $REPORTLOG 
${SCRPDIR}/index.sh ${SCRPDIR}/index.base $LOGFILE >> $REPORTLOG
if [ "$runoption" != 'Q' ]
then
  echo ""
  echo "=============================================================="
  cat $REPORTLOG
fi

exit
########################### Amen ######################
