48
votes

I used to do all my Linux profiling with gprof.

However, with my multi-threaded application, it's output appears to be inconsistent.

Now, I dug this up:

http://sam.zoy.org/writings/programming/gprof.html

However, it's from a long time ago and in my gprof output, it appears my gprof is listing functions used by non-main threads.

So, my questions are:

  1. In 2010, can I easily use gprof to profile multi-threaded Linux C++ applications? (Ubuntu 9.10)
  2. What other tools should I look into for profiling?
11
Preferably something that doesn't slow down as much as valgrind does.anon
Please tell me the alternatives, but not the alternatives. Check.dmckee --- ex-moderator kitten
@stefanB : the slowdown from gprof with g++'s -pg is fine; valgrind's slow down is like 10x atleastanon
@dmckee @stefanB why are you so harsh? It's a perfectly valid observation that valgrind slows down program very much. I for one go with the fastest profiler too, given the choice.Laurynas Biveinis
Valgrind has a lot of brand loyalty, in spite of being 10x slow. The problem is that it doesn't slow down I/O by a proportionate factor, so if normally there is 30% time spent in unnecessary I/O, under Valgrind it will only look like 3%.Mike Dunlavey

11 Answers

14
votes

Edit: added another answer on poor man's profiler, which IMHO is better for multithreaded apps.

Have a look at oprofile. The profiling overhead of this tool is negligible and it supports multithreaded applications---as long as you don't want to profile mutex contention (which is a very important part of profiling multithreaded applications)

7
votes

Have a look at poor man's profiler. Surprisingly there are few other tools that for multithreaded applications do both CPU profiling and mutex contention profiling, and PMP does both, while not even requiring to install anything (as long as you have gdb).

6
votes
6
votes

Have a look at Zoom.

6
votes

A Paul R said, have a look at Zoom. You can also use lsstack, which is a low-tech approach but surprisingly effective, compared to gprof.

Added: Since you clarified that you are running OpenGL at 33ms, my prior recommendation stands. In addition, what I personally have done in situations like that is both effective and non-intuitive. Just get it running with a typical or problematic workload, and just stop it, manually, in its tracks, and see what it's doing and why. Do this several times. Now, if it only occasionally misbehaves, you would like to stop it only while it's misbehaving. That's not easy, but I've used an alarm-clock interrupt set for just the right delay. For example, if one frame out of 100 takes more than 33ms, at the start of a frame, set the timer for 35ms, and at the end of a frame, turn it off. That way, it will interrupt only when the code is taking too long, and it will show you why. Of course, one sample might miss the guilty code, but 20 samples won't miss it.

6
votes

Try modern linux profiling tool, the perf (perf_events): https://perf.wiki.kernel.org/index.php/Tutorial and http://www.brendangregg.com/perf.html:

perf record ./application
# generates profile file perf.data
perf report
0
votes

You can randomly run pstack to find out the stack at a given point. E.g. 10 or 20 times. The most typical stack is where the application spends most of the time (according to experience, we can assume a Pareto distribution).

You can combine that knowledge with strace or truss (Solaris) to trace system calls, and pmap for the memory print.

If the application runs on a dedicated system, you have also sar to measure cpu, memory, i/o, etc. to profile the overall system.

0
votes

Since you didn't mention non-commercial, may I suggest Intel's VTune. It's not free but the level of detail is very impressive (and the overhead is negligible).

0
votes

Putting a slightly different twist on matters, you can actually get a pretty good idea as to what's going on in a multithreaded application using ftrace and kernelshark. Collecting the right trace and pressing the right buttons and you can see the scheduling of individual threads.

Depending on your distro's kernel you may have to build a kernel with the right configuration (but I think that a lot of them have it built in these days).

0
votes

Microprofile is another possible answer to this. It requires hand-instrumentation of the code, but it seems like it handles multi-threaded code pretty well. And it also has special hooks for profiling graphics pipelines, including what's going on inside the card itself.

0
votes

I tried valgrind and gprof. It is a crying shame that none of them work well with multi-threaded applications. Later, I found Intel VTune Amplifier. The good thing is, it handles multi-threading well, works with most of the major languages, works on Windows and Linux, and has many great profiling features. Moreover, the application itself is free. However, it only works with Intel processors.