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 * Portions Copyright 2006-2008 John Birrell jb@freebsd.org 22 * 23 * $FreeBSD$ 24 * 25 */ 26 27 /* 28 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 29 * Use is subject to license terms. 30 */ 31 32 #include <sys/cdefs.h> 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/conf.h> 36 #include <sys/cpuvar.h> 37 #include <sys/endian.h> 38 #include <sys/fcntl.h> 39 #include <sys/filio.h> 40 #include <sys/kdb.h> 41 #include <sys/kernel.h> 42 #include <sys/kmem.h> 43 #include <sys/kthread.h> 44 #include <sys/limits.h> 45 #include <sys/linker.h> 46 #include <sys/lock.h> 47 #include <sys/malloc.h> 48 #include <sys/module.h> 49 #include <sys/mutex.h> 50 #include <sys/poll.h> 51 #include <sys/proc.h> 52 #include <sys/selinfo.h> 53 #include <sys/smp.h> 54 #include <sys/sysctl.h> 55 #include <sys/uio.h> 56 #include <sys/unistd.h> 57 #include <machine/cpu.h> 58 #include <machine/stdarg.h> 59 60 #include <sys/dtrace.h> 61 #include <sys/dtrace_bsd.h> 62 63 #define PROF_NAMELEN 15 64 65 #define PROF_PROFILE 0 66 #define PROF_TICK 1 67 #define PROF_PREFIX_PROFILE "profile-" 68 #define PROF_PREFIX_TICK "tick-" 69 70 /* 71 * Regardless of platform, there are five artificial frames in the case of the 72 * profile provider: 73 * 74 * profile_fire 75 * cyclic_expire 76 * cyclic_fire 77 * [ cbe ] 78 * [ locore ] 79 * 80 * On amd64, there are two frames associated with locore: one in locore, and 81 * another in common interrupt dispatch code. (i386 has not been modified to 82 * use this common layer.) Further, on i386, the interrupted instruction 83 * appears as its own stack frame. All of this means that we need to add one 84 * frame for amd64, and then take one away for both amd64 and i386. 85 * 86 * All of the above constraints lead to the mess below. Yes, the profile 87 * provider should ideally figure this out on-the-fly by hiting one of its own 88 * probes and then walking its own stack trace. This is complicated, however, 89 * and the static definition doesn't seem to be overly brittle. Still, we 90 * allow for a manual override in case we get it completely wrong. 91 */ 92 #ifdef __amd64 93 #define PROF_ARTIFICIAL_FRAMES 10 94 #else 95 #ifdef __i386 96 #define PROF_ARTIFICIAL_FRAMES 6 97 #endif 98 #endif 99 100 #ifdef __mips 101 /* 102 * This value is bogus just to make module compilable on mips 103 */ 104 #define PROF_ARTIFICIAL_FRAMES 3 105 #endif 106 107 #ifdef __powerpc__ 108 /* 109 * This value is bogus just to make module compilable on powerpc 110 */ 111 #define PROF_ARTIFICIAL_FRAMES 3 112 #endif 113 114 struct profile_probe_percpu; 115 116 #ifdef __mips 117 /* bogus */ 118 #define PROF_ARTIFICIAL_FRAMES 3 119 #endif 120 121 #ifdef __arm__ 122 #define PROF_ARTIFICIAL_FRAMES 3 123 #endif 124 125 #ifdef __aarch64__ 126 #define PROF_ARTIFICIAL_FRAMES 12 127 #endif 128 129 #ifdef __riscv 130 /* TODO: verify */ 131 #define PROF_ARTIFICIAL_FRAMES 10 132 #endif 133 134 typedef struct profile_probe { 135 char prof_name[PROF_NAMELEN]; 136 dtrace_id_t prof_id; 137 int prof_kind; 138 #ifdef illumos 139 hrtime_t prof_interval; 140 cyclic_id_t prof_cyclic; 141 #else 142 sbintime_t prof_interval; 143 struct callout prof_cyclic; 144 sbintime_t prof_expected; 145 struct profile_probe_percpu **prof_pcpus; 146 #endif 147 } profile_probe_t; 148 149 typedef struct profile_probe_percpu { 150 hrtime_t profc_expected; 151 hrtime_t profc_interval; 152 profile_probe_t *profc_probe; 153 #ifdef __FreeBSD__ 154 struct callout profc_cyclic; 155 #endif 156 } profile_probe_percpu_t; 157 158 static d_open_t profile_open; 159 static int profile_unload(void); 160 static void profile_create(hrtime_t, char *, int); 161 static void profile_destroy(void *, dtrace_id_t, void *); 162 static void profile_enable(void *, dtrace_id_t, void *); 163 static void profile_disable(void *, dtrace_id_t, void *); 164 static void profile_load(void *); 165 static void profile_provide(void *, dtrace_probedesc_t *); 166 167 static int profile_rates[] = { 168 97, 199, 499, 997, 1999, 169 4001, 4999, 0, 0, 0, 170 0, 0, 0, 0, 0, 171 0, 0, 0, 0, 0 172 }; 173 174 static int profile_ticks[] = { 175 1, 10, 100, 500, 1000, 176 5000, 0, 0, 0, 0, 177 0, 0, 0, 0, 0 178 }; 179 180 /* 181 * profile_max defines the upper bound on the number of profile probes that 182 * can exist (this is to prevent malicious or clumsy users from exhausing 183 * system resources by creating a slew of profile probes). At mod load time, 184 * this gets its value from PROFILE_MAX_DEFAULT or profile-max-probes if it's 185 * present in the profile.conf file. 186 */ 187 #define PROFILE_MAX_DEFAULT 1000 /* default max. number of probes */ 188 static uint32_t profile_max = PROFILE_MAX_DEFAULT; 189 /* maximum number of profile probes */ 190 static uint32_t profile_total; /* current number of profile probes */ 191 192 static struct cdevsw profile_cdevsw = { 193 .d_version = D_VERSION, 194 .d_open = profile_open, 195 .d_name = "profile", 196 }; 197 198 static dtrace_pattr_t profile_attr = { 199 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON }, 200 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 201 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA }, 202 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON }, 203 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA }, 204 }; 205 206 static dtrace_pops_t profile_pops = { 207 .dtps_provide = profile_provide, 208 .dtps_provide_module = NULL, 209 .dtps_enable = profile_enable, 210 .dtps_disable = profile_disable, 211 .dtps_suspend = NULL, 212 .dtps_resume = NULL, 213 .dtps_getargdesc = NULL, 214 .dtps_getargval = NULL, 215 .dtps_usermode = NULL, 216 .dtps_destroy = profile_destroy 217 }; 218 219 static struct cdev *profile_cdev; 220 static dtrace_provider_id_t profile_id; 221 static hrtime_t profile_interval_min = NANOSEC / 5000; /* 5000 hz */ 222 static int profile_aframes = PROF_ARTIFICIAL_FRAMES; 223 224 SYSCTL_DECL(_kern_dtrace); 225 SYSCTL_NODE(_kern_dtrace, OID_AUTO, profile, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 226 "DTrace profile parameters"); 227 SYSCTL_INT(_kern_dtrace_profile, OID_AUTO, aframes, CTLFLAG_RW, &profile_aframes, 228 0, "Skipped frames for profile provider"); 229 230 static sbintime_t 231 nsec_to_sbt(hrtime_t nsec) 232 { 233 time_t sec; 234 235 /* 236 * We need to calculate nsec * 2^32 / 10^9 237 * Seconds and nanoseconds are split to avoid overflow. 238 */ 239 sec = nsec / NANOSEC; 240 nsec = nsec % NANOSEC; 241 return (((sbintime_t)sec << 32) | ((sbintime_t)nsec << 32) / NANOSEC); 242 } 243 244 static hrtime_t 245 sbt_to_nsec(sbintime_t sbt) 246 { 247 248 return ((sbt >> 32) * NANOSEC + 249 (((uint32_t)sbt * (hrtime_t)NANOSEC) >> 32)); 250 } 251 252 static void 253 profile_probe(profile_probe_t *prof, hrtime_t late) 254 { 255 struct thread *td; 256 struct trapframe *frame; 257 uintfptr_t pc, upc; 258 259 td = curthread; 260 pc = upc = 0; 261 262 /* 263 * td_intr_frame can be unset if this is a catch-up event upon waking up 264 * from idle sleep. This can only happen on a CPU idle thread. Use a 265 * representative arg0 value in this case so that one of the probe 266 * arguments is non-zero. 267 */ 268 frame = td->td_intr_frame; 269 if (frame != NULL) { 270 if (TRAPF_USERMODE(frame)) 271 upc = TRAPF_PC(frame); 272 else 273 pc = TRAPF_PC(frame); 274 } else if (TD_IS_IDLETHREAD(td)) 275 pc = (uintfptr_t)&cpu_idle; 276 277 dtrace_probe(prof->prof_id, pc, upc, late, 0, 0); 278 } 279 280 static void 281 profile_fire(void *arg) 282 { 283 profile_probe_percpu_t *pcpu = arg; 284 profile_probe_t *prof = pcpu->profc_probe; 285 hrtime_t late; 286 287 late = sbt_to_nsec(sbinuptime() - pcpu->profc_expected); 288 289 profile_probe(prof, late); 290 pcpu->profc_expected += pcpu->profc_interval; 291 callout_schedule_sbt_curcpu(&pcpu->profc_cyclic, 292 pcpu->profc_expected, 0, C_DIRECT_EXEC | C_ABSOLUTE); 293 } 294 295 static void 296 profile_tick(void *arg) 297 { 298 profile_probe_t *prof = arg; 299 300 profile_probe(prof, 0); 301 prof->prof_expected += prof->prof_interval; 302 callout_schedule_sbt(&prof->prof_cyclic, 303 prof->prof_expected, 0, C_DIRECT_EXEC | C_ABSOLUTE); 304 } 305 306 static void 307 profile_create(hrtime_t interval, char *name, int kind) 308 { 309 profile_probe_t *prof; 310 311 if (interval < profile_interval_min) 312 return; 313 314 if (dtrace_probe_lookup(profile_id, NULL, NULL, name) != 0) 315 return; 316 317 atomic_add_32(&profile_total, 1); 318 if (profile_total > profile_max) { 319 atomic_add_32(&profile_total, -1); 320 return; 321 } 322 323 prof = kmem_zalloc(sizeof (profile_probe_t), KM_SLEEP); 324 (void) strcpy(prof->prof_name, name); 325 #ifdef illumos 326 prof->prof_interval = interval; 327 prof->prof_cyclic = CYCLIC_NONE; 328 #else 329 prof->prof_interval = nsec_to_sbt(interval); 330 callout_init(&prof->prof_cyclic, 1); 331 #endif 332 prof->prof_kind = kind; 333 prof->prof_id = dtrace_probe_create(profile_id, 334 NULL, NULL, name, 335 profile_aframes, prof); 336 } 337 338 /*ARGSUSED*/ 339 static void 340 profile_provide(void *arg, dtrace_probedesc_t *desc) 341 { 342 int i, j, rate, kind; 343 hrtime_t val = 0, mult = 1, len = 0; 344 char *name, *suffix = NULL; 345 346 const struct { 347 char *prefix; 348 int kind; 349 } types[] = { 350 { PROF_PREFIX_PROFILE, PROF_PROFILE }, 351 { PROF_PREFIX_TICK, PROF_TICK }, 352 { 0, 0 } 353 }; 354 355 const struct { 356 char *name; 357 hrtime_t mult; 358 } suffixes[] = { 359 { "ns", NANOSEC / NANOSEC }, 360 { "nsec", NANOSEC / NANOSEC }, 361 { "us", NANOSEC / MICROSEC }, 362 { "usec", NANOSEC / MICROSEC }, 363 { "ms", NANOSEC / MILLISEC }, 364 { "msec", NANOSEC / MILLISEC }, 365 { "s", NANOSEC / SEC }, 366 { "sec", NANOSEC / SEC }, 367 { "m", NANOSEC * (hrtime_t)60 }, 368 { "min", NANOSEC * (hrtime_t)60 }, 369 { "h", NANOSEC * (hrtime_t)(60 * 60) }, 370 { "hour", NANOSEC * (hrtime_t)(60 * 60) }, 371 { "d", NANOSEC * (hrtime_t)(24 * 60 * 60) }, 372 { "day", NANOSEC * (hrtime_t)(24 * 60 * 60) }, 373 { "hz", 0 }, 374 { NULL } 375 }; 376 377 if (desc == NULL) { 378 char n[PROF_NAMELEN]; 379 380 /* 381 * If no description was provided, provide all of our probes. 382 */ 383 for (i = 0; i < sizeof (profile_rates) / sizeof (int); i++) { 384 if ((rate = profile_rates[i]) == 0) 385 continue; 386 387 (void) snprintf(n, PROF_NAMELEN, "%s%d", 388 PROF_PREFIX_PROFILE, rate); 389 profile_create(NANOSEC / rate, n, PROF_PROFILE); 390 } 391 392 for (i = 0; i < sizeof (profile_ticks) / sizeof (int); i++) { 393 if ((rate = profile_ticks[i]) == 0) 394 continue; 395 396 (void) snprintf(n, PROF_NAMELEN, "%s%d", 397 PROF_PREFIX_TICK, rate); 398 profile_create(NANOSEC / rate, n, PROF_TICK); 399 } 400 401 return; 402 } 403 404 name = desc->dtpd_name; 405 406 for (i = 0; types[i].prefix != NULL; i++) { 407 len = strlen(types[i].prefix); 408 409 if (strncmp(name, types[i].prefix, len) != 0) 410 continue; 411 break; 412 } 413 414 if (types[i].prefix == NULL) 415 return; 416 417 kind = types[i].kind; 418 j = strlen(name) - len; 419 420 /* 421 * We need to start before any time suffix. 422 */ 423 for (j = strlen(name); j >= len; j--) { 424 if (name[j] >= '0' && name[j] <= '9') 425 break; 426 suffix = &name[j]; 427 } 428 429 ASSERT(suffix != NULL); 430 431 /* 432 * Now determine the numerical value present in the probe name. 433 */ 434 for (; j >= len; j--) { 435 if (name[j] < '0' || name[j] > '9') 436 return; 437 438 val += (name[j] - '0') * mult; 439 mult *= (hrtime_t)10; 440 } 441 442 if (val == 0) 443 return; 444 445 /* 446 * Look-up the suffix to determine the multiplier. 447 */ 448 for (i = 0, mult = 0; suffixes[i].name != NULL; i++) { 449 if (strcasecmp(suffixes[i].name, suffix) == 0) { 450 mult = suffixes[i].mult; 451 break; 452 } 453 } 454 455 if (suffixes[i].name == NULL && *suffix != '\0') 456 return; 457 458 if (mult == 0) { 459 /* 460 * The default is frequency-per-second. 461 */ 462 val = NANOSEC / val; 463 } else { 464 val *= mult; 465 } 466 467 profile_create(val, name, kind); 468 } 469 470 /* ARGSUSED */ 471 static void 472 profile_destroy(void *arg, dtrace_id_t id, void *parg) 473 { 474 profile_probe_t *prof = parg; 475 476 #ifdef illumos 477 ASSERT(prof->prof_cyclic == CYCLIC_NONE); 478 #else 479 ASSERT(!callout_active(&prof->prof_cyclic) && prof->prof_pcpus == NULL); 480 #endif 481 kmem_free(prof, sizeof (profile_probe_t)); 482 483 ASSERT(profile_total >= 1); 484 atomic_add_32(&profile_total, -1); 485 } 486 487 #ifdef illumos 488 /*ARGSUSED*/ 489 static void 490 profile_online(void *arg, cpu_t *cpu, cyc_handler_t *hdlr, cyc_time_t *when) 491 { 492 profile_probe_t *prof = arg; 493 profile_probe_percpu_t *pcpu; 494 495 pcpu = kmem_zalloc(sizeof (profile_probe_percpu_t), KM_SLEEP); 496 pcpu->profc_probe = prof; 497 498 hdlr->cyh_func = profile_fire; 499 hdlr->cyh_arg = pcpu; 500 501 when->cyt_interval = prof->prof_interval; 502 when->cyt_when = gethrtime() + when->cyt_interval; 503 504 pcpu->profc_expected = when->cyt_when; 505 pcpu->profc_interval = when->cyt_interval; 506 } 507 508 /*ARGSUSED*/ 509 static void 510 profile_offline(void *arg, cpu_t *cpu, void *oarg) 511 { 512 profile_probe_percpu_t *pcpu = oarg; 513 514 ASSERT(pcpu->profc_probe == arg); 515 kmem_free(pcpu, sizeof (profile_probe_percpu_t)); 516 } 517 518 /* ARGSUSED */ 519 static void 520 profile_enable(void *arg, dtrace_id_t id, void *parg) 521 { 522 profile_probe_t *prof = parg; 523 cyc_omni_handler_t omni; 524 cyc_handler_t hdlr; 525 cyc_time_t when; 526 527 ASSERT(prof->prof_interval != 0); 528 ASSERT(MUTEX_HELD(&cpu_lock)); 529 530 if (prof->prof_kind == PROF_TICK) { 531 hdlr.cyh_func = profile_tick; 532 hdlr.cyh_arg = prof; 533 534 when.cyt_interval = prof->prof_interval; 535 when.cyt_when = gethrtime() + when.cyt_interval; 536 } else { 537 ASSERT(prof->prof_kind == PROF_PROFILE); 538 omni.cyo_online = profile_online; 539 omni.cyo_offline = profile_offline; 540 omni.cyo_arg = prof; 541 } 542 543 if (prof->prof_kind == PROF_TICK) { 544 prof->prof_cyclic = cyclic_add(&hdlr, &when); 545 } else { 546 prof->prof_cyclic = cyclic_add_omni(&omni); 547 } 548 } 549 550 /* ARGSUSED */ 551 static void 552 profile_disable(void *arg, dtrace_id_t id, void *parg) 553 { 554 profile_probe_t *prof = parg; 555 556 ASSERT(prof->prof_cyclic != CYCLIC_NONE); 557 ASSERT(MUTEX_HELD(&cpu_lock)); 558 559 cyclic_remove(prof->prof_cyclic); 560 prof->prof_cyclic = CYCLIC_NONE; 561 } 562 563 #else 564 565 static void 566 profile_enable_omni(profile_probe_t *prof) 567 { 568 profile_probe_percpu_t *pcpu; 569 int cpu; 570 571 prof->prof_pcpus = kmem_zalloc((mp_maxid + 1) * sizeof(pcpu), KM_SLEEP); 572 CPU_FOREACH(cpu) { 573 pcpu = kmem_zalloc(sizeof(profile_probe_percpu_t), KM_SLEEP); 574 prof->prof_pcpus[cpu] = pcpu; 575 pcpu->profc_probe = prof; 576 pcpu->profc_expected = sbinuptime() + prof->prof_interval; 577 pcpu->profc_interval = prof->prof_interval; 578 callout_init(&pcpu->profc_cyclic, 1); 579 callout_reset_sbt_on(&pcpu->profc_cyclic, 580 pcpu->profc_expected, 0, profile_fire, pcpu, 581 cpu, C_DIRECT_EXEC | C_ABSOLUTE); 582 } 583 } 584 585 static void 586 profile_disable_omni(profile_probe_t *prof) 587 { 588 profile_probe_percpu_t *pcpu; 589 int cpu; 590 591 ASSERT(prof->prof_pcpus != NULL); 592 CPU_FOREACH(cpu) { 593 pcpu = prof->prof_pcpus[cpu]; 594 ASSERT(pcpu->profc_probe == prof); 595 ASSERT(callout_active(&pcpu->profc_cyclic)); 596 callout_stop(&pcpu->profc_cyclic); 597 callout_drain(&pcpu->profc_cyclic); 598 kmem_free(pcpu, sizeof(profile_probe_percpu_t)); 599 } 600 kmem_free(prof->prof_pcpus, (mp_maxid + 1) * sizeof(pcpu)); 601 prof->prof_pcpus = NULL; 602 } 603 604 /* ARGSUSED */ 605 static void 606 profile_enable(void *arg, dtrace_id_t id, void *parg) 607 { 608 profile_probe_t *prof = parg; 609 610 if (prof->prof_kind == PROF_TICK) { 611 prof->prof_expected = sbinuptime() + prof->prof_interval; 612 callout_reset_sbt(&prof->prof_cyclic, 613 prof->prof_expected, 0, profile_tick, prof, 614 C_DIRECT_EXEC | C_ABSOLUTE); 615 } else { 616 ASSERT(prof->prof_kind == PROF_PROFILE); 617 profile_enable_omni(prof); 618 } 619 } 620 621 /* ARGSUSED */ 622 static void 623 profile_disable(void *arg, dtrace_id_t id, void *parg) 624 { 625 profile_probe_t *prof = parg; 626 627 if (prof->prof_kind == PROF_TICK) { 628 ASSERT(callout_active(&prof->prof_cyclic)); 629 callout_stop(&prof->prof_cyclic); 630 callout_drain(&prof->prof_cyclic); 631 } else { 632 ASSERT(prof->prof_kind == PROF_PROFILE); 633 profile_disable_omni(prof); 634 } 635 } 636 #endif 637 638 static void 639 profile_load(void *dummy) 640 { 641 /* Create the /dev/dtrace/profile entry. */ 642 profile_cdev = make_dev(&profile_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, 643 "dtrace/profile"); 644 645 if (dtrace_register("profile", &profile_attr, DTRACE_PRIV_USER, 646 NULL, &profile_pops, NULL, &profile_id) != 0) 647 return; 648 } 649 650 651 static int 652 profile_unload() 653 { 654 int error = 0; 655 656 if ((error = dtrace_unregister(profile_id)) != 0) 657 return (error); 658 659 destroy_dev(profile_cdev); 660 661 return (error); 662 } 663 664 /* ARGSUSED */ 665 static int 666 profile_modevent(module_t mod __unused, int type, void *data __unused) 667 { 668 int error = 0; 669 670 switch (type) { 671 case MOD_LOAD: 672 break; 673 674 case MOD_UNLOAD: 675 break; 676 677 case MOD_SHUTDOWN: 678 break; 679 680 default: 681 error = EOPNOTSUPP; 682 break; 683 684 } 685 return (error); 686 } 687 688 /* ARGSUSED */ 689 static int 690 profile_open(struct cdev *dev __unused, int oflags __unused, int devtype __unused, struct thread *td __unused) 691 { 692 return (0); 693 } 694 695 SYSINIT(profile_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, profile_load, NULL); 696 SYSUNINIT(profile_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, profile_unload, NULL); 697 698 DEV_MODULE(profile, profile_modevent, NULL); 699 MODULE_VERSION(profile, 1); 700 MODULE_DEPEND(profile, dtrace, 1, 1, 1); 701 MODULE_DEPEND(profile, opensolaris, 1, 1, 1); 702