Imported Upstream version 1.1.0
[platform/upstream/oprofile.git] / doc / oprofile.xml
1 <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN" "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd">
2
3 <book id="oprofile-guide">
4 <bookinfo>
5         <title>OProfile manual</title>
6
7         <authorgroup>
8                 <author>
9                         <firstname>John</firstname>
10                         <surname>Levon</surname>
11                         <affiliation>
12                                 <address><email>levon@movementarian.org</email></address>
13                         </affiliation>
14                 </author>
15         </authorgroup>
16
17         <copyright>
18                 <year>2000-2004</year>
19                 <holder>Victoria University of Manchester, John Levon and others</holder>
20         </copyright>
21 </bookinfo>
22
23 <toc></toc>
24
25 <chapter id="introduction">
26 <title>Introduction</title>
27
28 <para>
29 This manual applies to OProfile version <oprofileversion />.
30 OProfile is a set of performance monitoring tools for Linux 2.6 and higher systems, available on a number of architectures.
31 OProfile provides the following features:
32 <itemizedlist>
33 <listitem>Profiler</listitem>
34 <listitem>Post-processing tools for analyzing profile data</listitem>
35 <listitem>Event counter</listitem>
36 </itemizedlist>
37 </para>
38 <para>
39 OProfile is capable of monitoring native hardware events occurring in all parts of a running system, from the kernel
40 (including modules and interrupt handlers) to shared libraries
41 to binaries. OProfile can collect event information for the whole system in the background with very little overhead. These
42 features make it ideal for monitoring entire systems to determine bottle necks in real-world systems.
43 </para>
44
45 <para>
46 Many CPUs provide "performance counters", hardware registers that can count "events"; for example,
47 cache misses, or CPU cycles. OProfile can collect profiles of code based on the number of these occurring events:
48 repeatedly, every time a certain (configurable) number of events has occurred, the PC value is recorded.
49 This information is aggregated into profiles for each binary image.  Alternatively, OProfile's event counting
50 tool can collect simple raw event counts.</para>
51 <sect1 id="legacy_mode">
52 <title>OProfile legacy profiling mode</title>
53 Prior to release 1.0, OProfile included a profiling tool consisting of the <command>opcontrol</command> shell script, the <command>oprofiled</command> daemon,
54 and the attendant oprofile kernel driver. This "legacy profiler" was deprecated in release 0.9.8 with the introduction of
55 the <command>operf</command> profiling tool (see <xref linkend="perf_events"/>). Some older architectures/platforms
56 do not support the use of <command>operf</command>. For those cases, oprofile users should install release 0.9.9, which is the
57 last release to include the legacy profiler.
58 </sect1>
59 <sect1 id="perf_events">
60 <title>OProfile perf_events profiling mode</title>
61 <para>
62 OProfile has the ability to profile a single process or every currently running process (i.e., system-wide)
63 via the <command>operf</command> program. <command>operf</command> interfaces with the
64 kernel to collect samples via the Linux Kernel Performance Events Subsystem (hereafter
65 referred to as "perf_events").  OProfile can co-exist with other tools on your system that
66 may also be using the perf_events kernel subsystem.
67 </para>
68 <para>
69 Using <command>operf</command> to profile a single
70 process can be done as a normal user; however, root authority <emphasis>is</emphasis> required to run
71 <command>operf</command> in system-wide profiling mode.
72 <note>
73 <title>Note</title>
74 Some older processor models are not supported by the underlying perf_events kernel and, thus, are not supported by <command>operf</command>.
75 If you receive the message
76 <screen>  Your kernel's Performance Events Subsystem does not support your processor type</screen>
77 when attempting to use <command>operf</command>, install OProfile 0.9.9 and try profiling with <command>opcontrol</command>
78 to see if your processor type may be supported by OProfile's legacy mode.
79 </note>
80 </para>
81 </sect1>
82
83 <sect1 id="event_counting">
84 <title>OProfile event counting mode</title>
85 OProfile provides the <command>ocount</command> tool for
86 collecting raw event counts on a per-application, per-process, per-cpu, or system-wide basis.  Unlike the
87 profiling tools, post-processing of the data collected is not necessary -- the data is displayed in the
88 output of <command>ocount</command>.  A common use case for event counting tools is when performance analysts
89 want to determine the CPI (cycles per instruction) for an application. High CPI implies possible stalls,
90 and many architectures provide events that give detailed information about the different types of stalls.
91 The events provided are architecture-specific, so we refer the reader to the hardware manuals available for
92 the processor type being used.
93 </sect1>
94
95
96 <sect1 id="applications">
97 <title>Applications of OProfile</title>
98 <para>
99 OProfile is useful in a number of situations. You might want to use OProfile when you :
100 </para>
101 <itemizedlist>
102 <listitem><para>need low overhead</para></listitem>
103 <listitem><para>cannot use highly intrusive profiling methods</para></listitem>
104 <listitem><para>need to profile interrupt handlers</para></listitem>
105 <listitem><para>need to profile an application and its shared libraries</para></listitem>
106 <listitem><para>need to profile dynamically compiled code of supported virtual machines (see <xref linkend="jitsupport"/>)</para></listitem>
107 <listitem><para>need to capture the performance behaviour of entire system</para></listitem>
108 <listitem><para>want to examine hardware effects such as cache misses</para></listitem>
109 <listitem><para>want detailed source annotation</para></listitem>
110 <listitem><para>want instruction-level profiles</para></listitem>
111 <listitem><para>want call-graph profiles</para></listitem>
112 </itemizedlist>
113 <para>
114 OProfile is not a panacea. OProfile might not be a complete solution when you :
115 </para>
116 <itemizedlist>
117 <listitem><para>require call graph profiles on platforms other than x86, ARM, and PowerPC</para></listitem>
118 <listitem><para>require 100% instruction-accurate profiles</para></listitem>
119 <listitem><para>need function call counts or an interstitial profiling API</para></listitem>
120 <listitem><para>cannot tolerate any disturbance to the system whatsoever</para></listitem>
121 <listitem><para>need to profile interpreted or dynamically compiled code of non-supported virtual machines</para></listitem>
122 </itemizedlist>
123 <sect2 id="jitsupport">
124 <title>Support for dynamically compiled (JIT) code</title>
125 <para>
126 OProfile provides a framework to support JITed code ("just-in-time (JIT) compiled code").
127 A development library is provided to allow developers
128 to add support for any VM (virtual machine) that produces dynamically compiled code (see the <emphasis>OProfile JIT agent
129 developer guide</emphasis>).
130 In addition, built-in support is included for the following:</para>
131 <itemizedlist><listitem>JVMTI agent library for Java (1.5 and higher)</listitem>
132 <listitem>JVMPI agent library for Java (1.5 and lower)</listitem>
133 </itemizedlist>
134 These libraries make it possible for OProfile to attribute profile samples
135 to Java methods. Without a VM-specific agent library, OProfile will typically report
136 samples from JITed code similar to the following example:
137 <screen>     anon: &lt;tgid&gt;&lt;address range&gt;</screen>
138 For information on how to use OProfile's JIT support, see <xref linkend="setup-jit"/>.
139 </sect2>
140
141 <sect2 id="guestsupport">
142 <title>No support for virtual machine guests</title>
143 <para>
144 OProfile currently does not support event-based profiling (i.e, using hardware events like cache misses,
145 branch mispredicts) on virtual machine guests running under systems such as VMware.
146 (Note: KVM guests <emphasis>are</emphasis> supported.)  The list of
147 supported events displayed by ophelp is based on CPU type and does
148 not take into account whether the running system is a guest system or real system.  To use
149 OProfile on such guest systems, you must use the legacy profiler's timer mode (see <xref linkend="timer" />).
150 </para>
151 </sect2>
152
153
154 </sect1>
155
156 <sect1 id="requirements">
157 <title>System requirements</title>
158
159 <variablelist>
160         <varlistentry>
161                 <term>Linux kernel</term>
162                 <listitem><para>
163                         Release 2.6.31 or higher
164                 </para></listitem>
165         </varlistentry>
166         <varlistentry>
167                 <term>Supported architectures</term>
168                 <listitem><para>
169                         AMD, ARM, Intel, PowerPC, Tile, MIPS
170                 </para></listitem>
171         </varlistentry>
172         <varlistentry>
173                 <term>Required libraries</term>
174                 <listitem><para>
175                         These libraries are required : <filename>popt</filename>, <filename>bfd</filename>,
176                         <filename>liberty</filename> (debian users: libiberty is provided in binutils-dev package), <filename>dl</filename>,
177                         plus the standard C++ libraries.
178                 </para></listitem>
179         </varlistentry>
180         <varlistentry>
181                 <term>Required kernel headers</term>
182                 <listitem><para>
183                         Either the kernel-headers package must be installed or use the <code>--with-kernel</code>
184                         configure option.
185                 </para></listitem>
186         </varlistentry>
187         <varlistentry>
188                 <term>Required user account</term>
189                 <listitem><para>
190                         For secure processing of sample data from JIT virtual machines (e.g., Java),
191                         the special user account "oprofile" must exist on the system.  The 'configure'
192                         and 'make install' operations will print warning messages if this
193                         account is not found.  If you intend to profile JITed code, you must create
194                         a group account named 'oprofile' and then create the 'oprofile' user account,
195                         setting the default group to 'oprofile'.  A runtime error message is printed to
196                         the oprofile log when processing JIT samples if this special user
197                         account cannot be found.
198                 </para></listitem>
199         </varlistentry>
200         <varlistentry>
201                 <term><acronym>ELF</acronym></term>
202                 <listitem><para>
203                         Probably not too strenuous a requirement, but older <acronym>A.OUT</acronym> binaries/libraries are not supported.
204                 </para></listitem>
205         </varlistentry>
206         <varlistentry>
207                 <term>K&amp;R coding style</term>
208                 <listitem><para>
209                         OK, so it's not really a requirement, but I wish it was...
210                 </para></listitem>
211         </varlistentry>
212 </variablelist>
213
214
215 </sect1>
216
217 <sect1 id="resources">
218 <title>Internet resources</title>
219
220 <variablelist>
221         <varlistentry>
222                 <term>Web page</term>
223                 <listitem><para>
224                         There is a web page (which you may be reading now) at
225                         <ulink url="http://oprofile.sf.net/">http://oprofile.sf.net/</ulink>.
226                 </para></listitem>
227         </varlistentry>
228         <varlistentry>
229                 <term>Download</term>
230                 <listitem><para>
231                         You can download a source tarball or check out code from
232                         the code repository at the sourceforge page,
233                         <ulink url="http://sf.net/projects/oprofile/">http://sf.net/projects/oprofile/</ulink>.
234                 </para></listitem>
235         </varlistentry>
236         <varlistentry>
237                 <term>Mailing list</term>
238                 <listitem><para>
239                         There is a low-traffic OProfile-specific mailing list, details at
240                         <ulink url="http://sf.net/mail/?group_id=16191">http://sf.net/mail/?group_id=16191</ulink>.
241                 </para></listitem>
242         </varlistentry>
243         <varlistentry>
244                 <term>Bug tracker</term>
245                 <listitem><para>
246                         There is a bug tracker for OProfile at SourceForge,
247                         <ulink url="http://sourceforge.net/p/oprofile/bugs/">http://sourceforge.net/p/oprofile/bugs/</ulink>.
248                 </para></listitem>
249         </varlistentry>
250         <varlistentry>
251                 <term>IRC channel</term>
252                 <listitem><para>
253                         Several OProfile developers and users sometimes hang out on channel <command>#oprofile</command>
254                         on the <ulink url="http://oftc.net">OFTC</ulink> network. 
255                 </para></listitem>
256         </varlistentry>
257 </variablelist>
258
259 </sect1>
260
261 <sect1 id="install">
262 <title>Installation</title>
263
264 <para>
265 First you need to build OProfile and install it. <command>./configure</command>, <command>make</command>, <command>make install</command>
266 is often all you need, but note these arguments to <command>./configure</command> :
267 </para>
268 <variablelist>
269         <varlistentry>
270                 <term><option>--with-java</option></term>
271                 <listitem>
272                         <para>
273                         Use this option if you need to profile Java applications.  Also, see
274                         <xref linkend="requirements"/>, "Required user account".  This option
275                         is used to specify the location of the Java Development Kit (JDK)
276                         source tree you wish to use. This is necessary to get the interface description
277                         of the JVMPI (or JVMTI) interface to compile the JIT support code successfully.
278                         </para>
279                         <note>
280                                 <para>
281                                 The Java Runtime Environment (JRE) does not include the development
282                                 files that are required to compile the JIT support code, so the full
283                                 JDK must be installed in order to use this option.
284                                 </para>
285                         </note>
286                         <para>
287                         By default, the Oprofile JIT support libraries will be installed in
288                         <filename>&lt;oprof_install_dir&gt;/lib/oprofile</filename>.  To build
289                         and install OProfile and the JIT support libraries as 64-bit, you can
290                         do something like the following:
291                         <screen>
292                         # CFLAGS="-m64" CXXFLAGS="-m64" ./configure \
293                         --with-java={my_jdk_installdir} \
294                         --libdir=/usr/local/lib64
295                         </screen>
296                         </para>
297                         <note>
298                                 <para>
299                                 If you encounter errors building 64-bit, you should
300                                 install libtool 1.5.26 or later since that release of
301                                 libtool fixes known problems for certain platforms.
302                                 If you install libtool into a non-standard location,
303                                 you'll need to edit the invocation of 'aclocal' in
304                                 OProfile's autogen.sh as follows (assume an install
305                                 location of /usr/local):
306                                 </para>
307                                 <para>
308                                 <code>aclocal -I m4 -I /usr/local/share/aclocal</code>
309                                 </para>
310                         </note> 
311                 </listitem>
312         </varlistentry>
313         <varlistentry id="disable-werror">
314                 <term><option>--disable-werror</option></term>
315                 <listitem><para>
316                         Development versions of OProfile build by
317                         default with <option>-Werror</option>. This option turns
318                         <option>-Werror</option> off.
319                 </para></listitem>
320         </varlistentry>
321         <varlistentry id="disable-optimization">
322                 <term><option>--disable-optimization</option></term>
323                 <listitem><para>
324                         Disable the <option>-O2</option> compiler flag
325                         (useful if you discover an OProfile bug and want to give a useful
326                         back-trace etc.)
327                 </para></listitem>
328         </varlistentry>
329         <varlistentry id="with-kernel">
330                 <term><option>--with-kernel</option></term>
331                 <listitem><para>
332                         This option is used to specify the location of the kernel headers <filename>include</filename> directory
333                         needed to build the perf_events-enabled <command>operf</command> program. By default, the OProfile
334                         build system expects to find this directory under <filename>/usr</filename>. Use this option if your
335                         kernel headers are in a non-standard location or if building in a cross-compile enviroment or in a
336                         situation where the host system does not support perf_events but you wish to build binaries for a
337                         target system that does support perf_events.
338                 </para></listitem>
339         </varlistentry>
340 </variablelist>
341 <para>
342 It is recommended that if you have a
343 uniprocessor machine, you enable the local APIC / IO_APIC support for
344 your kernel (this is automatically enabled for SMP kernels). With many BIOS (kernel &gt;= 2.6.9 and UP kernel)
345 it's not sufficient to enable the local APIC -- you must also turn it on explicitly at boot
346 time by providing the "lapic" option to the kernel.
347 If you use the NMI watchdog, be aware that the watchdog is disabled when profiling starts
348 and not re-enabled until the profiling is stopped.
349 </para>
350
351 </sect1>
352
353 <sect1 id="uninstall">
354 <title>Uninstalling OProfile</title>
355 <para>
356 You must have the source tree available to uninstall OProfile; a <command>make uninstall</command> will
357 remove all installed files except your configuration file in the directory <filename>~/.oprofile</filename>.
358 </para>
359 </sect1>
360
361 </chapter>
362
363 <chapter id="overview"> 
364 <title>Overview</title>
365 <sect1 id="getting-started-with-operf">
366 <title>Getting started with OProfile using <command>operf</command></title>
367 <para>
368 Profiling with <command>operf</command> allows you to precisely target your profiling (i.e., single process
369 or system-wide). With <command>operf</command>, there is no initial setup needed -- simply invoke <command>operf</command> with
370 the options you need; then run the OProfile post-processing tool(s). The <command>operf</command> syntax
371 is as follows:
372 </para>
373 <screen>operf [ options ] [ --system-wide | --pid=&lt;PID&gt; | [ command [ args ] ] ]</screen>
374 <para>
375 A typical usage might look like this:
376 </para>
377 <screen>operf ./my_test_program my_arg</screen>
378 <para>
379 When <filename>./my_test_program</filename> completes (or when you press Ctrl-C), profiling
380 stops and you're ready to use <command>opreport</command> or other OProfile post-processing tools.
381 By default, <command>operf</command> stores the sample data in <filename>&lt;cur_dir&gt;/oprofile_data/samples/current</filename>,
382 and <command>opreport</command> and other post-processing tools will look in that location first for profile data,
383 unless you pass the <code>--session-dir</code> option.
384 </para>
385 </sect1>
386
387
388 <sect1 id="getting-started-with-ocount">
389 <title>Getting started with OProfile using <command>ocount</command></title>
390 <para>
391 <command>ocount</command> is an OProfile tool that can be used to count native hardware events occurring in either
392 a specific application, a set of processes or threads, a set of active system processors, or the
393 entire system. The data collected during a counting session is displayed to stdout by default, but may
394 also be saved to a file.  The <command>ocount</command> syntax is as follows:
395 <para>
396 <screen>ocount [ options ] [ --system-wide | --process-list &lt;pids&gt; | --thread-list &lt;tids&gt; | --cpu-list &lt;cpus&gt; [ command [ args ] ] ]
397 </screen>
398 </para>
399 A typical usage might look like this:
400 <para>
401 <screen>ocount --events=CPU_CLK_UNHALTED,INST_RETIRED /home/user1/my_test_program my_arg</screen>
402 </para>
403 When <filename>my_test_program</filename> completes (or when you press Ctrl-C), counting
404 stops and the results are displayed to the screen (as shown below).
405 <para>
406 <screen>
407 Events were actively counted for 2.8 seconds.
408 Event counts (actual) for /home/user1/my_test_program:
409         Event                   Count                    % time counted
410         CPU_CLK_UNHALTED        9,408,018,070            100.00
411         INST_RETIRED            16,719,918,108           100.00
412 </screen>
413 </para>
414 </para>
415 </sect1>
416
417 <sect1 id="eventspec">
418 <title>Specifying performance counter events</title>
419 <para>
420 Whether profiling with <command>operf</command> or doing simple event counting with <command>ocount</command>,
421 you can collect information about one more native hardware events using the <code>--events</code>
422 option -- a comma-separated list of event specfications. The event specification is the means to provide details
423 of how each hardware performance counter should be set up.
424 For profiling, the event specification is a colon-separated string of the form
425 <option><emphasis>name</emphasis>:<emphasis>count</emphasis>:<emphasis>unitmask</emphasis>:<emphasis>kernel</emphasis>:<emphasis>user</emphasis></option>
426 as described in the table below. For <command>ocount</command>, specification is of the form
427 <option><emphasis>name</emphasis>:<emphasis>unitmask</emphasis>:<emphasis>kernel</emphasis>:<emphasis>user</emphasis></option>.
428 Note the presence of the <emphasis>count</emphasis> field for profiling.  The <emphasis>count</emphasis> field tells the profiler
429 how many events should occur between a profile snapshot (usually referred to as a "sample").  Since
430 <command>ocount</command> does not do sampling, the <emphasis>count</emphasis> field is not needed.
431 </para>
432 <para>
433 If no event specs are passed to <command>operf</command> or <command>ocount</command>,
434 the default event will be used.
435 </para>
436 <para>
437 <note>The perf_events kernel subsystem allocates hardware counters as necessary, but some processor
438 types have restrictions as to what hardware events may be counted simultaneously.
439 The kernel employs a multiplexing technique when such
440 hardware restrictions are encountered, such that events are monitored on a rotating basis.
441 </note>
442 </para>
443 <informaltable frame="all">
444 <tgroup cols='2'>
445 <tbody>
446 <row><entry><option>name</option></entry><entry>The symbolic event name, e.g. <constant>CPU_CLK_UNHALTED</constant></entry></row>
447 <row><entry><option>count</option></entry><entry>The counter reset value, e.g. 100000; use only for profiling</entry></row>
448 <row><entry><option>unitmask</option></entry><entry>The unit mask, as given in the events list: e.g. 0x0f; or a symbolic name
449 if a <constant>name=&lt;um_name&gt;</constant> field is present</entry></row>
450 <row><entry><option>kernel</option></entry><entry>Enable profiling of kernel code</entry></row>
451 <row><entry><option>user</option></entry><entry>Enable profiling of userspace code</entry></row>
452 </tbody>
453 </tgroup>
454 </informaltable>
455 <para>
456 The last three values are optional; if you omit them (e.g. <option>operf --events=DATA_MEM_REFS:30000</option>),
457 they will be set to the default values (i.e., the default unit mask value for the given event, and profiling (or counting)
458 both kernel and userspace code will be enabled). Note that on some architectures, some events may
459 require a unit mask be specified.
460 </para>
461 <para>
462 You can specify unit mask values using either a numerical value (hex values
463 <emphasis>must</emphasis> begin with "0x") or a symbolic name (if the <constant>name=&lt;um_name&gt;</constant>
464 field is shown in the <command>ophelp</command> output). For some named unit masks, the hex value is not unique; thus, OProfile
465 tools enforce specifying such unit masks value by name.
466 </para>
467 <para>
468 The table below lists the default profiling event for various processor types. The same events
469 can be used for <command>ocount</command>, minus the <emphasis>count</emphasis> field.
470 </para>
471 <informaltable frame="all">
472 <tgroup cols='3'>
473 <tbody>
474 <row><entry>Processor</entry><entry>cpu_type</entry><entry>Default event</entry></row>
475 <row><entry>Alpha EV67</entry><entry>alpha/ev67</entry><entry>CYCLES:100000:0:1:1</entry></row>
476 <row><entry>ARM/XScale PMU1</entry><entry>arm/xscale1</entry><entry>CPU_CYCLES:100000:0:1:1</entry></row>
477 <row><entry>ARM/XScale PMU2</entry><entry>arm/xscale2</entry><entry>CPU_CYCLES:100000:0:1:1</entry></row>
478 <row><entry>ARM/MPCore</entry><entry>arm/mpcore</entry><entry>CPU_CYCLES:100000:0:1:1</entry></row>
479 <row><entry>Athlon</entry><entry>i386/athlon</entry><entry>CPU_CLK_UNHALTED:100000:0:1:1</entry></row>
480 <row><entry>Pentium Pro</entry><entry>i386/ppro</entry><entry>CPU_CLK_UNHALTED:100000:0:1:1</entry></row>
481 <row><entry>Pentium II</entry><entry>i386/pii</entry><entry>CPU_CLK_UNHALTED:100000:0:1:1</entry></row>
482 <row><entry>Pentium III</entry><entry>i386/piii</entry><entry>CPU_CLK_UNHALTED:100000:0:1:1</entry></row>
483 <row><entry>Pentium M (P6 core)</entry><entry>i386/p6_mobile</entry><entry>CPU_CLK_UNHALTED:100000:0:1:1</entry></row>
484 <row><entry>Pentium 4 (non-HT)</entry><entry>i386/p4</entry><entry>GLOBAL_POWER_EVENTS:100000:1:1:1</entry></row>
485 <row><entry>Pentium 4 (HT)</entry><entry>i386/p4-ht</entry><entry>GLOBAL_POWER_EVENTS:100000:1:1:1</entry></row>
486 <row><entry>Hammer</entry><entry>x86-64/hammer</entry><entry>CPU_CLK_UNHALTED:100000:0:1:1</entry></row>
487 <row><entry>Family10h</entry><entry>x86-64/family10</entry><entry>CPU_CLK_UNHALTED:100000:0:1:1</entry></row>
488 <row><entry>Family11h</entry><entry>x86-64/family11h</entry><entry>CPU_CLK_UNHALTED:100000:0:1:1</entry></row>
489 <row><entry>IBM pseries</entry><entry>ppc64/power{ 4|5|6|7|8|970 }</entry><entry>CYCLES:100000:0:1:1</entry></row>
490 <row><entry>IBM s390</entry><entry>s390/{ z10|z196|zEC12 }</entry><entry>HWSAMPLING:4127518:0:1:1</entry></row>
491 </tbody>
492 </tgroup>
493 </informaltable>
494
495 </sect1>
496
497 <sect1 id="tools-overview">
498 <title>Tools summary</title>
499 <para>
500 This section gives a brief description of the available OProfile utilities and their purpose.
501 </para>
502 <variablelist>
503 <varlistentry>
504         <term><filename>ophelp</filename></term>
505         <listitem><para>
506                 This utility lists the available events and short descriptions.
507         </para></listitem>
508 </varlistentry>
509
510 <varlistentry>
511         <term><filename>operf</filename></term>
512         <listitem><para>
513                 This is the program for collecting profile data, discussed in <xref linkend="controlling-operf" />.
514         </para></listitem>
515 </varlistentry>
516         
517 <varlistentry>
518         <term><filename>ocount</filename></term>
519         <listitem><para>
520                 This tool is used for simple event counting, as described in in <xref linkend="controlling-ocount" />.
521         </para></listitem>
522 </varlistentry>
523
524 <varlistentry>
525         <term><filename>agent libraries</filename></term>
526         <listitem><para>
527                         Used by virtual machines (like the Java VM) to record information about JITed code being profiled. See <xref linkend="setup-jit" />.
528                 </para></listitem>
529 </varlistentry>
530
531 <varlistentry>
532         <term><filename>opreport</filename></term>
533         <listitem><para>
534                 This is the main tool for retrieving useful profile data, described in
535                 <xref linkend="opreport" />.
536         </para></listitem>
537 </varlistentry>
538
539 <varlistentry>
540         <term><filename>opannotate</filename></term>
541         <listitem><para>
542                 This utility can be used to produce annotated source, assembly or mixed source/assembly.
543                 Source level annotation is available only if the application was compiled with 
544                 debugging symbols. See <xref linkend="opannotate" />.
545         </para></listitem>
546 </varlistentry>
547
548 <varlistentry>
549         <term><filename>opgprof</filename></term>
550         <listitem><para>
551                 This utility can output gprof-style data files for a binary, for use with
552                 <command>gprof -p</command>. See <xref linkend="opgprof" />.
553         </para></listitem>
554 </varlistentry>
555
556 <varlistentry>
557         <term><filename>oparchive</filename></term>
558         <listitem><para>
559                 This utility can be used to collect executables, debuginfo,
560                 and sample files and copy the files into an archive.
561                 The archive is self-contained and can be moved to another
562                 machine for further analysis.
563                 See <xref linkend="oparchive" />.
564         </para></listitem>
565 </varlistentry>
566
567 <varlistentry>
568         <term><filename>opimport</filename></term>
569         <listitem><para>
570                 This utility converts sample database files from a foreign binary format (abi) to
571                 the native format. This is useful only when moving sample files between hosts
572                 for analysis on platforms other than the one used for collection.
573                 See <xref linkend="opimport" />.
574         </para></listitem>
575 </varlistentry>
576
577 </variablelist>
578 </sect1>
579         
580 </chapter>
581
582 <chapter id="controlling-profiler">
583 <title>Controlling the profiler</title>
584
585 <sect1 id="controlling-operf">
586 <title>Using <command>operf</command></title>
587 <para>
588 This section describes in detail how <command>operf</command> is used to
589 control profiling. Unless otherwise directed, <command>operf</command> will profile using
590 the default event for your system. For most systems, the default event is some
591 cycles-based event, assuming your processor type supports hardware performance
592 counters. If your hardware <emphasis>does</emphasis> support performance counters, you can specify
593 something other than the default hardware event on which to profile. The performance
594 monitor counters can be programmed to count various hardware events,
595 such as cache misses or MMX operations. The event
596 chosen for each counter is reflected in the profile data collected
597 by OProfile: functions and binaries at the top of the profiles reflect
598 that most of the chosen events happened within that code.
599 </para>
600 <para>
601 Additionally, each counter is programmed with a "count" value, which corresponds to how
602 detailed the profile is. The lower the value, the more frequently profile
603 samples are taken. You can choose to sample only kernel code, user-space code,
604 or both (both is the default). Finally, some events have a "unit mask"
605 -- this is a value that further restricts the type of event being counted.
606 You can see the event types and unit masks for your CPU using <command>ophelp</command>.
607 More information on event specification can be found at <xref linkend="eventspec"/>.
608 </para>
609 <para>
610 The <command>operf</command> command syntax is:
611 </para>
612 <screen>operf [ options ] [ --system-wide | --pid=&lt;PID&gt; | [ command [ args ] ] ]</screen>
613 <para>
614 When profiling an application using either the <code>command</code> or <code>--pid</code> option of
615 <command>operf</command>, forks and execs of the profiled process will also be profiled. The samples
616 from an exec'ed process will be attributed to the executable binary run by that process. See
617 <xref linkend="interpreting_operf_results"/>
618 </para>
619 <para>
620 Following is a description of the <command>operf</command> options.
621 </para>
622 <variablelist>
623         <varlistentry>
624                 <term><option>command [args]</option></term>
625                 <listitem><para>
626                 The command or application to be profiled. The <emphasis>[args]</emphasis> are the input arguments
627         that the command or application requires. Either <code>command</code>, <code>--pid</code> or
628         <code>--system-wide</code> is required, but cannot be used simultaneously.
629                 </para></listitem>
630         </varlistentry>
631         <varlistentry>
632                 <term><option>--pid / -p [PID]</option></term>
633                 <listitem><para>
634                 This option enables <command>operf</command> to profile a running application. <code>PID</code>
635                 should be the process ID of the process you wish to profile. When
636                 finished profiling (e.g., when the profiled process ends), press
637                 Ctrl-c to stop <command>operf</command>.
638                 </para></listitem>
639         </varlistentry>
640         <varlistentry>
641                 <term><option>--system-wide / -s</option></term>
642                 <listitem><para>
643                 This option is for performing a system-wide profile. You must
644                 have root authority to run <command>operf</command> in this mode.
645                 When finished profiling, Ctrl-C to stop <command>operf</command>. If you run
646                 <code>operf --system-wide</code> as a background job (i.e., with the &amp;), you
647                 <emphasis>must</emphasis> stop it in a controlled manner in order to process
648                 the profile data it has collected.  Use <code>kill -SIGINT &lt;operf-PID&gt;</code>
649                 for this purpose. It is recommended that when running <command>operf</command>
650                 with this option, your current working directory should be <filename>/root</filename> or a subdirectory
651                 of <filename>/root</filename> to avoid storing sample data files in locations accessible by regular users.
652                 </para></listitem>
653         </varlistentry>
654         <varlistentry>
655                 <term><option>--vmlinux / k [vmlinux_path]</option></term>
656                 <listitem><para>
657                 A vmlinux file that matches the running kernel that has symbol and/or debuginfo.
658                 Kernel samples will be attributed to this binary, allowing post-processing tools
659                 (like <command>opreport</command>) to attribute samples to the appropriate kernel symbols.
660                 If this option is not specified, the file /proc/kallsyms is used to obtain
661                 kernel symbol addresses correponding to sample addresses.  However, the setting of
662                 /proc/sys/kernel/kptr_restrict may restrict a non-root user's access to
663                 /proc/kallsyms, in which case,
664                 all kernel samples are attributed to a pseudo binary named "no-vmlinux".
665                 </para></listitem>
666         </varlistentry>
667         <varlistentry>
668                 <term><option>--callgraph / -g</option></term>
669                 <listitem><para>
670                 This option enables the callgraph to be saved during profiling. NOTE: The
671                 full callchain is recorded, so there is no depth limit.
672                 </para></listitem>
673         </varlistentry>
674         <varlistentry>
675                 <term><option>--append / -a</option></term>
676                 <listitem><para>
677                 By default, <command>operf</command> moves old profile data from
678                 <filename>&lt;session_dir&gt;/samples/current</filename> to
679                 <filename>&lt;session_dir&gt;/samples/previous</filename>.
680                 If a 'previous' profile already existed, it will be replaced. If the
681                 <code>--append</code> option is passed, old profile data in 'current' is left in place and
682                 new profile data will be added to it, and the 'previous' profile (if one existed)
683                 will remain untouched. To access the 'previous' profile, simply add a session
684                 specification to the normal invocation of oprofile post-processing tools; for example:
685                 </para>
686                 <para>
687                 <screen>opreport session:previous</screen>
688                 </para></listitem>
689         </varlistentry>
690         <varlistentry>
691                 <term><option>--events / -e  [event1[,event2[,...]]]</option></term>
692                 <listitem><para>
693                 This option is for passing a comma-separated list of event specifications
694                 for profiling. Each event spec is of the form:
695                 </para>
696                 <screen>name:count[:unitmask[:kernel[:user]]]</screen>
697                 <para>
698                 When no event specification is given, the default event for the running
699                 processor type will be used for profiling. Use <command>ophelp</command>
700                 to list the available events for your processor type.
701                 </para></listitem>
702         </varlistentry>
703         <varlistentry>
704                 <term><option>--separate-thread / -t</option></term>
705                 <listitem><para>
706                 This option categorizes samples by thread group ID (tgid) and thread ID (tid).
707                 The <code>--separate-thread</code> option is useful for seeing per-thread samples in
708                 multi-threaded applications.  When used in conjuction with the <code>--system-wide</code>
709                 option, <code>--separate-thread</code> is also useful for seeing per-process
710                 (i.e., per-thread group) samples for the case where multiple processes are
711                 executing the same program during a profiling run.
712                 </para></listitem>
713         </varlistentry>
714         <varlistentry>
715                 <term><option>--separate-cpu / -c</option></term>
716                 <listitem><para>
717                 This option categorizes samples by cpu.
718                 </para></listitem>
719         </varlistentry>
720         <varlistentry>
721                 <term><option>--session-dir / -d [path]</option></term>
722                 <listitem><para>
723                 This option specifies the session directory to hold the sample data. If not specified,
724                 the data is saved in the <filename>oprofile_data</filename> directory on the current path.
725                 </para></listitem>
726         </varlistentry>
727         <varlistentry>
728            <term><option>---lazy-conversion / -l</option></term>
729                 <listitem><para>
730                 Use this option to reduce the overhead of <command>operf</command> during profiling.
731                 Normally, profile data received from the kernel is converted to OProfile format
732                 during profiling time. This is typically not an issue when profiling a single
733                 application. But when using the <code>--system-wide</code> option, this on-the-fly
734                 conversion process can cause noticeable overhead, particularly on busy
735                 multi-processor systems. The <code>--lazy-conversion</code> option directs
736                 <command>operf</command> to wait until profiling is completed to do the conversion
737                 of profile data.
738                 </para></listitem>
739         </varlistentry>
740         <varlistentry>
741                 <term><option>--verbose / -V [level]</option></term>
742                 <listitem><para>
743                 A comma-separated list of debugging control values used to increase the verbosity of the
744                 output. Valid values are: debug, record, convert, misc, sfile, arcs, and the special value, 'all'.
745                 </para></listitem>
746         </varlistentry>
747         <varlistentry>
748                 <term><option>--version -v </option></term>
749                 <listitem><para>
750                 Show <command>operf</command> version.
751                 </para></listitem>
752         </varlistentry>
753         <varlistentry>
754                 <term><option>--help / -h</option></term>
755                 <listitem><para>
756                 Show a help message.
757                 </para></listitem>
758         </varlistentry>
759 </variablelist>
760 </sect1>
761
762
763 <sect1 id="setup-jit">
764         <title>Setting up the JIT profiling feature</title>
765         <para>
766                 To gather information about JITed code from a virtual machine,
767                 it needs to be instrumented with an agent library. We use the
768                 agent libraries for Java in the following example. To use the
769                 Java profiling feature, you must build OProfile with the "--with-java" option
770                 (<xref linkend="install" />).
771
772         </para>
773
774         <sect2 id="setup-jit-jvm">
775                 <title>JVM instrumentation</title>
776                 <para>
777                         Add this to the startup parameters of the JVM (for JVMTI):
778
779                         <screen><option>-agentpath:&lt;libdir&gt;/libjvmti_oprofile.so[=&lt;options&gt;]</option> </screen>
780                         or
781                         <screen><option>-agentlib:jvmti_oprofile[=&lt;options&gt;]</option> </screen>
782                 </para>
783                 <para>
784                         The JVMPI agent implementation is enabled with the command line option
785                         <screen><option>-Xrunjvmpi_oprofile[:&lt;options&gt;]</option> </screen>
786                 </para>
787                 <para>
788                         Currently, there is just one option available -- <option>debug</option>. For JVMPI,
789                         the convention for specifying an option is <option>option_name=[yes|no]</option>.
790                         For JVMTI, the option specification is simply the option name, implying
791                         "yes"; no option specified implies "no".
792                 </para>
793                 <para>
794                         The agent library (installed in <filename>&lt;oprof_install_dir&gt;/lib/oprofile</filename>)
795                         needs to be in the library search path (e.g. add the library directory
796                         to <constant>LD_LIBRARY_PATH</constant>). If the command line of
797                         the JVM is not accessible, it may be buried within shell scripts or a
798                         launcher program. It may also be possible to set an environment variable to add
799                         the instrumentation.
800                         For Sun JVMs this is <constant>JAVA_TOOL_OPTIONS</constant>. Please check
801                         your JVM documentation for
802                         further information on the agent startup options.
803                 </para>
804
805         </sect2>
806 </sect1>
807
808
809 <sect1 id="detailed-parameters">
810 <title>Configuration details</title>
811
812 <sect2 id="hardware-counters">
813 <title>Hardware performance counters</title>
814 <para>Most processor models include performance monitor units that can be configured to monitor (count)
815 various types of hardware events.  This section is where you can find architecture-specific information
816 to help you use these events for profiling. You do not really need to read this section unless you are interested in using
817 events other than the default event chosen by OProfile.
818 </para>
819 <note>
820 <para>
821 Your CPU type may not include the requisite support for hardware performance counters, in which case
822 you must use OProfile in timer mode (see <xref linkend="timer" />), which is only available in
823 OProfile releases prior to 1.0.
824 </para>
825 </note>
826 <para>
827 The Intel hardware performance counters are detailed in the Intel IA-32 Architecture Manual, Volume 3, available
828 from <ulink url="http://developer.intel.com/">http://developer.intel.com/</ulink>. 
829 The AMD Athlon/Opteron/Phenom/Turion implementation is detailed in <ulink
830 url="http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/22007.pdf">
831 http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/22007.pdf</ulink>.
832 For IBM PowerPC processors, documentation is available at <ulink url="https://www.power.org/">
833 https://www.power.org/</ulink>. For example, <ulink url="https://www.power.org/events/Power7">
834 https://www.power.org/events/Power7</ulink> contains specific information on the performance
835 monitor unit for the IBM POWER7.
836 </para>
837 <para>
838 A physical performance monitor counter (PMC) is configured by a profiling tool to count a particular
839 type of event. When the counter overflows, an interrupt is delivered to the processor.
840 This is the basic mechanism on which OProfile is based. The delivery mode is <acronym>NMI</acronym>,
841 so blocking interrupts in the kernel does not prevent profiling. When the interrupt handler is called,
842 the current <acronym>PC</acronym> (program counter) value and the current task are recorded into the profiling structure.
843 This allows the overflow event to be attributed to a specific assembly instruction in a specific binary image.
844 OProfile receives this data (commonly referred to as a "sample") from the kernel and writes it to the sample files.
845 </para>
846 <para>
847 If we use an event such as <constant>CPU_CLK_UNHALTED</constant> or <constant>INST_RETIRED</constant>
848 (<constant>GLOBAL_POWER_EVENTS</constant> or <constant>INSTR_RETIRED</constant>, respectively, on the Pentium 4), we can
849 use the overflow counts (samples) as an estimate of actual time spent in each part of code. Alternatively we can profile interesting
850 data such as the cache behaviour of routines with the other available counters.
851 </para>
852 <para>
853 However there are several caveats. First, there are those issues listed in the Intel manual. There is a delay
854 between the counter overflow and the interrupt delivery that can skew results on a small scale - this means
855 you cannot rely on the profiles at the instruction level as being perfectly accurate.
856 For example, if you are profiling an application with an event that counts L1 cache misses, a sample attributed
857 to a particular instruction in the application doesn't necessarily mean that exact instruction is responsible
858 for that event; instead, it means the sample was taken in the dynamic vicinity of that instruction,
859 usually with a margin of error of a few instructions. Further details on this problem can be found in
860 <xref linkend="interpreting" /> and also in the Digital paper "ProfileMe: A Hardware Performance Counter".
861 </para>
862 <para>
863 Each counter has several configuration parameters besides the type of event to count.
864 First, there is the unit mask, which is used to further qualify exactly what to count.
865 Second, there is the <constant>count</constant> field, discussed below. Third, there are parameters
866 to specify whether to increment counts
867 whilst in kernel or user space. You can configure these separately for each counter.
868 </para>
869 <para>
870 When the profiler is initially setup, a performance monitor counter is chosen for counting the
871 event, and it is initialized using the <constant>count</constant> value.
872 Once profiling begins, the counter increments with each event detected, and the counter
873 <emphasis>overflows</emphasis> when the <constant>count</constant> value is reached.
874 As described above, the counter overflow generates an interrupt, and the sample is recorded.
875 After each overflow event, the counter is re-initialized using the <constant>count</constant> value,
876 and counting begins anew for the next sample. Higher values for <constant>count</constant>
877 result in samples being taken less frequently, and therefore less-detailed (and, potentially,
878 less accurate) profiling. Lower values mean more detail, but higher overhead.
879 Picking a good value for this parameter is, unfortunately, somewhat of a black art. It is
880 of course dependent on the event you have chosen.
881 Specifying too large a value will mean not enough interrupts are generated
882 to give a realistic profile (though this problem can be ameliorated by profiling for
883 longer time periods. Specifying too small a value can lead to higher performance overhead.
884 </para>
885
886 </sect2>
887
888 <sect2 id="timer">
889 <title>OProfile timer interrupt mode</title>
890 <para>
891 Some CPU types do not provide the needed hardware support for hardware performance counters.
892 Additionally, some older architectures are not supported by the perf_events kernel subsystem.
893 On such machines, the <command>operf</command> and <command>ocount</command> commands will exit with a message indicating the
894 processor type is not supported. However, you can install OProfile 0.9.9 and use the legacy
895 opcontrol-based profiler, which will fall back to using timer interrupts for profiling.
896 Note that in timer mode, OProfile is not able to profile code that has interrupts disabled.
897 <note>Timer mode is only available using the legacy <command>opcontrol</command> command,
898 available in releases prior to 1.0.</note>
899 </para>
900 </sect2>
901
902 <sect2 id="special-notes">
903 <title>Architecture-specific configuration notes</title>
904 <sect3 id="p4">
905 <title>Pentium 4 support</title>
906 <para>
907 The Pentium 4 / Xeon performance counters are organized around 3 types of model specific registers (MSRs): 45 event
908 selection control registers (ESCRs), 18 counter configuration control registers (CCCRs) and 18 counters. ESCRs describe a
909 particular set of events which are to be recorded, and CCCRs bind ESCRs to counters and configure their
910 operation. Unfortunately the relationship between these registers is quite complex; they cannot all be used with one
911 another at any time. There is, however, a subset of 8 counters, 8 ESCRs, and 8 CCCRs which can be used independently of
912 one another, so OProfile only accesses those registers, treating them as a bank of 8 "normal" counters, similar
913 to those in the P6 or Athlon/Opteron/Phenom/Turion families of CPU.
914 </para>
915 <para>
916 There is currently no support for Precision Event-Based Sampling (PEBS), nor any advanced uses of the Debug Store
917 (DS). Current support is limited to the conservative extension of OProfile's existing interrupt-based model described
918 above.
919 </para>
920 </sect3>
921
922 <sect3 id="ppc64">
923 <title>PowerPC64 support</title>
924 <para>
925 The performance monitoring unit (PMU) for the IBM PowerPC 64-bit processors 
926 consists of between 4 and 8 counters (depending on the model).  Advanced features
927 such as instruction matching and thresholding are not supported by OProfile.
928 </para>
929
930 </sect3>
931 </sect2>
932
933
934
935 </sect1>
936
937 </chapter>
938
939 <chapter id="results">
940 <title>Obtaining profiling results</title>
941 <para>
942 After collecting profile data, the raw data must undergo special processing in order for you to
943 perform your analysis. The analysis tools that perform this special processing are
944 <command>opreport</command>, <command>opannotate</command>, and <command>opgprof</command>.
945 Additionally, the <command>oparchive</command> is used to gather together profile
946 data, sampled binary files, etc. for the purpose of off-line analysis.  While
947 not really an analysis tool, <command>oparchive</command> is put in that category
948 for convenience since it takes many of the same options as the other analysis tools.
949 </para>
950
951 <sect1 id="profile-spec">
952 <title>Profile specifications</title>
953
954 <para>
955 All of the analysis tools take a <emphasis>profile specification</emphasis>
956 as an input argument.
957 This is a set of definitions that describes the specific profile data that should be
958 examined. The simplest profile specification is empty: this will match all
959 the available profile files for the current session.
960 </para>
961 <para>
962 Specification parameters are of the form <option>name:value[,value]</option>.
963 For example, if I wanted to get a combined symbol summary for
964 <filename>/bin/myprog</filename> and <filename>/bin/myprog2</filename>,
965 I could do <command>opreport -l image:/bin/myprog,/bin/myprog2</command>.
966 As a special case, you don't actually need to specify the <option>image:</option>
967 part of the specification. Anything left on the command line after all other
968 <command>opreport</command> options have been processed is assumed to be an
969 <option>image:</option> name. Similarly, if no <option>session:</option>
970 is specified, then <option>session:current</option> is assumed ("current"
971 is a special name of the current (i.e., most recent) profiling session).
972 </para>
973 <para>
974 In addition to the comma-separated list shown above, some of the 
975 specification parameters can take <command>glob</command>-style
976 values. For example, if I want to see image summaries for all
977 binaries profiled in <filename>/usr/bin/</filename>, I could do
978 <command>opreport image:/usr/bin/\*</command>. Note the necessity
979 to escape the special character from the shell.
980 </para>
981 <para>
982 For <command>opreport</command>, profile specifications can be used to
983 define two profiles, giving differential output. This is done by
984 enclosing each of the two specifications within curly braces, as shown
985 in the examples below. Any specifications outside of curly braces are
986 shared across both.
987 </para>
988
989 <sect2 id="profile-spec-examples">
990 <title>Examples</title>
991
992 <para>
993 Image summaries for all profiles with <constant>DATA_MEM_REFS</constant>
994 samples in the saved session called "stresstest" :
995 </para>
996 <screen>
997 # opreport session:stresstest event:DATA_MEM_REFS
998 </screen>
999
1000 <para>
1001 Symbol summary for the application called "test_sym53c8xx,9xx". Note the
1002 escaping is necessary as <option>image:</option> takes a comma-separated list.
1003 </para>
1004 <screen>
1005 # opreport -l ./test/test_sym53c8xx\,9xx
1006 </screen>
1007
1008 <para>
1009 Image summaries for all binaries in the <filename>test</filename> directory,
1010 excepting <filename>boring-test</filename> :
1011 </para>
1012 <screen>
1013 # opreport image:./test/\* image-exclude:./test/boring-test
1014 </screen>
1015
1016 <para>
1017 Differential profile of a binary stored in two archives :
1018 </para>
1019 <screen>
1020 # opreport -l /bin/bash { archive:./orig } { archive:./new }
1021 </screen>
1022
1023 <para>
1024 Differential profile of an archived binary with the current session :
1025 </para>
1026 <screen>
1027 # opreport -l /bin/bash { archive:./orig } { }
1028 </screen>
1029
1030 </sect2> <!-- profile spec examples -->
1031
1032 <sect2 id="profile-spec-details">
1033 <title>Profile specification parameters</title>
1034
1035 <variablelist>
1036         <varlistentry>
1037                 <term><option>archive:</option><emphasis>archivepath</emphasis></term>
1038                 <listitem><para>
1039                 A path to an archive made with <command>oparchive</command>.
1040                 Absence of this tag, unlike others, means "the current system",
1041                 equivalent to specifying "archive:".
1042                 </para></listitem>
1043         </varlistentry>
1044         <varlistentry>
1045                 <term><option>session:</option><emphasis>sessionlist</emphasis></term>
1046                 <listitem><para>
1047                 A comma-separated list of session names to resolve in. Absence of this
1048                 tag, unlike others, means "the current session", equivalent to
1049                 specifying "session:current".
1050                 </para></listitem>
1051         </varlistentry>
1052         <varlistentry>
1053                 <term><option>session-exclude:</option><emphasis>sessionlist</emphasis></term>
1054                 <listitem><para>
1055                 A comma-separated list of sessions to exclude.
1056                 </para></listitem>
1057         </varlistentry>
1058         <varlistentry>
1059                 <term><option>image:</option><emphasis>imagelist</emphasis></term>
1060                 <listitem><para>
1061                 A comma-separated list of image names to resolve. Each entry may be relative
1062                 path, <command>glob</command>-style name, or full path, e.g.</para>
1063                 <screen>opreport 'image:/usr/bin/oprofiled,*op*,./opreport'</screen>
1064                 </listitem>
1065         </varlistentry>
1066
1067         <varlistentry>
1068                 <term><option>image-exclude:</option><emphasis>imagelist</emphasis></term>
1069                 <listitem><para>
1070                 Same as <option>image:</option>, but the matching images are excluded.
1071                 </para></listitem>
1072         </varlistentry>
1073
1074         <varlistentry>
1075                 <term><option>lib-image:</option><emphasis>imagelist</emphasis></term>
1076                 <listitem><para>
1077                 Same as <option>image:</option>, but only for images that are for
1078                 a particular primary binary image (namely, an application).
1079                 </para></listitem>
1080         </varlistentry>
1081
1082         <varlistentry>
1083                 <term><option>lib-image-exclude:</option><emphasis>imagelist</emphasis></term>
1084                 <listitem><para>
1085                 Same as <option>lib-image:</option>, but the matching images
1086                 are excluded.
1087                 </para></listitem>
1088         </varlistentry>
1089
1090         <varlistentry>
1091                 <term><option>event:</option><emphasis>eventlist</emphasis></term>
1092                 <listitem><para>
1093                 The symbolic event name to match on, e.g. <option>event:DATA_MEM_REFS</option>.
1094                 You can pass a list of events for side-by-side comparison with <command>opreport</command>.
1095                 </para></listitem>
1096         </varlistentry>
1097
1098         <varlistentry>
1099                 <term><option>count:</option><emphasis>eventcountlist</emphasis></term>
1100                 <listitem><para>
1101                 The event count to match on, e.g. <option>event:DATA_MEM_REFS count:30000</option>.
1102                 Note that this value refers to the count value in the event spec you passed
1103                 to <command>operf</command> when setting up to do a
1104                 profile run.  It has nothing to do with the sample counts in the profile data
1105                 itself.
1106                 You can pass a list of events for side-by-side comparison with <command>opreport</command>.
1107                 </para></listitem>
1108         </varlistentry>
1109
1110         <varlistentry>
1111                 <term><option>unit-mask:</option><emphasis>masklist</emphasis></term>
1112                 <listitem><para>
1113                 The unit mask value of the event to match on, e.g. <option>unit-mask:1</option>.
1114                 You can pass a list of events for side-by-side comparison with <command>opreport</command>.
1115                 </para></listitem>
1116         </varlistentry>
1117
1118         <varlistentry>
1119                 <term><option>cpu:</option><emphasis>cpulist</emphasis></term>
1120                 <listitem><para>
1121                 Only consider profiles for the given numbered CPU (starting from zero).
1122                 This is only useful when using CPU profile separation.
1123                 </para></listitem>
1124         </varlistentry>
1125
1126         <varlistentry>
1127                 <term><option>tgid:</option><emphasis>pidlist</emphasis></term>
1128                 <listitem><para>
1129                 Only consider profiles for the given task groups. Unless some program
1130                 is using threads, the task group ID of a process is the same
1131                 as its process ID. This option corresponds to the POSIX
1132                 notion of a thread group.
1133                 This is only useful when using per-process profile separation.
1134                 </para></listitem>
1135         </varlistentry>
1136
1137         <varlistentry>
1138                 <term><option>tid:</option><emphasis>tidlist</emphasis></term>
1139                 <listitem><para>
1140                 Only consider profiles for the given threads. When using
1141                 recent thread libraries, all threads in a process share the
1142                 same task group ID, but have different thread IDs. You can
1143                 use this option in combination with <option>tgid:</option> to
1144                 restrict the results to particular threads within a process.
1145                 This is only useful when using per-process profile separation.
1146                 </para></listitem>
1147         </varlistentry>
1148 </variablelist>
1149
1150 </sect2>
1151
1152 <sect2 id="locating-and-managing-binary-images">
1153 <title>Locating and managing binary images</title>
1154 <para>
1155 Each session's sample files can be found in the $SESSION_DIR/samples/ directory (default
1156 for <command>operf</command> is <filename>&lt;cur_dir&gt;/oprofile_data/samples/</filename>).
1157 These are used, along with the binary image files, to produce human-readable data.
1158 In some circumstances (e.g., kernel modules), OProfile
1159 will not be able to find the binary images. All the tools have an <option>--image-path</option>
1160 option to which you can pass a comma-separated list of alternate paths to search. For example,
1161 I can let OProfile find my 2.6 modules by using <command>--image-path /lib/modules/2.6.0/kernel/</command>.
1162 It is your responsibility to ensure that the correct images are found when using this
1163 option.
1164 </para>
1165 <para>
1166 Note that if a binary image changes after the sample file was created, you won't be able to get useful
1167 symbol-based data out. This situation is detected for you. If you replace a binary, you should
1168 make sure to save the old binary if you need to do comparative profiles.
1169 </para>
1170
1171 </sect2>
1172
1173 <sect2 id="no-results">
1174 <title>What to do when you don't get any results</title>
1175 <para>
1176 When attempting to get output, you may see the error :
1177 </para>
1178 <screen>
1179 error: no sample files found: profile specification too strict ?
1180 </screen>
1181 <para>
1182 What this is saying is that the profile specification you passed in,
1183 when matched against the available sample files, resulted in no matches.
1184 There are a number of reasons this might happen:
1185 </para>
1186 <variablelist>
1187 <varlistentry><term>spelling</term><listitem><para>
1188 You specified a binary name, but spelt it wrongly. Check your spelling !
1189 </para></listitem></varlistentry>
1190 <varlistentry><term>profiler wasn't running</term><listitem><para>
1191 Make very sure that OProfile was actually up and running when you ran
1192 the application you wish to profile.
1193 </para></listitem></varlistentry>
1194 <varlistentry><term>application didn't run long enough</term><listitem><para>
1195 Remember OProfile is a statistical profiler - you're not guaranteed to
1196 get samples for short-running programs. You can help this by using a
1197 lower count for the performance counter, so there are a lot more samples
1198 taken per second.
1199 </para></listitem></varlistentry>
1200 <varlistentry><term>application spent most of its time in libraries</term><listitem><para>
1201 Similarly, if the application spends little time in the main binary image
1202 itself, with most of it spent in shared libraries it uses, you might
1203 not see any samples for the binary image (i.e., executable) itself.
1204 </para></listitem></varlistentry>
1205 <varlistentry><term>specification was really too strict</term><listitem><para>
1206 For example, you specified something like <option>tgid:3433</option>,
1207 but no task with that group ID ever ran the code.
1208 </para></listitem></varlistentry>
1209 <varlistentry><term>application didn't generate any events</term><listitem><para>
1210 If you're profiling a particular event, for example counting MMX
1211 operations, the code might simply have not generated any events in the
1212 first place. Verify the code you're profiling does what you expect it
1213 to.
1214 </para></listitem></varlistentry>
1215 <varlistentry><term>you didn't specify kernel module name correctly</term><listitem><para>
1216 If you're trying to get reports for a kernel
1217 module, make sure to use the <option>-p</option> option, and specify the
1218 module name <emphasis>with</emphasis> the <filename>.ko</filename>
1219 extension. Check if the module is one loaded from initrd.
1220 </para></listitem></varlistentry>
1221 </variablelist>
1222
1223 </sect2>
1224
1225 </sect1> <!-- profile-spec -->
1226
1227 <sect1 id="opreport">
1228 <title>Image summaries and symbol summaries (<command>opreport</command>)</title>
1229 <para>
1230 The <command>opreport</command> utility is the primary utility you will use for 
1231 getting formatted data out of OProfile. It produces two types of data: image summaries
1232 and symbol summaries. An image summary lists the number of samples for individual
1233 binary images such as libraries or applications. Symbol summaries provide per-symbol
1234 profile data. In the following truncated example, we see an image summary for the whole
1235 system:
1236 </para>
1237 <screen>
1238 $ opreport --long-filenames
1239 CPU: Intel Sandy Bridge microarchitecture, speed 2401 MHz (estimated)
1240 Counted CPU_CLK_UNHALTED events (Clock cycles when not halted) with a unit mask of 0x00 (No unit mask) count 100000
1241 CPU_CLK_UNHALT...|
1242   samples|      %|
1243 ------------------
1244     22577 28.9011 /usr/bin/Xorg
1245         CPU_CLK_UNHALT...|
1246           samples|      %|
1247         ------------------
1248             16846 74.6158 /proc/kallsyms
1249              2126  9.4167 /usr/bin/Xorg
1250               763  3.3795 /usr/lib64/libpixman-1.so.0.26.2
1251               ...
1252     17402 22.2766 /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.55.x86_64/jre/bin/java
1253         CPU_CLK_UNHALT...|
1254           samples|      %|
1255         ------------------
1256              5666 32.5595 anon (tgid:29664 range:0x7f3475000000-0x7f347616ffff)
1257              2312 13.2858 /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.55.x86_64/jre/lib/amd64/server/libjvm.so
1258              ...
1259     11554 14.7904 /home/user1/oprof-install/bin/operf
1260         CPU_CLK_UNHALT...|
1261           samples|      %|
1262         ------------------
1263              7467 64.6270 /proc/kallsyms
1264              1691 14.6356 /usr/bin/operf
1265              1324 11.4592 /lib64/libc-2.12.so
1266               455  3.9380 /usr/lib64/libstdc++.so.6.0.13
1267               315  2.7263 /ext4
1268               ...
1269     ...
1270 </screen>
1271 <para>
1272 If we had specified <option>--symbols</option> in the previous command, we would have
1273 gotten a symbol summary of all the images across the entire system. We can restrict this to only
1274 part of the system profile; for example,
1275 below is a symbol summary for the <command>operf</command> program used to collect the profile.
1276 </para>
1277 <screen>
1278 $ opreport -l -p /lib/modules/`uname -r` `which operf` 2>/dev/null | more
1279 CPU: Intel Sandy Bridge microarchitecture, speed 2401 MHz (estimated)
1280 Counted CPU_CLK_UNHALTED events (Clock cycles when not halted) with a unit mask of 0x00 (No unit mask) count 100000
1281 samples  %        image name               symbol name
1282 860       7.4607  kallsyms                 avtab_search_node
1283 474       4.1121  operf                    OP_perf_utils::op_write_event(event_union*, unsigned long long)
1284 461       3.9993  kallsyms                 avc_has_perm_noaudit
1285 455       3.9473  libstdc++.so.6.0.13      /usr/lib64/libstdc++.so.6.0.13
1286 412       3.5742  libc-2.12.so             _IO_vfscanf
1287 369       3.2012  kallsyms                 __d_lookup
1288 350       3.0363  kallsyms                 sidtab_context_to_sid
1289 274       2.3770  operf                    OP_perf_utils::op_record_process_exec_mmaps(int, int, int, operf_record*)
1290 232       2.0127  operf                    operf_process_info::find_mapping_for_sample(unsigned long long, bool)
1291 222       1.9259  kallsyms                 __link_path_walk
1292 191       1.6570  kallsyms                 pipe_read
1293 34        0.2950  ext4.ko                  ext4_mark_iloc_dirty
1294 ...
1295 </screen>
1296
1297 <para>
1298 These are the two basic ways you are most likely to use regularly, but <command>opreport</command>
1299 can do a lot more than that, as described below.
1300 </para>
1301
1302 <sect2 id="opreport-merging">
1303 <title>Merging separate profiles</title>
1304
1305 If you have used one of the <option>--separate[*]</option> options
1306 whilst profiling, there can be several separate profiles for
1307 a single binary image within a session. Normally the output
1308 will keep these images separated. So, for example, if you profiled
1309 with separation on a per-cpu basis (<code>operf --separate-cpu</code>),
1310 you would see separate columns in
1311 the output of <command>opreport</command> for each CPU where samples
1312 were recorded. But it can be useful to merge these results back together
1313 to make the report more readable. The <option>--merge</option> option allows
1314 you to do that.
1315 </sect2>
1316
1317 <sect2 id="opreport-comparison">
1318 <title>Side-by-side multiple results</title>
1319 If you have used multiple events when profiling, by default you get
1320 side-by-side results of each event's sample values from <command>opreport</command>.
1321 You can restrict which events to list by appropriate use of the
1322 <option>event:</option> profile specifications, etc.
1323 </sect2>
1324
1325 <sect2 id="opreport-callgraph">
1326 <title>Callgraph output</title>
1327 <para>
1328 This section provides details on how to use the OProfile callgraph feature.
1329 </para>
1330 <sect3 id="op-cg1">
1331 <title>Callgraph details</title>
1332 <para>
1333 When using the <option>--callgraph</option> option, you can see what
1334 functions are calling other functions in the output. Consider the
1335 following program:
1336 </para>
1337 <screen>
1338 #include &lt;string.h&gt;
1339 #include &lt;stdlib.h&gt;
1340 #include &lt;stdio.h&gt;
1341
1342 #define SIZE 500000
1343
1344 static int compare(const void *s1, const void *s2)
1345 {
1346         return strcmp(s1, s2);
1347 }
1348
1349 static void repeat(void)
1350 {
1351         int i;
1352         char *strings[SIZE];
1353         char str[] = "abcdefghijklmnopqrstuvwxyz";
1354
1355         for (i = 0; i &lt; SIZE; ++i) {
1356                 strings[i] = strdup(str);
1357                 strfry(strings[i]);
1358         }
1359
1360         qsort(strings, SIZE, sizeof(char *), compare);
1361 }
1362
1363 int main()
1364 {
1365         while (1)
1366                 repeat();
1367 }
1368 </screen>
1369 <para>
1370 When running with the call-graph option, OProfile will
1371 record the function stack every time it takes a sample.
1372 <command>opreport --callgraph</command> outputs an entry for each
1373 function, where each entry looks similar to:
1374 </para>
1375 <screen>
1376 samples  %        image name               symbol name
1377   197       0.1548  cg                       main
1378   127036   99.8452  cg                       repeat
1379 84590    42.5084  libc-2.3.2.so            strfry
1380   84590    66.4838  libc-2.3.2.so            strfry [self]
1381   39169    30.7850  libc-2.3.2.so            random_r
1382   3475      2.7312  libc-2.3.2.so            __i686.get_pc_thunk.bx
1383 -------------------------------------------------------------------------------
1384 </screen>
1385 <para>
1386 Here the non-indented line is the function we're focussing upon
1387 (<function>strfry()</function>). This
1388 line is the same as you'd get from a normal <command>opreport</command>
1389 output.
1390 </para>
1391 <para>
1392 Above the non-indented line we find the functions that called this
1393 function (for example, <function>repeat()</function> calls
1394 <function>strfry()</function>). The samples and percentage values here
1395 refer to the number of times we took a sample where this call was found
1396 in the stack; the percentage is relative to all other callers of the
1397 function we're focussing on. Note that these values are
1398 <emphasis>not</emphasis> call counts; they only reflect the call stack
1399 every time a sample is taken; that is, if a call is found in the stack
1400 at the time of a sample, it is recorded in this count.
1401 </para>
1402 <para>
1403 Below the line are functions that are called by
1404 <function>strfry()</function> (called <emphasis>callees</emphasis>).
1405 It's clear here that <function>strfry()</function> calls
1406 <function>random_r()</function>. We also see a special entry with a
1407 "[self]" marker. This records the normal samples for the function, but
1408 the percentage becomes relative to all callees. This allows you to
1409 compare time spent in the function itself compared to functions it
1410 calls. Note that if a function calls itself, then it will appear in the
1411 list of callees of itself, but without the "[self]" marker; so recursive
1412 calls are still clearly separable.
1413 </para>
1414 <para>
1415 You may have noticed that the output lists <function>main()</function>
1416 as calling <function>strfry()</function>, but it's clear from the source
1417 that this doesn't actually happen. See <xref
1418 linkend="interpreting-callgraph" /> for an explanation.
1419 </para>
1420 </sect3>
1421 <sect3 id="cg-with-jitsupport">
1422 <title>Callgraph is not supported with JIT samples</title>
1423 <para>
1424 Callgraph output where anonymously mapped code is in the callstack can sometimes be misleading.
1425 For all such code, the samples for the anonymously mapped code are stored in a samples subdirectory
1426 named <filename>{anon:anon}/&lt;tgid&gt;.&lt;begin_addr&gt;.&lt;end_addr&gt;</filename>.
1427 As stated earlier, if this anonymously mapped code is JITed code from a supported VM like Java,
1428 OProfile creates an ELF file to provide a (somewhat) permanent backing file for the code.
1429 However, when viewing callgraph output, any anonymously mapped code in the callstack
1430 will be attributed to <filename>anon (&lt;tgid&gt;: range:&lt;begin_addr&gt;-&lt;end_addr&gt;</filename>,
1431 even if a <filename>.jo</filename> ELF file had been created for it.  See the example below.
1432 </para>
1433 <screen>
1434 -------------------------------------------------------------------------------
1435   1         2.2727  libj9ute23.so            java.bin                 traceV
1436   2         4.5455  libj9ute23.so            java.bin                 utsTraceV
1437   4         9.0909  libj9trc23.so            java.bin                 fillInUTInterfaces
1438   37       84.0909  libj9trc23.so            java.bin                 twGetSequenceCounter
1439 8         0.0154  libj9prt23.so            java.bin                 j9time_hires_clock
1440   27       61.3636  anon (tgid:10014 range:0x100000-0x103000) java.bin                 (no symbols)
1441   9        20.4545  libc-2.4.so              java.bin                 gettimeofday
1442   8        18.1818  libj9prt23.so            java.bin                 j9time_hires_clock [self]
1443 -------------------------------------------------------------------------------
1444 </screen>
1445 <para>
1446 The output shows that "anon (tgid:10014 range:0x100000-0x103000)" was a callee of
1447 <code>j9time_hires_clock</code>, even though the ELF file <filename>10014.jo</filename> was
1448 created for this profile run.  Unfortunately, there is currently no way to correlate
1449 that anonymous callgraph entry with its corresponding <filename>.jo</filename> file.
1450 </para>
1451 </sect3>
1452
1453
1454 </sect2> <!-- opreport-callgraph -->
1455
1456 <sect2 id="opreport-diff">
1457 <title>Differential profiles with <command>opreport</command></title>
1458
1459 <para>
1460 Often, we'd like to be able to compare two profiles. For example, when
1461 analysing the performance of an application, we'd like to make code
1462 changes and examine the effect of the change. This is supported in
1463 <command>opreport</command> by giving a profile specification that
1464 identifies two different profiles. The general form is of:
1465 </para>
1466 <screen>
1467 $ opreport &lt;shared-spec&gt; { &lt;first-profile&gt; } { &lt;second-profile&gt; }
1468 </screen>
1469 <note><para>
1470 We lost our Dragon book down the back of the sofa, so you have to be
1471 careful to have spaces around those braces, or things will get
1472 hopelessly confused. We can only apologise.
1473 </para></note>
1474 <para>
1475 For each of the profiles, the shared section is prefixed, and then the
1476 specification is analysed. The usual parameters work both within the
1477 shared section, and in the sub-specification within the curly braces.
1478 </para>
1479 <para>
1480 A typical way to use this feature is with archives created with
1481 <command>oparchive</command>. Let's look at an example:
1482 </para>
1483 <screen>
1484 $ operf ./a
1485 $ oparchive -o orig ./a
1486   # edit and recompile a
1487 $ operf ./a
1488   # now compare the current profile of a with the archived profile
1489 $ opreport  --session-dir=`pwd`/oprofile_data/ -xl ./a { archive:./orig } { }
1490 CPU: PIII, speed 863.233 MHz (estimated)
1491 Counted CPU_CLK_UNHALTED events (clocks processor is not halted) with a
1492 unit mask of 0x00 (No unit mask) count 100000
1493 samples  %        diff %    symbol name
1494 92435    48.5366  +0.4999   a
1495 54226    ---      ---       c
1496 49222    25.8459  +++       d
1497 48787    25.6175  -2.2e-01  b
1498 </screen>
1499 <para>
1500 Note that we specified an empty second profile in the curly braces, as
1501 we wanted to use the current session; alternatively, we could
1502 have specified another archive, or a tgid etc. We specified the binary
1503 <command>a</command> in the shared section, so we matched that in both
1504 the profiles we're diffing.
1505 </para>
1506 <para>
1507 As in the normal output, the results are sorted by the number of
1508 samples, and the percentage field represents the relative percentage of
1509 the symbol's samples in the second profile.
1510 </para>
1511 <para>
1512 Notice the new column in the output. This value represents the
1513 percentage change of the relative percent between the first and the
1514 second profile: roughly, "how much more important this symbol is".
1515 Looking at the symbol <function>a()</function>, we can see that it took
1516 roughly the same amount of the total profile in both the first and the
1517 second profile. The function <function>c()</function> was not in the new
1518 profile, so has been marked with <function>---</function>. Note that the
1519 sample value is the number of samples in the first profile; since we're
1520 displaying results for the second profile, we don't list a percentage
1521 value for it, as it would be meaningless. <function>d()</function> is
1522 new in the second profile, and consequently marked with
1523 <function>+++</function>.
1524 </para>
1525 <para>
1526 When comparing profiles between different binaries, it should be clear
1527 that functions can change in terms of VMA and size. To avoid this
1528 problem, <command>opreport</command> considers a symbol to be the same
1529 if the symbol name, image name, and owning application name all match;
1530 any other factors are ignored. Note that the check for application name
1531 means that trying to compare library profiles between two different
1532 applications will not work as you might expect: each symbol will be
1533 considered different.
1534 </para>
1535
1536 </sect2> <!-- opreport-diff -->
1537
1538 <sect2 id="opreport-anon">
1539 <title>Anonymous executable mappings</title>
1540 <para>
1541 Many applications, typically ones involving dynamic compilation into
1542 machine code (just-in-time, or "JIT", compilation), have executable mappings that
1543 are not backed by an ELF file. <command>opreport</command> has basic support for showing the
1544 samples taken in these regions; for example:
1545 <screen>
1546 $ opreport /usr/bin/mono -l
1547 CPU: ppc64 POWER5, speed 1654.34 MHz (estimated)
1548 Counted CYCLES events (Processor Cycles using continuous sampling) with a unit mask of 0x00 (No unit mask) count 100000
1549 samples  %        image name                                    symbol name
1550 47       58.7500  mono                                          (no symbols)
1551 14       17.5000  anon (tgid:3189 range:0xf72aa000-0xf72fa000)  (no symbols)
1552 9        11.2500  anon (tgid:3189 range:0xf6cca000-0xf6dd9000)  (no symbols)
1553 .        .        .                                             .
1554 </screen>
1555 </para>
1556 <para>
1557 Note that, since such mappings are dependent upon individual invocations of
1558 a binary, these mappings are always listed as a dependent image.
1559 Equally, the results are not affected by the <option>--merge</option>
1560 option.
1561 </para>
1562 <para>
1563 As shown in the opreport output above, OProfile is unable to attribute the samples to any
1564 symbol(s) because there is no ELF file for this code.
1565 Enhanced support for JITed code is now available for some virtual machines; 
1566 e.g., the Java Virtual Machine.  For details about OProfile output for
1567 JITed code, see <xref linkend="getting-jit-reports" />.
1568 </para>
1569 <para>For more information about JIT support in OProfile, see <xref linkend="jitsupport"/>.
1570 </para>
1571 </sect2> <!-- opreport-anon -->
1572
1573 <sect2 id="opreport-xml">
1574 <title>XML formatted output</title>
1575 <para>
1576 The --xml option can be used to generate XML instead of the usual
1577 text format.  This allows opreport to eliminate some of the constraints
1578 dictated by the two dimensional text format.  For example, it is possible
1579 to separate the sample data across multiple events, cpus and threads.  The XML
1580 schema implemented by opreport is found in doc/opreport.xsd. It contains
1581 more detailed comments about the structure of the XML generated by opreport.
1582 </para>
1583 <para>
1584 Since XML is consumed by a client program rather than a user, its structure
1585 is fairly static.  In particular, the --sort option is incompatible with the
1586 --xml option.  Percentages are not dislayed in the XML so the options related
1587 to percentages will have no effect.  Full pathnames are always displayed in
1588 the XML so --long-filenames is not necessary.  The --details option will cause
1589 all of the individual sample data to be included in the XML as well as the
1590 instruction byte stream for each symbol (for doing disassembly) and can result
1591 in very large XML files.
1592 </para>
1593 </sect2> <!-- opreport-xml -->
1594
1595 <sect2 id="opreport-options">
1596 <title>Options for <command>opreport</command></title>
1597
1598 <variablelist>
1599 <varlistentry><term><option>--accumulated / -a</option></term><listitem><para>
1600 Accumulate sample and percentage counts in the symbol list.
1601 </para></listitem></varlistentry>
1602 <varlistentry><term><option>--callgraph / -c</option></term><listitem><para>
1603 Show callgraph information.
1604 </para></listitem></varlistentry>
1605 <varlistentry><term><option>--debug-info / -g</option></term><listitem><para>
1606 Show source file and line for each symbol.
1607 </para></listitem></varlistentry>
1608 <varlistentry><term><option>--demangle / -D none|normal|smart</option></term><listitem><para>
1609 none: no demangling. normal: use default demangler (default) smart: use
1610 pattern-matching to make C++ symbol demangling more readable.
1611 </para></listitem></varlistentry>
1612 <varlistentry><term><option>--details / -d</option></term><listitem><para>
1613 Show per-instruction details for all selected symbols. Note that, for
1614 binaries without symbol information, the VMA values shown are raw file
1615 offsets for the image binary.
1616 </para></listitem></varlistentry>
1617 <varlistentry><term><option>--exclude-dependent / -x</option></term><listitem><para>
1618 Do not include application-specific images for libraries, kernel modules
1619 and the kernel..
1620 </para></listitem></varlistentry>
1621 <varlistentry><term><option>--exclude-symbols / -e [symbols]</option></term><listitem><para>
1622 Exclude all the symbols in the given comma-separated list.
1623 </para></listitem></varlistentry>
1624 <varlistentry><term><option>--global-percent / -%</option></term><listitem><para>
1625 Make all percentages relative to the whole profile.
1626 </para></listitem></varlistentry>
1627 <varlistentry><term><option>--help / -? / --usage</option></term><listitem><para>
1628 Show help message.
1629 </para></listitem></varlistentry>
1630 <varlistentry><term><option>--image-path / -p [paths]</option></term><listitem><para>
1631 Comma-separated list of additional paths to search for binaries.
1632 This is needed to find kernel modules.
1633 </para></listitem></varlistentry>
1634 <varlistentry><term><option>--root / -R [path]</option></term><listitem><para>
1635 A path to a filesystem to search for additional binaries.
1636 </para></listitem></varlistentry>
1637 <varlistentry><term><option>--include-symbols / -i [symbols]</option></term><listitem><para>
1638 Only include symbols in the given comma-separated list.
1639 </para></listitem></varlistentry>
1640 <varlistentry><term><option>--long-filenames / -f</option></term><listitem><para>
1641 Output full paths instead of basenames.
1642 </para></listitem></varlistentry>
1643 <varlistentry><term><option>--merge / -m [lib,cpu,tid,tgid,unitmask,all]</option></term><listitem><para>
1644 Merge any profiles separated in a --separate session.
1645 </para></listitem></varlistentry>
1646 <varlistentry><term><option>--no-header</option></term><listitem><para>
1647 Don't output a header detailing profiling parameters.
1648 </para></listitem></varlistentry>
1649 <varlistentry><term><option>--output-file / -o [file]</option></term><listitem><para>
1650 Output to the given file instead of stdout.
1651 </para></listitem></varlistentry>
1652 <varlistentry><term><option>--reverse-sort / -r</option></term><listitem><para>
1653 Reverse the sort from the default.
1654 </para></listitem></varlistentry>
1655 <varlistentry><term><option>--session-dir=dir_path</option></term><listitem><para>
1656 Use sample database from the specified directory <filename>dir_path</filename> instead
1657 of the default location. If this option is not specified, then opreport will search for
1658 samples in <filename>&lt;cur_dir&gt;/oprofile_data</filename>
1659 first. If that directory does not exist, the standard session-dir of
1660 <filename>/var/lib/oprofile</filename> is used
1661 as the session directory.
1662 </para></listitem></varlistentry>
1663 <varlistentry><term><option>--show-address / -w</option></term><listitem><para>
1664 Show the VMA address of each symbol (off by default).
1665 </para></listitem></varlistentry>
1666 <varlistentry><term><option>--sort / -s [vma,sample,symbol,debug,image]</option></term><listitem><para>
1667 Sort the list of symbols by, respectively, symbol address,
1668 number of samples, symbol name, debug filename and line number,
1669 binary image filename.
1670 </para></listitem></varlistentry>
1671 <varlistentry><term><option>--symbols / -l</option></term><listitem><para>
1672 List per-symbol information instead of a binary image summary.
1673 </para></listitem></varlistentry>
1674 <varlistentry><term><option>--threshold / -t [percentage]</option></term><listitem><para>
1675 Only output data for symbols that have more than the given percentage
1676 of total samples. For profiles using multiple events, if the threshold is reached
1677 for any event, then all sample data for the symbol is shown.
1678 </para></listitem></varlistentry>
1679 <varlistentry><term><option>--verbose / -V [options]</option></term><listitem><para>
1680 Give verbose debugging output.
1681 </para></listitem></varlistentry>
1682 <varlistentry><term><option>--version / -v</option></term><listitem><para>
1683 Show version.
1684 </para></listitem></varlistentry>
1685 <varlistentry><term><option>--xml / -X</option></term><listitem><para>
1686 Generate XML output.
1687 </para></listitem></varlistentry>
1688 </variablelist>
1689
1690 </sect2>
1691
1692 </sect1> <!-- opreport -->
1693
1694 <sect1 id="opannotate">
1695 <title>Outputting annotated source (<command>opannotate</command>)</title>
1696 <para>
1697 The <command>opannotate</command> utility generates annotated source files or assembly listings, optionally
1698 mixed with source.
1699 If you want to see the source file, the profiled application needs to have debug information, and the source
1700 must be available through this debug information. For GCC, you must use the <option>-g</option> option
1701 when you are compiling.
1702 If the binary doesn't contain sufficient debug information, you can still
1703 use <command>opannotate <option>--assembly</option></command> to get annotated assembly
1704 as long as the binary has (at least) symbol information.
1705 </para>
1706 <para>
1707 Note that for the reason explained in <xref linkend="hardware-counters" /> the results can be
1708 inaccurate. The debug information itself can add other problems; for example, the line number for a symbol can be
1709 incorrect. Assembly instructions can be re-ordered and moved by the compiler, and this can lead to
1710 crediting source lines with samples not really "owned" by this line. Also see
1711 <xref linkend="interpreting" />.
1712 </para>
1713 <para>
1714 You can output the annotation to one single file, containing all the source found using the
1715 <option>--source</option>. You can use this in conjunction with <option>--assembly</option>
1716 to get combined source/assembly output.
1717 </para>
1718 <para>
1719 You can also output a directory of annotated source files that maintains the structure of
1720 the original sources. Each line in the annotated source is prepended with the samples
1721 for that line. Additionally, each symbol is annotated giving details for the symbol
1722 as a whole. An example:
1723 </para>
1724 <screen>
1725 $ opannotate --source --output-dir=annotated /usr/local/oprofile-pp/bin/oprofiled
1726 $ ls annotated/home/moz/src/oprofile-pp/daemon/
1727 opd_cookie.h  opd_image.c  opd_kernel.c  opd_sample_files.c  oprofiled.c
1728 </screen>
1729 <para>
1730 Line numbers are maintained in the source files, but each file has
1731 a footer appended describing the profiling details. The actual annotation
1732 looks something like this :
1733 </para>
1734 <screen>
1735 ...
1736                :static uint64_t pop_buffer_value(struct transient * trans)
1737  11510  1.9661 :{ /* pop_buffer_value total:  89901 15.3566 */
1738                :        uint64_t val;
1739                :
1740  10227  1.7469 :        if (!trans->remaining) {
1741                :                fprintf(stderr, "BUG: popping empty buffer !\n");
1742                :                exit(EXIT_FAILURE);
1743                :        }
1744                :
1745                :        val = get_buffer_value(trans->buffer, 0);
1746   2281  0.3896 :        trans->remaining--;
1747   2296  0.3922 :        trans->buffer += kernel_pointer_size;
1748                :        return val;
1749  10454  1.7857 :}
1750 ...
1751 </screen>
1752
1753 <para>
1754 The first number on each line is the number of samples, whilst the second is
1755 the relative percentage of total samples.
1756 </para>
1757
1758 <sect2 id="opannotate-finding-source">
1759 <title>Locating source files</title>
1760 <para>
1761 Of course, <command>opannotate</command> needs to be able to locate the source files
1762 for the binary image(s) in order to produce output. Some binary images have debug
1763 information where the given source file paths are relative, not absolute. You can
1764 specify search paths to look for these files (similar to <command>gdb</command>'s
1765 <option>dir</option> command) with the <option>--search-dirs</option> option.
1766 </para>
1767 <para>
1768 Sometimes you may have a binary image which gives absolute paths for the source files,
1769 but you have the actual sources elsewhere (commonly, you've installed an SRPM for
1770 a binary on your system and you want annotation from an existing profile). You can
1771 use the <option>--base-dirs</option> option to redirect OProfile to look somewhere
1772 else for source files. For example, imagine we have a binary generated from a source
1773 file that is given in the debug information as <filename>/tmp/build/libfoo/foo.c</filename>,
1774 and you have the source tree matching that binary installed in <filename>/home/user/libfoo/</filename>.
1775 You can redirect OProfile to find <filename>foo.c</filename> correctly like this :
1776 </para>
1777 <screen>
1778 $ opannotate --source --base-dirs=/tmp/build/libfoo/ --search-dirs=/home/user/libfoo/ --output-dir=annotated/ /lib/libfoo.so
1779 </screen>
1780 <para>
1781 You can specify multiple (comma-separated) paths to both options.
1782 </para>
1783 </sect2>
1784
1785 <sect2 id="opannotate-details">
1786 <title>Usage of <command>opannotate</command></title>
1787
1788 <variablelist>
1789 <varlistentry><term><option>--assembly / -a</option></term><listitem><para>
1790 Output annotated assembly. If this is combined with --source, then mixed
1791 source / assembly annotations are output.
1792 </para></listitem></varlistentry>
1793 <varlistentry><term><option>--base-dirs / -b [paths]/</option></term><listitem><para>
1794 Comma-separated list of path prefixes. This can be used to point OProfile to a
1795 different location for source files when the debug information specifies an
1796 absolute path on your system for the source that does not exist. The prefix
1797 is stripped from the debug source file paths, then searched in the search dirs
1798 specified by <option>--search-dirs</option>.
1799 </para></listitem></varlistentry>
1800 <varlistentry><term><option>--demangle / -D none|normal|smart</option></term><listitem><para>
1801 none: no demangling. normal: use default demangler (default) smart: use
1802 pattern-matching to make C++ symbol demangling more readable.
1803 </para></listitem></varlistentry>
1804 <varlistentry><term><option>--exclude-dependent / -x</option></term><listitem><para>
1805 Do not include application-specific images for libraries, kernel modules
1806 and the kernel.
1807 </para></listitem></varlistentry>
1808 <varlistentry><term><option>--exclude-file [files]</option></term><listitem><para>
1809 Exclude all files in the given comma-separated list of glob patterns.
1810 This option is supported solely with the <code>--source</code>
1811 option. It can be used to filter out source files in the output using the
1812 following types of specifications:
1813 <itemizedlist>
1814 <listitem>filenames (basename -- i.e., no path)</listitem>
1815 <listitem>filename glob specifications (all files whose base filename matches the given pattern)</listitem>
1816 <listitem>directory segments (all source files located in the specified directory; e.g. "libio")</listitem>
1817 <listitem>directory segment glob specifications (e.g., "libi*")</listitem>
1818 </itemizedlist>
1819 </para></listitem></varlistentry>
1820 <varlistentry><term><option>--exclude-symbols / -e [symbols]</option></term><listitem><para>
1821 Exclude all the symbols in the given comma-separated list.
1822 </para></listitem></varlistentry>
1823 <varlistentry><term><option>--help / -? / --usage</option></term><listitem><para>
1824 Show help message.
1825 </para></listitem></varlistentry>
1826 <varlistentry><term><option>--image-path / -p [paths]</option></term><listitem><para>
1827 Comma-separated list of additional paths to search for binaries.
1828 This is needed to find kernel modules.
1829 </para></listitem></varlistentry>
1830 <varlistentry><term><option>--root / -R [path]</option></term><listitem><para>
1831 A path to a filesystem to search for additional binaries.
1832 </para></listitem></varlistentry>
1833 <varlistentry><term><option>--include-file [files]</option></term><listitem><para>
1834 Only include files in the given comma-separated list of glob patterns.
1835 The same rules apply for this option as for the <code>--exclude-file</code> option.
1836 </para></listitem></varlistentry>
1837 <varlistentry><term><option>--include-symbols / -i [symbols]</option></term><listitem><para>
1838 Only include symbols in the given comma-separated list.
1839 </para></listitem></varlistentry>
1840 <varlistentry><term><option>--objdump-params [params]</option></term><listitem><para>
1841 Pass the given parameters as extra values when calling objdump.
1842 If more than one option is to be passed to objdump, the parameters must be enclosed in a
1843 quoted string.
1844 </para>
1845 <para>
1846 An example of where this option is useful is when your toolchain does not
1847 automatically recognize instructions that are specific to your processor.
1848 For example, on IBM POWER7/RHEL 6, objdump must be told that a binary file may have
1849 POWER7-specific instructions. The <command>opannotate</command> option to show the POWER7-specific
1850 instructions is:
1851 <screen>
1852    --objdump-params=-Mpower7
1853 </screen>
1854 </para>
1855 <para>
1856 The <command>opannotate</command> option to show the POWER7-specific instructions,
1857 the source code (--source) and the line numbers (-l) would be:
1858 <screen>
1859    --objdump-params="-Mpower7 -l --source"
1860 </screen>
1861 </para></listitem></varlistentry>
1862 <varlistentry><term><option>--output-dir / -o [dir]</option></term><listitem><para>
1863 Output directory. This makes opannotate output one annotated file for each
1864 source file. This option can't be used in conjunction with --assembly.
1865 </para></listitem></varlistentry>
1866 <varlistentry><term><option>--search-dirs / -d [paths]</option></term><listitem><para>
1867 Comma-separated list of paths to search for source files. This is useful to find
1868 source files when the debug information only contains relative paths.
1869 </para></listitem></varlistentry>
1870 <varlistentry><term><option>--source / -s</option></term><listitem><para>
1871 Output annotated source. This requires debugging information to be available
1872 for the binaries.
1873 </para></listitem></varlistentry>
1874 <varlistentry><term><option>--session-dir=dir_path</option></term><listitem><para>
1875 Use sample database from the specified directory <filename>dir_path</filename> instead
1876 of the default location. If this option is not specified, then opannotate will search for
1877 samples in <filename>&lt;cur_dir&gt;/oprofile_data</filename>
1878 first. If that directory does not exist, the standard session-dir of
1879 <filename>/var/lib/oprofile</filename> is used
1880 as the session directory.
1881 </para></listitem></varlistentry>
1882 <varlistentry><term><option>--threshold / -t [percentage]</option></term><listitem><para>
1883 For annotated assembly, only output data for symbols that have more than the given percentage
1884 of total samples. For profiles using multiple events, if the threshold is reached
1885 for any event, then all sample data for the symbol is shown.
1886 </para>
1887 <para>
1888 For annotated source, only output data for source files that have more than the given percentage
1889 of total samples. For profiles using multiple events, if the threshold is reached
1890 for any event, then all sample data for the source file is shown.
1891 </para></listitem></varlistentry>
1892 <varlistentry><term><option>--verbose / -V [options]</option></term><listitem><para>
1893 Give verbose debugging output.
1894 </para></listitem></varlistentry>
1895 <varlistentry><term><option>--version / -v</option></term><listitem><para>
1896 Show version.
1897 </para></listitem></varlistentry>
1898 </variablelist>
1899
1900
1901 </sect2> <!-- opannotate-details -->
1902
1903 </sect1> <!-- opannotate -->
1904
1905 <sect1 id="getting-jit-reports">
1906         <title>OProfile results with JIT samples</title>
1907         <para>
1908                 After profiling a Java (or other supported VM) application, the
1909                 OProfile JIT support creates ELF binaries from the
1910                 intermediate files that were written by the agent library.
1911                 The ELF binaries are named <filename>&lt;tgid&gt;.jo</filename>.
1912                 With the symbol information stored in these ELF files, it is
1913                 possible to map samples to the appropriate symbols.
1914         </para>
1915         <para>
1916                 The usual analysis tools (<command>opreport</command> and/or 
1917                 <command>opannotate</command>) can now be used
1918                 to get symbols and assembly code for the instrumented VM processes.
1919         </para>
1920 <para>
1921 Below is an example of a profile report of a Java application that has been
1922 instrumented with the provided agent library.
1923 <screen>
1924 $ opreport -l /usr/lib/jvm/jre-1.5.0-ibm/bin/java
1925 CPU: Core Solo / Duo, speed 2167 MHz (estimated)
1926 Counted CPU_CLK_UNHALTED events (Unhalted clock cycles) with a unit mask of 0x00 (Unhalted core cycles) count 100000
1927 samples  %        image name               symbol name
1928 186020   50.0523  no-vmlinux               no-vmlinux               (no symbols)
1929 34333     9.2380  7635.jo                  java                     void test.f1()
1930 19022     5.1182  libc-2.5.so              libc-2.5.so              _IO_file_xsputn@@GLIBC_2.1
1931 18762     5.0483  libc-2.5.so              libc-2.5.so              vfprintf
1932 16408     4.4149  7635.jo                  java                     void test$HelloThread.run()
1933 16250     4.3724  7635.jo                  java                     void test$test_1.f2(int)
1934 15303     4.1176  7635.jo                  java                     void test.f2(int, int)
1935 13252     3.5657  7635.jo                  java                     void test.f2(int)
1936 5165      1.3897  7635.jo                  java                     void test.f4()
1937 955       0.2570  7635.jo                  java                     void test$HelloThread.run()~
1938
1939 </screen>
1940 </para>
1941 <note><para>
1942           Depending on the JVM that is used, certain options of opreport and opannotate
1943           do NOT work since they rely on debug information (e.g. source code line number)
1944           that is not always available. The Sun JVM does provide the necessary debug
1945           information via the JVMTI[PI] interface,
1946           but other JVMs do not.
1947   </para></note>
1948         <para>
1949                 As you can see in the opreport output, the JIT support agent for Java
1950                 generates symbols to include the class and method signature.
1951                 A symbol with the suffix &tilde;&lt;n&gt; (e.g.
1952                 <code>void test$HelloThread.run()&tilde;1</code>) means that this is
1953                 the &lt;n&gt;th occurrence of the identical name. This happens if a method is re-JITed.
1954                 A symbol with the suffix %&lt;n&gt;, means that the address space of this symbol
1955                 was reused during the sample session (see <xref linkend="overlapping-symbols" />).
1956                 The value &lt;n&gt; is the percentage of time that this symbol/code was present in
1957                 relation to the total lifetime of all overlapping other symbols. A symbol of the form
1958                 <code>&lt;return_val&gt; &lt;class_name&gt;$&lt;method_sig&gt;</code> denotes an
1959                 inner class.
1960         </para>
1961 </sect1>
1962
1963 <sect1 id="opgprof">
1964 <title><command>gprof</command>-compatible output (<command>opgprof</command>)</title>
1965 <para>
1966 If you're familiar with the output produced by <command>GNU gprof</command>,
1967 you may find <command>opgprof</command> useful. It takes a single binary
1968 as an argument, and produces a <filename>gmon.out</filename> file for use
1969 with <command>gprof -p</command>. If call-graph profiling is enabled,
1970 then this is also included.
1971 </para>
1972 <screen>
1973 $ opgprof `which oprofiled` # generates gmon.out file
1974 $ gprof -p `which oprofiled` | head
1975 Flat profile:
1976
1977 Each sample counts as 1 samples.
1978   %   cumulative   self              self     total
1979  time   samples   samples    calls  T1/call  T1/call  name
1980  33.13 206237.00 206237.00                             odb_insert
1981  22.67 347386.00 141149.00                             pop_buffer_value
1982   9.56 406881.00 59495.00                             opd_put_sample
1983   7.34 452599.00 45718.00                             opd_find_image
1984   7.19 497327.00 44728.00                             opd_process_samples
1985 </screen>
1986
1987 <sect2 id="opgprof-details">
1988 <title>Usage of <command>opgprof</command></title>
1989
1990 <variablelist>
1991 <varlistentry><term><option>--help / -? / --usage</option></term><listitem><para>
1992 Show help message.
1993 </para></listitem></varlistentry>
1994 <varlistentry><term><option>--image-path / -p [paths]</option></term><listitem><para>
1995 Comma-separated list of additional paths to search for binaries.
1996 This is needed to find kernel modules.
1997 </para></listitem></varlistentry>
1998 <varlistentry><term><option>--root / -R [path]</option></term><listitem><para>
1999 A path to a filesystem to search for additional binaries.
2000 </para></listitem></varlistentry>
2001 <varlistentry><term><option>--output-filename / -o [file]</option></term><listitem><para>
2002 Output to the given file instead of the default, gmon.out
2003 </para></listitem></varlistentry>
2004 <varlistentry><term><option>--threshold / -t [percentage]</option></term><listitem><para>
2005 Only output data for symbols that have more than the given percentage
2006 of total samples.
2007 </para></listitem></varlistentry>
2008 <varlistentry><term><option>--verbose / -V [options]</option></term><listitem><para>
2009 Give verbose debugging output.
2010 <varlistentry><term><option>--session-dir=dir_path</option></term><listitem><para>
2011 Use sample database from the specified directory <filename>dir_path</filename> instead
2012 of the default location. If this option is not specified, then opgprof will search for
2013 samples in <filename>&lt;cur_dir&gt;/oprofile_data</filename>
2014 first. If that directory does not exist, the standard session-dir of
2015 <filename>/var/lib/oprofile</filename> is used
2016 as the session directory.
2017 </para></listitem></varlistentry>
2018 </para></listitem></varlistentry>
2019 <varlistentry><term><option>--version / -v</option></term><listitem><para>
2020 Show version.
2021 </para></listitem></varlistentry>
2022 </variablelist>
2023
2024 </sect2> <!-- opgprof-details -->
2025
2026 </sect1> <!-- opgprof -->
2027
2028 <sect1 id="oparchive">
2029 <title>Analyzing profile data on another system (<command>oparchive</command>)</title>
2030 <para>
2031         The <command>oparchive</command> utility generates a directory populated
2032         with executable, debug, and oprofile sample files. This directory can be
2033         copied to another (host) machine and analyzed offline, with no further need to
2034         access the data collection machine (target).
2035 </para>
2036
2037 <para>
2038         The following command, executed on the target system, will collect the
2039         sample files, the executables associated with the sample files, and the
2040         debuginfo files associated with the executables and copy them into
2041         <filename>/tmp/current_data</filename>:
2042 </para>
2043
2044 <screen>
2045 # oparchive -o /tmp/current_data
2046 </screen>
2047
2048 <para>
2049         When transferring archived profile data to a host machine for offline analysis,
2050         you need to determine if the oprofile ABI format is compatible between the
2051         target system and the host system; if it isn't, you must run the <command>opimport</command>
2052         command to convert the target's sample data files to the format of your host system.
2053         See <xref linkend="opimport"/> for more details.
2054 </para>
2055
2056 <para>
2057         After your profile data is transferred to the host system and (if necessary)
2058         you have run the <command>opimport</command> command to convert the file
2059         format, you can now run the <command>opreport</command> and
2060         <command>opannotate</command> commands.  However, you must provide an
2061         "archive specification" to let these post-processing tools know where to find
2062         of the profile data (sample files, executables, etc.); for example:
2063 </para>
2064
2065 <screen>
2066 # opreport archive:/home/user1/my_oprofile_archive --symbols
2067 </screen>
2068
2069 <para>
2070         Furthermore, if your profile was collected on your target system into a session-dir
2071         other than <filename>/var/lib/oprofile</filename>, the <command>oparchive</command>
2072         command will display a message similar to the following:
2073 </para>
2074
2075 <screen>
2076 # NOTE: The sample data in this archive is located at /home/user1/test-stuff/oprofile_data
2077 instead of the standard location of /var/lib/oprofile.  Hence, when using opreport
2078 and other post-processing tools on this archive, you must pass the following option:
2079         --session-dir=/home/user1/test-stuff/oprofile_data
2080 </screen>
2081
2082 <para>
2083         Then the above <command>opreport</command> example would have to include that
2084         <option>--session-dir</option> option.
2085 </para>
2086
2087 <para>
2088 <note>
2089          In some host/target development environments, all target executables, libraries, and
2090          debuginfo files are stored in a root directory on the host to facilitate offline
2091          analysis.  In such cases, the <command>oparchive</command> command collects more data
2092          than is necessary; so, when copying the resulting output of <command>oparchive</command>,
2093          you can skip all of the executables, etc, and just archive the <filename>$SESSION_DIR</filename>
2094          tree located within the output directory you specified in your <command>oparchive</command>
2095          command. Then, when running the <command>opreport</command> or <command>opannotate</command>
2096          commands on your host system, pass the <option>--root</option> option to point to the
2097          location of your target's executables, etc.
2098 </note>
2099 </para>
2100
2101 <sect2 id="oparchive-details">
2102 <title>Usage of <command>oparchive</command></title>
2103
2104 <variablelist>
2105 <varlistentry><term><option>--help / -? / --usage</option></term><listitem><para>
2106 Show help message.
2107 </para></listitem></varlistentry>
2108 <varlistentry><term><option>--exclude-dependent / -x</option></term><listitem><para>
2109 Do not include application-specific images for libraries, kernel modules
2110 and the kernel.
2111 </para></listitem></varlistentry>
2112 <varlistentry><term><option>--image-path / -p [paths]</option></term><listitem><para>
2113 Comma-separated list of additional paths to search for binaries.
2114 This is needed to find kernel modules.
2115 </para></listitem></varlistentry>
2116 <varlistentry><term><option>--root / -R [path]</option></term><listitem><para>
2117 A path to a filesystem to search for additional binaries.
2118 </para></listitem></varlistentry>
2119 <varlistentry><term><option>--output-directory / -o [directory]</option></term><listitem><para>
2120 Output to the given directory. There is no default. This must be specified.
2121 </para></listitem></varlistentry>
2122 <varlistentry><term><option>--list-files / -l</option></term><listitem><para>
2123 Only list the files that would be archived, don't copy them.
2124 </para></listitem></varlistentry>
2125 <varlistentry><term><option>--verbose / -V [options]</option></term><listitem><para>
2126 Give verbose debugging output.
2127 </para></listitem></varlistentry>
2128 <varlistentry><term><option>--session-dir=dir_path</option></term><listitem><para>
2129 Use sample database from the specified directory <filename>dir_path</filename> instead
2130 of the default location. If this option is not specified, then oparchive will search for
2131 samples in <filename>&lt;cur_dir&gt;/oprofile_data</filename>
2132 first. If that directory does not exist, the standard session-dir of
2133 <filename>/var/lib/oprofile</filename> is used
2134 as the session directory.
2135 </para></listitem></varlistentry>
2136 <varlistentry><term><option>--version / -v</option></term><listitem><para>
2137 Show version.
2138 </para></listitem></varlistentry>
2139 </variablelist>
2140
2141 </sect2> <!-- oparchive-details -->
2142
2143 </sect1> <!-- oparchive -->
2144
2145 <sect1 id="opimport">
2146 <title>Converting sample database files (<command>opimport</command>)</title>
2147 <para>
2148         This utility converts sample database files from a foreign binary format (abi) to
2149         the native format. This is required when moving sample files to a (host) system
2150         other than the one used for collection (target system), and the host and target systems are different
2151         architectures. The abi format of the sample files to be imported is described in a
2152         text file located in <filename>$SESSION_DIR/abi</filename>.  If you are unsure if
2153         your target and host systems have compatible architectures (in regard to the OProfile
2154         ABI), simply diff a <filename>$SESSION_DIR/abi</filename> file from the target system
2155         with one from the host system.  If any differences show up at all, you must run the
2156         <command>opimport</command> command.
2157 </para>
2158
2159 <para>
2160         The <command>oparchive</command> command should be used on the machine where
2161         the profile was taken (target) in order to collect sample files and all other necessary
2162         information. The archive directory that is the output from <command>oparchive</command>
2163         should be copied to the system where you wish to perform your performance analysis (host).
2164 </para>
2165
2166 <para>
2167         The following command converts an input sample file to the specified
2168         output sample file using the given abi file as a binary description
2169         of the input file and the curent platform abi as a binary description
2170         of the output file. (NOTE: The ellipses are used to make the example more
2171         compact and cannot be used in an actual command line.)
2172 </para>
2173
2174 <screen>
2175 # opimport -a /tmp/foreign-abi -o /tmp/imported/.../GLOBAL_POWER_EVENTS.200000.1.all.all.all /tmp/archived/var/lib/.../mprime/GLOBAL_POWER_EVENTS.200000.1.all.all.all
2176 </screen>
2177 <para>
2178     Since opimport converts just one file at a time, an example shell script is provided below
2179     that will perform an import/conversion of all sample files in a samples directory collected
2180     from the target system.
2181 <screen>
2182 #!/bin/bash
2183 #Usage: my-import.sh &lt;foreign-abi-fullpathname&gt;
2184
2185 # NOTE: Start from the "samples" directory containing the "current" directory
2186 # to be imported
2187
2188 mkdir current-imported
2189 cd current-imported; (cd ../current; find . -type d ! -name .) |xargs mkdir
2190 cd ../current; mv stats ../StatsSave; find . -type f | while read line; do opimport  -a $1 -o ../current-imported/$line $line; done; mv ../StatsSave stats;
2191 </screen>
2192 </para>
2193 <para>
2194 Example usage:  Assume that on the target system, a profile was collected using a session-dir of
2195 <filename>/var/lib/oprofile</filename>, and then <command>oparchive -o profile1</command> was run.
2196 Then the <filename>profile1</filename> directory is copied to the host system for analysis.  To import
2197 the sample data in <filename>profile1</filename>, you would perform the following steps:
2198 <screen>
2199 $cd profile1/var/lib/oprofile/samples
2200 $my-import.sh `pwd`/../abi
2201 </screen>
2202 </para>
2203 <para>
2204 If the OProfile ABI is truly different on host and target machines, then the end result of running the
2205 above script will place the converted (i.e., imported) files into the <filename>current-imported</filename>
2206 directory.  By default, <command>opreport</command> and other post-profiling tools will look for samples
2207 in <filename>samples/current</filename> of the specified session directory.  So you should either rename
2208 <filename>current-imported</filename> to <filename>current</filename> or specify the session specification of
2209 <command>session:current-imported</command> when running post-profiling tools.
2210 </para>
2211 <para>
2212 If the OProfile ABI is the same on the host and target machines, the <command>my-import.sh</command> script
2213 will print the following message for each sample file:
2214 <screen>
2215 input abi is identical to native. no conversion necessary.
2216 </screen>
2217 </para>
2218 <sect2 id="opimport-details">
2219 <title>Usage of <command>opimport</command></title>
2220
2221 <variablelist>
2222 <varlistentry><term><option>--help / -? / --usage</option></term><listitem><para>
2223 Show help message.
2224 </para></listitem></varlistentry>
2225 <varlistentry><term><option>--abi / -a [filename]</option></term><listitem><para>
2226 Input abi file description location.
2227 </para></listitem></varlistentry>
2228 <varlistentry><term><option>--force / -f</option></term><listitem><para>
2229 Force conversion even if the input and output abi are identical.
2230 </para></listitem></varlistentry>
2231 <varlistentry><term><option>--output / -o [filename]</option></term><listitem><para>
2232 Specify the output filename. If the output file already exists, the file is
2233 not overwritten but data are accumulated in. Sample filename are informative
2234 for post profile tools and must be kept identical, in other word the pathname
2235 from the first path component containing a '{' must be kept as it in the
2236 output filename.
2237 </para></listitem></varlistentry>
2238 <varlistentry><term><option>--verbose / -V</option></term><listitem><para>
2239 Give verbose debugging output.
2240 </para></listitem></varlistentry>
2241 <varlistentry><term><option>--version / -v</option></term><listitem><para>
2242 Show version.
2243 </para></listitem></varlistentry>
2244 </variablelist>
2245
2246 </sect2> <!-- opimport-details -->
2247
2248 </sect1> <!-- opimport -->
2249
2250 </chapter>
2251
2252 <chapter id="interpreting">
2253 <title>Interpreting profiling results</title>
2254 <para>
2255 The standard caveats of profiling apply in interpreting the results from OProfile:
2256 profile realistic situations, profile different scenarios, profile
2257 for as long as a time as possible, avoid system-specific artifacts, don't trust
2258 the profile data too much. Also bear in mind the comments on the performance
2259 counters above - you <emphasis>cannot</emphasis> rely on totally accurate
2260 instruction-level profiling.  However, for almost all circumstances the data
2261 can be useful. Ideally a utility such as Intel's VTUNE would be available to
2262 allow careful instruction-level analysis; go hassle Intel for this, not me ;)
2263 </para>
2264 <sect1 id="irq-latency">
2265 <title>Profiling interrupt latency</title>
2266 <para>
2267 This is an example of how the latency of delivery of profiling interrupts
2268 can impact the reliability of the profiling data. This is pretty much a 
2269 worst-case-scenario example: these problems are fairly rare.
2270 </para>
2271 <screen>
2272 double fun(double a, double b, double c)
2273 {
2274  double result = 0;
2275  for (int i = 0 ; i &lt; 10000; ++i) {
2276   result += a;
2277   result *= b;
2278   result /= c;
2279  }
2280  return result;
2281 }
2282 </screen>
2283 <para>
2284 Here the last instruction of the loop is very costly, and you would expect the result
2285 reflecting that - but (cutting the instructions inside the loop):
2286 </para>
2287 <screen>
2288 $ opannotate -a -t 10 ./a.out
2289
2290      88 15.38% : 8048337:       fadd   %st(3),%st
2291      48 8.391% : 8048339:       fmul   %st(2),%st
2292      68 11.88% : 804833b:       fdiv   %st(1),%st
2293     368 64.33% : 804833d:       inc    %eax
2294                : 804833e:       cmp    $0x270f,%eax
2295                : 8048343:       jle    8048337
2296 </screen>
2297 <para>
2298 The problem comes from the x86 hardware; when the counter overflows the IRQ
2299 is asserted but the hardware has features that can delay the NMI interrupt:
2300 x86 hardware is synchronous (i.e. cannot interrupt during an instruction);
2301 there is also a latency when the IRQ is asserted, and the multiple
2302 execution units and the out-of-order model of modern x86 CPUs also causes
2303 problems. This is the same function, with annotation :
2304 </para>
2305 <screen>
2306 $ opannotate -s -t 10 ./a.out
2307
2308                :double fun(double a, double b, double c)
2309                :{ /* _Z3funddd total:     572 100.0% */
2310                : double result = 0;
2311     368 64.33% : for (int i = 0 ; i &lt; 10000; ++i) {
2312      88 15.38% :  result += a;
2313      48 8.391% :  result *= b;
2314      68 11.88% :  result /= c;
2315                : }
2316                : return result;
2317                :}
2318 </screen>
2319 <para>
2320 The conclusion: don't trust samples coming at the end of a loop,
2321 particularly if the last instruction generated by the compiler is costly. This
2322 case can also occur for branches. Always bear in mind that samples
2323 can be delayed by a few cycles from its real position. That's a hardware
2324 problem and OProfile can do nothing about it.
2325 </para>
2326 </sect1>
2327 <sect1 id="kernel-profiling">
2328 <title>Kernel profiling</title>
2329 <sect2 id="irq-masking">
2330 <title>Interrupt masking</title>
2331 <para>
2332 OProfile uses non-maskable interrupts (NMI) on the P6 generation, Pentium 4,
2333 Athlon, Opteron, Phenom, and Turion processors. These interrupts can occur even in sections of the
2334 kernel where interrupts are disabled, allowing collection of samples in virtually
2335 all executable code.
2336 </para>
2337 </sect2>
2338 <sect2 id="idle">
2339 <title>Idle time</title>
2340 <para>
2341 Your kernel is likely to support halting the processor when a CPU is idle. As
2342 the typical hardware events like <constant>CPU_CLK_UNHALTED</constant> do not
2343 count when the CPU is halted, the kernel profile will not reflect the actual
2344 amount of time spent idle. You can change this behaviour by booting with
2345 the <option>idle=poll</option> option, which uses a different idle routine. This
2346 will appear as <function>poll_idle()</function> in your kernel profile.
2347 </para>
2348 </sect2>
2349 <sect2 id="kernel-modules">
2350 <title>Profiling kernel modules</title>
2351 <para>
2352 OProfile profiles kernel modules by default. However, there are a couple of problems
2353 you may have when trying to get results. First, you may have booted via an initrd;
2354 this means that the actual path for the module binaries cannot be determined automatically.
2355 To get around this, you can use the <option>-p</option> option to the analysis tools
2356 to specify where to look for the kernel modules.
2357 </para>
2358 <para>
2359 In kernel version 2.6, the information on where kernel module binaries are located was removed.
2360 This means OProfile needs guiding with the <option>-p</option> option to find your
2361 modules. Normally, you can just use your standard module top-level directory for this.
2362 Note that due to this problem, OProfile cannot check that the modification times match;
2363 it is your responsibility to make sure you do not modify a binary after a profile
2364 has been created.
2365 </para>
2366 <para>
2367 If you have run <command>insmod</command> or <command>modprobe</command> to insert a module
2368 in a particular directory, it is important that you specify this directory with the 
2369 <option>-p</option> option first, so that it over-rides an older module binary that might
2370 exist in other directories you've specified with <option>-p</option>. It is up to you
2371 to make sure that these values are correct: the kernel simply does not provide enough
2372 information for OProfile to get this information.
2373 </para>
2374 </sect2>
2375 </sect1>
2376
2377 <sect1 id="interpreting-callgraph">
2378 <title>Interpreting call-graph profiles</title>
2379 <para>
2380 Sometimes the results from call-graph profiles may be different from what
2381 you expect to see. The first thing to check is whether the target
2382 binaries where compiled with frame pointers enabled (if the binary was
2383 compiled using <command>gcc</command>'s
2384 <option>-fomit-frame-pointer</option> option, you will not get
2385 meaningful results). Note that as of this writing, the GCC developers
2386 plan to disable frame pointers by default. The Linux kernel is built
2387 without frame pointers by default; there is a configuration option you
2388 can use to turn it on under the "Kernel Hacking" menu.
2389 </para>
2390 <para>
2391 Often you may see a caller of a function that does not actually directly
2392 call the function you're looking at (e.g. if <function>a()</function>
2393 calls <function>b()</function>, which in turn calls
2394 <function>c()</function>, you may see an entry for
2395 <function>a()->c()</function>).  What's actually occurring is that we
2396 are taking samples at the very start (or the very end) of
2397 <function>c()</function>; at these few instructions, we haven't yet
2398 created the new function's frame, so it appears as if
2399 <function>a()</function> is calling directly into
2400 <function>c()</function>. Be careful not to be misled by these
2401 entries.
2402 </para>
2403 <para>
2404 Like the rest of OProfile, call-graph profiling uses a statistical
2405 approach; this means that sometimes a backtrace sample is truncated, or
2406 even partially wrong. Bear this in mind when examining results.
2407 </para>
2408 <!--  FIXME: what do we need here ? -->
2409 </sect1>
2410
2411 <sect1 id="debug-info">
2412 <title>Inaccuracies in annotated source</title>
2413 <sect2 id="effect-of-optimizations">
2414 <title>Side effects of optimizations</title>
2415 <para>
2416 The compiler can introduce some pitfalls in the annotated source output.
2417 The optimizer can move pieces of code in such manner that two line of codes
2418 are interlaced (instruction scheduling). Also debug info generated by the compiler 
2419 can show strange behavior. This is especially true for complex expressions e.g. inside
2420 an if statement:
2421 </para>
2422 <screen>
2423         if (a &amp;&amp; ..
2424             b &amp;&amp; ..
2425             c &amp;&amp;)
2426 </screen>
2427 <para>
2428 here the problem come from the position of line number. The available debug
2429 info does not give enough details for the if condition, so all samples are
2430 accumulated at the position of the right brace of the expression. Using
2431 <command>opannotate <option>-a</option></command> can help to show the real
2432 samples at an assembly level.
2433 </para>
2434 </sect2>
2435 <sect2 id="prologues">
2436 <title>Prologues and epilogues</title>
2437 <para>
2438 The compiler generally needs to generate "glue" code across function calls, dependent
2439 on the particular function call conventions used. Additionally other things
2440 need to happen, like stack pointer adjustment for the local variables; this
2441 code is known as the function prologue. Similar code is needed at function return,
2442 and is known as the function epilogue. This will show up in annotations as
2443 samples at the very start and end of a function, where there is no apparent
2444 executable code in the source.
2445 </para>
2446 </sect2>
2447 <sect2 id="inlined-function">
2448 <title>Inlined functions</title>
2449 <para>
2450 You may see that a function is credited with a certain number of samples, but
2451 the listing does not add up to the correct total. To pick a real example :
2452 </para>
2453 <screen>
2454                :internal_sk_buff_alloc_security(struct sk_buff *skb)
2455  353 2.342%    :{ /* internal_sk_buff_alloc_security total: 1882 12.48% */
2456                :
2457                :        sk_buff_security_t *sksec;
2458   15 0.0995%   :        int rc = 0;
2459                :
2460   10 0.06633%  :        sksec = skb-&gt;lsm_security;
2461  468 3.104%    :        if (sksec &amp;&amp; sksec-&gt;magic == DSI_MAGIC) {
2462                :                goto out;
2463                :        }
2464                :
2465                :        sksec = (sk_buff_security_t *) get_sk_buff_memory(skb);
2466    3 0.0199%   :        if (!sksec) {
2467   38 0.2521%   :                rc = -ENOMEM;
2468                :                goto out;
2469   10 0.06633%  :        }
2470                :        memset(sksec, 0, sizeof (sk_buff_security_t));
2471   44 0.2919%   :        sksec-&gt;magic = DSI_MAGIC;
2472   32 0.2123%   :        sksec-&gt;skb = skb;
2473   45 0.2985%   :        sksec-&gt;sid = DSI_SID_NORMAL;
2474   31 0.2056%   :        skb-&gt;lsm_security = sksec;
2475                :
2476                :      out:
2477                :
2478  146 0.9685%   :        return rc;
2479                :
2480   98 0.6501%   :}
2481 </screen>
2482 <para>
2483 Here, the function is credited with 1,882 samples, but the annotations
2484 below do not account for this. This is usually because of inline functions -
2485 the compiler marks such code with debug entries for the inline function
2486 definition, and this is where <command>opannotate</command> annotates
2487 such samples. In the case above, <function>memset</function> is the most
2488 likely candidate for this problem. Examining the mixed source/assembly
2489 output can help identify such results.
2490 </para>
2491 <para>
2492 This problem is more visible when there is no source file available, in the
2493 following example it's trivially visible the sums of symbols samples is less
2494 than the number of the samples for this file. The difference must be accounted
2495 to inline functions.
2496 </para>
2497 <screen>
2498 /*
2499  * Total samples for file : "arch/i386/kernel/process.c"
2500  *
2501  *    109  2.4616
2502  */
2503
2504  /* default_idle total:     84  1.8970 */
2505  /* cpu_idle total:         21  0.4743 */
2506  /* flush_thread total:      1  0.0226 */
2507  /* prepare_to_copy total:   1  0.0226 */
2508  /* __switch_to total:      18  0.4065 */
2509 </screen>
2510 <para>
2511 The missing samples are not lost, they will be credited to another source
2512 location where the inlined function is defined. The inlined function will be
2513 credited from multiple call site and merged in one place in the annotated
2514 source file so there is no way to see from what call site are coming the
2515 samples for an inlined function.
2516 </para>
2517 <para>
2518 When running <command>opannotate</command>, you may get a warning
2519 "some functions compiled without debug information may have incorrect source line attributions".
2520 In some rare cases, OProfile is not able to verify that the derived source line
2521 is correct (when some parts of the binary image are compiled without debugging
2522 information). Be wary of results if this warning appears.
2523 </para>
2524 <para>
2525 Furthermore, for some languages the compiler can implicitly generate functions,
2526 such as default copy constructors. Such functions are labelled by the compiler
2527 as having a line number of 0, which means the source annotation can be confusing.
2528 </para>
2529 <!-- FIXME so what *actually* happens to those samples ? ignored ? -->
2530 </sect2>
2531 <sect2 id="wrong-linenr-info">
2532 <title>Inaccuracy in line number information</title>
2533 <para>
2534 Depending on your compiler you can fall into the following problem:
2535 </para>
2536 <screen>
2537 struct big_object { int a[500]; };
2538
2539 int main()
2540 {
2541         big_object a, b;
2542         for (int i = 0 ; i != 1000 * 1000; ++i)
2543                 b = a;
2544         return 0;
2545 }
2546
2547 </screen>
2548 <para>
2549 Compiled with <command>gcc</command> 3.0.4 the annotated source is clearly inaccurate:
2550 </para>
2551 <screen>
2552                :int main()
2553                :{  /* main total: 7871 100% */
2554                :        big_object a, b;
2555                :        for (int i = 0 ; i != 1000 * 1000; ++i)
2556                :                b = a;
2557  7871 100%     :        return 0;
2558                :}
2559 </screen>
2560 <para>
2561 The problem here is distinct from the IRQ latency problem; the debug line number
2562 information is not precise enough; again, looking at output of <command>opannoatate -as</command> can help.
2563 </para>
2564 <screen>
2565                :int main()
2566                :{
2567                :        big_object a, b;
2568                :        for (int i = 0 ; i != 1000 * 1000; ++i)
2569                : 80484c0:       push   %ebp
2570                : 80484c1:       mov    %esp,%ebp
2571                : 80484c3:       sub    $0xfac,%esp
2572                : 80484c9:       push   %edi
2573                : 80484ca:       push   %esi
2574                : 80484cb:       push   %ebx
2575                :                b = a;
2576                : 80484cc:       lea    0xfffff060(%ebp),%edx
2577                : 80484d2:       lea    0xfffff830(%ebp),%eax
2578                : 80484d8:       mov    $0xf423f,%ebx
2579                : 80484dd:       lea    0x0(%esi),%esi
2580                :        return 0;
2581     3 0.03811% : 80484e0:       mov    %edx,%edi
2582                : 80484e2:       mov    %eax,%esi
2583     1 0.0127%  : 80484e4:       cld
2584     8 0.1016%  : 80484e5:       mov    $0x1f4,%ecx
2585  7850 99.73%   : 80484ea:       repz movsl %ds:(%esi),%es:(%edi)
2586     9 0.1143%  : 80484ec:       dec    %ebx
2587                : 80484ed:       jns    80484e0
2588                : 80484ef:       xor    %eax,%eax
2589                : 80484f1:       pop    %ebx
2590                : 80484f2:       pop    %esi
2591                : 80484f3:       pop    %edi
2592                : 80484f4:       leave
2593                : 80484f5:       ret
2594 </screen>
2595 <para>
2596 So here it's clear that copying is correctly credited with of all the samples, but the
2597 line number information is misplaced. <command>objdump -dS</command> exposes the
2598 same problem. Note that maintaining accurate debug information for compilers when optimizing is difficult, so this problem is not suprising.
2599 The problem of debug information
2600 accuracy is also dependent on the binutils version used; some BFD library versions
2601 contain a work-around for known problems of <command>gcc</command>, some others do not. This is unfortunate but we must live with that,
2602 since profiling is pointless when you disable optimisation (which would give better debugging entries).
2603 </para>
2604 </sect2>
2605 </sect1>
2606 <sect1 id="symbol-without-debug-info">
2607 <title>Assembly functions</title>
2608 <para>
2609 Often the assembler cannot generate debug information automatically.
2610 This means that you cannot get a source report unless 
2611 you manually define the neccessary debug information; read your assembler documentation for how you might
2612 do that. The only
2613 debugging info needed currently by OProfile is the line-number/filename-VMA association. When profiling assembly
2614 without debugging info you can always get report for symbols, and optionally for VMA, through <command>opreport -l</command>
2615 or <command>opreport -d</command>, but this works only for symbols with the right attributes.
2616 For <command>gas</command> you can get this by
2617 </para>
2618 <screen>
2619 .globl foo
2620         .type   foo,@function
2621 </screen>
2622 <para> 
2623 whilst for <command>nasm</command> you must use
2624 </para>
2625 <screen>
2626           GLOBAL foo:function           ; [1]
2627 </screen>
2628 <para>
2629 Note that OProfile does not need the global attribute, only the function attribute.
2630 </para>
2631 </sect1>
2632 <!-- 
2633
2634 FIXME: I commented this bit out until we've written something ...
2635
2636 improve this ? but look first why this file is special 
2637 <sect2 id="small-functions">
2638 <title>Small functions</title>
2639 <para>
2640 Very small functions can show strange behavior. The file in your source
2641 directory of OProfile <filename>$SRC/test-oprofile/understanding/puzzle.c</filename>
2642 show such example
2643 </para>
2644 </sect2>
2645 --> 
2646
2647 <sect1 id="overlapping-symbols">
2648         <title>Overlapping symbols in JITed code</title>
2649         <para>
2650         Some virtual machines (e.g., Java) may re-JIT a method, resulting in previously
2651         allocated space for a piece of compiled code to be reused. This means that, at one distinct
2652         code address, multiple symbols/methods may be present during the run time of the application.
2653         </para>
2654         <para>
2655         Since OProfile samples are buffered and don&prime;t have timing information, there is no way
2656         to correlate samples with the (possibly) varying address ranges in which the code for a symbol
2657         may reside.
2658         An alternative would be flushing the OProfile sampling buffer when we get an unload event,
2659         but this could result in high overhead.
2660         </para>
2661         <para>
2662         To moderate the problem of overlapping symbols, OProfile tries to select the symbol that was
2663         present at this address range most of the time. Additionally, other overlapping symbols
2664         are truncated in the overlapping area.
2665         This gives reasonable results, because in reality, address reuse typically takes place
2666         during phase changes of the application -- in particular, during application  startup.
2667         Thus, for optimum profiling results, start the sampling session after application startup
2668         and burn in.
2669         </para>
2670 </sect1>
2671
2672 <sect1 id="interpreting_operf_results">
2673 <title>Using operf to profile fork/execs</title>
2674 <para>
2675 When profiling an application that forks one or more new processes, <command>operf</command> will
2676 record samples for both the parent process and forked processes. This is also true even if the
2677 forked process performs an exec of some sort (e.g., <code>execvp</code>). If the
2678 process does <emphasis>not</emphasis> perform an exec, you will see that <command>opreport</command>
2679 will attribute samples for the forked process to the main application executable.  On the other
2680 hand, if the forked process <emphasis>does</emphasis> perform an exec, then <command>opreport</command>
2681 will attribute samples to the executable being exec'ed.
2682 </para>
2683 <para>
2684 To demonstrate this, consider the following examples.
2685 When using <command>operf</command> to profile a single application (either with the <code>--pid</code>
2686 option or <code>command</code> option), the normal <command>opreport</command> summary output
2687 (i.e., invoking <command>opreport</command> with no options) looks something like the following:
2688 <screen>
2689 CPU_CLK_UNHALT...|
2690   samples|      %|
2691 ------------------
2692    112342 100.000 sprintft
2693         CPU_CLK_UNHALT...|
2694           samples|      %|
2695         ------------------
2696            104209 92.7605 libc-2.12.so
2697              7273  6.4740 sprintft
2698               858  0.7637 no-vmlinux
2699                 2  0.0018 ld-2.12.so
2700 </screen>
2701 </para>
2702 <para>
2703 But if you profile an application that does a fork/exec, the <command>opreport</command> summary output
2704 will show samples for both the main application you profiled, as well as the exec'ed program.
2705 An example is shown below where <code>s-m-fork</code> is the main application being profiled, which
2706 in turn forks a process that does an <code>execvp</code> of the <code>memcpyt</code> program.
2707 <screen>
2708 CPU_CLK_UNHALT...|
2709   samples|      %|
2710 ------------------
2711    133382 70.5031 memcpyt
2712         CPU_CLK_UNHALT...|
2713           samples|      %|
2714         ------------------
2715            123852 92.8551 libc-2.12.so
2716              8522  6.3892 memcpyt
2717              1007  0.7550 no-vmlinux
2718                 1 7.5e-04 ld-2.12.so
2719     55804 29.4969 s-m-fork
2720         CPU_CLK_UNHALT...|
2721           samples|      %|
2722         ------------------
2723             51801 92.8267 libc-2.12.so
2724              3589  6.4314 s-m-fork
2725               414  0.7419 no-vmlinux
2726 </screen>
2727 </para>
2728 </sect1>
2729
2730 <sect1 id="hidden-cost">
2731 <title>Other discrepancies</title>
2732 <para>
2733 Another cause of apparent problems is the hidden cost of instructions. A very
2734 common example is two memory reads: one from L1 cache and the other from memory:
2735 the second memory read is likely to have more samples.
2736 There are many other causes of hidden cost of instructions. A non-exhaustive
2737 list: mis-predicted branch, TLB cache miss, partial register stall,
2738 partial register dependencies, memory mismatch stall, re-executed µops. If you want to write
2739 programs at the assembly level, be sure to take a look at the Intel and
2740 AMD documentation at <ulink url="http://developer.intel.com/">http://developer.intel.com/</ulink>
2741 and <ulink url="http://developer.amd.com/devguides.jsp/">http://developer.amd.com/devguides.jsp</ulink>.
2742 </para>
2743 </sect1>
2744 </chapter>
2745
2746 <chapter id="controlling-counter">
2747 <title>Controlling the event counter</title>
2748 <sect1 id="controlling-ocount">
2749 <title>Using <command>ocount</command></title>
2750 <para>
2751 This section describes in detail how <command>ocount</command> is used.
2752 Unless the <option>--events</option> option is specified, <command>ocount</command> will use
2753 the default event for your system. For most systems, the default event is some
2754 cycles-based event, assuming your processor type supports hardware performance
2755 counters. The event specification used for <command>ocount</command> is slightly
2756 different from that required for profiling -- a <emphasis>count</emphasis> value
2757 is not needed. You can see the event information for your CPU using <command>ophelp</command>.
2758 More information on event specification can be found at <xref linkend="eventspec"/>.
2759 </para>
2760 <para>
2761 The <command>ocount</command> command syntax is:
2762 <para>
2763 <screen>ocount [ options ] [ --system-wide | --process-list &lt;pids&gt; | --thread-list &lt;tids&gt; | --cpu-list &lt;cpus&gt; [ command [ args ] ] ]
2764 </screen>
2765 </para></para>
2766 <para>
2767 <command>ocount</command> has 5 run modes:
2768 <para>
2769 <itemizedlist>
2770 <listitem>system-wide</listitem>
2771 <listitem>process-list</listitem>
2772 <listitem>thread-list</listitem>
2773 <listitem>cpu-list</listitem>
2774 <listitem>command</listitem>
2775 </itemizedlist>
2776 </para></para>
2777 <para>
2778 One and only one of these 5 run modes must be specified when you run <command>ocount</command>.
2779 If you run <command>ocount</command> using a run mode other than <code>command [args]</code>, press Ctrl-c
2780 to stop it when finished counting (e.g., when the monitored process ends). If you background <command>ocount</command>
2781 (i.e., with ’&amp;’) while using one these run modes, you must stop it in a controlled manner so that
2782 the data collection process can be shut down cleanly and final results can be displayed.
2783 Use <code>kill -SIGINT &lt;ocount-PID&gt;</code> for this purpose.
2784 </para>
2785 <para>
2786 Following is a description of the <command>ocount</command> options.
2787 </para>
2788 <variablelist>
2789         <varlistentry>
2790                 <term><option>command [args]</option></term>
2791                 <listitem><para>
2792                 The command or application to be profiled. The <emphasis>[args]</emphasis> are the input arguments
2793         that the command or application requires. The command and its arguments must be positioned at the
2794         end of the command line, after all other <command>ocount</command> options.
2795         </para></listitem>
2796         </varlistentry>
2797         <varlistentry>
2798                 <term><option>--process-list / -p [PIDs]</option></term>
2799                 <listitem><para>
2800                 Use this option to count events for one or more already-running applications, specified via
2801         a comma-separated list (PIDs). Event counts will be collected for all children of the
2802         passed process(es) as well.
2803                 </para></listitem>
2804         </varlistentry>
2805         <varlistentry>
2806                 <term><option>--thread-list / -r [TIDs]</option></term>
2807                 <listitem><para>
2808                 Use this option to count events for one or more already-running threads, specified via
2809         a comma-separated list (TIDs). Event counts will <emphasis>not</emphasis> be collected
2810         for any children of the passed thread(s).
2811                 </para></listitem>
2812         </varlistentry>
2813         <varlistentry>
2814                 <term><option>--system-wide / -s</option></term>
2815                 <listitem><para>
2816                 This option is for counting events for all processes running on your system. You must have
2817         root authority to run <command>ocount</command> in this mode.
2818         </para></listitem>
2819         </varlistentry>
2820         <varlistentry>
2821                 <term><option>--cpu-list / -C [CPUs]</option></term>
2822                 <listitem><para>
2823                 This option is for counting events on a subset of processors on your system. You must have
2824         root authority to run <command>ocount</command> in this mode. This is a comma-separated list,
2825         where each element in the list may be either a single processor number or a range of processor
2826         numbers; for example: ’-C 2,3,4-11,15’.
2827                 </para></listitem>
2828         </varlistentry>
2829         <varlistentry>
2830                 <term><option>--events / -e  [event1[,event2[,...]]]</option></term>
2831                 <listitem><para>
2832                 This option is for passing a comma-separated list of event specifications
2833                 for counting. Each event spec is of the form:
2834                 </para>
2835                 <screen>name[:unitmask[:kernel[:user]]]</screen>
2836                 <para>
2837                 When no event specification is given, the default event for the running
2838                 processor type will be used for counting. Use <command>ophelp</command>
2839                 to list the available events for your processor type.
2840                 </para></listitem>
2841         </varlistentry>
2842         <varlistentry>
2843                 <term><option>--separate-thread / -t</option></term>
2844                 <listitem><para>
2845         This option can be used in conjunction with either the <code>--process-list</code> or
2846         <code>--thread-list</code> option to display event counts on a per-thread (per-process) basis.
2847         Without this option, all counts are aggregated.
2848                 </para></listitem>
2849         </varlistentry>
2850         <varlistentry>
2851                 <term><option>--separate-cpu / -c</option></term>
2852                 <listitem><para>
2853                 This option can be used in conjunction with either the <code>--system-wide</code> or
2854                 <code>--cpu-list</code> option to display event counts on a per-cpu basis. Without this option,
2855                 all counts are aggregated.
2856                 </para></listitem>
2857         </varlistentry>
2858         <varlistentry>
2859                 <term><option>--time-interval / -i interval_length[:num_intervals]</option></term>
2860                 <listitem><para>
2861                 <command>Note: </command>The <code>interval_length</code> is given in milliseconds.
2862               However, the current implementation only supports 100 ms
2863               granularity, so the given <code>interval_length</code> will be rounded
2864               to the nearest 100 ms.  Results collected for each time
2865               interval are printed immediately instead of the default
2866               of one dump of cumulative event counts at the end of the
2867               run.  Counters are reset to zero at the start of each
2868               interval.
2869                 </para>
2870                 <para>
2871               If <code>num_intervals</code> is specified, ocount exits after the
2872               specified number of intervals occur.
2873                 </para></listitem>
2874         </varlistentry>
2875         <varlistentry>
2876            <term><option>--brief-format / -b</option></term>
2877                 <listitem><para>
2878                 Use this option to print results in the following brief format:
2879                 <para><screen>
2880                   [optional cpu or thread,]&lt;event_name&gt;,&lt;count&gt;,&lt;percent_time_enabled&gt;
2881                   [        &lt;int&gt;         ,]&lt;  string  &gt;,&lt; u64 &gt;,&lt;     double         &gt;
2882         </screen></para>
2883         If <code>--timer-interval</code> is specified, a separate line formatted as
2884         <para><screen>
2885                   timestamp,&lt;num_seconds_since_epoch&gt;[.n]
2886         </screen></para>
2887         is printed ahead of each dump of event counts. If the time interval specified is
2888         less than one second, the timestamp will have 1/10 second precision.
2889                 </para></listitem>
2890         </varlistentry>
2891
2892         <varlistentry>
2893                 <term><option>--output-file / -f outfile_name</option></term>
2894                 <listitem><para>
2895                 Results are written to outfile_name instead of interactively to the terminal.
2896                 </para></listitem>
2897         </varlistentry>
2898         <varlistentry>
2899                 <term><option>--verbose / -V</option></term>
2900                 <listitem><para>
2901                 Use this option to increase the verbosity of the output.
2902                 </para></listitem>
2903         </varlistentry>
2904         <varlistentry>
2905                 <term><option>--version -v </option></term>
2906                 <listitem><para>
2907                 Show <command>ocount</command> version.
2908                 </para></listitem>
2909         </varlistentry>
2910         <varlistentry>
2911                 <term><option>--help / -h</option></term>
2912                 <listitem><para>
2913                 Show a help message.
2914                 </para></listitem>
2915         </varlistentry>
2916 </variablelist>
2917
2918 </sect1>
2919 </chapter>
2920
2921
2922 <chapter id="ack">
2923 <title>Acknowledgments</title>
2924 <para>
2925 Thanks to (in no particular order) : Arjan van de Ven, Rik van Riel, Juan Quintela, Philippe Elie,
2926 Phillipp Rumpf, Tigran Aivazian, Alex Brown, Alisdair Rawsthorne, Bob Montgomery, Ray Bryant, H.J. Lu,
2927 Jeff Esper, Will Cohen, Graydon Hoare, Cliff Woolley, Alex Tsariounov, Al Stone, Jason Yeh,
2928 Randolph Chung, Anton Blanchard, Richard Henderson, Andries Brouwer, Bryan Rittmeyer,
2929 Maynard P. Johnson,
2930 Richard Reich (rreich@rdrtech.com), Zwane Mwaikambo, Dave Jones, Charles Filtness; and finally Pulp, for "Intro".
2931 </para>
2932 </chapter>
2933
2934 </book>