82

I need to profile some code running C++ on Linux. Can you guys recommend some profilers?

4

9 Answers 9

39

Use gprof.

Just compile with -pg flag (I think (but am not sure) you have to turn of optimizations though.) and use gprof to analyze the gmon.out file that your executable will then produce.

eg:

gcc -pg -o whatever whatever.c

./whatever

gprof whatever gmon.out

Same thing with g++ and cpp.

2
  • 33
    Profiling unoptimized code is a bit pointless, isn't it? Similarly, profiling code that has been heavily modified with -pg often misguides you into optimizing the wrong spots.
    – federal
    Jan 5, 2012 at 15:44
  • 1
    -pg is a linker option, not compiler
    – Slug Pue
    Mar 18, 2018 at 16:19
25

valgrind is a well-know linux profiler

4
  • thought valgrind was more for memory leak checking.. I am trying to see which functions are getting called etc
    – shergill
    Jul 22, 2009 at 23:07
  • 14
    use the suite tool called "callgrind"
    – dfa
    Jul 22, 2009 at 23:08
  • 2
    Valgrind is simply a framework for building dynamic tools. Although, it's become synonymous with Memcheck, a tool built on Valgrind. Callgrind is a pretty good at profiler.
    – Falaina
    Jul 22, 2009 at 23:10
13

I'm a fan of Oprofile. It involves installing a kernel module and has a bit of a learning curve to it, but it's fairly powerful and works very well for optimized programs/programs without debugging symbols.

Vtune is another very powerful profiler made by Intel. I believe the Linux version is free for Non-commercial software.

There is also the Valgrind suite of tools proposed by dfa. Callgrind would probably be what you're most interested in. Cachegrind(whose featureset is a subset of Callgrind's) and Massif are interesting as well, but I have no experience with the latter.

4
  • 1
    +1 for oprofile, that is not an "easy tool"
    – dfa
    Jul 22, 2009 at 23:18
  • 1
    Haha, true. I should probably not make that sound so easy :) It's certainly not as simple as "run program under it" as Vtune and Valgrind tools, but I feel you get used to it pretty quickly.
    – Falaina
    Jul 22, 2009 at 23:20
  • oprofile looks interesting - does it support x86_64?
    – LiraNuna
    Jul 23, 2009 at 0:52
  • VTune isn't free for any uses, unfortunately.
    – rustyx
    Feb 28, 2019 at 20:41
13

Zoom from RotateRight ( http://www.rotateright.com ) is what I've been using. It has a butterfly view of functions and you can double-click any function to dive into source or asm code. Build with debugging information (-g) to see your source, but you should still build and profile optimized code.

2
  • 1
    Just gave this program a shot, it's really quite nice! Currently my favorite profiler on Linux; however it's worth mentioning that it requires code to be built with -fno-omit-frame-pointer to effectively profile.
    – Nik Reiman
    Mar 20, 2015 at 9:05
  • 1
    The link seems dead. Does anyone know where (or if) it can be found elsewhere?
    – Simon F
    Jun 16, 2017 at 12:07
5

Take a look at KCacheGrind which is a graphical frontend to valgrind and makes it really easy to use it.

5

Google also has a nice profiler as part of the google-perftools -- which are included in Debian / Ubuntu and possibly other distros.

2

gprof is the standard gnu tool for profiling.

0
2

Take a look at Sysprof. You distribution most likely has it available already.

Note that all of the mentioned profilers work best if your application is compiled with frame pointers. That is, you should use -fno-omit-frame-pointer on the gcc command line.

0

This is what I use.

3
  • you'll just base your judgement about the bottleneck on 10 samples you collect manually, instead of 1000 samples collected by prof. Jan 5, 2021 at 21:49
  • 1
    @DmitryGrigoryev: Correct, and that actually tells you what you should fix. The statistical explanation is here. In fact, the first mistake people make is to think that they are looking for a "bottleneck" rather than perfectly good but wasteful code ;-) Jan 6, 2021 at 19:41
  • Nice read, thanks. I totally buy your point that optimizing is way easier when you see an actual function call in the debugger. And I do understand that "bottleneck" doesn't mean "good optimization target", just a potential one. Still, I think it makes sense to start with prof anyway: if I see that f() is the most problematic function statistically, I'll stop the program several times until I land in f() instead of just starting with a random function I stopped in first. Jan 6, 2021 at 21:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.