导读 在具有多处理器的系统上禁用/启用处理器(动态)的过程是什么?如何将服务器限制为仅“N”个 CPU?在 CentOS/RHEL 中有三种方法可以限制 CPU 的数量。


在具有多处理器的系统上禁用/启用处理器(动态)的过程是什么?如何将服务器限制为仅“N”个 CPU?

在 CentOS/RHEL 中有三种方法可以限制 CPU 的数量:

使用 maxcpus 参数(RHEL/CentOS 6)
使用 nr_cpus 参数 (RHEL/CentOS 6,7)
在线禁用 CPU (RHEL/CentOS 6,7)

1. 使用maxcpus参数

此方法适用于 RHEL/CentOS 6 系统。如果在 RHEL/CentOS 7 系统中使用它可能会失败。尽管在较新版本的 RHEL 7 系统中,此错误已得到修复。

您可以在/boot/grub/grub.conf中添加内核参数maxcpus=N或在引导时添加到内核行。例如,要限制服务器仅使用 2 个 CPU,你可以使用以下方法:

# 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

注意:不能在 Red Hat Enterprise Linux 系统上禁用 CPU。

当使用 maxcpus 时,它将从所有可用的物理 CPU 中获取 CPU。例如,在具有两个双核 CPU 的系统上,maxcpus=2 将从每个物理 CPU 中获取一个 CPU。要了解正在使用的物理 CPU ID,可以使用下面的查询:

# cat /sys/devices/system/cpu/cpu*/topology/physical_package_id
使用nr_cpus参数

对于 CentOS/RHEL 6,在/boot/grub/grub.conf或引导时的内核行中添加内核参数nr_cpus=N 。例如,下面的条目会将服务器限制为只有 2 个 CPU。

# 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

对于 CentOS/RHEL 7:

(1) 对于 RHEL 7 系统,将nr_cpus=N参数添加到“ /etc/sysconfig/grub ”中的“

GRUB_CMDLINE_LINUX ”行,如下所示。

# 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= “串行--speed=115200”
GRUB_CMDLINE_LINUX="console=ttyS0,115200 console=tty0 vconsole.font=latarcyrheb-sun16 crashkernel=auto nr_cpus=2 "
GRUB_DISABLE_RECOVERY="true"
(2) 使用grub2-mkconfig命令重新生成/boot/grub2/grub.cfg文件。
# 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) 验证grub配置文件中nr_cpu参数的入口。
# 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 控制台=ttyS0,115200 控制台=tty0 vconsole.font=latarcyrheb-sun16 crashkernel=auto nr_cpus=2
...
禁用CPU在线

禁用 CPU 内核:

(1) 在运行时,可以使用以下命令禁用 cpu 内核。例如对于 4 核系统,我们可以禁用 3 个 CPU

如下所示:

# 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) 要验证您是否禁用了 3 个核心并且只启用了 1 个核心,请使用以下命令:
# grep "processor" /proc/cpuinfo
processor : 0

重新启用 CPU 内核:

(1) 可以通过以下命令重新激活 CPU 内核:
# 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) 再次验证 /proc/cpuinfo 中的 4 个启用核心的核心。
# grep "processor" /proc/cpuinfo
processor : 0
processor : 1
processor : 2
processor : 3

原文来自:https://os.51cto.com/article/711942.html

本文地址:https://www.linuxprobe.com/centos-rhel-cpu.html编辑:王华超,审核员:逄增宝

Linux命令大全:https://www.linuxcool.com/

Linux系统大全:https://www.linuxdown.com/

红帽认证RHCE考试心得:https://www.rhce.net/