ContentsThe library

Reading a Profile Honestly

One Profiler Interrupts a Thousand Times a Second and Guesses, the Other Watches Every Call and Changes the Answer

Last timeAsking the Right Question First

The two ways of measuring a program are a statistical sample and a complete count. They have different costs, different resolutions and different distortions, and the question decides which.

How sampling works

A sampling profiler does something simpler than most people expect. At a fixed

rate, commonly a thousand times a second, it interrupts the program, looks at

where it is, writes down the call stack, and lets it continue.

That is the entire mechanism. The output is a count of how many times each

stack was observed. A function that was on the stack for a third of the run

appears in about a third of the samples, and the share of samples converges on

the share of time as the samples accumulate.

FIG 1Four interrupts of the same program
stepsamplewhere it wasrunning total for the slow functionshare so farwhat happened
11inside the slow function1100 per centOne sample and the profile says this function is the entire program. Early samples say nothing; the number only means something once there are many.
2120inside the slow function4840 per centA hundred and twenty samples in, the estimate has steadied around forty per cent. The uncertainty here is still several percentage points, which is the subject of the next lesson.
33000somewhere else104734.9 per centThree thousand samples, three seconds of running at a thousand a second. The estimate is now good to a fraction of a per cent for functions holding a large share.
43000a function seen twice20.07 per centThe same run says almost nothing about a function seen twice. Two samples could as easily have been zero or five, so any statement about this function is noise. Resolution depends on the share, not on the length of the run alone.
4 steps
The same measurement is precise about the large shares and useless about the small ones, in the same profile at the same time. Reading a profile well means knowing which rows of it you are allowed to believe, and that depends entirely on how many samples each row received.

Two properties follow from the mechanism and are worth stating because they

explain most of the behaviour of these tools.

The cost does not depend on the program. A thousand interrupts a second cost

the same whether the program is calling a million functions a second or sitting

in one loop, which is why sampling overhead is typically under two per cent and

does not change as the program changes.

And nothing is recorded between samples. The profiler has no idea how many

times anything was called, how long any individual call took, or that a

function existed at all if it never happened to be running at an interrupt.

Sampling answers where the time went and refuses every other question.

How counting works

The other method writes the measurement into the program. At every function

entry a record is made, at every exit another, and the difference is that

function call.

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. 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 Answeryou are here
  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 Runningopening only
  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