RHEL: Limit the number of CPUs that server should make use of

# Tested on RHEL 7
 
# Notes mainly from Red Hat website
# For practical reasons, I will consider the terms “core”, “processor” and “cpu”
# equivalents from the Operating System point of view.
 
# On a server like this, with four processors:
 
root@myserver:/root#> grep ^proc /proc/cpuinfo
processor       : 0
processor       : 1
processor       : 2
processor       : 3
 
# CPU number can be limited by adding the ‘maxcpu‘ parameter to the grub file like this
# (or to the kernel line at boot time):
 
vi /etc/default/grub
[…]
GRUB_CMDLINE_LINUX=”crashkernel=auto rd.lvm.lv=vg_root/lv_root rd.lvm.lv=vg_root/lv_swap net.ifnames=0 rhgb quiet maxcpus=2
[…]
 
# then regenerate the GRUB configuration file:
 
grub2-mkconfig -o /boot/grub2/grub.cfg
shutdown -r now
 
 
# On RHEL 6:
vi /boot/grub/grub.conf
#
# […]
# title Red Hat Enterprise Linux Server (2.6.18-238.el5)
#     root (hd0,0)
#     kernel /vmlinuz-2.6.18-238.el5 ro root=/dev/VolGroup00/LogVol00 rhgb quiet maxcpus=2
#     initrd /initrd-2.6.18-238.el5.img
# […]
 
 
# If that doesn’t work, ‘nr_cpus‘ parameter may be used instead:
 
vi /etc/default/grub
[…]
GRUB_CMDLINE_LINUX=”crashkernel=auto rd.lvm.lv=vg_root/lv_root rd.lvm.lv=vg_root/lv_swap net.ifnames=0 rhgb quiet nr_cpus=2
[…]
 
grub2-mkconfig -o /boot/grub2/grub.cfg
shutdown -r now
 
 
root@myserver:/root#> grep ^proc /proc/cpuinfo
processor       : 0
processor       : 1
 
 
 
# For information
# ——————————————————————————————
#
nr_cpus: Maximum number of processors that an SMP kernel could support. nr_cpus=n : n >= 1
#          limits the kernel to supporting ‘n’ processors. Later in runtime you can not use
#          hotplug cpu feature to put more cpu back to online. Just like you compile the
#          kernel NR_CPUS=n
#
maxcpus: 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.
 
 
 
# Another way of enabling/disabling CPUs on a live system is by using the ‘chcpu‘ command
# or by directly modifying the content of then online sysfs attribute of the CPU:
# /sys/devices/system/cpu/cpuX/online
#
# *** Note that theses settings are not persistent across reboot !!!!
 
 
root@myserver:/root#> grep ^proc /proc/cpuinfo
processor       : 0
processor       : 1
processor       : 2
processor       : 3
 
root@myserver:/root#> grep . /sys/devices/system/cpu/cpu[0-9]*/online | sort -t/ -k 6.4n
/sys/devices/system/cpu/cpu0/online:1
/sys/devices/system/cpu/cpu1/online:1
/sys/devices/system/cpu/cpu2/online:1
/sys/devices/system/cpu/cpu3/online:1
 
 
chcpu -e|-d <N>
      <N>: is the number of the logical CPU.
      -e:  sets an offline CPU online.
      -d:  sets an online CPU offline.
 
# Set the logical CPU with number 2 offline:
 
root@myserver:/root#> chcpu -d 2
CPU 2 disabled
 
root@myserver:/root#> grep ^proc /proc/cpuinfo
processor       : 0
processor       : 1
processor       : 3
 
# Set the logical CPU with number 2 back online:
 
root@myserver:/root#> chcpu -e 2
CPU 2 enabled
 
root@myserver:/root#> grep ^proc /proc/cpuinfo
processor       : 0
processor       : 1
processor       : 2
processor       : 3
 
 
# The following commands achieve the same results by writing 0 (offline) / 1 (online)
# to the online sysfs attribute of the CPU:
 
root@myserver:/root#> echo 0 > /sys/devices/system/cpu/cpu2/online
 
root@myserver:/root#> grep ^proc /proc/cpuinfo
processor       : 0
processor       : 1
processor       : 3
 
root@myserver:/root#> echo 1 > /sys/devices/system/cpu/cpu2/online
 
root@myserver:/root#> grep ^proc /proc/cpuinfo
processor       : 0
processor       : 1
processor       : 2
processor       : 3

ZOSTAW ODPOWIEDŹ

Please enter your comment!
Please enter your name here