]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/doc/psd/18.gprof/present.me
MFV r356163,r356197:
[FreeBSD/FreeBSD.git] / share / doc / psd / 18.gprof / present.me
1 .\" Copyright (c) 1982, 1993
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\" 3. Neither the name of the University nor the names of its contributors
13 .\"    may be used to endorse or promote products derived from this software
14 .\"    without specific prior written permission.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 .\" SUCH DAMAGE.
27 .\"
28 .\"     @(#)present.me  8.1 (Berkeley) 6/8/93
29 .\"
30 .sh 1 "Data Presentation"
31 .pp
32 The data is presented to the user in two different formats.
33 The first presentation simply lists the routines
34 without regard to the amount of time their descendants use.
35 The second presentation incorporates the call graph of the
36 program.
37 .sh 2 "The Flat Profile
38 .pp
39 The flat profile consists of a list of all the routines 
40 that are called during execution of the program,
41 with the count of the number of times they are called
42 and the number of seconds of execution time for which they
43 are themselves accountable.
44 The routines are listed in decreasing order of execution time.
45 A list of the routines that are never called during execution of
46 the program is also available
47 to verify that nothing important is omitted by
48 this execution.
49 The flat profile gives a quick overview of the routines that are used,
50 and shows the routines that are themselves responsible
51 for large fractions of the execution time.
52 In practice,
53 this profile usually shows that no single function
54 is overwhelmingly responsible for 
55 the total time of the program.
56 Notice that for this profile,
57 the individual times sum to the total execution time.
58 .sh 2 "The Call Graph Profile"
59 .sz 10
60 .(z
61 .TS
62 box center;
63 c c c c c l l
64 c c c c c l l
65 c c c c c l l
66 l n n n c l l.
67                                 called/total    \ \ parents
68 index   %time   self    descendants     called+self     name    index
69                                 called/total    \ \ children
70 _
71                 0.20    1.20    4/10    \ \ \s-1CALLER1\s+1     [7]
72                 0.30    1.80    6/10    \ \ \s-1CALLER2\s+1     [1]
73 [2]     41.5    0.50    3.00    10+4    \s-1EXAMPLE\s+1 [2]
74                 1.50    1.00    20/40   \ \ \s-1SUB1\s+1 <cycle1>       [4]
75                 0.00    0.50    1/5     \ \ \s-1SUB2\s+1        [9]
76                 0.00    0.00    0/5     \ \ \s-1SUB3\s+1        [11]
77 .TE
78 .ce 2
79 Profile entry for \s-1EXAMPLE\s+1.
80 Figure 4.
81 .)z
82 .pp
83 Ideally, we would like to print the call graph of the program,
84 but we are limited by the two-dimensional nature of our output
85 devices.
86 We cannot assume that a call graph is planar,
87 and even if it is, that we can print a planar version of it.
88 Instead, we choose to list each routine,
89 together with information about
90 the routines that are its direct parents and children.
91 This listing presents a window into the call graph.
92 Based on our experience,
93 both parent information and child information
94 is important,
95 and should be available without searching
96 through the output.
97 .pp
98 The major entries of the call graph profile are the entries from the
99 flat profile, augmented by the time propagated to each 
100 routine from its descendants.
101 This profile is sorted by the sum of the time for the routine
102 itself plus the time inherited from its descendants.
103 The profile shows which of the higher level routines 
104 spend large portions of the total execution time 
105 in the routines that they call.
106 For each routine, we show the amount of time passed by each child
107 to the routine, which includes time for the child itself
108 and for the descendants of the child
109 (and thus the descendants of the routine).
110 We also show the percentage these times represent of the total time
111 accounted to the child.
112 Similarly, the parents of each routine are listed, 
113 along with time,
114 and percentage of total routine time,
115 propagated to each one.
116 .pp
117 Cycles are handled as single entities.
118 The cycle as a whole is shown as though it were a single routine,
119 except that members of the cycle are listed in place of the children.
120 Although the number of calls of each member
121 from within the cycle are shown,
122 they do not affect time propagation.
123 When a child is a member of a cycle,
124 the time shown is the appropriate fraction of the time
125 for the whole cycle.
126 Self-recursive routines have their calls broken
127 down into calls from the outside and self-recursive calls.
128 Only the outside calls affect the propagation of time.
129 .pp
130 The following example is a typical fragment of a call graph.
131 .(b
132 .so pres1.pic
133 .)b
134 The entry in the call graph profile listing for this example is
135 shown in Figure 4.
136 .pp
137 The entry is for routine \s-1EXAMPLE\s+1, which has
138 the Caller routines as its parents,
139 and the Sub routines as its children.
140 The reader should keep in mind that all information
141 is given \fIwith respect to \s-1EXAMPLE\s+1\fP.
142 The index in the first column shows that \s-1EXAMPLE\s+1
143 is the second entry in the profile listing.
144 The \s-1EXAMPLE\s+1 routine is called ten times, four times by \s-1CALLER1\s+1,
145 and six times by \s-1CALLER2\s+1.
146 Consequently 40% of \s-1EXAMPLE\s+1's time is propagated to \s-1CALLER1\s+1,
147 and 60% of \s-1EXAMPLE\s+1's time is propagated to \s-1CALLER2\s+1.
148 The self and descendant fields of the parents
149 show the amount of self and descendant time \s-1EXAMPLE\s+1
150 propagates to them (but not the time used by
151 the parents directly).
152 Note that \s-1EXAMPLE\s+1 calls itself recursively four times.
153 The routine \s-1EXAMPLE\s+1 calls routine \s-1SUB1\s+1 twenty times, \s-1SUB2\s+1 once,
154 and never calls \s-1SUB3\s+1.
155 Since \s-1SUB2\s+1 is called a total of five times,
156 20% of its self and descendant time is propagated to \s-1EXAMPLE\s+1's
157 descendant time field.
158 Because \s-1SUB1\s+1 is a member of \fIcycle 1\fR,
159 the self and descendant times
160 and call count fraction
161 are those for the cycle as a whole.
162 Since cycle 1 is called a total of forty times
163 (not counting calls among members of the cycle),
164 it propagates 50% of the cycle's self and descendant
165 time to \s-1EXAMPLE\s+1's descendant time field.
166 Finally each name is followed by an index that shows
167 where on the listing to find the entry for that routine.
168 .sh 1 "Using the Profiles"
169 .pp
170 The profiler is a useful tool for improving
171 a set of routines that implement an abstraction.
172 It can be helpful in identifying poorly coded routines,
173 and in evaluating the new algorithms and code that replace them.
174 Taking full advantage of the profiler 
175 requires a careful examination of the call graph profile,
176 and a thorough knowledge of the abstractions underlying
177 the program.
178 .pp
179 The easiest optimization that can be performed
180 is a small change
181 to a control construct or data structure that improves the
182 running time of the program.
183 An obvious starting point
184 is a routine that is called many times.
185 For example, suppose an output 
186 routine is the only parent
187 of a routine that formats the data.
188 If this format routine is expanded inline in the
189 output routine, the overhead of a function call and
190 return can be saved for each datum that needs to be formatted.
191 .pp
192 The drawback to inline expansion is that the data abstractions
193 in the program may become less parameterized,
194 hence less clearly defined.
195 The profiling will also become less useful since the loss of 
196 routines will make its output more granular.
197 For example,
198 if the symbol table functions ``lookup'', ``insert'', and ``delete''
199 are all merged into a single parameterized routine,
200 it will be impossible to determine the costs
201 of any one of these individual functions from the profile.
202 .pp
203 Further potential for optimization lies in routines that
204 implement data abstractions whose total execution
205 time is long.
206 For example, a lookup routine might be called only a few
207 times, but use an inefficient linear search algorithm,
208 that might be replaced with a binary search.
209 Alternately, the discovery that a rehashing function is being
210 called excessively, can lead to a different
211 hash function or a larger hash table.
212 If the data abstraction function cannot easily be speeded up,
213 it may be advantageous to cache its results,
214 and eliminate the need to rerun
215 it for identical inputs.
216 These and other ideas for program improvement are discussed in
217 [Bentley81].
218 .pp
219 This tool is best used in an iterative approach:
220 profiling the program,
221 eliminating one bottleneck,
222 then finding some other part of the program
223 that begins to dominate execution time.
224 For instance, we have used \fBgprof\fR on itself;
225 eliminating, rewriting, and inline expanding routines,
226 until reading
227 data files (hardly a target for optimization!)
228 represents the dominating factor in its execution time.
229 .pp
230 Certain types of programs are not easily analyzed by \fBgprof\fR.
231 They are typified by programs that exhibit a large degree of 
232 recursion, such as recursive descent compilers.
233 The problem is that most of the major routines are grouped
234 into a single monolithic cycle.
235 As in the symbol table abstraction that is placed
236 in one routine,
237 it is impossible to distinguish which members of the cycle are
238 responsible for the execution time.
239 Unfortunately there are no easy modifications to these programs that
240 make them amenable to analysis.
241 .pp
242 A completely different use of the profiler is to analyze the control
243 flow of an unfamiliar program.
244 If you receive a program from another user that you need to modify
245 in some small way,
246 it is often unclear where the changes need to be made.
247 By running the program on an example and then using \fBgprof\fR,
248 you can get a view of the structure of the program.
249 .pp
250 Consider an example in which you need to change the output format
251 of the program.
252 For purposes of this example suppose that the call graph
253 of the output portion of the program has the following structure:
254 .(b
255 .so pres2.pic
256 .)b
257 Initially you look through the \fBgprof\fR
258 output for the system call ``\s-1WRITE\s+1''.
259 The format routine you will need to change is probably
260 among the parents of the ``\s-1WRITE\s+1'' procedure.
261 The next step is to look at the profile entry for each
262 of parents of ``\s-1WRITE\s+1'',
263 in this example either ``\s-1FORMAT1\s+1'' or ``\s-1FORMAT2\s+1'',
264 to determine which one to change.
265 Each format routine will have one or more parents,
266 in this example ``\s-1CALC1\s+1'', ``\s-1CALC2\s+1'', and ``\s-1CALC3\s+1''.
267 By inspecting the source code for each of these routines
268 you can determine which format routine generates the output that
269 you wish to modify.
270 Since the \fBgprof\fR entry shows all the
271 potential calls to the format routine you intend to change,
272 you can determine if your modifications will affect output that
273 should be left alone.
274 If you desire to change the output of ``\s-1CALC2\s+1'', but not ``\s-1CALC3\s+1'',
275 then formatting routine ``\s-1FORMAT2\s+1'' needs to be split
276 into two separate routines,
277 one of which implements the new format.
278 You can then retarget just the call by ``\s-1CALC2\s+1''
279 that needs the new format.
280 It should be noted that the static call information is particularly
281 useful here since the test case you run probably will not
282 exercise the entire program.
283 .sh 1 "Conclusions"
284 .pp
285 We have created a profiler that aids in the evaluation
286 of modular programs.
287 For each routine in the program,
288 the profile shows the extent to which that routine
289 helps support various abstractions,
290 and how that routine uses other abstractions.
291 The profile accurately assesses the cost of routines
292 at all levels of the program decomposition.
293 The profiler is easily used,
294 and can be compiled into the program without any prior planning by
295 the programmer.
296 It adds only five to thirty percent execution overhead to the program
297 being profiled,
298 produces no additional output until after the program finishes,
299 and allows the program to be measured in its actual environment.
300 Finally, the profiler runs on a time-sharing system 
301 using only the normal services provided by the operating system
302 and compilers.