Home > System Tutorial > LINUX > body text

Linux CPU Interrupts: Asynchronous Events and Common Handling Mechanisms

WBOY
Release: 2024-02-11 15:06:40
forward
738 people have browsed it

Linux Interrupt is an asynchronous event that can occur at any time and can break the normal execution flow of the program. In order to handle these interrupts, the Linux kernel provides a general interrupt handling mechanism.

Linux CPU 中断:异步事件和通用处理机制

Interrupt is actually a signal called IRQ (interrupt request) sent by hardware or software.

Interrupts allow devices such as keyboards, serial cards, parallel ports, etc. to indicate that they require the CPU.

Once the CPU receives an interrupt request, the CPU will temporarily stop executing the running program and call a specific program called an interrupt handler or interrupt service routine (interrupt service routine).

The interrupt service routine or interrupt handler can be found in the interrupt vector table, which is located at a fixed address in memory. After the interrupt is processed by the CPU, execution of the previously interrupted program will resume.

In fact, when the machine starts, the system has already identified all devices and loaded the corresponding interrupt handlers into the interrupt table.

The following are two ways to request CPU attention:
\1. Based on interrupt
\2. Based on polling

All Linux operating systems are based on interrupt drivers.

When we press a key on the keyboard, the keyboard will tell the CPU that a key has been pressed. In this case, the voltage in the keyboard's IRQ line will change once, and this voltage change is a request from the device, which is equivalent to saying that the device has a request that needs to be processed.

/proc/interrupts file
On Linux machines, the file /proc/interrupts contains information about which interrupts are in use and how many times each processor has been interrupted.

\# cat /proc/interrupts 
     CPU0 CPU1 CPU2 CPU3 
 0: 3710374484   0  0  0 IO-APIC-edge timer 
 1:    20   0  0  0 IO-APIC-edge i8042 
 6:     5   0  0  0 IO-APIC-edge floppy 
 7:     0   0  0  0 IO-APIC-edge parport0 
 8:     0   0  0  0 IO-APIC-edge rtc 
 9:     0   0  0  0 IO-APIC-level acpi 
 12:    240   0  0  0 IO-APIC-edge i8042 
 14: 11200026   0  0  0 IO-APIC-edge ide0 
 51: 61281329   0  0  0 IO-APIC-level ioc0 
 59:     1   0  0  0 IO-APIC-level vmci 
 67: 19386473   0  0  0 IO-APIC-level eth0 
 75: 94595340   0  0  0 IO-APIC-level eth1 
NMI:     0   0  0  0
LOC: 3737150067 3737142382 3737145101 3737144204
ERR:     0
MIS:     0
Copy after login

The output of the above file is explained as follows:
● The first column indicates the IRQ number
● The second, third, and fourth columns indicate the number of times the corresponding CPU core was interrupted. In the above example, timer represents the interrupt name (which is the system clock). 3710374484 means that CPU0 was interrupted 3710374484 times. i8042 represents the keyboard controller that controls the keyboard and mouse.
● For interrupts like rtc (real time clock), the CPU will not be interrupted. Because RTC exists in electronic devices and is used to track time.
● NMI and LOC are drivers used by the system and cannot be accessed and configured by users.

The IRQ number determines the priority that needs to be processed by the CPU. The smaller the IRQ number means the higher the priority.
For example, if the CPU receives interrupts from both the keyboard and the system clock, the CPU will first service the system clock because its IRQ number is 0.
● IRQ0: System clock (cannot be changed)
● IRQ1: Keyboard controller (cannot be changed)
● IRQ3: Serial port controller of serial port 2 (if there is serial port 4, it also uses this interrupt)
● IRQ4: Serial port controller of serial port 1 (if there is serial port 3, it also uses this interrupt)
● IRQ5: Parallel port 2 and 3 or sound card
● IRQ6: Floppy disk controller
● IRQ7: Parallel port 1. It is used with printers or if there is no printer, can be used with any parallel port.

For the CPU on a joystick (or game controller), it does not wait for the device to send an interrupt. Because the joystick is mainly used for games, the movement of the joystick must be very fast, so it is ideal to use polling to detect whether the device needs the attention of the CPU. The disadvantage of using the polling method is that the CPU is in a busy waiting state because the CPU will check the device multiple times. But it should be noted that in Linux, this way of handling signals is also essential.

Hard interrupt
The scenarios discussed above are all examples of hard interrupts. Hard interrupts are mainly divided into two categories:
\1. Non-maskable interrupts (NMI): Just like the literal meaning of this interrupt type, this interrupt cannot be ignored or canceled by the CPU. NMI is sent on a separate interrupt line and is usually used for critical hardware errors, such as memory errors, fan failures, temperature sensor failures, etc.
\2. Maskable interrupts: These interrupts can be ignored or delayed by the CPU. This type of interrupt will be generated when the external pin of the cache controller is triggered, and the interrupt mask register will mask such interrupts. We can set a bit to 0 to disable the interrupt triggered on this pin.

Soft interrupt
These interrupts are generated when the CPU executes instructions (that is, when the process is running), because when executing instructions, the CPU (specifically, the arithmetic unit in the CPU) itself will generate an exception (this Exceptions at can also be understood as soft interrupts).

For example, dividing a number by 0 (of course this is impossible) will result in a divide-by-zero exception, causing the computer to cancel the calculation or display an error message.

The file /proc/stat contains some statistical information about the system kernel and some interrupt information.

\# cat /proc/stat 
cpu 17028082 5536753 5081493 1735530500 42592308 90006 479750 0
cpu0 5769176 1170683 1495750 403368354 39406374 90006 284864 0
cpu1 3714389 1451937 1186134 444082258 1084780 0 64876 0
cpu2 3791544 1471013 1211868 443988514 1056981 0 64764 0
cpu3 3752971 1443119 1187740 444091373 1044172 0 65244 0
intr 417756956 --- Output Truncated
Copy after login

In the intr line, the number of interrupts generated since the system was started is displayed. The first column represents the number of all serviced interrupts. Each subsequent column represents the total number of interrupts for a particular interrupt.

SMP_AFFINITY
SMP是指对称多处理器。smp_affinity文件主要用于某个特定IRQ要绑定到哪个CPU核心上。在/proc/irq/IRQ_NUMBER/目录下都有一个smp_affinity文件,这个文件中,所表示的CPU核心以十六进制来表示的。例如,网卡的中断号是:

grep eth0 /proc/interrupts
67: 23834931 0 0 0 IO-APIC-level eth0

cat /proc/irq/67/smp_affinity
00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001

上面的十六进制对应的十进制是1,也就是说所有的和网卡驱动相关的中断都是有CPU0来提供服务的。

我们可以通过手动改变smp_affinity文件中的值来将IRQ绑定到指定的CPU核心上,或者启用irqbalance服务来自动绑定IRQ到CPU核心上。

IRQ Balance
Irqbalance是一个linux的实用程序,它主要是用于分发中断请求到CPU核心上,有助于性能的提升。它的目的是寻求省电和性能优化之间的平衡。你可以使用yum进行安装:

 \# rpm -qa | grep irqbalance 
irqbalance-0.55-15.el5 
\# yum search irqbalance 
\# yum install irqbalance.x86_64
Copy after login

启动irqbalance服务后,中断在CPU上的分布如下:

 \# cat /proc/interrupts 
     CPU0  CPU1   CPU2   CPU3 
 0: 950901695    0    0     0 IO-APIC-edge timer 
 1:    13    0    0     0 IO-APIC-edge i8042 
 6:    96  10989   470     0 IO-APIC-edge floppy 
 7:     0    0    0     0 IO-APIC-edge parport0 
 8:     1    0    0     0 IO-APIC-edge rtc 
 9:     0    0    0     0 IO-APIC-level acpi 
 12:    109  1787    0     0 IO-APIC-edge i8042 
 15:    99 84813914    0     0 IO-APIC-edge ide1 
 51:   17371    0 46689970     0 IO-APIC-level ioc0 
 67:   1741    0    0 225409160 PCI-MSI eth0 
 83:     0    0    0     0 PCI-MSI vmci 
NMI:     0    0    0     0
LOC: 950902917 950903742 950901202 950901400
ERR:     0
MIS:     0
Copy after login

Irqbalance对于包含多个核心的系统来说是非常有用的。因为通常中断只被第一个CPU核心服务。以上就是教程网为各位朋友分享的Linu系统相关内容。想要了解更多Linux相关知识记得关注公众号“Linux”,或扫描下方二维码进行关注,更多干货等着你 !

Linux CPU 中断:异步事件和通用处理机制

The above is the detailed content of Linux CPU Interrupts: Asynchronous Events and Common Handling Mechanisms. For more information, please follow other related articles on the PHP Chinese website!

source:lxlinux.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template