~ubuntu-branches/ubuntu/jaunty/google-perftools/jaunty

« back to all changes in this revision

Viewing changes to doc/cpuprofile.html

  • Committer: Bazaar Package Importer
  • Author(s): Daigo Moriwaki
  • Date: 2008-06-15 23:41:36 UTC
  • mfrom: (3.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20080615234136-al5gawvdvt5vhdtz
Tags: 0.98-1
* New upstream release. (Closes: #425147)
* Compiled with GCC 4.3. (Closes: #454841)
* debian/watch: can now report upstream's version (Closes: #450294)
* Because of a file conflict between tau and libgoogle-perftools the
  binary pprof is renamed as google-pprof. (Closes: #404001)
  Great thanks to Michael Mende.
* debian/rules: autoconf files are now generated at the build time.
* Bumped up Standards-Version to 3.7.3, no changes are required.
* Split a new package, libtcmallc_minimal0. The upstream supports
  this module for wider platforms. So I leave its architecture to be
  `any'.
* libgoogle-perftools0's architecture is now i386. The upstream
  supports this module for x86 and x86_64. However, x86_64 requires
  libunwind's development head, which Debian does not have yet.
* Removed an unnecessary patch, debian/patches/02_profiler.cc_alpha.diff.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
 
2
<HTML>
 
3
 
 
4
<HEAD>
 
5
  <link rel="stylesheet" href="designstyle.css">
 
6
  <title>Google CPU Profiler</title>
 
7
</HEAD>
 
8
 
 
9
<BODY>
 
10
 
 
11
<p align=right>
 
12
  <i>Last modified
 
13
  <script type=text/javascript>
 
14
    var lm = new Date(document.lastModified);
 
15
    document.write(lm.toDateString());
 
16
  </script></i>
 
17
</p>
 
18
 
 
19
<p>This is the CPU profiler we use at Google.  There are three parts
 
20
to using it: linking the library into an application, running the
 
21
code, and analyzing the output.</p>
 
22
 
 
23
<p>On the off-chance that you should need to understand it, the CPU
 
24
profiler data file format is documented separately,
 
25
<a href="cpuprofile-fileformat.html">here</a>.
 
26
 
 
27
 
 
28
<H1>Linking in the Library</H1>
 
29
 
 
30
<p>To install the CPU profiler into your executable, add
 
31
<code>-lprofiler</code> to the link-time step for your executable.
 
32
(It's also probably possible to add in the profiler at run-time using
 
33
<code>LD_PRELOAD</code>, e.g.
 
34
<code>% env LD_PRELOAD="/usr/lib/libprofiler.so" &lt;binary&gt;</code>,
 
35
but this isn't necessarily recommended.)</p>
 
36
 
 
37
<p>This does <i>not</i> turn on CPU profiling; it just inserts the
 
38
code.  For that reason, it's practical to just always link
 
39
<code>-lprofiler</code> into a binary while developing; that's what we
 
40
do at Google.  (However, since any user can turn on the profiler by
 
41
setting an environment variable, it's not necessarily recommended to
 
42
install profiler-linked binaries into a production, running
 
43
system.)</p>
 
44
 
 
45
 
 
46
<H1>Running the Code</H1>
 
47
 
 
48
<p>There are several alternatives to actually turn on CPU profiling
 
49
for a given run of an executable:</p>
 
50
 
 
51
<ol>
 
52
  <li> <p>Define the environment variable CPUPROFILE to the filename
 
53
       to dump the profile to.  For instance, to profile
 
54
       <code>/usr/local/bin/my_binary_compiled_with_libprofiler_so</code>:</p>
 
55
       <pre>% env CPUPROFILE=/tmp/mybin.prof /usr/local/bin/my_binary_compiled_with_libprofiler_so</pre>
 
56
 
 
57
  <li> <p>In your code, bracket the code you want profiled in calls to
 
58
       <code>ProfilerStart()</code> and <code>ProfilerStop()</code>.
 
59
       (These functions are declared in <code>&lt;google/profiler.h&gt;</code>.)
 
60
       <code>ProfilerStart()</code> will take
 
61
       the profile-filename as an argument.</p>
 
62
</ol>
 
63
 
 
64
<p>In Linux 2.6 and above, profiling works correctly with threads,
 
65
automatically profiling all threads.  In Linux 2.4, profiling only
 
66
profiles the main thread (due to a kernel bug involving itimers and
 
67
threads).  Profiling works correctly with sub-processes: each child
 
68
process gets its own profile with its own name (generated by combining
 
69
CPUPROFILE with the child's process id).</p>
 
70
 
 
71
<p>For security reasons, CPU profiling will not write to a file -- and
 
72
is thus not usable -- for setuid programs.</p>
 
73
 
 
74
<H2>Modifying Runtime Behavior</H2>
 
75
 
 
76
<p>You can more finely control the behavior of the CPU profiler via
 
77
environment variables.</p>
 
78
 
 
79
<table frame=box rules=sides cellpadding=5 width=100%>
 
80
 
 
81
<tr valign=top>
 
82
  <td><code>CPUPROFILE_FREQUENCY=<i>x</i></code></td>
 
83
  <td>default: 100</td>
 
84
  <td>
 
85
    How many interrupts/second the cpu-profiler samples.
 
86
  </td>
 
87
</tr>
 
88
 
 
89
</table>
 
90
 
 
91
 
 
92
<h1><a name="pprof">Analyzing the Output</a></h1>
 
93
 
 
94
<p><code>pprof</code> is the script used to analyze a profile.  It has
 
95
many output modes, both textual and graphical.  Some give just raw
 
96
numbers, much like the <code>-pg</code> output of <code>gcc</code>,
 
97
and others show the data in the form of a dependency graph.</p>
 
98
 
 
99
<p>pprof <b>requires</b> <code>perl5</code> to be installed to run.
 
100
It also requires <code>dot</code> to be installed for any of the
 
101
graphical output routines, and <code>gv</code> to be installed for
 
102
<code>--gv</code> mode (described below).
 
103
</p>
 
104
 
 
105
<p>Here are some ways to call pprof.  These are described in more
 
106
detail below.</p>
 
107
 
 
108
<pre>
 
109
% pprof /bin/ls ls.prof
 
110
                       Enters "interactive" mode
 
111
% pprof --text /bin/ls ls.prof
 
112
                       Outputs one line per procedure
 
113
% pprof --gv /bin/ls ls.prof
 
114
                       Displays annotated call-graph via 'gv'
 
115
% pprof --gv --focus=Mutex /bin/ls ls.prof
 
116
                       Restricts to code paths including a .*Mutex.* entry
 
117
% pprof --gv --focus=Mutex --ignore=string /bin/ls ls.prof
 
118
                       Code paths including Mutex but not string
 
119
% pprof --list=getdir /bin/ls ls.prof
 
120
                       (Per-line) annotated source listing for getdir()
 
121
% pprof --disasm=getdir /bin/ls ls.prof
 
122
                       (Per-PC) annotated disassembly for getdir()
 
123
% pprof --text localhost:1234
 
124
                       Outputs one line per procedure for localhost:1234
 
125
% pprof --callgrind /bin/ls ls.prof
 
126
                       Outputs the call information in callgrind format
 
127
</pre>
 
128
 
 
129
 
 
130
<h3>Analyzing Text Output</h3>
 
131
 
 
132
<p>Text mode has lines of output that look like this:</p>
 
133
<pre>
 
134
       14   2.1%  17.2%       58   8.7% std::_Rb_tree::find
 
135
</pre>
 
136
 
 
137
<p>Here is how to interpret the columns:</p>
 
138
<ol>
 
139
  <li> Number of profiling samples in this function
 
140
  <li> Percentage of profiling samples in this function
 
141
  <li> Percentage of profiling samples in the functions printed so far
 
142
  <li> Number of profiling samples in this function and its callees
 
143
  <li> Percentage of profiling samples in this function and its callees
 
144
  <li> Function name
 
145
</ol>
 
146
 
 
147
<h3>Analyzing Callgrind Output</h3>
 
148
 
 
149
<p>Use <a href="http://kcachegrind.sourceforge.net">kcachegrind</a> to 
 
150
analyze your callgrind output:</p>
 
151
<pre>
 
152
% pprof --callgrind /bin/ls ls.prof > ls.callgrind
 
153
% kcachegrind ls.callgrind
 
154
</pre>
 
155
 
 
156
<p>The cost is specified in 'hits', i.e. how many times a function
 
157
appears in the recorded call stack information. The 'calls' from
 
158
function a to b record how many times function b was found in the
 
159
stack traces directly below function a.</p>
 
160
 
 
161
<p>Tip: if you use a debug build the output will include file and line
 
162
number information and kcachegrind will show an annotated source
 
163
code view.</p>
 
164
 
 
165
<h3>Node Information</h3>
 
166
 
 
167
<p>In the various graphical modes of pprof, the output is a call graph
 
168
annotated with timing information, like so:</p>
 
169
 
 
170
<A HREF="pprof-test-big.gif">
 
171
<center><table><tr><td>
 
172
   <img src="pprof-test.gif">
 
173
</td></tr></table></center>
 
174
</A>
 
175
 
 
176
<p>Each node represents a procedure.  The directed edges indicate
 
177
caller to callee relations.  Each node is formatted as follows:</p>
 
178
 
 
179
<center><pre>
 
180
Class Name
 
181
Method Name
 
182
local (percentage)
 
183
<b>of</b> cumulative (percentage)
 
184
</pre></center>
 
185
 
 
186
<p>The last one or two lines contains the timing information.  (The
 
187
profiling is done via a sampling method, where by default we take 100
 
188
samples a second.  Therefor one unit of time in the output corresponds
 
189
to about 10 milliseconds of execution time.) The "local" time is the
 
190
time spent executing the instructions directly contained in the
 
191
procedure (and in any other procedures that were inlined into the
 
192
procedure).  The "cumulative" time is the sum of the "local" time and
 
193
the time spent in any callees.  If the cumulative time is the same as
 
194
the local time, it is not printed.</p>
 
195
 
 
196
<p>For instance, the timing information for test_main_thread()
 
197
indicates that 155 units (about 1.55 seconds) were spent executing the
 
198
code in <code>test_main_thread()</code> and 200 units were spent while
 
199
executing <code>test_main_thread()</code> and its callees such as
 
200
<code>snprintf()</code>.</p>
 
201
 
 
202
<p>The size of the node is proportional to the local count.  The
 
203
percentage displayed in the node corresponds to the count divided by
 
204
the total run time of the program (that is, the cumulative count for
 
205
<code>main()</code>).</p>
 
206
 
 
207
<h3>Edge Information</h3>
 
208
 
 
209
<p>An edge from one node to another indicates a caller to callee
 
210
relationship.  Each edge is labelled with the time spent by the callee
 
211
on behalf of the caller.  E.g, the edge from
 
212
<code>test_main_thread()</code> to <code>snprintf()</code> indicates
 
213
that of the 200 samples in <code>test_main_thread()</code>, 37 are
 
214
because of calls to <code>snprintf()</code>.</p>
 
215
 
 
216
<p>Note that <code>test_main_thread()</code> has an edge to
 
217
<code>vsnprintf()</code>, even though <code>test_main_thread()</code>
 
218
doesn't call that function directly.  This is because the code was
 
219
compiled with <code>-O2</code>; the profile reflects the optimized
 
220
control flow.</p>
 
221
 
 
222
<h3>Meta Information</h3>
 
223
 
 
224
<p>The top of the display should contain some meta information
 
225
like:</p>
 
226
<pre>
 
227
      /tmp/profiler2_unittest
 
228
      Total samples: 202
 
229
      Focusing on: 202
 
230
      Dropped nodes with &lt;= 1 abs(samples)
 
231
      Dropped edges with &lt;= 0 samples
 
232
</pre>
 
233
 
 
234
<p>This section contains the name of the program, and the total
 
235
samples collected during the profiling run.  If the
 
236
<code>--focus</code> option is on (see the <a href="#focus">Focus</a>
 
237
section below), the legend also contains the number of samples being
 
238
shown in the focused display.  Furthermore, some unimportant nodes and
 
239
edges are dropped to reduce clutter.  The characteristics of the
 
240
dropped nodes and edges are also displayed in the legend.</p>
 
241
 
 
242
<h3><a name=focus>Focus and Ignore</a></h3>
 
243
 
 
244
<p>You can ask pprof to generate a display focused on a particular
 
245
piece of the program.  You specify a regular expression.  Any portion
 
246
of the call-graph that is on a path which contains at least one node
 
247
matching the regular expression is preserved.  The rest of the
 
248
call-graph is dropped on the floor.  For example, you can focus on the
 
249
<code>vsnprintf()</code> libc call in <code>profiler2_unittest</code>
 
250
as follows:</p>
 
251
 
 
252
<pre>
 
253
% pprof --gv --focus=vsnprintf /tmp/profiler2_unittest test.prof
 
254
</pre>
 
255
<A HREF="pprof-vsnprintf-big.gif">
 
256
<center><table><tr><td>
 
257
   <img src="pprof-vsnprintf.gif">
 
258
</td></tr></table></center>
 
259
</A>
 
260
 
 
261
<p>Similarly, you can supply the <code>--ignore</code> option to
 
262
ignore samples that match a specified regular expression.  E.g., if
 
263
you are interested in everything except calls to
 
264
<code>snprintf()</code>, you can say:</p>
 
265
<pre>
 
266
% pprof --gv --ignore=snprintf /tmp/profiler2_unittest test.prof
 
267
</pre>
 
268
 
 
269
 
 
270
<h3>Interactive mode</a></h3>
 
271
 
 
272
<p>By default -- if you don't specify any flags to the contrary --
 
273
pprof runs in interactive mode.  At the <code>(pprof)</code> prompt,
 
274
you can run many of the commands described above.  You can type
 
275
<code>help</code> for a list of what commands are available in
 
276
interactive mode.</p>
 
277
 
 
278
<h3><a name=options>pprof Options</a></h3>
 
279
 
 
280
For a complete list of pprof options, you can run <code>pprof
 
281
--help</code>.
 
282
 
 
283
<h4>Output Type</h4>
 
284
 
 
285
<p>
 
286
<center>
 
287
<table frame=box rules=sides cellpadding=5 width=100%>
 
288
<tr valign=top>
 
289
  <td><code>--text</code></td>
 
290
  <td>
 
291
    Produces a textual listing.  (Note: If you have an X display, and
 
292
    <code>dot</code> and <code>gv</code> installed, you will probably
 
293
    be happier with the <code>--gv</code> output.)
 
294
  </td>
 
295
</tr>
 
296
<tr valign=top>
 
297
  <td><code>--gv</code></td>
 
298
  <td>
 
299
    Generates annotated call-graph, converts to postscript, and
 
300
    displays via gv (requres <code>dot</code> and <code>gv</code> be
 
301
    installed).
 
302
  </td>
 
303
</tr>
 
304
<tr valign=top>
 
305
  <td><code>--dot</code></td>
 
306
  <td>
 
307
    Generates the annotated call-graph in dot format and
 
308
    emits to stdout (requres <code>dot</code> be installed).
 
309
  </td>
 
310
</tr>
 
311
<tr valign=top>
 
312
  <td><code>--ps</code></td>
 
313
  <td>
 
314
    Generates the annotated call-graph in Postscript format and
 
315
    emits to stdout (requres <code>dot</code> be installed).
 
316
  </td>
 
317
</tr>
 
318
<tr valign=top>
 
319
  <td><code>--pdf</code></td>
 
320
  <td>
 
321
    Generates the annotated call-graph in PDF format and emits to
 
322
    stdout (requires <code>dot</code> and <code>ps2pdf</code> be
 
323
    installed).
 
324
  </td>
 
325
</tr>
 
326
<tr valign=top>
 
327
  <td><code>--gif</code></td>
 
328
  <td>
 
329
    Generates the annotated call-graph in GIF format and
 
330
    emits to stdout (requres <code>dot</code> be installed).
 
331
  </td>
 
332
</tr>
 
333
<tr valign=top>
 
334
  <td><code>--list=&lt;<i>regexp</i>&gt;</code></td>
 
335
  <td>
 
336
    <p>Outputs source-code listing of routines whose
 
337
    name matches &lt;regexp&gt;.  Each line
 
338
    in the listing is annotated with flat and cumulative
 
339
    sample counts.</p>
 
340
 
 
341
    <p>In the presence of inlined calls, the samples
 
342
    associated with inlined code tend to get assigned
 
343
    to a line that follows the location of the 
 
344
    inlined call.  A more precise accounting can be
 
345
    obtained by disassembling the routine using the
 
346
    --disasm flag.</p>
 
347
  </td>
 
348
</tr>
 
349
<tr valign=top>
 
350
  <td><code>--disasm=&lt;<i>regexp</i>&gt;</code></td>
 
351
  <td>
 
352
    Generates disassembly of routines that match
 
353
    &lt;regexp&gt;, annotated with flat and
 
354
    cumulative sample counts and emits to stdout.
 
355
  </td>
 
356
</tr>
 
357
</table>
 
358
</center>
 
359
 
 
360
<h4>Reporting Granularity</h4>
 
361
 
 
362
<p>By default, pprof produces one entry per procedure.  However you can
 
363
use one of the following options to change the granularity of the
 
364
output.  The <code>--files</code> option seems to be particularly
 
365
useless, and may be removed eventually.</p>
 
366
 
 
367
<center>
 
368
<table frame=box rules=sides cellpadding=5 width=100%>
 
369
<tr valign=top>
 
370
  <td><code>--addresses</code></td>
 
371
  <td>
 
372
     Produce one node per program address.
 
373
  </td>
 
374
</tr>
 
375
  <td><code>--lines</code></td>
 
376
  <td>
 
377
     Produce one node per source line.
 
378
  </td>
 
379
</tr>
 
380
  <td><code>--functions</code></td>
 
381
  <td>
 
382
     Produce one node per function (this is the default).
 
383
  </td>
 
384
</tr>
 
385
  <td><code>--files</code></td>
 
386
  <td>
 
387
     Produce one node per source file.
 
388
  </td>
 
389
</tr>
 
390
</table>
 
391
</center>
 
392
 
 
393
<h4>Controlling the Call Graph Display</h4>
 
394
 
 
395
<p>Some nodes and edges are dropped to reduce clutter in the output
 
396
display.  The following options control this effect:</p>
 
397
 
 
398
<center>
 
399
<table frame=box rules=sides cellpadding=5 width=100%>
 
400
<tr valign=top>
 
401
  <td><code>--nodecount=&lt;n&gt;</code></td>
 
402
  <td>
 
403
    This option controls the number of displayed nodes.  The nodes
 
404
    are first sorted by decreasing cumulative count, and then only
 
405
    the top N nodes are kept.  The default value is 80.
 
406
  </td>
 
407
</tr>
 
408
<tr valign=top>
 
409
  <td><code>--nodefraction=&lt;f&gt;</code></td>
 
410
  <td>
 
411
    This option provides another mechanism for discarding nodes
 
412
    from the display.  If the cumulative count for a node is
 
413
    less than this option's value multiplied by the total count
 
414
    for the profile, the node is dropped.  The default value
 
415
    is 0.005; i.e. nodes that account for less than
 
416
    half a percent of the total time are dropped.  A node
 
417
    is dropped if either this condition is satisfied, or the
 
418
    --nodecount condition is satisfied.
 
419
  </td>
 
420
</tr>
 
421
<tr valign=top>
 
422
  <td><code>--edgefraction=&lt;f&gt;</code></td>
 
423
  <td>
 
424
    This option controls the number of displayed edges.  First of all,
 
425
    an edge is dropped if either its source or destination node is
 
426
    dropped.  Otherwise, the edge is dropped if the sample
 
427
    count along the edge is less than this option's value multiplied
 
428
    by the total count for the profile.  The default value is
 
429
    0.001; i.e., edges that account for less than
 
430
    0.1% of the total time are dropped.
 
431
  </td>
 
432
</tr>
 
433
<tr valign=top>
 
434
  <td><code>--focus=&lt;re&gt;</code></td>
 
435
  <td>
 
436
    This option controls what region of the graph is displayed
 
437
    based on the regular expression supplied with the option.
 
438
    For any path in the callgraph, we check all nodes in the path
 
439
    against the supplied regular expression.  If none of the nodes
 
440
    match, the path is dropped from the output.
 
441
  </td>
 
442
</tr>
 
443
<tr valign=top>
 
444
  <td><code>--ignore=&lt;re&gt;</code></td>
 
445
  <td>
 
446
    This option controls what region of the graph is displayed
 
447
    based on the regular expression supplied with the option.
 
448
    For any path in the callgraph, we check all nodes in the path
 
449
    against the supplied regular expression.  If any of the nodes
 
450
    match, the path is dropped from the output.
 
451
  </td>
 
452
</tr>
 
453
</table>
 
454
</center>
 
455
 
 
456
<p>The dropped edges and nodes account for some count mismatches in
 
457
the display.  For example, the cumulative count for
 
458
<code>snprintf()</code> in the first diagram above was 41.  However
 
459
the local count (1) and the count along the outgoing edges (12+1+20+6)
 
460
add up to only 40.</p>
 
461
 
 
462
 
 
463
<h1>Caveats</h1>
 
464
 
 
465
<ul>
 
466
  <li> If the program exits because of a signal, the generated profile
 
467
       will be <font color=red>incomplete, and may perhaps be
 
468
       completely empty</font>.
 
469
  <li> The displayed graph may have disconnected regions because
 
470
       of the edge-dropping heuristics described above.
 
471
  <li> If the program linked in a library that was not compiled
 
472
       with enough symbolic information, all samples associated
 
473
       with the library may be charged to the last symbol found
 
474
       in the program before the library.  This will artificially
 
475
       inflate the count for that symbol.
 
476
  <li> If you run the program on one machine, and profile it on
 
477
       another, and the shared libraries are different on the two
 
478
       machines, the profiling output may be confusing: samples that
 
479
       fall within  shared libaries may be assigned to arbitrary
 
480
       procedures.
 
481
  <li> If your program forks, the children will also be profiled
 
482
       (since they inherit the same CPUPROFILE setting).  Each process
 
483
       is profiled separately; to distinguish the child profiles from
 
484
       the parent profile and from each other, all children will have
 
485
       their process-id appended to the CPUPROFILE name.
 
486
  <li> Due to a hack we make to work around a possible gcc bug, your
 
487
       profiles may end up named strangely if the first character of
 
488
       your CPUPROFILE variable has ascii value greater than 127.
 
489
       This should be exceedingly rare, but if you need to use such a
 
490
       name, just set prepend <code>./</code> to your filename:
 
491
       <code>CPUPROFILE=./&Auml;gypten</code>.
 
492
</ul>
 
493
 
 
494
 
 
495
<hr>
 
496
<address>Sanjay Ghemawat<br>
 
497
<!-- Created: Tue Dec 19 10:43:14 PST 2000 -->
 
498
<!-- hhmts start -->
 
499
Last modified: Fri May  9 14:41:29 PDT 2008
 
500
<!-- hhmts end -->
 
501
</address>
 
502
</BODY>
 
503
</HTML>