#!/bin/sh
# PCP QA Test No. 1721
# Exercise the bpftrace PMDA - autostart script
#

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

. ./common.bpftrace

_pmdabpftrace_check

# this is early, but pmcd.control.creds_timeout has already
# been changed, so we need the trap and _cleanup(), but
# _pmdabpftrace_cleanup() fails if _prepare_pmda() has not
# been called
#
_prepare_pmda bpftrace

_cleanup()
{
    $sudo rm -f $PCP_PMDAS_DIR/bpftrace/autostart/*.bt
    _pmdabpftrace_cleanup
}

status=1       # failure is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

version=`bpftrace --version | sed -e 's/.* v0\.//' -e 's/\..*//'`
echo "bpftrace version=$version" >>$seq_full
if [ -f "$PCP_SHARE_DIR/pmdas/bpftrace/examples/biolatency-v$version.bt" ]
then
    $sudo cp "$PCP_SHARE_DIR/pmdas/bpftrace/examples/biolatency-v$version.bt" $PCP_PMDAS_DIR/bpftrace/autostart/biolatency.bt
else
    ( cd "$PCP_SHARE_DIR/pmdas/bpftrace/examples"; ls biolatency-v*.bt 2>/dev/null ) \
    | sed -e 's/biolatency-v//' -e 's/\.bt$//' \
    | sort -n >$tmp.tmp
    minver=`head -1 <$tmp.tmp`
    echo "minver=$minver" >>$seq_full
    if [ -n "$minver" -a "$minver" -gt $version ]
    then
	_notrun "no biolatency.bt for bpftrace old version $version (oldest we support is $minver)"
	#NOTREACHED
    fi
    maxver=`tail -1 <$tmp.tmp`
    echo "maxver=$maxver" >>$seq_full
    if [ -n "$maxver" -a "$maxver" -lt $version ]
    then
	echo "Arrgh: no biolatency.bt for new bpftrace version $version (newest we support is $maxver)"
	echo "       see README in the src/pmdas/bftrace/examples directory"
	echo "       for the refresh recipe"
	exit
    fi
    _notrun "missing biolatency.bt for bpftrace version $version"
    #NOTREACHED
fi

_pmdabpftrace_check_probes $PCP_PMDAS_DIR/bpftrace/autostart/biolatency.bt

_stop_auto_restart pmcd

# real QA test starts here
cat <<EOF | _pmdabpftrace_install
# Installed by PCP QA test $seq on `date`
EOF

echo "=== check script is running ==="
_pmdabpftrace_wait_for_value bpftrace.scripts.biolatency.probes '[45]$'
pminfo bpftrace

echo "=== check metrics ==="
pminfo -dfmtT bpftrace.scripts.biolatency.status


_pmdabpftrace_remove
status=0
exit
