Sub topic 1
Call stacks: the raw material of a profile
Prerequisites
This chapter assumes you can build and run a Rust program with Cargo, and that you are comfortable in a terminal. You should know what a function call is. You do not need any profiling experience. Later chapters cover sampling and flamegraphs. This one covers the data they are built from.
What a call stack is
A call stack is the list of functions that are running at one moment, each one waiting for the function above it to return. When main calls count_primes, and count_primes calls is_prime, the stack has three entries. The most recent call is at the top.
Each entry is a stack frame. A frame holds the function's local variables, its arguments, and the address where execution resumes when the function returns. The CPU keeps the address of the current frame in a register, so a tool can find the frame for the running function. It can then follow links from frame to frame down to main.
A stack trace is a copy of this list taken at one instant. It is usually written with the outermost caller first and the currently running function last:
main
count_primes
is_primeProfilers record thousands of these traces. A single trace tells you only where the program was at one instant. Many traces together show where it spends its time.
Seeing a real stack trace
The program below prints its own stack at a chosen point, so you can see what a profiler would see. It uses only the standard library.
use std::backtrace::Backtrace;
fn is_prime(n: u64) -> bool {
if n == 97 {
println!("{}", Backtrace::force_capture());
}
n >= 2 && (2..n).all(|d| n % d != 0)
}
fn count_primes(limit: u64) -> usize {
(0..limit).filter(|&n| is_prime(n)).count()
}
fn main() {
println!("{}", count_primes(100));
}Run it with cargo run. The trace lists is_prime first, then the closure and iterator code that called it, then count_primes, then main, and finally runtime startup frames. Backtraces print the innermost frame first, which is the reverse of the order used above.
You will see frames from core::iter that you did not write. Iterator adapters such as filter and all are ordinary functions, so they appear as frames too. Profiles show them as well.
Why stacks matter for profiling
A sample that records only "the program is in is_prime" is of limited use. The full stack records why it is there. Suppose is_prime is called from two places. Only the complete stack tells you which caller is responsible for the time. This is what lets a flamegraph attribute cost to callers as well as to the function doing the work.
The diagram below shows the stack at the moment of one sample.
How a tool reads the stack
A profiler cannot ask the program for a backtrace. It has to walk the stack itself, from outside, by reading memory. There are two common methods.
| Method | How it finds the caller | Cost and trade-off |
|---|---|---|
| Frame pointers | Each frame stores the address of the previous frame, forming a linked list | Cheap and fast. Fails if the compiler omits frame pointers |
| DWARF unwinding | Uses debug tables that describe how to undo each function's stack changes | Works without frame pointers. Slower and produces larger data |
Rust release builds usually omit frame pointers to free a register. A later chapter shows how to turn them on or use DWARF. For now, remember that a broken stack walk gives short or wrong stacks, and a profile built from them misleads you.
Two things to keep in mind
- Inlining removes frames. If the compiler copies a small function into its caller, no separate frame exists at run time. The function may vanish from the stack.
- Stack depth varies. Deep recursion creates very tall stacks, and iterator-heavy code adds frames you did not write.
Exercise
A profiler records the stack main -> countprimes -> isprime. Why is this more useful than recording only is_prime?
Show answer
The full stack shows which caller led to the work. If is_prime is called from several places, the stack lets the profiler assign the time to the specific call path, not just to the function.
Exercise
Why can a small function you wrote be missing from a stack trace in a release build?
Show answer
The compiler may inline it into its caller. The code runs, but it has no frame of its own, so it does not appear as a separate entry.