flamegraph-rs / flamegraph

Easy flamegraphs for Rust projects and everything else, without Perl or pipes <3
Apache License 2.0
4.61k stars 141 forks source link

command to generate flamegraph from existing perf.data #300

Closed yanns closed 8 months ago

yanns commented 8 months ago

On production, I'm using perf to collect data. I'd like to generate a flamegraph from this data, and I'm wondering if this tool provides a command to generate a flamegraph from the collected perf.data file, avoiding the pain of using the perl version.

djc commented 8 months ago

I think flamegraph --perfdata would do what you need?

yanns commented 8 months ago

Thanks, it's working with cargo flamegraph -- --perfdata perf.data I don't see this in https://github.com/flamegraph-rs/flamegraph#usage Should we add it there?

djc commented 8 months ago

The usage section you're linking to do is mostly for cargo flamegraph, not for flamegraph (which is a separate binary). I think for this scenario, the use of cargo flamegraph doesn't make much sense?

yanns commented 8 months ago

Oh, thanks for the precision. I though that was the same. I'm focusing on flamegraph now.

I got the following error when trying to use it:

flamegraph --perfdata perf.data
Error: failed to open dtrace output file 'cargo-flamegraph.stacks'

Caused by:
    No such file or directory (os error 2)

I've downloaded the perf.data from a running docker container on production.

djc commented 8 months ago

Ah, but you're on macOS or Windows locally? That might need code changes, actually... We switch the backend based on the host OS, but I think that might mean that --perfdata doesn't really work on macOS/Windows with data that's actually coming from perf. Happy to take a PR if you want to dig into what we'd need to change to support this.

yanns commented 8 months ago

I can try, but I'd need some help as I don't know where to start.

djc commented 8 months ago

In https://github.com/flamegraph-rs/flamegraph/blob/main/src/bin/flamegraph.rs#L56, we're passing a Workload::ReadPerf to generate_flamegraph_for_workload(). This calls arch::output() which is the step that on Linux would run perf script. I guess we don't have perf script on macOS so maybe this isn't really feasible?

yanns commented 8 months ago

No we don't have perf script on macOS. Would it be possible that https://github.com/jonhoo/inferno could generate the graph without having to rely on perf?

yanns commented 8 months ago

I see that inferno is also using perf script:

perf script | inferno-collapse-perf > stacks.folded

So it does not help. I guess I will create the flamegraph on the linux server.

yanns commented 8 months ago

https://github.com/KDAB/hotspot is also looking good, but is not available on mac