Linux CPU interruption [reprinted], linuxcpu reprinted
Interruption is actually a signal sent by hardware or software called IRQ (interrupt request.
The interrupt allows devices, such as keyboards, serial ports, and parallel ports, to indicate that they require CPU.
Once the CPU receives the interrupt request, the CPU temporarily stops running programs and calls 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 a fixed address in the memory. After the interrupted program is processed by the CPU, the interrupted program will be resumed.
In fact, when the machine is started, the system has identified all the devices and loaded the corresponding interrupt processor into the interrupt table.
The following are two methods to request CPU attention:
1. Interrupt-based
2. Polling-based
All linux operating systems are interrupt-driven.
When we press a button 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 the voltage changes are requests from the device, which means that the device has a request to process.
/Proc/interrupts File
On linux machines, the/proc/interrupts file package contains information about which interrupts are being used and how many times each processor is interrupted.
# Cat/proc/interrupts
CPU0 CPU1 CPU2 CPU3
0: 3710374484 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.
● Columns 2, 3, and 4 indicate the number of times the CPU core is interrupted. In the preceding example, timer indicates the interrupt name (system clock ). 3710374484 indicates that CPU0 has been interrupted for 3710374484 times. I8042 indicates the keyboard controller that controls the keyboard and mouse.
● For interruptions such as rtc (real time clock), the CPU will not be 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 users.
The IRQ Number determines the priority to be processed by the CPU. The smaller the IRQ Number, the higher the priority.
For example, if the CPU receives an interrupt from both the keyboard and system clock, the CPU will first serve the system clock because its IRQ Number is 0.
● IRQ0: system clock (cannot be changed)
● IRQ1: keyboard controller (unchangeable)
● IRQ3: Serial Port Controller of Serial Port 2 (if serial port 4 exists, 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 Controller
● IRQ7: Parallel Port 1. It is used for a printer or, if there is no printer, it can be used for any parallel port.
For a CPU like an operator lever (or a game handle), it does not wait until the device sends an interrupt. Because the operation lever is mainly used for games, the operation lever must be moved very quickly, so it is ideal to use the round-robin method to detect whether the device requires CPU attention. The disadvantage of using the round robin method is that the CPU is in a busy status because the CPU constantly checks the device multiple times. However, in linux, this method of signal processing is also essential.
Hard interrupt
The scenarios discussed above are examples of hard interruptions. There are two types of hard interrupt:
1. Non-maskable interrupts (NMI): Like the literal meaning of the interrupt type, such interruptions cannot be ignored or canceled by the CPU. NMI is sent on a separate disconnection path. It is usually used for critical hardware errors, such as memory errors, Fan faults, and temperature sensor faults.
2. Maskable interrupts: These interruptions can be ignored by the CPU or processed with delay. This type of interrupt occurs when the external pins of the cache controller are triggered, and the interrupt shielding register blocks the interrupt. We can set a bit to 0 to disable the interruption triggered on this pin.
Soft Interrupt
These interruptions are generated when the CPU executes commands (that is, when the process is running), because when the commands are executed, the CPU (specifically, the memory in the CPU) itself generates an exception (the exception here can also be understood as a soft interrupt ).
For example, if a number is divided by 0 (of course this is not possible), a divide-by-zero exception will occur, causing the computer to cancel the calculation or display an error message.
The file/proc/stat contains statistics about the system kernel and interrupt information.
# Cat/proc/stat
Cpu 17028082 5536753 5081493 1735530500 42592308 90006 0
Cpu0 5769176 1170683 1495750 403368354 39406374 90006 0
Cpu1 3714389 1451937 1186134 444082258 0 1084780 0
Cpu2 3791544 1471013 1211868 443988514 0 1056981 0
Cpu3 3752971 1443119 1187740 444091373 0 1044172 0
Intr 417756956 --- Output Truncated
The intr line shows the number of interruptions that have occurred since the system was started. The first column indicates the number of service interruptions. Each subsequent column represents the total number of specific interruptions.
SMP_AFFINITY
SMP is a symmetric multi-processor. The smp_affinity file is mainly used for the CPU core to which a specific IRQ is bound. There is an smp_affinity file in the/proc/irq/IRQ_NUMBER/directory. In this file, the CPU core is expressed in hexadecimal notation. For example, the NIC interrupt number is:
Grep eth0/proc/interrupts
67: 23834931 0 0 0 IO-APIC-level eth0
Cat/proc/irq/67/smp_affinity
00000000,00000000, 00000000,00000000, 000000000000,00000000, 000000000000,00000001
The hexadecimal value corresponding to the above is 1 in decimal format. That is to say, all the interruptions related to the NIC driver are provided with CPU0.
You can manually change the value in the smp_affinity file to bind IRQ to the specified CPU core, or enable the irqbalance service to automatically bind IRQ to the CPU core.
IRQ Balance
Irqbalance is a linux utility used to distribute interrupt requests to the CPU core and improve performance. It aims to seek a balance between power saving and performance optimization. You can use yum for installation:
# Rpm-qa | grep irqbalance
Irqbalance-0.55-15.el5
# Yum search irqbalance
# Yum install irqbalance. x86_64
After the irqbalance service is started, the interrupted CPU distribution is as follows:
# Cat/proc/interrupts
CPU0 CPU1 CPU2 CPU3
0: 950901695 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 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. Because the service is interrupted only by the first CPU core service.