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/param.h>
35 #include <sys/systm.h>
37 #include <vm/vm_param.h>
44 #include <machine/atomic.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 if (!atomic_cmpset_acq_int(&p->state, GMON_PROF_ON, GMON_PROF_BUSY))
91 frompci = frompc - p->lowpc;
95 * When we are called from an exception handler, frompci may be
96 * for a user address. Convert such frompci's to the index of
97 * user() to merge all user counts.
99 if (frompci >= p->textsize) {
100 if (frompci + p->lowpc
101 >= (uintfptr_t)(VM_MAXUSER_ADDRESS + UPAGES * PAGE_SIZE))
103 frompci = (uintfptr_t)user - p->lowpc;
104 if (frompci >= p->textsize)
110 if (p->state != GMON_PROF_HIRES)
111 goto skip_guprof_stuff;
113 * Look at the clock and add the count of clock cycles since the
114 * clock was last looked at to a counter for frompc. This
115 * solidifies the count for the function containing frompc and
116 * effectively starts another clock for the current function.
117 * The count for the new clock will be solidified when another
118 * function call is made or the function returns.
120 * We use the usual sampling counters since they can be located
121 * efficiently. 4-byte counters are usually necessary.
123 * There are many complications for subtracting the profiling
124 * overheads from the counts for normal functions and adding
125 * them to the counts for mcount(), mexitcount() and cputime().
126 * We attempt to handle fractional cycles, but the overheads
127 * are usually underestimated because they are calibrated for
128 * a simpler than usual setup.
130 delta = cputime() - p->mcount_overhead;
131 p->cputime_overhead_resid += p->cputime_overhead_frac;
132 p->mcount_overhead_resid += p->mcount_overhead_frac;
134 *p->mcount_count += delta + p->mcount_overhead
135 - p->cputime_overhead;
136 else if (delta != 0) {
137 if (p->cputime_overhead_resid >= CALIB_SCALE) {
138 p->cputime_overhead_resid -= CALIB_SCALE;
143 if (p->mcount_overhead_resid >= CALIB_SCALE) {
144 p->mcount_overhead_resid -= CALIB_SCALE;
148 KCOUNT(p, frompci) += delta;
150 *p->mcount_count += p->mcount_overhead_sub;
152 *p->cputime_count += p->cputime_overhead;
158 * When we are called from an exception handler, frompc is faked
159 * to be for where the exception occurred. We've just solidified
160 * the count for there. Now convert frompci to the index of btrap()
161 * for trap handlers and bintr() for interrupt handlers to make
162 * exceptions appear in the call graph as calls from btrap() and
163 * bintr() instead of calls from all over.
165 if ((uintfptr_t)selfpc >= (uintfptr_t)btrap
166 && (uintfptr_t)selfpc < (uintfptr_t)eintr) {
167 if ((uintfptr_t)selfpc >= (uintfptr_t)bintr)
168 frompci = (uintfptr_t)bintr - p->lowpc;
170 frompci = (uintfptr_t)btrap - p->lowpc;
175 * check that frompc is a reasonable pc value.
176 * for example: signal catchers get called from the stack,
177 * not from text space. too bad.
179 if (frompci >= p->textsize)
182 frompcindex = &p->froms[frompci / (p->hashfraction * sizeof(*p->froms))];
183 toindex = *frompcindex;
186 * first time traversing this arc
188 toindex = ++p->tos[0].link;
189 if (toindex >= p->tolimit)
190 /* halt further profiling */
193 *frompcindex = toindex;
194 top = &p->tos[toindex];
195 top->selfpc = selfpc;
200 top = &p->tos[toindex];
201 if (top->selfpc == selfpc) {
203 * arc at front of chain; usual case.
209 * have to go looking down chain for it.
210 * top points to what we are looking at,
211 * prevtop points to previous top.
212 * we know it is not at the head of the chain.
214 for (; /* goto done */; ) {
215 if (top->link == 0) {
217 * top is end of the chain and none of the chain
218 * had top->selfpc == selfpc.
219 * so we allocate a new tostruct
220 * and link it to the head of the chain.
222 toindex = ++p->tos[0].link;
223 if (toindex >= p->tolimit)
226 top = &p->tos[toindex];
227 top->selfpc = selfpc;
229 top->link = *frompcindex;
230 *frompcindex = toindex;
234 * otherwise, check the next arc on the chain.
237 top = &p->tos[top->link];
238 if (top->selfpc == selfpc) {
241 * increment its count
242 * move it to the head of the chain.
245 toindex = prevtop->link;
246 prevtop->link = top->link;
247 top->link = *frompcindex;
248 *frompcindex = toindex;
257 atomic_store_rel_int(&p->state, GMON_PROF_ON);
261 atomic_store_rel_int(&p->state, GMON_PROF_ERROR);
269 * Actual definition of mcount function. Defined in <machine/profile.h>,
270 * which is included by <sys/gmon.h>.
276 mexitcount(uintfptr_t selfpc)
279 uintfptr_t selfpcdiff;
282 selfpcdiff = selfpc - (uintfptr_t)p->lowpc;
283 if (selfpcdiff < p->textsize) {
287 * Solidify the count for the current function.
289 delta = cputime() - p->mexitcount_overhead;
290 p->cputime_overhead_resid += p->cputime_overhead_frac;
291 p->mexitcount_overhead_resid += p->mexitcount_overhead_frac;
293 *p->mexitcount_count += delta + p->mexitcount_overhead
294 - p->cputime_overhead;
295 else if (delta != 0) {
296 if (p->cputime_overhead_resid >= CALIB_SCALE) {
297 p->cputime_overhead_resid -= CALIB_SCALE;
302 if (p->mexitcount_overhead_resid
304 p->mexitcount_overhead_resid
306 ++*p->mexitcount_count;
309 KCOUNT(p, selfpcdiff) += delta;
311 *p->mexitcount_count += p->mexitcount_overhead_sub;
313 *p->cputime_count += p->cputime_overhead;