Linux "sprof" Command Line Options and Examples
read and display shared object profiling data

The sprof 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.


Usage:

sprof [option]... shared-object-path [profile-data-path]






Command Line Options:

-c
Print a list of pairs of call paths for the interfaces exported by the shared object, along with thenumber of times each path is used.
sprof -c ...
-p
Generate a flat profile of all of the functions in the monitored object, with counts and ticks.
sprof -p ...
-q
Generate a call graph.If none of the above options is specified, then the default behavior is to display a flat profile and a callgraph.The following additional command-line options are available:
sprof -q ...
-?
Display a summary of command-line options and arguments and exit.
sprof -? ...
--usage
Display a short usage message and exit.
sprof --usage ...
-V
Display the program version and exit.CONFORMING TOThe sprof command is a GNU extension, not present in POSIX.1.EXAMPLEThe following example demonstrates the use of sprof. The example consists of a main program that calls twofunctions in a shared object. First, the code of the main program:$ cat prog.c#include <stdlib.h>void x1(void);void x2(void);intmain(int argc, char *argv[]){x1();x2();exit(EXIT_SUCCESS);}The functions x1() and x2() are defined in the following source file that is used to construct the sharedobject:$ cat libdemo.c#include <unistd.h>voidconsumeCpu1(int lim){int j;for (j = 0; j < lim; j++)getppid();}voidx1(void) {int j;for (j = 0; j < 100; j++)consumeCpu1(200000);}voidconsumeCpu2(int lim){int j;for (j = 0; j < lim; j++)getppid();}voidx2(void){int j;for (j = 0; j < 1000; j++)consumeCpu2(10000);}Now we construct the shared object with the real name libdemo.so.1.0.1, and the soname libdemo.so.1:$ cc -g -fPIC -shared -Wl,-soname,libdemo.so.1 \
sprof -V ...
-o
Then we construct symbolic links for the library soname and the library linker name:$ ln -sf libdemo.so.1.0.1 libdemo.so.1$ ln -sf libdemo.so.1 libdemo.soNext, we compile the main program, linking it against the shared object, and then list the dynamic dependen‐cies of the program:$ cc -g -o prog prog.c -L. -ldemo$ ldd proglinux-vdso.so.1 => (0x00007fff86d66000)libdemo.so.1 => not foundlibc.so.6 => /lib64/libc.so.6 (0x00007fd4dc138000)/lib64/ld-linux-x86-64.so.2 (0x00007fd4dc51f000)In order to get profiling information for the shared object, we define the environment variable LD_PROFILEwith the soname of the library:$ export LD_PROFILE=libdemo.so.1We then define the environment variable LD_PROFILE_OUTPUT with the pathname of the directory where profileoutput should be written, and create that directory if it does not exist already:$ export LD_PROFILE_OUTPUT=$(pwd)/prof_data$ mkdir -p $LD_PROFILE_OUTPUTLD_PROFILE causes profiling output to be appended to the output file if it already exists, so we ensure thatthere is no preexisting profiling data:$ rm -f $LD_PROFILE_OUTPUT/$LD_PROFILE.profileWe then run the program to produce the profiling output, which is written to a file in the directory specifiedin LD_PROFILE_OUTPUT:$ LD_LIBRARY_PATH=. ./prog$ ls prof_datalibdemo.so.1.profileWe then use the sprof -p option to generate a flat profile with counts and ticks:$ sprof -p libdemo.so.1 $LD_PROFILE_OUTPUT/libdemo.so.1.profileFlat profile:Each sample counts as 0.01 seconds.% cumulative self self totaltime seconds seconds calls us/call us/call name60.00 0.06 0.06 100 600.00 consumeCpu140.00 0.10 0.04 1000 40.00 consumeCpu20.00 0.10 0.00 1 0.00 x10.00 0.10 0.00 1 0.00 x2The sprof -q option generates a call graph:$ sprof -q libdemo.so.1 $LD_PROFILE_OUTPUT/libdemo.so.1.profileindex % time self children called name0.00 0.00 100/100 x1 [1][0] 100.0 0.00 0.00 100 consumeCpu1 [0]
sprof -o ...