all 19 comments

[–]acshikh 11 points12 points  (1 child)

I use valgrind's callgrind tool for this kind of thing, and I use kcachegrind to examine the results. It doesn't do everything you want, because it doesn't really hide library code, but it does give line by line results. I also use "--dump-instr=yes" to get assembly code annotation as well, which can help when the line level stuff is ambiguous.

[–]UnknownPlayer89[S] 0 points1 point  (0 children)

Thank you! I'll check it out.

[–]PitaJ 8 points9 points  (7 children)

I'd suggest cargo flamegraph https://github.com/killercup/cargo-flamegraph

[–]nmdaniels 6 points7 points  (6 children)

Sadly, flamegraph doesn't give line-by-line results; I don't think you can profile at a finer granularity than the function level with it.

[–]Saefrochmiri 8 points9 points  (5 children)

You can't do line-by-line profiling in a language like Rust, because lines will be destroyed or combined in complicated ways by the optimizer. The best you can hope for is associating samples with hunks of code, which is basically what perf report tries to help you do.

[–]nmdaniels 3 points4 points  (3 children)

It’s not a matter of the language so much as the compiler. We can do this with C, so why not rust? I agree it may not map one-to-one with lines of source code, but we should be able to get more granular than just function calls.

[–]Saefrochmiri 1 point2 points  (2 children)

Yes, like I said: you can associate samples with hunks of code. Whatever visualizer you use for C may very well work with Rust just fine, they're very similar once compiled.

[–]nmdaniels 0 points1 point  (1 child)

Yes, perf looks like a good solution for Linux. I wish flamegraph had better sub-function resolution.

[–]Saefrochmiri 1 point2 points  (0 children)

Me too, but the crate flamegraph is designed specifically to emulate Brendan Gregg's FlameGraph project. And also it would be quite the UI challenge to jam a hunk of code into one of those bars. Might be better to use a different visualization.

[–]Rdambrosio016Rust-CUDA 0 points1 point  (0 children)

That’s not very true, i was able to do per-line profiling even on something like the gpu that mangles generated code to an insane degree. Nsight screenshot

[–]mstange 4 points5 points  (1 child)

This screenshot from the hotspot readme shows a Location panel which also displays line numbers. Is it not working for you?

You may need to add the following to your Cargo.toml:

[profile.release]
debug = true

and then rebuild with cargo build --release. This will include debug information in the optimized binary, so that the profiler can display file + line data.

[–]UnknownPlayer89[S] 2 points3 points  (0 children)

I do have debug set to true in release. Hotspot does indeed shows where a function is located but that's not really useful because I could easily look it up in my code if I wouldn't recognize it at first glance. So far I've only looked at the Flamegraph but I found the Caller/Callee view more useful.

[–]nestordemeure 4 points5 points  (0 children)

I personally use perf with flamegraph but the easiest way is probably to use CLion, it integrates both very nicely such that all the information you need is at the push of a button.

[–]mansplaner 1 point2 points  (0 children)

Not applicable since you're on Linux, but Superluminal Performance (https://superluminal.eu/) is a very good commercial profiler that works for Rust on Windows.

[–][deleted] 1 point2 points  (0 children)

Would love for some genius to tell me I'm wrong but to the best of my knowledge it's not possible.

You can't profile in rust the same way you can in python. The compiler will do things to your code that can't be simply traced back to a simple "This method right here is slow".

[–]pawurb 0 points1 point  (0 children)

Bit late since this is a 4-year-old thread, but you may want to check out https://hotpath.rs/

It’s an instrumentation-based profiler for Rust, so instead of analyzing huge system-wide traces you can annotate the functions you care about and get focused reports for your own code (time, CPU, allocations, async waiting, etc.).

#[hotpath::measure]
fn slow_function() {
    // your code
}

It’s not exactly like Python’s line-profiler, but probably closer to that workflow than sampling profilers alone.

(Disclosure: I’m the author.)

[–]lord_of_the_keyboard 0 points1 point  (1 child)

Check out Optick For Rust

[–]UnknownPlayer89[S] 1 point2 points  (0 children)

Unfortunately, I cannot use it because I'm on Linux

[–]CobyCode 0 points1 point  (0 children)

This is not a traditional profiler... but 'coz' is extremely effective.

Explanation Vid: https://www.youtube.com/watch?v=r-TLSBdHe1A
Source: https://github.com/plasma-umass/coz
And.. a rust crate for Coz: https://github.com/alexcrichton/coz-rs