1. 背景
硬件中断频繁发生(如:网络中断),将会很消耗CPU资源。尤其在当下多核CPU的情况下,如果将中断平均分派到各个核中进行处理,这无疑会增加CPU资源的消耗。如果将其固定在某一个核上进行专有处理,则可以提升总体CPU性能,也能够很好地平衡性能。
2. 以网络中断为例
2.1. 查看网口信息
eth0 Link encap:Ethernet HWaddr 26:0D:14:8B:BC:XX
inet addr:172.16.1.129 Bcast:172.16.1.255 Mask:255.255.254.0
inet6 addr: 2408:8640:8ff:f:1720::322/128 Scope:Global
inet6 addr: fe80::4214:248c:a483:d4b4/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:18764248 errors:0 dropped:3823 overruns:0 frame:0
TX packets:62993859 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:3788183548 (3.5 GiB) TX bytes:52869413020 (49.2 GiB)
Interrupt:119
2.2. 查看网络eth0对应的中断号(cat /proc/interrupts | grep eth0)
CPU0 CPU1 CPU2 CPU3 CPU4 CPU5 CPU6 CPU7
119: 2 0 0 0 0 0 0 0 GICv3 259 Level eth0
- 如上可知eth0对应的中断号为119
2.3. 查看CPU核数(cat /proc/cpuinfo | grep processor)
processor : 0
processor : 1
processor : 2
processor : 3
processor : 4
processor : 5
processor : 6
processor : 7
- 如上所知该CPU一共有8核
2.4. 绑定eth0到第5核
- Linux绑定CPU是通过掩码表示的,每个CPU对应1位,0表示关闭,1表示开启。
核 二进制 十六进制
CPU0 0000 0001 0x01
CPU1 0000 0010 0x02
CPU2 0000 0100 0x04
CPU3 0000 1000 0x08
CPU4 0001 0000 0x10
CPU5 0010 0000 0x20
CPU6 0100 0000 0x40
CPU7 1000 0000 0x80
- 将eth0绑定到第5核上,则对应的值为20
- echo 20 > /proc/irq/119/smp_affinity
2.5. 结果验证
CPU0 CPU1 CPU2 CPU3 CPU4 CPU5 CPU6 CPU7
119: 2 0 0 0 0 1615610 0 0 GICv3 259 Level eth0
- 从上可知CPU5对应的数值一直在改变,则即绑定到CPU5是成功的