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