📄 ms-manual.html
字号:
<html xmlns:cf="http://docbook.sourceforge.net/xmlns/chunkfast/1.0"><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>6.燤assif: a heap profiler</title><link rel="stylesheet" href="vg_basic.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.69.0"><link rel="start" href="index.html" title="Valgrind Documentation"><link rel="up" href="manual.html" title="Valgrind User Manual"><link rel="prev" href="cl-manual.html" title="5.燙allgrind: a heavyweight profiler"><link rel="next" href="hg-manual.html" title="7.燞elgrind: a data-race detector"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div><table class="nav" width="100%" cellspacing="3" cellpadding="3" border="0" summary="Navigation header"><tr><td width="22px" align="center" valign="middle"><a accesskey="p" href="cl-manual.html"><img src="images/prev.png" width="18" height="21" border="0" alt="Prev"></a></td><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><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><th align="center" valign="middle">Valgrind User Manual</th><td width="22px" align="center" valign="middle"><a accesskey="n" href="hg-manual.html"><img src="images/next.png" width="18" height="21" border="0" alt="Next"></a></td></tr></table></div><div class="chapter" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="ms-manual"></a>6.燤assif: a heap profiler</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="sect1"><a href="ms-manual.html#ms-manual.spaceprof">6.1. Heap profiling</a></span></dt><dd><dl><dt><span class="sect2"><a href="ms-manual.html#ms-manual.heapprof">6.1.1. Why Use a Heap Profiler?</a></span></dt></dl></dd><dt><span class="sect1"><a href="ms-manual.html#ms-manual.using">6.2. Using Massif</a></span></dt><dd><dl><dt><span class="sect2"><a href="ms-manual.html#ms-manual.overview">6.2.1. Overview</a></span></dt><dt><span class="sect2"><a href="ms-manual.html#ms-manual.basicresults">6.2.2. Basic Results of Profiling</a></span></dt><dt><span class="sect2"><a href="ms-manual.html#ms-manual.graphs">6.2.3. Spacetime Graphs</a></span></dt></dl></dd><dt><span class="sect1"><a href="ms-manual.html#ms-manual.heapdetails">6.3. Details of Heap Allocations</a></span></dt><dd><dl><dt><span class="sect2"><a href="ms-manual.html#ms-manual.accuracy">6.3.1. Accuracy</a></span></dt></dl></dd><dt><span class="sect1"><a href="ms-manual.html#ms-manual.options">6.4. Massif Options</a></span></dt></dl></div><p>To use this tool, you must specify<code class="computeroutput">--tool=massif</code> on the Valgrindcommand line.</p><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="ms-manual.spaceprof"></a>6.1.燞eap profiling</h2></div></div></div><p>Massif is a heap profiler, i.e. it measures how much heapmemory programs use. In particular, it can give you informationabout:</p><div class="itemizedlist"><ul type="disc"><li><p>Heap blocks;</p></li><li><p>Heap administration blocks;</p></li><li><p>Stack sizes.</p></li></ul></div><p>Heap profiling is useful to help you reduce the amount ofmemory your program uses. On modern machines with virtualmemory, this provides the following benefits:</p><div class="itemizedlist"><ul type="disc"><li><p>It can speed up your program -- a smaller program will interact better with your machine's caches and avoid paging.</p></li><li><p>If your program uses lots of memory, it will reduce the chance that it exhausts your machine's swap space.</p></li></ul></div><p>Also, there are certain space leaks that aren't detected bytraditional leak-checkers, such as Memcheck's. That's becausethe memory isn't ever actually lost -- a pointer remains to it --but it's not in use. Programs that have leaks like this canunnecessarily increase the amount of memory they are using overtime.</p><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="ms-manual.heapprof"></a>6.1.1.燱hy Use a Heap Profiler?</h3></div></div></div><p>Everybody knows how useful time profilers are for speedingup programs. They are particularly useful because people arenotoriously bad at predicting where are the bottlenecks in theirprograms.</p><p>But the story is different for heap profilers. Someprogramming languages, particularly lazy functional languageslike <a href="http://www.haskell.org" target="_top">Haskell</a>, havequite sophisticated heap profilers. But there are few tools aspowerful for profiling C and C++ programs.</p><p>Why is this? Maybe it's because C and C++ programmers mustthink that they know where the memory is being allocated. Afterall, you can see all the calls to<code class="computeroutput">malloc()</code> and<code class="computeroutput">new</code> and<code class="computeroutput">new[]</code>, right? But, in a bigprogram, do you really know which heap allocations are beingexecuted, how many times, and how large each allocation is? Canyou give even a vague estimate of the memory footprint for yourprogram? Do you know this for all the libraries your programuses? What about administration bytes required by the heapallocator to track heap blocks -- have you thought about them?What about the stack? If you are unsure about any of thesethings, maybe you should think about heap profiling.</p><p>Massif can tell you these things.</p><p>Or maybe it's because it's relatively easy to add basicheap profiling functionality into a program, to tell you how manybytes you have allocated for certain objects, or similar. Butthis information might only be simple like total counts for thewhole program's execution. What about space usage at differentpoints in the program's execution, for example? Andreimplementing heap profiling code for each project is apain.</p><p>Massif can save you this effort.</p></div></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="ms-manual.using"></a>6.2.燯sing Massif</h2></div></div></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="ms-manual.overview"></a>6.2.1.燨verview</h3></div></div></div><p>First off, as for normal Valgrind use, you probably want tocompile with debugging info (the<code class="computeroutput">-g</code> flag). But, as opposed toMemcheck, you probably <span><strong class="command">do</strong></span> want to turnoptimisation on, since you should profile your program as it willbe normally run.</p><p>Then, run your program with <code class="computeroutput">valgrind--tool=massif</code> in front of the normal commandline invocation. When the program finishes, Massif will printsummary space statistics. It also creates a graph representingthe program's heap usage in a file called<code class="filename">massif.pid.ps</code>, which can be read by anyPostScript viewer, such as Ghostview.</p><p>It also puts detailed information about heap consumption ina file <code class="filename">massif.pid.txt</code> (text format) or<code class="filename">massif.pid.html</code> (HTML format), where<span class="emphasis"><em>pid</em></span> is the program's process id.</p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="ms-manual.basicresults"></a>6.2.2.燘asic Results of Profiling</h3></div></div></div><p>To gather heap profiling information about the program<code class="computeroutput">prog</code>, type:</p><pre class="screen">% valgrind --tool=massif prog</pre><p>The program will execute (slowly). Upon completion,summary statistics that look like this will be printed:</p><pre class="programlisting">==27519== Total spacetime: 2,258,106 ms.B==27519== heap: 24.0%==27519== heap admin: 2.2%==27519== stack(s): 73.7%</pre><p>All measurements are done in<span class="emphasis"><em>spacetime</em></span>, i.e. space (in bytes) multipliedby time (in milliseconds). Note that because Massif slows aprogram down a lot, the actual spacetime figure is fairlymeaningless; it's the relative values that areinteresting.</p><p>Which entries you see in the breakdown depends on thecommand line options given. The above example measures all thepossible parts of memory:</p><div class="itemizedlist"><ul type="disc"><li><p>Heap: number of words allocated on the heap, via <code class="computeroutput">malloc()</code>, <code class="computeroutput">new</code> and <code class="computeroutput">new[]</code>.</p></li><li><p>Heap admin: each heap block allocated requires some administration data, which lets the allocator track certain things about the block. It is easy to forget about this, and if your program allocates lots of small blocks, it can add up. This value is an estimate of the space required for this administration data.</p></li><li><p>Stack(s): the spacetime used by the programs' stack(s). (Threaded programs can have multiple stacks.) This includes signal handler stacks.</p></li></ul></div></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="ms-manual.graphs"></a>6.2.3.燬pacetime Graphs</h3></div></div></div><p>As well as printing summary information, Massif alsocreates a file representing a spacetime graph,<code class="filename">massif.pid.hp</code>. It will produce a filecalled <code class="filename">massif.pid.ps</code>, which can be viewed ina PostScript viewer.</p><p>Massif uses a program called<code class="computeroutput">hp2ps</code> to convert the raw datainto the PostScript graph. It's distributed with Massif, butcame originally from the <a href="http://www.haskell.org/ghc/" target="_top">Glasgow HaskellCompiler</a>. You shouldn't need to worry about this at all.However, if the graph creation fails for any reason, Massif willtell you, and will leave behind a file named<code class="filename">massif.pid.hp</code>, containing the raw heapprofiling data.</p><p>Here's an example graph:</p><div class="mediaobject"><a name="spacetime-graph"></a><img src="images/massif-graph-sm.png" alt="Spacetime Graph"></div><p>The graph is broken into several bands. Most bandsrepresent a single line of your program that does some heapallocation; each such band represents all the allocations anddeallocations done from that line. Up to twenty bands are shown;less significant allocation sites are merged into "other" and/or"OTHER" bands. The accompanying text/HTML file produced byMassif has more detail about these heap allocation bands. Thenthere are single bands for the stack(s) and heap adminbytes.</p><p><b>Note:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -