2 * Copyright (c) 1983, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
37 #include <sys/systm.h>
40 #include <vm/vm_param.h>
45 * mcount is called on entry to each function compiled with the profiling
46 * switch set. _mcount(), which is declared in a machine-dependent way
47 * with _MCOUNT_DECL, does the actual work and is either inlined into a
48 * C routine or called by an assembly stub. In any case, this magic is
49 * taken care of by the MCOUNT definition in <machine/profile.h>.
51 * _mcount updates data structures that represent traversals of the
52 * program's call graph edges. frompc and selfpc are the return
53 * address and function address that represents the given call graph edge.
55 * Note: the original BSD code used the same variable (frompcindex) for
56 * both frompcindex and frompc. Any reasonable, modern compiler will
57 * perform this optimization.
59 _MCOUNT_DECL(frompc, selfpc) /* _mcount; may be static, inline, etc */
60 uintfptr_t frompc, selfpc;
67 struct tostruct *top, *prevtop;
75 #ifndef GUPROF /* XXX */
77 * check that we are profiling
78 * and that we aren't recursively invoked.
80 if (p->state != GMON_PROF_ON)
86 p->state = GMON_PROF_BUSY;
91 * When we are called from an exception handler, frompc may be
92 * a user address. Convert such frompc's to some representation
93 * in kernel address space.
95 frompc = MCOUNT_FROMPC_USER(frompc);
98 frompci = frompc - p->lowpc;
99 if (frompci >= p->textsize)
103 if (p->state == GMON_PROF_HIRES) {
105 * Count the time since cputime() was previously called
106 * against `frompc'. Compensate for overheads.
108 * cputime() sets its prev_count variable to the count when
109 * it is called. This in effect starts a counter for
110 * the next period of execution (normally from now until
111 * the next call to mcount() or mexitcount()). We set
112 * cputime_bias to compensate for our own overhead.
114 * We use the usual sampling counters since they can be
115 * located efficiently. 4-byte counters are usually
116 * necessary. gprof will add up the scattered counts
117 * just like it does for statistical profiling. All
118 * counts are signed so that underflow in the subtractions
119 * doesn't matter much (negative counts are normally
120 * compensated for by larger counts elsewhere). Underflow
121 * shouldn't occur, but may be caused by slightly wrong
122 * calibrations or from not clearing cputime_bias.
124 delta = cputime() - cputime_bias - p->mcount_pre_overhead;
125 cputime_bias = p->mcount_post_overhead;
126 KCOUNT(p, frompci) += delta;
127 *p->cputime_count += p->cputime_overhead;
128 *p->mcount_count += p->mcount_overhead;
134 * When we are called from an exception handler, frompc is faked
135 * to be for where the exception occurred. We've just solidified
136 * the count for there. Now convert frompci to an index that
137 * represents the kind of exception so that interruptions appear
138 * in the call graph as calls from those index instead of calls
141 frompc = MCOUNT_FROMPC_INTR(selfpc);
142 if ((frompc - p->lowpc) < p->textsize)
143 frompci = frompc - p->lowpc;
147 * check that frompc is a reasonable pc value.
148 * for example: signal catchers get called from the stack,
149 * not from text space. too bad.
151 if (frompci >= p->textsize)
154 frompcindex = &p->froms[frompci / (p->hashfraction * sizeof(*p->froms))];
155 toindex = *frompcindex;
158 * first time traversing this arc
160 toindex = ++p->tos[0].link;
161 if (toindex >= p->tolimit)
162 /* halt further profiling */
165 *frompcindex = toindex;
166 top = &p->tos[toindex];
167 top->selfpc = selfpc;
172 top = &p->tos[toindex];
173 if (top->selfpc == selfpc) {
175 * arc at front of chain; usual case.
181 * have to go looking down chain for it.
182 * top points to what we are looking at,
183 * prevtop points to previous top.
184 * we know it is not at the head of the chain.
186 for (; /* goto done */; ) {
187 if (top->link == 0) {
189 * top is end of the chain and none of the chain
190 * had top->selfpc == selfpc.
191 * so we allocate a new tostruct
192 * and link it to the head of the chain.
194 toindex = ++p->tos[0].link;
195 if (toindex >= p->tolimit)
198 top = &p->tos[toindex];
199 top->selfpc = selfpc;
201 top->link = *frompcindex;
202 *frompcindex = toindex;
206 * otherwise, check the next arc on the chain.
209 top = &p->tos[top->link];
210 if (top->selfpc == selfpc) {
213 * increment its count
214 * move it to the head of the chain.
217 toindex = prevtop->link;
218 prevtop->link = top->link;
219 top->link = *frompcindex;
220 *frompcindex = toindex;
229 p->state = GMON_PROF_ON;
233 p->state = GMON_PROF_ERROR;
241 * Actual definition of mcount function. Defined in <machine/profile.h>,
242 * which is included by <sys/gmon.h>.
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++)