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 int profile_unload(void); 159 static void profile_create(hrtime_t, char *, int); 160 static void profile_destroy(void *, dtrace_id_t, void *); 161 static void profile_enable(void *, dtrace_id_t, void *); 162 static void profile_disable(void *, dtrace_id_t, void *); 163 static void profile_load(void *); 164 static void profile_provide(void *, dtrace_probedesc_t *); 165 166 static int profile_rates[] = { 167 97, 199, 499, 997, 1999, 168 4001, 4999, 0, 0, 0, 169 0, 0, 0, 0, 0, 170 0, 0, 0, 0, 0 171 }; 172 173 static int profile_ticks[] = { 174 1, 10, 100, 500, 1000, 175 5000, 0, 0, 0, 0, 176 0, 0, 0, 0, 0 177 }; 178 179 /* 180 * profile_max defines the upper bound on the number of profile probes that 181 * can exist (this is to prevent malicious or clumsy users from exhausing 182 * system resources by creating a slew of profile probes). At mod load time, 183 * this gets its value from PROFILE_MAX_DEFAULT or profile-max-probes if it's 184 * present in the profile.conf file. 185 */ 186 #define PROFILE_MAX_DEFAULT 1000 /* default max. number of probes */ 187 static uint32_t profile_max = PROFILE_MAX_DEFAULT; 188 /* maximum number of profile probes */ 189 static uint32_t profile_total; /* current number of profile probes */ 190 191 static dtrace_pattr_t profile_attr = { 192 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON }, 193 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 194 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA }, 195 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON }, 196 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA }, 197 }; 198 199 static dtrace_pops_t profile_pops = { 200 .dtps_provide = profile_provide, 201 .dtps_provide_module = NULL, 202 .dtps_enable = profile_enable, 203 .dtps_disable = profile_disable, 204 .dtps_suspend = NULL, 205 .dtps_resume = NULL, 206 .dtps_getargdesc = NULL, 207 .dtps_getargval = NULL, 208 .dtps_usermode = NULL, 209 .dtps_destroy = profile_destroy 210 }; 211 212 static dtrace_provider_id_t profile_id; 213 static hrtime_t profile_interval_min = NANOSEC / 5000; /* 5000 hz */ 214 static int profile_aframes = PROF_ARTIFICIAL_FRAMES; 215 216 SYSCTL_DECL(_kern_dtrace); 217 SYSCTL_NODE(_kern_dtrace, OID_AUTO, profile, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 218 "DTrace profile parameters"); 219 SYSCTL_INT(_kern_dtrace_profile, OID_AUTO, aframes, CTLFLAG_RW, &profile_aframes, 220 0, "Skipped frames for profile provider"); 221 222 static sbintime_t 223 nsec_to_sbt(hrtime_t nsec) 224 { 225 time_t sec; 226 227 /* 228 * We need to calculate nsec * 2^32 / 10^9 229 * Seconds and nanoseconds are split to avoid overflow. 230 */ 231 sec = nsec / NANOSEC; 232 nsec = nsec % NANOSEC; 233 return (((sbintime_t)sec << 32) | ((sbintime_t)nsec << 32) / NANOSEC); 234 } 235 236 static hrtime_t 237 sbt_to_nsec(sbintime_t sbt) 238 { 239 240 return ((sbt >> 32) * NANOSEC + 241 (((uint32_t)sbt * (hrtime_t)NANOSEC) >> 32)); 242 } 243 244 static void 245 profile_probe(profile_probe_t *prof, hrtime_t late) 246 { 247 struct thread *td; 248 struct trapframe *frame; 249 uintfptr_t pc, upc; 250 251 td = curthread; 252 pc = upc = 0; 253 254 /* 255 * td_intr_frame can be unset if this is a catch-up event upon waking up 256 * from idle sleep. This can only happen on a CPU idle thread. Use a 257 * representative arg0 value in this case so that one of the probe 258 * arguments is non-zero. 259 */ 260 frame = td->td_intr_frame; 261 if (frame != NULL) { 262 if (TRAPF_USERMODE(frame)) 263 upc = TRAPF_PC(frame); 264 else 265 pc = TRAPF_PC(frame); 266 } else if (TD_IS_IDLETHREAD(td)) 267 pc = (uintfptr_t)&cpu_idle; 268 269 dtrace_probe(prof->prof_id, pc, upc, late, 0, 0); 270 } 271 272 static void 273 profile_fire(void *arg) 274 { 275 profile_probe_percpu_t *pcpu = arg; 276 profile_probe_t *prof = pcpu->profc_probe; 277 hrtime_t late; 278 279 late = sbt_to_nsec(sbinuptime() - pcpu->profc_expected); 280 281 profile_probe(prof, late); 282 pcpu->profc_expected += pcpu->profc_interval; 283 callout_schedule_sbt_curcpu(&pcpu->profc_cyclic, 284 pcpu->profc_expected, 0, C_DIRECT_EXEC | C_ABSOLUTE); 285 } 286 287 static void 288 profile_tick(void *arg) 289 { 290 profile_probe_t *prof = arg; 291 292 profile_probe(prof, 0); 293 prof->prof_expected += prof->prof_interval; 294 callout_schedule_sbt(&prof->prof_cyclic, 295 prof->prof_expected, 0, C_DIRECT_EXEC | C_ABSOLUTE); 296 } 297 298 static void 299 profile_create(hrtime_t interval, char *name, int kind) 300 { 301 profile_probe_t *prof; 302 303 if (interval < profile_interval_min) 304 return; 305 306 if (dtrace_probe_lookup(profile_id, NULL, NULL, name) != 0) 307 return; 308 309 atomic_add_32(&profile_total, 1); 310 if (profile_total > profile_max) { 311 atomic_add_32(&profile_total, -1); 312 return; 313 } 314 315 prof = kmem_zalloc(sizeof (profile_probe_t), KM_SLEEP); 316 (void) strcpy(prof->prof_name, name); 317 #ifdef illumos 318 prof->prof_interval = interval; 319 prof->prof_cyclic = CYCLIC_NONE; 320 #else 321 prof->prof_interval = nsec_to_sbt(interval); 322 callout_init(&prof->prof_cyclic, 1); 323 #endif 324 prof->prof_kind = kind; 325 prof->prof_id = dtrace_probe_create(profile_id, 326 NULL, NULL, name, 327 profile_aframes, prof); 328 } 329 330 /*ARGSUSED*/ 331 static void 332 profile_provide(void *arg, dtrace_probedesc_t *desc) 333 { 334 int i, j, rate, kind; 335 hrtime_t val = 0, mult = 1, len = 0; 336 char *name, *suffix = NULL; 337 338 const struct { 339 char *prefix; 340 int kind; 341 } types[] = { 342 { PROF_PREFIX_PROFILE, PROF_PROFILE }, 343 { PROF_PREFIX_TICK, PROF_TICK }, 344 { 0, 0 } 345 }; 346 347 const struct { 348 char *name; 349 hrtime_t mult; 350 } suffixes[] = { 351 { "ns", NANOSEC / NANOSEC }, 352 { "nsec", NANOSEC / NANOSEC }, 353 { "us", NANOSEC / MICROSEC }, 354 { "usec", NANOSEC / MICROSEC }, 355 { "ms", NANOSEC / MILLISEC }, 356 { "msec", NANOSEC / MILLISEC }, 357 { "s", NANOSEC / SEC }, 358 { "sec", NANOSEC / SEC }, 359 { "m", NANOSEC * (hrtime_t)60 }, 360 { "min", NANOSEC * (hrtime_t)60 }, 361 { "h", NANOSEC * (hrtime_t)(60 * 60) }, 362 { "hour", NANOSEC * (hrtime_t)(60 * 60) }, 363 { "d", NANOSEC * (hrtime_t)(24 * 60 * 60) }, 364 { "day", NANOSEC * (hrtime_t)(24 * 60 * 60) }, 365 { "hz", 0 }, 366 { NULL } 367 }; 368 369 if (desc == NULL) { 370 char n[PROF_NAMELEN]; 371 372 /* 373 * If no description was provided, provide all of our probes. 374 */ 375 for (i = 0; i < sizeof (profile_rates) / sizeof (int); i++) { 376 if ((rate = profile_rates[i]) == 0) 377 continue; 378 379 (void) snprintf(n, PROF_NAMELEN, "%s%d", 380 PROF_PREFIX_PROFILE, rate); 381 profile_create(NANOSEC / rate, n, PROF_PROFILE); 382 } 383 384 for (i = 0; i < sizeof (profile_ticks) / sizeof (int); i++) { 385 if ((rate = profile_ticks[i]) == 0) 386 continue; 387 388 (void) snprintf(n, PROF_NAMELEN, "%s%d", 389 PROF_PREFIX_TICK, rate); 390 profile_create(NANOSEC / rate, n, PROF_TICK); 391 } 392 393 return; 394 } 395 396 name = desc->dtpd_name; 397 398 for (i = 0; types[i].prefix != NULL; i++) { 399 len = strlen(types[i].prefix); 400 401 if (strncmp(name, types[i].prefix, len) != 0) 402 continue; 403 break; 404 } 405 406 if (types[i].prefix == NULL) 407 return; 408 409 kind = types[i].kind; 410 j = strlen(name) - len; 411 412 /* 413 * We need to start before any time suffix. 414 */ 415 for (j = strlen(name); j >= len; j--) { 416 if (name[j] >= '0' && name[j] <= '9') 417 break; 418 suffix = &name[j]; 419 } 420 421 ASSERT(suffix != NULL); 422 423 /* 424 * Now determine the numerical value present in the probe name. 425 */ 426 for (; j >= len; j--) { 427 if (name[j] < '0' || name[j] > '9') 428 return; 429 430 val += (name[j] - '0') * mult; 431 mult *= (hrtime_t)10; 432 } 433 434 if (val == 0) 435 return; 436 437 /* 438 * Look-up the suffix to determine the multiplier. 439 */ 440 for (i = 0, mult = 0; suffixes[i].name != NULL; i++) { 441 if (strcasecmp(suffixes[i].name, suffix) == 0) { 442 mult = suffixes[i].mult; 443 break; 444 } 445 } 446 447 if (suffixes[i].name == NULL && *suffix != '\0') 448 return; 449 450 if (mult == 0) { 451 /* 452 * The default is frequency-per-second. 453 */ 454 val = NANOSEC / val; 455 } else { 456 val *= mult; 457 } 458 459 profile_create(val, name, kind); 460 } 461 462 /* ARGSUSED */ 463 static void 464 profile_destroy(void *arg, dtrace_id_t id, void *parg) 465 { 466 profile_probe_t *prof = parg; 467 468 #ifdef illumos 469 ASSERT(prof->prof_cyclic == CYCLIC_NONE); 470 #else 471 ASSERT(!callout_active(&prof->prof_cyclic) && prof->prof_pcpus == NULL); 472 #endif 473 kmem_free(prof, sizeof (profile_probe_t)); 474 475 ASSERT(profile_total >= 1); 476 atomic_add_32(&profile_total, -1); 477 } 478 479 #ifdef illumos 480 /*ARGSUSED*/ 481 static void 482 profile_online(void *arg, cpu_t *cpu, cyc_handler_t *hdlr, cyc_time_t *when) 483 { 484 profile_probe_t *prof = arg; 485 profile_probe_percpu_t *pcpu; 486 487 pcpu = kmem_zalloc(sizeof (profile_probe_percpu_t), KM_SLEEP); 488 pcpu->profc_probe = prof; 489 490 hdlr->cyh_func = profile_fire; 491 hdlr->cyh_arg = pcpu; 492 493 when->cyt_interval = prof->prof_interval; 494 when->cyt_when = gethrtime() + when->cyt_interval; 495 496 pcpu->profc_expected = when->cyt_when; 497 pcpu->profc_interval = when->cyt_interval; 498 } 499 500 /*ARGSUSED*/ 501 static void 502 profile_offline(void *arg, cpu_t *cpu, void *oarg) 503 { 504 profile_probe_percpu_t *pcpu = oarg; 505 506 ASSERT(pcpu->profc_probe == arg); 507 kmem_free(pcpu, sizeof (profile_probe_percpu_t)); 508 } 509 510 /* ARGSUSED */ 511 static void 512 profile_enable(void *arg, dtrace_id_t id, void *parg) 513 { 514 profile_probe_t *prof = parg; 515 cyc_omni_handler_t omni; 516 cyc_handler_t hdlr; 517 cyc_time_t when; 518 519 ASSERT(prof->prof_interval != 0); 520 ASSERT(MUTEX_HELD(&cpu_lock)); 521 522 if (prof->prof_kind == PROF_TICK) { 523 hdlr.cyh_func = profile_tick; 524 hdlr.cyh_arg = prof; 525 526 when.cyt_interval = prof->prof_interval; 527 when.cyt_when = gethrtime() + when.cyt_interval; 528 } else { 529 ASSERT(prof->prof_kind == PROF_PROFILE); 530 omni.cyo_online = profile_online; 531 omni.cyo_offline = profile_offline; 532 omni.cyo_arg = prof; 533 } 534 535 if (prof->prof_kind == PROF_TICK) { 536 prof->prof_cyclic = cyclic_add(&hdlr, &when); 537 } else { 538 prof->prof_cyclic = cyclic_add_omni(&omni); 539 } 540 } 541 542 /* ARGSUSED */ 543 static void 544 profile_disable(void *arg, dtrace_id_t id, void *parg) 545 { 546 profile_probe_t *prof = parg; 547 548 ASSERT(prof->prof_cyclic != CYCLIC_NONE); 549 ASSERT(MUTEX_HELD(&cpu_lock)); 550 551 cyclic_remove(prof->prof_cyclic); 552 prof->prof_cyclic = CYCLIC_NONE; 553 } 554 555 #else 556 557 static void 558 profile_enable_omni(profile_probe_t *prof) 559 { 560 profile_probe_percpu_t *pcpu; 561 int cpu; 562 563 prof->prof_pcpus = kmem_zalloc((mp_maxid + 1) * sizeof(pcpu), KM_SLEEP); 564 CPU_FOREACH(cpu) { 565 pcpu = kmem_zalloc(sizeof(profile_probe_percpu_t), KM_SLEEP); 566 prof->prof_pcpus[cpu] = pcpu; 567 pcpu->profc_probe = prof; 568 pcpu->profc_expected = sbinuptime() + prof->prof_interval; 569 pcpu->profc_interval = prof->prof_interval; 570 callout_init(&pcpu->profc_cyclic, 1); 571 callout_reset_sbt_on(&pcpu->profc_cyclic, 572 pcpu->profc_expected, 0, profile_fire, pcpu, 573 cpu, C_DIRECT_EXEC | C_ABSOLUTE); 574 } 575 } 576 577 static void 578 profile_disable_omni(profile_probe_t *prof) 579 { 580 profile_probe_percpu_t *pcpu; 581 int cpu; 582 583 ASSERT(prof->prof_pcpus != NULL); 584 CPU_FOREACH(cpu) { 585 pcpu = prof->prof_pcpus[cpu]; 586 ASSERT(pcpu->profc_probe == prof); 587 ASSERT(callout_active(&pcpu->profc_cyclic)); 588 callout_stop(&pcpu->profc_cyclic); 589 callout_drain(&pcpu->profc_cyclic); 590 kmem_free(pcpu, sizeof(profile_probe_percpu_t)); 591 } 592 kmem_free(prof->prof_pcpus, (mp_maxid + 1) * sizeof(pcpu)); 593 prof->prof_pcpus = NULL; 594 } 595 596 /* ARGSUSED */ 597 static void 598 profile_enable(void *arg, dtrace_id_t id, void *parg) 599 { 600 profile_probe_t *prof = parg; 601 602 if (prof->prof_kind == PROF_TICK) { 603 prof->prof_expected = sbinuptime() + prof->prof_interval; 604 callout_reset_sbt(&prof->prof_cyclic, 605 prof->prof_expected, 0, profile_tick, prof, 606 C_DIRECT_EXEC | C_ABSOLUTE); 607 } else { 608 ASSERT(prof->prof_kind == PROF_PROFILE); 609 profile_enable_omni(prof); 610 } 611 } 612 613 /* ARGSUSED */ 614 static void 615 profile_disable(void *arg, dtrace_id_t id, void *parg) 616 { 617 profile_probe_t *prof = parg; 618 619 if (prof->prof_kind == PROF_TICK) { 620 ASSERT(callout_active(&prof->prof_cyclic)); 621 callout_stop(&prof->prof_cyclic); 622 callout_drain(&prof->prof_cyclic); 623 } else { 624 ASSERT(prof->prof_kind == PROF_PROFILE); 625 profile_disable_omni(prof); 626 } 627 } 628 #endif 629 630 static void 631 profile_load(void *dummy) 632 { 633 if (dtrace_register("profile", &profile_attr, DTRACE_PRIV_USER, 634 NULL, &profile_pops, NULL, &profile_id) != 0) 635 return; 636 } 637 638 639 static int 640 profile_unload() 641 { 642 int error = 0; 643 644 if ((error = dtrace_unregister(profile_id)) != 0) 645 return (error); 646 647 return (error); 648 } 649 650 /* ARGSUSED */ 651 static int 652 profile_modevent(module_t mod __unused, int type, void *data __unused) 653 { 654 int error = 0; 655 656 switch (type) { 657 case MOD_LOAD: 658 break; 659 660 case MOD_UNLOAD: 661 break; 662 663 case MOD_SHUTDOWN: 664 break; 665 666 default: 667 error = EOPNOTSUPP; 668 break; 669 670 } 671 return (error); 672 } 673 674 SYSINIT(profile_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, profile_load, NULL); 675 SYSUNINIT(profile_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, profile_unload, NULL); 676 677 DEV_MODULE(profile, profile_modevent, NULL); 678 MODULE_VERSION(profile, 1); 679 MODULE_DEPEND(profile, dtrace, 1, 1, 1); 680 MODULE_DEPEND(profile, opensolaris, 1, 1, 1); 681