Four Seconds of Wall Clock, Half a Second of Work, and Where the Rest Went
Last timeBeing Interrupted
The system keeps an account of your process whether you ask or not. Here is how to read it, how to attribute a slowdown to the processor, a device or a lock, and what the numbers cannot tell you.
The account that always balances
Run anything under a timer that reports three numbers rather than one and the shape of the problem is usually visible immediately.
| step | the line of the account | the number | where the number comes from | what happened |
|---|---|---|---|---|
| 1 | elapsed, on the wall clock | 4.0 s | a stopwatch, and the only one of these anybody has by default | This is the number that was complained about, and on its own it says nothing about which of the lines below it grew. |
| 2 | the processor executing your own code | 0.5 s | charged to the process by the kernel, and measured rather than inferred | The only line a profiler can help with, and here it is an eighth of the elapsed time. |
| 3 | the processor in kernel code on your behalf | 0.1 s | charged to the process as well, because the work was done because you asked | Still your time. A program whose charged time is mostly this line is usually making far too many requests rather than meeting slow kernel code. |
| 4 | charged time altogether | 0.5 + 0.1 = 0.6 s, which is 15 per cent of the elapsed time | addition, and the only arithmetic needed to halve the problem | Had this come to 3.8 s instead, the investigation would have been over and a profiler would have been the right next instrument. |
| 5 | not running at all | 4.0 - 0.6 = 3.4 s, call it three and a half | subtraction, because nothing on the machine reports this line | Where the complaint actually lives, and no amount of reading your own code will explain it, because your code was not executing for seven eighths of the time. |
Two refinements matter. On a machine with several processors, the first two terms can exceed the elapsed time, because they are summed across threads: four seconds elapsed with twelve seconds of charged time means three processors were busy throughout. And the second term is charged to you for work the kernel did because you asked, which includes the crossings from the second lesson, so an unexpectedly large second term is very often a request-count problem.
What the system will tell you
Four instruments, each answering one question well.
The per-process counters give the split above, plus the page fault counts and, crucially, two switch counts: voluntary, meaning the process gave up the processor because it blocked, and involuntary, meaning it was preempted. That pair is the most under-used diagnostic on the machine.
A call trace shows each request the process made, in order, with how long each took. It answers which device, which file, which address, and it is the fastest way to find a program making a hundred thousand tiny requests where it could have made ten.
System-wide utilisation says whether the machine is short of anything, which is a different question from whether your process is slow, and confusing the two is the commonest mistake in this area.
And a sampling profiler says where in your own code the charged time went, which matters only if the first term above was large. A profiler pointed at a program that is blocked tells you nothing, beautifully.
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 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