subr_prof.c revision 35596
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
3435596Sbde * $Id: subr_prof.c,v 1.25 1998/04/15 17:46:23 bde Exp $
351541Srgrimes */
361541Srgrimes
371541Srgrimes#include <sys/param.h>
381541Srgrimes#include <sys/systm.h>
3912221Sbde#include <sys/sysproto.h>
401541Srgrimes#include <sys/kernel.h>
411541Srgrimes#include <sys/proc.h>
4212657Sbde#include <sys/resourcevar.h>
437090Sbde#include <sys/sysctl.h>
447090Sbde
451541Srgrimes#include <machine/cpu.h>
461541Srgrimes
471541Srgrimes#ifdef GPROF
481541Srgrimes#include <sys/malloc.h>
491541Srgrimes#include <sys/gmon.h>
501541Srgrimes
5130354Sphkstatic MALLOC_DEFINE(M_GPROF, "gprof", "kernel profiling buffer");
5230309Sphk
5310653Sdgstatic void kmstartup __P((void *));
5410358SjulianSYSINIT(kmem, SI_SUB_KPROF, SI_ORDER_FIRST, kmstartup, NULL)
5510358Sjulian
561541Srgrimesstruct gmonparam _gmonparam = { GMON_PROF_OFF };
571541Srgrimes
5819000Sbde#ifdef GUPROF
5919000Sbdevoid
6019000Sbdenullfunc_loop_profiled()
6119000Sbde{
6219000Sbde	int i;
6319000Sbde
6419000Sbde	for (i = 0; i < CALIB_SCALE; i++)
6519000Sbde		nullfunc_profiled();
6619000Sbde}
6719000Sbde
6820396Sbde#define	nullfunc_loop_profiled_end	nullfunc_profiled	/* XXX */
6920396Sbde
7019000Sbdevoid
7119000Sbdenullfunc_profiled()
7219000Sbde{
7319000Sbde}
7419000Sbde#endif /* GUPROF */
7519000Sbde
7610407Sbdestatic void
7712569Sbdekmstartup(dummy)
7812569Sbde	void *dummy;
791541Srgrimes{
801541Srgrimes	char *cp;
811541Srgrimes	struct gmonparam *p = &_gmonparam;
8213107Sbde#ifdef GUPROF
8319000Sbde	int cputime_overhead;
8419000Sbde	int empty_loop_time;
8519000Sbde	int i;
8619000Sbde	int mcount_overhead;
8719000Sbde	int mexitcount_overhead;
8819000Sbde	int nullfunc_loop_overhead;
8919000Sbde	int nullfunc_loop_profiled_time;
9020396Sbde	fptrint_t tmp_addr;
9113107Sbde#endif
9213107Sbde
931541Srgrimes	/*
941541Srgrimes	 * Round lowpc and highpc to multiples of the density we're using
951541Srgrimes	 * so the rest of the scaling (here and in gprof) stays in ints.
961541Srgrimes	 */
976009Sbde	p->lowpc = ROUNDDOWN((u_long)btext, HISTFRACTION * sizeof(HISTCOUNTER));
981541Srgrimes	p->highpc = ROUNDUP((u_long)etext, HISTFRACTION * sizeof(HISTCOUNTER));
991541Srgrimes	p->textsize = p->highpc - p->lowpc;
10019000Sbde	printf("Profiling kernel, textsize=%lu [%x..%x]\n",
1011541Srgrimes	       p->textsize, p->lowpc, p->highpc);
1021541Srgrimes	p->kcountsize = p->textsize / HISTFRACTION;
1031541Srgrimes	p->hashfraction = HASHFRACTION;
1041541Srgrimes	p->fromssize = p->textsize / HASHFRACTION;
1051541Srgrimes	p->tolimit = p->textsize * ARCDENSITY / 100;
1061541Srgrimes	if (p->tolimit < MINARCS)
1071541Srgrimes		p->tolimit = MINARCS;
1081541Srgrimes	else if (p->tolimit > MAXARCS)
1091541Srgrimes		p->tolimit = MAXARCS;
1101541Srgrimes	p->tossize = p->tolimit * sizeof(struct tostruct);
1111541Srgrimes	cp = (char *)malloc(p->kcountsize + p->fromssize + p->tossize,
1121541Srgrimes	    M_GPROF, M_NOWAIT);
1131541Srgrimes	if (cp == 0) {
1141541Srgrimes		printf("No memory for profiling.\n");
1151541Srgrimes		return;
1161541Srgrimes	}
1171541Srgrimes	bzero(cp, p->kcountsize + p->tossize + p->fromssize);
1181541Srgrimes	p->tos = (struct tostruct *)cp;
1191541Srgrimes	cp += p->tossize;
12013107Sbde	p->kcount = (HISTCOUNTER *)cp;
1211541Srgrimes	cp += p->kcountsize;
1221541Srgrimes	p->froms = (u_short *)cp;
12313107Sbde
12413107Sbde#ifdef GUPROF
12519000Sbde	/* Initialize pointers to overhead counters. */
12613107Sbde	p->cputime_count = &KCOUNT(p, PC_TO_I(p, cputime));
12713107Sbde	p->mcount_count = &KCOUNT(p, PC_TO_I(p, mcount));
12813107Sbde	p->mexitcount_count = &KCOUNT(p, PC_TO_I(p, mexitcount));
12913107Sbde
13013107Sbde	/*
13119000Sbde	 * Disable interrupts to avoid interference while we calibrate
13219000Sbde	 * things.
13313107Sbde	 */
13413107Sbde	disable_intr();
13513107Sbde
13619000Sbde	/*
13719000Sbde	 * Determine overheads.
13819000Sbde	 * XXX this needs to be repeated for each useful timer/counter.
13919000Sbde	 */
14019000Sbde	cputime_overhead = 0;
14119000Sbde	startguprof(p);
14213107Sbde	for (i = 0; i < CALIB_SCALE; i++)
14319000Sbde		cputime_overhead += cputime();
14413107Sbde
14519000Sbde	empty_loop();
14619000Sbde	startguprof(p);
14719000Sbde	empty_loop();
14819000Sbde	empty_loop_time = cputime();
14919000Sbde
15019000Sbde	nullfunc_loop_profiled();
15119000Sbde
15219000Sbde	/*
15319000Sbde	 * Start profiling.  There won't be any normal function calls since
15419000Sbde	 * interrupts are disabled, but we will call the profiling routines
15519000Sbde	 * directly to determine their overheads.
15619000Sbde	 */
15719000Sbde	p->state = GMON_PROF_HIRES;
15819000Sbde
15919000Sbde	startguprof(p);
16019000Sbde	nullfunc_loop_profiled();
16119000Sbde
16219000Sbde	startguprof(p);
16313107Sbde	for (i = 0; i < CALIB_SCALE; i++)
16435596Sbde#if defined(__i386__) && __GNUC__ >= 2
16535210Sbde		__asm("pushl %0; call __mcount; popl %%ecx"
16635210Sbde		      :
16735210Sbde		      : "i" (profil)
16835210Sbde		      : "ax", "bx", "cx", "dx", "memory");
16913107Sbde#else
17013107Sbde#error
17113107Sbde#endif
17219000Sbde	mcount_overhead = KCOUNT(p, PC_TO_I(p, profil));
17313107Sbde
17419000Sbde	startguprof(p);
17513107Sbde	for (i = 0; i < CALIB_SCALE; i++)
17635596Sbde#if defined(__i386__) && __GNUC__ >= 2
17735210Sbde		    __asm("call mexitcount; 1:"
17835210Sbde			  : : : "ax", "bx", "cx", "dx", "memory");
17935210Sbde	__asm("movl $1b,%0" : "=rm" (tmp_addr));
18013107Sbde#else
18113107Sbde#error
18213107Sbde#endif
18320396Sbde	mexitcount_overhead = KCOUNT(p, PC_TO_I(p, tmp_addr));
18413107Sbde
18513107Sbde	p->state = GMON_PROF_OFF;
18619000Sbde	stopguprof(p);
18719000Sbde
18813107Sbde	enable_intr();
18913107Sbde
19019000Sbde	nullfunc_loop_profiled_time = 0;
19120396Sbde	for (tmp_addr = (fptrint_t)nullfunc_loop_profiled;
19220396Sbde	     tmp_addr < (fptrint_t)nullfunc_loop_profiled_end;
19320396Sbde	     tmp_addr += HISTFRACTION * sizeof(HISTCOUNTER))
19420396Sbde		nullfunc_loop_profiled_time += KCOUNT(p, PC_TO_I(p, tmp_addr));
19519000Sbde#define CALIB_DOSCALE(count)	(((count) + CALIB_SCALE / 3) / CALIB_SCALE)
19619000Sbde#define	c2n(count, freq)	((int)((count) * 1000000000LL / freq))
19719000Sbde	printf("cputime %d, empty_loop %d, nullfunc_loop_profiled %d, mcount %d, mexitcount %d\n",
19819000Sbde	       CALIB_DOSCALE(c2n(cputime_overhead, p->profrate)),
19919000Sbde	       CALIB_DOSCALE(c2n(empty_loop_time, p->profrate)),
20019000Sbde	       CALIB_DOSCALE(c2n(nullfunc_loop_profiled_time, p->profrate)),
20119000Sbde	       CALIB_DOSCALE(c2n(mcount_overhead, p->profrate)),
20219000Sbde	       CALIB_DOSCALE(c2n(mexitcount_overhead, p->profrate)));
20319000Sbde	cputime_overhead -= empty_loop_time;
20419000Sbde	mcount_overhead -= empty_loop_time;
20519000Sbde	mexitcount_overhead -= empty_loop_time;
20619000Sbde
20719000Sbde	/*-
20819000Sbde	 * Profiling overheads are determined by the times between the
20919000Sbde	 * following events:
21019000Sbde	 *	MC1: mcount() is called
21119000Sbde	 *	MC2: cputime() (called from mcount()) latches the timer
21219000Sbde	 *	MC3: mcount() completes
21319000Sbde	 *	ME1: mexitcount() is called
21419000Sbde	 *	ME2: cputime() (called from mexitcount()) latches the timer
21519000Sbde	 *	ME3: mexitcount() completes.
21619000Sbde	 * The times between the events vary slightly depending on instruction
21719000Sbde	 * combination and cache misses, etc.  Attempt to determine the
21819000Sbde	 * minimum times.  These can be subtracted from the profiling times
21919000Sbde	 * without much risk of reducing the profiling times below what they
22019000Sbde	 * would be when profiling is not configured.  Abbreviate:
22119000Sbde	 *	ab = minimum time between MC1 and MC3
22219000Sbde	 *	a  = minumum time between MC1 and MC2
22319000Sbde	 *	b  = minimum time between MC2 and MC3
22419000Sbde	 *	cd = minimum time between ME1 and ME3
22519000Sbde	 *	c  = minimum time between ME1 and ME2
22619000Sbde	 *	d  = minimum time between ME2 and ME3.
22719000Sbde	 * These satisfy the relations:
22819000Sbde	 *	ab            <= mcount_overhead		(just measured)
22919000Sbde	 *	a + b         <= ab
23019000Sbde	 *	        cd    <= mexitcount_overhead		(just measured)
23119000Sbde	 *	        c + d <= cd
23219000Sbde	 *	a         + d <= nullfunc_loop_profiled_time	(just measured)
23319000Sbde	 *	a >= 0, b >= 0, c >= 0, d >= 0.
23419000Sbde	 * Assume that ab and cd are equal to the minimums.
23519000Sbde	 */
23619000Sbde	p->cputime_overhead = CALIB_DOSCALE(cputime_overhead);
23719000Sbde	p->mcount_overhead = CALIB_DOSCALE(mcount_overhead - cputime_overhead);
23819000Sbde	p->mexitcount_overhead = CALIB_DOSCALE(mexitcount_overhead
23919000Sbde					       - cputime_overhead);
24019000Sbde	nullfunc_loop_overhead = nullfunc_loop_profiled_time - empty_loop_time;
24119000Sbde	p->mexitcount_post_overhead = CALIB_DOSCALE((mcount_overhead
24219000Sbde						     - nullfunc_loop_overhead)
24319000Sbde						    / 4);
24419000Sbde	p->mexitcount_pre_overhead = p->mexitcount_overhead
24519000Sbde				     + p->cputime_overhead
24619000Sbde				     - p->mexitcount_post_overhead;
24719000Sbde	p->mcount_pre_overhead = CALIB_DOSCALE(nullfunc_loop_overhead)
24819000Sbde				 - p->mexitcount_post_overhead;
24919000Sbde	p->mcount_post_overhead = p->mcount_overhead
25019000Sbde				  + p->cputime_overhead
25119000Sbde				  - p->mcount_pre_overhead;
25219000Sbde	printf(
25319000Sbde"Profiling overheads: mcount: %d+%d, %d+%d; mexitcount: %d+%d, %d+%d nsec\n",
25419000Sbde	       c2n(p->cputime_overhead, p->profrate),
25519000Sbde	       c2n(p->mcount_overhead, p->profrate),
25619000Sbde	       c2n(p->mcount_pre_overhead, p->profrate),
25719000Sbde	       c2n(p->mcount_post_overhead, p->profrate),
25819000Sbde	       c2n(p->cputime_overhead, p->profrate),
25919000Sbde	       c2n(p->mexitcount_overhead, p->profrate),
26019000Sbde	       c2n(p->mexitcount_pre_overhead, p->profrate),
26119000Sbde	       c2n(p->mexitcount_post_overhead, p->profrate));
26219000Sbde	printf(
26319000Sbde"Profiling overheads: mcount: %d+%d, %d+%d; mexitcount: %d+%d, %d+%d cycles\n",
26419000Sbde	       p->cputime_overhead, p->mcount_overhead,
26519000Sbde	       p->mcount_pre_overhead, p->mcount_post_overhead,
26619000Sbde	       p->cputime_overhead, p->mexitcount_overhead,
26719000Sbde	       p->mexitcount_pre_overhead, p->mexitcount_post_overhead);
26813107Sbde#endif /* GUPROF */
2691541Srgrimes}
2701541Srgrimes
2711541Srgrimes/*
2721541Srgrimes * Return kernel profiling information.
2731541Srgrimes */
27412429Sphkstatic int
27512429Sphksysctl_kern_prof SYSCTL_HANDLER_ARGS
2761541Srgrimes{
27712429Sphk	int *name = (int *) arg1;
27812429Sphk	u_int namelen = arg2;
2791541Srgrimes	struct gmonparam *gp = &_gmonparam;
2801541Srgrimes	int error;
28113107Sbde	int state;
2821541Srgrimes
2831541Srgrimes	/* all sysctl names at this level are terminal */
2841541Srgrimes	if (namelen != 1)
2851541Srgrimes		return (ENOTDIR);		/* overloaded */
2861541Srgrimes
2871541Srgrimes	switch (name[0]) {
2881541Srgrimes	case GPROF_STATE:
28913107Sbde		state = gp->state;
29013107Sbde		error = sysctl_handle_int(oidp, &state, 0, req);
2911541Srgrimes		if (error)
2921541Srgrimes			return (error);
29313107Sbde		if (!req->newptr)
29413107Sbde			return (0);
29513107Sbde		if (state == GMON_PROF_OFF) {
29619000Sbde			gp->state = state;
2971541Srgrimes			stopprofclock(&proc0);
29819000Sbde			stopguprof(gp);
29913107Sbde		} else if (state == GMON_PROF_ON) {
30019000Sbde			gp->state = GMON_PROF_OFF;
30119000Sbde			stopguprof(gp);
30213107Sbde			gp->profrate = profhz;
30319000Sbde			startprofclock(&proc0);
30413107Sbde			gp->state = state;
30513107Sbde#ifdef GUPROF
30613107Sbde		} else if (state == GMON_PROF_HIRES) {
30719000Sbde			gp->state = GMON_PROF_OFF;
30813107Sbde			stopprofclock(&proc0);
30919000Sbde			startguprof(gp);
31013107Sbde			gp->state = state;
31113107Sbde#endif
31213107Sbde		} else if (state != gp->state)
31313107Sbde			return (EINVAL);
3141541Srgrimes		return (0);
3151541Srgrimes	case GPROF_COUNT:
31612429Sphk		return (sysctl_handle_opaque(oidp,
31712429Sphk			gp->kcount, gp->kcountsize, req));
3181541Srgrimes	case GPROF_FROMS:
31912429Sphk		return (sysctl_handle_opaque(oidp,
32012429Sphk			gp->froms, gp->fromssize, req));
3211541Srgrimes	case GPROF_TOS:
32212429Sphk		return (sysctl_handle_opaque(oidp,
32312429Sphk			gp->tos, gp->tossize, req));
3241541Srgrimes	case GPROF_GMONPARAM:
32512429Sphk		return (sysctl_handle_opaque(oidp, gp, sizeof *gp, req));
3261541Srgrimes	default:
3271541Srgrimes		return (EOPNOTSUPP);
3281541Srgrimes	}
3291541Srgrimes	/* NOTREACHED */
3301541Srgrimes}
33112429Sphk
33212429SphkSYSCTL_NODE(_kern, KERN_PROF, prof, CTLFLAG_RW, sysctl_kern_prof, "");
3331541Srgrimes#endif /* GPROF */
3341541Srgrimes
3351541Srgrimes/*
3361541Srgrimes * Profiling system call.
3371541Srgrimes *
3381541Srgrimes * The scale factor is a fixed point number with 16 bits of fraction, so that
3391541Srgrimes * 1.0 is represented as 0x10000.  A scale factor of 0 turns off profiling.
3401541Srgrimes */
34112221Sbde#ifndef _SYS_SYSPROTO_H_
3421541Srgrimesstruct profil_args {
3431541Srgrimes	caddr_t	samples;
3441541Srgrimes	u_int	size;
3451541Srgrimes	u_int	offset;
3461541Srgrimes	u_int	scale;
3471541Srgrimes};
34812221Sbde#endif
3491541Srgrimes/* ARGSUSED */
3501549Srgrimesint
35130994Sphkprofil(p, uap)
3521541Srgrimes	struct proc *p;
3531541Srgrimes	register struct profil_args *uap;
3541541Srgrimes{
3551541Srgrimes	register struct uprof *upp;
3561541Srgrimes	int s;
3571541Srgrimes
3581541Srgrimes	if (uap->scale > (1 << 16))
3591541Srgrimes		return (EINVAL);
3601541Srgrimes	if (uap->scale == 0) {
3611541Srgrimes		stopprofclock(p);
3621541Srgrimes		return (0);
3631541Srgrimes	}
3641541Srgrimes	upp = &p->p_stats->p_prof;
3651541Srgrimes
3661541Srgrimes	/* Block profile interrupts while changing state. */
3671541Srgrimes	s = splstatclock();
3681541Srgrimes	upp->pr_off = uap->offset;
3691541Srgrimes	upp->pr_scale = uap->scale;
3701541Srgrimes	upp->pr_base = uap->samples;
3711541Srgrimes	upp->pr_size = uap->size;
3721541Srgrimes	startprofclock(p);
3731541Srgrimes	splx(s);
3741541Srgrimes
3751541Srgrimes	return (0);
3761541Srgrimes}
3771541Srgrimes
3781541Srgrimes/*
3791541Srgrimes * Scale is a fixed-point number with the binary point 16 bits
3801541Srgrimes * into the value, and is <= 1.0.  pc is at most 32 bits, so the
3811541Srgrimes * intermediate result is at most 48 bits.
3821541Srgrimes */
3831541Srgrimes#define	PC_TO_INDEX(pc, prof) \
3841541Srgrimes	((int)(((u_quad_t)((pc) - (prof)->pr_off) * \
3851541Srgrimes	    (u_quad_t)((prof)->pr_scale)) >> 16) & ~1)
3861541Srgrimes
3871541Srgrimes/*
3881541Srgrimes * Collect user-level profiling statistics; called on a profiling tick,
3891541Srgrimes * when a process is running in user-mode.  This routine may be called
3901541Srgrimes * from an interrupt context.  We try to update the user profiling buffers
3911541Srgrimes * cheaply with fuswintr() and suswintr().  If that fails, we revert to
3921541Srgrimes * an AST that will vector us to trap() with a context in which copyin
3931541Srgrimes * and copyout will work.  Trap will then call addupc_task().
3941541Srgrimes *
3951541Srgrimes * Note that we may (rarely) not get around to the AST soon enough, and
3961541Srgrimes * lose profile ticks when the next tick overwrites this one, but in this
3971541Srgrimes * case the system is overloaded and the profile is probably already
3981541Srgrimes * inaccurate.
3991541Srgrimes */
4001541Srgrimesvoid
4011541Srgrimesaddupc_intr(p, pc, ticks)
4021541Srgrimes	register struct proc *p;
4031541Srgrimes	register u_long pc;
4041541Srgrimes	u_int ticks;
4051541Srgrimes{
4061541Srgrimes	register struct uprof *prof;
4071541Srgrimes	register caddr_t addr;
4081541Srgrimes	register u_int i;
4091541Srgrimes	register int v;
4101541Srgrimes
4111541Srgrimes	if (ticks == 0)
4121541Srgrimes		return;
4131541Srgrimes	prof = &p->p_stats->p_prof;
4141541Srgrimes	if (pc < prof->pr_off ||
4151541Srgrimes	    (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
4161541Srgrimes		return;			/* out of range; ignore */
4171541Srgrimes
4181541Srgrimes	addr = prof->pr_base + i;
4191541Srgrimes	if ((v = fuswintr(addr)) == -1 || suswintr(addr, v + ticks) == -1) {
4201541Srgrimes		prof->pr_addr = pc;
4211541Srgrimes		prof->pr_ticks = ticks;
4221541Srgrimes		need_proftick(p);
4231541Srgrimes	}
4241541Srgrimes}
4251541Srgrimes
4261541Srgrimes/*
4271541Srgrimes * Much like before, but we can afford to take faults here.  If the
4281541Srgrimes * update fails, we simply turn off profiling.
4291541Srgrimes */
43013017Sbdevoid
4311541Srgrimesaddupc_task(p, pc, ticks)
4321541Srgrimes	register struct proc *p;
4331541Srgrimes	register u_long pc;
4341541Srgrimes	u_int ticks;
4351541Srgrimes{
4361541Srgrimes	register struct uprof *prof;
4371541Srgrimes	register caddr_t addr;
4381541Srgrimes	register u_int i;
4391541Srgrimes	u_short v;
4401541Srgrimes
4411541Srgrimes	/* Testing P_PROFIL may be unnecessary, but is certainly safe. */
4421541Srgrimes	if ((p->p_flag & P_PROFIL) == 0 || ticks == 0)
4431541Srgrimes		return;
4441541Srgrimes
4451541Srgrimes	prof = &p->p_stats->p_prof;
4461541Srgrimes	if (pc < prof->pr_off ||
4471541Srgrimes	    (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
4481541Srgrimes		return;
4491541Srgrimes
4501541Srgrimes	addr = prof->pr_base + i;
4511541Srgrimes	if (copyin(addr, (caddr_t)&v, sizeof(v)) == 0) {
4521541Srgrimes		v += ticks;
4531541Srgrimes		if (copyout((caddr_t)&v, addr, sizeof(v)) == 0)
4541541Srgrimes			return;
4551541Srgrimes	}
4561541Srgrimes	stopprofclock(p);
4571541Srgrimes}
458