blob: cfc1c8339c0e0f6000e18d0bb9143a4497458afc [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">
8<title>Cachegrind: a cache profiler</title>
9
njn3e986b22004-11-30 10:43:45 +000010<sect1 id="cg-manual.cache" xreflabel="Cache profiling">
11<title>Cache profiling</title>
12
13<para>To use this tool, you must specify
14<computeroutput>--tool=cachegrind</computeroutput> on the
15Valgrind command line.</para>
16
17<para>Cachegrind is a tool for doing cache simulations and
18annotating your source line-by-line with the number of cache
19misses. In particular, it records:</para>
20<itemizedlist>
21 <listitem>
22 <para>L1 instruction cache reads and misses;</para>
23 </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>
32</itemizedlist>
33
njnc8cccb12005-07-25 23:30:24 +000034<para>On a modern machine, an L1 miss will typically cost
njn3e986b22004-11-30 10:43:45 +000035around 10 cycles, and an L2 miss can cost as much as 200
36cycles. Detailed cache profiling can be very useful for improving
37the performance of your program.</para>
38
39<para>Also, since one instruction cache read is performed per
40instruction executed, you can find out how many instructions are
41executed per line, which can be useful for traditional profiling
42and test coverage.</para>
43
44<para>Any feedback, bug-fixes, suggestions, etc, welcome.</para>
45
46
47
48<sect2 id="cg-manual.overview" xreflabel="Overview">
49<title>Overview</title>
50
51<para>First off, as for normal Valgrind use, you probably want to
52compile with debugging info (the
53<computeroutput>-g</computeroutput> flag). But by contrast with
54normal Valgrind use, you probably <command>do</command> want to turn
55optimisation on, since you should profile your program as it will
56be normally run.</para>
57
58<para>The two steps are:</para>
59<orderedlist>
60 <listitem>
61 <para>Run your program with <computeroutput>valgrind
62 --tool=cachegrind</computeroutput> in front of the normal
63 command line invocation. When the program finishes,
64 Cachegrind will print summary cache statistics. It also
65 collects line-by-line information in a file
66 <computeroutput>cachegrind.out.pid</computeroutput>, where
67 <computeroutput>pid</computeroutput> is the program's process
68 id.</para>
69
70 <para>This step should be done every time you want to collect
71 information about a new program, a changed program, or about
72 the same program with different input.</para>
73 </listitem>
74
75 <listitem>
76 <para>Generate a function-by-function summary, and possibly
77 annotate source files, using the supplied
78 <computeroutput>cg_annotate</computeroutput> program. Source
79 files to annotate can be specified manually, or manually on
80 the command line, or "interesting" source files can be
81 annotated automatically with the
82 <computeroutput>--auto=yes</computeroutput> option. You can
83 annotate C/C++ files or assembly language files equally
84 easily.</para>
85
86 <para>This step can be performed as many times as you like
87 for each Step 2. You may want to do multiple annotations
88 showing different information each time.</para>
89 </listitem>
90
91</orderedlist>
92
93<para>The steps are described in detail in the following
94sections.</para>
95
96</sect2>
97
98
debc32e822005-06-25 14:43:05 +000099<sect2 id="cache-sim" xreflabel="Cache simulation specifics">
njn3e986b22004-11-30 10:43:45 +0000100<title>Cache simulation specifics</title>
101
102<para>Cachegrind uses a simulation for a machine with a split L1
103cache and a unified L2 cache. This configuration is used for all
104(modern) x86-based machines we are aware of. Old Cyrix CPUs had
105a unified I and D L1 cache, but they are ancient history
106now.</para>
107
108<para>The more specific characteristics of the simulation are as
109follows.</para>
110
111<itemizedlist>
112
113 <listitem>
114 <para>Write-allocate: when a write miss occurs, the block
115 written to is brought into the D1 cache. Most modern caches
116 have this property.</para>
117 </listitem>
118
119 <listitem>
120 <para>Bit-selection hash function: the line(s) in the cache
121 to which a memory block maps is chosen by the middle bits
122 M--(M+N-1) of the byte address, where:</para>
123 <itemizedlist>
124 <listitem>
125 <para>line size = 2^M bytes</para>
126 </listitem>
127 <listitem>
128 <para>(cache size / line size) = 2^N bytes</para>
129 </listitem>
130 </itemizedlist>
131 </listitem>
132
133 <listitem>
134 <para>Inclusive L2 cache: the L2 cache replicates all the
135 entries of the L1 cache. This is standard on Pentium chips,
136 but AMD Athlons use an exclusive L2 cache that only holds
137 blocks evicted from L1. Ditto AMD Durons and most modern
138 VIAs.</para>
139 </listitem>
140
141</itemizedlist>
142
143<para>The cache configuration simulated (cache size,
144associativity and line size) is determined automagically using
145the CPUID instruction. If you have an old machine that (a)
146doesn't support the CPUID instruction, or (b) supports it in an
147early incarnation that doesn't give any cache information, then
148Cachegrind will fall back to using a default configuration (that
149of a model 3/4 Athlon). Cachegrind will tell you if this
150happens. You can manually specify one, two or all three levels
151(I1/D1/L2) of the cache from the command line using the
152<computeroutput>--I1</computeroutput>,
153<computeroutput>--D1</computeroutput> and
154<computeroutput>--L2</computeroutput> options.</para>
155
156
157<para>Other noteworthy behaviour:</para>
158
159<itemizedlist>
160 <listitem>
161 <para>References that straddle two cache lines are treated as
162 follows:</para>
163 <itemizedlist>
164 <listitem>
165 <para>If both blocks hit --&gt; counted as one hit</para>
166 </listitem>
167 <listitem>
168 <para>If one block hits, the other misses --&gt; counted
169 as one miss.</para>
170 </listitem>
171 <listitem>
172 <para>If both blocks miss --&gt; counted as one miss (not
173 two)</para>
174 </listitem>
175 </itemizedlist>
176 </listitem>
177
178 <listitem>
179 <para>Instructions that modify a memory location
180 (eg. <computeroutput>inc</computeroutput> and
181 <computeroutput>dec</computeroutput>) are counted as doing
182 just a read, ie. a single data reference. This may seem
183 strange, but since the write can never cause a miss (the read
184 guarantees the block is in the cache) it's not very
185 interesting.</para>
186
187 <para>Thus it measures not the number of times the data cache
188 is accessed, but the number of times a data cache miss could
189 occur.</para>
190 </listitem>
191
192</itemizedlist>
193
194<para>If you are interested in simulating a cache with different
195properties, it is not particularly hard to write your own cache
196simulator, or to modify the existing ones in
197<computeroutput>vg_cachesim_I1.c</computeroutput>,
198<computeroutput>vg_cachesim_D1.c</computeroutput>,
199<computeroutput>vg_cachesim_L2.c</computeroutput> and
200<computeroutput>vg_cachesim_gen.c</computeroutput>. We'd be
201interested to hear from anyone who does.</para>
202
203</sect2>
204
205</sect1>
206
207
208
209<sect1 id="cg-manual.profile" xreflabel="Profiling programs">
210<title>Profiling programs</title>
211
212<para>To gather cache profiling information about the program
213<computeroutput>ls -l</computeroutput>, invoke Cachegrind like
214this:</para>
215
216<programlisting><![CDATA[
217valgrind --tool=cachegrind ls -l]]></programlisting>
218
219<para>The program will execute (slowly). Upon completion,
220summary statistics that look like this will be printed:</para>
221
222<programlisting><![CDATA[
223==31751== I refs: 27,742,716
224==31751== I1 misses: 276
225==31751== L2 misses: 275
226==31751== I1 miss rate: 0.0%
227==31751== L2i miss rate: 0.0%
228==31751==
229==31751== D refs: 15,430,290 (10,955,517 rd + 4,474,773 wr)
230==31751== D1 misses: 41,185 ( 21,905 rd + 19,280 wr)
231==31751== L2 misses: 23,085 ( 3,987 rd + 19,098 wr)
232==31751== D1 miss rate: 0.2% ( 0.1% + 0.4%)
233==31751== L2d miss rate: 0.1% ( 0.0% + 0.4%)
234==31751==
235==31751== L2 misses: 23,360 ( 4,262 rd + 19,098 wr)
236==31751== L2 miss rate: 0.0% ( 0.0% + 0.4%)]]></programlisting>
237
238<para>Cache accesses for instruction fetches are summarised
239first, giving the number of fetches made (this is the number of
240instructions executed, which can be useful to know in its own
241right), the number of I1 misses, and the number of L2 instruction
242(<computeroutput>L2i</computeroutput>) misses.</para>
243
244<para>Cache accesses for data follow. The information is similar
245to that of the instruction fetches, except that the values are
246also shown split between reads and writes (note each row's
247<computeroutput>rd</computeroutput> and
248<computeroutput>wr</computeroutput> values add up to the row's
249total).</para>
250
251<para>Combined instruction and data figures for the L2 cache
252follow that.</para>
253
254
255
256<sect2 id="cg-manual.outputfile" xreflabel="Output file">
257<title>Output file</title>
258
259<para>As well as printing summary information, Cachegrind also
sewardje1216cb2007-02-07 19:55:30 +0000260writes line-by-line cache profiling information to a user-specified
261file. By default this file is named
njn3e986b22004-11-30 10:43:45 +0000262<computeroutput>cachegrind.out.pid</computeroutput>. This file
sewardje1216cb2007-02-07 19:55:30 +0000263is human-readable, but is intended to be interpreted by the accompanying
njn3e986b22004-11-30 10:43:45 +0000264program <computeroutput>cg_annotate</computeroutput>, described
265in the next section.</para>
266
267<para>Things to note about the
268<computeroutput>cachegrind.out.pid</computeroutput>
269file:</para>
270
271<itemizedlist>
272 <listitem>
273 <para>It is written every time Cachegrind is run, and will
274 overwrite any existing
275 <computeroutput>cachegrind.out.pid</computeroutput>
276 in the current directory (but that won't happen very often
277 because it takes some time for process ids to be
278 recycled).</para>
sewardje1216cb2007-02-07 19:55:30 +0000279 <para>
280 To use a basename other than the default
281 <computeroutput>cachegrind.out.</computeroutput>,
282 use the <computeroutput>--cachegrind-out-file</computeroutput>
283 switch.</para>
284 <para>
285 To add further qualifiers to the output filename you can use
286 the core's <computeroutput>--log-file-qualifier</computeroutput>
287 flag.</para>
njn3e986b22004-11-30 10:43:45 +0000288 </listitem>
289 <listitem>
290 <para>It can be huge: <computeroutput>ls -l</computeroutput>
291 generates a file of about 350KB. Browsing a few files and
292 web pages with a Konqueror built with full debugging
293 information generates a file of around 15 MB.</para>
294 </listitem>
295</itemizedlist>
296
sewardj8d9fec52005-11-15 20:56:23 +0000297<para>The <computeroutput>.pid</computeroutput> suffix
de7e109d12005-11-18 22:09:58 +0000298on the output file name serves two purposes. Firstly, it means you
299don't have to rename old log files that you don't want to overwrite.
300Secondly, and more importantly, it allows correct profiling with the
njn3e986b22004-11-30 10:43:45 +0000301<computeroutput>--trace-children=yes</computeroutput> option of
302programs that spawn child processes.</para>
303
304</sect2>
305
306
307
308<sect2 id="cg-manual.cgopts" xreflabel="Cachegrind options">
309<title>Cachegrind options</title>
310
de03e0e7c2005-12-03 23:02:33 +0000311<!-- start of xi:include in the manpage -->
312<para id="cg.opts.para">Manually specifies the I1/D1/L2 cache
313configuration, where <varname>size</varname> and
314<varname>line_size</varname> are measured in bytes. The three items
315must be comma-separated, but with no spaces, eg:
316<literallayout> valgrind --tool=cachegrind --I1=65535,2,64</literallayout>
317
318You can specify one, two or three of the I1/D1/L2 caches. Any level not
319manually specified will be simulated using the configuration found in
320the normal way (via the CPUID instruction for automagic cache
321configuration, or failing that, via defaults).</para>
322
njn3e986b22004-11-30 10:43:45 +0000323<para>Cache-simulation specific options are:</para>
324
de03e0e7c2005-12-03 23:02:33 +0000325<variablelist id="cg.opts.list">
njn3e986b22004-11-30 10:43:45 +0000326
de03e0e7c2005-12-03 23:02:33 +0000327 <varlistentry id="opt.I1" xreflabel="--I1">
328 <term>
329 <option><![CDATA[--I1=<size>,<associativity>,<line size> ]]></option>
330 </term>
331 <listitem>
332 <para>Specify the size, associativity and line size of the level 1
333 instruction cache. </para>
334 </listitem>
335 </varlistentry>
njn3e986b22004-11-30 10:43:45 +0000336
de03e0e7c2005-12-03 23:02:33 +0000337 <varlistentry id="opt.D1" xreflabel="--D1">
338 <term>
339 <option><![CDATA[--D1=<size>,<associativity>,<line size> ]]></option>
340 </term>
341 <listitem>
342 <para>Specify the size, associativity and line size of the level 1
343 data cache.</para>
344 </listitem>
345 </varlistentry>
njn3e986b22004-11-30 10:43:45 +0000346
de03e0e7c2005-12-03 23:02:33 +0000347 <varlistentry id="opt.L2" xreflabel="--L2">
348 <term>
349 <option><![CDATA[--L2=<size>,<associativity>,<line size> ]]></option>
350 </term>
351 <listitem>
352 <para>Specify the size, associativity and line size of the level 2
353 cache.</para>
354 </listitem>
355 </varlistentry>
njn3e986b22004-11-30 10:43:45 +0000356
sewardje1216cb2007-02-07 19:55:30 +0000357 <varlistentry id="opt.cachegrind-out-file" xreflabel="--cachegrind-out-file">
358 <term>
359 <option><![CDATA[--cachegrind-out-file=<basename> ]]></option>
360 </term>
361 <listitem>
362 <para>Write the profile data to <![CDATA[<basename>.<pid>]]>
363 rather than to the default output file,
364 <![CDATA[cachegrind.out.<pid>]]>.
365 </para>
366 </listitem>
367 </varlistentry>
368
de03e0e7c2005-12-03 23:02:33 +0000369</variablelist>
370<!-- end of xi:include in the manpage -->
njn3e986b22004-11-30 10:43:45 +0000371
372</sect2>
373
374
375
376<sect2 id="cg-manual.annotate" xreflabel="Annotating C/C++ programs">
377<title>Annotating C/C++ programs</title>
378
379<para>Before using <computeroutput>cg_annotate</computeroutput>,
380it is worth widening your window to be at least 120-characters
381wide if possible, as the output lines can be quite long.</para>
382
383<para>To get a function-by-function summary, run
384<computeroutput>cg_annotate --pid</computeroutput> in a directory
de03e0e7c2005-12-03 23:02:33 +0000385containing a <filename>cachegrind.out.pid</filename> file. The
386<emphasis>--pid</emphasis> is required so that
387<computeroutput>cg_annotate</computeroutput> knows which log file to use
388when several are present.</para>
njn3e986b22004-11-30 10:43:45 +0000389
390<para>The output looks like this:</para>
391
392<programlisting><![CDATA[
393--------------------------------------------------------------------------------
394I1 cache: 65536 B, 64 B, 2-way associative
395D1 cache: 65536 B, 64 B, 2-way associative
396L2 cache: 262144 B, 64 B, 8-way associative
397Command: concord vg_to_ucode.c
398Events recorded: Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw
399Events shown: Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw
400Event sort order: Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw
401Threshold: 99%
402Chosen for annotation:
403Auto-annotation: on
404
405--------------------------------------------------------------------------------
406Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw
407--------------------------------------------------------------------------------
40827,742,716 276 275 10,955,517 21,905 3,987 4,474,773 19,280 19,098 PROGRAM TOTALS
409
410--------------------------------------------------------------------------------
411Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw file:function
412--------------------------------------------------------------------------------
4138,821,482 5 5 2,242,702 1,621 73 1,794,230 0 0 getc.c:_IO_getc
4145,222,023 4 4 2,276,334 16 12 875,959 1 1 concord.c:get_word
4152,649,248 2 2 1,344,810 7,326 1,385 . . . vg_main.c:strcmp
4162,521,927 2 2 591,215 0 0 179,398 0 0 concord.c:hash
4172,242,740 2 2 1,046,612 568 22 448,548 0 0 ctype.c:tolower
4181,496,937 4 4 630,874 9,000 1,400 279,388 0 0 concord.c:insert
419 897,991 51 51 897,831 95 30 62 1 1 ???:???
420 598,068 1 1 299,034 0 0 149,517 0 0 ../sysdeps/generic/lockfile.c:__flockfile
421 598,068 0 0 299,034 0 0 149,517 0 0 ../sysdeps/generic/lockfile.c:__funlockfile
422 598,024 4 4 213,580 35 16 149,506 0 0 vg_clientmalloc.c:malloc
423 446,587 1 1 215,973 2,167 430 129,948 14,057 13,957 concord.c:add_existing
424 341,760 2 2 128,160 0 0 128,160 0 0 vg_clientmalloc.c:vg_trap_here_WRAPPER
425 320,782 4 4 150,711 276 0 56,027 53 53 concord.c:init_hash_table
426 298,998 1 1 106,785 0 0 64,071 1 1 concord.c:create
427 149,518 0 0 149,516 0 0 1 0 0 ???:tolower@@GLIBC_2.0
428 149,518 0 0 149,516 0 0 1 0 0 ???:fgetc@@GLIBC_2.0
429 95,983 4 4 38,031 0 0 34,409 3,152 3,150 concord.c:new_word_node
430 85,440 0 0 42,720 0 0 21,360 0 0 vg_clientmalloc.c:vg_bogus_epilogue]]></programlisting>
431
432
433<para>First up is a summary of the annotation options:</para>
434
435<itemizedlist>
436
437 <listitem>
438 <para>I1 cache, D1 cache, L2 cache: cache configuration. So
439 you know the configuration with which these results were
440 obtained.</para>
441 </listitem>
442
443 <listitem>
444 <para>Command: the command line invocation of the program
445 under examination.</para>
446 </listitem>
447
448 <listitem>
449 <para>Events recorded: event abbreviations are:</para>
450 <itemizedlist>
451 <listitem>
452 <para><computeroutput>Ir </computeroutput>: I cache reads
453 (ie. instructions executed)</para>
454 </listitem>
455 <listitem>
456 <para><computeroutput>I1mr</computeroutput>: I1 cache read
457 misses</para>
458 </listitem>
459 <listitem>
460 <para><computeroutput>I2mr</computeroutput>: L2 cache
461 instruction read misses</para>
462 </listitem>
463 <listitem>
464 <para><computeroutput>Dr </computeroutput>: D cache reads
465 (ie. memory reads)</para>
466 </listitem>
467 <listitem>
468 <para><computeroutput>D1mr</computeroutput>: D1 cache read
469 misses</para>
470 </listitem>
471 <listitem>
472 <para><computeroutput>D2mr</computeroutput>: L2 cache data
473 read misses</para>
474 </listitem>
475 <listitem>
476 <para><computeroutput>Dw </computeroutput>: D cache writes
477 (ie. memory writes)</para>
478 </listitem>
479 <listitem>
480 <para><computeroutput>D1mw</computeroutput>: D1 cache write
481 misses</para>
482 </listitem>
483 <listitem>
484 <para><computeroutput>D2mw</computeroutput>: L2 cache data
485 write misses</para>
486 </listitem>
487 </itemizedlist>
488
489 <para>Note that D1 total accesses is given by
490 <computeroutput>D1mr</computeroutput> +
491 <computeroutput>D1mw</computeroutput>, and that L2 total
492 accesses is given by <computeroutput>I2mr</computeroutput> +
493 <computeroutput>D2mr</computeroutput> +
494 <computeroutput>D2mw</computeroutput>.</para>
495 </listitem>
496
497 <listitem>
498 <para>Events shown: the events shown (a subset of events
499 gathered). This can be adjusted with the
500 <computeroutput>--show</computeroutput> option.</para>
501 </listitem>
502
503 <listitem>
504 <para>Event sort order: the sort order in which functions are
505 shown. For example, in this case the functions are sorted
506 from highest <computeroutput>Ir</computeroutput> counts to
507 lowest. If two functions have identical
508 <computeroutput>Ir</computeroutput> counts, they will then be
509 sorted by <computeroutput>I1mr</computeroutput> counts, and
510 so on. This order can be adjusted with the
511 <computeroutput>--sort</computeroutput> option.</para>
512
513 <para>Note that this dictates the order the functions appear.
514 It is <command>not</command> the order in which the columns
515 appear; that is dictated by the "events shown" line (and can
516 be changed with the <computeroutput>--show</computeroutput>
517 option).</para>
518 </listitem>
519
520 <listitem>
521 <para>Threshold: <computeroutput>cg_annotate</computeroutput>
522 by default omits functions that cause very low numbers of
523 misses to avoid drowning you in information. In this case,
524 cg_annotate shows summaries the functions that account for
525 99% of the <computeroutput>Ir</computeroutput> counts;
526 <computeroutput>Ir</computeroutput> is chosen as the
527 threshold event since it is the primary sort event. The
528 threshold can be adjusted with the
529 <computeroutput>--threshold</computeroutput>
530 option.</para>
531 </listitem>
532
533 <listitem>
534 <para>Chosen for annotation: names of files specified
535 manually for annotation; in this case none.</para>
536 </listitem>
537
538 <listitem>
539 <para>Auto-annotation: whether auto-annotation was requested
540 via the <computeroutput>--auto=yes</computeroutput>
541 option. In this case no.</para>
542 </listitem>
543
544</itemizedlist>
545
546<para>Then follows summary statistics for the whole
547program. These are similar to the summary provided when running
de03e0e7c2005-12-03 23:02:33 +0000548<computeroutput>valgrind --tool=cachegrind</computeroutput>.</para>
njn3e986b22004-11-30 10:43:45 +0000549
550<para>Then follows function-by-function statistics. Each function
551is identified by a
552<computeroutput>file_name:function_name</computeroutput> pair. If
553a column contains only a dot it means the function never performs
554that event (eg. the third row shows that
555<computeroutput>strcmp()</computeroutput> contains no
556instructions that write to memory). The name
557<computeroutput>???</computeroutput> is used if the the file name
558and/or function name could not be determined from debugging
559information. If most of the entries have the form
560<computeroutput>???:???</computeroutput> the program probably
561wasn't compiled with <computeroutput>-g</computeroutput>. If any
562code was invalidated (either due to self-modifying code or
563unloading of shared objects) its counts are aggregated into a
564single cost centre written as
565<computeroutput>(discarded):(discarded)</computeroutput>.</para>
566
567<para>It is worth noting that functions will come from three
568types of source files:</para>
569
570<orderedlist>
571 <listitem>
572 <para>From the profiled program
573 (<filename>concord.c</filename> in this example).</para>
574 </listitem>
575 <listitem>
576 <para>From libraries (eg. <filename>getc.c</filename>)</para>
577 </listitem>
578 <listitem>
579 <para>From Valgrind's implementation of some libc functions
580 (eg. <computeroutput>vg_clientmalloc.c:malloc</computeroutput>).
581 These are recognisable because the filename begins with
582 <computeroutput>vg_</computeroutput>, and is probably one of
583 <filename>vg_main.c</filename>,
584 <filename>vg_clientmalloc.c</filename> or
585 <filename>vg_mylibc.c</filename>.</para>
586 </listitem>
587
588</orderedlist>
589
590<para>There are two ways to annotate source files -- by choosing
591them manually, or with the
592<computeroutput>--auto=yes</computeroutput> option. To do it
593manually, just specify the filenames as arguments to
594<computeroutput>cg_annotate</computeroutput>. For example, the
595output from running <filename>cg_annotate concord.c</filename>
596for our example produces the same output as above followed by an
597annotated version of <filename>concord.c</filename>, a section of
598which looks like:</para>
599
600<programlisting><![CDATA[
601--------------------------------------------------------------------------------
602-- User-annotated source: concord.c
603--------------------------------------------------------------------------------
604Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw
605
606[snip]
607
608 . . . . . . . . . void init_hash_table(char *file_name, Word_Node *table[])
609 3 1 1 . . . 1 0 0 {
610 . . . . . . . . . FILE *file_ptr;
611 . . . . . . . . . Word_Info *data;
612 1 0 0 . . . 1 1 1 int line = 1, i;
613 . . . . . . . . .
614 5 0 0 . . . 3 0 0 data = (Word_Info *) create(sizeof(Word_Info));
615 . . . . . . . . .
616 4,991 0 0 1,995 0 0 998 0 0 for (i = 0; i < TABLE_SIZE; i++)
617 3,988 1 1 1,994 0 0 997 53 52 table[i] = NULL;
618 . . . . . . . . .
619 . . . . . . . . . /* Open file, check it. */
620 6 0 0 1 0 0 4 0 0 file_ptr = fopen(file_name, "r");
621 2 0 0 1 0 0 . . . if (!(file_ptr)) {
622 . . . . . . . . . fprintf(stderr, "Couldn't open '%s'.\n", file_name);
623 1 1 1 . . . . . . exit(EXIT_FAILURE);
624 . . . . . . . . . }
625 . . . . . . . . .
626 165,062 1 1 73,360 0 0 91,700 0 0 while ((line = get_word(data, line, file_ptr)) != EOF)
627 146,712 0 0 73,356 0 0 73,356 0 0 insert(data->;word, data->line, table);
628 . . . . . . . . .
629 4 0 0 1 0 0 2 0 0 free(data);
630 4 0 0 1 0 0 2 0 0 fclose(file_ptr);
631 3 0 0 2 0 0 . . . }]]></programlisting>
632
633<para>(Although column widths are automatically minimised, a wide
634terminal is clearly useful.)</para>
635
636<para>Each source file is clearly marked
637(<computeroutput>User-annotated source</computeroutput>) as
638having been chosen manually for annotation. If the file was
639found in one of the directories specified with the
640<computeroutput>-I / --include</computeroutput> option, the directory
641and file are both given.</para>
642
643<para>Each line is annotated with its event counts. Events not
644applicable for a line are represented by a `.'; this is useful
645for distinguishing between an event which cannot happen, and one
646which can but did not.</para>
647
648<para>Sometimes only a small section of a source file is
sewardj8d9fec52005-11-15 20:56:23 +0000649executed. To minimise uninteresting output, Cachegrind only shows
njn3e986b22004-11-30 10:43:45 +0000650annotated lines and lines within a small distance of annotated
651lines. Gaps are marked with the line numbers so you know which
652part of a file the shown code comes from, eg:</para>
653
654<programlisting><![CDATA[
655(figures and code for line 704)
656-- line 704 ----------------------------------------
657-- line 878 ----------------------------------------
658(figures and code for line 878)]]></programlisting>
659
660<para>The amount of context to show around annotated lines is
661controlled by the <computeroutput>--context</computeroutput>
662option.</para>
663
664<para>To get automatic annotation, run
665<computeroutput>cg_annotate --auto=yes</computeroutput>.
666cg_annotate will automatically annotate every source file it can
667find that is mentioned in the function-by-function summary.
668Therefore, the files chosen for auto-annotation are affected by
669the <computeroutput>--sort</computeroutput> and
670<computeroutput>--threshold</computeroutput> options. Each
671source file is clearly marked (<computeroutput>Auto-annotated
672source</computeroutput>) as being chosen automatically. Any
673files that could not be found are mentioned at the end of the
674output, eg:</para>
675
676<programlisting><![CDATA[
677------------------------------------------------------------------
678The following files chosen for auto-annotation could not be found:
679------------------------------------------------------------------
680 getc.c
681 ctype.c
682 ../sysdeps/generic/lockfile.c]]></programlisting>
683
684<para>This is quite common for library files, since libraries are
685usually compiled with debugging information, but the source files
686are often not present on a system. If a file is chosen for
687annotation <command>both</command> manually and automatically, it
688is marked as <computeroutput>User-annotated
689source</computeroutput>. Use the <computeroutput>-I /
690--include</computeroutput> option to tell Valgrind where to look
691for source files if the filenames found from the debugging
692information aren't specific enough.</para>
693
694<para>Beware that cg_annotate can take some time to digest large
695<computeroutput>cachegrind.out.pid</computeroutput> files,
696e.g. 30 seconds or more. Also beware that auto-annotation can
697produce a lot of output if your program is large!</para>
698
699</sect2>
700
701
702<sect2 id="cg-manual.assembler" xreflabel="Annotating assembler programs">
703<title>Annotating assembler programs</title>
704
705<para>Valgrind can annotate assembler programs too, or annotate
706the assembler generated for your C program. Sometimes this is
707useful for understanding what is really happening when an
708interesting line of C code is translated into multiple
709instructions.</para>
710
711<para>To do this, you just need to assemble your
712<computeroutput>.s</computeroutput> files with assembler-level
713debug information. gcc doesn't do this, but you can use the GNU
714assembler with the <computeroutput>--gstabs</computeroutput>
715option to generate object files with this information, eg:</para>
716
717<programlisting><![CDATA[
718as --gstabs foo.s]]></programlisting>
719
720<para>You can then profile and annotate source files in the same
721way as for C/C++ programs.</para>
722
723</sect2>
724
725</sect1>
726
727
728<sect1 id="cg-manual.annopts" xreflabel="cg_annotate options">
729<title><computeroutput>cg_annotate</computeroutput> options</title>
730
731<itemizedlist>
732
debc32e822005-06-25 14:43:05 +0000733 <listitem id="pid">
njn3e986b22004-11-30 10:43:45 +0000734 <para><computeroutput>--pid</computeroutput></para>
735 <para>Indicates which
736 <computeroutput>cachegrind.out.pid</computeroutput> file to
737 read. Not actually an option -- it is required.</para>
738 </listitem>
739
740 <listitem>
741 <para><computeroutput>-h, --help</computeroutput></para>
742 <para><computeroutput>-v, --version</computeroutput></para>
743 <para>Help and version, as usual.</para>
744 </listitem>
745
debc32e822005-06-25 14:43:05 +0000746 <listitem id="sort">
njn3e986b22004-11-30 10:43:45 +0000747 <para><computeroutput>--sort=A,B,C</computeroutput> [default:
748 order in
749 <computeroutput>cachegrind.out.pid</computeroutput>]</para>
750 <para>Specifies the events upon which the sorting of the
751 function-by-function entries will be based. Useful if you
752 want to concentrate on eg. I cache misses
753 (<computeroutput>--sort=I1mr,I2mr</computeroutput>), or D
754 cache misses
755 (<computeroutput>--sort=D1mr,D2mr</computeroutput>), or L2
756 misses
757 (<computeroutput>--sort=D2mr,I2mr</computeroutput>).</para>
758 </listitem>
759
debc32e822005-06-25 14:43:05 +0000760 <listitem id="show">
njn3e986b22004-11-30 10:43:45 +0000761 <para><computeroutput>--show=A,B,C</computeroutput> [default:
762 all, using order in
763 <computeroutput>cachegrind.out.pid</computeroutput>]</para>
764 <para>Specifies which events to show (and the column
765 order). Default is to use all present in the
766 <computeroutput>cachegrind.out.pid</computeroutput> file (and
767 use the order in the file).</para>
768 </listitem>
769
debc32e822005-06-25 14:43:05 +0000770 <listitem id="threshold">
njn3e986b22004-11-30 10:43:45 +0000771 <para><computeroutput>--threshold=X</computeroutput>
772 [default: 99%]</para>
773 <para>Sets the threshold for the function-by-function
774 summary. Functions are shown that account for more than X%
775 of the primary sort event. If auto-annotating, also affects
776 which files are annotated.</para>
777
778 <para>Note: thresholds can be set for more than one of the
779 events by appending any events for the
780 <computeroutput>--sort</computeroutput> option with a colon
781 and a number (no spaces, though). E.g. if you want to see
782 the functions that cover 99% of L2 read misses and 99% of L2
783 write misses, use this option:</para>
784 <para><computeroutput>--sort=D2mr:99,D2mw:99</computeroutput></para>
785 </listitem>
786
debc32e822005-06-25 14:43:05 +0000787 <listitem id="auto">
njn3e986b22004-11-30 10:43:45 +0000788 <para><computeroutput>--auto=no</computeroutput> [default]</para>
789 <para><computeroutput>--auto=yes</computeroutput></para>
790 <para>When enabled, automatically annotates every file that
791 is mentioned in the function-by-function summary that can be
792 found. Also gives a list of those that couldn't be found.</para>
793 </listitem>
794
debc32e822005-06-25 14:43:05 +0000795 <listitem id="context">
njn3e986b22004-11-30 10:43:45 +0000796 <para><computeroutput>--context=N</computeroutput> [default:
797 8]</para>
798 <para>Print N lines of context before and after each
799 annotated line. Avoids printing large sections of source
800 files that were not executed. Use a large number
801 (eg. 10,000) to show all source lines.</para>
802 </listitem>
803
debc32e822005-06-25 14:43:05 +0000804 <listitem id="include">
sewardj8d9fec52005-11-15 20:56:23 +0000805 <para><computeroutput>-I&lt;dir&gt;,
njn3e986b22004-11-30 10:43:45 +0000806 --include=&lt;dir&gt;</computeroutput> [default: empty
807 string]</para>
808 <para>Adds a directory to the list in which to search for
809 files. Multiple -I/--include options can be given to add
810 multiple directories.</para>
811 </listitem>
812
813</itemizedlist>
814
815
816
817<sect2>
818<title>Warnings</title>
819
820<para>There are a couple of situations in which
821<computeroutput>cg_annotate</computeroutput> issues
822warnings.</para>
823
824<itemizedlist>
825 <listitem>
826 <para>If a source file is more recent than the
827 <computeroutput>cachegrind.out.pid</computeroutput> file.
828 This is because the information in
829 <computeroutput>cachegrind.out.pid</computeroutput> is only
830 recorded with line numbers, so if the line numbers change at
831 all in the source (eg. lines added, deleted, swapped), any
832 annotations will be incorrect.</para>
833 </listitem>
834 <listitem>
835 <para>If information is recorded about line numbers past the
836 end of a file. This can be caused by the above problem,
837 ie. shortening the source file while using an old
838 <computeroutput>cachegrind.out.pid</computeroutput> file. If
839 this happens, the figures for the bogus lines are printed
840 anyway (clearly marked as bogus) in case they are
841 important.</para>
842 </listitem>
843</itemizedlist>
844
845</sect2>
846
847
848
849<sect2>
850<title>Things to watch out for</title>
851
852<para>Some odd things that can occur during annotation:</para>
853
854<itemizedlist>
855 <listitem>
856 <para>If annotating at the assembler level, you might see
857 something like this:</para>
858<programlisting><![CDATA[
859 1 0 0 . . . . . . leal -12(%ebp),%eax
860 1 0 0 . . . 1 0 0 movl %eax,84(%ebx)
861 2 0 0 0 0 0 1 0 0 movl $1,-20(%ebp)
862 . . . . . . . . . .align 4,0x90
863 1 0 0 . . . . . . movl $.LnrB,%eax
864 1 0 0 . . . 1 0 0 movl %eax,-16(%ebp)]]></programlisting>
865
866 <para>How can the third instruction be executed twice when
867 the others are executed only once? As it turns out, it
868 isn't. Here's a dump of the executable, using
869 <computeroutput>objdump -d</computeroutput>:</para>
870<programlisting><![CDATA[
871 8048f25: 8d 45 f4 lea 0xfffffff4(%ebp),%eax
872 8048f28: 89 43 54 mov %eax,0x54(%ebx)
873 8048f2b: c7 45 ec 01 00 00 00 movl $0x1,0xffffffec(%ebp)
874 8048f32: 89 f6 mov %esi,%esi
875 8048f34: b8 08 8b 07 08 mov $0x8078b08,%eax
876 8048f39: 89 45 f0 mov %eax,0xfffffff0(%ebp)]]></programlisting>
877
878 <para>Notice the extra <computeroutput>mov
879 %esi,%esi</computeroutput> instruction. Where did this come
880 from? The GNU assembler inserted it to serve as the two
881 bytes of padding needed to align the <computeroutput>movl
882 $.LnrB,%eax</computeroutput> instruction on a four-byte
883 boundary, but pretended it didn't exist when adding debug
884 information. Thus when Valgrind reads the debug info it
885 thinks that the <computeroutput>movl
886 $0x1,0xffffffec(%ebp)</computeroutput> instruction covers the
887 address range 0x8048f2b--0x804833 by itself, and attributes
888 the counts for the <computeroutput>mov
889 %esi,%esi</computeroutput> to it.</para>
890 </listitem>
891
892 <listitem>
893 <para>Inlined functions can cause strange results in the
894 function-by-function summary. If a function
895 <computeroutput>inline_me()</computeroutput> is defined in
896 <filename>foo.h</filename> and inlined in the functions
897 <computeroutput>f1()</computeroutput>,
898 <computeroutput>f2()</computeroutput> and
899 <computeroutput>f3()</computeroutput> in
900 <filename>bar.c</filename>, there will not be a
901 <computeroutput>foo.h:inline_me()</computeroutput> function
902 entry. Instead, there will be separate function entries for
903 each inlining site, ie.
904 <computeroutput>foo.h:f1()</computeroutput>,
905 <computeroutput>foo.h:f2()</computeroutput> and
906 <computeroutput>foo.h:f3()</computeroutput>. To find the
907 total counts for
908 <computeroutput>foo.h:inline_me()</computeroutput>, add up
909 the counts from each entry.</para>
910
911 <para>The reason for this is that although the debug info
912 output by gcc indicates the switch from
913 <filename>bar.c</filename> to <filename>foo.h</filename>, it
914 doesn't indicate the name of the function in
915 <filename>foo.h</filename>, so Valgrind keeps using the old
916 one.</para>
917 </listitem>
918
919 <listitem>
920 <para>Sometimes, the same filename might be represented with
921 a relative name and with an absolute name in different parts
922 of the debug info, eg:
923 <filename>/home/user/proj/proj.h</filename> and
924 <filename>../proj.h</filename>. In this case, if you use
925 auto-annotation, the file will be annotated twice with the
926 counts split between the two.</para>
927 </listitem>
928
929 <listitem>
930 <para>Files with more than 65,535 lines cause difficulties
931 for the stabs debug info reader. This is because the line
932 number in the <computeroutput>struct nlist</computeroutput>
933 defined in <filename>a.out.h</filename> under Linux is only a
934 16-bit value. Valgrind can handle some files with more than
935 65,535 lines correctly by making some guesses to identify
936 line number overflows. But some cases are beyond it, in
937 which case you'll get a warning message explaining that
938 annotations for the file might be incorrect.</para>
939 </listitem>
940
941 <listitem>
942 <para>If you compile some files with
943 <computeroutput>-g</computeroutput> and some without, some
944 events that take place in a file without debug info could be
945 attributed to the last line of a file with debug info
946 (whichever one gets placed before the non-debug-info file in
947 the executable).</para>
948 </listitem>
949
950</itemizedlist>
951
952<para>This list looks long, but these cases should be fairly
953rare.</para>
954
955<formalpara>
956 <title>Note:</title>
957 <para><computeroutput>stabs</computeroutput> is not an easy
958 format to read. If you come across bizarre annotations that
959 look like might be caused by a bug in the stabs reader, please
960 let us know.</para>
961</formalpara>
962
963</sect2>
964
965
966
967<sect2>
968<title>Accuracy</title>
969
970<para>Valgrind's cache profiling has a number of
971shortcomings:</para>
972
973<itemizedlist>
974 <listitem>
975 <para>It doesn't account for kernel activity -- the effect of
976 system calls on the cache contents is ignored.</para>
977 </listitem>
978
979 <listitem>
980 <para>It doesn't account for other process activity (although
981 this is probably desirable when considering a single
982 program).</para>
983 </listitem>
984
985 <listitem>
986 <para>It doesn't account for virtual-to-physical address
987 mappings; hence the entire simulation is not a true
988 representation of what's happening in the
989 cache.</para>
990 </listitem>
991
992 <listitem>
993 <para>It doesn't account for cache misses not visible at the
994 instruction level, eg. those arising from TLB misses, or
995 speculative execution.</para>
996 </listitem>
997
998 <listitem>
sewardj8d9fec52005-11-15 20:56:23 +0000999 <para>Valgrind will schedule
1000 threads differently from how they would be when running natively.
1001 This could warp the results for threaded programs.</para>
njn3e986b22004-11-30 10:43:45 +00001002 </listitem>
1003
1004 <listitem>
sewardj8d9fec52005-11-15 20:56:23 +00001005 <para>The x86/amd64 instructions <computeroutput>bts</computeroutput>,
njn3e986b22004-11-30 10:43:45 +00001006 <computeroutput>btr</computeroutput> and
1007 <computeroutput>btc</computeroutput> will incorrectly be
1008 counted as doing a data read if both the arguments are
1009 registers, eg:</para>
1010<programlisting><![CDATA[
1011 btsl %eax, %edx]]></programlisting>
1012
1013 <para>This should only happen rarely.</para>
1014 </listitem>
1015
1016 <listitem>
sewardj8d9fec52005-11-15 20:56:23 +00001017 <para>x86/amd64 FPU instructions with data sizes of 28 and 108 bytes
njn3e986b22004-11-30 10:43:45 +00001018 (e.g. <computeroutput>fsave</computeroutput>) are treated as
1019 though they only access 16 bytes. These instructions seem to
1020 be rare so hopefully this won't affect accuracy much.</para>
1021 </listitem>
1022
1023</itemizedlist>
1024
1025<para>Another thing worth nothing is that results are very
1026sensitive. Changing the size of the
1027<filename>valgrind.so</filename> file, the size of the program
1028being profiled, or even the length of its name can perturb the
1029results. Variations will be small, but don't expect perfectly
1030repeatable results if your program changes at all.</para>
1031
1032<para>While these factors mean you shouldn't trust the results to
1033be super-accurate, hopefully they should be close enough to be
1034useful.</para>
1035
1036</sect2>
1037
njn534f7812006-10-21 22:22:59 +00001038</sect1>
1039
1040<sect1>
1041<title>Implementation details</title>
1042This section talks about details you don't need to know about in order to
1043use Cachegrind, but may be of interest to some people.
njn3e986b22004-11-30 10:43:45 +00001044
1045<sect2>
njn534f7812006-10-21 22:22:59 +00001046<title>How Cachegrind works</title>
1047<para>The best reference for understanding how Cachegrind works is chapter 3 of
1048"Dynamic Binary Analysis and Instrumentation", by Nicholas Nethercote. It
njn011215f2006-10-21 23:00:59 +00001049is available on the <ulink url="&vg-pubs;">Valgrind publications
1050page</ulink>.</para>
njn534f7812006-10-21 22:22:59 +00001051</sect2>
njn3e986b22004-11-30 10:43:45 +00001052
njn534f7812006-10-21 22:22:59 +00001053<sect2>
1054<title>Cachegrind output file format</title>
1055<para>The file format is fairly straightforward, basically giving the
1056cost centre for every line, grouped by files and
1057functions. Total counts (eg. total cache accesses, total L1
1058misses) are calculated when traversing this structure rather than
1059during execution, to save time; the cache simulation functions
1060are called so often that even one or two extra adds can make a
1061sizeable difference.</para>
1062
1063<para>The file format:</para>
1064<programlisting><![CDATA[
1065file ::= desc_line* cmd_line events_line data_line+ summary_line
1066desc_line ::= "desc:" ws? non_nl_string
1067cmd_line ::= "cmd:" ws? cmd
1068events_line ::= "events:" ws? (event ws)+
1069data_line ::= file_line | fn_line | count_line
1070file_line ::= "fl=" filename
1071fn_line ::= "fn=" fn_name
1072count_line ::= line_num ws? (count ws)+
1073summary_line ::= "summary:" ws? (count ws)+
1074count ::= num | "."]]></programlisting>
1075
1076<para>Where:</para>
njn3e986b22004-11-30 10:43:45 +00001077<itemizedlist>
1078 <listitem>
njn534f7812006-10-21 22:22:59 +00001079 <para><computeroutput>non_nl_string</computeroutput> is any
1080 string not containing a newline.</para>
njn3e986b22004-11-30 10:43:45 +00001081 </listitem>
njn534f7812006-10-21 22:22:59 +00001082 <listitem>
1083 <para><computeroutput>cmd</computeroutput> is a string holding the
1084 command line of the profiled program.</para>
1085 </listitem>
1086 <listitem>
njn26242122007-01-22 03:21:27 +00001087 <para><computeroutput>event</computeroutput> is a string containing
1088 no whitespace.</para>
1089 </listitem>
1090 <listitem>
njn534f7812006-10-21 22:22:59 +00001091 <para><computeroutput>filename</computeroutput> and
1092 <computeroutput>fn_name</computeroutput> are strings.</para>
1093 </listitem>
1094 <listitem>
1095 <para><computeroutput>num</computeroutput> and
1096 <computeroutput>line_num</computeroutput> are decimal
1097 numbers.</para>
1098 </listitem>
1099 <listitem>
1100 <para><computeroutput>ws</computeroutput> is whitespace.</para>
1101 </listitem>
1102</itemizedlist>
1103
1104<para>The contents of the "desc:" lines are printed out at the top
1105of the summary. This is a generic way of providing simulation
1106specific information, eg. for giving the cache configuration for
1107cache simulation.</para>
1108
1109<para>More than one line of info can be presented for each file/fn/line number.
1110In such cases, the counts for the named events will be accumulated.</para>
1111
1112<para>Counts can be "." to represent zero. This makes the files easier to
1113read.</para>
1114
1115<para>The number of counts in each
1116<computeroutput>line</computeroutput> and the
1117<computeroutput>summary_line</computeroutput> should not exceed
1118the number of events in the
1119<computeroutput>event_line</computeroutput>. If the number in
1120each <computeroutput>line</computeroutput> is less, cg_annotate
1121treats those missing as though they were a "." entry.</para>
1122
1123<para>A <computeroutput>file_line</computeroutput> changes the
1124current file name. A <computeroutput>fn_line</computeroutput>
1125changes the current function name. A
1126<computeroutput>count_line</computeroutput> contains counts that
1127pertain to the current filename/fn_name. A "fn="
1128<computeroutput>file_line</computeroutput> and a
1129<computeroutput>fn_line</computeroutput> must appear before any
1130<computeroutput>count_line</computeroutput>s to give the context
1131of the first <computeroutput>count_line</computeroutput>s.</para>
1132
1133<para>Each <computeroutput>file_line</computeroutput> will normally be
1134immediately followed by a <computeroutput>fn_line</computeroutput>. But it
1135doesn't have to be.</para>
1136
njn3e986b22004-11-30 10:43:45 +00001137
1138</sect2>
1139
1140</sect1>
1141</chapter>