ContentsThe library

What Happens While You Wait

Two Hundred Times the Price, for a Line That Looks the Same

Last timeTwo Worlds and a Door

A request across the boundary costs hundreds of times what a function call costs, and the reasons are specific. Here is where the time goes, and how to pay it once instead of a thousand times.

What the crossing actually does

Nothing in the syntax warns you. A function call and a request to the kernel

look alike in every language, and the second one costs between one and three

hundred times the first.

The reason is that a crossing is not a jump. It is a sequence of fixed work,

performed whether the request turns out to be large or trivial.

The register state has to be saved, because the kernel is about to use the same

processor and cannot disturb yours. The stack has to change, because your stack

is in memory you control and the kernel will not run on memory a program can

write under it. The privilege state changes. On most machines since 2018 the

address mappings change too, for reasons the third section covers. The

arguments get validated, every pointer checked against what your process is

allowed to touch. And on the way back, all of it is undone.

That list is the cost, and the important thing about it is that none of the

items depend on what you asked for. Reading one byte and reading a megabyte pay

the same crossing.

FIG 1One request, with the clock running
stepstepwhat happensnanosecondsrunning totalwhat happened
11your code puts a number and arguments in22Ordinary instructions in the weaker state. This part really is as cheap as it looks.
22the trap instruction, state switch, mapp180182The fixed cost. On a machine without the 2018 mitigations this step is nearer sixty.
33the kernel validates the arguments and l60242Every pointer checked against the calling process. Cheap individually, unavoidable, and paid every time.
44the work: reading from a buffer the kern90332The only step that is about your request. If the data were not already in memory this would be a hundred thousand instead of ninety.
55return, undo everything from step two170502The exit is nearly as expensive as the entry, because it is the same work in reverse.
5 steps
Five hundred nanoseconds, of which about four hundred and ten is the crossing and sixty is checking. The step that does what you asked for accounts for under a fifth of the time, and that is the good case where no device is involved. Read the column of nanoseconds rather than the running total if you want the point: the fixed costs dominate, and they are the same for every request you ever make.

The ladder of costs

Performance reasoning without these numbers is guessing, and most people are

out by at least two orders of magnitude somewhere on this list.

FIG 2How long things take, in nanoseconds
nanosecondssame number, so the scal
one add11
a function call22
a miss to main memory2020
a request to the kernel500500
a minor page fault30003000
switching to another pro1600016000
reading from a solid-sta100000100000
reading from a spinning 1000000010000000
Eight rows spanning seven orders of magnitude, which is why intuition fails here: the gap between the top and the bottom of this table is the gap between a second and four months. The two marked rows are the ones that matter most in practice. A kernel request is the cost you pay constantly and barely notice, and a solid-state read is two hundred times that, which is why the middle of this course is about what a program does while it waits.

Two entries deserve comment. The page fault at three thousand nanoseconds is

what it costs when your program touches memory the kernel has not yet given it

a real page for, which happens far more often than people expect and is the

subject of the memory course on this shelf. And the process switch at sixteen

thousand is the number that makes threads look cheap and processes look

expensive, which is only half true and is the subject of a later lesson.

The lesson stops here

3 more paragraphs to go

You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.

The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.

See the planThe contents

This is the reading half

Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.

The contents

The rest of this course

  1. 01Your Program Cannot Read a File, and Never Could
  2. 02Two Hundred Times the Price, for a Line That Looks the Sameyou are here
  3. 03Your Program Is the Small Part of Your Processopening only
  4. 04The Program That Waits Gets Served First, and It Is Not Being Rewardedopening only
  5. 05Not Running Is Two Different Problems With One Symptomopening only
  6. 06Ten Thousand Connections, One Thread, and One Thing It Still Cannot Doopening only
  7. 07Your Handler Runs Between Two Instructions You Did Not Chooseopening only
  8. 08Four Seconds of Wall Clock, Half a Second of Work, and Where the Rest Wentopening only

Read alongside