Author: thothonegan
Tags: valgrind callgrind c++

So continuing about tracing, lets talk about my favorite profiler. Callgrind!

Most people if they know valgrind is for its memory checking capabilities. Valgrind howver though is more a platform for those types of tools. The memory checking capabilities is part of 'memcheck', a heap profiler called 'massif', a thread debugger called 'helgrind', and lots of others. The big one we're going to talk about today is 'callgrind', which is part of 'cachegrind'.

Cachegrind is a cache profiler - it simulates the CPU caches so it can point out when your program causes cache misses. Callgrind adds to that the ability to profile callgraphs, which allows you to profile a program. So how do you use it?

valgrind --tool=callgrind [program]

Really straightforward. No special compiling for your program, just run it differently and take a 20x speed hit. If you're on linux, you can also use kcachegrind which is an awesome visualizer. So with the same example as last time, here's WolfExampleConsoleExample!

Its got all the main stuff - a nice list of all the functions sorted by cost, a nice call graph, and annotated source showing how much every line cost.

The only issues with valgrind are:

  • Performance. You take a huge speed hit when running your application, so it might be harder to measure realtime.
  • Correctness. If your program isn't valgrind safe already, you're going to get a ton of errors which will slow it down. Fix your code!

They're minor issues, but its still one of the best profilers i've used.