]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/subr_prof.c
8f9310b6f14ec8a6c72579aec890cf88b4934282
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
25 * Copyright (c) 1982, 1986, 1993
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * @(#)subr_prof.c 8.3 (Berkeley) 9/23/93
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/kernel.h>
62 #include <sys/proc_internal.h>
64 #include <machine/spl.h>
65 #include <machine/machine_routines.h>
67 #include <sys/mount_internal.h>
68 #include <sys/sysproto.h>
70 #include <mach/mach_types.h>
71 #include <kern/kern_types.h>
72 #include <kern/cpu_number.h>
73 #include <kern/kalloc.h>
75 extern boolean_t
ml_set_interrupts_enabled(boolean_t enable
);
78 #include <sys/malloc.h>
80 #include <kern/mach_header.h>
81 #include <machine/profile.h>
83 lck_spin_t
* mcount_lock
;
84 lck_grp_t
* mcount_lock_grp
;
85 lck_attr_t
* mcount_lock_attr
;
88 * Froms is actually a bunch of unsigned shorts indexing tos
90 struct gmonparam _gmonparam
= { GMON_PROF_OFF
};
93 * This code uses 32 bit mach object segment information from the currently
100 u_long fromssize
, tossize
;
101 struct segment_command
*sgp
; /* 32 bit mach object file segment */
102 struct gmonparam
*p
= &_gmonparam
;
104 sgp
= getsegbyname("__TEXT");
105 p
->lowpc
= (u_long
)sgp
->vmaddr
;
106 p
->highpc
= (u_long
)(sgp
->vmaddr
+ sgp
->vmsize
);
109 * Round lowpc and highpc to multiples of the density we're using
110 * so the rest of the scaling (here and in gprof) stays in ints.
112 p
->lowpc
= ROUNDDOWN(p
->lowpc
, HISTFRACTION
* sizeof(HISTCOUNTER
));
113 p
->highpc
= ROUNDUP(p
->highpc
, HISTFRACTION
* sizeof(HISTCOUNTER
));
114 p
->textsize
= p
->highpc
- p
->lowpc
;
115 printf("Profiling kernel, textsize=%d [0x%08x..0x%08x]\n",
116 p
->textsize
, p
->lowpc
, p
->highpc
);
117 p
->kcountsize
= p
->textsize
/ HISTFRACTION
;
118 p
->hashfraction
= HASHFRACTION
;
119 p
->fromssize
= p
->textsize
/ HASHFRACTION
;
120 p
->tolimit
= p
->textsize
* ARCDENSITY
/ 100;
121 if (p
->tolimit
< MINARCS
)
122 p
->tolimit
= MINARCS
;
123 else if (p
->tolimit
> MAXARCS
)
124 p
->tolimit
= MAXARCS
;
125 p
->tossize
= p
->tolimit
* sizeof(struct tostruct
);
126 /* Why not use MALLOC with M_GPROF ? */
127 cp
= (char *)kalloc(p
->kcountsize
+ p
->fromssize
+ p
->tossize
);
129 printf("No memory for profiling.\n");
132 bzero(cp
, p
->kcountsize
+ p
->tossize
+ p
->fromssize
);
133 p
->tos
= (struct tostruct
*)cp
;
135 p
->kcount
= (u_short
*)cp
;
137 p
->froms
= (u_short
*)cp
;
139 mcount_lock_grp
= lck_grp_alloc_init("MCOUNT", LCK_GRP_ATTR_NULL
);
140 mcount_lock_attr
= lck_attr_alloc_init();
141 //lck_attr_setdebug(mcount_lock_attr);
142 mcount_lock
= lck_spin_alloc_init(mcount_lock_grp
, mcount_lock_attr
);
147 * Return kernel profiling information.
150 sysctl_doprof(int *name
, u_int namelen
, user_addr_t oldp
, size_t *oldlenp
,
151 user_addr_t newp
, size_t newlen
)
153 struct gmonparam
*gp
= &_gmonparam
;
156 /* all sysctl names at this level are terminal */
158 return (ENOTDIR
); /* overloaded */
162 error
= sysctl_int(oldp
, oldlenp
, newp
, newlen
, &gp
->state
);
165 if (gp
->state
== GMON_PROF_OFF
)
166 stopprofclock(kernproc
);
168 startprofclock(kernproc
);
171 return (sysctl_struct(oldp
, oldlenp
, newp
, newlen
,
172 gp
->kcount
, gp
->kcountsize
));
174 return (sysctl_struct(oldp
, oldlenp
, newp
, newlen
,
175 gp
->froms
, gp
->fromssize
));
177 return (sysctl_struct(oldp
, oldlenp
, newp
, newlen
,
178 gp
->tos
, gp
->tossize
));
179 case GPROF_GMONPARAM
:
180 return (sysctl_rdstruct(oldp
, oldlenp
, newp
, gp
, sizeof *gp
));
189 * mcount() called with interrupts disabled.
193 register u_long frompc
,
194 register u_long selfpc
197 unsigned short *frompcindex
;
198 register struct tostruct
*top
, *prevtop
;
199 struct gmonparam
*p
= &_gmonparam
;
200 register long toindex
;
203 * check that we are profiling
204 * and that we aren't recursively invoked.
206 if (p
->state
!= GMON_PROF_ON
)
209 lck_spin_lock(mcount_lock
);
212 * check that frompcindex is a reasonable pc value.
213 * for example: signal catchers get called from the stack,
214 * not from text space. too bad.
217 if (frompc
> p
->textsize
)
220 frompcindex
= &p
->froms
[frompc
/ (p
->hashfraction
* sizeof(*p
->froms
))];
221 toindex
= *frompcindex
;
224 * first time traversing this arc
226 toindex
= ++p
->tos
[0].link
;
227 if (toindex
>= p
->tolimit
) {
228 /* halt further profiling */
231 *frompcindex
= toindex
;
232 top
= &p
->tos
[toindex
];
233 top
->selfpc
= selfpc
;
238 top
= &p
->tos
[toindex
];
239 if (top
->selfpc
== selfpc
) {
241 * arc at front of chain; usual case.
247 * have to go looking down chain for it.
248 * top points to what we are looking at,
249 * prevtop points to previous top.
250 * we know it is not at the head of the chain.
252 for (; /* goto done */; ) {
253 if (top
->link
== 0) {
255 * top is end of the chain and none of the chain
256 * had top->selfpc == selfpc.
257 * so we allocate a new tostruct
258 * and link it to the head of the chain.
260 toindex
= ++p
->tos
[0].link
;
261 if (toindex
>= p
->tolimit
) {
264 top
= &p
->tos
[toindex
];
265 top
->selfpc
= selfpc
;
267 top
->link
= *frompcindex
;
268 *frompcindex
= toindex
;
272 * otherwise, check the next arc on the chain.
275 top
= &p
->tos
[top
->link
];
276 if (top
->selfpc
== selfpc
) {
279 * increment its count
280 * move it to the head of the chain.
283 toindex
= prevtop
->link
;
284 prevtop
->link
= top
->link
;
285 top
->link
= *frompcindex
;
286 *frompcindex
= toindex
;
292 lck_spin_unlock(mcount_lock
);
296 p
->state
= GMON_PROF_ERROR
;
297 lck_spin_unlock(mcount_lock
);
298 printf("mcount: tos overflow\n");
304 #define PROFILE_LOCK(x)
305 #define PROFILE_UNLOCK(x)
308 profil(struct proc
*p
, register struct profil_args
*uap
, __unused register_t
*retval
)
310 struct uprof
*upp
= &p
->p_stats
->p_prof
;
313 if (uap
->pcscale
> (1 << 16))
315 if (uap
->pcscale
== 0) {
320 /* Block profile interrupts while changing state. */
321 s
= ml_set_interrupts_enabled(FALSE
);
323 if (proc_is64bit(p
)) {
324 struct user_uprof
*user_upp
= &p
->p_stats
->user_p_prof
;
325 struct user_uprof
*upc
, *nupc
;
327 PROFILE_LOCK(&user_upp
->pr_lock
);
328 user_upp
->pr_base
= uap
->bufbase
;
329 user_upp
->pr_size
= uap
->bufsize
;
330 user_upp
->pr_off
= uap
->pcoffset
;
331 user_upp
->pr_scale
= uap
->pcscale
;
336 /* remove buffers previously allocated with add_profil() */
337 for (upc
= user_upp
->pr_next
; upc
; upc
= nupc
) {
339 kfree(upc
, sizeof (*upc
));
341 user_upp
->pr_next
= 0;
342 PROFILE_UNLOCK(&user_upp
->pr_lock
);
345 struct uprof
*upc
, *nupc
;
347 PROFILE_LOCK(&upp
->pr_lock
);
348 upp
->pr_base
= CAST_DOWN(caddr_t
, uap
->bufbase
);
349 upp
->pr_size
= uap
->bufsize
;
350 upp
->pr_off
= uap
->pcoffset
;
351 upp
->pr_scale
= uap
->pcscale
;
353 /* remove buffers previously allocated with add_profil() */
354 for (upc
= upp
->pr_next
; upc
; upc
= nupc
) {
356 kfree(upc
, sizeof (struct uprof
));
359 PROFILE_UNLOCK(&upp
->pr_lock
);
363 ml_set_interrupts_enabled(s
);
368 add_profil(struct proc
*p
, register struct add_profil_args
*uap
, __unused register_t
*retval
)
370 struct uprof
*upp
= &p
->p_stats
->p_prof
, *upc
;
371 struct user_uprof
*user_upp
= NULL
, *user_upc
;
373 boolean_t is64bit
= proc_is64bit(p
);
376 user_upp
= &p
->p_stats
->user_p_prof
;
377 if (user_upp
->pr_scale
== 0)
381 if (upp
->pr_scale
== 0)
385 s
= ml_set_interrupts_enabled(FALSE
);
388 user_upc
= (struct user_uprof
*) kalloc(sizeof (struct user_uprof
));
389 user_upc
->pr_base
= uap
->bufbase
;
390 user_upc
->pr_size
= uap
->bufsize
;
391 user_upc
->pr_off
= uap
->pcoffset
;
392 user_upc
->pr_scale
= uap
->pcscale
;
393 PROFILE_LOCK(&user_upp
->pr_lock
);
394 user_upc
->pr_next
= user_upp
->pr_next
;
395 user_upp
->pr_next
= user_upc
;
396 PROFILE_UNLOCK(&user_upp
->pr_lock
);
399 upc
= (struct uprof
*) kalloc(sizeof (struct uprof
));
400 upc
->pr_base
= CAST_DOWN(caddr_t
, uap
->bufbase
);
401 upc
->pr_size
= uap
->bufsize
;
402 upc
->pr_off
= uap
->pcoffset
;
403 upc
->pr_scale
= uap
->pcscale
;
404 PROFILE_LOCK(&upp
->pr_lock
);
405 upc
->pr_next
= upp
->pr_next
;
407 PROFILE_UNLOCK(&upp
->pr_lock
);
410 ml_set_interrupts_enabled(s
);
415 * Scale is a fixed-point number with the binary point 16 bits
416 * into the value, and is <= 1.0. pc is at most 32 bits, so the
417 * intermediate result is at most 48 bits.
419 #define PC_TO_INDEX(pc, prof) \
420 ((int)(((u_quad_t)((pc) - (prof)->pr_off) * \
421 (u_quad_t)((prof)->pr_scale)) >> 16) & ~1)
424 * Collect user-level profiling statistics; called on a profiling tick,
425 * when a process is running in user-mode. We use
426 * an AST that will vector us to trap() with a context in which copyin
427 * and copyout will work. Trap will then call addupc_task().
429 * Note that we may (rarely) not get around to the AST soon enough, and
430 * lose profile ticks when the next tick overwrites this one, but in this
431 * case the system is overloaded and the profile is probably already
434 * We can afford to take faults here. If the
435 * update fails, we simply turn off profiling.
438 addupc_task(p
, pc
, ticks
)
439 register struct proc
*p
;
446 /* Testing P_PROFIL may be unnecessary, but is certainly safe. */
447 if ((p
->p_flag
& P_PROFIL
) == 0 || ticks
== 0)
450 if (proc_is64bit(p
)) {
451 struct user_uprof
*prof
;
454 for (prof
= &p
->p_stats
->user_p_prof
; prof
; prof
= prof
->pr_next
) {
455 off
= PC_TO_INDEX(pc
, prof
);
456 cell
= (prof
->pr_base
+ off
);
457 if (cell
>= prof
->pr_base
&&
458 cell
< (prof
->pr_size
+ prof
->pr_base
)) {
459 if (copyin(cell
, (caddr_t
) &count
, sizeof(count
)) == 0) {
461 if(copyout((caddr_t
) &count
, cell
, sizeof(count
)) == 0)
464 p
->p_stats
->user_p_prof
.pr_scale
= 0;
474 for (prof
= &p
->p_stats
->p_prof
; prof
; prof
= prof
->pr_next
) {
475 off
= PC_TO_INDEX(CAST_DOWN(uint
, pc
),prof
);
476 cell
= (short *)(prof
->pr_base
+ off
);
477 if (cell
>= (short *)prof
->pr_base
&&
478 cell
< (short*)(prof
->pr_size
+ (int) prof
->pr_base
)) {
479 if (copyin(CAST_USER_ADDR_T(cell
), (caddr_t
) &count
, sizeof(count
)) == 0) {
481 if(copyout((caddr_t
) &count
, CAST_USER_ADDR_T(cell
), sizeof(count
)) == 0)
484 p
->p_stats
->p_prof
.pr_scale
= 0;