Skip to content
BIASED

Input and output

Controllers and registers, port-mapped vs MMIO, polling vs interrupts, DMA, livelock/NAPI, buses and device latency orders.

Devices and controllers

A controller shows the CPU status, command and data registers; a driver is kernel code that knows them.

Port-mapped I/O (x86 in/out): a separate address space. Memory-mapped I/O (RISC-V, Arm, RARS at 0xffff0000): ordinary loads/stores to reserved physical addresses. MMIO must be uncached and ordered (fence w,o before a “go” write on RISC-V).

Worked example. In the model above, add 2000 cycles to set up each DMA transfer, plus the 1000-cycle interrupt at its end. A 4 KiB block costs 3000 cycles instead of 1024×100=102,4001024 × 100 = 102{,}4001024×100=102,400 for polling and 1024×1000=1,024,0001024 × 1000 = 1{,}024{,}0001024×1000=1,024,000 for interrupts. The 200 MB/s disk needs 200⋅106/4096=48,828200 · 10^6 / 4096 = 48{,}828200⋅106/4096=48,828 blocks a second, 1.46⋅1081.46 · 10^81.46⋅108 cycles: 7.3 % of the processor. With 64 KiB transfers it drops to 0.46 %. In this model DMA with 4 KiB blocks would use the whole processor only at 2.73 GB/s, which is why fast devices use larger transfers, descriptor rings and fewer interrupts.

Worked example. A keyboard: 10 key presses a second, one 4-byte transfer each. Polling costs 100×1000/(2⋅109)=5⋅10−5100 × 1000 / (2 · 10^9) = 5 · 10^{-5}100×1000/(2⋅109)=5⋅10−5 of the processor, 0.005 %; interrupts cost 1000×10/(2⋅109)=5⋅10−61000 × 10 / (2 · 10^9) = 5 · 10^{-6}1000×10/(2⋅109)=5⋅10−6, ten times less. Both are negligible, but polling would also add up to a millisecond of delay, and on a laptop the wake-ups of a polling loop keep the processor out of its low-power states. A disk delivering 200 MB/s: that is 5⋅1075 · 10^75⋅107 transfers a second. Polling once per transfer needs 5⋅1095 · 10^95⋅109 cycles a second, 2.5 processors; one interrupt per transfer needs 25. Neither works. The two costs are equal where 100×1000=1000×r/4100 × 1000 = 1000 × r/4100×1000=1000×r/4, at r=400 B/s: below that interrupts are cheaper, above it polling is, and both need a whole processor long before disk speeds, polling at 80 MB/s and interrupts at 8 MB/s.

Polling or interrupts

Polling pays per look (~100 cycles in the model); interrupts pay per event (~1000). Polling wins when data is almost always ready; interrupts when it is rare — they cross near 400 B/s in the note’s model.

DMA moves a block with one setup and one completion IRQ: 3000 cycles for 4 KiB vs ~102k for per-word IRQs or ~1M for per-word polling.

Worked example. A 7200 rpm disk turns once in 60/720060/720060/7200 s =8.33= 8.33=8.33 ms, so on average the wanted sector is half a turn away: 4.17 ms of rotational latency. A random read of 4 KiB from a disk with a 9 ms average seek and a 105 MB/s transfer rate takes 9+4.17+4096/(105⋅106)9 + 4.17 + 4096 / (105 · 10^6)9+4.17+4096/(105⋅106) s ≈13.2≈ 13.2≈13.2 ms, almost all of it mechanical: the transfer itself takes 0.04 ms. At 2 GHz, 13.2 ms is 26 million cycles. Nothing in this note makes the disk faster; what it does is let the processor run other programs in those 26 million cycles, and that is the job of the operating system.

Interrupt storms and buses

High IRQ rates cause livelock — the kernel only handles interrupts. Fixes: coalescing and switching to polling under load (NAPI). Latencies: DRAM ~100 ns, flash tens of µs, disk seek several ms. PCIe 4.0 ≈ 1.969 GB/s per lane per direction. Controllers and discovery. A UART, disk or NIC exposes status, command and data registers; the driver is kernel code that knows their layout. On QEMU’s virt board a UART might sit at physical 0x10000000; RARS places a keyboard/display pair near 0xffff0000. Walking those addresses with ordinary lw/sw is memory-mapped I/O: the same load/store ISA, but the “memory” is device logic. Those regions must be uncached and ordered — a fence w,o (or equivalent) before a doorbell write so status updates land first. Polling vs interrupts vs DMA. Model costs at 2 GHz: poll ~100 cycles, IRQ overhead ~1000, DMA setup ~2000. For a byte stream, polling wins when the device is almost always ready; interrupts win when ready events are rare — they cross near 400 B/s in the note’s arithmetic. Moving 4 KiB with per-word IRQs costs ~102k cycles; with per-word polling ~1M; with one DMA + completion IRQ about 3000. High IRQ rates cause livelock (NAPI / coalescing switch back toward polling under load). Buses. DRAM ~100 ns, flash tens of µs, disk seeks several ms. PCIe 4.0 offers ≈1.969 GB/s per lane per direction; a ×16 link is tens of GB/s before protocol overhead. Device drivers still spend their lives on descriptors, ring buffers and cache-line-aligned DMA mappings so the NIC and CPU do not false-share control structures. Discovery today is rarely hard-wired: device trees (embedded), ACPI tables (PCs) or virtio over a modern transport tell the OS which MMIO ranges and IRQ lines exist. A driver claims a range, programs DMA descriptors in memory the device can read (often via an IOMMU that translates device addresses), rings a doorbell, and sleeps until the completion IRQ. Forgetting the IOMMU map is a classic “works on the hacked board, faults in production” bug. Character devices vs block devices vs network: the syscall interface differs (read/write vs submit_bio vs sockets), but underneath each ends in a controller, a queue and either polling or IRQs. Understanding that ladder makes strace and driver logs readable when a production latency spike is “mystery I/O wait”. Next: Instruction-level parallelism — several instructions per cycle on one core. Notation. A device is the hardware that does the I/O (a disk, a network adapter, a keyboard); its controller (or adapter) is the electronics that connect it to the computer. Data rates are in bytes per second: 1 MB/s =106= 10^6=106 B/s, 1 GB/s =109= 10^9=109 B/s; sizes of buffers and

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.