| 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 | /* |
| 22 | * Copyright 2007 Sun Microsystems, Inc. All rights reserved. |
| 23 | * Use is subject to license terms. |
| 24 | */ |
| 25 | |
| 26 | /* #pragma ident "@(#)profile.c 1.7 07/01/10 SMI" */ |
| 27 | |
| 28 | #ifdef KERNEL |
| 29 | #ifndef _KERNEL |
| 30 | #define _KERNEL /* Solaris vs. Darwin */ |
| 31 | #endif |
| 32 | #endif |
| 33 | |
| 34 | #include <kern/cpu_data.h> |
| 35 | #include <kern/thread.h> |
| 36 | #include <kern/assert.h> |
| 37 | #include <mach/thread_status.h> |
| 38 | |
| 39 | #include <sys/param.h> |
| 40 | #include <sys/systm.h> |
| 41 | #include <sys/errno.h> |
| 42 | #include <sys/stat.h> |
| 43 | #include <sys/ioctl.h> |
| 44 | #include <sys/conf.h> |
| 45 | #include <sys/fcntl.h> |
| 46 | #include <miscfs/devfs/devfs.h> |
| 47 | |
| 48 | #include <sys/dtrace.h> |
| 49 | #include <sys/dtrace_impl.h> |
| 50 | |
| 51 | #include <sys/dtrace_glue.h> |
| 52 | |
| 53 | #include <machine/pal_routines.h> |
| 54 | |
| 55 | #if defined(__x86_64__) |
| 56 | extern x86_saved_state_t *find_kern_regs(thread_t); |
| 57 | #elif defined (__arm__) || defined(__arm64__) |
| 58 | extern struct arm_saved_state *find_kern_regs(thread_t); |
| 59 | #else |
| 60 | #error Unknown architecture |
| 61 | #endif |
| 62 | |
| 63 | #undef ASSERT |
| 64 | #define ASSERT(x) do {} while(0) |
| 65 | |
| 66 | extern void profile_init(void); |
| 67 | |
| 68 | static dtrace_provider_id_t profile_id; |
| 69 | |
| 70 | /* |
| 71 | * Regardless of platform, the stack frames look like this in the case of the |
| 72 | * profile provider: |
| 73 | * |
| 74 | * profile_fire |
| 75 | * cyclic_expire |
| 76 | * cyclic_fire |
| 77 | * [ cbe ] |
| 78 | * [ interrupt code ] |
| 79 | * |
| 80 | * On x86, there are five frames from the generic interrupt code; further, the |
| 81 | * interrupted instruction appears as its own stack frame, giving us a total of |
| 82 | * 10. |
| 83 | * |
| 84 | * On SPARC, the picture is further complicated because the compiler |
| 85 | * optimizes away tail-calls -- so the following frames are optimized away: |
| 86 | * |
| 87 | * profile_fire |
| 88 | * cyclic_expire |
| 89 | * |
| 90 | * This gives three frames. However, on DEBUG kernels, the cyclic_expire |
| 91 | * frame cannot be tail-call eliminated, yielding four frames in this case. |
| 92 | * |
| 93 | * All of the above constraints lead to the mess below. Yes, the profile |
| 94 | * provider should ideally figure this out on-the-fly by hitting one of its own |
| 95 | * probes and then walking its own stack trace. This is complicated, however, |
| 96 | * and the static definition doesn't seem to be overly brittle. Still, we |
| 97 | * allow for a manual override in case we get it completely wrong. |
| 98 | */ |
| 99 | |
| 100 | #if defined(__x86_64__) |
| 101 | #define PROF_ARTIFICIAL_FRAMES 9 |
| 102 | #elif defined(__arm__) || defined(__arm64__) |
| 103 | #define PROF_ARTIFICIAL_FRAMES 8 |
| 104 | #else |
| 105 | #error Unknown architecture |
| 106 | #endif |
| 107 | |
| 108 | #define PROF_NAMELEN 15 |
| 109 | |
| 110 | #define PROF_PROFILE 0 |
| 111 | #define PROF_TICK 1 |
| 112 | #define PROF_PREFIX_PROFILE "profile-" |
| 113 | #define PROF_PREFIX_TICK "tick-" |
| 114 | |
| 115 | typedef struct profile_probe { |
| 116 | char prof_name[PROF_NAMELEN]; |
| 117 | dtrace_id_t prof_id; |
| 118 | int prof_kind; |
| 119 | hrtime_t prof_interval; |
| 120 | cyclic_id_t prof_cyclic; |
| 121 | } profile_probe_t; |
| 122 | |
| 123 | typedef struct profile_probe_percpu { |
| 124 | hrtime_t profc_expected; |
| 125 | hrtime_t profc_interval; |
| 126 | profile_probe_t *profc_probe; |
| 127 | } profile_probe_percpu_t; |
| 128 | |
| 129 | hrtime_t profile_interval_min = NANOSEC / 5000; /* 5000 hz */ |
| 130 | int profile_aframes = 0; /* override */ |
| 131 | |
| 132 | static int profile_rates[] = { |
| 133 | 97, 199, 499, 997, 1999, |
| 134 | 4001, 4999, 0, 0, 0, |
| 135 | 0, 0, 0, 0, 0, |
| 136 | 0, 0, 0, 0, 0 |
| 137 | }; |
| 138 | |
| 139 | static int profile_ticks[] = { |
| 140 | 1, 10, 100, 500, 1000, |
| 141 | 5000, 0, 0, 0, 0, |
| 142 | 0, 0, 0, 0, 0 |
| 143 | }; |
| 144 | |
| 145 | /* |
| 146 | * profile_max defines the upper bound on the number of profile probes that |
| 147 | * can exist (this is to prevent malicious or clumsy users from exhausing |
| 148 | * system resources by creating a slew of profile probes). At mod load time, |
| 149 | * this gets its value from PROFILE_MAX_DEFAULT or profile-max-probes if it's |
| 150 | * present in the profile.conf file. |
| 151 | */ |
| 152 | #define PROFILE_MAX_DEFAULT 1000 /* default max. number of probes */ |
| 153 | static uint32_t profile_max; /* maximum number of profile probes */ |
| 154 | static uint32_t profile_total; /* current number of profile probes */ |
| 155 | |
| 156 | static void |
| 157 | profile_fire(void *arg) |
| 158 | { |
| 159 | profile_probe_percpu_t *pcpu = arg; |
| 160 | profile_probe_t *prof = pcpu->profc_probe; |
| 161 | hrtime_t late; |
| 162 | |
| 163 | late = dtrace_gethrtime() - pcpu->profc_expected; |
| 164 | pcpu->profc_expected += pcpu->profc_interval; |
| 165 | |
| 166 | #if defined(__x86_64__) |
| 167 | x86_saved_state_t *kern_regs = find_kern_regs(current_thread()); |
| 168 | |
| 169 | if (NULL != kern_regs) { |
| 170 | /* Kernel was interrupted. */ |
| 171 | dtrace_probe(prof->prof_id, saved_state64(kern_regs)->isf.rip, 0x0, late, 0, 0); |
| 172 | |
| 173 | } else { |
| 174 | pal_register_cache_state(current_thread(), VALID); |
| 175 | /* Possibly a user interrupt */ |
| 176 | x86_saved_state_t *tagged_regs = (x86_saved_state_t *)find_user_regs(current_thread()); |
| 177 | |
| 178 | if (NULL == tagged_regs) { |
| 179 | /* Too bad, so sad, no useful interrupt state. */ |
| 180 | dtrace_probe(prof->prof_id, 0xcafebabe, |
| 181 | 0x0, late, 0, 0); /* XXX_BOGUS also see profile_usermode() below. */ |
| 182 | } else if (is_saved_state64(tagged_regs)) { |
| 183 | x86_saved_state64_t *regs = saved_state64(tagged_regs); |
| 184 | |
| 185 | dtrace_probe(prof->prof_id, 0x0, regs->isf.rip, late, 0, 0); |
| 186 | } else { |
| 187 | x86_saved_state32_t *regs = saved_state32(tagged_regs); |
| 188 | |
| 189 | dtrace_probe(prof->prof_id, 0x0, regs->eip, late, 0, 0); |
| 190 | } |
| 191 | } |
| 192 | #elif defined(__arm__) |
| 193 | { |
| 194 | arm_saved_state_t *arm_kern_regs = (arm_saved_state_t *) find_kern_regs(current_thread()); |
| 195 | |
| 196 | // We should only come in here from interrupt context, so we should always have valid kernel regs |
| 197 | assert(NULL != arm_kern_regs); |
| 198 | |
| 199 | if (arm_kern_regs->cpsr & 0xF) { |
| 200 | /* Kernel was interrupted. */ |
| 201 | dtrace_probe(prof->prof_id, arm_kern_regs->pc, 0x0, late, 0, 0); |
| 202 | } else { |
| 203 | /* Possibly a user interrupt */ |
| 204 | arm_saved_state_t *arm_user_regs = (arm_saved_state_t *)find_user_regs(current_thread()); |
| 205 | |
| 206 | if (NULL == arm_user_regs) { |
| 207 | /* Too bad, so sad, no useful interrupt state. */ |
| 208 | dtrace_probe(prof->prof_id, 0xcafebabe, 0x0, late, 0, 0); /* XXX_BOGUS also see profile_usermode() below. */ |
| 209 | } else { |
| 210 | dtrace_probe(prof->prof_id, 0x0, arm_user_regs->pc, late, 0, 0); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | #elif defined(__arm64__) |
| 215 | { |
| 216 | arm_saved_state_t *arm_kern_regs = (arm_saved_state_t *) find_kern_regs(current_thread()); |
| 217 | |
| 218 | // We should only come in here from interrupt context, so we should always have valid kernel regs |
| 219 | assert(NULL != arm_kern_regs); |
| 220 | |
| 221 | if (saved_state64(arm_kern_regs)->cpsr & 0xF) { |
| 222 | /* Kernel was interrupted. */ |
| 223 | dtrace_probe(prof->prof_id, saved_state64(arm_kern_regs)->pc, 0x0, late, 0, 0); |
| 224 | } else { |
| 225 | /* Possibly a user interrupt */ |
| 226 | arm_saved_state_t *arm_user_regs = (arm_saved_state_t *)find_user_regs(current_thread()); |
| 227 | |
| 228 | if (NULL == arm_user_regs) { |
| 229 | /* Too bad, so sad, no useful interrupt state. */ |
| 230 | dtrace_probe(prof->prof_id, 0xcafebabe, 0x0, late, 0, 0); /* XXX_BOGUS also see profile_usermode() below. */ |
| 231 | } else { |
| 232 | dtrace_probe(prof->prof_id, 0x0, get_saved_state_pc(arm_user_regs), late, 0, 0); |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | #else |
| 237 | #error Unknown architecture |
| 238 | #endif |
| 239 | } |
| 240 | |
| 241 | static void |
| 242 | profile_tick(void *arg) |
| 243 | { |
| 244 | profile_probe_t *prof = arg; |
| 245 | |
| 246 | #if defined(__x86_64__) |
| 247 | x86_saved_state_t *kern_regs = find_kern_regs(current_thread()); |
| 248 | |
| 249 | if (NULL != kern_regs) { |
| 250 | /* Kernel was interrupted. */ |
| 251 | dtrace_probe(prof->prof_id, saved_state64(kern_regs)->isf.rip, 0x0, 0, 0, 0); |
| 252 | } else { |
| 253 | pal_register_cache_state(current_thread(), VALID); |
| 254 | /* Possibly a user interrupt */ |
| 255 | x86_saved_state_t *tagged_regs = (x86_saved_state_t *)find_user_regs(current_thread()); |
| 256 | |
| 257 | if (NULL == tagged_regs) { |
| 258 | /* Too bad, so sad, no useful interrupt state. */ |
| 259 | dtrace_probe(prof->prof_id, 0xcafebabe, |
| 260 | 0x0, 0, 0, 0); /* XXX_BOGUS also see profile_usermode() below. */ |
| 261 | } else if (is_saved_state64(tagged_regs)) { |
| 262 | x86_saved_state64_t *regs = saved_state64(tagged_regs); |
| 263 | |
| 264 | dtrace_probe(prof->prof_id, 0x0, regs->isf.rip, 0, 0, 0); |
| 265 | } else { |
| 266 | x86_saved_state32_t *regs = saved_state32(tagged_regs); |
| 267 | |
| 268 | dtrace_probe(prof->prof_id, 0x0, regs->eip, 0, 0, 0); |
| 269 | } |
| 270 | } |
| 271 | #elif defined(__arm__) |
| 272 | { |
| 273 | arm_saved_state_t *arm_kern_regs = (arm_saved_state_t *) find_kern_regs(current_thread()); |
| 274 | |
| 275 | if (NULL != arm_kern_regs) { |
| 276 | /* Kernel was interrupted. */ |
| 277 | dtrace_probe(prof->prof_id, arm_kern_regs->pc, 0x0, 0, 0, 0); |
| 278 | } else { |
| 279 | /* Possibly a user interrupt */ |
| 280 | arm_saved_state_t *arm_user_regs = (arm_saved_state_t *)find_user_regs(current_thread()); |
| 281 | |
| 282 | if (NULL == arm_user_regs) { |
| 283 | /* Too bad, so sad, no useful interrupt state. */ |
| 284 | dtrace_probe(prof->prof_id, 0xcafebabe, 0x0, 0, 0, 0); /* XXX_BOGUS also see profile_usermode() below. */ |
| 285 | } else { |
| 286 | dtrace_probe(prof->prof_id, 0x0, arm_user_regs->pc, 0, 0, 0); |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | #elif defined(__arm64__) |
| 291 | { |
| 292 | arm_saved_state_t *arm_kern_regs = (arm_saved_state_t *) find_kern_regs(current_thread()); |
| 293 | |
| 294 | if (NULL != arm_kern_regs) { |
| 295 | /* Kernel was interrupted. */ |
| 296 | dtrace_probe(prof->prof_id, saved_state64(arm_kern_regs)->pc, 0x0, 0, 0, 0); |
| 297 | } else { |
| 298 | /* Possibly a user interrupt */ |
| 299 | arm_saved_state_t *arm_user_regs = (arm_saved_state_t *)find_user_regs(current_thread()); |
| 300 | |
| 301 | if (NULL == arm_user_regs) { |
| 302 | /* Too bad, so sad, no useful interrupt state. */ |
| 303 | dtrace_probe(prof->prof_id, 0xcafebabe, 0x0, 0, 0, 0); /* XXX_BOGUS also see profile_usermode() below. */ |
| 304 | } else { |
| 305 | dtrace_probe(prof->prof_id, 0x0, get_saved_state_pc(arm_user_regs), 0, 0, 0); |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | #else |
| 311 | #error Unknown architecture |
| 312 | #endif |
| 313 | } |
| 314 | |
| 315 | static void |
| 316 | profile_create(hrtime_t interval, const char *name, int kind) |
| 317 | { |
| 318 | profile_probe_t *prof; |
| 319 | |
| 320 | if (interval < profile_interval_min) |
| 321 | return; |
| 322 | |
| 323 | if (dtrace_probe_lookup(profile_id, NULL, NULL, name) != 0) |
| 324 | return; |
| 325 | |
| 326 | atomic_add_32(&profile_total, 1); |
| 327 | if (profile_total > profile_max) { |
| 328 | atomic_add_32(&profile_total, -1); |
| 329 | return; |
| 330 | } |
| 331 | |
| 332 | if (PROF_TICK == kind) |
| 333 | prof = kmem_zalloc(sizeof (profile_probe_t), KM_SLEEP); |
| 334 | else |
| 335 | prof = kmem_zalloc(sizeof (profile_probe_t) + NCPU*sizeof(profile_probe_percpu_t), KM_SLEEP); |
| 336 | |
| 337 | (void) strlcpy(prof->prof_name, name, sizeof(prof->prof_name)); |
| 338 | prof->prof_interval = interval; |
| 339 | prof->prof_cyclic = CYCLIC_NONE; |
| 340 | prof->prof_kind = kind; |
| 341 | prof->prof_id = dtrace_probe_create(profile_id, |
| 342 | NULL, NULL, name, |
| 343 | profile_aframes ? profile_aframes : PROF_ARTIFICIAL_FRAMES, prof); |
| 344 | } |
| 345 | |
| 346 | /*ARGSUSED*/ |
| 347 | static void |
| 348 | profile_provide(void *arg, const dtrace_probedesc_t *desc) |
| 349 | { |
| 350 | #pragma unused(arg) /* __APPLE__ */ |
| 351 | int i, j, rate, kind; |
| 352 | hrtime_t val = 0, mult = 1, len; |
| 353 | const char *name, *suffix = NULL; |
| 354 | |
| 355 | const struct { |
| 356 | const char *prefix; |
| 357 | int kind; |
| 358 | } types[] = { |
| 359 | { PROF_PREFIX_PROFILE, PROF_PROFILE }, |
| 360 | { PROF_PREFIX_TICK, PROF_TICK }, |
| 361 | { NULL, 0 } |
| 362 | }; |
| 363 | |
| 364 | const struct { |
| 365 | const char *name; |
| 366 | hrtime_t mult; |
| 367 | } suffixes[] = { |
| 368 | { "ns" , NANOSEC / NANOSEC }, |
| 369 | { "nsec" , NANOSEC / NANOSEC }, |
| 370 | { "us" , NANOSEC / MICROSEC }, |
| 371 | { "usec" , NANOSEC / MICROSEC }, |
| 372 | { "ms" , NANOSEC / MILLISEC }, |
| 373 | { "msec" , NANOSEC / MILLISEC }, |
| 374 | { "s" , NANOSEC / SEC }, |
| 375 | { "sec" , NANOSEC / SEC }, |
| 376 | { "m" , NANOSEC * (hrtime_t)60 }, |
| 377 | { "min" , NANOSEC * (hrtime_t)60 }, |
| 378 | { "h" , NANOSEC * (hrtime_t)(60 * 60) }, |
| 379 | { "hour" , NANOSEC * (hrtime_t)(60 * 60) }, |
| 380 | { "d" , NANOSEC * (hrtime_t)(24 * 60 * 60) }, |
| 381 | { "day" , NANOSEC * (hrtime_t)(24 * 60 * 60) }, |
| 382 | { "hz" , 0 }, |
| 383 | { NULL, 0 } |
| 384 | }; |
| 385 | |
| 386 | if (desc == NULL) { |
| 387 | char n[PROF_NAMELEN]; |
| 388 | |
| 389 | /* |
| 390 | * If no description was provided, provide all of our probes. |
| 391 | */ |
| 392 | for (i = 0; i < (int)(sizeof (profile_rates) / sizeof (int)); i++) { |
| 393 | if ((rate = profile_rates[i]) == 0) |
| 394 | continue; |
| 395 | |
| 396 | (void) snprintf(n, PROF_NAMELEN, "%s%d" , |
| 397 | PROF_PREFIX_PROFILE, rate); |
| 398 | profile_create(NANOSEC / rate, n, PROF_PROFILE); |
| 399 | } |
| 400 | |
| 401 | for (i = 0; i < (int)(sizeof (profile_ticks) / sizeof (int)); i++) { |
| 402 | if ((rate = profile_ticks[i]) == 0) |
| 403 | continue; |
| 404 | |
| 405 | (void) snprintf(n, PROF_NAMELEN, "%s%d" , |
| 406 | PROF_PREFIX_TICK, rate); |
| 407 | profile_create(NANOSEC / rate, n, PROF_TICK); |
| 408 | } |
| 409 | |
| 410 | return; |
| 411 | } |
| 412 | |
| 413 | name = desc->dtpd_name; |
| 414 | |
| 415 | for (i = 0; types[i].prefix != NULL; i++) { |
| 416 | len = strlen(types[i].prefix); |
| 417 | |
| 418 | if (strncmp(name, types[i].prefix, len) != 0) |
| 419 | continue; |
| 420 | break; |
| 421 | } |
| 422 | |
| 423 | if (types[i].prefix == NULL) |
| 424 | return; |
| 425 | |
| 426 | kind = types[i].kind; |
| 427 | j = strlen(name) - len; |
| 428 | |
| 429 | /* |
| 430 | * We need to start before any time suffix. |
| 431 | */ |
| 432 | for (j = strlen(name); j >= len; j--) { |
| 433 | if (name[j] >= '0' && name[j] <= '9') |
| 434 | break; |
| 435 | suffix = &name[j]; |
| 436 | } |
| 437 | |
| 438 | ASSERT(suffix != NULL); |
| 439 | |
| 440 | /* |
| 441 | * Now determine the numerical value present in the probe name. |
| 442 | */ |
| 443 | for (; j >= len; j--) { |
| 444 | if (name[j] < '0' || name[j] > '9') |
| 445 | return; |
| 446 | |
| 447 | val += (name[j] - '0') * mult; |
| 448 | mult *= (hrtime_t)10; |
| 449 | } |
| 450 | |
| 451 | if (val == 0) |
| 452 | return; |
| 453 | |
| 454 | /* |
| 455 | * Look-up the suffix to determine the multiplier. |
| 456 | */ |
| 457 | for (i = 0, mult = 0; suffixes[i].name != NULL; i++) { |
| 458 | /* APPLE NOTE: Darwin employs size bounded string operations */ |
| 459 | if (strncasecmp(suffixes[i].name, suffix, strlen(suffixes[i].name) + 1) == 0) { |
| 460 | mult = suffixes[i].mult; |
| 461 | break; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | if (suffixes[i].name == NULL && *suffix != '\0') |
| 466 | return; |
| 467 | |
| 468 | if (mult == 0) { |
| 469 | /* |
| 470 | * The default is frequency-per-second. |
| 471 | */ |
| 472 | val = NANOSEC / val; |
| 473 | } else { |
| 474 | val *= mult; |
| 475 | } |
| 476 | |
| 477 | profile_create(val, name, kind); |
| 478 | } |
| 479 | |
| 480 | /*ARGSUSED*/ |
| 481 | static void |
| 482 | profile_destroy(void *arg, dtrace_id_t id, void *parg) |
| 483 | { |
| 484 | #pragma unused(arg,id) /* __APPLE__ */ |
| 485 | profile_probe_t *prof = parg; |
| 486 | |
| 487 | ASSERT(prof->prof_cyclic == CYCLIC_NONE); |
| 488 | |
| 489 | if (prof->prof_kind == PROF_TICK) |
| 490 | kmem_free(prof, sizeof (profile_probe_t)); |
| 491 | else |
| 492 | kmem_free(prof, sizeof (profile_probe_t) + NCPU*sizeof(profile_probe_percpu_t)); |
| 493 | |
| 494 | ASSERT(profile_total >= 1); |
| 495 | atomic_add_32(&profile_total, -1); |
| 496 | } |
| 497 | |
| 498 | /*ARGSUSED*/ |
| 499 | static void |
| 500 | profile_online(void *arg, dtrace_cpu_t *cpu, cyc_handler_t *hdlr, cyc_time_t *when) |
| 501 | { |
| 502 | #pragma unused(cpu) /* __APPLE__ */ |
| 503 | profile_probe_t *prof = arg; |
| 504 | profile_probe_percpu_t *pcpu; |
| 505 | |
| 506 | pcpu = ((profile_probe_percpu_t *)(&(prof[1]))) + cpu_number(); |
| 507 | pcpu->profc_probe = prof; |
| 508 | |
| 509 | hdlr->cyh_func = profile_fire; |
| 510 | hdlr->cyh_arg = pcpu; |
| 511 | hdlr->cyh_level = CY_HIGH_LEVEL; |
| 512 | |
| 513 | when->cyt_interval = prof->prof_interval; |
| 514 | when->cyt_when = dtrace_gethrtime() + when->cyt_interval; |
| 515 | |
| 516 | pcpu->profc_expected = when->cyt_when; |
| 517 | pcpu->profc_interval = when->cyt_interval; |
| 518 | } |
| 519 | |
| 520 | /*ARGSUSED*/ |
| 521 | static void |
| 522 | profile_offline(void *arg, dtrace_cpu_t *cpu, void *oarg) |
| 523 | { |
| 524 | profile_probe_percpu_t *pcpu = oarg; |
| 525 | |
| 526 | ASSERT(pcpu->profc_probe == arg); |
| 527 | #pragma unused(pcpu,arg,cpu) /* __APPLE__ */ |
| 528 | } |
| 529 | |
| 530 | /*ARGSUSED*/ |
| 531 | static int |
| 532 | profile_enable(void *arg, dtrace_id_t id, void *parg) |
| 533 | { |
| 534 | #pragma unused(arg,id) /* __APPLE__ */ |
| 535 | profile_probe_t *prof = parg; |
| 536 | cyc_omni_handler_t omni; |
| 537 | cyc_handler_t hdlr; |
| 538 | cyc_time_t when; |
| 539 | |
| 540 | ASSERT(prof->prof_interval != 0); |
| 541 | ASSERT(MUTEX_HELD(&cpu_lock)); |
| 542 | |
| 543 | if (prof->prof_kind == PROF_TICK) { |
| 544 | hdlr.cyh_func = profile_tick; |
| 545 | hdlr.cyh_arg = prof; |
| 546 | hdlr.cyh_level = CY_HIGH_LEVEL; |
| 547 | |
| 548 | when.cyt_interval = prof->prof_interval; |
| 549 | #if !defined(__APPLE__) |
| 550 | when.cyt_when = dtrace_gethrtime() + when.cyt_interval; |
| 551 | #else |
| 552 | when.cyt_when = 0; |
| 553 | #endif /* __APPLE__ */ |
| 554 | } else { |
| 555 | ASSERT(prof->prof_kind == PROF_PROFILE); |
| 556 | omni.cyo_online = profile_online; |
| 557 | omni.cyo_offline = profile_offline; |
| 558 | omni.cyo_arg = prof; |
| 559 | } |
| 560 | |
| 561 | if (prof->prof_kind == PROF_TICK) { |
| 562 | prof->prof_cyclic = cyclic_timer_add(&hdlr, &when); |
| 563 | } else { |
| 564 | prof->prof_cyclic = (cyclic_id_t)cyclic_add_omni(&omni); /* cast puns cyclic_id_list_t with cyclic_id_t */ |
| 565 | } |
| 566 | |
| 567 | return(0); |
| 568 | } |
| 569 | |
| 570 | /*ARGSUSED*/ |
| 571 | static void |
| 572 | profile_disable(void *arg, dtrace_id_t id, void *parg) |
| 573 | { |
| 574 | profile_probe_t *prof = parg; |
| 575 | |
| 576 | ASSERT(prof->prof_cyclic != CYCLIC_NONE); |
| 577 | ASSERT(MUTEX_HELD(&cpu_lock)); |
| 578 | |
| 579 | #pragma unused(arg,id) |
| 580 | if (prof->prof_kind == PROF_TICK) { |
| 581 | cyclic_timer_remove(prof->prof_cyclic); |
| 582 | } else { |
| 583 | cyclic_remove_omni((cyclic_id_list_t)prof->prof_cyclic); /* cast puns cyclic_id_list_t with cyclic_id_t */ |
| 584 | } |
| 585 | prof->prof_cyclic = CYCLIC_NONE; |
| 586 | } |
| 587 | |
| 588 | static uint64_t |
| 589 | profile_getarg(void *arg, dtrace_id_t id, void *parg, int argno, int aframes) |
| 590 | { |
| 591 | #pragma unused(arg, id, parg, argno, aframes) |
| 592 | /* |
| 593 | * All the required arguments for the profile probe are passed directly |
| 594 | * to dtrace_probe, and we do not go through dtrace_getarg which doesn't |
| 595 | * know how to hop to the kernel stack from the interrupt stack like |
| 596 | * dtrace_getpcstack |
| 597 | */ |
| 598 | return 0; |
| 599 | } |
| 600 | |
| 601 | static void |
| 602 | profile_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc) |
| 603 | { |
| 604 | #pragma unused(arg, id) |
| 605 | profile_probe_t *prof = parg; |
| 606 | const char *argdesc = NULL; |
| 607 | switch (desc->dtargd_ndx) { |
| 608 | case 0: |
| 609 | argdesc = "void*" ; |
| 610 | break; |
| 611 | case 1: |
| 612 | argdesc = "user_addr_t" ; |
| 613 | break; |
| 614 | case 2: |
| 615 | if (prof->prof_kind == PROF_PROFILE) { |
| 616 | argdesc = "hrtime_t" ; |
| 617 | } |
| 618 | break; |
| 619 | } |
| 620 | if (argdesc) { |
| 621 | strlcpy(desc->dtargd_native, argdesc, DTRACE_ARGTYPELEN); |
| 622 | } |
| 623 | else { |
| 624 | desc->dtargd_ndx = DTRACE_ARGNONE; |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | /* |
| 629 | * APPLE NOTE: profile_usermode call not supported. |
| 630 | */ |
| 631 | static int |
| 632 | profile_usermode(void *arg, dtrace_id_t id, void *parg) |
| 633 | { |
| 634 | #pragma unused(arg,id,parg) |
| 635 | return 1; /* XXX_BOGUS */ |
| 636 | } |
| 637 | |
| 638 | static dtrace_pattr_t profile_attr = { |
| 639 | { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON }, |
| 640 | { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_UNKNOWN }, |
| 641 | { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, |
| 642 | { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON }, |
| 643 | { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON }, |
| 644 | }; |
| 645 | |
| 646 | static dtrace_pops_t profile_pops = { |
| 647 | .dtps_provide = profile_provide, |
| 648 | .dtps_provide_module = NULL, |
| 649 | .dtps_enable = profile_enable, |
| 650 | .dtps_disable = profile_disable, |
| 651 | .dtps_suspend = NULL, |
| 652 | .dtps_resume = NULL, |
| 653 | .dtps_getargdesc = profile_getargdesc, |
| 654 | .dtps_getargval = profile_getarg, |
| 655 | .dtps_usermode = profile_usermode, |
| 656 | .dtps_destroy = profile_destroy |
| 657 | }; |
| 658 | |
| 659 | static int |
| 660 | profile_attach(dev_info_t *devi) |
| 661 | { |
| 662 | if (ddi_create_minor_node(devi, "profile" , S_IFCHR, 0, |
| 663 | DDI_PSEUDO, 0) == DDI_FAILURE || |
| 664 | dtrace_register("profile" , &profile_attr, |
| 665 | DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER, NULL, |
| 666 | &profile_pops, NULL, &profile_id) != 0) { |
| 667 | ddi_remove_minor_node(devi, NULL); |
| 668 | return (DDI_FAILURE); |
| 669 | } |
| 670 | |
| 671 | profile_max = PROFILE_MAX_DEFAULT; |
| 672 | |
| 673 | return (DDI_SUCCESS); |
| 674 | } |
| 675 | |
| 676 | /* |
| 677 | * APPLE NOTE: profile_detach not implemented |
| 678 | */ |
| 679 | #if !defined(__APPLE__) |
| 680 | static int |
| 681 | profile_detach(dev_info_t *devi, ddi_detach_cmd_t cmd) |
| 682 | { |
| 683 | switch (cmd) { |
| 684 | case DDI_DETACH: |
| 685 | break; |
| 686 | case DDI_SUSPEND: |
| 687 | return (DDI_SUCCESS); |
| 688 | default: |
| 689 | return (DDI_FAILURE); |
| 690 | } |
| 691 | |
| 692 | if (dtrace_unregister(profile_id) != 0) |
| 693 | return (DDI_FAILURE); |
| 694 | |
| 695 | ddi_remove_minor_node(devi, NULL); |
| 696 | return (DDI_SUCCESS); |
| 697 | } |
| 698 | #endif /* __APPLE__ */ |
| 699 | |
| 700 | d_open_t _profile_open; |
| 701 | |
| 702 | int _profile_open(dev_t dev, int flags, int devtype, struct proc *p) |
| 703 | { |
| 704 | #pragma unused(dev,flags,devtype,p) |
| 705 | return 0; |
| 706 | } |
| 707 | |
| 708 | #define PROFILE_MAJOR -24 /* let the kernel pick the device number */ |
| 709 | |
| 710 | /* |
| 711 | * A struct describing which functions will get invoked for certain |
| 712 | * actions. |
| 713 | */ |
| 714 | static struct cdevsw profile_cdevsw = |
| 715 | { |
| 716 | _profile_open, /* open */ |
| 717 | eno_opcl, /* close */ |
| 718 | eno_rdwrt, /* read */ |
| 719 | eno_rdwrt, /* write */ |
| 720 | eno_ioctl, /* ioctl */ |
| 721 | (stop_fcn_t *)nulldev, /* stop */ |
| 722 | (reset_fcn_t *)nulldev, /* reset */ |
| 723 | NULL, /* tty's */ |
| 724 | eno_select, /* select */ |
| 725 | eno_mmap, /* mmap */ |
| 726 | eno_strat, /* strategy */ |
| 727 | eno_getc, /* getc */ |
| 728 | eno_putc, /* putc */ |
| 729 | 0 /* type */ |
| 730 | }; |
| 731 | |
| 732 | void profile_init( void ) |
| 733 | { |
| 734 | int majdevno = cdevsw_add(PROFILE_MAJOR, &profile_cdevsw); |
| 735 | |
| 736 | if (majdevno < 0) { |
| 737 | printf("profile_init: failed to allocate a major number!\n" ); |
| 738 | return; |
| 739 | } |
| 740 | |
| 741 | profile_attach( (dev_info_t*)(uintptr_t)majdevno); |
| 742 | } |
| 743 | #undef PROFILE_MAJOR |
| 744 | |