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 * 3. 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
29 * @(#)mcount.c 8.1 (Berkeley) 6/4/93
30 * $FreeBSD: src/lib/libc/gmon/mcount.c,v 1.20 2004/10/16 06:32:43 obrien Exp $
31 * $DragonFly: src/lib/libc/gmon/mcount.c,v 1.5 2005/11/13 01:18:20 swildner Exp $
34 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <vm/vm_param.h>
48 * mcount is called on entry to each function compiled with the profiling
49 * switch set. _mcount(), which is declared in a machine-dependent way
50 * with _MCOUNT_DECL, does the actual work and is either inlined into a
51 * C routine or called by an assembly stub. In any case, this magic is
52 * taken care of by the MCOUNT definition in <machine/profile.h>.
54 * _mcount updates data structures that represent traversals of the
55 * program's call graph edges. frompc and selfpc are the return
56 * address and function address that represents the given call graph edge.
58 * Note: the original BSD code used the same variable (frompcindex) for
59 * both frompcindex and frompc. Any reasonable, modern compiler will
60 * perform this optimization.
62 /* _mcount; may be static, inline, etc */
63 _MCOUNT_DECL(uintfptr_t frompc
, uintfptr_t selfpc
)
70 struct tostruct
*top
, *prevtop
;
78 #ifndef GUPROF /* XXX */
80 * check that we are profiling
81 * and that we aren't recursively invoked.
83 if (p
->state
!= GMON_PROF_ON
)
89 p
->state
= GMON_PROF_BUSY
; /* XXX */
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 p
->state
= GMON_PROF_ON
; /* XXX */
261 p
->state
= GMON_PROF_ERROR
; /* XXX */
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
;