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