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