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