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;
92 * When we are called from an exception handler, frompc may be
93 * a user address. Convert such frompc's to some representation
94 * in kernel address space.
96 frompc = MCOUNT_FROMPC_USER(frompc);
99 frompci = frompc - p->lowpc;
100 if (frompci >= p->textsize)
104 if (p->state == GMON_PROF_HIRES) {
106 * Count the time since cputime() was previously called
107 * against `frompc'. Compensate for overheads.
109 * cputime() sets its prev_count variable to the count when
110 * it is called. This in effect starts a counter for
111 * the next period of execution (normally from now until
112 * the next call to mcount() or mexitcount()). We set
113 * cputime_bias to compensate for our own overhead.
115 * We use the usual sampling counters since they can be
116 * located efficiently. 4-byte counters are usually
117 * necessary. gprof will add up the scattered counts
118 * just like it does for statistical profiling. All
119 * counts are signed so that underflow in the subtractions
120 * doesn't matter much (negative counts are normally
121 * compensated for by larger counts elsewhere). Underflow
122 * shouldn't occur, but may be caused by slightly wrong
123 * calibrations or from not clearing cputime_bias.
125 delta = cputime() - cputime_bias - p->mcount_pre_overhead;
126 cputime_bias = p->mcount_post_overhead;
127 KCOUNT(p, frompci) += delta;
128 *p->cputime_count += p->cputime_overhead;
129 *p->mcount_count += p->mcount_overhead;
135 * When we are called from an exception handler, frompc is faked
136 * to be for where the exception occurred. We've just solidified
137 * the count for there. Now convert frompci to an index that
138 * represents the kind of exception so that interruptions appear
139 * in the call graph as calls from those index instead of calls
142 frompc = MCOUNT_FROMPC_INTR(selfpc);
143 if ((frompc - p->lowpc) < p->textsize)
144 frompci = frompc - p->lowpc;
148 * check that frompc is a reasonable pc value.
149 * for example: signal catchers get called from the stack,
150 * not from text space. too bad.
152 if (frompci >= p->textsize)
155 frompcindex = &p->froms[frompci / (p->hashfraction * sizeof(*p->froms))];
156 toindex = *frompcindex;
159 * first time traversing this arc
161 toindex = ++p->tos[0].link;
162 if (toindex >= p->tolimit)
163 /* halt further profiling */
166 *frompcindex = toindex;
167 top = &p->tos[toindex];
168 top->selfpc = selfpc;
173 top = &p->tos[toindex];
174 if (top->selfpc == selfpc) {
176 * arc at front of chain; usual case.
182 * have to go looking down chain for it.
183 * top points to what we are looking at,
184 * prevtop points to previous top.
185 * we know it is not at the head of the chain.
187 for (; /* goto done */; ) {
188 if (top->link == 0) {
190 * top is end of the chain and none of the chain
191 * had top->selfpc == selfpc.
192 * so we allocate a new tostruct
193 * and link it to the head of the chain.
195 toindex = ++p->tos[0].link;
196 if (toindex >= p->tolimit)
199 top = &p->tos[toindex];
200 top->selfpc = selfpc;
202 top->link = *frompcindex;
203 *frompcindex = toindex;
207 * otherwise, check the next arc on the chain.
210 top = &p->tos[top->link];
211 if (top->selfpc == selfpc) {
214 * increment its count
215 * move it to the head of the chain.
218 toindex = prevtop->link;
219 prevtop->link = top->link;
220 top->link = *frompcindex;
221 *frompcindex = toindex;
230 p->state = GMON_PROF_ON;
234 p->state = GMON_PROF_ERROR;
242 * Actual definition of mcount function. Defined in <machine/profile.h>,
243 * which is included by <sys/gmon.h>.
249 mexitcount(uintfptr_t selfpc)
252 uintfptr_t selfpcdiff;
255 selfpcdiff = selfpc - (uintfptr_t)p->lowpc;
256 if (selfpcdiff < p->textsize) {
260 * Count the time since cputime() was previously called
261 * against `selfpc'. Compensate for overheads.
263 delta = cputime() - cputime_bias - p->mexitcount_pre_overhead;
264 cputime_bias = p->mexitcount_post_overhead;
265 KCOUNT(p, selfpcdiff) += delta;
266 *p->cputime_count += p->cputime_overhead;
267 *p->mexitcount_count += p->mexitcount_overhead;
271 #ifndef __GNUCLIKE_ASM
272 #error "This file uses null asms to prevent timing loops being optimized away."
280 for (i = 0; i < CALIB_SCALE; i++)
281 __asm __volatile("");
287 __asm __volatile("");
295 for (i = 0; i < CALIB_SCALE; i++)