Home | History | Annotate | Line # | Download | only in kern
subr_prof.c revision 1.22.2.4
      1 /*	$NetBSD: subr_prof.c,v 1.22.2.4 2002/05/29 21:33:14 nathanw 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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)subr_prof.c	8.4 (Berkeley) 2/14/95
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: subr_prof.c,v 1.22.2.4 2002/05/29 21:33:14 nathanw Exp $");
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/kernel.h>
     44 #include <sys/lwp.h>
     45 #include <sys/proc.h>
     46 #include <sys/user.h>
     47 #include <sys/mount.h>
     48 #include <sys/sa.h>
     49 #include <sys/syscallargs.h>
     50 #include <sys/sysctl.h>
     51 
     52 #include <machine/cpu.h>
     53 
     54 #ifdef GPROF
     55 #include <sys/malloc.h>
     56 #include <sys/gmon.h>
     57 
     58 /*
     59  * Froms is actually a bunch of unsigned shorts indexing tos
     60  */
     61 struct gmonparam _gmonparam = { GMON_PROF_OFF };
     62 
     63 /* Actual start of the kernel text segment. */
     64 extern char kernel_text[];
     65 
     66 extern char etext[];
     67 
     68 
     69 void
     70 kmstartup()
     71 {
     72 	char *cp;
     73 	struct gmonparam *p = &_gmonparam;
     74 	/*
     75 	 * Round lowpc and highpc to multiples of the density we're using
     76 	 * so the rest of the scaling (here and in gprof) stays in ints.
     77 	 */
     78 	p->lowpc = ROUNDDOWN(((u_long)kernel_text),
     79 		HISTFRACTION * sizeof(HISTCOUNTER));
     80 	p->highpc = ROUNDUP((u_long)etext,
     81 		HISTFRACTION * sizeof(HISTCOUNTER));
     82 	p->textsize = p->highpc - p->lowpc;
     83 	printf("Profiling kernel, textsize=%ld [%lx..%lx]\n",
     84 	       p->textsize, p->lowpc, p->highpc);
     85 	p->kcountsize = p->textsize / HISTFRACTION;
     86 	p->hashfraction = HASHFRACTION;
     87 	p->fromssize = p->textsize / HASHFRACTION;
     88 	p->tolimit = p->textsize * ARCDENSITY / 100;
     89 	if (p->tolimit < MINARCS)
     90 		p->tolimit = MINARCS;
     91 	else if (p->tolimit > MAXARCS)
     92 		p->tolimit = MAXARCS;
     93 	p->tossize = p->tolimit * sizeof(struct tostruct);
     94 	cp = (char *)malloc(p->kcountsize + p->fromssize + p->tossize,
     95 	    M_GPROF, M_NOWAIT);
     96 	if (cp == 0) {
     97 		printf("No memory for profiling.\n");
     98 		return;
     99 	}
    100 	memset(cp, 0, p->kcountsize + p->tossize + p->fromssize);
    101 	p->tos = (struct tostruct *)cp;
    102 	cp += p->tossize;
    103 	p->kcount = (u_short *)cp;
    104 	cp += p->kcountsize;
    105 	p->froms = (u_short *)cp;
    106 }
    107 
    108 /*
    109  * Return kernel profiling information.
    110  */
    111 int
    112 sysctl_doprof(name, namelen, oldp, oldlenp, newp, newlen)
    113 	int *name;
    114 	u_int namelen;
    115 	void *oldp;
    116 	size_t *oldlenp;
    117 	void *newp;
    118 	size_t newlen;
    119 {
    120 	struct gmonparam *gp = &_gmonparam;
    121 	int error;
    122 
    123 	/* all sysctl names at this level are terminal */
    124 	if (namelen != 1)
    125 		return (ENOTDIR);		/* overloaded */
    126 
    127 	/* Check we got the necessary memory at startup. */
    128 	if (gp->kcount == NULL)
    129 		return (EOPNOTSUPP);
    130 
    131 	switch (name[0]) {
    132 	case GPROF_STATE:
    133 		error = sysctl_int(oldp, oldlenp, newp, newlen, &gp->state);
    134 		if (error)
    135 			return (error);
    136 		if (gp->state == GMON_PROF_OFF)
    137 			stopprofclock(&proc0);
    138 		else
    139 			startprofclock(&proc0);
    140 		return (0);
    141 	case GPROF_COUNT:
    142 		return (sysctl_struct(oldp, oldlenp, newp, newlen,
    143 		    gp->kcount, gp->kcountsize));
    144 	case GPROF_FROMS:
    145 		return (sysctl_struct(oldp, oldlenp, newp, newlen,
    146 		    gp->froms, gp->fromssize));
    147 	case GPROF_TOS:
    148 		return (sysctl_struct(oldp, oldlenp, newp, newlen,
    149 		    gp->tos, gp->tossize));
    150 	case GPROF_GMONPARAM:
    151 		return (sysctl_rdstruct(oldp, oldlenp, newp, gp, sizeof(*gp)));
    152 	default:
    153 		return (EOPNOTSUPP);
    154 	}
    155 	/* NOTREACHED */
    156 }
    157 #endif /* GPROF */
    158 
    159 /*
    160  * Profiling system call.
    161  *
    162  * The scale factor is a fixed point number with 16 bits of fraction, so that
    163  * 1.0 is represented as 0x10000.  A scale factor of 0 turns off profiling.
    164  */
    165 /* ARGSUSED */
    166 int
    167 sys_profil(l, v, retval)
    168 	struct lwp *l;
    169 	void *v;
    170 	register_t *retval;
    171 {
    172 	struct sys_profil_args /* {
    173 		syscallarg(caddr_t) samples;
    174 		syscallarg(u_int) size;
    175 		syscallarg(u_int) offset;
    176 		syscallarg(u_int) scale;
    177 	} */ *uap = v;
    178 	struct proc *p = l->l_proc;
    179 	struct uprof *upp;
    180 	int s;
    181 
    182 	if (SCARG(uap, scale) > (1 << 16))
    183 		return (EINVAL);
    184 	if (SCARG(uap, scale) == 0) {
    185 		stopprofclock(p);
    186 		return (0);
    187 	}
    188 	upp = &p->p_stats->p_prof;
    189 
    190 	/* Block profile interrupts while changing state. */
    191 	s = splstatclock();
    192 	upp->pr_off = SCARG(uap, offset);
    193 	upp->pr_scale = SCARG(uap, scale);
    194 	upp->pr_base = SCARG(uap, samples);
    195 	upp->pr_size = SCARG(uap, size);
    196 	startprofclock(p);
    197 	splx(s);
    198 
    199 	return (0);
    200 }
    201 
    202 /*
    203  * Scale is a fixed-point number with the binary point 16 bits
    204  * into the value, and is <= 1.0.  pc is at most 32 bits, so the
    205  * intermediate result is at most 48 bits.
    206  */
    207 #define	PC_TO_INDEX(pc, prof) \
    208 	((int)(((u_quad_t)((pc) - (prof)->pr_off) * \
    209 	    (u_quad_t)((prof)->pr_scale)) >> 16) & ~1)
    210 
    211 /*
    212  * Collect user-level profiling statistics; called on a profiling tick,
    213  * when a process is running in user-mode.  This routine may be called
    214  * from an interrupt context.  We try to update the user profiling buffers
    215  * cheaply with fuswintr() and suswintr().  If that fails, we revert to
    216  * an AST that will vector us to trap() with a context in which copyin
    217  * and copyout will work.  Trap will then call addupc_task().
    218  *
    219  * Note that we may (rarely) not get around to the AST soon enough, and
    220  * lose profile ticks when the next tick overwrites this one, but in this
    221  * case the system is overloaded and the profile is probably already
    222  * inaccurate.
    223  */
    224 void
    225 addupc_intr(p, pc)
    226 	struct proc *p;
    227 	u_long pc;
    228 {
    229 	struct uprof *prof;
    230 	caddr_t addr;
    231 	u_int i;
    232 	int v;
    233 
    234 	prof = &p->p_stats->p_prof;
    235 	if (pc < prof->pr_off ||
    236 	    (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
    237 		return;			/* out of range; ignore */
    238 
    239 	addr = prof->pr_base + i;
    240 	if ((v = fuswintr(addr)) == -1 || suswintr(addr, v + 1) == -1) {
    241 		prof->pr_addr = pc;
    242 		prof->pr_ticks++;
    243 		need_proftick(p);
    244 	}
    245 }
    246 
    247 /*
    248  * Much like before, but we can afford to take faults here.  If the
    249  * update fails, we simply turn off profiling.
    250  */
    251 void
    252 addupc_task(p, pc, ticks)
    253 	struct proc *p;
    254 	u_long pc;
    255 	u_int ticks;
    256 {
    257 	struct uprof *prof;
    258 	caddr_t addr;
    259 	u_int i;
    260 	u_short v;
    261 
    262 	/* Testing P_PROFIL may be unnecessary, but is certainly safe. */
    263 	if ((p->p_flag & P_PROFIL) == 0 || ticks == 0)
    264 		return;
    265 
    266 	prof = &p->p_stats->p_prof;
    267 	if (pc < prof->pr_off ||
    268 	    (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
    269 		return;
    270 
    271 	addr = prof->pr_base + i;
    272 	if (copyin(addr, (caddr_t)&v, sizeof(v)) == 0) {
    273 		v += ticks;
    274 		if (copyout((caddr_t)&v, addr, sizeof(v)) == 0)
    275 			return;
    276 	}
    277 	stopprofclock(p);
    278 }
    279