2

We have physical Linux machine with 16 cpus

lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                16

We want to disable 14 cpus on that machine , so its actually like we have linux machine with only 2 cpu

In order to achieve this , I did the following

echo 0 > /sys/devices/system/cpu/cpu15/online
echo 0 > /sys/devices/system/cpu/cpu14/online
echo 0 > /sys/devices/system/cpu/cpu13/online
echo 0 > /sys/devices/system/cpu/cpu12/online
echo 0 > /sys/devices/system/cpu/cpu1/online
echo 0 > /sys/devices/system/cpu/cpu11/online
echo 0 > /sys/devices/system/cpu/cpu10/online
echo 0 > /sys/devices/system/cpu/cpu9/online
echo 0 > /sys/devices/system/cpu/cpu8/online
echo 0 > /sys/devices/system/cpu/cpu7/online
echo 0 > /sys/devices/system/cpu/cpu6/online
echo 0 > /sys/devices/system/cpu/cpu5/online
echo 0 > /sys/devices/system/cpu/cpu4/online
echo 0 > /sys/devices/system/cpu/cpu5/online
echo 0 > /sys/devices/system/cpu/cpu4/online

and then also run mpstat

and we get

08:26:13 AM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
08:26:13 AM  all    0.34    0.00    0.09    0.04    0.00    0.00    0.00    0.00    0.00   99.53
08:26:13 AM    0    0.42    0.00    0.12    0.01    0.00    0.00    0.00    0.00    0.00   99.45
08:26:13 AM    1    0.37    0.00    0.10    0.01    0.00    0.00    0.00    0.00    0.00   99.52
08:26:13 AM    2    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
08:26:13 AM    3    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
08:26:13 AM    4    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
08:26:13 AM    5    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
08:26:13 AM    6    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
08:26:13 AM    7    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
08:26:13 AM    8    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
08:26:13 AM    9    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
08:26:13 AM   10    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
08:26:13 AM   11    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
08:26:13 AM   12    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
08:26:13 AM   13    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
08:26:13 AM   14    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
08:26:13 AM   15    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00

enter image description here

We can see that only 2 cpu’s are online

But I don’t sure if this approach is really works and I need advice For example how to be sure that any PID's will not use the other 14 cpu’s that are disabled?

Let me know if my procedure , disable the 14 cpu’s , and process will use only 2 cpus

6
  • 2
    If this is for licensing purposes, you’d be better off talking to the vendor... Feb 24 at 9:02
  • 2
    on a long term purpose (e.g. not a temporary performance test), I would suggest BIOS disabling.
    – Archemar
    Feb 24 at 9:06
  • @ Stephen Kitt , yes its about licensing purposes , we are pay per core quantity , so this is the reason that we want to know the right approach that limit the CPU , from Vendor side when we limit the CPU and application not used the disabled cpu then its ok from his side , but the question is about the procedure that I described , is it really works ?
    – yael
    Feb 24 at 9:08
  • @Archemar , can you give example how to disable CPU from BIOS HW side ?
    – yael
    Feb 24 at 9:10
  • this depend on hardware, you need to boot to bios (before actual OS boot) and disable hyperthreading (CPU gos from 16 to 8) for instance. In a same situation we end up removing one of the processor. (HP blade 4xx and oracle licencing)
    – Archemar
    Feb 24 at 9:13
3

This might or might not depending on the application.

If the application simply uses APIs to poll the number of available cores, it might not work because the Linux kernel might return all the cores.

However disabling CPU cores in BIOS must work - it depends on your BIOS implementation, so please consult with your motherboard documentation.

If I were you, I'd approach this issue differently: I'd run the app in a VM and allocate the required number of cores to it. This way your host OS will still be able to use the remaining cores.

Lastly you don't need to run echo 14 times.

Here's a simpler version for bash:

echo 0 | sudo tee /sys/devices/system/cpu/cpu{2..15}/online

Lastly make sure you leave two physical cores instead of a single core with HT. To learn your CPU topology run:

lscpu -p

Normally the Linux kernel first sees physical cores, then HT/SMT cores but I'm not sure it's always the case.

4
  • you said also - "it might not work because the Linux kernel might return all the cores" , any other suggestion how to force the cpu for disabled ant not release the cpu even if application want to use it?
    – yael
    Feb 24 at 9:46
  • 1
    If you disable CPU cores in BIOS or use virtualization your app will see the exact number of cores you've allocated for it. Feb 24 at 10:07
  • I already check that but we can because its also required license , so only from OS side
    – yael
    Feb 24 at 10:13
  • Kudos for virtualization, it is way too overlooked and underrated these days.
    – X.LINK
    Feb 24 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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