17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5ad4023c4Sdp * Common Development and Distribution License (the "License"). 6ad4023c4Sdp * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*ae115bc7Smrj * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include <sys/errno.h> 297c478bd9Sstevel@tonic-gate #include <sys/stat.h> 307c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 317c478bd9Sstevel@tonic-gate #include <sys/conf.h> 327c478bd9Sstevel@tonic-gate #include <sys/systm.h> 337c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 347c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 357c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h> 367c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 377c478bd9Sstevel@tonic-gate #include <sys/strsubr.h> 387c478bd9Sstevel@tonic-gate #include <sys/dtrace.h> 397c478bd9Sstevel@tonic-gate #include <sys/cyclic.h> 407c478bd9Sstevel@tonic-gate #include <sys/atomic.h> 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate static dev_info_t *profile_devi; 437c478bd9Sstevel@tonic-gate static dtrace_provider_id_t profile_id; 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate /* 46*ae115bc7Smrj * Regardless of platform, the stack frames look like this in the case of the 477c478bd9Sstevel@tonic-gate * profile provider: 487c478bd9Sstevel@tonic-gate * 497c478bd9Sstevel@tonic-gate * profile_fire 507c478bd9Sstevel@tonic-gate * cyclic_expire 517c478bd9Sstevel@tonic-gate * cyclic_fire 527c478bd9Sstevel@tonic-gate * [ cbe ] 53*ae115bc7Smrj * [ interrupt code ] 547c478bd9Sstevel@tonic-gate * 55*ae115bc7Smrj * On x86, there are five frames from the generic interrupt code; further, the 56*ae115bc7Smrj * interrupted instruction appears as its own stack frame, giving us a total of 57*ae115bc7Smrj * 10. 587c478bd9Sstevel@tonic-gate * 597c478bd9Sstevel@tonic-gate * On SPARC, the picture is further complicated because the compiler 607c478bd9Sstevel@tonic-gate * optimizes away tail-calls -- so the following frames are optimized away: 617c478bd9Sstevel@tonic-gate * 627c478bd9Sstevel@tonic-gate * profile_fire 637c478bd9Sstevel@tonic-gate * cyclic_expire 647c478bd9Sstevel@tonic-gate * 657c478bd9Sstevel@tonic-gate * This gives three frames. However, on DEBUG kernels, the cyclic_expire 667c478bd9Sstevel@tonic-gate * frame cannot be tail-call eliminated, yielding four frames in this case. 677c478bd9Sstevel@tonic-gate * 687c478bd9Sstevel@tonic-gate * All of the above constraints lead to the mess below. Yes, the profile 69*ae115bc7Smrj * provider should ideally figure this out on-the-fly by hitting one of its own 707c478bd9Sstevel@tonic-gate * probes and then walking its own stack trace. This is complicated, however, 717c478bd9Sstevel@tonic-gate * and the static definition doesn't seem to be overly brittle. Still, we 727c478bd9Sstevel@tonic-gate * allow for a manual override in case we get it completely wrong. 737c478bd9Sstevel@tonic-gate */ 74*ae115bc7Smrj #ifdef __x86 75*ae115bc7Smrj #define PROF_ARTIFICIAL_FRAMES 10 767c478bd9Sstevel@tonic-gate #else 777c478bd9Sstevel@tonic-gate #ifdef __sparc 787c478bd9Sstevel@tonic-gate #ifdef DEBUG 797c478bd9Sstevel@tonic-gate #define PROF_ARTIFICIAL_FRAMES 4 807c478bd9Sstevel@tonic-gate #else 817c478bd9Sstevel@tonic-gate #define PROF_ARTIFICIAL_FRAMES 3 827c478bd9Sstevel@tonic-gate #endif 837c478bd9Sstevel@tonic-gate #endif 847c478bd9Sstevel@tonic-gate #endif 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate #define PROF_NAMELEN 15 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate #define PROF_PROFILE 0 897c478bd9Sstevel@tonic-gate #define PROF_TICK 1 907c478bd9Sstevel@tonic-gate #define PROF_PREFIX_PROFILE "profile-" 917c478bd9Sstevel@tonic-gate #define PROF_PREFIX_TICK "tick-" 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate typedef struct profile_probe { 947c478bd9Sstevel@tonic-gate char prof_name[PROF_NAMELEN]; 957c478bd9Sstevel@tonic-gate dtrace_id_t prof_id; 967c478bd9Sstevel@tonic-gate int prof_kind; 977c478bd9Sstevel@tonic-gate hrtime_t prof_interval; 987c478bd9Sstevel@tonic-gate cyclic_id_t prof_cyclic; 997c478bd9Sstevel@tonic-gate } profile_probe_t; 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate typedef struct profile_probe_percpu { 1027c478bd9Sstevel@tonic-gate hrtime_t profc_expected; 1037c478bd9Sstevel@tonic-gate hrtime_t profc_interval; 1047c478bd9Sstevel@tonic-gate profile_probe_t *profc_probe; 1057c478bd9Sstevel@tonic-gate } profile_probe_percpu_t; 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate hrtime_t profile_interval_min = NANOSEC / 5000; /* 5000 hz */ 1087c478bd9Sstevel@tonic-gate int profile_aframes = 0; /* override */ 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate static int profile_rates[] = { 1117c478bd9Sstevel@tonic-gate 97, 199, 499, 997, 1999, 1127c478bd9Sstevel@tonic-gate 4001, 4999, 0, 0, 0, 1137c478bd9Sstevel@tonic-gate 0, 0, 0, 0, 0, 1147c478bd9Sstevel@tonic-gate 0, 0, 0, 0, 0 1157c478bd9Sstevel@tonic-gate }; 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate static int profile_ticks[] = { 1187c478bd9Sstevel@tonic-gate 1, 10, 100, 500, 1000, 1197c478bd9Sstevel@tonic-gate 5000, 0, 0, 0, 0, 1207c478bd9Sstevel@tonic-gate 0, 0, 0, 0, 0 1217c478bd9Sstevel@tonic-gate }; 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate /* 1247c478bd9Sstevel@tonic-gate * profile_max defines the upper bound on the number of profile probes that 1257c478bd9Sstevel@tonic-gate * can exist (this is to prevent malicious or clumsy users from exhausing 1267c478bd9Sstevel@tonic-gate * system resources by creating a slew of profile probes). At mod load time, 1277c478bd9Sstevel@tonic-gate * this gets its value from PROFILE_MAX_DEFAULT or profile-max-probes if it's 1287c478bd9Sstevel@tonic-gate * present in the profile.conf file. 1297c478bd9Sstevel@tonic-gate */ 1307c478bd9Sstevel@tonic-gate #define PROFILE_MAX_DEFAULT 1000 /* default max. number of probes */ 1317c478bd9Sstevel@tonic-gate static uint32_t profile_max; /* maximum number of profile probes */ 1327c478bd9Sstevel@tonic-gate static uint32_t profile_total; /* current number of profile probes */ 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate static void 1357c478bd9Sstevel@tonic-gate profile_fire(void *arg) 1367c478bd9Sstevel@tonic-gate { 1377c478bd9Sstevel@tonic-gate profile_probe_percpu_t *pcpu = arg; 1387c478bd9Sstevel@tonic-gate profile_probe_t *prof = pcpu->profc_probe; 1397c478bd9Sstevel@tonic-gate hrtime_t late; 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate late = dtrace_gethrtime() - pcpu->profc_expected; 1427c478bd9Sstevel@tonic-gate pcpu->profc_expected += pcpu->profc_interval; 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate dtrace_probe(prof->prof_id, CPU->cpu_profile_pc, 1457c478bd9Sstevel@tonic-gate CPU->cpu_profile_upc, late, 0, 0); 1467c478bd9Sstevel@tonic-gate } 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate static void 1497c478bd9Sstevel@tonic-gate profile_tick(void *arg) 1507c478bd9Sstevel@tonic-gate { 1517c478bd9Sstevel@tonic-gate profile_probe_t *prof = arg; 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate dtrace_probe(prof->prof_id, CPU->cpu_profile_pc, 1547c478bd9Sstevel@tonic-gate CPU->cpu_profile_upc, 0, 0, 0); 1557c478bd9Sstevel@tonic-gate } 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate static void 1587c478bd9Sstevel@tonic-gate profile_create(hrtime_t interval, const char *name, int kind) 1597c478bd9Sstevel@tonic-gate { 1607c478bd9Sstevel@tonic-gate profile_probe_t *prof; 161*ae115bc7Smrj int nr_frames = PROF_ARTIFICIAL_FRAMES + dtrace_mach_aframes(); 162*ae115bc7Smrj 163*ae115bc7Smrj if (profile_aframes) 164*ae115bc7Smrj nr_frames = profile_aframes; 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate if (interval < profile_interval_min) 1677c478bd9Sstevel@tonic-gate return; 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate if (dtrace_probe_lookup(profile_id, NULL, NULL, name) != 0) 1707c478bd9Sstevel@tonic-gate return; 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate atomic_add_32(&profile_total, 1); 1737c478bd9Sstevel@tonic-gate if (profile_total > profile_max) { 1747c478bd9Sstevel@tonic-gate atomic_add_32(&profile_total, -1); 1757c478bd9Sstevel@tonic-gate return; 1767c478bd9Sstevel@tonic-gate } 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate prof = kmem_zalloc(sizeof (profile_probe_t), KM_SLEEP); 1797c478bd9Sstevel@tonic-gate (void) strcpy(prof->prof_name, name); 1807c478bd9Sstevel@tonic-gate prof->prof_interval = interval; 1817c478bd9Sstevel@tonic-gate prof->prof_cyclic = CYCLIC_NONE; 1827c478bd9Sstevel@tonic-gate prof->prof_kind = kind; 1837c478bd9Sstevel@tonic-gate prof->prof_id = dtrace_probe_create(profile_id, 184*ae115bc7Smrj NULL, NULL, name, nr_frames, prof); 1857c478bd9Sstevel@tonic-gate } 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 1887c478bd9Sstevel@tonic-gate static void 1897c478bd9Sstevel@tonic-gate profile_provide(void *arg, const dtrace_probedesc_t *desc) 1907c478bd9Sstevel@tonic-gate { 1917c478bd9Sstevel@tonic-gate int i, j, rate, kind; 1927c478bd9Sstevel@tonic-gate hrtime_t val = 0, mult = 1, len; 1937c478bd9Sstevel@tonic-gate const char *name, *suffix = NULL; 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate const struct { 1967c478bd9Sstevel@tonic-gate char *prefix; 1977c478bd9Sstevel@tonic-gate int kind; 1987c478bd9Sstevel@tonic-gate } types[] = { 1997c478bd9Sstevel@tonic-gate { PROF_PREFIX_PROFILE, PROF_PROFILE }, 2007c478bd9Sstevel@tonic-gate { PROF_PREFIX_TICK, PROF_TICK }, 2017c478bd9Sstevel@tonic-gate { NULL, NULL } 2027c478bd9Sstevel@tonic-gate }; 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate const struct { 2057c478bd9Sstevel@tonic-gate char *name; 2067c478bd9Sstevel@tonic-gate hrtime_t mult; 2077c478bd9Sstevel@tonic-gate } suffixes[] = { 2087c478bd9Sstevel@tonic-gate { "ns", NANOSEC / NANOSEC }, 2097c478bd9Sstevel@tonic-gate { "nsec", NANOSEC / NANOSEC }, 2107c478bd9Sstevel@tonic-gate { "us", NANOSEC / MICROSEC }, 2117c478bd9Sstevel@tonic-gate { "usec", NANOSEC / MICROSEC }, 2127c478bd9Sstevel@tonic-gate { "ms", NANOSEC / MILLISEC }, 2137c478bd9Sstevel@tonic-gate { "msec", NANOSEC / MILLISEC }, 2147c478bd9Sstevel@tonic-gate { "s", NANOSEC / SEC }, 2157c478bd9Sstevel@tonic-gate { "sec", NANOSEC / SEC }, 2167c478bd9Sstevel@tonic-gate { "m", NANOSEC * (hrtime_t)60 }, 2177c478bd9Sstevel@tonic-gate { "min", NANOSEC * (hrtime_t)60 }, 2187c478bd9Sstevel@tonic-gate { "h", NANOSEC * (hrtime_t)(60 * 60) }, 2197c478bd9Sstevel@tonic-gate { "hour", NANOSEC * (hrtime_t)(60 * 60) }, 2207c478bd9Sstevel@tonic-gate { "d", NANOSEC * (hrtime_t)(24 * 60 * 60) }, 2217c478bd9Sstevel@tonic-gate { "day", NANOSEC * (hrtime_t)(24 * 60 * 60) }, 2227c478bd9Sstevel@tonic-gate { "hz", 0 }, 2237c478bd9Sstevel@tonic-gate { NULL } 2247c478bd9Sstevel@tonic-gate }; 2257c478bd9Sstevel@tonic-gate 2267c478bd9Sstevel@tonic-gate if (desc == NULL) { 2277c478bd9Sstevel@tonic-gate char n[PROF_NAMELEN]; 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate /* 2307c478bd9Sstevel@tonic-gate * If no description was provided, provide all of our probes. 2317c478bd9Sstevel@tonic-gate */ 2327c478bd9Sstevel@tonic-gate for (i = 0; i < sizeof (profile_rates) / sizeof (int); i++) { 2337c478bd9Sstevel@tonic-gate if ((rate = profile_rates[i]) == 0) 2347c478bd9Sstevel@tonic-gate continue; 2357c478bd9Sstevel@tonic-gate 2367c478bd9Sstevel@tonic-gate (void) snprintf(n, PROF_NAMELEN, "%s%d", 2377c478bd9Sstevel@tonic-gate PROF_PREFIX_PROFILE, rate); 2387c478bd9Sstevel@tonic-gate profile_create(NANOSEC / rate, n, PROF_PROFILE); 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate for (i = 0; i < sizeof (profile_ticks) / sizeof (int); i++) { 2427c478bd9Sstevel@tonic-gate if ((rate = profile_ticks[i]) == 0) 2437c478bd9Sstevel@tonic-gate continue; 2447c478bd9Sstevel@tonic-gate 2457c478bd9Sstevel@tonic-gate (void) snprintf(n, PROF_NAMELEN, "%s%d", 2467c478bd9Sstevel@tonic-gate PROF_PREFIX_TICK, rate); 2477c478bd9Sstevel@tonic-gate profile_create(NANOSEC / rate, n, PROF_TICK); 2487c478bd9Sstevel@tonic-gate } 2497c478bd9Sstevel@tonic-gate 2507c478bd9Sstevel@tonic-gate return; 2517c478bd9Sstevel@tonic-gate } 2527c478bd9Sstevel@tonic-gate 2537c478bd9Sstevel@tonic-gate name = desc->dtpd_name; 2547c478bd9Sstevel@tonic-gate 2557c478bd9Sstevel@tonic-gate for (i = 0; types[i].prefix != NULL; i++) { 2567c478bd9Sstevel@tonic-gate len = strlen(types[i].prefix); 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate if (strncmp(name, types[i].prefix, len) != 0) 2597c478bd9Sstevel@tonic-gate continue; 2607c478bd9Sstevel@tonic-gate break; 2617c478bd9Sstevel@tonic-gate } 2627c478bd9Sstevel@tonic-gate 2637c478bd9Sstevel@tonic-gate if (types[i].prefix == NULL) 2647c478bd9Sstevel@tonic-gate return; 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate kind = types[i].kind; 2677c478bd9Sstevel@tonic-gate j = strlen(name) - len; 2687c478bd9Sstevel@tonic-gate 2697c478bd9Sstevel@tonic-gate /* 2707c478bd9Sstevel@tonic-gate * We need to start before any time suffix. 2717c478bd9Sstevel@tonic-gate */ 2727c478bd9Sstevel@tonic-gate for (j = strlen(name); j >= len; j--) { 2737c478bd9Sstevel@tonic-gate if (name[j] >= '0' && name[j] <= '9') 2747c478bd9Sstevel@tonic-gate break; 2757c478bd9Sstevel@tonic-gate suffix = &name[j]; 2767c478bd9Sstevel@tonic-gate } 2777c478bd9Sstevel@tonic-gate 2787c478bd9Sstevel@tonic-gate ASSERT(suffix != NULL); 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate /* 2817c478bd9Sstevel@tonic-gate * Now determine the numerical value present in the probe name. 2827c478bd9Sstevel@tonic-gate */ 2837c478bd9Sstevel@tonic-gate for (; j >= len; j--) { 2847c478bd9Sstevel@tonic-gate if (name[j] < '0' || name[j] > '9') 2857c478bd9Sstevel@tonic-gate return; 2867c478bd9Sstevel@tonic-gate 2877c478bd9Sstevel@tonic-gate val += (name[j] - '0') * mult; 2887c478bd9Sstevel@tonic-gate mult *= (hrtime_t)10; 2897c478bd9Sstevel@tonic-gate } 2907c478bd9Sstevel@tonic-gate 2917c478bd9Sstevel@tonic-gate if (val == 0) 2927c478bd9Sstevel@tonic-gate return; 2937c478bd9Sstevel@tonic-gate 2947c478bd9Sstevel@tonic-gate /* 2957c478bd9Sstevel@tonic-gate * Look-up the suffix to determine the multiplier. 2967c478bd9Sstevel@tonic-gate */ 2977c478bd9Sstevel@tonic-gate for (i = 0, mult = 0; suffixes[i].name != NULL; i++) { 2987c478bd9Sstevel@tonic-gate if (strcasecmp(suffixes[i].name, suffix) == 0) { 2997c478bd9Sstevel@tonic-gate mult = suffixes[i].mult; 3007c478bd9Sstevel@tonic-gate break; 3017c478bd9Sstevel@tonic-gate } 3027c478bd9Sstevel@tonic-gate } 3037c478bd9Sstevel@tonic-gate 3047c478bd9Sstevel@tonic-gate if (suffixes[i].name == NULL && *suffix != '\0') 3057c478bd9Sstevel@tonic-gate return; 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate if (mult == 0) { 3087c478bd9Sstevel@tonic-gate /* 3097c478bd9Sstevel@tonic-gate * The default is frequency-per-second. 3107c478bd9Sstevel@tonic-gate */ 3117c478bd9Sstevel@tonic-gate val = NANOSEC / val; 3127c478bd9Sstevel@tonic-gate } else { 3137c478bd9Sstevel@tonic-gate val *= mult; 3147c478bd9Sstevel@tonic-gate } 3157c478bd9Sstevel@tonic-gate 3167c478bd9Sstevel@tonic-gate profile_create(val, name, kind); 3177c478bd9Sstevel@tonic-gate } 3187c478bd9Sstevel@tonic-gate 3197c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 3207c478bd9Sstevel@tonic-gate static void 3217c478bd9Sstevel@tonic-gate profile_destroy(void *arg, dtrace_id_t id, void *parg) 3227c478bd9Sstevel@tonic-gate { 3237c478bd9Sstevel@tonic-gate profile_probe_t *prof = parg; 3247c478bd9Sstevel@tonic-gate 3257c478bd9Sstevel@tonic-gate ASSERT(prof->prof_cyclic == CYCLIC_NONE); 3267c478bd9Sstevel@tonic-gate kmem_free(prof, sizeof (profile_probe_t)); 3277c478bd9Sstevel@tonic-gate 3287c478bd9Sstevel@tonic-gate ASSERT(profile_total >= 1); 3297c478bd9Sstevel@tonic-gate atomic_add_32(&profile_total, -1); 3307c478bd9Sstevel@tonic-gate } 3317c478bd9Sstevel@tonic-gate 3327c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 3337c478bd9Sstevel@tonic-gate static void 3347c478bd9Sstevel@tonic-gate profile_online(void *arg, cpu_t *cpu, cyc_handler_t *hdlr, cyc_time_t *when) 3357c478bd9Sstevel@tonic-gate { 3367c478bd9Sstevel@tonic-gate profile_probe_t *prof = arg; 3377c478bd9Sstevel@tonic-gate profile_probe_percpu_t *pcpu; 3387c478bd9Sstevel@tonic-gate 3397c478bd9Sstevel@tonic-gate pcpu = kmem_zalloc(sizeof (profile_probe_percpu_t), KM_SLEEP); 3407c478bd9Sstevel@tonic-gate pcpu->profc_probe = prof; 3417c478bd9Sstevel@tonic-gate 3427c478bd9Sstevel@tonic-gate hdlr->cyh_func = profile_fire; 3437c478bd9Sstevel@tonic-gate hdlr->cyh_arg = pcpu; 3447c478bd9Sstevel@tonic-gate hdlr->cyh_level = CY_HIGH_LEVEL; 3457c478bd9Sstevel@tonic-gate 3467c478bd9Sstevel@tonic-gate when->cyt_interval = prof->prof_interval; 3477c478bd9Sstevel@tonic-gate when->cyt_when = dtrace_gethrtime() + when->cyt_interval; 3487c478bd9Sstevel@tonic-gate 3497c478bd9Sstevel@tonic-gate pcpu->profc_expected = when->cyt_when; 3507c478bd9Sstevel@tonic-gate pcpu->profc_interval = when->cyt_interval; 3517c478bd9Sstevel@tonic-gate } 3527c478bd9Sstevel@tonic-gate 3537c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 3547c478bd9Sstevel@tonic-gate static void 3557c478bd9Sstevel@tonic-gate profile_offline(void *arg, cpu_t *cpu, void *oarg) 3567c478bd9Sstevel@tonic-gate { 3577c478bd9Sstevel@tonic-gate profile_probe_percpu_t *pcpu = oarg; 3587c478bd9Sstevel@tonic-gate 3597c478bd9Sstevel@tonic-gate ASSERT(pcpu->profc_probe == arg); 3607c478bd9Sstevel@tonic-gate kmem_free(pcpu, sizeof (profile_probe_percpu_t)); 3617c478bd9Sstevel@tonic-gate } 3627c478bd9Sstevel@tonic-gate 3637c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 3647c478bd9Sstevel@tonic-gate static void 3657c478bd9Sstevel@tonic-gate profile_enable(void *arg, dtrace_id_t id, void *parg) 3667c478bd9Sstevel@tonic-gate { 3677c478bd9Sstevel@tonic-gate profile_probe_t *prof = parg; 3687c478bd9Sstevel@tonic-gate cyc_omni_handler_t omni; 3697c478bd9Sstevel@tonic-gate cyc_handler_t hdlr; 3707c478bd9Sstevel@tonic-gate cyc_time_t when; 3717c478bd9Sstevel@tonic-gate 3727c478bd9Sstevel@tonic-gate ASSERT(prof->prof_interval != 0); 3737c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 3747c478bd9Sstevel@tonic-gate 3757c478bd9Sstevel@tonic-gate if (prof->prof_kind == PROF_TICK) { 3767c478bd9Sstevel@tonic-gate hdlr.cyh_func = profile_tick; 3777c478bd9Sstevel@tonic-gate hdlr.cyh_arg = prof; 3787c478bd9Sstevel@tonic-gate hdlr.cyh_level = CY_HIGH_LEVEL; 3797c478bd9Sstevel@tonic-gate 3807c478bd9Sstevel@tonic-gate when.cyt_interval = prof->prof_interval; 3817c478bd9Sstevel@tonic-gate when.cyt_when = dtrace_gethrtime() + when.cyt_interval; 3827c478bd9Sstevel@tonic-gate } else { 3837c478bd9Sstevel@tonic-gate ASSERT(prof->prof_kind == PROF_PROFILE); 3847c478bd9Sstevel@tonic-gate omni.cyo_online = profile_online; 3857c478bd9Sstevel@tonic-gate omni.cyo_offline = profile_offline; 3867c478bd9Sstevel@tonic-gate omni.cyo_arg = prof; 3877c478bd9Sstevel@tonic-gate } 3887c478bd9Sstevel@tonic-gate 3897c478bd9Sstevel@tonic-gate if (prof->prof_kind == PROF_TICK) { 3907c478bd9Sstevel@tonic-gate prof->prof_cyclic = cyclic_add(&hdlr, &when); 3917c478bd9Sstevel@tonic-gate } else { 3927c478bd9Sstevel@tonic-gate prof->prof_cyclic = cyclic_add_omni(&omni); 3937c478bd9Sstevel@tonic-gate } 3947c478bd9Sstevel@tonic-gate } 3957c478bd9Sstevel@tonic-gate 3967c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 3977c478bd9Sstevel@tonic-gate static void 3987c478bd9Sstevel@tonic-gate profile_disable(void *arg, dtrace_id_t id, void *parg) 3997c478bd9Sstevel@tonic-gate { 4007c478bd9Sstevel@tonic-gate profile_probe_t *prof = parg; 4017c478bd9Sstevel@tonic-gate 4027c478bd9Sstevel@tonic-gate ASSERT(prof->prof_cyclic != CYCLIC_NONE); 4037c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 4047c478bd9Sstevel@tonic-gate 4057c478bd9Sstevel@tonic-gate cyclic_remove(prof->prof_cyclic); 4067c478bd9Sstevel@tonic-gate prof->prof_cyclic = CYCLIC_NONE; 4077c478bd9Sstevel@tonic-gate } 4087c478bd9Sstevel@tonic-gate 4097c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 4107c478bd9Sstevel@tonic-gate static int 4117c478bd9Sstevel@tonic-gate profile_usermode(void *arg, dtrace_id_t id, void *parg) 4127c478bd9Sstevel@tonic-gate { 4137c478bd9Sstevel@tonic-gate return (CPU->cpu_profile_pc == 0); 4147c478bd9Sstevel@tonic-gate } 4157c478bd9Sstevel@tonic-gate 4167c478bd9Sstevel@tonic-gate static dtrace_pattr_t profile_attr = { 4177c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON }, 4187c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_UNKNOWN }, 4197c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 4207c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON }, 4217c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON }, 4227c478bd9Sstevel@tonic-gate }; 4237c478bd9Sstevel@tonic-gate 4247c478bd9Sstevel@tonic-gate static dtrace_pops_t profile_pops = { 4257c478bd9Sstevel@tonic-gate profile_provide, 4267c478bd9Sstevel@tonic-gate NULL, 4277c478bd9Sstevel@tonic-gate profile_enable, 4287c478bd9Sstevel@tonic-gate profile_disable, 4297c478bd9Sstevel@tonic-gate NULL, 4307c478bd9Sstevel@tonic-gate NULL, 4317c478bd9Sstevel@tonic-gate NULL, 4327c478bd9Sstevel@tonic-gate NULL, 4337c478bd9Sstevel@tonic-gate profile_usermode, 4347c478bd9Sstevel@tonic-gate profile_destroy 4357c478bd9Sstevel@tonic-gate }; 4367c478bd9Sstevel@tonic-gate 4377c478bd9Sstevel@tonic-gate static int 4387c478bd9Sstevel@tonic-gate profile_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 4397c478bd9Sstevel@tonic-gate { 4407c478bd9Sstevel@tonic-gate switch (cmd) { 4417c478bd9Sstevel@tonic-gate case DDI_ATTACH: 4427c478bd9Sstevel@tonic-gate break; 4437c478bd9Sstevel@tonic-gate case DDI_RESUME: 4447c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 4457c478bd9Sstevel@tonic-gate default: 4467c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 4477c478bd9Sstevel@tonic-gate } 4487c478bd9Sstevel@tonic-gate 4497c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(devi, "profile", S_IFCHR, 0, 4507c478bd9Sstevel@tonic-gate DDI_PSEUDO, NULL) == DDI_FAILURE || 4517c478bd9Sstevel@tonic-gate dtrace_register("profile", &profile_attr, 452ad4023c4Sdp DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER, NULL, 4537c478bd9Sstevel@tonic-gate &profile_pops, NULL, &profile_id) != 0) { 4547c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL); 4557c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 4567c478bd9Sstevel@tonic-gate } 4577c478bd9Sstevel@tonic-gate 4587c478bd9Sstevel@tonic-gate profile_max = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 4597c478bd9Sstevel@tonic-gate "profile-max-probes", PROFILE_MAX_DEFAULT); 4607c478bd9Sstevel@tonic-gate 4617c478bd9Sstevel@tonic-gate ddi_report_dev(devi); 4627c478bd9Sstevel@tonic-gate profile_devi = devi; 4637c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 4647c478bd9Sstevel@tonic-gate } 4657c478bd9Sstevel@tonic-gate 4667c478bd9Sstevel@tonic-gate static int 4677c478bd9Sstevel@tonic-gate profile_detach(dev_info_t *devi, ddi_detach_cmd_t cmd) 4687c478bd9Sstevel@tonic-gate { 4697c478bd9Sstevel@tonic-gate switch (cmd) { 4707c478bd9Sstevel@tonic-gate case DDI_DETACH: 4717c478bd9Sstevel@tonic-gate break; 4727c478bd9Sstevel@tonic-gate case DDI_SUSPEND: 4737c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 4747c478bd9Sstevel@tonic-gate default: 4757c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 4767c478bd9Sstevel@tonic-gate } 4777c478bd9Sstevel@tonic-gate 4787c478bd9Sstevel@tonic-gate if (dtrace_unregister(profile_id) != 0) 4797c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 4807c478bd9Sstevel@tonic-gate 4817c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL); 4827c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 4837c478bd9Sstevel@tonic-gate } 4847c478bd9Sstevel@tonic-gate 4857c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 4867c478bd9Sstevel@tonic-gate static int 4877c478bd9Sstevel@tonic-gate profile_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 4887c478bd9Sstevel@tonic-gate { 4897c478bd9Sstevel@tonic-gate int error; 4907c478bd9Sstevel@tonic-gate 4917c478bd9Sstevel@tonic-gate switch (infocmd) { 4927c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 4937c478bd9Sstevel@tonic-gate *result = (void *)profile_devi; 4947c478bd9Sstevel@tonic-gate error = DDI_SUCCESS; 4957c478bd9Sstevel@tonic-gate break; 4967c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 4977c478bd9Sstevel@tonic-gate *result = (void *)0; 4987c478bd9Sstevel@tonic-gate error = DDI_SUCCESS; 4997c478bd9Sstevel@tonic-gate break; 5007c478bd9Sstevel@tonic-gate default: 5017c478bd9Sstevel@tonic-gate error = DDI_FAILURE; 5027c478bd9Sstevel@tonic-gate } 5037c478bd9Sstevel@tonic-gate return (error); 5047c478bd9Sstevel@tonic-gate } 5057c478bd9Sstevel@tonic-gate 5067c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 5077c478bd9Sstevel@tonic-gate static int 5087c478bd9Sstevel@tonic-gate profile_open(dev_t *devp, int flag, int otyp, cred_t *cred_p) 5097c478bd9Sstevel@tonic-gate { 5107c478bd9Sstevel@tonic-gate return (0); 5117c478bd9Sstevel@tonic-gate } 5127c478bd9Sstevel@tonic-gate 5137c478bd9Sstevel@tonic-gate static struct cb_ops profile_cb_ops = { 5147c478bd9Sstevel@tonic-gate profile_open, /* open */ 5157c478bd9Sstevel@tonic-gate nodev, /* close */ 5167c478bd9Sstevel@tonic-gate nulldev, /* strategy */ 5177c478bd9Sstevel@tonic-gate nulldev, /* print */ 5187c478bd9Sstevel@tonic-gate nodev, /* dump */ 5197c478bd9Sstevel@tonic-gate nodev, /* read */ 5207c478bd9Sstevel@tonic-gate nodev, /* write */ 5217c478bd9Sstevel@tonic-gate nodev, /* ioctl */ 5227c478bd9Sstevel@tonic-gate nodev, /* devmap */ 5237c478bd9Sstevel@tonic-gate nodev, /* mmap */ 5247c478bd9Sstevel@tonic-gate nodev, /* segmap */ 5257c478bd9Sstevel@tonic-gate nochpoll, /* poll */ 5267c478bd9Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */ 5277c478bd9Sstevel@tonic-gate 0, /* streamtab */ 5287c478bd9Sstevel@tonic-gate D_NEW | D_MP /* Driver compatibility flag */ 5297c478bd9Sstevel@tonic-gate }; 5307c478bd9Sstevel@tonic-gate 5317c478bd9Sstevel@tonic-gate static struct dev_ops profile_ops = { 5327c478bd9Sstevel@tonic-gate DEVO_REV, /* devo_rev, */ 5337c478bd9Sstevel@tonic-gate 0, /* refcnt */ 5347c478bd9Sstevel@tonic-gate profile_info, /* get_dev_info */ 5357c478bd9Sstevel@tonic-gate nulldev, /* identify */ 5367c478bd9Sstevel@tonic-gate nulldev, /* probe */ 5377c478bd9Sstevel@tonic-gate profile_attach, /* attach */ 5387c478bd9Sstevel@tonic-gate profile_detach, /* detach */ 5397c478bd9Sstevel@tonic-gate nodev, /* reset */ 5407c478bd9Sstevel@tonic-gate &profile_cb_ops, /* driver operations */ 5417c478bd9Sstevel@tonic-gate NULL, /* bus operations */ 5427c478bd9Sstevel@tonic-gate nodev /* dev power */ 5437c478bd9Sstevel@tonic-gate }; 5447c478bd9Sstevel@tonic-gate 5457c478bd9Sstevel@tonic-gate /* 5467c478bd9Sstevel@tonic-gate * Module linkage information for the kernel. 5477c478bd9Sstevel@tonic-gate */ 5487c478bd9Sstevel@tonic-gate static struct modldrv modldrv = { 5497c478bd9Sstevel@tonic-gate &mod_driverops, /* module type (this is a pseudo driver) */ 5507c478bd9Sstevel@tonic-gate "Profile Interrupt Tracing", /* name of module */ 5517c478bd9Sstevel@tonic-gate &profile_ops, /* driver ops */ 5527c478bd9Sstevel@tonic-gate }; 5537c478bd9Sstevel@tonic-gate 5547c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = { 5557c478bd9Sstevel@tonic-gate MODREV_1, 5567c478bd9Sstevel@tonic-gate (void *)&modldrv, 5577c478bd9Sstevel@tonic-gate NULL 5587c478bd9Sstevel@tonic-gate }; 5597c478bd9Sstevel@tonic-gate 5607c478bd9Sstevel@tonic-gate int 5617c478bd9Sstevel@tonic-gate _init(void) 5627c478bd9Sstevel@tonic-gate { 5637c478bd9Sstevel@tonic-gate return (mod_install(&modlinkage)); 5647c478bd9Sstevel@tonic-gate } 5657c478bd9Sstevel@tonic-gate 5667c478bd9Sstevel@tonic-gate int 5677c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 5687c478bd9Sstevel@tonic-gate { 5697c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 5707c478bd9Sstevel@tonic-gate } 5717c478bd9Sstevel@tonic-gate 5727c478bd9Sstevel@tonic-gate int 5737c478bd9Sstevel@tonic-gate _fini(void) 5747c478bd9Sstevel@tonic-gate { 5757c478bd9Sstevel@tonic-gate return (mod_remove(&modlinkage)); 5767c478bd9Sstevel@tonic-gate } 577