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

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

. ./common.bcc

$sudo rm -rf $tmp.* $seq.full

_pmdabcc_check
_pmdabcc_require_kernel_version 4 7
_java_check

status=1       # failure is the default!
signal=$PCP_BINADM_DIR/pmsignal

# Launch a Java test program
cd $here/java
if [ "$__java_major" -ge 19 ]
then
    # -XX:+ExtendedDTraceProbes deprecated here
    #
    __java_flags="-XX:+DTraceAllocProbes -XX:+DTraceMethodProbes -XX:+DTraceMonitorProbes"
else
    __java_flags="-XX:+DTraceAllocProbes -XX:+ExtendedDTraceProbes"
fi
java $__java_flags USDTJVMTest &
javapid=$!
disown > /dev/null 2>&1 || :
cd $here

cat <<EOF >$tmp.conf
# Installed by PCP QA test $seq on `date`
[pmda]
modules = ustat
prefix = bcc.
[ustat]
module = ustat
cluster = 17
process = java
language = java
EOF

_pmdabcc_try_compile $tmp.conf

_prepare_pmda bcc
trap "\$signal -s KILL \$javapid > /dev/null 2>&1; _pmdabcc_cleanup; exit \$status" 0 1 2 3 15
_stop_auto_restart pmcd

# real QA test starts here
_pmdabcc_install <$tmp.conf | _pmdabcc_install_filter
_pmdabcc_wait_for_metric

# Wait for values to appear
pmsleep 1

echo "=== report metric values for object creation ==="
pminfo -dfmtT bcc.proc.ustat.object 2>&1 | tee -a $here/$seq.full \
| grep $javapid\" | _value_filter_nonzero

echo "=== report metric values for method calls ==="
pminfo -dfmtT bcc.proc.ustat.method 2>&1 | tee -a $here/$seq.full \
| grep $javapid\" | _value_filter_nonzero

echo "=== report metric values for threads started ==="
pminfo -dfmtT bcc.proc.ustat.thread 2>&1 | tee -a $here/$seq.full \
| grep $javapid\" | _value_filter_nonzero

$signal -s KILL $javapid > /dev/null 2>&1
_pmdabcc_remove 2>&1

status=0
exit
