Skip to content

Commit

Permalink
[detect_numa] Filter empty NUMA nodes (#29)
Browse files Browse the repository at this point in the history
* Add development option to provide command for lscpu

* Filter out empty NUMA nodes by default, added option to include them in a count if desired
  • Loading branch information
kdvalin authored Jan 18, 2024
1 parent cb7622e commit 1153323
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
37 changes: 31 additions & 6 deletions detect_numa
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

get_nodes=0
get_cpus=0
remove_empty_nodes=1
lscpu_command="lscpu"
node=""

usage()
Expand All @@ -23,13 +25,20 @@ usage()
echo -e "\t-n/--node"
echo -e "\t\tRestrict CPUs listed by --cpu-list to certain nodes"
echo -e "\t\t\tSupports ranges (0-5), comma separated values (0,2,4), and single digits (1)"

echo -e "\t--include-empty"
echo -e "\t\tInclude empty NUMA nodes in --node-count output"

echo -e "\t-i/--input"
echo -e "\t\tProvide a command to gather information in lscpu format"
echo -e "\t\tThis is intended for development"
exit 0
}

# Verifies that NUMA exists on the system
has_numa()
{
local numa_nodes=`lscpu | grep NUMA | grep CPU | wc -l`
local numa_nodes=`$lscpu_command | grep NUMA | grep CPU | wc -l`
if [ -z "$numa_nodes" ] || [ $numa_nodes -le 0 ]; then
echo 0
else
Expand All @@ -40,9 +49,14 @@ has_numa()
# Fetches the number of NUMA nodes
get_num_nodes()
{
local numa_nodes=`lscpu | grep NUMA | grep CPU | wc -l`
if [ "`has_numa`" -eq 0 ]; then
numa_nodes=1
local numa_nodes=1
if [ "`has_numa`" -eq 1 ]; then
local numa_node_info=`$lscpu_command | grep NUMA | grep CPU`
if [ $remove_empty_nodes -ne 0 ]; then
numa_nodes=`echo "$numa_node_info" | awk '{ print $4 }' | grep -v "^$" | wc -l`
else
numa_nodes=`echo "$numa_node_info" | wc -l`
fi
fi

echo $numa_nodes
Expand All @@ -65,7 +79,7 @@ fetch_cpus() {
echo "Error: NUMA Node $node does not exist" > /dev/stderr
exit 1
fi
lscpu | grep -E "$prefix$node" | sort | awk '{ print $4 }'
$lscpu_command | grep -E "$prefix$node" | sort | awk '{ print $4 }'
}

NOARG_OPTS=(
Expand All @@ -76,18 +90,21 @@ NOARG_OPTS=(
"cpu-list"
"list-cpu"
"node-count"
"include-empty"
)

ARG_OPTS=(
"node"
"n"
"i"
"input"
)

opts=$(getopt \
--longoptions "$(printf "%s," "${NOARG_OPTS[@]}")" \
--longoptions "$(printf "%s:," "${ARG_OPTS[@]}")" \
--name "$(basename "$0")" \
--options "hn:" \
--options "hi:n:" \
-- "$@"
)

Expand All @@ -114,6 +131,14 @@ while [[ $# -gt 0 ]]; do
get_nodes=1
shift 1
;;
-i | --input)
lscpu_command=$2
shift 2
;;
--include-empty)
remove_empty_nodes=0
shift 1
;;
--)
break
;;
Expand Down
11 changes: 11 additions & 0 deletions tests/resources/detect_numa/empty_numa.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
NUMA:
NUMA node(s): 9
NUMA node0 CPU(s): 0-71
NUMA node1 CPU(s):
NUMA node2 CPU(s):
NUMA node3 CPU(s):
NUMA node4 CPU(s):
NUMA node5 CPU(s):
NUMA node6 CPU(s):
NUMA node7 CPU(s):
NUMA node8 CPU(s):
6 changes: 6 additions & 0 deletions tests/test_detect_numa
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ assert_raises "$exec_file --node $((num_nodes)) --cpu-list" 1 # num_nodes is inv
# Ensure that the --node flag fails when a negative NUMA node is provided
assert_raises "$exec_file --node -1 --cpu-list" 1

# Ensure that empty NUMA nodes are not counted by default
assert "$exec_file -i \"cat tests/resources/detect_numa/empty_numa.txt\"" 1

# Ensure that --include-empty works as expected
assert "$exec_file --include-empty -i \"cat tests/resources/detect_numa/empty_numa.txt\"" 9

assert_end

0 comments on commit 1153323

Please sign in to comment.