A device tells the CPU about each event with a tap, called an interrupt. The tap costs 2,000 cycles. The work for the event costs 2,000 more. A cycle is one tick of the CPU clock.
The CPU has 1,000,000,000 cycles a second. How many events a second can it handle?
guess first
It handles 233,042 a second now. Events rise to 1,000,000. Then?
233,042handled a second. 254,050 came in
events handled as events rise
81 slices kept up19 lost events
run it
Raise the events. Watch what gets handled.
10k50k100k200k250k300k400k500k1M
Each square is 10 ms of one second. Green handled every event that came. Red lost some.
why it falls
Each tap uses cycles before any work starts.
Each event costs 4,000 cycles in all: 2,000 for the tap and 2,000 for the work. At most 250,000 events fit in one second.
Above that, taps keep coming and take their cycles first. The CPU spends the whole second being told, and does no work.
the working
One event: 1,500 + 500 + 2,000 = 4,000 cycles1,000,000,000 ÷ 4,000 = 250,000 events a second200,000: 203,240 × 4,000 = 812,960,000812,960,000 used, 18.7% idle1,000,000: 500,000 taps × 2,000 = 1,000,000,000Cycles left for work: 0
your call
1,000,000 events a second. How should the device tell the CPU?
How Computers WorkI/O: interrupts against polling
The interrupt storm
The device taps the CPU on every event. What happens when it taps a million times a second?
A device with work to hand over either waits to be asked or taps the processor on the shoulder, and the tabs are the four ways of settling that. The first slider sets how often the outside world speaks. Watch the share of cycles spent being told about work crowd out the work itself.
try this Open Let the device tap you every time, push Events a second arriving to the top, and watch the share of arrivals handled.
400,000
8
The second slider sets coalescing and affects only the third strategy. Every run lasts one second, with the same bursty arrivals, on one otherwise idle core.
25%of arrivals handled
79%of cycles spent being told
39.7 msmean wait, landing to handled
A machine services 233,000 device events a second and looks healthy. The rate rises to a million. How many does it service now?
More events in, fewer handled
One machine taking an interrupt for every event, at three arrival rates. Watch the green plates (handled) and the red heap (lost). Then move the sliders above and it plays your setting.
10 ms
10 ms: entering the handler. Saving registers and jumping, and no event is any closer to done.
an event on its way to the CPU
handled
lost: arrived to a full ring
CPU lid: told, work, idle
Show the code and trace
The polling program, running
1// Polling: sweep the device on a timer2voidmain(void) {3for (;;) {4sleep_until(next_sweep); // wait out the 5 µs5 next_sweep += 5000;6while (device_ready(dev)) // MMIO read, 500 cycles7handle(ring_pop(dev)); // 2,000 cycles of real work8 }9}
The interrupts program, running
1// Interrupts: the device taps on every event2voidon_irq(void) { // save, jump here: 1,500 cycles3queue_push(ring_pop(dev));4ack(dev);5} // return to a cold cache: 50067voidmain(void) {8for (;;) {9while (queue_len() > 0)10handle(queue_pop()); // 2,000 cycles of real work11wait_for_irq(); // nothing to do, so sleep12 }13}
The interrupt coalescing program, running
1// Coalescing: the device holds a batch, then taps once2voidon_irq(void) { // one entry per batch: 1,500 cycles3while (ring_has(dev))4queue_push(ring_pop(dev));5ack(dev);6} // return to a cold cache: 50078voidmain(void) {9for (;;) {10while (queue_len() > 0)11handle(queue_pop()); // 2,000 cycles of real work12wait_for_irq(); // nothing to do, so sleep13 }14}
The NAPI program, running
1// NAPI: one tap to wake up, then poll until the ring is empty2voidon_irq(void) { // save, jump here: 1,500 cycles3irq_disable(dev); // no more taps while we drain4schedule_poll();5} // return to a cold cache: 5006voidpoll(void) {7while (ring_has(dev))8handle(ring_pop(dev)); // 2,000 cycles of real work9irq_enable(dev); // ring empty: arm the tap again10}11voidmain(void) {12for (;;) wait_for_irq(); // nothing to do, so sleep13}
Tracepaused
10 ms +3,640 in · 3,640 taps · 1,360 done · 2,280 waiting · 0 lost
What got done
102,520
events serviced, in that second
406,480 arrived. 299,864 were never handled at all. 4,096 were still waiting in the ring when the second ended.
Where the cycles went
1.00 B cycles in that second, and 79% of them spent finding out that work had arrived.
real work21%
being told79%
idle0%
The cost of being told
795 M
cycles a second, before any work is done
397,480 taps, 0.3 events handled each.
Call 1: the keyboard
A keyboard sends about 1,000 events a second. Asking means one look across the bus every 5 µs, at 0.5 µs a look. A tap costs 2 µs: jumping to the handler and back, then refilling the cache it pushed out.
Which spends less of the second finding out that a key was pressed?
The dials wait until you make the call.
Call 2: one tap for sixteen
A network card at 400,000 events a second, tapping for every one. 79% of the second goes on being told, and 74% of the events are lost. Now the card waits for 16 events and taps once for the lot.
What share of the second goes on being told now?
The dials wait until you make the call.
Call 3: the storm
A storm: about a million events a second. This driver takes the first tap, turns its tap off, and drains the ring of waiting events until it is empty. Only then does it turn the tap back on.
How many taps does it take in that whole second?
The dials wait until you make the call.
The working
Where the second went and how many arrivals made it, redone every time you move a control.
The price of one tap01,5002,000Saving its place and jumping to the handler costs 1,500 cycles, and refilling the cache the handler pushed out costs 500 more. On this 1 GHz clock that is 2 µs. 1,500 + 500= 2,000 cycles
The second, spent being told01 sThe device taps once for every event, until a slice has no time left to take another. 397,480 taps this second at 2 µs each. That is 79% of the second gone before any work. 397,480 × 2= 794,960 µs
Left for real workreal work205,040 µsidle0 µsHandling one event takes 2 µs, so what is left fits 102,520 events at most. Every bit of it was used. 1,000,000 − 794,960= 205,040 µs
Arrivals handledarrived406,480handled102,520lost299,864102,520 handled out of 406,480 that arrived. 299,864 were lost and 4,096 were still waiting when the second ended. 102,520 ÷ 406,480= 25%
All four, at this arrival rate
Same second, same arrivals, same core. Only the way the CPU finds out is different.
polling405,732 serviced · 10% told · 2.0 ms
interrupts102,520 serviced · 79% told · 39.7 ms
interrupt coalescing403,392 serviced · 10% told · 2.3 ms
NAPI406,392 serviced · 14% told · 757 µs
Every arrival rate, side by side
Struck-through rows service fewer events a second than a lower rate on the same strategy. More work arrives and less gets finished.
1k/s1,024 serviced · 0% told · 3.5 µs
10k/s10,162 serviced · 2% told · 3.5 µs
50k/s50,810 serviced · 10% told · 3.5 µs
100k/s101,620 serviced · 20% told · 3.5 µs
200k/s203,240 serviced · 41% told · 706 µs
250k/s233,042 serviced · 51% told · 8.4 ms
300k/s195,140 serviced · 61% told · 18.9 ms
400k/s102,520 serviced · 79% told · 39.7 ms
500k/s45,950 serviced · 91% told · 89.1 ms
750k/s2,375 serviced · 100% told · 1724.6 ms
1M/s0 serviced · 100% told · -
2M/s0 serviced · 100% told · -
Why interrupts costs what it does
Each event triggers one interrupt and one context switch. The CPU saves registers, jumps to the handler, runs it, restores and resumes. Then it pays again to refill the cache the handler evicted. At low rates this is nearly free, and latency is the best on the page. That is why the board recommends it and why keyboards work this way. The problem is that the cost is charged per event, and the hardware takes it before the operating system can intervene. Past a few hundred thousand events a second, entries and exits use the whole budget. The deferred work that finishes each event never runs, and the CPU sits at 100% with nothing completed.
The tradeoff: The lowest latency available, until the arrival rate leaves the machine fully busy with no work completed.
What the board covers, and what it leaves out
The board says polling is bad, because while(!ready) { ask again; } burns a core doing nothing. It says interrupts are good, because the CPU issues a request, does other work, and jumps to the handler when the device is ready. That is correct, and it is why your keyboard does not cost a core. The board's second panel also covers the per-byte cost. With DMA, the device writes into RAM itself and the CPU gets one interrupt per block, so this lab ignores copying.
That leaves the per-event cost, which is not zero. Taking an interrupt costs 1,500 cycles to save registers, jump to the handler, run it, restore and return. The interrupted code then pays roughly 500 more to refill the cache lines the handler evicted. That is 2,000 cycles of notification against 2,000 cycles of real work. On one keypress it is invisible. But it is charged per event, and the device sets the rate.
More load, less work done
Leave the tab on interrupts and drag the rate slider from end to end. The serviced count climbs to 233,042 a second at 250k/s, then falls. At 1M/s arriving, this machine services 0 of them, with 100% of every cycle spent on entries and exits. The load rose four times, and finished work dropped to nothing.
This is receive livelock, and it comes from ordering, not from any queue. An interrupt is hardware. It preempts whatever is running, including the deferred half of the driver that finishes each event. So interrupt entries are paid first from the same budget, and only leftover cycles can service events. Past the crossover, each new event takes the cycles that would have completed an earlier one. The profiler shows a core at 100%. The throughput graph shows zero. Both are correct.
Watch latency while this happens. Up to the knee, taking every interrupt gives the best mean latency here, a few microseconds, better than any batching strategy. Then it becomes 39.7 ms for the events that survive, while 75% of arrivals are dropped. The events that get through are still handled quickly. Almost none get through.
Polling costs the same at any load
Switch to polling and put the rate back at the bottom. It sweeps every 5,000 cycles at 500 cycles a sweep, so at 1k/s it spends 100 M cycles, or 10% of the machine, confirming that nothing happened. Interrupts at the same rate spend 2 M. That factor of 49 is the board's case against polling, and it holds.
Now drag to the top. Polling's cost has not moved. It is 10% at a thousand events a second and 10% at two million, because it never depended on the work. It services 450,000 a second at 1M/s, while interrupts service 0. The strategy the board calls bad is the one that stays up. The reason is the same one that makes it bad: the device never decides how often the CPU is interrupted.
Coalescing moves the cliff further out
The second slider is the device's own fix. It holds events back and interrupts once per batch. If the batch has not filled within 50,000 cycles, it interrupts anyway. On the coalescing tab at 1M/s, a batch target of 8 services 372,954 a second. Raise it to 64 and the same second finishes 480,133. Fewer interruptions, more work.
The cost is latency, and you pay it whether or not throughput improves. Drop the rate to 300k/s, where this machine has room to spare, and compare the two batch targets: both service 304,860, which is every event. But mean latency goes from 15.0 µs to 28.5 µs, with nothing gained. The quiet end is worse: 1k/s with a batch target of 8 takes exactly as many interrupts as not coalescing at all (1,024 against 1,024), and each event waits 28.5 µs instead of 3.5 µs.
The cliff is still there. A fixed batch target divides the interrupt rate by a constant, so the interrupt count still grows with load, only 8 times slower. Push the rate to the top with a small batch target and the serviced count falls again. That is why real NICs make coalescing adaptive. Tuning rx-usecs by hand never stays right, because the best hold time at 3 a.m. is wrong at peak.
Fewer interrupts as load rises
NAPI is what Linux uses. The first event raises an interrupt. The driver takes it,disables that interrupt, and polls the ring until it is empty. Then it re-enables the interrupt and sleeps. No rate is configured anywhere.
At light load the ring empties between events, so every event gets its own tap and this behaves exactly like plain interrupts, with the same 3.5 µs and near-zero overhead. Under load the ring never empties, the interrupt is never re-armed, and one interrupt covers thousands of events. At 1M/s it takes 1 interruptions for the entire second and services 499,999. The interrupt count moves opposite to the load. No other strategy on this page has that property.
It has a cost. In the middle of the dial, around 250k/s, the hybrid takes far more interrupts than polling. The queue keeps emptying and the driver keeps re-arming, so polling's flat sweep is cheaper there. Real systems combine approaches. They use NAPI to survive the storm and coalescing to reduce interrupts in the middle. At the very top they use kernel bypass, where the device sends no interrupts at all.
Limits of this model
This is a cost model. No number on this page was measured on real hardware. An interrupt entry costs 1,500 cycles here, the cache refill after it 500, servicing an event 2,000, an MMIO register read 500, and the clock is a flat 1.00 B cycles a second. These are assumptions at plausible orders of magnitude, chosen so the slider can show the crossover. The shapes reproduce on real hardware: a flat cost for polling, a per-event cost for interrupts, and a curve that turns over. The exact rate where your machine turns over will differ.
One core, one device, one kind of event. A real machine spreads interrupts across cores with RSS and IRQ affinity, runs several devices with different rates and priorities, and schedules all of it. This lab does not model interrupt priority, nested handlers, softirq versus threaded IRQ, or ksoftirqd competing with the application it serves. Those change the numbers. They do not remove the cause of the collapse.
The second runs in a hundred slices, not event by event. Arrivals inside a slice are counted, not timed individually, and the queue carries over only at slice boundaries. So the lab reproduces backlog and drops, but not the fine structure of a microburst. The hybrid's interrupt count per slice comes from a busy-period formula instead of simulating each drain. It is exact on average and approximate per tick.
Drops here are simpler than a NIC's. The lab uses a ring of 4,096 descriptors, overwritten when full, plus events the CPU could not take an interrupt for. There is no flow control, no pause frames, no backpressure to the sender, and no difference between a dropped and a retransmitted packet. Mean latency covers only serviced events. Dropped events have no latency, which is why a livelocked machine looks fast in a latency histogram.