#!/bin/bash
# check numactl --hardware output
# this checks most of the topology discovery in libnuma

testdir=`dirname "$0"`
: ${srcdir:=${testdir}/..}
: ${builddir:=${srcdir}}
export PATH=${builddir}:$PATH

numcpus=$(grep -c processor /proc/cpuinfo)
numnodes=$(ls -1d /sys/devices/system/node/node[0-9]* | wc -l)

nccpus=$(numactl --hardware | grep cpus | sed 's/node.*cpus://' | wc -w ) 
ncnodes=$(numactl --hardware | grep -c 'node.*size' ) 
node_has_cpus=""

if [ $numnodes != $ncnodes ] ; then
    echo "numactl --hardware doesnt report all nodes"
    exit 1
fi

if [ $numcpus != $nccpus -a \( $[$nccpus / $numnodes] != $numcpus \) ] ; then
    echo "numactl --hardware cpus look bogus"
    exit 1
fi

if [ -s /sys/devices/system/node/has_cpu ]; then
    node_has_cpus=$(cat /sys/devices/system/node/has_cpu | sed 's/,/ /')
fi

numactl --hardware | grep cpus | while read n ; do 
    node=${n/ cpus*/} 
    node=${node/ /}
    cpus=${n/*: /}
    check_node=$(echo $node | sed 's/node//')
    if [[ -n ${node_has_cpus} ]]; then
        if ! [[ "${node_has_cpus}" == *"$check_node"* ]]; then
            echo "Skipping cpu less $node"
            continue
	fi
    fi
    k=0
    for i in $cpus ; do 
	if [ ! -h "/sys/devices/system/node/$node/cpu$i" ] ; then
	    echo "$node doesn't have cpu $i"
	    exit 1
	fi
	k=$[$k+1]
    done
    if [ $k != $(echo $cpus | wc -w) ] ; then
	echo "$node missing cpu"
	exit 1	
    fi
done
