blob: 2a643ecc06c93e09410f260ba5f5eee28e04ceb7 [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
njn3e986b22004-11-30 10:43:45 +00007<chapter id="cg-manual" xreflabel="Cachegrind: a cache-miss profiler">
sewardj8badbaa2007-05-08 09:20:25 +00008<title>Cachegrind: a cache and branch profiler</title>
njn3e986b22004-11-30 10:43:45 +00009
njn3e986b22004-11-30 10:43:45 +000010<sect1 id="cg-manual.cache" xreflabel="Cache profiling">
sewardj8badbaa2007-05-08 09:20:25 +000011<title>Cache and branch profiling</title>
njn3e986b22004-11-30 10:43:45 +000012
13<para>To use this tool, you must specify
14<computeroutput>--tool=cachegrind</computeroutput> on the
15Valgrind command line.</para>
16
sewardj8badbaa2007-05-08 09:20:25 +000017<para>Cachegrind is a tool for finding places where programs
18interact badly with typical modern superscalar processors
19and run slowly as a result.
20In particular, it will do a cache simulation of your program,
21and optionally a branch-predictor simulation, and can
22then annotate your source line-by-line with the number of cache
23misses and branch mispredictions. The following statistics are
24collected:</para>
njn3e986b22004-11-30 10:43:45 +000025<itemizedlist>
26 <listitem>
27 <para>L1 instruction cache reads and misses;</para>
28 </listitem>
29 <listitem>
30 <para>L1 data cache reads and read misses, writes and write
31 misses;</para>
32 </listitem>
33 <listitem>
34 <para>L2 unified cache reads and read misses, writes and
35 writes misses.</para>
36 </listitem>
sewardj8badbaa2007-05-08 09:20:25 +000037 <listitem>
38 <para>Conditional branches and mispredicted conditional branches.</para>
39 </listitem>
40 <listitem>
41 <para>Indirect branches and mispredicted indirect branches. An
42 indirect branch is a jump or call to a destination only known at
43 run time.</para>
44 </listitem>
njn3e986b22004-11-30 10:43:45 +000045</itemizedlist>
46
njnc8cccb12005-07-25 23:30:24 +000047<para>On a modern machine, an L1 miss will typically cost
sewardj8badbaa2007-05-08 09:20:25 +000048around 10 cycles, an L2 miss can cost as much as 200
49cycles, and a mispredicted branch costs in the region of 10
50to 30 cycles. Detailed cache and branch profiling can be very useful
51for improving the performance of your program.</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
55executed per line, which can be useful for traditional profiling
56and test coverage.</para>
57
sewardj8badbaa2007-05-08 09:20:25 +000058<para>Branch profiling is not enabled by default. To use it, you must
59additionally specify <computeroutput>--branch-sim=yes</computeroutput>
60on the command line.</para>
61
njn3e986b22004-11-30 10:43:45 +000062
63<sect2 id="cg-manual.overview" xreflabel="Overview">
64<title>Overview</title>
65
66<para>First off, as for normal Valgrind use, you probably want to
67compile with debugging info (the
68<computeroutput>-g</computeroutput> flag). But by contrast with
69normal Valgrind use, you probably <command>do</command> want to turn
70optimisation on, since you should profile your program as it will
71be normally run.</para>
72
73<para>The two steps are:</para>
74<orderedlist>
75 <listitem>
76 <para>Run your program with <computeroutput>valgrind
77 --tool=cachegrind</computeroutput> in front of the normal
78 command line invocation. When the program finishes,
79 Cachegrind will print summary cache statistics. It also
80 collects line-by-line information in a file
81 <computeroutput>cachegrind.out.pid</computeroutput>, where
82 <computeroutput>pid</computeroutput> is the program's process
83 id.</para>
84
sewardj8badbaa2007-05-08 09:20:25 +000085 <para>Branch prediction statistics are not collected by default.
86 To do so, add the flag
87 <computeroutput>--branch-sim=yes</computeroutput>.
88 </para>
89
njn3e986b22004-11-30 10:43:45 +000090 <para>This step should be done every time you want to collect
91 information about a new program, a changed program, or about
92 the same program with different input.</para>
93 </listitem>
94
95 <listitem>
96 <para>Generate a function-by-function summary, and possibly
97 annotate source files, using the supplied
98 <computeroutput>cg_annotate</computeroutput> program. Source
99 files to annotate can be specified manually, or manually on
100 the command line, or "interesting" source files can be
101 annotated automatically with the
102 <computeroutput>--auto=yes</computeroutput> option. You can
103 annotate C/C++ files or assembly language files equally
104 easily.</para>
105
106 <para>This step can be performed as many times as you like
107 for each Step 2. You may want to do multiple annotations
108 showing different information each time.</para>
109 </listitem>
110
111</orderedlist>
112
sewardj94dc5082007-02-08 11:31:03 +0000113<para>As an optional intermediate step, you can use the supplied
114<computeroutput>cg_merge</computeroutput> program to sum together the
115outputs of multiple Cachegrind runs, into a single file which you then
116use as the input for
117<computeroutput>cg_annotate</computeroutput>.</para>
118
sewardj08e31e22007-05-23 21:58:33 +0000119<para>These steps are described in detail in the following
njn3e986b22004-11-30 10:43:45 +0000120sections.</para>
121
122</sect2>
123
124
debc32e822005-06-25 14:43:05 +0000125<sect2 id="cache-sim" xreflabel="Cache simulation specifics">
njn3e986b22004-11-30 10:43:45 +0000126<title>Cache simulation specifics</title>
127
sewardj08e31e22007-05-23 21:58:33 +0000128<para>Cachegrind simulates a machine with independent
129first level instruction and data caches (I1 and D1), backed by a
130unified second level cache (L2). This configuration is used by almost
131all modern machines. Some old Cyrix CPUs had a unified I and D L1
132cache, but they are ancient history now.</para>
njn3e986b22004-11-30 10:43:45 +0000133
sewardj08e31e22007-05-23 21:58:33 +0000134<para>Specific characteristics of the simulation are as
135follows:</para>
njn3e986b22004-11-30 10:43:45 +0000136
137<itemizedlist>
138
139 <listitem>
140 <para>Write-allocate: when a write miss occurs, the block
141 written to is brought into the D1 cache. Most modern caches
142 have this property.</para>
143 </listitem>
144
145 <listitem>
146 <para>Bit-selection hash function: the line(s) in the cache
147 to which a memory block maps is chosen by the middle bits
148 M--(M+N-1) of the byte address, where:</para>
149 <itemizedlist>
150 <listitem>
151 <para>line size = 2^M bytes</para>
152 </listitem>
153 <listitem>
154 <para>(cache size / line size) = 2^N bytes</para>
155 </listitem>
156 </itemizedlist>
157 </listitem>
158
159 <listitem>
160 <para>Inclusive L2 cache: the L2 cache replicates all the
161 entries of the L1 cache. This is standard on Pentium chips,
sewardj08e31e22007-05-23 21:58:33 +0000162 but AMD Opterons, Athlons and Durons
163 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
171the CPUID instruction. If you have an old machine that (a)
172doesn'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
178<computeroutput>--I1</computeroutput>,
179<computeroutput>--D1</computeroutput> and
180<computeroutput>--L2</computeroutput> options.</para>
181
sewardj08e31e22007-05-23 21:58:33 +0000182<para>On PowerPC platforms
183Cachegrind cannot automatically
184determine the cache configuration, so you will
185need to specify it with the
186<computeroutput>--I1</computeroutput>,
187<computeroutput>--D1</computeroutput> and
188<computeroutput>--L2</computeroutput> options.</para>
189
njn3e986b22004-11-30 10:43:45 +0000190
191<para>Other noteworthy behaviour:</para>
192
193<itemizedlist>
194 <listitem>
195 <para>References that straddle two cache lines are treated as
196 follows:</para>
197 <itemizedlist>
198 <listitem>
199 <para>If both blocks hit --&gt; counted as one hit</para>
200 </listitem>
201 <listitem>
202 <para>If one block hits, the other misses --&gt; counted
203 as one miss.</para>
204 </listitem>
205 <listitem>
206 <para>If both blocks miss --&gt; counted as one miss (not
207 two)</para>
208 </listitem>
209 </itemizedlist>
210 </listitem>
211
212 <listitem>
213 <para>Instructions that modify a memory location
214 (eg. <computeroutput>inc</computeroutput> and
215 <computeroutput>dec</computeroutput>) are counted as doing
216 just a read, ie. a single data reference. This may seem
217 strange, but since the write can never cause a miss (the read
218 guarantees the block is in the cache) it's not very
219 interesting.</para>
220
221 <para>Thus it measures not the number of times the data cache
222 is accessed, but the number of times a data cache miss could
223 occur.</para>
224 </listitem>
225
226</itemizedlist>
227
228<para>If you are interested in simulating a cache with different
229properties, it is not particularly hard to write your own cache
230simulator, or to modify the existing ones in
231<computeroutput>vg_cachesim_I1.c</computeroutput>,
232<computeroutput>vg_cachesim_D1.c</computeroutput>,
233<computeroutput>vg_cachesim_L2.c</computeroutput> and
234<computeroutput>vg_cachesim_gen.c</computeroutput>. We'd be
235interested 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
njn3e986b22004-11-30 10:43:45 +0000339<computeroutput>cachegrind.out.pid</computeroutput>. This file
sewardje1216cb2007-02-07 19:55:30 +0000340is human-readable, but is intended to be interpreted by the accompanying
njn3e986b22004-11-30 10:43:45 +0000341program <computeroutput>cg_annotate</computeroutput>, described
342in the next section.</para>
343
344<para>Things to note about the
345<computeroutput>cachegrind.out.pid</computeroutput>
346file:</para>
347
348<itemizedlist>
349 <listitem>
350 <para>It is written every time Cachegrind is run, and will
351 overwrite any existing
352 <computeroutput>cachegrind.out.pid</computeroutput>
353 in the current directory (but that won't happen very often
354 because it takes some time for process ids to be
355 recycled).</para>
sewardje1216cb2007-02-07 19:55:30 +0000356 <para>
357 To use a basename other than the default
sewardj8693e012007-02-08 06:47:19 +0000358 <computeroutput>cachegrind.out</computeroutput>,
sewardje1216cb2007-02-07 19:55:30 +0000359 use the <computeroutput>--cachegrind-out-file</computeroutput>
360 switch.</para>
361 <para>
362 To add further qualifiers to the output filename you can use
363 the core's <computeroutput>--log-file-qualifier</computeroutput>
sewardj8693e012007-02-08 06:47:19 +0000364 flag. This extends the file name further with the text
365 <computeroutput>.lfq.</computeroutput>followed by the
366 contents of the environment variable specified by
367 <computeroutput>--log-file-qualifier</computeroutput>.
368 </para>
njn3e986b22004-11-30 10:43:45 +0000369 </listitem>
370 <listitem>
371 <para>It can be huge: <computeroutput>ls -l</computeroutput>
372 generates a file of about 350KB. Browsing a few files and
373 web pages with a Konqueror built with full debugging
374 information generates a file of around 15 MB.</para>
375 </listitem>
376</itemizedlist>
377
sewardj8d9fec52005-11-15 20:56:23 +0000378<para>The <computeroutput>.pid</computeroutput> suffix
de7e109d12005-11-18 22:09:58 +0000379on the output file name serves two purposes. Firstly, it means you
380don't have to rename old log files that you don't want to overwrite.
381Secondly, and more importantly, it allows correct profiling with the
njn3e986b22004-11-30 10:43:45 +0000382<computeroutput>--trace-children=yes</computeroutput> option of
383programs that spawn child processes.</para>
384
385</sect2>
386
387
388
389<sect2 id="cg-manual.cgopts" xreflabel="Cachegrind options">
390<title>Cachegrind options</title>
391
de03e0e7c2005-12-03 23:02:33 +0000392<!-- start of xi:include in the manpage -->
sewardj08e31e22007-05-23 21:58:33 +0000393<para id="cg.opts.para">Using command line options, you can
394manually specify the I1/D1/L2 cache
395configuration to simulate. For each cache, you can specify the
396size, associativity and line size. The size and line size
397are measured in bytes. The three items
de03e0e7c2005-12-03 23:02:33 +0000398must be comma-separated, but with no spaces, eg:
399<literallayout> valgrind --tool=cachegrind --I1=65535,2,64</literallayout>
400
401You can specify one, two or three of the I1/D1/L2 caches. Any level not
402manually specified will be simulated using the configuration found in
403the normal way (via the CPUID instruction for automagic cache
404configuration, or failing that, via defaults).</para>
405
njn3e986b22004-11-30 10:43:45 +0000406<para>Cache-simulation specific options are:</para>
407
de03e0e7c2005-12-03 23:02:33 +0000408<variablelist id="cg.opts.list">
njn3e986b22004-11-30 10:43:45 +0000409
de03e0e7c2005-12-03 23:02:33 +0000410 <varlistentry id="opt.I1" xreflabel="--I1">
411 <term>
412 <option><![CDATA[--I1=<size>,<associativity>,<line size> ]]></option>
413 </term>
414 <listitem>
415 <para>Specify the size, associativity and line size of the level 1
416 instruction cache. </para>
417 </listitem>
418 </varlistentry>
njn3e986b22004-11-30 10:43:45 +0000419
de03e0e7c2005-12-03 23:02:33 +0000420 <varlistentry id="opt.D1" xreflabel="--D1">
421 <term>
422 <option><![CDATA[--D1=<size>,<associativity>,<line size> ]]></option>
423 </term>
424 <listitem>
425 <para>Specify the size, associativity and line size of the level 1
426 data cache.</para>
427 </listitem>
428 </varlistentry>
njn3e986b22004-11-30 10:43:45 +0000429
de03e0e7c2005-12-03 23:02:33 +0000430 <varlistentry id="opt.L2" xreflabel="--L2">
431 <term>
432 <option><![CDATA[--L2=<size>,<associativity>,<line size> ]]></option>
433 </term>
434 <listitem>
435 <para>Specify the size, associativity and line size of the level 2
436 cache.</para>
437 </listitem>
438 </varlistentry>
njn3e986b22004-11-30 10:43:45 +0000439
sewardje1216cb2007-02-07 19:55:30 +0000440 <varlistentry id="opt.cachegrind-out-file" xreflabel="--cachegrind-out-file">
441 <term>
442 <option><![CDATA[--cachegrind-out-file=<basename> ]]></option>
443 </term>
444 <listitem>
sewardj8693e012007-02-08 06:47:19 +0000445 <para>Write the profile data to
446 <computeroutput>basename.pid</computeroutput>
sewardje1216cb2007-02-07 19:55:30 +0000447 rather than to the default output file,
sewardj8693e012007-02-08 06:47:19 +0000448 <computeroutput>cachegrind.out.pid</computeroutput>.
sewardje1216cb2007-02-07 19:55:30 +0000449 </para>
450 </listitem>
451 </varlistentry>
452
sewardj8badbaa2007-05-08 09:20:25 +0000453 <varlistentry id="opt.cache-sim" xreflabel="--cache-sim">
454 <term>
455 <option><![CDATA[--cache-sim=no|yes [yes] ]]></option>
456 </term>
457 <listitem>
458 <para>Enables or disables collection of cache access and miss
459 counts.</para>
460 </listitem>
461 </varlistentry>
462
463 <varlistentry id="opt.branch-sim" xreflabel="--branch-sim">
464 <term>
465 <option><![CDATA[--branch-sim=no|yes [no] ]]></option>
466 </term>
467 <listitem>
468 <para>Enables or disables collection of branch instruction and
469 misprediction counts. By default this is disabled as it
470 slows Cachegrind down by approximately 25%. Note that you
471 cannot specify <computeroutput>--cache-sim=no</computeroutput>
472 and <computeroutput>--branch-sim=no</computeroutput>
473 together, as that would leave Cachegrind with no
474 information to collect.</para>
475 </listitem>
476 </varlistentry>
477
de03e0e7c2005-12-03 23:02:33 +0000478</variablelist>
479<!-- end of xi:include in the manpage -->
njn3e986b22004-11-30 10:43:45 +0000480
481</sect2>
482
483
484
485<sect2 id="cg-manual.annotate" xreflabel="Annotating C/C++ programs">
486<title>Annotating C/C++ programs</title>
487
488<para>Before using <computeroutput>cg_annotate</computeroutput>,
489it is worth widening your window to be at least 120-characters
490wide if possible, as the output lines can be quite long.</para>
491
492<para>To get a function-by-function summary, run
493<computeroutput>cg_annotate --pid</computeroutput> in a directory
de03e0e7c2005-12-03 23:02:33 +0000494containing a <filename>cachegrind.out.pid</filename> file. The
495<emphasis>--pid</emphasis> is required so that
496<computeroutput>cg_annotate</computeroutput> knows which log file to use
497when several are present.</para>
njn3e986b22004-11-30 10:43:45 +0000498
499<para>The output looks like this:</para>
500
501<programlisting><![CDATA[
502--------------------------------------------------------------------------------
503I1 cache: 65536 B, 64 B, 2-way associative
504D1 cache: 65536 B, 64 B, 2-way associative
505L2 cache: 262144 B, 64 B, 8-way associative
506Command: concord vg_to_ucode.c
507Events recorded: Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw
508Events shown: Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw
509Event sort order: Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw
510Threshold: 99%
511Chosen for annotation:
512Auto-annotation: on
513
514--------------------------------------------------------------------------------
515Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw
516--------------------------------------------------------------------------------
51727,742,716 276 275 10,955,517 21,905 3,987 4,474,773 19,280 19,098 PROGRAM TOTALS
518
519--------------------------------------------------------------------------------
520Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw file:function
521--------------------------------------------------------------------------------
5228,821,482 5 5 2,242,702 1,621 73 1,794,230 0 0 getc.c:_IO_getc
5235,222,023 4 4 2,276,334 16 12 875,959 1 1 concord.c:get_word
5242,649,248 2 2 1,344,810 7,326 1,385 . . . vg_main.c:strcmp
5252,521,927 2 2 591,215 0 0 179,398 0 0 concord.c:hash
5262,242,740 2 2 1,046,612 568 22 448,548 0 0 ctype.c:tolower
5271,496,937 4 4 630,874 9,000 1,400 279,388 0 0 concord.c:insert
528 897,991 51 51 897,831 95 30 62 1 1 ???:???
529 598,068 1 1 299,034 0 0 149,517 0 0 ../sysdeps/generic/lockfile.c:__flockfile
530 598,068 0 0 299,034 0 0 149,517 0 0 ../sysdeps/generic/lockfile.c:__funlockfile
531 598,024 4 4 213,580 35 16 149,506 0 0 vg_clientmalloc.c:malloc
532 446,587 1 1 215,973 2,167 430 129,948 14,057 13,957 concord.c:add_existing
533 341,760 2 2 128,160 0 0 128,160 0 0 vg_clientmalloc.c:vg_trap_here_WRAPPER
534 320,782 4 4 150,711 276 0 56,027 53 53 concord.c:init_hash_table
535 298,998 1 1 106,785 0 0 64,071 1 1 concord.c:create
536 149,518 0 0 149,516 0 0 1 0 0 ???:tolower@@GLIBC_2.0
537 149,518 0 0 149,516 0 0 1 0 0 ???:fgetc@@GLIBC_2.0
538 95,983 4 4 38,031 0 0 34,409 3,152 3,150 concord.c:new_word_node
539 85,440 0 0 42,720 0 0 21,360 0 0 vg_clientmalloc.c:vg_bogus_epilogue]]></programlisting>
540
541
542<para>First up is a summary of the annotation options:</para>
543
544<itemizedlist>
545
546 <listitem>
547 <para>I1 cache, D1 cache, L2 cache: cache configuration. So
548 you know the configuration with which these results were
549 obtained.</para>
550 </listitem>
551
552 <listitem>
553 <para>Command: the command line invocation of the program
554 under examination.</para>
555 </listitem>
556
557 <listitem>
558 <para>Events recorded: event abbreviations are:</para>
559 <itemizedlist>
560 <listitem>
sewardj08e31e22007-05-23 21:58:33 +0000561 <para><computeroutput>Ir</computeroutput>: I cache reads
njn3e986b22004-11-30 10:43:45 +0000562 (ie. instructions executed)</para>
563 </listitem>
564 <listitem>
565 <para><computeroutput>I1mr</computeroutput>: I1 cache read
566 misses</para>
567 </listitem>
568 <listitem>
569 <para><computeroutput>I2mr</computeroutput>: L2 cache
570 instruction read misses</para>
571 </listitem>
572 <listitem>
sewardj08e31e22007-05-23 21:58:33 +0000573 <para><computeroutput>Dr</computeroutput>: D cache reads
njn3e986b22004-11-30 10:43:45 +0000574 (ie. memory reads)</para>
575 </listitem>
576 <listitem>
577 <para><computeroutput>D1mr</computeroutput>: D1 cache read
578 misses</para>
579 </listitem>
580 <listitem>
581 <para><computeroutput>D2mr</computeroutput>: L2 cache data
582 read misses</para>
583 </listitem>
584 <listitem>
sewardj08e31e22007-05-23 21:58:33 +0000585 <para><computeroutput>Dw</computeroutput>: D cache writes
njn3e986b22004-11-30 10:43:45 +0000586 (ie. memory writes)</para>
587 </listitem>
588 <listitem>
589 <para><computeroutput>D1mw</computeroutput>: D1 cache write
590 misses</para>
591 </listitem>
592 <listitem>
593 <para><computeroutput>D2mw</computeroutput>: L2 cache data
594 write misses</para>
595 </listitem>
sewardj8badbaa2007-05-08 09:20:25 +0000596 <listitem>
597 <para><computeroutput>Bc</computeroutput>: Conditional branches
598 executed</para>
599 </listitem>
600 <listitem>
601 <para><computeroutput>Bcm</computeroutput>: Conditional branches
602 mispredicted</para>
603 </listitem>
604 <listitem>
605 <para><computeroutput>Bi</computeroutput>: Indirect branches
606 executed</para>
607 </listitem>
608 <listitem>
609 <para><computeroutput>Bim</computeroutput>: Conditional branches
610 mispredicted</para>
611 </listitem>
njn3e986b22004-11-30 10:43:45 +0000612 </itemizedlist>
613
614 <para>Note that D1 total accesses is given by
615 <computeroutput>D1mr</computeroutput> +
616 <computeroutput>D1mw</computeroutput>, and that L2 total
617 accesses is given by <computeroutput>I2mr</computeroutput> +
618 <computeroutput>D2mr</computeroutput> +
619 <computeroutput>D2mw</computeroutput>.</para>
620 </listitem>
621
622 <listitem>
sewardj08e31e22007-05-23 21:58:33 +0000623 <para>Events shown: the events shown, which is a subset of the events
624 gathered. This can be adjusted with the
njn3e986b22004-11-30 10:43:45 +0000625 <computeroutput>--show</computeroutput> option.</para>
626 </listitem>
627
628 <listitem>
629 <para>Event sort order: the sort order in which functions are
630 shown. For example, in this case the functions are sorted
631 from highest <computeroutput>Ir</computeroutput> counts to
632 lowest. If two functions have identical
633 <computeroutput>Ir</computeroutput> counts, they will then be
634 sorted by <computeroutput>I1mr</computeroutput> counts, and
635 so on. This order can be adjusted with the
636 <computeroutput>--sort</computeroutput> option.</para>
637
638 <para>Note that this dictates the order the functions appear.
639 It is <command>not</command> the order in which the columns
640 appear; that is dictated by the "events shown" line (and can
641 be changed with the <computeroutput>--show</computeroutput>
642 option).</para>
643 </listitem>
644
645 <listitem>
646 <para>Threshold: <computeroutput>cg_annotate</computeroutput>
sewardj08e31e22007-05-23 21:58:33 +0000647 by default omits functions that cause very low counts
648 to avoid drowning you in information. In this case,
njn3e986b22004-11-30 10:43:45 +0000649 cg_annotate shows summaries the functions that account for
650 99% of the <computeroutput>Ir</computeroutput> counts;
651 <computeroutput>Ir</computeroutput> is chosen as the
652 threshold event since it is the primary sort event. The
653 threshold can be adjusted with the
654 <computeroutput>--threshold</computeroutput>
655 option.</para>
656 </listitem>
657
658 <listitem>
659 <para>Chosen for annotation: names of files specified
660 manually for annotation; in this case none.</para>
661 </listitem>
662
663 <listitem>
664 <para>Auto-annotation: whether auto-annotation was requested
665 via the <computeroutput>--auto=yes</computeroutput>
666 option. In this case no.</para>
667 </listitem>
668
669</itemizedlist>
670
671<para>Then follows summary statistics for the whole
672program. These are similar to the summary provided when running
de03e0e7c2005-12-03 23:02:33 +0000673<computeroutput>valgrind --tool=cachegrind</computeroutput>.</para>
njn3e986b22004-11-30 10:43:45 +0000674
675<para>Then follows function-by-function statistics. Each function
676is identified by a
677<computeroutput>file_name:function_name</computeroutput> pair. If
678a column contains only a dot it means the function never performs
679that event (eg. the third row shows that
680<computeroutput>strcmp()</computeroutput> contains no
681instructions that write to memory). The name
682<computeroutput>???</computeroutput> is used if the the file name
683and/or function name could not be determined from debugging
684information. If most of the entries have the form
685<computeroutput>???:???</computeroutput> the program probably
686wasn't compiled with <computeroutput>-g</computeroutput>. If any
687code was invalidated (either due to self-modifying code or
688unloading of shared objects) its counts are aggregated into a
689single cost centre written as
690<computeroutput>(discarded):(discarded)</computeroutput>.</para>
691
sewardj08e31e22007-05-23 21:58:33 +0000692<para>It is worth noting that functions will come both from
693the profiled program (eg. <filename>concord.c</filename>)
694and from libraries (eg. <filename>getc.c</filename>)</para>
njn3e986b22004-11-30 10:43:45 +0000695
696<para>There are two ways to annotate source files -- by choosing
697them manually, or with the
698<computeroutput>--auto=yes</computeroutput> option. To do it
699manually, just specify the filenames as arguments to
700<computeroutput>cg_annotate</computeroutput>. For example, the
701output from running <filename>cg_annotate concord.c</filename>
702for our example produces the same output as above followed by an
703annotated version of <filename>concord.c</filename>, a section of
704which looks like:</para>
705
706<programlisting><![CDATA[
707--------------------------------------------------------------------------------
708-- User-annotated source: concord.c
709--------------------------------------------------------------------------------
710Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw
711
712[snip]
713
714 . . . . . . . . . void init_hash_table(char *file_name, Word_Node *table[])
715 3 1 1 . . . 1 0 0 {
716 . . . . . . . . . FILE *file_ptr;
717 . . . . . . . . . Word_Info *data;
718 1 0 0 . . . 1 1 1 int line = 1, i;
719 . . . . . . . . .
720 5 0 0 . . . 3 0 0 data = (Word_Info *) create(sizeof(Word_Info));
721 . . . . . . . . .
722 4,991 0 0 1,995 0 0 998 0 0 for (i = 0; i < TABLE_SIZE; i++)
723 3,988 1 1 1,994 0 0 997 53 52 table[i] = NULL;
724 . . . . . . . . .
725 . . . . . . . . . /* Open file, check it. */
726 6 0 0 1 0 0 4 0 0 file_ptr = fopen(file_name, "r");
727 2 0 0 1 0 0 . . . if (!(file_ptr)) {
728 . . . . . . . . . fprintf(stderr, "Couldn't open '%s'.\n", file_name);
729 1 1 1 . . . . . . exit(EXIT_FAILURE);
730 . . . . . . . . . }
731 . . . . . . . . .
732 165,062 1 1 73,360 0 0 91,700 0 0 while ((line = get_word(data, line, file_ptr)) != EOF)
733 146,712 0 0 73,356 0 0 73,356 0 0 insert(data->;word, data->line, table);
734 . . . . . . . . .
735 4 0 0 1 0 0 2 0 0 free(data);
736 4 0 0 1 0 0 2 0 0 fclose(file_ptr);
737 3 0 0 2 0 0 . . . }]]></programlisting>
738
739<para>(Although column widths are automatically minimised, a wide
740terminal is clearly useful.)</para>
741
742<para>Each source file is clearly marked
743(<computeroutput>User-annotated source</computeroutput>) as
744having been chosen manually for annotation. If the file was
745found in one of the directories specified with the
746<computeroutput>-I / --include</computeroutput> option, the directory
747and file are both given.</para>
748
749<para>Each line is annotated with its event counts. Events not
sewardj08e31e22007-05-23 21:58:33 +0000750applicable for a line are represented by a dot. This is useful
njn3e986b22004-11-30 10:43:45 +0000751for distinguishing between an event which cannot happen, and one
752which can but did not.</para>
753
754<para>Sometimes only a small section of a source file is
sewardj8d9fec52005-11-15 20:56:23 +0000755executed. To minimise uninteresting output, Cachegrind only shows
njn3e986b22004-11-30 10:43:45 +0000756annotated lines and lines within a small distance of annotated
757lines. Gaps are marked with the line numbers so you know which
758part of a file the shown code comes from, eg:</para>
759
760<programlisting><![CDATA[
761(figures and code for line 704)
762-- line 704 ----------------------------------------
763-- line 878 ----------------------------------------
764(figures and code for line 878)]]></programlisting>
765
766<para>The amount of context to show around annotated lines is
767controlled by the <computeroutput>--context</computeroutput>
768option.</para>
769
770<para>To get automatic annotation, run
771<computeroutput>cg_annotate --auto=yes</computeroutput>.
772cg_annotate will automatically annotate every source file it can
773find that is mentioned in the function-by-function summary.
774Therefore, the files chosen for auto-annotation are affected by
775the <computeroutput>--sort</computeroutput> and
776<computeroutput>--threshold</computeroutput> options. Each
777source file is clearly marked (<computeroutput>Auto-annotated
778source</computeroutput>) as being chosen automatically. Any
779files that could not be found are mentioned at the end of the
780output, eg:</para>
781
782<programlisting><![CDATA[
783------------------------------------------------------------------
784The following files chosen for auto-annotation could not be found:
785------------------------------------------------------------------
786 getc.c
787 ctype.c
788 ../sysdeps/generic/lockfile.c]]></programlisting>
789
790<para>This is quite common for library files, since libraries are
791usually compiled with debugging information, but the source files
792are often not present on a system. If a file is chosen for
793annotation <command>both</command> manually and automatically, it
794is marked as <computeroutput>User-annotated
795source</computeroutput>. Use the <computeroutput>-I /
796--include</computeroutput> option to tell Valgrind where to look
797for source files if the filenames found from the debugging
798information aren't specific enough.</para>
799
800<para>Beware that cg_annotate can take some time to digest large
801<computeroutput>cachegrind.out.pid</computeroutput> files,
802e.g. 30 seconds or more. Also beware that auto-annotation can
803produce a lot of output if your program is large!</para>
804
805</sect2>
806
807
808<sect2 id="cg-manual.assembler" xreflabel="Annotating assembler programs">
809<title>Annotating assembler programs</title>
810
811<para>Valgrind can annotate assembler programs too, or annotate
812the assembler generated for your C program. Sometimes this is
813useful for understanding what is really happening when an
814interesting line of C code is translated into multiple
815instructions.</para>
816
817<para>To do this, you just need to assemble your
818<computeroutput>.s</computeroutput> files with assembler-level
819debug information. gcc doesn't do this, but you can use the GNU
820assembler with the <computeroutput>--gstabs</computeroutput>
821option to generate object files with this information, eg:</para>
822
823<programlisting><![CDATA[
824as --gstabs foo.s]]></programlisting>
825
826<para>You can then profile and annotate source files in the same
827way as for C/C++ programs.</para>
828
829</sect2>
830
831</sect1>
832
833
834<sect1 id="cg-manual.annopts" xreflabel="cg_annotate options">
835<title><computeroutput>cg_annotate</computeroutput> options</title>
836
837<itemizedlist>
838
debc32e822005-06-25 14:43:05 +0000839 <listitem id="pid">
njn3e986b22004-11-30 10:43:45 +0000840 <para><computeroutput>--pid</computeroutput></para>
sewardj8693e012007-02-08 06:47:19 +0000841 <para>Indicates that profile data should be read from
842 the file
843 <computeroutput>cachegrind.out.pid</computeroutput>.
844 read.
845 Note that you must specify either
846 <computeroutput>--pid</computeroutput>
847 or <computeroutput>--cachegrind-out-file=filename</computeroutput>
848 exactly once.
849 </para>
850 </listitem>
851
852 <listitem id="cachegrind-out-file">
853 <para><computeroutput>--cachegrind-out-file=filename</computeroutput></para>
854 <para>Indicates that profile data
855 should be read from <computeroutput>filename</computeroutput>.
856 Note that you must specify either
857 <computeroutput>--pid</computeroutput>
858 or <computeroutput>--cachegrind-out-file=filename</computeroutput>
859 exactly once.
860 </para>
njn3e986b22004-11-30 10:43:45 +0000861 </listitem>
862
863 <listitem>
864 <para><computeroutput>-h, --help</computeroutput></para>
865 <para><computeroutput>-v, --version</computeroutput></para>
866 <para>Help and version, as usual.</para>
867 </listitem>
868
debc32e822005-06-25 14:43:05 +0000869 <listitem id="sort">
njn3e986b22004-11-30 10:43:45 +0000870 <para><computeroutput>--sort=A,B,C</computeroutput> [default:
871 order in
872 <computeroutput>cachegrind.out.pid</computeroutput>]</para>
873 <para>Specifies the events upon which the sorting of the
874 function-by-function entries will be based. Useful if you
875 want to concentrate on eg. I cache misses
876 (<computeroutput>--sort=I1mr,I2mr</computeroutput>), or D
877 cache misses
878 (<computeroutput>--sort=D1mr,D2mr</computeroutput>), or L2
879 misses
880 (<computeroutput>--sort=D2mr,I2mr</computeroutput>).</para>
881 </listitem>
882
debc32e822005-06-25 14:43:05 +0000883 <listitem id="show">
njn3e986b22004-11-30 10:43:45 +0000884 <para><computeroutput>--show=A,B,C</computeroutput> [default:
885 all, using order in
886 <computeroutput>cachegrind.out.pid</computeroutput>]</para>
887 <para>Specifies which events to show (and the column
888 order). Default is to use all present in the
889 <computeroutput>cachegrind.out.pid</computeroutput> file (and
890 use the order in the file).</para>
891 </listitem>
892
debc32e822005-06-25 14:43:05 +0000893 <listitem id="threshold">
njn3e986b22004-11-30 10:43:45 +0000894 <para><computeroutput>--threshold=X</computeroutput>
895 [default: 99%]</para>
896 <para>Sets the threshold for the function-by-function
897 summary. Functions are shown that account for more than X%
898 of the primary sort event. If auto-annotating, also affects
899 which files are annotated.</para>
900
901 <para>Note: thresholds can be set for more than one of the
902 events by appending any events for the
903 <computeroutput>--sort</computeroutput> option with a colon
904 and a number (no spaces, though). E.g. if you want to see
905 the functions that cover 99% of L2 read misses and 99% of L2
906 write misses, use this option:</para>
907 <para><computeroutput>--sort=D2mr:99,D2mw:99</computeroutput></para>
908 </listitem>
909
debc32e822005-06-25 14:43:05 +0000910 <listitem id="auto">
njn3e986b22004-11-30 10:43:45 +0000911 <para><computeroutput>--auto=no</computeroutput> [default]</para>
912 <para><computeroutput>--auto=yes</computeroutput></para>
913 <para>When enabled, automatically annotates every file that
914 is mentioned in the function-by-function summary that can be
915 found. Also gives a list of those that couldn't be found.</para>
916 </listitem>
917
debc32e822005-06-25 14:43:05 +0000918 <listitem id="context">
njn3e986b22004-11-30 10:43:45 +0000919 <para><computeroutput>--context=N</computeroutput> [default:
920 8]</para>
921 <para>Print N lines of context before and after each
922 annotated line. Avoids printing large sections of source
923 files that were not executed. Use a large number
924 (eg. 10,000) to show all source lines.</para>
925 </listitem>
926
debc32e822005-06-25 14:43:05 +0000927 <listitem id="include">
sewardj8d9fec52005-11-15 20:56:23 +0000928 <para><computeroutput>-I&lt;dir&gt;,
njn3e986b22004-11-30 10:43:45 +0000929 --include=&lt;dir&gt;</computeroutput> [default: empty
930 string]</para>
931 <para>Adds a directory to the list in which to search for
932 files. Multiple -I/--include options can be given to add
933 multiple directories.</para>
934 </listitem>
935
936</itemizedlist>
937
938
939
940<sect2>
941<title>Warnings</title>
942
943<para>There are a couple of situations in which
944<computeroutput>cg_annotate</computeroutput> issues
945warnings.</para>
946
947<itemizedlist>
948 <listitem>
949 <para>If a source file is more recent than the
950 <computeroutput>cachegrind.out.pid</computeroutput> file.
951 This is because the information in
952 <computeroutput>cachegrind.out.pid</computeroutput> is only
953 recorded with line numbers, so if the line numbers change at
954 all in the source (eg. lines added, deleted, swapped), any
955 annotations will be incorrect.</para>
956 </listitem>
957 <listitem>
958 <para>If information is recorded about line numbers past the
959 end of a file. This can be caused by the above problem,
960 ie. shortening the source file while using an old
961 <computeroutput>cachegrind.out.pid</computeroutput> file. If
962 this happens, the figures for the bogus lines are printed
963 anyway (clearly marked as bogus) in case they are
964 important.</para>
965 </listitem>
966</itemizedlist>
967
968</sect2>
969
970
971
972<sect2>
973<title>Things to watch out for</title>
974
975<para>Some odd things that can occur during annotation:</para>
976
977<itemizedlist>
978 <listitem>
979 <para>If annotating at the assembler level, you might see
980 something like this:</para>
981<programlisting><![CDATA[
982 1 0 0 . . . . . . leal -12(%ebp),%eax
983 1 0 0 . . . 1 0 0 movl %eax,84(%ebx)
984 2 0 0 0 0 0 1 0 0 movl $1,-20(%ebp)
985 . . . . . . . . . .align 4,0x90
986 1 0 0 . . . . . . movl $.LnrB,%eax
987 1 0 0 . . . 1 0 0 movl %eax,-16(%ebp)]]></programlisting>
988
989 <para>How can the third instruction be executed twice when
990 the others are executed only once? As it turns out, it
991 isn't. Here's a dump of the executable, using
992 <computeroutput>objdump -d</computeroutput>:</para>
993<programlisting><![CDATA[
994 8048f25: 8d 45 f4 lea 0xfffffff4(%ebp),%eax
995 8048f28: 89 43 54 mov %eax,0x54(%ebx)
996 8048f2b: c7 45 ec 01 00 00 00 movl $0x1,0xffffffec(%ebp)
997 8048f32: 89 f6 mov %esi,%esi
998 8048f34: b8 08 8b 07 08 mov $0x8078b08,%eax
999 8048f39: 89 45 f0 mov %eax,0xfffffff0(%ebp)]]></programlisting>
1000
1001 <para>Notice the extra <computeroutput>mov
1002 %esi,%esi</computeroutput> instruction. Where did this come
1003 from? The GNU assembler inserted it to serve as the two
1004 bytes of padding needed to align the <computeroutput>movl
1005 $.LnrB,%eax</computeroutput> instruction on a four-byte
1006 boundary, but pretended it didn't exist when adding debug
1007 information. Thus when Valgrind reads the debug info it
1008 thinks that the <computeroutput>movl
1009 $0x1,0xffffffec(%ebp)</computeroutput> instruction covers the
1010 address range 0x8048f2b--0x804833 by itself, and attributes
1011 the counts for the <computeroutput>mov
1012 %esi,%esi</computeroutput> to it.</para>
1013 </listitem>
1014
1015 <listitem>
1016 <para>Inlined functions can cause strange results in the
1017 function-by-function summary. If a function
1018 <computeroutput>inline_me()</computeroutput> is defined in
1019 <filename>foo.h</filename> and inlined in the functions
1020 <computeroutput>f1()</computeroutput>,
1021 <computeroutput>f2()</computeroutput> and
1022 <computeroutput>f3()</computeroutput> in
1023 <filename>bar.c</filename>, there will not be a
1024 <computeroutput>foo.h:inline_me()</computeroutput> function
1025 entry. Instead, there will be separate function entries for
1026 each inlining site, ie.
1027 <computeroutput>foo.h:f1()</computeroutput>,
1028 <computeroutput>foo.h:f2()</computeroutput> and
1029 <computeroutput>foo.h:f3()</computeroutput>. To find the
1030 total counts for
1031 <computeroutput>foo.h:inline_me()</computeroutput>, add up
1032 the counts from each entry.</para>
1033
1034 <para>The reason for this is that although the debug info
1035 output by gcc indicates the switch from
1036 <filename>bar.c</filename> to <filename>foo.h</filename>, it
1037 doesn't indicate the name of the function in
1038 <filename>foo.h</filename>, so Valgrind keeps using the old
1039 one.</para>
1040 </listitem>
1041
1042 <listitem>
1043 <para>Sometimes, the same filename might be represented with
1044 a relative name and with an absolute name in different parts
1045 of the debug info, eg:
1046 <filename>/home/user/proj/proj.h</filename> and
1047 <filename>../proj.h</filename>. In this case, if you use
1048 auto-annotation, the file will be annotated twice with the
1049 counts split between the two.</para>
1050 </listitem>
1051
1052 <listitem>
1053 <para>Files with more than 65,535 lines cause difficulties
sewardj08e31e22007-05-23 21:58:33 +00001054 for the Stabs-format debug info reader. This is because the line
njn3e986b22004-11-30 10:43:45 +00001055 number in the <computeroutput>struct nlist</computeroutput>
1056 defined in <filename>a.out.h</filename> under Linux is only a
1057 16-bit value. Valgrind can handle some files with more than
1058 65,535 lines correctly by making some guesses to identify
1059 line number overflows. But some cases are beyond it, in
1060 which case you'll get a warning message explaining that
1061 annotations for the file might be incorrect.</para>
sewardj08e31e22007-05-23 21:58:33 +00001062
1063 <para>If you are using gcc 3.1 or later, this is most likely
1064 irrelevant, since gcc switched to using the more modern DWARF2
1065 format by default at version 3.1. DWARF2 does not have any such
1066 limitations on line numbers.</para>
njn3e986b22004-11-30 10:43:45 +00001067 </listitem>
1068
1069 <listitem>
1070 <para>If you compile some files with
1071 <computeroutput>-g</computeroutput> and some without, some
1072 events that take place in a file without debug info could be
1073 attributed to the last line of a file with debug info
1074 (whichever one gets placed before the non-debug-info file in
1075 the executable).</para>
1076 </listitem>
1077
1078</itemizedlist>
1079
1080<para>This list looks long, but these cases should be fairly
1081rare.</para>
1082
njn3e986b22004-11-30 10:43:45 +00001083</sect2>
1084
1085
1086
1087<sect2>
1088<title>Accuracy</title>
1089
1090<para>Valgrind's cache profiling has a number of
1091shortcomings:</para>
1092
1093<itemizedlist>
1094 <listitem>
1095 <para>It doesn't account for kernel activity -- the effect of
1096 system calls on the cache contents is ignored.</para>
1097 </listitem>
1098
1099 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001100 <para>It doesn't account for other process activity.
1101 This is probably desirable when considering a single
1102 program.</para>
njn3e986b22004-11-30 10:43:45 +00001103 </listitem>
1104
1105 <listitem>
1106 <para>It doesn't account for virtual-to-physical address
sewardj08e31e22007-05-23 21:58:33 +00001107 mappings. Hence the simulation is not a true
njn3e986b22004-11-30 10:43:45 +00001108 representation of what's happening in the
sewardj08e31e22007-05-23 21:58:33 +00001109 cache. Most caches are physically indexed, but Cachegrind
1110 simulates caches using virtual addresses.</para>
njn3e986b22004-11-30 10:43:45 +00001111 </listitem>
1112
1113 <listitem>
1114 <para>It doesn't account for cache misses not visible at the
1115 instruction level, eg. those arising from TLB misses, or
1116 speculative execution.</para>
1117 </listitem>
1118
1119 <listitem>
sewardj8d9fec52005-11-15 20:56:23 +00001120 <para>Valgrind will schedule
1121 threads differently from how they would be when running natively.
1122 This could warp the results for threaded programs.</para>
njn3e986b22004-11-30 10:43:45 +00001123 </listitem>
1124
1125 <listitem>
sewardj8d9fec52005-11-15 20:56:23 +00001126 <para>The x86/amd64 instructions <computeroutput>bts</computeroutput>,
njn3e986b22004-11-30 10:43:45 +00001127 <computeroutput>btr</computeroutput> and
1128 <computeroutput>btc</computeroutput> will incorrectly be
1129 counted as doing a data read if both the arguments are
1130 registers, eg:</para>
1131<programlisting><![CDATA[
1132 btsl %eax, %edx]]></programlisting>
1133
1134 <para>This should only happen rarely.</para>
1135 </listitem>
1136
1137 <listitem>
sewardj8d9fec52005-11-15 20:56:23 +00001138 <para>x86/amd64 FPU instructions with data sizes of 28 and 108 bytes
njn3e986b22004-11-30 10:43:45 +00001139 (e.g. <computeroutput>fsave</computeroutput>) are treated as
1140 though they only access 16 bytes. These instructions seem to
1141 be rare so hopefully this won't affect accuracy much.</para>
1142 </listitem>
1143
1144</itemizedlist>
1145
sewardj08e31e22007-05-23 21:58:33 +00001146<para>Another thing worth noting is that results are very sensitive.
1147Changing the size of the the executable being profiled, or the sizes
1148of any of the shared libraries it uses, or even the length of their
1149file names, can perturb the results. Variations will be small, but
1150don't expect perfectly repeatable results if your program changes at
1151all.</para>
njn3e986b22004-11-30 10:43:45 +00001152
sewardj08e31e22007-05-23 21:58:33 +00001153<para>More recent GNU/Linux distributions do address space
1154randomisation, in which identical runs of the same program have their
1155shared libraries loaded at different locations, as a security measure.
1156This also perturbs the results.</para>
sewardj94dc5082007-02-08 11:31:03 +00001157
njn3e986b22004-11-30 10:43:45 +00001158<para>While these factors mean you shouldn't trust the results to
1159be super-accurate, hopefully they should be close enough to be
1160useful.</para>
1161
1162</sect2>
1163
njn534f7812006-10-21 22:22:59 +00001164</sect1>
1165
sewardj94dc5082007-02-08 11:31:03 +00001166
1167
1168<sect1 id="cg-manual.cg_merge" xreflabel="cg_merge">
1169<title>Merging profiles with <computeroutput>cg_merge</computeroutput></title>
1170
1171<para>
1172<computeroutput>cg_merge</computeroutput> is a simple program which
1173reads multiple profile files, as created by cachegrind, merges them
1174together, and writes the results into another file in the same format.
1175You can then examine the merged results using
1176<computeroutput>cg_annotate
1177--cachegrind-out-file=outputfile</computeroutput>, as
1178described above. The merging functionality might be useful if you
1179want to aggregate costs over multiple runs of the same program, or
1180from a single parallel run with multiple instances of the same
1181program.</para>
1182
1183<para>
1184<computeroutput>cg_merge</computeroutput> is invoked as follows:
1185</para>
1186
1187<programlisting><![CDATA[
1188cg_merge -o outputfile file1 file2 file3 ...]]></programlisting>
1189
1190<para>
1191It reads and checks <computeroutput>file1</computeroutput>, then read
1192and checks <computeroutput>file2</computeroutput> and merges it into
1193the running totals, then the same with
1194<computeroutput>file3</computeroutput>, etc. The final results are
1195written to <computeroutput>outputfile</computeroutput>, or to standard
1196out if no output file is specified.</para>
1197
1198<para>
1199Costs are summed on a per-function, per-line and per-instruction
1200basis. Because of this, the order in which the input files does not
1201matter, although you should take care to only mention each file once,
1202since any file mentioned twice will be added in twice.</para>
1203
1204<para>
1205<computeroutput>cg_merge</computeroutput> does not attempt to check
1206that the input files come from runs of the same executable. It will
1207happily merge together profile files from completely unrelated
1208programs. It does however check that the
1209<computeroutput>Events:</computeroutput> lines of all the inputs are
1210identical, so as to ensure that the addition of costs makes sense.
1211For example, it would be nonsensical for it to add a number indicating
1212D1 read references to a number from a different file indicating L2
1213write misses.</para>
1214
1215<para>
1216A number of other syntax and sanity checks are done whilst reading the
1217inputs. <computeroutput>cg_merge</computeroutput> will stop and
1218attempt to print a helpful error message if any of the input files
1219fail these checks.</para>
1220
1221</sect1>
1222
1223
njn3a9d5dc2007-09-17 22:19:01 +00001224<sect1>
1225<title>Acting on Cachegrind's information</title>
1226<para>
1227So, you've managed to profile your program with Cachegrind. Now what?
1228What's the best way to actually act on the information it provides to speed
njn07f96562007-09-17 22:28:21 +00001229up your program? Here are some rules of thumb that we have found to be
1230useful.</para>
njn3a9d5dc2007-09-17 22:19:01 +00001231
1232<para>
1233First of all, the global hit/miss rate numbers are not that useful. If you
1234have multiple programs or multiple runs of a program, comparing the numbers
njn07f96562007-09-17 22:28:21 +00001235might identify if any are outliers and worthy of closer investigation.
1236Otherwise, they're not enough to act on.</para>
njn3a9d5dc2007-09-17 22:19:01 +00001237
1238<para>
njn07f96562007-09-17 22:28:21 +00001239The line-by-line source code annotations are much more useful. In our
1240experience, the best place to start is by looking at the
1241<computeroutput>Ir</computeroutput> numbers. They simply measure how many
1242instructions were executed for each line, and don't include any cache
1243information, but they can still be very useful for identifying
1244bottlenecks.</para>
njn3a9d5dc2007-09-17 22:19:01 +00001245
1246<para>
1247After that, we have found that L2 misses are typically a much bigger source
1248of slow-downs than L1 misses. So it's worth looking for any snippets of
njn07f96562007-09-17 22:28:21 +00001249code that cause a high proportion of the L2 misses. If you find any, it's
1250still not always easy to work out how to improve things. You need to have a
1251reasonable understanding of how caches work, the principles of locality, and
1252your program's data access patterns. Improving things may require
1253redesigning a data structure, for example.</para>
njn3a9d5dc2007-09-17 22:19:01 +00001254
1255<para>
1256In short, Cachegrind can tell you where some of the bottlenecks in your code
1257are, but it can't tell you how to fix them. You have to work that out for
1258yourself. But at least you have the information!
1259</para>
1260
1261</sect1>
sewardj94dc5082007-02-08 11:31:03 +00001262
njn534f7812006-10-21 22:22:59 +00001263<sect1>
1264<title>Implementation details</title>
njn3a9d5dc2007-09-17 22:19:01 +00001265<para>
njn534f7812006-10-21 22:22:59 +00001266This section talks about details you don't need to know about in order to
1267use Cachegrind, but may be of interest to some people.
njn3a9d5dc2007-09-17 22:19:01 +00001268</para>
njn3e986b22004-11-30 10:43:45 +00001269
1270<sect2>
njn534f7812006-10-21 22:22:59 +00001271<title>How Cachegrind works</title>
1272<para>The best reference for understanding how Cachegrind works is chapter 3 of
1273"Dynamic Binary Analysis and Instrumentation", by Nicholas Nethercote. It
njn011215f2006-10-21 23:00:59 +00001274is available on the <ulink url="&vg-pubs;">Valgrind publications
1275page</ulink>.</para>
njn534f7812006-10-21 22:22:59 +00001276</sect2>
njn3e986b22004-11-30 10:43:45 +00001277
njn534f7812006-10-21 22:22:59 +00001278<sect2>
1279<title>Cachegrind output file format</title>
1280<para>The file format is fairly straightforward, basically giving the
1281cost centre for every line, grouped by files and
1282functions. Total counts (eg. total cache accesses, total L1
1283misses) are calculated when traversing this structure rather than
1284during execution, to save time; the cache simulation functions
1285are called so often that even one or two extra adds can make a
1286sizeable difference.</para>
1287
1288<para>The file format:</para>
1289<programlisting><![CDATA[
1290file ::= desc_line* cmd_line events_line data_line+ summary_line
1291desc_line ::= "desc:" ws? non_nl_string
1292cmd_line ::= "cmd:" ws? cmd
1293events_line ::= "events:" ws? (event ws)+
1294data_line ::= file_line | fn_line | count_line
1295file_line ::= "fl=" filename
1296fn_line ::= "fn=" fn_name
1297count_line ::= line_num ws? (count ws)+
1298summary_line ::= "summary:" ws? (count ws)+
1299count ::= num | "."]]></programlisting>
1300
1301<para>Where:</para>
njn3e986b22004-11-30 10:43:45 +00001302<itemizedlist>
1303 <listitem>
njn534f7812006-10-21 22:22:59 +00001304 <para><computeroutput>non_nl_string</computeroutput> is any
1305 string not containing a newline.</para>
njn3e986b22004-11-30 10:43:45 +00001306 </listitem>
njn534f7812006-10-21 22:22:59 +00001307 <listitem>
1308 <para><computeroutput>cmd</computeroutput> is a string holding the
1309 command line of the profiled program.</para>
1310 </listitem>
1311 <listitem>
njn26242122007-01-22 03:21:27 +00001312 <para><computeroutput>event</computeroutput> is a string containing
1313 no whitespace.</para>
1314 </listitem>
1315 <listitem>
njn534f7812006-10-21 22:22:59 +00001316 <para><computeroutput>filename</computeroutput> and
1317 <computeroutput>fn_name</computeroutput> are strings.</para>
1318 </listitem>
1319 <listitem>
1320 <para><computeroutput>num</computeroutput> and
1321 <computeroutput>line_num</computeroutput> are decimal
1322 numbers.</para>
1323 </listitem>
1324 <listitem>
1325 <para><computeroutput>ws</computeroutput> is whitespace.</para>
1326 </listitem>
1327</itemizedlist>
1328
1329<para>The contents of the "desc:" lines are printed out at the top
1330of the summary. This is a generic way of providing simulation
1331specific information, eg. for giving the cache configuration for
1332cache simulation.</para>
1333
1334<para>More than one line of info can be presented for each file/fn/line number.
1335In such cases, the counts for the named events will be accumulated.</para>
1336
njn3a9d5dc2007-09-17 22:19:01 +00001337<para>Counts can be "." to represent zero. This makes the files easier for
1338humans to read.</para>
njn534f7812006-10-21 22:22:59 +00001339
1340<para>The number of counts in each
1341<computeroutput>line</computeroutput> and the
1342<computeroutput>summary_line</computeroutput> should not exceed
1343the number of events in the
1344<computeroutput>event_line</computeroutput>. If the number in
1345each <computeroutput>line</computeroutput> is less, cg_annotate
njn3a9d5dc2007-09-17 22:19:01 +00001346treats those missing as though they were a "." entry. This saves space.
1347</para>
njn534f7812006-10-21 22:22:59 +00001348
1349<para>A <computeroutput>file_line</computeroutput> changes the
1350current file name. A <computeroutput>fn_line</computeroutput>
1351changes the current function name. A
1352<computeroutput>count_line</computeroutput> contains counts that
1353pertain to the current filename/fn_name. A "fn="
1354<computeroutput>file_line</computeroutput> and a
1355<computeroutput>fn_line</computeroutput> must appear before any
1356<computeroutput>count_line</computeroutput>s to give the context
1357of the first <computeroutput>count_line</computeroutput>s.</para>
1358
1359<para>Each <computeroutput>file_line</computeroutput> will normally be
1360immediately followed by a <computeroutput>fn_line</computeroutput>. But it
1361doesn't have to be.</para>
1362
njn3e986b22004-11-30 10:43:45 +00001363
1364</sect2>
1365
1366</sect1>
1367</chapter>