xref: /freebsd/lib/libc/gmon/mcount.c (revision fbbd9655e5107c68e4e0146ff22b73d7350475bc)
158f0484fSRodney W. Grimes /*-
258f0484fSRodney W. Grimes  * Copyright (c) 1983, 1992, 1993
358f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
458f0484fSRodney W. Grimes  *
558f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
658f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
758f0484fSRodney W. Grimes  * are met:
858f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
958f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1058f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1158f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1258f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13*fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
1458f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1558f0484fSRodney W. Grimes  *    without specific prior written permission.
1658f0484fSRodney W. Grimes  *
1758f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1858f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1958f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2058f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2158f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2258f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2358f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2458f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2558f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2658f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2758f0484fSRodney W. Grimes  * SUCH DAMAGE.
2858f0484fSRodney W. Grimes  */
2958f0484fSRodney W. Grimes 
30a9bdcd37SDavid E. O'Brien #if !defined(_KERNEL) && defined(LIBC_SCCS) && !defined(lint)
3158f0484fSRodney W. Grimes static char sccsid[] = "@(#)mcount.c	8.1 (Berkeley) 6/4/93";
3258f0484fSRodney W. Grimes #endif
33a99d1013SDavid E. O'Brien #include <sys/cdefs.h>
34a99d1013SDavid E. O'Brien __FBSDID("$FreeBSD$");
3558f0484fSRodney W. Grimes 
3658f0484fSRodney W. Grimes #include <sys/param.h>
3758f0484fSRodney W. Grimes #include <sys/gmon.h>
38c4473420SPeter Wemm #ifdef _KERNEL
39912e6037SBruce Evans #include <sys/systm.h>
40912e6037SBruce Evans #include <vm/vm.h>
41912e6037SBruce Evans #include <vm/vm_param.h>
42912e6037SBruce Evans #include <vm/pmap.h>
43a99d1013SDavid E. O'Brien void	bintr(void);
44a99d1013SDavid E. O'Brien void	btrap(void);
45a99d1013SDavid E. O'Brien void	eintr(void);
46a99d1013SDavid E. O'Brien void	user(void);
47691071ffSPaul Richards #endif
480fa2f943SDavid Xu #include <machine/atomic.h>
4958f0484fSRodney W. Grimes 
5058f0484fSRodney W. Grimes /*
5158f0484fSRodney W. Grimes  * mcount is called on entry to each function compiled with the profiling
5258f0484fSRodney W. Grimes  * switch set.  _mcount(), which is declared in a machine-dependent way
5358f0484fSRodney W. Grimes  * with _MCOUNT_DECL, does the actual work and is either inlined into a
5458f0484fSRodney W. Grimes  * C routine or called by an assembly stub.  In any case, this magic is
5558f0484fSRodney W. Grimes  * taken care of by the MCOUNT definition in <machine/profile.h>.
5658f0484fSRodney W. Grimes  *
5758f0484fSRodney W. Grimes  * _mcount updates data structures that represent traversals of the
5858f0484fSRodney W. Grimes  * program's call graph edges.  frompc and selfpc are the return
5958f0484fSRodney W. Grimes  * address and function address that represents the given call graph edge.
6058f0484fSRodney W. Grimes  *
6158f0484fSRodney W. Grimes  * Note: the original BSD code used the same variable (frompcindex) for
6258f0484fSRodney W. Grimes  * both frompcindex and frompc.  Any reasonable, modern compiler will
6358f0484fSRodney W. Grimes  * perform this optimization.
6458f0484fSRodney W. Grimes  */
651d0342a3SJacques Vidrine /* _mcount; may be static, inline, etc */
661d0342a3SJacques Vidrine _MCOUNT_DECL(uintfptr_t frompc, uintfptr_t selfpc)
6758f0484fSRodney W. Grimes {
68912e6037SBruce Evans #ifdef GUPROF
69912e6037SBruce Evans 	u_int delta;
70912e6037SBruce Evans #endif
71a99d1013SDavid E. O'Brien 	fptrdiff_t frompci;
72a99d1013SDavid E. O'Brien 	u_short *frompcindex;
73a99d1013SDavid E. O'Brien 	struct tostruct *top, *prevtop;
74a99d1013SDavid E. O'Brien 	struct gmonparam *p;
75a99d1013SDavid E. O'Brien 	long toindex;
76c4473420SPeter Wemm #ifdef _KERNEL
771f403fcfSBruce Evans 	MCOUNT_DECL(s)
7858f0484fSRodney W. Grimes #endif
7958f0484fSRodney W. Grimes 
8058f0484fSRodney W. Grimes 	p = &_gmonparam;
81912e6037SBruce Evans #ifndef GUPROF			/* XXX */
8258f0484fSRodney W. Grimes 	/*
8358f0484fSRodney W. Grimes 	 * check that we are profiling
8458f0484fSRodney W. Grimes 	 * and that we aren't recursively invoked.
8558f0484fSRodney W. Grimes 	 */
8658f0484fSRodney W. Grimes 	if (p->state != GMON_PROF_ON)
8758f0484fSRodney W. Grimes 		return;
88912e6037SBruce Evans #endif
89c4473420SPeter Wemm #ifdef _KERNEL
901f403fcfSBruce Evans 	MCOUNT_ENTER(s);
9158f0484fSRodney W. Grimes #else
920fa2f943SDavid Xu 	if (!atomic_cmpset_acq_int(&p->state, GMON_PROF_ON, GMON_PROF_BUSY))
930fa2f943SDavid Xu 		return;
9458f0484fSRodney W. Grimes #endif
95912e6037SBruce Evans 	frompci = frompc - p->lowpc;
96912e6037SBruce Evans 
97c4473420SPeter Wemm #ifdef _KERNEL
9858f0484fSRodney W. Grimes 	/*
99912e6037SBruce Evans 	 * When we are called from an exception handler, frompci may be
100912e6037SBruce Evans 	 * for a user address.  Convert such frompci's to the index of
101912e6037SBruce Evans 	 * user() to merge all user counts.
102912e6037SBruce Evans 	 */
103912e6037SBruce Evans 	if (frompci >= p->textsize) {
104912e6037SBruce Evans 		if (frompci + p->lowpc
10537889b39SBruce Evans 		    >= (uintfptr_t)(VM_MAXUSER_ADDRESS + UPAGES * PAGE_SIZE))
106912e6037SBruce Evans 			goto done;
10737889b39SBruce Evans 		frompci = (uintfptr_t)user - p->lowpc;
108912e6037SBruce Evans 		if (frompci >= p->textsize)
109912e6037SBruce Evans 		    goto done;
110912e6037SBruce Evans 	}
111c4473420SPeter Wemm #endif
112912e6037SBruce Evans 
113912e6037SBruce Evans #ifdef GUPROF
114912e6037SBruce Evans 	if (p->state != GMON_PROF_HIRES)
115912e6037SBruce Evans 		goto skip_guprof_stuff;
116912e6037SBruce Evans 	/*
117912e6037SBruce Evans 	 * Look at the clock and add the count of clock cycles since the
118912e6037SBruce Evans 	 * clock was last looked at to a counter for frompc.  This
119912e6037SBruce Evans 	 * solidifies the count for the function containing frompc and
120912e6037SBruce Evans 	 * effectively starts another clock for the current function.
121912e6037SBruce Evans 	 * The count for the new clock will be solidified when another
122912e6037SBruce Evans 	 * function call is made or the function returns.
123912e6037SBruce Evans 	 *
124912e6037SBruce Evans 	 * We use the usual sampling counters since they can be located
125912e6037SBruce Evans 	 * efficiently.  4-byte counters are usually necessary.
126912e6037SBruce Evans 	 *
127912e6037SBruce Evans 	 * There are many complications for subtracting the profiling
128912e6037SBruce Evans 	 * overheads from the counts for normal functions and adding
129912e6037SBruce Evans 	 * them to the counts for mcount(), mexitcount() and cputime().
130912e6037SBruce Evans 	 * We attempt to handle fractional cycles, but the overheads
131912e6037SBruce Evans 	 * are usually underestimated because they are calibrated for
132912e6037SBruce Evans 	 * a simpler than usual setup.
133912e6037SBruce Evans 	 */
134912e6037SBruce Evans 	delta = cputime() - p->mcount_overhead;
135912e6037SBruce Evans 	p->cputime_overhead_resid += p->cputime_overhead_frac;
136912e6037SBruce Evans 	p->mcount_overhead_resid += p->mcount_overhead_frac;
137912e6037SBruce Evans 	if ((int)delta < 0)
138912e6037SBruce Evans 		*p->mcount_count += delta + p->mcount_overhead
139912e6037SBruce Evans 				    - p->cputime_overhead;
140912e6037SBruce Evans 	else if (delta != 0) {
141912e6037SBruce Evans 		if (p->cputime_overhead_resid >= CALIB_SCALE) {
142912e6037SBruce Evans 			p->cputime_overhead_resid -= CALIB_SCALE;
143912e6037SBruce Evans 			++*p->cputime_count;
144912e6037SBruce Evans 			--delta;
145912e6037SBruce Evans 		}
146912e6037SBruce Evans 		if (delta != 0) {
147912e6037SBruce Evans 			if (p->mcount_overhead_resid >= CALIB_SCALE) {
148912e6037SBruce Evans 				p->mcount_overhead_resid -= CALIB_SCALE;
149912e6037SBruce Evans 				++*p->mcount_count;
150912e6037SBruce Evans 				--delta;
151912e6037SBruce Evans 			}
152912e6037SBruce Evans 			KCOUNT(p, frompci) += delta;
153912e6037SBruce Evans 		}
154912e6037SBruce Evans 		*p->mcount_count += p->mcount_overhead_sub;
155912e6037SBruce Evans 	}
156912e6037SBruce Evans 	*p->cputime_count += p->cputime_overhead;
157912e6037SBruce Evans skip_guprof_stuff:
158912e6037SBruce Evans #endif /* GUPROF */
159912e6037SBruce Evans 
160c4473420SPeter Wemm #ifdef _KERNEL
161912e6037SBruce Evans 	/*
162912e6037SBruce Evans 	 * When we are called from an exception handler, frompc is faked
163912e6037SBruce Evans 	 * to be for where the exception occurred.  We've just solidified
164912e6037SBruce Evans 	 * the count for there.  Now convert frompci to the index of btrap()
165912e6037SBruce Evans 	 * for trap handlers and bintr() for interrupt handlers to make
166912e6037SBruce Evans 	 * exceptions appear in the call graph as calls from btrap() and
167912e6037SBruce Evans 	 * bintr() instead of calls from all over.
168912e6037SBruce Evans 	 */
16937889b39SBruce Evans 	if ((uintfptr_t)selfpc >= (uintfptr_t)btrap
17037889b39SBruce Evans 	    && (uintfptr_t)selfpc < (uintfptr_t)eintr) {
17137889b39SBruce Evans 		if ((uintfptr_t)selfpc >= (uintfptr_t)bintr)
17237889b39SBruce Evans 			frompci = (uintfptr_t)bintr - p->lowpc;
173912e6037SBruce Evans 		else
17437889b39SBruce Evans 			frompci = (uintfptr_t)btrap - p->lowpc;
175912e6037SBruce Evans 	}
176c4473420SPeter Wemm #endif
177912e6037SBruce Evans 
178912e6037SBruce Evans 	/*
179912e6037SBruce Evans 	 * check that frompc is a reasonable pc value.
18058f0484fSRodney W. Grimes 	 * for example:	signal catchers get called from the stack,
18158f0484fSRodney W. Grimes 	 *		not from text space.  too bad.
18258f0484fSRodney W. Grimes 	 */
183912e6037SBruce Evans 	if (frompci >= p->textsize)
18458f0484fSRodney W. Grimes 		goto done;
18558f0484fSRodney W. Grimes 
186912e6037SBruce Evans 	frompcindex = &p->froms[frompci / (p->hashfraction * sizeof(*p->froms))];
18758f0484fSRodney W. Grimes 	toindex = *frompcindex;
18858f0484fSRodney W. Grimes 	if (toindex == 0) {
18958f0484fSRodney W. Grimes 		/*
19058f0484fSRodney W. Grimes 		 *	first time traversing this arc
19158f0484fSRodney W. Grimes 		 */
19258f0484fSRodney W. Grimes 		toindex = ++p->tos[0].link;
19358f0484fSRodney W. Grimes 		if (toindex >= p->tolimit)
19458f0484fSRodney W. Grimes 			/* halt further profiling */
19558f0484fSRodney W. Grimes 			goto overflow;
19658f0484fSRodney W. Grimes 
19758f0484fSRodney W. Grimes 		*frompcindex = toindex;
19858f0484fSRodney W. Grimes 		top = &p->tos[toindex];
19958f0484fSRodney W. Grimes 		top->selfpc = selfpc;
20058f0484fSRodney W. Grimes 		top->count = 1;
20158f0484fSRodney W. Grimes 		top->link = 0;
20258f0484fSRodney W. Grimes 		goto done;
20358f0484fSRodney W. Grimes 	}
20458f0484fSRodney W. Grimes 	top = &p->tos[toindex];
20558f0484fSRodney W. Grimes 	if (top->selfpc == selfpc) {
20658f0484fSRodney W. Grimes 		/*
20758f0484fSRodney W. Grimes 		 * arc at front of chain; usual case.
20858f0484fSRodney W. Grimes 		 */
20958f0484fSRodney W. Grimes 		top->count++;
21058f0484fSRodney W. Grimes 		goto done;
21158f0484fSRodney W. Grimes 	}
21258f0484fSRodney W. Grimes 	/*
21358f0484fSRodney W. Grimes 	 * have to go looking down chain for it.
21458f0484fSRodney W. Grimes 	 * top points to what we are looking at,
21558f0484fSRodney W. Grimes 	 * prevtop points to previous top.
21658f0484fSRodney W. Grimes 	 * we know it is not at the head of the chain.
21758f0484fSRodney W. Grimes 	 */
21858f0484fSRodney W. Grimes 	for (; /* goto done */; ) {
21958f0484fSRodney W. Grimes 		if (top->link == 0) {
22058f0484fSRodney W. Grimes 			/*
22158f0484fSRodney W. Grimes 			 * top is end of the chain and none of the chain
22258f0484fSRodney W. Grimes 			 * had top->selfpc == selfpc.
22358f0484fSRodney W. Grimes 			 * so we allocate a new tostruct
22458f0484fSRodney W. Grimes 			 * and link it to the head of the chain.
22558f0484fSRodney W. Grimes 			 */
22658f0484fSRodney W. Grimes 			toindex = ++p->tos[0].link;
22758f0484fSRodney W. Grimes 			if (toindex >= p->tolimit)
22858f0484fSRodney W. Grimes 				goto overflow;
22958f0484fSRodney W. Grimes 
23058f0484fSRodney W. Grimes 			top = &p->tos[toindex];
23158f0484fSRodney W. Grimes 			top->selfpc = selfpc;
23258f0484fSRodney W. Grimes 			top->count = 1;
23358f0484fSRodney W. Grimes 			top->link = *frompcindex;
23458f0484fSRodney W. Grimes 			*frompcindex = toindex;
23558f0484fSRodney W. Grimes 			goto done;
23658f0484fSRodney W. Grimes 		}
23758f0484fSRodney W. Grimes 		/*
23858f0484fSRodney W. Grimes 		 * otherwise, check the next arc on the chain.
23958f0484fSRodney W. Grimes 		 */
24058f0484fSRodney W. Grimes 		prevtop = top;
24158f0484fSRodney W. Grimes 		top = &p->tos[top->link];
24258f0484fSRodney W. Grimes 		if (top->selfpc == selfpc) {
24358f0484fSRodney W. Grimes 			/*
24458f0484fSRodney W. Grimes 			 * there it is.
24558f0484fSRodney W. Grimes 			 * increment its count
24658f0484fSRodney W. Grimes 			 * move it to the head of the chain.
24758f0484fSRodney W. Grimes 			 */
24858f0484fSRodney W. Grimes 			top->count++;
24958f0484fSRodney W. Grimes 			toindex = prevtop->link;
25058f0484fSRodney W. Grimes 			prevtop->link = top->link;
25158f0484fSRodney W. Grimes 			top->link = *frompcindex;
25258f0484fSRodney W. Grimes 			*frompcindex = toindex;
25358f0484fSRodney W. Grimes 			goto done;
25458f0484fSRodney W. Grimes 		}
25558f0484fSRodney W. Grimes 
25658f0484fSRodney W. Grimes 	}
25758f0484fSRodney W. Grimes done:
258c4473420SPeter Wemm #ifdef _KERNEL
2591f403fcfSBruce Evans 	MCOUNT_EXIT(s);
26058f0484fSRodney W. Grimes #else
2610fa2f943SDavid Xu 	atomic_store_rel_int(&p->state, GMON_PROF_ON);
26258f0484fSRodney W. Grimes #endif
26358f0484fSRodney W. Grimes 	return;
26458f0484fSRodney W. Grimes overflow:
2650fa2f943SDavid Xu 	atomic_store_rel_int(&p->state, GMON_PROF_ERROR);
266c4473420SPeter Wemm #ifdef _KERNEL
2671f403fcfSBruce Evans 	MCOUNT_EXIT(s);
26858f0484fSRodney W. Grimes #endif
26958f0484fSRodney W. Grimes 	return;
27058f0484fSRodney W. Grimes }
27158f0484fSRodney W. Grimes 
27258f0484fSRodney W. Grimes /*
27358f0484fSRodney W. Grimes  * Actual definition of mcount function.  Defined in <machine/profile.h>,
27458f0484fSRodney W. Grimes  * which is included by <sys/gmon.h>.
27558f0484fSRodney W. Grimes  */
27658f0484fSRodney W. Grimes MCOUNT
277912e6037SBruce Evans 
278912e6037SBruce Evans #ifdef GUPROF
279912e6037SBruce Evans void
280912e6037SBruce Evans mexitcount(selfpc)
28137889b39SBruce Evans 	uintfptr_t selfpc;
282912e6037SBruce Evans {
283912e6037SBruce Evans 	struct gmonparam *p;
28437889b39SBruce Evans 	uintfptr_t selfpcdiff;
285912e6037SBruce Evans 
286912e6037SBruce Evans 	p = &_gmonparam;
28737889b39SBruce Evans 	selfpcdiff = selfpc - (uintfptr_t)p->lowpc;
288912e6037SBruce Evans 	if (selfpcdiff < p->textsize) {
289912e6037SBruce Evans 		u_int delta;
290912e6037SBruce Evans 
291912e6037SBruce Evans 		/*
292912e6037SBruce Evans 		 * Solidify the count for the current function.
293912e6037SBruce Evans 		 */
294912e6037SBruce Evans 		delta = cputime() - p->mexitcount_overhead;
295912e6037SBruce Evans 		p->cputime_overhead_resid += p->cputime_overhead_frac;
296912e6037SBruce Evans 		p->mexitcount_overhead_resid += p->mexitcount_overhead_frac;
297912e6037SBruce Evans 		if ((int)delta < 0)
298912e6037SBruce Evans 			*p->mexitcount_count += delta + p->mexitcount_overhead
299912e6037SBruce Evans 						- p->cputime_overhead;
300912e6037SBruce Evans 		else if (delta != 0) {
301912e6037SBruce Evans 			if (p->cputime_overhead_resid >= CALIB_SCALE) {
302912e6037SBruce Evans 				p->cputime_overhead_resid -= CALIB_SCALE;
303912e6037SBruce Evans 				++*p->cputime_count;
304912e6037SBruce Evans 				--delta;
305912e6037SBruce Evans 			}
306912e6037SBruce Evans 			if (delta != 0) {
307912e6037SBruce Evans 				if (p->mexitcount_overhead_resid
308912e6037SBruce Evans 				    >= CALIB_SCALE) {
309912e6037SBruce Evans 					p->mexitcount_overhead_resid
310912e6037SBruce Evans 					    -= CALIB_SCALE;
311912e6037SBruce Evans 					++*p->mexitcount_count;
312912e6037SBruce Evans 					--delta;
313912e6037SBruce Evans 				}
314912e6037SBruce Evans 				KCOUNT(p, selfpcdiff) += delta;
315912e6037SBruce Evans 			}
316912e6037SBruce Evans 			*p->mexitcount_count += p->mexitcount_overhead_sub;
317912e6037SBruce Evans 		}
318912e6037SBruce Evans 		*p->cputime_count += p->cputime_overhead;
319912e6037SBruce Evans 	}
320912e6037SBruce Evans }
321912e6037SBruce Evans #endif /* GUPROF */
322