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