5.3.16 · D1Build Systems & Toolchain

Foundations — Profiling — gprof, perf, Valgrind - Callgrind

2,128 words10 min readBack to topic

Before you can read a perf report or a gprof table, you need the vocabulary. This page builds every symbol and word the parent note leans on, starting from things a 12-year-old already knows (a clock, a tally sheet, a stopwatch) and ending exactly where the parent note begins.


1. Time, and how we chop it up

Everything in profiling is about time, so we need to be precise about which time.

We write the total run's wall time as . The subscript is just a name tag; it does not mean multiplication. Whenever you see a symbol like later, read it out loud as "T-sub-self" = "the time labelled self".


2. Functions, callers, and the call graph

A function is a named block of code you can invoke. When function main runs some code inside normalize, we say main called normalize.

Draw every function as a box and every "calls" relationship as an arrow, and you get a call graph — a map of who calls whom.


3. Self time vs total time — the symbols and

This distinction trips up almost everyone, so we anchor it to a picture.

The big- symbol () means "add up over" — here, add the total times of every function that calls. Read it as: "total time of f = its own time, plus the sum of the total times of everything it called."


4. Sampling: turning a stopwatch into a tally sheet

We cannot pause a program a billion times to time each line — that would change what we measure. Instead we sample.

Each snapshot is one tally mark for whichever function was caught in the act.


5. Instrumentation vs sampling — the accuracy/overhead trade

There are two ways to gather data, and they pull in opposite directions.


6. Cycles, instructions, IPC and CPI

To read perf, you need what the CPU counts natively.


7. Cache references, misses, and the miss rate


8. Fractions and speedup — the , , and Amdahl symbols

Profiling ends with a decision: is optimizing this worth it? That needs two symbols.


Prerequisite map

Wall-clock time

Self vs total time

Functions and calls

Call graph and counts

Sampling estimate

Instrumentation vs sampling

Cycles and instructions

IPC and CPI

Cache references and misses

Profiling gprof perf Callgrind

Fraction p and speedup s

Amdahl Law

Each foundation box feeds the profiling topic: sampling gives you self time, the call graph gives you counts, IPC and miss rate give you why a function is slow, and Amdahl tells you whether to bother.


Equipment checklist

Cover the right side and test yourself — you are ready when every line is instant.

What does measure?
The whole program's wall-clock time, start to finish, including everything it calls.
Self time vs total time in one sentence?
Self = seconds inside a function's own lines; total = self plus all the time in functions it calls.
Which one is main usually highest in, and why doesn't that mean it's slow?
Highest in total time, because its subtree is the whole program; its own self time is near zero.
What does stand for in sampling?
The sampling interval — the time gap between two consecutive PC snapshots (e.g. s at 100 Hz).
Give the sampling formula for a function's self time.
.
Instrumentation vs sampling — the core trade?
Instrumentation = exact counts but high overhead that distorts timing; sampling = low overhead but statistical estimates.
Define IPC and CPI.
IPC = instructions retired ÷ cycles; CPI = 1/IPC = cycles ÷ instructions retired.
An IPC of 0.5 means what?
The CPU is stalling — waiting most cycles, likely on cache misses or branch mispredictions, not doing real work.
Formula for cache miss rate?
miss rate = cache-misses ÷ cache-references.
In Amdahl's Law, what do and mean?
= fraction of runtime in the optimized part; = how many times faster you make that part.
Best possible speedup when ?
.