FreeBSD/Linux Kernel Cross Reference
sys/libkern/mcount.c
1 /*-
2 * Copyright (c) 1983, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD: releng/5.3/sys/libkern/mcount.c 128019 2004-04-07 20:46:16Z imp $");
32
33 #include <sys/param.h>
34 #include <sys/gmon.h>
35 #ifdef _KERNEL
36 #ifndef GUPROF
37 #include <sys/systm.h>
38 #endif
39 #include <vm/vm.h>
40 #include <vm/vm_param.h>
41 #include <vm/pmap.h>
42 void bintr(void);
43 void btrap(void);
44 void eintr(void);
45 void user(void);
46 #endif
47
48 /*
49 * mcount is called on entry to each function compiled with the profiling
50 * switch set. _mcount(), which is declared in a machine-dependent way
51 * with _MCOUNT_DECL, does the actual work and is either inlined into a
52 * C routine or called by an assembly stub. In any case, this magic is
53 * taken care of by the MCOUNT definition in <machine/profile.h>.
54 *
55 * _mcount updates data structures that represent traversals of the
56 * program's call graph edges. frompc and selfpc are the return
57 * address and function address that represents the given call graph edge.
58 *
59 * Note: the original BSD code used the same variable (frompcindex) for
60 * both frompcindex and frompc. Any reasonable, modern compiler will
61 * perform this optimization.
62 */
63 _MCOUNT_DECL(frompc, selfpc) /* _mcount; may be static, inline, etc */
64 register uintfptr_t frompc, selfpc;
65 {
66 #ifdef GUPROF
67 int delta;
68 #endif
69 register fptrdiff_t frompci;
70 register u_short *frompcindex;
71 register struct tostruct *top, *prevtop;
72 register struct gmonparam *p;
73 register long toindex;
74 #ifdef _KERNEL
75 MCOUNT_DECL(s)
76 #endif
77
78 p = &_gmonparam;
79 #ifndef GUPROF /* XXX */
80 /*
81 * check that we are profiling
82 * and that we aren't recursively invoked.
83 */
84 if (p->state != GMON_PROF_ON)
85 return;
86 #endif
87 #ifdef _KERNEL
88 MCOUNT_ENTER(s);
89 #else
90 p->state = GMON_PROF_BUSY;
91 #endif
92 frompci = frompc - p->lowpc;
93
94 #ifdef _KERNEL
95 /*
96 * When we are called from an exception handler, frompci may be
97 * for a user address. Convert such frompci's to the index of
98 * user() to merge all user counts.
99 */
100 if (frompci >= p->textsize) {
101 if (frompci + p->lowpc
102 >= (uintfptr_t)(VM_MAXUSER_ADDRESS))
103 goto done;
104 frompci = (uintfptr_t)user - p->lowpc;
105 if (frompci >= p->textsize)
106 goto done;
107 }
108 #endif
109
110 #ifdef GUPROF
111 if (p->state == GMON_PROF_HIRES) {
112 /*
113 * Count the time since cputime() was previously called
114 * against `frompc'. Compensate for overheads.
115 *
116 * cputime() sets its prev_count variable to the count when
117 * it is called. This in effect starts a counter for
118 * the next period of execution (normally from now until
119 * the next call to mcount() or mexitcount()). We set
120 * cputime_bias to compensate for our own overhead.
121 *
122 * We use the usual sampling counters since they can be
123 * located efficiently. 4-byte counters are usually
124 * necessary. gprof will add up the scattered counts
125 * just like it does for statistical profiling. All
126 * counts are signed so that underflow in the subtractions
127 * doesn't matter much (negative counts are normally
128 * compensated for by larger counts elsewhere). Underflow
129 * shouldn't occur, but may be caused by slightly wrong
130 * calibrations or from not clearing cputime_bias.
131 */
132 delta = cputime() - cputime_bias - p->mcount_pre_overhead;
133 cputime_bias = p->mcount_post_overhead;
134 KCOUNT(p, frompci) += delta;
135 *p->cputime_count += p->cputime_overhead;
136 *p->mcount_count += p->mcount_overhead;
137 }
138 #endif /* GUPROF */
139
140 #ifdef _KERNEL
141 /*
142 * When we are called from an exception handler, frompc is faked
143 * to be for where the exception occurred. We've just solidified
144 * the count for there. Now convert frompci to the index of btrap()
145 * for trap handlers and bintr() for interrupt handlers to make
146 * exceptions appear in the call graph as calls from btrap() and
147 * bintr() instead of calls from all over.
148 */
149 if ((uintfptr_t)selfpc >= (uintfptr_t)btrap
150 && (uintfptr_t)selfpc < (uintfptr_t)eintr) {
151 if ((uintfptr_t)selfpc >= (uintfptr_t)bintr)
152 frompci = (uintfptr_t)bintr - p->lowpc;
153 else
154 frompci = (uintfptr_t)btrap - p->lowpc;
155 }
156 #endif
157
158 /*
159 * check that frompc is a reasonable pc value.
160 * for example: signal catchers get called from the stack,
161 * not from text space. too bad.
162 */
163 if (frompci >= p->textsize)
164 goto done;
165
166 frompcindex = &p->froms[frompci / (p->hashfraction * sizeof(*p->froms))];
167 toindex = *frompcindex;
168 if (toindex == 0) {
169 /*
170 * first time traversing this arc
171 */
172 toindex = ++p->tos[0].link;
173 if (toindex >= p->tolimit)
174 /* halt further profiling */
175 goto overflow;
176
177 *frompcindex = toindex;
178 top = &p->tos[toindex];
179 top->selfpc = selfpc;
180 top->count = 1;
181 top->link = 0;
182 goto done;
183 }
184 top = &p->tos[toindex];
185 if (top->selfpc == selfpc) {
186 /*
187 * arc at front of chain; usual case.
188 */
189 top->count++;
190 goto done;
191 }
192 /*
193 * have to go looking down chain for it.
194 * top points to what we are looking at,
195 * prevtop points to previous top.
196 * we know it is not at the head of the chain.
197 */
198 for (; /* goto done */; ) {
199 if (top->link == 0) {
200 /*
201 * top is end of the chain and none of the chain
202 * had top->selfpc == selfpc.
203 * so we allocate a new tostruct
204 * and link it to the head of the chain.
205 */
206 toindex = ++p->tos[0].link;
207 if (toindex >= p->tolimit)
208 goto overflow;
209
210 top = &p->tos[toindex];
211 top->selfpc = selfpc;
212 top->count = 1;
213 top->link = *frompcindex;
214 *frompcindex = toindex;
215 goto done;
216 }
217 /*
218 * otherwise, check the next arc on the chain.
219 */
220 prevtop = top;
221 top = &p->tos[top->link];
222 if (top->selfpc == selfpc) {
223 /*
224 * there it is.
225 * increment its count
226 * move it to the head of the chain.
227 */
228 top->count++;
229 toindex = prevtop->link;
230 prevtop->link = top->link;
231 top->link = *frompcindex;
232 *frompcindex = toindex;
233 goto done;
234 }
235
236 }
237 done:
238 #ifdef _KERNEL
239 MCOUNT_EXIT(s);
240 #else
241 p->state = GMON_PROF_ON;
242 #endif
243 return;
244 overflow:
245 p->state = GMON_PROF_ERROR;
246 #ifdef _KERNEL
247 MCOUNT_EXIT(s);
248 #endif
249 return;
250 }
251
252 /*
253 * Actual definition of mcount function. Defined in <machine/profile.h>,
254 * which is included by <sys/gmon.h>.
255 */
256 MCOUNT
257
258 #ifdef GUPROF
259 void
260 mexitcount(selfpc)
261 uintfptr_t selfpc;
262 {
263 struct gmonparam *p;
264 uintfptr_t selfpcdiff;
265
266 p = &_gmonparam;
267 selfpcdiff = selfpc - (uintfptr_t)p->lowpc;
268 if (selfpcdiff < p->textsize) {
269 int delta;
270
271 /*
272 * Count the time since cputime() was previously called
273 * against `selfpc'. Compensate for overheads.
274 */
275 delta = cputime() - cputime_bias - p->mexitcount_pre_overhead;
276 cputime_bias = p->mexitcount_post_overhead;
277 KCOUNT(p, selfpcdiff) += delta;
278 *p->cputime_count += p->cputime_overhead;
279 *p->mexitcount_count += p->mexitcount_overhead;
280 }
281 }
282
283 void
284 empty_loop()
285 {
286 int i;
287
288 for (i = 0; i < CALIB_SCALE; i++)
289 ;
290 }
291
292 void
293 nullfunc()
294 {
295 }
296
297 void
298 nullfunc_loop()
299 {
300 int i;
301
302 for (i = 0; i < CALIB_SCALE; i++)
303 nullfunc();
304 }
305 #endif /* GUPROF */
Cache object: 6676935dc796ee3aa121540c7cc7beb0
|