subr_prof.c revision 1.43.12.1 1 /* $NetBSD: subr_prof.c,v 1.43.12.1 2008/05/10 23:49:05 wrstuden 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.43.12.1 2008/05/10 23:49:05 wrstuden 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/user.h>
42 #include <sys/mount.h>
43 #include <sys/sa.h>
44 #include <sys/syscallargs.h>
45 #include <sys/sysctl.h>
46
47 #include <sys/cpu.h>
48
49 #ifdef GPROF
50 #include <sys/malloc.h>
51 #include <sys/gmon.h>
52
53 MALLOC_DEFINE(M_GPROF, "gprof", "kernel profiling buffer");
54
55 /*
56 * Froms is actually a bunch of unsigned shorts indexing tos
57 */
58 struct gmonparam _gmonparam = { .state = GMON_PROF_OFF };
59
60 /* Actual start of the kernel text segment. */
61 extern char kernel_text[];
62
63 extern char etext[];
64
65
66 void
67 kmstartup(void)
68 {
69 char *cp;
70 struct gmonparam *p = &_gmonparam;
71 /*
72 * Round lowpc and highpc to multiples of the density we're using
73 * so the rest of the scaling (here and in gprof) stays in ints.
74 */
75 p->lowpc = rounddown(((u_long)kernel_text),
76 HISTFRACTION * sizeof(HISTCOUNTER));
77 p->highpc = roundup((u_long)etext,
78 HISTFRACTION * sizeof(HISTCOUNTER));
79 p->textsize = p->highpc - p->lowpc;
80 printf("Profiling kernel, textsize=%ld [%lx..%lx]\n",
81 p->textsize, p->lowpc, p->highpc);
82 p->kcountsize = p->textsize / HISTFRACTION;
83 p->hashfraction = HASHFRACTION;
84 p->fromssize = p->textsize / HASHFRACTION;
85 p->tolimit = p->textsize * ARCDENSITY / 100;
86 if (p->tolimit < MINARCS)
87 p->tolimit = MINARCS;
88 else if (p->tolimit > MAXARCS)
89 p->tolimit = MAXARCS;
90 p->tossize = p->tolimit * sizeof(struct tostruct);
91 cp = (char *)malloc(p->kcountsize + p->fromssize + p->tossize,
92 M_GPROF, M_NOWAIT | M_ZERO);
93 if (cp == 0) {
94 printf("No memory for profiling.\n");
95 return;
96 }
97 p->tos = (struct tostruct *)cp;
98 cp += p->tossize;
99 p->kcount = (u_short *)cp;
100 cp += p->kcountsize;
101 p->froms = (u_short *)cp;
102 }
103
104 /*
105 * Return kernel profiling information.
106 */
107 /*
108 * sysctl helper routine for kern.profiling subtree. enables/disables
109 * kernel profiling and gives out copies of the profiling data.
110 */
111 static int
112 sysctl_kern_profiling(SYSCTLFN_ARGS)
113 {
114 struct gmonparam *gp = &_gmonparam;
115 int error;
116 struct sysctlnode node;
117
118 node = *rnode;
119
120 switch (node.sysctl_num) {
121 case GPROF_STATE:
122 node.sysctl_data = &gp->state;
123 break;
124 case GPROF_COUNT:
125 node.sysctl_data = gp->kcount;
126 node.sysctl_size = gp->kcountsize;
127 break;
128 case GPROF_FROMS:
129 node.sysctl_data = gp->froms;
130 node.sysctl_size = gp->fromssize;
131 break;
132 case GPROF_TOS:
133 node.sysctl_data = gp->tos;
134 node.sysctl_size = gp->tossize;
135 break;
136 case GPROF_GMONPARAM:
137 node.sysctl_data = gp;
138 node.sysctl_size = sizeof(*gp);
139 break;
140 default:
141 return (EOPNOTSUPP);
142 }
143
144 error = sysctl_lookup(SYSCTLFN_CALL(&node));
145 if (error || newp == NULL)
146 return (error);
147
148 if (node.sysctl_num == GPROF_STATE) {
149 mutex_spin_enter(&proc0.p_stmutex);
150 if (gp->state == GMON_PROF_OFF)
151 stopprofclock(&proc0);
152 else
153 startprofclock(&proc0);
154 mutex_spin_exit(&proc0.p_stmutex);
155 }
156
157 return (0);
158 }
159
160 SYSCTL_SETUP(sysctl_kern_gprof_setup, "sysctl kern.profiling subtree setup")
161 {
162
163 sysctl_createv(clog, 0, NULL, NULL,
164 CTLFLAG_PERMANENT,
165 CTLTYPE_NODE, "kern", NULL,
166 NULL, 0, NULL, 0,
167 CTL_KERN, CTL_EOL);
168 sysctl_createv(clog, 0, NULL, NULL,
169 CTLFLAG_PERMANENT,
170 CTLTYPE_NODE, "profiling",
171 SYSCTL_DESCR("Profiling information (available)"),
172 NULL, 0, NULL, 0,
173 CTL_KERN, KERN_PROF, CTL_EOL);
174
175 sysctl_createv(clog, 0, NULL, NULL,
176 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
177 CTLTYPE_INT, "state",
178 SYSCTL_DESCR("Profiling state"),
179 sysctl_kern_profiling, 0, NULL, 0,
180 CTL_KERN, KERN_PROF, GPROF_STATE, CTL_EOL);
181 sysctl_createv(clog, 0, NULL, NULL,
182 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
183 CTLTYPE_STRUCT, "count",
184 SYSCTL_DESCR("Array of statistical program counters"),
185 sysctl_kern_profiling, 0, NULL, 0,
186 CTL_KERN, KERN_PROF, GPROF_COUNT, CTL_EOL);
187 sysctl_createv(clog, 0, NULL, NULL,
188 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
189 CTLTYPE_STRUCT, "froms",
190 SYSCTL_DESCR("Array indexed by program counter of "
191 "call-from points"),
192 sysctl_kern_profiling, 0, NULL, 0,
193 CTL_KERN, KERN_PROF, GPROF_FROMS, CTL_EOL);
194 sysctl_createv(clog, 0, NULL, NULL,
195 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
196 CTLTYPE_STRUCT, "tos",
197 SYSCTL_DESCR("Array of structures describing "
198 "destination of calls and their counts"),
199 sysctl_kern_profiling, 0, NULL, 0,
200 CTL_KERN, KERN_PROF, GPROF_TOS, CTL_EOL);
201 sysctl_createv(clog, 0, NULL, NULL,
202 CTLFLAG_PERMANENT,
203 CTLTYPE_STRUCT, "gmonparam",
204 SYSCTL_DESCR("Structure giving the sizes of the above "
205 "arrays"),
206 sysctl_kern_profiling, 0, NULL, 0,
207 CTL_KERN, KERN_PROF, GPROF_GMONPARAM, CTL_EOL);
208 }
209 #endif /* GPROF */
210
211 /*
212 * Profiling system call.
213 *
214 * The scale factor is a fixed point number with 16 bits of fraction, so that
215 * 1.0 is represented as 0x10000. A scale factor of 0 turns off profiling.
216 */
217 /* ARGSUSED */
218 int
219 sys_profil(struct lwp *l, const struct sys_profil_args *uap, register_t *retval)
220 {
221 /* {
222 syscallarg(char *) samples;
223 syscallarg(u_int) size;
224 syscallarg(u_int) offset;
225 syscallarg(u_int) scale;
226 } */
227 struct proc *p = l->l_proc;
228 struct uprof *upp;
229
230 if (SCARG(uap, scale) > (1 << 16))
231 return (EINVAL);
232 if (SCARG(uap, scale) == 0) {
233 mutex_spin_enter(&p->p_stmutex);
234 stopprofclock(p);
235 mutex_spin_exit(&p->p_stmutex);
236 return (0);
237 }
238 upp = &p->p_stats->p_prof;
239
240 /* Block profile interrupts while changing state. */
241 mutex_spin_enter(&p->p_stmutex);
242 upp->pr_off = SCARG(uap, offset);
243 upp->pr_scale = SCARG(uap, scale);
244 upp->pr_base = SCARG(uap, samples);
245 upp->pr_size = SCARG(uap, size);
246 startprofclock(p);
247 mutex_spin_exit(&p->p_stmutex);
248
249 return (0);
250 }
251
252 /*
253 * Scale is a fixed-point number with the binary point 16 bits
254 * into the value, and is <= 1.0. pc is at most 32 bits, so the
255 * intermediate result is at most 48 bits.
256 */
257 #define PC_TO_INDEX(pc, prof) \
258 ((int)(((u_quad_t)((pc) - (prof)->pr_off) * \
259 (u_quad_t)((prof)->pr_scale)) >> 16) & ~1)
260
261 /*
262 * Collect user-level profiling statistics; called on a profiling tick,
263 * when a process is running in user-mode. This routine may be called
264 * from an interrupt context. We try to update the user profiling buffers
265 * cheaply with fuswintr() and suswintr(). If that fails, we revert to
266 * an AST that will vector us to trap() with a context in which copyin
267 * and copyout will work. Trap will then call addupc_task().
268 *
269 * Note that we may (rarely) not get around to the AST soon enough, and
270 * lose profile ticks when the next tick overwrites this one, but in this
271 * case the system is overloaded and the profile is probably already
272 * inaccurate.
273 */
274 void
275 addupc_intr(struct lwp *l, u_long pc)
276 {
277 struct uprof *prof;
278 struct proc *p;
279 void *addr;
280 u_int i;
281 int v;
282
283 p = l->l_proc;
284
285 KASSERT(mutex_owned(&p->p_stmutex));
286
287 prof = &p->p_stats->p_prof;
288 if (pc < prof->pr_off ||
289 (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
290 return; /* out of range; ignore */
291
292 addr = prof->pr_base + i;
293 mutex_spin_exit(&p->p_stmutex);
294 if ((v = fuswintr(addr)) == -1 || suswintr(addr, v + 1) == -1) {
295 /* XXXSMP */
296 prof->pr_addr = pc;
297 prof->pr_ticks++;
298 cpu_need_proftick(l);
299 }
300 mutex_spin_enter(&p->p_stmutex);
301 }
302
303 /*
304 * Much like before, but we can afford to take faults here. If the
305 * update fails, we simply turn off profiling.
306 */
307 void
308 addupc_task(struct lwp *l, u_long pc, u_int ticks)
309 {
310 struct uprof *prof;
311 struct proc *p;
312 void *addr;
313 int error;
314 u_int i;
315 u_short v;
316
317 p = l->l_proc;
318
319 if (ticks == 0)
320 return;
321
322 mutex_spin_enter(&p->p_stmutex);
323 prof = &p->p_stats->p_prof;
324
325 /* Testing P_PROFIL may be unnecessary, but is certainly safe. */
326 if ((p->p_stflag & PST_PROFIL) == 0 || pc < prof->pr_off ||
327 (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size) {
328 mutex_spin_exit(&p->p_stmutex);
329 return;
330 }
331
332 addr = prof->pr_base + i;
333 mutex_spin_exit(&p->p_stmutex);
334 if ((error = copyin(addr, (void *)&v, sizeof(v))) == 0) {
335 v += ticks;
336 error = copyout((void *)&v, addr, sizeof(v));
337 }
338 if (error != 0) {
339 mutex_spin_enter(&p->p_stmutex);
340 stopprofclock(p);
341 mutex_spin_exit(&p->p_stmutex);
342 }
343 }
344