man-pages/man1/sprof.1.html
2021-03-31 01:06:50 +01:00

390 lines
11 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of SPROF</TITLE>
</HEAD><BODY>
<H1>SPROF</H1>
Section: Linux User Manual (1)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
sprof - read and display shared object profiling data
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>sprof</B> [<I>option</I>]... <I>shared-object-path</I> [<I>profile-data-path</I>]
</PRE>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The
<B>sprof</B>
command displays a profiling summary for the
shared object (shared library) specified as its first command-line argument.
The profiling summary is created using previously generated
profiling data in the (optional) second command-line argument.
If the profiling data pathname is omitted, then
<B>sprof</B>
will attempt to deduce it using the soname of the shared object,
looking for a file with the name
<I>&lt;soname&gt;.profile</I>
in the current directory.
<A NAME="lbAE">&nbsp;</A>
<H2>OPTIONS</H2>
The following command-line options specify the profile output
to be produced:
<DL COMPACT>
<DT id="1"><B>-c</B>, <B>--call-pairs</B>
<DD>
Print a list of pairs of call paths for the interfaces exported
by the shared object,
along with the number of times each path is used.
<DT id="2"><B>-p</B>, <B>--flat-profile</B>
<DD>
Generate a flat profile of all of the functions in the monitored object,
with counts and ticks.
<DT id="3"><B>-q</B>, <B>--graph</B>
<DD>
Generate a call graph.
</DL>
<P>
If none of the above options is specified,
then the default behavior is to display a flat profile and a call graph.
<P>
The following additional command-line options are available:
<DL COMPACT>
<DT id="4"><B>-?</B>, <B>--help</B>
<DD>
Display a summary of command-line options and arguments and exit.
<DT id="5"><B>--usage</B>
<DD>
Display a short usage message and exit.
<DT id="6"><B>-V</B>, <B>--version</B>
<DD>
Display the program version and exit.
</DL>
<A NAME="lbAF">&nbsp;</A>
<H2>CONFORMING TO</H2>
The
<B>sprof</B>
command is a GNU extension, not present in POSIX.1.
<A NAME="lbAG">&nbsp;</A>
<H2>EXAMPLE</H2>
The following example demonstrates the use of
<B>sprof</B>.
The example consists of a main program that calls two functions
in a shared object.
First, the code of the main program:
<P>
$ <B>cat prog.c</B>
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
<P>
void x1(void);
void x2(void);
<P>
int
main(int argc, char *argv[])
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;x1();
<BR>&nbsp;&nbsp;&nbsp;&nbsp;x2();
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<P>
The functions
<I>x1</I>()
and
<I>x2</I>()
are defined in the following source file that is used to
construct the shared object:
<P>
$ <B>cat libdemo.c</B>
#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;
<P>
void
consumeCpu1(int lim)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;j;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(j&nbsp;=&nbsp;0;&nbsp;j&nbsp;&lt;&nbsp;lim;&nbsp;j++)
<TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TT>getppid();<BR>
}
<P>
void
x1(void) {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;j;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(j&nbsp;=&nbsp;0;&nbsp;j&nbsp;&lt;&nbsp;100;&nbsp;j++)
<TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TT>consumeCpu1(200000);<BR>
}
<P>
void
consumeCpu2(int lim)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;j;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(j&nbsp;=&nbsp;0;&nbsp;j&nbsp;&lt;&nbsp;lim;&nbsp;j++)
<TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TT>getppid();<BR>
}
<P>
void
x2(void)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;j;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(j&nbsp;=&nbsp;0;&nbsp;j&nbsp;&lt;&nbsp;1000;&nbsp;j++)
<TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TT>consumeCpu2(10000);<BR>
}
<P>
Now we construct the shared object with the real name
<I>libdemo.so.1.0.1</I>,
and the soname
<I>libdemo.so.1</I>:
<P>
$ <B>cc -g -fPIC -shared -Wl,-soname,libdemo.so.1 \</B>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B>-o&nbsp;libdemo.so.1.0.1&nbsp;libdemo.c</B>
<P>
Then we construct symbolic links for the library soname and
the library linker name:
<P>
$ <B>ln -sf libdemo.so.1.0.1 libdemo.so.1</B>
$ <B>ln -sf libdemo.so.1 libdemo.so</B>
<P>
Next, we compile the main program, linking it against the shared object,
and then list the dynamic dependencies of the program:
<P>
$ <B>cc -g -o prog prog.c -L. -ldemo</B>
$ <B>ldd prog</B>
<TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TT>linux-vdso.so.1 =&gt; (0x00007fff86d66000)<BR>
<TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TT>libdemo.so.1 =&gt; not found<BR>
<TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TT>libc.so.6 =&gt; /lib64/libc.so.6 (0x00007fd4dc138000)<BR>
<TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TT>/lib64/ld-linux-x86-64.so.2 (0x00007fd4dc51f000)<BR>
<P>
In order to get profiling information for the shared object,
we define the environment variable
<B>LD_PROFILE</B>
with the soname of the library:
<P>
$ <B>export LD_PROFILE=libdemo.so.1</B>
<P>
We then define the environment variable
<B>LD_PROFILE_OUTPUT</B>
with the pathname of the directory where profile output should be written,
and create that directory if it does not exist already:
<P>
$ <B>export LD_PROFILE_OUTPUT=$(pwd)/prof_data</B>
$ <B>mkdir -p $LD_PROFILE_OUTPUT</B>
<P>
<B>LD_PROFILE</B>
causes profiling output to be
<I>appended</I>
to the output file if it already exists,
so we ensure that there is no preexisting profiling data:
<P>
$ <B>rm -f $LD_PROFILE_OUTPUT/$LD_PROFILE.profile</B>
<P>
We then run the program to produce the profiling output,
which is written to a file in the directory specified in
<B>LD_PROFILE_OUTPUT</B>:
<P>
$ <B>LD_LIBRARY_PATH=. ./prog</B>
$ <B>ls prof_data</B>
libdemo.so.1.profile
<P>
We then use the
<B>sprof -p</B>
option to generate a flat profile with counts and ticks:
<P>
$ <B>sprof -p libdemo.so.1 $LD_PROFILE_OUTPUT/libdemo.so.1.profile</B>
Flat profile:
<P>
Each sample counts as 0.01 seconds.
<BR>&nbsp;&nbsp;%&nbsp;&nbsp;&nbsp;cumulative&nbsp;&nbsp;&nbsp;self&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;total
<BR>&nbsp;time&nbsp;&nbsp;&nbsp;seconds&nbsp;&nbsp;&nbsp;seconds&nbsp;&nbsp;&nbsp;&nbsp;calls&nbsp;&nbsp;us/call&nbsp;&nbsp;us/call&nbsp;&nbsp;name
<BR>&nbsp;60.00&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0.06&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0.06&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;100&nbsp;&nbsp;&nbsp;600.00&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;consumeCpu1
<BR>&nbsp;40.00&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0.10&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0.04&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1000&nbsp;&nbsp;&nbsp;&nbsp;40.00&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;consumeCpu2
<BR>&nbsp;&nbsp;0.00&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0.10&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0.00&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0.00&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x1
<BR>&nbsp;&nbsp;0.00&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0.10&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0.00&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0.00&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x2
<P>
The
<B>sprof -q</B>
option generates a call graph:
<P>
$ <B>sprof -q libdemo.so.1 $LD_PROFILE_OUTPUT/libdemo.so.1.profile</B>
<P>
index % time self children called name
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0.00&nbsp;&nbsp;&nbsp;&nbsp;0.00&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;100/100&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x1&nbsp;[1]
[0] 100.0 0.00 0.00 100 consumeCpu1 [0]
-----------------------------------------------
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0.00&nbsp;&nbsp;&nbsp;&nbsp;0.00&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1/1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;UNKNOWN&gt;
[1] 0.0 0.00 0.00 1 x1 [1]
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0.00&nbsp;&nbsp;&nbsp;&nbsp;0.00&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;100/100&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;consumeCpu1&nbsp;[0]
-----------------------------------------------
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0.00&nbsp;&nbsp;&nbsp;&nbsp;0.00&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1000/1000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x2&nbsp;[3]
[2] 0.0 0.00 0.00 1000 consumeCpu2 [2]
-----------------------------------------------
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0.00&nbsp;&nbsp;&nbsp;&nbsp;0.00&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1/1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;UNKNOWN&gt;
[3] 0.0 0.00 0.00 1 x2 [3]
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0.00&nbsp;&nbsp;&nbsp;&nbsp;0.00&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1000/1000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;consumeCpu2&nbsp;[2]
-----------------------------------------------
<P>
Above and below, the &quot;&lt;UNKNOWN&gt;&quot; strings represent identifiers that
are outside of the profiled object (in this example, these are instances of
<I>main()</I>).
<P>
The
<B>sprof -c</B>
option generates a list of call pairs and the number of their occurrences:
<P>
$ <B>sprof -c libdemo.so.1 $LD_PROFILE_OUTPUT/libdemo.so.1.profile</B>
&lt;UNKNOWN&gt; x1 1
x1 consumeCpu1 100
&lt;UNKNOWN&gt; x2 1
x2 consumeCpu2 1000
<A NAME="lbAH">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?1+gprof">gprof</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?1+ldd">ldd</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?8+ld.so">ld.so</A></B>(8)
<A NAME="lbAI">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="7"><A HREF="#lbAB">NAME</A><DD>
<DT id="8"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="9"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="10"><A HREF="#lbAE">OPTIONS</A><DD>
<DT id="11"><A HREF="#lbAF">CONFORMING TO</A><DD>
<DT id="12"><A HREF="#lbAG">EXAMPLE</A><DD>
<DT id="13"><A HREF="#lbAH">SEE ALSO</A><DD>
<DT id="14"><A HREF="#lbAI">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:27 GMT, March 31, 2021
</BODY>
</HTML>