S445 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

S4 · Lesson 45 · I/O interconnects, DMA and interrupts

Moving data between devices and memory at tens of gigabytes per second without drowning the CPU

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.

  • 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.

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?

Name it

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 λ with a latency L per transaction, a device needs λL transactions in flight: deep queues are what let high-latency devices such as SSDs reach high bandwidth.

Worked 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).

Show the solution
  1. PCIe 3.0 per lane. B=8×109×128130/8=0.985GB/s per lane, so 7.88 GB/s for eight.
  2. PCIe 2.0 per lane. B=5×109×0.8/8=0.5GB/s 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.

Worked 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?

Show the solution
  1. Packet rate. r=1010/(1500×8)=833000per s
  2. One interrupt per packet. 833000×5µs=4.17cores 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.

Worked 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?

Show the solution
  1. Reads in flight. By Little's law: 106×80×10−6=80 reads outstanding.
  2. Bandwidth. 106×4096B/s=4.1GB/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

A link has 1 lanes at 8 GT/s with an encoding efficiency of 0.9846. What is its bandwidth in each direction?

Check

A NIC receives 10 Gb/s of 64-byte packets; each interrupt costs 5 µs of CPU.

(a)

How many cores do interrupts use at one per packet?

(b)

And with one interrupt per 64 packets?

Check

A device has 20 µs of latency per request. How many requests must be outstanding to sustain 0.5 million per second?

Check

A PCIe transfer carries 256 bytes of payload with 24 bytes of packet overhead. What percentage of the link carries payload?

Check

A NIC at 40 Gb/s coalesces 64 packets of 1500 bytes per interrupt. Up to how much latency does that add to the first packet of a batch?

Write units the way you normally would: 1.5 µs, 1500 ns and 0.0015 ms are the same answer.

Check

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

Check

Simulate interrupt coalescing. Packets arrive as a Poisson process at lam = 0.6 per µs (exponential gaps, rng = random.Random(1), 100 000 packets). The NIC raises an interrupt when B = 8 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 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

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.

Practice

Keep practicing

8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.