#!/bin/ksh
#
#ident	"@(#)memory.stf	1.9	04/12/15 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Virtual Memory Test
#

# variables
#maxwarn=50      # max number of nonfatal warnings to tolerate.

# test level
tlevel=$(cat /tmp/level.dat)

# available virtual memory size in MB / 2
vm=`swap -s | awk '{print $11}' | cut -f1 -d'k'`
vm=`expr $vm / 2048`

# total physical memory / 2
pm=`/usr/sbin/prtconf | grep Memory | awk '{print $3}'`
half_pm=`expr $pm / 2`
#((half_pm=$pm / 2))
#((test_mem=$pm + $half_pm))
test_mem="$pm"

#if [ "$vm" -lt "$test_mem" ]
#then
#	test_mem=$vm
#fi

[ $tlevel = L1 ] && test_mem="$half_pm"


###MAIN###

# set trap
trap 'exit' 17


sleep $RUNTIME &
sleepid=$!

echo "Starting Level $tlevel Memory test..."
##
echo "Physical memory size = $pm"
#echo "Size of memory to be tested = $vm"
echo "Size of memory to be tested = $test_mem"
if [ $tlevel = L2 ]
then
	echo "Reserving 64Mbytes of memory."
fi

while [ -d /proc/$sleepid ]
do
# start test based on test level


	starttime=`../../prgs/time_sec`
	if [ $tlevel = L2 ]
	then
		bin/vmemtest -f -v -o reserve=64 -o amount=$test_mem 
	else
		bin/vmemtest -f -v -o amount=$test_mem
	fi

	if [ $? -ne 0 ]
	then
		echo "!ERROR: Problem(s) during memory test."
		exit 1
	fi
	
	endtime=`../../prgs/time_sec`

        ltime=`expr $endtime - $starttime`
        if [ $ltime -gt 300 ]
        then
               	echo LOOPTIME: $ltime
	fi

done
echo "!PASS: Memory test completed"
