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