Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save centminmod/4b724424334d9d28eeb28703febc87aa to your computer and use it in GitHub Desktop.
Save centminmod/4b724424334d9d28eeb28703febc87aa to your computer and use it in GitHub Desktop.
Invokes `taskset` on the child threads of the specified processes.
#!/bin/bash
# Copyright 2013-2019 Neomantra BV. All rights reserved.
# Released under the MIT License.
usage()
{
cat >&2 <<EOF
usage: $0 [-h] [-v] [-c cpulist] ppid1 [ppid2 [...]]
Given a list of parent process IDs, this script finds their child
threads using the /proc filesystem. It then tasksets these child threads
(but not the parent), to the cpulist.
If -c is not specified, then it just prints the thread IDs.
Must be run as the user that owns those cmds (or root).
EOF
}
unset THE_CPULIST
VERBOSE=
while getopts "hv?c:" opt ; do
case "$opt" in
c) THE_CPULIST=$OPTARG ;;
v) VERBOSE=1 ;;
h) usage ; exit ;;
[?]) usage ; exit ;;
esac
done
shift $((OPTIND-1))
# Needs ppid arguments
if (($# == 0)) ; then
usage
exit 1
fi
# Iterate the pids, taskset-ing the child processes
for ppid in "$@" ; do
TASKPIDS=$(ls -1 /proc/$ppid/task | grep -v $ppid)
if [[ -z "$THE_CPULIST" ]] ; then
echo "$TASKPIDS"
else
for pid in $TASKPIDS
do
if [[ -z $VERBOSE ]] ; then
taskset -pc "$THE_CPULIST" $pid > /dev/null
else
taskset -pc "$THE_CPULIST" $pid
fi
done
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment