Rust support in Linux `perf` tool

As my first contribution to Linux I added support for showing readable Rust symbols in Linux's perf performance counter profiling tool. If you are doing performance-related work in Rust on Linux you want this patch.

http://git.kernel.org/cgit/linux/kernel/git/tip/tip.git/commit/?id=cae15db74999edb96dd9f5bbd4d55849391dd92b

For some examples of what perf is useful for, here is perf showing relative amount of time spent in individual functions:

And at the machine instruction level, inline with the source code:

Here are the steps for cloning and building perf with my patch. Note that the build requires about 500 MB of disk space.

git init perf-core
cd perf-core/
git remote add tip git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
git config core.sparsecheckout true
cat > .git/info/sparse-checkout << SPARSE
include/*
tools/arch/*
tools/build/*
tools/include/*
tools/lib/*
tools/perf/*
tools/scripts/*
SPARSE
git pull --depth=1 tip perf/core
cd tools/perf/
CC=gcc make

The make command will tell you which development headers you need to install. Just keep trying until you get it right. I had to install binutils-dev, libaudit-dev, libdw-dev, libelf-dev, libgtk2.0-dev, libiberty-dev, liblzma-dev, libnuma-dev, libperl-dev, libslang2-dev, libunwind8-dev. There may have been others that I already had installed.

38 Likes

Thank you for doing that! I think my brain had just gotten used to reading the mangled form too. :slight_smile:

4 Likes

Awesome!

We just recently tweaked the stdlib's demangler to add a couple more cases that it looks like the kernel patch missed: https://github.com/rust-lang/rust/pull/34753. Might be worth pushing upstream?

2 Likes

Nice, hopefully it gets integrated by cargo-profiler soon.

Callgrind and cachegrind have been usable for a while now.

1 Like

This is so awesome!

In response to some reddit comments:

@pcwalton

This is awesome, but I'm not thrilled about the de facto stabilization of our symbol mangling, which was mostly hacked in to prevent LLVM from complaining.

@michaelwoerister

I'm not sure I like this... In principle, it's great that perf gets Rust support. But our current symbol mangling scheme is historically grown without much of a long-term plan behind it, afaik.

The patch as written will not do The Wrong Thing. In the worst case it will do nothing - showing you what you would have seen without the patch. I guess consider it a preview of how nice it will be when things are stabilized.

3 Likes

@dtolnay: Sorry for sounding a bit negative there. It really is great that we have support for the current encoding! Especially since it will still be around for a while.

1 Like

This is available in the new 4.8 release of Linux.

4 Likes

Do you know if it ever was or if there's a a github issue for it?

perf seems to be struggling with some symbols generated by current nightlies

  0.36 │       cmp          %rcx,%r15
       │     → jae          5d0ef <<alloc::vec::Vec<T> as alloc::vec::SpecFrom<T,I>>::from_iter+0x15f>
       │     _ZN78_$LT$alloc..vec..Vec$LT$T$GT$$u20$as$u20$alloc..vec..SpecFrom$LT$T$C$I$GT$$GT$9from_iter17ha1d74c2b1d204911E(): 
  0.30 │       mov          %r14,%rdx

Hi, are you using a text ui for exploring perf report?

1 Like

Doh, figured it out..