Basis Lab

What Happens While You Wait

Not Running Is Two Different Problems With One Symptom

Last timeTaking Turns

A program that is not running is either waiting for a turn or waiting for a device, and the fixes are opposites. Here is what each one does to the numbers you can see from outside.

Two reasons, opposite fixes

A program takes four seconds to do something that should take one. From inside the program, the picture is the same in both of the cases below: a line of code took longer than expected.

The two cases are these. The program was ready to run and the processors were busy with other work, so it waited its turn. Or the program was not ready to run at all, because it had asked for something and the answer had not arrived.

These have opposite remedies. The first is a capacity problem: there is more demand than machine, and the answer is less work, better work, or more processors. The second is a latency problem: nothing is short of capacity, the program is waiting on a disk or a network or another process, and adding processors changes nothing whatsoever. Doubling the machine for a program in the second state is the single most common wasted response to a performance problem.

So the diagnosis cannot come from inside. A timer around a slow line reports elapsed time, which includes both, and tells you nothing about which it was.

FIG 1The four seconds that should have been one, split three ways under each cause
stepwhat the elapsed time was spent onthe capacity casethe latency casewhat answers itwhat happened
1running on a processor1 second1 secondnothing: this is the work, and it took exactly as long as it should in bothThe system charges processes for this directly, so it is the one of the three you can read off without inferring anything.
2ready to run and not chosen3 secondsnoneless work, better work, or more processorsNobody measures this by default, and a process merely waiting its turn looks exactly like a process that is slow. This is the most misattributed quantity in the subject.
3blocked, waiting for something that had not happenednone3 secondsa faster disk, a nearer network, a lock held for less timeUsually inferred from what the process was waiting on rather than measured as a duration. Adding processors to this column changes nothing whatsoever.
4what the stopwatch around the slow line reports1 + 3 = 4 seconds1 + 3 = 4 secondsnothing, which is the problemThe two columns are identical here and nowhere else, which is why the diagnosis cannot come from inside the program.
4 steps
Every performance investigation is the question of which of the first three rows grew, and the fourth row is why that question cannot be answered with a timer: both columns add up to the same four seconds from the same one second of work. Doubling the machine fixes the middle row and is the single most common wasted response to the bottom one, and the rest of this lesson is about telling the two apart from outside the program, where the numbers differ.

What blocking is

Blocking is not a state of nature; it is a specific sequence of things the kernel does. Worth having exactly, because the next lesson is about avoiding it.

Your program makes a request. The kernel discovers it cannot be satisfied immediately: the data is not in memory, the socket has nothing in it, the lock is held. So it puts the process on a list of things waiting for that particular event, marks the process not runnable so the scheduler stops considering it, and switches to somebody else. Your process is now not a candidate for the processor at all, and the scheduler from the previous lesson never sees it.

Later, something happens. A disk controller raises an interrupt, a packet arrives, another process releases the lock. The kernel finds the processes waiting on that event, marks them runnable, and they rejoin the comparison from the previous lesson, where, having accumulated almost no charged time, they are chosen almost immediately. Your process resumes execution in the middle of the request it made, and returns from it with an answer. From inside, one line of code took eight milliseconds.

FIG 2The states, and what moves a process between them
Trace the two paths out of running into waiting and notice they differ only in whether signals are delivered. That single difference accounts for the whole experience of a process that cannot be killed: it is not refusing, nobody is delivering the signal, and it will leave the state when the device replies or never. Also notice there is no arrow from a waiting state directly to running, because a woken process has to be chosen like everybody else.

Telling them apart from outside

The system records enough to attribute a slowdown, and the trick is knowing which numbers move for which cause.

The lesson stops here

2 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 Sameopening only
  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 Symptomyou are here
  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