-
Notifications
You must be signed in to change notification settings - Fork 97
Debugging & Profiling Tips
Tomas Mlcoch edited this page Mar 3, 2014
·
16 revisions
- Valgrind - Holy grail of debugging :)
- Valgrind Massif - Heap profiler
- GooglePerformanceTools (gperftools) - Fast, multi-threaded malloc() and nifty performance analysis tools
For example for gperftools use:
cmake -DCMAKE_BUILD_TYPE:STRING=DEBUG -DCMAKE_C_FLAGS:STRING=" -ltcmalloc " ..
valgrind prog
If the program uses Glib2 library, there can be a lot of false positives. So use the command instead:
G_SLICE=always-malloc G_DEBUG=gc-friendly valgrind prog
Tip: Add this line to your ~/.bashrc: alias gvalgrind='G_SLICE=always-malloc G_DEBUG=gc-friendly valgrind' and use gvalgrind instead of valgrind.
-
--leak-check=full- show details for each definitely lost or possibly lost block, including where it was allocated
A heap profiler
valgrind --tool=massif prog
# And then
ms_print massif.out.12345
HEAPCHECK=normal prog # Heap checker
HEAPPROFILE=/tmp/netheap prog # Heap profiler
CPUPROFILE=/tmp/profile prog # CPU profiler
Note: If it complain that PPROF_PATH is not set and memory addresses are not translated to symbols properly, prepend PPROF_PATH=/usr/bin/pprof (of course with path that match your system - use whereis pprof to obtain the path)
pprof --text prog /tmp/netheap.0001.heap
You can run your program prog and then set watch its memory run-time CPU and memory usage.
top -d 0.2 -p $(ps -A | grep prog | awk '{print $1}')
- VIRT -- Virtual Image (kb) The total amount of virtual memory used by the task. It includes all code, data and shared libraries plus pages that have been swapped out and pages that have been mapped but not used.
- RES -- Resident size (kb) The non-swapped physical memory a task has used.
- SHR -- Shared Mem size (kb) The amount of shared memory used by a task. It simply reflects memory that could be potentially shared with other processes.
Simple resource usage. Its returns three time values:
- real - Elapsed real time between invocation and termination.
- user - User CPU time (the sum of the tms_utime and tms_cutime values in a struct tms as returned by times(2))
- sys - System CPU time (the sum of the tms_stime and tms_cstime values in a struct tms as returned by times(2))
time prog