Debian Codegraph @

docs/html/cg-manual.html

raw
1 <html>
2 <head>
3 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4 <title>5. Cachegrind: a high-precision tracing 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="mc-manual.html" title="4. Memcheck: a memory error detector">
10 <link rel="next" href="cl-manual.html" title="6. Callgrind: a call-graph generating cache and branch prediction profiler">
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="mc-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="cl-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="cg-manual"></a>5. Cachegrind: a high-precision tracing 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="cg-manual.html#cg-manual.overview">5.1. Overview</a></span></dt>
27 <dt><span class="sect1"><a href="cg-manual.html#cg-manual.profile">5.2. Using Cachegrind and cg_annotate</a></span></dt>
28 <dd><dl>
29 <dt><span class="sect2"><a href="cg-manual.html#cg-manual.running-cachegrind">5.2.1. Running Cachegrind</a></span></dt>
30 <dt><span class="sect2"><a href="cg-manual.html#cg-manual.outputfile">5.2.2. Output File</a></span></dt>
31 <dt><span class="sect2"><a href="cg-manual.html#cg-manual.running-cg_annotate">5.2.3. Running cg_annotate</a></span></dt>
32 <dt><span class="sect2"><a href="cg-manual.html#cg-manual.the-metadata">5.2.4. The Metadata Section</a></span></dt>
33 <dt><span class="sect2"><a href="cg-manual.html#cg-manual.the-global">5.2.5. Global, File, and Function-level Counts</a></span></dt>
34 <dt><span class="sect2"><a href="cg-manual.html#cg-manual.line-by-line">5.2.6. Per-line Counts</a></span></dt>
35 <dt><span class="sect2"><a href="cg-manual.html#cg-manual.forkingprograms">5.2.7. Forking Programs</a></span></dt>
36 <dt><span class="sect2"><a href="cg-manual.html#cg-manual.annopts.warnings">5.2.8. cg_annotate Warnings</a></span></dt>
37 <dt><span class="sect2"><a href="cg-manual.html#cg-manual.cg_merge">5.2.9. Merging Cachegrind Output Files</a></span></dt>
38 <dt><span class="sect2"><a href="cg-manual.html#cg-manual.cg_diff">5.2.10. Differencing Cachegrind output files</a></span></dt>
39 <dt><span class="sect2"><a href="cg-manual.html#cg-manual.cache-branch-sim">5.2.11. Cache and Branch Simulation</a></span></dt>
40 </dl></dd>
41 <dt><span class="sect1"><a href="cg-manual.html#cg-manual.cgopts">5.3. Cachegrind Command-line Options</a></span></dt>
42 <dt><span class="sect1"><a href="cg-manual.html#cg-manual.annopts">5.4. cg_annotate Command-line Options</a></span></dt>
43 <dt><span class="sect1"><a href="cg-manual.html#cg-manual.mergeopts">5.5. cg_merge Command-line Options</a></span></dt>
44 <dt><span class="sect1"><a href="cg-manual.html#cg-manual.diffopts">5.6. cg_diff Command-line Options</a></span></dt>
45 <dt><span class="sect1"><a href="cg-manual.html#cg-manual.clientrequests">5.7. Cachegrind Client Requests</a></span></dt>
46 <dt><span class="sect1"><a href="cg-manual.html#cg-manual.sim-details">5.8. Simulation Details</a></span></dt>
47 <dd><dl>
48 <dt><span class="sect2"><a href="cg-manual.html#cache-sim">5.8.1. Cache Simulation Specifics</a></span></dt>
49 <dt><span class="sect2"><a href="cg-manual.html#branch-sim">5.8.2. Branch Simulation Specifics</a></span></dt>
50 <dt><span class="sect2"><a href="cg-manual.html#cg-manual.annopts.accuracy">5.8.3. Accuracy</a></span></dt>
51 </dl></dd>
52 <dt><span class="sect1"><a href="cg-manual.html#cg-manual.impl-details">5.9. Implementation Details</a></span></dt>
53 <dd><dl>
54 <dt><span class="sect2"><a href="cg-manual.html#cg-manual.impl-details.how-cg-works">5.9.1. How Cachegrind Works</a></span></dt>
55 <dt><span class="sect2"><a href="cg-manual.html#cg-manual.impl-details.file-format">5.9.2. Cachegrind Output File Format</a></span></dt>
56 </dl></dd>
57 </dl>
58 </div>
59 <p>
60 To use this tool, specify <code class="option">--tool=cachegrind</code> on the Valgrind
61 command line.
62 </p>
63 <div class="sect1">
64 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
65 <a name="cg-manual.overview"></a>5.1. Overview</h2></div></div></div>
66 <p>
67 Cachegrind is a high-precision tracing profiler. It runs slowly, but collects
68 precise and reproducible profiling data. It can merge and diff data from
69 different runs. To expand on these characteristics:
70 </p>
71 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
72 <li class="listitem"><p>
73 <span class="emphasis"><em>Precise.</em></span> Cachegrind measures the exact number of
74 instructions executed by your program, not an approximation. Furthermore,
75 it presents the gathered data at the file, function, and line level. This
76 is different to many other profilers that measure approximate execution
77 time, using sampling, and only at the function level.
78 </p></li>
79 <li class="listitem"><p>
80 <span class="emphasis"><em>Reproducible.</em></span> In general, execution time is a better
81 metric than instruction counts because it's what users perceive. However,
82 execution time often has high variability. When running the exact same
83 program on the exact same input multiple times, execution time might vary
84 by several percent. Furthermore, small changes in a program can change its
85 memory layout and have even larger effects on runtime. In contrast,
86 instruction counts are highly reproducible; for some programs they are
87 perfectly reproducible. This means the effects of small changes in a
88 program can be measured with high precision.
89 </p></li>
90 </ul></div>
91 <p>
92 For these reasons, Cachegrind is an excellent complement to time-based profilers.
93 </p>
94 <p>
95 Cachegrind can annotate programs written in any language, so long as debug info
96 is present to map machine code back to the original source code. Cachegrind has
97 been used successfully on programs written in C, C++, Rust, and assembly.
98 </p>
99 <p>
100 Cachegrind can also simulate how your program interacts with a machine's cache
101 hierarchy and branch predictor. This simulation was the original motivation for
102 the tool, hence its name. However, the simulations are basic and unlikely to
103 reflect the behaviour of a modern machine. For this reason they are off by
104 default. If you really want cache and branch information, a profiler like
105 <code class="computeroutput">perf</code> that accesses hardware counters is a
106 better choice.
107 </p>
108 </div>
109 <div class="sect1">
110 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
111 <a name="cg-manual.profile"></a>5.2. Using Cachegrind and cg_annotate</h2></div></div></div>
112 <p>
113 First, as for normal Valgrind use, you should compile with debugging info (the
114 <code class="option">-g</code> option in most compilers). But by contrast with normal
115 Valgrind use, you probably do want to turn optimisation on, since you should
116 profile your program as it will be normally run.
117 </p>
118 <p>
119 Second, run Cachegrind itself to gather the profiling data.
120 </p>
121 <p>
122 Third, run cg_annotate to get a detailed presentation of that data. cg_annotate
123 can combine the results of multiple Cachegrind output files. It can also
124 perform a diff between two Cachegrind output files.
125 </p>
126 <div class="sect2">
127 <div class="titlepage"><div><div><h3 class="title">
128 <a name="cg-manual.running-cachegrind"></a>5.2.1. Running Cachegrind</h3></div></div></div>
129 <p>
130 To run Cachegrind on a program <code class="filename">prog</code>, run:
131 </p>
132 <pre class="screen">
133 valgrind --tool=cachegrind prog
134 </pre>
135 <p>
136 </p>
137 <p>
138 The program will execute (slowly). Upon completion, summary statistics that
139 look like this will be printed:
140 </p>
141 <pre class="programlisting">
142 ==17942== I refs: 8,195,070
143 </pre>
144 <p>
145 The <code class="computeroutput">I refs</code> number is short for "Instruction
146 cache references", which is equivalent to "instructions executed". If you
147 enable the cache and/or branch simulation, additional counts will be shown.
148 </p>
149 </div>
150 <div class="sect2">
151 <div class="titlepage"><div><div><h3 class="title">
152 <a name="cg-manual.outputfile"></a>5.2.2. Output File</h3></div></div></div>
153 <p>
154 Cachegrind also writes more detailed profiling data to a file. By default this
155 Cachegrind output file is named <code class="filename">cachegrind.out.&lt;pid&gt;</code>
156 (where <code class="filename">&lt;pid&gt;</code> is the program's process ID), but its
157 name can be changed with the <code class="option">--cachegrind-out-file</code> option.
158 This file is human-readable, but is intended to be interpreted by the
159 accompanying program cg_annotate, described in the next section.
160 </p>
161 <p>
162 The default <code class="computeroutput">.&lt;pid&gt;</code> suffix on the output
163 file name serves two purposes. First, it means existing Cachegrind output files
164 aren't immediately overwritten. Second, and more importantly, it allows correct
165 profiling with the <code class="option">--trace-children=yes</code> option of programs
166 that spawn child processes.
167 </p>
168 </div>
169 <div class="sect2">
170 <div class="titlepage"><div><div><h3 class="title">
171 <a name="cg-manual.running-cg_annotate"></a>5.2.3. Running cg_annotate</h3></div></div></div>
172 <p>
173 Before using cg_annotate, it is worth widening your window to be at least 120
174 characters wide if possible, because the output lines can be quite long.
175 </p>
176 <p>
177 Then run:
178 </p>
179 <pre class="screen">cg_annotate &lt;filename&gt;</pre>
180 <p>
181 on a Cachegrind output file.
182 </p>
183 </div>
184 <div class="sect2">
185 <div class="titlepage"><div><div><h3 class="title">
186 <a name="cg-manual.the-metadata"></a>5.2.4. The Metadata Section</h3></div></div></div>
187 <p>
188 The first part of the output looks like this:
189 </p>
190 <pre class="programlisting">
191 --------------------------------------------------------------------------------
192 -- Metadata
193 --------------------------------------------------------------------------------
194 Invocation: ../cg_annotate concord.cgout
195 Command: ./concord ../cg_main.c
196 Events recorded: Ir
197 Events shown: Ir
198 Event sort order: Ir
199 Threshold: 0.1%
200 Annotation: on
201 </pre>
202 <p>
203 It summarizes how Cachegrind and the profiled program were run.
204 </p>
205 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
206 <li class="listitem"><p>
207 Invocation: the command line used to produce this output.
208 </p></li>
209 <li class="listitem"><p>
210 Command: the command line used to run the profiled program.
211 </p></li>
212 <li class="listitem"><p>
213 Events recorded: which events were recorded. By default, this is
214 <code class="computeroutput">Ir</code>. More events will be recorded if cache
215 and/or branch simulation is enabled.
216 </p></li>
217 <li class="listitem"><p>
218 Events shown: the events shown, which is a subset of the events gathered.
219 This can be adjusted with the <code class="option">--show</code> option.
220 </p></li>
221 <li class="listitem"><p>
222 Event sort order: the sort order used for the subsequent sections. For
223 example, in this case those sections are sorted from highest
224 <code class="computeroutput">Ir</code> counts to lowest. If there are multiple
225 events, one will be the primary sort event, and then there can be a
226 secondary sort event, tertiary sort event, etc., though more than one is
227 rarely needed. This order can be adjusted with the <code class="option">--sort</code>
228 option. Note that this does <span class="emphasis"><em>not</em></span> specify the order in
229 which the columns appear. That is specified by the "events shown" line (and
230 can be changed with the <code class="option">--show</code> option).
231 </p></li>
232 <li class="listitem"><p>
233 Threshold: cg_annotate by default omits files and functions with very low
234 counts to keep the output size reasonable. By default cg_annotate only
235 shows files and functions that account for at least 0.1% of the primary
236 sort event. The threshold can be adjusted with the
237 <code class="option">--threshold</code> option.
238 </p></li>
239 <li class="listitem"><p>
240 Annotation: whether source file annotation is enabled. Controlled with the
241 <code class="option">--annotate</code> option.
242 </p></li>
243 </ul></div>
244 <p>
245 If cache simulation is enabled, details of the cache parameters will be shown
246 above the "Invocation" line.
247 </p>
248 </div>
249 <div class="sect2">
250 <div class="titlepage"><div><div><h3 class="title">
251 <a name="cg-manual.the-global"></a>5.2.5. Global, File, and Function-level Counts</h3></div></div></div>
252 <p>
253 Next comes the summary for the whole program:
254 </p>
255 <pre class="programlisting">
256 --------------------------------------------------------------------------------
257 -- Summary
258 --------------------------------------------------------------------------------
259 Ir________________
260
261 8,195,070 (100.0%) PROGRAM TOTALS
262 </pre>
263 <p>
264 The <code class="computeroutput">Ir</code> column label is suffixed with
265 underscores to show the bounds of the columns underneath.
266 </p>
267 <p>
268 Then comes file:function counts. Here is the first part of that section:
269 </p>
270 <pre class="programlisting">
271 --------------------------------------------------------------------------------
272 -- File:function summary
273 --------------------------------------------------------------------------------
274 Ir______________________ file:function
275
276 &lt; 3,078,746 (37.6%, 37.6%) /home/njn/grind/ws1/cachegrind/concord.c:
277 1,630,232 (19.9%) get_word
278 630,918 (7.7%) hash
279 461,095 (5.6%) insert
280 130,560 (1.6%) add_existing
281 91,014 (1.1%) init_hash_table
282 88,056 (1.1%) create
283 46,676 (0.6%) new_word_node
284
285 &lt; 1,746,038 (21.3%, 58.9%) ./malloc/./malloc/malloc.c:
286 1,285,938 (15.7%) _int_malloc
287 458,225 (5.6%) malloc
288
289 &lt; 1,107,550 (13.5%, 72.4%) ./libio/./libio/getc.c:getc
290
291 &lt; 551,071 (6.7%, 79.1%) ./string/../sysdeps/x86_64/multiarch/strcmp-avx2.S:__strcmp_avx2
292
293 &lt; 521,228 (6.4%, 85.5%) ./ctype/../include/ctype.h:
294 260,616 (3.2%) __ctype_tolower_loc
295 260,612 (3.2%) __ctype_b_loc
296
297 &lt; 468,163 (5.7%, 91.2%) ???:
298 468,151 (5.7%) ???
299
300 &lt; 456,071 (5.6%, 96.8%) /usr/include/ctype.h:get_word
301
302 </pre>
303 <p>
304 Each entry covers one file, and one or more functions within that file. If
305 there is only one significant function within a file, as in the first entry,
306 the file and function are shown on the same line separate by a colon. If there
307 are multiple significant functions within a file, as in the third entry, each
308 function gets its own line.
309 </p>
310 <p>
311 This example involves a small C program, and shows a combination of code from
312 the program itself (including functions like <code class="function">get_word</code> and
313 <code class="function">hash</code> in the file <code class="filename">concord.c</code>) as well
314 as code from system libraries, such as functions like
315 <code class="function">malloc</code> and <code class="function">getc</code>.
316 </p>
317 <p>
318 Each entry is preceded with a <code class="computeroutput">&lt;</code>, which can
319 be useful when navigating through the output in an editor, or grepping through
320 results.
321 </p>
322 <p>
323 The first percentage in each column indicates the proportion of the total event
324 count is covered by this line. The second percentage, which only shows on the
325 first line of each entry, shows the cumulative percentage of all the entries up
326 to and including this one. The entries shown here account for 96.8% of the
327 instructions executed by the program.
328 </p>
329 <p>
330 The name <code class="computeroutput">???</code> is used if the file name and/or
331 function name could not be determined from debugging information. If
332 <code class="filename">???</code> filenames dominate, the program probably wasn't
333 compiled with <code class="option">-g</code>. If <code class="function">???</code> function names
334 dominate, the program may have had symbols stripped.
335 </p>
336 <p>
337 After that comes function:file counts. Here is the first part of that section:
338 </p>
339 <pre class="programlisting">
340 --------------------------------------------------------------------------------
341 -- Function:file summary
342 --------------------------------------------------------------------------------
343 Ir______________________ function:file
344
345 &gt; 2,086,303 (25.5%, 25.5%) get_word:
346 1,630,232 (19.9%) /home/njn/grind/ws1/cachegrind/concord.c
347 456,071 (5.6%) /usr/include/ctype.h
348
349 &gt; 1,285,938 (15.7%, 41.1%) _int_malloc:./malloc/./malloc/malloc.c
350
351 &gt; 1,107,550 (13.5%, 54.7%) getc:./libio/./libio/getc.c
352
353 &gt; 630,918 (7.7%, 62.4%) hash:/home/njn/grind/ws1/cachegrind/concord.c
354
355 &gt; 551,071 (6.7%, 69.1%) __strcmp_avx2:./string/../sysdeps/x86_64/multiarch/strcmp-avx2.S
356
357 &gt; 480,248 (5.9%, 74.9%) malloc:
358 458,225 (5.6%) ./malloc/./malloc/malloc.c
359 22,023 (0.3%) ./malloc/./malloc/arena.c
360
361 &gt; 468,151 (5.7%, 80.7%) ???:???
362
363 &gt; 461,095 (5.6%, 86.3%) insert:/home/njn/grind/ws1/cachegrind/concord.c
364 </pre>
365 <p>
366 This is similar to the previous section, but is grouped by functions first and
367 files second. Also, the entry markers are <code class="computeroutput">&gt;</code>
368 instead of <code class="computeroutput">&lt;</code>.
369 </p>
370 <p>
371 You might wonder why this section is needed, and how it differs from the
372 previous section. The answer is inlining. In this example there are two entries
373 demonstrating a function whose code is effectively spread across more than one
374 file: <code class="function">get_word</code> and <code class="function">malloc</code>. Here is an
375 example from profiling the Rust compiler, a much larger program that uses
376 inlining more:
377 </p>
378 <pre class="programlisting">
379 &gt; 30,469,230 (1.3%, 11.1%) &lt;rustc_middle::ty::context::CtxtInterners&gt;::intern_ty:
380 10,269,220 (0.5%) /home/njn/.cargo/registry/src/github.com-1ecc6299db9ec823/hashbrown-0.12.3/src/raw/mod.rs
381 7,696,827 (0.3%) /home/njn/dev/rust0/compiler/rustc_middle/src/ty/context.rs
382 3,858,099 (0.2%) /home/njn/dev/rust0/library/core/src/cell.rs
383 </pre>
384 <p>
385 In this case the compiled function <code class="function">intern_ty</code> includes code
386 from three different source files, due to inlining. These should be examined
387 together. Older versions of cg_annotate presented this entry as three separate
388 file:function entries, which would typically be intermixed with all the other
389 entries, making it hard to see that they are all really part of the same
390 function.
391 </p>
392 </div>
393 <div class="sect2">
394 <div class="titlepage"><div><div><h3 class="title">
395 <a name="cg-manual.line-by-line"></a>5.2.6. Per-line Counts</h3></div></div></div>
396 <p>
397 By default, a source file is annotated if it contains at least one function
398 that meets the significance threshold. This can be disabled with the
399 <code class="option">--annotate</code> option.
400 </p>
401 <p>
402 To continue the previous example, here is part of the annotation of the file
403 <code class="filename">concord.c</code>:
404 </p>
405 <pre class="programlisting">
406 --------------------------------------------------------------------------------
407 -- Annotated source file: /home/njn/grind/ws1/cachegrind/docs/concord.c
408 --------------------------------------------------------------------------------
409 Ir____________
410
411 . /* Function builds the hash table from the given file. */
412 . void init_hash_table(char *file_name, Word_Node *table[])
413 8 (0.0%) {
414 . FILE *file_ptr;
415 . Word_Info *data;
416 2 (0.0%) int line = 1, i;
417 .
418 . /* Structure used when reading in words and line numbers. */
419 3 (0.0%) data = (Word_Info *) create(sizeof(Word_Info));
420 .
421 . /* Initialise entire table to NULL. */
422 2,993 (0.0%) for (i = 0; i &lt; TABLE_SIZE; i++)
423 997 (0.0%) table[i] = NULL;
424 .
425 . /* Open file, check it. */
426 4 (0.0%) file_ptr = fopen(file_name, "r");
427 2 (0.0%) if (!(file_ptr)) {
428 . fprintf(stderr, "Couldn't open '%s'.\n", file_name);
429 . exit(EXIT_FAILURE);
430 . }
431 .
432 . /* 'Get' the words and lines one at a time from the file, and insert them
433 . ** into the table one at a time. */
434 55,363 (0.7%) while ((line = get_word(data, line, file_ptr)) != EOF)
435 31,632 (0.4%) insert(data-&gt;word, data-&gt;line, table);
436 .
437 2 (0.0%) free(data);
438 2 (0.0%) fclose(file_ptr);
439 6 (0.0%) }
440 </pre>
441 <p>
442 Each executed line is annotated with its event counts. Other lines are
443 annotated with a dot. This may be because they contain no executable code, or
444 they contain executable code but were never executed.
445 </p>
446 <p>
447 You can easily tell if a function is inlined from this output. If it is not
448 inlined, it will have event counts on the lines containing the opening and
449 closing braces. If it is inlined, it will not have event counts on those lines.
450 In the example above, <code class="function">init_hash_table</code> does have counts,
451 so you can tell it is not inlined.
452 </p>
453 <p>
454 Note again that inlining can lead to surprising results. If a function
455 <code class="function">f</code> is always inlined, in the file:function and
456 function:file sections counts will be attributed to the functions it is inlined
457 into, rather than itself. However, if you look at the line-by-line annotations
458 for <code class="function">f</code> you'll see the counts that belong to
459 <code class="function">f</code>. So it's worth looking for large counts/percentages in the
460 line-by-line annotations.
461 </p>
462 <p>
463 Sometimes only a small section of a source file is executed. To minimise
464 uninteresting output, Cachegrind only shows annotated lines and lines within a
465 small distance of annotated lines. Gaps are marked with line numbers, for
466 example:
467 </p>
468 <pre class="programlisting">
469 (counts and code for line 704)
470 -- line 375 ----------------------------------------
471 -- line 514 ----------------------------------------
472 (counts and code for line 878)
473 </pre>
474 <p>
475 The number of lines of context shown around annotated lines is controlled by
476 the <code class="option">--context</code> option.
477 </p>
478 <p>
479 Any significant source files that could not be found are shown like this:
480 </p>
481 <pre class="programlisting">
482 --------------------------------------------------------------------------------
483 -- Annotated source file: ./malloc/./malloc/malloc.c
484 --------------------------------------------------------------------------------
485 Unannotated because one or more of these original files are unreadable:
486 - ./malloc/./malloc/malloc.c
487 </pre>
488 <p>
489 This is common for library files, because libraries are usually compiled with
490 debugging information but the source files are rarely present on a system.
491 </p>
492 <p>
493 Cachegrind relies heavily on accurate debug info. Sometimes compilers do not
494 map a particular compiled instruction to line number 0, where the 0 represents
495 "unknown" or "none". This is annoying but does happen in practice. cg_annotate
496 prints these in the following way:
497 </p>
498 <pre class="programlisting">
499 --------------------------------------------------------------------------------
500 -- Annotated source file: /home/njn/dev/rust0/compiler/rustc_borrowck/src/lib.rs
501 --------------------------------------------------------------------------------
502 Ir______________
503
504 1,046,746 (0.0%) &lt;unknown (line 0)&gt;
505 </pre>
506 <p>
507 Finally, when annotation is performed, the output ends with a summary of how
508 many counts were annotated and unannotated, and why. For example:
509 </p>
510 <pre class="programlisting">
511 --------------------------------------------------------------------------------
512 -- Annotation summary
513 --------------------------------------------------------------------------------
514 Ir_______________
515
516 3,534,817 (43.1%) annotated: files known &amp; above threshold &amp; readable, line numbers known
517 0 annotated: files known &amp; above threshold &amp; readable, line numbers unknown
518 0 unannotated: files known &amp; above threshold &amp; two or more non-identical
519 4,132,126 (50.4%) unannotated: files known &amp; above threshold &amp; unreadable
520 59,950 (0.7%) unannotated: files known &amp; below threshold
521 468,163 (5.7%) unannotated: files unknown
522 </pre>
523 </div>
524 <div class="sect2">
525 <div class="titlepage"><div><div><h3 class="title">
526 <a name="cg-manual.forkingprograms"></a>5.2.7. Forking Programs</h3></div></div></div>
527 <p>
528 If your program forks, the child will inherit all the profiling data that
529 has been gathered for the parent.
530 </p>
531 <p>
532 If the output file name (controlled by <code class="option">--cachegrind-out-file</code>)
533 does not contain <code class="option">%p</code>, then the outputs from the parent and
534 child will be intermingled in a single output file, which will almost certainly
535 make it unreadable by cg_annotate.
536 </p>
537 </div>
538 <div class="sect2">
539 <div class="titlepage"><div><div><h3 class="title">
540 <a name="cg-manual.annopts.warnings"></a>5.2.8. cg_annotate Warnings</h3></div></div></div>
541 <p>
542 There are two situations in which cg_annotate prints warnings.
543 </p>
544 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
545 <li class="listitem"><p>
546 If a source file is more recent than the Cachegrind output file. This is
547 because the information in the Cachegrind output file is only recorded with
548 line numbers, so if the line numbers change at all in the source (e.g.
549 lines added, deleted, swapped), any annotations will be incorrect.
550 </p></li>
551 <li class="listitem"><p>
552 If information is recorded about line numbers past the end of a file. This
553 can be caused by the above problem, e.g. shortening the source file while
554 using an old Cachegrind output file. If this happens, the figures for the
555 bogus lines are printed anyway (and clearly marked as bogus) in case they
556 are important.
557 </p></li>
558 </ul></div>
559 </div>
560 <div class="sect2">
561 <div class="titlepage"><div><div><h3 class="title">
562 <a name="cg-manual.cg_merge"></a>5.2.9. Merging Cachegrind Output Files</h3></div></div></div>
563 <p>
564 cg_annotate can merge data from multiple Cachegrind output files in a single
565 run. (There is also a program called cg_merge that can merge multiple
566 Cachegrind output files into a single Cachegrind output file, but it is now
567 deprecated because cg_annotate's merging does a better job.)
568 </p>
569 <p>
570 Use it as follows:
571 </p>
572 <pre class="programlisting">
573 cg_annotate file1 file2 file3 ...
574 </pre>
575 <p>
576 cg_annotate computes the sum of these files (effectively
577 <code class="filename">file1</code> + <code class="filename">file2</code> +
578 <code class="filename">file3</code>), and then produces output as usual that shows the
579 summed counts.
580 </p>
581 <p>
582 The most common merging scenario is if you want to aggregate costs over
583 multiple runs of the same program, possibly on different inputs.
584 </p>
585 </div>
586 <div class="sect2">
587 <div class="titlepage"><div><div><h3 class="title">
588 <a name="cg-manual.cg_diff"></a>5.2.10. Differencing Cachegrind output files</h3></div></div></div>
589 <p>
590 cg_annotate can diff data from two Cachegrind output files in a single run.
591 (There is also a program called cg_diff that can diff two Cachegrind output
592 files into a single Cachegrind output file, but it is now deprecated because
593 cg_annotate's differencing does a better job.)
594 </p>
595 <p>
596 Use it as follows:
597 </p>
598 <pre class="programlisting">
599 cg_annotate --diff file1 file2
600 </pre>
601 <p>
602 cg_annotate computes the difference between these two files (effectively
603 <code class="filename">file2</code> - <code class="filename">file1</code>), and then
604 produces output as usual that shows the count differences. Note that many of
605 the counts may be negative; this indicates that the counts for the relevant
606 file/function/line are smaller in the second version than those in the first
607 version.
608 </p>
609 <p>
610 The simplest common scenario is comparing two Cachegrind output files that came
611 from the same program, but on different inputs. cg_annotate will do a good job
612 on this without assistance.
613 </p>
614 <p>
615 A more complex scenario is if you want to compare Cachegrind output files from
616 two slightly different versions of a program that you have sitting
617 side-by-side, running on the same input. For example, you might have
618 <code class="filename">version1/prog.c</code> and <code class="filename">version2/prog.c</code>.
619 A straight comparison of the two would not be useful. Because functions are
620 always paired with filenames, a function <code class="function">f</code> would be listed
621 as <code class="filename">version1/prog.c:f</code> for the first version but
622 <code class="filename">version2/prog.c:f</code> for the second version.
623 </p>
624 <p>
625 In this case, use the <code class="option">--mod-filename</code> option. Its argument is a
626 search-and-replace expression that will be applied to all the filenames in both
627 Cachegrind output files. It can be used to remove minor differences in
628 filenames. For example, the option
629 <code class="option">--mod-filename='s/version[0-9]/versionN/'</code> will suffice for the
630 above example.
631 </p>
632 <p>
633 Similarly, sometimes compilers auto-generate certain functions and give them
634 randomized names like <code class="function">T.1234</code> where the suffixes vary from
635 build to build. You can use the <code class="option">--mod-funcname</code> option to
636 remove small differences like these; it works in the same way as
637 <code class="option">--mod-filename</code>.
638 </p>
639 <p>
640 When <code class="option">--mod-filename</code> is used to compare two different versions
641 of the same program, cg_annotate will not annotate any file that is different
642 between the two versions, because the per-line counts are not reliable in such
643 a case. For example, imagine if <code class="filename">version2/prog.c</code> is the
644 same as <code class="filename">version1/prog.c</code> except with an extra blank line at
645 the top of the file. Every single per-line count will have changed. In
646 comparison, the per-file and per-function counts have not changed, and are
647 still very useful for determining differences between programs. You might think
648 that this means every interesting file will be left unannotated, but again
649 inlining means that files that are identical in the two versions can have
650 different counts on many lines.
651 </p>
652 </div>
653 <div class="sect2">
654 <div class="titlepage"><div><div><h3 class="title">
655 <a name="cg-manual.cache-branch-sim"></a>5.2.11. Cache and Branch Simulation</h3></div></div></div>
656 <p>
657 Cachegrind can simulate how your program interacts with a machine's cache
658 hierarchy and/or branch predictor.
659
660 The cache simulation models a machine with independent first-level instruction
661 and data caches (I1 and D1), backed by a unified second-level cache (L2). For
662 these machines (in the cases where Cachegrind can auto-detect the cache
663 configuration) Cachegrind simulates the first-level and last-level caches.
664 Therefore, Cachegrind always refers to the I1, D1 and LL (last-level) caches.
665 </p>
666 <p>
667 When simulating the cache, with <code class="option">--cache-sim=yes</code>, Cachegrind
668 gathers the following statistics:
669 </p>
670 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
671 <li class="listitem"><p>
672 I cache reads (<code class="computeroutput">Ir</code>, which equals the number
673 of instructions executed), I1 cache read misses
674 (<code class="computeroutput">I1mr</code>) and LL cache instruction read
675 misses (<code class="computeroutput">ILmr</code>).
676 </p></li>
677 <li class="listitem"><p>
678 D cache reads (<code class="computeroutput">Dr</code>, which equals the number
679 of memory reads), D1 cache read misses
680 (<code class="computeroutput">D1mr</code>), and LL cache data read misses
681 (<code class="computeroutput">DLmr</code>).
682 </p></li>
683 <li class="listitem"><p>
684 D cache writes (<code class="computeroutput">Dw</code>, which equals the
685 number of memory writes), D1 cache write misses
686 (<code class="computeroutput">D1mw</code>), and LL cache data write misses
687 (<code class="computeroutput">DLmw</code>).
688 </p></li>
689 </ul></div>
690 <p>
691 Note that D1 total accesses is given by <code class="computeroutput">D1mr</code> +
692 <code class="computeroutput">D1mw</code>, and that LL total accesses is given by
693 <code class="computeroutput">ILmr</code> + <code class="computeroutput">DLmr</code> +
694 <code class="computeroutput">DLmw</code>.
695 </p>
696 <p>
697 When simulating the branch predictor, with <code class="option">--branch-sim=yes</code>,
698 Cachegrind gathers the following statistics:
699 </p>
700 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
701 <li class="listitem"><p>
702 Conditional branches executed (<code class="computeroutput">Bc</code>) and
703 conditional branches mispredicted (<code class="computeroutput">Bcm</code>).
704 </p></li>
705 <li class="listitem"><p>
706 Indirect branches executed (<code class="computeroutput">Bi</code>) and
707 indirect branches mispredicted (<code class="computeroutput">Bim</code>).
708 </p></li>
709 </ul></div>
710 <p>
711 When cache and/or branch simulation is enabled, cg_annotate will print multiple
712 counts per line of output. For example:
713 </p>
714 <pre class="programlisting">
715 Ir______________________ Bc____________________ Bcm__________________ Bi____________________ Bim______________ function:file
716
717 &gt; 8,547 (0.1%, 99.4%) 936 (0.1%, 99.1%) 177 (0.3%, 96.7%) 59 (0.0%, 99.9%) 38 (19.4%, 66.3%) strcmp:
718 8,503 (0.1%) 928 (0.1%) 175 (0.3%) 59 (0.0%) 38 (19.4%) ./string/../sysdeps/x86_64/multiarch/../multiarch/strcmp-sse2.S
719 </pre>
720 </div>
721 </div>
722 <div class="sect1">
723 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
724 <a name="cg-manual.cgopts"></a>5.3. Cachegrind Command-line Options</h2></div></div></div>
725 <p>
726 Cachegrind-specific options are:
727 </p>
728 <div class="variablelist">
729 <a name="cg.opts.list"></a><dl class="variablelist">
730 <dt>
731 <a name="opt.cachegrind-out-file"></a><span class="term">
732 <code class="option">--cachegrind-out-file=&lt;file&gt; </code>
733 </span>
734 </dt>
735 <dd><p>
736 Write the Cachegrind output file to <code class="filename">file</code> rather than
737 to the default output file,
738 <code class="filename">cachegrind.out.&lt;pid&gt;</code>. The <code class="option">%p</code>
739 and <code class="option">%q</code> format specifiers can be used to embed the
740 process ID and/or the contents of an environment variable in the name, as
741 is the case for the core option
742 <code class="option"><a class="link" href="manual-core.html#opt.log-file">--log-file</a></code>.
743 </p></dd>
744 <dt>
745 <a name="opt.cache-sim"></a><span class="term">
746 <code class="option">--cache-sim=no|yes [no] </code>
747 </span>
748 </dt>
749 <dd><p>
750 Enables or disables collection of cache access and miss counts.
751 </p></dd>
752 <dt>
753 <a name="opt.branch-sim"></a><span class="term">
754 <code class="option">--branch-sim=no|yes [no] </code>
755 </span>
756 </dt>
757 <dd><p>
758 Enables or disables collection of branch instruction and
759 misprediction counts.
760 </p></dd>
761 <dt>
762 <a name="opt.instr-at-start"></a><span class="term">
763 <code class="option">--instr-at-start=no|yes [yes] </code>
764 </span>
765 </dt>
766 <dd><p>
767 Enables or disables instrumentation at the start of execution.
768 Use this in combination with
769 <code class="computeroutput">CACHEGRIND_START_INSTRUMENTATION</code> and
770 <code class="computeroutput">CACHEGRIND_STOP_INSTRUMENTATION</code> to
771 measure only part of a client program's execution.
772 </p></dd>
773 <dt>
774 <a name="cg.opt.I1"></a><span class="term">
775 <code class="option">--I1=&lt;size&gt;,&lt;associativity&gt;,&lt;line size&gt; </code>
776 </span>
777 </dt>
778 <dd><p>
779 Specify the size, associativity and line size of the level 1 instruction
780 cache. Only useful with <code class="option">--cache-sim=yes</code>.
781 </p></dd>
782 <dt>
783 <a name="cg.opt.D1"></a><span class="term">
784 <code class="option">--D1=&lt;size&gt;,&lt;associativity&gt;,&lt;line size&gt; </code>
785 </span>
786 </dt>
787 <dd><p>
788 Specify the size, associativity and line size of the level 1 data cache.
789 Only useful with <code class="option">--cache-sim=yes</code>.
790 </p></dd>
791 <dt>
792 <a name="cg.opt.LL"></a><span class="term">
793 <code class="option">--LL=&lt;size&gt;,&lt;associativity&gt;,&lt;line size&gt; </code>
794 </span>
795 </dt>
796 <dd><p>
797 Specify the size, associativity and line size of the last-level cache.
798 Only useful with <code class="option">--cache-sim=yes</code>.
799 </p></dd>
800 </dl>
801 </div>
802 </div>
803 <div class="sect1">
804 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
805 <a name="cg-manual.annopts"></a>5.4. cg_annotate Command-line Options</h2></div></div></div>
806 <div class="variablelist">
807 <a name="cg_annotate.opts.list"></a><dl class="variablelist">
808 <dt><span class="term">
809 <code class="option">-h --help </code>
810 </span></dt>
811 <dd><p>Show the help message.</p></dd>
812 <dt><span class="term">
813 <code class="option">--version </code>
814 </span></dt>
815 <dd><p>Show the version number.</p></dd>
816 <dt><span class="term">
817 <code class="option">--diff </code>
818 </span></dt>
819 <dd><p>Diff two Cachegrind output files.</p></dd>
820 <dt><span class="term">
821 <code class="option">--mod-filename &lt;regex&gt; [default: none]</code>
822 </span></dt>
823 <dd><p>
824 Specifies an <code class="option">s/old/new/</code> search-and-replace expression
825 that is applied to all filenames. Useful when differencing, for removing
826 minor differences in paths between two different versions of a program
827 that are sitting in different directories. An <code class="option">i</code> suffix
828 makes the regex case-insensitive, and a <code class="option">g</code> suffix makes
829 it match multiple times.
830 </p></dd>
831 <dt><span class="term">
832 <code class="option">--mod-funcname &lt;regex&gt; [default: none]</code>
833 </span></dt>
834 <dd><p>
835 Like <code class="option">--mod-filename</code>, but for filenames. Useful for
836 removing minor differences in randomized names of auto-generated
837 functions generated by some compilers.
838 </p></dd>
839 <dt><span class="term">
840 <code class="option">--show=A,B,C [default: all, using order in
841 the Cachegrind output file] </code>
842 </span></dt>
843 <dd><p>
844 Specifies which events to show (and the column order). Default is to use
845 all present in the Cachegrind output file (and use the order in the
846 file). Best used in conjunction with <code class="option">--sort</code>.
847 </p></dd>
848 <dt><span class="term">
849 <code class="option">--sort=A,B,C [default: order in the Cachegrind output file] </code>
850 </span></dt>
851 <dd><p>
852 Specifies the events upon which the sorting of the file:function and
853 function:file entries will be based.
854 </p></dd>
855 <dt><span class="term">
856 <code class="option">--threshold=X [default: 0.1%] </code>
857 </span></dt>
858 <dd><p>
859 Sets the significance threshold for the file:function and function:files
860 sections. A file or function is shown if it accounts for more than X% of
861 the counts for the primary sort event. If annotating source files, this
862 also affects which files are annotated.
863 </p></dd>
864 <dt><span class="term">
865 <code class="option">--show-percs, --no-show-percs, --show-percs=&lt;no|yes&gt; [default: yes] </code>
866 </span></dt>
867 <dd><p>
868 When enabled, a percentage is printed next to all event counts. This
869 helps gauge the relative importance of each function and line.
870 </p></dd>
871 <dt><span class="term">
872 <code class="option">--annotate, --no-annotate, --auto=&lt;no|yes&gt; [default: yes] </code>
873 </span></dt>
874 <dd><p>
875 Enables or disables source file annotation.
876 </p></dd>
877 <dt><span class="term">
878 <code class="option">--context=N [default: 8] </code>
879 </span></dt>
880 <dd><p>
881 The number of lines of context to show before and after each annotated
882 line. Use a large number (e.g. 100000) to show all source lines.
883 </p></dd>
884 </dl>
885 </div>
886 </div>
887 <div class="sect1">
888 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
889 <a name="cg-manual.mergeopts"></a>5.5. cg_merge Command-line Options</h2></div></div></div>
890 <div class="variablelist">
891 <a name="cg_merge.opts.list"></a><dl class="variablelist">
892 <dt><span class="term">
893 <code class="option">-o outfile</code>
894 </span></dt>
895 <dd><p>
896 Write the output to to <code class="computeroutput">outfile</code>
897 instead of standard output.
898 </p></dd>
899 </dl>
900 </div>
901 </div>
902 <div class="sect1">
903 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
904 <a name="cg-manual.diffopts"></a>5.6. cg_diff Command-line Options</h2></div></div></div>
905 <div class="variablelist">
906 <a name="cg_diff.opts.list"></a><dl class="variablelist">
907 <dt><span class="term">
908 <code class="option">-h --help </code>
909 </span></dt>
910 <dd><p>Show the help message.</p></dd>
911 <dt><span class="term">
912 <code class="option">--version </code>
913 </span></dt>
914 <dd><p>Show the version number.</p></dd>
915 <dt><span class="term">
916 <code class="option">--mod-filename=&lt;expr&gt; [default: none]</code>
917 </span></dt>
918 <dd><p>
919 Specifies an <code class="option">s/old/new/</code> search-and-replace expression
920 that is applied to all filenames.
921 </p></dd>
922 <dt><span class="term">
923 <code class="option">--mod-funcname=&lt;expr&gt; [default: none]</code>
924 </span></dt>
925 <dd><p>
926 Like <code class="option">--mod-filename</code>, but for filenames.
927 </p></dd>
928 </dl>
929 </div>
930 </div>
931 <div class="sect1">
932 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
933 <a name="cg-manual.clientrequests"></a>5.7. Cachegrind Client Requests</h2></div></div></div>
934 <p>Cachegrind provides the following client requests in
935 <code class="filename">cachegrind.h</code>.
936 </p>
937 <div class="variablelist">
938 <a name="cg.clientrequests.list"></a><dl class="variablelist">
939 <dt>
940 <a name="cg.cr.start-instr"></a><span class="term">
941 <code class="computeroutput">CACHEGRIND_START_INSTRUMENTATION</code>
942 </span>
943 </dt>
944 <dd><p>Start Cachegrind instrumentation if not already enabled. Use this
945 in combination with
946 <code class="computeroutput">CACHEGRIND_STOP_INSTRUMENTATION</code> and
947 <code class="option"><a class="link" href="cg-manual.html#opt.instr-at-start">--instr-at-start</a></code>
948 to measure only part of a client program's execution.
949 </p></dd>
950 <dt>
951 <a name="cg.cr.stop-instr"></a><span class="term">
952 <code class="computeroutput">CACHEGRIND_STOP_INSTRUMENTATION</code>
953 </span>
954 </dt>
955 <dd><p>Stop Cachegrind instrumentation if not already disabled. Use this
956 in combination with
957 <code class="computeroutput">CACHEGRIND_START_INSTRUMENTATION</code> and
958 <code class="option"><a class="link" href="cg-manual.html#opt.instr-at-start">--instr-at-start</a></code>
959 to measure only part of a client program's execution.
960 </p></dd>
961 </dl>
962 </div>
963 </div>
964 <div class="sect1">
965 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
966 <a name="cg-manual.sim-details"></a>5.8. Simulation Details</h2></div></div></div>
967 <p>
968 This section talks about details you don't need to know about in order to
969 use Cachegrind, but may be of interest to some people.
970 </p>
971 <div class="sect2">
972 <div class="titlepage"><div><div><h3 class="title">
973 <a name="cache-sim"></a>5.8.1. Cache Simulation Specifics</h3></div></div></div>
974 <p>
975 The cache simulation approximates the hardware of an AMD Athlon CPU circa 2002.
976 Its specific characteristics are as follows:</p>
977 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
978 <li class="listitem"><p>Write-allocate: when a write miss occurs, the block
979 written to is brought into the D1 cache. Most modern caches
980 have this property.</p></li>
981 <li class="listitem">
982 <p>Bit-selection hash function: the set of line(s) in the cache
983 to which a memory block maps is chosen by the middle bits
984 M--(M+N-1) of the byte address, where:</p>
985 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; ">
986 <li class="listitem"><p>line size = 2^M bytes</p></li>
987 <li class="listitem"><p>(cache size / line size / associativity) = 2^N bytes</p></li>
988 </ul></div>
989 </li>
990 <li class="listitem"><p>Inclusive LL cache: the LL cache typically replicates all
991 the entries of the L1 caches, because fetching into L1 involves
992 fetching into LL first (this does not guarantee strict inclusiveness,
993 as lines evicted from LL still could reside in L1). This is
994 standard on Pentium chips, but AMD Opterons, Athlons and Durons
995 use an exclusive LL cache that only holds
996 blocks evicted from L1. Ditto most modern VIA CPUs.</p></li>
997 </ul></div>
998 <p>The cache configuration simulated (cache size,
999 associativity and line size) is determined automatically using
1000 the x86 CPUID instruction. If you have a machine that (a)
1001 doesn't support the CPUID instruction, or (b) supports it in an
1002 early incarnation that doesn't give any cache information, then
1003 Cachegrind will fall back to using a default configuration (that
1004 of a model 3/4 Athlon). Cachegrind will tell you if this
1005 happens. You can manually specify one, two or all three levels
1006 (I1/D1/LL) of the cache from the command line using the
1007 <code class="option">--I1</code>,
1008 <code class="option">--D1</code> and
1009 <code class="option">--LL</code> options.
1010 For cache parameters to be valid for simulation, the number
1011 of sets (with associativity being the number of cache lines in
1012 each set) has to be a power of two.</p>
1013 <p>On PowerPC platforms
1014 Cachegrind cannot automatically
1015 determine the cache configuration, so you will
1016 need to specify it with the
1017 <code class="option">--I1</code>,
1018 <code class="option">--D1</code> and
1019 <code class="option">--LL</code> options.</p>
1020 <p>Other noteworthy behaviour:</p>
1021 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
1022 <li class="listitem">
1023 <p>References that straddle two cache lines are treated as
1024 follows:</p>
1025 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; ">
1026 <li class="listitem"><p>If both blocks hit --&gt; counted as one hit</p></li>
1027 <li class="listitem"><p>If one block hits, the other misses --&gt; counted
1028 as one miss.</p></li>
1029 <li class="listitem"><p>If both blocks miss --&gt; counted as one miss (not
1030 two)</p></li>
1031 </ul></div>
1032 </li>
1033 <li class="listitem">
1034 <p>Instructions that modify a memory location
1035 (e.g. <code class="computeroutput">inc</code> and
1036 <code class="computeroutput">dec</code>) are counted as doing
1037 just a read, i.e. a single data reference. This may seem
1038 strange, but since the write can never cause a miss (the read
1039 guarantees the block is in the cache) it's not very
1040 interesting.</p>
1041 <p>Thus it measures not the number of times the data cache
1042 is accessed, but the number of times a data cache miss could
1043 occur.</p>
1044 </li>
1045 </ul></div>
1046 <p>
1047 If you are interested in simulating a cache with different properties, it is
1048 not particularly hard to write your own cache simulator, or to modify the
1049 existing ones in <code class="computeroutput">cg_sim.c</code>.
1050 </p>
1051 </div>
1052 <div class="sect2">
1053 <div class="titlepage"><div><div><h3 class="title">
1054 <a name="branch-sim"></a>5.8.2. Branch Simulation Specifics</h3></div></div></div>
1055 <p>Cachegrind simulates branch predictors intended to be
1056 typical of mainstream desktop/server processors of around 2004.</p>
1057 <p>Conditional branches are predicted using an array of 16384 2-bit
1058 saturating counters. The array index used for a branch instruction is
1059 computed partly from the low-order bits of the branch instruction's
1060 address and partly using the taken/not-taken behaviour of the last few
1061 conditional branches. As a result the predictions for any specific
1062 branch depend both on its own history and the behaviour of previous
1063 branches. This is a standard technique for improving prediction
1064 accuracy.</p>
1065 <p>For indirect branches (that is, jumps to unknown destinations)
1066 Cachegrind uses a simple branch target address predictor. Targets are
1067 predicted using an array of 512 entries indexed by the low order 9
1068 bits of the branch instruction's address. Each branch is predicted to
1069 jump to the same address it did last time. Any other behaviour causes
1070 a mispredict.</p>
1071 <p>More recent processors have better branch predictors, in
1072 particular better indirect branch predictors. Cachegrind's predictor
1073 design is deliberately conservative so as to be representative of the
1074 large installed base of processors which pre-date widespread
1075 deployment of more sophisticated indirect branch predictors. In
1076 particular, late model Pentium 4s (Prescott), Pentium M, Core and Core
1077 2 have more sophisticated indirect branch predictors than modelled by
1078 Cachegrind. </p>
1079 <p>Cachegrind does not simulate a return stack predictor. It
1080 assumes that processors perfectly predict function return addresses,
1081 an assumption which is probably close to being true.</p>
1082 <p>See Hennessy and Patterson's classic text "Computer
1083 Architecture: A Quantitative Approach", 4th edition (2007), Section
1084 2.3 (pages 80-89) for background on modern branch predictors.</p>
1085 </div>
1086 <div class="sect2">
1087 <div class="titlepage"><div><div><h3 class="title">
1088 <a name="cg-manual.annopts.accuracy"></a>5.8.3. Accuracy</h3></div></div></div>
1089 <p>
1090 Cachegrind's instruction counting has one shortcoming on x86/amd64:
1091 </p>
1092 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
1093 When a <code class="function">REP</code>-prefixed instruction executes each
1094 iteration is counted separately. In contrast, hardware counters count each
1095 such instruction just once, no matter how many times it iterates. It is
1096 arguable that Cachegrind's behaviour is more useful.
1097 </p></li></ul></div>
1098 <p>
1099 Cachegrind's cache profiling has a number of shortcomings:
1100 </p>
1101 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
1102 <li class="listitem"><p>
1103 It doesn't account for kernel activity. The effect of system calls on the
1104 cache and branch predictor contents is ignored.
1105 </p></li>
1106 <li class="listitem"><p>
1107 It doesn't account for other process activity. This is arguably desirable
1108 when considering a single program.
1109 </p></li>
1110 <li class="listitem"><p>It doesn't account for virtual-to-physical address
1111 mappings. Hence the simulation is not a true
1112 representation of what's happening in the
1113 cache. Most caches and branch predictors are physically indexed, but
1114 Cachegrind simulates caches using virtual addresses.</p></li>
1115 <li class="listitem"><p>It doesn't account for cache misses not visible at the
1116 instruction level, e.g. those arising from TLB misses, or
1117 speculative execution.</p></li>
1118 <li class="listitem"><p>Valgrind will schedule
1119 threads differently from how they would be when running natively.
1120 This could warp the results for threaded programs.</p></li>
1121 <li class="listitem">
1122 <p>
1123 The x86/amd64 instructions <code class="computeroutput">bts</code>,
1124 <code class="computeroutput">btr</code> and
1125 <code class="computeroutput">btc</code> will incorrectly be counted as doing a
1126 data read if both the arguments are registers, e.g.:
1127 </p>
1128 <pre class="programlisting">
1129 btsl %eax, %edx</pre>
1130 <p>
1131 This should only happen rarely.
1132 </p>
1133 </li>
1134 <li class="listitem"><p>x86/amd64 FPU instructions with data sizes of 28 and 108 bytes
1135 (e.g. <code class="computeroutput">fsave</code>) are treated as
1136 though they only access 16 bytes. These instructions seem to
1137 be rare so hopefully this won't affect accuracy much.</p></li>
1138 </ul></div>
1139 <p>Another thing worth noting is that results are very sensitive.
1140 Changing the size of the executable being profiled, or the sizes
1141 of any of the shared libraries it uses, or even the length of their
1142 file names, can perturb the results. Variations will be small, but
1143 don't expect perfectly repeatable results if your program changes at
1144 all.</p>
1145 <p>
1146 Many Linux distributions perform address space layout randomisation (ASLR), in
1147 which identical runs of the same program have their shared libraries loaded at
1148 different locations, as a security measure. This also perturbs the
1149 results.
1150 </p>
1151 </div>
1152 </div>
1153 <div class="sect1">
1154 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
1155 <a name="cg-manual.impl-details"></a>5.9. Implementation Details</h2></div></div></div>
1156 <p>
1157 This section talks about details you don't need to know about in order to
1158 use Cachegrind, but may be of interest to some people.
1159 </p>
1160 <div class="sect2">
1161 <div class="titlepage"><div><div><h3 class="title">
1162 <a name="cg-manual.impl-details.how-cg-works"></a>5.9.1. How Cachegrind Works</h3></div></div></div>
1163 <p>The best reference for understanding how Cachegrind works is chapter 3 of
1164 "Dynamic Binary Analysis and Instrumentation", by Nicholas Nethercote. It
1165 is available on the <a class="ulink" href="http://www.valgrind.org/docs/pubs.html" target="_top">Valgrind publications
1166 page</a>.</p>
1167 </div>
1168 <div class="sect2">
1169 <div class="titlepage"><div><div><h3 class="title">
1170 <a name="cg-manual.impl-details.file-format"></a>5.9.2. Cachegrind Output File Format</h3></div></div></div>
1171 <p>The file format is fairly straightforward, basically giving the
1172 cost centre for every line, grouped by files and
1173 functions. It's also totally generic and self-describing, in the sense that
1174 it can be used for any events that can be counted on a line-by-line basis,
1175 not just cache and branch predictor events. For example, earlier versions
1176 of Cachegrind didn't have a branch predictor simulation. When this was
1177 added, the file format didn't need to change at all. So the format (and
1178 consequently, cg_annotate) could be used by other tools.</p>
1179 <p>The file format:</p>
1180 <pre class="programlisting">
1181 file ::= desc_line* cmd_line events_line data_line+ summary_line
1182 desc_line ::= "desc:" ws? non_nl_string
1183 cmd_line ::= "cmd:" ws? cmd
1184 events_line ::= "events:" ws? (event ws)+
1185 data_line ::= file_line | fn_line | count_line
1186 file_line ::= "fl=" filename
1187 fn_line ::= "fn=" fn_name
1188 count_line ::= line_num (ws+ count)* ws*
1189 summary_line ::= "summary:" ws? count (ws+ count)+ ws*
1190 count ::= num</pre>
1191 <p>Where:</p>
1192 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
1193 <li class="listitem"><p><code class="computeroutput">non_nl_string</code> is any
1194 string not containing a newline.</p></li>
1195 <li class="listitem"><p><code class="computeroutput">cmd</code> is a string holding the
1196 command line of the profiled program.</p></li>
1197 <li class="listitem"><p><code class="computeroutput">event</code> is a string containing
1198 no whitespace.</p></li>
1199 <li class="listitem"><p><code class="computeroutput">filename</code> and
1200 <code class="computeroutput">fn_name</code> are strings.</p></li>
1201 <li class="listitem"><p><code class="computeroutput">num</code> and
1202 <code class="computeroutput">line_num</code> are decimal
1203 numbers.</p></li>
1204 <li class="listitem"><p><code class="computeroutput">ws</code> is whitespace.</p></li>
1205 </ul></div>
1206 <p>The contents of the "desc:" lines are printed out at the top
1207 of the summary. This is a generic way of providing simulation
1208 specific information, e.g. for giving the cache configuration for
1209 cache simulation.</p>
1210 <p>More than one line of info can be present for each file/fn/line number.
1211 In such cases, the counts for the named events will be accumulated.</p>
1212 <p>The number of counts in each
1213 <code class="computeroutput">line</code> and the
1214 <code class="computeroutput">summary_line</code> should not exceed
1215 the number of events in the
1216 <code class="computeroutput">event_line</code>. If the number in
1217 each <code class="computeroutput">line</code> is less, cg_annotate
1218 treats those missing as though they were a "0" entry. This can reduce
1219 file size.
1220 </p>
1221 <p>A <code class="computeroutput">file_line</code> changes the
1222 current file name. A <code class="computeroutput">fn_line</code>
1223 changes the current function name. A
1224 <code class="computeroutput">count_line</code> contains counts that
1225 pertain to the current filename/fn_name. A "fn="
1226 <code class="computeroutput">file_line</code> and a
1227 <code class="computeroutput">fn_line</code> must appear before any
1228 <code class="computeroutput">count_line</code>s to give the context
1229 of the first <code class="computeroutput">count_line</code>s.</p>
1230 <p>Similarly, each <code class="computeroutput">file_line</code> must be
1231 immediately followed by a <code class="computeroutput">fn_line</code>.
1232 </p>
1233 <p>The summary line is redundant, because it just holds the total counts
1234 for each event. But this serves as a useful sanity check of the data; if
1235 the totals for each event don't match the summary line, something has gone
1236 wrong.</p>
1237 </div>
1238 </div>
1239 </div>
1240 <div>
1241 <br><table class="nav" width="100%" cellspacing="3" cellpadding="2" border="0" summary="Navigation footer">
1242 <tr>
1243 <td rowspan="2" width="40%" align="left">
1244 <a accesskey="p" href="mc-manual.html">&lt;&lt; 4. Memcheck: a memory error detector</a> </td>
1245 <td width="20%" align="center"><a accesskey="u" href="manual.html">Up</a></td>
1246 <td rowspan="2" width="40%" align="right"> <a accesskey="n" href="cl-manual.html">6. Callgrind: a call-graph generating cache and branch prediction profiler &gt;&gt;</a>
1247 </td>
1248 </tr>
1249 <tr><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td></tr>
1250 </table>
1251 </div>
1252 </body>
1253 </html>