#!/bin/sh
# PCP QA Test No. 1273
# optFetch and pmlogger interraction for repeated metric in a logging
# group
#
# Copyright (c) 2020 Ken McDonell.  All Rights Reserved.
#

seq=`basename $0`
if [ $# -eq 0 ]
then
    echo "QA output created by $seq"
else
    echo "QA output created by $seq $*"
fi

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

_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

_do_it()
{
    rm -f $tmp.0 $tmp.meta $tmp.index $tmp.log
    pmlogger -D optfetch,desperate -l $tmp.log -c $tmp.config -s 2 -t 0.25sec $tmp
    echo >>$seq.full
    cat $tmp.config >>$seq.full
    cat $tmp.log >>$seq.full

    # delete log down to last "Task 0x5608ac112100 -> t_fetch ..." line
    # then summarize
    # Fetch Control @ 0x5608ac1127d0: cost=20 state=NEW PMID PROFILE 
    # ...
    #   PMID 60.0.4 Control @ 0x5608ac112810:
    # ...
    #
    line=`$PCP_AWK_PROG <$tmp.log '/ t_fetch/ { print NR }' | tail -1`
    if [ -z "$line" ]
    then
	echo "Arrgh, no t_fetch line in log file ... see $seq.full"
    else
	$PCP_AWK_PROG <$tmp.log '
BEGIN			{ i = 0 }
NR < '$line'		{ next }
/^Fetch Control /	{ print "Fetch Control [" i "]"; i++; next }
$1 == "PMID"		{ print "  " $1,$2; next }'
    fi

    pmdumplog $tmp "$1" \
    | _filter_pmdumplog
}

# real QA test starts here
echo "sample.bin - simple - sample.bin"
cat <<End-of-File >$tmp.config
log mandatory on default {
    sample.bin
    simple
    sample.bin
}
End-of-File
_do_it sample.bin

echo
echo "sample.bin - simple - disk.dev.read - sample.bin"
cat <<End-of-File >$tmp.config
log mandatory on default {
    sample.bin
    simple
    disk.dev.read
    sample.bin
}
End-of-File
_do_it sample.bin

echo
echo "sample.bin - simple - disk.dev.read - sampledso.long - sample.bin"
cat <<End-of-File >$tmp.config
log mandatory on default {
    sample.bin
    simple
    disk.dev.read
    sampledso.long
    sample.bin
}
End-of-File
_do_it sample.bin

echo
echo "sampledso.long - sample.bin - simple - disk.dev.read - sample.bin"
cat <<End-of-File >$tmp.config
log mandatory on default {
    sampledso.long
    sample.bin
    simple
    disk.dev.read
    sample.bin
}
End-of-File
_do_it sample.bin

echo
echo "sample.bin - sampledso.long - simple - disk.dev.read - sample.bin - sampledso.longlong"
cat <<End-of-File >$tmp.config
log mandatory on default {
    sample.bin
    sampledso.long
    simple
    disk.dev.read
    sample.bin
    sampledso.longlong
}
End-of-File
_do_it sample.bin

echo
echo "sample.bin[i1] - simple - sample.bin[i2]"
echo "i1 and i2 disjoint"
cat <<End-of-File >$tmp.config
log mandatory on default {
    sample.bin["bin-100","bin-300","bin-500"]
    simple
    sample.bin["bin-200","bin-400","bin-600"]
}
End-of-File
_do_it sample.bin

echo
echo "sample.bin[i1] - sampledso.long - simple - disk.dev.read - sample.bin[i2]"
echo "i1 and i2 disjoint"
cat <<End-of-File >$tmp.config
log mandatory on default {
    sample.bin["bin-100","bin-300","bin-500"]
    sampledso.long
    simple
    disk.dev.read
    sample.bin["bin-200","bin-400","bin-600"]
}
End-of-File
_do_it sample.bin

echo
echo "sample.bin[i1] - sampledso.long - simple - disk.dev.read - sample.bin[i2]"
echo "i1 and i2 overlap"
cat <<End-of-File >$tmp.config
log mandatory on default {
    sample.bin["bin-100","bin-200","bin-300"]
    sampledso.long
    simple
    disk.dev.read
    sample.bin["bin-200","bin-300","bin-400","bin-500"]
}
End-of-File
_do_it sample.bin

echo
echo "sample.bin - sampledso.long - simple - disk.dev.read - sample.bin[i2]"
cat <<End-of-File >$tmp.config
log mandatory on default {
    sample.bin
    sampledso.long
    simple
    disk.dev.read
    sample.bin["bin-200","bin-300","bin-400","bin-500"]
}
End-of-File
_do_it sample.bin

echo
echo "sample.bin[i1] - sampledso.long - simple - disk.dev.read - sample.bin"
cat <<End-of-File >$tmp.config
log mandatory on default {
    sample.bin["bin-100","bin-200","bin-300"]
    sampledso.long
    simple
    disk.dev.read
    sample.bin
}
End-of-File
_do_it sample.bin

# success, all done
exit
