#!/bin/ksh
#
#ident	"@(#)bonnie.stf	1.16	05/05/25 SMI"
#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# This is the Fs_stress test module.
#
#########################################################################
# NOTES ON OPERATION AND ERROR HANDLING:
# This script is called by staf.eng, which is the main test harness.
# Standard output gets recorded in the test module log file by staf.eng.
#
# Variable maxwarn is checked by staf.eng in the "chk_err()" monitoring process,# by grep'ing the log file of this test module.
# If maxwarn is exceeded, the staf.eng script will kill this test module.
# There are two formats for the error echos to the log files;
#
# 1.   echo "!ERROR: blah blah foo bar";
#      which will terminate the test.  If the test harness detects
#      a string match with "!ERROR", then all testing terminates.
#
# 2.   echo "!WARNING: blah blah foo bar";
#       The above format "!WARNING:" will make the log file entry,
#       but all testing will continue, no termination of tests,
#       UNLESS maxwarnings variable is exceeded.
#
# DANGER!!:  Even if you do not intend to use WARNING messages, variable
# maxwarn *MUST* be defined and the "maxwarnings=" line *MUST* be
# echo'ed to the logfile.  This syntax is mandatory.
#########################################################################


# variables
prgdir=../../prgs	# program directory
maxwarn=50		# max number of nonfatal warnings to tolerate.
size=$1			# file size.
iter=$2			# number of iterations.
sdir=$3			# Scratch directory to use.
hname=`hostname`
results=/opt/SUNWhcts/results/$hname


# functions

clean_up()
{
	echo "Bonnie Ending!"
	if [ -f /tmp/drives ]
	then
		rm -f /tmp/drives
	fi
}

### MAIN ###


if [ -z "$size" ]
then
	size=200
elif [ $size -le 0 ]
then
	echo "$0 error"
	echo "Wrong option:"
	echo "Usage: $0 size iteration source_directory"
	exit 1
fi

if [ -z "$sdir" ]
then
	sdir=`$prgdir/sliceinfo | awk '{print $1}' | sed -n '1p'`
	if [ "$sdir" == "reserved" ]
	then
		sdir=`$prgdir/sliceinfo | awk '{print $2}' | sed -n '2p'`
	else
		sdir=`$prgdir/sliceinfo | awk '{print $2}' | sed -n '3p'`
	fi
	mkdir -p $sdir/bonnie.tmp
	sdir=$sdir/bonnie.tmp
fi

if [ -z "$iter" ]
then
	iter=5
elif [ $iter -le 0 ]
then
	echo "$0 error"
	echo "Wrong options:"
	echo "Usage: $0 size iter sdir"
	exit 1
fi

# set trap
trap 'clean_up; exit' 17

# SET LOGFILE FLAG FOR MAX WARNING COUNT
# This is checked by staf.eng, and if exceeded, staf.eng will kill this module.
# ( This only makes sense if this script echos warning messages, but
#   inclusion of this variable in the log file is *MANDATORY* in all cases. )
#echo "maxwarnings=${maxwarn}"


echo "Starting Bonnie..."

if [ -f $results/bonnie.results ]
then
	rm -f $results/bonnie.results
fi

BONDIR=$sdir

echo "Bonnie scratch directory is: $BONDIR"
cd bonnie/bin
./bonnie.sh -s $size -i $iter -d $BONDIR &
bonid=$!

wait $bonid

cp $BONDIR/bonnie.results $results
rm -rf $sdir

clean_up








