---
title: I/O interconnects, DMA and interrupts
heading: Moving data between devices and memory at tens of gigabytes per second without drowning the CPU
eyebrow: S4 · Lesson 45 · I/O interconnects, DMA and interrupts
xp: 50
---

::: hook
A 100 Gb/s network card delivers millions of packets a second; an NVMe drive several gigabytes per second. Lesson 44 measured storage in IOPS and queues. This lesson follows the data across the link: serial lanes of PCI Express with their encoding overhead, DMA engines that copy into memory while the CPU works, and interrupts that tell the CPU something arrived. Each has a fixed cost per event, and at high rates those costs, not the link's raw bandwidth, decide how much of a processor I/O consumes. The answers are the same ones lesson 27's memory systems used: wider, batched and overlapped.
:::

::: play
- The fraction of one core spent on interrupts for a 10 Gb/s stream of packets, against packet size, with 5 µs per interrupt. Interrupt coalescing (one interrupt per batch of packets) divides it by the batch size.
- At small packets and one interrupt each, a single core can't keep up at all (the fraction exceeds 1); coalescing or polling brings it down to a few percent.
:::

```sim plot
view: graph
x: P = 64..9000 log
y: F = 0.001..20 log
slider: B = 1..256 step 1 = 1
plot: 10e9/(P*8)*5e-6/B | label: cores busy with interrupts
plot: 1 + 0*P | dashed | color: ch2 | label: one core
readout: packets per second at 1500 bytes, millions = 10e9/(1500*8)/1e6
```

::: predict
## A PCIe 4.0 x16 slot runs 16 lanes at 16 GT/s with 128b/130b encoding. About how much data can it move in each direction?

- [ ] 256 GB/s
- [ ] 64 GB/s
- [x] 31.5 GB/s
- [ ] 16 GB/s

::: reveal
Each lane carries $16 \times 10^9 \times 128/130$ bits per second, 1.97 GB/s, and sixteen lanes give 31.5 GB/s in each direction at once. Protocol overhead (packet headers, acknowledgments) takes a further 5 to 15% in practice, depending on transfer size.
:::
:::

::: nameit
## Serial links

PCI Express, USB, SATA and Ethernet send data over differential lanes. Each lane's line rate, in transfers per second, is reduced by its **line coding**: 8b/10b (PCIe 1 and 2, 80% efficient) or 128b/130b (PCIe 3 to 5, 98.5%). PCIe doubles the per-lane rate each generation: 2.5, 5, 8, 16, 32 GT/s. Bandwidth scales with the number of lanes, x1 to x16, and runs full duplex. On top, each transaction carries headers of about 20 to 30 bytes, so small transfers waste a larger share.

## DMA

A **direct memory access** engine, built into the device, copies data between the device and memory itself, given a list of buffer addresses (a descriptor ring) by the driver. The CPU only sets up the descriptors and handles completions; it never touches the bytes. Without DMA, a CPU copying data spends its time on loads and stores that do no computation.

## Interrupts, coalescing and polling

An interrupt costs microseconds once its handler, cache misses and context switches are counted. At one interrupt per event, a fast device can overwhelm a core: $N$ events per second at $t$ each uses $Nt$ cores. Two remedies:

- **Interrupt coalescing:** interrupt once per $B$ events, or after a timeout, dividing the cost by $B$ at the price of latency.
- **Polling:** at high load, turn interrupts off and check the descriptor ring in a loop; turn them back on when the load falls. Linux's network stack does this automatically.

## Little's law for I/O

To sustain a rate $\lambda$ with a latency $L$ per transaction, a device needs $\lambda L$ transactions in flight: deep queues are what let high-latency devices such as SSDs reach high bandwidth.
:::

::: example
## Compute the per-direction bandwidth of PCIe 3.0 x8 (8 GT/s, 128b/130b) and PCIe 2.0 x8 (5 GT/s, 8b/10b).

::: solution
1. **PCIe 3.0 per lane.**
   $$\begin{aligned} B &= 8 \times 10^9 \times \tfrac{128}{130}/8 \\ &= 0.985\ \text{GB/s} \end{aligned}$$
   per lane, so 7.88 GB/s for eight.
2. **PCIe 2.0 per lane.**
   $$\begin{aligned} B &= 5 \times 10^9 \times 0.8/8 \\ &= 0.5\ \text{GB/s} \end{aligned}$$
   per lane, so 4.0 GB/s for eight.
3. **Why the jump.** Going from 5 to 8 GT/s gave nearly double the bandwidth because the more efficient encoding came with it.

**Answer:** PCIe 3.0 x8 carries 7.88 GB/s per direction, and PCIe 2.0 x8 carries 4.0 GB/s.
:::
:::

::: example
## A 10 Gb/s NIC receives 1500-byte packets at line rate. With 5 µs of CPU per interrupt, how many cores do interrupts consume with one interrupt per packet, and with one per 64 packets?

::: solution
1. **Packet rate.**
   $$\begin{aligned} r &= 10^{10}/(1500 \times 8) \\ &= 833\,000\ \text{per s} \end{aligned}$$
2. **One interrupt per packet.**
   $$833\,000 \times 5\ \text{µs} = 4.17\ \text{cores}$$
   which is impossible for one core.
3. **One interrupt per 64 packets.** That's 13 000 interrupts per second, 0.065 cores, 6.5% of one.
4. **The cost.** Coalescing adds up to 64 packet-times of latency, about 77 µs, which is why coalescing timers are tunable.

**Answer:** interrupts consume 4.17 cores with one per packet, and 0.065 cores (6.5% of one) with one per 64 packets.
:::
:::

::: example
## An NVMe SSD has 80 µs of read latency. How many reads must be in flight to reach 1 million 4 KB reads per second, and what bandwidth is that?

::: solution
1. **Reads in flight.** By Little's law:
   $$10^6 \times 80 \times 10^{-6} = 80$$
   reads outstanding.
2. **Bandwidth.**
   $$10^6 \times 4096\ \text{B/s} = 4.1\ \text{GB/s}$$
   which needs at least a PCIe 4.0 x4 link (7.9 GB/s).
3. **One read in flight.** The same drive would do 12 500 reads per second, so the throughput is set by the queue depth rather than by the flash itself.

**Answer:** 80 reads must be in flight, for a bandwidth of 4.1 GB/s.
:::
:::

::: check
given: r = 2.5 | 8 | 16 | 32; n = 1 | 4 | 16; c = 0.8 | 0.9846
answer: r*c*n/8
unit: GB/s
tolerance: 0.5%

## A link has {n} lanes at {r} GT/s with an encoding efficiency of {c}. What is its bandwidth in each direction?

::: right
Right. Line rate × efficiency × lanes ÷ 8 = {answer}.
:::

::: misses
- [r*n/8] Account for the encoding overhead.
- [r*c*n] Convert bits to bytes.
- [any] Not yet. $r \cdot c \cdot n/8$.
:::
:::

::: check
kind: parts
given: G = 1 | 10 | 100; P = 64 | 512 | 1500; t = 2 | 5; B = 16 | 64

## A NIC receives {G} Gb/s of {P}-byte packets; each interrupt costs {t} µs of CPU.

::: part
answer: G*1e9/(P*8)*t*1e-6
tolerance: 0.5%

### How many cores do interrupts use at one per packet?

::: misses
- [G*1e9/P*t*1e-6] Packets per second is bits per second over bits per packet.
- [any] Not yet. Packets/s × time per interrupt.
:::
:::

::: part
answer: G*1e9/(P*8)*t*1e-6/B
tolerance: 0.5%

### And with one interrupt per {B} packets?

::: misses
- [G*1e9/(P*8)*t*1e-6*B] Coalescing divides the count by {B}.
- [any] Not yet. Divide by {B}.
:::
:::

::: right
Right. Small packets at high rates are the hardest case, which is why fast networking polls.
:::
:::

::: check
given: R = 0.1 | 0.5 | 1 | 2; L = 20 | 80 | 200
answer: R*1e6*L*1e-6
tolerance: 0.5%

## A device has {L} µs of latency per request. How many requests must be outstanding to sustain {R} million per second?

::: right
Right. Little's law: $\lambda L$ = {answer}.
:::

::: misses
- [R*1e6/(L*1e-6)] Multiply rate by latency.
- [any] Not yet. $N = \lambda L$.
:::
:::

::: check
given: S = 64 | 256 | 4096; h = 24 | 28
answer: S/(S + h)*100
tolerance: 0.5%

## A PCIe transfer carries {S} bytes of payload with {h} bytes of packet overhead. What percentage of the link carries payload?

::: right
Right. {answer}%: large transfers amortize the overhead.
:::

::: misses
- [h/(S + h)*100] That's the overhead's share.
- [any] Not yet. Payload over payload plus overhead.
:::
:::

::: check
given: B = 16 | 64 | 256; P = 1500 | 9000; G = 10 | 40
answer: B*P*8/(G*1e9)*1e6
unit: µs
tolerance: 0.5%

## A NIC at {G} Gb/s coalesces {B} packets of {P} bytes per interrupt. Up to how much latency does that add to the first packet of a batch?

::: right
Right. The time to receive {B} packets: {answer}.
:::

::: misses
- [P*8/(G*1e9)*1e6] That's one packet's time; the first waits for the whole batch.
- [any] Not yet. $BP \times 8/\text{rate}$.
:::
:::

::: check
kind: choice

## Why does a CPU copy with loads and stores make a poor substitute for DMA when receiving 10 GB/s from a device?

- [x] The CPU would spend its cycles moving bytes that it doesn't otherwise need to touch, while a DMA engine moves them without using the CPU at all
- [ ] The CPU can't address device memory
- [ ] Loads and stores are slower than the PCIe link
- [ ] DMA transfers don't need memory bandwidth

::: right
Right. DMA frees the CPU for computation; the CPU only handles descriptors and completions.
:::

::: misses
- [2] CPUs can access device registers and memory-mapped buffers; it's just wasteful for bulk data.
- [3] A core can move tens of GB/s; the point is that it shouldn't have to.
- [4] DMA still uses memory bandwidth; it saves CPU time.
:::
:::

::: check
kind: python
given: lam = 0.2 | 0.6 | 0.9; B = 8 | 32

## Simulate interrupt coalescing. Packets arrive as a Poisson process at `lam` = {lam} per µs (exponential gaps, `rng = random.Random(1)`, 100 000 packets). The NIC raises an interrupt when `B` = {B} packets have accumulated or 20 µs after the first unserviced packet, whichever comes first. Set `irq_rate` (interrupts per µs), `mean_wait` (average µs from a packet's arrival to its interrupt) and `per_packet` (`irq_rate / lam`).

```python starter
irq_rate = 0.01
mean_wait = 0.0
per_packet = irq_rate / lam
```

```python solution
import random
rng = random.Random(1)
t = 0.0
arrivals = []
for _ in range(100000):
    t += rng.expovariate(lam)
    arrivals.append(t)
irqs, waits = 0, 0.0
i, n = 0, len(arrivals)
while i < n:
    first = arrivals[i]
    deadline = first + 20.0
    j = i
    while j < n and j - i < B and arrivals[j] <= deadline:
        j += 1
    if j - i == B:
        fire = arrivals[j - 1]
    else:
        fire = deadline
    waits += sum(fire - arrivals[k] for k in range(i, j))
    irqs += 1
    i = j
irq_rate = irqs / arrivals[-1]
mean_wait = waits / n
per_packet = irq_rate / lam
```

```python tests
import random
rng = random.Random(1)
t, a = 0.0, []
for _ in range(100000):
    t += rng.expovariate(lam); a.append(t)
irq, w, i = 0, 0.0, 0
while i < len(a):
    d = a[i] + 20.0; j = i
    while j < len(a) and j - i < B and a[j] <= d: j += 1
    f = a[j - 1] if j - i == B else d
    w += sum(f - a[k] for k in range(i, j)); irq += 1; i = j
assert abs(irq_rate - irq / a[-1]) < 1e-12, "irq_rate counts interrupts per µs."
assert abs(mean_wait - w / len(a)) < 1e-9, "mean_wait averages each packet's wait until its interrupt."
assert abs(per_packet - irq_rate / lam) < 1e-12 and per_packet <= 1, "Coalescing should give at most one interrupt per packet."
```

::: right
Right. At high rates batches fill fast, so interrupts per packet approach $1/B$ with little added wait; at low rates the timeout fires first, so latency is bounded but each interrupt serves fewer packets.
:::
:::

::: explain
## A storage server with a fast NVMe drive reaches only a fraction of the drive's rated IOPS when an application issues one read at a time, but full speed when it issues many reads concurrently. Explain why.

::: model
Each read has a fixed latency, tens of microseconds through the drive and the I/O stack, so a single outstanding read limits the rate to one per latency: at 80 µs, 12 500 per second. The drive's internal parallelism across many flash dies and channels can serve far more at once, but only if requests are waiting. By Little's law, sustaining a rate λ at latency L needs λL requests in flight, so reaching a million IOPS at 80 µs needs about 80 outstanding requests. Issuing many reads concurrently, through asynchronous I/O or many threads, fills the drive's queues; the per-request latency hardly changes, but throughput scales with the number in flight until the drive or the link saturates.
:::
:::

::: practice

::: check
given: r = 8 | 32; n = 2 | 8
answer: r*128/130*n/8
unit: GB/s
tolerance: 0.5%

## What is the per-direction bandwidth of PCIe with {n} lanes at {r} GT/s and 128b/130b encoding?

::: misses
- [r*n/8] Include the 128/130 efficiency.
- [any] $r \times 128/130 \times n/8$.
:::
:::

::: check
given: R = 0.25 | 3; L = 50 | 120
answer: R*1e6*L*1e-6
tolerance: 0.5%

## How many requests must be in flight to sustain {R} million per second at {L} µs latency?

::: misses
- [R*1e6/(L*1e-6)] Multiply, don't divide.
- [any] $\lambda L$.
:::
:::
:::
