• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.reuse/H18-Dec-2021-2519

3rdparty/H03-May-2022-34,46023,397

LICENSES/H03-May-2022-

cmake/H03-May-2022-10387

po/H18-Dec-2021-29,76425,049

screenshots/H03-May-2022-54

src/H03-May-2022-13,75611,073

tests/H03-May-2022-2,3281,676

tools/H18-Dec-2021-212134

.arcconfigH A D18-Dec-202176 43

.clang-formatH A D18-Dec-20211.3 KiB4846

.dockerignoreH A D18-Dec-202135 65

.gitignoreH A D18-Dec-2021287 3330

.kateconfigH A D18-Dec-202161 21

.travis.ymlH A D18-Dec-2021367 1814

README.mdH A D18-Dec-20219.7 KiB242161

format_sourcesH A D18-Dec-2021444 86

heaptrack.kdev4H A D18-Dec-202150 43

README.md

1# heaptrack - a heap memory profiler for Linux
2
3![heaptrack_gui summary page](screenshots/gui_summary.png?raw=true "heaptrack_gui summary page")
4
5Heaptrack traces all memory allocations and annotates these events with stack traces.
6Dedicated analysis tools then allow you to interpret the heap memory profile to:
7
8- find hotspots that need to be optimized to reduce the **memory footprint** of your application
9- find **memory leaks**, i.e. locations that allocate memory which is never deallocated
10- find **allocation hotspots**, i.e. code locations that trigger a lot of memory allocation calls
11- find **temporary allocations**, which are allocations that are directly followed by their deallocation
12
13## Using heaptrack
14
15The recommended way is to launch your application and start tracing from the beginning:
16
17    heaptrack <your application and its parameters>
18
19    heaptrack output will be written to "/tmp/heaptrack.APP.PID.gz"
20    starting application, this might take some time...
21
22    ...
23
24    heaptrack stats:
25        allocations:            65
26        leaked allocations:     60
27        temporary allocations:  1
28
29    Heaptrack finished! Now run the following to investigate the data:
30
31        heaptrack_gui "/tmp/heaptrack.APP.PID.gz"
32
33Alternatively, you can attach to an already running process:
34
35    heaptrack --pid $(pidof <your application>)
36
37    heaptrack output will be written to "/tmp/heaptrack.APP.PID.gz"
38    injecting heaptrack into application via GDB, this might take some time...
39    injection finished
40
41    ...
42
43    Heaptrack finished! Now run the following to investigate the data:
44
45        heaptrack_gui "/tmp/heaptrack.APP.PID.gz"
46
47## Building heaptrack
48
49Heaptrack is split into two parts: The data collector, i.e. `heaptrack` itself, and the
50analyzer GUI called `heaptrack_gui`. The following summarizes the dependencies for these
51two parts as they can be build independently. You will find corresponding development
52packages on all major distributions for these dependencies.
53
54On an embedded device or older Linux distribution, you will only want to build `heaptrack`.
55The data can then be analyzed on a different machine with a more modern Linux distribution
56that has access to the required GUI dependencies.
57
58If you need help with building, deploying or using heaptrack, you can contact KDAB for
59commercial support: https://www.kdab.com/software-services/workshops/profiling-workshops/
60
61### Shared dependencies
62
63Both parts require the following tools and libraries:
64
65- cmake 2.8.9 or higher
66- a C\+\+11 enabled compiler like g\+\+ or clang\+\+
67- zlib
68- optionally: zstd for faster (de)compression
69- libdl
70- pthread
71- libc
72
73### `heaptrack` dependencies
74
75The heaptrack data collector and the simplistic `heaptrack_print` analyzer depend on the
76following libraries:
77
78- boost 1.41 or higher: iostreams, program_options
79- libunwind
80
81For runtime-attaching, you will need `gdb` installed.
82
83### `heaptrack_gui` dependencies
84
85The graphical user interface to interpret and analyze the data collected by heaptrack
86depends on Qt 5 and some KDE libraries:
87
88- extra-cmake-modules
89- Qt 5.2 or higher: Core, Widgets
90- KDE Frameworks 5: CoreAddons, I18n, ItemModels, ThreadWeaver, ConfigWidgets, KIO, IconThemes
91
92When any of these dependencies is missing, `heaptrack_gui` will not be build.
93Optionally, install the following dependencies to get additional features in
94the GUI:
95
96- KDiagram: KChart (for chart visualizations)
97
98### Compiling
99
100Run the following commands to compile heaptrack. Do pay attention to the output
101of the CMake command, as it will tell you about missing dependencies!
102
103    cd heaptrack # i.e. the source folder
104    mkdir build
105    cd build
106    cmake -DCMAKE_BUILD_TYPE=Release .. # look for messages about missing dependencies!
107    make -j$(nproc)
108
109#### Compile `heaptrack_gui` on macOS using homebrew
110
111`heaptrack_print` and `heaptrack_gui` can be built on platforms other than Linux, using the dependencies mentioned above.
112On macOS the dependencies can be installed easily using [homebrew](http://brew.sh) and the [KDE homebrew tap](https://github.com/KDE-mac/homebrew-kde).
113
114    # prepare tap
115    brew tap kde-mac/kde https://invent.kde.org/packaging/homebrew-kde.git
116    "$(brew --repo kde-mac/kde)/tools/do-caveats.sh"
117
118    # install dependencies
119    brew install kde-mac/kde/kf5-kcoreaddons kde-mac/kde/kf5-kitemmodels kde-mac/kde/kf5-kconfigwidgets \
120                 kde-mac/kde/kf5-kio kde-mac/kde/kdiagram \
121                 kde-extra-cmake-modules kde-ki18n kde-threadweaver \
122                 boost zstd gettext
123
124    # run manual steps as printed by brew
125    ln -sfv "$(brew --prefix)/share/kf5" "$HOME/Library/Application Support"
126    ln -sfv "$(brew --prefix)/share/knotifications5" "$HOME/Library/Application Support"
127    ln -sfv "$(brew --prefix)/share/kservices5" "$HOME/Library/Application Support"
128    ln -sfv "$(brew --prefix)/share/kservicetypes5" "$HOME/Library/Application Support"
129
130To compile make sure to use Qt from homebrew and to have gettext in the path:
131
132    cd heaptrack # i.e. the source folder
133    mkdir build
134    cd build
135    CMAKE_PREFIX_PATH=/usr/local/opt/qt PATH=$PATH:/usr/local/opt/gettext/bin cmake ..
136    cmake -DCMAKE_BUILD_TYPE=Release .. # look for messages about missing dependencies!
137    make heaptrack_gui heaptrack_print
138
139## Interpreting the heap profile
140
141Heaptrack generates data files that are impossible to analyze for a human. Instead, you need
142to use either `heaptrack_print` or `heaptrack_gui` to interpret the results.
143
144### heaptrack_gui
145
146![heaptrack_gui flamegraph page](screenshots/gui_flamegraph.png?raw=true "heaptrack_gui flamegraph page")
147
148![heaptrack_gui allocations chart page](screenshots/gui_allocations_chart.png?raw=true "heaptrack_gui allocations chart page")
149
150The highly recommended way to analyze a heap profile is by using the `heaptrack_gui` tool.
151It depends on Qt 5 and KF 5 to graphically visualize the recorded data. It features:
152
153- a summary page of the data
154- bottom-up and top-down tree views of the code locations that allocated memory with
155  their aggregated cost and stack traces
156- flame graph visualization
157- graphs of allocation costs over time
158
159### heaptrack_print
160
161The `heaptrack_print` tool is a command line application with minimal dependencies. It takes
162the heap profile, analyzes it, and prints the results in ASCII format to the command line.
163
164In its most simple form, you can use it like this:
165
166    heaptrack_print heaptrack.APP.PID.gz | less
167
168By default, the report will contain three sections:
169
170    MOST CALLS TO ALLOCATION FUNCTIONS
171    PEAK MEMORY CONSUMERS
172    MOST TEMPORARY ALLOCATIONS
173
174Each section then lists the top ten hotspots, i.e. code locations that triggered e.g.
175the most memory allocations.
176
177Have a look at `heaptrack_print --help` for changing the output format and other options.
178
179Note that you can use this tool to convert a heaptrack data file to the Massif data format.
180You can generate a collapsed stack report for consumption by `flamegraph.pl`.
181
182## Comparison to Valgrind's massif
183
184The idea to build heaptrack was born out of the pain in working with Valgrind's massif.
185Valgrind comes with a huge overhead in both memory and time, which sometimes prevent you
186from running it on larger real-world applications. Most of what Valgrind does is not
187needed for a simple heap profiler.
188
189### Advantages of heaptrack over massif
190
191- *speed and memory overhead*
192
193  Multi-threaded applications are not serialized when you trace them with heaptrack and
194  even for single-threaded applications the overhead in both time and memory is significantly
195  lower. Most notably, you only pay a price when you allocate memory -- time-intensive CPU
196  calculations are not slowed down at all, contrary to what happens in Valgrind.
197
198- *more data*
199
200  Valgrind's massif aggregates data before writing the report. This step loses a lot of
201  useful information. Most notably, you are not longer able to find out how often memory
202  was allocated, or where temporary allocations are triggered. Heaptrack does not aggregate the
203  data until you interpret it, which allows for more useful insights into your allocation patterns.
204
205### Advantages of massif over heaptrack
206
207- *ability to profile page allocations as heap*
208
209  This allows you to heap-profile applications that use pool allocators that circumvent
210  malloc & friends. Heaptrack can in principle also profile such applications, but it
211  requires code changes to annotate the memory pool implementation.
212
213- *ability to profile stack allocations*
214
215  This is inherently impossible to implement efficiently in heaptrack as far as I know.
216
217## Contributing to heaptrack
218
219As a FOSS project, we welcome contributions of any form. You can help improve the project by:
220
221- submitting bug reports at https://bugs.kde.org/enter_bug.cgi?product=Heaptrack
222- contributing patches via https://invent.kde.org/sdk/heaptrack
223- translating the GUI with the help of https://l10n.kde.org/
224- writing documentation on https://userbase.kde.org/Heaptrack
225
226When submitting bug reports, you can anonymize your data with the `tools/anonymize` script:
227
228    tools/anonymize heaptrack.APP.PID.gz heaptrack.bug_report_data.gz
229
230## Known bugs and limitations
231
232### Issues with old gold linker
233
234Libunwind may produce bogus backtraces when unwinding from code linked with old versions of the gold linker.
235In such cases, recording with heaptrack seems to work and produces data files. But parsing these data files
236with heaptrack_gui will often lead to out-of-memory crashes. Looking at the data with heaptrack_print, one
237will see garbage backtraces that are completely broken.
238
239If you encounter such issues, try to relink your application and also libunwind with `ld.bfd` instead of `ld.gold`.
240You can see if you are affected by running the libunwind unit tests via `make check`. But do note that you
241need to relink your application too, not only libunwind.
242