#!/bin/sh
# PCP QA Test No. 747
# Exercise Linux hardware inventory online/offline metrics.
#
# Copyright (c) 2014,2020 Red Hat.
#

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

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

[ $PCP_PLATFORM = linux ] || _notrun "Linux sysfs test, only works with Linux"

status=1	# failure is the default!
mkdir -p $tmp.root

trap "cd $here; $sudo rm -rf $tmp.*; exit \$status" 0 1 2 3 15

_sort_instname()
{
    $sudo rm -f $tmp.sort
    tee $tmp.sort | sed -n 2p
    awk '/inst / {print "  ", $4, $6}' $tmp.sort | sed -e 's/\]//' | LC_COLLATE=POSIX sort
}

# real QA test starts here
export LINUX_HERTZ=100
export LINUX_STATSPATH=$tmp.root
pmda=$PCP_PMDAS_DIR/linux/pmda_linux.so,linux_init
local="-L -K clear -K add,60,$pmda"

online_metrics="hinv.cpu.online hinv.node.online"
allcpu_metrics=`pminfo $local kernel.all.cpu | LC_COLLATE=POSIX sort`
percpu_metrics=`pminfo $local kernel.percpu.cpu | LC_COLLATE=POSIX sort`
pernode_metrics=`pminfo $local kernel.pernode.cpu | LC_COLLATE=POSIX sort`
thermal_metrics=`pminfo $local hinv.cpu.thermal_throttle| LC_COLLATE=POSIX sort`
cpufreq_metrics=`pminfo $local hinv.cpu.frequency_scaling | LC_COLLATE=POSIX sort`

for tgz in $here/linux/sysdev-*.tgz $here/linux/sysfs-numa-001.tgz
do
    $sudo rm -fr $tmp.root/*
    cd $tmp.root
    $sudo tar xzf $tgz
    base=`basename $tgz`

    echo "=== $base ===" >>$seq_full

    # need the number of cpus in this data set ... from the number of
    # lines like
    # cpu7 850802 2207 394292 41789513 119722 1143651 200784 0 0 0
    # in proc/stat
    #
    if [ ! -f proc/stat ]
    then
	echo "Arrgh, proc/stat not included in $base stats tarball"
	exit
    fi
    LINUX_NCPUS=`grep '^cpu[0-9][0-9]* ' proc/stat | wc -l | sed -e 's/ //g'`
    export LINUX_NCPUS
    echo "LINUX_NCPUS=$LINUX_NCPUS" >>$seq_full

    echo "== Checking hinv online metric values - $base"
    for m in $online_metrics
    do
	pminfo $local -f $m | _sort_instname
    done
    echo && echo "== done" && echo

    echo "== Checking aggregate CPU metric values - $base"
    pminfo $local -f $allcpu_metrics
    echo && echo "== done" && echo

    echo "== Checking per-CPU metric values - $base"
    for m in $percpu_metrics
    do
	pminfo $local -f $m | _sort_instname
    done
    echo && echo "== done" && echo

    echo "== Checking per-node CPU metric values - $base"
    for m in $pernode_metrics
    do
	pminfo $local -f $m | _sort_instname
    done
    echo && echo "== done" && echo

    echo "== Checking hinv thermal_throttle values - $base"
    for m in $thermal_metrics
    do
	pminfo $local -f $m | _sort_instname
    done
    echo && echo "== done" && echo

    echo "== Checking hinv frequency_scaling values - $base"
    for m in $cpufreq_metrics
    do
	pminfo $local -f $m | _sort_instname
    done
    echo && echo "== done" && echo

    cd $here
done

# success, all done
status=0
exit
