#!/bin/ksh
#
#ident	"@(#)hds.stf	1.15	04/12/08 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# THIS IS THE HARD DISK 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.
#########################################################################
#modify by tony, for 64-bit and 32-bit test, on 64-bit platform, each ruan half RUNTIME.

# variables
log=/tmp/stress_log.hds
starttimes_file=/tmp/start_times.hds
HDS_BIN=`isainfo -b`


hds_run64()
{
	echo "Ending $HDS_BIN bit test..."
	HDS_BIN=32
	echo "Starting $HDS_BIN bit test..."

	sleep $RUNTIME &
	sleepid=$!

	if [ "$mode" == "x" ]
	then
		hds/bin/runtest-x.hds $HDS_BIN $sleepid
	else
		hds/bin/runtest.hds $HDS_BIN $sleepid
	fi

	
	if [ -f /tmp/mdisk ]
	then
		rm /tmp/mdisk
	fi

	while :
	do
	        pid=$(/usr/bin/pgrep disktest)
		
	        if [ -z "$pid" ] && [ ! -d /proc/$sleepid ]
	        then
	                if [ $? -ne 0 ]
	                then
	                        echo "!ERROR during storage test"; exit 1
			else
				echo "Ending $HDS_BIN bit test..."
	                	echo "!PASS: Storage test completed"; exit 0	
	                fi
	        fi
	done
	
}

while getopts x name
do
	case $name in
		x)	mode=x;;
	esac
done

clean_up()
{
	echo "hds Ending!"
	if [ -n `/usr/bin/pgrep disktest` ]
	then
		echo `/usr/bin/pgrep disktest` | while read kill_id
		do
			kill -9 $kill_id	  
		done
	fi
}

### MAIN ###

# set trap
trap 'clean_up; exit' 17

# Remove any old starttimes file
if [ -f $starttimes_file ]
then
	rm $starttimes_file
fi

if [ -f /tmp/mdisk ]
then
	rm /tmp/mdisk
fi

if [ $HDS_BIN = 64 ]
then
	RUNTIME=`expr $RUNTIME / 2`
fi


echo "Starting $HDS_BIN bit test..."

sleep $RUNTIME &
sleepid=$!

if [ "$mode" == "x" ]
then
	hds/bin/runtest-x.hds $HDS_BIN $sleepid
else
	hds/bin/runtest.hds $HDS_BIN $sleepid
fi

wait

if [ $HDS_BIN = 32 ]
then
	if [ -f /tmp/mdisk ]
	then
		rm /tmp/mdisk
	fi
fi

while :
do
        pid=$(/usr/bin/pgrep disktest)

        if [ -z "$pid" ] && [ ! -d /proc/$sleepid ]
        then
                if [ $? -ne 0 ]
                then
                        echo "!ERROR during storage test"; exit 1
		else
			if [ $HDS_BIN = 64 ]
			then
				hds_run64	
			else	
				echo "Ending $HDS_BIN bit test..."
                		echo "!PASS: Storage test completed"; exit 0
			fi
                fi
        fi
done


