#!/bin/sh
# PCP QA Test No. 749
# Check pmcd static probes
#
# Copyright (c) 2013-2014 Red Hat.

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

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

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

# If no systemtap, we cannot run the script
which stap >/dev/null 2>&1 || _notrun "No systemtap stap executable found"

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

# If running in FIPS mode, we cannot run this either
fips_enabled=0
fips_stsfile="/proc/sys/crypto/fips_enabled"
[ -f $fips_stsfile ] && fips_enabled=`cat $fips_stsfile`
test $fips_enabled -eq 0 || _notrun "FIPS enabled, no kernel stap testing"

# real QA test starts here
cat <<End-of-File >$tmp.stap
global probe_hits 
probe begin {
       system("pminfo -f hinv >/dev/null")
       system("pminfo -f kernel >/dev/null")
       system("pminfo -d mem >/dev/null")
       system("pminfo -f network >/dev/null")
       system("pminfo -tT disk >/dev/null")
}
probe process("$PCP_BINADM_DIR/pmcd").mark("*") {
       probe_hits <<< 1
}
probe timer.ms(1000) {
       if (@count(probe_hits) > 0) 
          println("PASS: probe_hits=" .  sprint(@count(probe_hits)))
       else
          println("FAIL: probe_hits=" .  sprint(@count(probe_hits)))
       exit()
}
End-of-File
echo "Using stap config:" >> $seq.full
cat $tmp.stap >> $seq.full
echo "Running pmcd process:" >> $seq.full
$PCP_PS_PROG $PCP_PS_ALL_FLAGS | grep pmcd >> $seq.full

# check for stap user probing support
stap -p4 $tmp.stap >$tmp.out 2>&1
if [ $? -ne 0 ]
then
    echo "Systemtap compile check failed"
    cat $tmp.out >> $seq.full
    _notrun "Systemtap userspace probing unsupported here"
fi

# start systemtap and count pmcd probe hits
echo "Fire stap probes:" >> $seq.full
$sudo stap $tmp.stap >$tmp.out 2>&1

# This seems safe to dodge if we PASS ...
# warning: the compiler differs from the one used to build the kernel
#  The kernel was built by: gcc (Ubuntu 11.2.0-19ubuntu1) 11.2.0
#  You are using:           gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
# and ditto for this one from vm21 (Debian 12.2)
# WARNING: Kernel function symbol table missing [man warning::symbols]
#
echo "Output from stap:" >> $seq.full
cat $tmp.out \
| tee -a $seq.full \
| if grep '^PASS:' $tmp.out >/dev/null
then
    sed \
	-e '/the compiler differs from the one used/d' \
	-e '/The kernel was built by:/d' \
	-e '/You are using:/d' \
	-e '/^WARNING: Kernel function symbol table missing/d' \
	-e '/^PASS:/s/=[1-9][0-9]*/=NNNN/' \
    # end
else
    cat
fi

# success, all done
status=0
exit
