随着cpu内核的增多的及中断处理机制的提升,linux社区在kernel 2.4 以后的版本支持把不同的硬件中断请求(IRQ)分配到特定的 “CPU”上,这个绑定技术被称为 SMP IRQ Affinity

1、查看当前系统上的中断是如何分配的
cat /proc/interrupts
2、查看当前eth0对应的IRQ中断的smp_affinity值
cat /proc/irq/进程ID/smp_affinity
3、改变eth0的中断设置
echo "1" > /proc/irq/进程ID/smp_affinity

自动绑定脚本

例如DELL服务器,6核双cpu(cpu0 cpu1),双网卡(em1 em2)

1、添加到开机启动 cat /etc/rc.local

/usr/local/bin/set_irp_cpulist 0-11 em1
/usr/local/bin/set_irp_cpulist 12-23 em2

2、脚本 /usr/local/bin/set_irp_cpulist

#! /bin/bash
if [ -z $2 ]; then
    echo "usage: $0 <cpu list> <interface> "
    echo "       <cpu list> can be either a comma separated list of single core numbers (0,1,2,3) or core groups (0-3)"
    exit 1
fi
cpulist=$1
interface=$2
#interface2=$3

IRQS=$(cat /proc/interrupts | grep $interface | awk '{print $1}' | sed 's/://')

if [ $interface2 ]; then
    IRQS_2=$(cat /proc/interrupts | grep $interface2 | awk '{print $1}' | sed 's/://')
        echo "---------------------------------------"
        echo "Optimizing IRQs for Dual port traffic"
        echo "---------------------------------------"
else
        echo "-------------------------------------"
        echo "Optimizing IRQs for Single port traffic"
        echo "-------------------------------------"
fi

#cpulist=$(cat /sys/devices/system/node/node$node/cpulist )
#if [ "$(echo $?)" != "0" ]; then
#   echo "Node id '$node' does not exists."
#   exit
#i
CORES=$( echo $cpulist | sed 's/,/ /g' | wc -w )
for word in $(seq 1 $CORES)
do
    SEQ=$(echo $cpulist | cut -d "," -f $word | sed 's/-/ /')
    if [ "$(echo $SEQ | wc -w)" != "1" ]; then
        CPULIST="$CPULIST $( echo $(seq $SEQ) | sed 's/ /,/g' )"
    fi
done
if [ "$CPULIST" != "" ]; then
    cpulist=$(echo $CPULIST | sed 's/ /,/g')
fi
CORES=$( echo $cpulist | sed 's/,/ /g' | wc -w )
echo Discovered irqs for $interface: $IRQS
I=1
for IRQ in $IRQS
do
    core_id=$(echo $cpulist | cut -d "," -f $I)
    echo Assign irq $IRQ mask 0x$(printf "%x" $((2**core_id)) )
    echo $(printf "%x" $((2**core_id)) ) > /proc/irq/$IRQ/smp_affinity
    if [ -z $interface2 ]; then
        I=$(( (I%CORES) + 1 ))
    else
        I=$(( (I%(CORES/2)) + 1 ))
    fi
done
if [ $interface2 ]; then
    echo
    echo Discovered irqs for $interface2: $IRQS_2
fi
I=$(( (CORES/2) + 1 ))
for IRQ in $IRQS_2
do
    core_id=$(echo $cpulist | cut -d "," -f $I)
    echo Assign irq $IRQ mask 0x$(printf "%x" $((2**core_id)) )
    echo $(printf "%x" $((2**core_id)) ) > /proc/irq/$IRQ/smp_affinity
    I=$(( (I%(CORES/2)) + 1 + (CORES/2) ))
done
echo
echo done.