什么是CPU亲和力?
所谓CPU亲和力是在多核CPU的系统中,可以指定CPU的某一个或多个核心,分配给指定的进行执行,在Linux中可以设置进程的CPU亲和力,一方面可以减少进程由于内核的上下文切换引起的时延,另一方面可以把一些中断之类的频繁执行的程序限定到某一个或几个核心上,以减小中断对其他进程的实时性的影响。
查看指定进程的CPU亲和力
查看进程CPU亲和力,使用taskset命令:
taskset -cp pid
隔离指定CPU
隔离CPU的核心,可以使得系统关键进程尽量不调度到被隔离的核心,然后有较高实时要求的进程可以指定到被隔离的核心上运行,以提高进程的实时性。
- 在/boot/grub/grub.cfg中添加启动参数:isolcpus=0-2,3,4
- 避免每次都手动该grub.cfg,可以按如下进行操作
First, open the file /etc/default/grub and add the phrase intel_pstate=disable to the line GRUB_CMDLINE_LINUX_DEFAULT. After adding the phrase it should looks like
GRUB_CMDLINE_LINUX_DEFAULT=“quiet splash intel_pstate=disable”
Then, execute the command:
$ update-grub
指定某一个已经运行的进程到特定核心运行
指定进程到CPUx上运行,使用如下命令:
taskset -pc x pid
指定中断的CPU亲和力
中断是影响实时性的一个比较重要的因素,将中断指定到特定的核心运行,可以减少中断对实时进程的影响。
指定对应中断到指定CPU(root用户下):
echo 1 > /proc/irq/121/smp_affinity
cat /sys/devices/system/cpu/isolated
评论区