zoukankan      html  css  js  c++  java
  • 网卡软中断问题

    原文连接:网卡软中断过高问题优化总结_约会远行的专栏-CSDN博客

    问题背景

    Contents [show]
    游戏网关高峰期时出网络丢包,CPU0软中断%sys高达90%

    预备知识

    什么是中断?

    由于接收来自外围硬件(相对于CPU和内存)的异步信号或者来自软件的同步信号,而进行相应的硬件、软件处理;发出这样的信号称为进行中断请求(interrupt request, IRQ)

    硬中断与软中断?

    硬中断:

    外围硬件发给CPU或者内存的异步信号就称之为硬中断

    软中断:

    由软件系统本身发给操作系统内核的中断信号,称之为软中断。通常是由硬中断处理程序或进程调度程序对操作系统内核的中断,也就是我们常说的系统调用(System Call)

    硬中断与软中断之区别与联系?

    1、硬中断是有外设硬件发出的,需要有中断控制器之参与。其过程是外设侦测到变化,告知中断控制器,中断控制器通过CPU或内存的中断脚通知CPU,然后硬件进行程序计数器及堆栈寄存器之现场保存工作(引发上下文切换),并根据中断向量调用硬中断处理程序进行中断处理
    2、软中断则通常是由硬中断处理程序或者进程调度程序等软件程序发出的中断信号,无需中断控制器之参与,直接以一个CPU指令之形式指示CPU进行程序计数器及堆栈寄存器之现场保存工作(亦会引发上下文切换),并调用相应的软中断处理程序进行中断处理(即我们通常所言之系统调用)
    3、硬中断直接以硬件的方式引发,处理速度快。软中断以软件指令之方式适合于对响应速度要求不是特别严格的场景
    4、硬中断通过设置CPU的屏蔽位可进行屏蔽,软中断则由于是指令之方式给出,不能屏蔽
    5、硬中断发生后,通常会在硬中断处理程序中调用一个软中断来进行后续工作的处理
    6、硬中断和软中断均会引起上下文切换(进程/线程之切换),进程切换的过程是差不多的

    查看中断情况

    查看中断分布情况即CPU都在哪些设备上干活,干了多少(也可以使用itop工具实时查看)?

    root@geekwolf:~# cat /proc/interrupts
    
               CPU0       CPU1       CPU2       CPU3       CPU4       CPU5       CPU6       CPU7       CPU8       CPU9       CPU10      CPU11      CPU12      CPU13      CPU14      CPU15      CPU16      CPU17      CPU18      CPU19      CPU20      CPU21      CPU22      CPU23      CPU24      CPU25      CPU26      CPU27      CPU28      CPU29      CPU30      CPU31      
      0:        620          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-IO-APIC-edge      timer
      8:          1          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-IO-APIC-edge      rtc0
      9:      20774          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-IO-APIC-fasteoi   acpi
     16:         28          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-IO-APIC-fasteoi   ehci_hcd:usb1
     23:        243          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-IO-APIC-fasteoi   ehci_hcd:usb2
     88:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  DMAR_MSI-edge      dmar0
     89:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  DMAR_MSI-edge      dmar1
     90:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      PCIe PME
     91:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      PCIe PME
     92:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      PCIe PME
     93:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      PCIe PME
     94:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      PCIe PME
     95:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      PCIe PME
     96:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      PCIe PME
     97:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      PCIe PME
     98:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      PCIe PME
     99:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      PCIe PME
    100:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      PCIe PME
    101:     169988          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      mpt2sas0-msix0
    134:    1900138          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      eth2-q0
    150:    4262209          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      eth3-q0
    166:          4          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      ioat-msix
    167:          4          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      ioat-msix
    168:          2          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      ioat-msix
    169:          2          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      ioat-msix
    170:          2          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      ioat-msix
    171:          2          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      ioat-msix
    172:          2          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      ioat-msix
    173:          2          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      ioat-msix
    174:          4          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      ioat-msix
    175:          4          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      ioat-msix
    176:          2          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      ioat-msix
    177:          2          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      ioat-msix
    178:          2          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      ioat-msix
    179:          2          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      ioat-msix
    180:          2          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      ioat-msix
    181:          2          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      ioat-msix
    NMI:        710        280        658        235        114         91         76         74        208        123        176        128        106         93        102         95         30        360        790         46         28         17         10          8         10        129       1166         22         18         16         11          7   Non-maskable interrupts
    LOC:    4230314    2640664    2427443    1337890    1091372     892129     819153     816781    2695809    1563153    1368637    1608410    1241692    1166692    1205270    1124865     120831    1966946     328048     816162     163492     222276     129805     121126     111906     599782    1247371     194215     162828     145678     118762     114295   Local timer interrupts
    SPU:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0   Spurious interrupts
    PMI:        710        280        658        235        114         91         76         74        208        123        176        128        106         93        102         95         30        360        790         46         28         17         10          8         10        129       1166         22         18         16         11          7   Performance monitoring interrupts
    IWI:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0   IRQ work interrupts
    RES:     679921    1369165    1013002     573776     543083     540027     593345     588120     842115     846190     874862     890102     873810     860080     867322     848916       3879      63916      10863      12850       7463       6350      10889      16041       2065      13207       6870       6817       4030       4700       5190       7430   Rescheduling interrupts
    CAL:      46507      67439      67569      67567      67565      67566      67566      67568     154689      67553      67511      67538      67568      67557      67534      67519      67520      26471      67470      67470      67476      67525      67518      67525      67545      64065      67210      67506      67485      67492      67526      67521   Function call interrupts
    TLB:       6547       3416       1798       1015        361        637        271        447        822        113       1079        222        259        198        265        844        157       1470       3468        767        499        262        338        230         41       1457       4023        290        105         93         46        177   TLB shootdowns
    TRM:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0   Thermal event interrupts
    THR:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0   Threshold APIC interrupts
    MCE:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0   Machine check exceptions
    MCP:        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569   Machine check polls
    ERR:          0
    MIS:          0
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    从上面的数据可以看出网卡eth2、eth3软中断都落在CPU0可以通过cat /proc/softirqs查看具体的软中断情况,总的中断次数可以通过vmstat或者dstat查看,其中vmstat中的in表示每秒的中断次数;
    通过mpstat -P ALL 2,每隔两秒查看下所有核状态信息,其中%irq为硬中断,%soft为软中断

    root@geekwolf:~# mpstat -P ALL 2
    08:42:04 AM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest   %idle
    08:42:05 AM  all    4.31    0.00    0.70    0.00    0.00    0.06    0.00    0.00   94.93
    08:42:05 AM    0    5.26    0.00    1.05    0.00    0.00    60.05    0.00    0.00   92.63
    08:42:05 AM    1    7.07    0.00    1.01    0.00    0.00    0.00    0.00    0.00   91.92
    08:42:05 AM    2    8.91    0.00    0.99    0.00    0.00    0.00    0.00    0.00   90.10
    08:42:05 AM    3    8.00    0.00    1.00    0.00    0.00    0.00    0.00    0.00   91.00
    08:42:05 AM    4    8.00    0.00    1.00    0.00    0.00    0.00    0.00    0.00   91.00
    08:42:05 AM    5    7.00    0.00    2.00    0.00    0.00    0.00    0.00    0.00   91.00
    08:42:05 AM    6    7.00    0.00    1.00    0.00    0.00    0.00    0.00    0.00   92.00
    08:42:05 AM    7    4.12    0.00    1.03    0.00    0.00    0.00    0.00    0.00   94.85
    08:42:05 AM    8    4.17    0.00    1.04    0.00    0.00    0.00    0.00    0.00   94.79
    08:42:05 AM    9    8.91    0.00    0.99    0.00    0.00    0.00    0.00    0.00   90.10
    08:42:05 AM   10    4.17    0.00    2.08    0.00    0.00    0.00    0.00    0.00   93.75
    08:42:05 AM   11    6.12    0.00    1.02    0.00    0.00    0.00    0.00    0.00   92.86
    08:42:05 AM   12    6.00    0.00    2.00    0.00    0.00    0.00    0.00    0.00   92.00
    08:42:05 AM   13    3.16    0.00    1.05    0.00    0.00    0.00    0.00    0.00   95.79
    08:42:05 AM   14    8.16    0.00    1.02    0.00    0.00    0.00    0.00    0.00   90.82
    08:42:05 AM   15    6.06    0.00    1.01    0.00    0.00    1.01    0.00    0.00   91.92
    08:42:05 AM   16    3.00    0.00    1.00    0.00    0.00    0.00    0.00    0.00   96.00
    08:42:05 AM   17    2.02    0.00    1.01    0.00    0.00    0.00    0.00    0.00   96.97
    08:42:05 AM   18    2.04    0.00    1.02    0.00    0.00    0.00    0.00    0.00   96.94
    08:42:05 AM   19    2.97    0.00    0.99    0.00    0.00    0.00    0.00    0.00   96.04
    08:42:05 AM   20    2.04    0.00    0.00    0.00    0.00    0.00    0.00    0.00   97.96
    08:42:05 AM   21    2.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00   98.00
    08:42:05 AM   22    3.03    0.00    0.00    0.00    0.00    0.00    0.00    0.00   96.97
    08:42:05 AM   23    2.04    0.00    0.00    0.00    0.00    0.00    0.00    0.00   97.96
    08:42:05 AM   24    4.95    0.00    0.00    0.00    0.00    0.00    0.00    0.00   95.05
    08:42:05 AM   25    2.02    0.00    0.00    0.00    0.00    0.00    0.00    0.00   97.98
    08:42:05 AM   26    3.03    0.00    0.00    0.00    0.00    0.00    0.00    0.00   96.97
    08:42:05 AM   27    2.04    0.00    0.00    0.00    0.00    0.00    0.00    0.00   97.96
    08:42:05 AM   28    1.01    0.00    1.01    0.00    0.00    0.00    0.00    0.00   97.98
    08:42:05 AM   29    1.02    0.00    0.00    0.00    0.00    0.00    0.00    0.00   98.98
    08:42:05 AM   30    1.01    0.00    0.00    0.00    0.00    0.00    0.00    0.00   98.99
    08:42:05 AM   31    1.02    0.00    0.00    0.00    0.00    0.00    0.00    0.00   98.98
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    如何优化软中断CPU0过高问题

    RSS(Receive Side Scaling,需网卡支持多队列)

    查看网卡是否支持队列

    root@geekwolf:~# lscpi -vvv
    06:00.0 Ethernet controller: Broadcom Corporation BCM57840 NetXtreme II 10/20-Gigabit Ethernet (rev 11)
     Subsystem: Hewlett-Packard Company Device 22fa
     Control: I/O- Mem+ BusMaster+ SpecCycle- MemWINV- VGASnoop- ParErr+ Stepping- SERR+ FastB2B- DisINTx+
     Status: Cap+ 66MHz- UDF- FastB2B- ParErr- DEVSEL=fast >TAbort- <tabort - <MAbort- >SERR- <perr - INTx-
     Latency: 0, Cache Line Size: 64 bytes
     Interrupt: pin A routed to IRQ 32
     Region 0: Memory at 93800000 (64-bit, prefetchable) [size=8M]
     Region 2: Memory at 93000000 (64-bit, prefetchable) [size=8M]
     Region 4: Memory at 95000000 (64-bit, prefetchable) [size=64K]
     [virtual] Expansion ROM at 95080000 [disabled] [size=512K]
     Capabilities: [48] Power Management version 3
       Flags: PMEClk- DSI- D1- D2- AuxCurrent=0mA PME(D0+,D1-,D2-,D3hot+,D3cold+)
       Status: D0 NoSoftRst+ PME-Enable- DSel=0 DScale=1 PME-
     Capabilities: [50] Vital Product Data
       Product Name: HP FlexFabric 10Gb 2-port 536FLB Adapter
       Read-only fields:
         [PN] Part number: 766488-001
         [EC] Engineering changes: A-5444
         [MN] Manufacture ID: 31 30 33 43
         [V0] Vendor specific: 12W PCIeGen3
         [V1] Vendor specific: 7.10.55
         [V3] Vendor specific: 7.10.72
         [V5] Vendor specific: 0A
         [V6] Vendor specific: 7.10.72
         [V7] Vendor specific: 536FLB
         [SN] Serial number: 7C444703LG
         [V2] Vendor specific: 5447
         [V4] Vendor specific: 8CDCD419D870
         [RV] Reserved: checksum good, 186 byte(s) reserved
       End
     Capabilities: [a0] MSI-X: Enable+ Count=32 Masked-
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    找到Ethernet controller项,如果有MSI-X,Enable+ 并且Count>1,表示该网卡支持多队列

    查看网卡支持多少个队列

    root@geekwolf:~# grep eth0 /proc/interrupts |awk '{print $NF}'
    eth0
    eth0-fp-0
    eth0-fp-1
    eth0-fp-2
    eth0-fp-3
    eth0-fp-4
    eth0-fp-5
    eth0-fp-6
    eth0-fp-7
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    配置SMP IRQ affinity

    (即绑定队列到不同CPU,Kernel>2.4)

    方法1:开启系统irqbalance服务

    apt-get -y install irqbalance
    service irqbalance start
    • 1
    • 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等,
    
    smp_affinity和smp_affinity_list修改其一即可,下面修改smp_affinity:
    
    echo $bitmask > /proc/irq/IRQ#/smp_affinity
    示例(把140号中断绑定到前4个CPU[cpu0-3]上面):
    echo  f >/proc/irq/140/smp_affinity
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    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,以此类推
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    配置RSS

    过滤eth0中断号,绑定到0-7号CPU核上(eth0-fp命名可能有所不同):

    root@geekwolf:~# grep eth0-fp /proc/interrupts |awk '{print $1, $NF}'
    147: eth0-fp-0
    148: eth0-fp-1
    149: eth0-fp-2
    150: eth0-fp-3
    151: eth0-fp-4
    152: eth0-fp-5
    153: eth0-fp-6
    154: eth0-fp-7
    
    echo 1  >/proc/irq/147/smp_affinity
    echo 2  >/proc/irq/148/smp_affinity
    echo 4  >/proc/irq/149/smp_affinity
    echo 8  >/proc/irq/150/smp_affinity
    echo 10 >/proc/irq/151/smp_affinity
    echo 20 >/proc/irq/152/smp_affinity
    echo 40 >/proc/irq/153/smp_affinity
    echo 80 >/proc/irq/154/smp_affinity
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    可以通过top命令查看%si是否均衡分摊到0-7核CPU

    注意事项

    1、启动irqbalance后,手动绑定将失效
    2、当CPU工作在最高性能模式时,irqbalance会均匀分配中断到其他CPU,节能模式时中断会集中分配到CPU0
    3、以上设置均以网卡支持多队列为前提,建议手动绑定SMP IRQ affinity
    网卡多队列需tg3,bnx2,bnx2x,b44等驱动的支持,Broadcom的网卡驱动已经内置在内核中向后兼容大部分的2.6内核及大于2.4.24的2.4内核
    4、笔者实际测试过程中遇到BladeCenter HS23刀片服务器Emulex Corporation OneConnect 10Gb NIC (be3)本身支持多队列,在连接到千兆网5、环境下无法使用多队列问题,万兆网络下可以使用,只好通过下面RPS/RFS方式实现

    RPS/RFS

    Receive Packet Steering/Receive Flow Streering,软件方式实现CPU均衡,接收包中断的优化
    RPS: 网卡驱动对每一个数据库包根据四元组(SIP,SPORT,DIP,DPORT)生成HASH值,通过HASH值将每个连接和CPU 绑定
    RFS: 由于RPS只是单纯的把数据包均衡到不同的CPU上,此时如果应用程序所在CPU和中断处理的CPU不在同一个核,将会对CPU Cache影响很大,RFS的作用就是将应用程序和软中断处理分配到同一个CPU
    配置步骤:

    根据上述说明一个十六进制f表示四个CPU核,那么均衡到32核即ffffffff

    配置RPS

    rps_cpus='ffffffffff'
    for rxdir in /sys/class/net/eth0/queues/rx-*
    do
        echo $rps_cpus >$rxdir/rps_cpus
    done
    • 1
    • 2
    • 3
    • 4
    • 5

    配置RFS

    RFS扩展了RPS的性能以增加CPU缓存命中率,减少网络延迟,默认是禁用的

    /proc/sys/net/core/rps_sock_flow_entries
    设置此文件至同时活跃连接数的最大预期值。对于中等服务器负载,推荐值为 32768 。所有输入的值四舍五入至最接近的2的幂
    /sys/class/net/device/queues/rx-queue/rps_flow_cnt
    将 device 改为想要配置的网络设备名称(例如,eth0),将 rx-queue 改为想要配置的接收队列名称(例如,rx-0)。
    将此文件的值设为 rps_sock_flow_entries 除以 N,其中 N 是设备中接收队列的数量。例如,如果 rps_flow_entries 设为 32768,并且有 16 个配置接收队列,那么 rps_flow_cnt 就应设为 2048。对于单一队列的设备,rps_flow_cnt 的值和 rps_sock_flow_entries 的值是一样的

    ls /sys/class/net/eth0/queues/rx-*|grep queues|wc -l
    8
    
    rps_flow_cnt=32768/8=4096
    echo 32768 >/proc/sys/net/core/rps_sock_flow_entries
    for rxdir in /sys/class/net/eth0/queues/rx-*
    do
        echo $rps_cpus >$rxdir/rps_cpus
        echo $rps_flow_cnt >$rxdir/rps_flow_cnt
    done
    
    echo 32768 >/proc/sys/net/core/rps_sock_flow_entries
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    优化脚本可参考: https://github.com/geekwolf/sa-scripts/blob/master/ops-scripts/performance_tuning/set_rps.sh

    网卡常规优化方案
    常规网卡优化方案
    关于发包的优化XPS 还未做测试,有时间在做补充

    原文连接:http://www.simlinux.com/archives/1798.html

    问题背景

    Contents [show]
    游戏网关高峰期时出网络丢包,CPU0软中断%sys高达90%

    预备知识

    什么是中断?

    由于接收来自外围硬件(相对于CPU和内存)的异步信号或者来自软件的同步信号,而进行相应的硬件、软件处理;发出这样的信号称为进行中断请求(interrupt request, IRQ)

    硬中断与软中断?

    硬中断:

    外围硬件发给CPU或者内存的异步信号就称之为硬中断

    软中断:

    由软件系统本身发给操作系统内核的中断信号,称之为软中断。通常是由硬中断处理程序或进程调度程序对操作系统内核的中断,也就是我们常说的系统调用(System Call)

    硬中断与软中断之区别与联系?

    1、硬中断是有外设硬件发出的,需要有中断控制器之参与。其过程是外设侦测到变化,告知中断控制器,中断控制器通过CPU或内存的中断脚通知CPU,然后硬件进行程序计数器及堆栈寄存器之现场保存工作(引发上下文切换),并根据中断向量调用硬中断处理程序进行中断处理
    2、软中断则通常是由硬中断处理程序或者进程调度程序等软件程序发出的中断信号,无需中断控制器之参与,直接以一个CPU指令之形式指示CPU进行程序计数器及堆栈寄存器之现场保存工作(亦会引发上下文切换),并调用相应的软中断处理程序进行中断处理(即我们通常所言之系统调用)
    3、硬中断直接以硬件的方式引发,处理速度快。软中断以软件指令之方式适合于对响应速度要求不是特别严格的场景
    4、硬中断通过设置CPU的屏蔽位可进行屏蔽,软中断则由于是指令之方式给出,不能屏蔽
    5、硬中断发生后,通常会在硬中断处理程序中调用一个软中断来进行后续工作的处理
    6、硬中断和软中断均会引起上下文切换(进程/线程之切换),进程切换的过程是差不多的

    查看中断情况

    查看中断分布情况即CPU都在哪些设备上干活,干了多少(也可以使用itop工具实时查看)?

    root@geekwolf:~# cat /proc/interrupts
    
               CPU0       CPU1       CPU2       CPU3       CPU4       CPU5       CPU6       CPU7       CPU8       CPU9       CPU10      CPU11      CPU12      CPU13      CPU14      CPU15      CPU16      CPU17      CPU18      CPU19      CPU20      CPU21      CPU22      CPU23      CPU24      CPU25      CPU26      CPU27      CPU28      CPU29      CPU30      CPU31      
      0:        620          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-IO-APIC-edge      timer
      8:          1          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-IO-APIC-edge      rtc0
      9:      20774          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-IO-APIC-fasteoi   acpi
     16:         28          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-IO-APIC-fasteoi   ehci_hcd:usb1
     23:        243          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-IO-APIC-fasteoi   ehci_hcd:usb2
     88:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  DMAR_MSI-edge      dmar0
     89:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  DMAR_MSI-edge      dmar1
     90:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      PCIe PME
     91:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      PCIe PME
     92:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      PCIe PME
     93:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      PCIe PME
     94:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      PCIe PME
     95:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      PCIe PME
     96:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      PCIe PME
     97:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      PCIe PME
     98:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      PCIe PME
     99:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      PCIe PME
    100:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      PCIe PME
    101:     169988          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      mpt2sas0-msix0
    134:    1900138          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      eth2-q0
    150:    4262209          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      eth3-q0
    166:          4          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      ioat-msix
    167:          4          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      ioat-msix
    168:          2          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      ioat-msix
    169:          2          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      ioat-msix
    170:          2          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      ioat-msix
    171:          2          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      ioat-msix
    172:          2          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      ioat-msix
    173:          2          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      ioat-msix
    174:          4          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      ioat-msix
    175:          4          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      ioat-msix
    176:          2          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      ioat-msix
    177:          2          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      ioat-msix
    178:          2          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      ioat-msix
    179:          2          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      ioat-msix
    180:          2          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      ioat-msix
    181:          2          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0  IR-PCI-MSI-edge      ioat-msix
    NMI:        710        280        658        235        114         91         76         74        208        123        176        128        106         93        102         95         30        360        790         46         28         17         10          8         10        129       1166         22         18         16         11          7   Non-maskable interrupts
    LOC:    4230314    2640664    2427443    1337890    1091372     892129     819153     816781    2695809    1563153    1368637    1608410    1241692    1166692    1205270    1124865     120831    1966946     328048     816162     163492     222276     129805     121126     111906     599782    1247371     194215     162828     145678     118762     114295   Local timer interrupts
    SPU:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0   Spurious interrupts
    PMI:        710        280        658        235        114         91         76         74        208        123        176        128        106         93        102         95         30        360        790         46         28         17         10          8         10        129       1166         22         18         16         11          7   Performance monitoring interrupts
    IWI:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0   IRQ work interrupts
    RES:     679921    1369165    1013002     573776     543083     540027     593345     588120     842115     846190     874862     890102     873810     860080     867322     848916       3879      63916      10863      12850       7463       6350      10889      16041       2065      13207       6870       6817       4030       4700       5190       7430   Rescheduling interrupts
    CAL:      46507      67439      67569      67567      67565      67566      67566      67568     154689      67553      67511      67538      67568      67557      67534      67519      67520      26471      67470      67470      67476      67525      67518      67525      67545      64065      67210      67506      67485      67492      67526      67521   Function call interrupts
    TLB:       6547       3416       1798       1015        361        637        271        447        822        113       1079        222        259        198        265        844        157       1470       3468        767        499        262        338        230         41       1457       4023        290        105         93         46        177   TLB shootdowns
    TRM:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0   Thermal event interrupts
    THR:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0   Threshold APIC interrupts
    MCE:          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0          0   Machine check exceptions
    MCP:        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569        569   Machine check polls
    ERR:          0
    MIS:          0
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    从上面的数据可以看出网卡eth2、eth3软中断都落在CPU0可以通过cat /proc/softirqs查看具体的软中断情况,总的中断次数可以通过vmstat或者dstat查看,其中vmstat中的in表示每秒的中断次数;
    通过mpstat -P ALL 2,每隔两秒查看下所有核状态信息,其中%irq为硬中断,%soft为软中断

    root@geekwolf:~# mpstat -P ALL 2
    08:42:04 AM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest   %idle
    08:42:05 AM  all    4.31    0.00    0.70    0.00    0.00    0.06    0.00    0.00   94.93
    08:42:05 AM    0    5.26    0.00    1.05    0.00    0.00    60.05    0.00    0.00   92.63
    08:42:05 AM    1    7.07    0.00    1.01    0.00    0.00    0.00    0.00    0.00   91.92
    08:42:05 AM    2    8.91    0.00    0.99    0.00    0.00    0.00    0.00    0.00   90.10
    08:42:05 AM    3    8.00    0.00    1.00    0.00    0.00    0.00    0.00    0.00   91.00
    08:42:05 AM    4    8.00    0.00    1.00    0.00    0.00    0.00    0.00    0.00   91.00
    08:42:05 AM    5    7.00    0.00    2.00    0.00    0.00    0.00    0.00    0.00   91.00
    08:42:05 AM    6    7.00    0.00    1.00    0.00    0.00    0.00    0.00    0.00   92.00
    08:42:05 AM    7    4.12    0.00    1.03    0.00    0.00    0.00    0.00    0.00   94.85
    08:42:05 AM    8    4.17    0.00    1.04    0.00    0.00    0.00    0.00    0.00   94.79
    08:42:05 AM    9    8.91    0.00    0.99    0.00    0.00    0.00    0.00    0.00   90.10
    08:42:05 AM   10    4.17    0.00    2.08    0.00    0.00    0.00    0.00    0.00   93.75
    08:42:05 AM   11    6.12    0.00    1.02    0.00    0.00    0.00    0.00    0.00   92.86
    08:42:05 AM   12    6.00    0.00    2.00    0.00    0.00    0.00    0.00    0.00   92.00
    08:42:05 AM   13    3.16    0.00    1.05    0.00    0.00    0.00    0.00    0.00   95.79
    08:42:05 AM   14    8.16    0.00    1.02    0.00    0.00    0.00    0.00    0.00   90.82
    08:42:05 AM   15    6.06    0.00    1.01    0.00    0.00    1.01    0.00    0.00   91.92
    08:42:05 AM   16    3.00    0.00    1.00    0.00    0.00    0.00    0.00    0.00   96.00
    08:42:05 AM   17    2.02    0.00    1.01    0.00    0.00    0.00    0.00    0.00   96.97
    08:42:05 AM   18    2.04    0.00    1.02    0.00    0.00    0.00    0.00    0.00   96.94
    08:42:05 AM   19    2.97    0.00    0.99    0.00    0.00    0.00    0.00    0.00   96.04
    08:42:05 AM   20    2.04    0.00    0.00    0.00    0.00    0.00    0.00    0.00   97.96
    08:42:05 AM   21    2.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00   98.00
    08:42:05 AM   22    3.03    0.00    0.00    0.00    0.00    0.00    0.00    0.00   96.97
    08:42:05 AM   23    2.04    0.00    0.00    0.00    0.00    0.00    0.00    0.00   97.96
    08:42:05 AM   24    4.95    0.00    0.00    0.00    0.00    0.00    0.00    0.00   95.05
    08:42:05 AM   25    2.02    0.00    0.00    0.00    0.00    0.00    0.00    0.00   97.98
    08:42:05 AM   26    3.03    0.00    0.00    0.00    0.00    0.00    0.00    0.00   96.97
    08:42:05 AM   27    2.04    0.00    0.00    0.00    0.00    0.00    0.00    0.00   97.96
    08:42:05 AM   28    1.01    0.00    1.01    0.00    0.00    0.00    0.00    0.00   97.98
    08:42:05 AM   29    1.02    0.00    0.00    0.00    0.00    0.00    0.00    0.00   98.98
    08:42:05 AM   30    1.01    0.00    0.00    0.00    0.00    0.00    0.00    0.00   98.99
    08:42:05 AM   31    1.02    0.00    0.00    0.00    0.00    0.00    0.00    0.00   98.98
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    如何优化软中断CPU0过高问题

    RSS(Receive Side Scaling,需网卡支持多队列)

    查看网卡是否支持队列

    root@geekwolf:~# lscpi -vvv
    06:00.0 Ethernet controller: Broadcom Corporation BCM57840 NetXtreme II 10/20-Gigabit Ethernet (rev 11)
     Subsystem: Hewlett-Packard Company Device 22fa
     Control: I/O- Mem+ BusMaster+ SpecCycle- MemWINV- VGASnoop- ParErr+ Stepping- SERR+ FastB2B- DisINTx+
     Status: Cap+ 66MHz- UDF- FastB2B- ParErr- DEVSEL=fast >TAbort- <tabort - <MAbort- >SERR- <perr - INTx-
     Latency: 0, Cache Line Size: 64 bytes
     Interrupt: pin A routed to IRQ 32
     Region 0: Memory at 93800000 (64-bit, prefetchable) [size=8M]
     Region 2: Memory at 93000000 (64-bit, prefetchable) [size=8M]
     Region 4: Memory at 95000000 (64-bit, prefetchable) [size=64K]
     [virtual] Expansion ROM at 95080000 [disabled] [size=512K]
     Capabilities: [48] Power Management version 3
       Flags: PMEClk- DSI- D1- D2- AuxCurrent=0mA PME(D0+,D1-,D2-,D3hot+,D3cold+)
       Status: D0 NoSoftRst+ PME-Enable- DSel=0 DScale=1 PME-
     Capabilities: [50] Vital Product Data
       Product Name: HP FlexFabric 10Gb 2-port 536FLB Adapter
       Read-only fields:
         [PN] Part number: 766488-001
         [EC] Engineering changes: A-5444
         [MN] Manufacture ID: 31 30 33 43
         [V0] Vendor specific: 12W PCIeGen3
         [V1] Vendor specific: 7.10.55
         [V3] Vendor specific: 7.10.72
         [V5] Vendor specific: 0A
         [V6] Vendor specific: 7.10.72
         [V7] Vendor specific: 536FLB
         [SN] Serial number: 7C444703LG
         [V2] Vendor specific: 5447
         [V4] Vendor specific: 8CDCD419D870
         [RV] Reserved: checksum good, 186 byte(s) reserved
       End
     Capabilities: [a0] MSI-X: Enable+ Count=32 Masked-
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    找到Ethernet controller项,如果有MSI-X,Enable+ 并且Count>1,表示该网卡支持多队列

    查看网卡支持多少个队列

    root@geekwolf:~# grep eth0 /proc/interrupts |awk '{print $NF}'
    eth0
    eth0-fp-0
    eth0-fp-1
    eth0-fp-2
    eth0-fp-3
    eth0-fp-4
    eth0-fp-5
    eth0-fp-6
    eth0-fp-7
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    配置SMP IRQ affinity

    (即绑定队列到不同CPU,Kernel>2.4)

    方法1:开启系统irqbalance服务

    apt-get -y install irqbalance
    service irqbalance start
    • 1
    • 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等,
    
    smp_affinity和smp_affinity_list修改其一即可,下面修改smp_affinity:
    
    echo $bitmask > /proc/irq/IRQ#/smp_affinity
    示例(把140号中断绑定到前4个CPU[cpu0-3]上面):
    echo  f >/proc/irq/140/smp_affinity
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    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,以此类推
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    配置RSS

    过滤eth0中断号,绑定到0-7号CPU核上(eth0-fp命名可能有所不同):

    root@geekwolf:~# grep eth0-fp /proc/interrupts |awk '{print $1, $NF}'
    147: eth0-fp-0
    148: eth0-fp-1
    149: eth0-fp-2
    150: eth0-fp-3
    151: eth0-fp-4
    152: eth0-fp-5
    153: eth0-fp-6
    154: eth0-fp-7
    
    echo 1  >/proc/irq/147/smp_affinity
    echo 2  >/proc/irq/148/smp_affinity
    echo 4  >/proc/irq/149/smp_affinity
    echo 8  >/proc/irq/150/smp_affinity
    echo 10 >/proc/irq/151/smp_affinity
    echo 20 >/proc/irq/152/smp_affinity
    echo 40 >/proc/irq/153/smp_affinity
    echo 80 >/proc/irq/154/smp_affinity
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    可以通过top命令查看%si是否均衡分摊到0-7核CPU

    注意事项

    1、启动irqbalance后,手动绑定将失效
    2、当CPU工作在最高性能模式时,irqbalance会均匀分配中断到其他CPU,节能模式时中断会集中分配到CPU0
    3、以上设置均以网卡支持多队列为前提,建议手动绑定SMP IRQ affinity
    网卡多队列需tg3,bnx2,bnx2x,b44等驱动的支持,Broadcom的网卡驱动已经内置在内核中向后兼容大部分的2.6内核及大于2.4.24的2.4内核
    4、笔者实际测试过程中遇到BladeCenter HS23刀片服务器Emulex Corporation OneConnect 10Gb NIC (be3)本身支持多队列,在连接到千兆网5、环境下无法使用多队列问题,万兆网络下可以使用,只好通过下面RPS/RFS方式实现

    RPS/RFS

    Receive Packet Steering/Receive Flow Streering,软件方式实现CPU均衡,接收包中断的优化
    RPS: 网卡驱动对每一个数据库包根据四元组(SIP,SPORT,DIP,DPORT)生成HASH值,通过HASH值将每个连接和CPU 绑定
    RFS: 由于RPS只是单纯的把数据包均衡到不同的CPU上,此时如果应用程序所在CPU和中断处理的CPU不在同一个核,将会对CPU Cache影响很大,RFS的作用就是将应用程序和软中断处理分配到同一个CPU
    配置步骤:

    根据上述说明一个十六进制f表示四个CPU核,那么均衡到32核即ffffffff

    配置RPS

    rps_cpus='ffffffffff'
    for rxdir in /sys/class/net/eth0/queues/rx-*
    do
        echo $rps_cpus >$rxdir/rps_cpus
    done
    • 1
    • 2
    • 3
    • 4
    • 5

    配置RFS

    RFS扩展了RPS的性能以增加CPU缓存命中率,减少网络延迟,默认是禁用的

    /proc/sys/net/core/rps_sock_flow_entries
    设置此文件至同时活跃连接数的最大预期值。对于中等服务器负载,推荐值为 32768 。所有输入的值四舍五入至最接近的2的幂
    /sys/class/net/device/queues/rx-queue/rps_flow_cnt
    将 device 改为想要配置的网络设备名称(例如,eth0),将 rx-queue 改为想要配置的接收队列名称(例如,rx-0)。
    将此文件的值设为 rps_sock_flow_entries 除以 N,其中 N 是设备中接收队列的数量。例如,如果 rps_flow_entries 设为 32768,并且有 16 个配置接收队列,那么 rps_flow_cnt 就应设为 2048。对于单一队列的设备,rps_flow_cnt 的值和 rps_sock_flow_entries 的值是一样的

    ls /sys/class/net/eth0/queues/rx-*|grep queues|wc -l
    8
    
    rps_flow_cnt=32768/8=4096
    echo 32768 >/proc/sys/net/core/rps_sock_flow_entries
    for rxdir in /sys/class/net/eth0/queues/rx-*
    do
        echo $rps_cpus >$rxdir/rps_cpus
        echo $rps_flow_cnt >$rxdir/rps_flow_cnt
    done
    
    echo 32768 >/proc/sys/net/core/rps_sock_flow_entries
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    优化脚本可参考: https://github.com/geekwolf/sa-scripts/blob/master/ops-scripts/performance_tuning/set_rps.sh

    网卡常规优化方案
    常规网卡优化方案
    关于发包的优化XPS 还未做测试,有时间在做补充

    网卡软中断过高问题优化总结_约会远行的专栏-CSDN博客

  • 相关阅读:
    SourceForge上发布dedup util 收藏
    IBM快速整合Diligent 触动了谁的神经?
    Data Domain揭开其神秘面纱 加速中国业务发展
    Java NIO原理和使用
    用PDB库调试Python程序
    windows下配置eclipse + Python + PyDev + tornado,并使用pylint进行静态检测
    函数fork与vfork的区别与联系详解
    关于C++中友元类和友元函数的示例讲解
    const成员函数
    pthread_cond_wait()深入解析
  • 原文地址:https://www.cnblogs.com/ip99/p/15252213.html
Copyright © 2011-2022 走看看