8 lessons

Interrupts, DMA & I/O

How the world outside the CPU gets in: interrupts against polling, DMA moving bytes without the CPU copying them, the path to storage, and why a CPU cache is not the OS page cache.

SourceCompilerInstructionsFront EndExecutionRegistersCachesMemoryI/OBehavior
Interrupts: How Hardware Gets the CPU's Attention

A network card cannot call a function. It raises a line, and the CPU abandons what it was doing at the next instruction boundary. The handler's instruction count is the smallest part of what that costs.

Q · A device runs on its own clock and has no way to call your code — so how does it get the CPU to notice that something happened?
Polling versus Interrupts

Being told costs a fixed amount per event; asking costs a fixed amount per unit of time. Which is cheaper is arithmetic, and above a crossover rate the "wasteful" busy loop wins decisively.

Q · Should the CPU wait to be told an event happened, or keep asking — and what actually decides which one is cheaper?
DMA: Moving Bytes Without the CPU

A disk read does not consume a core, because the CPU never touches the bytes. It writes a descriptor, the device masters the bus and writes straight into RAM, and the CPU finds out afterwards.

Q · When a device delivers a megabyte of data, who actually moves the bytes into memory — and what does the CPU do while that happens?
I/O Architecture: The Interconnect Is a Shared Resource

Storage, network and accelerators do not each get a private path to memory. They share an interconnect with finite bandwidth, and a saturated link explains slowdowns that look like they belong to whichever device you happened to be watching.

Q · How does the CPU actually reach a disk, a network card and a GPU at the same time — and what happens when all three are busy?
Memory-Mapped I/O: When a Store Is Not a Store

Device registers live in the address space, so talking to hardware looks exactly like writing to memory. It is not memory: the write has a side effect, the read may change state, and every optimisation the machine normally applies has to be turned off.

Q · If a device register appears at an address, why can I not just read and write it like any other variable?
PCIe: Lanes, Generations and the Transfer Budget

The link between a CPU and an accelerator is not free capacity. It has a width, a generation and a ceiling — and for many workloads the transfer over it, not the computation at either end, is what sets the runtime.

Q · How much data can actually move between the CPU and a GPU or SSD, and when does that link become the thing that limits the program?
The Storage Path: Why One Small Read Is the Worst Case
▶ lab

An SSD is a parallel device pretending to be a disk. Give it one request at a time and you measure its latency; give it many and you measure its throughput — and those two numbers are not related the way rotational intuition expects.

Q · A read reaches the SSD in microseconds of CPU work and takes far longer to return — where does that time go, and why does issuing more requests not make it worse?
CPU Cache Is Not the Page Cache

Both are called cache, both make things faster, both live in the machine. One is hardware holding lines of physical memory and you cannot address it; the other is ordinary RAM the kernel fills with file data and you can control it precisely.

Q · When someone says "it is in cache", which cache do they mean — and does the answer change what I should do about it?