#!/bin/sh
# PCP QA Test No. 1741
# check hinv.cpu.* metrics using code borrowed from lscpu with various
# "fake" data files
#
# non-valgrind variant, see qa/1743 for the valgrind variant
#
# Copyright (c) 2026 Ken McDonell.  All Rights Reserved.
#

if [ $# -eq 0 ]
then
    seq=`basename $0`
    echo "QA output created by $seq"
else
    # use $seq from caller, unless not set
    [ -n "$seq" ] || seq=`basename $0`
    echo "QA output created by `basename $0` $*"
fi

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

[ $PCP_PLATFORM = linux ] || _notrun "Linux hinv.cpu test, only works with Linux"

do_valgrind=false
if [ "$1" = "--valgrind" ]
then
    _check_valgrind
    do_valgrind=true
elif which valgrind >/dev/null 2>&1
then
    [ "$PCPQA_VALGRIND" = both ] || \
        _notrun "valgrind variant qa/1743 will be run"
fi

which lscpu >/dev/null 2>&1 || _notrun "lscpu executable not installed"
lscpu_version=`lscpu --version | $PCP_AWK_PROG '{ print $NF }'`
echo "lscpu_version=$lscpu_version" >>$seq_full
# not worth the pain here for lscpu versions older than 2.39
#
case "$lscpu_version"
in
    2.2*|2.3[0-8]|2.3[0-8].*)
	_notrun "lscpu version $lscpu_version is too old"
	;;
esac

_cleanup()
{
    cd $here
    $sudo rm -rf $tmp $tmp.*
}

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

LC_COLLATE=POSIX
export LC_COLLATE

_filter()
{
    sed \
	-e "s@$tmp@TMP@g" \
	-e "s@/privateTMP@TMP@g" \
    # end
}

# general fake out of /proc files and env var setup
# $1 = number of CPUs
#
_setup()
{
    if [ $# -ne 1 ]
    then
	echo "Usage: _setup botch \$#=$#"
	_exit 1
    fi
    rm -f $tmp/sys/devices/system/cpu/*
    if [ "$1" = 1 ]
    then
	echo "0" >$tmp/sys/devices/system/cpu/possible
    else
	echo 0-"`expr "$1" - 1`" >$tmp/sys/devices/system/cpu/possible
    fi
    ln $tmp/sys/devices/system/cpu/possible $tmp/sys/devices/system/cpu/present
    ln $tmp/sys/devices/system/cpu/possible $tmp/sys/devices/system/cpu/online
    cpu=0
    echo 'cpu  1 2 3 4 5 6 7 8 9' >$tmp/proc/stat
    while [ $cpu -lt "$1" ]
    do
	echo "cpu$cpu 0 1 2 3 4 5 6 7 8 9" >>$tmp/proc/stat
	cpu=`expr $cpu + 1`
    done
}

# some versions of lscpu just get things wrong ...
#
# $cpuinfo - cpuinfo dataset
_map_lscpu_output()
{
    if [ "$cpuinfo" = "linux/cpuinfo-2cpu-ppc-umax-s900dp" ]
    then
	# there are 2 different revision : lines here ...
	# pvr 0009 0202 and pvr 0009 0204 and lscpu sometimes
	# reports only one ... fake it as required
	#
	case "$lscpu_version"
	in
	    2.39|2.39.*|2.40|2.40.*|2.41|2.41.*|2.42|2.42.*)
		# 
		sed -e '/^Model:[ 	]/{
s/2\../2.2/
s/20./202/
p
s/2\.2/2.4/
s/202/204/

}'
		return
		;;
	esac
    elif [ "$cpuinfo" = "linux/cpuinfo-4cpu-riscv64" ]
    then
	# uarch           : sifive,u74-mc does not get turned in
	# a Model name: line in some versions of lscpu
	#
	case "$lscpu_version"
	in
	    2.39|2.39.*|2.40|2.40.*)
		echo "Model name: sifive,u74-mc"
		cat
		;;
	esac
    fi
    cat
}

# Note:
#    model from lscpu is unreliable ... on Ubuntu 26.04 it reports "1" for
#    the aarch64 cases, but lscpu build from github source reports "0x022"
#    similarly model is 7 for the ia64 test case on Ubuntu but 0 for the
#    github lscpu ... may need to filter these differences away
#
_doit()
{
    cat <$tmp/proc/cpuinfo >>$seq_full
    FAKEUNAME_MACHINE="$arch" LD_PRELOAD=$here/src/fakeuname.so \
	lscpu -s $tmp >$tmp.out 2>$tmp.err
    if [ -s $tmp.err ]
    then
	echo "lscpu Warnings ..."
	cat $tmp.err
    fi
    cat $tmp.out \
    | tee -a $seq_full \
    | _map_lscpu_output  \
    | sed -n >$tmp.tmp \
	-e 's/^Architecture:[ 	]*/architecture /p' \
	-e 's/^Vendor ID:[ 	]*/vendor /p' \
	-e 's/^Model name:[ 	]*/model_name /p' \
	-e 's/^Model:[ 	]*/model /p'
    for tag in architecture vendor model_name model
    do
	if ! grep -q "^$tag " $tmp.tmp
	then
	    echo "$tag unknown" >>$tmp.tmp
	fi
    done
    sed -e 's/ -$/ unknown/' <$tmp.tmp \
    | sort \
    | uniq >$tmp.lscpu
    echo "lscpu ..."
    cat $tmp.lscpu

    if $do_valgrind
    then
	FAKEUNAME_MACHINE="$arch" LD_PRELOAD=$here/src/fakeuname.so \
	LINUX_NNODES=1 LINUX_NCPUS="$ncpu" LINUX_STATSPATH=$tmp \
	    _run_valgrind --save-output pminfo -L -f hinv.cpu kernel.uname.machine
    else
	FAKEUNAME_MACHINE="$arch" LD_PRELOAD=$here/src/fakeuname.so \
	LINUX_NNODES=1 LINUX_NCPUS="$ncpu" LINUX_STATSPATH=$tmp \
	    pminfo -L -f hinv.cpu kernel.uname.machine >$tmp.out 2>$tmp.err
    fi
    if [ -s $tmp.err ]
    then
	echo "pminfo Warnings ..."
	cat $tmp.err
    fi
    cat $tmp.out \
    | tee -a $seq_full \
    | _filter \
    | $PCP_AWK_PROG '
NF == 1				{ tag = "" }
$1 == "kernel.uname.machine"	{ tag = "architecture"; next }
$1 == "hinv.cpu.vendor"		{ tag = "vendor"; next }
$1 == "hinv.cpu.model_name"	{ tag = "model_name"; next }
$1 == "hinv.cpu.model"		{ tag = "model"; next }
NF > 1 && tag != ""		{ printf "%s",tag
				  val = 0
				  for (i = 1; i <= NF; i++) {
				    if (val == 1) {
					printf " %s",$i
				    }
				    else if ($i == "value") {
					val = 1
				    }
				  }
				  printf "\n"
				}' \
    | sed -e 's/"//g' \
    | sort | uniq >$tmp.pminfo
    echo "pminfo ..."
    cat $tmp.pminfo

    diff $tmp.lscpu $tmp.pminfo >$tmp.tmp
    if [ -s $tmp.tmp ]
    then
	echo "Differences ..."
	cat $tmp.tmp
    fi
}



# real QA test starts here
mkdir -p $tmp/proc || _exit 1
mkdir -p $tmp/sys/devices/system/cpu || _exit 1

files=`echo linux/cpuinfo*`

for cpuinfo in $files
do
    base=`basename $cpuinfo`
    cat $cpuinfo >$tmp/proc/cpuinfo
    echo
    echo "=== $base ===" | tee -a $seq_full
    ncpu=`echo $base | sed -e 's/.*-\([0-9][0-9]*\)cpu-.*/\1/'`
    arch=`echo $base | sed -n -E -e '/.*-[0-9][0-9]*cpu-([a-z][a-z0-9_]*)($|-.*)/s//\1/p'`
    echo "ncpu=$ncpu arch=$arch" >>$seq_full
    _setup "$ncpu"
    _doit
done

# success, all done
exit
