xref: /netbsd-src/sys/kern/subr_prof.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /*	$NetBSD: subr_prof.c,v 1.47 2014/07/10 21:13:52 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1982, 1986, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)subr_prof.c	8.4 (Berkeley) 2/14/95
32  */
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: subr_prof.c,v 1.47 2014/07/10 21:13:52 christos Exp $");
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/proc.h>
41 #include <sys/mount.h>
42 #include <sys/syscallargs.h>
43 #include <sys/sysctl.h>
44 
45 #include <sys/cpu.h>
46 
47 #ifdef GPROF
48 #include <sys/malloc.h>
49 #include <sys/gmon.h>
50 
51 MALLOC_DEFINE(M_GPROF, "gprof", "kernel profiling buffer");
52 
53 /*
54  * Froms is actually a bunch of unsigned shorts indexing tos
55  */
56 struct gmonparam _gmonparam = { .state = GMON_PROF_OFF };
57 
58 /* Actual start of the kernel text segment. */
59 extern char kernel_text[];
60 
61 extern char etext[];
62 
63 
64 void
65 kmstartup(void)
66 {
67 	char *cp;
68 	struct gmonparam *p = &_gmonparam;
69 	/*
70 	 * Round lowpc and highpc to multiples of the density we're using
71 	 * so the rest of the scaling (here and in gprof) stays in ints.
72 	 */
73 	p->lowpc = rounddown(((u_long)kernel_text),
74 		HISTFRACTION * sizeof(HISTCOUNTER));
75 	p->highpc = roundup((u_long)etext,
76 		HISTFRACTION * sizeof(HISTCOUNTER));
77 	p->textsize = p->highpc - p->lowpc;
78 	printf("Profiling kernel, textsize=%ld [%lx..%lx]\n",
79 	       p->textsize, p->lowpc, p->highpc);
80 	p->kcountsize = p->textsize / HISTFRACTION;
81 	p->hashfraction = HASHFRACTION;
82 	p->fromssize = p->textsize / HASHFRACTION;
83 	p->tolimit = p->textsize * ARCDENSITY / 100;
84 	if (p->tolimit < MINARCS)
85 		p->tolimit = MINARCS;
86 	else if (p->tolimit > MAXARCS)
87 		p->tolimit = MAXARCS;
88 	p->tossize = p->tolimit * sizeof(struct tostruct);
89 	cp = malloc(p->kcountsize + p->fromssize + p->tossize,
90 	    M_GPROF, M_NOWAIT | M_ZERO);
91 	if (cp == 0) {
92 		printf("No memory for profiling.\n");
93 		return;
94 	}
95 	p->tos = (struct tostruct *)cp;
96 	cp += p->tossize;
97 	p->kcount = (u_short *)cp;
98 	cp += p->kcountsize;
99 	p->froms = (u_short *)cp;
100 }
101 
102 /*
103  * Return kernel profiling information.
104  */
105 /*
106  * sysctl helper routine for kern.profiling subtree.  enables/disables
107  * kernel profiling and gives out copies of the profiling data.
108  */
109 static int
110 sysctl_kern_profiling(SYSCTLFN_ARGS)
111 {
112 	struct gmonparam *gp = &_gmonparam;
113 	int error;
114 	struct sysctlnode node;
115 
116 	node = *rnode;
117 
118 	switch (node.sysctl_num) {
119 	case GPROF_STATE:
120 		node.sysctl_data = &gp->state;
121 		break;
122 	case GPROF_COUNT:
123 		node.sysctl_data = gp->kcount;
124 		node.sysctl_size = gp->kcountsize;
125 		break;
126 	case GPROF_FROMS:
127 		node.sysctl_data = gp->froms;
128 		node.sysctl_size = gp->fromssize;
129 		break;
130 	case GPROF_TOS:
131 		node.sysctl_data = gp->tos;
132 		node.sysctl_size = gp->tossize;
133 		break;
134 	case GPROF_GMONPARAM:
135 		node.sysctl_data = gp;
136 		node.sysctl_size = sizeof(*gp);
137 		break;
138 	default:
139 		return (EOPNOTSUPP);
140 	}
141 
142 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
143 	if (error || newp == NULL)
144 		return (error);
145 
146 	if (node.sysctl_num == GPROF_STATE) {
147 		mutex_spin_enter(&proc0.p_stmutex);
148 		if (gp->state == GMON_PROF_OFF)
149 			stopprofclock(&proc0);
150 		else
151 			startprofclock(&proc0);
152 		mutex_spin_exit(&proc0.p_stmutex);
153 	}
154 
155 	return (0);
156 }
157 
158 SYSCTL_SETUP(sysctl_kern_gprof_setup, "sysctl kern.profiling subtree setup")
159 {
160 
161 	sysctl_createv(clog, 0, NULL, NULL,
162 		       CTLFLAG_PERMANENT,
163 		       CTLTYPE_NODE, "profiling",
164 		       SYSCTL_DESCR("Profiling information (available)"),
165 		       NULL, 0, NULL, 0,
166 		       CTL_KERN, KERN_PROF, CTL_EOL);
167 
168 	sysctl_createv(clog, 0, NULL, NULL,
169 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
170 		       CTLTYPE_INT, "state",
171 		       SYSCTL_DESCR("Profiling state"),
172 		       sysctl_kern_profiling, 0, NULL, 0,
173 		       CTL_KERN, KERN_PROF, GPROF_STATE, CTL_EOL);
174 	sysctl_createv(clog, 0, NULL, NULL,
175 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
176 		       CTLTYPE_STRUCT, "count",
177 		       SYSCTL_DESCR("Array of statistical program counters"),
178 		       sysctl_kern_profiling, 0, NULL, 0,
179 		       CTL_KERN, KERN_PROF, GPROF_COUNT, CTL_EOL);
180 	sysctl_createv(clog, 0, NULL, NULL,
181 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
182 		       CTLTYPE_STRUCT, "froms",
183 		       SYSCTL_DESCR("Array indexed by program counter of "
184 				    "call-from points"),
185 		       sysctl_kern_profiling, 0, NULL, 0,
186 		       CTL_KERN, KERN_PROF, GPROF_FROMS, CTL_EOL);
187 	sysctl_createv(clog, 0, NULL, NULL,
188 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
189 		       CTLTYPE_STRUCT, "tos",
190 		       SYSCTL_DESCR("Array of structures describing "
191 				    "destination of calls and their counts"),
192 		       sysctl_kern_profiling, 0, NULL, 0,
193 		       CTL_KERN, KERN_PROF, GPROF_TOS, CTL_EOL);
194 	sysctl_createv(clog, 0, NULL, NULL,
195 		       CTLFLAG_PERMANENT,
196 		       CTLTYPE_STRUCT, "gmonparam",
197 		       SYSCTL_DESCR("Structure giving the sizes of the above "
198 				    "arrays"),
199 		       sysctl_kern_profiling, 0, NULL, 0,
200 		       CTL_KERN, KERN_PROF, GPROF_GMONPARAM, CTL_EOL);
201 }
202 #endif /* GPROF */
203 
204 /*
205  * Profiling system call.
206  *
207  * The scale factor is a fixed point number with 16 bits of fraction, so that
208  * 1.0 is represented as 0x10000.  A scale factor of 0 turns off profiling.
209  */
210 /* ARGSUSED */
211 int
212 sys_profil(struct lwp *l, const struct sys_profil_args *uap, register_t *retval)
213 {
214 	/* {
215 		syscallarg(char *) samples;
216 		syscallarg(size_t) size;
217 		syscallarg(u_long) offset;
218 		syscallarg(u_int) scale;
219 	} */
220 	struct proc *p = l->l_proc;
221 	struct uprof *upp;
222 
223 	if (SCARG(uap, scale) > (1 << 16))
224 		return (EINVAL);
225 	if (SCARG(uap, scale) == 0) {
226 		mutex_spin_enter(&p->p_stmutex);
227 		stopprofclock(p);
228 		mutex_spin_exit(&p->p_stmutex);
229 		return (0);
230 	}
231 	upp = &p->p_stats->p_prof;
232 
233 	/* Block profile interrupts while changing state. */
234 	mutex_spin_enter(&p->p_stmutex);
235 	upp->pr_off = SCARG(uap, offset);
236 	upp->pr_scale = SCARG(uap, scale);
237 	upp->pr_base = SCARG(uap, samples);
238 	upp->pr_size = SCARG(uap, size);
239 	startprofclock(p);
240 	mutex_spin_exit(&p->p_stmutex);
241 
242 	return (0);
243 }
244 
245 /*
246  * Scale is a fixed-point number with the binary point 16 bits
247  * into the value, and is <= 1.0.  pc is at most 32 bits, so the
248  * intermediate result is at most 48 bits.
249  */
250 #define	PC_TO_INDEX(pc, prof) \
251 	((int)(((u_quad_t)((pc) - (prof)->pr_off) * \
252 	    (u_quad_t)((prof)->pr_scale)) >> 16) & ~1)
253 
254 /*
255  * Collect user-level profiling statistics; called on a profiling tick,
256  * when a process is running in user-mode.  This routine may be called
257  * from an interrupt context.  We try to update the user profiling buffers
258  * cheaply with fuswintr() and suswintr().  If that fails, we revert to
259  * an AST that will vector us to trap() with a context in which copyin
260  * and copyout will work.  Trap will then call addupc_task().
261  *
262  * Note that we may (rarely) not get around to the AST soon enough, and
263  * lose profile ticks when the next tick overwrites this one, but in this
264  * case the system is overloaded and the profile is probably already
265  * inaccurate.
266  */
267 void
268 addupc_intr(struct lwp *l, u_long pc)
269 {
270 	struct uprof *prof;
271 	struct proc *p;
272 	void *addr;
273 	u_int i;
274 	int v;
275 
276 	p = l->l_proc;
277 
278 	KASSERT(mutex_owned(&p->p_stmutex));
279 
280 	prof = &p->p_stats->p_prof;
281 	if (pc < prof->pr_off ||
282 	    (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
283 		return;			/* out of range; ignore */
284 
285 	addr = prof->pr_base + i;
286 	mutex_spin_exit(&p->p_stmutex);
287 	if ((v = fuswintr(addr)) == -1 || suswintr(addr, v + 1) == -1) {
288 		/* XXXSMP */
289 		prof->pr_addr = pc;
290 		prof->pr_ticks++;
291 		cpu_need_proftick(l);
292 	}
293 	mutex_spin_enter(&p->p_stmutex);
294 }
295 
296 /*
297  * Much like before, but we can afford to take faults here.  If the
298  * update fails, we simply turn off profiling.
299  */
300 void
301 addupc_task(struct lwp *l, u_long pc, u_int ticks)
302 {
303 	struct uprof *prof;
304 	struct proc *p;
305 	void *addr;
306 	int error;
307 	u_int i;
308 	u_short v;
309 
310 	p = l->l_proc;
311 
312 	if (ticks == 0)
313 		return;
314 
315 	mutex_spin_enter(&p->p_stmutex);
316 	prof = &p->p_stats->p_prof;
317 
318 	/* Testing P_PROFIL may be unnecessary, but is certainly safe. */
319 	if ((p->p_stflag & PST_PROFIL) == 0 || pc < prof->pr_off ||
320 	    (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size) {
321 		mutex_spin_exit(&p->p_stmutex);
322 		return;
323 	}
324 
325 	addr = prof->pr_base + i;
326 	mutex_spin_exit(&p->p_stmutex);
327 	if ((error = copyin(addr, (void *)&v, sizeof(v))) == 0) {
328 		v += ticks;
329 		error = copyout((void *)&v, addr, sizeof(v));
330 	}
331 	if (error != 0) {
332 		mutex_spin_enter(&p->p_stmutex);
333 		stopprofclock(p);
334 		mutex_spin_exit(&p->p_stmutex);
335 	}
336 }
337