blob: e8ab23db512e457bd240fdf33b12069751e2e913 [file] [log] [blame]
njn3e986b22004-11-30 10:43:45 +00001<?xml version="1.0"?> <!-- -*- sgml -*- -->
2<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
sewardj7aeb10f2006-12-10 02:59:16 +00003 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd"
4[ <!ENTITY % vg-entities SYSTEM "../../docs/xml/vg-entities.xml"> %vg-entities; ]>
njn3e986b22004-11-30 10:43:45 +00005
de03e0e7c2005-12-03 23:02:33 +00006
njn05a89172009-07-29 02:36:21 +00007<chapter id="cg-manual" xreflabel="Cachegrind: a cache and branch-prediction profiler">
8<title>Cachegrind: a cache and branch-prediction profiler</title>
njn3e986b22004-11-30 10:43:45 +00009
10<para>To use this tool, you must specify
njn7e5d4ed2009-07-30 02:57:52 +000011<option>--tool=cachegrind</option> on the
njn3e986b22004-11-30 10:43:45 +000012Valgrind command line.</para>
13
njn05a89172009-07-29 02:36:21 +000014<sect1 id="cg-manual.overview" xreflabel="Overview">
15<title>Overview</title>
16
17<para>Cachegrind simulates how your program interacts with a machine's cache
njn3da81962009-08-07 00:18:25 +000018hierarchy and (optionally) branch predictor. It simulates a machine with
19independent first level instruction and data caches (I1 and D1), backed by a
20unified second level cache (L2). This configuration is used by almost all
21modern machines.</para>
22
23<para>
24It gathers the following statistics (abbreviations used for each statistic
25is given in parentheses):</para>
njn3e986b22004-11-30 10:43:45 +000026<itemizedlist>
27 <listitem>
njn3da81962009-08-07 00:18:25 +000028 <para>I cache reads (<computeroutput>Ir</computeroutput>,
29 which equals the number of instructions executed),
30 I1 cache read misses (<computeroutput>I1mr</computeroutput>) and
31 L2 cache instruction read misses (<computeroutput>I1mr</computeroutput>).
32 </para>
njn3e986b22004-11-30 10:43:45 +000033 </listitem>
34 <listitem>
njn3da81962009-08-07 00:18:25 +000035 <para>D cache reads (<computeroutput>Dr</computeroutput>, which
36 equals the number of memory reads),
37 D1 cache read misses (<computeroutput>D1mr</computeroutput>), and
38 L2 cache data read misses (<computeroutput>D2mr</computeroutput>).
39 </para>
njn3e986b22004-11-30 10:43:45 +000040 </listitem>
41 <listitem>
njn3da81962009-08-07 00:18:25 +000042 <para>D cache writes (<computeroutput>Dw</computeroutput>, which equals
43 the number of memory writes),
44 D1 cache write misses (<computeroutput>D1mw</computeroutput>), and
45 L2 cache data write misses (<computeroutput>D2mw</computeroutput>).
46 </para>
njn3e986b22004-11-30 10:43:45 +000047 </listitem>
sewardj8badbaa2007-05-08 09:20:25 +000048 <listitem>
njn3da81962009-08-07 00:18:25 +000049 <para>Conditional branches executed (<computeroutput>Bc</computeroutput>) and
50 conditional branches mispredicted (<computeroutput>Bcm</computeroutput>).
51 </para>
sewardj8badbaa2007-05-08 09:20:25 +000052 </listitem>
53 <listitem>
njn3da81962009-08-07 00:18:25 +000054 <para>Indirect branches executed (<computeroutput>Bi</computeroutput>) and
55 indirect branches mispredicted (<computeroutput>Bim</computeroutput>).
56 </para>
sewardj8badbaa2007-05-08 09:20:25 +000057 </listitem>
njn3e986b22004-11-30 10:43:45 +000058</itemizedlist>
59
njn3da81962009-08-07 00:18:25 +000060<para>Note that D1 total accesses is given by
61<computeroutput>D1mr</computeroutput> +
62<computeroutput>D1mw</computeroutput>, and that L2 total
63accesses is given by <computeroutput>I2mr</computeroutput> +
64<computeroutput>D2mr</computeroutput> +
65<computeroutput>D2mw</computeroutput>.
66</para>
67
njn05a89172009-07-29 02:36:21 +000068<para>These statistics are presented for the entire program and for each
69function in the program. You can also annotate each line of source code in
70the program with the counts that were caused directly by it.</para>
71
njnc8cccb12005-07-25 23:30:24 +000072<para>On a modern machine, an L1 miss will typically cost
sewardj8badbaa2007-05-08 09:20:25 +000073around 10 cycles, an L2 miss can cost as much as 200
74cycles, and a mispredicted branch costs in the region of 10
75to 30 cycles. Detailed cache and branch profiling can be very useful
njn05a89172009-07-29 02:36:21 +000076for understanding how your program interacts with the machine and thus how
77to make it faster.</para>
njn3e986b22004-11-30 10:43:45 +000078
79<para>Also, since one instruction cache read is performed per
80instruction executed, you can find out how many instructions are
njn05a89172009-07-29 02:36:21 +000081executed per line, which can be useful for traditional profiling.</para>
njn3e986b22004-11-30 10:43:45 +000082
njn3e986b22004-11-30 10:43:45 +000083</sect1>
84
85
86
njn3da81962009-08-07 00:18:25 +000087<sect1 id="cg-manual.profile"
88 xreflabel="Using Cachegrind, cg_annotate and cg_merge">
89<title>Using Cachegrind, cg_annotate and cg_merge</title>
njn3e986b22004-11-30 10:43:45 +000090
njn3da81962009-08-07 00:18:25 +000091<para>First off, as for normal Valgrind use, you probably want to
92compile with debugging info (the
njna3311642009-08-10 01:29:14 +000093<option>-g</option> option). But by contrast with
njn3da81962009-08-07 00:18:25 +000094normal Valgrind use, you probably do want to turn
95optimisation on, since you should profile your program as it will
96be normally run.</para>
njn3e986b22004-11-30 10:43:45 +000097
njn3da81962009-08-07 00:18:25 +000098<para>Then, you need to run Cachegrind itself to gather the profiling
99information, and then run cg_annotate to get a detailed presentation of that
100information. As an optional intermediate step, you can use cg_merge to sum
101together the outputs of multiple Cachegrind runs, into a single file which
102you then use as the input for cg_annotate.</para>
103
104
105<sect2 id="cg-manual.running-cachegrind" xreflabel="Running Cachegrind">
106<title>Running Cachegrind</title>
107
108<para>To run Cachegrind on a program <filename>prog</filename>, run:</para>
109<screen><![CDATA[
110valgrind --tool=cachegrind prog
111]]></screen>
njn3e986b22004-11-30 10:43:45 +0000112
113<para>The program will execute (slowly). Upon completion,
114summary statistics that look like this will be printed:</para>
115
116<programlisting><![CDATA[
117==31751== I refs: 27,742,716
118==31751== I1 misses: 276
njn3da81962009-08-07 00:18:25 +0000119==31751== L2i misses: 275
njn3e986b22004-11-30 10:43:45 +0000120==31751== I1 miss rate: 0.0%
121==31751== L2i miss rate: 0.0%
122==31751==
123==31751== D refs: 15,430,290 (10,955,517 rd + 4,474,773 wr)
124==31751== D1 misses: 41,185 ( 21,905 rd + 19,280 wr)
njn3da81962009-08-07 00:18:25 +0000125==31751== L2d misses: 23,085 ( 3,987 rd + 19,098 wr)
njn3e986b22004-11-30 10:43:45 +0000126==31751== D1 miss rate: 0.2% ( 0.1% + 0.4%)
127==31751== L2d miss rate: 0.1% ( 0.0% + 0.4%)
128==31751==
129==31751== L2 misses: 23,360 ( 4,262 rd + 19,098 wr)
130==31751== L2 miss rate: 0.0% ( 0.0% + 0.4%)]]></programlisting>
131
132<para>Cache accesses for instruction fetches are summarised
133first, giving the number of fetches made (this is the number of
134instructions executed, which can be useful to know in its own
135right), the number of I1 misses, and the number of L2 instruction
136(<computeroutput>L2i</computeroutput>) misses.</para>
137
138<para>Cache accesses for data follow. The information is similar
139to that of the instruction fetches, except that the values are
140also shown split between reads and writes (note each row's
141<computeroutput>rd</computeroutput> and
142<computeroutput>wr</computeroutput> values add up to the row's
143total).</para>
144
145<para>Combined instruction and data figures for the L2 cache
njn3da81962009-08-07 00:18:25 +0000146follow that. Note that the L2 miss rate is computed relative to the total
147number of memory accesses, not the number of L1 misses. I.e. it is
148<computeroutput>(I2mr + D2mr + D2mw) / (Ir + Dr + Dw)</computeroutput>
149not
150<computeroutput>(I2mr + D2mr + D2mw) / (I1mr + D1mr + D1mw)</computeroutput>
151</para>
152
153<para>Branch prediction statistics are not collected by default.
njna3311642009-08-10 01:29:14 +0000154To do so, add the option <option>--branch-sim=yes</option>.</para>
njn3da81962009-08-07 00:18:25 +0000155
156</sect2>
njn3e986b22004-11-30 10:43:45 +0000157
158
njn3da81962009-08-07 00:18:25 +0000159<sect2 id="cg-manual.outputfile" xreflabel="Output File">
160<title>Output File</title>
njn3e986b22004-11-30 10:43:45 +0000161
njn3da81962009-08-07 00:18:25 +0000162<para>As well as printing summary information, Cachegrind also writes
163more detailed profiling information to a file. By default this file is named
164<filename>cachegrind.out.&lt;pid&gt;</filename> (where
165<filename>&lt;pid&gt;</filename> is the program's process ID), but its name
166can be changed with the <option>--cachegrind-out-file</option> option. This
167file is human-readable, but is intended to be interpreted by the
168accompanying program cg_annotate, described in the next section.</para>
njn3e986b22004-11-30 10:43:45 +0000169
njn374a36d2007-11-23 01:41:32 +0000170<para>The default <computeroutput>.&lt;pid&gt;</computeroutput> suffix
de7e109d12005-11-18 22:09:58 +0000171on the output file name serves two purposes. Firstly, it means you
172don't have to rename old log files that you don't want to overwrite.
173Secondly, and more importantly, it allows correct profiling with the
njn7e5d4ed2009-07-30 02:57:52 +0000174<option>--trace-children=yes</option> option of
njn3e986b22004-11-30 10:43:45 +0000175programs that spawn child processes.</para>
176
njn3da81962009-08-07 00:18:25 +0000177<para>The output file can be big, many megabytes for large applications
178built with full debugging information.</para>
njn3e986b22004-11-30 10:43:45 +0000179
180</sect2>
181
182
183
njn3da81962009-08-07 00:18:25 +0000184<sect2 id="cg-manual.running-cg_annotate" xreflabel="Running cg_annotate">
185<title>Running cg_annotate</title>
njn3e986b22004-11-30 10:43:45 +0000186
njn374a36d2007-11-23 01:41:32 +0000187<para>Before using cg_annotate,
njn3e986b22004-11-30 10:43:45 +0000188it is worth widening your window to be at least 120-characters
189wide if possible, as the output lines can be quite long.</para>
190
njn3da81962009-08-07 00:18:25 +0000191<para>To get a function-by-function summary, run:</para>
njn3e986b22004-11-30 10:43:45 +0000192
njn3da81962009-08-07 00:18:25 +0000193<screen>cg_annotate &lt;filename&gt;</screen>
194
195<para>on a Cachegrind output file.</para>
196
197</sect2>
198
199
200<sect2 id="cg-manual.the-output-preamble" xreflabel="The Output Preamble">
201<title>The Output Preamble</title>
202
203<para>The first part of the output looks like this:</para>
njn3e986b22004-11-30 10:43:45 +0000204
205<programlisting><![CDATA[
206--------------------------------------------------------------------------------
207I1 cache: 65536 B, 64 B, 2-way associative
208D1 cache: 65536 B, 64 B, 2-way associative
209L2 cache: 262144 B, 64 B, 8-way associative
210Command: concord vg_to_ucode.c
211Events recorded: Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw
212Events shown: Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw
213Event sort order: Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw
214Threshold: 99%
215Chosen for annotation:
njn3da81962009-08-07 00:18:25 +0000216Auto-annotation: off
217]]></programlisting>
njn3e986b22004-11-30 10:43:45 +0000218
219
njn3da81962009-08-07 00:18:25 +0000220<para>This is a summary of the annotation options:</para>
njn3e986b22004-11-30 10:43:45 +0000221
222<itemizedlist>
223
224 <listitem>
225 <para>I1 cache, D1 cache, L2 cache: cache configuration. So
226 you know the configuration with which these results were
227 obtained.</para>
228 </listitem>
229
230 <listitem>
231 <para>Command: the command line invocation of the program
232 under examination.</para>
233 </listitem>
234
235 <listitem>
njn3da81962009-08-07 00:18:25 +0000236 <para>Events recorded: which events were recorded.</para>
njn3e986b22004-11-30 10:43:45 +0000237
njn3e986b22004-11-30 10:43:45 +0000238 </listitem>
239
240 <listitem>
sewardj08e31e22007-05-23 21:58:33 +0000241 <para>Events shown: the events shown, which is a subset of the events
242 gathered. This can be adjusted with the
njn7e5d4ed2009-07-30 02:57:52 +0000243 <option>--show</option> option.</para>
njn3e986b22004-11-30 10:43:45 +0000244 </listitem>
245
246 <listitem>
247 <para>Event sort order: the sort order in which functions are
248 shown. For example, in this case the functions are sorted
249 from highest <computeroutput>Ir</computeroutput> counts to
250 lowest. If two functions have identical
251 <computeroutput>Ir</computeroutput> counts, they will then be
252 sorted by <computeroutput>I1mr</computeroutput> counts, and
253 so on. This order can be adjusted with the
njn7e5d4ed2009-07-30 02:57:52 +0000254 <option>--sort</option> option.</para>
njn3e986b22004-11-30 10:43:45 +0000255
256 <para>Note that this dictates the order the functions appear.
njn3da81962009-08-07 00:18:25 +0000257 It is <emphasis>not</emphasis> the order in which the columns
njn3e986b22004-11-30 10:43:45 +0000258 appear; that is dictated by the "events shown" line (and can
njn7e5d4ed2009-07-30 02:57:52 +0000259 be changed with the <option>--show</option>
njn3e986b22004-11-30 10:43:45 +0000260 option).</para>
261 </listitem>
262
263 <listitem>
njn374a36d2007-11-23 01:41:32 +0000264 <para>Threshold: cg_annotate
sewardj08e31e22007-05-23 21:58:33 +0000265 by default omits functions that cause very low counts
266 to avoid drowning you in information. In this case,
njn3e986b22004-11-30 10:43:45 +0000267 cg_annotate shows summaries the functions that account for
268 99% of the <computeroutput>Ir</computeroutput> counts;
269 <computeroutput>Ir</computeroutput> is chosen as the
270 threshold event since it is the primary sort event. The
271 threshold can be adjusted with the
njn7e5d4ed2009-07-30 02:57:52 +0000272 <option>--threshold</option>
njn3e986b22004-11-30 10:43:45 +0000273 option.</para>
274 </listitem>
275
276 <listitem>
277 <para>Chosen for annotation: names of files specified
278 manually for annotation; in this case none.</para>
279 </listitem>
280
281 <listitem>
282 <para>Auto-annotation: whether auto-annotation was requested
njn7e5d4ed2009-07-30 02:57:52 +0000283 via the <option>--auto=yes</option>
njn3e986b22004-11-30 10:43:45 +0000284 option. In this case no.</para>
285 </listitem>
286
287</itemizedlist>
288
njn3da81962009-08-07 00:18:25 +0000289</sect2>
290
291
292<sect2 id="cg-manual.the-global"
293 xreflabel="The Global and Function-level Counts">
294<title>The Global and Function-level Counts</title>
295
njn3e986b22004-11-30 10:43:45 +0000296<para>Then follows summary statistics for the whole
njn3da81962009-08-07 00:18:25 +0000297program:</para>
njn3e986b22004-11-30 10:43:45 +0000298
njn3da81962009-08-07 00:18:25 +0000299<programlisting><![CDATA[
300--------------------------------------------------------------------------------
301Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw
302--------------------------------------------------------------------------------
30327,742,716 276 275 10,955,517 21,905 3,987 4,474,773 19,280 19,098 PROGRAM TOTALS]]></programlisting>
304
305<para>
306These are similar to the summary provided when Cachegrind finishes running.
307</para>
308
309<para>Then comes function-by-function statistics:</para>
310
311<programlisting><![CDATA[
312--------------------------------------------------------------------------------
313Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw file:function
314--------------------------------------------------------------------------------
3158,821,482 5 5 2,242,702 1,621 73 1,794,230 0 0 getc.c:_IO_getc
3165,222,023 4 4 2,276,334 16 12 875,959 1 1 concord.c:get_word
3172,649,248 2 2 1,344,810 7,326 1,385 . . . vg_main.c:strcmp
3182,521,927 2 2 591,215 0 0 179,398 0 0 concord.c:hash
3192,242,740 2 2 1,046,612 568 22 448,548 0 0 ctype.c:tolower
3201,496,937 4 4 630,874 9,000 1,400 279,388 0 0 concord.c:insert
321 897,991 51 51 897,831 95 30 62 1 1 ???:???
322 598,068 1 1 299,034 0 0 149,517 0 0 ../sysdeps/generic/lockfile.c:__flockfile
323 598,068 0 0 299,034 0 0 149,517 0 0 ../sysdeps/generic/lockfile.c:__funlockfile
324 598,024 4 4 213,580 35 16 149,506 0 0 vg_clientmalloc.c:malloc
325 446,587 1 1 215,973 2,167 430 129,948 14,057 13,957 concord.c:add_existing
326 341,760 2 2 128,160 0 0 128,160 0 0 vg_clientmalloc.c:vg_trap_here_WRAPPER
327 320,782 4 4 150,711 276 0 56,027 53 53 concord.c:init_hash_table
328 298,998 1 1 106,785 0 0 64,071 1 1 concord.c:create
329 149,518 0 0 149,516 0 0 1 0 0 ???:tolower@@GLIBC_2.0
330 149,518 0 0 149,516 0 0 1 0 0 ???:fgetc@@GLIBC_2.0
331 95,983 4 4 38,031 0 0 34,409 3,152 3,150 concord.c:new_word_node
332 85,440 0 0 42,720 0 0 21,360 0 0 vg_clientmalloc.c:vg_bogus_epilogue]]></programlisting>
333
334<para>Each function
njn3e986b22004-11-30 10:43:45 +0000335is identified by a
336<computeroutput>file_name:function_name</computeroutput> pair. If
337a column contains only a dot it means the function never performs
njn3da81962009-08-07 00:18:25 +0000338that event (e.g. the third row shows that
njn3e986b22004-11-30 10:43:45 +0000339<computeroutput>strcmp()</computeroutput> contains no
340instructions that write to memory). The name
341<computeroutput>???</computeroutput> is used if the the file name
342and/or function name could not be determined from debugging
343information. If most of the entries have the form
344<computeroutput>???:???</computeroutput> the program probably
njn3da81962009-08-07 00:18:25 +0000345wasn't compiled with <option>-g</option>.</para>
njn3e986b22004-11-30 10:43:45 +0000346
sewardj08e31e22007-05-23 21:58:33 +0000347<para>It is worth noting that functions will come both from
njn3da81962009-08-07 00:18:25 +0000348the profiled program (e.g. <filename>concord.c</filename>)
349and from libraries (e.g. <filename>getc.c</filename>)</para>
njn3e986b22004-11-30 10:43:45 +0000350
njn3da81962009-08-07 00:18:25 +0000351</sect2>
352
353
354<sect2 id="cg-manual.line-by-line" xreflabel="Line-by-line Counts">
355<title>Line-by-line Counts</title>
356
357<para>There are two ways to annotate source files -- by specifying them
358manually as arguments to cg_annotate, or with the
359<option>--auto=yes</option> option. For example, the output from running
360<filename>cg_annotate &lt;filename&gt; concord.c</filename> for our example
361produces the same output as above followed by an annotated version of
362<filename>concord.c</filename>, a section of which looks like:</para>
njn3e986b22004-11-30 10:43:45 +0000363
364<programlisting><![CDATA[
365--------------------------------------------------------------------------------
366-- User-annotated source: concord.c
367--------------------------------------------------------------------------------
368Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw
369
njn3e986b22004-11-30 10:43:45 +0000370 . . . . . . . . . void init_hash_table(char *file_name, Word_Node *table[])
371 3 1 1 . . . 1 0 0 {
372 . . . . . . . . . FILE *file_ptr;
373 . . . . . . . . . Word_Info *data;
374 1 0 0 . . . 1 1 1 int line = 1, i;
375 . . . . . . . . .
376 5 0 0 . . . 3 0 0 data = (Word_Info *) create(sizeof(Word_Info));
377 . . . . . . . . .
378 4,991 0 0 1,995 0 0 998 0 0 for (i = 0; i < TABLE_SIZE; i++)
379 3,988 1 1 1,994 0 0 997 53 52 table[i] = NULL;
380 . . . . . . . . .
381 . . . . . . . . . /* Open file, check it. */
382 6 0 0 1 0 0 4 0 0 file_ptr = fopen(file_name, "r");
383 2 0 0 1 0 0 . . . if (!(file_ptr)) {
384 . . . . . . . . . fprintf(stderr, "Couldn't open '%s'.\n", file_name);
385 1 1 1 . . . . . . exit(EXIT_FAILURE);
386 . . . . . . . . . }
387 . . . . . . . . .
388 165,062 1 1 73,360 0 0 91,700 0 0 while ((line = get_word(data, line, file_ptr)) != EOF)
389 146,712 0 0 73,356 0 0 73,356 0 0 insert(data->;word, data->line, table);
390 . . . . . . . . .
391 4 0 0 1 0 0 2 0 0 free(data);
392 4 0 0 1 0 0 2 0 0 fclose(file_ptr);
393 3 0 0 2 0 0 . . . }]]></programlisting>
394
395<para>(Although column widths are automatically minimised, a wide
396terminal is clearly useful.)</para>
397
398<para>Each source file is clearly marked
399(<computeroutput>User-annotated source</computeroutput>) as
400having been chosen manually for annotation. If the file was
401found in one of the directories specified with the
njn7e5d4ed2009-07-30 02:57:52 +0000402<option>-I</option>/<option>--include</option> option, the directory
njn3e986b22004-11-30 10:43:45 +0000403and file are both given.</para>
404
405<para>Each line is annotated with its event counts. Events not
sewardj08e31e22007-05-23 21:58:33 +0000406applicable for a line are represented by a dot. This is useful
njn3e986b22004-11-30 10:43:45 +0000407for distinguishing between an event which cannot happen, and one
408which can but did not.</para>
409
410<para>Sometimes only a small section of a source file is
sewardj8d9fec52005-11-15 20:56:23 +0000411executed. To minimise uninteresting output, Cachegrind only shows
njn3e986b22004-11-30 10:43:45 +0000412annotated lines and lines within a small distance of annotated
413lines. Gaps are marked with the line numbers so you know which
414part of a file the shown code comes from, eg:</para>
415
416<programlisting><![CDATA[
417(figures and code for line 704)
418-- line 704 ----------------------------------------
419-- line 878 ----------------------------------------
420(figures and code for line 878)]]></programlisting>
421
422<para>The amount of context to show around annotated lines is
njn7e5d4ed2009-07-30 02:57:52 +0000423controlled by the <option>--context</option>
njn3e986b22004-11-30 10:43:45 +0000424option.</para>
425
njn3da81962009-08-07 00:18:25 +0000426<para>To get automatic annotation, use the <option>--auto=yes</option> option.
njn3e986b22004-11-30 10:43:45 +0000427cg_annotate will automatically annotate every source file it can
428find that is mentioned in the function-by-function summary.
429Therefore, the files chosen for auto-annotation are affected by
njn7e5d4ed2009-07-30 02:57:52 +0000430the <option>--sort</option> and
431<option>--threshold</option> options. Each
njn3e986b22004-11-30 10:43:45 +0000432source file is clearly marked (<computeroutput>Auto-annotated
433source</computeroutput>) as being chosen automatically. Any
434files that could not be found are mentioned at the end of the
435output, eg:</para>
436
437<programlisting><![CDATA[
438------------------------------------------------------------------
439The following files chosen for auto-annotation could not be found:
440------------------------------------------------------------------
441 getc.c
442 ctype.c
443 ../sysdeps/generic/lockfile.c]]></programlisting>
444
445<para>This is quite common for library files, since libraries are
446usually compiled with debugging information, but the source files
447are often not present on a system. If a file is chosen for
njn3da81962009-08-07 00:18:25 +0000448annotation both manually and automatically, it
njn3e986b22004-11-30 10:43:45 +0000449is marked as <computeroutput>User-annotated
njn7e5d4ed2009-07-30 02:57:52 +0000450source</computeroutput>. Use the
451<option>-I</option>/<option>--include</option> option to tell Valgrind where
452to look for source files if the filenames found from the debugging
njn3e986b22004-11-30 10:43:45 +0000453information aren't specific enough.</para>
454
455<para>Beware that cg_annotate can take some time to digest large
njn3da81962009-08-07 00:18:25 +0000456<filename>cachegrind.out.&lt;pid&gt;</filename> files,
njn3e986b22004-11-30 10:43:45 +0000457e.g. 30 seconds or more. Also beware that auto-annotation can
458produce a lot of output if your program is large!</para>
459
460</sect2>
461
462
njn3da81962009-08-07 00:18:25 +0000463<sect2 id="cg-manual.assembler" xreflabel="Annotating Assembly Code Programs">
464<title>Annotating Assembly Code Programs</title>
njn3e986b22004-11-30 10:43:45 +0000465
njn374a36d2007-11-23 01:41:32 +0000466<para>Valgrind can annotate assembly code programs too, or annotate
467the assembly code generated for your C program. Sometimes this is
njn3e986b22004-11-30 10:43:45 +0000468useful for understanding what is really happening when an
469interesting line of C code is translated into multiple
470instructions.</para>
471
472<para>To do this, you just need to assemble your
njn85a38bc2008-10-30 02:41:13 +0000473<computeroutput>.s</computeroutput> files with assembly-level debug
njn7316df22009-08-04 01:16:01 +0000474information. You can use compile with the <option>-S</option> to compile C/C++
475programs to assembly code, and then assemble the assembly code files with
476<option>-g</option> to achieve this. You can then profile and annotate the
477assembly code source files in the same way as C/C++ source files.</para>
njn3e986b22004-11-30 10:43:45 +0000478
479</sect2>
480
njn7064fb22008-05-29 23:09:52 +0000481<sect2 id="ms-manual.forkingprograms" xreflabel="Forking Programs">
482<title>Forking Programs</title>
483<para>If your program forks, the child will inherit all the profiling data that
484has been gathered for the parent.</para>
485
486<para>If the output file format string (controlled by
487<option>--cachegrind-out-file</option>) does not contain <option>%p</option>,
488then the outputs from the parent and child will be intermingled in a single
489output file, which will almost certainly make it unreadable by
490cg_annotate.</para>
491</sect2>
492
493
njn3da81962009-08-07 00:18:25 +0000494<sect2 id="cg-manual.annopts.warnings" xreflabel="cg_annotate Warnings">
495<title>cg_annotate Warnings</title>
njn3e986b22004-11-30 10:43:45 +0000496
497<para>There are a couple of situations in which
njn374a36d2007-11-23 01:41:32 +0000498cg_annotate issues warnings.</para>
njn3e986b22004-11-30 10:43:45 +0000499
500<itemizedlist>
501 <listitem>
502 <para>If a source file is more recent than the
njn3da81962009-08-07 00:18:25 +0000503 <filename>cachegrind.out.&lt;pid&gt;</filename> file.
njn3e986b22004-11-30 10:43:45 +0000504 This is because the information in
njn3da81962009-08-07 00:18:25 +0000505 <filename>cachegrind.out.&lt;pid&gt;</filename> is only
njn3e986b22004-11-30 10:43:45 +0000506 recorded with line numbers, so if the line numbers change at
njn3da81962009-08-07 00:18:25 +0000507 all in the source (e.g. lines added, deleted, swapped), any
njn3e986b22004-11-30 10:43:45 +0000508 annotations will be incorrect.</para>
509 </listitem>
510 <listitem>
511 <para>If information is recorded about line numbers past the
512 end of a file. This can be caused by the above problem,
njn3da81962009-08-07 00:18:25 +0000513 i.e. shortening the source file while using an old
514 <filename>cachegrind.out.&lt;pid&gt;</filename> file. If
njn3e986b22004-11-30 10:43:45 +0000515 this happens, the figures for the bogus lines are printed
516 anyway (clearly marked as bogus) in case they are
517 important.</para>
518 </listitem>
519</itemizedlist>
520
521</sect2>
522
523
524
sewardj778d7832007-11-22 01:21:56 +0000525<sect2 id="cg-manual.annopts.things-to-watch-out-for"
njn3da81962009-08-07 00:18:25 +0000526 xreflabel="Unusual Annotation Cases">
527<title>Unusual Annotation Cases</title>
njn3e986b22004-11-30 10:43:45 +0000528
529<para>Some odd things that can occur during annotation:</para>
530
531<itemizedlist>
532 <listitem>
533 <para>If annotating at the assembler level, you might see
534 something like this:</para>
535<programlisting><![CDATA[
536 1 0 0 . . . . . . leal -12(%ebp),%eax
537 1 0 0 . . . 1 0 0 movl %eax,84(%ebx)
538 2 0 0 0 0 0 1 0 0 movl $1,-20(%ebp)
539 . . . . . . . . . .align 4,0x90
540 1 0 0 . . . . . . movl $.LnrB,%eax
541 1 0 0 . . . 1 0 0 movl %eax,-16(%ebp)]]></programlisting>
542
543 <para>How can the third instruction be executed twice when
544 the others are executed only once? As it turns out, it
545 isn't. Here's a dump of the executable, using
546 <computeroutput>objdump -d</computeroutput>:</para>
547<programlisting><![CDATA[
548 8048f25: 8d 45 f4 lea 0xfffffff4(%ebp),%eax
549 8048f28: 89 43 54 mov %eax,0x54(%ebx)
550 8048f2b: c7 45 ec 01 00 00 00 movl $0x1,0xffffffec(%ebp)
551 8048f32: 89 f6 mov %esi,%esi
552 8048f34: b8 08 8b 07 08 mov $0x8078b08,%eax
553 8048f39: 89 45 f0 mov %eax,0xfffffff0(%ebp)]]></programlisting>
554
555 <para>Notice the extra <computeroutput>mov
556 %esi,%esi</computeroutput> instruction. Where did this come
557 from? The GNU assembler inserted it to serve as the two
558 bytes of padding needed to align the <computeroutput>movl
559 $.LnrB,%eax</computeroutput> instruction on a four-byte
560 boundary, but pretended it didn't exist when adding debug
561 information. Thus when Valgrind reads the debug info it
562 thinks that the <computeroutput>movl
563 $0x1,0xffffffec(%ebp)</computeroutput> instruction covers the
564 address range 0x8048f2b--0x804833 by itself, and attributes
565 the counts for the <computeroutput>mov
566 %esi,%esi</computeroutput> to it.</para>
567 </listitem>
568
njn3da81962009-08-07 00:18:25 +0000569 <!--
570 I think this isn't true any more, not since cost centres were moved from
571 being associated with instruction addresses to being associated with
572 source line numbers.
njn3e986b22004-11-30 10:43:45 +0000573 <listitem>
574 <para>Inlined functions can cause strange results in the
575 function-by-function summary. If a function
576 <computeroutput>inline_me()</computeroutput> is defined in
577 <filename>foo.h</filename> and inlined in the functions
578 <computeroutput>f1()</computeroutput>,
579 <computeroutput>f2()</computeroutput> and
580 <computeroutput>f3()</computeroutput> in
581 <filename>bar.c</filename>, there will not be a
582 <computeroutput>foo.h:inline_me()</computeroutput> function
583 entry. Instead, there will be separate function entries for
njn3da81962009-08-07 00:18:25 +0000584 each inlining site, i.e.
njn3e986b22004-11-30 10:43:45 +0000585 <computeroutput>foo.h:f1()</computeroutput>,
586 <computeroutput>foo.h:f2()</computeroutput> and
587 <computeroutput>foo.h:f3()</computeroutput>. To find the
588 total counts for
589 <computeroutput>foo.h:inline_me()</computeroutput>, add up
590 the counts from each entry.</para>
591
592 <para>The reason for this is that although the debug info
njn7316df22009-08-04 01:16:01 +0000593 output by GCC indicates the switch from
njn3e986b22004-11-30 10:43:45 +0000594 <filename>bar.c</filename> to <filename>foo.h</filename>, it
595 doesn't indicate the name of the function in
596 <filename>foo.h</filename>, so Valgrind keeps using the old
597 one.</para>
598 </listitem>
njn3da81962009-08-07 00:18:25 +0000599 -->
njn3e986b22004-11-30 10:43:45 +0000600
601 <listitem>
602 <para>Sometimes, the same filename might be represented with
603 a relative name and with an absolute name in different parts
604 of the debug info, eg:
605 <filename>/home/user/proj/proj.h</filename> and
606 <filename>../proj.h</filename>. In this case, if you use
607 auto-annotation, the file will be annotated twice with the
608 counts split between the two.</para>
609 </listitem>
610
611 <listitem>
612 <para>Files with more than 65,535 lines cause difficulties
sewardj08e31e22007-05-23 21:58:33 +0000613 for the Stabs-format debug info reader. This is because the line
njn3e986b22004-11-30 10:43:45 +0000614 number in the <computeroutput>struct nlist</computeroutput>
615 defined in <filename>a.out.h</filename> under Linux is only a
616 16-bit value. Valgrind can handle some files with more than
617 65,535 lines correctly by making some guesses to identify
618 line number overflows. But some cases are beyond it, in
619 which case you'll get a warning message explaining that
620 annotations for the file might be incorrect.</para>
sewardj08e31e22007-05-23 21:58:33 +0000621
njn7316df22009-08-04 01:16:01 +0000622 <para>If you are using GCC 3.1 or later, this is most likely
623 irrelevant, since GCC switched to using the more modern DWARF2
sewardj08e31e22007-05-23 21:58:33 +0000624 format by default at version 3.1. DWARF2 does not have any such
625 limitations on line numbers.</para>
njn3e986b22004-11-30 10:43:45 +0000626 </listitem>
627
628 <listitem>
629 <para>If you compile some files with
njn7e5d4ed2009-07-30 02:57:52 +0000630 <option>-g</option> and some without, some
njn3e986b22004-11-30 10:43:45 +0000631 events that take place in a file without debug info could be
632 attributed to the last line of a file with debug info
633 (whichever one gets placed before the non-debug-info file in
634 the executable).</para>
635 </listitem>
636
637</itemizedlist>
638
639<para>This list looks long, but these cases should be fairly
640rare.</para>
641
njn3e986b22004-11-30 10:43:45 +0000642</sect2>
643
644
njn3da81962009-08-07 00:18:25 +0000645<sect2 id="cg-manual.cg_merge" xreflabel="cg_merge">
646<title>Merging Profiles with cg_merge</title>
sewardj94dc5082007-02-08 11:31:03 +0000647
648<para>
njn374a36d2007-11-23 01:41:32 +0000649cg_merge is a simple program which
njn3da81962009-08-07 00:18:25 +0000650reads multiple profile files, as created by Cachegrind, merges them
sewardj94dc5082007-02-08 11:31:03 +0000651together, and writes the results into another file in the same format.
652You can then examine the merged results using
njn374a36d2007-11-23 01:41:32 +0000653<computeroutput>cg_annotate &lt;filename&gt;</computeroutput>, as
sewardj94dc5082007-02-08 11:31:03 +0000654described above. The merging functionality might be useful if you
655want to aggregate costs over multiple runs of the same program, or
656from a single parallel run with multiple instances of the same
657program.</para>
658
659<para>
njn374a36d2007-11-23 01:41:32 +0000660cg_merge is invoked as follows:
sewardj94dc5082007-02-08 11:31:03 +0000661</para>
662
663<programlisting><![CDATA[
664cg_merge -o outputfile file1 file2 file3 ...]]></programlisting>
665
666<para>
667It reads and checks <computeroutput>file1</computeroutput>, then read
668and checks <computeroutput>file2</computeroutput> and merges it into
669the running totals, then the same with
670<computeroutput>file3</computeroutput>, etc. The final results are
671written to <computeroutput>outputfile</computeroutput>, or to standard
672out if no output file is specified.</para>
673
674<para>
675Costs are summed on a per-function, per-line and per-instruction
676basis. Because of this, the order in which the input files does not
677matter, although you should take care to only mention each file once,
678since any file mentioned twice will be added in twice.</para>
679
680<para>
njn374a36d2007-11-23 01:41:32 +0000681cg_merge does not attempt to check
sewardj94dc5082007-02-08 11:31:03 +0000682that the input files come from runs of the same executable. It will
683happily merge together profile files from completely unrelated
684programs. It does however check that the
685<computeroutput>Events:</computeroutput> lines of all the inputs are
686identical, so as to ensure that the addition of costs makes sense.
687For example, it would be nonsensical for it to add a number indicating
688D1 read references to a number from a different file indicating L2
689write misses.</para>
690
691<para>
692A number of other syntax and sanity checks are done whilst reading the
njn374a36d2007-11-23 01:41:32 +0000693inputs. cg_merge will stop and
sewardj94dc5082007-02-08 11:31:03 +0000694attempt to print a helpful error message if any of the input files
695fail these checks.</para>
696
njn3da81962009-08-07 00:18:25 +0000697</sect2>
698
699
sewardj94dc5082007-02-08 11:31:03 +0000700</sect1>
701
702
njn3da81962009-08-07 00:18:25 +0000703
njna3311642009-08-10 01:29:14 +0000704<sect1 id="cg-manual.cgopts" xreflabel="Cachegrind Command-line Options">
705<title>Cachegrind Command-line Options</title>
njn3da81962009-08-07 00:18:25 +0000706
707<!-- start of xi:include in the manpage -->
708<para>Cachegrind-specific options are:</para>
709
710<variablelist id="cg.opts.list">
711
712 <varlistentry id="opt.I1" xreflabel="--I1">
713 <term>
714 <option><![CDATA[--I1=<size>,<associativity>,<line size> ]]></option>
715 </term>
716 <listitem>
717 <para>Specify the size, associativity and line size of the level 1
718 instruction cache. </para>
719 </listitem>
720 </varlistentry>
721
722 <varlistentry id="opt.D1" xreflabel="--D1">
723 <term>
724 <option><![CDATA[--D1=<size>,<associativity>,<line size> ]]></option>
725 </term>
726 <listitem>
727 <para>Specify the size, associativity and line size of the level 1
728 data cache.</para>
729 </listitem>
730 </varlistentry>
731
732 <varlistentry id="opt.L2" xreflabel="--L2">
733 <term>
734 <option><![CDATA[--L2=<size>,<associativity>,<line size> ]]></option>
735 </term>
736 <listitem>
737 <para>Specify the size, associativity and line size of the level 2
738 cache.</para>
739 </listitem>
740 </varlistentry>
741
742 <varlistentry id="opt.cache-sim" xreflabel="--cache-sim">
743 <term>
744 <option><![CDATA[--cache-sim=no|yes [yes] ]]></option>
745 </term>
746 <listitem>
747 <para>Enables or disables collection of cache access and miss
748 counts.</para>
749 </listitem>
750 </varlistentry>
751
752 <varlistentry id="opt.branch-sim" xreflabel="--branch-sim">
753 <term>
754 <option><![CDATA[--branch-sim=no|yes [no] ]]></option>
755 </term>
756 <listitem>
757 <para>Enables or disables collection of branch instruction and
758 misprediction counts. By default this is disabled as it
759 slows Cachegrind down by approximately 25%. Note that you
760 cannot specify <option>--cache-sim=no</option>
761 and <option>--branch-sim=no</option>
762 together, as that would leave Cachegrind with no
763 information to collect.</para>
764 </listitem>
765 </varlistentry>
766
767 <varlistentry id="opt.cachegrind-out-file" xreflabel="--cachegrind-out-file">
768 <term>
769 <option><![CDATA[--cachegrind-out-file=<file> ]]></option>
770 </term>
771 <listitem>
772 <para>Write the profile data to
773 <computeroutput>file</computeroutput> rather than to the default
774 output file,
775 <filename>cachegrind.out.&lt;pid&gt;</filename>. The
776 <option>%p</option> and <option>%q</option> format specifiers
777 can be used to embed the process ID and/or the contents of an
778 environment variable in the name, as is the case for the core
779 option <option><xref linkend="opt.log-file"/></option>.
780 </para>
781 </listitem>
782 </varlistentry>
783
784</variablelist>
785<!-- end of xi:include in the manpage -->
786
787</sect1>
788
789
790
njna3311642009-08-10 01:29:14 +0000791<sect1 id="cg-manual.annopts" xreflabel="cg_annotate Command-line Options">
792<title>cg_annotate Command-line Options</title>
njn3da81962009-08-07 00:18:25 +0000793
njnc206a812009-08-07 07:56:20 +0000794<!-- start of xi:include in the manpage -->
795<variablelist id="cg_annotate.opts.list">
njn3da81962009-08-07 00:18:25 +0000796
797 <varlistentry>
798 <term>
799 <option><![CDATA[-h --help ]]></option>
800 </term>
801 <listitem>
802 <para>Show the help message.</para>
803 </listitem>
804 </varlistentry>
805
806 <varlistentry>
807 <term>
808 <option><![CDATA[--version ]]></option>
809 </term>
810 <listitem>
811 <para>Show the version number.</para>
812 </listitem>
813 </varlistentry>
814
815 <varlistentry>
816 <term>
817 <option><![CDATA[--show=A,B,C [default: all, using order in
818 cachegrind.out.<pid>] ]]></option>
819 </term>
820 <listitem>
821 <para>Specifies which events to show (and the column
822 order). Default is to use all present in the
823 <filename>cachegrind.out.&lt;pid&gt;</filename> file (and
824 use the order in the file). Useful if you want to concentrate on, for
825 example, I cache misses (<option>--show=I1mr,I2mr</option>), or data
826 read misses (<option>--show=D1mr,D2mr</option>), or L2 data misses
827 (<option>--show=D2mr,D2mw</option>). Best used in conjunction with
828 <option>--sort</option>.</para>
829 </listitem>
830 </varlistentry>
831
832 <varlistentry>
833 <term>
834 <option><![CDATA[--sort=A,B,C [default: order in
835 cachegrind.out.<pid>] ]]></option>
836 </term>
837 <listitem>
838 <para>Specifies the events upon which the sorting of the
839 function-by-function entries will be based.</para>
840 </listitem>
841 </varlistentry>
842
843 <varlistentry>
844 <term>
845 <option><![CDATA[--threshold=X [default: 99%] ]]></option>
846 </term>
847 <listitem>
848 <para>Sets the threshold for the function-by-function
849 summary. Functions are shown that account for more than X%
850 of the primary sort event. If auto-annotating, also affects
851 which files are annotated.</para>
852
853 <para>Note: thresholds can be set for more than one of the
854 events by appending any events for the
855 <option>--sort</option> option with a colon
856 and a number (no spaces, though). E.g. if you want to see
857 the functions that cover 99% of L2 read misses and 99% of L2
858 write misses, use this option:</para>
859 <para><option>--sort=D2mr:99,D2mw:99</option></para>
860 </listitem>
861 </varlistentry>
862
863 <varlistentry>
864 <term>
865 <option><![CDATA[--auto=<no|yes> [default: no] ]]></option>
866 </term>
867 <listitem>
868 <para>When enabled, automatically annotates every file that
869 is mentioned in the function-by-function summary that can be
870 found. Also gives a list of those that couldn't be found.</para>
871 </listitem>
872 </varlistentry>
873
874 <varlistentry>
875 <term>
876 <option><![CDATA[--context=N [default: 8] ]]></option>
877 </term>
878 <listitem>
879 <para>Print N lines of context before and after each
880 annotated line. Avoids printing large sections of source
881 files that were not executed. Use a large number
882 (e.g. 100000) to show all source lines.</para>
883 </listitem>
884 </varlistentry>
885
886 <varlistentry>
887 <term>
888 <option><![CDATA[-I<dir> --include=<dir> [default: none] ]]></option>
889 </term>
890 <listitem>
891 <para>Adds a directory to the list in which to search for
892 files. Multiple <option>-I</option>/<option>--include</option>
893 options can be given to add multiple directories.</para>
894 </listitem>
895 </varlistentry>
896
897</variablelist>
njnc206a812009-08-07 07:56:20 +0000898<!-- end of xi:include in the manpage -->
njn3da81962009-08-07 00:18:25 +0000899
900</sect1>
901
902
903
sewardj778d7832007-11-22 01:21:56 +0000904<sect1 id="cg-manual.acting-on"
njn3da81962009-08-07 00:18:25 +0000905 xreflabel="Acting on Cachegrind's Information">
906<title>Acting on Cachegrind's Information</title>
njn3a9d5dc2007-09-17 22:19:01 +0000907<para>
njna31dac22009-07-30 03:21:42 +0000908Cachegrind gives you lots of information, but acting on that information
909isn't always easy. Here are some rules of thumb that we have found to be
njn07f96562007-09-17 22:28:21 +0000910useful.</para>
njn3a9d5dc2007-09-17 22:19:01 +0000911
912<para>
njn3da81962009-08-07 00:18:25 +0000913First of all, the global hit/miss counts and miss rates are not that useful.
914If you have multiple programs or multiple runs of a program, comparing the
915numbers might identify if any are outliers and worthy of closer
916investigation. Otherwise, they're not enough to act on.</para>
njn3a9d5dc2007-09-17 22:19:01 +0000917
918<para>
njna31dac22009-07-30 03:21:42 +0000919The function-by-function counts are more useful to look at, as they pinpoint
920which functions are causing large numbers of counts. However, beware that
921inlining can make these counts misleading. If a function
922<function>f</function> is always inlined, counts will be attributed to the
923functions it is inlined into, rather than itself. However, if you look at
924the line-by-line annotations for <function>f</function> you'll see the
925counts that belong to <function>f</function>. (This is hard to avoid, it's
926how the debug info is structured.) So it's worth looking for large numbers
927in the line-by-line annotations.</para>
928
929<para>
njn07f96562007-09-17 22:28:21 +0000930The line-by-line source code annotations are much more useful. In our
931experience, the best place to start is by looking at the
932<computeroutput>Ir</computeroutput> numbers. They simply measure how many
933instructions were executed for each line, and don't include any cache
934information, but they can still be very useful for identifying
935bottlenecks.</para>
njn3a9d5dc2007-09-17 22:19:01 +0000936
937<para>
938After that, we have found that L2 misses are typically a much bigger source
939of slow-downs than L1 misses. So it's worth looking for any snippets of
njna31dac22009-07-30 03:21:42 +0000940code with high <computeroutput>D2mr</computeroutput> or
941<computeroutput>D2mw</computeroutput> counts. (You can use
942<option>--show=D2mr
943--sort=D2mr</option> with cg_annotate to focus just on
944<literal>D2mr</literal> counts, for example.) If you find any, it's still
945not always easy to work out how to improve things. You need to have a
njn07f96562007-09-17 22:28:21 +0000946reasonable understanding of how caches work, the principles of locality, and
947your program's data access patterns. Improving things may require
948redesigning a data structure, for example.</para>
njn3a9d5dc2007-09-17 22:19:01 +0000949
950<para>
njna31dac22009-07-30 03:21:42 +0000951Looking at the <computeroutput>Bcm</computeroutput> and
952<computeroutput>Bim</computeroutput> misses can also be helpful.
953In particular, <computeroutput>Bim</computeroutput> misses are often caused
954by <literal>switch</literal> statements, and in some cases these
955<literal>switch</literal> statements can be replaced with table-driven code.
956For example, you might replace code like this:</para>
957
958<programlisting><![CDATA[
959enum E { A, B, C };
960enum E e;
961int i;
962...
963switch (e)
964{
965 case A: i += 1;
966 case B: i += 2;
967 case C: i += 3;
968}
969]]></programlisting>
970
971<para>with code like this:</para>
972
973<programlisting><![CDATA[
974enum E { A, B, C };
975enum E e;
976enum E table[] = { 1, 2, 3 };
977int i;
978...
979i += table[e];
980]]></programlisting>
981
982<para>
983This is obviously a contrived example, but the basic principle applies in a
984wide variety of situations.</para>
985
986<para>
njn3a9d5dc2007-09-17 22:19:01 +0000987In short, Cachegrind can tell you where some of the bottlenecks in your code
988are, but it can't tell you how to fix them. You have to work that out for
989yourself. But at least you have the information!
990</para>
991
992</sect1>
sewardj94dc5082007-02-08 11:31:03 +0000993
njn3da81962009-08-07 00:18:25 +0000994
995<sect1 id="cg-manual.sim-details"
996 xreflabel="Simulation Details">
997<title>Simulation Details</title>
998<para>
999This section talks about details you don't need to know about in order to
1000use Cachegrind, but may be of interest to some people.
1001</para>
1002
1003<sect2 id="cache-sim" xreflabel="Cache Simulation Specifics">
1004<title>Cache Simulation Specifics</title>
1005
1006<para>Specific characteristics of the cache simulation are as
1007follows:</para>
1008
1009<itemizedlist>
1010
1011 <listitem>
1012 <para>Write-allocate: when a write miss occurs, the block
1013 written to is brought into the D1 cache. Most modern caches
1014 have this property.</para>
1015 </listitem>
1016
1017 <listitem>
1018 <para>Bit-selection hash function: the set of line(s) in the cache
1019 to which a memory block maps is chosen by the middle bits
1020 M--(M+N-1) of the byte address, where:</para>
1021 <itemizedlist>
1022 <listitem>
1023 <para>line size = 2^M bytes</para>
1024 </listitem>
1025 <listitem>
1026 <para>(cache size / line size / associativity) = 2^N bytes</para>
1027 </listitem>
1028 </itemizedlist>
1029 </listitem>
1030
1031 <listitem>
1032 <para>Inclusive L2 cache: the L2 cache typically replicates all
1033 the entries of the L1 caches, because fetching into L1 involves
1034 fetching into L2 first (this does not guarantee strict inclusiveness,
1035 as lines evicted from L2 still could reside in L1). This is
1036 standard on Pentium chips, but AMD Opterons, Athlons and Durons
1037 use an exclusive L2 cache that only holds
1038 blocks evicted from L1. Ditto most modern VIA CPUs.</para>
1039 </listitem>
1040
1041</itemizedlist>
1042
1043<para>The cache configuration simulated (cache size,
1044associativity and line size) is determined automatically using
1045the x86 CPUID instruction. If you have a machine that (a)
1046doesn't support the CPUID instruction, or (b) supports it in an
1047early incarnation that doesn't give any cache information, then
1048Cachegrind will fall back to using a default configuration (that
1049of a model 3/4 Athlon). Cachegrind will tell you if this
1050happens. You can manually specify one, two or all three levels
1051(I1/D1/L2) of the cache from the command line using the
1052<option>--I1</option>,
1053<option>--D1</option> and
1054<option>--L2</option> options.
1055For cache parameters to be valid for simulation, the number
1056of sets (with associativity being the number of cache lines in
1057each set) has to be a power of two.</para>
1058
1059<para>On PowerPC platforms
1060Cachegrind cannot automatically
1061determine the cache configuration, so you will
1062need to specify it with the
1063<option>--I1</option>,
1064<option>--D1</option> and
1065<option>--L2</option> options.</para>
1066
1067
1068<para>Other noteworthy behaviour:</para>
1069
1070<itemizedlist>
1071 <listitem>
1072 <para>References that straddle two cache lines are treated as
1073 follows:</para>
1074 <itemizedlist>
1075 <listitem>
1076 <para>If both blocks hit --&gt; counted as one hit</para>
1077 </listitem>
1078 <listitem>
1079 <para>If one block hits, the other misses --&gt; counted
1080 as one miss.</para>
1081 </listitem>
1082 <listitem>
1083 <para>If both blocks miss --&gt; counted as one miss (not
1084 two)</para>
1085 </listitem>
1086 </itemizedlist>
1087 </listitem>
1088
1089 <listitem>
1090 <para>Instructions that modify a memory location
1091 (e.g. <computeroutput>inc</computeroutput> and
1092 <computeroutput>dec</computeroutput>) are counted as doing
1093 just a read, i.e. a single data reference. This may seem
1094 strange, but since the write can never cause a miss (the read
1095 guarantees the block is in the cache) it's not very
1096 interesting.</para>
1097
1098 <para>Thus it measures not the number of times the data cache
1099 is accessed, but the number of times a data cache miss could
1100 occur.</para>
1101 </listitem>
1102
1103</itemizedlist>
1104
1105<para>If you are interested in simulating a cache with different
1106properties, it is not particularly hard to write your own cache
1107simulator, or to modify the existing ones in
1108<computeroutput>cg_sim.c</computeroutput>. We'd be
1109interested to hear from anyone who does.</para>
1110
1111</sect2>
1112
1113
1114<sect2 id="branch-sim" xreflabel="Branch Simulation Specifics">
1115<title>Branch Simulation Specifics</title>
1116
1117<para>Cachegrind simulates branch predictors intended to be
1118typical of mainstream desktop/server processors of around 2004.</para>
1119
1120<para>Conditional branches are predicted using an array of 16384 2-bit
1121saturating counters. The array index used for a branch instruction is
1122computed partly from the low-order bits of the branch instruction's
1123address and partly using the taken/not-taken behaviour of the last few
1124conditional branches. As a result the predictions for any specific
1125branch depend both on its own history and the behaviour of previous
1126branches. This is a standard technique for improving prediction
1127accuracy.</para>
1128
1129<para>For indirect branches (that is, jumps to unknown destinations)
1130Cachegrind uses a simple branch target address predictor. Targets are
1131predicted using an array of 512 entries indexed by the low order 9
1132bits of the branch instruction's address. Each branch is predicted to
1133jump to the same address it did last time. Any other behaviour causes
1134a mispredict.</para>
1135
1136<para>More recent processors have better branch predictors, in
1137particular better indirect branch predictors. Cachegrind's predictor
1138design is deliberately conservative so as to be representative of the
1139large installed base of processors which pre-date widespread
1140deployment of more sophisticated indirect branch predictors. In
1141particular, late model Pentium 4s (Prescott), Pentium M, Core and Core
11422 have more sophisticated indirect branch predictors than modelled by
1143Cachegrind. </para>
1144
1145<para>Cachegrind does not simulate a return stack predictor. It
1146assumes that processors perfectly predict function return addresses,
1147an assumption which is probably close to being true.</para>
1148
1149<para>See Hennessy and Patterson's classic text "Computer
1150Architecture: A Quantitative Approach", 4th edition (2007), Section
11512.3 (pages 80-89) for background on modern branch predictors.</para>
1152
1153</sect2>
1154
1155<sect2 id="cg-manual.annopts.accuracy" xreflabel="Accuracy">
1156<title>Accuracy</title>
1157
1158<para>Valgrind's cache profiling has a number of
1159shortcomings:</para>
1160
1161<itemizedlist>
1162 <listitem>
1163 <para>It doesn't account for kernel activity -- the effect of system
1164 calls on the cache and branch predictor contents is ignored.</para>
1165 </listitem>
1166
1167 <listitem>
1168 <para>It doesn't account for other process activity.
1169 This is probably desirable when considering a single
1170 program.</para>
1171 </listitem>
1172
1173 <listitem>
1174 <para>It doesn't account for virtual-to-physical address
1175 mappings. Hence the simulation is not a true
1176 representation of what's happening in the
1177 cache. Most caches and branch predictors are physically indexed, but
1178 Cachegrind simulates caches using virtual addresses.</para>
1179 </listitem>
1180
1181 <listitem>
1182 <para>It doesn't account for cache misses not visible at the
1183 instruction level, e.g. those arising from TLB misses, or
1184 speculative execution.</para>
1185 </listitem>
1186
1187 <listitem>
1188 <para>Valgrind will schedule
1189 threads differently from how they would be when running natively.
1190 This could warp the results for threaded programs.</para>
1191 </listitem>
1192
1193 <listitem>
1194 <para>The x86/amd64 instructions <computeroutput>bts</computeroutput>,
1195 <computeroutput>btr</computeroutput> and
1196 <computeroutput>btc</computeroutput> will incorrectly be
1197 counted as doing a data read if both the arguments are
1198 registers, eg:</para>
1199<programlisting><![CDATA[
1200 btsl %eax, %edx]]></programlisting>
1201
1202 <para>This should only happen rarely.</para>
1203 </listitem>
1204
1205 <listitem>
1206 <para>x86/amd64 FPU instructions with data sizes of 28 and 108 bytes
1207 (e.g. <computeroutput>fsave</computeroutput>) are treated as
1208 though they only access 16 bytes. These instructions seem to
1209 be rare so hopefully this won't affect accuracy much.</para>
1210 </listitem>
1211
1212</itemizedlist>
1213
1214<para>Another thing worth noting is that results are very sensitive.
1215Changing the size of the the executable being profiled, or the sizes
1216of any of the shared libraries it uses, or even the length of their
1217file names, can perturb the results. Variations will be small, but
1218don't expect perfectly repeatable results if your program changes at
1219all.</para>
1220
1221<para>More recent GNU/Linux distributions do address space
1222randomisation, in which identical runs of the same program have their
1223shared libraries loaded at different locations, as a security measure.
1224This also perturbs the results.</para>
1225
1226<para>While these factors mean you shouldn't trust the results to
1227be super-accurate, they should be close enough to be useful.</para>
1228
1229</sect2>
1230
1231</sect1>
1232
1233
1234
sewardj778d7832007-11-22 01:21:56 +00001235<sect1 id="cg-manual.impl-details"
njn3da81962009-08-07 00:18:25 +00001236 xreflabel="Implementation Details">
1237<title>Implementation Details</title>
njn3a9d5dc2007-09-17 22:19:01 +00001238<para>
njn534f7812006-10-21 22:22:59 +00001239This section talks about details you don't need to know about in order to
1240use Cachegrind, but may be of interest to some people.
njn3a9d5dc2007-09-17 22:19:01 +00001241</para>
njn3e986b22004-11-30 10:43:45 +00001242
sewardj778d7832007-11-22 01:21:56 +00001243<sect2 id="cg-manual.impl-details.how-cg-works"
njn3da81962009-08-07 00:18:25 +00001244 xreflabel="How Cachegrind Works">
1245<title>How Cachegrind Works</title>
njn534f7812006-10-21 22:22:59 +00001246<para>The best reference for understanding how Cachegrind works is chapter 3 of
1247"Dynamic Binary Analysis and Instrumentation", by Nicholas Nethercote. It
njn25ac3842009-08-07 02:58:11 +00001248is available on the <ulink url="&vg-pubs-url;">Valgrind publications
njn011215f2006-10-21 23:00:59 +00001249page</ulink>.</para>
njn534f7812006-10-21 22:22:59 +00001250</sect2>
njn3e986b22004-11-30 10:43:45 +00001251
sewardj778d7832007-11-22 01:21:56 +00001252<sect2 id="cg-manual.impl-details.file-format"
njn3da81962009-08-07 00:18:25 +00001253 xreflabel="Cachegrind Output File Format">
1254<title>Cachegrind Output File Format</title>
njn534f7812006-10-21 22:22:59 +00001255<para>The file format is fairly straightforward, basically giving the
1256cost centre for every line, grouped by files and
njn3da81962009-08-07 00:18:25 +00001257functions. It's also totally generic and self-describing, in the sense that
1258it can be used for any events that can be counted on a line-by-line basis,
1259not just cache and branch predictor events. For example, earlier versions
1260of Cachegrind didn't have a branch predictor simulation. When this was
1261added, the file format didn't need to change at all. So the format (and
1262consequently, cg_annotate) could be used by other tools.</para>
njn534f7812006-10-21 22:22:59 +00001263
1264<para>The file format:</para>
1265<programlisting><![CDATA[
1266file ::= desc_line* cmd_line events_line data_line+ summary_line
1267desc_line ::= "desc:" ws? non_nl_string
1268cmd_line ::= "cmd:" ws? cmd
1269events_line ::= "events:" ws? (event ws)+
1270data_line ::= file_line | fn_line | count_line
1271file_line ::= "fl=" filename
1272fn_line ::= "fn=" fn_name
1273count_line ::= line_num ws? (count ws)+
1274summary_line ::= "summary:" ws? (count ws)+
1275count ::= num | "."]]></programlisting>
1276
1277<para>Where:</para>
njn3e986b22004-11-30 10:43:45 +00001278<itemizedlist>
1279 <listitem>
njn534f7812006-10-21 22:22:59 +00001280 <para><computeroutput>non_nl_string</computeroutput> is any
1281 string not containing a newline.</para>
njn3e986b22004-11-30 10:43:45 +00001282 </listitem>
njn534f7812006-10-21 22:22:59 +00001283 <listitem>
1284 <para><computeroutput>cmd</computeroutput> is a string holding the
1285 command line of the profiled program.</para>
1286 </listitem>
1287 <listitem>
njn26242122007-01-22 03:21:27 +00001288 <para><computeroutput>event</computeroutput> is a string containing
1289 no whitespace.</para>
1290 </listitem>
1291 <listitem>
njn534f7812006-10-21 22:22:59 +00001292 <para><computeroutput>filename</computeroutput> and
1293 <computeroutput>fn_name</computeroutput> are strings.</para>
1294 </listitem>
1295 <listitem>
1296 <para><computeroutput>num</computeroutput> and
1297 <computeroutput>line_num</computeroutput> are decimal
1298 numbers.</para>
1299 </listitem>
1300 <listitem>
1301 <para><computeroutput>ws</computeroutput> is whitespace.</para>
1302 </listitem>
1303</itemizedlist>
1304
1305<para>The contents of the "desc:" lines are printed out at the top
1306of the summary. This is a generic way of providing simulation
njn3da81962009-08-07 00:18:25 +00001307specific information, e.g. for giving the cache configuration for
njn534f7812006-10-21 22:22:59 +00001308cache simulation.</para>
1309
1310<para>More than one line of info can be presented for each file/fn/line number.
1311In such cases, the counts for the named events will be accumulated.</para>
1312
njn3a9d5dc2007-09-17 22:19:01 +00001313<para>Counts can be "." to represent zero. This makes the files easier for
1314humans to read.</para>
njn534f7812006-10-21 22:22:59 +00001315
1316<para>The number of counts in each
1317<computeroutput>line</computeroutput> and the
1318<computeroutput>summary_line</computeroutput> should not exceed
1319the number of events in the
1320<computeroutput>event_line</computeroutput>. If the number in
1321each <computeroutput>line</computeroutput> is less, cg_annotate
njn3a9d5dc2007-09-17 22:19:01 +00001322treats those missing as though they were a "." entry. This saves space.
1323</para>
njn534f7812006-10-21 22:22:59 +00001324
1325<para>A <computeroutput>file_line</computeroutput> changes the
1326current file name. A <computeroutput>fn_line</computeroutput>
1327changes the current function name. A
1328<computeroutput>count_line</computeroutput> contains counts that
1329pertain to the current filename/fn_name. A "fn="
1330<computeroutput>file_line</computeroutput> and a
1331<computeroutput>fn_line</computeroutput> must appear before any
1332<computeroutput>count_line</computeroutput>s to give the context
1333of the first <computeroutput>count_line</computeroutput>s.</para>
1334
1335<para>Each <computeroutput>file_line</computeroutput> will normally be
1336immediately followed by a <computeroutput>fn_line</computeroutput>. But it
1337doesn't have to be.</para>
1338
njn3da81962009-08-07 00:18:25 +00001339<para>The summary line is redundant, because it just holds the total counts
1340for each event. But this serves as a useful sanity check of the data; if
1341the totals for each event don't match the summary line, something has gone
1342wrong.</para>
njn3e986b22004-11-30 10:43:45 +00001343
1344</sect2>
1345
1346</sect1>
1347</chapter>