subr_prof.c revision 82717
11541Srgrimes/*-
21541Srgrimes * Copyright (c) 1982, 1986, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 3. All advertising materials mentioning features or use of this software
141541Srgrimes *    must display the following acknowledgement:
151541Srgrimes *	This product includes software developed by the University of
161541Srgrimes *	California, Berkeley and its contributors.
171541Srgrimes * 4. Neither the name of the University nor the names of its contributors
181541Srgrimes *    may be used to endorse or promote products derived from this software
191541Srgrimes *    without specific prior written permission.
201541Srgrimes *
211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311541Srgrimes * SUCH DAMAGE.
321541Srgrimes *
331541Srgrimes *	@(#)subr_prof.c	8.3 (Berkeley) 9/23/93
3450477Speter * $FreeBSD: head/sys/kern/subr_prof.c 82717 2001-09-01 05:47:58Z dillon $
351541Srgrimes */
361541Srgrimes
371541Srgrimes#include <sys/param.h>
381541Srgrimes#include <sys/systm.h>
3912221Sbde#include <sys/sysproto.h>
4052147Sbde#include <sys/kernel.h>
4176166Smarkm#include <sys/lock.h>
4276166Smarkm#include <sys/mutex.h>
431541Srgrimes#include <sys/proc.h>
4412657Sbde#include <sys/resourcevar.h>
4552147Sbde#include <sys/sysctl.h>
467090Sbde
471541Srgrimes#include <machine/cpu.h>
481541Srgrimes
491541Srgrimes#ifdef GPROF
501541Srgrimes#include <sys/malloc.h>
511541Srgrimes#include <sys/gmon.h>
5246548Sbde#undef MCOUNT
531541Srgrimes
5430354Sphkstatic MALLOC_DEFINE(M_GPROF, "gprof", "kernel profiling buffer");
5530309Sphk
5610653Sdgstatic void kmstartup __P((void *));
5710358SjulianSYSINIT(kmem, SI_SUB_KPROF, SI_ORDER_FIRST, kmstartup, NULL)
5810358Sjulian
591541Srgrimesstruct gmonparam _gmonparam = { GMON_PROF_OFF };
601541Srgrimes
6119000Sbde#ifdef GUPROF
6246548Sbde#include <machine/asmacros.h>
6346548Sbde
6419000Sbdevoid
6519000Sbdenullfunc_loop_profiled()
6619000Sbde{
6719000Sbde	int i;
6819000Sbde
6919000Sbde	for (i = 0; i < CALIB_SCALE; i++)
7019000Sbde		nullfunc_profiled();
7119000Sbde}
7219000Sbde
7320396Sbde#define	nullfunc_loop_profiled_end	nullfunc_profiled	/* XXX */
7420396Sbde
7519000Sbdevoid
7619000Sbdenullfunc_profiled()
7719000Sbde{
7819000Sbde}
7919000Sbde#endif /* GUPROF */
8019000Sbde
8110407Sbdestatic void
8212569Sbdekmstartup(dummy)
8312569Sbde	void *dummy;
841541Srgrimes{
851541Srgrimes	char *cp;
861541Srgrimes	struct gmonparam *p = &_gmonparam;
8713107Sbde#ifdef GUPROF
8819000Sbde	int cputime_overhead;
8919000Sbde	int empty_loop_time;
9019000Sbde	int i;
9119000Sbde	int mcount_overhead;
9219000Sbde	int mexitcount_overhead;
9319000Sbde	int nullfunc_loop_overhead;
9419000Sbde	int nullfunc_loop_profiled_time;
9537629Sbde	uintfptr_t tmp_addr;
9674903Sjhb	critical_t savecrit;
9713107Sbde#endif
9813107Sbde
991541Srgrimes	/*
1001541Srgrimes	 * Round lowpc and highpc to multiples of the density we're using
1011541Srgrimes	 * so the rest of the scaling (here and in gprof) stays in ints.
1021541Srgrimes	 */
1036009Sbde	p->lowpc = ROUNDDOWN((u_long)btext, HISTFRACTION * sizeof(HISTCOUNTER));
1041541Srgrimes	p->highpc = ROUNDUP((u_long)etext, HISTFRACTION * sizeof(HISTCOUNTER));
1051541Srgrimes	p->textsize = p->highpc - p->lowpc;
10619000Sbde	printf("Profiling kernel, textsize=%lu [%x..%x]\n",
1071541Srgrimes	       p->textsize, p->lowpc, p->highpc);
1081541Srgrimes	p->kcountsize = p->textsize / HISTFRACTION;
1091541Srgrimes	p->hashfraction = HASHFRACTION;
1101541Srgrimes	p->fromssize = p->textsize / HASHFRACTION;
1111541Srgrimes	p->tolimit = p->textsize * ARCDENSITY / 100;
1121541Srgrimes	if (p->tolimit < MINARCS)
1131541Srgrimes		p->tolimit = MINARCS;
1141541Srgrimes	else if (p->tolimit > MAXARCS)
1151541Srgrimes		p->tolimit = MAXARCS;
1161541Srgrimes	p->tossize = p->tolimit * sizeof(struct tostruct);
1171541Srgrimes	cp = (char *)malloc(p->kcountsize + p->fromssize + p->tossize,
11869781Sdwmalone	    M_GPROF, M_NOWAIT | M_ZERO);
1191541Srgrimes	if (cp == 0) {
1201541Srgrimes		printf("No memory for profiling.\n");
1211541Srgrimes		return;
1221541Srgrimes	}
1231541Srgrimes	p->tos = (struct tostruct *)cp;
1241541Srgrimes	cp += p->tossize;
12513107Sbde	p->kcount = (HISTCOUNTER *)cp;
1261541Srgrimes	cp += p->kcountsize;
1271541Srgrimes	p->froms = (u_short *)cp;
12813107Sbde
12913107Sbde#ifdef GUPROF
13019000Sbde	/* Initialize pointers to overhead counters. */
13113107Sbde	p->cputime_count = &KCOUNT(p, PC_TO_I(p, cputime));
13213107Sbde	p->mcount_count = &KCOUNT(p, PC_TO_I(p, mcount));
13313107Sbde	p->mexitcount_count = &KCOUNT(p, PC_TO_I(p, mexitcount));
13413107Sbde
13513107Sbde	/*
13619000Sbde	 * Disable interrupts to avoid interference while we calibrate
13719000Sbde	 * things.
13813107Sbde	 */
13974903Sjhb	savecrit = critical_enter();
14013107Sbde
14119000Sbde	/*
14219000Sbde	 * Determine overheads.
14319000Sbde	 * XXX this needs to be repeated for each useful timer/counter.
14419000Sbde	 */
14519000Sbde	cputime_overhead = 0;
14619000Sbde	startguprof(p);
14713107Sbde	for (i = 0; i < CALIB_SCALE; i++)
14819000Sbde		cputime_overhead += cputime();
14913107Sbde
15019000Sbde	empty_loop();
15119000Sbde	startguprof(p);
15219000Sbde	empty_loop();
15319000Sbde	empty_loop_time = cputime();
15419000Sbde
15519000Sbde	nullfunc_loop_profiled();
15619000Sbde
15719000Sbde	/*
15819000Sbde	 * Start profiling.  There won't be any normal function calls since
15919000Sbde	 * interrupts are disabled, but we will call the profiling routines
16019000Sbde	 * directly to determine their overheads.
16119000Sbde	 */
16219000Sbde	p->state = GMON_PROF_HIRES;
16319000Sbde
16419000Sbde	startguprof(p);
16519000Sbde	nullfunc_loop_profiled();
16619000Sbde
16719000Sbde	startguprof(p);
16813107Sbde	for (i = 0; i < CALIB_SCALE; i++)
16935596Sbde#if defined(__i386__) && __GNUC__ >= 2
17035210Sbde		__asm("pushl %0; call __mcount; popl %%ecx"
17135210Sbde		      :
17235210Sbde		      : "i" (profil)
17335210Sbde		      : "ax", "bx", "cx", "dx", "memory");
17413107Sbde#else
17513107Sbde#error
17613107Sbde#endif
17719000Sbde	mcount_overhead = KCOUNT(p, PC_TO_I(p, profil));
17813107Sbde
17919000Sbde	startguprof(p);
18013107Sbde	for (i = 0; i < CALIB_SCALE; i++)
18135596Sbde#if defined(__i386__) && __GNUC__ >= 2
18246548Sbde		    __asm("call " __XSTRING(HIDENAME(mexitcount)) "; 1:"
18335210Sbde			  : : : "ax", "bx", "cx", "dx", "memory");
18435210Sbde	__asm("movl $1b,%0" : "=rm" (tmp_addr));
18513107Sbde#else
18613107Sbde#error
18713107Sbde#endif
18820396Sbde	mexitcount_overhead = KCOUNT(p, PC_TO_I(p, tmp_addr));
18913107Sbde
19013107Sbde	p->state = GMON_PROF_OFF;
19119000Sbde	stopguprof(p);
19219000Sbde
19374903Sjhb	critical_exit(savecrit);
19413107Sbde
19519000Sbde	nullfunc_loop_profiled_time = 0;
19637629Sbde	for (tmp_addr = (uintfptr_t)nullfunc_loop_profiled;
19737629Sbde	     tmp_addr < (uintfptr_t)nullfunc_loop_profiled_end;
19820396Sbde	     tmp_addr += HISTFRACTION * sizeof(HISTCOUNTER))
19920396Sbde		nullfunc_loop_profiled_time += KCOUNT(p, PC_TO_I(p, tmp_addr));
20019000Sbde#define CALIB_DOSCALE(count)	(((count) + CALIB_SCALE / 3) / CALIB_SCALE)
20119000Sbde#define	c2n(count, freq)	((int)((count) * 1000000000LL / freq))
20219000Sbde	printf("cputime %d, empty_loop %d, nullfunc_loop_profiled %d, mcount %d, mexitcount %d\n",
20319000Sbde	       CALIB_DOSCALE(c2n(cputime_overhead, p->profrate)),
20419000Sbde	       CALIB_DOSCALE(c2n(empty_loop_time, p->profrate)),
20519000Sbde	       CALIB_DOSCALE(c2n(nullfunc_loop_profiled_time, p->profrate)),
20619000Sbde	       CALIB_DOSCALE(c2n(mcount_overhead, p->profrate)),
20719000Sbde	       CALIB_DOSCALE(c2n(mexitcount_overhead, p->profrate)));
20819000Sbde	cputime_overhead -= empty_loop_time;
20919000Sbde	mcount_overhead -= empty_loop_time;
21019000Sbde	mexitcount_overhead -= empty_loop_time;
21119000Sbde
21219000Sbde	/*-
21319000Sbde	 * Profiling overheads are determined by the times between the
21419000Sbde	 * following events:
21519000Sbde	 *	MC1: mcount() is called
21619000Sbde	 *	MC2: cputime() (called from mcount()) latches the timer
21719000Sbde	 *	MC3: mcount() completes
21819000Sbde	 *	ME1: mexitcount() is called
21919000Sbde	 *	ME2: cputime() (called from mexitcount()) latches the timer
22019000Sbde	 *	ME3: mexitcount() completes.
22119000Sbde	 * The times between the events vary slightly depending on instruction
22219000Sbde	 * combination and cache misses, etc.  Attempt to determine the
22319000Sbde	 * minimum times.  These can be subtracted from the profiling times
22419000Sbde	 * without much risk of reducing the profiling times below what they
22519000Sbde	 * would be when profiling is not configured.  Abbreviate:
22619000Sbde	 *	ab = minimum time between MC1 and MC3
22719000Sbde	 *	a  = minumum time between MC1 and MC2
22819000Sbde	 *	b  = minimum time between MC2 and MC3
22919000Sbde	 *	cd = minimum time between ME1 and ME3
23019000Sbde	 *	c  = minimum time between ME1 and ME2
23119000Sbde	 *	d  = minimum time between ME2 and ME3.
23219000Sbde	 * These satisfy the relations:
23319000Sbde	 *	ab            <= mcount_overhead		(just measured)
23419000Sbde	 *	a + b         <= ab
23519000Sbde	 *	        cd    <= mexitcount_overhead		(just measured)
23619000Sbde	 *	        c + d <= cd
23719000Sbde	 *	a         + d <= nullfunc_loop_profiled_time	(just measured)
23819000Sbde	 *	a >= 0, b >= 0, c >= 0, d >= 0.
23919000Sbde	 * Assume that ab and cd are equal to the minimums.
24019000Sbde	 */
24119000Sbde	p->cputime_overhead = CALIB_DOSCALE(cputime_overhead);
24219000Sbde	p->mcount_overhead = CALIB_DOSCALE(mcount_overhead - cputime_overhead);
24319000Sbde	p->mexitcount_overhead = CALIB_DOSCALE(mexitcount_overhead
24419000Sbde					       - cputime_overhead);
24519000Sbde	nullfunc_loop_overhead = nullfunc_loop_profiled_time - empty_loop_time;
24619000Sbde	p->mexitcount_post_overhead = CALIB_DOSCALE((mcount_overhead
24719000Sbde						     - nullfunc_loop_overhead)
24819000Sbde						    / 4);
24919000Sbde	p->mexitcount_pre_overhead = p->mexitcount_overhead
25019000Sbde				     + p->cputime_overhead
25119000Sbde				     - p->mexitcount_post_overhead;
25219000Sbde	p->mcount_pre_overhead = CALIB_DOSCALE(nullfunc_loop_overhead)
25319000Sbde				 - p->mexitcount_post_overhead;
25419000Sbde	p->mcount_post_overhead = p->mcount_overhead
25519000Sbde				  + p->cputime_overhead
25619000Sbde				  - p->mcount_pre_overhead;
25719000Sbde	printf(
25819000Sbde"Profiling overheads: mcount: %d+%d, %d+%d; mexitcount: %d+%d, %d+%d nsec\n",
25919000Sbde	       c2n(p->cputime_overhead, p->profrate),
26019000Sbde	       c2n(p->mcount_overhead, p->profrate),
26119000Sbde	       c2n(p->mcount_pre_overhead, p->profrate),
26219000Sbde	       c2n(p->mcount_post_overhead, p->profrate),
26319000Sbde	       c2n(p->cputime_overhead, p->profrate),
26419000Sbde	       c2n(p->mexitcount_overhead, p->profrate),
26519000Sbde	       c2n(p->mexitcount_pre_overhead, p->profrate),
26619000Sbde	       c2n(p->mexitcount_post_overhead, p->profrate));
26719000Sbde	printf(
26819000Sbde"Profiling overheads: mcount: %d+%d, %d+%d; mexitcount: %d+%d, %d+%d cycles\n",
26919000Sbde	       p->cputime_overhead, p->mcount_overhead,
27019000Sbde	       p->mcount_pre_overhead, p->mcount_post_overhead,
27119000Sbde	       p->cputime_overhead, p->mexitcount_overhead,
27219000Sbde	       p->mexitcount_pre_overhead, p->mexitcount_post_overhead);
27313107Sbde#endif /* GUPROF */
2741541Srgrimes}
2751541Srgrimes
2761541Srgrimes/*
2771541Srgrimes * Return kernel profiling information.
2781541Srgrimes */
27912429Sphkstatic int
28062573Sphksysctl_kern_prof(SYSCTL_HANDLER_ARGS)
2811541Srgrimes{
28212429Sphk	int *name = (int *) arg1;
28312429Sphk	u_int namelen = arg2;
2841541Srgrimes	struct gmonparam *gp = &_gmonparam;
2851541Srgrimes	int error;
28613107Sbde	int state;
2871541Srgrimes
2881541Srgrimes	/* all sysctl names at this level are terminal */
2891541Srgrimes	if (namelen != 1)
2901541Srgrimes		return (ENOTDIR);		/* overloaded */
2911541Srgrimes
2921541Srgrimes	switch (name[0]) {
2931541Srgrimes	case GPROF_STATE:
29413107Sbde		state = gp->state;
29513107Sbde		error = sysctl_handle_int(oidp, &state, 0, req);
2961541Srgrimes		if (error)
2971541Srgrimes			return (error);
29813107Sbde		if (!req->newptr)
29913107Sbde			return (0);
30013107Sbde		if (state == GMON_PROF_OFF) {
30119000Sbde			gp->state = state;
3021541Srgrimes			stopprofclock(&proc0);
30319000Sbde			stopguprof(gp);
30413107Sbde		} else if (state == GMON_PROF_ON) {
30519000Sbde			gp->state = GMON_PROF_OFF;
30619000Sbde			stopguprof(gp);
30713107Sbde			gp->profrate = profhz;
30819000Sbde			startprofclock(&proc0);
30913107Sbde			gp->state = state;
31013107Sbde#ifdef GUPROF
31113107Sbde		} else if (state == GMON_PROF_HIRES) {
31219000Sbde			gp->state = GMON_PROF_OFF;
31313107Sbde			stopprofclock(&proc0);
31419000Sbde			startguprof(gp);
31513107Sbde			gp->state = state;
31613107Sbde#endif
31713107Sbde		} else if (state != gp->state)
31813107Sbde			return (EINVAL);
3191541Srgrimes		return (0);
3201541Srgrimes	case GPROF_COUNT:
32112429Sphk		return (sysctl_handle_opaque(oidp,
32212429Sphk			gp->kcount, gp->kcountsize, req));
3231541Srgrimes	case GPROF_FROMS:
32412429Sphk		return (sysctl_handle_opaque(oidp,
32512429Sphk			gp->froms, gp->fromssize, req));
3261541Srgrimes	case GPROF_TOS:
32712429Sphk		return (sysctl_handle_opaque(oidp,
32812429Sphk			gp->tos, gp->tossize, req));
3291541Srgrimes	case GPROF_GMONPARAM:
33012429Sphk		return (sysctl_handle_opaque(oidp, gp, sizeof *gp, req));
3311541Srgrimes	default:
3321541Srgrimes		return (EOPNOTSUPP);
3331541Srgrimes	}
3341541Srgrimes	/* NOTREACHED */
3351541Srgrimes}
33612429Sphk
33712429SphkSYSCTL_NODE(_kern, KERN_PROF, prof, CTLFLAG_RW, sysctl_kern_prof, "");
3381541Srgrimes#endif /* GPROF */
3391541Srgrimes
3401541Srgrimes/*
3411541Srgrimes * Profiling system call.
3421541Srgrimes *
3431541Srgrimes * The scale factor is a fixed point number with 16 bits of fraction, so that
3441541Srgrimes * 1.0 is represented as 0x10000.  A scale factor of 0 turns off profiling.
3451541Srgrimes */
34612221Sbde#ifndef _SYS_SYSPROTO_H_
3471541Srgrimesstruct profil_args {
3481541Srgrimes	caddr_t	samples;
34938864Sbde	size_t	size;
35038864Sbde	size_t	offset;
3511541Srgrimes	u_int	scale;
3521541Srgrimes};
35312221Sbde#endif
35482717Sdillon/*
35582717Sdillon * MPSAFE
35682717Sdillon */
3571541Srgrimes/* ARGSUSED */
3581549Srgrimesint
35930994Sphkprofil(p, uap)
3601541Srgrimes	struct proc *p;
3611541Srgrimes	register struct profil_args *uap;
3621541Srgrimes{
3631541Srgrimes	register struct uprof *upp;
3641541Srgrimes	int s;
36582717Sdillon	int error = 0;
3661541Srgrimes
36782717Sdillon	mtx_lock(&Giant);
36882717Sdillon
36982717Sdillon	if (uap->scale > (1 << 16)) {
37082717Sdillon		error = EINVAL;
37182717Sdillon		goto done2;
37282717Sdillon	}
3731541Srgrimes	if (uap->scale == 0) {
3741541Srgrimes		stopprofclock(p);
37582717Sdillon		goto done2;
3761541Srgrimes	}
3771541Srgrimes	upp = &p->p_stats->p_prof;
3781541Srgrimes
3791541Srgrimes	/* Block profile interrupts while changing state. */
3801541Srgrimes	s = splstatclock();
3811541Srgrimes	upp->pr_off = uap->offset;
3821541Srgrimes	upp->pr_scale = uap->scale;
3831541Srgrimes	upp->pr_base = uap->samples;
3841541Srgrimes	upp->pr_size = uap->size;
3851541Srgrimes	startprofclock(p);
3861541Srgrimes	splx(s);
3871541Srgrimes
38882717Sdillondone2:
38982717Sdillon	mtx_unlock(&Giant);
39082717Sdillon	return (error);
3911541Srgrimes}
3921541Srgrimes
3931541Srgrimes/*
3941541Srgrimes * Scale is a fixed-point number with the binary point 16 bits
3951541Srgrimes * into the value, and is <= 1.0.  pc is at most 32 bits, so the
3961541Srgrimes * intermediate result is at most 48 bits.
3971541Srgrimes */
3981541Srgrimes#define	PC_TO_INDEX(pc, prof) \
3991541Srgrimes	((int)(((u_quad_t)((pc) - (prof)->pr_off) * \
4001541Srgrimes	    (u_quad_t)((prof)->pr_scale)) >> 16) & ~1)
4011541Srgrimes
4021541Srgrimes/*
4031541Srgrimes * Collect user-level profiling statistics; called on a profiling tick,
4041541Srgrimes * when a process is running in user-mode.  This routine may be called
4051541Srgrimes * from an interrupt context.  We try to update the user profiling buffers
4061541Srgrimes * cheaply with fuswintr() and suswintr().  If that fails, we revert to
4071541Srgrimes * an AST that will vector us to trap() with a context in which copyin
4081541Srgrimes * and copyout will work.  Trap will then call addupc_task().
4091541Srgrimes *
4101541Srgrimes * Note that we may (rarely) not get around to the AST soon enough, and
4111541Srgrimes * lose profile ticks when the next tick overwrites this one, but in this
4121541Srgrimes * case the system is overloaded and the profile is probably already
4131541Srgrimes * inaccurate.
4141541Srgrimes */
4151541Srgrimesvoid
4161541Srgrimesaddupc_intr(p, pc, ticks)
4171541Srgrimes	register struct proc *p;
41872912Sjhb	register uintptr_t pc;
4191541Srgrimes	u_int ticks;
4201541Srgrimes{
4211541Srgrimes	register struct uprof *prof;
4221541Srgrimes	register caddr_t addr;
4231541Srgrimes	register u_int i;
4241541Srgrimes	register int v;
4251541Srgrimes
4261541Srgrimes	if (ticks == 0)
4271541Srgrimes		return;
4281541Srgrimes	prof = &p->p_stats->p_prof;
4291541Srgrimes	if (pc < prof->pr_off ||
4301541Srgrimes	    (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
4311541Srgrimes		return;			/* out of range; ignore */
4321541Srgrimes
4331541Srgrimes	addr = prof->pr_base + i;
4341541Srgrimes	if ((v = fuswintr(addr)) == -1 || suswintr(addr, v + ticks) == -1) {
43581493Sjhb		mtx_lock_spin(&sched_lock);
4361541Srgrimes		prof->pr_addr = pc;
4371541Srgrimes		prof->pr_ticks = ticks;
43881493Sjhb		p->p_sflag |= PS_OWEUPC | PS_ASTPENDING;
43981493Sjhb		mtx_unlock_spin(&sched_lock);
4401541Srgrimes	}
4411541Srgrimes}
4421541Srgrimes
4431541Srgrimes/*
4441541Srgrimes * Much like before, but we can afford to take faults here.  If the
4451541Srgrimes * update fails, we simply turn off profiling.
4461541Srgrimes */
44713017Sbdevoid
4481541Srgrimesaddupc_task(p, pc, ticks)
4491541Srgrimes	register struct proc *p;
45072912Sjhb	register uintptr_t pc;
4511541Srgrimes	u_int ticks;
4521541Srgrimes{
4531541Srgrimes	register struct uprof *prof;
4541541Srgrimes	register caddr_t addr;
4551541Srgrimes	register u_int i;
4561541Srgrimes	u_short v;
4571541Srgrimes
45871565Sjhb	/* Testing PS_PROFIL may be unnecessary, but is certainly safe. */
45977840Sjhb	if ((p->p_sflag & PS_PROFIL) == 0 || ticks == 0)
4601541Srgrimes		return;
4611541Srgrimes
4621541Srgrimes	prof = &p->p_stats->p_prof;
4631541Srgrimes	if (pc < prof->pr_off ||
4641541Srgrimes	    (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
4651541Srgrimes		return;
4661541Srgrimes
4671541Srgrimes	addr = prof->pr_base + i;
4681541Srgrimes	if (copyin(addr, (caddr_t)&v, sizeof(v)) == 0) {
4691541Srgrimes		v += ticks;
4701541Srgrimes		if (copyout((caddr_t)&v, addr, sizeof(v)) == 0)
4711541Srgrimes			return;
4721541Srgrimes	}
4731541Srgrimes	stopprofclock(p);
4741541Srgrimes}
475