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