[Networkit] Profiling guide

Daniel Hoske daniel.hoske at student.kit.edu
Fri Jun 20 18:45:40 CEST 2014


I wrote a brief guide for profiling NetworKit with callgrind (enclosed).
I also pushed it to Dev in my fork NetworKit-SDD.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: Profile.pdf
Type: application/x-pdf
Size: 112244 bytes
Desc: not available
URL: <https://lists.ira.uni-karlsruhe.de/mailman/private/networkit/attachments/20140620/93f12201/attachment-0001.bin>
-------------- next part --------------
# Profiling NetworKit with callgrind

You can profile NetworKit using the [callgrind](http://valgrind.org/docs/manual/cl-manual.html) tool of valgrind. It generates a call graph for the program
annotated with call counts and optionally cache misses and branch-prediction

## Step-by-step guide 
- Remove `-pg` from the build options in `SConstruct`:
        profileCppFlags = ["-O2", "-DNDEBUG", "-g"]
        profileCFlags = ["-O2", "-DNDEBUG", "-g"]

- Compile NetworKit for Profiling:

        scons --optimize=Pro --target=Tests

- Profile your tests using `callgrind`:

        valgrind --tool=callgrind ./NetworKit-Tests-Pro --tests --gtest_filter=<filter>

- Analyse the profiling results in `callgrind.out.<pid>` visually:


## Compile options
- It is necessary to compile with debugging information `-g` to get a
  readable profile.
- We recommend compiling with optimizations `-O2` enabled, so inlining 
  is performed and you get realistic call traces. You should also 
  compile with `-DNDEBUG` so debug code such as asserts are disabled.
- Do not compile with `-pg`. Otherwise you will get misleading calls
  to `mcount()`.

## Program control
You can control the profiler in your code with the macros
in the `valgrind/callgrind.h` header:

    #include <valgrind/callgrind.h>

### Profiling parts of the program
You can profile parts of your program by only enabling
profiling in these parts:

    ...code to profile...

In this case you should also start callgrind with instrumentation disabled:
    valgrind --tool=callgrind --instr-atstart=no ./NetworKit-Tests-Pro --tests --gtest_filter=<filter>

Note that this may impact the simulated cache since cache loads outside
of the profiled sections are not taken into account.

### Multiple profiles in a single run
You can create multiple profiles in a single run of NetworKit by
dumping the current stats and resetting them to zero:


This will create profiles ```callgrind.out.<pid>.<id>``` with numerical
ids ```ids```. Although the ```name``` you give will be contained in the
profiles, I have not found a way to add it to the generated file names.

## Cache simulation and branch prediction
You can enable cache simulation with ```--cache-sim=yes``` and
branch-prediction simulation with ```--branch-sim=yes```.

## Restrictions
You should be aware that callgrind only takes the direct caller of a function
into account when collecting stats, i.e. it cannot distinguish
two call stacks ```f1->g->h``` and ```f2->g->h```. Thus, some parts
of the output such as the callee map may not be entirely correct.

See [How profilers lie](http://www.yosefk.com/blog/how-profilers-lie-the-cases-of-gprof-and-kcachegrind.html).

More information about the NetworKit mailing list