How to limit CPU count or disable CPUs in a multi core server in RHEL 7 / CentOS 7

There are three different methods using which the number of CPU can be limited in Red Hat Enterprise Linux 7.

Method 1: maxcpus

Using maxcpus parameter : Add kernel parameter maxcpus=N in /boot/grub2/grub.cfg.

IMPORTANT NOTE:
It is not possible to disable CPU0 on Red Hat Enterprise Linux systems.

After enabling above parameter it is not possible to HOT plug more CPU's Online.

maxcpus=    [SMP] Maximum number of processors that an SMP kernel should make use of.  maxcpus=n : n >= 0 limits the kernel to using 'n' processors.  
 n=0 is a special case, it is equivalent to "nosmp", which also disables the IO APIC.

Provide the CPU count which you want to use in your system by using maxcpus variable under 'GRUBCMDLINELINUX' in "/etc/sysconfig/grub" as shown below I am limiting the CPU count to 6

GRUBTIMEOUT=5  
GRUBDISTRIBUTOR="$(sed 's, release .\*$,,g' /etc/system-release)"  
GRUBDEFAULT=saved  
GRUBDISABLESUBMENU=true  
GRUBTERMINALOUTPUT="console"  
GRUBCMDLINELINUX="novga panic=1 numa=off crashkernel=auto noht rhgb quiet console=tty0 maxcpus=6"  
GRUBDISABLERECOVERY="true"

Next regenerate the grub2 configuration file using

# grub2-mkconfig -o /boot/grub2/grub.cfg  
Generating grub configuration file ...  
Found linux image: /boot/vmlinuz-3.10.0-693.5.2.el7.x8664  
Found initrd image: /boot/initramfs-3.10.0-693.5.2.el7.x8664.img  
Found linux image: /boot/vmlinuz-0-rescue-1334797d644549a8aa195f756eaab1e1  
Found initrd image: /boot/initramfs-0-rescue-1334797d644549a8aa195f756eaab1e1.img  
done

Validate your changes

# grep maxcpus /boot/grub2/grub.cfg  
linux16 /vmlinuz-3.10.0-693.5.2.el7.x8664 root=/dev/mapper/os-root ro novga panic=1 numa=off crashkernel=auto rhgb quiet console=tty0 maxcpus=6  
linux16 /vmlinuz-0-rescue-1334797d644549a8aa195f756eaab1e1 root=/dev/mapper/os-root ro novga panic=1 numa=off crashkernel=auto rhgb quiet console=tty0 maxcpus=6

Due to a known BUG in systemd kernel adds an udev rule that automatically sets CPUs to "online" state after they appear in the system hence that must be disabled for maxcpus to work
This rule can be disabled using the below command in /usr/lib/udev/rules.d/40-redhat.rules file

# sed -i 's/^(SUBSYSTEM=="cpu".\*TEST=="online".\*ATTR{online}="1")/#1/' /usr/lib/udev/rules.d/40-redhat.rules

Here we are commenting out below line which enables CPU during the reboot of the system

#SUBSYSTEM=="cpu", ACTION=="add", TEST=="online", ATTR{online}=="0", ATTR{online}="1"

Next rebuild the initramfs

# dracut -f

Next reboot the blade to make the changes affect and validate the number of cpus once the node comes up

# cat /proc/cmdline  
BOOTIMAGE=/vmlinuz-3.10.0-693.5.2.el7.x8664 root=/dev/mapper/os-root ro novga panic=1 numa=off crashkernel=auto rhgb quiet console=tty0 maxcpus=6

# grep processor /proc/cpuinfo | wc -l  
6

# lscpu | grep -i numa  
NUMA node(s):          1  
NUMA node0 CPU(s):     0-5

As you see now we have only 6 cpu cores.

Method 2: nrcpus

Using nrcpus parameter : Add kernel parameter nrcpus=N in /boot/grub2/grub.cfg

IMPORTANT NOTE:
It is not possible to disable CPU0 on Red Hat Enterprise Linux systems.

After enabling above parameter it is not possible to HOT plug more CPU's Online.

nrcpus=    [SMP] Maximum number of processors that an SMP kernel could support.  nrcpus=n : n >= 1 limits the kernel to supporting 'n' processors.

Provide the CPU count which you want to use in your system by using maxcpus variable under GRUBCMDLINELINUX in "/etc/sysconfig/grub" as shown below I am limiting the CPU count to 5

GRUBTIMEOUT=5  
GRUBDISTRIBUTOR="$(sed 's, release .\*$,,g' /etc/system-release)"  
GRUBDEFAULT=saved  
GRUBDISABLESUBMENU=true  
GRUBTERMINALOUTPUT="console"  
GRUBCMDLINELINUX="novga panic=1 numa=off crashkernel=auto rhgb quiet console=tty0 nrcpus=5"  
GRUBDISABLERECOVERY="true"

Next regenerate the grub2 configuration file using

# grub2-mkconfig -o /boot/grub2/grub.cfg  
Generating grub configuration file ...  
Found linux image: /boot/vmlinuz-3.10.0-693.5.2.el7.x8664  
Found initrd image: /boot/initramfs-3.10.0-693.5.2.el7.x8664.img  
Found linux image: /boot/vmlinuz-0-rescue-1be8540dbd0449f4b6c1a94f585eb350  
Found initrd image: /boot/initramfs-0-rescue-1be8540dbd0449f4b6c1a94f585eb350.img  
done

Validate your changes

# grep nrcpus /boot/grub2/grub.cfg  
linux16 /vmlinuz-3.10.0-693.5.2.el7.x8664 root=/dev/mapper/os-root ro novga panic=1 numa=off crashkernel=auto noht rhgb quiet console=tty0 nrcpus=5  
linux16 /vmlinuz-0-rescue-1be8540dbd0449f4b6c1a94f585eb350 root=/dev/mapper/os-root ro novga panic=1 numa=off crashkernel=auto rhgb quiet console=tty0 nrcpus=5

Before going for reboot lets check the available cores

# grep processor /proc/cpuinfo | wc -l  
16

Next reboot the server to activate the changes
Once the system comes up validate the cpu count active on the system

# grep processor /proc/cpuinfo  
processor       : 0  
processor       : 1  
processor       : 2  
processor       : 3  
processor       : 4

Method 3

Manually disable individual CPU using "/sys/devices/system/cpu/cpu/online" file

IMPORTANT NOTE:

This method is temporary and will not be persistent across reboot.

Below article contains more details on the steps to use this method

How to disable or enable hyper threading on my Linux server

I hope the article was useful.