0

Is there a way to run a process at (for example) 10 CPU cores and later override the CPU core usage to 5?

closed as unclear what you're asking by Jeff Schaller, Kusalananda, GAD3R, countermode, Anthony Geoghegan Jan 25 '17 at 15:27

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

2

Not really.

This can certainly not be done in a portable way. You didn't specify which system you're trying to do this on. I'll assume it's Linux, since that's what I know best, but feel free to clarify.

The Linux kernel has two system calls dealing with "CPU affinity", sched_setaffinity() and sched_getaffinity(). These can be used to tell the kernel which cores a process may run on. If the number of CPUs is lower than the number of threads the process is currently using, then by necessity it will start using less cores. The problem with this method, however, is that it forces a particular set of CPU cores for the process to run on. If it's a CPU-intensive job, then that means these cores are liable to heat up; if the system does not have enough cooling to cool down the CPU enough for that to be less problematic, then usually the solution is that the kernel re-schedules the process in question to a different core, allowing the one just used to cool down a bit in the mean time. By forcing a set of cores to run on, this method no longer works, and the kernel will then have to fall back to changing the CPU frequency instead. If you're trying to limit cores because there are other processes running too that you want to give some CPU time, however, then this probably doesn't matter as much.

The sched_setaffinity() and sched_getaffinity() system calls can be accessed from the command line with the taskset command:

taskset -p 0x3 $PID

will tell the kernel that the process with pid $PID may, from now on, only run on processor cores 0 and 1. (the 0x3 is a bit mask, where bit 1 refers to core 0, and bit 2 refers to core 1).

The affinity mask is binary and is entered as a hexadecimal number, you can specify what CPUs to utilize by using the full table for a quad-core processor below:

0x1 - cpu0
0x2 - cpu1
0x3 - cpu0 and cpu1
0x4 - cpu2
0x5 - cpu0 and cpu2
0x6 - cpu1 and cpu2
0x7 - cpu0, cpu1 and cpu2
0x8 - cpu3
0x9 - cpu0 and cpu3
0xA - cpu1 and cpu3
0xB - cpu0, cpu1 and cpu3
0xC - cpu2 and cpu3
0xD - cpu0, cpu2 and cpu3
0xE - cpu1, cpu2 and cpu3
0xF - cpu0, cpu1, cpu2 and cpu3
0

I found a way!!

cpulimit works pretty well for this:

https://github.com/opsengine/cpulimit

  • 2
    This limits the percentage of CPU used, not the number of cores used. – Kusalananda Jan 25 '17 at 12:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.