http://hi.baidu.com/autoelectron/blog/item/145d6a2de1264c3f349bf778
在多 CPU 的环境中,还有一个中断平衡的问题,比如,网卡中断会教给哪个 CPU 处理,这个参数控制哪些 CPU 可以绑定IRQ 中断。其中的 {number} 是对应设备的中断编号,可以用下面的命令找出:
cat /proc/interrupt
比如,一般 eth0 的 IRQ 编号是 16,所以控制 eth0 中断绑定的 /proc 文件名是/proc/irq/16/smp_affinity。上面这个命令还可以看到某些中断对应的CPU处理的次数,缺省的时候肯定是不平衡的。
设置其值的方法很简单,smp_affinity 自身是一个位掩码(bitmask),特定的位对应特定的 CPU,这样,01就意味着只有第一个 CPU 可以处理对应的中断,而 0f(0x1111)意味着四个 CPU 都会参与中断处理。
几乎所有外设都有这个参数设置,可以关注一下。
这个数值的推荐设置,其实在很大程度上,让专门的CPU处理专门的中断是效率最高的,比如,给磁盘IO一个CPU,给网卡一个CPU,这样是比较合理的。
一些测试结论
Background:
Whenever a piece of hardware, such as disk controller or ethernetcard,
needs attention from the CPU, it throws aninterrupt.
the CPU that something has happened and that the CPU should dropwhat
it's doing to handle the event.
sending the same interrupts, the IRQ system was established whereeach device
in a computer system is assigned its own special IRQ so that itsinterrupts
are unique.
Starting with the 2.4 kernel, Linux has gained the ability toassign certain
IRQs to specific processors (or groups ofprocessors).
as SMP IRQ affinity, and it allows you control how your system willrespond
to various hardware events.
the work load that you server must do so that it can moreefficiently do
it's job.
Obviously, in order for this to work, you will need a system thathas more
than one processor (SMP).
kernel.
Some brief and very bare information on SMP IRQ affinity isprovided in
the kernel source tree of the 2.4 kernel in the file:
How to use it:
SMP affinity is controlled by manipulating files in the /proc/irq/directory.
In /proc/irq/ are directories that correspond to the IRQs presenton your
system (not all IRQs may be available). In each of thesedirectories is
the "smp_affinity" file, and this is where we will work ourmagic.
The first order of business is to figure out what IRQ a device isusing.
This information is available in the /proc/interruptsfile.
[root@archimedes /proc]# cat /proc/interrupts
NMI:
LOC:
ERR:
MIS:
As you can see, this is a 4 processor machine.
lists the IRQs used on the system.
are parts of other drivers used on the system and aren't reallyaccessible
to us, so we'll just ignore them.
The second through fifth columns (labelled CPU0-CPU3) show thenumber of times
the corresponding process has handled an interrupt from thatparticular IRQ.
For example, all of the CPUs have handled roughly the same numberof interrupts
for IRQ 24 (around 86,000 with CPU0 handling a little over87,000).
The sixth column lists whether or not the device driver associatedwith the
interrupt supports IO-APIC (see/usr/src/linux/Documentation/i386/IO-APIC.txt
for more information).
SMP affinity will only work for IO-APIC enabled devicedrivers.
example, we will not be able to change the affinity for the"cascade"
driver (IRQ 2) because it doesn't support IO-APIC.
Finally, the seventh and last column lists the driver or devicethat is
associated with the interrupt.
(eth0) is using IRQ 31, and our SCSI controller (aic7xxx) is usingIRQ 24.
The first and last columns are really the only ones we'reinterested in here.
For the rest of this example, I'm going to assume that we want toadjust
the SMP affinity for th SCSI controller (IRQ 24).
Now that we've got the IRQ, we can change the processoraffinity.
do this, we'll go into the /proc/irq/24/ directory, and see whatthe
affinity is currently set to:
[root@archimedes Documentation]# cat/proc/irq/24/smp_affinity
ffffffff
This is a bitmask that represents which processors any interruptson IRQ
24 should be routed to.
The number held in the "smp_affinity" file is presented inhexadecimal format,
so in order to manipulate it properly we will need to convert ourbit patterns
from binary to hex before setting them in the proc file.
Each of the "f"s above represents a group of 4 CPUs, with therightmost
group being the least significant.
we're going to limit ourselves to only the first 4 CPUs (althoughwe can
address up to 32).
In short, this means you only have to worry about the rightmost "f"and you
can assume everything else is a "0" (ie, our bitmask is"0000000f").
"f" is the hexadecimal represenatation for the decimal number 15(fifteen)
and the binary pattern of "1111".
corresponds to a CPU in the server, which means we can use thefollowing
chart to represent the CPU bit patterns:
By combining these bit patterns (basically, just adding the Hexvalues), we
can address more than one processor at atime.
to talk to both CPU0 and CPU2 at the same time, the resultis:
If I want to address all four of the processors at once, then theresult is:
(Remember that we use the letters "a" through "f" to represent thenumbers
"10" to "15" in hex notation).
Given that, we now know that if we have a four processor system, wecan
assign any of 15 different CPU combinations to an IRQ (it would be16, but
it isn't legal to assign an IRQ affinity of "0" to any IRQ... ifyou try,
Linux will just ignore your attempt).
So.
above that all four of our CPUs had handled the close to the sameamount of
interrupts for our SCSI card?
the SCSI card to just one processor and leave the other three freeto
concentrate on doing othertasks.
our first CPU (CPU0) to handling the SCSI controllerinterrupts.
we would simply run the following command:
[root@archimedes /proc]# echo 1 >/proc/irq/24/smp_affinity
[root@archimedes /proc]# cat /proc/irq/24/smp_affinity
00000001
Now, let's test it out and see what happens:
[root@archimedes /proc]# cd /tmp/
[root@archimedes /tmp]# tar -zcf test.tgz/usr/src/linux-2.4.2
tar: Removing leading `/' from member names
[root@archimedes /tmp]# tar -zxf test.tgz&& rm -rf usr/
[root@archimedes /tmp]# tar -zxf test.tgz&& rm -rf usr/
[root@archimedes /tmp]# tar -zxf test.tgz&& rm -rf usr/
[root@archimedes /tmp]# tar -zxf test.tgz&& rm -rf usr/
[root@archimedes /tmp]# tar -zxf test.tgz&& rm -rf usr/
[root@archimedes /tmp]# cat /proc/interrupts | grep 24:
Compare that to the previous run without having the IRQ bound toCPU0:
All of the interrupts from the disk controller are now handledexclusively
by the first CPU (CPU0), which means that our other 3 proccessorsare free
to do other stuff now.
Finally, it should be pointed out that if you decide you no longerwant
SMP affinity and would rather have the system revert back to theold set up,
then you can simply do:
[root@archimedes /tmp]# cat /proc/irq/prof_cpu_mask>/proc/irq/24/smp_affinity
This will reset the "smp_affinity" file to use all "f"s, and willreturn to
the load sharing arrangement that we saw earlier.
What can I use it for?
- "balance" out multiple NICs in a multi-processormachine.
- database servers (or servers with lots of disk storage) that alsohave
Can I do this with processes?
At this time, no.