subr_prof.c revision 83366
1/*-
2 * Copyright (c) 1982, 1986, 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. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 *	@(#)subr_prof.c	8.3 (Berkeley) 9/23/93
34 * $FreeBSD: head/sys/kern/subr_prof.c 83366 2001-09-12 08:38:13Z julian $
35 */
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/sysproto.h>
40#include <sys/kernel.h>
41#include <sys/lock.h>
42#include <sys/mutex.h>
43#include <sys/proc.h>
44#include <sys/resourcevar.h>
45#include <sys/sysctl.h>
46
47#include <machine/cpu.h>
48
49#ifdef GPROF
50#include <sys/malloc.h>
51#include <sys/gmon.h>
52#undef MCOUNT
53
54static MALLOC_DEFINE(M_GPROF, "gprof", "kernel profiling buffer");
55
56static void kmstartup __P((void *));
57SYSINIT(kmem, SI_SUB_KPROF, SI_ORDER_FIRST, kmstartup, NULL)
58
59struct gmonparam _gmonparam = { GMON_PROF_OFF };
60
61#ifdef GUPROF
62#include <machine/asmacros.h>
63
64void
65nullfunc_loop_profiled()
66{
67	int i;
68
69	for (i = 0; i < CALIB_SCALE; i++)
70		nullfunc_profiled();
71}
72
73#define	nullfunc_loop_profiled_end	nullfunc_profiled	/* XXX */
74
75void
76nullfunc_profiled()
77{
78}
79#endif /* GUPROF */
80
81static void
82kmstartup(dummy)
83	void *dummy;
84{
85	char *cp;
86	struct gmonparam *p = &_gmonparam;
87#ifdef GUPROF
88	int cputime_overhead;
89	int empty_loop_time;
90	int i;
91	int mcount_overhead;
92	int mexitcount_overhead;
93	int nullfunc_loop_overhead;
94	int nullfunc_loop_profiled_time;
95	uintfptr_t tmp_addr;
96	critical_t savecrit;
97#endif
98
99	/*
100	 * Round lowpc and highpc to multiples of the density we're using
101	 * so the rest of the scaling (here and in gprof) stays in ints.
102	 */
103	p->lowpc = ROUNDDOWN((u_long)btext, HISTFRACTION * sizeof(HISTCOUNTER));
104	p->highpc = ROUNDUP((u_long)etext, HISTFRACTION * sizeof(HISTCOUNTER));
105	p->textsize = p->highpc - p->lowpc;
106	printf("Profiling kernel, textsize=%lu [%x..%x]\n",
107	       p->textsize, p->lowpc, p->highpc);
108	p->kcountsize = p->textsize / HISTFRACTION;
109	p->hashfraction = HASHFRACTION;
110	p->fromssize = p->textsize / HASHFRACTION;
111	p->tolimit = p->textsize * ARCDENSITY / 100;
112	if (p->tolimit < MINARCS)
113		p->tolimit = MINARCS;
114	else if (p->tolimit > MAXARCS)
115		p->tolimit = MAXARCS;
116	p->tossize = p->tolimit * sizeof(struct tostruct);
117	cp = (char *)malloc(p->kcountsize + p->fromssize + p->tossize,
118	    M_GPROF, M_NOWAIT | M_ZERO);
119	if (cp == 0) {
120		printf("No memory for profiling.\n");
121		return;
122	}
123	p->tos = (struct tostruct *)cp;
124	cp += p->tossize;
125	p->kcount = (HISTCOUNTER *)cp;
126	cp += p->kcountsize;
127	p->froms = (u_short *)cp;
128
129#ifdef GUPROF
130	/* Initialize pointers to overhead counters. */
131	p->cputime_count = &KCOUNT(p, PC_TO_I(p, cputime));
132	p->mcount_count = &KCOUNT(p, PC_TO_I(p, mcount));
133	p->mexitcount_count = &KCOUNT(p, PC_TO_I(p, mexitcount));
134
135	/*
136	 * Disable interrupts to avoid interference while we calibrate
137	 * things.
138	 */
139	savecrit = critical_enter();
140
141	/*
142	 * Determine overheads.
143	 * XXX this needs to be repeated for each useful timer/counter.
144	 */
145	cputime_overhead = 0;
146	startguprof(p);
147	for (i = 0; i < CALIB_SCALE; i++)
148		cputime_overhead += cputime();
149
150	empty_loop();
151	startguprof(p);
152	empty_loop();
153	empty_loop_time = cputime();
154
155	nullfunc_loop_profiled();
156
157	/*
158	 * Start profiling.  There won't be any normal function calls since
159	 * interrupts are disabled, but we will call the profiling routines
160	 * directly to determine their overheads.
161	 */
162	p->state = GMON_PROF_HIRES;
163
164	startguprof(p);
165	nullfunc_loop_profiled();
166
167	startguprof(p);
168	for (i = 0; i < CALIB_SCALE; i++)
169#if defined(__i386__) && __GNUC__ >= 2
170		__asm("pushl %0; call __mcount; popl %%ecx"
171		      :
172		      : "i" (profil)
173		      : "ax", "bx", "cx", "dx", "memory");
174#else
175#error
176#endif
177	mcount_overhead = KCOUNT(p, PC_TO_I(p, profil));
178
179	startguprof(p);
180	for (i = 0; i < CALIB_SCALE; i++)
181#if defined(__i386__) && __GNUC__ >= 2
182		    __asm("call " __XSTRING(HIDENAME(mexitcount)) "; 1:"
183			  : : : "ax", "bx", "cx", "dx", "memory");
184	__asm("movl $1b,%0" : "=rm" (tmp_addr));
185#else
186#error
187#endif
188	mexitcount_overhead = KCOUNT(p, PC_TO_I(p, tmp_addr));
189
190	p->state = GMON_PROF_OFF;
191	stopguprof(p);
192
193	critical_exit(savecrit);
194
195	nullfunc_loop_profiled_time = 0;
196	for (tmp_addr = (uintfptr_t)nullfunc_loop_profiled;
197	     tmp_addr < (uintfptr_t)nullfunc_loop_profiled_end;
198	     tmp_addr += HISTFRACTION * sizeof(HISTCOUNTER))
199		nullfunc_loop_profiled_time += KCOUNT(p, PC_TO_I(p, tmp_addr));
200#define CALIB_DOSCALE(count)	(((count) + CALIB_SCALE / 3) / CALIB_SCALE)
201#define	c2n(count, freq)	((int)((count) * 1000000000LL / freq))
202	printf("cputime %d, empty_loop %d, nullfunc_loop_profiled %d, mcount %d, mexitcount %d\n",
203	       CALIB_DOSCALE(c2n(cputime_overhead, p->profrate)),
204	       CALIB_DOSCALE(c2n(empty_loop_time, p->profrate)),
205	       CALIB_DOSCALE(c2n(nullfunc_loop_profiled_time, p->profrate)),
206	       CALIB_DOSCALE(c2n(mcount_overhead, p->profrate)),
207	       CALIB_DOSCALE(c2n(mexitcount_overhead, p->profrate)));
208	cputime_overhead -= empty_loop_time;
209	mcount_overhead -= empty_loop_time;
210	mexitcount_overhead -= empty_loop_time;
211
212	/*-
213	 * Profiling overheads are determined by the times between the
214	 * following events:
215	 *	MC1: mcount() is called
216	 *	MC2: cputime() (called from mcount()) latches the timer
217	 *	MC3: mcount() completes
218	 *	ME1: mexitcount() is called
219	 *	ME2: cputime() (called from mexitcount()) latches the timer
220	 *	ME3: mexitcount() completes.
221	 * The times between the events vary slightly depending on instruction
222	 * combination and cache misses, etc.  Attempt to determine the
223	 * minimum times.  These can be subtracted from the profiling times
224	 * without much risk of reducing the profiling times below what they
225	 * would be when profiling is not configured.  Abbreviate:
226	 *	ab = minimum time between MC1 and MC3
227	 *	a  = minumum time between MC1 and MC2
228	 *	b  = minimum time between MC2 and MC3
229	 *	cd = minimum time between ME1 and ME3
230	 *	c  = minimum time between ME1 and ME2
231	 *	d  = minimum time between ME2 and ME3.
232	 * These satisfy the relations:
233	 *	ab            <= mcount_overhead		(just measured)
234	 *	a + b         <= ab
235	 *	        cd    <= mexitcount_overhead		(just measured)
236	 *	        c + d <= cd
237	 *	a         + d <= nullfunc_loop_profiled_time	(just measured)
238	 *	a >= 0, b >= 0, c >= 0, d >= 0.
239	 * Assume that ab and cd are equal to the minimums.
240	 */
241	p->cputime_overhead = CALIB_DOSCALE(cputime_overhead);
242	p->mcount_overhead = CALIB_DOSCALE(mcount_overhead - cputime_overhead);
243	p->mexitcount_overhead = CALIB_DOSCALE(mexitcount_overhead
244					       - cputime_overhead);
245	nullfunc_loop_overhead = nullfunc_loop_profiled_time - empty_loop_time;
246	p->mexitcount_post_overhead = CALIB_DOSCALE((mcount_overhead
247						     - nullfunc_loop_overhead)
248						    / 4);
249	p->mexitcount_pre_overhead = p->mexitcount_overhead
250				     + p->cputime_overhead
251				     - p->mexitcount_post_overhead;
252	p->mcount_pre_overhead = CALIB_DOSCALE(nullfunc_loop_overhead)
253				 - p->mexitcount_post_overhead;
254	p->mcount_post_overhead = p->mcount_overhead
255				  + p->cputime_overhead
256				  - p->mcount_pre_overhead;
257	printf(
258"Profiling overheads: mcount: %d+%d, %d+%d; mexitcount: %d+%d, %d+%d nsec\n",
259	       c2n(p->cputime_overhead, p->profrate),
260	       c2n(p->mcount_overhead, p->profrate),
261	       c2n(p->mcount_pre_overhead, p->profrate),
262	       c2n(p->mcount_post_overhead, p->profrate),
263	       c2n(p->cputime_overhead, p->profrate),
264	       c2n(p->mexitcount_overhead, p->profrate),
265	       c2n(p->mexitcount_pre_overhead, p->profrate),
266	       c2n(p->mexitcount_post_overhead, p->profrate));
267	printf(
268"Profiling overheads: mcount: %d+%d, %d+%d; mexitcount: %d+%d, %d+%d cycles\n",
269	       p->cputime_overhead, p->mcount_overhead,
270	       p->mcount_pre_overhead, p->mcount_post_overhead,
271	       p->cputime_overhead, p->mexitcount_overhead,
272	       p->mexitcount_pre_overhead, p->mexitcount_post_overhead);
273#endif /* GUPROF */
274}
275
276/*
277 * Return kernel profiling information.
278 */
279static int
280sysctl_kern_prof(SYSCTL_HANDLER_ARGS)
281{
282	int *name = (int *) arg1;
283	u_int namelen = arg2;
284	struct gmonparam *gp = &_gmonparam;
285	int error;
286	int state;
287
288	/* all sysctl names at this level are terminal */
289	if (namelen != 1)
290		return (ENOTDIR);		/* overloaded */
291
292	switch (name[0]) {
293	case GPROF_STATE:
294		state = gp->state;
295		error = sysctl_handle_int(oidp, &state, 0, req);
296		if (error)
297			return (error);
298		if (!req->newptr)
299			return (0);
300		if (state == GMON_PROF_OFF) {
301			gp->state = state;
302			stopprofclock(&proc0);
303			stopguprof(gp);
304		} else if (state == GMON_PROF_ON) {
305			gp->state = GMON_PROF_OFF;
306			stopguprof(gp);
307			gp->profrate = profhz;
308			startprofclock(&proc0);
309			gp->state = state;
310#ifdef GUPROF
311		} else if (state == GMON_PROF_HIRES) {
312			gp->state = GMON_PROF_OFF;
313			stopprofclock(&proc0);
314			startguprof(gp);
315			gp->state = state;
316#endif
317		} else if (state != gp->state)
318			return (EINVAL);
319		return (0);
320	case GPROF_COUNT:
321		return (sysctl_handle_opaque(oidp,
322			gp->kcount, gp->kcountsize, req));
323	case GPROF_FROMS:
324		return (sysctl_handle_opaque(oidp,
325			gp->froms, gp->fromssize, req));
326	case GPROF_TOS:
327		return (sysctl_handle_opaque(oidp,
328			gp->tos, gp->tossize, req));
329	case GPROF_GMONPARAM:
330		return (sysctl_handle_opaque(oidp, gp, sizeof *gp, req));
331	default:
332		return (EOPNOTSUPP);
333	}
334	/* NOTREACHED */
335}
336
337SYSCTL_NODE(_kern, KERN_PROF, prof, CTLFLAG_RW, sysctl_kern_prof, "");
338#endif /* GPROF */
339
340/*
341 * Profiling system call.
342 *
343 * The scale factor is a fixed point number with 16 bits of fraction, so that
344 * 1.0 is represented as 0x10000.  A scale factor of 0 turns off profiling.
345 */
346#ifndef _SYS_SYSPROTO_H_
347struct profil_args {
348	caddr_t	samples;
349	size_t	size;
350	size_t	offset;
351	u_int	scale;
352};
353#endif
354/*
355 * MPSAFE
356 */
357/* ARGSUSED */
358int
359profil(td, uap)
360	struct thread *td;
361	register struct profil_args *uap;
362{
363	register struct uprof *upp;
364	int s;
365	int error = 0;
366
367	mtx_lock(&Giant);
368
369	if (uap->scale > (1 << 16)) {
370		error = EINVAL;
371		goto done2;
372	}
373	if (uap->scale == 0) {
374		stopprofclock(td->td_proc);
375		goto done2;
376	}
377	upp = &td->td_proc->p_stats->p_prof;
378
379	/* Block profile interrupts while changing state. */
380	s = splstatclock();
381	upp->pr_off = uap->offset;
382	upp->pr_scale = uap->scale;
383	upp->pr_base = uap->samples;
384	upp->pr_size = uap->size;
385	startprofclock(td->td_proc);
386	splx(s);
387
388done2:
389	mtx_unlock(&Giant);
390	return (error);
391}
392
393/*
394 * Scale is a fixed-point number with the binary point 16 bits
395 * into the value, and is <= 1.0.  pc is at most 32 bits, so the
396 * intermediate result is at most 48 bits.
397 */
398#define	PC_TO_INDEX(pc, prof) \
399	((int)(((u_quad_t)((pc) - (prof)->pr_off) * \
400	    (u_quad_t)((prof)->pr_scale)) >> 16) & ~1)
401
402/*
403 * Collect user-level profiling statistics; called on a profiling tick,
404 * when a process is running in user-mode.  This routine may be called
405 * from an interrupt context.  We try to update the user profiling buffers
406 * cheaply with fuswintr() and suswintr().  If that fails, we revert to
407 * an AST that will vector us to trap() with a context in which copyin
408 * and copyout will work.  Trap will then call addupc_task().
409 *
410 * Note that we may (rarely) not get around to the AST soon enough, and
411 * lose profile ticks when the next tick overwrites this one, but in this
412 * case the system is overloaded and the profile is probably already
413 * inaccurate.
414 */
415void
416addupc_intr(ke, pc, ticks)
417	register struct kse *ke;
418	register uintptr_t pc;
419	u_int ticks;
420{
421	register struct uprof *prof;
422	register caddr_t addr;
423	register u_int i;
424	register int v;
425
426	if (ticks == 0)
427		return;
428	prof = &ke->ke_proc->p_stats->p_prof;
429	if (pc < prof->pr_off ||
430	    (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
431		return;			/* out of range; ignore */
432
433	addr = prof->pr_base + i;
434	if ((v = fuswintr(addr)) == -1 || suswintr(addr, v + ticks) == -1) {
435		mtx_lock_spin(&sched_lock);
436		prof->pr_addr = pc;
437		prof->pr_ticks = ticks;
438		ke->ke_flags |= KEF_OWEUPC | KEF_ASTPENDING ;
439		mtx_unlock_spin(&sched_lock);
440	}
441}
442
443/*
444 * Much like before, but we can afford to take faults here.  If the
445 * update fails, we simply turn off profiling.
446 */
447void
448addupc_task(ke, pc, ticks)
449	register struct kse *ke;
450	register uintptr_t pc;
451	u_int ticks;
452{
453	struct proc *p = ke->ke_proc;
454	register struct uprof *prof;
455	register caddr_t addr;
456	register u_int i;
457	u_short v;
458
459	/* Testing PS_PROFIL may be unnecessary, but is certainly safe. */
460	if ((p->p_sflag & PS_PROFIL) == 0 || ticks == 0)
461		return;
462
463	prof = &p->p_stats->p_prof;
464	if (pc < prof->pr_off ||
465	    (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
466		return;
467
468	addr = prof->pr_base + i;
469	if (copyin(addr, (caddr_t)&v, sizeof(v)) == 0) {
470		v += ticks;
471		if (copyout((caddr_t)&v, addr, sizeof(v)) == 0)
472			return;
473	}
474	stopprofclock(p);
475}
476