flamegraph icon indicating copy to clipboard operation
flamegraph copied to clipboard

Partially missed stack trace

Open dima74 opened this issue 1 year ago • 5 comments

Consider simple Rust program calculating fibonacci number:

fn main() {
    println!("{}", fibonacci(42));
}

fn fibonacci(n: u32) -> u32 {
    match n {
        0 => 0,
        1 => 1,
        _ => fibonacci(n - 1) + fibonacci(n - 2),
    }
}

Here is result of cargo flamegraph:

image

Approximately half of the fibonacci calls have correct stacktrace, however other half doesn't have stacktrace

dima74 avatar Sep 03 '24 15:09 dima74

This is more due to the underlying profiling backend (perf if you're on Linux), not flamegraph itself. You might be able to increase the profiling frequency to capture a larger percentage of stack traces, at the expense of slower runtime.

djc avatar Sep 04 '24 07:09 djc

I don't observe such behaviour when using perf directly with the same frequence as cargo flamegraph uses by default.

With perf:

cargo build --example fib && perf record -F 997 -g -- ./target/debug/examples/fib && perf script | stackcollapse-perf.pl | flamegraph.pl >flamegraph1.svg

image

With cargo flamegraph:

cargo build --example fib && flamegraph -o flamegraph2.svg -- ./target/debug/examples/fib

image


When using cargo flamegraph, there are 9% [unknown] samples, when using perf directly there are no [unknown] samples

dima74 avatar Sep 04 '24 17:09 dima74

The difference is probably the passing of -g rather than --call-graph dwarf. So maybe the issue is caused by perf's dwarf unwinding.

mstange avatar Sep 04 '24 19:09 mstange

Maybe add info to the README that cargo flamegraph uses --call-graph dwarf and example how to use -g instead

dima74 avatar Sep 13 '24 09:09 dima74

Happy to review a PR.

djc avatar Sep 13 '24 09:09 djc