blob: c1377b63a864d5c682dd40531fad1c53f58ab19b [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
18hierarchy and (optionally) branch predictor. It gathers the following
19statistics:</para>
njn3e986b22004-11-30 10:43:45 +000020<itemizedlist>
21 <listitem>
njn05a89172009-07-29 02:36:21 +000022 <para>L1 instruction cache reads and read misses;</para>
njn3e986b22004-11-30 10:43:45 +000023 </listitem>
24 <listitem>
25 <para>L1 data cache reads and read misses, writes and write
26 misses;</para>
27 </listitem>
28 <listitem>
29 <para>L2 unified cache reads and read misses, writes and
30 writes misses.</para>
31 </listitem>
sewardj8badbaa2007-05-08 09:20:25 +000032 <listitem>
33 <para>Conditional branches and mispredicted conditional branches.</para>
34 </listitem>
35 <listitem>
36 <para>Indirect branches and mispredicted indirect branches. An
37 indirect branch is a jump or call to a destination only known at
38 run time.</para>
39 </listitem>
njn3e986b22004-11-30 10:43:45 +000040</itemizedlist>
41
njn05a89172009-07-29 02:36:21 +000042<para>These statistics are presented for the entire program and for each
43function in the program. You can also annotate each line of source code in
44the program with the counts that were caused directly by it.</para>
45
njnc8cccb12005-07-25 23:30:24 +000046<para>On a modern machine, an L1 miss will typically cost
sewardj8badbaa2007-05-08 09:20:25 +000047around 10 cycles, an L2 miss can cost as much as 200
48cycles, and a mispredicted branch costs in the region of 10
49to 30 cycles. Detailed cache and branch profiling can be very useful
njn05a89172009-07-29 02:36:21 +000050for understanding how your program interacts with the machine and thus how
51to make it faster.</para>
njn3e986b22004-11-30 10:43:45 +000052
53<para>Also, since one instruction cache read is performed per
54instruction executed, you can find out how many instructions are
njn05a89172009-07-29 02:36:21 +000055executed per line, which can be useful for traditional profiling.</para>
njn3e986b22004-11-30 10:43:45 +000056
sewardj8badbaa2007-05-08 09:20:25 +000057<para>Branch profiling is not enabled by default. To use it, you must
njn7e5d4ed2009-07-30 02:57:52 +000058additionally specify <option>--branch-sim=yes</option>
sewardj8badbaa2007-05-08 09:20:25 +000059on the command line.</para>
60
njn3e986b22004-11-30 10:43:45 +000061
njn05a89172009-07-29 02:36:21 +000062<sect2 id="cg-manual.basics" xreflabel="Basics">
63<title>Basics</title>
njn3e986b22004-11-30 10:43:45 +000064
65<para>First off, as for normal Valgrind use, you probably want to
66compile with debugging info (the
njn7e5d4ed2009-07-30 02:57:52 +000067<option>-g</option> flag). But by contrast with
njn3e986b22004-11-30 10:43:45 +000068normal Valgrind use, you probably <command>do</command> want to turn
69optimisation on, since you should profile your program as it will
70be normally run.</para>
71
72<para>The two steps are:</para>
73<orderedlist>
74 <listitem>
75 <para>Run your program with <computeroutput>valgrind
76 --tool=cachegrind</computeroutput> in front of the normal
77 command line invocation. When the program finishes,
78 Cachegrind will print summary cache statistics. It also
79 collects line-by-line information in a file
njn374a36d2007-11-23 01:41:32 +000080 <computeroutput>cachegrind.out.&lt;pid&gt;</computeroutput>, where
81 <computeroutput>&lt;pid&gt;</computeroutput> is the program's process
82 ID.</para>
njn3e986b22004-11-30 10:43:45 +000083
sewardj8badbaa2007-05-08 09:20:25 +000084 <para>Branch prediction statistics are not collected by default.
85 To do so, add the flag
njn7e5d4ed2009-07-30 02:57:52 +000086 <option>--branch-sim=yes</option>.
sewardj8badbaa2007-05-08 09:20:25 +000087 </para>
88
njn3e986b22004-11-30 10:43:45 +000089 <para>This step should be done every time you want to collect
90 information about a new program, a changed program, or about
91 the same program with different input.</para>
92 </listitem>
93
94 <listitem>
95 <para>Generate a function-by-function summary, and possibly
96 annotate source files, using the supplied
njn374a36d2007-11-23 01:41:32 +000097 cg_annotate program. Source
njn3e986b22004-11-30 10:43:45 +000098 files to annotate can be specified manually, or manually on
99 the command line, or "interesting" source files can be
100 annotated automatically with the
njn7e5d4ed2009-07-30 02:57:52 +0000101 <option>--auto=yes</option> option. You can
njn3e986b22004-11-30 10:43:45 +0000102 annotate C/C++ files or assembly language files equally
103 easily.</para>
104
105 <para>This step can be performed as many times as you like
106 for each Step 2. You may want to do multiple annotations
107 showing different information each time.</para>
108 </listitem>
109
110</orderedlist>
111
sewardj94dc5082007-02-08 11:31:03 +0000112<para>As an optional intermediate step, you can use the supplied
njn374a36d2007-11-23 01:41:32 +0000113cg_merge program to sum together the
sewardj94dc5082007-02-08 11:31:03 +0000114outputs of multiple Cachegrind runs, into a single file which you then
njn374a36d2007-11-23 01:41:32 +0000115use as the input for cg_annotate.</para>
sewardj94dc5082007-02-08 11:31:03 +0000116
sewardj08e31e22007-05-23 21:58:33 +0000117<para>These steps are described in detail in the following
njn3e986b22004-11-30 10:43:45 +0000118sections.</para>
119
120</sect2>
121
122
debc32e822005-06-25 14:43:05 +0000123<sect2 id="cache-sim" xreflabel="Cache simulation specifics">
njn3e986b22004-11-30 10:43:45 +0000124<title>Cache simulation specifics</title>
125
sewardj08e31e22007-05-23 21:58:33 +0000126<para>Cachegrind simulates a machine with independent
127first level instruction and data caches (I1 and D1), backed by a
128unified second level cache (L2). This configuration is used by almost
129all modern machines. Some old Cyrix CPUs had a unified I and D L1
130cache, but they are ancient history now.</para>
njn3e986b22004-11-30 10:43:45 +0000131
sewardj08e31e22007-05-23 21:58:33 +0000132<para>Specific characteristics of the simulation are as
133follows:</para>
njn3e986b22004-11-30 10:43:45 +0000134
135<itemizedlist>
136
137 <listitem>
138 <para>Write-allocate: when a write miss occurs, the block
139 written to is brought into the D1 cache. Most modern caches
140 have this property.</para>
141 </listitem>
142
143 <listitem>
weidendo144b76c2009-01-26 22:56:14 +0000144 <para>Bit-selection hash function: the set of line(s) in the cache
njn3e986b22004-11-30 10:43:45 +0000145 to which a memory block maps is chosen by the middle bits
146 M--(M+N-1) of the byte address, where:</para>
147 <itemizedlist>
148 <listitem>
149 <para>line size = 2^M bytes</para>
150 </listitem>
151 <listitem>
weidendo144b76c2009-01-26 22:56:14 +0000152 <para>(cache size / line size / associativity) = 2^N bytes</para>
njn3e986b22004-11-30 10:43:45 +0000153 </listitem>
154 </itemizedlist>
155 </listitem>
156
157 <listitem>
weidendo144b76c2009-01-26 22:56:14 +0000158 <para>Inclusive L2 cache: the L2 cache typically replicates all
159 the entries of the L1 caches, because fetching into L1 involves
160 fetching into L2 first (this does not guarantee strict inclusiveness,
161 as lines evicted from L2 still could reside in L1). This is
162 standard on Pentium chips, but AMD Opterons, Athlons and Durons
sewardj08e31e22007-05-23 21:58:33 +0000163 use an exclusive L2 cache that only holds
164 blocks evicted from L1. Ditto most modern VIA CPUs.</para>
njn3e986b22004-11-30 10:43:45 +0000165 </listitem>
166
167</itemizedlist>
168
169<para>The cache configuration simulated (cache size,
170associativity and line size) is determined automagically using
njn05a89172009-07-29 02:36:21 +0000171the x86 CPUID instruction. If you have an machine that (a)
njn3e986b22004-11-30 10:43:45 +0000172doesn't support the CPUID instruction, or (b) supports it in an
173early incarnation that doesn't give any cache information, then
174Cachegrind will fall back to using a default configuration (that
175of a model 3/4 Athlon). Cachegrind will tell you if this
176happens. You can manually specify one, two or all three levels
177(I1/D1/L2) of the cache from the command line using the
njn7e5d4ed2009-07-30 02:57:52 +0000178<option>--I1</option>,
179<option>--D1</option> and
180<option>--L2</option> options.
weidendo144b76c2009-01-26 22:56:14 +0000181For cache parameters to be valid for simulation, the number
182of sets (with associativity being the number of cache lines in
183each set) has to be a power of two.</para>
njn3e986b22004-11-30 10:43:45 +0000184
sewardj08e31e22007-05-23 21:58:33 +0000185<para>On PowerPC platforms
186Cachegrind cannot automatically
187determine the cache configuration, so you will
188need to specify it with the
njn7e5d4ed2009-07-30 02:57:52 +0000189<option>--I1</option>,
190<option>--D1</option> and
191<option>--L2</option> options.</para>
sewardj08e31e22007-05-23 21:58:33 +0000192
njn3e986b22004-11-30 10:43:45 +0000193
194<para>Other noteworthy behaviour:</para>
195
196<itemizedlist>
197 <listitem>
198 <para>References that straddle two cache lines are treated as
199 follows:</para>
200 <itemizedlist>
201 <listitem>
202 <para>If both blocks hit --&gt; counted as one hit</para>
203 </listitem>
204 <listitem>
205 <para>If one block hits, the other misses --&gt; counted
206 as one miss.</para>
207 </listitem>
208 <listitem>
209 <para>If both blocks miss --&gt; counted as one miss (not
210 two)</para>
211 </listitem>
212 </itemizedlist>
213 </listitem>
214
215 <listitem>
216 <para>Instructions that modify a memory location
217 (eg. <computeroutput>inc</computeroutput> and
218 <computeroutput>dec</computeroutput>) are counted as doing
219 just a read, ie. a single data reference. This may seem
220 strange, but since the write can never cause a miss (the read
221 guarantees the block is in the cache) it's not very
222 interesting.</para>
223
224 <para>Thus it measures not the number of times the data cache
225 is accessed, but the number of times a data cache miss could
226 occur.</para>
227 </listitem>
228
229</itemizedlist>
230
231<para>If you are interested in simulating a cache with different
232properties, it is not particularly hard to write your own cache
233simulator, or to modify the existing ones in
weidendo144b76c2009-01-26 22:56:14 +0000234<computeroutput>cg_sim.c</computeroutput>. We'd be
njn3e986b22004-11-30 10:43:45 +0000235interested to hear from anyone who does.</para>
236
237</sect2>
238
sewardj8badbaa2007-05-08 09:20:25 +0000239
240<sect2 id="branch-sim" xreflabel="Branch simulation specifics">
241<title>Branch simulation specifics</title>
242
243<para>Cachegrind simulates branch predictors intended to be
244typical of mainstream desktop/server processors of around 2004.</para>
245
246<para>Conditional branches are predicted using an array of 16384 2-bit
247saturating counters. The array index used for a branch instruction is
248computed partly from the low-order bits of the branch instruction's
249address and partly using the taken/not-taken behaviour of the last few
250conditional branches. As a result the predictions for any specific
251branch depend both on its own history and the behaviour of previous
252branches. This is a standard technique for improving prediction
253accuracy.</para>
254
255<para>For indirect branches (that is, jumps to unknown destinations)
256Cachegrind uses a simple branch target address predictor. Targets are
257predicted using an array of 512 entries indexed by the low order 9
258bits of the branch instruction's address. Each branch is predicted to
259jump to the same address it did last time. Any other behaviour causes
260a mispredict.</para>
261
262<para>More recent processors have better branch predictors, in
263particular better indirect branch predictors. Cachegrind's predictor
264design is deliberately conservative so as to be representative of the
265large installed base of processors which pre-date widespread
266deployment of more sophisticated indirect branch predictors. In
267particular, late model Pentium 4s (Prescott), Pentium M, Core and Core
2682 have more sophisticated indirect branch predictors than modelled by
269Cachegrind. </para>
270
271<para>Cachegrind does not simulate a return stack predictor. It
272assumes that processors perfectly predict function return addresses,
273an assumption which is probably close to being true.</para>
274
275<para>See Hennessy and Patterson's classic text "Computer
276Architecture: A Quantitative Approach", 4th edition (2007), Section
2772.3 (pages 80-89) for background on modern branch predictors.</para>
278
279</sect2>
280
281
njn3e986b22004-11-30 10:43:45 +0000282</sect1>
283
284
285
286<sect1 id="cg-manual.profile" xreflabel="Profiling programs">
287<title>Profiling programs</title>
288
289<para>To gather cache profiling information about the program
290<computeroutput>ls -l</computeroutput>, invoke Cachegrind like
291this:</para>
292
293<programlisting><![CDATA[
294valgrind --tool=cachegrind ls -l]]></programlisting>
295
296<para>The program will execute (slowly). Upon completion,
297summary statistics that look like this will be printed:</para>
298
299<programlisting><![CDATA[
300==31751== I refs: 27,742,716
301==31751== I1 misses: 276
302==31751== L2 misses: 275
303==31751== I1 miss rate: 0.0%
304==31751== L2i miss rate: 0.0%
305==31751==
306==31751== D refs: 15,430,290 (10,955,517 rd + 4,474,773 wr)
307==31751== D1 misses: 41,185 ( 21,905 rd + 19,280 wr)
308==31751== L2 misses: 23,085 ( 3,987 rd + 19,098 wr)
309==31751== D1 miss rate: 0.2% ( 0.1% + 0.4%)
310==31751== L2d miss rate: 0.1% ( 0.0% + 0.4%)
311==31751==
312==31751== L2 misses: 23,360 ( 4,262 rd + 19,098 wr)
313==31751== L2 miss rate: 0.0% ( 0.0% + 0.4%)]]></programlisting>
314
315<para>Cache accesses for instruction fetches are summarised
316first, giving the number of fetches made (this is the number of
317instructions executed, which can be useful to know in its own
318right), the number of I1 misses, and the number of L2 instruction
319(<computeroutput>L2i</computeroutput>) misses.</para>
320
321<para>Cache accesses for data follow. The information is similar
322to that of the instruction fetches, except that the values are
323also shown split between reads and writes (note each row's
324<computeroutput>rd</computeroutput> and
325<computeroutput>wr</computeroutput> values add up to the row's
326total).</para>
327
328<para>Combined instruction and data figures for the L2 cache
329follow that.</para>
330
331
332
333<sect2 id="cg-manual.outputfile" xreflabel="Output file">
334<title>Output file</title>
335
336<para>As well as printing summary information, Cachegrind also
sewardje1216cb2007-02-07 19:55:30 +0000337writes line-by-line cache profiling information to a user-specified
338file. By default this file is named
njn374a36d2007-11-23 01:41:32 +0000339<computeroutput>cachegrind.out.&lt;pid&gt;</computeroutput>. This file
sewardje1216cb2007-02-07 19:55:30 +0000340is human-readable, but is intended to be interpreted by the accompanying
njn374a36d2007-11-23 01:41:32 +0000341program cg_annotate, described in the next section.</para>
njn3e986b22004-11-30 10:43:45 +0000342
343<para>Things to note about the
njn374a36d2007-11-23 01:41:32 +0000344<computeroutput>cachegrind.out.&lt;pid&gt;</computeroutput>
njn3e986b22004-11-30 10:43:45 +0000345file:</para>
346
347<itemizedlist>
348 <listitem>
349 <para>It is written every time Cachegrind is run, and will
350 overwrite any existing
njn374a36d2007-11-23 01:41:32 +0000351 <computeroutput>cachegrind.out.&lt;pid&gt;</computeroutput>
njn3e986b22004-11-30 10:43:45 +0000352 in the current directory (but that won't happen very often
353 because it takes some time for process ids to be
354 recycled).</para>
njn374a36d2007-11-23 01:41:32 +0000355 </listitem>
356 <listitem>
357 <para>To use an output file name other than the default
sewardj8693e012007-02-08 06:47:19 +0000358 <computeroutput>cachegrind.out</computeroutput>,
njn7e5d4ed2009-07-30 02:57:52 +0000359 use the <option>--cachegrind-out-file</option>
sewardje1216cb2007-02-07 19:55:30 +0000360 switch.</para>
njn3e986b22004-11-30 10:43:45 +0000361 </listitem>
362 <listitem>
njn374a36d2007-11-23 01:41:32 +0000363 <para>It can be big: <computeroutput>ls -l</computeroutput>
njn3e986b22004-11-30 10:43:45 +0000364 generates a file of about 350KB. Browsing a few files and
365 web pages with a Konqueror built with full debugging
366 information generates a file of around 15 MB.</para>
367 </listitem>
368</itemizedlist>
369
njn374a36d2007-11-23 01:41:32 +0000370<para>The default <computeroutput>.&lt;pid&gt;</computeroutput> suffix
de7e109d12005-11-18 22:09:58 +0000371on the output file name serves two purposes. Firstly, it means you
372don't have to rename old log files that you don't want to overwrite.
373Secondly, and more importantly, it allows correct profiling with the
njn7e5d4ed2009-07-30 02:57:52 +0000374<option>--trace-children=yes</option> option of
njn3e986b22004-11-30 10:43:45 +0000375programs that spawn child processes.</para>
376
377</sect2>
378
379
380
381<sect2 id="cg-manual.cgopts" xreflabel="Cachegrind options">
382<title>Cachegrind options</title>
383
de03e0e7c2005-12-03 23:02:33 +0000384<!-- start of xi:include in the manpage -->
sewardj08e31e22007-05-23 21:58:33 +0000385<para id="cg.opts.para">Using command line options, you can
386manually specify the I1/D1/L2 cache
387configuration to simulate. For each cache, you can specify the
388size, associativity and line size. The size and line size
389are measured in bytes. The three items
de03e0e7c2005-12-03 23:02:33 +0000390must be comma-separated, but with no spaces, eg:
391<literallayout> valgrind --tool=cachegrind --I1=65535,2,64</literallayout>
392
393You can specify one, two or three of the I1/D1/L2 caches. Any level not
394manually specified will be simulated using the configuration found in
395the normal way (via the CPUID instruction for automagic cache
396configuration, or failing that, via defaults).</para>
397
njn3e986b22004-11-30 10:43:45 +0000398<para>Cache-simulation specific options are:</para>
399
de03e0e7c2005-12-03 23:02:33 +0000400<variablelist id="cg.opts.list">
njn3e986b22004-11-30 10:43:45 +0000401
de03e0e7c2005-12-03 23:02:33 +0000402 <varlistentry id="opt.I1" xreflabel="--I1">
403 <term>
404 <option><![CDATA[--I1=<size>,<associativity>,<line size> ]]></option>
405 </term>
406 <listitem>
407 <para>Specify the size, associativity and line size of the level 1
408 instruction cache. </para>
409 </listitem>
410 </varlistentry>
njn3e986b22004-11-30 10:43:45 +0000411
de03e0e7c2005-12-03 23:02:33 +0000412 <varlistentry id="opt.D1" xreflabel="--D1">
413 <term>
414 <option><![CDATA[--D1=<size>,<associativity>,<line size> ]]></option>
415 </term>
416 <listitem>
417 <para>Specify the size, associativity and line size of the level 1
418 data cache.</para>
419 </listitem>
420 </varlistentry>
njn3e986b22004-11-30 10:43:45 +0000421
de03e0e7c2005-12-03 23:02:33 +0000422 <varlistentry id="opt.L2" xreflabel="--L2">
423 <term>
424 <option><![CDATA[--L2=<size>,<associativity>,<line size> ]]></option>
425 </term>
426 <listitem>
427 <para>Specify the size, associativity and line size of the level 2
428 cache.</para>
429 </listitem>
430 </varlistentry>
njn3e986b22004-11-30 10:43:45 +0000431
sewardje1216cb2007-02-07 19:55:30 +0000432 <varlistentry id="opt.cachegrind-out-file" xreflabel="--cachegrind-out-file">
433 <term>
njn374a36d2007-11-23 01:41:32 +0000434 <option><![CDATA[--cachegrind-out-file=<file> ]]></option>
sewardje1216cb2007-02-07 19:55:30 +0000435 </term>
436 <listitem>
sewardj8693e012007-02-08 06:47:19 +0000437 <para>Write the profile data to
njn374a36d2007-11-23 01:41:32 +0000438 <computeroutput>file</computeroutput> rather than to the default
439 output file,
440 <computeroutput>cachegrind.out.&lt;pid&gt;</computeroutput>. The
441 <option>%p</option> and <option>%q</option> format specifiers
442 can be used to embed the process ID and/or the contents of an
443 environment variable in the name, as is the case for the core
444 option <option>--log-file</option>. See <link
445 linkend="manual-core.basicopts">here</link> for details.
sewardje1216cb2007-02-07 19:55:30 +0000446 </para>
447 </listitem>
448 </varlistentry>
449
sewardj8badbaa2007-05-08 09:20:25 +0000450 <varlistentry id="opt.cache-sim" xreflabel="--cache-sim">
451 <term>
452 <option><![CDATA[--cache-sim=no|yes [yes] ]]></option>
453 </term>
454 <listitem>
455 <para>Enables or disables collection of cache access and miss
456 counts.</para>
457 </listitem>
458 </varlistentry>
459
460 <varlistentry id="opt.branch-sim" xreflabel="--branch-sim">
461 <term>
462 <option><![CDATA[--branch-sim=no|yes [no] ]]></option>
463 </term>
464 <listitem>
465 <para>Enables or disables collection of branch instruction and
466 misprediction counts. By default this is disabled as it
467 slows Cachegrind down by approximately 25%. Note that you
njn7e5d4ed2009-07-30 02:57:52 +0000468 cannot specify <option>--cache-sim=no</option>
469 and <option>--branch-sim=no</option>
sewardj8badbaa2007-05-08 09:20:25 +0000470 together, as that would leave Cachegrind with no
471 information to collect.</para>
472 </listitem>
473 </varlistentry>
474
de03e0e7c2005-12-03 23:02:33 +0000475</variablelist>
476<!-- end of xi:include in the manpage -->
njn3e986b22004-11-30 10:43:45 +0000477
478</sect2>
479
480
481
482<sect2 id="cg-manual.annotate" xreflabel="Annotating C/C++ programs">
483<title>Annotating C/C++ programs</title>
484
njn374a36d2007-11-23 01:41:32 +0000485<para>Before using cg_annotate,
njn3e986b22004-11-30 10:43:45 +0000486it is worth widening your window to be at least 120-characters
487wide if possible, as the output lines can be quite long.</para>
488
njn374a36d2007-11-23 01:41:32 +0000489<para>To get a function-by-function summary, run <computeroutput>cg_annotate
490&lt;filename&gt;</computeroutput> on a Cachegrind output file.</para>
njn3e986b22004-11-30 10:43:45 +0000491
492<para>The output looks like this:</para>
493
494<programlisting><![CDATA[
495--------------------------------------------------------------------------------
496I1 cache: 65536 B, 64 B, 2-way associative
497D1 cache: 65536 B, 64 B, 2-way associative
498L2 cache: 262144 B, 64 B, 8-way associative
499Command: concord vg_to_ucode.c
500Events recorded: Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw
501Events shown: Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw
502Event sort order: Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw
503Threshold: 99%
504Chosen for annotation:
505Auto-annotation: on
506
507--------------------------------------------------------------------------------
508Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw
509--------------------------------------------------------------------------------
51027,742,716 276 275 10,955,517 21,905 3,987 4,474,773 19,280 19,098 PROGRAM TOTALS
511
512--------------------------------------------------------------------------------
513Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw file:function
514--------------------------------------------------------------------------------
5158,821,482 5 5 2,242,702 1,621 73 1,794,230 0 0 getc.c:_IO_getc
5165,222,023 4 4 2,276,334 16 12 875,959 1 1 concord.c:get_word
5172,649,248 2 2 1,344,810 7,326 1,385 . . . vg_main.c:strcmp
5182,521,927 2 2 591,215 0 0 179,398 0 0 concord.c:hash
5192,242,740 2 2 1,046,612 568 22 448,548 0 0 ctype.c:tolower
5201,496,937 4 4 630,874 9,000 1,400 279,388 0 0 concord.c:insert
521 897,991 51 51 897,831 95 30 62 1 1 ???:???
522 598,068 1 1 299,034 0 0 149,517 0 0 ../sysdeps/generic/lockfile.c:__flockfile
523 598,068 0 0 299,034 0 0 149,517 0 0 ../sysdeps/generic/lockfile.c:__funlockfile
524 598,024 4 4 213,580 35 16 149,506 0 0 vg_clientmalloc.c:malloc
525 446,587 1 1 215,973 2,167 430 129,948 14,057 13,957 concord.c:add_existing
526 341,760 2 2 128,160 0 0 128,160 0 0 vg_clientmalloc.c:vg_trap_here_WRAPPER
527 320,782 4 4 150,711 276 0 56,027 53 53 concord.c:init_hash_table
528 298,998 1 1 106,785 0 0 64,071 1 1 concord.c:create
529 149,518 0 0 149,516 0 0 1 0 0 ???:tolower@@GLIBC_2.0
530 149,518 0 0 149,516 0 0 1 0 0 ???:fgetc@@GLIBC_2.0
531 95,983 4 4 38,031 0 0 34,409 3,152 3,150 concord.c:new_word_node
532 85,440 0 0 42,720 0 0 21,360 0 0 vg_clientmalloc.c:vg_bogus_epilogue]]></programlisting>
533
534
535<para>First up is a summary of the annotation options:</para>
536
537<itemizedlist>
538
539 <listitem>
540 <para>I1 cache, D1 cache, L2 cache: cache configuration. So
541 you know the configuration with which these results were
542 obtained.</para>
543 </listitem>
544
545 <listitem>
546 <para>Command: the command line invocation of the program
547 under examination.</para>
548 </listitem>
549
550 <listitem>
551 <para>Events recorded: event abbreviations are:</para>
552 <itemizedlist>
553 <listitem>
sewardj08e31e22007-05-23 21:58:33 +0000554 <para><computeroutput>Ir</computeroutput>: I cache reads
njn3e986b22004-11-30 10:43:45 +0000555 (ie. instructions executed)</para>
556 </listitem>
557 <listitem>
558 <para><computeroutput>I1mr</computeroutput>: I1 cache read
559 misses</para>
560 </listitem>
561 <listitem>
562 <para><computeroutput>I2mr</computeroutput>: L2 cache
563 instruction read misses</para>
564 </listitem>
565 <listitem>
sewardj08e31e22007-05-23 21:58:33 +0000566 <para><computeroutput>Dr</computeroutput>: D cache reads
njn3e986b22004-11-30 10:43:45 +0000567 (ie. memory reads)</para>
568 </listitem>
569 <listitem>
570 <para><computeroutput>D1mr</computeroutput>: D1 cache read
571 misses</para>
572 </listitem>
573 <listitem>
574 <para><computeroutput>D2mr</computeroutput>: L2 cache data
575 read misses</para>
576 </listitem>
577 <listitem>
sewardj08e31e22007-05-23 21:58:33 +0000578 <para><computeroutput>Dw</computeroutput>: D cache writes
njn3e986b22004-11-30 10:43:45 +0000579 (ie. memory writes)</para>
580 </listitem>
581 <listitem>
582 <para><computeroutput>D1mw</computeroutput>: D1 cache write
583 misses</para>
584 </listitem>
585 <listitem>
586 <para><computeroutput>D2mw</computeroutput>: L2 cache data
587 write misses</para>
588 </listitem>
sewardj8badbaa2007-05-08 09:20:25 +0000589 <listitem>
590 <para><computeroutput>Bc</computeroutput>: Conditional branches
591 executed</para>
592 </listitem>
593 <listitem>
594 <para><computeroutput>Bcm</computeroutput>: Conditional branches
595 mispredicted</para>
596 </listitem>
597 <listitem>
598 <para><computeroutput>Bi</computeroutput>: Indirect branches
599 executed</para>
600 </listitem>
601 <listitem>
602 <para><computeroutput>Bim</computeroutput>: Conditional branches
603 mispredicted</para>
604 </listitem>
njn3e986b22004-11-30 10:43:45 +0000605 </itemizedlist>
606
607 <para>Note that D1 total accesses is given by
608 <computeroutput>D1mr</computeroutput> +
609 <computeroutput>D1mw</computeroutput>, and that L2 total
610 accesses is given by <computeroutput>I2mr</computeroutput> +
611 <computeroutput>D2mr</computeroutput> +
612 <computeroutput>D2mw</computeroutput>.</para>
613 </listitem>
614
615 <listitem>
sewardj08e31e22007-05-23 21:58:33 +0000616 <para>Events shown: the events shown, which is a subset of the events
617 gathered. This can be adjusted with the
njn7e5d4ed2009-07-30 02:57:52 +0000618 <option>--show</option> option.</para>
njn3e986b22004-11-30 10:43:45 +0000619 </listitem>
620
621 <listitem>
622 <para>Event sort order: the sort order in which functions are
623 shown. For example, in this case the functions are sorted
624 from highest <computeroutput>Ir</computeroutput> counts to
625 lowest. If two functions have identical
626 <computeroutput>Ir</computeroutput> counts, they will then be
627 sorted by <computeroutput>I1mr</computeroutput> counts, and
628 so on. This order can be adjusted with the
njn7e5d4ed2009-07-30 02:57:52 +0000629 <option>--sort</option> option.</para>
njn3e986b22004-11-30 10:43:45 +0000630
631 <para>Note that this dictates the order the functions appear.
632 It is <command>not</command> the order in which the columns
633 appear; that is dictated by the "events shown" line (and can
njn7e5d4ed2009-07-30 02:57:52 +0000634 be changed with the <option>--show</option>
njn3e986b22004-11-30 10:43:45 +0000635 option).</para>
636 </listitem>
637
638 <listitem>
njn374a36d2007-11-23 01:41:32 +0000639 <para>Threshold: cg_annotate
sewardj08e31e22007-05-23 21:58:33 +0000640 by default omits functions that cause very low counts
641 to avoid drowning you in information. In this case,
njn3e986b22004-11-30 10:43:45 +0000642 cg_annotate shows summaries the functions that account for
643 99% of the <computeroutput>Ir</computeroutput> counts;
644 <computeroutput>Ir</computeroutput> is chosen as the
645 threshold event since it is the primary sort event. The
646 threshold can be adjusted with the
njn7e5d4ed2009-07-30 02:57:52 +0000647 <option>--threshold</option>
njn3e986b22004-11-30 10:43:45 +0000648 option.</para>
649 </listitem>
650
651 <listitem>
652 <para>Chosen for annotation: names of files specified
653 manually for annotation; in this case none.</para>
654 </listitem>
655
656 <listitem>
657 <para>Auto-annotation: whether auto-annotation was requested
njn7e5d4ed2009-07-30 02:57:52 +0000658 via the <option>--auto=yes</option>
njn3e986b22004-11-30 10:43:45 +0000659 option. In this case no.</para>
660 </listitem>
661
662</itemizedlist>
663
664<para>Then follows summary statistics for the whole
665program. These are similar to the summary provided when running
de03e0e7c2005-12-03 23:02:33 +0000666<computeroutput>valgrind --tool=cachegrind</computeroutput>.</para>
njn3e986b22004-11-30 10:43:45 +0000667
668<para>Then follows function-by-function statistics. Each function
669is identified by a
670<computeroutput>file_name:function_name</computeroutput> pair. If
671a column contains only a dot it means the function never performs
672that event (eg. the third row shows that
673<computeroutput>strcmp()</computeroutput> contains no
674instructions that write to memory). The name
675<computeroutput>???</computeroutput> is used if the the file name
676and/or function name could not be determined from debugging
677information. If most of the entries have the form
678<computeroutput>???:???</computeroutput> the program probably
njn7e5d4ed2009-07-30 02:57:52 +0000679wasn't compiled with <option>-g</option>. If any
njn3e986b22004-11-30 10:43:45 +0000680code was invalidated (either due to self-modifying code or
681unloading of shared objects) its counts are aggregated into a
682single cost centre written as
683<computeroutput>(discarded):(discarded)</computeroutput>.</para>
684
sewardj08e31e22007-05-23 21:58:33 +0000685<para>It is worth noting that functions will come both from
686the profiled program (eg. <filename>concord.c</filename>)
687and from libraries (eg. <filename>getc.c</filename>)</para>
njn3e986b22004-11-30 10:43:45 +0000688
689<para>There are two ways to annotate source files -- by choosing
690them manually, or with the
njn7e5d4ed2009-07-30 02:57:52 +0000691<option>--auto=yes</option> option. To do it
njn374a36d2007-11-23 01:41:32 +0000692manually, just specify the filenames as additional arguments to
693cg_annotate. For example, the
694output from running <filename>cg_annotate &lt;filename&gt;
695concord.c</filename> for our example produces the same output as above
696followed by an annotated version of <filename>concord.c</filename>, a
697section of which looks like:</para>
njn3e986b22004-11-30 10:43:45 +0000698
699<programlisting><![CDATA[
700--------------------------------------------------------------------------------
701-- User-annotated source: concord.c
702--------------------------------------------------------------------------------
703Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw
704
705[snip]
706
707 . . . . . . . . . void init_hash_table(char *file_name, Word_Node *table[])
708 3 1 1 . . . 1 0 0 {
709 . . . . . . . . . FILE *file_ptr;
710 . . . . . . . . . Word_Info *data;
711 1 0 0 . . . 1 1 1 int line = 1, i;
712 . . . . . . . . .
713 5 0 0 . . . 3 0 0 data = (Word_Info *) create(sizeof(Word_Info));
714 . . . . . . . . .
715 4,991 0 0 1,995 0 0 998 0 0 for (i = 0; i < TABLE_SIZE; i++)
716 3,988 1 1 1,994 0 0 997 53 52 table[i] = NULL;
717 . . . . . . . . .
718 . . . . . . . . . /* Open file, check it. */
719 6 0 0 1 0 0 4 0 0 file_ptr = fopen(file_name, "r");
720 2 0 0 1 0 0 . . . if (!(file_ptr)) {
721 . . . . . . . . . fprintf(stderr, "Couldn't open '%s'.\n", file_name);
722 1 1 1 . . . . . . exit(EXIT_FAILURE);
723 . . . . . . . . . }
724 . . . . . . . . .
725 165,062 1 1 73,360 0 0 91,700 0 0 while ((line = get_word(data, line, file_ptr)) != EOF)
726 146,712 0 0 73,356 0 0 73,356 0 0 insert(data->;word, data->line, table);
727 . . . . . . . . .
728 4 0 0 1 0 0 2 0 0 free(data);
729 4 0 0 1 0 0 2 0 0 fclose(file_ptr);
730 3 0 0 2 0 0 . . . }]]></programlisting>
731
732<para>(Although column widths are automatically minimised, a wide
733terminal is clearly useful.)</para>
734
735<para>Each source file is clearly marked
736(<computeroutput>User-annotated source</computeroutput>) as
737having been chosen manually for annotation. If the file was
738found in one of the directories specified with the
njn7e5d4ed2009-07-30 02:57:52 +0000739<option>-I</option>/<option>--include</option> option, the directory
njn3e986b22004-11-30 10:43:45 +0000740and file are both given.</para>
741
742<para>Each line is annotated with its event counts. Events not
sewardj08e31e22007-05-23 21:58:33 +0000743applicable for a line are represented by a dot. This is useful
njn3e986b22004-11-30 10:43:45 +0000744for distinguishing between an event which cannot happen, and one
745which can but did not.</para>
746
747<para>Sometimes only a small section of a source file is
sewardj8d9fec52005-11-15 20:56:23 +0000748executed. To minimise uninteresting output, Cachegrind only shows
njn3e986b22004-11-30 10:43:45 +0000749annotated lines and lines within a small distance of annotated
750lines. Gaps are marked with the line numbers so you know which
751part of a file the shown code comes from, eg:</para>
752
753<programlisting><![CDATA[
754(figures and code for line 704)
755-- line 704 ----------------------------------------
756-- line 878 ----------------------------------------
757(figures and code for line 878)]]></programlisting>
758
759<para>The amount of context to show around annotated lines is
njn7e5d4ed2009-07-30 02:57:52 +0000760controlled by the <option>--context</option>
njn3e986b22004-11-30 10:43:45 +0000761option.</para>
762
763<para>To get automatic annotation, run
njn374a36d2007-11-23 01:41:32 +0000764<computeroutput>cg_annotate &lt;filename&gt; --auto=yes</computeroutput>.
njn3e986b22004-11-30 10:43:45 +0000765cg_annotate will automatically annotate every source file it can
766find that is mentioned in the function-by-function summary.
767Therefore, the files chosen for auto-annotation are affected by
njn7e5d4ed2009-07-30 02:57:52 +0000768the <option>--sort</option> and
769<option>--threshold</option> options. Each
njn3e986b22004-11-30 10:43:45 +0000770source file is clearly marked (<computeroutput>Auto-annotated
771source</computeroutput>) as being chosen automatically. Any
772files that could not be found are mentioned at the end of the
773output, eg:</para>
774
775<programlisting><![CDATA[
776------------------------------------------------------------------
777The following files chosen for auto-annotation could not be found:
778------------------------------------------------------------------
779 getc.c
780 ctype.c
781 ../sysdeps/generic/lockfile.c]]></programlisting>
782
783<para>This is quite common for library files, since libraries are
784usually compiled with debugging information, but the source files
785are often not present on a system. If a file is chosen for
786annotation <command>both</command> manually and automatically, it
787is marked as <computeroutput>User-annotated
njn7e5d4ed2009-07-30 02:57:52 +0000788source</computeroutput>. Use the
789<option>-I</option>/<option>--include</option> option to tell Valgrind where
790to look for source files if the filenames found from the debugging
njn3e986b22004-11-30 10:43:45 +0000791information aren't specific enough.</para>
792
793<para>Beware that cg_annotate can take some time to digest large
njn374a36d2007-11-23 01:41:32 +0000794<computeroutput>cachegrind.out.&lt;pid&gt;</computeroutput> files,
njn3e986b22004-11-30 10:43:45 +0000795e.g. 30 seconds or more. Also beware that auto-annotation can
796produce a lot of output if your program is large!</para>
797
798</sect2>
799
800
801<sect2 id="cg-manual.assembler" xreflabel="Annotating assembler programs">
njn374a36d2007-11-23 01:41:32 +0000802<title>Annotating assembly code programs</title>
njn3e986b22004-11-30 10:43:45 +0000803
njn374a36d2007-11-23 01:41:32 +0000804<para>Valgrind can annotate assembly code programs too, or annotate
805the assembly code generated for your C program. Sometimes this is
njn3e986b22004-11-30 10:43:45 +0000806useful for understanding what is really happening when an
807interesting line of C code is translated into multiple
808instructions.</para>
809
810<para>To do this, you just need to assemble your
njn85a38bc2008-10-30 02:41:13 +0000811<computeroutput>.s</computeroutput> files with assembly-level debug
812information. You can use <computeroutput>gcc
813-S</computeroutput> to compile C/C++ programs to assembly code, and then
814<computeroutput>gcc -g</computeroutput> on the assembly code files to
815achieve this. You can then profile and annotate the assembly code source
816files in the same way as C/C++ source files.</para>
njn3e986b22004-11-30 10:43:45 +0000817
818</sect2>
819
njn7064fb22008-05-29 23:09:52 +0000820<sect2 id="ms-manual.forkingprograms" xreflabel="Forking Programs">
821<title>Forking Programs</title>
822<para>If your program forks, the child will inherit all the profiling data that
823has been gathered for the parent.</para>
824
825<para>If the output file format string (controlled by
826<option>--cachegrind-out-file</option>) does not contain <option>%p</option>,
827then the outputs from the parent and child will be intermingled in a single
828output file, which will almost certainly make it unreadable by
829cg_annotate.</para>
830</sect2>
831
832
njn3e986b22004-11-30 10:43:45 +0000833</sect1>
834
835
836<sect1 id="cg-manual.annopts" xreflabel="cg_annotate options">
njn374a36d2007-11-23 01:41:32 +0000837<title>cg_annotate options</title>
njn3e986b22004-11-30 10:43:45 +0000838
839<itemizedlist>
840
njn3e986b22004-11-30 10:43:45 +0000841 <listitem>
njn7e5d4ed2009-07-30 02:57:52 +0000842 <para><option>-h --help</option></para>
843 <para><option>-v --version</option></para>
njn3e986b22004-11-30 10:43:45 +0000844 <para>Help and version, as usual.</para>
845 </listitem>
846
debc32e822005-06-25 14:43:05 +0000847 <listitem id="sort">
njn7e5d4ed2009-07-30 02:57:52 +0000848 <para><option>--sort=A,B,C</option> [default:
njn3e986b22004-11-30 10:43:45 +0000849 order in
njn374a36d2007-11-23 01:41:32 +0000850 <computeroutput>cachegrind.out.&lt;pid&gt;</computeroutput>]</para>
njn3e986b22004-11-30 10:43:45 +0000851 <para>Specifies the events upon which the sorting of the
852 function-by-function entries will be based. Useful if you
853 want to concentrate on eg. I cache misses
njn7e5d4ed2009-07-30 02:57:52 +0000854 (<option>--sort=I1mr,I2mr</option>), or D cache misses
855 (<option>--sort=D1mr,D2mr</option>), or L2 misses
856 (<option>--sort=D2mr,I2mr</option>).</para>
njn3e986b22004-11-30 10:43:45 +0000857 </listitem>
858
debc32e822005-06-25 14:43:05 +0000859 <listitem id="show">
njn7e5d4ed2009-07-30 02:57:52 +0000860 <para><option>--show=A,B,C</option> [default:
njn3e986b22004-11-30 10:43:45 +0000861 all, using order in
njn374a36d2007-11-23 01:41:32 +0000862 <computeroutput>cachegrind.out.&lt;pid&gt;</computeroutput>]</para>
njn3e986b22004-11-30 10:43:45 +0000863 <para>Specifies which events to show (and the column
864 order). Default is to use all present in the
njn374a36d2007-11-23 01:41:32 +0000865 <computeroutput>cachegrind.out.&lt;pid&gt;</computeroutput> file (and
njn3e986b22004-11-30 10:43:45 +0000866 use the order in the file).</para>
867 </listitem>
868
debc32e822005-06-25 14:43:05 +0000869 <listitem id="threshold">
njn7e5d4ed2009-07-30 02:57:52 +0000870 <para><option>--threshold=X</option>
njn3e986b22004-11-30 10:43:45 +0000871 [default: 99%]</para>
872 <para>Sets the threshold for the function-by-function
873 summary. Functions are shown that account for more than X%
874 of the primary sort event. If auto-annotating, also affects
875 which files are annotated.</para>
876
877 <para>Note: thresholds can be set for more than one of the
878 events by appending any events for the
njn7e5d4ed2009-07-30 02:57:52 +0000879 <option>--sort</option> option with a colon
njn3e986b22004-11-30 10:43:45 +0000880 and a number (no spaces, though). E.g. if you want to see
881 the functions that cover 99% of L2 read misses and 99% of L2
882 write misses, use this option:</para>
njn7e5d4ed2009-07-30 02:57:52 +0000883 <para><option>--sort=D2mr:99,D2mw:99</option></para>
njn3e986b22004-11-30 10:43:45 +0000884 </listitem>
885
debc32e822005-06-25 14:43:05 +0000886 <listitem id="auto">
njn7e5d4ed2009-07-30 02:57:52 +0000887 <para><option>--auto=no</option> [default]</para>
888 <para><option>--auto=yes</option></para>
njn3e986b22004-11-30 10:43:45 +0000889 <para>When enabled, automatically annotates every file that
890 is mentioned in the function-by-function summary that can be
891 found. Also gives a list of those that couldn't be found.</para>
892 </listitem>
893
debc32e822005-06-25 14:43:05 +0000894 <listitem id="context">
njn7e5d4ed2009-07-30 02:57:52 +0000895 <para><option>--context=N</option> [default: 8]</para>
njn3e986b22004-11-30 10:43:45 +0000896 <para>Print N lines of context before and after each
897 annotated line. Avoids printing large sections of source
898 files that were not executed. Use a large number
899 (eg. 10,000) to show all source lines.</para>
900 </listitem>
901
debc32e822005-06-25 14:43:05 +0000902 <listitem id="include">
njn7e5d4ed2009-07-30 02:57:52 +0000903 <para><option>-I&lt;dir&gt;, --include=&lt;dir&gt;</option>
904 [default: empty string]</para>
njn3e986b22004-11-30 10:43:45 +0000905 <para>Adds a directory to the list in which to search for
906 files. Multiple -I/--include options can be given to add
907 multiple directories.</para>
908 </listitem>
909
910</itemizedlist>
911
912
913
sewardj778d7832007-11-22 01:21:56 +0000914<sect2 id="cg-manual.annopts.warnings" xreflabel="Warnings">
njn3e986b22004-11-30 10:43:45 +0000915<title>Warnings</title>
916
917<para>There are a couple of situations in which
njn374a36d2007-11-23 01:41:32 +0000918cg_annotate issues warnings.</para>
njn3e986b22004-11-30 10:43:45 +0000919
920<itemizedlist>
921 <listitem>
922 <para>If a source file is more recent than the
njn374a36d2007-11-23 01:41:32 +0000923 <computeroutput>cachegrind.out.&lt;pid&gt;</computeroutput> file.
njn3e986b22004-11-30 10:43:45 +0000924 This is because the information in
njn374a36d2007-11-23 01:41:32 +0000925 <computeroutput>cachegrind.out.&lt;pid&gt;</computeroutput> is only
njn3e986b22004-11-30 10:43:45 +0000926 recorded with line numbers, so if the line numbers change at
927 all in the source (eg. lines added, deleted, swapped), any
928 annotations will be incorrect.</para>
929 </listitem>
930 <listitem>
931 <para>If information is recorded about line numbers past the
932 end of a file. This can be caused by the above problem,
933 ie. shortening the source file while using an old
njn374a36d2007-11-23 01:41:32 +0000934 <computeroutput>cachegrind.out.&lt;pid&gt;</computeroutput> file. If
njn3e986b22004-11-30 10:43:45 +0000935 this happens, the figures for the bogus lines are printed
936 anyway (clearly marked as bogus) in case they are
937 important.</para>
938 </listitem>
939</itemizedlist>
940
941</sect2>
942
943
944
sewardj778d7832007-11-22 01:21:56 +0000945<sect2 id="cg-manual.annopts.things-to-watch-out-for"
946 xreflabel="Things to watch out for">
njn3e986b22004-11-30 10:43:45 +0000947<title>Things to watch out for</title>
948
949<para>Some odd things that can occur during annotation:</para>
950
951<itemizedlist>
952 <listitem>
953 <para>If annotating at the assembler level, you might see
954 something like this:</para>
955<programlisting><![CDATA[
956 1 0 0 . . . . . . leal -12(%ebp),%eax
957 1 0 0 . . . 1 0 0 movl %eax,84(%ebx)
958 2 0 0 0 0 0 1 0 0 movl $1,-20(%ebp)
959 . . . . . . . . . .align 4,0x90
960 1 0 0 . . . . . . movl $.LnrB,%eax
961 1 0 0 . . . 1 0 0 movl %eax,-16(%ebp)]]></programlisting>
962
963 <para>How can the third instruction be executed twice when
964 the others are executed only once? As it turns out, it
965 isn't. Here's a dump of the executable, using
966 <computeroutput>objdump -d</computeroutput>:</para>
967<programlisting><![CDATA[
968 8048f25: 8d 45 f4 lea 0xfffffff4(%ebp),%eax
969 8048f28: 89 43 54 mov %eax,0x54(%ebx)
970 8048f2b: c7 45 ec 01 00 00 00 movl $0x1,0xffffffec(%ebp)
971 8048f32: 89 f6 mov %esi,%esi
972 8048f34: b8 08 8b 07 08 mov $0x8078b08,%eax
973 8048f39: 89 45 f0 mov %eax,0xfffffff0(%ebp)]]></programlisting>
974
975 <para>Notice the extra <computeroutput>mov
976 %esi,%esi</computeroutput> instruction. Where did this come
977 from? The GNU assembler inserted it to serve as the two
978 bytes of padding needed to align the <computeroutput>movl
979 $.LnrB,%eax</computeroutput> instruction on a four-byte
980 boundary, but pretended it didn't exist when adding debug
981 information. Thus when Valgrind reads the debug info it
982 thinks that the <computeroutput>movl
983 $0x1,0xffffffec(%ebp)</computeroutput> instruction covers the
984 address range 0x8048f2b--0x804833 by itself, and attributes
985 the counts for the <computeroutput>mov
986 %esi,%esi</computeroutput> to it.</para>
987 </listitem>
988
989 <listitem>
990 <para>Inlined functions can cause strange results in the
991 function-by-function summary. If a function
992 <computeroutput>inline_me()</computeroutput> is defined in
993 <filename>foo.h</filename> and inlined in the functions
994 <computeroutput>f1()</computeroutput>,
995 <computeroutput>f2()</computeroutput> and
996 <computeroutput>f3()</computeroutput> in
997 <filename>bar.c</filename>, there will not be a
998 <computeroutput>foo.h:inline_me()</computeroutput> function
999 entry. Instead, there will be separate function entries for
1000 each inlining site, ie.
1001 <computeroutput>foo.h:f1()</computeroutput>,
1002 <computeroutput>foo.h:f2()</computeroutput> and
1003 <computeroutput>foo.h:f3()</computeroutput>. To find the
1004 total counts for
1005 <computeroutput>foo.h:inline_me()</computeroutput>, add up
1006 the counts from each entry.</para>
1007
1008 <para>The reason for this is that although the debug info
1009 output by gcc indicates the switch from
1010 <filename>bar.c</filename> to <filename>foo.h</filename>, it
1011 doesn't indicate the name of the function in
1012 <filename>foo.h</filename>, so Valgrind keeps using the old
1013 one.</para>
1014 </listitem>
1015
1016 <listitem>
1017 <para>Sometimes, the same filename might be represented with
1018 a relative name and with an absolute name in different parts
1019 of the debug info, eg:
1020 <filename>/home/user/proj/proj.h</filename> and
1021 <filename>../proj.h</filename>. In this case, if you use
1022 auto-annotation, the file will be annotated twice with the
1023 counts split between the two.</para>
1024 </listitem>
1025
1026 <listitem>
1027 <para>Files with more than 65,535 lines cause difficulties
sewardj08e31e22007-05-23 21:58:33 +00001028 for the Stabs-format debug info reader. This is because the line
njn3e986b22004-11-30 10:43:45 +00001029 number in the <computeroutput>struct nlist</computeroutput>
1030 defined in <filename>a.out.h</filename> under Linux is only a
1031 16-bit value. Valgrind can handle some files with more than
1032 65,535 lines correctly by making some guesses to identify
1033 line number overflows. But some cases are beyond it, in
1034 which case you'll get a warning message explaining that
1035 annotations for the file might be incorrect.</para>
sewardj08e31e22007-05-23 21:58:33 +00001036
1037 <para>If you are using gcc 3.1 or later, this is most likely
1038 irrelevant, since gcc switched to using the more modern DWARF2
1039 format by default at version 3.1. DWARF2 does not have any such
1040 limitations on line numbers.</para>
njn3e986b22004-11-30 10:43:45 +00001041 </listitem>
1042
1043 <listitem>
1044 <para>If you compile some files with
njn7e5d4ed2009-07-30 02:57:52 +00001045 <option>-g</option> and some without, some
njn3e986b22004-11-30 10:43:45 +00001046 events that take place in a file without debug info could be
1047 attributed to the last line of a file with debug info
1048 (whichever one gets placed before the non-debug-info file in
1049 the executable).</para>
1050 </listitem>
1051
1052</itemizedlist>
1053
1054<para>This list looks long, but these cases should be fairly
1055rare.</para>
1056
njn3e986b22004-11-30 10:43:45 +00001057</sect2>
1058
1059
1060
sewardj778d7832007-11-22 01:21:56 +00001061<sect2 id="cg-manual.annopts.accuracy" xreflabel="Accuracy">
njn3e986b22004-11-30 10:43:45 +00001062<title>Accuracy</title>
1063
1064<para>Valgrind's cache profiling has a number of
1065shortcomings:</para>
1066
1067<itemizedlist>
1068 <listitem>
1069 <para>It doesn't account for kernel activity -- the effect of
1070 system calls on the cache contents is ignored.</para>
1071 </listitem>
1072
1073 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001074 <para>It doesn't account for other process activity.
1075 This is probably desirable when considering a single
1076 program.</para>
njn3e986b22004-11-30 10:43:45 +00001077 </listitem>
1078
1079 <listitem>
1080 <para>It doesn't account for virtual-to-physical address
sewardj08e31e22007-05-23 21:58:33 +00001081 mappings. Hence the simulation is not a true
njn3e986b22004-11-30 10:43:45 +00001082 representation of what's happening in the
sewardj08e31e22007-05-23 21:58:33 +00001083 cache. Most caches are physically indexed, but Cachegrind
1084 simulates caches using virtual addresses.</para>
njn3e986b22004-11-30 10:43:45 +00001085 </listitem>
1086
1087 <listitem>
1088 <para>It doesn't account for cache misses not visible at the
1089 instruction level, eg. those arising from TLB misses, or
1090 speculative execution.</para>
1091 </listitem>
1092
1093 <listitem>
sewardj8d9fec52005-11-15 20:56:23 +00001094 <para>Valgrind will schedule
1095 threads differently from how they would be when running natively.
1096 This could warp the results for threaded programs.</para>
njn3e986b22004-11-30 10:43:45 +00001097 </listitem>
1098
1099 <listitem>
sewardj8d9fec52005-11-15 20:56:23 +00001100 <para>The x86/amd64 instructions <computeroutput>bts</computeroutput>,
njn3e986b22004-11-30 10:43:45 +00001101 <computeroutput>btr</computeroutput> and
1102 <computeroutput>btc</computeroutput> will incorrectly be
1103 counted as doing a data read if both the arguments are
1104 registers, eg:</para>
1105<programlisting><![CDATA[
1106 btsl %eax, %edx]]></programlisting>
1107
1108 <para>This should only happen rarely.</para>
1109 </listitem>
1110
1111 <listitem>
sewardj8d9fec52005-11-15 20:56:23 +00001112 <para>x86/amd64 FPU instructions with data sizes of 28 and 108 bytes
njn3e986b22004-11-30 10:43:45 +00001113 (e.g. <computeroutput>fsave</computeroutput>) are treated as
1114 though they only access 16 bytes. These instructions seem to
1115 be rare so hopefully this won't affect accuracy much.</para>
1116 </listitem>
1117
1118</itemizedlist>
1119
sewardj08e31e22007-05-23 21:58:33 +00001120<para>Another thing worth noting is that results are very sensitive.
1121Changing the size of the the executable being profiled, or the sizes
1122of any of the shared libraries it uses, or even the length of their
1123file names, can perturb the results. Variations will be small, but
1124don't expect perfectly repeatable results if your program changes at
1125all.</para>
njn3e986b22004-11-30 10:43:45 +00001126
sewardj08e31e22007-05-23 21:58:33 +00001127<para>More recent GNU/Linux distributions do address space
1128randomisation, in which identical runs of the same program have their
1129shared libraries loaded at different locations, as a security measure.
1130This also perturbs the results.</para>
sewardj94dc5082007-02-08 11:31:03 +00001131
njn3e986b22004-11-30 10:43:45 +00001132<para>While these factors mean you shouldn't trust the results to
1133be super-accurate, hopefully they should be close enough to be
1134useful.</para>
1135
1136</sect2>
1137
njn534f7812006-10-21 22:22:59 +00001138</sect1>
1139
sewardj94dc5082007-02-08 11:31:03 +00001140
1141
1142<sect1 id="cg-manual.cg_merge" xreflabel="cg_merge">
njn374a36d2007-11-23 01:41:32 +00001143<title>Merging profiles with cg_merge</title>
sewardj94dc5082007-02-08 11:31:03 +00001144
1145<para>
njn374a36d2007-11-23 01:41:32 +00001146cg_merge is a simple program which
sewardj94dc5082007-02-08 11:31:03 +00001147reads multiple profile files, as created by cachegrind, merges them
1148together, and writes the results into another file in the same format.
1149You can then examine the merged results using
njn374a36d2007-11-23 01:41:32 +00001150<computeroutput>cg_annotate &lt;filename&gt;</computeroutput>, as
sewardj94dc5082007-02-08 11:31:03 +00001151described above. The merging functionality might be useful if you
1152want to aggregate costs over multiple runs of the same program, or
1153from a single parallel run with multiple instances of the same
1154program.</para>
1155
1156<para>
njn374a36d2007-11-23 01:41:32 +00001157cg_merge is invoked as follows:
sewardj94dc5082007-02-08 11:31:03 +00001158</para>
1159
1160<programlisting><![CDATA[
1161cg_merge -o outputfile file1 file2 file3 ...]]></programlisting>
1162
1163<para>
1164It reads and checks <computeroutput>file1</computeroutput>, then read
1165and checks <computeroutput>file2</computeroutput> and merges it into
1166the running totals, then the same with
1167<computeroutput>file3</computeroutput>, etc. The final results are
1168written to <computeroutput>outputfile</computeroutput>, or to standard
1169out if no output file is specified.</para>
1170
1171<para>
1172Costs are summed on a per-function, per-line and per-instruction
1173basis. Because of this, the order in which the input files does not
1174matter, although you should take care to only mention each file once,
1175since any file mentioned twice will be added in twice.</para>
1176
1177<para>
njn374a36d2007-11-23 01:41:32 +00001178cg_merge does not attempt to check
sewardj94dc5082007-02-08 11:31:03 +00001179that the input files come from runs of the same executable. It will
1180happily merge together profile files from completely unrelated
1181programs. It does however check that the
1182<computeroutput>Events:</computeroutput> lines of all the inputs are
1183identical, so as to ensure that the addition of costs makes sense.
1184For example, it would be nonsensical for it to add a number indicating
1185D1 read references to a number from a different file indicating L2
1186write misses.</para>
1187
1188<para>
1189A number of other syntax and sanity checks are done whilst reading the
njn374a36d2007-11-23 01:41:32 +00001190inputs. cg_merge will stop and
sewardj94dc5082007-02-08 11:31:03 +00001191attempt to print a helpful error message if any of the input files
1192fail these checks.</para>
1193
1194</sect1>
1195
1196
sewardj778d7832007-11-22 01:21:56 +00001197<sect1 id="cg-manual.acting-on"
1198 xreflabel="Acting on Cachegrind's information">
njn3a9d5dc2007-09-17 22:19:01 +00001199<title>Acting on Cachegrind's information</title>
1200<para>
1201So, you've managed to profile your program with Cachegrind. Now what?
1202What's the best way to actually act on the information it provides to speed
njn07f96562007-09-17 22:28:21 +00001203up your program? Here are some rules of thumb that we have found to be
1204useful.</para>
njn3a9d5dc2007-09-17 22:19:01 +00001205
1206<para>
1207First of all, the global hit/miss rate numbers are not that useful. If you
1208have multiple programs or multiple runs of a program, comparing the numbers
njn07f96562007-09-17 22:28:21 +00001209might identify if any are outliers and worthy of closer investigation.
1210Otherwise, they're not enough to act on.</para>
njn3a9d5dc2007-09-17 22:19:01 +00001211
1212<para>
njn07f96562007-09-17 22:28:21 +00001213The line-by-line source code annotations are much more useful. In our
1214experience, the best place to start is by looking at the
1215<computeroutput>Ir</computeroutput> numbers. They simply measure how many
1216instructions were executed for each line, and don't include any cache
1217information, but they can still be very useful for identifying
1218bottlenecks.</para>
njn3a9d5dc2007-09-17 22:19:01 +00001219
1220<para>
1221After that, we have found that L2 misses are typically a much bigger source
1222of slow-downs than L1 misses. So it's worth looking for any snippets of
njn07f96562007-09-17 22:28:21 +00001223code that cause a high proportion of the L2 misses. If you find any, it's
1224still not always easy to work out how to improve things. You need to have a
1225reasonable understanding of how caches work, the principles of locality, and
1226your program's data access patterns. Improving things may require
1227redesigning a data structure, for example.</para>
njn3a9d5dc2007-09-17 22:19:01 +00001228
1229<para>
1230In short, Cachegrind can tell you where some of the bottlenecks in your code
1231are, but it can't tell you how to fix them. You have to work that out for
1232yourself. But at least you have the information!
1233</para>
1234
1235</sect1>
sewardj94dc5082007-02-08 11:31:03 +00001236
sewardj778d7832007-11-22 01:21:56 +00001237<sect1 id="cg-manual.impl-details"
1238 xreflabel="Implementation details">
njn534f7812006-10-21 22:22:59 +00001239<title>Implementation details</title>
njn3a9d5dc2007-09-17 22:19:01 +00001240<para>
njn534f7812006-10-21 22:22:59 +00001241This section talks about details you don't need to know about in order to
1242use Cachegrind, but may be of interest to some people.
njn3a9d5dc2007-09-17 22:19:01 +00001243</para>
njn3e986b22004-11-30 10:43:45 +00001244
sewardj778d7832007-11-22 01:21:56 +00001245<sect2 id="cg-manual.impl-details.how-cg-works"
1246 xreflabel="How Cachegrind works">
njn534f7812006-10-21 22:22:59 +00001247<title>How Cachegrind works</title>
1248<para>The best reference for understanding how Cachegrind works is chapter 3 of
1249"Dynamic Binary Analysis and Instrumentation", by Nicholas Nethercote. It
njn011215f2006-10-21 23:00:59 +00001250is available on the <ulink url="&vg-pubs;">Valgrind publications
1251page</ulink>.</para>
njn534f7812006-10-21 22:22:59 +00001252</sect2>
njn3e986b22004-11-30 10:43:45 +00001253
sewardj778d7832007-11-22 01:21:56 +00001254<sect2 id="cg-manual.impl-details.file-format"
1255 xreflabel="Cachegrind output file format">
njn534f7812006-10-21 22:22:59 +00001256<title>Cachegrind output file format</title>
1257<para>The file format is fairly straightforward, basically giving the
1258cost centre for every line, grouped by files and
1259functions. Total counts (eg. total cache accesses, total L1
1260misses) are calculated when traversing this structure rather than
1261during execution, to save time; the cache simulation functions
1262are called so often that even one or two extra adds can make a
1263sizeable difference.</para>
1264
1265<para>The file format:</para>
1266<programlisting><![CDATA[
1267file ::= desc_line* cmd_line events_line data_line+ summary_line
1268desc_line ::= "desc:" ws? non_nl_string
1269cmd_line ::= "cmd:" ws? cmd
1270events_line ::= "events:" ws? (event ws)+
1271data_line ::= file_line | fn_line | count_line
1272file_line ::= "fl=" filename
1273fn_line ::= "fn=" fn_name
1274count_line ::= line_num ws? (count ws)+
1275summary_line ::= "summary:" ws? (count ws)+
1276count ::= num | "."]]></programlisting>
1277
1278<para>Where:</para>
njn3e986b22004-11-30 10:43:45 +00001279<itemizedlist>
1280 <listitem>
njn534f7812006-10-21 22:22:59 +00001281 <para><computeroutput>non_nl_string</computeroutput> is any
1282 string not containing a newline.</para>
njn3e986b22004-11-30 10:43:45 +00001283 </listitem>
njn534f7812006-10-21 22:22:59 +00001284 <listitem>
1285 <para><computeroutput>cmd</computeroutput> is a string holding the
1286 command line of the profiled program.</para>
1287 </listitem>
1288 <listitem>
njn26242122007-01-22 03:21:27 +00001289 <para><computeroutput>event</computeroutput> is a string containing
1290 no whitespace.</para>
1291 </listitem>
1292 <listitem>
njn534f7812006-10-21 22:22:59 +00001293 <para><computeroutput>filename</computeroutput> and
1294 <computeroutput>fn_name</computeroutput> are strings.</para>
1295 </listitem>
1296 <listitem>
1297 <para><computeroutput>num</computeroutput> and
1298 <computeroutput>line_num</computeroutput> are decimal
1299 numbers.</para>
1300 </listitem>
1301 <listitem>
1302 <para><computeroutput>ws</computeroutput> is whitespace.</para>
1303 </listitem>
1304</itemizedlist>
1305
1306<para>The contents of the "desc:" lines are printed out at the top
1307of the summary. This is a generic way of providing simulation
1308specific information, eg. for giving the cache configuration for
1309cache simulation.</para>
1310
1311<para>More than one line of info can be presented for each file/fn/line number.
1312In such cases, the counts for the named events will be accumulated.</para>
1313
njn3a9d5dc2007-09-17 22:19:01 +00001314<para>Counts can be "." to represent zero. This makes the files easier for
1315humans to read.</para>
njn534f7812006-10-21 22:22:59 +00001316
1317<para>The number of counts in each
1318<computeroutput>line</computeroutput> and the
1319<computeroutput>summary_line</computeroutput> should not exceed
1320the number of events in the
1321<computeroutput>event_line</computeroutput>. If the number in
1322each <computeroutput>line</computeroutput> is less, cg_annotate
njn3a9d5dc2007-09-17 22:19:01 +00001323treats those missing as though they were a "." entry. This saves space.
1324</para>
njn534f7812006-10-21 22:22:59 +00001325
1326<para>A <computeroutput>file_line</computeroutput> changes the
1327current file name. A <computeroutput>fn_line</computeroutput>
1328changes the current function name. A
1329<computeroutput>count_line</computeroutput> contains counts that
1330pertain to the current filename/fn_name. A "fn="
1331<computeroutput>file_line</computeroutput> and a
1332<computeroutput>fn_line</computeroutput> must appear before any
1333<computeroutput>count_line</computeroutput>s to give the context
1334of the first <computeroutput>count_line</computeroutput>s.</para>
1335
1336<para>Each <computeroutput>file_line</computeroutput> will normally be
1337immediately followed by a <computeroutput>fn_line</computeroutput>. But it
1338doesn't have to be.</para>
1339
njn3e986b22004-11-30 10:43:45 +00001340
1341</sect2>
1342
1343</sect1>
1344</chapter>