Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Interrupt storm lab1 of 5
device CPU each tap costs 2,000 cycles each event's work costs 2,000 cycles one second is 1,000,000,000 cycles

the story

How many events a second can the CPU handle?

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?

250,000 events a second come in CPU 233,042events handled 1,000,000 events a second come in CPU ?events handled

guess first

It handles 233,042 a second now. Events rise to 1,000,000. Then?

233,042handled a second. 254,050 came in

81 slices kept up 19 lost events

run it

Raise the events. Watch what gets handled.

250,000 a second

Each square is 10 ms of one second. Green handled every event that came. Red lost some.

200,000 a secondtold 40.6%work 40.6% 250,000 a secondtold 50.8%work 46.6% 1,000,000 a secondtold 100.0%work 0.0%

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
device CPU 1,000,000 events a second how should the device tell the CPU?

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

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.

The device on the left fills a ring of events and signals the CPU on the right, either over its interrupt line or when the CPU asks across the bus. The CPU's ten milliseconds are split into time spent being told, real work and idle, with a pin at the sampled instant.interrupt line3,640 tapsbusthe device+3,640 arrivedring 2,280 of 4,096nothing lostthe CPU, this slice1,360 handledtold 73% · work 27% · idle 0%
Show the code and trace
The interrupts program, running
// Interrupts: the device taps on every eventvoid on_irq(void) {  // save, jump here: 1,500 cycles  queue_push(ring_pop(dev));  ack(dev);}  // return to a cold cache: 500 void main(void) {  for (;;) {    while (queue_len() > 0)      handle(queue_pop());  // 2,000 cycles of real work    wait_for_irq();  // nothing to do, so sleep  }}
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.

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.

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.

The working

Where the second went and how many arrivals made it, redone every time you move a control.

  1. 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
  2. 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
  3. 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
  4. 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.

Sources

the lesson I/O, Interrupts & the Bus
The whole idea this lab came from, told start to finish.

on the path Module 3 of 7 in How Computers Work: Many programs, one machine
The lessons, boards and videos around this lab, in reading order.

Board: IO Interrupts and the Bus

next lab Counting the crowd
A hundred thousand different customers, and a tally board of 12 KB. How far off is its count?