#!/bin/bash
#
# Copyright 2003 Michael Frank, email: mhf@linuxmail.org
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# Version 2 as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA	 02111-1307, USA.
# http://www.gnu.org/copyleft/gpl.html
#
# ---------------------------------------------------------------------------
#
# Version: 1.0-beta
#
# ---------------------------------------------------------------------------
#
# See the file README-TI for more info
#
#*****************************************************************
#
# Optional configuration: 
# 
# where to find the kerneldir for kernel compile
TESTKERNEL=/usr/src/linuxtest
# 
# set xterm	 options
#
XTOPTS="-fn *-fixed-*-*-normal-*-8-*-*-*-*-*-iso8859-1 +sb"
#
# xterm geometries
#
XG20="-geometry 20x20"
XG30="-geometry 30x20"
XG40="-geometry 40x20"
XG50="-geometry 50x20"
XG80="-geometry 80x20"
#
# Edit TEMP to a user writeable temporary directory if not set
#
if [ "$TEMP" = "" ]; then
	TEMP=/tmp
fi
#
# Edit FILE to another test file prefix as required
#
FILE=_x_y_z_
#
#*****************************************************************
#
# Internal use
#
TEMPFILER=$TEMP/$FILE.r
TEMPFILEW=$TEMP/$FILE.w
HPRIO="-p user.emerg"
#
# For further invocation - supports symbolic links referencing full path
#

if [ "$TSTINTERD" = "" ]; then
  if [ -h $0 ]; then
    TSTINTERD=`readlink $0`
  else 
  	if [ "${0:0:1}" = "/" ]; then
    	TSTINTERD=$0
    else
			TSTINTERD=`pwd`/$0    	
  	fi
  fi  
  TSTINTERD=${TSTINTERD%/*}
	export TSTINTERD
	cd $TSTINTERD
	make all
fi
TSTU=$TSTINTERD/ti-run
TSTI=$TSTINTERD/ti
#
# print statistics
#


printstats()
{
	echo -e "\n""$1-$2: $pass, `date "+%R:%S %D"`"
}
#
# Unixbench invoation
#
unixbench()
{
	invok=$2
	((pass += 1))#
	printstats $1 Pass: $invok
	logger "$1-$invok Pass: $pass"
  case $1 in
  syscall | pipe | context1 | spawn | execl |\
  	fstime | fsbuffer | fsdisk | shell | C)
      $TSTU -3 $1 > /dev/null;;
  sp) $TSTU -3 syscall pipe > /dev/null;;
  cs) $TSTU -3 context1 spawn > /dev/null;;
  ef) $TSTU -3 execl fsbuffer > /dev/null;;
  td) $TSTU -3 fstime fsdisk > /dev/null;;
  sc) $TSTU -3 shell C > /dev/null;;
  spcse) $TSTU -3 syscall pipe context1 spawn execl> /dev/null;;
  ftdsc) $TSTU -3 fstime fsdisk fsbuffer shell C > /dev/null;;
  *) echo bad test $1
  exit 1
  esac
}

dokillall()
{
  killall ti make gcc gcc295 stress dd sustrun looper fstime fsdisk fsbuffer \
    multi.sh sort time syscall context1 execl spawn pipe vmstat
}



#*****************************************************************
#
# Execute function
#
while [ "$1" != "" ]; do
	case $1 in
#
# show dmesg
#
_dmesg)
		while :; do dmesg -s2048; usleep 1000000;	 done
		;;
dmesg)
		if [ "$DISPLAY" != "" ]; then
			xterm -T "dmesg" $XG80 $XTOPTS -e $TSTI _dmesg&
		else
			openvt -- $TSTI _dmesg
		fi
		;;
#
# follow messages - must be user readable
#
_msg)	
		tail -f /var/log/messages
    ;;
msg)
		if [ "$DISPLAY" != "" ]; then
			xterm -T "msg" $XG80 $XTOPTS -e $TSTI _msg
		else
			openvt -- $TSTI _msg
		fi
		;;
#
# vmstat
#
vm)
		if [ "$DISPLAY" != "" ]; then
			xterm -T "vmstat" $XG80 $XTOPTS -e vmstat 1&
		else
			openvt -- vmstat 1
		fi
		;;
#
# loadavg
#
_la)
		while :; do cat /proc/loadavg; sleep 1;	 done
		;;
la)
		if [ "$DISPLAY" != "" ]; then
			xterm -T "loadavg" $XG30 $XTOPTS -e $TSTI _la&
		else
			openvt -- sust _la
		fi
		;;
#
# list processes
#
_ps)
		while :; do ps -A; usleep 1000000;	done
		;;
ps)
		if [ "$DISPLAY" != "" ]; then
			xterm -T "ps" $XG40 $XTOPTS -e $TSTI _ps&
		else
			openvt -- sust _ps
		fi
		;;
# Loop timed dd read of $2 4K blocks from $TEMPFILER$3
# $TEMPFILE$3 is created first
#
_ddr) 
		if [ "$3" = "" ]; then
			count=$DEFCOUNT
		else
			count=$3
		fi
		dd if=/dev/zero of=$TEMPFILER$2 bs=4096 count=$count
		while (( 1 )); do
			time dd if=$TEMPFILER$2 of=/dev/null bs=4096 count=$count &> /dev/null
		done
		;;
#
# Read as above in a xterm
#
ddr) 
			((!$2 || $2 > 10)) && echo "ti ddr $2 bad loop count: range is 1-10" && exit
			n=$2
      while ((n)); do
				((n -= 1))
        if [ "$DISPLAY" != "" ]; then
          xterm -T "ddr" $XG20 $XTOPTS -e $TSTI _ddr $n $3 &
        else
          openvt -- $TSTI _ddr $n $3
        fi
      done
			shift 2
			;;
#
# Loop timed dd write of $2 4096 blocks to file $3
#
_ddw) 
		if [ "$3" = "" ]; then
			count=$DEFCOUNT
		else
			count=$3
		fi
		while (( 1 )); do
			time dd if=/dev/zero of=$TEMPFILEW$2 bs=4096 count=$count
		done
		;;
#
# Write as above in a xterm
#
ddw) 
			((!$2 || $2 > 10)) && echo "ti ddw $2 bad loop count: range is 1-10" && exit
			n=$2
      while ((n)); do
				((n -= 1))
        if [ "$DISPLAY" != "" ]; then
          xterm -T "ddw" $XG20 $XTOPTS -e $TSTI _ddw $n $3 &
        else
          openvt -- $TSTI _ddw $n $3
        fi
			done
			shift 2
			;;
#							
# Loop vmstat -n with a short delay
#
_vmf)
		i=0
		while (( 1 )); do
			((i += 1)); echo "$i "
			vmstat -n
			usleep 30000
		done
		;;
#
# Start a xterm printing vmstat 
#
vmf) 
			if [ "$DISPLAY" != "" ]; then
				xterm -T "vmstat fast" $XG80 $XTOPTS -e $TSTI _vmf "$2" $3 &
			else
				openvt -- $TSTI _vmf
			fi
			;;
#							
# Loop ls -l $TEMPFILE with a delay
#
_lsf)
		i=0
		while (( 1 )); do
			((i += 1)); echo "$i "
			ls -l $TEMPFILE*
			sleep 1
		done
		;;
lsf) 
			if [ "$DISPLAY" != "" ]; then
				xterm -T "ls dd files" $XG80 $XTOPTS -e $TSTI _lsf "$2" $3 &
			else
				openvt -- $TSTI _lsf
			fi
			;;
#
# compile linux test kernel
#
_compk) 
		cd $TESTKERNEL
		while :; do
			(( pass += 1))
			logger "compk: Pass $pass"
			make clean
			make dep
			make -j2 bzImage
			if [ "$?" != "0" ] ; then 
				logger $HPRIO "compk: Pass $pass error bzimage compile failed" 
				while :; do sleep 3600; done
			else
      	logger "compk: Pass $pass bzimage done"
			fi
			make -j2 modules
			if [ "$?" != "0" ] ; then 
				logger $HPRIO "compk: Pass $pass error modules compile failed" 
				while :; do sleep 3600; done
			else
      	logger "compk: Pass $pass modules done"
			fi
      sleep 1
		done
		;;
compk)
		if [ "$DISPLAY" != "" ]; then
			xterm -T "compk" $XG50 $XTOPTS	-e $TSTI _compk&
		else
			openvt -- $TSTI _compk
		fi
		[ "$2" != "" ] && sleep 3
		;;
#
# Run single unixbench test
#
syscall | pipe | context1 | spawn | execl | \
	fstime | fsbuffer | fsdisk | shell | C | \
	sp | cs | ef | td | sc | spcse | ftdsc)
		while :; do
			unixbench $*
		done
		;;
#
# Run 2 unix bench tests
#
_ub2) 
		$TSTI spcse 1& $TSTI ftdsc 1&
		while :; do sleep 3600; done
		;;
ub2) 
		if [ "$DISPLAY" != "" ]; then
			xterm -T "ub2" $XG40 $XTOPTS -e $TSTI _ub2&
		else
			openvt -- $TSTI _ub2	 
		fi
		[ "$2" != "" ] && sleep 3
		;;
#
# Run 5 unix bench tests
#
_ub5)
		$TSTI sp 1& $TSTI cs 1& $TSTI ef 1& $TSTI td 1& $TSTI sc 1&
		while :; do sleep 3600; done
		;;
ub5) 
		if [ "$DISPLAY" != "" ]; then
			xterm -T "ub5" $XG40 $XTOPTS -e $TSTI _ub5&
		else
			openvt -- $TSTI _ub5
		fi
		[ "$2" != "" ] && sleep 3
		;;
#
# Run 10 unix bench tests
#
_ub10)
		$TSTI syscall 1& $TSTI pipe 1& $TSTI context1 1& $TSTI spawn 1& $TSTI execl 1& \
		$TSTI fstime 1& $TSTI fsbuffer 1& $TSTI fsdisk 1& $TSTI shell 1& $TSTI C 1& 
		while :; do sleep 3600; done
		;;
ub10) 
		if [ "$DISPLAY" != "" ]; then
			xterm -T "ub10" $XG40 $XTOPTS -e $TSTI _ub10&
		else
			openvt -- $TSTI _ub10
		fi
		[ "$2" != "" ] && sleep 3
		;;
#
# Run 17 unix bench tests
#
ub17)
		$TSTI ub2; $TSTI ub5; $TSTI ub10
		;;
#
# show logs
#
logs) cat /tmp/resumed.log /tmp/ST*.log ;;
#
# Prepare for testing
#
prepare)
		make
		;;
#
# combined status
#
stat)
		$TSTI msg&
		$TSTI dmesg&
		$TSTI vm&
		sleep 2
		$TSTI la&
		$TSTI ps&
		[ "$2" != "" ] && sleep 3
		;;
0 | stop) dokillall;;

1)  $TSTI stat compk ub17 ddw 4 5000 ;;
    
#
# Default
#
*) 	echo $TSTI bad function $*
		exit $?
		;;
	esac
	shift
done
#
# end of script
#
