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