~bkerensa/ubuntu/raring/valgrind/merge-from-deb

« back to all changes in this revision

Viewing changes to callgrind/docs/cl-manual.xml

  • Committer: Bazaar Package Importer
  • Author(s): Andrés Roldán
  • Date: 2008-06-13 02:31:40 UTC
  • mto: (1.4.1 upstream) (2.2.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 24.
  • Revision ID: james.westby@ubuntu.com-20080613023140-iwk33rz9rhvfkr96
Import upstream version 3.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
[ <!ENTITY % cl-entities SYSTEM "cl-entities.xml"> %cl-entities; ]>
5
5
 
6
6
<chapter id="cl-manual" xreflabel="Callgrind Manual">
7
 
<title>Callgrind: a heavyweight profiler</title>
 
7
<title>Callgrind: a call graph profiler</title>
8
8
 
9
9
 
10
10
<sect1 id="cl-manual.use" xreflabel="Overview">
11
11
<title>Overview</title>
12
12
 
13
 
<para>Callgrind is a Valgrind tool for profiling programs.
14
 
The collected data consists of
15
 
the number of instructions executed on a run, their relationship
16
 
to source lines, and
17
 
call relationship among functions together with call counts.
 
13
<para>Callgrind is a profiling tool that can
 
14
construct a call graph for a program's run.
 
15
By default, the collected data consists of
 
16
the number of instructions executed, their relationship
 
17
to source lines, the caller/callee relationship between functions,
 
18
and the numbers of such calls.
18
19
Optionally, a cache simulator (similar to cachegrind) can produce
19
20
further information about the memory access behavior of the application.
20
21
</para>
27
28
  <term><command>callgrind_annotate</command></term>
28
29
  <listitem>
29
30
    <para>This command reads in the profile data, and prints a
30
 
    sorted lists of functions, optionally with annotation.</para>
 
31
    sorted lists of functions, optionally with source annotation.</para>
31
32
<!--
32
33
    <para>You can read the manpage here: <xref
33
34
              linkend="callgrind-annotate"/>.</para>
34
35
-->
35
 
    <para>For graphical visualization of the data, check out
36
 
    <ulink url="&cl-gui;">KCachegrind</ulink>.</para>
 
36
    <para>For graphical visualization of the data, try
 
37
    <ulink url="&cl-gui;">KCachegrind</ulink>, which is a KDE/Qt based
 
38
    GUI that makes it easy to navigate the large amount of data that
 
39
    Callgrind produces.</para>
37
40
 
38
41
  </listitem>
39
42
  </varlistentry>
44
47
    <para>This command enables you to interactively observe and control 
45
48
    the status of currently running applications, without stopping
46
49
    the application.  You can 
47
 
    get statistics information, the current stack trace, and request 
48
 
    zeroing of counters, and dumping of profiles data.</para>
 
50
    get statistics information as well as the current stack trace, and
 
51
    you can request zeroing of counters or dumping of profile data.</para>
49
52
<!--
50
53
    <para>You can read the manpage here: <xref linkend="callgrind-control"/>.</para>
51
54
-->
55
58
 
56
59
<para>To use Callgrind, you must specify 
57
60
<computeroutput>--tool=callgrind</computeroutput> on the Valgrind 
58
 
command line or use the supplied script 
59
 
<computeroutput>callgrind</computeroutput>.</para>
 
61
command line.</para>
 
62
 
 
63
  <sect2 id="cl-manual.functionality" xreflabel="Functionality">
 
64
  <title>Functionality</title>
 
65
 
 
66
<para>Cachegrind collects flat profile data: event counts (data reads,
 
67
cache misses, etc.) are attributed directly to the function they
 
68
occurred in.  This cost attribution mechanism is
 
69
called <emphasis>self</emphasis> or <emphasis>exclusive</emphasis>
 
70
attribution.</para>
 
71
 
 
72
<para>Callgrind extends this functionality by propagating costs
 
73
across function call boundaries.  If function <code>foo</code> calls
 
74
<code>bar</code>, the costs from <code>bar</code> are added into
 
75
<code>foo</code>'s costs.  When applied to the program as a whole,
 
76
this builds up a picture of so called <emphasis>inclusive</emphasis>
 
77
costs, that is, where the cost of each function includes the costs of
 
78
all functions it called, directly or indirectly.</para>
 
79
 
 
80
<para>As an example, the inclusive cost of
 
81
<computeroutput>main</computeroutput> should be almost 100 percent
 
82
of the total program cost.  Because of costs arising before 
 
83
<computeroutput>main</computeroutput> is run, such as
 
84
initialization of the run time linker and construction of global C++
 
85
objects, the inclusive cost of <computeroutput>main</computeroutput>
 
86
is not exactly 100 percent of the total program cost.</para>
 
87
 
 
88
<para>Together with the call graph, this allows you to find the
 
89
specific call chains starting from
 
90
<computeroutput>main</computeroutput> in which the majority of the
 
91
program's costs occur.  Caller/callee cost attribution is also useful
 
92
for profiling functions called from multiple call sites, and where
 
93
optimization opportunities depend on changing code in the callers, in
 
94
particular by reducing the call count.</para>
60
95
 
61
96
<para>Callgrind's cache simulation is based on the 
62
 
<ulink url="&cg-tool-url;">Cachegrind tool</ulink> of the 
63
 
<ulink url="&vg-url;">Valgrind</ulink> package.  Read 
64
 
<ulink url="&cg-doc-url;">Cachegrind's documentation</ulink> first; 
65
 
this page describes the features supported in addition to 
 
97
<ulink url="&cg-tool-url;">Cachegrind tool</ulink>. Read 
 
98
<ulink url="&cg-doc-url;">Cachegrind's documentation</ulink> first.
 
99
The material below describes the features supported in addition to 
66
100
Cachegrind's features.</para>
67
101
 
68
 
</sect1>
69
 
 
70
 
 
71
 
<sect1 id="cl-manual.purpose" xreflabel="Purpose">
72
 
<title>Purpose</title>
73
 
 
74
 
 
75
 
  <sect2 id="cl-manual.devel" 
76
 
         xreflabel="Profiling as part of Application Development">
77
 
  <title>Profiling as part of Application Development</title>
78
 
 
79
 
  <para>With application development, a common step is
80
 
  to improve runtime performance.  To not waste time on
81
 
  optimizing functions which are rarely used, one needs to know
82
 
  in which parts of the program most of the time is spent.</para>
83
 
 
84
 
  <para>This is done with a technique called profiling. The program
85
 
  is run under control of a profiling tool, which gives the time
86
 
  distribution of executed functions in the run. After examination
87
 
  of the program's profile, it should be clear if and where optimization
88
 
  is useful. Afterwards, one should verify any runtime changes by another
89
 
  profile run.</para>
90
 
 
91
 
  </sect2>
92
 
 
93
 
 
94
 
  <sect2 id="cl-manual.tools" xreflabel="Profiling Tools">
95
 
  <title>Profiling Tools</title>
96
 
 
97
 
  <para>Most widely known is the GCC profiling tool <command>GProf</command>:
98
 
  one needs to compile an application with the compiler option 
99
 
  <computeroutput>-pg</computeroutput>.  Running the program generates
100
 
  a file <computeroutput>gmon.out</computeroutput>, which can be 
101
 
  transformed into human readable form with the command line tool 
102
 
  <computeroutput>gprof</computeroutput>.  A disadvantage here is the 
103
 
  the need to recompile everything, and also the need to statically link the
104
 
  executable.</para>
105
 
 
106
 
  <para>Another profiling tool is <command>Cachegrind</command>, part
107
 
  of <ulink url="&vg-url;">Valgrind</ulink>. It uses the processor
108
 
  emulation of Valgrind to run the executable, and catches all memory
109
 
  accesses, which are used to drive a cache simulator.
110
 
  The program does not need to be
111
 
  recompiled, it can use shared libraries and plugins, and the profile
112
 
  measurement doesn't influence the memory access behaviour. 
113
 
  The trace includes 
114
 
  the number of instruction/data memory accesses and 1st/2nd level
115
 
  cache misses, and relates it to source lines and functions of the
116
 
  run program.  A disadvantage is the slowdown involved in the
117
 
  processor emulation, around 50 times slower.</para>
118
 
 
119
 
  <para>Cachegrind can only deliver a flat profile. There is no call 
120
 
  relationship among the functions of an application stored.  Thus, 
121
 
  inclusive costs, i.e. costs of a function including the cost of all 
122
 
  functions called from there, cannot be calculated. Callgrind extends 
123
 
  Cachegrind by including call relationship and exact event counts
124
 
  spent while doing a call.</para>
125
 
 
126
 
  <para>Because Callgrind (and Cachegrind) is based on simulation, the
127
 
  slowdown due to processing the synthetic runtime events does not
128
 
  influence the results.  See <xref linkend="cl-manual.usage"/> for more 
129
 
  details on the possibilities.</para>
130
 
 
131
 
  </sect2>
132
 
 
133
 
</sect1>
134
 
 
135
 
 
136
 
<sect1 id="cl-manual.usage" xreflabel="Usage">
137
 
<title>Usage</title>
138
 
 
139
 
  <sect2 id="cl-manual.basics" xreflabel="Basics">
140
 
  <title>Basics</title>
 
102
<para>Callgrind's ability to detect function calls and returns depends
 
103
on the instruction set of the platform it is run on.  It works best
 
104
on x86 and amd64, and unfortunately currently does not work so well
 
105
on PowerPC code.  This is because there are no explicit call or return
 
106
instructions in the PowerPC instruction set, so Callgrind has to rely
 
107
on heuristics to detect calls and returns.</para>
 
108
 
 
109
  </sect2>
 
110
 
 
111
  <sect2 id="cl-manual.basics" xreflabel="Basic Usage">
 
112
  <title>Basic Usage</title>
 
113
 
 
114
  <para>As with Cachegrind, you probably want to compile with debugging info
 
115
  (the -g flag), but with optimization turned on.</para>
141
116
 
142
117
  <para>To start a profile run for a program, execute:
143
 
  <screen>callgrind [callgrind options] your-program [program options]</screen>
 
118
  <screen>valgrind --tool=callgrind [callgrind options] your-program [program options]</screen>
144
119
  </para>
145
120
 
146
121
  <para>While the simulation is running, you can observe execution with
147
122
  <screen>callgrind_control -b</screen>
148
 
  This will print out a current backtrace. To annotate the backtrace with
 
123
  This will print out the current backtrace. To annotate the backtrace with
149
124
  event counts, run
150
125
  <screen>callgrind_control -e -b</screen>
151
126
  </para>
152
127
 
153
128
  <para>After program termination, a profile data file named 
154
 
  <computeroutput>callgrind.out.pid</computeroutput>
155
 
  is generated with <emphasis>pid</emphasis> being the process ID 
156
 
  of the execution of this profile run.</para>
157
 
 
158
 
  <para>The data file contains information about the calls made in the
 
129
  <computeroutput>callgrind.out.&lt;pid&gt;</computeroutput>
 
130
  is generated, where <emphasis>pid</emphasis> is the process ID 
 
131
  of the program being profiled.
 
132
  The data file contains information about the calls made in the
159
133
  program among the functions executed, together with events of type
160
134
  <command>Instruction Read Accesses</command> (Ir).</para>
161
135
 
 
136
  <para>To generate a function-by-function summary from the profile
 
137
  data file, use
 
138
  <screen>callgrind_annotate [options] callgrind.out.&lt;pid&gt;</screen>
 
139
  This summary is similar to the output you get from a Cachegrind
 
140
  run with <computeroutput>cg_annotate</computeroutput>: the list
 
141
  of functions is ordered by exclusive cost of functions, which also
 
142
  are the ones that are shown.
 
143
  Important for the additional features of Callgrind are
 
144
  the following two options:</para>
 
145
 
 
146
  <itemizedlist>
 
147
    <listitem>
 
148
      <para><option>--inclusive=yes</option>: Instead of using
 
149
      exclusive cost of functions as sorting order, use and show
 
150
      inclusive cost.</para>
 
151
    </listitem>
 
152
 
 
153
    <listitem>
 
154
      <para><option>--tree=both</option>: Interleave into the
 
155
      top level list of functions, information on the callers and the callees
 
156
      of each function. In these lines, which represents executed
 
157
      calls, the cost gives the number of events spent in the call.
 
158
      Indented, above each function, there is the list of callers,
 
159
      and below, the list of callees. The sum of events in calls to
 
160
      a given function (caller lines), as well as the sum of events in
 
161
      calls from the function (callee lines) together with the self
 
162
      cost, gives the total inclusive cost of the function.</para>
 
163
     </listitem>
 
164
  </itemizedlist>
 
165
 
 
166
  <para>Use <option>--auto=yes</option> to get annotated source code
 
167
  for all relevant functions for which the source can be found. In
 
168
  addition to source annotation as produced by
 
169
  <computeroutput>cg_annotate</computeroutput>, you will see the
 
170
  annotated call sites with call counts. For all other options, 
 
171
  consult the (Cachegrind) documentation for
 
172
  <computeroutput>cg_annotate</computeroutput>.
 
173
  </para>
 
174
 
 
175
  <para>For better call graph browsing experience, it is highly recommended
 
176
  to use <ulink url="&cl-gui;">KCachegrind</ulink>.
 
177
  If your code
 
178
  has a significant fraction of its cost in <emphasis>cycles</emphasis> (sets
 
179
  of functions calling each other in a recursive manner), you have to
 
180
  use KCachegrind, as <computeroutput>callgrind_annotate</computeroutput>
 
181
  currently does not do any cycle detection, which is important to get correct
 
182
  results in this case.</para>
 
183
 
162
184
  <para>If you are additionally interested in measuring the 
163
 
  cache behaviour of your 
 
185
  cache behavior of your 
164
186
  program, use Callgrind with the option
165
187
  <option><xref linkend="opt.simulate-cache"/>=yes.</option>
166
 
  This will further slow down the run approximately by a factor of 2.</para>
 
188
  However, expect a  further slow down approximately by a factor of 2.</para>
167
189
 
168
190
  <para>If the program section you want to profile is somewhere in the
169
191
  middle of the run, it is beneficial to 
170
192
  <emphasis>fast forward</emphasis> to this section without any 
171
 
  profiling at all, and switch it on later.  This is achieved by using
 
193
  profiling, and then switch on profiling.  This is achieved by using
 
194
  the command line option
172
195
  <option><xref linkend="opt.instr-atstart"/>=no</option> 
173
 
  and interactively use 
174
 
  <computeroutput>callgrind_control -i on</computeroutput> before the 
175
 
  interesting code section is about to be executed.</para>
 
196
  and running, in a shell,
 
197
  <computeroutput>callgrind_control -i on</computeroutput> just before the 
 
198
  interesting code section is executed. To exactly specify
 
199
  the code position where profiling should start, use the client request
 
200
  <computeroutput>CALLGRIND_START_INSTRUMENTATION</computeroutput>.</para>
176
201
 
177
 
  <para>If you want to be able to see assembler annotation, specify
 
202
  <para>If you want to be able to see assembly code level annotation, specify
178
203
  <option><xref linkend="opt.dump-instr"/>=yes</option>. This will produce
179
204
  profile data at instruction granularity. Note that the resulting profile
180
205
  data
181
 
  can only be viewed with KCachegrind. For assembler annotation, it also is
 
206
  can only be viewed with KCachegrind. For assembly annotation, it also is
182
207
  interesting to see more details of the control flow inside of functions,
183
208
  ie. (conditional) jumps. This will be collected by further specifying
184
209
  <option><xref linkend="opt.collect-jumps"/>=yes</option>.</para>
185
210
 
186
211
  </sect2>
187
212
 
 
213
</sect1>
 
214
 
 
215
<sect1 id="cl-manual.usage" xreflabel="Advanced Usage">
 
216
<title>Advanced Usage</title>
188
217
 
189
218
  <sect2 id="cl-manual.dumps" 
190
219
         xreflabel="Multiple dumps from one program run">
191
220
  <title>Multiple profiling dumps from one program run</title>
192
221
 
193
 
  <para>Often, you aren't interested in time characteristics of a full 
194
 
  program run, but only of a small part of it (e.g. execution of one
195
 
  algorithm).  If there are multiple algorithms or one algorithm 
196
 
  running with different input data, it's even useful to get different
197
 
  profile information for multiple parts of one program run.</para>
 
222
  <para>Sometimes you are not interested in characteristics of a full 
 
223
  program run, but only of a small part of it, for example execution of one
 
224
  algorithm.  If there are multiple algorithms, or one algorithm 
 
225
  running with different input data, it may even be useful to get different
 
226
  profile information for different parts of a single program run.</para>
198
227
 
199
228
  <para>Profile data files have names of the form
200
229
<screen>
220
249
    <listitem>
221
250
      <para><command>Dump on program termination.</command>
222
251
      This method is the standard way and doesn't need any special
223
 
      action from your side.</para>
 
252
      action on your part.</para>
224
253
    </listitem>
225
254
 
226
255
    <listitem>
232
261
      distinguish profile dumps.  The control program will not terminate
233
262
      before the dump is completely written.  Note that the application
234
263
      must be actively running for detection of the dump command. So,
235
 
      for a GUI application, resize the window or for a server send a
 
264
      for a GUI application, resize the window, or for a server, send a
236
265
      request.</para>
237
266
      <para>If you are using <ulink url="&cl-gui;">KCachegrind</ulink>
238
267
      for browsing of profile information, you can use the toolbar
248
277
    </listitem>
249
278
 
250
279
    <listitem>
251
 
      <para><command>Dumping at enter/leave of all functions whose name
252
 
      starts with</command> <emphasis>funcprefix</emphasis>.  Use the
253
 
      option <option><xref linkend="opt.dump-before"/>=funcprefix</option>
254
 
      and <option><xref linkend="opt.dump-after"/>=funcprefix</option>.
 
280
      <para><command>Dumping at enter/leave of specified functions.</command>
 
281
      Use the
 
282
      option <option><xref linkend="opt.dump-before"/>=function</option>
 
283
      and <option><xref linkend="opt.dump-after"/>=function</option>.
255
284
      To zero cost counters before entering a function, use
256
 
      <option><xref linkend="opt.zero-before"/>=funcprefix</option>.
257
 
      The prefix method for specifying function names was choosen to
258
 
      ease the use with C++: you don't have to specify full
259
 
      signatures.</para> <para>You can specify these options multiple
260
 
      times for different function prefixes.</para>
 
285
      <option><xref linkend="opt.zero-before"/>=function</option>.</para>
 
286
      <para>You can specify these options multiple times for different
 
287
      functions. Function specifications support wildcards: eg. use
 
288
      <option><xref linkend="opt.dump-before"/>='foo*'</option> to
 
289
      generate dumps before entering any function starting with 
 
290
      <emphasis>foo</emphasis>.</para>
261
291
    </listitem>
262
292
 
263
293
    <listitem>
315
345
  <para>In addition to enabling instrumentation, you must also enable
316
346
  event collection for the parts of your program you are interested in.
317
347
  By default, event collection is enabled everywhere.
318
 
  You can limit collection to specific function(s)
 
348
  You can limit collection to a specific function
319
349
  by using 
320
 
  <option><xref linkend="opt.toggle-collect"/>=funcprefix</option>. 
 
350
  <option><xref linkend="opt.toggle-collect"/>=function</option>. 
321
351
  This will toggle the collection state on entering and leaving
322
352
  the specified functions.
323
353
  When this option is in effect, the default collection state
324
354
  at program start is "off".  Only events happening while running
325
 
  inside of functions starting with <emphasis>funcprefix</emphasis> will
326
 
  be collected. Recursive
327
 
  calls of functions with <emphasis>funcprefix</emphasis> do not trigger
328
 
  any action.</para>
 
355
  inside of the given function will be collected. Recursive
 
356
  calls of the given function do not trigger any action.</para>
329
357
 
330
358
  <para>It is important to note that with instrumentation switched off, the
331
359
  cache simulator cannot see any memory access events, and thus, any
335
363
  probably leading to many <emphasis>cold misses</emphasis>
336
364
  which would not have happened in reality. If you do not want to see these,
337
365
  start event collection a few million instructions after you have switched
338
 
  on instrumentation</para>.
 
366
  on instrumentation.</para>
339
367
 
340
368
 
341
369
  </sect2>
345
373
  <sect2 id="cl-manual.cycles" xreflabel="Avoiding cycles">
346
374
  <title>Avoiding cycles</title>
347
375
 
348
 
  <para>Each group of functions with any two of them happening to have a
349
 
  call chain from one to the other, is called a cycle.  For example,
350
 
  with A calling B, B calling C, and C calling A, the three functions
351
 
  A,B,C build up one cycle.</para>
352
 
 
353
 
  <para>If a call chain goes multiple times around inside of a cycle,
354
 
  with profiling, you can not distinguish event counts coming from the
355
 
  first round or the second. Thus, it makes no sense to attach any inclusive
356
 
  cost to a call among functions inside of one cycle.
357
 
  If "A &gt; B" appears multiple times in a call chain, you
358
 
  have no way to partition the one big sum of all appearances of "A &gt;
359
 
  B".  Thus, for profile data presentation, all functions of a cycle are
360
 
  seen as one big virtual function.</para>
361
 
 
362
 
  <para>Unfortunately, if you have an application using some callback
363
 
  mechanism (like any GUI program), or even with normal polymorphism (as
364
 
  in OO languages like C++), it's quite possible to get large cycles.
365
 
  As it is often impossible to say anything about performance behaviour
366
 
  inside of cycles, it is useful to introduce some mechanisms to avoid
367
 
  cycles in call graphs.  This is done by treating the same
368
 
  function in different ways, depending on the current execution
369
 
  context, either by giving them different names, or by ignoring calls to
370
 
  functions.</para>
371
 
 
372
 
  <para>There is an option to ignore calls to a function with
373
 
  <option><xref linkend="opt.fn-skip"/>=funcprefix</option>.  E.g., you
374
 
  usually do not want to see the trampoline functions in the PLT sections
 
376
  <para>Informally speaking, a cycle is a group of functions which
 
377
  call each other in a recursive way.</para>
 
378
 
 
379
  <para>Formally speaking, a cycle is a nonempty set S of functions,
 
380
  such that for every pair of functions F and G in S, it is possible
 
381
  to call from F to G (possibly via intermediate functions) and also
 
382
  from G to F.  Furthermore, S must be maximal -- that is, be the
 
383
  largest set of functions satisfying this property.  For example, if
 
384
  a third function H is called from inside S and calls back into S,
 
385
  then H is also part of the cycle and should be included in S.</para>
 
386
 
 
387
  <para>Recursion is quite usual in programs, and therefore, cycles
 
388
  sometimes appear in the call graph output of Callgrind. However,
 
389
  the title of this chapter should raise two questions: What is bad
 
390
  about cycles which makes you want to avoid them? And: How can
 
391
  cycles be avoided without changing program code?</para>
 
392
 
 
393
  <para>Cycles are not bad in itself, but tend to make performance
 
394
  analysis of your code harder. This is because inclusive costs
 
395
  for calls inside of a cycle are meaningless. The definition of
 
396
  inclusive cost, ie. self cost of a function plus inclusive cost
 
397
  of its callees, needs a topological order among functions. For
 
398
  cycles, this does not hold true: callees of a function in a cycle include
 
399
  the function itself. Therefore, KCachegrind does cycle detection
 
400
  and skips visualization of any inclusive cost for calls inside
 
401
  of cycles. Further, all functions in a cycle are collapsed into artifical
 
402
  functions called like <computeroutput>Cycle 1</computeroutput>.</para>
 
403
 
 
404
  <para>Now, when a program exposes really big cycles (as is
 
405
  true for some GUI code, or in general code using event or callback based
 
406
  programming style), you loose the nice property to let you pinpoint
 
407
  the bottlenecks by following call chains from
 
408
  <computeroutput>main()</computeroutput>, guided via
 
409
  inclusive cost. In addition, KCachegrind looses its ability to show
 
410
  interesting parts of the call graph, as it uses inclusive costs to
 
411
  cut off uninteresting areas.</para>
 
412
 
 
413
  <para>Despite the meaningless of inclusive costs in cycles, the big
 
414
  drawback for visualization motivates the possibility to temporarily
 
415
  switch off cycle detection in KCachegrind, which can lead to
 
416
  misguiding visualization. However, often cycles appear because of
 
417
  unlucky superposition of independent call chains in a way that
 
418
  the profile result will see a cycle. Neglecting uninteresting
 
419
  calls with very small measured inclusive cost would break these
 
420
  cycles. In such cases, incorrect handling of cycles by not detecting
 
421
  them still gives meaningful profiling visualization.</para>
 
422
 
 
423
  <para>It has to be noted that currently, <command>callgrind_annotate</command>
 
424
  does not do any cycle detection at all. For program executions with function
 
425
  recursion, it e.g. can print nonsense inclusive costs way above 100%.</para>
 
426
 
 
427
  <para>After describing why cycles are bad for profiling, it is worth
 
428
  talking about cycle avoidance. The key insight here is that symbols in
 
429
  the profile data do not have to exactly match the symbols found in the
 
430
  program. Instead, the symbol name could encode additional information
 
431
  from the current execution context such as recursion level of the
 
432
  current function, or even some part of the call chain leading to the
 
433
  function. While encoding of additional information into symbols is
 
434
  quite capable of avoiding cycles, it has to be used carefully to not cause
 
435
  symbol explosion. The latter imposes large memory requirement for Callgrind
 
436
  with possible out-of-memory conditions, and big profile data files.</para>
 
437
 
 
438
  <para>A further possibility to avoid cycles in Callgrind's profile data
 
439
  output is to simply leave out given functions in the call graph. Of course, this
 
440
  also skips any call information from and to an ignored function, and thus can
 
441
  break a cycle. Candidates for this typically are dispatcher functions in event
 
442
  driven code. The option to ignore calls to a function is
 
443
  <option><xref linkend="opt.fn-skip"/>=function</option>. Aside from
 
444
  possibly breaking cycles, this is used in Callgrind to skip
 
445
  trampoline functions in the PLT sections
375
446
  for calls to functions in shared libraries. You can see the difference
376
447
  if you profile with <option><xref linkend="opt.skip-plt"/>=no</option>.
377
 
  If a call is ignored, cost events happening will be attached to the
 
448
  If a call is ignored, its cost events will be propagated to the
378
449
  enclosing function.</para>
379
450
 
380
451
  <para>If you have a recursive function, you can distinguish the first
381
452
  10 recursion levels by specifying
382
 
  <option><xref linkend="opt.fn-recursion-num"/>=funcprefix</option>.  
 
453
  <option><xref linkend="opt.separate-recs-num"/>=function</option>.  
383
454
  Or for all functions with 
384
 
  <option><xref linkend="opt.fn-recursion"/>=10</option>, but this will 
 
455
  <option><xref linkend="opt.separate-recs"/>=10</option>, but this will 
385
456
  give you much bigger profile data files.  In the profile data, you will see
386
457
  the recursion levels of "func" as the different functions with names
387
458
  "func", "func'2", "func'3" and so on.</para>
388
459
 
389
460
  <para>If you have call chains "A &gt; B &gt; C" and "A &gt; C &gt; B"
390
461
  in your program, you usually get a "false" cycle "B &lt;&gt; C". Use 
391
 
  <option><xref linkend="opt.fn-caller-num"/>=B</option> 
392
 
  <option><xref linkend="opt.fn-caller-num"/>=C</option>,
 
462
  <option><xref linkend="opt.separate-callers-num"/>=B</option> 
 
463
  <option><xref linkend="opt.separate-callers-num"/>=C</option>,
393
464
  and functions "B" and "C" will be treated as different functions 
394
465
  depending on the direct caller. Using the apostrophe for appending 
395
466
  this "context" to the function name, you get "A &gt; B'A &gt; C'B" 
396
467
  and "A &gt; C'A &gt; B'C", and there will be no cycle. Use 
397
 
  <option><xref linkend="opt.fn-caller"/>=3</option> to get a 2-caller 
 
468
  <option><xref linkend="opt.separate-callers"/>=2</option> to get a 2-caller 
398
469
  dependency for all functions.  Note that doing this will increase
399
470
  the size of profile data files.</para>
400
471
 
407
478
<title>Command line option reference</title>
408
479
 
409
480
<para>
410
 
In the following, options are grouped into classes, in same order as
411
 
the output as <computeroutput>callgrind --help</computeroutput>.
 
481
In the following, options are grouped into classes, in the same order as
 
482
the output of <computeroutput>callgrind --help</computeroutput>.
412
483
</para>
 
484
<para>
 
485
Some options allow the specification of a function/symbol name, such as
 
486
<option><xref linkend="opt.dump-before"/>=function</option>, or
 
487
<option><xref linkend="opt.fn-skip"/>=function</option>. All these options
 
488
can be specified multiple times for different functions.
 
489
In addition, the function specifications actually are patterns by supporting
 
490
the use of wildcards '*' (zero or more arbitrary characters) and '?'
 
491
(exactly one arbitrary character), similar to file name globbing in the
 
492
shell. This feature is important especially for C++, as without wildcard
 
493
usage, the function would have to be specified in full extent, including
 
494
parameter signature. </para>
413
495
 
414
496
<sect2 id="cl-manual.options.misc" 
415
497
       xreflabel="Miscellaneous options">
416
498
<title>Miscellaneous options</title>
417
499
 
418
 
<variablelist id="cmd-options.misc">
 
500
<variablelist id="cl.opts.list.misc">
419
501
 
420
502
  <varlistentry>
421
503
    <term><option>--help</option></term>
443
525
These options influence the name and format of the profile data files.
444
526
</para>
445
527
 
446
 
<variablelist id="cmd-options.creation">
 
528
<variablelist id="cl.opts.list.creation">
447
529
 
448
 
  <varlistentry id="opt.base">
 
530
  <varlistentry id="opt.callgrind-out-file" xreflabel="--callgrind-out-file">
449
531
    <term>
450
 
      <option><![CDATA[--base=<prefix> [default: callgrind.out] ]]></option>
 
532
      <option><![CDATA[--callgrind-out-file=<file> ]]></option>
451
533
    </term>
452
534
    <listitem>
453
 
      <para>Specify the base name for the dump file names. To
454
 
      distinguish different profile runs of the same application,
455
 
      <computeroutput>.&lt;pid&gt;</computeroutput> is appended to the
456
 
      base dump file name with
457
 
      <computeroutput>&lt;pid&gt;</computeroutput> being the process ID
458
 
      of the profile run (with multiple dumps happening, the file name
459
 
      is modified further; see below).</para> <para>This option is
460
 
      especially usefull if your application changes its working
461
 
      directory.  Usually, the dump file is generated in the current
462
 
      working directory of the application at program termination.  By
463
 
      giving an absolute path with the base specification, you can force
464
 
      a fixed directory for the dump files.</para>
 
535
      <para>Write the profile data to
 
536
            <computeroutput>file</computeroutput> rather than to the default
 
537
            output file,
 
538
            <computeroutput>callgrind.out.&lt;pid&gt;</computeroutput>.  The
 
539
            <option>%p</option> and <option>%q</option> format specifiers
 
540
            can be used to embed the process ID and/or the contents of an
 
541
            environment variable in the name, as is the case for the core
 
542
            option <option>--log-file</option>.  See <link
 
543
            linkend="manual-core.basicopts">here</link> for details.
 
544
            When multiple dumps are made, the file name
 
545
            is modified further; see below.</para> 
465
546
    </listitem>
466
547
  </varlistentry>
467
548
 
472
553
    <listitem>
473
554
      <para>This specifies that event counting should be performed at
474
555
      per-instruction granularity.
475
 
      This allows for assembler code
476
 
      annotation, but currently the results can only be shown with KCachegrind.</para>
 
556
      This allows for assembly code
 
557
      annotation.  Currently the results can only be 
 
558
      displayed by KCachegrind.</para>
477
559
  </listitem>
478
560
  </varlistentry>
479
561
 
495
577
    <listitem>
496
578
      <para>This option influences the output format of the profile data.
497
579
      It specifies whether strings (file and function names) should be
498
 
      identified by numbers. This shrinks the file size, but makes it more difficult
499
 
      for humans to read (which is not recommand either way).</para>
500
 
      <para>However, this currently has to be switched off if
501
 
      the files are to be read by
502
 
      <computeroutput>callgrind_annotate</computeroutput>!</para>
 
580
      identified by numbers. This shrinks the file, 
 
581
      but makes it more difficult
 
582
      for humans to read (which is not recommended in any case).</para>
503
583
    </listitem>
504
584
  </varlistentry>
505
585
 
512
592
      It specifies whether numerical positions are always specified as absolute
513
593
      values or are allowed to be relative to previous numbers.
514
594
      This shrinks the file size,</para>
515
 
      <para>However, this currently has to be switched off if
516
 
      the files are to be read by
517
 
      <computeroutput>callgrind_annotate</computeroutput>!</para>
518
595
    </listitem>
519
596
  </varlistentry>
520
597
 
525
602
    <listitem>
526
603
      <para>When multiple profile data parts are to be generated, these
527
604
      parts are appended to the same output file if this option is set to
528
 
      "yes". Not recommand.</para>
 
605
      "yes". Not recommended.</para>
529
606
  </listitem>
530
607
  </varlistentry>
531
608
 
542
619
<computeroutput>callgrind_control</computeroutput>.
543
620
</para>
544
621
 
545
 
<variablelist id="cmd-options.activity">
 
622
<variablelist id="cl.opts.list.activity">
546
623
 
547
624
  <varlistentry id="opt.dump-every-bb" xreflabel="--dump-every-bb">
548
625
    <term>
550
627
    </term>
551
628
    <listitem>
552
629
      <para>Dump profile data every &lt;count&gt; basic blocks.
553
 
      Whether a dump is needed is only checked when Valgrinds internal
 
630
      Whether a dump is needed is only checked when Valgrind's internal
554
631
      scheduler is run. Therefore, the minimum setting useful is about 100000.
555
632
      The count is a 64-bit value to make long dump periods possible.
556
633
      </para>
559
636
 
560
637
  <varlistentry id="opt.dump-before" xreflabel="--dump-before">
561
638
    <term>
562
 
      <option><![CDATA[--dump-before=<prefix> ]]></option>
 
639
      <option><![CDATA[--dump-before=<function> ]]></option>
563
640
    </term>
564
641
    <listitem>
565
 
      <para>Dump when entering a function starting with &lt;prefix&gt;</para>
 
642
      <para>Dump when entering &lt;function&gt;</para>
566
643
    </listitem>
567
644
  </varlistentry>
568
645
 
569
646
  <varlistentry id="opt.zero-before" xreflabel="--zero-before">
570
647
    <term>
571
 
      <option><![CDATA[--zero-before=<prefix> ]]></option>
 
648
      <option><![CDATA[--zero-before=<function> ]]></option>
572
649
    </term>
573
650
    <listitem>
574
 
      <para>Zero all costs when entering a function starting with &lt;prefix&gt;</para>
 
651
      <para>Zero all costs when entering &lt;function&gt;</para>
575
652
    </listitem>
576
653
  </varlistentry>
577
654
 
578
655
  <varlistentry id="opt.dump-after" xreflabel="--dump-after">
579
656
    <term>
580
 
      <option><![CDATA[--dump-after=<prefix> ]]></option>
 
657
      <option><![CDATA[--dump-after=<function> ]]></option>
581
658
    </term>
582
659
    <listitem>
583
 
      <para>Dump when leaving a function starting with &lt;prefix&gt;</para>
 
660
      <para>Dump when leaving &lt;function&gt;</para>
584
661
    </listitem>
585
662
  </varlistentry>
586
663
 
595
672
These options specify when events are to be aggregated into event counts.
596
673
Also see <xref linkend="cl-manual.limits"/>.</para>
597
674
 
598
 
<variablelist id="cmd-options.collection">
 
675
<variablelist id="cl.opts.list.collection">
599
676
 
600
677
  <varlistentry id="opt.instr-atstart" xreflabel="--instr-atstart">
601
678
    <term>
668
745
 
669
746
  <varlistentry id="opt.toggle-collect" xreflabel="--toggle-collect">
670
747
    <term>
671
 
      <option><![CDATA[--toggle-collect=<prefix> ]]></option>
 
748
      <option><![CDATA[--toggle-collect=<function> ]]></option>
672
749
    </term>
673
750
    <listitem>
674
 
      <para>Toggle collection on entry/exit of a function whose name
675
 
      starts with
676
 
      &lt;prefix&gt;.</para>
 
751
      <para>Toggle collection on entry/exit of &lt;function&gt;.</para>
677
752
    </listitem>
678
753
  </varlistentry>
679
754
 
680
 
  <varlistentry id="opt.collect-jumps" xreflabel="--collect-jumps=">
 
755
  <varlistentry id="opt.collect-jumps" xreflabel="--collect-jumps">
681
756
    <term>
682
757
      <option><![CDATA[--collect-jumps=<no|yes> [default: no] ]]></option>
683
758
    </term>
699
774
<para>
700
775
These options specify how event counts should be attributed to execution
701
776
contexts.
702
 
More specifically, they specify e.g. if the recursion level or the
703
 
call chain leading to a function should be accounted for, and whether the
704
 
thread ID should be remembered.
 
777
For example, they specify whether the recursion level or the
 
778
call chain leading to a function should be taken into account, 
 
779
and whether the thread ID should be considered.
705
780
Also see <xref linkend="cl-manual.cycles"/>.</para>
706
781
 
707
782
<variablelist id="cmd-options.separation">
717
792
    </listitem>
718
793
  </varlistentry>
719
794
 
720
 
  <varlistentry id="opt.fn-recursion" xreflabel="--fn-recursion">
 
795
  <varlistentry id="opt.separate-recs" xreflabel="--separate-recs">
721
796
    <term>
722
 
      <option><![CDATA[--fn-recursion=<level> [default: 2] ]]></option>
 
797
      <option><![CDATA[--separate-recs=<level> [default: 2] ]]></option>
723
798
    </term>
724
799
    <listitem>
725
 
      <para>Separate function recursions, maximal &lt;level&gt;.
 
800
      <para>Separate function recursions by at most &lt;level&gt; levels.
726
801
      See <xref linkend="cl-manual.cycles"/>.</para>
727
802
    </listitem>
728
803
  </varlistentry>
729
804
 
730
 
  <varlistentry id="opt.fn-caller" xreflabel="--fn-caller">
 
805
  <varlistentry id="opt.separate-callers" xreflabel="--separate-callers">
731
806
    <term>
732
 
      <option><![CDATA[--fn-caller=<callers> [default: 0] ]]></option>
 
807
      <option><![CDATA[--separate-callers=<callers> [default: 0] ]]></option>
733
808
    </term>
734
809
    <listitem>
735
 
      <para>Separate contexts by maximal &lt;callers&gt; functions in the
 
810
      <para>Separate contexts by at most &lt;callers&gt; functions in the
736
811
      call chain. See <xref linkend="cl-manual.cycles"/>.</para>
737
812
    </listitem>
738
813
  </varlistentry>
755
830
      call chain A &gt; B &gt; C, and you specify function B to be
756
831
      ignored, you will only see A &gt; C.</para>
757
832
      <para>This is very convenient to skip functions handling callback
758
 
      behaviour. E.g. for the SIGNAL/SLOT mechanism in QT, you only want
 
833
      behaviour.  For example, with the signal/slot mechanism in the
 
834
      Qt graphics library, you only want
759
835
      to see the function emitting a signal to call the slots connected
760
836
      to that signal. First, determine the real call chain to see the
761
837
      functions needed to be skipped, then use this option.</para>
768
844
    </term>
769
845
    <listitem>
770
846
      <para>Put a function into a separate group. This influences the
771
 
      context name for cycle avoidance. All functions inside of such a
 
847
      context name for cycle avoidance. All functions inside such a
772
848
      group are treated as being the same for context name building, which
773
849
      resembles the call chain leading to a context. By specifying function
774
850
      groups with this option, you can shorten the context name, as functions
776
852
    </listitem>
777
853
  </varlistentry>
778
854
  
779
 
  <varlistentry id="opt.fn-recursion-num" xreflabel="--fn-recursion10">
 
855
  <varlistentry id="opt.separate-recs-num" xreflabel="--separate-recs10">
780
856
    <term>
781
 
      <option><![CDATA[--fn-recursion<number>=<function> ]]></option>
 
857
      <option><![CDATA[--separate-recs<number>=<function> ]]></option>
782
858
    </term>
783
859
    <listitem>
784
860
      <para>Separate &lt;number&gt; recursions for &lt;function&gt;.
786
862
    </listitem>
787
863
  </varlistentry>
788
864
 
789
 
  <varlistentry id="opt.fn-caller-num" xreflabel="--fn-caller2">
 
865
  <varlistentry id="opt.separate-callers-num" xreflabel="--separate-callers2">
790
866
    <term>
791
 
      <option><![CDATA[--fn-caller<number>=<function> ]]></option>
 
867
      <option><![CDATA[--separate-callers<number>=<function> ]]></option>
792
868
    </term>
793
869
    <listitem>
794
870
      <para>Separate &lt;number&gt; callers for &lt;function&gt;.
803
879
       xreflabel="Cache simulation options">
804
880
<title>Cache simulation options</title>
805
881
 
806
 
<variablelist id="cmd-options.simulation">
 
882
<variablelist id="cl.opts.list.simulation">
807
883
  
808
884
  <varlistentry id="opt.simulate-cache" xreflabel="--simulate-cache">
809
885
    <term>
814
890
      only instruction read accesses will be profiled.</para>
815
891
    </listitem>
816
892
  </varlistentry>
817
 
  
 
893
 
 
894
  <varlistentry id="opt.simulate-hwpref" xreflabel="--simulate-hwpref">
 
895
    <term>
 
896
      <option><![CDATA[--simulate-hwpref=<yes|no> [default: no] ]]></option>
 
897
    </term>
 
898
    <listitem>
 
899
      <para>Specify whether simulation of a hardware prefetcher should be
 
900
      added which is able to detect stream access in the second level cache
 
901
      by comparing accesses to separate to each page.
 
902
      As the simulation can not decide about any timing issues of prefetching,
 
903
      it is assumed that any hardware prefetch triggered succeeds before a
 
904
      real access is done. Thus, this gives a best-case scenario by covering
 
905
      all possible stream accesses.</para>
 
906
    </listitem>
 
907
  </varlistentry>
 
908
 
818
909
</variablelist>
819
910
 
820
911
</sect2>