2

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

Will
  • 1,171
  • 2
  • 14
  • 26

1 Answers1

3

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.

Ole Tange
  • 31,768
  • 5
  • 86
  • 104
  • Is that C program long or hard? If not, do you mind including an example? – Will Jan 16 '20 at 16:12
  • I imagine it is relatively straight forward if you are a proficient C programmer (the magic part is `sched_getaffinity`and `sched_setaffinity`). I (being a rusty C programmer) would have to spend multiple hours getting it to work. – Ole Tange Jan 17 '20 at 06:28
  • Can I ask why you need something that runs faster than the 0.5s which `xargs` supplies? – Ole Tange Jan 17 '20 at 06:28
  • Ok, thanks. I've got a lot of processes, and 0.5 seconds * 500 processes is too long – Will Jan 17 '20 at 16:51
  • No. We are talking 0.5 seconds to do all 500 processes (i.e. 1 ms per process). Do you really need faster than 1 ms per process? – Ole Tange Jan 18 '20 at 02:07
  • In a time test, 100 calls to `xargs -n1 -I{} sudo /bin/taskset -p -c 0 {}` takes ~5s. Is there something wrong here? – Will Jan 22 '20 at 14:19
  • I figured it out: taskset is being run with sudo. taskset itself takes <2ms, but sudo takes >50ms. On my machine, I only have permission to run taskset as root (all other commands are banned). Is there a way to speed this up? Thank you! – Will Jan 22 '20 at 15:13
  • If it is a multicore machine, you can probably run multiple `sudo`s in parallel. But I seen no way you can avoid paying the 50 ms price for each job. – Ole Tange Jan 22 '20 at 19:12