1SPROF(1) Linux User Manual SPROF(1)
2
3
4
6 sprof - read and display shared object profiling data
7
9 sprof [option]... shared-object-path [profile-data-path]
10
12 The sprof command displays a profiling summary for the shared object
13 (shared library) specified as its first command-line argument. The
14 profiling summary is created using previously generated profiling data
15 in the (optional) second command-line argument. If the profiling data
16 pathname is omitted, then sprof will attempt to deduce it using the
17 soname of the shared object, looking for a file with the name <son‐
18 ame>.profile in the current directory.
19
21 The following command-line options specify the profile output to be
22 produced:
23
24 -c, --call-pairs
25 Print a list of pairs of call paths for the interfaces exported
26 by the shared object, along with the number of times each path
27 is used.
28
29 -p, --flat-profile
30 Generate a flat profile of all of the functions in the monitored
31 object, with counts and ticks.
32
33 -q, --graph
34 Generate a call graph.
35
36 If none of the above options is specified, then the default behavior is
37 to display a flat profile and a call graph.
38
39 The following additional command-line options are available:
40
41 -?, --help
42 Display a summary of command-line options and arguments and
43 exit.
44
45 --usage
46 Display a short usage message and exit.
47
48 -V, --version
49 Display the program version and exit.
50
52 The sprof command is a GNU extension, not present in POSIX.1.
53
55 The following example demonstrates the use of sprof. The example con‐
56 sists of a main program that calls two functions in a shared object.
57 First, the code of the main program:
58
59 $ cat prog.c
60 #include <stdlib.h>
61
62 void x1(void);
63 void x2(void);
64
65 int
66 main(int argc, char *argv[])
67 {
68 x1();
69 x2();
70 exit(EXIT_SUCCESS);
71 }
72
73 The functions x1() and x2() are defined in the following source file
74 that is used to construct the shared object:
75
76 $ cat libdemo.c
77 #include <unistd.h>
78
79 void
80 consumeCpu1(int lim)
81 {
82 int j;
83
84 for (j = 0; j < lim; j++)
85 getppid();
86 }
87
88 void
89 x1(void) {
90 int j;
91
92 for (j = 0; j < 100; j++)
93 consumeCpu1(200000);
94 }
95
96 void
97 consumeCpu2(int lim)
98 {
99 int j;
100
101 for (j = 0; j < lim; j++)
102 getppid();
103 }
104
105 void
106 x2(void)
107 {
108 int j;
109
110 for (j = 0; j < 1000; j++)
111 consumeCpu2(10000);
112 }
113
114 Now we construct the shared object with the real name libdemo.so.1.0.1,
115 and the soname libdemo.so.1:
116
117 $ cc -g -fPIC -shared -Wl,-soname,libdemo.so.1 \
118 -o libdemo.so.1.0.1 libdemo.c
119
120 Then we construct symbolic links for the library soname and the library
121 linker name:
122
123 $ ln -sf libdemo.so.1.0.1 libdemo.so.1
124 $ ln -sf libdemo.so.1 libdemo.so
125
126 Next, we compile the main program, linking it against the shared
127 object, and then list the dynamic dependencies of the program:
128
129 $ cc -g -o prog prog.c -L. -ldemo
130 $ ldd prog
131 linux-vdso.so.1 => (0x00007fff86d66000)
132 libdemo.so.1 => not found
133 libc.so.6 => /lib64/libc.so.6 (0x00007fd4dc138000)
134 /lib64/ld-linux-x86-64.so.2 (0x00007fd4dc51f000)
135
136 In order to get profiling information for the shared object, we define
137 the environment variable LD_PROFILE with the soname of the library:
138
139 $ export LD_PROFILE=libdemo.so.1
140
141 We then define the environment variable LD_PROFILE_OUTPUT with the
142 pathname of the directory where profile output should be written, and
143 create that directory if it does not exist already:
144
145 $ export LD_PROFILE_OUTPUT=$(pwd)/prof_data
146 $ mkdir -p $LD_PROFILE_OUTPUT
147
148 LD_PROFILE causes profiling output to be appended to the output file if
149 it already exists, so we ensure that there is no preexisting profiling
150 data:
151
152 $ rm -f $LD_PROFILE_OUTPUT/$LD_PROFILE.profile
153
154 We then run the program to produce the profiling output, which is writ‐
155 ten to a file in the directory specified in LD_PROFILE_OUTPUT:
156
157 $ LD_LIBRARY_PATH=. ./prog
158 $ ls prof_data
159 libdemo.so.1.profile
160
161 We then use the sprof -p option to generate a flat profile with counts
162 and ticks:
163
164 $ sprof -p libdemo.so.1 $LD_PROFILE_OUTPUT/libdemo.so.1.profile
165 Flat profile:
166
167 Each sample counts as 0.01 seconds.
168 % cumulative self self total
169 time seconds seconds calls us/call us/call name
170 60.00 0.06 0.06 100 600.00 consumeCpu1
171 40.00 0.10 0.04 1000 40.00 consumeCpu2
172 0.00 0.10 0.00 1 0.00 x1
173 0.00 0.10 0.00 1 0.00 x2
174
175 The sprof -q option generates a call graph:
176
177 $ sprof -q libdemo.so.1 $LD_PROFILE_OUTPUT/libdemo.so.1.profile
178
179 index % time self children called name
180
181 0.00 0.00 100/100 x1 [1]
182 [0] 100.0 0.00 0.00 100 consumeCpu1 [0]
183 -----------------------------------------------
184 0.00 0.00 1/1 <UNKNOWN>
185 [1] 0.0 0.00 0.00 1 x1 [1]
186 0.00 0.00 100/100 consumeCpu1 [0]
187 -----------------------------------------------
188 0.00 0.00 1000/1000 x2 [3]
189 [2] 0.0 0.00 0.00 1000 consumeCpu2 [2]
190 -----------------------------------------------
191 0.00 0.00 1/1 <UNKNOWN>
192 [3] 0.0 0.00 0.00 1 x2 [3]
193 0.00 0.00 1000/1000 consumeCpu2 [2]
194 -----------------------------------------------
195
196 Above and below, the "<UNKNOWN>" strings represent identifiers that are
197 outside of the profiled object (in this example, these are instances of
198 main()).
199
200 The sprof -c option generates a list of call pairs and the number of
201 their occurrences:
202
203 $ sprof -c libdemo.so.1 $LD_PROFILE_OUTPUT/libdemo.so.1.profile
204 <UNKNOWN> x1 1
205 x1 consumeCpu1 100
206 <UNKNOWN> x2 1
207 x2 consumeCpu2 1000
208
210 gprof(1), ldd(1), ld.so(8)
211
213 This page is part of release 5.07 of the Linux man-pages project. A
214 description of the project, information about reporting bugs, and the
215 latest version of this page, can be found at
216 https://www.kernel.org/doc/man-pages/.
217
218
219
220Linux 2020-06-09 SPROF(1)