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*b9e93c10SJonathan Haslam * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #include <sys/errno.h> 287c478bd9Sstevel@tonic-gate #include <sys/stat.h> 297c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 307c478bd9Sstevel@tonic-gate #include <sys/conf.h> 317c478bd9Sstevel@tonic-gate #include <sys/systm.h> 327c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 337c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 347c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h> 357c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 367c478bd9Sstevel@tonic-gate #include <sys/strsubr.h> 377c478bd9Sstevel@tonic-gate #include <sys/dtrace.h> 387c478bd9Sstevel@tonic-gate #include <sys/cyclic.h> 397c478bd9Sstevel@tonic-gate #include <sys/atomic.h> 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate static dev_info_t *profile_devi; 427c478bd9Sstevel@tonic-gate static dtrace_provider_id_t profile_id; 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate /* 45ae115bc7Smrj * Regardless of platform, the stack frames look like this in the case of the 467c478bd9Sstevel@tonic-gate * profile provider: 477c478bd9Sstevel@tonic-gate * 487c478bd9Sstevel@tonic-gate * profile_fire 497c478bd9Sstevel@tonic-gate * cyclic_expire 507c478bd9Sstevel@tonic-gate * cyclic_fire 517c478bd9Sstevel@tonic-gate * [ cbe ] 52ae115bc7Smrj * [ interrupt code ] 537c478bd9Sstevel@tonic-gate * 54ae115bc7Smrj * On x86, there are five frames from the generic interrupt code; further, the 55ae115bc7Smrj * interrupted instruction appears as its own stack frame, giving us a total of 56ae115bc7Smrj * 10. 577c478bd9Sstevel@tonic-gate * 587c478bd9Sstevel@tonic-gate * On SPARC, the picture is further complicated because the compiler 597c478bd9Sstevel@tonic-gate * optimizes away tail-calls -- so the following frames are optimized away: 607c478bd9Sstevel@tonic-gate * 617c478bd9Sstevel@tonic-gate * profile_fire 627c478bd9Sstevel@tonic-gate * cyclic_expire 637c478bd9Sstevel@tonic-gate * 647c478bd9Sstevel@tonic-gate * This gives three frames. However, on DEBUG kernels, the cyclic_expire 657c478bd9Sstevel@tonic-gate * frame cannot be tail-call eliminated, yielding four frames in this case. 667c478bd9Sstevel@tonic-gate * 677c478bd9Sstevel@tonic-gate * All of the above constraints lead to the mess below. Yes, the profile 68ae115bc7Smrj * provider should ideally figure this out on-the-fly by hitting one of its own 697c478bd9Sstevel@tonic-gate * probes and then walking its own stack trace. This is complicated, however, 707c478bd9Sstevel@tonic-gate * and the static definition doesn't seem to be overly brittle. Still, we 717c478bd9Sstevel@tonic-gate * allow for a manual override in case we get it completely wrong. 727c478bd9Sstevel@tonic-gate */ 73ae115bc7Smrj #ifdef __x86 74ae115bc7Smrj #define PROF_ARTIFICIAL_FRAMES 10 757c478bd9Sstevel@tonic-gate #else 767c478bd9Sstevel@tonic-gate #ifdef __sparc 777c478bd9Sstevel@tonic-gate #ifdef DEBUG 787c478bd9Sstevel@tonic-gate #define PROF_ARTIFICIAL_FRAMES 4 797c478bd9Sstevel@tonic-gate #else 807c478bd9Sstevel@tonic-gate #define PROF_ARTIFICIAL_FRAMES 3 817c478bd9Sstevel@tonic-gate #endif 827c478bd9Sstevel@tonic-gate #endif 837c478bd9Sstevel@tonic-gate #endif 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate #define PROF_NAMELEN 15 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate #define PROF_PROFILE 0 887c478bd9Sstevel@tonic-gate #define PROF_TICK 1 897c478bd9Sstevel@tonic-gate #define PROF_PREFIX_PROFILE "profile-" 907c478bd9Sstevel@tonic-gate #define PROF_PREFIX_TICK "tick-" 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate typedef struct profile_probe { 937c478bd9Sstevel@tonic-gate char prof_name[PROF_NAMELEN]; 947c478bd9Sstevel@tonic-gate dtrace_id_t prof_id; 957c478bd9Sstevel@tonic-gate int prof_kind; 967c478bd9Sstevel@tonic-gate hrtime_t prof_interval; 977c478bd9Sstevel@tonic-gate cyclic_id_t prof_cyclic; 987c478bd9Sstevel@tonic-gate } profile_probe_t; 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate typedef struct profile_probe_percpu { 1017c478bd9Sstevel@tonic-gate hrtime_t profc_expected; 1027c478bd9Sstevel@tonic-gate hrtime_t profc_interval; 1037c478bd9Sstevel@tonic-gate profile_probe_t *profc_probe; 1047c478bd9Sstevel@tonic-gate } profile_probe_percpu_t; 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate hrtime_t profile_interval_min = NANOSEC / 5000; /* 5000 hz */ 1077c478bd9Sstevel@tonic-gate int profile_aframes = 0; /* override */ 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate static int profile_rates[] = { 1107c478bd9Sstevel@tonic-gate 97, 199, 499, 997, 1999, 1117c478bd9Sstevel@tonic-gate 4001, 4999, 0, 0, 0, 1127c478bd9Sstevel@tonic-gate 0, 0, 0, 0, 0, 1137c478bd9Sstevel@tonic-gate 0, 0, 0, 0, 0 1147c478bd9Sstevel@tonic-gate }; 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate static int profile_ticks[] = { 1177c478bd9Sstevel@tonic-gate 1, 10, 100, 500, 1000, 1187c478bd9Sstevel@tonic-gate 5000, 0, 0, 0, 0, 1197c478bd9Sstevel@tonic-gate 0, 0, 0, 0, 0 1207c478bd9Sstevel@tonic-gate }; 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate /* 1237c478bd9Sstevel@tonic-gate * profile_max defines the upper bound on the number of profile probes that 1247c478bd9Sstevel@tonic-gate * can exist (this is to prevent malicious or clumsy users from exhausing 1257c478bd9Sstevel@tonic-gate * system resources by creating a slew of profile probes). At mod load time, 1267c478bd9Sstevel@tonic-gate * this gets its value from PROFILE_MAX_DEFAULT or profile-max-probes if it's 1277c478bd9Sstevel@tonic-gate * present in the profile.conf file. 1287c478bd9Sstevel@tonic-gate */ 1297c478bd9Sstevel@tonic-gate #define PROFILE_MAX_DEFAULT 1000 /* default max. number of probes */ 1307c478bd9Sstevel@tonic-gate static uint32_t profile_max; /* maximum number of profile probes */ 1317c478bd9Sstevel@tonic-gate static uint32_t profile_total; /* current number of profile probes */ 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate static void 1347c478bd9Sstevel@tonic-gate profile_fire(void *arg) 1357c478bd9Sstevel@tonic-gate { 1367c478bd9Sstevel@tonic-gate profile_probe_percpu_t *pcpu = arg; 1377c478bd9Sstevel@tonic-gate profile_probe_t *prof = pcpu->profc_probe; 1387c478bd9Sstevel@tonic-gate hrtime_t late; 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate late = dtrace_gethrtime() - pcpu->profc_expected; 1417c478bd9Sstevel@tonic-gate pcpu->profc_expected += pcpu->profc_interval; 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate dtrace_probe(prof->prof_id, CPU->cpu_profile_pc, 1447c478bd9Sstevel@tonic-gate CPU->cpu_profile_upc, late, 0, 0); 1457c478bd9Sstevel@tonic-gate } 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate static void 1487c478bd9Sstevel@tonic-gate profile_tick(void *arg) 1497c478bd9Sstevel@tonic-gate { 1507c478bd9Sstevel@tonic-gate profile_probe_t *prof = arg; 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate dtrace_probe(prof->prof_id, CPU->cpu_profile_pc, 1537c478bd9Sstevel@tonic-gate CPU->cpu_profile_upc, 0, 0, 0); 1547c478bd9Sstevel@tonic-gate } 1557c478bd9Sstevel@tonic-gate 1567c478bd9Sstevel@tonic-gate static void 1577c478bd9Sstevel@tonic-gate profile_create(hrtime_t interval, const char *name, int kind) 1587c478bd9Sstevel@tonic-gate { 1597c478bd9Sstevel@tonic-gate profile_probe_t *prof; 160ae115bc7Smrj int nr_frames = PROF_ARTIFICIAL_FRAMES + dtrace_mach_aframes(); 161ae115bc7Smrj 162ae115bc7Smrj if (profile_aframes) 163ae115bc7Smrj nr_frames = profile_aframes; 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate if (interval < profile_interval_min) 1667c478bd9Sstevel@tonic-gate return; 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate if (dtrace_probe_lookup(profile_id, NULL, NULL, name) != 0) 1697c478bd9Sstevel@tonic-gate return; 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate atomic_add_32(&profile_total, 1); 1727c478bd9Sstevel@tonic-gate if (profile_total > profile_max) { 1737c478bd9Sstevel@tonic-gate atomic_add_32(&profile_total, -1); 1747c478bd9Sstevel@tonic-gate return; 1757c478bd9Sstevel@tonic-gate } 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate prof = kmem_zalloc(sizeof (profile_probe_t), KM_SLEEP); 1787c478bd9Sstevel@tonic-gate (void) strcpy(prof->prof_name, name); 1797c478bd9Sstevel@tonic-gate prof->prof_interval = interval; 1807c478bd9Sstevel@tonic-gate prof->prof_cyclic = CYCLIC_NONE; 1817c478bd9Sstevel@tonic-gate prof->prof_kind = kind; 1827c478bd9Sstevel@tonic-gate prof->prof_id = dtrace_probe_create(profile_id, 183ae115bc7Smrj NULL, NULL, name, nr_frames, prof); 1847c478bd9Sstevel@tonic-gate } 1857c478bd9Sstevel@tonic-gate 1867c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 1877c478bd9Sstevel@tonic-gate static void 1887c478bd9Sstevel@tonic-gate profile_provide(void *arg, const dtrace_probedesc_t *desc) 1897c478bd9Sstevel@tonic-gate { 1907c478bd9Sstevel@tonic-gate int i, j, rate, kind; 1917c478bd9Sstevel@tonic-gate hrtime_t val = 0, mult = 1, len; 1927c478bd9Sstevel@tonic-gate const char *name, *suffix = NULL; 1937c478bd9Sstevel@tonic-gate 1947c478bd9Sstevel@tonic-gate const struct { 1957c478bd9Sstevel@tonic-gate char *prefix; 1967c478bd9Sstevel@tonic-gate int kind; 1977c478bd9Sstevel@tonic-gate } types[] = { 1987c478bd9Sstevel@tonic-gate { PROF_PREFIX_PROFILE, PROF_PROFILE }, 1997c478bd9Sstevel@tonic-gate { PROF_PREFIX_TICK, PROF_TICK }, 2007c478bd9Sstevel@tonic-gate { NULL, NULL } 2017c478bd9Sstevel@tonic-gate }; 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate const struct { 2047c478bd9Sstevel@tonic-gate char *name; 2057c478bd9Sstevel@tonic-gate hrtime_t mult; 2067c478bd9Sstevel@tonic-gate } suffixes[] = { 2077c478bd9Sstevel@tonic-gate { "ns", NANOSEC / NANOSEC }, 2087c478bd9Sstevel@tonic-gate { "nsec", NANOSEC / NANOSEC }, 2097c478bd9Sstevel@tonic-gate { "us", NANOSEC / MICROSEC }, 2107c478bd9Sstevel@tonic-gate { "usec", NANOSEC / MICROSEC }, 2117c478bd9Sstevel@tonic-gate { "ms", NANOSEC / MILLISEC }, 2127c478bd9Sstevel@tonic-gate { "msec", NANOSEC / MILLISEC }, 2137c478bd9Sstevel@tonic-gate { "s", NANOSEC / SEC }, 2147c478bd9Sstevel@tonic-gate { "sec", NANOSEC / SEC }, 2157c478bd9Sstevel@tonic-gate { "m", NANOSEC * (hrtime_t)60 }, 2167c478bd9Sstevel@tonic-gate { "min", NANOSEC * (hrtime_t)60 }, 2177c478bd9Sstevel@tonic-gate { "h", NANOSEC * (hrtime_t)(60 * 60) }, 2187c478bd9Sstevel@tonic-gate { "hour", NANOSEC * (hrtime_t)(60 * 60) }, 2197c478bd9Sstevel@tonic-gate { "d", NANOSEC * (hrtime_t)(24 * 60 * 60) }, 2207c478bd9Sstevel@tonic-gate { "day", NANOSEC * (hrtime_t)(24 * 60 * 60) }, 2217c478bd9Sstevel@tonic-gate { "hz", 0 }, 2227c478bd9Sstevel@tonic-gate { NULL } 2237c478bd9Sstevel@tonic-gate }; 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate if (desc == NULL) { 2267c478bd9Sstevel@tonic-gate char n[PROF_NAMELEN]; 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate /* 2297c478bd9Sstevel@tonic-gate * If no description was provided, provide all of our probes. 2307c478bd9Sstevel@tonic-gate */ 2317c478bd9Sstevel@tonic-gate for (i = 0; i < sizeof (profile_rates) / sizeof (int); i++) { 2327c478bd9Sstevel@tonic-gate if ((rate = profile_rates[i]) == 0) 2337c478bd9Sstevel@tonic-gate continue; 2347c478bd9Sstevel@tonic-gate 2357c478bd9Sstevel@tonic-gate (void) snprintf(n, PROF_NAMELEN, "%s%d", 2367c478bd9Sstevel@tonic-gate PROF_PREFIX_PROFILE, rate); 2377c478bd9Sstevel@tonic-gate profile_create(NANOSEC / rate, n, PROF_PROFILE); 2387c478bd9Sstevel@tonic-gate } 2397c478bd9Sstevel@tonic-gate 2407c478bd9Sstevel@tonic-gate for (i = 0; i < sizeof (profile_ticks) / sizeof (int); i++) { 2417c478bd9Sstevel@tonic-gate if ((rate = profile_ticks[i]) == 0) 2427c478bd9Sstevel@tonic-gate continue; 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate (void) snprintf(n, PROF_NAMELEN, "%s%d", 2457c478bd9Sstevel@tonic-gate PROF_PREFIX_TICK, rate); 2467c478bd9Sstevel@tonic-gate profile_create(NANOSEC / rate, n, PROF_TICK); 2477c478bd9Sstevel@tonic-gate } 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate return; 2507c478bd9Sstevel@tonic-gate } 2517c478bd9Sstevel@tonic-gate 2527c478bd9Sstevel@tonic-gate name = desc->dtpd_name; 2537c478bd9Sstevel@tonic-gate 2547c478bd9Sstevel@tonic-gate for (i = 0; types[i].prefix != NULL; i++) { 2557c478bd9Sstevel@tonic-gate len = strlen(types[i].prefix); 2567c478bd9Sstevel@tonic-gate 2577c478bd9Sstevel@tonic-gate if (strncmp(name, types[i].prefix, len) != 0) 2587c478bd9Sstevel@tonic-gate continue; 2597c478bd9Sstevel@tonic-gate break; 2607c478bd9Sstevel@tonic-gate } 2617c478bd9Sstevel@tonic-gate 2627c478bd9Sstevel@tonic-gate if (types[i].prefix == NULL) 2637c478bd9Sstevel@tonic-gate return; 2647c478bd9Sstevel@tonic-gate 2657c478bd9Sstevel@tonic-gate kind = types[i].kind; 2667c478bd9Sstevel@tonic-gate j = strlen(name) - len; 2677c478bd9Sstevel@tonic-gate 2687c478bd9Sstevel@tonic-gate /* 2697c478bd9Sstevel@tonic-gate * We need to start before any time suffix. 2707c478bd9Sstevel@tonic-gate */ 2717c478bd9Sstevel@tonic-gate for (j = strlen(name); j >= len; j--) { 2727c478bd9Sstevel@tonic-gate if (name[j] >= '0' && name[j] <= '9') 2737c478bd9Sstevel@tonic-gate break; 2747c478bd9Sstevel@tonic-gate suffix = &name[j]; 2757c478bd9Sstevel@tonic-gate } 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate ASSERT(suffix != NULL); 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate /* 2807c478bd9Sstevel@tonic-gate * Now determine the numerical value present in the probe name. 2817c478bd9Sstevel@tonic-gate */ 2827c478bd9Sstevel@tonic-gate for (; j >= len; j--) { 2837c478bd9Sstevel@tonic-gate if (name[j] < '0' || name[j] > '9') 2847c478bd9Sstevel@tonic-gate return; 2857c478bd9Sstevel@tonic-gate 2867c478bd9Sstevel@tonic-gate val += (name[j] - '0') * mult; 2877c478bd9Sstevel@tonic-gate mult *= (hrtime_t)10; 2887c478bd9Sstevel@tonic-gate } 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate if (val == 0) 2917c478bd9Sstevel@tonic-gate return; 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate /* 2947c478bd9Sstevel@tonic-gate * Look-up the suffix to determine the multiplier. 2957c478bd9Sstevel@tonic-gate */ 2967c478bd9Sstevel@tonic-gate for (i = 0, mult = 0; suffixes[i].name != NULL; i++) { 2977c478bd9Sstevel@tonic-gate if (strcasecmp(suffixes[i].name, suffix) == 0) { 2987c478bd9Sstevel@tonic-gate mult = suffixes[i].mult; 2997c478bd9Sstevel@tonic-gate break; 3007c478bd9Sstevel@tonic-gate } 3017c478bd9Sstevel@tonic-gate } 3027c478bd9Sstevel@tonic-gate 3037c478bd9Sstevel@tonic-gate if (suffixes[i].name == NULL && *suffix != '\0') 3047c478bd9Sstevel@tonic-gate return; 3057c478bd9Sstevel@tonic-gate 3067c478bd9Sstevel@tonic-gate if (mult == 0) { 3077c478bd9Sstevel@tonic-gate /* 3087c478bd9Sstevel@tonic-gate * The default is frequency-per-second. 3097c478bd9Sstevel@tonic-gate */ 3107c478bd9Sstevel@tonic-gate val = NANOSEC / val; 3117c478bd9Sstevel@tonic-gate } else { 3127c478bd9Sstevel@tonic-gate val *= mult; 3137c478bd9Sstevel@tonic-gate } 3147c478bd9Sstevel@tonic-gate 3157c478bd9Sstevel@tonic-gate profile_create(val, name, kind); 3167c478bd9Sstevel@tonic-gate } 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 3197c478bd9Sstevel@tonic-gate static void 3207c478bd9Sstevel@tonic-gate profile_destroy(void *arg, dtrace_id_t id, void *parg) 3217c478bd9Sstevel@tonic-gate { 3227c478bd9Sstevel@tonic-gate profile_probe_t *prof = parg; 3237c478bd9Sstevel@tonic-gate 3247c478bd9Sstevel@tonic-gate ASSERT(prof->prof_cyclic == CYCLIC_NONE); 3257c478bd9Sstevel@tonic-gate kmem_free(prof, sizeof (profile_probe_t)); 3267c478bd9Sstevel@tonic-gate 3277c478bd9Sstevel@tonic-gate ASSERT(profile_total >= 1); 3287c478bd9Sstevel@tonic-gate atomic_add_32(&profile_total, -1); 3297c478bd9Sstevel@tonic-gate } 3307c478bd9Sstevel@tonic-gate 3317c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 3327c478bd9Sstevel@tonic-gate static void 3337c478bd9Sstevel@tonic-gate profile_online(void *arg, cpu_t *cpu, cyc_handler_t *hdlr, cyc_time_t *when) 3347c478bd9Sstevel@tonic-gate { 3357c478bd9Sstevel@tonic-gate profile_probe_t *prof = arg; 3367c478bd9Sstevel@tonic-gate profile_probe_percpu_t *pcpu; 3377c478bd9Sstevel@tonic-gate 3387c478bd9Sstevel@tonic-gate pcpu = kmem_zalloc(sizeof (profile_probe_percpu_t), KM_SLEEP); 3397c478bd9Sstevel@tonic-gate pcpu->profc_probe = prof; 3407c478bd9Sstevel@tonic-gate 3417c478bd9Sstevel@tonic-gate hdlr->cyh_func = profile_fire; 3427c478bd9Sstevel@tonic-gate hdlr->cyh_arg = pcpu; 3437c478bd9Sstevel@tonic-gate hdlr->cyh_level = CY_HIGH_LEVEL; 3447c478bd9Sstevel@tonic-gate 3457c478bd9Sstevel@tonic-gate when->cyt_interval = prof->prof_interval; 3467c478bd9Sstevel@tonic-gate when->cyt_when = dtrace_gethrtime() + when->cyt_interval; 3477c478bd9Sstevel@tonic-gate 3487c478bd9Sstevel@tonic-gate pcpu->profc_expected = when->cyt_when; 3497c478bd9Sstevel@tonic-gate pcpu->profc_interval = when->cyt_interval; 3507c478bd9Sstevel@tonic-gate } 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 3537c478bd9Sstevel@tonic-gate static void 3547c478bd9Sstevel@tonic-gate profile_offline(void *arg, cpu_t *cpu, void *oarg) 3557c478bd9Sstevel@tonic-gate { 3567c478bd9Sstevel@tonic-gate profile_probe_percpu_t *pcpu = oarg; 3577c478bd9Sstevel@tonic-gate 3587c478bd9Sstevel@tonic-gate ASSERT(pcpu->profc_probe == arg); 3597c478bd9Sstevel@tonic-gate kmem_free(pcpu, sizeof (profile_probe_percpu_t)); 3607c478bd9Sstevel@tonic-gate } 3617c478bd9Sstevel@tonic-gate 3627c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 363*b9e93c10SJonathan Haslam static int 3647c478bd9Sstevel@tonic-gate profile_enable(void *arg, dtrace_id_t id, void *parg) 3657c478bd9Sstevel@tonic-gate { 3667c478bd9Sstevel@tonic-gate profile_probe_t *prof = parg; 3677c478bd9Sstevel@tonic-gate cyc_omni_handler_t omni; 3687c478bd9Sstevel@tonic-gate cyc_handler_t hdlr; 3697c478bd9Sstevel@tonic-gate cyc_time_t when; 3707c478bd9Sstevel@tonic-gate 3717c478bd9Sstevel@tonic-gate ASSERT(prof->prof_interval != 0); 3727c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 3737c478bd9Sstevel@tonic-gate 3747c478bd9Sstevel@tonic-gate if (prof->prof_kind == PROF_TICK) { 3757c478bd9Sstevel@tonic-gate hdlr.cyh_func = profile_tick; 3767c478bd9Sstevel@tonic-gate hdlr.cyh_arg = prof; 3777c478bd9Sstevel@tonic-gate hdlr.cyh_level = CY_HIGH_LEVEL; 3787c478bd9Sstevel@tonic-gate 3797c478bd9Sstevel@tonic-gate when.cyt_interval = prof->prof_interval; 3807c478bd9Sstevel@tonic-gate when.cyt_when = dtrace_gethrtime() + when.cyt_interval; 3817c478bd9Sstevel@tonic-gate } else { 3827c478bd9Sstevel@tonic-gate ASSERT(prof->prof_kind == PROF_PROFILE); 3837c478bd9Sstevel@tonic-gate omni.cyo_online = profile_online; 3847c478bd9Sstevel@tonic-gate omni.cyo_offline = profile_offline; 3857c478bd9Sstevel@tonic-gate omni.cyo_arg = prof; 3867c478bd9Sstevel@tonic-gate } 3877c478bd9Sstevel@tonic-gate 3887c478bd9Sstevel@tonic-gate if (prof->prof_kind == PROF_TICK) { 3897c478bd9Sstevel@tonic-gate prof->prof_cyclic = cyclic_add(&hdlr, &when); 3907c478bd9Sstevel@tonic-gate } else { 3917c478bd9Sstevel@tonic-gate prof->prof_cyclic = cyclic_add_omni(&omni); 3927c478bd9Sstevel@tonic-gate } 393*b9e93c10SJonathan Haslam return (0); 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 */ 54219397407SSherry Moore nodev, /* dev power */ 54319397407SSherry Moore ddi_quiesce_not_needed, /* quiesce */ 5447c478bd9Sstevel@tonic-gate }; 5457c478bd9Sstevel@tonic-gate 5467c478bd9Sstevel@tonic-gate /* 5477c478bd9Sstevel@tonic-gate * Module linkage information for the kernel. 5487c478bd9Sstevel@tonic-gate */ 5497c478bd9Sstevel@tonic-gate static struct modldrv modldrv = { 5507c478bd9Sstevel@tonic-gate &mod_driverops, /* module type (this is a pseudo driver) */ 5517c478bd9Sstevel@tonic-gate "Profile Interrupt Tracing", /* name of module */ 5527c478bd9Sstevel@tonic-gate &profile_ops, /* driver ops */ 5537c478bd9Sstevel@tonic-gate }; 5547c478bd9Sstevel@tonic-gate 5557c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = { 5567c478bd9Sstevel@tonic-gate MODREV_1, 5577c478bd9Sstevel@tonic-gate (void *)&modldrv, 5587c478bd9Sstevel@tonic-gate NULL 5597c478bd9Sstevel@tonic-gate }; 5607c478bd9Sstevel@tonic-gate 5617c478bd9Sstevel@tonic-gate int 5627c478bd9Sstevel@tonic-gate _init(void) 5637c478bd9Sstevel@tonic-gate { 5647c478bd9Sstevel@tonic-gate return (mod_install(&modlinkage)); 5657c478bd9Sstevel@tonic-gate } 5667c478bd9Sstevel@tonic-gate 5677c478bd9Sstevel@tonic-gate int 5687c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 5697c478bd9Sstevel@tonic-gate { 5707c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 5717c478bd9Sstevel@tonic-gate } 5727c478bd9Sstevel@tonic-gate 5737c478bd9Sstevel@tonic-gate int 5747c478bd9Sstevel@tonic-gate _fini(void) 5757c478bd9Sstevel@tonic-gate { 5767c478bd9Sstevel@tonic-gate return (mod_remove(&modlinkage)); 5777c478bd9Sstevel@tonic-gate } 578