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