Home | History | Annotate | Line # | Download | only in kern
subr_prof.c revision 1.48
      1 /*	$NetBSD: subr_prof.c,v 1.48 2018/02/04 17:31:51 maxv 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. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  *	@(#)subr_prof.c	8.4 (Berkeley) 2/14/95
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: subr_prof.c,v 1.48 2018/02/04 17:31:51 maxv Exp $");
     36 
     37 #ifdef _KERNEL_OPT
     38 #include "opt_gprof.h"
     39 #endif
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/kernel.h>
     44 #include <sys/proc.h>
     45 #include <sys/mount.h>
     46 #include <sys/syscallargs.h>
     47 #include <sys/sysctl.h>
     48 
     49 #include <sys/cpu.h>
     50 
     51 #ifdef GPROF
     52 #include <sys/malloc.h>
     53 #include <sys/gmon.h>
     54 
     55 MALLOC_DEFINE(M_GPROF, "gprof", "kernel profiling buffer");
     56 
     57 /*
     58  * Froms is actually a bunch of unsigned shorts indexing tos
     59  */
     60 struct gmonparam _gmonparam = { .state = GMON_PROF_OFF };
     61 
     62 /* Actual start of the kernel text segment. */
     63 extern char kernel_text[];
     64 
     65 extern char etext[];
     66 
     67 
     68 void
     69 kmstartup(void)
     70 {
     71 	char *cp;
     72 	struct gmonparam *p = &_gmonparam;
     73 	/*
     74 	 * Round lowpc and highpc to multiples of the density we're using
     75 	 * so the rest of the scaling (here and in gprof) stays in ints.
     76 	 */
     77 	p->lowpc = rounddown(((u_long)kernel_text),
     78 		HISTFRACTION * sizeof(HISTCOUNTER));
     79 	p->highpc = roundup((u_long)etext,
     80 		HISTFRACTION * sizeof(HISTCOUNTER));
     81 	p->textsize = p->highpc - p->lowpc;
     82 	printf("Profiling kernel, textsize=%ld [%lx..%lx]\n",
     83 	       p->textsize, p->lowpc, p->highpc);
     84 	p->kcountsize = p->textsize / HISTFRACTION;
     85 	p->hashfraction = HASHFRACTION;
     86 	p->fromssize = p->textsize / HASHFRACTION;
     87 	p->tolimit = p->textsize * ARCDENSITY / 100;
     88 	if (p->tolimit < MINARCS)
     89 		p->tolimit = MINARCS;
     90 	else if (p->tolimit > MAXARCS)
     91 		p->tolimit = MAXARCS;
     92 	p->tossize = p->tolimit * sizeof(struct tostruct);
     93 	cp = malloc(p->kcountsize + p->fromssize + p->tossize,
     94 	    M_GPROF, M_NOWAIT | M_ZERO);
     95 	if (cp == 0) {
     96 		printf("No memory for profiling.\n");
     97 		return;
     98 	}
     99 	p->tos = (struct tostruct *)cp;
    100 	cp += p->tossize;
    101 	p->kcount = (u_short *)cp;
    102 	cp += p->kcountsize;
    103 	p->froms = (u_short *)cp;
    104 }
    105 
    106 /*
    107  * Return kernel profiling information.
    108  */
    109 /*
    110  * sysctl helper routine for kern.profiling subtree.  enables/disables
    111  * kernel profiling and gives out copies of the profiling data.
    112  */
    113 static int
    114 sysctl_kern_profiling(SYSCTLFN_ARGS)
    115 {
    116 	struct gmonparam *gp = &_gmonparam;
    117 	int error;
    118 	struct sysctlnode node;
    119 
    120 	node = *rnode;
    121 
    122 	switch (node.sysctl_num) {
    123 	case GPROF_STATE:
    124 		node.sysctl_data = &gp->state;
    125 		break;
    126 	case GPROF_COUNT:
    127 		node.sysctl_data = gp->kcount;
    128 		node.sysctl_size = gp->kcountsize;
    129 		break;
    130 	case GPROF_FROMS:
    131 		node.sysctl_data = gp->froms;
    132 		node.sysctl_size = gp->fromssize;
    133 		break;
    134 	case GPROF_TOS:
    135 		node.sysctl_data = gp->tos;
    136 		node.sysctl_size = gp->tossize;
    137 		break;
    138 	case GPROF_GMONPARAM:
    139 		node.sysctl_data = gp;
    140 		node.sysctl_size = sizeof(*gp);
    141 		break;
    142 	default:
    143 		return (EOPNOTSUPP);
    144 	}
    145 
    146 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    147 	if (error || newp == NULL)
    148 		return (error);
    149 
    150 	if (node.sysctl_num == GPROF_STATE) {
    151 		mutex_spin_enter(&proc0.p_stmutex);
    152 		if (gp->state == GMON_PROF_OFF)
    153 			stopprofclock(&proc0);
    154 		else
    155 			startprofclock(&proc0);
    156 		mutex_spin_exit(&proc0.p_stmutex);
    157 	}
    158 
    159 	return (0);
    160 }
    161 
    162 SYSCTL_SETUP(sysctl_kern_gprof_setup, "sysctl kern.profiling subtree setup")
    163 {
    164 
    165 	sysctl_createv(clog, 0, NULL, NULL,
    166 		       CTLFLAG_PERMANENT,
    167 		       CTLTYPE_NODE, "profiling",
    168 		       SYSCTL_DESCR("Profiling information (available)"),
    169 		       NULL, 0, NULL, 0,
    170 		       CTL_KERN, KERN_PROF, CTL_EOL);
    171 
    172 	sysctl_createv(clog, 0, NULL, NULL,
    173 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    174 		       CTLTYPE_INT, "state",
    175 		       SYSCTL_DESCR("Profiling state"),
    176 		       sysctl_kern_profiling, 0, NULL, 0,
    177 		       CTL_KERN, KERN_PROF, GPROF_STATE, CTL_EOL);
    178 	sysctl_createv(clog, 0, NULL, NULL,
    179 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    180 		       CTLTYPE_STRUCT, "count",
    181 		       SYSCTL_DESCR("Array of statistical program counters"),
    182 		       sysctl_kern_profiling, 0, NULL, 0,
    183 		       CTL_KERN, KERN_PROF, GPROF_COUNT, CTL_EOL);
    184 	sysctl_createv(clog, 0, NULL, NULL,
    185 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    186 		       CTLTYPE_STRUCT, "froms",
    187 		       SYSCTL_DESCR("Array indexed by program counter of "
    188 				    "call-from points"),
    189 		       sysctl_kern_profiling, 0, NULL, 0,
    190 		       CTL_KERN, KERN_PROF, GPROF_FROMS, CTL_EOL);
    191 	sysctl_createv(clog, 0, NULL, NULL,
    192 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    193 		       CTLTYPE_STRUCT, "tos",
    194 		       SYSCTL_DESCR("Array of structures describing "
    195 				    "destination of calls and their counts"),
    196 		       sysctl_kern_profiling, 0, NULL, 0,
    197 		       CTL_KERN, KERN_PROF, GPROF_TOS, CTL_EOL);
    198 	sysctl_createv(clog, 0, NULL, NULL,
    199 		       CTLFLAG_PERMANENT,
    200 		       CTLTYPE_STRUCT, "gmonparam",
    201 		       SYSCTL_DESCR("Structure giving the sizes of the above "
    202 				    "arrays"),
    203 		       sysctl_kern_profiling, 0, NULL, 0,
    204 		       CTL_KERN, KERN_PROF, GPROF_GMONPARAM, CTL_EOL);
    205 }
    206 #endif /* GPROF */
    207 
    208 /*
    209  * Profiling system call.
    210  *
    211  * The scale factor is a fixed point number with 16 bits of fraction, so that
    212  * 1.0 is represented as 0x10000.  A scale factor of 0 turns off profiling.
    213  */
    214 /* ARGSUSED */
    215 int
    216 sys_profil(struct lwp *l, const struct sys_profil_args *uap, register_t *retval)
    217 {
    218 	/* {
    219 		syscallarg(char *) samples;
    220 		syscallarg(size_t) size;
    221 		syscallarg(u_long) offset;
    222 		syscallarg(u_int) scale;
    223 	} */
    224 	struct proc *p = l->l_proc;
    225 	struct uprof *upp;
    226 
    227 	if (SCARG(uap, scale) > (1 << 16))
    228 		return (EINVAL);
    229 	if (SCARG(uap, scale) == 0) {
    230 		mutex_spin_enter(&p->p_stmutex);
    231 		stopprofclock(p);
    232 		mutex_spin_exit(&p->p_stmutex);
    233 		return (0);
    234 	}
    235 	upp = &p->p_stats->p_prof;
    236 
    237 	/* Block profile interrupts while changing state. */
    238 	mutex_spin_enter(&p->p_stmutex);
    239 	upp->pr_off = SCARG(uap, offset);
    240 	upp->pr_scale = SCARG(uap, scale);
    241 	upp->pr_base = SCARG(uap, samples);
    242 	upp->pr_size = SCARG(uap, size);
    243 	startprofclock(p);
    244 	mutex_spin_exit(&p->p_stmutex);
    245 
    246 	return (0);
    247 }
    248 
    249 /*
    250  * Scale is a fixed-point number with the binary point 16 bits
    251  * into the value, and is <= 1.0.  pc is at most 32 bits, so the
    252  * intermediate result is at most 48 bits.
    253  */
    254 #define	PC_TO_INDEX(pc, prof) \
    255 	((int)(((u_quad_t)((pc) - (prof)->pr_off) * \
    256 	    (u_quad_t)((prof)->pr_scale)) >> 16) & ~1)
    257 
    258 /*
    259  * Collect user-level profiling statistics; called on a profiling tick,
    260  * when a process is running in user-mode.  This routine may be called
    261  * from an interrupt context.  We try to update the user profiling buffers
    262  * cheaply with fuswintr() and suswintr().  If that fails, we revert to
    263  * an AST that will vector us to trap() with a context in which copyin
    264  * and copyout will work.  Trap will then call addupc_task().
    265  *
    266  * Note that we may (rarely) not get around to the AST soon enough, and
    267  * lose profile ticks when the next tick overwrites this one, but in this
    268  * case the system is overloaded and the profile is probably already
    269  * inaccurate.
    270  */
    271 void
    272 addupc_intr(struct lwp *l, u_long pc)
    273 {
    274 	struct uprof *prof;
    275 	struct proc *p;
    276 	void *addr;
    277 	u_int i;
    278 	int v;
    279 
    280 	p = l->l_proc;
    281 
    282 	KASSERT(mutex_owned(&p->p_stmutex));
    283 
    284 	prof = &p->p_stats->p_prof;
    285 	if (pc < prof->pr_off ||
    286 	    (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
    287 		return;			/* out of range; ignore */
    288 
    289 	addr = prof->pr_base + i;
    290 	mutex_spin_exit(&p->p_stmutex);
    291 	if ((v = fuswintr(addr)) == -1 || suswintr(addr, v + 1) == -1) {
    292 		/* XXXSMP */
    293 		prof->pr_addr = pc;
    294 		prof->pr_ticks++;
    295 		cpu_need_proftick(l);
    296 	}
    297 	mutex_spin_enter(&p->p_stmutex);
    298 }
    299 
    300 /*
    301  * Much like before, but we can afford to take faults here.  If the
    302  * update fails, we simply turn off profiling.
    303  */
    304 void
    305 addupc_task(struct lwp *l, u_long pc, u_int ticks)
    306 {
    307 	struct uprof *prof;
    308 	struct proc *p;
    309 	void *addr;
    310 	int error;
    311 	u_int i;
    312 	u_short v;
    313 
    314 	p = l->l_proc;
    315 
    316 	if (ticks == 0)
    317 		return;
    318 
    319 	mutex_spin_enter(&p->p_stmutex);
    320 	prof = &p->p_stats->p_prof;
    321 
    322 	/* Testing P_PROFIL may be unnecessary, but is certainly safe. */
    323 	if ((p->p_stflag & PST_PROFIL) == 0 || pc < prof->pr_off ||
    324 	    (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size) {
    325 		mutex_spin_exit(&p->p_stmutex);
    326 		return;
    327 	}
    328 
    329 	addr = prof->pr_base + i;
    330 	mutex_spin_exit(&p->p_stmutex);
    331 	if ((error = copyin(addr, (void *)&v, sizeof(v))) == 0) {
    332 		v += ticks;
    333 		error = copyout((void *)&v, addr, sizeof(v));
    334 	}
    335 	if (error != 0) {
    336 		mutex_spin_enter(&p->p_stmutex);
    337 		stopprofclock(p);
    338 		mutex_spin_exit(&p->p_stmutex);
    339 	}
    340 }
    341