#/bin/ksh
#
#ident	"@(#)mpflip.sh	1.11	04/12/08 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Microprocessor Stress Test
# Repeatedly brings MP's on/off line
# Ciaran Davies June 1999
# Modified by Andrew McCarthy September 1999

DEFAULTPAUSE=2
maxwarn=80
warnings=0

Cleanup()
{
printf "\nResetting processors...\n\n"

count=1

while [ $count -lt $numbermps ]
do
	psradm -n $count
	count=`expr $count + 1`
done
exit 0

}

trap Cleanup 2 15 19 
echo $1
if [ $# -eq 1 ]; then	

	pausetime=$1

	if [ $pausetime -gt 0  ]; then
		printf "\nUsing inter-flip pause of $pausetime seconds.\n"
	else
		printf "\n\nUsage: $0 [pause]\n\n"
		exit 1
	fi

else

	printf "\nUsing Default pause of $DEFAULTPAUSE seconds.\n"
	pausetime=$DEFAULTPAUSE
fi

echo Running time: $RUNTIME seconds
printf "\n\nStarting to loop thru processors...\n" 

numberonline=0
numbermps=0

mpstatus=`psrinfo -s $numbermps`

while [ $? -eq 0 ]
do
	if [ $mpstatus -eq 1 ]; then
		numberonline=`expr $numberonline + 1`
	fi

	numbermps=`expr $numbermps + 1`

	mpstatus=`psrinfo -s $numbermps 2>/dev/null`

done



printf "\n\nFound $numberonline processor(s) on-line.\n"
printf "Found $numbermps processor(s) in the system.\n\n"


if [ $numbermps -eq 1 ]; then
	printf "\n!PASS: Only one processor installed.\n"
	exit 1
else
	sleep $RUNTIME &
	sleepid=$!
	while [ 1 ]
	do

		# Count to end of MP list				
		count=1

		while [ $count -ne $numbermps ]
		do
			#printf "Taking processor #$count off-line:"
			psradm -f $count

			if [ $? -ne 0 ]; then

				printf "\n!WARNING:Could not take off-line.\n"
				warnings=`expr $warnings + 1`
			fi


			if [ "$warnings" -ge "$maxwarn" ]
			then
				printf "\n!ERROR: Received $maxwarn !WARNING messages. Terminating test.\n"
				exit 1
			fi

			sleep $pausetime 

			if [ ! -d /proc/$sleepid ] ; then
				echo "!PASS: no problems in mpflip"
				Cleanup
			fi

			count=`expr $count + 1`
		done


		# Count backwards turning processors on

		count=`expr $numbermps - 1` 

		while [ $count -ne 0 ]
		do
			#printf "Putting processor #$count on-line:"
			psradm -n $count

			if [ $? -ne 0 ]; then
				printf "\n!WARNING: Could not put on-line.\n"
				warnings=`expr $warnings + 1`
			fi
			

			if [ "$warnings" -ge "$maxwarn" ]
			then
				printf "\n!ERROR: Received $maxwarn !WARNING messages. Terminating test.\n"
				exit 1
			fi

			sleep $pausetime 

			if [ ! -d /proc/$sleepid ] ; then
				echo "!PASS: no problems in mpflip"
				Cleanup
			fi

			count=`expr $count - 1`
		done
	done			
fi	
