ContentsThe library

Reading a Profile Honestly

Three and a Half of the Four Seconds Are Invisible, Because the Profiler Only Looks When the Program Is Running

Last timeReading the Shape

A processor profile samples a thread while it executes. A thread that is waiting executes nothing, so the wait leaves no trace and the profile reports a program with no problem.

What a processor profile misses

The mechanism from the second lesson has a consequence that is obvious once

seen and surprises almost everybody the first time.

A sampling profiler interrupts a thread while it is executing and records the

stack. A thread that is blocked, waiting for a disk read or a network reply or

a lock somebody else holds, is not executing. It is not interrupted. It

produces no samples.

So a request that spends half a second computing and three and a half seconds

waiting produces a profile of half a second. The profile is correct, complete

and useless for the question being asked, and it will typically look healthy:

time spread reasonably across a handful of functions, no obvious hot spot.

FIG 1One four second request, two measurements
stepphasewall clock, secondsprocessor sampleswhat the profile showswhat happened
1parse the input0.2200a visible entryReal computation. This appears in the profile at roughly its true size relative to the other computing phases.
2wait for the database2.40nothing at allSixty per cent of the request and zero samples. The thread was blocked the entire time, so the profiler never looked at it. There is no line for this anywhere in the output.
3wait for a lock1.10nothing at allAnother twenty seven per cent, equally invisible. Worse, the holder of the lock is a different thread doing real work, so its samples make the program look busy rather than stuck.
4format the reply0.3300the largest entryThe profile reports this as the biggest cost in the request, and relative to the other sampled work it is. It is seven per cent of what the user waited for.
4 steps
The profile accounts for half a second out of four and ranks reply formatting first. Everything a user experienced as slowness is in the two rows with zero samples. This is the single most common way a performance investigation goes wrong, and the diagnostic is in the first two columns: processor time far below wall clock time.

The states a thread is in

The underlying model is small and worth knowing precisely, because the names

appear in every tool.

FIG 2Where a thread can be
Three states and a profiler that sees one. The two arrows into the blocked box are the two categories of wait worth separating: waiting for something outside the program, which is usually a design question, and waiting for another thread inside it, which is usually a contention question with a different remedy.

The third state deserves a note because it is misread so often. A thread that

is ready but not running is not waiting for anything in your program. It is

waiting for a processor, because the machine has more runnable work than cores.

Adding threads makes it worse, optimising code barely helps, and the symptom is

that everything is slow at once rather than one operation being slow.

The lesson stops here

4 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. 01It Is Slow Is Not a Measurement, and Profiling Before You Have One Wastes the Week
  2. 02One Profiler Interrupts a Thousand Times a Second and Guesses, the Other Watches Every Call and Changes the Answeropening only
  3. 03The Function at Number Seven Received Nine Samples, Which Means You Know Almost Nothing About Itopening only
  4. 04The Function Holding Ninety Per Cent of the Time Does Nothing, and the One Doing the Work Holds Four Per Centopening only
  5. 05The Same Function Is Cheap in Two Places and Ruinous in the Third, and the Flat List Shows One Numberopening only
  6. 06Three and a Half of the Four Seconds Are Invisible, Because the Profiler Only Looks When the Program Is Runningyou are here
  7. 07The Sample Landed Three Instructions After the One That Was Slow, and the Function It Blames Does Not Exist Any Moreopening only
  8. 08The New Version Is Eight Per Cent Faster, and So Is the Old One If You Run It Enough Timesopening only

Read alongside