Sub topic 2
How sampling profilers work
This chapter explains how a sampling profiler turns a running program into data. It assumes you can build and run Rust programs with Cargo and know what a call stack is: the chain of function calls that led to the current line of code. You do not need any profiling tools installed yet.
The core idea: look often, count what you see
A profiler answers one question: where does the program spend its time? A sampling profiler answers it by interrupting the program at regular intervals and recording what it was doing at that moment.
Each interruption produces one sample. A sample is the call stack of a thread at one instant, for example main → count_primes → is_prime. The profiler does not time individual functions. It collects many samples and counts how often each function appears in them.
If is_prime appears in 900 of 1,000 samples, the program was inside is_prime about 90% of the time. That estimate is good enough to find the code worth optimizing.
One sample, step by step
On Linux, perf is the usual sampling tool. A sample goes through these steps:
- A timer fires at a fixed rate, such as 99 times per second.
- The kernel interrupts the running thread.
- The profiler reads the instruction pointer, which is the address of the code being executed.
- It walks up the stack, following saved return addresses, to find every caller.
- It stores the list of addresses and lets the program continue.
- After the run, it converts addresses to function names, using the symbols in your binary.
Step 6 is why debug symbols matter. Without them you get hex addresses instead of names. A later chapter covers building Rust so this works.
From samples to a flamegraph
Many samples repeat the same stack. The profiler merges them by counting identical stacks. Here is a tiny example:
| Stack | Samples |
|---|---|
| `main;count_primes;is_prime` | 90 |
| `main;count_primes` | 6 |
| `main` | 4 |
This list of stacks with counts is called folded stacks. A flamegraph draws it. Each box is a function, and its width is proportional to the number of samples that include it. The next chapter covers how to read that picture.
Choosing the sampling frequency
The sampling frequency is how many samples are taken per second, measured in hertz (Hz). It is a trade-off:
- A higher rate gives more samples and finer detail.
- A higher rate also adds overhead, because each interrupt costs time, and it can distort the result.
- A lower rate is cheap but needs a longer run to collect enough samples.
The perf record -F 99 command used later samples 99 times per second. Using 99 instead of 100 avoids lockstep with timers that fire at round rates, such as 100 Hz, which could bias the samples. A 10-second run at 99 Hz gives about 990 samples. That is enough to find a function that takes 20% of the time, but too few to trust a function that takes 0.5%.
Common errors and limits
- Too short a run. With only a few dozen samples, the counts are noisy. Run the program long enough to collect hundreds or thousands of samples.
- Reading widths as exact. A sample count is an estimate. Treat a 31% versus 33% difference as a tie.
- Missing names. Addresses without symbols mean the binary lacks debug info.
- Truncated stacks. If the profiler cannot walk the stack correctly, callers go missing. A later chapter explains frame pointers and DWARF.
- Only running code is sampled. A thread waiting on a lock, disk, or network is not using the CPU, so a CPU profile does not show that time.
Exercise
A profile of 10 seconds at 99 Hz contains about 990 samples. A function appears in 297 of them. Roughly how much CPU time did the program spend in that function, and why is the result an estimate?
Show answer
About 30% of the run, or around 3 seconds, because 297 / 990 is 0.30. It is an estimate because the profiler only looks at the stack 99 times per second. It never measures the function directly, so the true share could differ slightly if the samples happened to land unevenly.
Exercise
Why does a sampling profiler not slow a program down as much as adding a timer around every function would?
Show answer
The profiler only interrupts the program a fixed number of times per second, no matter how many function calls happen. Timers around every function add work to each call, which costs more in code that calls small functions millions of times.