On Linux, how do I the check CPU affinity of a process and its threads?
On Linux, how do I the check CPU affinity of a process and its threads?
Solution 1:
It's pretty simple. Gather the all process id and thread ids then call program taskset
. Like,
taskset -cp 2
taskset -cp 4
…
print all by process name
taskset
has the --all-tasks
optional argument
taskset --all-tasks -p $(pgrep java)
For some processes, the --all-tasks
doesn't appear to print all of the child processes (in my testing, it didn't print child processes for kthreadd
, I'm not sure why that is).
print all by process name helper script
Here is a short Linux shell script to print CPU affinity for a process by name and all of it's child threads.
#!/usr/bin/env bash
set -eu
pname=${1:-kthreadd} # default to 'kthreadd'
for pid in $(pgrep "${pname}"); do
echo "PID: ${pid} (${pname})"
for tid in $(pgrep -P "${pid}" | tr '\n' ' '); do
taskset -cp "${tid}"
done
done
Outputs
PID: 2 (kthreadd)
pid 4's current affinity list: 0
pid 6's current affinity list: 0
pid 7's current affinity list: 0
pid 8's current affinity list: 0
…
Tested on Ubuntu 12, bash 4.