Disabling CPU cores can be beneficial if we want to optimize power consumption and troubleshoot performance issues. Fortunately, Linux provides methods to manage CPU cores to control the resources efficiently. In this tutorial, we’ll cover how we can disable CPU cores temporarily and on boot. We’ll also discuss how to restrict a process to certain cores.
2. Disable CPU Cores
Before we disable the CPU cores, we should first check for the available CPU cores. For this purpose, we’ll use the lscpu command:
$ lscpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Address sizes: 36 bits physical, 48 bits virtual Byte Order: Little Endian CPU(s): 4 On-line CPU(s) list: 0-3 ...
Notably, the machine has four cores, and all of them are online. In the next sections, we disable these cores.
2.1. Disable Cores Temporarily
We can temporarily disable CPU cores by modifying their corresponding “online” file in the /sys/devices/system/cpu/cpuN directory. So, we can replace cpuN with its respective CPU number. First, we’ll list the CPUs in the directory:
$ ls -l /sys/devices/system/cpu total 0 drwxr-xr-x 6 root root 0 Jun 14 16:28 cpu0 drwxr-xr-x 6 root root 0 Jun 14 16:28 cpu1 drwxr-xr-x 6 root root 0 Jun 14 16:28 cpu2 drwxr-xr-x 6 root root 0 Jun 14 16:28 cpu3 ...
By default, these directories contain a file named “online” that will either contain 1 or 0, where 1 is active and 0 is inactive. Therefore, we write 0 to the “online” files of the cores we want to disable:
$ echo "0" > /sys/devices/system/cpu/cpu1/online
Alternatively, we can also go through a range of CPUs with a loop:
for x in /sys/devices/system/cpu/cpu[1-2]*/online; do echo 0 > "$x" done
This will disable the cpu1 and cpu2 cores.
2.2. Disable Cores on Boot
We can also disable CPU cores on boot by modifying the GRUB configuration. So, let’s open up /etc/default/grub and locate the line GRUB_CMDLINE_LINUX_DEFAULT:
$ grep -i linux_default /etc/default/grub GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
We’ll need to append or prepend the maxcpus=N option to the string, where N is the number of CPUs we need to be enabled:
sudo sed -i 's/GRUB_CMDLINE_LINUX_DEFAULT="/&maxcpus=2 /' /etc/default/grub
Next, let’s update the GRUB configuration for the changes to take effect:
$ sudo update-grub
Now, once we reboot the system, the machine will use only two cores.
3. Disable Cores for a Specific Process Using taskset
taskset is a command-line utility in Linux used to set or retrieve the CPU affinity of a process. It allows you to bind a specific process to a set of CPU cores, restricting its execution to those cores. By default, it’s included in the util-linux package, so it should be installed on most Linux distributions. We can check the CPU affinity of a process through the -p option:
$ taskset -p <PID>
To easily retrieve the PID of a process, use pgrep:
$ taskset -p $(pgrep gnome-shell) pid 988's current affinity mask: f
Notably, the output indicates that the process with ID 988 has an affinity mask represented by the hexadecimal value “f”. The affinity mask specifies the CPU cores or processors that the process is allowed to run on. Therefore, we can restrict a process to a number of cores. For instance, let’s limit the gnome-shell process to Core 0 and Core 1:
$ taskset -p --cpu-list 0,1 $(pgrep gnome-shell) pid 988's new affinity mask: 0,1
In the same way, “0” will assign only a single core to a process:
$ taskset -p --cpu-list 0 $(pgrep gnome-shell) pid 988's new affinity mask: 0
Finally, we can also specify a range to –cpu-list if we have a lot of CPUs:
$ taskset -p --cpu-list 0-3 $(pgrep gnome-shell) pid 988's new affinity mask: 0-3
This will effectively limit the process to the first four cores.
In this article, we learned how to disable CPU cores in Linux. We went through a couple of methods for disabling CPU cores temporarily and permanently. Finally, we leveraged the taskset command to restrict a process to a specific CPU core or set of CPU cores.