In continuation of the previous text第10章:中断处理-1: Installing an Interrupt Handler, let's GO ahead.
The /proc Interface
Whenever a hardware interrupt reaches the processor, an internal counter is incremented, providing a way to check whether the device is working as expected. Reported interrupts are shown in /proc/interrupts. The following snapshot was taken on a two-processor Pentium system:
当硬件中断到达处理器时,一个内部计数器会递增,这为检查设备是否正常工作提供了一种方式。已报告的中断信息会显示在 /proc/interrupts 中。以下是在双处理器奔腾系统上截取的快照:
root@montalcino:/bike/corbet/write/ldd3/src/short# m /proc/interrupts
CPU0 CPU1
0: 4848108 34 IO-APIC-edge timer
2: 0 0 XT-PIC cascade
8: 3 1 IO-APIC-edge rtc
10: 4335 1 IO-APIC-level aic7xxx
11: 8903 0 IO-APIC-level uhci_hcd
12: 49 1 IO-APIC-edge i8042
NMI: 0 0
LOC:4848187 4848186
ERR: 0
MIS: 0
The first column is the IRQ number. You can see from the IRQs that are missing that the file shows only interrupts corresponding to installed handlers. For example, the first serial port (which uses interrupt number 4) is not shown, indicating that the modem isn’t being used. In fact, even if the modem had been used earlier but wasn’t in use at the time of the snapshot, it would not show up in the file; the serial ports are well behaved and release their interrupt handlers when the device is closed.
第一列是 IRQ 号。从缺失的 IRQ 可以看出,该文件仅显示已安装处理程序的中断。例如,第一个串口(使用中断号 4)未显示,说明调制解调器未被使用。实际上,即使调制解调器之前被使用过,但在快照拍摄时未被使用,也不会出现在文件中 —— 串口驱动在设备关闭时会妥善释放中断处理程序。
The /proc/interrupts display shows how many interrupts have been delivered to each CPU on the system. As you can see from the output, the Linux kernel generally handles interrupts on the first CPU as a way of maximizing cache locality.* The last two columns give information on the programmable interrupt controller that handles the interrupt (and that a driver writer does not need to worry about), and the name(s) of the device(s) that have registered handlers for the interrupt (as specified in the dev_name argument to request_irq).
/proc/interrupts 的显示内容包含系统中每个 CPU 接收的中断次数。从输出可以看出,Linux 内核通常在第一个 CPU 上处理中断,以最大化缓存局部性 *。最后两列提供了处理该中断的可编程中断控制器信息(驱动开发者无需关注),以及已为该中断注册处理程序的设备名称(即 request_irq 函数中 dev_name 参数指定的名称)。
The /proc tree contains another interrupt-related file, /proc/stat; sometimes you’ll find one file more useful and sometimes you’ll prefer the other. /proc/stat records several low-level statistics about system activity, including (but not limited to) the number of interrupts received since system boot. Each line of stat begins with a text string that is the key to the line; the intr mark is what we are looking for. The following (truncated) snapshot was taken shortly after the previous one:
/proc 目录中还有另一个与中断相关的文件 /proc/stat,两者各有适用场景。/proc/stat 记录了系统活动的多项底层统计信息,包括(但不限于)系统启动以来接收的中断次数。该文件的每一行以一个文本字符串开头(作为行的标识),我们关注的是 intr 开头的行。以下是在前一个快照后不久截取的(截断的)快照:
intr 5167833 5154006 2 0 2 4907 0 2 68 4 0 4406 9291 50 0 0
The first number is the total of all interrupts, while each of the others represents a single IRQ line, starting with interrupt 0. All of the counts are summed across all processors in the system. This snapshot shows that interrupt number 4 has been used 4907 times, even though no handler is currently installed. If the driver you’re testing acquires and releases the interrupt at each open and close cycle, you may find /proc/stat more useful than /proc/interrupts.
第一个数字是所有中断的总数,其余每个数字代表一个 IRQ 线的中断次数(从中断 0 开始)。所有计数是系统中所有处理器的总和。此快照显示,即使当前未安装处理程序,中断号 4 也已被使用了 4907 次。如果你测试的驱动在每次打开和关闭循环中获取并释放中断,可能会发现 /proc/stat 比 /proc/interrupts 更有用。
Another difference between the two files is that interrupts is not architecture dependent (except, perhaps, for a couple of lines at the end), whereas stat is; the number of fields depends on the hardware underlying the kernel. The number of available interrupts varies from as few as 15 on the SPARC to as many as 256 on the IA-64 and a few other systems. It’s interesting to note that the number of interrupts defined on the x86 is currently 224, not 16 as you may expect; this, as explained in include/asm-i386/irq.h, depends on Linux using the architectural limit instead of an implementation-specific limit (such as the 16 interrupt sources of the old-fashioned PC
interrupt controller).
两个文件的另一个区别是:interrupts 与架构无关(可能最后几行除外),而 stat 则依赖于架构 —— 字段数量由内核底层的硬件决定。可用中断的数量因架构而异:SPARC 系统最少仅 15 个,IA-64 和其他一些系统最多可达 256 个。值得注意的是,x86 架构上定义的中断数量目前为 224 个,而非预期的 16 个。正如 include/asm-i386/irq.h 中所解释的,这是因为 Linux 使用了架构限制,而非实现特定限制(如老式 PC 中断控制器的 16 个中断源)。
补充说明:
-
/proc/interrupts关键字段解析-
IRQ 号列:仅显示已注册处理程序的中断,未使用的中断不列出;
-
CPU 列:每个 CPU 核心处理该中断的次数,反映中断在多核心间的分配情况;
-
控制器类型列:如
IO-APIC-edge(边缘触发)、IO-APIC-level(电平触发),指示中断控制器的类型和触发方式; -
设备名列:对应
request_irq中的dev_name,用于识别中断所属的驱动 / 设备。
-
-
/proc/stat中intr行的特点-
包含系统启动以来的所有中断计数(包括已释放处理程序的中断);
-
第一个数值是总中断数,后续数值按 IRQ 号顺序排列(从 0 开始),未使用的 IRQ 对应数值为 0;
-
适合长期跟踪中断使用情况,尤其适用于 “动态申请 / 释放中断” 的驱动测试。
-
-
中断触发方式的含义
-
边缘触发(edge):中断仅在信号电平从低到高(或高到低)的瞬间触发,适合短脉冲信号(如老式串口);
-
电平触发(level):只要信号保持高(或低)电平,就会持续触发中断,需驱动及时处理以避免重复触发(如 USB 设备)。
-
-
多 CPU 中断分配的意义
内核默认将中断分配给第一个 CPU,是为了利用缓存局部性 —— 中断处理程序的数据可能仍在 CPU 缓存中,减少内存访问开销。现代内核也支持中断均衡(如
irqbalance服务),可将中断分配到不同 CPU,提升多核心系统的整体性能。

被折叠的 条评论
为什么被折叠?



