我正在尝试禁用我服务器的某些CPU.
我找到了这个链接:https://www.cyberciti.biz/faq/debian-rhel-centos-redhat-suse-hotplug-cpu/linux-turn-on-off-cpu-core-commands/,它提供了一个方法如下:
我想禁用16到63之间的所有CPU,所以我编写了一个名为opCPUs.sh的脚本,如下所示:
#!/bin/bash
for i in {16..63}; do
if [[ "$1" == "enable" ]]; then
echo 1 > /sys/devices/system/cpu/cpu$i/online
elif [[ "$1" == "disable" ]]; then
echo 0 > /sys/devices/system/cpu/cpu$i/online
else
echo 'illegal parameter'
fi
done
grep "processor" /proc/cpuinfo
然后我执行它:./ opCPUs.sh disable,我可以在脚本中看到grep的结果:
它似乎工作.
现在我认为所有进程都应该在CPU 0 – 15中,因为其他进程已被禁用.
所以我使用现有的进程dbus来验证如下:
ps -Lo psr $(pgrep dbus)
psr告诉我进程正在运行哪个CPU,对吧?如果是这样,我已经禁用了CPU 60,CPU 52等,为什么他们还在这里?
解决方法:
除了@Yves的回答,你实际上可以使用isolcpus内核参数.
要使用Debian或Ubuntu禁用第4个CPU /核心(CPU 3):
在/ etc / default / grub中将isolcpus = 3添加到GRUB_CMDLINE_LINUX_DEFAULT
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash isolcpus=3"
跑
sudo update-grub
重新启动服务器.
isolcpus — Isolate CPUs from the kernel scheduler.
Synopsis isolcpus= cpu_number [, cpu_number ,…]
Description Remove the specified CPUs, as defined by the cpu_number
values, from the general kernel SMP balancing and scheduler
algroithms. The only way to move a process onto or off an “isolated”
CPU is via the CPU affinity syscalls. cpu_number begins at 0, so the
maximum value is 1 less than the number of CPUs on the system.This option is the preferred way to isolate CPUs. The alternative,
manually setting the CPU mask of all tasks in the system, can cause
problems and suboptimal load balancer performance.
有趣的是,这个内核参数的使用可以留出一个CPU,以便稍后将CPU亲和性用于一个进程/将进程固定到CPU,从而确保在该CPU上不再运行用户进程.
此外,还可以使服务器更稳定,保证具有很高负载的特定进程将有自己的CPU使用.在看到这个设置之前,我已经看到Meru用他们的基于Linux的控制器做这件事.
然后,将进程分配给第四个CPU(CPU 3)的相关命令是:
sudo taskset -cp PID
taskset
is used to set or retrieve the CPU affinity of a running
process given its PID or to launch a new COMMAND with a given CPU
affinity. CPU affinity is a scheduler property that “bonds” a process
to a given set of CPUs on the system. The Linux scheduler will honor
the given CPU affinity and the process will not run on any other CPUs.
Note that the Linux scheduler also supports natural CPU affinity: the
scheduler attempts to keep processes on the same CPU as long as
practical for performance reasons. Therefore, forcing a specific CPU
affinity is useful only in certain applications.
摘要
有几种技术适用于这个问题:
在grub中设置isolcpus = 4并重新启动可以永久禁用第5个CPU / CPU 4用于用户进程;
echo 0> / sys / devices / system / cpu / cpu4 / online禁用第5个CPU / CPU 4,它仍将继续用于已分配给它的进程,但不再为CPU 4分配新进程;
taskset -c 3 ./MyShell.sh将强制MyShell.sh分配给第4个CPU / CPU 3,而第4个CPU仍然可以接受其他用户登陆进程,如果isolcpus没有将其排除在外.
PS.有趣的是,我在场上使用isolcpus / taskset的最好例子是一个非常繁忙的网站的SSL前端,它每隔几周就会变得不稳定,Ansible / ssh不再允许我远程访问.
我应用了上面讨论的技术,从那以后它一直以非常稳定的方式工作.