Enable/Disable CPU Cores on Linux

PowerADM.com / Linux / Enable/Disable CPU Cores on Linux

On Linux, the chcpu console tool allows you to temporarily enable or disable processor cores without rebooting.

View information about available CPU cores on a Linux host:

$ lscpu

lscpu - get cpu and cores info

CPU(s): 3
On-line CPU(s) list: 0-2

You can disable any processor cores on Linux except the core zero (CPU0). In order to disable cores 1 and 2, run the command:

$ sudo chcpu -d 1,2

chcpu - disable cpu cores

CPU 1 disabled
CPU 2 disabled

Now check which cores are available:

# lscpu | grep list

On-line CPU(s) list: 0
Off-line CPU(s) list: 1,2

You can also view information about available processor cores:

$ grep "processor" /proc/cpuinfo

To enable processor cores, run the following:

$ sudo chcpu –e 1,2

CPU 1 enabled
CPU 2 enabled

You can also disable a specific CPU core with the command:

$ sudo echo 0 | sudo tee /sys/devices/system/cpu/cpu1/online

We have disabled CPU1 in this example.

Check messages in dmesg:

$ sudo dmesg

[ 2257.253801] smpboot: CPU 1 is now offline

To enable the CPU1 core::

$ sudo echo 1 | sudo tee /sys/devices/system/cpu/cpu1/online

If you would like Linux to always boot with some of the cores disabled, you will need to set the kernel parameter maxcpus=N.

N is the number of CPU cores that should be available in Linux.

$ sudo mcedit /etc/default/grub

Change the line like this:

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash maxcpu=2"

Limit CPU count or disable CPUs in Grub

Update Grub configuration.

$ sudo update-grub

You can use CPU core disabling in Linux when you need to reduce the number of vCPUs for a Linux VM and want to test how the operating system and applications perform with fewer CPUs available.

Linux also allows you to run applications on specific cores using CPU affinity. To find out the affinity of a specific process to the cores:

$ taskset -p 894

pid 894's current affinity mask: 3

$ taskset -cp 894

pid 894's current affinity list: 0,1

taskset - set process CPU affinity mask

This process is allowed to run only on cores 0 and 1.

To allow a running process to run on CPU0 only:

$ sudo taskset -cp 0 894

pid 894's current affinity list: 0,1
pid 894's new affinity list: 0

Or you can allow the Firefox process to use only the cores from 0 to 3:

$ sudo taskset -c 0-3 firefox
Leave a Reply

Your email address will not be published. Required fields are marked *