Interrupts are actually signals sent by hardware or software called IRQ (Interrupt request).
Interrupts allow devices such as keyboards, serial cards, and ports to indicate that they require a CPU.
Once the CPU receives the interrupt request, the CPU temporarily stops executing the running program and invokes a specific program called the interrupt processor or interrupt Service program (interrupt service routine).
The interrupt service program or interrupt processor can be found in the interrupt vector table, which is located in the fixed address in memory. After the interrupt is processed by the CPU, the program that was interrupted before execution resumes.
In fact, when the machine starts, the system has identified all the devices, and the corresponding interrupt processor is loaded into the interrupt table.
Here are two ways to request CPU attention:
1. Interrupt-based
2. Poll-based
All Linux operating systems are interrupt-driven.
When we press a key on the keyboard, the keyboard will say to the CPU that a key has been pressed. In this case, the voltage in the IRQ line of the keyboard changes once, and this voltage change is the request from the device, which is equivalent to saying that the device has a request to be processed.
/proc/interrupts file
On a Linux machine,/proc/interrupts This file contains information about which interrupts are being used 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
The output of the above file is explained as follows:
The first column indicates the IRQ number
The combined second column indicates the number of times the corresponding CPU core has been interrupted. In the example above, the timer indicates the interrupt name (for the system clock). 3710374484 indicates that the CPU0 was interrupted 3,710,374,484 times. The i8042 represents a keyboard controller that controls the keyboard and mouse.
For interrupts such as RTC (real Time clock), the CPU is not interrupted. Because RTC exists in electronic devices, it is used to track time.
NMI and Loc are the drivers used by the system and cannot be accessed and configured by the user.
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 at the same time, the CPU will first serve the system clock because his IRQ number is 0.
IRQ0: System clock (cannot be changed)
IRQ1: Keyboard controller (cannot change)
IRQ3: Serial port 2 serial controller (if there is a serial port 4, it also use this interrupt)
IRQ4: Serial port 1 Serial controller (if there is a serial port 3, it also use this interrupt)
IRQ5: With Port 2 and 3 or sound card
IRQ6: Floppy Controller
IRQ7: And Port 1. It is used for printers or if there is no printer, can be used for any of the same port.
For CPUs like the joystick (or gamepad), it does not wait for the device to send interrupts. Because the joystick is used primarily for games, the movement of the joystick must be very fast, so it is desirable to use polling to detect whether the device requires CPU attention. The disadvantage of using polling is that the CPU is in a busy state because the CPU keeps checking the device multiple times. However, it is important to note that this approach to signal processing is also essential in Linux.
Hard Interrupt
The scenarios discussed above are examples of hard interrupts. There are two main categories of hard interrupts:
1. unshielded interrupts (non-maskable interrupts, NMI): Like the literal meaning of this type of interrupt, this interruption is not likely to be ignored or canceled by the CPU. NMI is sent on a separate interrupt line and is typically used for errors in critical hardware such as memory errors, fan failures, temperature sensor failures, and so on.
2. Shielded interrupts (maskable interrupts): These interrupts can be ignored or deferred by the CPU. This type of interrupt occurs when the external pins of the cache controller are triggered, and the interrupt screen register will mask such interrupts. We can set a bit to 0 to disable the interrupt triggered by this pin.
Soft interrupt
These interrupts are generated when the CPU executes instructions (that is, when the process is running), because the CPU (which is exactly the operator in the CPU) itself produces an exception (the exception here can also be understood as a soft interrupt) when executing the instruction.
For example, dividing a number by 0 (which is not possible, of course) results in an Divide-by-zero exception that causes the computer to cancel the calculation or display an incorrect message.
In the file/proc/stat, there are some statistical information about the kernel of the system, and some interrupt information is included.
# 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
In Intr, this line shows the number of interrupts that have been generated since the system started. The first column represents the number of interrupts that are serviced. Each subsequent column represents the total number of a particular interrupt.
Smp_affinity
SMP refers to symmetric multi-processor. The smp_affinity file is used primarily for the CPU core to which a particular IRQ is bound. There is a smp_affinity file in the/proc/irq/irq_number/directory, in which the CPU cores represented in hexadecimal are represented in this file. For example, the interrupt number for the network card is:
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
The hexadecimal corresponding to the above decimal is 1, which means that all the network card driver-related interrupts are CPU0 to provide services.
We can bind the IRQ to the specified CPU core by manually changing the value in the Smp_affinity file, or enable the Irqbalance service to automatically bind the IRQ to the CPU core.
IRQ Balance
Irqbalance is a Linux utility that is primarily used to distribute interrupt requests to the CPU core, helping to improve performance. Its purpose is to seek a balance between power saving and performance optimization. You can use Yum to install:
# Rpm-qa | grep irqbalance
Irqbalance-0.55-15.el5
# Yum Search Irqbalance
# yum Install irqbalance.x86_64
After starting the Irqbalance service, the interrupt is distributed on the CPU as follows:
# 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
Irqbalance is very useful for systems that contain multiple cores. Since usually interrupts are only served by the first CPU core.
Linux CPU Interrupt "reprint"