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 for (int j = 0; j < lim; j++)
83 getppid();
84 }
85
86 void
87 x1(void) {
88 for (int j = 0; j < 100; j++)
89 consumeCpu1(200000);
90 }
91
92 void
93 consumeCpu2(int lim)
94 {
95 for (int j = 0; j < lim; j++)
96 getppid();
97 }
98
99 void
100 x2(void)
101 {
102 for (int j = 0; j < 1000; j++)
103 consumeCpu2(10000);
104 }
105
106 Now we construct the shared object with the real name libdemo.so.1.0.1,
107 and the soname libdemo.so.1:
108
109 $ cc -g -fPIC -shared -Wl,-soname,libdemo.so.1 \
110 -o libdemo.so.1.0.1 libdemo.c
111
112 Then we construct symbolic links for the library soname and the library
113 linker name:
114
115 $ ln -sf libdemo.so.1.0.1 libdemo.so.1
116 $ ln -sf libdemo.so.1 libdemo.so
117
118 Next, we compile the main program, linking it against the shared ob‐
119 ject, and then list the dynamic dependencies of the program:
120
121 $ cc -g -o prog prog.c -L. -ldemo
122 $ ldd prog
123 linux-vdso.so.1 => (0x00007fff86d66000)
124 libdemo.so.1 => not found
125 libc.so.6 => /lib64/libc.so.6 (0x00007fd4dc138000)
126 /lib64/ld-linux-x86-64.so.2 (0x00007fd4dc51f000)
127
128 In order to get profiling information for the shared object, we define
129 the environment variable LD_PROFILE with the soname of the library:
130
131 $ export LD_PROFILE=libdemo.so.1
132
133 We then define the environment variable LD_PROFILE_OUTPUT with the
134 pathname of the directory where profile output should be written, and
135 create that directory if it does not exist already:
136
137 $ export LD_PROFILE_OUTPUT=$(pwd)/prof_data
138 $ mkdir -p $LD_PROFILE_OUTPUT
139
140 LD_PROFILE causes profiling output to be appended to the output file if
141 it already exists, so we ensure that there is no preexisting profiling
142 data:
143
144 $ rm -f $LD_PROFILE_OUTPUT/$LD_PROFILE.profile
145
146 We then run the program to produce the profiling output, which is writ‐
147 ten to a file in the directory specified in LD_PROFILE_OUTPUT:
148
149 $ LD_LIBRARY_PATH=. ./prog
150 $ ls prof_data
151 libdemo.so.1.profile
152
153 We then use the sprof -p option to generate a flat profile with counts
154 and ticks:
155
156 $ sprof -p libdemo.so.1 $LD_PROFILE_OUTPUT/libdemo.so.1.profile
157 Flat profile:
158
159 Each sample counts as 0.01 seconds.
160 % cumulative self self total
161 time seconds seconds calls us/call us/call name
162 60.00 0.06 0.06 100 600.00 consumeCpu1
163 40.00 0.10 0.04 1000 40.00 consumeCpu2
164 0.00 0.10 0.00 1 0.00 x1
165 0.00 0.10 0.00 1 0.00 x2
166
167 The sprof -q option generates a call graph:
168
169 $ sprof -q libdemo.so.1 $LD_PROFILE_OUTPUT/libdemo.so.1.profile
170
171 index % time self children called name
172
173 0.00 0.00 100/100 x1 [1]
174 [0] 100.0 0.00 0.00 100 consumeCpu1 [0]
175 -----------------------------------------------
176 0.00 0.00 1/1 <UNKNOWN>
177 [1] 0.0 0.00 0.00 1 x1 [1]
178 0.00 0.00 100/100 consumeCpu1 [0]
179 -----------------------------------------------
180 0.00 0.00 1000/1000 x2 [3]
181 [2] 0.0 0.00 0.00 1000 consumeCpu2 [2]
182 -----------------------------------------------
183 0.00 0.00 1/1 <UNKNOWN>
184 [3] 0.0 0.00 0.00 1 x2 [3]
185 0.00 0.00 1000/1000 consumeCpu2 [2]
186 -----------------------------------------------
187
188 Above and below, the "<UNKNOWN>" strings represent identifiers that are
189 outside of the profiled object (in this example, these are instances of
190 main()).
191
192 The sprof -c option generates a list of call pairs and the number of
193 their occurrences:
194
195 $ sprof -c libdemo.so.1 $LD_PROFILE_OUTPUT/libdemo.so.1.profile
196 <UNKNOWN> x1 1
197 x1 consumeCpu1 100
198 <UNKNOWN> x2 1
199 x2 consumeCpu2 1000
200
202 gprof(1), ldd(1), ld.so(8)
203
205 This page is part of release 5.12 of the Linux man-pages project. A
206 description of the project, information about reporting bugs, and the
207 latest version of this page, can be found at
208 https://www.kernel.org/doc/man-pages/.
209
210
211
212Linux 2020-11-01 SPROF(1)