linux cpu – Setting Process Affinity with Taskset Fails

cpulinux

I'm attempting to limit a process to a given number of CPU cores. According to the taskset man page and this documentation, the following should work:

[fedora@dfarrell-opendaylight-cbench-devel ~]$ taskset -pc 0 <PID>
pid 24395's current affinity list: 0-3
pid 24395's new affinity list: 0

To put it simply – this doesn't work. Putting the process under load and watching top, it sits around 350% CPU usage (same as without taskset). It should max out at 100%.

I can properly set affinity via taskset -c 0 <cmd to start process> at process spawn time. Using cpulimit -p <PID> -l 99 also kinda-works. In both cases, putting the process under the same load results in it maxing out at 100% CPU usage.

What's going wrong here?

Best Answer

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.

Related Question