]>
Commit | Line | Data |
---|---|---|
2d21ac55 A |
1 | /* |
2 | * CDDL HEADER START | |
3 | * | |
4 | * The contents of this file are subject to the terms of the | |
5 | * Common Development and Distribution License (the "License"). | |
6 | * You may not use this file except in compliance with the License. | |
7 | * | |
8 | * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE | |
9 | * or http://www.opensolaris.org/os/licensing. | |
10 | * See the License for the specific language governing permissions | |
11 | * and limitations under the License. | |
12 | * | |
13 | * When distributing Covered Code, include this CDDL HEADER in each | |
14 | * file and include the License file at usr/src/OPENSOLARIS.LICENSE. | |
15 | * If applicable, add the following below this CDDL HEADER, with the | |
16 | * fields enclosed by brackets "[]" replaced with your own identifying | |
17 | * information: Portions Copyright [yyyy] [name of copyright owner] | |
18 | * | |
19 | * CDDL HEADER END | |
20 | */ | |
21 | /* | |
b0d623f7 | 22 | * Copyright 2007 Sun Microsystems, Inc. All rights reserved. |
2d21ac55 A |
23 | * Use is subject to license terms. |
24 | */ | |
25 | ||
b0d623f7 | 26 | /* #pragma ident "@(#)profile.c 1.7 07/01/10 SMI" */ |
2d21ac55 A |
27 | |
28 | #if !defined(__APPLE__) | |
29 | #include <sys/errno.h> | |
30 | #include <sys/stat.h> | |
31 | #include <sys/modctl.h> | |
32 | #include <sys/conf.h> | |
33 | #include <sys/systm.h> | |
34 | #include <sys/ddi.h> | |
35 | #include <sys/sunddi.h> | |
36 | #include <sys/cpuvar.h> | |
37 | #include <sys/kmem.h> | |
38 | #include <sys/strsubr.h> | |
39 | #include <sys/dtrace.h> | |
40 | #include <sys/cyclic.h> | |
41 | #include <sys/atomic.h> | |
42 | #else | |
43 | #ifdef KERNEL | |
44 | #ifndef _KERNEL | |
45 | #define _KERNEL /* Solaris vs. Darwin */ | |
46 | #endif | |
47 | #endif | |
48 | ||
49 | #define MACH__POSIX_C_SOURCE_PRIVATE 1 /* pulls in suitable savearea from mach/ppc/thread_status.h */ | |
50 | #include <kern/cpu_data.h> | |
51 | #include <kern/thread.h> | |
6d2010ae | 52 | #include <kern/assert.h> |
2d21ac55 A |
53 | #include <mach/thread_status.h> |
54 | ||
55 | #include <sys/param.h> | |
56 | #include <sys/systm.h> | |
57 | #include <sys/errno.h> | |
58 | #include <sys/stat.h> | |
59 | #include <sys/ioctl.h> | |
60 | #include <sys/conf.h> | |
61 | #include <sys/fcntl.h> | |
62 | #include <miscfs/devfs/devfs.h> | |
63 | ||
64 | #include <sys/dtrace.h> | |
65 | #include <sys/dtrace_impl.h> | |
66 | ||
67 | #include <sys/dtrace_glue.h> | |
68 | ||
6d2010ae A |
69 | #include <machine/pal_routines.h> |
70 | ||
71 | #if defined(__i386__) || defined(__x86_64__) | |
b0d623f7 | 72 | extern x86_saved_state_t *find_kern_regs(thread_t); |
2d21ac55 A |
73 | #else |
74 | #error Unknown architecture | |
75 | #endif | |
76 | ||
77 | #undef ASSERT | |
78 | #define ASSERT(x) do {} while(0) | |
79 | ||
80 | extern void profile_init(void); | |
81 | #endif /* __APPLE__ */ | |
82 | ||
83 | static dev_info_t *profile_devi; | |
84 | static dtrace_provider_id_t profile_id; | |
85 | ||
86 | /* | |
b0d623f7 | 87 | * Regardless of platform, the stack frames look like this in the case of the |
2d21ac55 A |
88 | * profile provider: |
89 | * | |
90 | * profile_fire | |
91 | * cyclic_expire | |
92 | * cyclic_fire | |
93 | * [ cbe ] | |
b0d623f7 | 94 | * [ interrupt code ] |
2d21ac55 | 95 | * |
b0d623f7 A |
96 | * On x86, there are five frames from the generic interrupt code; further, the |
97 | * interrupted instruction appears as its own stack frame, giving us a total of | |
98 | * 10. | |
2d21ac55 A |
99 | * |
100 | * On SPARC, the picture is further complicated because the compiler | |
101 | * optimizes away tail-calls -- so the following frames are optimized away: | |
102 | * | |
103 | * profile_fire | |
104 | * cyclic_expire | |
105 | * | |
106 | * This gives three frames. However, on DEBUG kernels, the cyclic_expire | |
107 | * frame cannot be tail-call eliminated, yielding four frames in this case. | |
108 | * | |
109 | * All of the above constraints lead to the mess below. Yes, the profile | |
b0d623f7 | 110 | * provider should ideally figure this out on-the-fly by hitting one of its own |
2d21ac55 A |
111 | * probes and then walking its own stack trace. This is complicated, however, |
112 | * and the static definition doesn't seem to be overly brittle. Still, we | |
113 | * allow for a manual override in case we get it completely wrong. | |
114 | */ | |
115 | #if !defined(__APPLE__) | |
116 | ||
b0d623f7 A |
117 | #ifdef __x86 |
118 | #define PROF_ARTIFICIAL_FRAMES 10 | |
2d21ac55 A |
119 | #else |
120 | #ifdef __sparc | |
b0d623f7 | 121 | #if DEBUG |
2d21ac55 A |
122 | #define PROF_ARTIFICIAL_FRAMES 4 |
123 | #else | |
124 | #define PROF_ARTIFICIAL_FRAMES 3 | |
125 | #endif | |
126 | #endif | |
127 | #endif | |
2d21ac55 A |
128 | |
129 | #else /* is Mac OS X */ | |
130 | ||
6d2010ae | 131 | #if defined(__i386__) || defined(__x86_64__) |
2d21ac55 | 132 | #define PROF_ARTIFICIAL_FRAMES 9 |
2d21ac55 A |
133 | #else |
134 | #error Unknown architecture | |
135 | #endif | |
136 | ||
137 | #endif /* __APPLE__ */ | |
138 | ||
139 | #define PROF_NAMELEN 15 | |
140 | ||
141 | #define PROF_PROFILE 0 | |
142 | #define PROF_TICK 1 | |
143 | #define PROF_PREFIX_PROFILE "profile-" | |
144 | #define PROF_PREFIX_TICK "tick-" | |
145 | ||
146 | typedef struct profile_probe { | |
147 | char prof_name[PROF_NAMELEN]; | |
148 | dtrace_id_t prof_id; | |
149 | int prof_kind; | |
150 | hrtime_t prof_interval; | |
151 | cyclic_id_t prof_cyclic; | |
152 | } profile_probe_t; | |
153 | ||
154 | typedef struct profile_probe_percpu { | |
155 | hrtime_t profc_expected; | |
156 | hrtime_t profc_interval; | |
157 | profile_probe_t *profc_probe; | |
158 | } profile_probe_percpu_t; | |
159 | ||
160 | hrtime_t profile_interval_min = NANOSEC / 5000; /* 5000 hz */ | |
161 | int profile_aframes = 0; /* override */ | |
162 | ||
163 | static int profile_rates[] = { | |
164 | 97, 199, 499, 997, 1999, | |
165 | 4001, 4999, 0, 0, 0, | |
166 | 0, 0, 0, 0, 0, | |
167 | 0, 0, 0, 0, 0 | |
168 | }; | |
169 | ||
170 | static int profile_ticks[] = { | |
171 | 1, 10, 100, 500, 1000, | |
172 | 5000, 0, 0, 0, 0, | |
173 | 0, 0, 0, 0, 0 | |
174 | }; | |
175 | ||
176 | /* | |
177 | * profile_max defines the upper bound on the number of profile probes that | |
178 | * can exist (this is to prevent malicious or clumsy users from exhausing | |
179 | * system resources by creating a slew of profile probes). At mod load time, | |
180 | * this gets its value from PROFILE_MAX_DEFAULT or profile-max-probes if it's | |
181 | * present in the profile.conf file. | |
182 | */ | |
183 | #define PROFILE_MAX_DEFAULT 1000 /* default max. number of probes */ | |
184 | static uint32_t profile_max; /* maximum number of profile probes */ | |
185 | static uint32_t profile_total; /* current number of profile probes */ | |
186 | ||
187 | static void | |
188 | profile_fire(void *arg) | |
189 | { | |
190 | profile_probe_percpu_t *pcpu = arg; | |
191 | profile_probe_t *prof = pcpu->profc_probe; | |
192 | hrtime_t late; | |
193 | ||
194 | late = dtrace_gethrtime() - pcpu->profc_expected; | |
195 | pcpu->profc_expected += pcpu->profc_interval; | |
196 | ||
197 | #if !defined(__APPLE__) | |
198 | dtrace_probe(prof->prof_id, CPU->cpu_profile_pc, | |
199 | CPU->cpu_profile_upc, late, 0, 0); | |
200 | #else | |
6d2010ae | 201 | #if defined(__i386__) || defined(__x86_64__) |
b0d623f7 | 202 | x86_saved_state_t *kern_regs = find_kern_regs(current_thread()); |
2d21ac55 A |
203 | |
204 | if (NULL != kern_regs) { | |
205 | /* Kernel was interrupted. */ | |
b0d623f7 A |
206 | #if defined(__i386__) |
207 | dtrace_probe(prof->prof_id, saved_state32(kern_regs)->eip, 0x0, 0, 0, 0); | |
208 | #elif defined(__x86_64__) | |
209 | dtrace_probe(prof->prof_id, saved_state64(kern_regs)->isf.rip, 0x0, 0, 0, 0); | |
210 | #else | |
211 | #error Unknown arch | |
212 | #endif | |
2d21ac55 | 213 | } else { |
6d2010ae | 214 | pal_register_cache_state(current_thread(), VALID); |
2d21ac55 A |
215 | /* Possibly a user interrupt */ |
216 | x86_saved_state_t *tagged_regs = (x86_saved_state_t *)find_user_regs(current_thread()); | |
217 | ||
218 | if (NULL == tagged_regs) { | |
219 | /* Too bad, so sad, no useful interrupt state. */ | |
220 | dtrace_probe(prof->prof_id, 0xcafebabe, | |
221 | 0x0, 0, 0, 0); /* XXX_BOGUS also see profile_usermode() below. */ | |
222 | } else if (is_saved_state64(tagged_regs)) { | |
223 | x86_saved_state64_t *regs = saved_state64(tagged_regs); | |
224 | ||
225 | dtrace_probe(prof->prof_id, 0x0, regs->isf.rip, 0, 0, 0); | |
226 | } else { | |
227 | x86_saved_state32_t *regs = saved_state32(tagged_regs); | |
228 | ||
229 | dtrace_probe(prof->prof_id, 0x0, regs->eip, 0, 0, 0); | |
316670eb | 230 | } |
2d21ac55 A |
231 | } |
232 | #else | |
233 | #error Unknown architecture | |
234 | #endif | |
235 | #endif /* __APPLE__ */ | |
236 | } | |
237 | ||
238 | static void | |
239 | profile_tick(void *arg) | |
240 | { | |
241 | profile_probe_t *prof = arg; | |
242 | ||
243 | #if !defined(__APPLE__) | |
244 | dtrace_probe(prof->prof_id, CPU->cpu_profile_pc, | |
245 | CPU->cpu_profile_upc, 0, 0, 0); | |
246 | #else | |
6d2010ae | 247 | #if defined(__i386__) || defined(__x86_64__) |
b0d623f7 | 248 | x86_saved_state_t *kern_regs = find_kern_regs(current_thread()); |
2d21ac55 A |
249 | |
250 | if (NULL != kern_regs) { | |
251 | /* Kernel was interrupted. */ | |
b0d623f7 A |
252 | #if defined(__i386__) |
253 | dtrace_probe(prof->prof_id, saved_state32(kern_regs)->eip, 0x0, 0, 0, 0); | |
254 | #elif defined(__x86_64__) | |
255 | dtrace_probe(prof->prof_id, saved_state64(kern_regs)->isf.rip, 0x0, 0, 0, 0); | |
256 | #else | |
257 | #error Unknown arch | |
258 | #endif | |
2d21ac55 | 259 | } else { |
6d2010ae | 260 | pal_register_cache_state(current_thread(), VALID); |
2d21ac55 A |
261 | /* Possibly a user interrupt */ |
262 | x86_saved_state_t *tagged_regs = (x86_saved_state_t *)find_user_regs(current_thread()); | |
263 | ||
264 | if (NULL == tagged_regs) { | |
265 | /* Too bad, so sad, no useful interrupt state. */ | |
266 | dtrace_probe(prof->prof_id, 0xcafebabe, | |
267 | 0x0, 0, 0, 0); /* XXX_BOGUS also see profile_usermode() below. */ | |
268 | } else if (is_saved_state64(tagged_regs)) { | |
269 | x86_saved_state64_t *regs = saved_state64(tagged_regs); | |
270 | ||
271 | dtrace_probe(prof->prof_id, 0x0, regs->isf.rip, 0, 0, 0); | |
272 | } else { | |
273 | x86_saved_state32_t *regs = saved_state32(tagged_regs); | |
274 | ||
275 | dtrace_probe(prof->prof_id, 0x0, regs->eip, 0, 0, 0); | |
316670eb | 276 | } |
2d21ac55 A |
277 | } |
278 | #else | |
279 | #error Unknown architecture | |
280 | #endif | |
281 | #endif /* __APPLE__ */ | |
282 | } | |
283 | ||
284 | static void | |
285 | profile_create(hrtime_t interval, const char *name, int kind) | |
286 | { | |
287 | profile_probe_t *prof; | |
288 | ||
289 | if (interval < profile_interval_min) | |
290 | return; | |
291 | ||
292 | if (dtrace_probe_lookup(profile_id, NULL, NULL, name) != 0) | |
293 | return; | |
294 | ||
295 | atomic_add_32(&profile_total, 1); | |
296 | if (profile_total > profile_max) { | |
297 | atomic_add_32(&profile_total, -1); | |
298 | return; | |
299 | } | |
300 | ||
301 | #if !defined(__APPLE__) | |
302 | prof = kmem_zalloc(sizeof (profile_probe_t), KM_SLEEP); | |
303 | #else | |
304 | if (PROF_TICK == kind) | |
305 | prof = kmem_zalloc(sizeof (profile_probe_t), KM_SLEEP); | |
306 | else | |
307 | prof = kmem_zalloc(sizeof (profile_probe_t) + NCPU*sizeof(profile_probe_percpu_t), KM_SLEEP); | |
308 | #endif /* __APPLE__ */ | |
309 | (void) strlcpy(prof->prof_name, name, sizeof(prof->prof_name)); | |
310 | prof->prof_interval = interval; | |
311 | prof->prof_cyclic = CYCLIC_NONE; | |
312 | prof->prof_kind = kind; | |
313 | prof->prof_id = dtrace_probe_create(profile_id, | |
314 | NULL, NULL, name, | |
315 | profile_aframes ? profile_aframes : PROF_ARTIFICIAL_FRAMES, prof); | |
316 | } | |
317 | ||
318 | /*ARGSUSED*/ | |
319 | static void | |
320 | profile_provide(void *arg, const dtrace_probedesc_t *desc) | |
321 | { | |
b0d623f7 | 322 | #pragma unused(arg) /* __APPLE__ */ |
2d21ac55 A |
323 | int i, j, rate, kind; |
324 | hrtime_t val = 0, mult = 1, len; | |
325 | const char *name, *suffix = NULL; | |
326 | ||
b0d623f7 | 327 | #if !defined(__APPLE__) |
2d21ac55 A |
328 | const struct { |
329 | char *prefix; | |
330 | int kind; | |
331 | } types[] = { | |
332 | { PROF_PREFIX_PROFILE, PROF_PROFILE }, | |
333 | { PROF_PREFIX_TICK, PROF_TICK }, | |
334 | { NULL, NULL } | |
335 | }; | |
336 | ||
337 | const struct { | |
338 | char *name; | |
339 | hrtime_t mult; | |
340 | } suffixes[] = { | |
341 | { "ns", NANOSEC / NANOSEC }, | |
342 | { "nsec", NANOSEC / NANOSEC }, | |
343 | { "us", NANOSEC / MICROSEC }, | |
344 | { "usec", NANOSEC / MICROSEC }, | |
345 | { "ms", NANOSEC / MILLISEC }, | |
346 | { "msec", NANOSEC / MILLISEC }, | |
347 | { "s", NANOSEC / SEC }, | |
348 | { "sec", NANOSEC / SEC }, | |
349 | { "m", NANOSEC * (hrtime_t)60 }, | |
350 | { "min", NANOSEC * (hrtime_t)60 }, | |
351 | { "h", NANOSEC * (hrtime_t)(60 * 60) }, | |
352 | { "hour", NANOSEC * (hrtime_t)(60 * 60) }, | |
353 | { "d", NANOSEC * (hrtime_t)(24 * 60 * 60) }, | |
354 | { "day", NANOSEC * (hrtime_t)(24 * 60 * 60) }, | |
355 | { "hz", 0 }, | |
2d21ac55 | 356 | { NULL } |
b0d623f7 | 357 | }; |
2d21ac55 | 358 | #else |
b0d623f7 A |
359 | const struct { |
360 | const char *prefix; | |
361 | int kind; | |
362 | } types[] = { | |
363 | { PROF_PREFIX_PROFILE, PROF_PROFILE }, | |
364 | { PROF_PREFIX_TICK, PROF_TICK }, | |
2d21ac55 | 365 | { NULL, 0 } |
2d21ac55 A |
366 | }; |
367 | ||
b0d623f7 A |
368 | const struct { |
369 | const char *name; | |
370 | hrtime_t mult; | |
371 | } suffixes[] = { | |
372 | { "ns", NANOSEC / NANOSEC }, | |
373 | { "nsec", NANOSEC / NANOSEC }, | |
374 | { "us", NANOSEC / MICROSEC }, | |
375 | { "usec", NANOSEC / MICROSEC }, | |
376 | { "ms", NANOSEC / MILLISEC }, | |
377 | { "msec", NANOSEC / MILLISEC }, | |
378 | { "s", NANOSEC / SEC }, | |
379 | { "sec", NANOSEC / SEC }, | |
380 | { "m", NANOSEC * (hrtime_t)60 }, | |
381 | { "min", NANOSEC * (hrtime_t)60 }, | |
382 | { "h", NANOSEC * (hrtime_t)(60 * 60) }, | |
383 | { "hour", NANOSEC * (hrtime_t)(60 * 60) }, | |
384 | { "d", NANOSEC * (hrtime_t)(24 * 60 * 60) }, | |
385 | { "day", NANOSEC * (hrtime_t)(24 * 60 * 60) }, | |
386 | { "hz", 0 }, | |
387 | { NULL, 0 } | |
388 | }; | |
389 | #endif /* __APPLE__ */ | |
390 | ||
391 | ||
2d21ac55 A |
392 | if (desc == NULL) { |
393 | char n[PROF_NAMELEN]; | |
394 | ||
395 | /* | |
396 | * If no description was provided, provide all of our probes. | |
397 | */ | |
b0d623f7 | 398 | #if !defined(__APPLE__) |
2d21ac55 | 399 | for (i = 0; i < sizeof (profile_rates) / sizeof (int); i++) { |
b0d623f7 A |
400 | #else |
401 | for (i = 0; i < (int)(sizeof (profile_rates) / sizeof (int)); i++) { | |
402 | #endif /* __APPLE__ */ | |
2d21ac55 A |
403 | if ((rate = profile_rates[i]) == 0) |
404 | continue; | |
405 | ||
406 | (void) snprintf(n, PROF_NAMELEN, "%s%d", | |
407 | PROF_PREFIX_PROFILE, rate); | |
408 | profile_create(NANOSEC / rate, n, PROF_PROFILE); | |
409 | } | |
410 | ||
b0d623f7 | 411 | #if !defined(__APPLE__) |
2d21ac55 | 412 | for (i = 0; i < sizeof (profile_ticks) / sizeof (int); i++) { |
b0d623f7 A |
413 | #else |
414 | for (i = 0; i < (int)(sizeof (profile_ticks) / sizeof (int)); i++) { | |
415 | #endif /* __APPLE__ */ | |
2d21ac55 A |
416 | if ((rate = profile_ticks[i]) == 0) |
417 | continue; | |
418 | ||
419 | (void) snprintf(n, PROF_NAMELEN, "%s%d", | |
420 | PROF_PREFIX_TICK, rate); | |
421 | profile_create(NANOSEC / rate, n, PROF_TICK); | |
422 | } | |
423 | ||
424 | return; | |
425 | } | |
426 | ||
427 | name = desc->dtpd_name; | |
428 | ||
429 | for (i = 0; types[i].prefix != NULL; i++) { | |
430 | len = strlen(types[i].prefix); | |
431 | ||
432 | if (strncmp(name, types[i].prefix, len) != 0) | |
433 | continue; | |
434 | break; | |
435 | } | |
436 | ||
437 | if (types[i].prefix == NULL) | |
438 | return; | |
439 | ||
440 | kind = types[i].kind; | |
441 | j = strlen(name) - len; | |
442 | ||
443 | /* | |
444 | * We need to start before any time suffix. | |
445 | */ | |
446 | for (j = strlen(name); j >= len; j--) { | |
447 | if (name[j] >= '0' && name[j] <= '9') | |
448 | break; | |
449 | suffix = &name[j]; | |
450 | } | |
451 | ||
452 | ASSERT(suffix != NULL); | |
453 | ||
454 | /* | |
455 | * Now determine the numerical value present in the probe name. | |
456 | */ | |
457 | for (; j >= len; j--) { | |
458 | if (name[j] < '0' || name[j] > '9') | |
459 | return; | |
460 | ||
461 | val += (name[j] - '0') * mult; | |
462 | mult *= (hrtime_t)10; | |
463 | } | |
464 | ||
465 | if (val == 0) | |
466 | return; | |
467 | ||
468 | /* | |
469 | * Look-up the suffix to determine the multiplier. | |
470 | */ | |
471 | for (i = 0, mult = 0; suffixes[i].name != NULL; i++) { | |
b0d623f7 | 472 | #if !defined(__APPLE__) |
2d21ac55 A |
473 | if (strcasecmp(suffixes[i].name, suffix) == 0) { |
474 | mult = suffixes[i].mult; | |
475 | break; | |
476 | } | |
b0d623f7 A |
477 | #else |
478 | if (strncasecmp(suffixes[i].name, suffix, strlen(suffixes[i].name) + 1) == 0) { | |
479 | mult = suffixes[i].mult; | |
480 | break; | |
481 | } | |
482 | #endif /* __APPLE__ */ | |
2d21ac55 A |
483 | } |
484 | ||
485 | if (suffixes[i].name == NULL && *suffix != '\0') | |
486 | return; | |
487 | ||
488 | if (mult == 0) { | |
489 | /* | |
490 | * The default is frequency-per-second. | |
491 | */ | |
492 | val = NANOSEC / val; | |
493 | } else { | |
494 | val *= mult; | |
495 | } | |
496 | ||
497 | profile_create(val, name, kind); | |
498 | } | |
499 | ||
500 | /*ARGSUSED*/ | |
501 | static void | |
502 | profile_destroy(void *arg, dtrace_id_t id, void *parg) | |
503 | { | |
b0d623f7 | 504 | #pragma unused(arg,id) /* __APPLE__ */ |
2d21ac55 A |
505 | profile_probe_t *prof = parg; |
506 | ||
507 | ASSERT(prof->prof_cyclic == CYCLIC_NONE); | |
508 | #if !defined(__APPLE__) | |
509 | kmem_free(prof, sizeof (profile_probe_t)); | |
510 | #else | |
511 | if (prof->prof_kind == PROF_TICK) | |
512 | kmem_free(prof, sizeof (profile_probe_t)); | |
513 | else | |
514 | kmem_free(prof, sizeof (profile_probe_t) + NCPU*sizeof(profile_probe_percpu_t)); | |
515 | #endif /* __APPLE__ */ | |
516 | ||
517 | ASSERT(profile_total >= 1); | |
518 | atomic_add_32(&profile_total, -1); | |
519 | } | |
520 | ||
521 | /*ARGSUSED*/ | |
522 | static void | |
6d2010ae | 523 | profile_online(void *arg, dtrace_cpu_t *cpu, cyc_handler_t *hdlr, cyc_time_t *when) |
2d21ac55 | 524 | { |
b0d623f7 | 525 | #pragma unused(cpu) /* __APPLE__ */ |
2d21ac55 A |
526 | profile_probe_t *prof = arg; |
527 | profile_probe_percpu_t *pcpu; | |
528 | ||
529 | #if !defined(__APPLE__) | |
530 | pcpu = kmem_zalloc(sizeof (profile_probe_percpu_t), KM_SLEEP); | |
531 | #else | |
532 | pcpu = ((profile_probe_percpu_t *)(&(prof[1]))) + cpu_number(); | |
533 | #endif /* __APPLE__ */ | |
534 | pcpu->profc_probe = prof; | |
535 | ||
536 | hdlr->cyh_func = profile_fire; | |
537 | hdlr->cyh_arg = pcpu; | |
538 | hdlr->cyh_level = CY_HIGH_LEVEL; | |
539 | ||
540 | when->cyt_interval = prof->prof_interval; | |
541 | #if !defined(__APPLE__) | |
542 | when->cyt_when = dtrace_gethrtime() + when->cyt_interval; | |
543 | #else | |
544 | when->cyt_when = 0; | |
545 | #endif /* __APPLE__ */ | |
546 | ||
547 | pcpu->profc_expected = when->cyt_when; | |
548 | pcpu->profc_interval = when->cyt_interval; | |
549 | } | |
550 | ||
551 | /*ARGSUSED*/ | |
552 | static void | |
6d2010ae | 553 | profile_offline(void *arg, dtrace_cpu_t *cpu, void *oarg) |
2d21ac55 A |
554 | { |
555 | profile_probe_percpu_t *pcpu = oarg; | |
556 | ||
557 | ASSERT(pcpu->profc_probe == arg); | |
558 | #if !defined(__APPLE__) | |
559 | kmem_free(pcpu, sizeof (profile_probe_percpu_t)); | |
b0d623f7 A |
560 | #else |
561 | #pragma unused(pcpu,arg,cpu) /* __APPLE__ */ | |
2d21ac55 A |
562 | #endif /* __APPLE__ */ |
563 | } | |
564 | ||
565 | /*ARGSUSED*/ | |
6d2010ae | 566 | static int |
2d21ac55 A |
567 | profile_enable(void *arg, dtrace_id_t id, void *parg) |
568 | { | |
b0d623f7 | 569 | #pragma unused(arg,id) /* __APPLE__ */ |
2d21ac55 A |
570 | profile_probe_t *prof = parg; |
571 | cyc_omni_handler_t omni; | |
572 | cyc_handler_t hdlr; | |
573 | cyc_time_t when; | |
574 | ||
575 | ASSERT(prof->prof_interval != 0); | |
576 | ASSERT(MUTEX_HELD(&cpu_lock)); | |
577 | ||
578 | if (prof->prof_kind == PROF_TICK) { | |
579 | hdlr.cyh_func = profile_tick; | |
580 | hdlr.cyh_arg = prof; | |
581 | hdlr.cyh_level = CY_HIGH_LEVEL; | |
582 | ||
583 | when.cyt_interval = prof->prof_interval; | |
584 | #if !defined(__APPLE__) | |
585 | when.cyt_when = dtrace_gethrtime() + when.cyt_interval; | |
586 | #else | |
587 | when.cyt_when = 0; | |
588 | #endif /* __APPLE__ */ | |
589 | } else { | |
590 | ASSERT(prof->prof_kind == PROF_PROFILE); | |
591 | omni.cyo_online = profile_online; | |
592 | omni.cyo_offline = profile_offline; | |
593 | omni.cyo_arg = prof; | |
594 | } | |
595 | ||
596 | #if !defined(__APPLE__) | |
597 | if (prof->prof_kind == PROF_TICK) { | |
598 | prof->prof_cyclic = cyclic_add(&hdlr, &when); | |
599 | } else { | |
600 | prof->prof_cyclic = cyclic_add_omni(&omni); | |
601 | } | |
602 | #else | |
603 | if (prof->prof_kind == PROF_TICK) { | |
604 | prof->prof_cyclic = cyclic_timer_add(&hdlr, &when); | |
605 | } else { | |
606 | prof->prof_cyclic = (cyclic_id_t)cyclic_add_omni(&omni); /* cast puns cyclic_id_list_t with cyclic_id_t */ | |
607 | } | |
608 | #endif /* __APPLE__ */ | |
6d2010ae | 609 | return(0); |
2d21ac55 A |
610 | } |
611 | ||
612 | /*ARGSUSED*/ | |
613 | static void | |
614 | profile_disable(void *arg, dtrace_id_t id, void *parg) | |
615 | { | |
616 | profile_probe_t *prof = parg; | |
617 | ||
618 | ASSERT(prof->prof_cyclic != CYCLIC_NONE); | |
619 | ASSERT(MUTEX_HELD(&cpu_lock)); | |
620 | ||
621 | #if !defined(__APPLE__) | |
622 | cyclic_remove(prof->prof_cyclic); | |
623 | #else | |
b0d623f7 | 624 | #pragma unused(arg,id) |
2d21ac55 A |
625 | if (prof->prof_kind == PROF_TICK) { |
626 | cyclic_timer_remove(prof->prof_cyclic); | |
627 | } else { | |
628 | cyclic_remove_omni((cyclic_id_list_t)prof->prof_cyclic); /* cast puns cyclic_id_list_t with cyclic_id_t */ | |
629 | } | |
630 | #endif /* __APPLE__ */ | |
631 | prof->prof_cyclic = CYCLIC_NONE; | |
632 | } | |
633 | ||
634 | #if !defined(__APPLE__) | |
635 | /*ARGSUSED*/ | |
636 | static int | |
637 | profile_usermode(void *arg, dtrace_id_t id, void *parg) | |
638 | { | |
639 | return (CPU->cpu_profile_pc == 0); | |
640 | } | |
641 | #else | |
642 | static int | |
643 | profile_usermode(void *arg, dtrace_id_t id, void *parg) | |
644 | { | |
645 | #pragma unused(arg,id,parg) | |
646 | return 1; /* XXX_BOGUS */ | |
647 | } | |
648 | #endif /* __APPLE__ */ | |
649 | ||
650 | static dtrace_pattr_t profile_attr = { | |
651 | { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON }, | |
652 | { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_UNKNOWN }, | |
653 | { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, | |
654 | { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON }, | |
655 | { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON }, | |
656 | }; | |
657 | ||
658 | static dtrace_pops_t profile_pops = { | |
659 | profile_provide, | |
660 | NULL, | |
661 | profile_enable, | |
662 | profile_disable, | |
663 | NULL, | |
664 | NULL, | |
665 | NULL, | |
666 | NULL, | |
667 | profile_usermode, | |
668 | profile_destroy | |
669 | }; | |
670 | ||
671 | static int | |
672 | profile_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) | |
673 | { | |
674 | switch (cmd) { | |
675 | case DDI_ATTACH: | |
676 | break; | |
677 | case DDI_RESUME: | |
678 | return (DDI_SUCCESS); | |
679 | default: | |
680 | return (DDI_FAILURE); | |
681 | } | |
682 | ||
b0d623f7 | 683 | #if !defined(__APPLE__) |
2d21ac55 A |
684 | if (ddi_create_minor_node(devi, "profile", S_IFCHR, 0, |
685 | DDI_PSEUDO, NULL) == DDI_FAILURE || | |
686 | dtrace_register("profile", &profile_attr, | |
687 | DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER, NULL, | |
688 | &profile_pops, NULL, &profile_id) != 0) { | |
689 | ddi_remove_minor_node(devi, NULL); | |
690 | return (DDI_FAILURE); | |
691 | } | |
b0d623f7 | 692 | |
2d21ac55 A |
693 | profile_max = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, |
694 | "profile-max-probes", PROFILE_MAX_DEFAULT); | |
695 | #else | |
b0d623f7 A |
696 | if (ddi_create_minor_node(devi, "profile", S_IFCHR, 0, |
697 | DDI_PSEUDO, 0) == DDI_FAILURE || | |
698 | dtrace_register("profile", &profile_attr, | |
699 | DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER, NULL, | |
700 | &profile_pops, NULL, &profile_id) != 0) { | |
701 | ddi_remove_minor_node(devi, NULL); | |
702 | return (DDI_FAILURE); | |
703 | } | |
704 | ||
2d21ac55 A |
705 | profile_max = PROFILE_MAX_DEFAULT; |
706 | #endif /* __APPLE__ */ | |
707 | ||
708 | ddi_report_dev(devi); | |
709 | profile_devi = devi; | |
710 | return (DDI_SUCCESS); | |
711 | } | |
712 | ||
713 | #if !defined(__APPLE__) | |
714 | static int | |
715 | profile_detach(dev_info_t *devi, ddi_detach_cmd_t cmd) | |
716 | { | |
717 | switch (cmd) { | |
718 | case DDI_DETACH: | |
719 | break; | |
720 | case DDI_SUSPEND: | |
721 | return (DDI_SUCCESS); | |
722 | default: | |
723 | return (DDI_FAILURE); | |
724 | } | |
725 | ||
726 | if (dtrace_unregister(profile_id) != 0) | |
727 | return (DDI_FAILURE); | |
728 | ||
729 | ddi_remove_minor_node(devi, NULL); | |
730 | return (DDI_SUCCESS); | |
731 | } | |
732 | ||
733 | /*ARGSUSED*/ | |
734 | static int | |
735 | profile_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) | |
736 | { | |
737 | int error; | |
738 | ||
739 | switch (infocmd) { | |
740 | case DDI_INFO_DEVT2DEVINFO: | |
741 | *result = (void *)profile_devi; | |
742 | error = DDI_SUCCESS; | |
743 | break; | |
744 | case DDI_INFO_DEVT2INSTANCE: | |
745 | *result = (void *)0; | |
746 | error = DDI_SUCCESS; | |
747 | break; | |
748 | default: | |
749 | error = DDI_FAILURE; | |
750 | } | |
751 | return (error); | |
752 | } | |
753 | ||
754 | /*ARGSUSED*/ | |
755 | static int | |
756 | profile_open(dev_t *devp, int flag, int otyp, cred_t *cred_p) | |
757 | { | |
758 | return (0); | |
759 | } | |
760 | ||
761 | static struct cb_ops profile_cb_ops = { | |
762 | profile_open, /* open */ | |
763 | nodev, /* close */ | |
764 | nulldev, /* strategy */ | |
765 | nulldev, /* print */ | |
766 | nodev, /* dump */ | |
767 | nodev, /* read */ | |
768 | nodev, /* write */ | |
769 | nodev, /* ioctl */ | |
770 | nodev, /* devmap */ | |
771 | nodev, /* mmap */ | |
772 | nodev, /* segmap */ | |
773 | nochpoll, /* poll */ | |
774 | ddi_prop_op, /* cb_prop_op */ | |
775 | 0, /* streamtab */ | |
776 | D_NEW | D_MP /* Driver compatibility flag */ | |
777 | }; | |
778 | ||
779 | static struct dev_ops profile_ops = { | |
780 | DEVO_REV, /* devo_rev, */ | |
781 | 0, /* refcnt */ | |
782 | profile_info, /* get_dev_info */ | |
783 | nulldev, /* identify */ | |
784 | nulldev, /* probe */ | |
785 | profile_attach, /* attach */ | |
786 | profile_detach, /* detach */ | |
787 | nodev, /* reset */ | |
788 | &profile_cb_ops, /* driver operations */ | |
789 | NULL, /* bus operations */ | |
790 | nodev /* dev power */ | |
791 | }; | |
792 | ||
793 | /* | |
794 | * Module linkage information for the kernel. | |
795 | */ | |
796 | static struct modldrv modldrv = { | |
797 | &mod_driverops, /* module type (this is a pseudo driver) */ | |
798 | "Profile Interrupt Tracing", /* name of module */ | |
799 | &profile_ops, /* driver ops */ | |
800 | }; | |
801 | ||
802 | static struct modlinkage modlinkage = { | |
803 | MODREV_1, | |
804 | (void *)&modldrv, | |
805 | NULL | |
806 | }; | |
807 | ||
808 | int | |
809 | _init(void) | |
810 | { | |
811 | return (mod_install(&modlinkage)); | |
812 | } | |
813 | ||
814 | int | |
815 | _info(struct modinfo *modinfop) | |
816 | { | |
817 | return (mod_info(&modlinkage, modinfop)); | |
818 | } | |
819 | ||
820 | int | |
821 | _fini(void) | |
822 | { | |
823 | return (mod_remove(&modlinkage)); | |
824 | } | |
825 | #else | |
826 | d_open_t _profile_open; | |
827 | ||
828 | int _profile_open(dev_t dev, int flags, int devtype, struct proc *p) | |
829 | { | |
830 | #pragma unused(dev,flags,devtype,p) | |
831 | return 0; | |
832 | } | |
833 | ||
834 | #define PROFILE_MAJOR -24 /* let the kernel pick the device number */ | |
835 | ||
836 | /* | |
837 | * A struct describing which functions will get invoked for certain | |
838 | * actions. | |
839 | */ | |
840 | static struct cdevsw profile_cdevsw = | |
841 | { | |
842 | _profile_open, /* open */ | |
843 | eno_opcl, /* close */ | |
844 | eno_rdwrt, /* read */ | |
845 | eno_rdwrt, /* write */ | |
846 | eno_ioctl, /* ioctl */ | |
847 | (stop_fcn_t *)nulldev, /* stop */ | |
848 | (reset_fcn_t *)nulldev, /* reset */ | |
849 | NULL, /* tty's */ | |
850 | eno_select, /* select */ | |
851 | eno_mmap, /* mmap */ | |
852 | eno_strat, /* strategy */ | |
853 | eno_getc, /* getc */ | |
854 | eno_putc, /* putc */ | |
855 | 0 /* type */ | |
856 | }; | |
857 | ||
858 | static int gProfileInited = 0; | |
859 | ||
860 | void profile_init( void ) | |
861 | { | |
862 | if (0 == gProfileInited) | |
863 | { | |
864 | int majdevno = cdevsw_add(PROFILE_MAJOR, &profile_cdevsw); | |
865 | ||
866 | if (majdevno < 0) { | |
867 | printf("profile_init: failed to allocate a major number!\n"); | |
868 | gProfileInited = 0; | |
869 | return; | |
870 | } | |
871 | ||
b0d623f7 | 872 | profile_attach( (dev_info_t *)(uintptr_t)majdevno, DDI_ATTACH ); |
2d21ac55 A |
873 | |
874 | gProfileInited = 1; | |
875 | } else | |
876 | panic("profile_init: called twice!\n"); | |
877 | } | |
878 | #undef PROFILE_MAJOR | |
879 | #endif /* __APPLE__ */ |