linux内核中cpu被禁用,linux – 如何禁用一个CPU

这是numactl – 硬件给了我的东西:

ikcWk.png

我想禁用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的结果:

TVoJQ.png

它似乎工作.

现在我认为所有进程都应该在CPU 0 – 15中,因为其他进程已被禁用.

所以我使用现有的进程dbus来验证如下:

ps -Lo psr $(pgrep dbus)

我明白了:

ky0OI.png

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不再允许我远程访问.

我应用了上面讨论的技术,从那以后它一直以非常稳定的方式工作.

标签:linux,cpu,cpu-usage,ps,numa

来源: https://codeday.me/bug/20190810/1636565.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值