#!/bin/sh
# PCP QA Test No. 1150
# Exercise the BCC PMDA USDT hits module - install, remove and values.
#
# Copyright (c) 2018 Red Hat.
#

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

. ./common.bcc

_pmdabcc_check
_pmdabcc_require_kernel_version 4 7

# If no PCP support for probes, also bail out
_get_libpcp_config
$static_probes || _notrun "No static probe support available in PCP build"

if [ -f /etc/lsb-release ]
then
    . /etc/lsb-release
    if [ "$DISTRIB_ID" = Ubuntu ]
    then
	# This test failed for Ubuntu 16.*, 17.* and 18.* but it
	# appears to work for 19.04, so assume others beyond this
	# release are also OK
	#
	version=`echo $DISTRIB_RELEASE | sed -e 's/\..*//'`
	if [ $version -lt 19 ]
	then
	    _notrun "bcc USDT does not work on Ubuntu $DISTRIB_RELEASE"
	fi
    fi
fi

status=1       # failure is the default!
signal=$PCP_BINADM_DIR/pmsignal
$sudo rm -rf $tmp.* $seq.full

_log_filter()
{
    sed \
        -e 's,Using PID.*,Found PMCD.,g' \
	-e "s,$PCP_BINADM_DIR,PCP_BINADM_DIR,g" \
    #end
}

_value_filter()
{
    awk '/pmcd::PMCD_PDU/ && /value [1-9][0-9]*/ {print "OK"; exit}'
}

_prepare_pmda bcc
trap "_pmdabcc_cleanup; exit \$status" 0 1 2 3 15
_stop_auto_restart pmcd

# real QA test starts here
cat <<EOF | _pmdabcc_install | _log_filter
# Installed by PCP QA test $seq on `date`
[pmda]
modules = usdt_hits
prefix = bcc.
[usdt_hits]
module = usdt_hits
cluster = 101
process = pmcd
usdts = $PCP_BINADM_DIR/pmcd:PMCD,$PCP_BINADM_DIR/pmcd:PMCD_PDU
EOF
if _pmdabcc_wait_for_metric
then
    echo "back from _pmdabcc_wait_for_metric: `date`" >>$here/$seq.full
else
    exit
fi

# Generate system activity for the BCC uprobe hits module
pminfo -f bcc >>$here/$seq.full
pminfo -f mem > /dev/null

echo "=== report metric values ==="
pminfo -dfmtT bcc.usdt.hits 2>&1 | tee -a $here/$seq.full \
| _value_filter

_pmdabcc_remove

status=0
exit
