#! /bin/sh
#
###############################################################################
#
# tstinter-run Version 1.0 based on:
#
#	 UnixBench - Release 4.1, based on:
#
#		The BYTE UNIX Benchmarks - Release 3 Module: Ru SID: 3.11 5/15/91 19:30:14
#
# Original Byte benchmarks written by:
# 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
# 4/4/92		Jon Tombs <jon@robots.ox.ac.uk> fixed for GNU time to look like
#		BSD (don't know the format of syssV!)
# 12/95		- Massive changes for portability, speed, and more meaningful index
#		DCN David C Niemi <niemi@tux.org>
# 1997.06.20	DCN Fixed overflow condition in fstime.c on fast machines
# 1997.08.24	DCN Modified "system", replaced double with
#			whetstone-double in "index"
# 1997.09.10	DCN Added perlbench as an Exhibition benchmark
# 1997.09.23	DCN Added rgooch's select as an Exhibition benchmark
# 1999.07.28	DCN "select" not compiled or run by default, because it
#			does not compile on many platforms.	 PerlBench also
#			not run by default.
#
# 2003-10-26 mhf@linuxmail.org cleaned out functionality 
# not used by interactivity tests
#
###############################################################################
# We use only system level functions
#
ID="tstinter-run";
version="1.0"
#
#######################################################################
FULL_SUITE="syscall pipe context1 spawn execl fstime fsbuffer fsdisk shell C"
#
# General purpose interactivity tests 
#		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
#
#
# begin jon mods
CC=gcc
export CC
#end Jon mods
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 "time"
	Fcount=`/usr/bin/time date 2>&1 | wc -l | sed 's/ //g'`
	case "$Fcount" in
	2) FLAVOR=BSD ;;
	3) FLAVOR=Linux ;;
	*) echo "Flavor of UNIX is not known."
		 echo "Please define FLAVOR in Run script"
		 exit;;
	esac
fi
export FLAVOR
TIME="				%e real				 %U user				%S sys"
export TIME

if [ $FLAVOR = "BSD" ]; then
	UNAME="hostname"
else
	UNAME="uname -a"
fi


# establish full paths to directories
cd $TSTINTERD
BINDIR=${BINDIR-${TSTINTERD}/pgms}
cd $BINDIR
BINDIR=`pwd`
cd $TSTINTERD
# let's extend the path to this directory
PATH="${PATH}:${BINDIR}"

SCRPDIR=${SCRPDIR-${TSTINTERD}/pgms}
cd $SCRPDIR
SCRPDIR=`pwd`
cd $TSTINTERD

TMPDIR=${TSTINTERD}/tmp
cd $TMPDIR
TMPDIR=`pwd`
cd $TSTINTERD

RESULTDIR=${RESULTDIR-${TSTINTERD}/results}
cd $RESULTDIR
RESULTDIR=`pwd`
cd $TSTINTERD



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

TESTDIR=${TESTDIR-${TSTINTERD}/testdir}
cd $TESTDIR
TESTDIR=`pwd`
cd $TSTINTERD

export BINDIR TMPDIR RESULTDIR PATH TESTDIR TIMEACCUM

# check that the required files are in the proper places
if make check; then
				:
else
	make all
fi


#
rm -f ${TIMEACCUM}			# clean out old time accumulation file
echo "kill -9 $$" > ${TMPDIR}/kill_run ; chmod u+x ${TMPDIR}/kill_run
#
arithmetic=""
oldsystem="execl fstime fsbuffer fsdisk pipe context1 spawn syscall"
system="$oldsystem shell"
misc="C"
load="shell" # cummulative load tests
index="$system"

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 | dhrystone)
		args="$args dhry2reg"
		;;
	whets | whetstone)
		args="$args whetstone-double"
		;;
	load)
		args="$args $load"
		;;
	misc)
		args="$args $misc"
		;;
	speed)
		args="$args $arithmetic $system"
		;;
	oldsystem)
		args="$args $oldsystem"
		;;
	system)
		args="$args $system"
		;;
	index)
		args="$args $index"
		JUSTINDEX=yes
		;;
	-[Qq])
		runoption=Q #quiet
		shift
		;;
	-[Vv])
		runoption=V #verbose
		shift
		;;
	-[Dd])
		runoption=D #debug
		shift
		;;
	-[0-9]*)
		iterations=`echo $word | tr -d '-'`
		shift
		;;
	*)
		args="$args $word"
		;;
	esac
done # end do level 1

#if no test 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 [ $# -eq 0 ]; then	 #no arguments specified
	set - $FULL_SUITE
fi

if [ "$runoption" = 'D' ]; then
	set -x
	set -v
fi

date=`date`
TMPTIMES=${TMPDIR}/$$.tmp
LOGFILE=/dev/null
REPORTLOG=${RESULTDIR}/report
#add old log to accumulated log or move it
if [ -w ${RESULTDIR}/log ]; then
	if [ -w ${RESULTDIR}/log.accum ]; then
		cat ${RESULTDIR}/log >> ${RESULTDIR}/log.accum
		rm ${RESULTDIR}/log
	else
		mv ${RESULTDIR}/log ${RESULTDIR}/log.accum
	fi
fi
#if not specified, do each bench 6 iterations
iter=${iterations-10}
# generate list of loop numbers
short=`expr \( $iter + 1 \) / 3`
if [ "$short" -lt 1 ]; then
	short=1
fi
longloop=
shortloop=
while [ $iter -gt 0 ]; do
	# do level 1
	longloop="$iter $longloop"
	if [ "$iter" -le "$short" ]; then
		shortloop="$iter $shortloop"
	fi
	iter=`expr $iter - 1`
done # end do level 1


####################################################################
############## the major control loop ##############################
####################################################################
for bench
do				# line argument processing
	# 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=$shortloop
	stdout=/dev/null
	stdin=
	options=
	logmsg=
	cleanopt="-l $TMPTIMES"
	bgnumber=
	###################### select the bench specific values ##########
	case $bench in

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

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

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

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

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

	fstime) 
		logmsg='Filesystem Throughput 1024 bufsize 2000 maxblocks'
		where=${where-${TMPDIR}}
		cleanopt="-f $TMPTIMES"
		options='$param '"$where"
		paramlist=${seconds:-30}
		parammsg='Test Time: $param secs'
		;;

	fsbuffer) 
		logmsg='Filesystem Throughput 256 bufsize 500 maxblocks'
		where=${where-${TMPDIR}}
		cleanopt="-f $TMPTIMES"
		options='$param '"$where"
		paramlist=${seconds:-30}
		parammsg='Test Time: $param secs'
		;;

	fsdisk) 
		logmsg='Filesystem Throughput 4096 bufsize 8000 maxblocks'
		where=${where-${TMPDIR}}
		cleanopt="-f $TMPTIMES"
		options='$param '"$where"
		paramlist=${seconds:-30}
		parammsg='Test Time: $param secs'
		;;

	shell)
		logmsg='Shell Scripts ($param concurrent)'
		prog="looper ${looper-60} multi.sh"
		stdout=/dev/null
		if [ -n "${JUSTINDEX-}" ]; then
			paramlist=${background-"8 "}
		else
			paramlist=${background-"1 8 16 "}
		fi
		parammsg='$param concurrent background processes'
		bgnumber='$param'
		cleanopt="-m $TMPTIMES"
		;;


	##########################
	## Non-Index Benchmarks ##
	##########################

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

	*)
"run: unknown test \"$bench\" \n Known tests 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

			echo "TEST|$Lmsg" >>$TMPTIMES
			echo "FLAVOR|${FLAVOR}" >>$TMPTIMES
			if [ "$runoption" != 'Q' ]; then
				echo ""
				echo -n "$Lmsg "
			fi
			for i in $repeat			 # loop for the specified number
			do	# do depth 3
				# make an attempt to flush buffers
				sync; sleep 1; sync; sleep 2
				# display heartbeat
				pwd=`pwd`											# remember where we are
				cd $testdir										# move to the test directory

				# execute any prepratory command string
				if [ -n "$prep" ]; then
					$prep 2>&1 >>$stdout 
				fi
		############ THE BENCH IS TIMED ##############
				if [ -z "${stdin-}" ]; then
		# without redirected stdin
					/usr/bin/time $prog $opt $bg 2>>$TMPTIMES >>$stdout
				else
		# with redirected stdin
					/usr/bin/time $prog $opt $bg <$stdin 2>>$TMPTIMES >>$stdout
				fi 
		###############################################
				cd $pwd										 # move back home
			done # end do depth 3 - repeat of bench
		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 ${TESTDIR}/a.out
			;;

			fstime | fsbuffer | fsdisk)
			sync; sleep 1; sync; sleep 1
			;;
		esac
done # end do level 1	 - all tests requested
exit 0
########################### Amen ######################
