#!/bin/sh
# PCP QA Test No. 512
# lock recursion depth for some pcp apps
#
# Copyright (c) 2012 Ken McDonell.  All Rights Reserved.
#

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

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

src/check_fault_injection >/dev/null 2>&1 || \
    _notrun "libpcp not built with fault injection & lock tracing enabled"

if [ -d ../src ]
then
    SRC=../src
else
    _notrun "PCP source not found at ../src"
fi

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

# we need to run part of the make to get headers and libraries setup
# in $SRC
#
for dir in include libpcp/src
do
    echo "$SRC/$dir ..." >>$seq.full
    cd $SRC/$dir
    if $PCP_MAKE_PROG 2>$tmp.err >>$here/$seq.full
    then
	cat $tmp.err >>$here/$seq.full
    else
	echo "Arrgh ... make failed in $SRC/$dir"
	cat $tmp.err
	status=1
	exit
    fi
    cd $here
done

_filter()
{
    tee -a $seq.full \
    | sed -n \
	-e '/lock(/s/^[^ ][^ ]* //p' \
    | LC_COLLATE=POSIX sort \
    | uniq
}

_pre()
{
    if [ ! -d $1 ]
    then
	echo "_pre: botch: $1 does not exist"
    else
	cd $1
	$PCP_MAKE_PROG clean >/dev/null 2>&1
	# This is a gross hack ...
	# need to over-ride LDFLAGS from the PCP builddefs so we search for
	# libpcp.so in the libpcp_fault directory
	#
	if LCFLAGS=-DPM_MULTI_THREAD_DEBUG=1 LDFLAGS="-L../../src/libpcp_fault/src" $PCP_MAKE_PROG $2 >$tmp.out 2>&1
	then
	    mv $2 $2.debug
	    $PCP_MAKE_PROG clean >/dev/null 2>&1
	else
	    echo "_pre: botch: make failed"
	    cat $tmp.out
	fi
	cd $here
    fi
}

_post()
{
    if [ ! -d $1 ]
    then
	echo "_post: botch: $1 does not exist"
    else
	cd $1
	rm -f $2.debug
	cd $here
    fi
}

# real QA test starts here
export LD_PRELOAD=$PCP_LIB_DIR/libpcp_fault.so


echo "== pminfo ==" | tee -a $seq.full
_pre $SRC/pminfo pminfo
$SRC/pminfo/pminfo.debug -v -Dlock >/dev/null 2>$tmp.trace
_post $SRC/pminfo pminfo
_filter <$tmp.trace

echo | tee -a $seq.full
echo "== pmlogger ==" | tee -a $seq.full
sed -e 's/1 sec/50 msec/' <tmparch/config.foo >$tmp.config
_pre $SRC/pmlogger/src pmlogger
$SRC/pmlogger/src/pmlogger.debug -Dlock -c $tmp.config -s 20 -l $tmp.log $tmp
_post $SRC/pmlogger/src pmlogger
_filter <$tmp.log

echo | tee -a $seq.full
echo "== pmlogextract ==" | tee -a $seq.full
for arch in a b c
do
    for i in 0 index meta
    do
	cp tmparch/foo.$i $tmp-$arch.$i
    done
done
_pre $SRC/pmlogextract pmlogextract
$SRC/pmlogextract/pmlogextract.debug -Dlock $tmp-a $tmp-b $tmp-c $tmp-out 2>$tmp.trace
_post $SRC/pmlogextract pmlogextract
_filter <$tmp.trace

echo | tee -a $seq.full
echo "== pmdumplog ==" | tee -a $seq.full
_pre $SRC/pmdumplog pmdumplog
$SRC/pmdumplog/pmdumplog.debug -dilmst -Dlock $tmp-out >$tmp.err 2>$tmp.out
_post $SRC/pmdumplog pmdumplog
cat $tmp.out $tmp.err | _filter

echo | tee -a $seq.full
echo "== dbpmda ==" | tee -a $seq.full
_pre $SRC/dbpmda/src dbpmda
# based on QA 137
cat <<End-of-File >$tmp.in
open dso $PCP_PMDAS_DIR/simple/pmda_simple.$DSO_SUFFIX simple_init 253
getdesc on
desc simple.numfetch
fetch simple.numfetch
desc simple.color
fetch simple.color
instance 253.0
open pipe $PCP_PMDAS_DIR/simple/pmdasimple -d 253 -l $tmp.log
desc simple.numfetch
fetch simple.numfetch
desc simple.color
fetch simple.color
instance 253.0
End-of-File
$sudo -u $PCP_USER sh -c "export LD_PRELOAD=$PCP_LIB_DIR/libpcp_fault.so; $SRC/dbpmda/src/dbpmda.debug -Dlock -n $PCP_PMDAS_DIR/simple/root -ie" <$tmp.in >$tmp.out 2>$tmp.err
_post $SRC/dbpmda/src dbpmda
_filter <$tmp.out
_filter <$tmp.err

cat $tmp.log >>$here/$seq.full

# success, all done
exit
