Bash: Ensure processes run on allowed CPUs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# function to expand the range of allowed CPUs | |
range_expand () ( | |
IFS=, | |
set -- $1 | |
n=$# | |
for element; do | |
if [[ $element =~ ^(-?[0-9]+)-(-?[0-9]+)$ ]]; then | |
set -- "$@" $(eval echo "{${BASH_REMATCH[1]}..${BASH_REMATCH[2]}}") | |
else | |
set -- "$@" $element | |
fi | |
done | |
shift $n | |
echo "$@" | |
) | |
# Loop through all processes | |
for i in $(ps -A -o pid=); do | |
allowed=$(range_expand $(grep Cpus_allowed_list /proc/${i}/status | awk '{ print $2 }')) | |
running=$(set -- $(</proc/${i}/stat); echo ${39}) | |
if [[ "${allowed/$running}" = "${allowed}" ]] && [[ "${allowed}" ]]; then | |
printf "Process %d running on CPU %d but only allowed on %s\n" "$i" "$running" "$allowed" | |
fi | |
done 2>/dev/null # otherwise we get errors for some short lived processes like the grep invocation used |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment