Merge from vendor branch PKGSRC:
[netbsd-mini2440.git] / usr.bin / gprof / PSD.doc / gathering.me
blob64b41ce49dbe0568886e4a877fd4337c963b50bb
1 .\"     $NetBSD: gathering.me,v 1.2 1995/04/19 07:16:39 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 .\"     @(#)gathering.me        8.1 (Berkeley) 6/8/93
31 .\"
32 .sh 1 "Gathering Profile Data"
33 .pp
34 Routine calls or statement executions can be measured by having a
35 compiler augment the code at strategic points.
36 The additions can be inline increments to counters [Knuth71]
37 [Satterthwaite72] [Joy79] or calls to
38 monitoring routines [Unix].
39 The counter increment overhead is low, and is suitable for
40 profiling statements.
41 A call of the monitoring routine has an overhead comparable with a
42 call of a regular routine, and is therefore only suited
43 to profiling on a routine by routine basis.
44 However, the monitoring routine solution has certain advantages.
45 Whatever counters are needed by the monitoring routine can be
46 managed by the monitoring routine itself, rather than being
47 distributed around the code.
48 In particular, a monitoring routine can easily be called from separately
49 compiled programs.
50 In addition, different monitoring routines can be linked into the
51 program
52 being measured
53 to assemble different profiling data without having to
54 change the compiler or recompile the program.
55 We have exploited this approach;
56 our compilers for C, Fortran77, and Pascal can insert calls to a
57 monitoring routine in the prologue for each routine.
58 Use of the monitoring routine requires no planning on part of a
59 programmer other than to request that augmented routine
60 prologues be produced during compilation.
61 .pp
62 We are interested in gathering three pieces of information during
63 program execution: call counts and execution times for 
64 each profiled routine, and the arcs of the dynamic call graph
65 traversed by this execution of the program.
66 By post-processing of this data we can build the dynamic call
67 graph for this execution of the program and propagate times along
68 the edges of this graph to attribute times for routines to the
69 routines that invoke them.
70 .pp
71 Gathering of the profiling information should not greatly
72 interfere with the running of the program.
73 Thus, the monitoring routine must not produce trace output each
74 time it is invoked.
75 The volume of data thus produced would be unmanageably large,
76 and the time required to record it would overwhelm the running
77 time of most programs.
78 Similarly, the monitoring routine can not do the analysis of
79 the profiling data (e.g. assembling the call graph, propagating
80 times around it, discovering cycles, etc.) during program
81 execution.
82 Our solution is to gather profiling data in memory during program
83 execution and to condense it to a file as the profiled
84 program exits.
85 This file is then processed by a separate program to produce the
86 listing of the profile data.
87 An advantage of this approach is that the profile data for
88 several executions of a program can be combined by the
89 post-processing to provide a profile of many
90 executions.
91 .pp
92 The execution time monitoring consists of three parts.
93 The first part allocates and initializes the runtime monitoring data
94 structures before the program begins execution.
95 The second part is the monitoring routine invoked from the
96 prologue of each profiled routine.
97 The third part condenses the data structures and writes them
98 to a file as the program terminates.
99 The monitoring routine is discussed in detail in the following sections.
100 .sh 2 "Execution Counts"
102 The \fBgprof\fP monitoring routine counts the number of times
103 each profiled routine is called.
104 The monitoring routine also records the arc in the call graph
105 that activated the profiled routine.
106 The count is associated with the arc in the call graph
107 rather than with the routine.
108 Call counts for routines can then be determined by summing the counts
109 on arcs directed into that routine.
110 In a machine-dependent fashion, the monitoring routine notes its
111 own return address.
112 This address is in the prologue of some profiled routine that is
113 the destination of an arc in the dynamic call graph.
114 The monitoring routine also discovers the return address for that
115 routine, thus identifying the call site, or source of the arc.
116 The source of the arc is in the \fIcaller\fP, and the destination is in
117 the \fIcallee\fP.
118 For example, if a routine A calls a routine B, A is the caller, 
119 and B is the callee.
120 The prologue of B will include a call to the monitoring routine
121 that will note the arc from A to B and either initialize or
122 increment a counter for that arc.
124 One can not afford to have the monitoring routine output tracing
125 information as each arc is identified.
126 Therefore, the monitoring routine maintains a table of all the
127 arcs discovered, with counts of the numbers of times each is
128 traversed during execution.
129 This table is accessed once per routine call.
130 Access to it
131 must be as fast as possible so as not to overwhelm the time
132 required to execute the program.
134 Our solution is to access the table through a hash table.
135 We use the call site as the primary key with the callee
136 address being the secondary key.
137 Since each call site typically calls only one callee, we can
138 reduce (usually to one) the number of minor lookups based on the callee.
139 Another alternative would use the callee as the primary key and the
140 call site as the secondary key.
141 Such an organization has the advantage of associating callers with
142 callees, at the expense of longer lookups in the monitoring
143 routine.
144 We are fortunate to be running in a virtual memory environment,
145 and (for the sake of speed) were able to allocate enough space
146 for the primary hash table to allow a one-to-one mapping from
147 call site addresses to the primary hash table.
148 Thus our hash function is trivial to calculate and collisions
149 occur only for call sites that call multiple
150 destinations (e.g. functional parameters and functional variables).
151 A one level hash function using both call site and callee would
152 result in an unreasonably large hash table.
153 Further, the number of dynamic call sites and callees is not known during
154 execution of the profiled program.
156 Not all callers and callees can be identified by the monitoring
157 routine.
158 Routines that were compiled without the profiling augmentations
159 will not call the monitoring routine as part of their prologue,
160 and thus no arcs will be recorded whose destinations are in these
161 routines.
162 One need not profile all the routines in a program.
163 Routines that are not profiled run at full speed.
164 Certain routines, notably exception handlers, are invoked by
165 non-standard calling sequences.
166 Thus the monitoring routine may know the destination of an arc
167 (the callee),
168 but find it difficult or
169 impossible to determine the source of the arc (the caller).
170 Often in these cases the apparent source of the arc is not a call
171 site at all.
172 Such anomalous invocations are declared ``spontaneous''.
173 .sh 2 "Execution Times"
175 The execution times for routines can be gathered in at least two
176 ways.
177 One method measures the execution time of a routine by measuring
178 the elapsed time from routine entry to routine exit.
179 Unfortunately, time measurement is complicated on time-sharing
180 systems by the time-slicing of the program.
181 A second method samples the value of the program counter at some
182 interval, and infers execution time from the distribution of the
183 samples within the program.
184 This technique is particularly suited to time-sharing systems,
185 where the time-slicing can serve as the basis for sampling
186 the program counter.
187 Notice that, whereas the first method could provide exact timings,
188 the second is inherently a statistical approximation.
190 The sampling method need not require support from the operating
191 system:  all that is needed is the ability to set and respond to
192 ``alarm clock'' interrupts that run relative to program time.
193 It is imperative that the intervals be uniform since the
194 sampling of the program counter rather than the duration of the
195 interval is the basis of the distribution.
196 If sampling is done too often, the interruptions to sample the
197 program counter will overwhelm the running of the profiled program.
198 On the other hand, the program must run for enough sampled
199 intervals that the distribution of the samples accurately
200 represents the distribution of time for the execution of the
201 program.
202 As with routine call tracing, the monitoring routine can not
203 afford to output information for each program counter
204 sample.
205 In our computing environment, the operating system can provide a
206 histogram of the location of the program counter at the end of
207 each clock tick (1/60th of a second) in which a program runs.
208 The histogram is assembled in memory as the program runs.
209 This facility is enabled by our monitoring routine.
210 We have adjusted the granularity of the histogram so that
211 program counter values map one-to-one onto the histogram.
212 We make the simplifying assumption that all calls to a specific
213 routine require the same amount of time to execute.
214 This assumption may disguise that some calls
215 (or worse, some call sites) always invoke a routine
216 such that its execution is faster (or slower)
217 than the average time for that routine.
219 When the profiled program terminates, 
220 the arc table and the histogram of
221 program counter samples is written to a file.
222 The arc table is condensed to consist of the source and destination
223 addresses of the arc and the count of the number of times the arc
224 was traversed by this execution of the program.
225 The recorded histogram consists of counters of the number of
226 times the program counter was found to be in each of the ranges covered
227 by the histogram.
228 The ranges themselves are summarized as a
229 lower and upper bound and a step size.