#! /bin/sh
# PCP QA Test No. 178
# pmlogextract exerciser
#
# Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

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

trap "rm -f $tmp.*; exit" 0 1 2 3 15
#debug# tmp=`pwd`/tmp

_filter()
{
    $PCP_AWK_PROG '
/<mark>/	{ next }
/suspended/	{ next }
/No values/	{ noval++; next }
/\?/		{ noval++; next }
/^[0-2][0-9]:/	{ sample++; next }
END		{ printf "%d samples",sample
		  if (noval > 0) printf ", %d \"no values\"",noval
		  print ""
		}'
}

_range()
{
    $PCP_AWK_PROG '
'$1' <= $1 && $1 <= '$2'	{ print "Pass",$2,$3; next }
				{ print "Fail (" $1 " " $2 " " $3 ")" }'
}

_stamp()
{
    sed -e 's/ [0-2][0-9]:.*$/ datestamp/'
}

_count_rec()
{
    $PCP_AWK_PROG '
/^[0-2][0-9]:.*<mark>/		{ mark++; next }
/^[0-2][0-9]:/			{ rec++; next }
END				{ printf "%d",rec
				  if (mark == 1) printf " (+%d mark)",mark
				  else if (mark > 1) printf " (+%d marks)",mark
				  print ""
				}'
}

rm -f $seq.full $tmp.*

# real QA test starts here
cat >$tmp.a.config <<End-of-File
log mandatory on 600 msec sample.bin
End-of-File

cat >$tmp.b.config <<End-of-File
log mandatory on 300 msec {
	sample.bin [ "bin-200" ]
	sample.drift
}
End-of-File

cat >$tmp.c.config <<End-of-File
log mandatory on 300 msec {
	sample.bin
	sample.drift
}
End-of-File

# find a port for each pmlogger to use, avoiding (small!) race
# when starting multiple pmlogger processes at the same time.
port1=`_find_free_port`
port2=`expr $port1 + 1`
port2=`_find_free_port $port2`
port3=`expr $port2 + 1`
port3=`_find_free_port $port3`

# Avoid derived metrics delay at startup ... want "a" and "b" to run
# as close as possible to concurrently
#
export PCP_DERIVED_CONFIG=

# a and b concurrently
# a: 25 x 0.6 sec + prologue + epilogue = 27
# b: 20 x 0.3 sec + prologue + epilogue = 22
#
PMLOGGER_PORT=$port1 \
pmlogger -c $tmp.a.config -l $tmp.a.log -s 25 $tmp.a >$tmp.a.err 2>&1 &
PMLOGGER_PORT=$port2 \
pmlogger -c $tmp.b.config -l $tmp.b.log -s 20 $tmp.b >$tmp.b.err 2>&1 &

wait
echo "log for archive 'a' ..." >>$seq.full
cat $tmp.a.err $tmp.a.log | tee -a $seq.full | _filter_pmlogger_log
echo >>$seq.full
echo "log for archive 'b' ..." >>$seq.full
cat $tmp.b.err $tmp.b.log | tee -a $seq.full | _filter_pmlogger_log

sleep 2

# c later
# c: 10 x 0.3 sec + prologue + epilogue = 12
#
PMLOGGER_PORT=$port3 \
pmlogger -c $tmp.c.config -l $tmp.c.log -s 10 $tmp.c >$tmp.c.err 2>&1
echo >>$seq.full
echo "log for archive 'c' ..." >>$seq.full
cat $tmp.c.err $tmp.c.log | tee -a $seq.full | _filter_pmlogger_log

echo "Full merge ..."
rm -f $tmp.merge.*
pmlogextract $tmp.a $tmp.b $tmp.c $tmp.merge
echo >>$seq.full
echo "Log record counts ..." | tee -a $seq.full
for i in a b c merge
do
    $PCP_ECHO_PROG $PCP_ECHO_N "$i: ""$PCP_ECHO_C"
    ( echo; echo "=== $i ===" ) >>$seq.full
    pmdumplog -m $tmp.$i | tee -a $seq.full | _count_rec
done

echo
echo 'sample.bin ["bin-200"] ... expect 55 samples'
pmval -i "bin-200" -U $tmp.merge sample.bin 2>&1 \
| _filter

echo
echo 'sample.bin ["bin-100"] ... expect 35 samples, 20 no values'
pmval -i "bin-100" -U $tmp.merge sample.bin 2>&1 \
| _filter

echo
echo "sample.drift ... expect 30 samples"
pmval -U $tmp.merge sample.drift 2>&1 \
| _filter

echo | tee -a $seq.full
echo "merge -s 10" | tee -a $seq.full
pmlogextract -s 10 $tmp.a $tmp.b $tmp.c $tmp.merge-10
$PCP_ECHO_PROG $PCP_ECHO_N "merge (expect 10): ""$PCP_ECHO_C"
pmdumplog -m $tmp.merge-10 \
| tee -a $seq.full \
| _count_rec 

# all of a + b
#
echo | tee -a $seq.full
echo "merge -T 16" | tee -a $seq.full
pmlogextract -T 16 $tmp.a $tmp.b $tmp.c $tmp.merge-T16
$PCP_ECHO_PROG $PCP_ECHO_N "merge (expect 49): ""$PCP_ECHO_C"
pmdumplog -m $tmp.merge-T16 \
| tee -a $seq.full \
| _count_rec

# all of c, none of a or b
echo | tee -a $seq.full
echo "merge -S 16" | tee -a $seq.full
pmlogextract -S 16 $tmp.a $tmp.b $tmp.c $tmp.merge-S16
$PCP_ECHO_PROG $PCP_ECHO_N "merge (expect 12): ""$PCP_ECHO_C"
pmdumplog -m $tmp.merge-S16 \
| tee -a $seq.full \
| _count_rec

echo | tee -a $seq.full
echo "Full merge with volume switch ..." | tee -a $seq.full
pmlogextract -v 15 $tmp.a $tmp.b $tmp.c $tmp.merge-mv 2>&1 \
| tee -a $seq.full \
| _stamp
echo "Log record counts ..." | tee -a $seq.full
for i in a b c merge-mv
do
    $PCP_ECHO_PROG $PCP_ECHO_N "$i: ""$PCP_ECHO_C"
    ( echo; echo "=== $i ===" ) >>$seq.full
    pmdumplog -m $tmp.$i \
    | tee -a $seq.full \
    | _count_rec
done

for file in $tmp.merge-mv.[0-9]*
do
    mv $file $file.x
done

echo "Log record counts per merged volume ..." | tee -a $seq.full
for vol in `echo $tmp.merge-mv.[0-9]* | sed -e "s;$tmp\\.merge-mv\\.\\([0-9]*\\)\\.x;\\1;g"`
do
    $PCP_ECHO_PROG $PCP_ECHO_N "volume $vol: ""$PCP_ECHO_C"
    ( echo; echo "=== volume $vol ===" ) >>$seq.full
    mv $tmp.merge-mv.$vol.x $tmp.merge-mv.$vol
    pmdumplog -m $tmp.merge-mv \
    | tee -a $seq.full \
    | _count_rec
    mv $tmp.merge-mv.$vol $tmp.merge-mv.$vol.x
done

for file in $tmp.merge-mv.[0-9]*
do
    file=`echo $file | sed -e 's/\.x$//'`
    mv $file.x $file
done

