Home | History | Annotate | Line # | Download | only in kern
subr_prof.c revision 1.27.2.3
      1  1.27.2.3     skrll /*	$NetBSD: subr_prof.c,v 1.27.2.3 2004/09/21 13:35:12 skrll Exp $	*/
      2       1.3       cgd 
      3       1.1       cgd /*-
      4       1.1       cgd  * Copyright (c) 1982, 1986, 1993
      5       1.1       cgd  *	The Regents of the University of California.  All rights reserved.
      6       1.1       cgd  *
      7       1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8       1.1       cgd  * modification, are permitted provided that the following conditions
      9       1.1       cgd  * are met:
     10       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.27.2.1     skrll  * 3. Neither the name of the University nor the names of its contributors
     16       1.1       cgd  *    may be used to endorse or promote products derived from this software
     17       1.1       cgd  *    without specific prior written permission.
     18       1.1       cgd  *
     19       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29       1.1       cgd  * SUCH DAMAGE.
     30       1.1       cgd  *
     31      1.17      fvdl  *	@(#)subr_prof.c	8.4 (Berkeley) 2/14/95
     32       1.1       cgd  */
     33      1.25     lukem 
     34      1.25     lukem #include <sys/cdefs.h>
     35  1.27.2.3     skrll __KERNEL_RCSID(0, "$NetBSD: subr_prof.c,v 1.27.2.3 2004/09/21 13:35:12 skrll Exp $");
     36       1.1       cgd 
     37       1.1       cgd #include <sys/param.h>
     38       1.1       cgd #include <sys/systm.h>
     39       1.1       cgd #include <sys/kernel.h>
     40       1.1       cgd #include <sys/proc.h>
     41       1.1       cgd #include <sys/user.h>
     42       1.4       cgd #include <sys/mount.h>
     43      1.26   thorpej #include <sys/sa.h>
     44       1.4       cgd #include <sys/syscallargs.h>
     45      1.16  jonathan #include <sys/sysctl.h>
     46       1.4       cgd 
     47       1.1       cgd #include <machine/cpu.h>
     48       1.9  christos 
     49       1.1       cgd #ifdef GPROF
     50       1.1       cgd #include <sys/malloc.h>
     51       1.1       cgd #include <sys/gmon.h>
     52      1.27   thorpej 
     53      1.27   thorpej MALLOC_DEFINE(M_GPROF, "gprof", "kernel profiling buffer");
     54       1.1       cgd 
     55       1.1       cgd /*
     56       1.1       cgd  * Froms is actually a bunch of unsigned shorts indexing tos
     57       1.1       cgd  */
     58       1.1       cgd struct gmonparam _gmonparam = { GMON_PROF_OFF };
     59       1.1       cgd 
     60      1.15       gwr /* Actual start of the kernel text segment. */
     61      1.15       gwr extern char kernel_text[];
     62      1.15       gwr 
     63       1.1       cgd extern char etext[];
     64       1.1       cgd 
     65       1.9  christos 
     66       1.9  christos void
     67       1.1       cgd kmstartup()
     68       1.1       cgd {
     69       1.1       cgd 	char *cp;
     70       1.1       cgd 	struct gmonparam *p = &_gmonparam;
     71       1.1       cgd 	/*
     72       1.1       cgd 	 * Round lowpc and highpc to multiples of the density we're using
     73       1.1       cgd 	 * so the rest of the scaling (here and in gprof) stays in ints.
     74       1.1       cgd 	 */
     75      1.15       gwr 	p->lowpc = ROUNDDOWN(((u_long)kernel_text),
     76      1.15       gwr 		HISTFRACTION * sizeof(HISTCOUNTER));
     77      1.15       gwr 	p->highpc = ROUNDUP((u_long)etext,
     78      1.15       gwr 		HISTFRACTION * sizeof(HISTCOUNTER));
     79       1.1       cgd 	p->textsize = p->highpc - p->lowpc;
     80      1.14  christos 	printf("Profiling kernel, textsize=%ld [%lx..%lx]\n",
     81       1.1       cgd 	       p->textsize, p->lowpc, p->highpc);
     82       1.1       cgd 	p->kcountsize = p->textsize / HISTFRACTION;
     83       1.1       cgd 	p->hashfraction = HASHFRACTION;
     84       1.1       cgd 	p->fromssize = p->textsize / HASHFRACTION;
     85       1.1       cgd 	p->tolimit = p->textsize * ARCDENSITY / 100;
     86       1.1       cgd 	if (p->tolimit < MINARCS)
     87       1.1       cgd 		p->tolimit = MINARCS;
     88       1.1       cgd 	else if (p->tolimit > MAXARCS)
     89       1.1       cgd 		p->tolimit = MAXARCS;
     90       1.1       cgd 	p->tossize = p->tolimit * sizeof(struct tostruct);
     91       1.1       cgd 	cp = (char *)malloc(p->kcountsize + p->fromssize + p->tossize,
     92       1.1       cgd 	    M_GPROF, M_NOWAIT);
     93       1.1       cgd 	if (cp == 0) {
     94      1.14  christos 		printf("No memory for profiling.\n");
     95       1.1       cgd 		return;
     96       1.1       cgd 	}
     97      1.19     perry 	memset(cp, 0, p->kcountsize + p->tossize + p->fromssize);
     98       1.1       cgd 	p->tos = (struct tostruct *)cp;
     99       1.1       cgd 	cp += p->tossize;
    100       1.1       cgd 	p->kcount = (u_short *)cp;
    101       1.1       cgd 	cp += p->kcountsize;
    102       1.1       cgd 	p->froms = (u_short *)cp;
    103       1.1       cgd }
    104       1.1       cgd 
    105       1.1       cgd /*
    106       1.1       cgd  * Return kernel profiling information.
    107       1.1       cgd  */
    108  1.27.2.1     skrll /*
    109  1.27.2.1     skrll  * sysctl helper routine for kern.profiling subtree.  enables/disables
    110  1.27.2.1     skrll  * kernel profiling and gives out copies of the profiling data.
    111  1.27.2.1     skrll  */
    112  1.27.2.1     skrll static int
    113  1.27.2.1     skrll sysctl_kern_profiling(SYSCTLFN_ARGS)
    114       1.1       cgd {
    115       1.1       cgd 	struct gmonparam *gp = &_gmonparam;
    116       1.1       cgd 	int error;
    117  1.27.2.1     skrll 	struct sysctlnode node;
    118       1.1       cgd 
    119  1.27.2.1     skrll 	node = *rnode;
    120      1.23     bjh21 
    121  1.27.2.1     skrll 	switch (node.sysctl_num) {
    122       1.1       cgd 	case GPROF_STATE:
    123  1.27.2.1     skrll 		node.sysctl_data = &gp->state;
    124  1.27.2.1     skrll 		break;
    125       1.1       cgd 	case GPROF_COUNT:
    126  1.27.2.1     skrll 		node.sysctl_data = gp->kcount;
    127  1.27.2.1     skrll 		node.sysctl_size = gp->kcountsize;
    128  1.27.2.1     skrll 		break;
    129       1.1       cgd 	case GPROF_FROMS:
    130  1.27.2.1     skrll 		node.sysctl_data = gp->froms;
    131  1.27.2.1     skrll 		node.sysctl_size = gp->fromssize;
    132  1.27.2.1     skrll 		break;
    133       1.1       cgd 	case GPROF_TOS:
    134  1.27.2.1     skrll 		node.sysctl_data = gp->tos;
    135  1.27.2.1     skrll 		node.sysctl_size = gp->tossize;
    136  1.27.2.1     skrll 		break;
    137       1.1       cgd 	case GPROF_GMONPARAM:
    138  1.27.2.1     skrll 		node.sysctl_data = gp;
    139  1.27.2.1     skrll 		node.sysctl_size = sizeof(*gp);
    140  1.27.2.1     skrll 		break;
    141       1.1       cgd 	default:
    142       1.1       cgd 		return (EOPNOTSUPP);
    143       1.1       cgd 	}
    144  1.27.2.1     skrll 
    145  1.27.2.1     skrll 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    146  1.27.2.1     skrll 	if (error || newp == NULL)
    147  1.27.2.1     skrll 		return (error);
    148  1.27.2.1     skrll 
    149  1.27.2.1     skrll 	if (node.sysctl_num == GPROF_STATE) {
    150  1.27.2.1     skrll 		if (gp->state == GMON_PROF_OFF)
    151  1.27.2.1     skrll 			stopprofclock(&proc0);
    152  1.27.2.1     skrll 		else
    153  1.27.2.1     skrll 			startprofclock(&proc0);
    154  1.27.2.1     skrll 	}
    155  1.27.2.1     skrll 
    156  1.27.2.1     skrll 	return (0);
    157  1.27.2.1     skrll }
    158  1.27.2.1     skrll 
    159  1.27.2.1     skrll SYSCTL_SETUP(sysctl_kern_gprof_setup, "sysctl kern.profiling subtree setup")
    160  1.27.2.1     skrll {
    161  1.27.2.1     skrll 
    162  1.27.2.1     skrll 	sysctl_createv(clog, 0, NULL, NULL,
    163  1.27.2.1     skrll 		       CTLFLAG_PERMANENT,
    164  1.27.2.1     skrll 		       CTLTYPE_NODE, "kern", NULL,
    165  1.27.2.1     skrll 		       NULL, 0, NULL, 0,
    166  1.27.2.1     skrll 		       CTL_KERN, CTL_EOL);
    167  1.27.2.1     skrll 	sysctl_createv(clog, 0, NULL, NULL,
    168  1.27.2.1     skrll 		       CTLFLAG_PERMANENT,
    169  1.27.2.1     skrll 		       CTLTYPE_NODE, "profiling",
    170  1.27.2.1     skrll 		       SYSCTL_DESCR("Profiling information (available)"),
    171  1.27.2.1     skrll 		       NULL, 0, NULL, 0,
    172  1.27.2.1     skrll 		       CTL_KERN, KERN_PROF, CTL_EOL);
    173  1.27.2.1     skrll 
    174  1.27.2.1     skrll 	sysctl_createv(clog, 0, NULL, NULL,
    175  1.27.2.1     skrll 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    176  1.27.2.1     skrll 		       CTLTYPE_INT, "state",
    177  1.27.2.1     skrll 		       SYSCTL_DESCR("Profiling state"),
    178  1.27.2.1     skrll 		       sysctl_kern_profiling, 0, NULL, 0,
    179  1.27.2.1     skrll 		       CTL_KERN, KERN_PROF, GPROF_STATE, CTL_EOL);
    180  1.27.2.1     skrll 	sysctl_createv(clog, 0, NULL, NULL,
    181  1.27.2.1     skrll 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    182  1.27.2.1     skrll 		       CTLTYPE_STRUCT, "count",
    183  1.27.2.1     skrll 		       SYSCTL_DESCR("Array of statistical program counters"),
    184  1.27.2.1     skrll 		       sysctl_kern_profiling, 0, NULL, 0,
    185  1.27.2.1     skrll 		       CTL_KERN, KERN_PROF, GPROF_COUNT, CTL_EOL);
    186  1.27.2.1     skrll 	sysctl_createv(clog, 0, NULL, NULL,
    187  1.27.2.1     skrll 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    188  1.27.2.1     skrll 		       CTLTYPE_STRUCT, "froms",
    189  1.27.2.1     skrll 		       SYSCTL_DESCR("Array indexed by program counter of "
    190  1.27.2.1     skrll 				    "call-from points"),
    191  1.27.2.1     skrll 		       sysctl_kern_profiling, 0, NULL, 0,
    192  1.27.2.1     skrll 		       CTL_KERN, KERN_PROF, GPROF_FROMS, CTL_EOL);
    193  1.27.2.1     skrll 	sysctl_createv(clog, 0, NULL, NULL,
    194  1.27.2.1     skrll 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    195  1.27.2.1     skrll 		       CTLTYPE_STRUCT, "tos",
    196  1.27.2.1     skrll 		       SYSCTL_DESCR("Array of structures describing "
    197  1.27.2.1     skrll 				    "destination of calls and their counts"),
    198  1.27.2.1     skrll 		       sysctl_kern_profiling, 0, NULL, 0,
    199  1.27.2.1     skrll 		       CTL_KERN, KERN_PROF, GPROF_TOS, CTL_EOL);
    200  1.27.2.1     skrll 	sysctl_createv(clog, 0, NULL, NULL,
    201  1.27.2.1     skrll 		       CTLFLAG_PERMANENT,
    202  1.27.2.1     skrll 		       CTLTYPE_STRUCT, "gmonparam",
    203  1.27.2.1     skrll 		       SYSCTL_DESCR("Structure giving the sizes of the above "
    204  1.27.2.1     skrll 				    "arrays"),
    205  1.27.2.1     skrll 		       sysctl_kern_profiling, 0, NULL, 0,
    206  1.27.2.1     skrll 		       CTL_KERN, KERN_PROF, GPROF_GMONPARAM, CTL_EOL);
    207       1.1       cgd }
    208       1.1       cgd #endif /* GPROF */
    209       1.1       cgd 
    210       1.1       cgd /*
    211       1.1       cgd  * Profiling system call.
    212       1.1       cgd  *
    213       1.1       cgd  * The scale factor is a fixed point number with 16 bits of fraction, so that
    214       1.1       cgd  * 1.0 is represented as 0x10000.  A scale factor of 0 turns off profiling.
    215       1.1       cgd  */
    216       1.1       cgd /* ARGSUSED */
    217       1.9  christos int
    218      1.26   thorpej sys_profil(l, v, retval)
    219      1.26   thorpej 	struct lwp *l;
    220       1.6   thorpej 	void *v;
    221       1.6   thorpej 	register_t *retval;
    222       1.6   thorpej {
    223      1.20  augustss 	struct sys_profil_args /* {
    224       1.4       cgd 		syscallarg(caddr_t) samples;
    225       1.9  christos 		syscallarg(u_int) size;
    226       1.9  christos 		syscallarg(u_int) offset;
    227       1.4       cgd 		syscallarg(u_int) scale;
    228       1.6   thorpej 	} */ *uap = v;
    229      1.26   thorpej 	struct proc *p = l->l_proc;
    230      1.20  augustss 	struct uprof *upp;
    231       1.1       cgd 	int s;
    232       1.1       cgd 
    233       1.4       cgd 	if (SCARG(uap, scale) > (1 << 16))
    234       1.1       cgd 		return (EINVAL);
    235       1.4       cgd 	if (SCARG(uap, scale) == 0) {
    236       1.1       cgd 		stopprofclock(p);
    237       1.1       cgd 		return (0);
    238       1.1       cgd 	}
    239       1.1       cgd 	upp = &p->p_stats->p_prof;
    240       1.1       cgd 
    241       1.1       cgd 	/* Block profile interrupts while changing state. */
    242       1.1       cgd 	s = splstatclock();
    243       1.4       cgd 	upp->pr_off = SCARG(uap, offset);
    244       1.4       cgd 	upp->pr_scale = SCARG(uap, scale);
    245       1.4       cgd 	upp->pr_base = SCARG(uap, samples);
    246       1.4       cgd 	upp->pr_size = SCARG(uap, size);
    247       1.1       cgd 	startprofclock(p);
    248       1.1       cgd 	splx(s);
    249       1.1       cgd 
    250       1.1       cgd 	return (0);
    251       1.1       cgd }
    252       1.1       cgd 
    253       1.1       cgd /*
    254       1.1       cgd  * Scale is a fixed-point number with the binary point 16 bits
    255       1.1       cgd  * into the value, and is <= 1.0.  pc is at most 32 bits, so the
    256       1.1       cgd  * intermediate result is at most 48 bits.
    257       1.1       cgd  */
    258       1.1       cgd #define	PC_TO_INDEX(pc, prof) \
    259       1.1       cgd 	((int)(((u_quad_t)((pc) - (prof)->pr_off) * \
    260       1.1       cgd 	    (u_quad_t)((prof)->pr_scale)) >> 16) & ~1)
    261       1.1       cgd 
    262       1.1       cgd /*
    263       1.1       cgd  * Collect user-level profiling statistics; called on a profiling tick,
    264       1.1       cgd  * when a process is running in user-mode.  This routine may be called
    265       1.1       cgd  * from an interrupt context.  We try to update the user profiling buffers
    266       1.1       cgd  * cheaply with fuswintr() and suswintr().  If that fails, we revert to
    267       1.1       cgd  * an AST that will vector us to trap() with a context in which copyin
    268       1.1       cgd  * and copyout will work.  Trap will then call addupc_task().
    269       1.1       cgd  *
    270       1.1       cgd  * Note that we may (rarely) not get around to the AST soon enough, and
    271       1.1       cgd  * lose profile ticks when the next tick overwrites this one, but in this
    272       1.1       cgd  * case the system is overloaded and the profile is probably already
    273       1.1       cgd  * inaccurate.
    274       1.1       cgd  */
    275       1.1       cgd void
    276      1.22   mycroft addupc_intr(p, pc)
    277      1.20  augustss 	struct proc *p;
    278      1.20  augustss 	u_long pc;
    279       1.1       cgd {
    280      1.20  augustss 	struct uprof *prof;
    281      1.20  augustss 	caddr_t addr;
    282      1.20  augustss 	u_int i;
    283      1.20  augustss 	int v;
    284       1.1       cgd 
    285       1.1       cgd 	prof = &p->p_stats->p_prof;
    286       1.1       cgd 	if (pc < prof->pr_off ||
    287       1.1       cgd 	    (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
    288       1.1       cgd 		return;			/* out of range; ignore */
    289       1.1       cgd 
    290       1.1       cgd 	addr = prof->pr_base + i;
    291      1.22   mycroft 	if ((v = fuswintr(addr)) == -1 || suswintr(addr, v + 1) == -1) {
    292       1.1       cgd 		prof->pr_addr = pc;
    293      1.22   mycroft 		prof->pr_ticks++;
    294       1.1       cgd 		need_proftick(p);
    295       1.1       cgd 	}
    296       1.1       cgd }
    297       1.1       cgd 
    298       1.1       cgd /*
    299       1.1       cgd  * Much like before, but we can afford to take faults here.  If the
    300       1.1       cgd  * update fails, we simply turn off profiling.
    301       1.1       cgd  */
    302       1.1       cgd void
    303       1.1       cgd addupc_task(p, pc, ticks)
    304      1.20  augustss 	struct proc *p;
    305      1.20  augustss 	u_long pc;
    306       1.1       cgd 	u_int ticks;
    307       1.1       cgd {
    308      1.20  augustss 	struct uprof *prof;
    309      1.20  augustss 	caddr_t addr;
    310      1.20  augustss 	u_int i;
    311       1.1       cgd 	u_short v;
    312       1.1       cgd 
    313       1.1       cgd 	/* Testing P_PROFIL may be unnecessary, but is certainly safe. */
    314       1.1       cgd 	if ((p->p_flag & P_PROFIL) == 0 || ticks == 0)
    315       1.1       cgd 		return;
    316       1.1       cgd 
    317       1.1       cgd 	prof = &p->p_stats->p_prof;
    318       1.1       cgd 	if (pc < prof->pr_off ||
    319       1.1       cgd 	    (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
    320       1.1       cgd 		return;
    321       1.1       cgd 
    322       1.1       cgd 	addr = prof->pr_base + i;
    323       1.1       cgd 	if (copyin(addr, (caddr_t)&v, sizeof(v)) == 0) {
    324       1.1       cgd 		v += ticks;
    325       1.1       cgd 		if (copyout((caddr_t)&v, addr, sizeof(v)) == 0)
    326       1.1       cgd 			return;
    327       1.1       cgd 	}
    328       1.1       cgd 	stopprofclock(p);
    329       1.1       cgd }
    330