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
Save rothwerx/b60f1fa7b543e8761b3c to your computer and use it in GitHub Desktop.
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