2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
39 #include <sys/systm.h>
42 #include <vm/vm_param.h>
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>.
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.
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.
61 /* _mcount; may be static, inline, etc */
62 _MCOUNT_DECL(uintfptr_t frompc, uintfptr_t selfpc)
69 struct tostruct *top, *prevtop;
77 #ifndef GUPROF /* XXX */
79 * check that we are profiling
80 * and that we aren't recursively invoked.
82 if (p->state != GMON_PROF_ON)
88 p->state = GMON_PROF_BUSY;
92 /* De-relocate any addresses in a (single) trampoline. */
94 MCOUNT_DETRAMP(frompc);
95 MCOUNT_DETRAMP(selfpc);
98 * When we are called from an exception handler, frompc may be
99 * a user address. Convert such frompc's to some representation
100 * in kernel address space.
102 #ifdef MCOUNT_FROMPC_USER
103 frompc = MCOUNT_FROMPC_USER(frompc);
104 #elif defined(MCOUNT_USERPC)
106 * For separate address spaces, we can only guess that addresses
107 * in the range known to us are actually kernel addresses. Outside
108 * of this range, conerting to the user address is fail-safe.
110 if (frompc < p->lowpc || frompc - p->lowpc >= p->textsize)
111 frompc = MCOUNT_USERPC;
115 frompci = frompc - p->lowpc;
116 if (frompci >= p->textsize)
120 if (p->state == GMON_PROF_HIRES) {
122 * Count the time since cputime() was previously called
123 * against `frompc'. Compensate for overheads.
125 * cputime() sets its prev_count variable to the count when
126 * it is called. This in effect starts a counter for
127 * the next period of execution (normally from now until
128 * the next call to mcount() or mexitcount()). We set
129 * cputime_bias to compensate for our own overhead.
131 * We use the usual sampling counters since they can be
132 * located efficiently. 4-byte counters are usually
133 * necessary. gprof will add up the scattered counts
134 * just like it does for statistical profiling. All
135 * counts are signed so that underflow in the subtractions
136 * doesn't matter much (negative counts are normally
137 * compensated for by larger counts elsewhere). Underflow
138 * shouldn't occur, but may be caused by slightly wrong
139 * calibrations or from not clearing cputime_bias.
141 delta = cputime() - cputime_bias - p->mcount_pre_overhead;
142 cputime_bias = p->mcount_post_overhead;
143 KCOUNT(p, frompci) += delta;
144 *p->cputime_count += p->cputime_overhead;
145 *p->mcount_count += p->mcount_overhead;
151 * When we are called from an exception handler, frompc is faked
152 * to be for where the exception occurred. We've just solidified
153 * the count for there. Now convert frompci to an index that
154 * represents the kind of exception so that interruptions appear
155 * in the call graph as calls from those index instead of calls
158 frompc = MCOUNT_FROMPC_INTR(selfpc);
159 if ((frompc - p->lowpc) < p->textsize)
160 frompci = frompc - p->lowpc;
164 * check that frompc is a reasonable pc value.
165 * for example: signal catchers get called from the stack,
166 * not from text space. too bad.
168 if (frompci >= p->textsize)
171 frompcindex = &p->froms[frompci / (p->hashfraction * sizeof(*p->froms))];
172 toindex = *frompcindex;
175 * first time traversing this arc
177 toindex = ++p->tos[0].link;
178 if (toindex >= p->tolimit)
179 /* halt further profiling */
182 *frompcindex = toindex;
183 top = &p->tos[toindex];
184 top->selfpc = selfpc;
189 top = &p->tos[toindex];
190 if (top->selfpc == selfpc) {
192 * arc at front of chain; usual case.
198 * have to go looking down chain for it.
199 * top points to what we are looking at,
200 * prevtop points to previous top.
201 * we know it is not at the head of the chain.
203 for (; /* goto done */; ) {
204 if (top->link == 0) {
206 * top is end of the chain and none of the chain
207 * had top->selfpc == selfpc.
208 * so we allocate a new tostruct
209 * and link it to the head of the chain.
211 toindex = ++p->tos[0].link;
212 if (toindex >= p->tolimit)
215 top = &p->tos[toindex];
216 top->selfpc = selfpc;
218 top->link = *frompcindex;
219 *frompcindex = toindex;
223 * otherwise, check the next arc on the chain.
226 top = &p->tos[top->link];
227 if (top->selfpc == selfpc) {
230 * increment its count
231 * move it to the head of the chain.
234 toindex = prevtop->link;
235 prevtop->link = top->link;
236 top->link = *frompcindex;
237 *frompcindex = toindex;
246 p->state = GMON_PROF_ON;
250 p->state = GMON_PROF_ERROR;
258 * Actual definition of mcount function. Defined in <machine/profile.h>,
259 * which is included by <sys/gmon.h>.
265 mexitcount(uintfptr_t selfpc)
268 uintfptr_t selfpcdiff;
271 #ifdef MCOUNT_DETRAMP
272 MCOUNT_DETRAMP(selfpc);
274 selfpcdiff = selfpc - (uintfptr_t)p->lowpc;
275 if (selfpcdiff < p->textsize) {
279 * Count the time since cputime() was previously called
280 * against `selfpc'. Compensate for overheads.
282 delta = cputime() - cputime_bias - p->mexitcount_pre_overhead;
283 cputime_bias = p->mexitcount_post_overhead;
284 KCOUNT(p, selfpcdiff) += delta;
285 *p->cputime_count += p->cputime_overhead;
286 *p->mexitcount_count += p->mexitcount_overhead;
290 #ifndef __GNUCLIKE_ASM
291 #error "This file uses null asms to prevent timing loops being optimized away."
299 for (i = 0; i < CALIB_SCALE; i++)
300 __asm __volatile("");
306 __asm __volatile("");
314 for (i = 0; i < CALIB_SCALE; i++)