]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/subr_prof.c
49f50b5362fd563e2b7a63a72b6691cdb40dbef4
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
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
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
32 * Copyright (c) 1982, 1986, 1993
33 * The Regents of the University of California. All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
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.
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
63 * @(#)subr_prof.c 8.3 (Berkeley) 9/23/93
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/kernel.h>
69 #include <sys/proc_internal.h>
71 #include <machine/spl.h>
72 #include <machine/machine_routines.h>
74 #include <sys/mount_internal.h>
75 #include <sys/sysproto.h>
77 #include <mach/mach_types.h>
78 #include <kern/kern_types.h>
79 #include <kern/cpu_number.h>
80 #include <kern/kalloc.h>
82 extern boolean_t
ml_set_interrupts_enabled(boolean_t enable
);
85 #include <sys/malloc.h>
87 #include <kern/mach_header.h>
88 #include <machine/profile.h>
90 lck_spin_t
* mcount_lock
;
91 lck_grp_t
* mcount_lock_grp
;
92 lck_attr_t
* mcount_lock_attr
;
95 * Froms is actually a bunch of unsigned shorts indexing tos
97 struct gmonparam _gmonparam
= { GMON_PROF_OFF
};
100 * This code uses 32 bit mach object segment information from the currently
107 u_long fromssize
, tossize
;
108 struct segment_command
*sgp
; /* 32 bit mach object file segment */
109 struct gmonparam
*p
= &_gmonparam
;
111 sgp
= getsegbyname("__TEXT");
112 p
->lowpc
= (u_long
)sgp
->vmaddr
;
113 p
->highpc
= (u_long
)(sgp
->vmaddr
+ sgp
->vmsize
);
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.
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
);
136 printf("No memory for profiling.\n");
139 bzero(cp
, p
->kcountsize
+ p
->tossize
+ p
->fromssize
);
140 p
->tos
= (struct tostruct
*)cp
;
142 p
->kcount
= (u_short
*)cp
;
144 p
->froms
= (u_short
*)cp
;
146 mcount_lock_grp
= lck_grp_alloc_init("MCOUNT", LCK_GRP_ATTR_NULL
);
147 mcount_lock_attr
= lck_attr_alloc_init();
148 mcount_lock
= lck_spin_alloc_init(mcount_lock_grp
, mcount_lock_attr
);
153 * Return kernel profiling information.
156 sysctl_doprof(int *name
, u_int namelen
, user_addr_t oldp
, size_t *oldlenp
,
157 user_addr_t newp
, size_t newlen
)
159 struct gmonparam
*gp
= &_gmonparam
;
162 /* all sysctl names at this level are terminal */
164 return (ENOTDIR
); /* overloaded */
168 error
= sysctl_int(oldp
, oldlenp
, newp
, newlen
, &gp
->state
);
171 if (gp
->state
== GMON_PROF_OFF
)
172 stopprofclock(kernproc
);
174 startprofclock(kernproc
);
177 return (sysctl_struct(oldp
, oldlenp
, newp
, newlen
,
178 gp
->kcount
, gp
->kcountsize
));
180 return (sysctl_struct(oldp
, oldlenp
, newp
, newlen
,
181 gp
->froms
, gp
->fromssize
));
183 return (sysctl_struct(oldp
, oldlenp
, newp
, newlen
,
184 gp
->tos
, gp
->tossize
));
185 case GPROF_GMONPARAM
:
186 return (sysctl_rdstruct(oldp
, oldlenp
, newp
, gp
, sizeof *gp
));
195 * mcount() called with interrupts disabled.
199 register u_long frompc
,
200 register u_long selfpc
203 unsigned short *frompcindex
;
204 register struct tostruct
*top
, *prevtop
;
205 struct gmonparam
*p
= &_gmonparam
;
206 register long toindex
;
209 * check that we are profiling
210 * and that we aren't recursively invoked.
212 if (p
->state
!= GMON_PROF_ON
)
215 lck_spin_lock(mcount_lock
);
218 * check that frompcindex is a reasonable pc value.
219 * for example: signal catchers get called from the stack,
220 * not from text space. too bad.
223 if (frompc
> p
->textsize
)
226 frompcindex
= &p
->froms
[frompc
/ (p
->hashfraction
* sizeof(*p
->froms
))];
227 toindex
= *frompcindex
;
230 * first time traversing this arc
232 toindex
= ++p
->tos
[0].link
;
233 if (toindex
>= p
->tolimit
) {
234 /* halt further profiling */
237 *frompcindex
= toindex
;
238 top
= &p
->tos
[toindex
];
239 top
->selfpc
= selfpc
;
244 top
= &p
->tos
[toindex
];
245 if (top
->selfpc
== selfpc
) {
247 * arc at front of chain; usual case.
253 * have to go looking down chain for it.
254 * top points to what we are looking at,
255 * prevtop points to previous top.
256 * we know it is not at the head of the chain.
258 for (; /* goto done */; ) {
259 if (top
->link
== 0) {
261 * top is end of the chain and none of the chain
262 * had top->selfpc == selfpc.
263 * so we allocate a new tostruct
264 * and link it to the head of the chain.
266 toindex
= ++p
->tos
[0].link
;
267 if (toindex
>= p
->tolimit
) {
270 top
= &p
->tos
[toindex
];
271 top
->selfpc
= selfpc
;
273 top
->link
= *frompcindex
;
274 *frompcindex
= toindex
;
278 * otherwise, check the next arc on the chain.
281 top
= &p
->tos
[top
->link
];
282 if (top
->selfpc
== selfpc
) {
285 * increment its count
286 * move it to the head of the chain.
289 toindex
= prevtop
->link
;
290 prevtop
->link
= top
->link
;
291 top
->link
= *frompcindex
;
292 *frompcindex
= toindex
;
298 lck_spin_unlock(mcount_lock
);
302 p
->state
= GMON_PROF_ERROR
;
303 lck_spin_unlock(mcount_lock
);
304 printf("mcount: tos overflow\n");
310 #define PROFILE_LOCK(x)
311 #define PROFILE_UNLOCK(x)
314 profil(struct proc
*p
, register struct profil_args
*uap
, __unused register_t
*retval
)
316 struct uprof
*upp
= &p
->p_stats
->p_prof
;
319 if (uap
->pcscale
> (1 << 16))
321 if (uap
->pcscale
== 0) {
326 /* Block profile interrupts while changing state. */
327 s
= ml_set_interrupts_enabled(FALSE
);
329 if (proc_is64bit(p
)) {
330 struct user_uprof
*user_upp
= &p
->p_stats
->user_p_prof
;
331 struct user_uprof
*upc
, *nupc
;
333 PROFILE_LOCK(&user_upp
->pr_lock
);
334 user_upp
->pr_base
= uap
->bufbase
;
335 user_upp
->pr_size
= uap
->bufsize
;
336 user_upp
->pr_off
= uap
->pcoffset
;
337 user_upp
->pr_scale
= uap
->pcscale
;
342 /* remove buffers previously allocated with add_profil() */
343 for (upc
= user_upp
->pr_next
; upc
; upc
= nupc
) {
345 kfree(upc
, sizeof (*upc
));
347 user_upp
->pr_next
= 0;
348 PROFILE_UNLOCK(&user_upp
->pr_lock
);
351 struct uprof
*upc
, *nupc
;
353 PROFILE_LOCK(&upp
->pr_lock
);
354 upp
->pr_base
= CAST_DOWN(caddr_t
, uap
->bufbase
);
355 upp
->pr_size
= uap
->bufsize
;
356 upp
->pr_off
= uap
->pcoffset
;
357 upp
->pr_scale
= uap
->pcscale
;
359 /* remove buffers previously allocated with add_profil() */
360 for (upc
= upp
->pr_next
; upc
; upc
= nupc
) {
362 kfree(upc
, sizeof (struct uprof
));
365 PROFILE_UNLOCK(&upp
->pr_lock
);
369 ml_set_interrupts_enabled(s
);
374 add_profil(struct proc
*p
, register struct add_profil_args
*uap
, __unused register_t
*retval
)
376 struct uprof
*upp
= &p
->p_stats
->p_prof
, *upc
;
377 struct user_uprof
*user_upp
= NULL
, *user_upc
;
379 boolean_t is64bit
= proc_is64bit(p
);
382 user_upp
= &p
->p_stats
->user_p_prof
;
383 if (user_upp
->pr_scale
== 0)
387 if (upp
->pr_scale
== 0)
391 s
= ml_set_interrupts_enabled(FALSE
);
394 user_upc
= (struct user_uprof
*) kalloc(sizeof (struct user_uprof
));
395 user_upc
->pr_base
= uap
->bufbase
;
396 user_upc
->pr_size
= uap
->bufsize
;
397 user_upc
->pr_off
= uap
->pcoffset
;
398 user_upc
->pr_scale
= uap
->pcscale
;
399 PROFILE_LOCK(&user_upp
->pr_lock
);
400 user_upc
->pr_next
= user_upp
->pr_next
;
401 user_upp
->pr_next
= user_upc
;
402 PROFILE_UNLOCK(&user_upp
->pr_lock
);
405 upc
= (struct uprof
*) kalloc(sizeof (struct uprof
));
406 upc
->pr_base
= CAST_DOWN(caddr_t
, uap
->bufbase
);
407 upc
->pr_size
= uap
->bufsize
;
408 upc
->pr_off
= uap
->pcoffset
;
409 upc
->pr_scale
= uap
->pcscale
;
410 PROFILE_LOCK(&upp
->pr_lock
);
411 upc
->pr_next
= upp
->pr_next
;
413 PROFILE_UNLOCK(&upp
->pr_lock
);
416 ml_set_interrupts_enabled(s
);
421 * Scale is a fixed-point number with the binary point 16 bits
422 * into the value, and is <= 1.0. pc is at most 32 bits, so the
423 * intermediate result is at most 48 bits.
425 #define PC_TO_INDEX(pc, prof) \
426 ((int)(((u_quad_t)((pc) - (prof)->pr_off) * \
427 (u_quad_t)((prof)->pr_scale)) >> 16) & ~1)
430 * Collect user-level profiling statistics; called on a profiling tick,
431 * when a process is running in user-mode. We use
432 * an AST that will vector us to trap() with a context in which copyin
433 * and copyout will work. Trap will then call addupc_task().
435 * Note that we may (rarely) not get around to the AST soon enough, and
436 * lose profile ticks when the next tick overwrites this one, but in this
437 * case the system is overloaded and the profile is probably already
440 * We can afford to take faults here. If the
441 * update fails, we simply turn off profiling.
444 addupc_task(p
, pc
, ticks
)
445 register struct proc
*p
;
452 /* Testing P_PROFIL may be unnecessary, but is certainly safe. */
453 if ((p
->p_flag
& P_PROFIL
) == 0 || ticks
== 0)
456 if (proc_is64bit(p
)) {
457 struct user_uprof
*prof
;
460 for (prof
= &p
->p_stats
->user_p_prof
; prof
; prof
= prof
->pr_next
) {
461 off
= PC_TO_INDEX(pc
, prof
);
462 cell
= (prof
->pr_base
+ off
);
463 if (cell
>= prof
->pr_base
&&
464 cell
< (prof
->pr_size
+ prof
->pr_base
)) {
465 if (copyin(cell
, (caddr_t
) &count
, sizeof(count
)) == 0) {
467 if(copyout((caddr_t
) &count
, cell
, sizeof(count
)) == 0)
470 p
->p_stats
->user_p_prof
.pr_scale
= 0;
480 for (prof
= &p
->p_stats
->p_prof
; prof
; prof
= prof
->pr_next
) {
481 off
= PC_TO_INDEX(CAST_DOWN(uint
, pc
),prof
);
482 cell
= (short *)(prof
->pr_base
+ off
);
483 if (cell
>= (short *)prof
->pr_base
&&
484 cell
< (short*)(prof
->pr_size
+ (int) prof
->pr_base
)) {
485 if (copyin(CAST_USER_ADDR_T(cell
), (caddr_t
) &count
, sizeof(count
)) == 0) {
487 if(copyout((caddr_t
) &count
, CAST_USER_ADDR_T(cell
), sizeof(count
)) == 0)
490 p
->p_stats
->p_prof
.pr_scale
= 0;