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_DECL(uintfptr_t frompc, uintfptr_t selfpc) /* _mcount; may be static, inline, etc */
68 struct tostruct *top, *prevtop;
76 #ifndef GUPROF /* XXX */
78 * check that we are profiling
79 * and that we aren't recursively invoked.
81 if (p->state != GMON_PROF_ON)
87 p->state = GMON_PROF_BUSY;
91 /* De-relocate any addresses in a (single) trampoline. */
93 MCOUNT_DETRAMP(frompc);
94 MCOUNT_DETRAMP(selfpc);
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.
101 #ifdef MCOUNT_FROMPC_USER
102 frompc = MCOUNT_FROMPC_USER(frompc);
103 #elif defined(MCOUNT_USERPC)
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.
109 if (frompc < p->lowpc || frompc - p->lowpc >= p->textsize)
110 frompc = MCOUNT_USERPC;
114 frompci = frompc - p->lowpc;
115 if (frompci >= p->textsize)
119 if (p->state == GMON_PROF_HIRES) {
121 * Count the time since cputime() was previously called
122 * against `frompc'. Compensate for overheads.
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.
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.
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;
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
157 frompc = MCOUNT_FROMPC_INTR(selfpc);
158 if ((frompc - p->lowpc) < p->textsize)
159 frompci = frompc - p->lowpc;
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.
167 if (frompci >= p->textsize)
170 frompcindex = &p->froms[frompci / (p->hashfraction * sizeof(*p->froms))];
171 toindex = *frompcindex;
174 * first time traversing this arc
176 toindex = ++p->tos[0].link;
177 if (toindex >= p->tolimit)
178 /* halt further profiling */
181 *frompcindex = toindex;
182 top = &p->tos[toindex];
183 top->selfpc = selfpc;
188 top = &p->tos[toindex];
189 if (top->selfpc == selfpc) {
191 * arc at front of chain; usual case.
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.
202 for (; /* goto done */; ) {
203 if (top->link == 0) {
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.
210 toindex = ++p->tos[0].link;
211 if (toindex >= p->tolimit)
214 top = &p->tos[toindex];
215 top->selfpc = selfpc;
217 top->link = *frompcindex;
218 *frompcindex = toindex;
222 * otherwise, check the next arc on the chain.
225 top = &p->tos[top->link];
226 if (top->selfpc == selfpc) {
229 * increment its count
230 * move it to the head of the chain.
233 toindex = prevtop->link;
234 prevtop->link = top->link;
235 top->link = *frompcindex;
236 *frompcindex = toindex;
245 p->state = GMON_PROF_ON;
249 p->state = GMON_PROF_ERROR;
257 * Actual definition of mcount function. Defined in <machine/profile.h>,
258 * which is included by <sys/gmon.h>.
264 mexitcount(uintfptr_t selfpc)
267 uintfptr_t selfpcdiff;
270 #ifdef MCOUNT_DETRAMP
271 MCOUNT_DETRAMP(selfpc);
273 selfpcdiff = selfpc - (uintfptr_t)p->lowpc;
274 if (selfpcdiff < p->textsize) {
278 * Count the time since cputime() was previously called
279 * against `selfpc'. Compensate for overheads.
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;
289 #ifndef __GNUCLIKE_ASM
290 #error "This file uses null asms to prevent timing loops being optimized away."
298 for (i = 0; i < CALIB_SCALE; i++)
299 __asm __volatile("");
305 __asm __volatile("");
313 for (i = 0; i < CALIB_SCALE; i++)