Linux Internals
IRQ Affinity on Linux: How Interrupts Reach Your CPU Cores
By NileVPS Team · Published · 5 min read
اقرأ المقال بالعربيEvery time a network card receives a packet or a disk finishes a read, it tells the CPU through an interrupt request, or IRQ. The kernel then pauses whatever that core was doing and runs a handler. On a quiet server nobody notices. On a busy web server or proxy pushing many small packets, interrupts can pile up on a single core while the others sit idle, and latency starts to wobble. IRQ affinity is the setting that decides which CPU cores are allowed to handle each interrupt. This guide explains how it works, how to inspect it and when changing it is worth your time.
What an IRQ actually is
Hardware devices cannot call your program directly. Instead they raise a signal, and the interrupt controller delivers it to a CPU. Modern devices mostly use MSI or MSI-X, which lets one device expose several interrupt vectors, typically one per queue. A multi-queue network card can therefore spread receive work across cores, but only if the interrupts for those queues are routed to different cores.
The handling happens in two phases. The hard interrupt handler runs immediately and does the minimum. The heavier work, such as processing packets through the network stack, is deferred to a softirq. You can watch both: hard IRQs in /proc/interrupts and softirqs in /proc/softirqs, or as ksoftirqd/N threads in top when a core is overloaded.
Inspecting interrupts
Start with the counters:
cat /proc/interrupts
watch -n1 -d "grep -E 'virtio|eth|ens' /proc/interrupts"Each row is an IRQ number, each column is a CPU and the values are how many interrupts that CPU has handled. On a KVM-based VPS you will usually see entries such as virtio0-input.0 and virtio0-output.0 for the network device and virtio1-req.0 for the disk. If one column grows much faster than the rest, that core is doing most of the interrupt work.
To see per-core time spent servicing interrupts, use mpstat from the sysstat package:
mpstat -P ALL 1The %irq and %soft columns show hard and soft interrupt load per core.
Reading and setting affinity
Each IRQ has a directory under /proc/irq/<number>/. Two files matter:
smp_affinity: a hexadecimal bitmask of allowed CPUs (1= CPU0,2= CPU1,4= CPU2,f= CPUs 0-3).smp_affinity_list: the same thing as a readable list, such as0-3or2.
cat /proc/irq/24/smp_affinity_list
echo 2 | sudo tee /proc/irq/24/smp_affinity_listThe second command pins IRQ 24 to CPU2. Some interrupts are managed by the kernel and will refuse the change with an I/O error; that is expected for certain MSI-X vectors. Changes made this way are lost at reboot, so persist them with a small systemd unit or script that runs at boot.
irqbalance: the automatic option
Most distributions ship irqbalance, a daemon that periodically redistributes interrupts based on load:
systemctl status irqbalance
sudo systemctl enable --now irqbalanceFor general-purpose servers it is the sensible default. If you decide to set affinity by hand, either stop irqbalance or tell it to leave specific IRQs alone using the IRQBALANCE_BANNED_CPUS or --banirq options in /etc/default/irqbalance (the file is /etc/sysconfig/irqbalance on RHEL-family systems). Otherwise it may quietly undo your work.
When the device has only one queue: RPS and RFS
Many virtual network devices on smaller VPS plans expose a single receive queue. Pinning that one IRQ cannot spread work across cores. Receive Packet Steering (RPS) solves this in software by letting the kernel hand packet processing to other CPUs after the interrupt arrives:
echo f | sudo tee /sys/class/net/eth0/queues/rx-0/rps_cpusReceive Flow Steering (RFS) goes further and tries to process each flow on the core where the consuming application runs:
echo 32768 | sudo tee /proc/sys/net/core/rps_sock_flow_entries
echo 32768 | sudo tee /sys/class/net/eth0/queues/rx-0/rps_flow_cntReplace eth0 with your interface name from ip link. Check how many queues you have with ethtool -l eth0.
Does this matter on a VPS?
Be realistic. On a virtual machine the hypervisor already handles physical interrupts, and your guest sees virtual ones. Your "CPU2" is a virtual CPU that the host schedules onto physical cores. IRQ tuning in the guest still affects how work is split among your vCPUs, but the gains are smaller than on bare metal and depend heavily on workload.
| Situation | Worth tuning? |
|---|---|
| Low traffic site, database, app server | No, keep irqbalance defaults |
| High packet-rate proxy, DNS, game server | Possibly, measure %soft per core first |
One core shows high %soft, others idle | Yes, try RPS or spreading queues |
| Latency-sensitive app pinned to certain cores | Yes, keep IRQs off those cores |
The last row pairs naturally with CPU pinning with taskset: put interrupts on one set of cores and your critical process on another so they do not compete.
A safe tuning checklist
- Record a baseline:
mpstat -P ALL 1and your application latency under real load. - Identify the hot IRQs in
/proc/interrupts. - Try one change at a time: RPS first for single-queue devices, then affinity.
- Measure again under the same load.
- Persist only the changes that helped, and document them.
If you want more vCPUs to spread network work across, compare plan sizes on the pricing page.
FAQ
Should I disable irqbalance?
Only if you are setting affinity manually for a specific reason and verifying the result. For most servers irqbalance gives a reasonable spread with no maintenance. Disabling it without a plan usually leaves all interrupts on CPU0.
Why do I get "Input/output error" when writing smp_affinity?
Some interrupts are managed by the kernel, particularly per-queue MSI-X vectors on multi-queue devices, and their affinity is fixed. The kernel already spreads those across CPUs, so there is generally nothing to fix.
Will IRQ affinity make my website faster?
For typical sites, no measurable difference. It helps when a server handles a very high rate of packets and one core is saturated with softirq work. Measure before changing anything.