Setting running process affinity with taskset fails
Update: Newer versions of taskset have a -a
/--all-tasks
option that "operates on all the tasks (threads) for a given pid" and should solve the behavior I show below.
I wrote a Python script that simply spins up some threads and burns CPU cycles. The idea is to test taskset against it, as it's quite simple.
#!/usr/bin/env python
import threading
def cycle_burner():
while True:
meh = 84908230489 % 323422
for i in range(3):
thread = threading.Thread(target=cycle_burner)
print "Starting a thread"
thread.start()
Just running the Python script eats up about 150% CPU usage.
[~/cbench]$ ./burn_cycles.py
Starting a thread
Starting a thread
Starting a thread
Launching my Python script with taskset works as expected. Watching top shows the Python process pegged at 100% usage.
[~/cbench]$ taskset -c 0 ./burn_cycles.py
Starting a thread
Starting a thread
Starting a thread
Interestingly, launching the Python script and then immediately using taskset to set the just-started process' affinity caps the process at 100%. Note from the output that the Linux scheduler finished executing the Bash commands before spawning the Python threads. So, the Python process was started, then it was set to run on CPU 0, then it spawned its threads, which inherited the proper affinity.
[~/cbench]$ ./burn_cycles.py &; taskset -pc 0 `pgrep python`
[1] 8561
pid 8561's current affinity list: 0-3
pid 8561's new affinity list: 0
Starting a thread
[~/cbench]$ Starting a thread
Starting a thread
That result contrasts with this method, which is exactly the same but allows the Python threads to spawn before setting the affinity of the Python process. This replicates the "taskset does nothing" results I described above.
[~/cbench]$ ./burn_cycles.py &
[1] 8996
[~/cbench]$ Starting a thread
Starting a thread
Starting a thread
[~/cbench]$ taskset -pc 0 `pgrep python`
pid 8996's current affinity list: 0-3
pid 8996's new affinity list: 0
What's going wrong here?
Apparently threads spawned before the parent process' affinity is changed don't inherit the affinity of their parent. If someone could edit in a link to documentation that explains this, that would be helpful.
I think you'll need to call taskset once per thread, i.e. use ps -eL
instead of pgrep
and pipe that to taskset -cp 0
ps -eLo cmd,tid | grep python | perl -pe 's/.* (\d+)$/\1/' | xargs -n 1 taskset -cp 0
This calls task set for all thread ids.
try numactl with --physcpubind
(or -C
) instead. The man page says:
... The policy is set for command and inherited by all of its children.
(in recent versions of taskset
there is also an -a
option which Sets or retrieves the CPU affinity of all the tasks (threads) for a given PID.
but it's not clear whether this also works for daughter processes of a task launched with taskset
as opposed to modifying an already running process)