Skip to content

Instantly share code, notes, and snippets.

@rothwerx
Created July 13, 2015 21:42
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Bash: Ensure processes run on allowed CPUs
#!/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