]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/subr_prof.c
66fd9b2ec157093a1be12677bbdf109951b6a1f3
[apple/xnu.git] / bsd / kern / subr_prof.c
1 /*
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
31 /*-
32 * Copyright (c) 1982, 1986, 1993
33 * The Regents of the University of California. All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 *
63 * @(#)subr_prof.c 8.3 (Berkeley) 9/23/93
64 */
65
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/kernel.h>
69 #include <sys/proc_internal.h>
70 #include <sys/user.h>
71 #include <machine/spl.h>
72 #include <machine/machine_routines.h>
73
74 #include <sys/mount_internal.h>
75 #include <sys/sysproto.h>
76
77 #include <mach/mach_types.h>
78 #include <kern/kern_types.h>
79 #include <kern/cpu_number.h>
80 #include <kern/kalloc.h>
81
82 extern boolean_t ml_set_interrupts_enabled(boolean_t enable);
83
84 #ifdef GPROF
85 #include <sys/malloc.h>
86 #include <sys/gmon.h>
87 #include <kern/mach_header.h>
88 #include <machine/profile.h>
89
90 lck_spin_t * mcount_lock;
91 lck_grp_t * mcount_lock_grp;
92 lck_attr_t * mcount_lock_attr;
93
94 /*
95 * Froms is actually a bunch of unsigned shorts indexing tos
96 */
97 struct gmonparam _gmonparam = { GMON_PROF_OFF };
98
99 /*
100 * This code uses 32 bit mach object segment information from the currently
101 * running kernel.
102 */
103 void
104 kmstartup(void)
105 {
106 char *cp;
107 u_long fromssize, tossize;
108 struct segment_command *sgp; /* 32 bit mach object file segment */
109 struct gmonparam *p = &_gmonparam;
110
111 sgp = getsegbyname("__TEXT");
112 p->lowpc = (u_long)sgp->vmaddr;
113 p->highpc = (u_long)(sgp->vmaddr + sgp->vmsize);
114
115 /*
116 * Round lowpc and highpc to multiples of the density we're using
117 * so the rest of the scaling (here and in gprof) stays in ints.
118 */
119 p->lowpc = ROUNDDOWN(p->lowpc, HISTFRACTION * sizeof(HISTCOUNTER));
120 p->highpc = ROUNDUP(p->highpc, HISTFRACTION * sizeof(HISTCOUNTER));
121 p->textsize = p->highpc - p->lowpc;
122 printf("Profiling kernel, textsize=%d [0x%08x..0x%08x]\n",
123 p->textsize, p->lowpc, p->highpc);
124 p->kcountsize = p->textsize / HISTFRACTION;
125 p->hashfraction = HASHFRACTION;
126 p->fromssize = p->textsize / HASHFRACTION;
127 p->tolimit = p->textsize * ARCDENSITY / 100;
128 if (p->tolimit < MINARCS)
129 p->tolimit = MINARCS;
130 else if (p->tolimit > MAXARCS)
131 p->tolimit = MAXARCS;
132 p->tossize = p->tolimit * sizeof(struct tostruct);
133 /* Why not use MALLOC with M_GPROF ? */
134 cp = (char *)kalloc(p->kcountsize + p->fromssize + p->tossize);
135 if (cp == 0) {
136 printf("No memory for profiling.\n");
137 return;
138 }
139 bzero(cp, p->kcountsize + p->tossize + p->fromssize);
140 p->tos = (struct tostruct *)cp;
141 cp += p->tossize;
142 p->kcount = (u_short *)cp;
143 cp += p->kcountsize;
144 p->froms = (u_short *)cp;
145
146 mcount_lock_grp = lck_grp_alloc_init("MCOUNT", LCK_GRP_ATTR_NULL);
147 mcount_lock_attr = lck_attr_alloc_init();
148 //lck_attr_setdebug(mcount_lock_attr);
149 mcount_lock = lck_spin_alloc_init(mcount_lock_grp, mcount_lock_attr);
150
151 }
152
153 /*
154 * Return kernel profiling information.
155 */
156 int
157 sysctl_doprof(int *name, u_int namelen, user_addr_t oldp, size_t *oldlenp,
158 user_addr_t newp, size_t newlen)
159 {
160 struct gmonparam *gp = &_gmonparam;
161 int error;
162
163 /* all sysctl names at this level are terminal */
164 if (namelen != 1)
165 return (ENOTDIR); /* overloaded */
166
167 switch (name[0]) {
168 case GPROF_STATE:
169 error = sysctl_int(oldp, oldlenp, newp, newlen, &gp->state);
170 if (error)
171 return (error);
172 if (gp->state == GMON_PROF_OFF)
173 stopprofclock(kernproc);
174 else
175 startprofclock(kernproc);
176 return (0);
177 case GPROF_COUNT:
178 return (sysctl_struct(oldp, oldlenp, newp, newlen,
179 gp->kcount, gp->kcountsize));
180 case GPROF_FROMS:
181 return (sysctl_struct(oldp, oldlenp, newp, newlen,
182 gp->froms, gp->fromssize));
183 case GPROF_TOS:
184 return (sysctl_struct(oldp, oldlenp, newp, newlen,
185 gp->tos, gp->tossize));
186 case GPROF_GMONPARAM:
187 return (sysctl_rdstruct(oldp, oldlenp, newp, gp, sizeof *gp));
188 default:
189 return (ENOTSUP);
190 }
191 /* NOTREACHED */
192 }
193
194
195 /*
196 * mcount() called with interrupts disabled.
197 */
198 void
199 mcount(
200 register u_long frompc,
201 register u_long selfpc
202 )
203 {
204 unsigned short *frompcindex;
205 register struct tostruct *top, *prevtop;
206 struct gmonparam *p = &_gmonparam;
207 register long toindex;
208
209 /*
210 * check that we are profiling
211 * and that we aren't recursively invoked.
212 */
213 if (p->state != GMON_PROF_ON)
214 return;
215
216 lck_spin_lock(mcount_lock);
217
218 /*
219 * check that frompcindex is a reasonable pc value.
220 * for example: signal catchers get called from the stack,
221 * not from text space. too bad.
222 */
223 frompc -= p->lowpc;
224 if (frompc > p->textsize)
225 goto done;
226
227 frompcindex = &p->froms[frompc / (p->hashfraction * sizeof(*p->froms))];
228 toindex = *frompcindex;
229 if (toindex == 0) {
230 /*
231 * first time traversing this arc
232 */
233 toindex = ++p->tos[0].link;
234 if (toindex >= p->tolimit) {
235 /* halt further profiling */
236 goto overflow;
237 }
238 *frompcindex = toindex;
239 top = &p->tos[toindex];
240 top->selfpc = selfpc;
241 top->count = 1;
242 top->link = 0;
243 goto done;
244 }
245 top = &p->tos[toindex];
246 if (top->selfpc == selfpc) {
247 /*
248 * arc at front of chain; usual case.
249 */
250 top->count++;
251 goto done;
252 }
253 /*
254 * have to go looking down chain for it.
255 * top points to what we are looking at,
256 * prevtop points to previous top.
257 * we know it is not at the head of the chain.
258 */
259 for (; /* goto done */; ) {
260 if (top->link == 0) {
261 /*
262 * top is end of the chain and none of the chain
263 * had top->selfpc == selfpc.
264 * so we allocate a new tostruct
265 * and link it to the head of the chain.
266 */
267 toindex = ++p->tos[0].link;
268 if (toindex >= p->tolimit) {
269 goto overflow;
270 }
271 top = &p->tos[toindex];
272 top->selfpc = selfpc;
273 top->count = 1;
274 top->link = *frompcindex;
275 *frompcindex = toindex;
276 goto done;
277 }
278 /*
279 * otherwise, check the next arc on the chain.
280 */
281 prevtop = top;
282 top = &p->tos[top->link];
283 if (top->selfpc == selfpc) {
284 /*
285 * there it is.
286 * increment its count
287 * move it to the head of the chain.
288 */
289 top->count++;
290 toindex = prevtop->link;
291 prevtop->link = top->link;
292 top->link = *frompcindex;
293 *frompcindex = toindex;
294 goto done;
295 }
296
297 }
298 done:
299 lck_spin_unlock(mcount_lock);
300 return;
301
302 overflow:
303 p->state = GMON_PROF_ERROR;
304 lck_spin_unlock(mcount_lock);
305 printf("mcount: tos overflow\n");
306 return;
307 }
308
309 #endif /* GPROF */
310
311 #define PROFILE_LOCK(x)
312 #define PROFILE_UNLOCK(x)
313
314 int
315 profil(struct proc *p, register struct profil_args *uap, __unused register_t *retval)
316 {
317 struct uprof *upp = &p->p_stats->p_prof;
318 int s;
319
320 if (uap->pcscale > (1 << 16))
321 return (EINVAL);
322 if (uap->pcscale == 0) {
323 stopprofclock(p);
324 return (0);
325 }
326
327 /* Block profile interrupts while changing state. */
328 s = ml_set_interrupts_enabled(FALSE);
329
330 if (proc_is64bit(p)) {
331 struct user_uprof *user_upp = &p->p_stats->user_p_prof;
332 struct user_uprof *upc, *nupc;
333
334 PROFILE_LOCK(&user_upp->pr_lock);
335 user_upp->pr_base = uap->bufbase;
336 user_upp->pr_size = uap->bufsize;
337 user_upp->pr_off = uap->pcoffset;
338 user_upp->pr_scale = uap->pcscale;
339 upp->pr_base = NULL;
340 upp->pr_size = 0;
341 upp->pr_scale = 0;
342
343 /* remove buffers previously allocated with add_profil() */
344 for (upc = user_upp->pr_next; upc; upc = nupc) {
345 nupc = upc->pr_next;
346 kfree(upc, sizeof (*upc));
347 }
348 user_upp->pr_next = 0;
349 PROFILE_UNLOCK(&user_upp->pr_lock);
350 }
351 else {
352 struct uprof *upc, *nupc;
353
354 PROFILE_LOCK(&upp->pr_lock);
355 upp->pr_base = CAST_DOWN(caddr_t, uap->bufbase);
356 upp->pr_size = uap->bufsize;
357 upp->pr_off = uap->pcoffset;
358 upp->pr_scale = uap->pcscale;
359
360 /* remove buffers previously allocated with add_profil() */
361 for (upc = upp->pr_next; upc; upc = nupc) {
362 nupc = upc->pr_next;
363 kfree(upc, sizeof (struct uprof));
364 }
365 upp->pr_next = 0;
366 PROFILE_UNLOCK(&upp->pr_lock);
367 }
368
369 startprofclock(p);
370 ml_set_interrupts_enabled(s);
371 return(0);
372 }
373
374 int
375 add_profil(struct proc *p, register struct add_profil_args *uap, __unused register_t *retval)
376 {
377 struct uprof *upp = &p->p_stats->p_prof, *upc;
378 struct user_uprof *user_upp = NULL, *user_upc;
379 int s;
380 boolean_t is64bit = proc_is64bit(p);
381
382 if (is64bit) {
383 user_upp = &p->p_stats->user_p_prof;
384 if (user_upp->pr_scale == 0)
385 return (0);
386 }
387 else {
388 if (upp->pr_scale == 0)
389 return (0);
390 }
391
392 s = ml_set_interrupts_enabled(FALSE);
393
394 if (is64bit) {
395 user_upc = (struct user_uprof *) kalloc(sizeof (struct user_uprof));
396 user_upc->pr_base = uap->bufbase;
397 user_upc->pr_size = uap->bufsize;
398 user_upc->pr_off = uap->pcoffset;
399 user_upc->pr_scale = uap->pcscale;
400 PROFILE_LOCK(&user_upp->pr_lock);
401 user_upc->pr_next = user_upp->pr_next;
402 user_upp->pr_next = user_upc;
403 PROFILE_UNLOCK(&user_upp->pr_lock);
404 }
405 else {
406 upc = (struct uprof *) kalloc(sizeof (struct uprof));
407 upc->pr_base = CAST_DOWN(caddr_t, uap->bufbase);
408 upc->pr_size = uap->bufsize;
409 upc->pr_off = uap->pcoffset;
410 upc->pr_scale = uap->pcscale;
411 PROFILE_LOCK(&upp->pr_lock);
412 upc->pr_next = upp->pr_next;
413 upp->pr_next = upc;
414 PROFILE_UNLOCK(&upp->pr_lock);
415 }
416
417 ml_set_interrupts_enabled(s);
418 return(0);
419 }
420
421 /*
422 * Scale is a fixed-point number with the binary point 16 bits
423 * into the value, and is <= 1.0. pc is at most 32 bits, so the
424 * intermediate result is at most 48 bits.
425 */
426 #define PC_TO_INDEX(pc, prof) \
427 ((int)(((u_quad_t)((pc) - (prof)->pr_off) * \
428 (u_quad_t)((prof)->pr_scale)) >> 16) & ~1)
429
430 /*
431 * Collect user-level profiling statistics; called on a profiling tick,
432 * when a process is running in user-mode. We use
433 * an AST that will vector us to trap() with a context in which copyin
434 * and copyout will work. Trap will then call addupc_task().
435 *
436 * Note that we may (rarely) not get around to the AST soon enough, and
437 * lose profile ticks when the next tick overwrites this one, but in this
438 * case the system is overloaded and the profile is probably already
439 * inaccurate.
440 *
441 * We can afford to take faults here. If the
442 * update fails, we simply turn off profiling.
443 */
444 void
445 addupc_task(p, pc, ticks)
446 register struct proc *p;
447 user_addr_t pc;
448 u_int ticks;
449 {
450 register u_int off;
451 u_short count;
452
453 /* Testing P_PROFIL may be unnecessary, but is certainly safe. */
454 if ((p->p_flag & P_PROFIL) == 0 || ticks == 0)
455 return;
456
457 if (proc_is64bit(p)) {
458 struct user_uprof *prof;
459 user_addr_t cell;
460
461 for (prof = &p->p_stats->user_p_prof; prof; prof = prof->pr_next) {
462 off = PC_TO_INDEX(pc, prof);
463 cell = (prof->pr_base + off);
464 if (cell >= prof->pr_base &&
465 cell < (prof->pr_size + prof->pr_base)) {
466 if (copyin(cell, (caddr_t) &count, sizeof(count)) == 0) {
467 count += ticks;
468 if(copyout((caddr_t) &count, cell, sizeof(count)) == 0)
469 return;
470 }
471 p->p_stats->user_p_prof.pr_scale = 0;
472 stopprofclock(p);
473 break;
474 }
475 }
476 }
477 else {
478 struct uprof *prof;
479 short *cell;
480
481 for (prof = &p->p_stats->p_prof; prof; prof = prof->pr_next) {
482 off = PC_TO_INDEX(CAST_DOWN(uint, pc),prof);
483 cell = (short *)(prof->pr_base + off);
484 if (cell >= (short *)prof->pr_base &&
485 cell < (short*)(prof->pr_size + (int) prof->pr_base)) {
486 if (copyin(CAST_USER_ADDR_T(cell), (caddr_t) &count, sizeof(count)) == 0) {
487 count += ticks;
488 if(copyout((caddr_t) &count, CAST_USER_ADDR_T(cell), sizeof(count)) == 0)
489 return;
490 }
491 p->p_stats->p_prof.pr_scale = 0;
492 stopprofclock(p);
493 break;
494 }
495 }
496 }
497 }