Debian Codegraph @

docs/html/ms-manual.html

raw
1 <html>
2 <head>
3 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4 <title>9. Massif: a heap profiler</title>
5 <link rel="stylesheet" type="text/css" href="vg_basic.css">
6 <meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
7 <link rel="home" href="index.html" title="Valgrind Documentation">
8 <link rel="up" href="manual.html" title="Valgrind User Manual">
9 <link rel="prev" href="drd-manual.html" title="8. DRD: a thread error detector">
10 <link rel="next" href="dh-manual.html" title="10. DHAT: a dynamic heap analysis tool">
11 </head>
12 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13 <div><table class="nav" width="100%" cellspacing="3" cellpadding="3" border="0" summary="Navigation header"><tr>
14 <td width="22px" align="center" valign="middle"><a accesskey="p" href="drd-manual.html"><img src="images/prev.png" width="18" height="21" border="0" alt="Prev"></a></td>
15 <td width="25px" align="center" valign="middle"><a accesskey="u" href="manual.html"><img src="images/up.png" width="21" height="18" border="0" alt="Up"></a></td>
16 <td width="31px" align="center" valign="middle"><a accesskey="h" href="index.html"><img src="images/home.png" width="27" height="20" border="0" alt="Up"></a></td>
17 <th align="center" valign="middle">Valgrind User Manual</th>
18 <td width="22px" align="center" valign="middle"><a accesskey="n" href="dh-manual.html"><img src="images/next.png" width="18" height="21" border="0" alt="Next"></a></td>
19 </tr></table></div>
20 <div class="chapter">
21 <div class="titlepage"><div><div><h1 class="title">
22 <a name="ms-manual"></a>9. Massif: a heap profiler</h1></div></div></div>
23 <div class="toc">
24 <p><b>Table of Contents</b></p>
25 <dl class="toc">
26 <dt><span class="sect1"><a href="ms-manual.html#ms-manual.overview">9.1. Overview</a></span></dt>
27 <dt><span class="sect1"><a href="ms-manual.html#ms-manual.using-print">9.2. Using Massif and ms_print</a></span></dt>
28 <dd><dl>
29 <dt><span class="sect2"><a href="ms-manual.html#ms-manual.anexample">9.2.1. An Example Program</a></span></dt>
30 <dt><span class="sect2"><a href="ms-manual.html#ms-manual.running-massif">9.2.2. Running Massif</a></span></dt>
31 <dt><span class="sect2"><a href="ms-manual.html#ms-manual.running-ms_print">9.2.3. Running ms_print</a></span></dt>
32 <dt><span class="sect2"><a href="ms-manual.html#ms-manual.theoutputpreamble">9.2.4. The Output Preamble</a></span></dt>
33 <dt><span class="sect2"><a href="ms-manual.html#ms-manual.theoutputgraph">9.2.5. The Output Graph</a></span></dt>
34 <dt><span class="sect2"><a href="ms-manual.html#ms-manual.thesnapshotdetails">9.2.6. The Snapshot Details</a></span></dt>
35 <dt><span class="sect2"><a href="ms-manual.html#ms-manual.forkingprograms">9.2.7. Forking Programs</a></span></dt>
36 <dt><span class="sect2"><a href="ms-manual.html#ms-manual.not-measured">9.2.8. Measuring All Memory in a Process</a></span></dt>
37 <dt><span class="sect2"><a href="ms-manual.html#ms-manual.acting">9.2.9. Acting on Massif's Information</a></span></dt>
38 </dl></dd>
39 <dt><span class="sect1"><a href="ms-manual.html#ms-manual.using-visualizer">9.3. Using massif-visualizer</a></span></dt>
40 <dt><span class="sect1"><a href="ms-manual.html#ms-manual.options">9.4. Massif Command-line Options</a></span></dt>
41 <dt><span class="sect1"><a href="ms-manual.html#ms-manual.monitor-commands">9.5. Massif Monitor Commands</a></span></dt>
42 <dt><span class="sect1"><a href="ms-manual.html#ms-manual.clientreqs">9.6. Massif Client Requests</a></span></dt>
43 <dt><span class="sect1"><a href="ms-manual.html#ms-manual.ms_print-options">9.7. ms_print Command-line Options</a></span></dt>
44 <dt><span class="sect1"><a href="ms-manual.html#ms-manual.fileformat">9.8. Massif's Output File Format</a></span></dt>
45 </dl>
46 </div>
47 <p>To use this tool, you must specify
48 <code class="option">--tool=massif</code> on the Valgrind
49 command line.</p>
50 <div class="sect1">
51 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
52 <a name="ms-manual.overview"></a>9.1. Overview</h2></div></div></div>
53 <p>Massif is a heap profiler. It measures how much heap memory your
54 program uses. This includes both the useful space, and the extra bytes
55 allocated for book-keeping and alignment purposes. It can also
56 measure the size of your program's stack(s), although it does not do so by
57 default.</p>
58 <p>Heap profiling can help you reduce the amount of memory your program
59 uses. On modern machines with virtual memory, this provides the following
60 benefits:</p>
61 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
62 <li class="listitem"><p>It can speed up your program -- a smaller
63 program will interact better with your machine's caches and
64 avoid paging.</p></li>
65 <li class="listitem"><p>If your program uses lots of memory, it will
66 reduce the chance that it exhausts your machine's swap
67 space.</p></li>
68 </ul></div>
69 <p>Also, there are certain space leaks that aren't detected by
70 traditional leak-checkers, such as Memcheck's. That's because
71 the memory isn't ever actually lost -- a pointer remains to it --
72 but it's not in use. Programs that have leaks like this can
73 unnecessarily increase the amount of memory they are using over
74 time. Massif can help identify these leaks.</p>
75 <p>Importantly, Massif tells you not only how much heap memory your
76 program is using, it also gives very detailed information that indicates
77 which parts of your program are responsible for allocating the heap memory.
78 </p>
79 <p>Massif also provides <a class="xref" href="manual-core.html#manual-core.xtree" title="2.10. Execution Trees">Execution Trees</a> memory
80 profiling using the command line
81 option <code class="computeroutput">--xtree-memory</code> and the monitor command
82 <code class="computeroutput">xtmemory</code>.</p>
83 </div>
84 <div class="sect1">
85 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
86 <a name="ms-manual.using-print"></a>9.2. Using Massif and ms_print</h2></div></div></div>
87 <p>First off, as for the other Valgrind tools, you should compile with
88 debugging info (the <code class="option">-g</code> option). It shouldn't
89 matter much what optimisation level you compile your program with, as this
90 is unlikely to affect the heap memory usage.</p>
91 <p>Then, you need to run Massif itself to gather the profiling
92 information, and then run ms_print to present it in a readable way.</p>
93 <div class="sect2">
94 <div class="titlepage"><div><div><h3 class="title">
95 <a name="ms-manual.anexample"></a>9.2.1. An Example Program</h3></div></div></div>
96 <p>An example will make things clear. Consider the following C program
97 (annotated with line numbers) which allocates a number of different blocks
98 on the heap.</p>
99 <pre class="screen">
100 1 #include &lt;stdlib.h&gt;
101 2
102 3 void g(void)
103 4 {
104 5 malloc(4000);
105 6 }
106 7
107 8 void f(void)
108 9 {
109 10 malloc(2000);
110 11 g();
111 12 }
112 13
113 14 int main(void)
114 15 {
115 16 int i;
116 17 int* a[10];
117 18
118 19 for (i = 0; i &lt; 10; i++) {
119 20 a[i] = malloc(1000);
120 21 }
121 22
122 23 f();
123 24
124 25 g();
125 26
126 27 for (i = 0; i &lt; 10; i++) {
127 28 free(a[i]);
128 29 }
129 30
130 31 return 0;
131 32 }
132 </pre>
133 </div>
134 <div class="sect2">
135 <div class="titlepage"><div><div><h3 class="title">
136 <a name="ms-manual.running-massif"></a>9.2.2. Running Massif</h3></div></div></div>
137 <p>To gather heap profiling information about the program
138 <code class="computeroutput">prog</code>, type:</p>
139 <pre class="screen">
140 valgrind --tool=massif prog
141 </pre>
142 <p>The program will execute (slowly). Upon completion, no summary
143 statistics are printed to Valgrind's commentary; all of Massif's profiling
144 data is written to a file. By default, this file is called
145 <code class="filename">massif.out.&lt;pid&gt;</code>, where
146 <code class="filename">&lt;pid&gt;</code> is the process ID, although this filename
147 can be changed with the <code class="option">--massif-out-file</code> option.</p>
148 </div>
149 <div class="sect2">
150 <div class="titlepage"><div><div><h3 class="title">
151 <a name="ms-manual.running-ms_print"></a>9.2.3. Running ms_print</h3></div></div></div>
152 <p>To see the information gathered by Massif in an easy-to-read form, use
153 ms_print. If the output file's name is
154 <code class="filename">massif.out.12345</code>, type:</p>
155 <pre class="screen">
156 ms_print massif.out.12345</pre>
157 <p>ms_print will produce (a) a graph showing the memory consumption over
158 the program's execution, and (b) detailed information about the responsible
159 allocation sites at various points in the program, including the point of
160 peak memory allocation. The use of a separate script for presenting the
161 results is deliberate: it separates the data gathering from its
162 presentation, and means that new methods of presenting the data can be added in
163 the future.</p>
164 </div>
165 <div class="sect2">
166 <div class="titlepage"><div><div><h3 class="title">
167 <a name="ms-manual.theoutputpreamble"></a>9.2.4. The Output Preamble</h3></div></div></div>
168 <p>After running this program under Massif, the first part of ms_print's
169 output contains a preamble which just states how the program, Massif and
170 ms_print were each invoked:</p>
171 <pre class="screen">
172 --------------------------------------------------------------------------------
173 Command: example
174 Massif arguments: (none)
175 ms_print arguments: massif.out.12797
176 --------------------------------------------------------------------------------
177 </pre>
178 </div>
179 <div class="sect2">
180 <div class="titlepage"><div><div><h3 class="title">
181 <a name="ms-manual.theoutputgraph"></a>9.2.5. The Output Graph</h3></div></div></div>
182 <p>The next part is the graph that shows how memory consumption occurred
183 as the program executed:</p>
184 <pre class="screen">
185 KB
186 19.63^ #
187 | #
188 | #
189 | #
190 | #
191 | #
192 | #
193 | #
194 | #
195 | #
196 | #
197 | #
198 | #
199 | #
200 | #
201 | #
202 | #
203 | :#
204 | :#
205 | :#
206 0 +-----------------------------------------------------------------------&gt;ki 0 113.4
207
208
209 Number of snapshots: 25
210 Detailed snapshots: [9, 14 (peak), 24]
211 </pre>
212 <p>Why is most of the graph empty, with only a couple of bars at the very
213 end? By default, Massif uses "instructions executed" as the unit of time.
214 For very short-run programs such as the example, most of the executed
215 instructions involve the loading and dynamic linking of the program. The
216 execution of <code class="computeroutput">main</code> (and thus the heap
217 allocations) only occur at the very end. For a short-running program like
218 this, we can use the <code class="option">--time-unit=B</code> option
219 to specify that we want the time unit to instead be the number of bytes
220 allocated/deallocated on the heap and stack(s).</p>
221 <p>If we re-run the program under Massif with this option, and then
222 re-run ms_print, we get this more useful graph:</p>
223 <pre class="screen">
224 19.63^ ###
225 | #
226 | # ::
227 | # : :::
228 | :::::::::# : : ::
229 | : # : : : ::
230 | : # : : : : :::
231 | : # : : : : : ::
232 | ::::::::::: # : : : : : : :::
233 | : : # : : : : : : : ::
234 | ::::: : # : : : : : : : : ::
235 | @@@: : : # : : : : : : : : : @
236 | ::@ : : : # : : : : : : : : : @
237 | :::: @ : : : # : : : : : : : : : @
238 | ::: : @ : : : # : : : : : : : : : @
239 | ::: : : @ : : : # : : : : : : : : : @
240 | :::: : : : @ : : : # : : : : : : : : : @
241 | ::: : : : : @ : : : # : : : : : : : : : @
242 | :::: : : : : : @ : : : # : : : : : : : : : @
243 | ::: : : : : : : @ : : : # : : : : : : : : : @
244 0 +-----------------------------------------------------------------------&gt;KB 0 29.48
245
246 Number of snapshots: 25
247 Detailed snapshots: [9, 14 (peak), 24]
248 </pre>
249 <p>The size of the graph can be changed with ms_print's
250 <code class="option">--x</code> and <code class="option">--y</code> options. Each vertical bar
251 represents a snapshot, i.e. a measurement of the memory usage at a certain
252 point in time. If the next snapshot is more than one column away, a
253 horizontal line of characters is drawn from the top of the snapshot to just
254 before the next snapshot column. The text at the bottom show that 25
255 snapshots were taken for this program, which is one per heap
256 allocation/deallocation, plus a couple of extras. Massif starts by taking
257 snapshots for every heap allocation/deallocation, but as a program runs for
258 longer, it takes snapshots less frequently. It also discards older
259 snapshots as the program goes on; when it reaches the maximum number of
260 snapshots (100 by default, although changeable with the
261 <code class="option">--max-snapshots</code> option) half of them are
262 deleted. This means that a reasonable number of snapshots are always
263 maintained.</p>
264 <p>Most snapshots are <span class="emphasis"><em>normal</em></span>, and only basic
265 information is recorded for them. Normal snapshots are represented in the
266 graph by bars consisting of ':' characters.</p>
267 <p>Some snapshots are <span class="emphasis"><em>detailed</em></span>. Information about
268 where allocations happened are recorded for these snapshots, as we will see
269 shortly. Detailed snapshots are represented in the graph by bars consisting
270 of '@' characters. The text at the bottom show that 3 detailed
271 snapshots were taken for this program (snapshots 9, 14 and 24). By default,
272 every 10th snapshot is detailed, although this can be changed via the
273 <code class="option">--detailed-freq</code> option.</p>
274 <p>Finally, there is at most one <span class="emphasis"><em>peak</em></span> snapshot. The
275 peak snapshot is a detailed snapshot, and records the point where memory
276 consumption was greatest. The peak snapshot is represented in the graph by
277 a bar consisting of '#' characters. The text at the bottom shows
278 that snapshot 14 was the peak.</p>
279 <p>Massif's determination of when the peak occurred can be wrong, for
280 two reasons.</p>
281 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
282 <li class="listitem"><p>Peak snapshots are only ever taken after a deallocation
283 happens. This avoids lots of unnecessary peak snapshot recordings
284 (imagine what happens if your program allocates a lot of heap blocks in
285 succession, hitting a new peak every time). But it means that if your
286 program never deallocates any blocks, no peak will be recorded. It also
287 means that if your program does deallocate blocks but later allocates to a
288 higher peak without subsequently deallocating, the reported peak will be
289 too low.
290 </p></li>
291 <li class="listitem"><p>Even with this behaviour, recording the peak accurately
292 is slow. So by default Massif records a peak whose size is within 1% of
293 the size of the true peak. This inaccuracy in the peak measurement can be
294 changed with the <code class="option">--peak-inaccuracy</code> option.</p></li>
295 </ul></div>
296 <p>The following graph is from an execution of Konqueror, the KDE web
297 browser. It shows what graphs for larger programs look like.</p>
298 <pre class="screen">
299 MB
300 3.952^ #
301 | @#:
302 | :@@#:
303 | @@::::@@#:
304 | @ :: :@@#::
305 | @@@ :: :@@#::
306 | @@:@@@ :: :@@#::
307 | :::@ :@@@ :: :@@#::
308 | : :@ :@@@ :: :@@#::
309 | :@: :@ :@@@ :: :@@#::
310 | @@:@: :@ :@@@ :: :@@#:::
311 | : :: ::@@:@: :@ :@@@ :: :@@#:::
312 | :@@: ::::: ::::@@@:::@@:@: :@ :@@@ :: :@@#:::
313 | ::::@@: ::: ::::::: @ :::@@:@: :@ :@@@ :: :@@#:::
314 | @: ::@@: ::: ::::::: @ :::@@:@: :@ :@@@ :: :@@#:::
315 | @: ::@@: ::: ::::::: @ :::@@:@: :@ :@@@ :: :@@#:::
316 | @: ::@@:::::: ::::::: @ :::@@:@: :@ :@@@ :: :@@#:::
317 | ::@@@: ::@@:: ::: ::::::: @ :::@@:@: :@ :@@@ :: :@@#:::
318 | :::::@ @: ::@@:: ::: ::::::: @ :::@@:@: :@ :@@@ :: :@@#:::
319 | @@:::::@ @: ::@@:: ::: ::::::: @ :::@@:@: :@ :@@@ :: :@@#:::
320 0 +-----------------------------------------------------------------------&gt;Mi
321 0 626.4
322
323 Number of snapshots: 63
324 Detailed snapshots: [3, 4, 10, 11, 15, 16, 29, 33, 34, 36, 39, 41,
325 42, 43, 44, 49, 50, 51, 53, 55, 56, 57 (peak)]
326 </pre>
327 <p>Note that the larger size units are KB, MB, GB, etc. As is typical
328 for memory measurements, these are based on a multiplier of 1024, rather
329 than the standard SI multiplier of 1000. Strictly speaking, they should be
330 written KiB, MiB, GiB, etc.</p>
331 </div>
332 <div class="sect2">
333 <div class="titlepage"><div><div><h3 class="title">
334 <a name="ms-manual.thesnapshotdetails"></a>9.2.6. The Snapshot Details</h3></div></div></div>
335 <p>Returning to our example, the graph is followed by the detailed
336 information for each snapshot. The first nine snapshots are normal, so only
337 a small amount of information is recorded for each one:</p>
338 <pre class="screen">
339 --------------------------------------------------------------------------------
340 n time(B) total(B) useful-heap(B) extra-heap(B) stacks(B)
341 --------------------------------------------------------------------------------
342 0 0 0 0 0 0
343 1 1,008 1,008 1,000 8 0
344 2 2,016 2,016 2,000 16 0
345 3 3,024 3,024 3,000 24 0
346 4 4,032 4,032 4,000 32 0
347 5 5,040 5,040 5,000 40 0
348 6 6,048 6,048 6,000 48 0
349 7 7,056 7,056 7,000 56 0
350 8 8,064 8,064 8,000 64 0
351 </pre>
352 <p>Each normal snapshot records several things.</p>
353 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
354 <li class="listitem"><p>Its number.</p></li>
355 <li class="listitem"><p>The time it was taken. In this case, the time unit is
356 bytes, due to the use of
357 <code class="option">--time-unit=B</code>.</p></li>
358 <li class="listitem"><p>The total memory consumption at that point.</p></li>
359 <li class="listitem"><p>The number of useful heap bytes allocated at that point.
360 This reflects the number of bytes asked for by the
361 program.</p></li>
362 <li class="listitem">
363 <p>The number of extra heap bytes allocated at that point.
364 This reflects the number of bytes allocated in excess of what the program
365 asked for. There are two sources of extra heap bytes.</p>
366 <p>First, every heap block has administrative bytes associated with it.
367 The exact number of administrative bytes depends on the details of the
368 allocator. By default Massif assumes 8 bytes per block, as can be seen
369 from the example, but this number can be changed via the
370 <code class="option">--heap-admin</code> option.</p>
371 <p>Second, allocators often round up the number of bytes asked for to a
372 larger number, usually 8 or 16. This is required to ensure that elements
373 within the block are suitably aligned. If N bytes are asked for, Massif
374 rounds N up to the nearest multiple of the value specified by the
375 <code class="option"><a class="link" href="manual-core.html#opt.alignment">--alignment</a></code> option.
376 </p>
377 </li>
378 <li class="listitem"><p>The size of the stack(s). By default, stack profiling is
379 off as it slows Massif down greatly. Therefore, the stack column is zero
380 in the example. Stack profiling can be turned on with the
381 <code class="option">--stacks=yes</code> option.
382
383 </p></li>
384 </ul></div>
385 <p>The next snapshot is detailed. As well as the basic counts, it gives
386 an allocation tree which indicates exactly which pieces of code were
387 responsible for allocating heap memory:</p>
388 <pre class="screen">
389 9 9,072 9,072 9,000 72 0
390 99.21% (9,000B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
391 -&gt;99.21% (9,000B) 0x804841A: main (example.c:20)
392 </pre>
393 <p>The allocation tree can be read from the top down. The first line
394 indicates all heap allocation functions such as <code class="function">malloc</code>
395 and C++ <code class="function">new</code>. All heap allocations go through these
396 functions, and so all 9,000 useful bytes (which is 99.21% of all allocated
397 bytes) go through them. But how were <code class="function">malloc</code> and new
398 called? At this point, every allocation so far has been due to line 20
399 inside <code class="function">main</code>, hence the second line in the tree. The
400 <code class="option">-&gt;</code> indicates that main (line 20) called
401 <code class="function">malloc</code>.</p>
402 <p>Let's see what the subsequent output shows happened next:</p>
403 <pre class="screen">
404 --------------------------------------------------------------------------------
405 n time(B) total(B) useful-heap(B) extra-heap(B) stacks(B)
406 --------------------------------------------------------------------------------
407 10 10,080 10,080 10,000 80 0
408 11 12,088 12,088 12,000 88 0
409 12 16,096 16,096 16,000 96 0
410 13 20,104 20,104 20,000 104 0
411 14 20,104 20,104 20,000 104 0
412 99.48% (20,000B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
413 -&gt;49.74% (10,000B) 0x804841A: main (example.c:20)
414 |
415 -&gt;39.79% (8,000B) 0x80483C2: g (example.c:5)
416 | -&gt;19.90% (4,000B) 0x80483E2: f (example.c:11)
417 | | -&gt;19.90% (4,000B) 0x8048431: main (example.c:23)
418 | |
419 | -&gt;19.90% (4,000B) 0x8048436: main (example.c:25)
420 |
421 -&gt;09.95% (2,000B) 0x80483DA: f (example.c:10)
422 -&gt;09.95% (2,000B) 0x8048431: main (example.c:23)
423 </pre>
424 <p>The first four snapshots are similar to the previous ones. But then
425 the global allocation peak is reached, and a detailed snapshot (number 14)
426 is taken. Its allocation tree shows that 20,000B of useful heap memory has
427 been allocated, and the lines and arrows indicate that this is from three
428 different code locations: line 20, which is responsible for 10,000B
429 (49.74%); line 5, which is responsible for 8,000B (39.79%); and line 10,
430 which is responsible for 2,000B (9.95%).</p>
431 <p>We can then drill down further in the allocation tree. For example,
432 of the 8,000B asked for by line 5, half of it was due to a call from line
433 11, and half was due to a call from line 25.</p>
434 <p>In short, Massif collates the stack trace of every single allocation
435 point in the program into a single tree, which gives a complete picture at
436 a particular point in time of how and why all heap memory was
437 allocated.</p>
438 <p>Note that the tree entries correspond not to functions, but to
439 individual code locations. For example, if function <code class="function">A</code>
440 calls <code class="function">malloc</code>, and function <code class="function">B</code> calls
441 <code class="function">A</code> twice, once on line 10 and once on line 11, then
442 the two calls will result in two distinct stack traces in the tree. In
443 contrast, if <code class="function">B</code> calls <code class="function">A</code> repeatedly
444 from line 15 (e.g. due to a loop), then each of those calls will be
445 represented by the same stack trace in the tree.</p>
446 <p>Note also that each tree entry with children in the example satisfies an
447 invariant: the entry's size is equal to the sum of its children's sizes.
448 For example, the first entry has size 20,000B, and its children have sizes
449 10,000B, 8,000B, and 2,000B. In general, this invariant almost always
450 holds. However, in rare circumstances stack traces can be malformed, in
451 which case a stack trace can be a sub-trace of another stack trace. This
452 means that some entries in the tree may not satisfy the invariant -- the
453 entry's size will be greater than the sum of its children's sizes. This is
454 not a big problem, but could make the results confusing. Massif can
455 sometimes detect when this happens; if it does, it issues a warning:</p>
456 <pre class="screen">
457 Warning: Malformed stack trace detected. In Massif's output,
458 the size of an entry's child entries may not sum up
459 to the entry's size as they normally do.
460 </pre>
461 <p>However, Massif does not detect and warn about every such occurrence.
462 Fortunately, malformed stack traces are rare in practice.</p>
463 <p>Returning now to ms_print's output, the final part is similar:</p>
464 <pre class="screen">
465 --------------------------------------------------------------------------------
466 n time(B) total(B) useful-heap(B) extra-heap(B) stacks(B)
467 --------------------------------------------------------------------------------
468 15 21,112 19,096 19,000 96 0
469 16 22,120 18,088 18,000 88 0
470 17 23,128 17,080 17,000 80 0
471 18 24,136 16,072 16,000 72 0
472 19 25,144 15,064 15,000 64 0
473 20 26,152 14,056 14,000 56 0
474 21 27,160 13,048 13,000 48 0
475 22 28,168 12,040 12,000 40 0
476 23 29,176 11,032 11,000 32 0
477 24 30,184 10,024 10,000 24 0
478 99.76% (10,000B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
479 -&gt;79.81% (8,000B) 0x80483C2: g (example.c:5)
480 | -&gt;39.90% (4,000B) 0x80483E2: f (example.c:11)
481 | | -&gt;39.90% (4,000B) 0x8048431: main (example.c:23)
482 | |
483 | -&gt;39.90% (4,000B) 0x8048436: main (example.c:25)
484 |
485 -&gt;19.95% (2,000B) 0x80483DA: f (example.c:10)
486 | -&gt;19.95% (2,000B) 0x8048431: main (example.c:23)
487 |
488 -&gt;00.00% (0B) in 1+ places, all below ms_print's threshold (01.00%)
489 </pre>
490 <p>The final detailed snapshot shows how the heap looked at termination.
491 The 00.00% entry represents the code locations for which memory was
492 allocated and then freed (line 20 in this case, the memory for which was
493 freed on line 28). However, no code location details are given for this
494 entry; by default, Massif only records the details for code locations
495 responsible for more than 1% of useful memory bytes, and ms_print likewise
496 only prints the details for code locations responsible for more than 1%.
497 The entries that do not meet this threshold are aggregated. This avoids
498 filling up the output with large numbers of unimportant entries. The
499 thresholds can be changed with the
500 <code class="option">--threshold</code> option that both Massif and
501 ms_print support.</p>
502 </div>
503 <div class="sect2">
504 <div class="titlepage"><div><div><h3 class="title">
505 <a name="ms-manual.forkingprograms"></a>9.2.7. Forking Programs</h3></div></div></div>
506 <p>If your program forks, the child will inherit all the profiling data that
507 has been gathered for the parent.</p>
508 <p>If the output file format string (controlled by
509 <code class="option">--massif-out-file</code>) does not contain <code class="option">%p</code>, then
510 the outputs from the parent and child will be intermingled in a single output
511 file, which will almost certainly make it unreadable by ms_print.</p>
512 </div>
513 <div class="sect2">
514 <div class="titlepage"><div><div><h3 class="title">
515 <a name="ms-manual.not-measured"></a>9.2.8. Measuring All Memory in a Process</h3></div></div></div>
516 <p>
517 It is worth emphasising that by default Massif measures only heap memory, i.e.
518 memory allocated with
519 <code class="function">malloc</code>,
520 <code class="function">calloc</code>,
521 <code class="function">realloc</code>,
522 <code class="function">memalign</code>,
523 <code class="function">new</code>,
524 <code class="function">new[]</code>,
525 and a few other, similar functions. (And it can optionally measure stack
526 memory, of course.) This means it does <span class="emphasis"><em>not</em></span> directly
527 measure memory allocated with lower-level system calls such as
528 <code class="function">mmap</code>,
529 <code class="function">mremap</code>, and
530 <code class="function">brk</code>.
531 </p>
532 <p>
533 Heap allocation functions such as <code class="function">malloc</code> are built on
534 top of these system calls. For example, when needed, an allocator will
535 typically call <code class="function">mmap</code> to allocate a large chunk of
536 memory, and then hand over pieces of that memory chunk to the client program
537 in response to calls to <code class="function">malloc</code> et al. Massif directly
538 measures only these higher-level <code class="function">malloc</code> et al calls,
539 not the lower-level system calls.
540 </p>
541 <p>
542 Furthermore, a client program may use these lower-level system calls
543 directly to allocate memory. By default, Massif does not measure these. Nor
544 does it measure the size of code, data and BSS segments. Therefore, the
545 numbers reported by Massif may be significantly smaller than those reported by
546 tools such as <code class="filename">top</code> that measure a program's total size in
547 memory.
548 </p>
549 <p>
550 However, if you wish to measure <span class="emphasis"><em>all</em></span> the memory used by
551 your program, you can use the <code class="option">--pages-as-heap=yes</code>. When this
552 option is enabled, Massif's normal heap block profiling is replaced by
553 lower-level page profiling. Every page allocated via
554 <code class="function">mmap</code> and similar system calls is treated as a distinct
555 block. This means that code, data and BSS segments are all measured, as they
556 are just memory pages. Even the stack is measured, since it is ultimately
557 allocated (and extended when necessary) via <code class="function">mmap</code>; for
558 this reason <code class="option">--stacks=yes</code> is not allowed in conjunction with
559 <code class="option">--pages-as-heap=yes</code>.
560 </p>
561 <p>
562 After <code class="option">--pages-as-heap=yes</code> is used, ms_print's output is
563 mostly unchanged. One difference is that the start of each detailed snapshot
564 says:
565 </p>
566 <pre class="screen">
567 (page allocation syscalls) mmap/mremap/brk, --alloc-fns, etc.
568 </pre>
569 <p>instead of the usual:</p>
570 <pre class="screen">
571 (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
572 </pre>
573 <p>
574 The stack traces in the output may be more difficult to read, and interpreting
575 them may require some detailed understanding of the lower levels of a program
576 like the memory allocators. But for some programs having the full information
577 about memory usage can be very useful.
578 </p>
579 </div>
580 <div class="sect2">
581 <div class="titlepage"><div><div><h3 class="title">
582 <a name="ms-manual.acting"></a>9.2.9. Acting on Massif's Information</h3></div></div></div>
583 <p>Massif's information is generally fairly easy to act upon. The
584 obvious place to start looking is the peak snapshot.</p>
585 <p>It can also be useful to look at the overall shape of the graph, to
586 see if memory usage climbs and falls as you expect; spikes in the graph
587 might be worth investigating.</p>
588 <p>The detailed snapshots can get quite large. It is worth viewing them
589 in a very wide window. It's also a good idea to view them with a text
590 editor. That makes it easy to scroll up and down while keeping the cursor
591 in a particular column, which makes following the allocation chains easier.
592 </p>
593 </div>
594 </div>
595 <div class="sect1">
596 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
597 <a name="ms-manual.using-visualizer"></a>9.3. Using massif-visualizer</h2></div></div></div>
598 <p>
599 <a class="ulink" href="https://github.com/KDE/massif-visualizer" target="_top">massif-visualizer</a>
600 is a graphical viewer for Massif data that is often easier to use than
601 ms_print. massif-visualizer is not shipped within Valgrind, but is available in
602 various places online.
603 </p>
604 </div>
605 <div class="sect1">
606 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
607 <a name="ms-manual.options"></a>9.4. Massif Command-line Options</h2></div></div></div>
608 <p>Massif-specific command-line options are:</p>
609 <div class="variablelist">
610 <a name="ms.opts.list"></a><dl class="variablelist">
611 <dt>
612 <a name="opt.heap"></a><span class="term">
613 <code class="option">--heap=&lt;yes|no&gt; [default: yes] </code>
614 </span>
615 </dt>
616 <dd><p>Specifies whether heap profiling should be done.</p></dd>
617 <dt>
618 <a name="opt.heap-admin"></a><span class="term">
619 <code class="option">--heap-admin=&lt;size&gt; [default: 8] </code>
620 </span>
621 </dt>
622 <dd><p>If heap profiling is enabled, gives the number of administrative
623 bytes per block to use. This should be an estimate of the average,
624 since it may vary. For example, the allocator used by
625 glibc on Linux requires somewhere between 4 to
626 15 bytes per block, depending on various factors. That allocator also
627 requires admin space for freed blocks, but Massif cannot
628 account for this.</p></dd>
629 <dt>
630 <a name="opt.stacks"></a><span class="term">
631 <code class="option">--stacks=&lt;yes|no&gt; [default: no] </code>
632 </span>
633 </dt>
634 <dd>
635 <p>Specifies whether stack profiling should be done. This option
636 slows Massif down greatly, and so is off by default. Note that Massif
637 assumes that the main stack has size zero at start-up. This is not
638 true, but doing otherwise accurately is difficult. Furthermore,
639 starting at zero better indicates the size of the part of the main
640 stack that a user program actually has control over.</p>
641 <p>If you give at least 4 <code class="option">-v</code> verbosity arguments,
642 then massif produces a trace for each stack increase and decrease.
643 The stack increase trace contains the IP address that increased the stack.
644 Note that to get fully precise IP address, you must specify the options
645 <code class="option">-px-default=unwindregs-at-mem-access
646 --px-file-backed=unwindregs-at-mem-access</code>.
647 </p>
648 </dd>
649 <dt>
650 <a name="opt.pages-as-heap"></a><span class="term">
651 <code class="option">--pages-as-heap=&lt;yes|no&gt; [default: no] </code>
652 </span>
653 </dt>
654 <dd><p>Tells Massif to profile memory at the page level rather
655 than at the malloc'd block level. See above for details.
656 </p></dd>
657 <dt>
658 <a name="opt.depth"></a><span class="term">
659 <code class="option">--depth=&lt;number&gt; [default: 30] </code>
660 </span>
661 </dt>
662 <dd><p>Maximum depth of the allocation trees recorded for detailed
663 snapshots. Increasing it will make Massif run somewhat more slowly,
664 use more memory, and produce bigger output files.</p></dd>
665 <dt>
666 <a name="opt.alloc-fn"></a><span class="term">
667 <code class="option">--alloc-fn=&lt;name&gt; </code>
668 </span>
669 </dt>
670 <dd>
671 <p>Functions specified with this option will be treated as though
672 they were a heap allocation function such as
673 <code class="function">malloc</code>. This is useful for functions that are
674 wrappers to <code class="function">malloc</code> or <code class="function">new</code>,
675 which can fill up the allocation trees with uninteresting information.
676 This option can be specified multiple times on the command line, to
677 name multiple functions.</p>
678 <p>Note that the named function will only be treated this way if it is
679 the top entry in a stack trace, or just below another function treated
680 this way. For example, if you have a function
681 <code class="function">malloc1</code> that wraps <code class="function">malloc</code>,
682 and <code class="function">malloc2</code> that wraps
683 <code class="function">malloc1</code>, just specifying
684 <code class="option">--alloc-fn=malloc2</code> will have no effect. You need to
685 specify <code class="option">--alloc-fn=malloc1</code> as well. This is a little
686 inconvenient, but the reason is that checking for allocation functions
687 is slow, and it saves a lot of time if Massif can stop looking through
688 the stack trace entries as soon as it finds one that doesn't match
689 rather than having to continue through all the entries.</p>
690 <p>Note that C++ names are demangled. Note also that overloaded
691 C++ names must be written in full. Single quotes may be necessary to
692 prevent the shell from breaking them up. For example:
693 </p>
694 <pre class="screen">
695 --alloc-fn='operator new(unsigned, std::nothrow_t const&amp;)'
696 </pre>
697 <p>
698 Arguments of type <code class="computeroutput">size_t</code> need to be replaced
699 with <code class="computeroutput">unsigned long</code> on 64bit platforms and <code class="computeroutput">unsigned</code>
700 on 32bit platforms.
701 </p>
702 <p><code class="option">--alloc-fn</code> will work with inline functions.
703 Inline function names are not mangled, which means that you only need
704 to provide the function name and not the argument list.
705 </p>
706 <p><code class="option">--alloc-fn</code> does not support wildcards.
707 </p>
708 </dd>
709 <dt>
710 <a name="opt.ignore-fn"></a><span class="term">
711 <code class="option">--ignore-fn=&lt;name&gt; </code>
712 </span>
713 </dt>
714 <dd>
715 <p>Any direct heap allocation (i.e. a call to
716 <code class="function">malloc</code>, <code class="function">new</code>, etc, or a call
717 to a function named by an <code class="option">--alloc-fn</code>
718 option) that occurs in a function specified by this option will be
719 ignored. This is mostly useful for testing purposes. This option can
720 be specified multiple times on the command line, to name multiple
721 functions.
722 </p>
723 <p>Any <code class="function">realloc</code> of an ignored block will
724 also be ignored, even if the <code class="function">realloc</code> call does
725 not occur in an ignored function. This avoids the possibility of
726 negative heap sizes if ignored blocks are shrunk with
727 <code class="function">realloc</code>.
728 </p>
729 <p>The rules for writing C++ function names are the same as
730 for <code class="option">--alloc-fn</code> above.
731 </p>
732 </dd>
733 <dt>
734 <a name="opt.threshold"></a><span class="term">
735 <code class="option">--threshold=&lt;m.n&gt; [default: 1.0] </code>
736 </span>
737 </dt>
738 <dd><p>The significance threshold for heap allocations, as a
739 percentage of total memory size. Allocation tree entries that account
740 for less than this will be aggregated. Note that this should be
741 specified in tandem with ms_print's option of the same name.</p></dd>
742 <dt>
743 <a name="opt.peak-inaccuracy"></a><span class="term">
744 <code class="option">--peak-inaccuracy=&lt;m.n&gt; [default: 1.0] </code>
745 </span>
746 </dt>
747 <dd><p>Massif does not necessarily record the actual global memory
748 allocation peak; by default it records a peak only when the global
749 memory allocation size exceeds the previous peak by at least 1.0%.
750 This is because there can be many local allocation peaks along the way,
751 and doing a detailed snapshot for every one would be expensive and
752 wasteful, as all but one of them will be later discarded. This
753 inaccuracy can be changed (even to 0.0%) via this option, but Massif
754 will run drastically slower as the number approaches zero.</p></dd>
755 <dt>
756 <a name="opt.time-unit"></a><span class="term">
757 <code class="option">--time-unit=&lt;i|ms|B&gt; [default: i] </code>
758 </span>
759 </dt>
760 <dd><p>The time unit used for the profiling. There are three
761 possibilities: instructions executed (i), which is good for most
762 cases; real (wallclock) time (ms, i.e. milliseconds), which is
763 sometimes useful; and bytes allocated/deallocated on the heap and/or
764 stack (B), which is useful for very short-run programs, and for
765 testing purposes, because it is the most reproducible across different
766 machines.</p></dd>
767 <dt>
768 <a name="opt.detailed-freq"></a><span class="term">
769 <code class="option">--detailed-freq=&lt;n&gt; [default: 10] </code>
770 </span>
771 </dt>
772 <dd><p>Frequency of detailed snapshots. With
773 <code class="option">--detailed-freq=1</code>, every snapshot is
774 detailed.</p></dd>
775 <dt>
776 <a name="opt.max-snapshots"></a><span class="term">
777 <code class="option">--max-snapshots=&lt;n&gt; [default: 100] </code>
778 </span>
779 </dt>
780 <dd><p>The maximum number of snapshots recorded. If set to N, for all
781 programs except very short-running ones, the final number of snapshots
782 will be between N/2 and N.</p></dd>
783 <dt>
784 <a name="opt.massif-out-file"></a><span class="term">
785 <code class="option">--massif-out-file=&lt;file&gt; [default: massif.out.%p] </code>
786 </span>
787 </dt>
788 <dd><p>Write the profile data to <code class="computeroutput">file</code>
789 rather than to the default output file,
790 <code class="computeroutput">massif.out.&lt;pid&gt;</code>. The
791 <code class="option">%p</code> and <code class="option">%q</code> format specifiers can be
792 used to embed the process ID and/or the contents of an environment
793 variable in the name, as is the case for the core option
794 <code class="option"><a class="link" href="manual-core.html#opt.log-file">--log-file</a></code>.
795 </p></dd>
796 </dl>
797 </div>
798 </div>
799 <div class="sect1">
800 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
801 <a name="ms-manual.monitor-commands"></a>9.5. Massif Monitor Commands</h2></div></div></div>
802 <p>The Massif tool provides monitor commands handled by the Valgrind
803 gdbserver (see <a class="xref" href="manual-core-adv.html#manual-core-adv.gdbserver-commandhandling" title="3.2.5. Monitor command handling by the Valgrind gdbserver">Monitor command handling by the Valgrind gdbserver</a>).
804 Valgrind python code provides GDB front end commands giving an easier usage of
805 the massif monitor commands (see
806 <a class="xref" href="manual-core-adv.html#manual-core-adv.gdbserver-gdbmonitorfrontend" title="3.2.6. GDB front end commands for Valgrind gdbserver monitor commands">GDB front end commands for Valgrind gdbserver monitor commands</a>). To launch a
807 massif monitor command via its GDB front end command, instead of prefixing
808 the command with "monitor", you must use the GDB <code class="varname">massif</code>
809 command (or the shorter aliases <code class="varname">ms</code>). Using the massif GDB
810 front end command provide a more flexible usage, such as auto-completion of the
811 command by GDB. In GDB, you can use <code class="varname">help massif</code> to get
812 help about the massif front end monitor commands and you can
813 use <code class="varname">apropos massif</code> to get all the commands mentionning the
814 word "massif" in their name or on-line help.
815 </p>
816 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
817 <li class="listitem"><p><code class="varname">snapshot [&lt;filename&gt;]</code> requests
818 to take a snapshot and save it in the given &lt;filename&gt;
819 (default massif.vgdb.out).
820 </p></li>
821 <li class="listitem"><p><code class="varname">detailed_snapshot [&lt;filename&gt;]</code>
822 requests to take a detailed snapshot and save it in the given
823 &lt;filename&gt; (default massif.vgdb.out).
824 </p></li>
825 <li class="listitem"><p><code class="varname">all_snapshots [&lt;filename&gt;]</code>
826 requests to take all captured snapshots so far and save them in the given
827 &lt;filename&gt; (default massif.vgdb.out).
828 </p></li>
829 <li class="listitem"><p><code class="varname">xtmemory [&lt;filename&gt; default xtmemory.kcg.%p.%n]</code>
830 requests Massif tool to produce an xtree heap memory report.
831 See <a class="xref" href="manual-core.html#manual-core.xtree" title="2.10. Execution Trees">Execution Trees</a> for
832 a detailed explanation about execution trees. </p></li>
833 </ul></div>
834 </div>
835 <div class="sect1">
836 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
837 <a name="ms-manual.clientreqs"></a>9.6. Massif Client Requests</h2></div></div></div>
838 <p>Massif does not have a <code class="filename">massif.h</code> file, but it does
839 implement two of the core client requests:
840 <code class="function">VALGRIND_MALLOCLIKE_BLOCK</code> and
841 <code class="function">VALGRIND_FREELIKE_BLOCK</code>; they are described in
842 <a class="xref" href="manual-core-adv.html#manual-core-adv.clientreq" title="3.1. The Client Request mechanism">The Client Request mechanism</a>.
843 </p>
844 </div>
845 <div class="sect1">
846 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
847 <a name="ms-manual.ms_print-options"></a>9.7. ms_print Command-line Options</h2></div></div></div>
848 <p>ms_print's options are:</p>
849 <div class="variablelist">
850 <a name="ms_print.opts.list"></a><dl class="variablelist">
851 <dt><span class="term">
852 <code class="option">-h --help </code>
853 </span></dt>
854 <dd><p>Show the help message.</p></dd>
855 <dt><span class="term">
856 <code class="option">--version </code>
857 </span></dt>
858 <dd><p>Show the version number.</p></dd>
859 <dt><span class="term">
860 <code class="option">--threshold=&lt;m.n&gt; [default: 1.0] </code>
861 </span></dt>
862 <dd><p>Same as Massif's <code class="option">--threshold</code> option, but
863 applied after profiling rather than during.</p></dd>
864 <dt><span class="term">
865 <code class="option">--x=&lt;4..1000&gt; [default: 72]</code>
866 </span></dt>
867 <dd><p>Width of the graph, in columns.</p></dd>
868 <dt><span class="term">
869 <code class="option">--y=&lt;4..1000&gt; [default: 20] </code>
870 </span></dt>
871 <dd><p>Height of the graph, in rows.</p></dd>
872 </dl>
873 </div>
874 </div>
875 <div class="sect1">
876 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
877 <a name="ms-manual.fileformat"></a>9.8. Massif's Output File Format</h2></div></div></div>
878 <p>Massif's file format is plain text (i.e. not binary) and deliberately
879 easy to read for both humans and machines. Nonetheless, the exact format
880 is not described here. This is because the format is currently very
881 Massif-specific. In the future we hope to make the format more general, and
882 thus suitable for possible use with other tools. Once this has been done,
883 the format will be documented here.</p>
884 </div>
885 </div>
886 <div>
887 <br><table class="nav" width="100%" cellspacing="3" cellpadding="2" border="0" summary="Navigation footer">
888 <tr>
889 <td rowspan="2" width="40%" align="left">
890 <a accesskey="p" href="drd-manual.html">&lt;&lt; 8. DRD: a thread error detector</a> </td>
891 <td width="20%" align="center"><a accesskey="u" href="manual.html">Up</a></td>
892 <td rowspan="2" width="40%" align="right"> <a accesskey="n" href="dh-manual.html">10. DHAT: a dynamic heap analysis tool &gt;&gt;</a>
893 </td>
894 </tr>
895 <tr><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td></tr>
896 </table>
897 </div>
898 </body>
899 </html>