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