Basis Lab

What Happens While You Wait

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.

FIG 1The account for one four-second program, and the line that is left over
stepthe line of the accountthe numberwhere the number comes fromwhat happened
1elapsed, on the wall clock4.0 sa stopwatch, and the only one of these anybody has by defaultThis is the number that was complained about, and on its own it says nothing about which of the lines below it grew.
2the processor executing your own code0.5 scharged to the process by the kernel, and measured rather than inferredThe only line a profiler can help with, and here it is an eighth of the elapsed time.
3the processor in kernel code on your behalf0.1 scharged to the process as well, because the work was done because you askedStill your time. A program whose charged time is mostly this line is usually making far too many requests rather than meeting slow kernel code.
4charged time altogether0.5 + 0.1 = 0.6 s, which is 15 per cent of the elapsed timeaddition, and the only arithmetic needed to halve the problemHad this come to 3.8 s instead, the investigation would have been over and a profiler would have been the right next instrument.
5not running at all4.0 - 0.6 = 3.4 s, call it three and a halfsubtraction, because nothing on the machine reports this lineWhere 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.
5 steps
Three measured lines and a fourth got by subtraction, which is the most useful arithmetic in this course. The account balances by construction, so the question is only ever which line is large: the second sends you to a profiler, the third to a count of requests, and the last to the rest of this lesson, since the machine will not tell you directly why a process was not running. Note that a program spending 0.5 s of its own time inside 4.0 s has had its slowness described by no instrument yet.

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 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 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 Wentyou are here

Read alongside