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