There are three ways to restrict number of CPU’s in CentOS/RHEL.

 

  • Using maxcpus parameter (RHEL/CentOS 6)
  • Using nr_cpus parameter (RHEL/CentOS 6,7)
  • Disabling CPU Online (RHEL/CentOS 6,7)

 

1. Using maxcpus parameter

 

This method works with RHEL/CentOS 6 systems. It may fail if you use it in RHEL/CentOS 7 systems. Although in newer version of RHEL 7 system this bug has been fixed.

 

You can add the kernel parameter maxcpus=N in /boot/grub/grub.conf or to the kernel line at boot time. For example to limit the server to use only 2 CPU’s use the below entry in the file

 # 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=3
    initrd /initrd-2.6.18-238.el5.img

 

NOTE:

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

 

When maxcpus is used, it will take the CPUs from all the available physical CPUs. For example on a system with two dual core CPUs, maxcpus=2 will take one CPU from each physical CPUs. To know the physical CPU IDs in use:

# cat /sys/devices/system/cpu/cpu*/topology/physical_package_id 

 

2. Using nr_cpus parameter

 

a. For CentOS/RHEL 6

 

Add kernel parameter nr_cpus=N in /boot/grub/grub.conf or to the kernel line at boot time. For example, Below entry will restrict server to only 2 CPU’s.

# 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 nr_cpus=2
    initrd /initrd-2.6.18-238.el5.img 

 

b. For CentOS/RHEL 7

 

1. For RHEL 7 systems add the nr_cpus=N parameter to the “GRUB_CMDLINE_LINUX” line in “/etc/sysconfig/grub” as shown below.

# cat /etc/default/grub 
GRUB_TIMEOUT=1
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL="serial console"
GRUB_SERIAL_COMMAND="serial --speed=115200"
GRUB_CMDLINE_LINUX="console=ttyS0,115200 console=tty0 vconsole.font=latarcyrheb-sun16 crashkernel=auto nr_cpus=2"
GRUB_DISABLE_RECOVERY="true" 

 

2. Use the grub2-mkconfig command to regenrate the /boot/grub2/grub.cfg file.

 # grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-693.21.1.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-693.21.1.el7.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-693.17.1.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-693.17.1.el7.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-693.11.6.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-693.11.6.el7.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-693.11.1.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-693.11.1.el7.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-693.5.2.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-693.5.2.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-f9afeb75a5a382dce8269887a67fbf58
Found initrd image: /boot/initramfs-0-rescue-f9afeb75a5a382dce8269887a67fbf58.img
done

 

3. Verify the entry of the nr_cpu parameter in the grub configuration file.

# grep linux16 /boot/grub2/grub.cfg
	linux16 /boot/vmlinuz-3.10.0-693.21.1.el7.x86_64 root=UUID=0f790447-ebef-4ca0-b229-d0aa1985d57f ro console=ttyS0,115200 console=tty0 vconsole.font=latarcyrheb-sun16 crashkernel=auto nr_cpus=2  

 

3. Disabling CPU Online

 

Disabling CPU cores

 

1. At runtime it is possible to disable cpu cores with the following commands. For example for a 4 core system, we can disable 3 CPUs as shown below.

 # echo 0 > /sys/devices/system/cpu/cpu3/online
# echo 0 > /sys/devices/system/cpu/cpu2/online
# echo 0 > /sys/devices/system/cpu/cpu1/online

2. To verify if you have disable 3 cores and left with only 1 core enabled, use the below command.

# grep "processor" /proc/cpuinfo
processor	: 0 

 

Enabling CPU cores back

1. The cpu cores can be reactivated again by below command.

# echo 1 > /sys/devices/system/cpu/cpu3/online
# echo 1 > /sys/devices/system/cpu/cpu2/online
# echo 1 > /sys/devices/system/cpu/cpu1/online 

 

2. Verify again for 4 core enabled cores in /proc/cpuinfo.

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

 

NOTE:

These settings are not persistent across reboot.

Hai trovato utile questa risposta? 0 Utenti hanno trovato utile questa risposta (0 Voti)