affinitytaskset

Set the process affinity on a large number of processes


I'm running taskset over a large number of pids (500+). The total time is 30s+. Is there a fast way to set many pids to the same cpu? It doesn't improve performance much to run the taskset processes in parallel.[1] Thanks!

[1] I tried both backgrounding via & and parallelization via gnu parallel


Solution

  • Running taskset on a single pid takes 2 ms on my computer. GNU Parallel has an overhead of 2-10 ms, so that will slow down running them. On my system I can run 500 tasksets in 5 seconds:

    seq 500 | time parallel taskset -p {} $$
    

    (Obviously you will want to change the input to PIDs and not masks, and the command template to PIDs).

    So I am puzzled if your system takes > 30s to do the same.

    If the 5 secs is too long, xargs has fewer safety features, but is faster:

    seq 500 | time xargs -P4 -n1 -I{} taskset -p {} $$
    

    If this is still too slow, you are looking at making your own C-program.