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.
| step | what the elapsed time was spent on | the capacity case | the latency case | what answers it | what happened |
|---|---|---|---|---|---|
| 1 | running on a processor | 1 second | 1 second | nothing: this is the work, and it took exactly as long as it should in both | The system charges processes for this directly, so it is the one of the three you can read off without inferring anything. |
| 2 | ready to run and not chosen | 3 seconds | none | less work, better work, or more processors | Nobody 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. |
| 3 | blocked, waiting for something that had not happened | none | 3 seconds | a faster disk, a nearer network, a lock held for less time | Usually inferred from what the process was waiting on rather than measured as a duration. Adding processors to this column changes nothing whatsoever. |
| 4 | what the stopwatch around the slow line reports | 1 + 3 = 4 seconds | 1 + 3 = 4 seconds | nothing, which is the problem | The two columns are identical here and nowhere else, which is why the diagnosis cannot come from inside the program. |
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.
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 contentsThis 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