Home | History | Annotate | Line # | Download | only in kern
subr_prof.c revision 1.9
      1 /*	$NetBSD: subr_prof.c,v 1.9 1996/02/04 02:16:46 christos 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.3 (Berkeley) 9/23/93
     36  */
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/kernel.h>
     41 #include <sys/proc.h>
     42 #include <sys/user.h>
     43 
     44 #include <sys/mount.h>
     45 #include <sys/syscallargs.h>
     46 
     47 #include <machine/cpu.h>
     48 
     49 #include <kern/kern_extern.h>
     50 
     51 #ifdef GPROF
     52 #include <sys/malloc.h>
     53 #include <sys/gmon.h>
     54 
     55 /*
     56  * Froms is actually a bunch of unsigned shorts indexing tos
     57  */
     58 struct gmonparam _gmonparam = { GMON_PROF_OFF };
     59 
     60 extern char etext[];
     61 
     62 
     63 void
     64 kmstartup()
     65 {
     66 	char *cp;
     67 	struct gmonparam *p = &_gmonparam;
     68 	/*
     69 	 * Round lowpc and highpc to multiples of the density we're using
     70 	 * so the rest of the scaling (here and in gprof) stays in ints.
     71 	 */
     72 	p->lowpc = ROUNDDOWN(KERNBASE, HISTFRACTION * sizeof(HISTCOUNTER));
     73 	p->highpc = ROUNDUP((u_long)etext, HISTFRACTION * sizeof(HISTCOUNTER));
     74 	p->textsize = p->highpc - p->lowpc;
     75 	printf("Profiling kernel, textsize=%d [%p..%p]\n",
     76 	       p->textsize, p->lowpc, p->highpc);
     77 	p->kcountsize = p->textsize / HISTFRACTION;
     78 	p->hashfraction = HASHFRACTION;
     79 	p->fromssize = p->textsize / HASHFRACTION;
     80 	p->tolimit = p->textsize * ARCDENSITY / 100;
     81 	if (p->tolimit < MINARCS)
     82 		p->tolimit = MINARCS;
     83 	else if (p->tolimit > MAXARCS)
     84 		p->tolimit = MAXARCS;
     85 	p->tossize = p->tolimit * sizeof(struct tostruct);
     86 	cp = (char *)malloc(p->kcountsize + p->fromssize + p->tossize,
     87 	    M_GPROF, M_NOWAIT);
     88 	if (cp == 0) {
     89 		printf("No memory for profiling.\n");
     90 		return;
     91 	}
     92 	bzero(cp, p->kcountsize + p->tossize + p->fromssize);
     93 	p->tos = (struct tostruct *)cp;
     94 	cp += p->tossize;
     95 	p->kcount = (u_short *)cp;
     96 	cp += p->kcountsize;
     97 	p->froms = (u_short *)cp;
     98 }
     99 
    100 /*
    101  * Return kernel profiling information.
    102  */
    103 sysctl_doprof(name, namelen, oldp, oldlenp, newp, newlen, p)
    104 	int *name;
    105 	u_int namelen;
    106 	void *oldp;
    107 	size_t *oldlenp;
    108 	void *newp;
    109 	size_t newlen;
    110 {
    111 	struct gmonparam *gp = &_gmonparam;
    112 	int error;
    113 
    114 	/* all sysctl names at this level are terminal */
    115 	if (namelen != 1)
    116 		return (ENOTDIR);		/* overloaded */
    117 
    118 	switch (name[0]) {
    119 	case GPROF_STATE:
    120 		error = sysctl_int(oldp, oldlenp, newp, newlen, &gp->state);
    121 		if (error)
    122 			return (error);
    123 		if (gp->state == GMON_PROF_OFF)
    124 			stopprofclock(&proc0);
    125 		else
    126 			startprofclock(&proc0);
    127 		return (0);
    128 	case GPROF_COUNT:
    129 		return (sysctl_struct(oldp, oldlenp, newp, newlen,
    130 		    gp->kcount, gp->kcountsize));
    131 	case GPROF_FROMS:
    132 		return (sysctl_struct(oldp, oldlenp, newp, newlen,
    133 		    gp->froms, gp->fromssize));
    134 	case GPROF_TOS:
    135 		return (sysctl_struct(oldp, oldlenp, newp, newlen,
    136 		    gp->tos, gp->tossize));
    137 	case GPROF_GMONPARAM:
    138 		return (sysctl_rdstruct(oldp, oldlenp, newp, gp, sizeof *gp));
    139 	default:
    140 		return (EOPNOTSUPP);
    141 	}
    142 	/* NOTREACHED */
    143 }
    144 #endif /* GPROF */
    145 
    146 /*
    147  * Profiling system call.
    148  *
    149  * The scale factor is a fixed point number with 16 bits of fraction, so that
    150  * 1.0 is represented as 0x10000.  A scale factor of 0 turns off profiling.
    151  */
    152 /* ARGSUSED */
    153 int
    154 sys_profil(p, v, retval)
    155 	struct proc *p;
    156 	void *v;
    157 	register_t *retval;
    158 {
    159 	register struct sys_profil_args /* {
    160 		syscallarg(caddr_t) samples;
    161 		syscallarg(u_int) size;
    162 		syscallarg(u_int) offset;
    163 		syscallarg(u_int) scale;
    164 	} */ *uap = v;
    165 	register struct uprof *upp;
    166 	int s;
    167 
    168 	if (SCARG(uap, scale) > (1 << 16))
    169 		return (EINVAL);
    170 	if (SCARG(uap, scale) == 0) {
    171 		stopprofclock(p);
    172 		return (0);
    173 	}
    174 	upp = &p->p_stats->p_prof;
    175 
    176 	/* Block profile interrupts while changing state. */
    177 	s = splstatclock();
    178 	upp->pr_off = SCARG(uap, offset);
    179 	upp->pr_scale = SCARG(uap, scale);
    180 	upp->pr_base = SCARG(uap, samples);
    181 	upp->pr_size = SCARG(uap, size);
    182 	startprofclock(p);
    183 	splx(s);
    184 
    185 	return (0);
    186 }
    187 
    188 /*
    189  * Scale is a fixed-point number with the binary point 16 bits
    190  * into the value, and is <= 1.0.  pc is at most 32 bits, so the
    191  * intermediate result is at most 48 bits.
    192  */
    193 #define	PC_TO_INDEX(pc, prof) \
    194 	((int)(((u_quad_t)((pc) - (prof)->pr_off) * \
    195 	    (u_quad_t)((prof)->pr_scale)) >> 16) & ~1)
    196 
    197 /*
    198  * Collect user-level profiling statistics; called on a profiling tick,
    199  * when a process is running in user-mode.  This routine may be called
    200  * from an interrupt context.  We try to update the user profiling buffers
    201  * cheaply with fuswintr() and suswintr().  If that fails, we revert to
    202  * an AST that will vector us to trap() with a context in which copyin
    203  * and copyout will work.  Trap will then call addupc_task().
    204  *
    205  * Note that we may (rarely) not get around to the AST soon enough, and
    206  * lose profile ticks when the next tick overwrites this one, but in this
    207  * case the system is overloaded and the profile is probably already
    208  * inaccurate.
    209  */
    210 void
    211 addupc_intr(p, pc, ticks)
    212 	register struct proc *p;
    213 	register u_long pc;
    214 	u_int ticks;
    215 {
    216 	register struct uprof *prof;
    217 	register caddr_t addr;
    218 	register u_int i;
    219 	register int v;
    220 
    221 	if (ticks == 0)
    222 		return;
    223 	prof = &p->p_stats->p_prof;
    224 	if (pc < prof->pr_off ||
    225 	    (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
    226 		return;			/* out of range; ignore */
    227 
    228 	addr = prof->pr_base + i;
    229 	if ((v = fuswintr(addr)) == -1 || suswintr(addr, v + ticks) == -1) {
    230 		prof->pr_addr = pc;
    231 		prof->pr_ticks = ticks;
    232 		need_proftick(p);
    233 	}
    234 }
    235 
    236 /*
    237  * Much like before, but we can afford to take faults here.  If the
    238  * update fails, we simply turn off profiling.
    239  */
    240 void
    241 addupc_task(p, pc, ticks)
    242 	register struct proc *p;
    243 	register u_long pc;
    244 	u_int ticks;
    245 {
    246 	register struct uprof *prof;
    247 	register caddr_t addr;
    248 	register u_int i;
    249 	u_short v;
    250 
    251 	/* Testing P_PROFIL may be unnecessary, but is certainly safe. */
    252 	if ((p->p_flag & P_PROFIL) == 0 || ticks == 0)
    253 		return;
    254 
    255 	prof = &p->p_stats->p_prof;
    256 	if (pc < prof->pr_off ||
    257 	    (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
    258 		return;
    259 
    260 	addr = prof->pr_base + i;
    261 	if (copyin(addr, (caddr_t)&v, sizeof(v)) == 0) {
    262 		v += ticks;
    263 		if (copyout((caddr_t)&v, addr, sizeof(v)) == 0)
    264 			return;
    265 	}
    266 	stopprofclock(p);
    267 }
    268