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