Home | History | Annotate | Line # | Download | only in kern
subr_prof.c revision 1.22.2.1
      1 /*	$NetBSD: subr_prof.c,v 1.22.2.1 2001/03/05 22:49:44 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/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/kernel.h>
     41 #include <sys/lwp.h>
     42 #include <sys/proc.h>
     43 #include <sys/user.h>
     44 #include <sys/mount.h>
     45 #include <sys/syscallargs.h>
     46 #include <uvm/uvm_extern.h>
     47 #include <sys/sysctl.h>
     48 
     49 #include <machine/cpu.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 /* Actual start of the kernel text segment. */
     61 extern char kernel_text[];
     62 
     63 extern char etext[];
     64 
     65 
     66 void
     67 kmstartup()
     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 int
    109 sysctl_doprof(name, namelen, oldp, oldlenp, newp, newlen)
    110 	int *name;
    111 	u_int namelen;
    112 	void *oldp;
    113 	size_t *oldlenp;
    114 	void *newp;
    115 	size_t newlen;
    116 {
    117 	struct gmonparam *gp = &_gmonparam;
    118 	int error;
    119 
    120 	/* all sysctl names at this level are terminal */
    121 	if (namelen != 1)
    122 		return (ENOTDIR);		/* overloaded */
    123 
    124 	switch (name[0]) {
    125 	case GPROF_STATE:
    126 		error = sysctl_int(oldp, oldlenp, newp, newlen, &gp->state);
    127 		if (error)
    128 			return (error);
    129 		if (gp->state == GMON_PROF_OFF)
    130 			stopprofclock(&proc0);
    131 		else
    132 			startprofclock(&proc0);
    133 		return (0);
    134 	case GPROF_COUNT:
    135 		return (sysctl_struct(oldp, oldlenp, newp, newlen,
    136 		    gp->kcount, gp->kcountsize));
    137 	case GPROF_FROMS:
    138 		return (sysctl_struct(oldp, oldlenp, newp, newlen,
    139 		    gp->froms, gp->fromssize));
    140 	case GPROF_TOS:
    141 		return (sysctl_struct(oldp, oldlenp, newp, newlen,
    142 		    gp->tos, gp->tossize));
    143 	case GPROF_GMONPARAM:
    144 		return (sysctl_rdstruct(oldp, oldlenp, newp, gp, sizeof(*gp)));
    145 	default:
    146 		return (EOPNOTSUPP);
    147 	}
    148 	/* NOTREACHED */
    149 }
    150 #endif /* GPROF */
    151 
    152 /*
    153  * Profiling system call.
    154  *
    155  * The scale factor is a fixed point number with 16 bits of fraction, so that
    156  * 1.0 is represented as 0x10000.  A scale factor of 0 turns off profiling.
    157  */
    158 /* ARGSUSED */
    159 int
    160 sys_profil(l, v, retval)
    161 	struct lwp *l;
    162 	void *v;
    163 	register_t *retval;
    164 {
    165 	struct sys_profil_args /* {
    166 		syscallarg(caddr_t) samples;
    167 		syscallarg(u_int) size;
    168 		syscallarg(u_int) offset;
    169 		syscallarg(u_int) scale;
    170 	} */ *uap = v;
    171 	struct proc *p = l->l_proc;
    172 	struct uprof *upp;
    173 	int s;
    174 
    175 	if (SCARG(uap, scale) > (1 << 16))
    176 		return (EINVAL);
    177 	if (SCARG(uap, scale) == 0) {
    178 		stopprofclock(p);
    179 		return (0);
    180 	}
    181 	upp = &p->p_stats->p_prof;
    182 
    183 	/* Block profile interrupts while changing state. */
    184 	s = splstatclock();
    185 	upp->pr_off = SCARG(uap, offset);
    186 	upp->pr_scale = SCARG(uap, scale);
    187 	upp->pr_base = SCARG(uap, samples);
    188 	upp->pr_size = SCARG(uap, size);
    189 	startprofclock(p);
    190 	splx(s);
    191 
    192 	return (0);
    193 }
    194 
    195 /*
    196  * Scale is a fixed-point number with the binary point 16 bits
    197  * into the value, and is <= 1.0.  pc is at most 32 bits, so the
    198  * intermediate result is at most 48 bits.
    199  */
    200 #define	PC_TO_INDEX(pc, prof) \
    201 	((int)(((u_quad_t)((pc) - (prof)->pr_off) * \
    202 	    (u_quad_t)((prof)->pr_scale)) >> 16) & ~1)
    203 
    204 /*
    205  * Collect user-level profiling statistics; called on a profiling tick,
    206  * when a process is running in user-mode.  This routine may be called
    207  * from an interrupt context.  We try to update the user profiling buffers
    208  * cheaply with fuswintr() and suswintr().  If that fails, we revert to
    209  * an AST that will vector us to trap() with a context in which copyin
    210  * and copyout will work.  Trap will then call addupc_task().
    211  *
    212  * Note that we may (rarely) not get around to the AST soon enough, and
    213  * lose profile ticks when the next tick overwrites this one, but in this
    214  * case the system is overloaded and the profile is probably already
    215  * inaccurate.
    216  */
    217 void
    218 addupc_intr(p, pc)
    219 	struct proc *p;
    220 	u_long pc;
    221 {
    222 	struct uprof *prof;
    223 	caddr_t addr;
    224 	u_int i;
    225 	int v;
    226 
    227 	prof = &p->p_stats->p_prof;
    228 	if (pc < prof->pr_off ||
    229 	    (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
    230 		return;			/* out of range; ignore */
    231 
    232 	addr = prof->pr_base + i;
    233 	if ((v = fuswintr(addr)) == -1 || suswintr(addr, v + 1) == -1) {
    234 		prof->pr_addr = pc;
    235 		prof->pr_ticks++;
    236 		need_proftick(p);
    237 	}
    238 }
    239 
    240 /*
    241  * Much like before, but we can afford to take faults here.  If the
    242  * update fails, we simply turn off profiling.
    243  */
    244 void
    245 addupc_task(p, pc, ticks)
    246 	struct proc *p;
    247 	u_long pc;
    248 	u_int ticks;
    249 {
    250 	struct uprof *prof;
    251 	caddr_t addr;
    252 	u_int i;
    253 	u_short v;
    254 
    255 	/* Testing P_PROFIL may be unnecessary, but is certainly safe. */
    256 	if ((p->p_flag & P_PROFIL) == 0 || ticks == 0)
    257 		return;
    258 
    259 	prof = &p->p_stats->p_prof;
    260 	if (pc < prof->pr_off ||
    261 	    (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
    262 		return;
    263 
    264 	addr = prof->pr_base + i;
    265 	if (copyin(addr, (caddr_t)&v, sizeof(v)) == 0) {
    266 		v += ticks;
    267 		if (copyout((caddr_t)&v, addr, sizeof(v)) == 0)
    268 			return;
    269 	}
    270 	stopprofclock(p);
    271 }
    272