xref: /dragonfly/lib/libc/gmon/mcount.c (revision e98bdfd3)
1 /*-
2  * Copyright (c) 1983, 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  *
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 $
32  */
33 
34 #include <sys/param.h>
35 #include <sys/gmon.h>
36 #ifdef _KERNEL
37 #include <sys/systm.h>
38 #include <vm/vm.h>
39 #include <vm/vm_param.h>
40 #include <vm/pmap.h>
41 void	bintr(void);
42 void	btrap(void);
43 void	eintr(void);
44 void	user(void);
45 #endif
46 
47 /*
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>.
53  *
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.
57  *
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.
61  */
62 /* _mcount; may be static, inline, etc */
63 _MCOUNT_DECL(uintfptr_t frompc, uintfptr_t selfpc)
64 {
65 #ifdef GUPROF
66 	u_int delta;
67 #endif
68 	fptrdiff_t frompci;
69 	u_short *frompcindex;
70 	struct tostruct *top, *prevtop;
71 	struct gmonparam *p;
72 	long toindex;
73 #ifdef _KERNEL
74 	MCOUNT_DECL(s)
75 #endif
76 
77 	p = &_gmonparam;
78 #ifndef GUPROF			/* XXX */
79 	/*
80 	 * check that we are profiling
81 	 * and that we aren't recursively invoked.
82 	 */
83 	if (p->state != GMON_PROF_ON)
84 		return;
85 #endif
86 #ifdef _KERNEL
87 	MCOUNT_ENTER(s);
88 #else
89 	p->state = GMON_PROF_BUSY;	/* XXX */
90 #endif
91 	frompci = frompc - p->lowpc;
92 
93 #ifdef _KERNEL
94 	/*
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.
98 	 */
99 	if (frompci >= p->textsize) {
100 		if (frompci + p->lowpc
101 		    >= (uintfptr_t)(VM_MAXUSER_ADDRESS + UPAGES * PAGE_SIZE))
102 			goto done;
103 		frompci = (uintfptr_t)user - p->lowpc;
104 		if (frompci >= p->textsize)
105 		    goto done;
106 	}
107 #endif
108 
109 #ifdef GUPROF
110 	if (p->state != GMON_PROF_HIRES)
111 		goto skip_guprof_stuff;
112 	/*
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.
119 	 *
120 	 * We use the usual sampling counters since they can be located
121 	 * efficiently.  4-byte counters are usually necessary.
122 	 *
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.
129 	 */
130 	delta = cputime() - p->mcount_overhead;
131 	p->cputime_overhead_resid += p->cputime_overhead_frac;
132 	p->mcount_overhead_resid += p->mcount_overhead_frac;
133 	if ((int)delta < 0)
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;
139 			++*p->cputime_count;
140 			--delta;
141 		}
142 		if (delta != 0) {
143 			if (p->mcount_overhead_resid >= CALIB_SCALE) {
144 				p->mcount_overhead_resid -= CALIB_SCALE;
145 				++*p->mcount_count;
146 				--delta;
147 			}
148 			KCOUNT(p, frompci) += delta;
149 		}
150 		*p->mcount_count += p->mcount_overhead_sub;
151 	}
152 	*p->cputime_count += p->cputime_overhead;
153 skip_guprof_stuff:
154 #endif /* GUPROF */
155 
156 #ifdef _KERNEL
157 	/*
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.
164 	 */
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;
169 		else
170 			frompci = (uintfptr_t)btrap - p->lowpc;
171 	}
172 #endif
173 
174 	/*
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.
178 	 */
179 	if (frompci >= p->textsize)
180 		goto done;
181 
182 	frompcindex = &p->froms[frompci / (p->hashfraction * sizeof(*p->froms))];
183 	toindex = *frompcindex;
184 	if (toindex == 0) {
185 		/*
186 		 *	first time traversing this arc
187 		 */
188 		toindex = ++p->tos[0].link;
189 		if (toindex >= p->tolimit)
190 			/* halt further profiling */
191 			goto overflow;
192 
193 		*frompcindex = toindex;
194 		top = &p->tos[toindex];
195 		top->selfpc = selfpc;
196 		top->count = 1;
197 		top->link = 0;
198 		goto done;
199 	}
200 	top = &p->tos[toindex];
201 	if (top->selfpc == selfpc) {
202 		/*
203 		 * arc at front of chain; usual case.
204 		 */
205 		top->count++;
206 		goto done;
207 	}
208 	/*
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.
213 	 */
214 	for (; /* goto done */; ) {
215 		if (top->link == 0) {
216 			/*
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.
221 			 */
222 			toindex = ++p->tos[0].link;
223 			if (toindex >= p->tolimit)
224 				goto overflow;
225 
226 			top = &p->tos[toindex];
227 			top->selfpc = selfpc;
228 			top->count = 1;
229 			top->link = *frompcindex;
230 			*frompcindex = toindex;
231 			goto done;
232 		}
233 		/*
234 		 * otherwise, check the next arc on the chain.
235 		 */
236 		prevtop = top;
237 		top = &p->tos[top->link];
238 		if (top->selfpc == selfpc) {
239 			/*
240 			 * there it is.
241 			 * increment its count
242 			 * move it to the head of the chain.
243 			 */
244 			top->count++;
245 			toindex = prevtop->link;
246 			prevtop->link = top->link;
247 			top->link = *frompcindex;
248 			*frompcindex = toindex;
249 			goto done;
250 		}
251 
252 	}
253 done:
254 #ifdef _KERNEL
255 	MCOUNT_EXIT(s);
256 #else
257 	p->state = GMON_PROF_ON;	/* XXX */
258 #endif
259 	return;
260 overflow:
261 	p->state = GMON_PROF_ERROR;	/* XXX */
262 #ifdef _KERNEL
263 	MCOUNT_EXIT(s);
264 #endif
265 	return;
266 }
267 
268 /*
269  * Actual definition of mcount function.  Defined in <machine/profile.h>,
270  * which is included by <sys/gmon.h>.
271  */
272 MCOUNT
273 
274 #ifdef GUPROF
275 void
276 mexitcount(uintfptr_t selfpc)
277 {
278 	struct gmonparam *p;
279 	uintfptr_t selfpcdiff;
280 
281 	p = &_gmonparam;
282 	selfpcdiff = selfpc - (uintfptr_t)p->lowpc;
283 	if (selfpcdiff < p->textsize) {
284 		u_int delta;
285 
286 		/*
287 		 * Solidify the count for the current function.
288 		 */
289 		delta = cputime() - p->mexitcount_overhead;
290 		p->cputime_overhead_resid += p->cputime_overhead_frac;
291 		p->mexitcount_overhead_resid += p->mexitcount_overhead_frac;
292 		if ((int)delta < 0)
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;
298 				++*p->cputime_count;
299 				--delta;
300 			}
301 			if (delta != 0) {
302 				if (p->mexitcount_overhead_resid
303 				    >= CALIB_SCALE) {
304 					p->mexitcount_overhead_resid
305 					    -= CALIB_SCALE;
306 					++*p->mexitcount_count;
307 					--delta;
308 				}
309 				KCOUNT(p, selfpcdiff) += delta;
310 			}
311 			*p->mexitcount_count += p->mexitcount_overhead_sub;
312 		}
313 		*p->cputime_count += p->cputime_overhead;
314 	}
315 }
316 #endif /* GUPROF */
317