2 · The Linux platform Draft
Chapter 2 — The Linux platform
On this page
Chapter 1 followed one instruction through the core and one load through the caches as if the program had the machine to itself. It never does. Your binary runs inside a process, on a core the operating system lends it, in memory the operating system maps for it, fed by packets the operating system received for it, and at any moment the operating system can take the core back, move the thread, or turn out not to have the page it is about to touch. The program’s surroundings are not neutral. They are a second machine, built in software on top of the first, with mechanisms and latencies of its own, and a hot loop that does not understand them is measuring them without knowing it.
This chapter is about that second machine: what Linux does for a process, why it does it that way, and what each service costs at the microsecond scale. The costs are easy to state. A trivial system call is 100–300 ns, a quarter of the 1 µs budget from Chapter 1. A page touched for the first time costs about a microsecond, the whole budget. The timer interrupt that lands on every busy core 250 or 1,000 times a second costs 1–5 µs each time. A context switch costs not the 1–2 µs of saving registers but the one to four milliseconds the other thread then runs, a thousand budgets, and the tens of microseconds of cache misses afterwards. None of these happens often, which is exactly why they are invisible at p50 and are the whole story at p99.9 and max.
The chapter is built the way Chapter 1 was: a map of the platform first, then the ladder of what its services cost, then one mechanism per section, the scheduler, interrupts, virtual memory, the allocator and the idle path, asking of each what it is, why a shared machine needs it, what it costs, and what follows for a thread that wants a core to itself. What follows is collected in one place near the end, after the understanding that justifies it. The labs specify programs to implement under ch02-os/; this checkout does not include companion binaries. Their numbers are predictions for a stated setup, to compare against measurements.