| 1 |
<html> |
| 2 |
<head> |
| 3 |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| 4 |
<title>10. DHAT: a dynamic heap analysis tool</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="ms-manual.html" title="9. Massif: a heap profiler"> |
| 10 |
<link rel="next" href="lk-manual.html" title="11. Lackey: an example 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="ms-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="lk-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="dh-manual"></a>10. DHAT: a dynamic heap analysis tool</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="dh-manual.html#dh-manual.overview">10.1. Overview</a></span></dt> |
| 27 |
<dt><span class="sect1"><a href="dh-manual.html#dh-manual.profile">10.2. Using DHAT</a></span></dt> |
| 28 |
<dd><dl> |
| 29 |
<dt><span class="sect2"><a href="dh-manual.html#dh-manual.running-DHAT">10.2.1. Running DHAT</a></span></dt> |
| 30 |
<dt><span class="sect2"><a href="dh-manual.html#dh-manual.outputfile">10.2.2. Output File</a></span></dt> |
| 31 |
</dl></dd> |
| 32 |
<dt><span class="sect1"><a href="dh-manual.html#dh-manual.viewer">10.3. DHAT's Viewer</a></span></dt> |
| 33 |
<dd><dl> |
| 34 |
<dt><span class="sect2"><a href="dh-manual.html#dh-output-header">10.3.1. The Output Header</a></span></dt> |
| 35 |
<dt><span class="sect2"><a href="dh-manual.html#dh-ap-tree">10.3.2. The PP Tree</a></span></dt> |
| 36 |
<dt><span class="sect2"><a href="dh-manual.html#dh-output-footer">10.3.3. The Output Footer</a></span></dt> |
| 37 |
<dt><span class="sect2"><a href="dh-manual.html#dh-sort-metrics">10.3.4. Sort Metrics</a></span></dt> |
| 38 |
</dl></dd> |
| 39 |
<dt><span class="sect1"><a href="dh-manual.html#dh-manual.realloc">10.4. Treatment of realloc</a></span></dt> |
| 40 |
<dt><span class="sect1"><a href="dh-manual.html#dh-manual.copy-profiling">10.5. Copy profiling</a></span></dt> |
| 41 |
<dt><span class="sect1"><a href="dh-manual.html#dh-manual.ad-hoc-profiling">10.6. Ad hoc profiling</a></span></dt> |
| 42 |
<dt><span class="sect1"><a href="dh-manual.html#dh-manual.options">10.7. DHAT Command-line Options</a></span></dt> |
| 43 |
</dl> |
| 44 |
</div> |
| 45 |
<p>To use this tool, you must specify |
| 46 |
<code class="option">--tool=dhat</code> on the Valgrind command line.</p> |
| 47 |
<div class="sect1"> |
| 48 |
<div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
| 49 |
<a name="dh-manual.overview"></a>10.1. Overview</h2></div></div></div> |
| 50 |
<p>DHAT is primarily a tool for examining how programs use their heap |
| 51 |
allocations.</p> |
| 52 |
<p>It tracks the allocated blocks, and inspects every memory access |
| 53 |
to find which block, if any, it is to. It presents, on a program point |
| 54 |
basis, information about these blocks such as sizes, lifetimes, numbers of |
| 55 |
reads and writes, and read and write patterns.</p> |
| 56 |
<p>Using this information it is possible to identify program points with |
| 57 |
the following characteristics:</p> |
| 58 |
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> |
| 59 |
<li class="listitem"><p>potential process-lifetime leaks: blocks allocated |
| 60 |
by the point just accumulate, and are freed only at the end of the |
| 61 |
run.</p></li> |
| 62 |
<li class="listitem"><p>excessive turnover: points which chew through a lot |
| 63 |
of heap, even if it is not held onto for very long</p></li> |
| 64 |
<li class="listitem"><p>excessively transient: points which allocate very |
| 65 |
short lived blocks</p></li> |
| 66 |
<li class="listitem"><p>useless or underused allocations: blocks which are |
| 67 |
allocated but not completely filled in, or are filled in but not |
| 68 |
subsequently read.</p></li> |
| 69 |
<li class="listitem"><p>blocks with inefficient layout -- areas never |
| 70 |
accessed, or with hot fields scattered throughout the |
| 71 |
block.</p></li> |
| 72 |
</ul></div> |
| 73 |
<p>As with the Massif heap profiler, DHAT measures program progress |
| 74 |
by counting instructions, and so presents all age/time related figures |
| 75 |
as instruction counts. This sounds a little odd at first, but it |
| 76 |
makes runs repeatable in a way which is not possible if CPU time is |
| 77 |
used.</p> |
| 78 |
<p>DHAT also has support for copy profiling and ad hoc profiling. These are |
| 79 |
described below.</p> |
| 80 |
</div> |
| 81 |
<div class="sect1"> |
| 82 |
<div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
| 83 |
<a name="dh-manual.profile"></a>10.2. Using DHAT</h2></div></div></div> |
| 84 |
<p>First off, as for normal Valgrind use, you probably want to compile with |
| 85 |
debugging info (the <code class="option">-g</code> option). But by contrast with normal |
| 86 |
Valgrind use, you probably do want to turn optimisation on, since you should |
| 87 |
profile your program as it will be normally run.</p> |
| 88 |
<p>Second, you need to run your program under DHAT to gather the profiling |
| 89 |
information. You might need to reduce the <code class="option">--num-callers</code> value |
| 90 |
to get reasonably-sized output files, especially if you are profiling a large |
| 91 |
program; some trial and error might be needed to find a good value.</p> |
| 92 |
<p>Finally, you need to use DHAT's viewer (in a web browser) to get a |
| 93 |
detailed presentation of that information.</p> |
| 94 |
<div class="sect2"> |
| 95 |
<div class="titlepage"><div><div><h3 class="title"> |
| 96 |
<a name="dh-manual.running-DHAT"></a>10.2.1. Running DHAT</h3></div></div></div> |
| 97 |
<p>To run DHAT on a program <code class="filename">prog</code>, run:</p> |
| 98 |
<pre class="screen"> |
| 99 |
valgrind --tool=dhat prog |
| 100 |
</pre> |
| 101 |
<p>The program will execute (slowly). Upon completion, summary statistics |
| 102 |
that look like this will be printed:</p> |
| 103 |
<pre class="programlisting"> |
| 104 |
==11514== Total: 823,849,731 bytes in 3,929,133 blocks |
| 105 |
==11514== At t-gmax: 133,485,082 bytes in 436,521 blocks |
| 106 |
==11514== At t-end: 258,002 bytes in 2,129 blocks |
| 107 |
==11514== Reads: 2,807,182,810 bytes |
| 108 |
==11514== Writes: 1,149,617,086 bytes |
| 109 |
</pre> |
| 110 |
<p>The first line shows how many heap blocks and bytes were allocated over |
| 111 |
the entire execution.</p> |
| 112 |
<p>The second line shows how many heap blocks and bytes were alive at |
| 113 |
<code class="computeroutput">t-gmax</code>, i.e. the time when the heap size |
| 114 |
reached its global maximum (as measured in bytes).</p> |
| 115 |
<p>The third line shows how many heap blocks and bytes were alive at |
| 116 |
<code class="computeroutput">t-end</code>, i.e. the end of execution. In other |
| 117 |
words, how many blocks and bytes were not explicitly freed. </p> |
| 118 |
<p>The fourth and fifth lines show how many bytes within heap blocks were |
| 119 |
read and written during the entire execution. </p> |
| 120 |
<p>These lines are moderately interesting at best. More useful information |
| 121 |
can be seen with DHAT's viewer.</p> |
| 122 |
</div> |
| 123 |
<div class="sect2"> |
| 124 |
<div class="titlepage"><div><div><h3 class="title"> |
| 125 |
<a name="dh-manual.outputfile"></a>10.2.2. Output File</h3></div></div></div> |
| 126 |
<p>As well as printing summary information, DHAT also writes more detailed |
| 127 |
profiling information to a file. By default this file is named |
| 128 |
<code class="filename">dhat.out.<pid></code> (where |
| 129 |
<code class="filename"><pid></code> is the program's process ID), but its name can |
| 130 |
be changed with the <code class="option">--dhat-out-file</code> option. This file is JSON, |
| 131 |
and intended to be viewed by DHAT's viewer, which is described in the next |
| 132 |
section.</p> |
| 133 |
<p>The default <code class="computeroutput">.<pid></code> suffix on the |
| 134 |
output file name serves two purposes. Firstly, it means you don't have to |
| 135 |
rename old log files that you don't want to overwrite. Secondly, and more |
| 136 |
importantly, it allows correct profiling with the |
| 137 |
<code class="option">--trace-children=yes</code> option of programs that spawn child |
| 138 |
processes.</p> |
| 139 |
<p>The output file can be big, many megabytes for large applications |
| 140 |
built with full debugging information.</p> |
| 141 |
</div> |
| 142 |
</div> |
| 143 |
<div class="sect1"> |
| 144 |
<div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
| 145 |
<a name="dh-manual.viewer"></a>10.3. DHAT's Viewer</h2></div></div></div> |
| 146 |
<p>DHAT's viewer can be run in a web browser by loading the file |
| 147 |
<code class="computeroutput">dh_view.html</code>. Use the "Load" button to choose |
| 148 |
a DHAT output file to view.</p> |
| 149 |
<p>If loading takes a long time, it might be worth re-running DHAT with a |
| 150 |
smaller <code class="option">--num-callers</code> value to reduce the stack depths, |
| 151 |
because this can significantly reduce the size of DHAT's output files.</p> |
| 152 |
<div class="sect2"> |
| 153 |
<div class="titlepage"><div><div><h3 class="title"> |
| 154 |
<a name="dh-output-header"></a>10.3.1. The Output Header</h3></div></div></div> |
| 155 |
<p>The first part of the output shows the mode, program command and process |
| 156 |
ID. For example:</p> |
| 157 |
<pre class="programlisting"> |
| 158 |
Invocation { |
| 159 |
Mode: heap |
| 160 |
Command: /home/njn/moz/rust0/build/x86_64-unknown-linux-gnu/stage2/bin/rustc --crate-name tuple_stress src/main.rs |
| 161 |
PID: 18816 |
| 162 |
} |
| 163 |
</pre> |
| 164 |
<p>The second part of the output shows the |
| 165 |
<code class="computeroutput">t-gmax</code> and |
| 166 |
<code class="computeroutput">t-end</code> values again. For example:</p> |
| 167 |
<pre class="programlisting"> |
| 168 |
Times { |
| 169 |
t-gmax: 8,138,210,673 instrs (86.92% of program duration) |
| 170 |
t-end: 9,362,544,994 instrs |
| 171 |
} |
| 172 |
</pre> |
| 173 |
</div> |
| 174 |
<div class="sect2"> |
| 175 |
<div class="titlepage"><div><div><h3 class="title"> |
| 176 |
<a name="dh-ap-tree"></a>10.3.2. The PP Tree</h3></div></div></div> |
| 177 |
<p>The third part of the output is the largest and most interesting part, |
| 178 |
showing the program point (PP) tree.</p> |
| 179 |
<div class="sect3"> |
| 180 |
<div class="titlepage"><div><div><h4 class="title"> |
| 181 |
<a name="dh-structure"></a>10.3.2.1. Structure</h4></div></div></div> |
| 182 |
<p>The following image shows a screenshot of part of a PP |
| 183 |
tree. The font is very small because this screenshot is intended to |
| 184 |
demonstrate the high-level structure of the tree rather than the |
| 185 |
details within the text. (It is also slightly out-of-date, and doesn't quite |
| 186 |
match the current output produced by DHAT's viewer.)</p> |
| 187 |
<div><img src="images/dh-tree.png"></div> |
| 188 |
<p>Like any tree, it has a root node, leaf nodes, and non-leaf nodes. The |
| 189 |
structure of the tree is shown by the lines connecting nodes. Child nodes are |
| 190 |
beneath their parent and indented one level.</p> |
| 191 |
<p>The sub-trees beneath a non-leaf node can be collapsed or expanded by |
| 192 |
clicking on the node. It is useful to collapse sub-trees that you aren't |
| 193 |
interested in.</p> |
| 194 |
<p>Colours are meaningful, and are intended to ease tree navigation, but the |
| 195 |
information they represent is also present within the text. (This means that |
| 196 |
colour-blind users are not denied any information.)</p> |
| 197 |
<p>Each leaf node is coloured green. Each non-leaf node is coloured blue |
| 198 |
and has a down arrow (<code class="computeroutput">▼</code>) next to it when |
| 199 |
its sub-tree is expanded. Each non-leaf node is coloured yellow and has a |
| 200 |
left arrow (<code class="computeroutput">▶</code>) next to it when its sub-tree |
| 201 |
is collapsed.</p> |
| 202 |
<p>The shade of green, blue or yellow used for a node indicate its |
| 203 |
significance. Darker shades represent greater significance (in terms of bytes |
| 204 |
or blocks).</p> |
| 205 |
<p>Note that the entire output is text, even the arrows and lines connecting |
| 206 |
nodes. This means you can copy and paste any part of the output easily into an |
| 207 |
email, bug report, etc.</p> |
| 208 |
</div> |
| 209 |
<div class="sect3"> |
| 210 |
<div class="titlepage"><div><div><h4 class="title"> |
| 211 |
<a name="dh-root-node"></a>10.3.2.2. The Root Node</h4></div></div></div> |
| 212 |
<p>The root node looks like this:</p> |
| 213 |
<pre class="programlisting"> |
| 214 |
PP 1/1 (25 children) { |
| 215 |
Total: 1,355,253,987 bytes (100%, 67,454.81/Minstr) in 5,943,417 blocks (100%, 295.82/Minstr), avg size 228.03 bytes, avg lifetime 3,134,692,250.67 instrs (15.6% of program duration) |
| 216 |
At t-gmax: 423,930,307 bytes (100%) in 1,575,682 blocks (100%), avg size 269.05 bytes |
| 217 |
At t-end: 258,002 bytes (100%) in 2,129 blocks (100%), avg size 121.18 bytes |
| 218 |
Reads: 5,478,606,988 bytes (100%, 272,685.7/Minstr), 4.04/byte |
| 219 |
Writes: 2,040,294,800 bytes (100%, 101,551.22/Minstr), 1.51/byte |
| 220 |
Allocated at { |
| 221 |
#0: [root] |
| 222 |
} |
| 223 |
} |
| 224 |
</pre> |
| 225 |
<p>The root node covers the entire execution. The information is a superset |
| 226 |
of the information shown when DHAT ran, adding details such as allocation |
| 227 |
rates, average block sizes, block lifetimes, and read and write ratios. The |
| 228 |
next example will explain these in more detail.</p> |
| 229 |
</div> |
| 230 |
<div class="sect3"> |
| 231 |
<div class="titlepage"><div><div><h4 class="title"> |
| 232 |
<a name="dh-interior-nodes"></a>10.3.2.3. Interior Nodes</h4></div></div></div> |
| 233 |
<p>PP nodes further down the tree show information about a subset of |
| 234 |
allocations. For example:</p> |
| 235 |
<pre class="programlisting"> |
| 236 |
PP 1.1/25 (2 children) { |
| 237 |
Total: 54,533,440 bytes (4.02%, 2,714.28/Minstr) in 458,839 blocks (7.72%, 22.84/Minstr), avg size 118.85 bytes, avg lifetime 1,127,259,403.64 instrs (5.61% of program duration) |
| 238 |
At t-gmax: 0 bytes (0%) in 0 blocks (0%), avg size 0 bytes |
| 239 |
At t-end: 0 bytes (0%) in 0 blocks (0%), avg size 0 bytes |
| 240 |
Reads: 15,993,012 bytes (0.29%, 796.02/Minstr), 0.29/byte |
| 241 |
Writes: 20,974,752 bytes (1.03%, 1,043.97/Minstr), 0.38/byte |
| 242 |
Allocated at { |
| 243 |
#1: 0x95CACC9: alloc (alloc.rs:72) |
| 244 |
#2: 0x95CACC9: alloc (alloc.rs:148) |
| 245 |
#3: 0x95CACC9: reserve_internal<syntax::tokenstream::TokenStream,alloc::alloc::Global> (raw_vec.rs:669) |
| 246 |
#4: 0x95CACC9: reserve<syntax::tokenstream::TokenStream,alloc::alloc::Global> (raw_vec.rs:492) |
| 247 |
#5: 0x95CACC9: reserve<syntax::tokenstream::TokenStream> (vec.rs:460) |
| 248 |
#6: 0x95CACC9: push<syntax::tokenstream::TokenStream> (vec.rs:989) |
| 249 |
#7: 0x95CACC9: parse_token_trees_until_close_delim (tokentrees.rs:27) |
| 250 |
#8: 0x95CACC9: syntax::parse::lexer::tokentrees::<impl syntax::parse::lexer::StringReader<'a>>::parse_token_tree (tokentrees.rs:81) |
| 251 |
} |
| 252 |
} |
| 253 |
</pre> |
| 254 |
<p>The first line indicates the node's position in the tree. The |
| 255 |
<code class="computeroutput">1.1</code> is a unique identifier for the node and |
| 256 |
also says that it is the first child node <code class="computeroutput">1</code> |
| 257 |
(which is the root). The <code class="computeroutput">/25</code> says that it is |
| 258 |
one of 25 children, i.e. it has 24 siblings. The <code class="computeroutput">(2 |
| 259 |
children)</code> says that this node node has two children of its |
| 260 |
own.</p> |
| 261 |
<p>Allocations are aggregated by their allocation stack trace. The |
| 262 |
<code class="computeroutput">Allocated at</code> section shows the allocation |
| 263 |
stack trace that is shared by all the blocks covered by this node.</p> |
| 264 |
<p>The <code class="computeroutput">Total</code> line shows that this node |
| 265 |
accounts for 4.02% of all bytes allocated during execution, and 7.72% of all |
| 266 |
blocks. These percentages are useful for comparing the significance of |
| 267 |
different nodes within a single profile; a PP that accounts for 10% of bytes |
| 268 |
allocated is likely to be more interesting than one that accounts for |
| 269 |
2%.</p> |
| 270 |
<p>The <code class="computeroutput">Total</code> line also shows allocation |
| 271 |
rates, measured in bytes and blocks per million instructions. These rates are |
| 272 |
useful for comparing the significance of nodes across profiles made with |
| 273 |
different workloads.</p> |
| 274 |
<p>Finally, the <code class="computeroutput">Total</code> line shows the |
| 275 |
average size and lifetimes of these blocks.</p> |
| 276 |
<p>The <code class="computeroutput">At t-gmax</code> line says shows that no |
| 277 |
blocks from this PP were alive when the global heap peak occurred. In other |
| 278 |
words, these blocks do not contribute at all to the global heap peak.</p> |
| 279 |
<p>The <code class="computeroutput">At t-end</code> line shows that no blocks |
| 280 |
were from this PP were alive at shutdown. In other words, all those blocks were |
| 281 |
explicitly freed before termination.</p> |
| 282 |
<p>The <code class="computeroutput">Reads</code> and |
| 283 |
<code class="computeroutput">Writes</code> lines show how many bytes were read |
| 284 |
within this PP's blocks, the fraction this represents of all heap reads, and |
| 285 |
the read rate. Finally, it shows the read ratio, which is the number of reads |
| 286 |
per byte. In this case the number is 0.29, which is quite low -- if no byte was |
| 287 |
read twice, then only 29% of the allocated bytes, which means that at least 71% |
| 288 |
of the bytes were never read! This suggests that the blocks are being |
| 289 |
underutilized and might be worth optimizing.</p> |
| 290 |
<p>The <code class="computeroutput">Writes</code> lines is similar to the |
| 291 |
<code class="computeroutput">Reads</code> line. In this case, at most 38% of the |
| 292 |
bytes are ever written, and at least 62% of the bytes were never written. |
| 293 |
</p> |
| 294 |
<p>The <code class="computeroutput">Reads</code> and |
| 295 |
<code class="computeroutput">Writes</code> measurements suggest that the blocks |
| 296 |
are being under-utilised and might be worth optimizing. Having said that, this |
| 297 |
kind of under-utilisation is common in data structures that grow, such as |
| 298 |
vectors and hash tables, and isn't always fixable. </p> |
| 299 |
</div> |
| 300 |
<div class="sect3"> |
| 301 |
<div class="titlepage"><div><div><h4 class="title"> |
| 302 |
<a name="dh-leaf-nodes"></a>10.3.2.4. Leaf Nodes</h4></div></div></div> |
| 303 |
<p>This is a leaf node:</p> |
| 304 |
<pre class="programlisting"> |
| 305 |
PP 1.1.1.1/2 { |
| 306 |
Total: 31,460,928 bytes (2.32%, 1,565.9/Minstr) in 262,171 blocks (4.41%, 13.05/Minstr), avg size 120 bytes, avg lifetime 986,406,885.05 instrs (4.91% of program duration) |
| 307 |
Max: 16,779,136 bytes in 65,543 blocks, avg size 256 bytes |
| 308 |
At t-gmax: 0 bytes (0%) in 0 blocks (0%), avg size 0 bytes |
| 309 |
At t-end: 0 bytes (0%) in 0 blocks (0%), avg size 0 bytes |
| 310 |
Reads: 5,964,704 bytes (0.11%, 296.88/Minstr), 0.19/byte |
| 311 |
Writes: 10,487,200 bytes (0.51%, 521.98/Minstr), 0.33/byte |
| 312 |
Allocated at { |
| 313 |
^1: 0x95CACC9: alloc (alloc.rs:72) |
| 314 |
^2: 0x95CACC9: alloc (alloc.rs:148) |
| 315 |
^3: 0x95CACC9: reserve_internal<syntax::tokenstream::TokenStream,alloc::alloc::Global> (raw_vec.rs:669) |
| 316 |
^4: 0x95CACC9: reserve<syntax::tokenstream::TokenStream,alloc::alloc::Global> (raw_vec.rs:492) |
| 317 |
^5: 0x95CACC9: reserve<syntax::tokenstream::TokenStream> (vec.rs:460) |
| 318 |
^6: 0x95CACC9: push<syntax::tokenstream::TokenStream> (vec.rs:989) |
| 319 |
^7: 0x95CACC9: parse_token_trees_until_close_delim (tokentrees.rs:27) |
| 320 |
^8: 0x95CACC9: syntax::parse::lexer::tokentrees::<impl syntax::parse::lexer::StringReader<'a>>::parse_token_tree (tokentrees.rs:81) |
| 321 |
^9: 0x95CAC39: parse_token_trees_until_close_delim (tokentrees.rs:26) |
| 322 |
^10: 0x95CAC39: syntax::parse::lexer::tokentrees::<impl syntax::parse::lexer::StringReader<'a>>::parse_token_tree (tokentrees.rs:81) |
| 323 |
#11: 0x95CAC39: parse_token_trees_until_close_delim (tokentrees.rs:26) |
| 324 |
#12: 0x95CAC39: syntax::parse::lexer::tokentrees::<impl syntax::parse::lexer::StringReader<'a>>::parse_token_tree (tokentrees.rs:81) |
| 325 |
} |
| 326 |
} |
| 327 |
</pre> |
| 328 |
<p>The <code class="computeroutput">1.1.1.1/2</code> indicates that this node |
| 329 |
is a great-grandchild of the root; is the first grandchild of the node in the |
| 330 |
previous example; and has no children.</p> |
| 331 |
<p>Leaf nodes contain an additional <code class="computeroutput">Max</code> |
| 332 |
line, indicating the peak memory use for the blocks covered by this PP. (This |
| 333 |
peak may have occurred at a time other than |
| 334 |
<code class="computeroutput">t-gmax</code>.) In this case, 31,460,298 bytes were |
| 335 |
allocated from this PP, but the maximum size alive at once was 16,779,136 |
| 336 |
bytes.</p> |
| 337 |
<p>Stack frames that begin with a <code class="computeroutput">^</code> rather |
| 338 |
than a <code class="computeroutput">#</code> are copied from ancestor nodes. |
| 339 |
(In this example, the first 8 frames are identical to those from the node in |
| 340 |
the previous example.) These frames could be found by tracing back through |
| 341 |
ancestor nodes, but that can be annoying, which is why they are duplicated. |
| 342 |
This also means that each node makes complete sense on its own.</p> |
| 343 |
</div> |
| 344 |
<div class="sect3"> |
| 345 |
<div class="titlepage"><div><div><h4 class="title"> |
| 346 |
<a name="dh-access-counts"></a>10.3.2.5. Access Counts</h4></div></div></div> |
| 347 |
<p>If all blocks covered by a PP node have the same size, an additional |
| 348 |
<code class="computeroutput">Accesses</code> field will be present. It indicates |
| 349 |
how the reads and writes within these blocks were distributed. For |
| 350 |
example:</p> |
| 351 |
<pre class="programlisting"> |
| 352 |
Total: 8,388,672 bytes (0.62%, 417.53/Minstr) in 262,146 blocks (4.41%, 13.05/Minstr), avg size 32 bytes, avg lifetime 16,726,078,401.51 instrs (83.25% of program duration) |
| 353 |
At t-gmax: 8,388,672 bytes (1.98%) in 262,146 blocks (16.64%), avg size 32 bytes |
| 354 |
At t-end: 0 bytes (0%) in 0 blocks (0%), avg size 0 bytes |
| 355 |
Reads: 9,109,682 bytes (0.17%, 453.41/Minstr), 1.09/byte |
| 356 |
Writes: 7,340,088 bytes (0.36%, 365.34/Minstr), 0.88/byte |
| 357 |
Accesses: { |
| 358 |
[ 0] 65547 7 8 4 65529 〃 〃 〃 16 〃 〃 〃 12 〃 〃 〃 〃 〃 〃 〃 〃 〃 〃 〃 65542 〃 〃 〃 - - - - |
| 359 |
} |
| 360 |
</pre> |
| 361 |
<p>Every block covered by this PP was 32 bytes. Within all of those blocks, |
| 362 |
byte 0 was accessed (read or written) 65,547 times, byte 1 was accessed 7 |
| 363 |
times, byte 2 was accessed 8 times, and so on.</p> |
| 364 |
<p>The ditto symbol (<code class="computeroutput">〃</code>) means "same access |
| 365 |
count as the previous byte".</p> |
| 366 |
<p>A dash (<code class="computeroutput">-</code>) means "zero". (It is used |
| 367 |
instead of <code class="computeroutput">0</code> because it makes unaccessed |
| 368 |
regions more easily identifiable.)</p> |
| 369 |
<p>The infinity symbol (<code class="computeroutput">∞</code>, not present in |
| 370 |
this example) means "exceeded the maximum tracked count".</p> |
| 371 |
<p>Block layout can often be inferred from counts. For example, these blocks |
| 372 |
probably have four separate byte-sized fields, followed by a four-byte field, |
| 373 |
and so on.</p> |
| 374 |
<p>The size of the blocks that measure and display access counts is limited |
| 375 |
to 1024 bytes. This is done to limit the performance overhead and also to keep |
| 376 |
the size of the generated output reasonable. However, it is possible to override |
| 377 |
this limit using client requests. The use-case for this is to first run DHAT |
| 378 |
normally, and then identify any large blocks that you would like to further |
| 379 |
investigate with access count histograms. The client request is declared in |
| 380 |
<code class="filename">dhat/dhat.h</code> and is called <code class="computeroutput">DHAT_HISTOGRAM_MEMORY</code>. |
| 381 |
The macro should be placed immediately after the call to the allocator, |
| 382 |
and use the pointer returned by the allocator.</p> |
| 383 |
<pre class="programlisting"> |
| 384 |
// LargeStruct bigger than 1024 bytes |
| 385 |
struct LargeStruct* ls = malloc(sizeof(struct LargeStruct)); |
| 386 |
DHAT_HISTOGRAM_MEMORY(ls); |
| 387 |
</pre> |
| 388 |
<p>The memory that can be profiled in this way with user requests |
| 389 |
has a further upper limit of 25kbytes. Be aware that the access counts |
| 390 |
will all be set to zero. This means that the access counts will not |
| 391 |
include any reads or writes performed during initialisation. An example where this |
| 392 |
will happen are uses of C++ <code class="computeroutput">new</code> with user-defined constructors.</p> |
| 393 |
<p>Access counts can be useful for identifying data alignment holes or other |
| 394 |
layout inefficiencies.</p> |
| 395 |
</div> |
| 396 |
<div class="sect3"> |
| 397 |
<div class="titlepage"><div><div><h4 class="title"> |
| 398 |
<a name="aggregate-nodes"></a>10.3.2.6. Aggregate Nodes</h4></div></div></div> |
| 399 |
<p>The PP tree is very large and many nodes represent tiny numbers of blocks |
| 400 |
and bytes. Therefore, DHAT's viewer aggregates insignificant nodes like |
| 401 |
this:</p> |
| 402 |
<pre class="programlisting"> |
| 403 |
PP 1.14.2/2 { |
| 404 |
Total: 5,175 blocks (0.09%, 0.26/Minstr) |
| 405 |
Allocated at { |
| 406 |
[5 insignificant] |
| 407 |
} |
| 408 |
} |
| 409 |
</pre> |
| 410 |
<p>Much of the detail is stripped away, leaving only basic measurements, |
| 411 |
along with an indication of how many nodes were aggregated together (5 in this |
| 412 |
case).</p> |
| 413 |
</div> |
| 414 |
</div> |
| 415 |
<div class="sect2"> |
| 416 |
<div class="titlepage"><div><div><h3 class="title"> |
| 417 |
<a name="dh-output-footer"></a>10.3.3. The Output Footer</h3></div></div></div> |
| 418 |
<p>Below the PP tree is a line like this:</p> |
| 419 |
<pre class="programlisting"> |
| 420 |
PP significance threshold: total >= 59,434.17 blocks (1%) |
| 421 |
</pre> |
| 422 |
<p>It shows the function used to determine if a PP node is significant. All |
| 423 |
nodes that don't satisfy this function are aggregated. It is occasionally |
| 424 |
useful if you don't understand why a PP node has been aggregated. The exact |
| 425 |
threshold depends on the sort metric (see below).</p> |
| 426 |
<p>Finally, the bottom of the page shows a legend that explains some of the |
| 427 |
terms, abbreviations and symbols used in the output.</p> |
| 428 |
</div> |
| 429 |
<div class="sect2"> |
| 430 |
<div class="titlepage"><div><div><h3 class="title"> |
| 431 |
<a name="dh-sort-metrics"></a>10.3.4. Sort Metrics</h3></div></div></div> |
| 432 |
<p>The order in which sub-trees are sorted can be changed via the "Sort |
| 433 |
metric" drop-down menu at the top of DHAT's viewer. Different sort metrics can |
| 434 |
be useful for finding different things. Some sort metrics also incorporate some |
| 435 |
filtering, so that only nodes meeting a particular criteria are shown.</p> |
| 436 |
<div class="variablelist"><dl class="variablelist"> |
| 437 |
<dt><span class="term">Total (bytes)</span></dt> |
| 438 |
<dd><p>The total number of bytes allocated during the execution. |
| 439 |
Highly useful for evaluating heap churn, though not quite as useful as |
| 440 |
"Total (blocks)". |
| 441 |
</p></dd> |
| 442 |
<dt><span class="term">Total (blocks)</span></dt> |
| 443 |
<dd><p>The total number of blocks allocated during the execution. |
| 444 |
Highly useful for evaluating heap churn; reducing the number of calls to |
| 445 |
the allocator can significantly speed up a program. This is the default |
| 446 |
sort metric. |
| 447 |
</p></dd> |
| 448 |
<dt><span class="term">Total (blocks), tiny</span></dt> |
| 449 |
<dd><p>Like "Total (blocks)", but shows only very small blocks. |
| 450 |
Moderately useful, because such blocks are often easy to avoid allocating. |
| 451 |
</p></dd> |
| 452 |
<dt><span class="term">Total (blocks), short-lived</span></dt> |
| 453 |
<dd><p>Like "Total (blocks)", but shows only very short-lived |
| 454 |
blocks. Moderately useful, because such blocks are often easy to avoid |
| 455 |
allocating. |
| 456 |
</p></dd> |
| 457 |
<dt><span class="term">Total (bytes), zero reads or zero writes</span></dt> |
| 458 |
<dd><p>Like "Total (bytes)", but shows only blocks that are |
| 459 |
never read or never written to (or both). Highly useful, because such |
| 460 |
blocks indicate poor use of memory and are often easy to avoid allocating. |
| 461 |
For example, sometimes a block is allocated and written to but then only |
| 462 |
read if a condition C is true; in that case, it may be possible to delay |
| 463 |
creating the block until condition C is true. Alternatively, sometimes |
| 464 |
blocks are created and never used; such blocks are trivial to remove. |
| 465 |
</p></dd> |
| 466 |
<dt><span class="term">Total (blocks), zero reads or zero writes</span></dt> |
| 467 |
<dd><p>Like "Total (bytes), zero reads or zero writes" but for |
| 468 |
blocks. Highly useful. |
| 469 |
</p></dd> |
| 470 |
<dt><span class="term">Total (bytes), low-access</span></dt> |
| 471 |
<dd><p>Like "Total (bytes)", but shows only blocks that have low |
| 472 |
numbers of reads or low numbers of writes (or both). Moderately useful, |
| 473 |
because such blocks indicate poor use of memory. |
| 474 |
</p></dd> |
| 475 |
<dt><span class="term">Total (blocks), low-access</span></dt> |
| 476 |
<dd><p>Like "Total (bytes), low-access", but for blocks. |
| 477 |
</p></dd> |
| 478 |
<dt><span class="term">At t-gmax (bytes)</span></dt> |
| 479 |
<dd><p>This shows the breakdown of memory at the point of peak |
| 480 |
heap memory usage. Highly useful for reducing peak memory usage. |
| 481 |
</p></dd> |
| 482 |
<dt><span class="term">At t-end (bytes)</span></dt> |
| 483 |
<dd><p>This shows the breakdown of memory at program termination. |
| 484 |
Highly useful for identifying process-lifetime leaks. |
| 485 |
</p></dd> |
| 486 |
<dt><span class="term">Reads (bytes)</span></dt> |
| 487 |
<dd><p>The number of bytes read within heap blocks. Occasionally |
| 488 |
useful. |
| 489 |
</p></dd> |
| 490 |
<dt><span class="term">Reads (bytes), high-access</span></dt> |
| 491 |
<dd><p>Like "Reads (bytes)", but only shows blocks with high read |
| 492 |
ratios. Occasionally useful for identifying hot areas of memory. |
| 493 |
</p></dd> |
| 494 |
<dt><span class="term">Writes (bytes)</span></dt> |
| 495 |
<dd><p>Like "Reads (bytes)", but for writes. Occasionally useful. |
| 496 |
</p></dd> |
| 497 |
<dt><span class="term">Writes (bytes), high-access</span></dt> |
| 498 |
<dd><p>Like "Reads (bytes), high-access", but for writes. |
| 499 |
Occasionally useful. |
| 500 |
</p></dd> |
| 501 |
</dl></div> |
| 502 |
<p>The values within a node that represent the chosen sort metric are shown |
| 503 |
in bold, so they stand out.</p> |
| 504 |
<p>Here is part of a PP node found with "Total (blocks), tiny", showing |
| 505 |
blocks with an average size of only 8.67 bytes:</p> |
| 506 |
<pre class="programlisting"> |
| 507 |
Total: 3,407,848 bytes (0.25%, 169.62/Minstr) in 393,214 blocks (6.62%, 19.57/Minstr), avg size 8.67 bytes, avg lifetime 1,167,795,629.1 instrs (5.81% of program duration) |
| 508 |
</pre> |
| 509 |
<p>Here is part of a PP node found with "Total (blocks), short-lived", |
| 510 |
showing blocks with an average lifetime of only 181.75 instructions:</p> |
| 511 |
<pre class="programlisting"> |
| 512 |
Total: 23,068,584 bytes (1.7%, 1,148.19/Minstr) in 262,143 blocks (4.41%, 13.05/Minstr), avg size 88 bytes, avg lifetime 181.75 instrs (0% of program duration) |
| 513 |
</pre> |
| 514 |
<p>Here is an example of a PP identified with "Total (blocks), zero reads |
| 515 |
or zero writes", showing blocks that are allocated but never touched:</p> |
| 516 |
<pre class="programlisting"> |
| 517 |
Total: 7,339,920 bytes (0.54%, 365.33/Minstr) in 262,140 blocks (4.41%, 13.05/Minstr), avg size 28 bytes, avg lifetime 1,141,103,997.69 instrs (5.68% of program duration) |
| 518 |
Max: 3,669,960 bytes in 131,070 blocks, avg size 28 bytes |
| 519 |
At t-gmax: 3,336,400 bytes (0.79%) in 119,157 blocks (7.56%), avg size 28 bytes |
| 520 |
At t-end: 0 bytes (0%) in 0 blocks (0%), avg size 0 bytes |
| 521 |
Reads: 0 bytes (0%, 0/Minstr), 0/byte |
| 522 |
Writes: 0 bytes (0%, 0/Minstr), 0/byte |
| 523 |
</pre> |
| 524 |
<p>All the blocks identified by these PPs are good candidates for |
| 525 |
optimization.</p> |
| 526 |
</div> |
| 527 |
</div> |
| 528 |
<div class="sect1"> |
| 529 |
<div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
| 530 |
<a name="dh-manual.realloc"></a>10.4. Treatment of realloc</h2></div></div></div> |
| 531 |
<p><code class="computeroutput">realloc</code> is a tricky function and there |
| 532 |
are several different ways that DHAT could handle it.</p> |
| 533 |
<p>Imagine a <code class="computeroutput">malloc(100)</code> call followed by |
| 534 |
a <code class="computeroutput">realloc(200)</code> call. This combination is |
| 535 |
considered to add two to the total block count, and 300 bytes to the total |
| 536 |
bytes count. (An alternative would be to only add one to the total block |
| 537 |
count, and 200 bytes to the total bytes count, as if a single |
| 538 |
<code class="computeroutput">malloc(200)</code> call had occurred. While this |
| 539 |
would be defensible from a semantic point of view, it is silly from an |
| 540 |
operational point of view, because making two calls to allocator functions is |
| 541 |
more expensive than one call, and DHAT is a profiler that aims to help with |
| 542 |
runtime costs.)</p> |
| 543 |
<p>Furthermore, the implicit copying of the 100 bytes is added to the reads |
| 544 |
and writes counts. Without this, the read and write counts would be |
| 545 |
under-measured and misleading.</p> |
| 546 |
<p>However, DHAT only increases the current heap size by 100 bytes for this |
| 547 |
combination, and does not change the current block count. (As opposed to |
| 548 |
increasing the current heap size by 200 bytes and then decreasing it by 100 |
| 549 |
bytes.) As a result, it can only increase the global heap peak (if indeed, |
| 550 |
this results in a new peak) by 100 bytes.</p> |
| 551 |
<p>Finally, the program point assigned to the block allocated by the |
| 552 |
<code class="computeroutput">malloc(100)</code> call is retained once the block |
| 553 |
is reallocated. Which means that all 300 bytes are attributed to that |
| 554 |
program point, and no separate program point is created for the |
| 555 |
<code class="computeroutput">realloc(200)</code> call. This may be surprising, |
| 556 |
but it has one large benefit.</p> |
| 557 |
<p>Imagine some code that starts with an empty buffer, and then gradually |
| 558 |
adds data to that buffer from numerous different points in the code, |
| 559 |
reallocating the buffer each time it gets full. (E.g. code generation in a |
| 560 |
compiler might work this way.) With the described approach, the first heap |
| 561 |
block and all subsequent heap blocks are attributed to the same program point. |
| 562 |
While this is something of a lie -- the first program point isn't actually |
| 563 |
responsible for the other allocations -- it is arguably better than having the |
| 564 |
program points spread around in a distribution that unpredictably depends on |
| 565 |
whenever the reallocations were triggered.</p> |
| 566 |
</div> |
| 567 |
<div class="sect1"> |
| 568 |
<div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
| 569 |
<a name="dh-manual.copy-profiling"></a>10.5. Copy profiling</h2></div></div></div> |
| 570 |
<p>If DHAT is invoked with <code class="option">--mode=copy</code>, instead of |
| 571 |
profiling heap operations (allocations and deallocations), it profiles copy |
| 572 |
operations, such as <code class="computeroutput">memcpy</code>, |
| 573 |
<code class="computeroutput">memmove</code>, |
| 574 |
<code class="computeroutput">strcpy</code>, and |
| 575 |
<code class="computeroutput">bcopy</code>. This is sometimes useful.</p> |
| 576 |
<p>Here is an example PP node from this mode:</p> |
| 577 |
<pre class="programlisting"> |
| 578 |
PP 1.1.2/5 (4 children) { |
| 579 |
Total: 1,210,925 bytes (10.03%, 4,358.66/Minstr) in 112,717 blocks (35.2%, 405.72/Minstr), avg size 10.74 bytes |
| 580 |
Copied at { |
| 581 |
^1: 0x4842524: memmove (vg_replace_strmem.c:1289) |
| 582 |
#2: 0x1F0A0D: copy_nonoverlapping<u8> (intrinsics.rs:1858) |
| 583 |
#3: 0x1F0A0D: copy_from_slice<u8> (mod.rs:2524) |
| 584 |
#4: 0x1F0A0D: spec_extend<u8> (vec.rs:2227) |
| 585 |
#5: 0x1F0A0D: extend_from_slice<u8> (vec.rs:1619) |
| 586 |
#6: 0x1F0A0D: push_str (string.rs:821) |
| 587 |
#7: 0x1F0A0D: write_str (string.rs:2418) |
| 588 |
#8: 0x1F0A0D: <&mut W as core::fmt::Write>::write_str (mod.rs:195) |
| 589 |
} |
| 590 |
} |
| 591 |
</pre> |
| 592 |
<p>It is very similar to the PP nodes for heap profiling, but with less |
| 593 |
information, because copy profiling doesn't involve any tracking of memory |
| 594 |
regions with lifetimes.</p> |
| 595 |
</div> |
| 596 |
<div class="sect1"> |
| 597 |
<div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
| 598 |
<a name="dh-manual.ad-hoc-profiling"></a>10.6. Ad hoc profiling</h2></div></div></div> |
| 599 |
<p>If DHAT is invoked with <code class="option">--mode=ad-hoc</code>, instead of |
| 600 |
profiling heap operations (allocations and deallocations), it profiles calls to |
| 601 |
the <code class="computeroutput">DHAT_AD_HOC_EVENT</code> client request, which is |
| 602 |
declared in <code class="filename">dhat/dhat.h</code>.</p> |
| 603 |
<p>Here is an example PP node from this mode:</p> |
| 604 |
<pre class="programlisting"> |
| 605 |
PP 1.1.1.1/2 { |
| 606 |
Total: 30 units (17.65%, 115.97/Minstr) in 1 events (14.29%, 3.87/Minstr), avg size 30 units |
| 607 |
Occurred at { |
| 608 |
^1: 0x109407: g (ad-hoc.c:4) |
| 609 |
^2: 0x109425: f (ad-hoc.c:8) |
| 610 |
#3: 0x109497: main (ad-hoc.c:14) |
| 611 |
} |
| 612 |
} |
| 613 |
</pre> |
| 614 |
<p>This kind of profiling is useful when you know a code path is hot but you |
| 615 |
want to know more about it.</p> |
| 616 |
<p>For example, you might want to know which callsites of a hot function |
| 617 |
account for most of the calls. You could put a |
| 618 |
<code class="computeroutput">DHAT_AD_HOC_EVENT(1);</code> call at the start of |
| 619 |
that function.</p> |
| 620 |
<p>Alternatively, you might want to know the typical length of a vector in a |
| 621 |
hot location. You could put a |
| 622 |
<code class="computeroutput">DHAT_AD_HOC_EVENT(len);</code> call at the |
| 623 |
appropriate location, when <code class="computeroutput">len</code> is the length |
| 624 |
of the vector.</p> |
| 625 |
</div> |
| 626 |
<div class="sect1"> |
| 627 |
<div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
| 628 |
<a name="dh-manual.options"></a>10.7. DHAT Command-line Options</h2></div></div></div> |
| 629 |
<p>DHAT-specific command-line options are:</p> |
| 630 |
<div class="variablelist"> |
| 631 |
<a name="dh.opts.list"></a><dl class="variablelist"> |
| 632 |
<dt> |
| 633 |
<a name="opt.dhat-out-file"></a><span class="term"> |
| 634 |
<code class="option">--dhat-out-file=<file> </code> |
| 635 |
</span> |
| 636 |
</dt> |
| 637 |
<dd><p>Write the profile data to |
| 638 |
<code class="computeroutput">file</code> rather than to the default |
| 639 |
output file, |
| 640 |
<code class="filename">dhat.out.<pid></code>. The |
| 641 |
<code class="option">%p</code> and <code class="option">%q</code> format specifiers |
| 642 |
can be used to embed the process ID and/or the contents of an |
| 643 |
environment variable in the name, as is the case for the core |
| 644 |
option <code class="option"><a class="link" href="manual-core.html#opt.log-file">--log-file</a></code>. |
| 645 |
</p></dd> |
| 646 |
<dt> |
| 647 |
<a name="opt.mode"></a><span class="term"> |
| 648 |
<code class="option">--mode=<heap|copy|ad-hoc> [default: heap] </code> |
| 649 |
</span> |
| 650 |
</dt> |
| 651 |
<dd><p>The profiling mode: heap profiling, copy profiling, or ad hoc |
| 652 |
profiling. |
| 653 |
</p></dd> |
| 654 |
</dl> |
| 655 |
</div> |
| 656 |
<p>Note that stacks by default have 12 frames. This may be more than |
| 657 |
necessary, in which case the <code class="option">--num-callers</code> flag can be used to |
| 658 |
reduce the number, which may make DHAT run slightly faster. |
| 659 |
</p> |
| 660 |
</div> |
| 661 |
</div> |
| 662 |
<div> |
| 663 |
<br><table class="nav" width="100%" cellspacing="3" cellpadding="2" border="0" summary="Navigation footer"> |
| 664 |
<tr> |
| 665 |
<td rowspan="2" width="40%" align="left"> |
| 666 |
<a accesskey="p" href="ms-manual.html"><< 9. Massif: a heap profiler</a> </td> |
| 667 |
<td width="20%" align="center"><a accesskey="u" href="manual.html">Up</a></td> |
| 668 |
<td rowspan="2" width="40%" align="right"> <a accesskey="n" href="lk-manual.html">11. Lackey: an example tool >></a> |
| 669 |
</td> |
| 670 |
</tr> |
| 671 |
<tr><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td></tr> |
| 672 |
</table> |
| 673 |
</div> |
| 674 |
</body> |
| 675 |
</html> |