xref: /freebsd/sys/kern/subr_prof.c (revision 69a2875821ed9404a95f41bbeeb561908e3c38dc)
1df8bae1dSRodney W. Grimes /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13*69a28758SEd Maste  * 3. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  *	@(#)subr_prof.c	8.3 (Berkeley) 9/23/93
30df8bae1dSRodney W. Grimes  */
31df8bae1dSRodney W. Grimes 
32677b542eSDavid E. O'Brien #include <sys/cdefs.h>
33677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
34677b542eSDavid E. O'Brien 
35df8bae1dSRodney W. Grimes #include <sys/param.h>
36df8bae1dSRodney W. Grimes #include <sys/systm.h>
37d2d3e875SBruce Evans #include <sys/sysproto.h>
389e420850SBruce Evans #include <sys/kernel.h>
39fb919e4dSMark Murray #include <sys/lock.h>
40fb919e4dSMark Murray #include <sys/mutex.h>
41df8bae1dSRodney W. Grimes #include <sys/proc.h>
422baeef32SBruce Evans #include <sys/resourcevar.h>
439e420850SBruce Evans #include <sys/sysctl.h>
44b5e8ce9fSBruce Evans 
45df8bae1dSRodney W. Grimes #include <machine/cpu.h>
46df8bae1dSRodney W. Grimes 
47df8bae1dSRodney W. Grimes #ifdef GPROF
48df8bae1dSRodney W. Grimes #include <sys/malloc.h>
49df8bae1dSRodney W. Grimes #include <sys/gmon.h>
50ea2b3e3dSBruce Evans #undef MCOUNT
51df8bae1dSRodney W. Grimes 
52a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_GPROF, "gprof", "kernel profiling buffer");
5355166637SPoul-Henning Kamp 
544d77a549SAlfred Perlstein static void kmstartup(void *);
55237fdd78SRobert Watson SYSINIT(kmem, SI_SUB_KPROF, SI_ORDER_FIRST, kmstartup, NULL);
562b14f991SJulian Elischer 
57df8bae1dSRodney W. Grimes struct gmonparam _gmonparam = { GMON_PROF_OFF };
58df8bae1dSRodney W. Grimes 
59d6b9e17eSBruce Evans #ifdef GUPROF
60d6b9e17eSBruce Evans void
61d6b9e17eSBruce Evans nullfunc_loop_profiled()
62d6b9e17eSBruce Evans {
63d6b9e17eSBruce Evans 	int i;
64d6b9e17eSBruce Evans 
65d6b9e17eSBruce Evans 	for (i = 0; i < CALIB_SCALE; i++)
66d6b9e17eSBruce Evans 		nullfunc_profiled();
67d6b9e17eSBruce Evans }
68d6b9e17eSBruce Evans 
69e408eccfSBruce Evans #define	nullfunc_loop_profiled_end	nullfunc_profiled	/* XXX */
70e408eccfSBruce Evans 
71d6b9e17eSBruce Evans void
72d6b9e17eSBruce Evans nullfunc_profiled()
73d6b9e17eSBruce Evans {
74d6b9e17eSBruce Evans }
75d6b9e17eSBruce Evans #endif /* GUPROF */
76d6b9e17eSBruce Evans 
774a44bd4bSBrian Feldman /*
784a44bd4bSBrian Feldman  * Update the histograms to support extending the text region arbitrarily.
794a44bd4bSBrian Feldman  * This is done slightly naively (no sparse regions), so will waste slight
804a44bd4bSBrian Feldman  * amounts of memory, but will overall work nicely enough to allow profiling
814a44bd4bSBrian Feldman  * of KLDs.
824a44bd4bSBrian Feldman  */
834a44bd4bSBrian Feldman void
844a44bd4bSBrian Feldman kmupetext(uintfptr_t nhighpc)
854a44bd4bSBrian Feldman {
864a44bd4bSBrian Feldman 	struct gmonparam np;	/* slightly large */
874a44bd4bSBrian Feldman 	struct gmonparam *p = &_gmonparam;
884a44bd4bSBrian Feldman 	char *cp;
894a44bd4bSBrian Feldman 
904a44bd4bSBrian Feldman 	GIANT_REQUIRED;
914a44bd4bSBrian Feldman 	bcopy(p, &np, sizeof(*p));
924a44bd4bSBrian Feldman 	np.highpc = ROUNDUP(nhighpc, HISTFRACTION * sizeof(HISTCOUNTER));
934a44bd4bSBrian Feldman 	if (np.highpc <= p->highpc)
944a44bd4bSBrian Feldman 		return;
954a44bd4bSBrian Feldman 	np.textsize = np.highpc - p->lowpc;
964a44bd4bSBrian Feldman 	np.kcountsize = np.textsize / HISTFRACTION;
974a44bd4bSBrian Feldman 	np.hashfraction = HASHFRACTION;
984a44bd4bSBrian Feldman 	np.fromssize = np.textsize / HASHFRACTION;
994a44bd4bSBrian Feldman 	np.tolimit = np.textsize * ARCDENSITY / 100;
1004a44bd4bSBrian Feldman 	if (np.tolimit < MINARCS)
1014a44bd4bSBrian Feldman 		np.tolimit = MINARCS;
1024a44bd4bSBrian Feldman 	else if (np.tolimit > MAXARCS)
1034a44bd4bSBrian Feldman 		np.tolimit = MAXARCS;
1044a44bd4bSBrian Feldman 	np.tossize = np.tolimit * sizeof(struct tostruct);
1054a44bd4bSBrian Feldman 	cp = malloc(np.kcountsize + np.fromssize + np.tossize,
106a163d034SWarner Losh 	    M_GPROF, M_WAITOK);
1074a44bd4bSBrian Feldman 	/*
1084a44bd4bSBrian Feldman 	 * Check for something else extending highpc while we slept.
1094a44bd4bSBrian Feldman 	 */
1104a44bd4bSBrian Feldman 	if (np.highpc <= p->highpc) {
1114a44bd4bSBrian Feldman 		free(cp, M_GPROF);
1124a44bd4bSBrian Feldman 		return;
1134a44bd4bSBrian Feldman 	}
1144a44bd4bSBrian Feldman 	np.tos = (struct tostruct *)cp;
1154a44bd4bSBrian Feldman 	cp += np.tossize;
1164a44bd4bSBrian Feldman 	np.kcount = (HISTCOUNTER *)cp;
1174a44bd4bSBrian Feldman 	cp += np.kcountsize;
1184a44bd4bSBrian Feldman 	np.froms = (u_short *)cp;
1194a44bd4bSBrian Feldman #ifdef GUPROF
1204a44bd4bSBrian Feldman 	/* Reinitialize pointers to overhead counters. */
1214a44bd4bSBrian Feldman 	np.cputime_count = &KCOUNT(&np, PC_TO_I(&np, cputime));
1224a44bd4bSBrian Feldman 	np.mcount_count = &KCOUNT(&np, PC_TO_I(&np, mcount));
1234a44bd4bSBrian Feldman 	np.mexitcount_count = &KCOUNT(&np, PC_TO_I(&np, mexitcount));
1244a44bd4bSBrian Feldman #endif
1257e1f6dfeSJohn Baldwin 	critical_enter();
1264a44bd4bSBrian Feldman 	bcopy(p->tos, np.tos, p->tossize);
1274a44bd4bSBrian Feldman 	bzero((char *)np.tos + p->tossize, np.tossize - p->tossize);
1284a44bd4bSBrian Feldman 	bcopy(p->kcount, np.kcount, p->kcountsize);
1294a44bd4bSBrian Feldman 	bzero((char *)np.kcount + p->kcountsize, np.kcountsize -
1304a44bd4bSBrian Feldman 	    p->kcountsize);
1314a44bd4bSBrian Feldman 	bcopy(p->froms, np.froms, p->fromssize);
1324a44bd4bSBrian Feldman 	bzero((char *)np.froms + p->fromssize, np.fromssize - p->fromssize);
1334a44bd4bSBrian Feldman 	cp = (char *)p->tos;
1344a44bd4bSBrian Feldman 	bcopy(&np, p, sizeof(*p));
1357e1f6dfeSJohn Baldwin 	critical_exit();
1364a44bd4bSBrian Feldman 	free(cp, M_GPROF);
1374a44bd4bSBrian Feldman }
1384a44bd4bSBrian Feldman 
1395c7761b2SBruce Evans static void
140d841aaa7SBruce Evans kmstartup(dummy)
141d841aaa7SBruce Evans 	void *dummy;
142df8bae1dSRodney W. Grimes {
143df8bae1dSRodney W. Grimes 	char *cp;
144df8bae1dSRodney W. Grimes 	struct gmonparam *p = &_gmonparam;
145912e6037SBruce Evans #ifdef GUPROF
146d6b9e17eSBruce Evans 	int cputime_overhead;
147d6b9e17eSBruce Evans 	int empty_loop_time;
148912e6037SBruce Evans 	int i;
149d6b9e17eSBruce Evans 	int mcount_overhead;
150d6b9e17eSBruce Evans 	int mexitcount_overhead;
151d6b9e17eSBruce Evans 	int nullfunc_loop_overhead;
152d6b9e17eSBruce Evans 	int nullfunc_loop_profiled_time;
15337889b39SBruce Evans 	uintfptr_t tmp_addr;
1545e1aea9fSPoul-Henning Kamp #endif
155912e6037SBruce Evans 
156df8bae1dSRodney W. Grimes 	/*
157df8bae1dSRodney W. Grimes 	 * Round lowpc and highpc to multiples of the density we're using
158df8bae1dSRodney W. Grimes 	 * so the rest of the scaling (here and in gprof) stays in ints.
159df8bae1dSRodney W. Grimes 	 */
160e45d35c3SBruce Evans 	p->lowpc = ROUNDDOWN((u_long)btext, HISTFRACTION * sizeof(HISTCOUNTER));
161df8bae1dSRodney W. Grimes 	p->highpc = ROUNDUP((u_long)etext, HISTFRACTION * sizeof(HISTCOUNTER));
162df8bae1dSRodney W. Grimes 	p->textsize = p->highpc - p->lowpc;
163372c2e96SBruce Evans 	printf("Profiling kernel, textsize=%lu [%jx..%jx]\n",
164372c2e96SBruce Evans 	    p->textsize, (uintmax_t)p->lowpc, (uintmax_t)p->highpc);
165df8bae1dSRodney W. Grimes 	p->kcountsize = p->textsize / HISTFRACTION;
166df8bae1dSRodney W. Grimes 	p->hashfraction = HASHFRACTION;
167df8bae1dSRodney W. Grimes 	p->fromssize = p->textsize / HASHFRACTION;
168df8bae1dSRodney W. Grimes 	p->tolimit = p->textsize * ARCDENSITY / 100;
169df8bae1dSRodney W. Grimes 	if (p->tolimit < MINARCS)
170df8bae1dSRodney W. Grimes 		p->tolimit = MINARCS;
171df8bae1dSRodney W. Grimes 	else if (p->tolimit > MAXARCS)
172df8bae1dSRodney W. Grimes 		p->tolimit = MAXARCS;
173df8bae1dSRodney W. Grimes 	p->tossize = p->tolimit * sizeof(struct tostruct);
174df8bae1dSRodney W. Grimes 	cp = (char *)malloc(p->kcountsize + p->fromssize + p->tossize,
175a163d034SWarner Losh 	    M_GPROF, M_WAITOK | M_ZERO);
176df8bae1dSRodney W. Grimes 	p->tos = (struct tostruct *)cp;
177df8bae1dSRodney W. Grimes 	cp += p->tossize;
178912e6037SBruce Evans 	p->kcount = (HISTCOUNTER *)cp;
179df8bae1dSRodney W. Grimes 	cp += p->kcountsize;
180df8bae1dSRodney W. Grimes 	p->froms = (u_short *)cp;
181c81d4a03SBruce Evans 	p->histcounter_type = FUNCTION_ALIGNMENT / HISTFRACTION * NBBY;
182912e6037SBruce Evans 
183912e6037SBruce Evans #ifdef GUPROF
184c81d4a03SBruce Evans 	/* Signed counters. */
185c81d4a03SBruce Evans 	p->histcounter_type = -p->histcounter_type;
186c81d4a03SBruce Evans 
187d6b9e17eSBruce Evans 	/* Initialize pointers to overhead counters. */
188912e6037SBruce Evans 	p->cputime_count = &KCOUNT(p, PC_TO_I(p, cputime));
189912e6037SBruce Evans 	p->mcount_count = &KCOUNT(p, PC_TO_I(p, mcount));
190912e6037SBruce Evans 	p->mexitcount_count = &KCOUNT(p, PC_TO_I(p, mexitcount));
191912e6037SBruce Evans 
192912e6037SBruce Evans 	/*
193d6b9e17eSBruce Evans 	 * Disable interrupts to avoid interference while we calibrate
194d6b9e17eSBruce Evans 	 * things.
195912e6037SBruce Evans 	 */
1967e1f6dfeSJohn Baldwin 	critical_enter();
197d6b9e17eSBruce Evans 
198d6b9e17eSBruce Evans 	/*
199d6b9e17eSBruce Evans 	 * Determine overheads.
200d6b9e17eSBruce Evans 	 * XXX this needs to be repeated for each useful timer/counter.
201d6b9e17eSBruce Evans 	 */
202d6b9e17eSBruce Evans 	cputime_overhead = 0;
203d6b9e17eSBruce Evans 	startguprof(p);
204d6b9e17eSBruce Evans 	for (i = 0; i < CALIB_SCALE; i++)
205d6b9e17eSBruce Evans 		cputime_overhead += cputime();
206d6b9e17eSBruce Evans 
207d6b9e17eSBruce Evans 	empty_loop();
208d6b9e17eSBruce Evans 	startguprof(p);
209d6b9e17eSBruce Evans 	empty_loop();
210d6b9e17eSBruce Evans 	empty_loop_time = cputime();
211d6b9e17eSBruce Evans 
212d6b9e17eSBruce Evans 	nullfunc_loop_profiled();
213d6b9e17eSBruce Evans 
214d6b9e17eSBruce Evans 	/*
215d6b9e17eSBruce Evans 	 * Start profiling.  There won't be any normal function calls since
216d6b9e17eSBruce Evans 	 * interrupts are disabled, but we will call the profiling routines
217d6b9e17eSBruce Evans 	 * directly to determine their overheads.
218d6b9e17eSBruce Evans 	 */
219912e6037SBruce Evans 	p->state = GMON_PROF_HIRES;
220912e6037SBruce Evans 
221d6b9e17eSBruce Evans 	startguprof(p);
222d6b9e17eSBruce Evans 	nullfunc_loop_profiled();
223912e6037SBruce Evans 
224d6b9e17eSBruce Evans 	startguprof(p);
225912e6037SBruce Evans 	for (i = 0; i < CALIB_SCALE; i++)
2268451d0ddSKip Macy 		MCOUNT_OVERHEAD(sys_profil);
2278451d0ddSKip Macy 	mcount_overhead = KCOUNT(p, PC_TO_I(p, sys_profil));
228912e6037SBruce Evans 
229d6b9e17eSBruce Evans 	startguprof(p);
230912e6037SBruce Evans 	for (i = 0; i < CALIB_SCALE; i++)
231e77c22bfSBruce Evans 		MEXITCOUNT_OVERHEAD();
232e77c22bfSBruce Evans 	MEXITCOUNT_OVERHEAD_GETLABEL(tmp_addr);
233e408eccfSBruce Evans 	mexitcount_overhead = KCOUNT(p, PC_TO_I(p, tmp_addr));
234912e6037SBruce Evans 
235912e6037SBruce Evans 	p->state = GMON_PROF_OFF;
236d6b9e17eSBruce Evans 	stopguprof(p);
237d6b9e17eSBruce Evans 
2387e1f6dfeSJohn Baldwin 	critical_exit();
239912e6037SBruce Evans 
240d6b9e17eSBruce Evans 	nullfunc_loop_profiled_time = 0;
24137889b39SBruce Evans 	for (tmp_addr = (uintfptr_t)nullfunc_loop_profiled;
24237889b39SBruce Evans 	     tmp_addr < (uintfptr_t)nullfunc_loop_profiled_end;
243e408eccfSBruce Evans 	     tmp_addr += HISTFRACTION * sizeof(HISTCOUNTER))
244e408eccfSBruce Evans 		nullfunc_loop_profiled_time += KCOUNT(p, PC_TO_I(p, tmp_addr));
245d6b9e17eSBruce Evans #define CALIB_DOSCALE(count)	(((count) + CALIB_SCALE / 3) / CALIB_SCALE)
246d6b9e17eSBruce Evans #define	c2n(count, freq)	((int)((count) * 1000000000LL / freq))
247d6b9e17eSBruce Evans 	printf("cputime %d, empty_loop %d, nullfunc_loop_profiled %d, mcount %d, mexitcount %d\n",
248d6b9e17eSBruce Evans 	       CALIB_DOSCALE(c2n(cputime_overhead, p->profrate)),
249d6b9e17eSBruce Evans 	       CALIB_DOSCALE(c2n(empty_loop_time, p->profrate)),
250d6b9e17eSBruce Evans 	       CALIB_DOSCALE(c2n(nullfunc_loop_profiled_time, p->profrate)),
251d6b9e17eSBruce Evans 	       CALIB_DOSCALE(c2n(mcount_overhead, p->profrate)),
252d6b9e17eSBruce Evans 	       CALIB_DOSCALE(c2n(mexitcount_overhead, p->profrate)));
253d6b9e17eSBruce Evans 	cputime_overhead -= empty_loop_time;
254d6b9e17eSBruce Evans 	mcount_overhead -= empty_loop_time;
255d6b9e17eSBruce Evans 	mexitcount_overhead -= empty_loop_time;
256d6b9e17eSBruce Evans 
2571a996ed1SEdward Tomasz Napierala 	/*-
258d6b9e17eSBruce Evans 	 * Profiling overheads are determined by the times between the
259d6b9e17eSBruce Evans 	 * following events:
260d6b9e17eSBruce Evans 	 *	MC1: mcount() is called
261d6b9e17eSBruce Evans 	 *	MC2: cputime() (called from mcount()) latches the timer
262d6b9e17eSBruce Evans 	 *	MC3: mcount() completes
263d6b9e17eSBruce Evans 	 *	ME1: mexitcount() is called
264d6b9e17eSBruce Evans 	 *	ME2: cputime() (called from mexitcount()) latches the timer
265d6b9e17eSBruce Evans 	 *	ME3: mexitcount() completes.
266d6b9e17eSBruce Evans 	 * The times between the events vary slightly depending on instruction
267d6b9e17eSBruce Evans 	 * combination and cache misses, etc.  Attempt to determine the
268d6b9e17eSBruce Evans 	 * minimum times.  These can be subtracted from the profiling times
269d6b9e17eSBruce Evans 	 * without much risk of reducing the profiling times below what they
270d6b9e17eSBruce Evans 	 * would be when profiling is not configured.  Abbreviate:
271d6b9e17eSBruce Evans 	 *	ab = minimum time between MC1 and MC3
272e3043798SPedro F. Giffuni 	 *	a  = minimum time between MC1 and MC2
273d6b9e17eSBruce Evans 	 *	b  = minimum time between MC2 and MC3
274d6b9e17eSBruce Evans 	 *	cd = minimum time between ME1 and ME3
275d6b9e17eSBruce Evans 	 *	c  = minimum time between ME1 and ME2
276d6b9e17eSBruce Evans 	 *	d  = minimum time between ME2 and ME3.
277d6b9e17eSBruce Evans 	 * These satisfy the relations:
278d6b9e17eSBruce Evans 	 *	ab            <= mcount_overhead		(just measured)
279d6b9e17eSBruce Evans 	 *	a + b         <= ab
280d6b9e17eSBruce Evans 	 *	        cd    <= mexitcount_overhead		(just measured)
281d6b9e17eSBruce Evans 	 *	        c + d <= cd
282d6b9e17eSBruce Evans 	 *	a         + d <= nullfunc_loop_profiled_time	(just measured)
283d6b9e17eSBruce Evans 	 *	a >= 0, b >= 0, c >= 0, d >= 0.
284d6b9e17eSBruce Evans 	 * Assume that ab and cd are equal to the minimums.
285d6b9e17eSBruce Evans 	 */
286d6b9e17eSBruce Evans 	p->cputime_overhead = CALIB_DOSCALE(cputime_overhead);
287d6b9e17eSBruce Evans 	p->mcount_overhead = CALIB_DOSCALE(mcount_overhead - cputime_overhead);
288d6b9e17eSBruce Evans 	p->mexitcount_overhead = CALIB_DOSCALE(mexitcount_overhead
289d6b9e17eSBruce Evans 					       - cputime_overhead);
290d6b9e17eSBruce Evans 	nullfunc_loop_overhead = nullfunc_loop_profiled_time - empty_loop_time;
291d6b9e17eSBruce Evans 	p->mexitcount_post_overhead = CALIB_DOSCALE((mcount_overhead
292d6b9e17eSBruce Evans 						     - nullfunc_loop_overhead)
293d6b9e17eSBruce Evans 						    / 4);
294d6b9e17eSBruce Evans 	p->mexitcount_pre_overhead = p->mexitcount_overhead
295d6b9e17eSBruce Evans 				     + p->cputime_overhead
296d6b9e17eSBruce Evans 				     - p->mexitcount_post_overhead;
297d6b9e17eSBruce Evans 	p->mcount_pre_overhead = CALIB_DOSCALE(nullfunc_loop_overhead)
298d6b9e17eSBruce Evans 				 - p->mexitcount_post_overhead;
299d6b9e17eSBruce Evans 	p->mcount_post_overhead = p->mcount_overhead
300d6b9e17eSBruce Evans 				  + p->cputime_overhead
301d6b9e17eSBruce Evans 				  - p->mcount_pre_overhead;
302d6b9e17eSBruce Evans 	printf(
303d6b9e17eSBruce Evans "Profiling overheads: mcount: %d+%d, %d+%d; mexitcount: %d+%d, %d+%d nsec\n",
304d6b9e17eSBruce Evans 	       c2n(p->cputime_overhead, p->profrate),
305d6b9e17eSBruce Evans 	       c2n(p->mcount_overhead, p->profrate),
306d6b9e17eSBruce Evans 	       c2n(p->mcount_pre_overhead, p->profrate),
307d6b9e17eSBruce Evans 	       c2n(p->mcount_post_overhead, p->profrate),
308d6b9e17eSBruce Evans 	       c2n(p->cputime_overhead, p->profrate),
309d6b9e17eSBruce Evans 	       c2n(p->mexitcount_overhead, p->profrate),
310d6b9e17eSBruce Evans 	       c2n(p->mexitcount_pre_overhead, p->profrate),
311d6b9e17eSBruce Evans 	       c2n(p->mexitcount_post_overhead, p->profrate));
312d6b9e17eSBruce Evans 	printf(
313d6b9e17eSBruce Evans "Profiling overheads: mcount: %d+%d, %d+%d; mexitcount: %d+%d, %d+%d cycles\n",
314d6b9e17eSBruce Evans 	       p->cputime_overhead, p->mcount_overhead,
315d6b9e17eSBruce Evans 	       p->mcount_pre_overhead, p->mcount_post_overhead,
316d6b9e17eSBruce Evans 	       p->cputime_overhead, p->mexitcount_overhead,
317d6b9e17eSBruce Evans 	       p->mexitcount_pre_overhead, p->mexitcount_post_overhead);
318912e6037SBruce Evans #endif /* GUPROF */
319df8bae1dSRodney W. Grimes }
320df8bae1dSRodney W. Grimes 
321df8bae1dSRodney W. Grimes /*
322df8bae1dSRodney W. Grimes  * Return kernel profiling information.
323df8bae1dSRodney W. Grimes  */
3244b2af45fSPoul-Henning Kamp static int
32582d9ae4eSPoul-Henning Kamp sysctl_kern_prof(SYSCTL_HANDLER_ARGS)
326df8bae1dSRodney W. Grimes {
3274b2af45fSPoul-Henning Kamp 	int *name = (int *) arg1;
3284b2af45fSPoul-Henning Kamp 	u_int namelen = arg2;
329df8bae1dSRodney W. Grimes 	struct gmonparam *gp = &_gmonparam;
330df8bae1dSRodney W. Grimes 	int error;
331912e6037SBruce Evans 	int state;
332df8bae1dSRodney W. Grimes 
333df8bae1dSRodney W. Grimes 	/* all sysctl names at this level are terminal */
334df8bae1dSRodney W. Grimes 	if (namelen != 1)
335df8bae1dSRodney W. Grimes 		return (ENOTDIR);		/* overloaded */
336df8bae1dSRodney W. Grimes 
337df8bae1dSRodney W. Grimes 	switch (name[0]) {
338df8bae1dSRodney W. Grimes 	case GPROF_STATE:
339912e6037SBruce Evans 		state = gp->state;
340912e6037SBruce Evans 		error = sysctl_handle_int(oidp, &state, 0, req);
341df8bae1dSRodney W. Grimes 		if (error)
342df8bae1dSRodney W. Grimes 			return (error);
343912e6037SBruce Evans 		if (!req->newptr)
344912e6037SBruce Evans 			return (0);
345912e6037SBruce Evans 		if (state == GMON_PROF_OFF) {
346d6b9e17eSBruce Evans 			gp->state = state;
347a282253aSJulian Elischer 			PROC_LOCK(&proc0);
348df8bae1dSRodney W. Grimes 			stopprofclock(&proc0);
349a282253aSJulian Elischer 			PROC_UNLOCK(&proc0);
350d6b9e17eSBruce Evans 			stopguprof(gp);
351912e6037SBruce Evans 		} else if (state == GMON_PROF_ON) {
352d6b9e17eSBruce Evans 			gp->state = GMON_PROF_OFF;
353d6b9e17eSBruce Evans 			stopguprof(gp);
354912e6037SBruce Evans 			gp->profrate = profhz;
3559752f794SJohn Baldwin 			PROC_LOCK(&proc0);
356df8bae1dSRodney W. Grimes 			startprofclock(&proc0);
3579752f794SJohn Baldwin 			PROC_UNLOCK(&proc0);
358d6b9e17eSBruce Evans 			gp->state = state;
359912e6037SBruce Evans #ifdef GUPROF
360912e6037SBruce Evans 		} else if (state == GMON_PROF_HIRES) {
361d6b9e17eSBruce Evans 			gp->state = GMON_PROF_OFF;
362a282253aSJulian Elischer 			PROC_LOCK(&proc0);
363912e6037SBruce Evans 			stopprofclock(&proc0);
364a282253aSJulian Elischer 			PROC_UNLOCK(&proc0);
365d6b9e17eSBruce Evans 			startguprof(gp);
366912e6037SBruce Evans 			gp->state = state;
367912e6037SBruce Evans #endif
368912e6037SBruce Evans 		} else if (state != gp->state)
369912e6037SBruce Evans 			return (EINVAL);
370df8bae1dSRodney W. Grimes 		return (0);
371df8bae1dSRodney W. Grimes 	case GPROF_COUNT:
3724b2af45fSPoul-Henning Kamp 		return (sysctl_handle_opaque(oidp,
3734b2af45fSPoul-Henning Kamp 			gp->kcount, gp->kcountsize, req));
374df8bae1dSRodney W. Grimes 	case GPROF_FROMS:
3754b2af45fSPoul-Henning Kamp 		return (sysctl_handle_opaque(oidp,
3764b2af45fSPoul-Henning Kamp 			gp->froms, gp->fromssize, req));
377df8bae1dSRodney W. Grimes 	case GPROF_TOS:
3784b2af45fSPoul-Henning Kamp 		return (sysctl_handle_opaque(oidp,
3794b2af45fSPoul-Henning Kamp 			gp->tos, gp->tossize, req));
380df8bae1dSRodney W. Grimes 	case GPROF_GMONPARAM:
3814b2af45fSPoul-Henning Kamp 		return (sysctl_handle_opaque(oidp, gp, sizeof *gp, req));
382df8bae1dSRodney W. Grimes 	default:
383df8bae1dSRodney W. Grimes 		return (EOPNOTSUPP);
384df8bae1dSRodney W. Grimes 	}
385df8bae1dSRodney W. Grimes 	/* NOTREACHED */
386df8bae1dSRodney W. Grimes }
3874b2af45fSPoul-Henning Kamp 
3886472ac3dSEd Schouten static SYSCTL_NODE(_kern, KERN_PROF, prof, CTLFLAG_RW, sysctl_kern_prof, "");
389df8bae1dSRodney W. Grimes #endif /* GPROF */
390df8bae1dSRodney W. Grimes 
391df8bae1dSRodney W. Grimes /*
392df8bae1dSRodney W. Grimes  * Profiling system call.
393df8bae1dSRodney W. Grimes  *
394df8bae1dSRodney W. Grimes  * The scale factor is a fixed point number with 16 bits of fraction, so that
395df8bae1dSRodney W. Grimes  * 1.0 is represented as 0x10000.  A scale factor of 0 turns off profiling.
396df8bae1dSRodney W. Grimes  */
397d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
398df8bae1dSRodney W. Grimes struct profil_args {
399df8bae1dSRodney W. Grimes 	caddr_t	samples;
400134e06feSBruce Evans 	size_t	size;
401134e06feSBruce Evans 	size_t	offset;
402df8bae1dSRodney W. Grimes 	u_int	scale;
403df8bae1dSRodney W. Grimes };
404d2d3e875SBruce Evans #endif
405df8bae1dSRodney W. Grimes /* ARGSUSED */
40626f9a767SRodney W. Grimes int
4078451d0ddSKip Macy sys_profil(struct thread *td, struct profil_args *uap)
408df8bae1dSRodney W. Grimes {
409a282253aSJulian Elischer 	struct uprof *upp;
4109752f794SJohn Baldwin 	struct proc *p;
411df8bae1dSRodney W. Grimes 
4129752f794SJohn Baldwin 	if (uap->scale > (1 << 16))
4139752f794SJohn Baldwin 		return (EINVAL);
4146f1e8c18SMatthew Dillon 
4159752f794SJohn Baldwin 	p = td->td_proc;
416df8bae1dSRodney W. Grimes 	if (uap->scale == 0) {
417a3a70178SJohn Baldwin 		PROC_LOCK(p);
418a3a70178SJohn Baldwin 		stopprofclock(p);
419a3a70178SJohn Baldwin 		PROC_UNLOCK(p);
4209752f794SJohn Baldwin 		return (0);
421df8bae1dSRodney W. Grimes 	}
422a3a70178SJohn Baldwin 	PROC_LOCK(p);
423b40ce416SJulian Elischer 	upp = &td->td_proc->p_stats->p_prof;
4245c7bebf9SKonstantin Belousov 	PROC_PROFLOCK(p);
425df8bae1dSRodney W. Grimes 	upp->pr_off = uap->offset;
426df8bae1dSRodney W. Grimes 	upp->pr_scale = uap->scale;
427df8bae1dSRodney W. Grimes 	upp->pr_base = uap->samples;
428df8bae1dSRodney W. Grimes 	upp->pr_size = uap->size;
4295c7bebf9SKonstantin Belousov 	PROC_PROFUNLOCK(p);
4309752f794SJohn Baldwin 	startprofclock(p);
4319752f794SJohn Baldwin 	PROC_UNLOCK(p);
432df8bae1dSRodney W. Grimes 
4339752f794SJohn Baldwin 	return (0);
434df8bae1dSRodney W. Grimes }
435df8bae1dSRodney W. Grimes 
436df8bae1dSRodney W. Grimes /*
437df8bae1dSRodney W. Grimes  * Scale is a fixed-point number with the binary point 16 bits
438df8bae1dSRodney W. Grimes  * into the value, and is <= 1.0.  pc is at most 32 bits, so the
439df8bae1dSRodney W. Grimes  * intermediate result is at most 48 bits.
440df8bae1dSRodney W. Grimes  */
441df8bae1dSRodney W. Grimes #define	PC_TO_INDEX(pc, prof) \
442df8bae1dSRodney W. Grimes 	((int)(((u_quad_t)((pc) - (prof)->pr_off) * \
443df8bae1dSRodney W. Grimes 	    (u_quad_t)((prof)->pr_scale)) >> 16) & ~1)
444df8bae1dSRodney W. Grimes 
445df8bae1dSRodney W. Grimes /*
446df8bae1dSRodney W. Grimes  * Collect user-level profiling statistics; called on a profiling tick,
447df8bae1dSRodney W. Grimes  * when a process is running in user-mode.  This routine may be called
448df8bae1dSRodney W. Grimes  * from an interrupt context.  We try to update the user profiling buffers
449df8bae1dSRodney W. Grimes  * cheaply with fuswintr() and suswintr().  If that fails, we revert to
450df8bae1dSRodney W. Grimes  * an AST that will vector us to trap() with a context in which copyin
451df8bae1dSRodney W. Grimes  * and copyout will work.  Trap will then call addupc_task().
452df8bae1dSRodney W. Grimes  *
453df8bae1dSRodney W. Grimes  * Note that we may (rarely) not get around to the AST soon enough, and
454df8bae1dSRodney W. Grimes  * lose profile ticks when the next tick overwrites this one, but in this
455df8bae1dSRodney W. Grimes  * case the system is overloaded and the profile is probably already
456df8bae1dSRodney W. Grimes  * inaccurate.
457df8bae1dSRodney W. Grimes  */
458df8bae1dSRodney W. Grimes void
459cb49fcd1SJohn Baldwin addupc_intr(struct thread *td, uintfptr_t pc, u_int ticks)
460df8bae1dSRodney W. Grimes {
461a282253aSJulian Elischer 	struct uprof *prof;
462a282253aSJulian Elischer 	caddr_t addr;
463a282253aSJulian Elischer 	u_int i;
464a282253aSJulian Elischer 	int v;
465df8bae1dSRodney W. Grimes 
466df8bae1dSRodney W. Grimes 	if (ticks == 0)
467df8bae1dSRodney W. Grimes 		return;
4684a338afdSJulian Elischer 	prof = &td->td_proc->p_stats->p_prof;
4695c7bebf9SKonstantin Belousov 	PROC_PROFLOCK(td->td_proc);
470df8bae1dSRodney W. Grimes 	if (pc < prof->pr_off ||
471a3a70178SJohn Baldwin 	    (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size) {
4725c7bebf9SKonstantin Belousov 		PROC_PROFUNLOCK(td->td_proc);
473df8bae1dSRodney W. Grimes 		return;			/* out of range; ignore */
474a3a70178SJohn Baldwin 	}
475df8bae1dSRodney W. Grimes 
476df8bae1dSRodney W. Grimes 	addr = prof->pr_base + i;
4775c7bebf9SKonstantin Belousov 	PROC_PROFUNLOCK(td->td_proc);
478df8bae1dSRodney W. Grimes 	if ((v = fuswintr(addr)) == -1 || suswintr(addr, v + ticks) == -1) {
47952eb8464SJohn Baldwin 		td->td_profil_addr = pc;
48052eb8464SJohn Baldwin 		td->td_profil_ticks = ticks;
48152eb8464SJohn Baldwin 		td->td_pflags |= TDP_OWEUPC;
482982d11f8SJeff Roberson 		thread_lock(td);
48352eb8464SJohn Baldwin 		td->td_flags |= TDF_ASTPENDING;
484982d11f8SJeff Roberson 		thread_unlock(td);
485df8bae1dSRodney W. Grimes 	}
486df8bae1dSRodney W. Grimes }
487df8bae1dSRodney W. Grimes 
488df8bae1dSRodney W. Grimes /*
489df8bae1dSRodney W. Grimes  * Much like before, but we can afford to take faults here.  If the
490df8bae1dSRodney W. Grimes  * update fails, we simply turn off profiling.
491df8bae1dSRodney W. Grimes  */
492037d027cSBruce Evans void
493cb49fcd1SJohn Baldwin addupc_task(struct thread *td, uintfptr_t pc, u_int ticks)
494df8bae1dSRodney W. Grimes {
4954a338afdSJulian Elischer 	struct proc *p = td->td_proc;
496a282253aSJulian Elischer 	struct uprof *prof;
497a282253aSJulian Elischer 	caddr_t addr;
498a282253aSJulian Elischer 	u_int i;
499df8bae1dSRodney W. Grimes 	u_short v;
500a282253aSJulian Elischer 	int stop = 0;
501df8bae1dSRodney W. Grimes 
50248fd1f38SJohn Baldwin 	if (ticks == 0)
503df8bae1dSRodney W. Grimes 		return;
504df8bae1dSRodney W. Grimes 
505a282253aSJulian Elischer 	PROC_LOCK(p);
5069752f794SJohn Baldwin 	if (!(p->p_flag & P_PROFIL)) {
507a282253aSJulian Elischer 		PROC_UNLOCK(p);
508a282253aSJulian Elischer 		return;
509a282253aSJulian Elischer 	}
510a282253aSJulian Elischer 	p->p_profthreads++;
511df8bae1dSRodney W. Grimes 	prof = &p->p_stats->p_prof;
5125c7bebf9SKonstantin Belousov 	PROC_PROFLOCK(p);
513df8bae1dSRodney W. Grimes 	if (pc < prof->pr_off ||
514a282253aSJulian Elischer 	    (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size) {
5155c7bebf9SKonstantin Belousov 		PROC_PROFUNLOCK(p);
516a282253aSJulian Elischer 		goto out;
517a282253aSJulian Elischer 	}
518df8bae1dSRodney W. Grimes 
519df8bae1dSRodney W. Grimes 	addr = prof->pr_base + i;
5205c7bebf9SKonstantin Belousov 	PROC_PROFUNLOCK(p);
521a3a70178SJohn Baldwin 	PROC_UNLOCK(p);
52201609114SAlfred Perlstein 	if (copyin(addr, &v, sizeof(v)) == 0) {
523df8bae1dSRodney W. Grimes 		v += ticks;
524a3a70178SJohn Baldwin 		if (copyout(&v, addr, sizeof(v)) == 0) {
525a3a70178SJohn Baldwin 			PROC_LOCK(p);
526a282253aSJulian Elischer 			goto out;
527df8bae1dSRodney W. Grimes 		}
528a3a70178SJohn Baldwin 	}
529a282253aSJulian Elischer 	stop = 1;
530a3a70178SJohn Baldwin 	PROC_LOCK(p);
531a282253aSJulian Elischer 
532a282253aSJulian Elischer out:
533a282253aSJulian Elischer 	if (--p->p_profthreads == 0) {
5349752f794SJohn Baldwin 		if (p->p_flag & P_STOPPROF) {
535a282253aSJulian Elischer 			wakeup(&p->p_profthreads);
5360436fcb8SKonstantin Belousov 			p->p_flag &= ~P_STOPPROF;
537a282253aSJulian Elischer 			stop = 0;
538a282253aSJulian Elischer 		}
539a282253aSJulian Elischer 	}
540a282253aSJulian Elischer 	if (stop)
541df8bae1dSRodney W. Grimes 		stopprofclock(p);
542a282253aSJulian Elischer 	PROC_UNLOCK(p);
543df8bae1dSRodney W. Grimes }
544