#!/bin/sh
# PCP QA Test No. 523
# more pmie max_* and min_* tests
#
# Copyright (c) 2013 Ken McDonell.  All Rights Reserved.
#

# get standard filters
. ./common.product
. ./common.filter
. ./common.check

seq=`basename $0`

rm -f $seq.out
if [ $PCP_VER -lt 30900 ]
then
    ln $seq.out.1 $seq.out || exit 1
else
    ln $seq.out.2 $seq.out || exit 1
fi

echo "QA output created by $seq"

status=1	# failure is the default!
$sudo rm -rf $tmp.* $seq.full
trap "rm -f $tmp.*; exit \$status" 0 1 2 3 15

_filter()
{
    sed \
	-e 's/[A-Z][a-z][a-z] [A-Z][a-z][a-z]  *[0-9][0-9]* [0-9][0-9]:[0-9][0-9]:[0-9][0-9]/DATE/' \
	-e 's/DATE [12][0-9][0-9][0-9]/DATE/' \
	-e '/evaluator exiting/s/pmie([0-9][0-9]*)/pmie(PID)/' \
	-e '/Performance Co-Pilot Inference Engine/s/Version .*/Version .../'
}

_filter2()
{
    $PCP_AWK_PROG '
$1 == "pmie:"		{ print; next }
/failed: /		{ print; next }
$1 == "bundle:"	{ skip=1 }
skip == 1		{ if ($1 == "Rule:") skip = 0 }
skip == 1		{ next }
			{ print }' >$tmp.tmp
    tr ' =' '\012' <$tmp.tmp \
    | grep '0x' \
    | $PCP_AWK_PROG >$tmp.sed '
BEGIN	{ n = 0 }
    	{ if (seen[$1] == 1) next
	  printf "s/%s/ADDR%03d/\n",$1,n
	  n++
	  seen[$1] = 1
	}'
    sed -f $tmp.sed <$tmp.tmp
}

cat <<'End-of-File' >$tmp.pmie
vm00 = sampledso.colour :vm00;
vm01 = sampledso.colour :vm01;
vm02 = sampledso.colour :vm02;
vm03 = sampledso.colour :vm03;
min_sample sampledso.colour #'red' @0..2 > 0 -> print "min_sample:" " @%h [%i] %v";
min_inst sampledso.colour > 0 -> print "min_inst:" " @%h [%i] %v";
min_host sampledso.colour :vm00 :vm01 :vm02 :vm03 #'red' > 0 -> print "min_host:" " @%h [%i] %v";

max_sample sampledso.colour #'red' @0..2 > 0 -> print "max_sample:" " @%h [%i] %v";
max_inst sampledso.colour > 0 -> print "max_inst:" " @%h [%i] %v";
max_host sampledso.colour :vm00 :vm01 :vm02 :vm03 #'red' > 0 -> print "max_host:" " @%h [%i] %v";
End-of-File

# real QA test starts here
cat <<End-of-File | pmie -v -t 2sec -d -Dappl1 -a src/multi-vm00 -a src/multi-vm01 -a src/multi-vm02 -a src/multi-vm03 >$tmp.out 2>$tmp.err &
f $tmp.pmie
l
r 10sec
v
End-of-File

wait

echo >>$seq.full
echo "pmie PID $!" >>$seq.full
echo >>$seq.full
echo "=== pmie output ===" >>$seq.full
cat $tmp.out >>$seq.full
echo >>$seq.full
echo "=== pmie errors ===" >>$seq.full
cat $tmp.err >>$seq.full

_filter2 <$tmp.err | _filter
_filter <$tmp.out

# success, all done
status=0
exit
