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