xref: /csrg-svn/sys/kern/subr_prof.c (revision 54929)
154790Storek /*-
229100Smckusick  * Copyright (c) 1982, 1986 Regents of the University of California.
341966Smckusick  * All rights reserved.
423380Smckusick  *
541966Smckusick  * %sccs.include.redist.c%
641966Smckusick  *
7*54929Storek  *	@(#)subr_prof.c	7.17 (Berkeley) 07/10/92
823380Smckusick  */
97332Ssam 
1054790Storek #include <sys/param.h>
1154790Storek #include <sys/systm.h>
1254790Storek #include <sys/kernel.h>
1354790Storek #include <sys/proc.h>
1454790Storek #include <sys/user.h>
1554790Storek #include <machine/cpu.h>
1654790Storek 
177332Ssam #ifdef GPROF
1854790Storek #include <sys/malloc.h>
1954790Storek #include <sys/gmon.h>
207332Ssam 
217332Ssam /*
227332Ssam  * Froms is actually a bunch of unsigned shorts indexing tos
237332Ssam  */
2454790Storek struct gmonparam _gmonparam = { GMON_PROF_OFF };
2554790Storek 
267332Ssam u_short	*kcount;
2754790Storek extern char etext[];
287332Ssam 
297332Ssam kmstartup()
307332Ssam {
3154790Storek 	char *cp;
3254790Storek 	int fsize, tsize, ksize;
3354790Storek 	struct gmonparam *p = &_gmonparam;
3410292Smckusick 	/*
3529946Skarels 	 * Round lowpc and highpc to multiples of the density we're using
3629946Skarels 	 * so the rest of the scaling (here and in gprof) stays in ints.
3710292Smckusick 	 */
3854790Storek 	p->lowpc = ROUNDDOWN(KERNBASE, HISTFRACTION * sizeof(HISTCOUNTER));
3954790Storek 	p->highpc = ROUNDUP((u_long)etext, HISTFRACTION * sizeof(HISTCOUNTER));
4054790Storek 	p->textsize = p->highpc - p->lowpc;
4154790Storek 	printf("Profiling kernel, textsize=%d [%x..%x]\n",
4254790Storek 	       p->textsize, p->lowpc, p->highpc);
4354790Storek 	ksize = p->textsize / HISTFRACTION;
4454790Storek 	fsize = p->textsize / HASHFRACTION;
4554790Storek 	p->tolimit = p->textsize * ARCDENSITY / 100;
4654790Storek 	if (p->tolimit < MINARCS)
4754790Storek 		p->tolimit = MINARCS;
4854790Storek 	else if (p->tolimit > MAXARCS)
4954790Storek 		p->tolimit = MAXARCS;
5054790Storek 	tsize = p->tolimit * sizeof(struct tostruct);
5154790Storek 	cp = (char *)malloc(ksize + fsize + tsize, M_GPROF, M_NOWAIT);
5254790Storek 	if (cp == 0) {
5354790Storek 		printf("No memory for profiling.\n");
547332Ssam 		return;
557332Ssam 	}
5654790Storek 	bzero(cp, ksize + tsize + fsize);
5754790Storek 	p->tos = (struct tostruct *)cp;
5854790Storek 	cp += tsize;
5954790Storek 	kcount = (u_short *)cp;
6054790Storek 	cp += ksize;
6154790Storek 	p->froms = (u_short *)cp;
6254137Smckusick 	startprofclock(&proc0);
637332Ssam }
6454790Storek #endif
657332Ssam 
667332Ssam /*
6754790Storek  * Profiling system call.
6854790Storek  *
6954790Storek  * The scale factor is a fixed point number with 16 bits of fraction, so that
7054790Storek  * 1.0 is represented as 0x10000.  A scale factor of 0 turns off profiling.
717332Ssam  */
72*54929Storek struct profil_args {
73*54929Storek 	caddr_t	buf;
74*54929Storek 	u_int	bufsize;
75*54929Storek 	u_int	offset;
76*54929Storek 	u_int	scale;
77*54929Storek };
7854790Storek /* ARGSUSED */
7954790Storek profil(p, uap, retval)
8054790Storek 	struct proc *p;
81*54929Storek 	register struct profil_args *uap;
8254790Storek 	int *retval;
837332Ssam {
8454790Storek 	register struct uprof *upp;
8554790Storek 	int s;
867332Ssam 
8754790Storek 	if (uap->scale > (1 << 16))
8854790Storek 		return (EINVAL);
8954790Storek 	if (uap->scale == 0) {
9054790Storek 		stopprofclock(p);
9154790Storek 		return (0);
927332Ssam 	}
9354790Storek 	upp = &p->p_stats->p_prof;
9454790Storek 	s = splstatclock(); /* block profile interrupts while changing state */
9554790Storek 	upp->pr_base = uap->buf;
9654790Storek 	upp->pr_size = uap->bufsize;
9754790Storek 	upp->pr_off = uap->offset;
9854790Storek 	upp->pr_scale = uap->scale;
9954790Storek 	startprofclock(p);
10054790Storek 	splx(s);
10154790Storek 	return (0);
10254790Storek }
10354790Storek 
10454790Storek /*
10554790Storek  * Scale is a fixed-point number with the binary point 16 bits
10654790Storek  * into the value, and is <= 1.0.  pc is at most 32 bits, so the
10754790Storek  * intermediate result is at most 48 bits.
10854790Storek  */
10954790Storek #define	PC_TO_INDEX(pc, prof) \
11054790Storek 	((int)(((u_quad_t)((pc) - (prof)->pr_off) * \
11154790Storek 	    (u_quad_t)((prof)->pr_scale)) >> 16) & ~1)
11254790Storek 
11354790Storek /*
11454790Storek  * Collect user-level profiling statistics; called on a profiling tick,
11554790Storek  * when a process is running in user-mode.  This routine may be called
11654790Storek  * from an interrupt context.  We try to update the user profiling buffers
11754790Storek  * cheaply with fuswintr() and suswintr().  If that fails, we revert to
11854790Storek  * an AST that will vector us to trap() with a context in which copyin
11954790Storek  * and copyout will work.  Trap will then call addupc_task().
12054790Storek  *
12154790Storek  * Note that we may (rarely) not get around to the AST soon enough, and
12254790Storek  * lose profile ticks when the next tick overwrites this one, but in this
12354790Storek  * case the system is overloaded and the profile is probably already
12454790Storek  * inaccurate.
12554790Storek  */
12654790Storek void
12754790Storek addupc_intr(p, pc, ticks)
12854790Storek 	register struct proc *p;
12954790Storek 	register u_long pc;
13054790Storek 	u_int ticks;
13154790Storek {
13254790Storek 	register struct uprof *prof;
13354790Storek 	register caddr_t addr;
13454790Storek 	register u_int i;
13554790Storek 	register int v;
13654790Storek 
13754790Storek 	if (ticks == 0)
13854790Storek 		return;
13954790Storek 	prof = &p->p_stats->p_prof;
14054790Storek 	if (pc < prof->pr_off ||
14154790Storek 	    (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
14254790Storek 		return;			/* out of range; ignore */
14354790Storek 
14454790Storek 	addr = prof->pr_base + i;
14554790Storek 	if ((v = fuswintr(addr)) == -1 || suswintr(addr, v + ticks) == -1) {
14654790Storek 		prof->pr_addr = pc;
14754790Storek 		prof->pr_ticks = ticks;
14854790Storek 		need_proftick(p);
14910292Smckusick 	}
15054790Storek }
15110292Smckusick 
15254790Storek /*
15354790Storek  * Much like before, but we can afford to take faults here.  If the
15454790Storek  * update fails, we simply turn off profiling.
15554790Storek  */
15654790Storek void
15754790Storek addupc_task(p, pc, ticks)
15854790Storek 	register struct proc *p;
15954790Storek 	register u_long pc;
16054790Storek 	u_int ticks;
16154790Storek {
16254790Storek 	register struct uprof *prof;
16354790Storek 	register caddr_t addr;
16454790Storek 	register u_int i;
16554790Storek 	u_short v;
16654790Storek 
16754790Storek 	/* testing SPROFIL may be unnecessary, but is certainly safe */
16854790Storek 	if ((p->p_flag & SPROFIL) == 0 || ticks == 0)
16954790Storek 		return;
17054790Storek 
17154790Storek 	prof = &p->p_stats->p_prof;
17254790Storek 	if (pc < prof->pr_off ||
17354790Storek 	    (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
17454790Storek 		return;
17554790Storek 
17654790Storek 	addr = prof->pr_base + i;
17754790Storek 	if (copyin(addr, (caddr_t)&v, sizeof(v)) == 0) {
17854790Storek 		v += ticks;
17954790Storek 		if (copyout((caddr_t)&v, addr, sizeof(v)) == 0)
18054790Storek 			return;
1817332Ssam 	}
18254790Storek 	stopprofclock(p);
1837332Ssam }
184