xref: /freebsd/sys/kern/subr_prof.c (revision e627b39baccd1ec9129690167cf5e6d860509655)
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.15 1995/12/26 01:21:39 bde Exp $
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/sysproto.h>
40 #include <sys/kernel.h>
41 #include <sys/proc.h>
42 #include <sys/resourcevar.h>
43 #include <sys/sysctl.h>
44 
45 #include <machine/cpu.h>
46 
47 #ifdef GPROF
48 #include <sys/malloc.h>
49 #include <sys/gmon.h>
50 
51 static void kmstartup __P((void *));
52 SYSINIT(kmem, SI_SUB_KPROF, SI_ORDER_FIRST, kmstartup, NULL)
53 
54 struct gmonparam _gmonparam = { GMON_PROF_OFF };
55 
56 extern char btext[];
57 extern char etext[];
58 
59 static void
60 kmstartup(dummy)
61 	void *dummy;
62 {
63 	char *cp;
64 	struct gmonparam *p = &_gmonparam;
65 #ifdef GUPROF
66 	fptrint_t kmstartup_addr;
67 	int i;
68 #endif
69 
70 	/*
71 	 * Round lowpc and highpc to multiples of the density we're using
72 	 * so the rest of the scaling (here and in gprof) stays in ints.
73 	 */
74 	p->lowpc = ROUNDDOWN((u_long)btext, HISTFRACTION * sizeof(HISTCOUNTER));
75 	p->highpc = ROUNDUP((u_long)etext, HISTFRACTION * sizeof(HISTCOUNTER));
76 	p->textsize = p->highpc - p->lowpc;
77 	printf("Profiling kernel, textsize=%d [%x..%x]\n",
78 	       p->textsize, p->lowpc, p->highpc);
79 	p->kcountsize = p->textsize / HISTFRACTION;
80 	p->hashfraction = HASHFRACTION;
81 	p->fromssize = p->textsize / HASHFRACTION;
82 	p->tolimit = p->textsize * ARCDENSITY / 100;
83 	if (p->tolimit < MINARCS)
84 		p->tolimit = MINARCS;
85 	else if (p->tolimit > MAXARCS)
86 		p->tolimit = MAXARCS;
87 	p->tossize = p->tolimit * sizeof(struct tostruct);
88 	cp = (char *)malloc(p->kcountsize + p->fromssize + p->tossize,
89 	    M_GPROF, M_NOWAIT);
90 	if (cp == 0) {
91 		printf("No memory for profiling.\n");
92 		return;
93 	}
94 	bzero(cp, p->kcountsize + p->tossize + p->fromssize);
95 	p->tos = (struct tostruct *)cp;
96 	cp += p->tossize;
97 	p->kcount = (HISTCOUNTER *)cp;
98 	cp += p->kcountsize;
99 	p->froms = (u_short *)cp;
100 
101 #ifdef GUPROF
102 	/*
103 	 * Initialize pointers to overhead counters.
104 	 */
105 	p->cputime_count = &KCOUNT(p, PC_TO_I(p, cputime));
106 	p->mcount_count = &KCOUNT(p, PC_TO_I(p, mcount));
107 	p->mexitcount_count = &KCOUNT(p, PC_TO_I(p, mexitcount));
108 
109 	/*
110 	 * Determine overheads.
111 	 */
112 	disable_intr();
113 	p->state = GMON_PROF_HIRES;
114 
115 	p->cputime_overhead = 0;
116 	(void)cputime();
117 	for (i = 0; i < CALIB_SCALE; i++)
118 		p->cputime_overhead += cputime();
119 
120 	(void)cputime();
121 	for (i = 0; i < CALIB_SCALE; i++)
122 #if defined(i386) && __GNUC__ >= 2
123 		/*
124 		 * Underestimate slightly by always calling __mcount, never
125 		 * mcount.
126 		 */
127 		asm("pushl %0; call __mcount; popl %%ecx"
128 		    :
129 		    : "i" (kmstartup)
130 		    : "ax", "bx", "cx", "dx", "memory");
131 #else
132 #error
133 #endif
134 	p->mcount_overhead = KCOUNT(p, PC_TO_I(p, kmstartup));
135 
136 	(void)cputime();
137 	for (i = 0; i < CALIB_SCALE; i++)
138 #if defined(i386) && __GNUC__ >= 2
139 		    asm("call mexitcount; 1:"
140 			: : : "ax", "bx", "cx", "dx", "memory");
141 	asm("movl $1b,%0" : "=rm" (kmstartup_addr));
142 #else
143 #error
144 #endif
145 	p->mexitcount_overhead = KCOUNT(p, PC_TO_I(p, kmstartup_addr));
146 
147 	p->state = GMON_PROF_OFF;
148 	enable_intr();
149 
150 	p->mcount_overhead_sub = p->mcount_overhead - p->cputime_overhead;
151 	p->mexitcount_overhead_sub = p->mexitcount_overhead
152 				     - p->cputime_overhead;
153 	printf("Profiling overheads: %u+%u %u+%u\n",
154 		p->cputime_overhead, p->mcount_overhead_sub,
155 		p->cputime_overhead, p->mexitcount_overhead_sub);
156 	p->cputime_overhead_frac = p->cputime_overhead % CALIB_SCALE;
157 	p->cputime_overhead /= CALIB_SCALE;
158 	p->mcount_overhead_frac = p->mcount_overhead_sub % CALIB_SCALE;
159 	p->mcount_overhead_sub /= CALIB_SCALE;
160 	p->mcount_overhead /= CALIB_SCALE;
161 	p->mexitcount_overhead_frac = p->mexitcount_overhead_sub % CALIB_SCALE;
162 	p->mexitcount_overhead_sub /= CALIB_SCALE;
163 	p->mexitcount_overhead /= CALIB_SCALE;
164 #endif /* GUPROF */
165 }
166 
167 /*
168  * Return kernel profiling information.
169  */
170 static int
171 sysctl_kern_prof SYSCTL_HANDLER_ARGS
172 {
173 	int *name = (int *) arg1;
174 	u_int namelen = arg2;
175 	struct gmonparam *gp = &_gmonparam;
176 	int error;
177 	int state;
178 
179 	/* all sysctl names at this level are terminal */
180 	if (namelen != 1)
181 		return (ENOTDIR);		/* overloaded */
182 
183 	switch (name[0]) {
184 	case GPROF_STATE:
185 		state = gp->state;
186 		error = sysctl_handle_int(oidp, &state, 0, req);
187 		if (error)
188 			return (error);
189 		if (!req->newptr)
190 			return (0);
191 		if (state == GMON_PROF_OFF) {
192 			stopprofclock(&proc0);
193 			gp->state = state;
194 		} else if (state == GMON_PROF_ON) {
195 			gp->profrate = profhz;
196 			gp->state = state;
197 			startprofclock(&proc0);
198 #ifdef GUPROF
199 		} else if (state == GMON_PROF_HIRES) {
200 			gp->profrate = 1193182;	/* XXX */
201 			stopprofclock(&proc0);
202 			gp->state = state;
203 #endif
204 		} else if (state != gp->state)
205 			return (EINVAL);
206 		return (0);
207 	case GPROF_COUNT:
208 		return (sysctl_handle_opaque(oidp,
209 			gp->kcount, gp->kcountsize, req));
210 	case GPROF_FROMS:
211 		return (sysctl_handle_opaque(oidp,
212 			gp->froms, gp->fromssize, req));
213 	case GPROF_TOS:
214 		return (sysctl_handle_opaque(oidp,
215 			gp->tos, gp->tossize, req));
216 	case GPROF_GMONPARAM:
217 		return (sysctl_handle_opaque(oidp, gp, sizeof *gp, req));
218 	default:
219 		return (EOPNOTSUPP);
220 	}
221 	/* NOTREACHED */
222 }
223 
224 SYSCTL_NODE(_kern, KERN_PROF, prof, CTLFLAG_RW, sysctl_kern_prof, "");
225 #endif /* GPROF */
226 
227 /*
228  * Profiling system call.
229  *
230  * The scale factor is a fixed point number with 16 bits of fraction, so that
231  * 1.0 is represented as 0x10000.  A scale factor of 0 turns off profiling.
232  */
233 #ifndef _SYS_SYSPROTO_H_
234 struct profil_args {
235 	caddr_t	samples;
236 	u_int	size;
237 	u_int	offset;
238 	u_int	scale;
239 };
240 #endif
241 /* ARGSUSED */
242 int
243 profil(p, uap, retval)
244 	struct proc *p;
245 	register struct profil_args *uap;
246 	int *retval;
247 {
248 	register struct uprof *upp;
249 	int s;
250 
251 	if (uap->scale > (1 << 16))
252 		return (EINVAL);
253 	if (uap->scale == 0) {
254 		stopprofclock(p);
255 		return (0);
256 	}
257 	upp = &p->p_stats->p_prof;
258 
259 	/* Block profile interrupts while changing state. */
260 	s = splstatclock();
261 	upp->pr_off = uap->offset;
262 	upp->pr_scale = uap->scale;
263 	upp->pr_base = uap->samples;
264 	upp->pr_size = uap->size;
265 	startprofclock(p);
266 	splx(s);
267 
268 	return (0);
269 }
270 
271 /*
272  * Scale is a fixed-point number with the binary point 16 bits
273  * into the value, and is <= 1.0.  pc is at most 32 bits, so the
274  * intermediate result is at most 48 bits.
275  */
276 #define	PC_TO_INDEX(pc, prof) \
277 	((int)(((u_quad_t)((pc) - (prof)->pr_off) * \
278 	    (u_quad_t)((prof)->pr_scale)) >> 16) & ~1)
279 
280 /*
281  * Collect user-level profiling statistics; called on a profiling tick,
282  * when a process is running in user-mode.  This routine may be called
283  * from an interrupt context.  We try to update the user profiling buffers
284  * cheaply with fuswintr() and suswintr().  If that fails, we revert to
285  * an AST that will vector us to trap() with a context in which copyin
286  * and copyout will work.  Trap will then call addupc_task().
287  *
288  * Note that we may (rarely) not get around to the AST soon enough, and
289  * lose profile ticks when the next tick overwrites this one, but in this
290  * case the system is overloaded and the profile is probably already
291  * inaccurate.
292  */
293 void
294 addupc_intr(p, pc, ticks)
295 	register struct proc *p;
296 	register u_long pc;
297 	u_int ticks;
298 {
299 	register struct uprof *prof;
300 	register caddr_t addr;
301 	register u_int i;
302 	register int v;
303 
304 	if (ticks == 0)
305 		return;
306 	prof = &p->p_stats->p_prof;
307 	if (pc < prof->pr_off ||
308 	    (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
309 		return;			/* out of range; ignore */
310 
311 	addr = prof->pr_base + i;
312 	if ((v = fuswintr(addr)) == -1 || suswintr(addr, v + ticks) == -1) {
313 		prof->pr_addr = pc;
314 		prof->pr_ticks = ticks;
315 		need_proftick(p);
316 	}
317 }
318 
319 /*
320  * Much like before, but we can afford to take faults here.  If the
321  * update fails, we simply turn off profiling.
322  */
323 void
324 addupc_task(p, pc, ticks)
325 	register struct proc *p;
326 	register u_long pc;
327 	u_int ticks;
328 {
329 	register struct uprof *prof;
330 	register caddr_t addr;
331 	register u_int i;
332 	u_short v;
333 
334 	/* Testing P_PROFIL may be unnecessary, but is certainly safe. */
335 	if ((p->p_flag & P_PROFIL) == 0 || ticks == 0)
336 		return;
337 
338 	prof = &p->p_stats->p_prof;
339 	if (pc < prof->pr_off ||
340 	    (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
341 		return;
342 
343 	addr = prof->pr_base + i;
344 	if (copyin(addr, (caddr_t)&v, sizeof(v)) == 0) {
345 		v += ticks;
346 		if (copyout((caddr_t)&v, addr, sizeof(v)) == 0)
347 			return;
348 	}
349 	stopprofclock(p);
350 }
351