ContentsThe library

Reading a Profile Honestly

The Sample Landed Three Instructions After the One That Was Slow, and the Function It Blames Does Not Exist Any More

Last timeThe Time It Was Not Running

Even a correctly taken profile of the right thing puts time on the wrong line in four specific ways. All four are recognisable, and knowing them changes what you trust.

The sample lands late

The first distortion is in the mechanism itself. When a sampling interrupt

fires, the processor does not stop instantly. It finishes what is in flight,

takes the interrupt, and the handler reads the instruction pointer. By then the

program has moved on.

FIG 1Where the sample thinks the program was
stepmomentwhat the processor was doingattributed toerrorwhat happened
11a load that misses all the cachesnothing yetnoneThis is the expensive instruction. It will stall for a few hundred cycles. It is the thing a reader of the profile wants to find.
22stalled, waiting for memorynothing yetnoneHundreds of cycles pass here. If the interrupt fires now, the recorded pointer is somewhere after the load, because the hardware reports the next instruction not yet retired rather than the one stalling.
33the interrupt is taken and handledthe instruction after the loada few instructionsOn straightforward hardware this is a small error and the sample lands within the same statement. The line attribution is wrong and the function attribution is right.
44an out-of-order processor with many insttens of instructions latercan cross a function boundaryHere the error becomes qualitative. The sample can be attributed past the end of a short function to the caller, or past a branch into code that had nothing to do with the cost.
4 steps
The error is called skid and it is why instruction-level and line-level attribution should be treated as approximate. Modern hardware offers precise sampling that records the responsible instruction in a register at the time, which removes most of this. It is worth finding out whether the tool in use enables it, because the default often does not.

The practical rule is proportional to the distance. At function granularity,

skid is usually harmless. At line granularity it is unreliable unless precise

sampling is on. And for a hot line inside a short function, treat the

attribution as pointing at a neighbourhood rather than at a line.

Functions that are not there

The second distortion is the compiler doing exactly what it should.

The lesson stops here

5 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 Runningopening only
  7. 07The Sample Landed Three Instructions After the One That Was Slow, and the Function It Blames Does Not Exist Any Moreyou are here
  8. 08The New Version Is Eight Per Cent Faster, and So Is the Old One If You Run It Enough Timesopening only

Read alongside