#!/bin/sh
# PCP QA Test No. 1364
# pmlogger delta-indom write path ... check on-disk metadata
# and verify that libpcp can load and "undelta" the indom
#
# non-valgrind variant, see qa/1365 for the valgrind variant
#
# Copyright (c) 2022 Ken McDonell.  All Rights Reserved.
#

if [ $# -eq 0 ]
then
    seq=`basename $0`
    echo "QA output created by $seq"
else
    # use $seq from caller, unless not set
    [ -n "$seq" ] || seq=`basename $0`
    echo "QA output created by `basename $0` $*"
fi

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

do_valgrind=false
if [ "$1" = "--valgrind" ]
then
    _check_valgrind
    do_valgrind=true
elif which valgrind >/dev/null 2>&1
then
    [ $PCPQA_VALGRIND = both ] || \
	_notrun "valgrind variant qa/1365 will be run"
fi

_cleanup()
{
    cd $here
    $sudo rm -rf $tmp $tmp.*
}

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

# standard pmdumplog filtering, but need to also cull the
# 2.1 indom (from the pmcd.pmlogger.* metrics) 'cause this
# contains PIDs that are not deterministic ... so from here
# 06:57:43.743730000 InDom: 2.1 1 instances
# down to just before next InDom line
#
_filter()
{
    _filter_pmdumplog \
    | $PCP_AWK_PROG '
BEGIN				{ skip = 0 }
$2 == "InDom:" && $3 == "2.1"	{ skip = 1; next }
skip == 1 && $2 == "InDom:"	{ skip = 0 }
$1 == "InDom:" && $2 == "2.1"	{ skip = 2; next }
skip == 2 && $1 == "InDom:"	{ skip = 0 }
skip == 0			{ print }'
}

cat <<End-of-File >$tmp.config
log mandatory on 100 msec {
    sample.long
    sample.bin
    sample.proc
}
End-of-File

# real QA test starts here

# V2 archive for baseline comparison ...
#
pmstore sample.proc.reset 1
pmlogger -V 2 -c $tmp.config -l $tmp.log -s 20 $tmp.v2
cat $tmp.log >>$seq.full
echo "=== V2 archive ===" | tee -a $seq.full
pmdumplog -Ii $tmp.v2 | tee -a $seq.full | _filter | tee $tmp.v2.dump

pmstore sample.proc.reset 1
if $do_valgrind
then
    _run_valgrind pmlogger -V 3 -c $tmp.config -l $tmp.log -s 20 $tmp.v3
else
    pmlogger -V 3 -c $tmp.config -l $tmp.log -s 20 $tmp.v3
fi \
| sed -e "s@$tmp@TMP@g"
cat $tmp.log >>$seq.full

echo
echo "=== V3 archive ===" | tee -a $seq.full
pmdumplog -Ii $tmp.v3 | tee -a $seq.full | _filter | tee $tmp.v3.dump

echo
echo "=== diffs ... ==="
diff $tmp.v2.dump $tmp.v3.dump

# success, all done
exit
