在一个多core 服务器上,怎么限制RHEL识别到的cpu的数量?
来源于:
https://access.redhat.com/solutions/325593
How to limit number of CPU's in Red Hat Enterprise Linux on a multi-core server
适用于:
Red Hat Enterprise Linux (RHEL) 5
Red Hat Enterprise Linux (RHEL) 6
Red Hat Enterprise Linux (RHEL) 7
决议
Note : It is not possible to disable CPU0 on Red Hat Enterprise Linux systems.
There are three ways to restrict number of CPU's
[1] Using maxcpus parameter : Add kernel parameter maxcpus=N
in /boot/grub/grub.conf
or to the kernel line at boot time.
For Eg : Below entry will restrict server to only 2 CPU's.
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
[2] Using nr_cpus parameter : Add kernel parameter nr_cpus=N
in /boot/grub/grub.conf
or to the kernel line at boot time.
For Eg : Below entry will restrict server to only 2 CPU's.
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
NOTE : After enabling above parameter it is not possible to HOT plug more CPU's Online.
NOTE : Supported in Red Hat Enterprise Linux 7
[3] Disabling CPU Online : At runtime it is possible to disable cpu cores with the following commands (Assuming a 4 core system):
# echo 0 > /sys/devices/system/cpu/cpu3/online
# echo 0 > /sys/devices/system/cpu/cpu2/online
# echo 0 > /sys/devices/system/cpu/cpu1/online
- Number of cores can be verified with the following command :-
# grep "processor" /proc/cpuinfo
processor : 0
- The cpu cores can be reactivated again by below command. These settings are not persistent across reboot.
# echo 1 > /sys/devices/system/cpu/cpu3/online
# echo 1 > /sys/devices/system/cpu/cpu2/online
# echo 1 > /sys/devices/system/cpu/cpu1/online
# grep "processor" /proc/cpuinfo
processor : 0
processor : 1
processor : 2
processor : 3
NOTE : Supported in Red Hat Enterprise Linux 7
根源
- From kernel Documentation
nr_cpus= [SMP] 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= [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.
nosmp [SMP] Tells an SMP kernel to act as a UP kernel, and disable the IO APIC. legacy for "maxcpus=0".