#!/bin/ksh
#
#set -x
#
##############################################################################
#                                                                            #
#  RCS Filename : measure,v
#  RCS Date     : 1996/06/21 15:50:30
#  RCS Revision : 1.5
#  RCS Author   : lamberts
#  RCS State    : V2_0_B
#                                                                            #
#  Authors: Stefan Lamberts                                                  #
#                                                                            #
##############################################################################

USAGE="Usage: $0"'\n
\t[usecompute]\t# Use existing .compute file\n
\t<size>\t\t# -sz flag for application\n
<arguments for meas>'


######################################################################
# Defaults
######################################################################

RF="RESULTS"
PFSDHOST=""
COMPUTE=".compute"
PFSDHOSTFILE=".pfsdhost"
MACHINES=".machines"
TESTFILE="/tmp/pfslibtest"
MEASRES="/tmp/pfslibresults"

MEAS="./meas"
GETHN="./gethn"


######################################################################
# Check command line and file existence
######################################################################

if [ $# -lt 1 ]
then	echo $USAGE
	exit 1
fi

if [ $1 = "usecompute" ]
then	USECOMPUTE=T
	shift
	if [ ! -r $COMPUTE ]
	then	echo "$0: Cannot read file '$COMPUTE'"
		exit 1;
	fi
	if [ ! -r $PFSDHOSTFILE ]
	then	echo "$0: Cannot read file '$PFSDHOSTFILE'"
		exit 1
	fi
	if [ $# -lt 1 ]
	then	echo $USAGE
		exit 1
	fi
else	USECOMPUTE=F
	if [ ! -r $MACHINES ]
	then	echo "$0: Cannot read file '$MACHINES'"
		exit 1
	fi
fi

SIZE=$1
shift

######################################################################
# Make a compute file with a certain size and a pfsdhost file using
# rup and the $MACHINES file
######################################################################

mkcomp ()
{
	if [ $# -eq 1 ]
	then	MAXLN="NR <= $1"
	else	MAXLN=""
	fi

	##############################################################
	# Remove $COMPUTE files and $PFSHOSTFILE
	##############################################################
	rm -f $COMPUTE.first $PFSDHOSTFILE $COMPUTE.rest $COMPUTE

	##############################################################
	# get machines with lowest load
	##############################################################
	rup -l `cat $MACHINES` |\
	awk '	BEGIN{
			first = "UNSET"
			second = "UNSET"
		}
		/^[ 	]*hp[0-9][ 	]/{
			if (first == "UNSET")
			{
				first = "SET"
				print > "'$COMPUTE'.first"
			}
			else if (second == "UNSET")
			{
				second = "SET"
				print > "'$PFSDHOSTFILE'"
			}
			else
				print >> "'$COMPUTE'.rest"
		}
		/^[ 	]*hp[0-9][a-j][ 	]/{
			print >> "'$COMPUTE'.rest";
		}'

	##############################################################
	# make a .compute file out of it
	##############################################################
	awk 	"$MAXLN"'{
			printf("%d\t%s:$PWD\t# %s\n",
				NR-1,$1,substr($0,index($0,"load average:")))
		}' $COMPUTE.first $COMPUTE.rest > $COMPUTE
}

if [ "$USECOMPUTE" = "F" ]
then	mkcomp $SIZE
fi

######################################################################
# Set the host of the pfsd
######################################################################

PFSDHOST=`awk '{print $1}' $PFSDHOSTFILE`

######################################################################
# Get the number of differnt machines
######################################################################
MCNT=`$GETHN $COMPUTE $SIZE | sort | uniq | wc -l`

######################################################################
# Start pfsd and wait for it
######################################################################
rm -f $HOME/iodlog.*

rsh $PFSDHOST $PFSLIBPATH/bin/pfsd `$GETHN $COMPUTE $SIZE` &

sleep 10

until pfsdstat $PFSDHOST > /dev/null 2>&1
do	sleep 10
done

######################################################################
# Build the command line for the measurement
######################################################################

CMDLN="$MEAS -sz $SIZE -pn $COMPUTE -d $MCNT -h $PFSDHOST -f $TESTFILE -r $MEASRES $*"

######################################################################
# Write information about this measurement to the $RF file
######################################################################

echo "##############################################################################" >> $RF
echo "#" >> $RF
echo "#" `date` >> $RF
echo "#" >> $RF
echo "# PFSLib Vesion tag: "`cat $PFSLIBPATH/.rcs.state.tag` >> $RF
echo "#" >> $RF
echo "# CPP Defines: "`cat $PFSLIBPATH/.cppdefines` >> $RF
echo "#" >> $RF
echo "# pfsd on "`cat $PFSDHOSTFILE` >> $RF
echo "#" >> $RF
echo "# $COMPUTE file" >> $RF

awk '$1 < '$SIZE'{print "#" $0}' $COMPUTE >> $RF

echo "#" >> $RF
echo "# Commandline" >> $RF
echo "#" >> $RF

echo "# "$CMDLN >> $RF

######################################################################
# Remove log files of application processes
######################################################################

rm -f *.log

######################################################################
# Run the measurement
######################################################################

$CMDLN

######################################################################
# Append the results to the $RF file
######################################################################

pfscat $PFSDHOST $MEASRES >> $RF

######################################################################
# Remove files generated by the measurement
######################################################################

pfsrm $PFSDHOST $TESTFILE
pfsrm $PFSDHOST $MEASRES

######################################################################
# Tell the pfsd to exit and wait for its exit
######################################################################

pfsdexit $PFSDHOST

wait

exit 0
