一、前言
1.1、什么是中断?
由于接收来自外围硬件(相对于CPU和内存)的异步信号或者来自软件的同步信号,而进行相应的硬件、软件处理;发出这样的信号称为进行中断请求(interrupt request, IRQ)
1.2、硬中断与软中断?
硬中断:外围硬件发给CPU或者内存的异步信号就称之为硬中断
软中断:由软件系统本身发给操作系统内核的中断信号,称之为软中断。通常是由硬中断处理程序或进程调度程序对操作系统 内核的中断,也就是我们常说的系统调用(System Call)
1.3、硬中断与软中断之区别与联系?
硬中断是有外设硬件发出的,需要有中断控制器之参与。其过程是外设侦测到变化,告知中断控制器,中断控制器通过CPU或内存的中断脚通知CPU,然后硬件进行程序计数器及堆栈寄存器之现场保存工作(引发上下文切换),并根据中断向量调用硬中断处理程序进行中断处理
软中断则通常是由硬中断处理程序或者进程调度程序等软件程序发出的中断信号,无需中断控制器之参与,直接以一个CPU指令之形式指示CPU进行程序计数器及堆栈寄存器之现场保存工作(亦会引发上下文切换),并调用相应的软中断处理程序进行中断处理(即我们通常所言之系统调用)
硬中断直接以硬件的方式引发,处理速度快。软中断以软件指令之方式适合于对响应速度要求不是特别严格的场景
硬中断通过设置CPU的屏蔽位可进行屏蔽,软中断则由于是指令之方式给出,不能屏蔽
硬中断发生后,通常会在硬中断处理程序中调用一个软中断来进行后续工作的处理
硬中断和软中断均会引起上下文切换(进程/线程之切换),进程切换的过程是差不多的
1.4、查看中断情况
1、查看中断分布情况即CPU都在哪些设备上干活,干了多少(也可以使用itop工具实时查看)?
[[email protected] ~]$ top
top - 04:28:51 up 87 days, 8:47, 3 users, load average: 4.69, 4.44, 4.17
Tasks: 482 total, 8 running, 474 sleeping, 0 stopped, 0 zombie
Cpu0 : 39.0%us, 29.9%sy, 0.0%ni, 31.2%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu1 : 52.9%us, 27.1%sy, 0.0%ni, 18.8%id, 0.0%wa, 0.0%hi, 1.2%si, 0.0%st
Cpu2 : 37.2%us, 26.9%sy, 0.0%ni, 34.6%id, 0.0%wa, 0.0%hi, 1.3%si, 0.0%st
Cpu3 : 52.3%us, 30.2%sy, 0.0%ni, 16.3%id, 0.0%wa, 0.0%hi, 1.2%si, 0.0%st
Cpu4 : 33.8%us, 29.9%sy, 0.0%ni, 36.4%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu5 : 63.3%us, 24.5%sy, 0.0%ni, 12.2%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu6 : 48.4%us, 20.0%sy, 0.0%ni, 31.6%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu7 : 62.9%us, 20.6%sy, 0.0%ni, 16.5%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu8 : 30.9%us, 20.2%sy, 0.0%ni, 6.4%id, 0.0%wa, 0.0%hi, 42.6%si, 0.0%st
Cpu9 : 34.7%us, 22.1%sy, 0.0%ni, 2.1%id, 0.0%wa, 0.0%hi, 41.1%si, 0.0%st
Cpu10 : 27.2%us, 19.6%sy, 0.0%ni, 9.8%id, 0.0%wa, 0.0%hi, 43.5%si, 0.0%st
Cpu11 : 31.9%us, 22.3%sy, 0.0%ni, 2.1%id, 0.0%wa, 0.0%hi, 43.6%si, 0.0%st
Cpu12 : 54.7%us, 17.9%sy, 0.0%ni, 27.4%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu13 : 63.4%us, 18.8%sy, 0.0%ni, 17.8%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu14 : 56.6%us, 15.0%sy, 0.0%ni, 28.3%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu15 : 31.6%us, 24.2%sy, 0.0%ni, 1.1%id, 0.0%wa, 0.0%hi, 43.2%si, 0.0%st
Cpu16 : 57.7%us, 11.7%sy, 0.0%ni, 30.6%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu17 : 33.7%us, 24.2%sy, 0.0%ni, 1.1%id, 0.0%wa, 0.0%hi, 41.1%si, 0.0%st
Cpu18 : 49.5%us, 16.8%sy, 0.0%ni, 29.0%id, 0.0%wa, 0.0%hi, 4.7%si, 0.0%st
Cpu19 : 31.6%us, 24.2%sy, 0.0%ni, 4.2%id, 0.0%wa, 0.0%hi, 40.0%si, 0.0%st
Cpu20 : 48.6%us, 18.3%sy, 0.0%ni, 33.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu21 : 60.8%us, 22.7%sy, 0.0%ni, 16.5%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu22 : 45.8%us, 18.7%sy, 0.0%ni, 35.5%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu23 : 59.0%us, 22.0%sy, 0.0%ni, 19.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 32862536k total, 32263892k used, 598644k free, 127192k buffers
Swap: 16498680k total, 11336k used, 16487344k free, 23852296k cached
2、通过mpstat -P ALL 2,每隔两秒查看下所有核状态信息,其中 %irq为硬中断,%soft为软中断
[[email protected] ~]$ mpstat -P ALL 2
Linux 2.6.32-431.el6.x86_64 (localhost.localdomain) 08/22/2018 _x86_64_ (24 CPU)
04:12:57 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
04:12:59 AM all 47.69 0.00 21.00 0.00 0.00 12.18 0.00 0.00 19.12
04:12:59 AM 0 43.02 0.00 30.23 0.00 0.00 7.56 0.00 0.00 19.19
04:12:59 AM 1 32.66 0.00 22.61 0.00 0.00 43.22 0.00 0.00 1.51
04:12:59 AM 2 32.49 0.00 19.80 0.00 0.00 45.18 0.00 0.00 2.54
04:12:59 AM 3 33.00 0.00 23.50 0.00 0.00 42.00 0.00 0.00 1.50
04:12:59 AM 4 30.96 0.00 21.32 0.00 0.00 43.65 0.00 0.00 4.06
04:12:59 AM 5 30.81 0.00 23.23 0.00 0.00 41.92 0.00 0.00 4.04
04:12:59 AM 6 30.26 0.00 22.05 0.00 0.00 42.56 0.00 0.00 5.13
04:12:59 AM 7 31.31 0.00 21.72 0.00 0.00 41.41 0.00 0.00 5.56
04:12:59 AM 8 49.76 0.00 24.15 0.00 0.00 0.00 0.00 0.00 26.09
04:12:59 AM 9 53.40 0.00 23.30 0.00 0.00 0.00 0.00 0.00 23.30
04:12:59 AM 10 47.87 0.00 23.22 0.00 0.00 0.47 0.00 0.00 28.44
04:12:59 AM 11 47.34 0.00 25.12 0.00 0.00 0.00 0.00 0.00 27.54
04:12:59 AM 12 61.09 0.00 16.29 0.00 0.00 0.00 0.00 0.00 22.62
04:12:59 AM 13 57.89 0.00 22.49 0.00 0.00 0.00 0.00 0.00 19.62
04:12:59 AM 14 55.81 0.00 20.93 0.00 0.00 0.00 0.00 0.00 23.26
04:12:59 AM 15 59.91 0.00 20.75 0.00 0.00 0.00 0.00 0.00 19.34
04:12:59 AM 16 57.08 0.00 18.26 0.00 0.00 0.00 0.00 0.00 24.66
04:12:59 AM 17 59.35 0.00 20.09 0.00 0.00 0.00 0.00 0.00 20.56
04:12:59 AM 18 53.92 0.00 18.89 0.00 0.00 0.00 0.00 0.00 27.19
04:12:59 AM 19 50.47 0.00 22.43 0.00 0.00 0.47 0.00 0.00 26.64
04:12:59 AM 20 55.56 0.00 16.00 0.00 0.00 0.00 0.00 0.00 28.44
04:12:59 AM 21 54.79 0.00 17.35 0.00 0.00 0.00 0.00 0.00 27.85
04:12:59 AM 22 54.67 0.00 15.56 0.00 0.00 0.00 0.00 0.00 29.78
04:12:59 AM 23 50.90 0.00 18.02 0.00 0.00 0.00 0.00 0.00 31.08
二、如何优化软中断CPU0过高问题
2.1、查看网卡是否支持队列
在CentOS的最小化安装中,默认是不会安装lspci工具的,需要自己手动安装。
1、yum whatprovides */lspci # /*查找lspci是通过哪个安装包来提供的
2、yum install pciutils
3、查看网卡是否支持队列
/sbin/lspci -vvv |grep -E "Count|Enable+|MSI-X"
找到Ethernet controller项,如果有MSI-X,Enable+ 并且Count>1,表示该网卡支持多队列
2.2、查看网卡支持多少个队列
[[email protected] ~]$ grep em1 /proc/interrupts |awk '{print $NF}'
em1-0
em1-1
em1-2
em1-3
em1-4
em1-5
em1-6
em1-7
方法二、查看/proc/interrupts文件内的关键字:input和output,这2个是对应的,一个input.0加一个output.0算一个队列。
三、配置SMP IRQ affinity (即绑定队列到不同CPU,Kernel>2.4)
3.1、方法1:开启系统irqbalance服务 启动irqbalance后,手动绑定将失效。
yum -y install irqbalance
service irqbalance start
3.2、方法2: 手动绑定
/proc/irq/:该目录下存放的是以IRQ号命名的目录,如/proc/irq/40/,表示中断号为40的相关信息
/proc/irq/[irq_num]/smp_affinity:该文件存放的是CPU位掩码(十六进制)。修改该文件中的值可以改变CPU和某中断的亲和性
/proc/irq/[irq_num]/smp_affinity_list:该文件存放的是CPU列表(十进制)。注意,CPU核心个数用表示编号从0开始,如cpu0,cpu1等,
3.2.1、smp_affinity和smp_affinity_list修改其一即可:
配置smp_affinity:CPU核数是十六进制算出来
用法:echo $bitmask > /proc/irq/59/smp_affinity
例子:echo f >/proc/irq/59/smp_affinity #把59号中断绑定到前4个CPU[cpu0-3]上面
或者配置smp_affinity_list:CPU核数是十进制算出来
用法:echo $cpuindex1-$cpuindex2 >/proc/irq/59/smp_affinity_list
例子1:echo 0-3 > /proc/irq/59/smp_affinity_list #表示 把中断号59分配给:CPU 0,CPU 1,CPU 2,CPU 3
例子2:echo 0,1,2 > /proc/irq/59/smp_affinity_list #表示 把中断号59分配给:CPU 0,CPU 1,CPU 2
3.2.2、注释:$bitmask 是什么?如何计算得到的? 如下:
CPU位掩码计算:
一个十六进制f转换成二进制为1111,每一位表示一个CPU核,最靠右值是最低位即CPU0
Binary Hex
CPU 0 0001 1
CPU 1 0010 2
CPU 2 0100 4
CPU 3 1000 8
其中十六进制2就表示CPU1,十六进制8就表示CPU3
#######################分割线#######################
Binary Hex
CPU 0 0001 1
+ CPU 2 0100 4
-----------------------
both 0101 5
其中得出的十六进制和5表示CPU0 和CPU2
#######################分割线#######################
Binary Hex
CPU 0 0001 1
CPU 1 0010 2
CPU 2 0100 4
+ CPU 3 1000 8
-----------------------
both 1111 f
4个CPU参与中断,即可设置为f,8个CPU参与中断可设置为ff,以此类推
四、具体配置RSS
3.1、把中断号配置进smp_affinity文件里。 smp_affinity_list使用的是十六进制,
如何找出中断号? ## grep em1-[0-9] /proc/interrupts |awk '{print $1, $NF}' 或者 grep -E "eth|em|bond|virtio" /proc/interrupts
如何把中断号绑定到CPU的某个核上? ## echo 1 >/proc/irq/59/smp_affinity
过滤em1中断号,绑定到0-7号CPU核上(em1-0命名可能有所不同):
[root@ 111.111.111.111 ~]$ grep em1-[0-9] /proc/interrupts |awk '{print $1, $NF}'
59: em1-0
60: em1-1
61: em1-2
62: em1-3
63: em1-4
64: em1-5
65: em1-6
66: em1-7
echo 1 >/proc/irq/59/smp_affinity
echo 2 >/proc/irq/60/smp_affinity
echo 4 >/proc/irq/61/smp_affinity
echo 8 >/proc/irq/62/smp_affinity
echo 10 >/proc/irq/63/smp_affinity
echo 20 >/proc/irq/64/smp_affinity
echo 40 >/proc/irq/65/smp_affinity
echo 80 >/proc/irq/66/smp_affinity
查找中断号方法2:
grep -E "eth|em|bond|virtio" /proc/interrupts
[[email protected] ~]$ grep -E "eth|em|bond|virtio" /proc/interrupts
10: 0 0 0 0 IO-APIC-fasteoi virtio4
24: 0 0 0 0 PCI-MSI-edge virtio2-config
25: 20246517 106 1194 0 PCI-MSI-edge virtio2-requests
26: 0 0 0 0 PCI-MSI-edge virtio3-config
27: 36406351 0 0 0 PCI-MSI-edge virtio3-requests
28: 0 0 0 0 PCI-MSI-edge virtio0-config
29: 1688911394 0 16537237 787225 PCI-MSI-edge virtio0-input.0
30: 6819673 0 0 658 PCI-MSI-edge virtio0-output.0
31: 0 0 0 0 PCI-MSI-edge virtio1-config
32: 3 0 0 0 PCI-MSI-edge virtio1-virtqueues
# 如上所示:29即为中断号
可以通过top命令查看%si是否均衡分摊到0-7核CPU。
如图所示,8个中断都分配到了0-7核CPU上
top - 04:13:52 up 87 days, 8:32, 3 users, load average: 3.09, 3.76, 3.72
Tasks: 481 total, 9 running, 472 sleeping, 0 stopped, 0 zombie
Cpu0 : 42.4%us, 30.2%sy, 0.0%ni, 21.0%id, 0.0%wa, 0.0%hi, 6.5%si, 0.0%st
Cpu1 : 32.8%us, 22.3%sy, 0.0%ni, 1.3%id, 0.0%wa, 0.0%hi, 43.6%si, 0.0%st
Cpu2 : 28.7%us, 22.8%sy, 0.0%ni, 2.9%id, 0.0%wa, 0.0%hi, 45.6%si, 0.0%st
Cpu3 : 30.4%us, 23.5%sy, 0.0%ni, 1.0%id, 0.0%wa, 0.0%hi, 45.1%si, 0.0%st
Cpu4 : 30.1%us, 22.9%sy, 0.0%ni, 3.6%id, 0.0%wa, 0.0%hi, 43.5%si, 0.0%st
Cpu5 : 32.6%us, 21.7%sy, 0.0%ni, 3.0%id, 0.0%wa, 0.0%hi, 42.8%si, 0.0%st
Cpu6 : 30.9%us, 21.3%sy, 0.0%ni, 5.3%id, 0.0%wa, 0.0%hi, 42.5%si, 0.0%st
Cpu7 : 32.5%us, 24.3%sy, 0.0%ni, 3.9%id, 0.0%wa, 0.0%hi, 39.3%si, 0.0%st
Cpu8 : 51.6%us, 22.8%sy, 0.0%ni, 25.6%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu9 : 57.7%us, 22.1%sy, 0.0%ni, 20.2%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu10 : 46.9%us, 24.1%sy, 0.0%ni, 26.9%id, 2.2%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu11 : 50.8%us, 24.9%sy, 0.0%ni, 24.3%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu12 : 58.1%us, 18.9%sy, 0.0%ni, 23.1%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu13 : 57.9%us, 23.3%sy, 0.0%ni, 18.9%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu14 : 53.6%us, 21.8%sy, 0.0%ni, 24.5%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu15 : 57.7%us, 22.2%sy, 0.0%ni, 20.1%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu16 : 52.5%us, 22.1%sy, 0.0%ni, 25.4%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu17 : 58.5%us, 21.5%sy, 0.0%ni, 20.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu18 : 53.3%us, 19.5%sy, 0.0%ni, 26.9%id, 0.0%wa, 0.0%hi, 0.3%si, 0.0%st
Cpu19 : 54.4%us, 22.6%sy, 0.0%ni, 22.9%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu20 : 56.3%us, 16.0%sy, 0.0%ni, 27.7%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu21 : 56.6%us, 18.3%sy, 0.0%ni, 24.9%id, 0.0%wa, 0.0%hi, 0.3%si, 0.0%st
Cpu22 : 54.4%us, 14.8%sy, 0.0%ni, 30.5%id, 0.0%wa, 0.0%hi, 0.3%si, 0.0%st
Cpu23 : 55.2%us, 17.2%sy, 0.0%ni, 27.6%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 32862536k total, 32110364k used, 752172k free, 127140k buffers
Swap: 16498680k total, 11336k used, 16487344k free, 23714072k cached
这里分享一个脚本,直接算CPU对应的二进制。
#!/bin/bash
#
echo "统计cpu的16进制"
[ $# -ne 1 ] && echo ‘$1 is Cpu core number’ && exit 1
CCN=$1
echo “Print eth0 affinity”
for((i=0; i<${CCN}; i++))
do
echo ==============================
echo "Cpu Core $i is affinity"
((affinity=(1<
4.2、把中断号配置进smp_affinity_list文件里。 smp_affinity_list使用的是十进制,相比较smp_affinity的十六进制,可读性更好些。
过滤em1中断号,绑定到0-7号CPU核上(em1-0命名可能有所不同):
[root@ 111.111.111.111 ~]$ grep em1-[0-9] /proc/interrupts |awk '{print $1, $NF}'
59: em1-0
60: em1-1
61: em1-2
62: em1-3
63: em1-4
64: em1-5
65: em1-6
66: em1-7
echo 0 >/proc/irq/59/smp_affinity_list
echo 1 >/proc/irq/60/smp_affinity_list
echo 2 >/proc/irq/61/smp_affinity_list
echo 3 >/proc/irq/62/smp_affinity_list
echo 4 >/proc/irq/63/smp_affinity_list
echo 5 >/proc/irq/64/smp_affinity_list
echo 6 >/proc/irq/65/smp_affinity_list
echo 7 >/proc/irq/66/smp_affinity_list
可以通过top命令查看%si是否均衡分摊到0-7核CPU。
如图所示,8个中断都分配到了0-7核CPU上
top - 04:13:52 up 87 days, 8:32, 3 users, load average: 3.09, 3.76, 3.72
Tasks: 481 total, 9 running, 472 sleeping, 0 stopped, 0 zombie
Cpu0 : 42.4%us, 30.2%sy, 0.0%ni, 21.0%id, 0.0%wa, 0.0%hi, 6.5%si, 0.0%st
Cpu1 : 32.8%us, 22.3%sy, 0.0%ni, 1.3%id, 0.0%wa, 0.0%hi, 43.6%si, 0.0%st
Cpu2 : 28.7%us, 22.8%sy, 0.0%ni, 2.9%id, 0.0%wa, 0.0%hi, 45.6%si, 0.0%st
Cpu3 : 30.4%us, 23.5%sy, 0.0%ni, 1.0%id, 0.0%wa, 0.0%hi, 45.1%si, 0.0%st
Cpu4 : 30.1%us, 22.9%sy, 0.0%ni, 3.6%id, 0.0%wa, 0.0%hi, 43.5%si, 0.0%st
Cpu5 : 32.6%us, 21.7%sy, 0.0%ni, 3.0%id, 0.0%wa, 0.0%hi, 42.8%si, 0.0%st
Cpu6 : 30.9%us, 21.3%sy, 0.0%ni, 5.3%id, 0.0%wa, 0.0%hi, 42.5%si, 0.0%st
Cpu7 : 32.5%us, 24.3%sy, 0.0%ni, 3.9%id, 0.0%wa, 0.0%hi, 39.3%si, 0.0%st
Cpu8 : 51.6%us, 22.8%sy, 0.0%ni, 25.6%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu9 : 57.7%us, 22.1%sy, 0.0%ni, 20.2%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu10 : 46.9%us, 24.1%sy, 0.0%ni, 26.9%id, 2.2%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu11 : 50.8%us, 24.9%sy, 0.0%ni, 24.3%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu12 : 58.1%us, 18.9%sy, 0.0%ni, 23.1%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu13 : 57.9%us, 23.3%sy, 0.0%ni, 18.9%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu14 : 53.6%us, 21.8%sy, 0.0%ni, 24.5%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu15 : 57.7%us, 22.2%sy, 0.0%ni, 20.1%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu16 : 52.5%us, 22.1%sy, 0.0%ni, 25.4%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu17 : 58.5%us, 21.5%sy, 0.0%ni, 20.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu18 : 53.3%us, 19.5%sy, 0.0%ni, 26.9%id, 0.0%wa, 0.0%hi, 0.3%si, 0.0%st
Cpu19 : 54.4%us, 22.6%sy, 0.0%ni, 22.9%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu20 : 56.3%us, 16.0%sy, 0.0%ni, 27.7%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu21 : 56.6%us, 18.3%sy, 0.0%ni, 24.9%id, 0.0%wa, 0.0%hi, 0.3%si, 0.0%st
Cpu22 : 54.4%us, 14.8%sy, 0.0%ni, 30.5%id, 0.0%wa, 0.0%hi, 0.3%si, 0.0%st
Cpu23 : 55.2%us, 17.2%sy, 0.0%ni, 27.6%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 32862536k total, 32110364k used, 752172k free, 127140k buffers
Swap: 16498680k total, 11336k used, 16487344k free, 23714072k cached
网卡软中断调优的脚本:
#!/usr/bin/env python
# -*- coding: utf8 -*-
import re
from os import system
interrupts_file = '/proc/interrupts'
def file_hander(TARGET,VALUE='0'):
with open(TARGET,'w') as f_hander:
f_hander.write(VALUE)
def set_irq_affinity():
stop_irq_balance = 'service irqbalance stop'
system(stop_irq_balance)
interrupts_ct = open(interrupts_file)
cores_nr = len(interrupts_ct.readline().split())
irq_bit = 0
while True:
inter_line = interrupts_ct.readline()
if inter_line == "":
break
irq_list = inter_line.split()
if re.findall(r'eth|em|bond|virtio',irq_list[-1]):
irq_nr = irq_list[0][:-1]
TARGET = '/proc/irq/%s/smp_affinity_list' %(irq_nr)
VALUE = str(irq_bit)
file_hander(TARGET,VALUE)
irq_bit += 1
if irq_bit == cores_nr:
irq_bit = 0
if __name__ == '__main__':
set_irq_affinity()
注意事项
1、启动irqbalance后,手动绑定将失效。
2、当CPU工作在最高性能模式时,irqbalance会均匀分配中断到其他CPU,节能模式时中断会集中分配到CPU0
3、以上设置均以网卡支持多队列为前提,建议手动绑定SMP IRQ affinity
4、网卡多队列需tg3,bnx2,bnx2x,b44等驱动的支持,Broadcom的网卡驱动已经内置在内核中向后兼容大部分的2.6内核及大于2.4.24的2.4内核
参考文档:
http://www.simlinux.com/2017/02/28/net-softirq.html
https://www.cnblogs.com/Bozh/archive/2013/03/21/2973769.html
http://blog.51cto.com/rfyiamcool/1335700