xref: /onnv-gate/usr/src/uts/common/dtrace/profile.c (revision 1677:072efdedd007)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1677Sdp  * Common Development and Distribution License (the "License").
6*1677Sdp  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*1677Sdp  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/errno.h>
290Sstevel@tonic-gate #include <sys/stat.h>
300Sstevel@tonic-gate #include <sys/modctl.h>
310Sstevel@tonic-gate #include <sys/conf.h>
320Sstevel@tonic-gate #include <sys/systm.h>
330Sstevel@tonic-gate #include <sys/ddi.h>
340Sstevel@tonic-gate #include <sys/sunddi.h>
350Sstevel@tonic-gate #include <sys/cpuvar.h>
360Sstevel@tonic-gate #include <sys/kmem.h>
370Sstevel@tonic-gate #include <sys/strsubr.h>
380Sstevel@tonic-gate #include <sys/dtrace.h>
390Sstevel@tonic-gate #include <sys/cyclic.h>
400Sstevel@tonic-gate #include <sys/atomic.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate static dev_info_t *profile_devi;
430Sstevel@tonic-gate static dtrace_provider_id_t profile_id;
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate  * Regardless of platform, there are five artificial frames in the case of the
470Sstevel@tonic-gate  * profile provider:
480Sstevel@tonic-gate  *
490Sstevel@tonic-gate  *	profile_fire
500Sstevel@tonic-gate  *	cyclic_expire
510Sstevel@tonic-gate  *	cyclic_fire
520Sstevel@tonic-gate  *	[ cbe ]
530Sstevel@tonic-gate  *	[ locore ]
540Sstevel@tonic-gate  *
550Sstevel@tonic-gate  * On amd64, there are two frames associated with locore:  one in locore, and
560Sstevel@tonic-gate  * another in common interrupt dispatch code.  (i386 has not been modified to
570Sstevel@tonic-gate  * use this common layer.)  Further, on i386, the interrupted instruction
580Sstevel@tonic-gate  * appears as its own stack frame.  All of this means that we need to add one
590Sstevel@tonic-gate  * frame for amd64, and then take one away for both amd64 and i386.
600Sstevel@tonic-gate  *
610Sstevel@tonic-gate  * On SPARC, the picture is further complicated because the compiler
620Sstevel@tonic-gate  * optimizes away tail-calls -- so the following frames are optimized away:
630Sstevel@tonic-gate  *
640Sstevel@tonic-gate  * 	profile_fire
650Sstevel@tonic-gate  *	cyclic_expire
660Sstevel@tonic-gate  *
670Sstevel@tonic-gate  * This gives three frames.  However, on DEBUG kernels, the cyclic_expire
680Sstevel@tonic-gate  * frame cannot be tail-call eliminated, yielding four frames in this case.
690Sstevel@tonic-gate  *
700Sstevel@tonic-gate  * All of the above constraints lead to the mess below.  Yes, the profile
710Sstevel@tonic-gate  * provider should ideally figure this out on-the-fly by hiting one of its own
720Sstevel@tonic-gate  * probes and then walking its own stack trace.  This is complicated, however,
730Sstevel@tonic-gate  * and the static definition doesn't seem to be overly brittle.  Still, we
740Sstevel@tonic-gate  * allow for a manual override in case we get it completely wrong.
750Sstevel@tonic-gate  */
760Sstevel@tonic-gate #ifdef __amd64
770Sstevel@tonic-gate #define	PROF_ARTIFICIAL_FRAMES	7
780Sstevel@tonic-gate #else
790Sstevel@tonic-gate #ifdef __i386
800Sstevel@tonic-gate #define	PROF_ARTIFICIAL_FRAMES	6
810Sstevel@tonic-gate #else
820Sstevel@tonic-gate #ifdef __sparc
830Sstevel@tonic-gate #ifdef DEBUG
840Sstevel@tonic-gate #define	PROF_ARTIFICIAL_FRAMES	4
850Sstevel@tonic-gate #else
860Sstevel@tonic-gate #define	PROF_ARTIFICIAL_FRAMES	3
870Sstevel@tonic-gate #endif
880Sstevel@tonic-gate #endif
890Sstevel@tonic-gate #endif
900Sstevel@tonic-gate #endif
910Sstevel@tonic-gate 
920Sstevel@tonic-gate #define	PROF_NAMELEN		15
930Sstevel@tonic-gate 
940Sstevel@tonic-gate #define	PROF_PROFILE		0
950Sstevel@tonic-gate #define	PROF_TICK		1
960Sstevel@tonic-gate #define	PROF_PREFIX_PROFILE	"profile-"
970Sstevel@tonic-gate #define	PROF_PREFIX_TICK	"tick-"
980Sstevel@tonic-gate 
990Sstevel@tonic-gate typedef struct profile_probe {
1000Sstevel@tonic-gate 	char		prof_name[PROF_NAMELEN];
1010Sstevel@tonic-gate 	dtrace_id_t	prof_id;
1020Sstevel@tonic-gate 	int		prof_kind;
1030Sstevel@tonic-gate 	hrtime_t	prof_interval;
1040Sstevel@tonic-gate 	cyclic_id_t	prof_cyclic;
1050Sstevel@tonic-gate } profile_probe_t;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate typedef struct profile_probe_percpu {
1080Sstevel@tonic-gate 	hrtime_t	profc_expected;
1090Sstevel@tonic-gate 	hrtime_t	profc_interval;
1100Sstevel@tonic-gate 	profile_probe_t	*profc_probe;
1110Sstevel@tonic-gate } profile_probe_percpu_t;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate hrtime_t	profile_interval_min = NANOSEC / 5000;		/* 5000 hz */
1140Sstevel@tonic-gate int		profile_aframes = 0;				/* override */
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate static int profile_rates[] = {
1170Sstevel@tonic-gate     97, 199, 499, 997, 1999,
1180Sstevel@tonic-gate     4001, 4999, 0, 0, 0,
1190Sstevel@tonic-gate     0, 0, 0, 0, 0,
1200Sstevel@tonic-gate     0, 0, 0, 0, 0
1210Sstevel@tonic-gate };
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate static int profile_ticks[] = {
1240Sstevel@tonic-gate     1, 10, 100, 500, 1000,
1250Sstevel@tonic-gate     5000, 0, 0, 0, 0,
1260Sstevel@tonic-gate     0, 0, 0, 0, 0
1270Sstevel@tonic-gate };
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate /*
1300Sstevel@tonic-gate  * profile_max defines the upper bound on the number of profile probes that
1310Sstevel@tonic-gate  * can exist (this is to prevent malicious or clumsy users from exhausing
1320Sstevel@tonic-gate  * system resources by creating a slew of profile probes). At mod load time,
1330Sstevel@tonic-gate  * this gets its value from PROFILE_MAX_DEFAULT or profile-max-probes if it's
1340Sstevel@tonic-gate  * present in the profile.conf file.
1350Sstevel@tonic-gate  */
1360Sstevel@tonic-gate #define	PROFILE_MAX_DEFAULT	1000	/* default max. number of probes */
1370Sstevel@tonic-gate static uint32_t profile_max;		/* maximum number of profile probes */
1380Sstevel@tonic-gate static uint32_t profile_total;	/* current number of profile probes */
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate static void
1410Sstevel@tonic-gate profile_fire(void *arg)
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate 	profile_probe_percpu_t *pcpu = arg;
1440Sstevel@tonic-gate 	profile_probe_t *prof = pcpu->profc_probe;
1450Sstevel@tonic-gate 	hrtime_t late;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	late = dtrace_gethrtime() - pcpu->profc_expected;
1480Sstevel@tonic-gate 	pcpu->profc_expected += pcpu->profc_interval;
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	dtrace_probe(prof->prof_id, CPU->cpu_profile_pc,
1510Sstevel@tonic-gate 	    CPU->cpu_profile_upc, late, 0, 0);
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate static void
1550Sstevel@tonic-gate profile_tick(void *arg)
1560Sstevel@tonic-gate {
1570Sstevel@tonic-gate 	profile_probe_t *prof = arg;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	dtrace_probe(prof->prof_id, CPU->cpu_profile_pc,
1600Sstevel@tonic-gate 	    CPU->cpu_profile_upc, 0, 0, 0);
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate static void
1640Sstevel@tonic-gate profile_create(hrtime_t interval, const char *name, int kind)
1650Sstevel@tonic-gate {
1660Sstevel@tonic-gate 	profile_probe_t *prof;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	if (interval < profile_interval_min)
1690Sstevel@tonic-gate 		return;
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	if (dtrace_probe_lookup(profile_id, NULL, NULL, name) != 0)
1720Sstevel@tonic-gate 		return;
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	atomic_add_32(&profile_total, 1);
1750Sstevel@tonic-gate 	if (profile_total > profile_max) {
1760Sstevel@tonic-gate 		atomic_add_32(&profile_total, -1);
1770Sstevel@tonic-gate 		return;
1780Sstevel@tonic-gate 	}
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	prof = kmem_zalloc(sizeof (profile_probe_t), KM_SLEEP);
1810Sstevel@tonic-gate 	(void) strcpy(prof->prof_name, name);
1820Sstevel@tonic-gate 	prof->prof_interval = interval;
1830Sstevel@tonic-gate 	prof->prof_cyclic = CYCLIC_NONE;
1840Sstevel@tonic-gate 	prof->prof_kind = kind;
1850Sstevel@tonic-gate 	prof->prof_id = dtrace_probe_create(profile_id,
1860Sstevel@tonic-gate 	    NULL, NULL, name,
1870Sstevel@tonic-gate 	    profile_aframes ? profile_aframes : PROF_ARTIFICIAL_FRAMES, prof);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate /*ARGSUSED*/
1910Sstevel@tonic-gate static void
1920Sstevel@tonic-gate profile_provide(void *arg, const dtrace_probedesc_t *desc)
1930Sstevel@tonic-gate {
1940Sstevel@tonic-gate 	int i, j, rate, kind;
1950Sstevel@tonic-gate 	hrtime_t val = 0, mult = 1, len;
1960Sstevel@tonic-gate 	const char *name, *suffix = NULL;
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	const struct {
1990Sstevel@tonic-gate 		char *prefix;
2000Sstevel@tonic-gate 		int kind;
2010Sstevel@tonic-gate 	} types[] = {
2020Sstevel@tonic-gate 		{ PROF_PREFIX_PROFILE, PROF_PROFILE },
2030Sstevel@tonic-gate 		{ PROF_PREFIX_TICK, PROF_TICK },
2040Sstevel@tonic-gate 		{ NULL, NULL }
2050Sstevel@tonic-gate 	};
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	const struct {
2080Sstevel@tonic-gate 		char *name;
2090Sstevel@tonic-gate 		hrtime_t mult;
2100Sstevel@tonic-gate 	} suffixes[] = {
2110Sstevel@tonic-gate 		{ "ns", 	NANOSEC / NANOSEC },
2120Sstevel@tonic-gate 		{ "nsec",	NANOSEC / NANOSEC },
2130Sstevel@tonic-gate 		{ "us",		NANOSEC / MICROSEC },
2140Sstevel@tonic-gate 		{ "usec",	NANOSEC / MICROSEC },
2150Sstevel@tonic-gate 		{ "ms",		NANOSEC / MILLISEC },
2160Sstevel@tonic-gate 		{ "msec",	NANOSEC / MILLISEC },
2170Sstevel@tonic-gate 		{ "s",		NANOSEC / SEC },
2180Sstevel@tonic-gate 		{ "sec",	NANOSEC / SEC },
2190Sstevel@tonic-gate 		{ "m",		NANOSEC * (hrtime_t)60 },
2200Sstevel@tonic-gate 		{ "min",	NANOSEC * (hrtime_t)60 },
2210Sstevel@tonic-gate 		{ "h",		NANOSEC * (hrtime_t)(60 * 60) },
2220Sstevel@tonic-gate 		{ "hour",	NANOSEC * (hrtime_t)(60 * 60) },
2230Sstevel@tonic-gate 		{ "d",		NANOSEC * (hrtime_t)(24 * 60 * 60) },
2240Sstevel@tonic-gate 		{ "day",	NANOSEC * (hrtime_t)(24 * 60 * 60) },
2250Sstevel@tonic-gate 		{ "hz",		0 },
2260Sstevel@tonic-gate 		{ NULL }
2270Sstevel@tonic-gate 	};
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	if (desc == NULL) {
2300Sstevel@tonic-gate 		char n[PROF_NAMELEN];
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 		/*
2330Sstevel@tonic-gate 		 * If no description was provided, provide all of our probes.
2340Sstevel@tonic-gate 		 */
2350Sstevel@tonic-gate 		for (i = 0; i < sizeof (profile_rates) / sizeof (int); i++) {
2360Sstevel@tonic-gate 			if ((rate = profile_rates[i]) == 0)
2370Sstevel@tonic-gate 				continue;
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 			(void) snprintf(n, PROF_NAMELEN, "%s%d",
2400Sstevel@tonic-gate 			    PROF_PREFIX_PROFILE, rate);
2410Sstevel@tonic-gate 			profile_create(NANOSEC / rate, n, PROF_PROFILE);
2420Sstevel@tonic-gate 		}
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 		for (i = 0; i < sizeof (profile_ticks) / sizeof (int); i++) {
2450Sstevel@tonic-gate 			if ((rate = profile_ticks[i]) == 0)
2460Sstevel@tonic-gate 				continue;
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 			(void) snprintf(n, PROF_NAMELEN, "%s%d",
2490Sstevel@tonic-gate 			    PROF_PREFIX_TICK, rate);
2500Sstevel@tonic-gate 			profile_create(NANOSEC / rate, n, PROF_TICK);
2510Sstevel@tonic-gate 		}
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 		return;
2540Sstevel@tonic-gate 	}
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	name = desc->dtpd_name;
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	for (i = 0; types[i].prefix != NULL; i++) {
2590Sstevel@tonic-gate 		len = strlen(types[i].prefix);
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 		if (strncmp(name, types[i].prefix, len) != 0)
2620Sstevel@tonic-gate 			continue;
2630Sstevel@tonic-gate 		break;
2640Sstevel@tonic-gate 	}
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	if (types[i].prefix == NULL)
2670Sstevel@tonic-gate 		return;
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	kind = types[i].kind;
2700Sstevel@tonic-gate 	j = strlen(name) - len;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	/*
2730Sstevel@tonic-gate 	 * We need to start before any time suffix.
2740Sstevel@tonic-gate 	 */
2750Sstevel@tonic-gate 	for (j = strlen(name); j >= len; j--) {
2760Sstevel@tonic-gate 		if (name[j] >= '0' && name[j] <= '9')
2770Sstevel@tonic-gate 			break;
2780Sstevel@tonic-gate 		suffix = &name[j];
2790Sstevel@tonic-gate 	}
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	ASSERT(suffix != NULL);
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	/*
2840Sstevel@tonic-gate 	 * Now determine the numerical value present in the probe name.
2850Sstevel@tonic-gate 	 */
2860Sstevel@tonic-gate 	for (; j >= len; j--) {
2870Sstevel@tonic-gate 		if (name[j] < '0' || name[j] > '9')
2880Sstevel@tonic-gate 			return;
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 		val += (name[j] - '0') * mult;
2910Sstevel@tonic-gate 		mult *= (hrtime_t)10;
2920Sstevel@tonic-gate 	}
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	if (val == 0)
2950Sstevel@tonic-gate 		return;
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	/*
2980Sstevel@tonic-gate 	 * Look-up the suffix to determine the multiplier.
2990Sstevel@tonic-gate 	 */
3000Sstevel@tonic-gate 	for (i = 0, mult = 0; suffixes[i].name != NULL; i++) {
3010Sstevel@tonic-gate 		if (strcasecmp(suffixes[i].name, suffix) == 0) {
3020Sstevel@tonic-gate 			mult = suffixes[i].mult;
3030Sstevel@tonic-gate 			break;
3040Sstevel@tonic-gate 		}
3050Sstevel@tonic-gate 	}
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	if (suffixes[i].name == NULL && *suffix != '\0')
3080Sstevel@tonic-gate 		return;
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	if (mult == 0) {
3110Sstevel@tonic-gate 		/*
3120Sstevel@tonic-gate 		 * The default is frequency-per-second.
3130Sstevel@tonic-gate 		 */
3140Sstevel@tonic-gate 		val = NANOSEC / val;
3150Sstevel@tonic-gate 	} else {
3160Sstevel@tonic-gate 		val *= mult;
3170Sstevel@tonic-gate 	}
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	profile_create(val, name, kind);
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate /*ARGSUSED*/
3230Sstevel@tonic-gate static void
3240Sstevel@tonic-gate profile_destroy(void *arg, dtrace_id_t id, void *parg)
3250Sstevel@tonic-gate {
3260Sstevel@tonic-gate 	profile_probe_t *prof = parg;
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	ASSERT(prof->prof_cyclic == CYCLIC_NONE);
3290Sstevel@tonic-gate 	kmem_free(prof, sizeof (profile_probe_t));
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	ASSERT(profile_total >= 1);
3320Sstevel@tonic-gate 	atomic_add_32(&profile_total, -1);
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate /*ARGSUSED*/
3360Sstevel@tonic-gate static void
3370Sstevel@tonic-gate profile_online(void *arg, cpu_t *cpu, cyc_handler_t *hdlr, cyc_time_t *when)
3380Sstevel@tonic-gate {
3390Sstevel@tonic-gate 	profile_probe_t *prof = arg;
3400Sstevel@tonic-gate 	profile_probe_percpu_t *pcpu;
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	pcpu = kmem_zalloc(sizeof (profile_probe_percpu_t), KM_SLEEP);
3430Sstevel@tonic-gate 	pcpu->profc_probe = prof;
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	hdlr->cyh_func = profile_fire;
3460Sstevel@tonic-gate 	hdlr->cyh_arg = pcpu;
3470Sstevel@tonic-gate 	hdlr->cyh_level = CY_HIGH_LEVEL;
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	when->cyt_interval = prof->prof_interval;
3500Sstevel@tonic-gate 	when->cyt_when = dtrace_gethrtime() + when->cyt_interval;
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	pcpu->profc_expected = when->cyt_when;
3530Sstevel@tonic-gate 	pcpu->profc_interval = when->cyt_interval;
3540Sstevel@tonic-gate }
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate /*ARGSUSED*/
3570Sstevel@tonic-gate static void
3580Sstevel@tonic-gate profile_offline(void *arg, cpu_t *cpu, void *oarg)
3590Sstevel@tonic-gate {
3600Sstevel@tonic-gate 	profile_probe_percpu_t *pcpu = oarg;
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	ASSERT(pcpu->profc_probe == arg);
3630Sstevel@tonic-gate 	kmem_free(pcpu, sizeof (profile_probe_percpu_t));
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate /*ARGSUSED*/
3670Sstevel@tonic-gate static void
3680Sstevel@tonic-gate profile_enable(void *arg, dtrace_id_t id, void *parg)
3690Sstevel@tonic-gate {
3700Sstevel@tonic-gate 	profile_probe_t *prof = parg;
3710Sstevel@tonic-gate 	cyc_omni_handler_t omni;
3720Sstevel@tonic-gate 	cyc_handler_t hdlr;
3730Sstevel@tonic-gate 	cyc_time_t when;
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	ASSERT(prof->prof_interval != 0);
3760Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	if (prof->prof_kind == PROF_TICK) {
3790Sstevel@tonic-gate 		hdlr.cyh_func = profile_tick;
3800Sstevel@tonic-gate 		hdlr.cyh_arg = prof;
3810Sstevel@tonic-gate 		hdlr.cyh_level = CY_HIGH_LEVEL;
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 		when.cyt_interval = prof->prof_interval;
3840Sstevel@tonic-gate 		when.cyt_when = dtrace_gethrtime() + when.cyt_interval;
3850Sstevel@tonic-gate 	} else {
3860Sstevel@tonic-gate 		ASSERT(prof->prof_kind == PROF_PROFILE);
3870Sstevel@tonic-gate 		omni.cyo_online = profile_online;
3880Sstevel@tonic-gate 		omni.cyo_offline = profile_offline;
3890Sstevel@tonic-gate 		omni.cyo_arg = prof;
3900Sstevel@tonic-gate 	}
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	if (prof->prof_kind == PROF_TICK) {
3930Sstevel@tonic-gate 		prof->prof_cyclic = cyclic_add(&hdlr, &when);
3940Sstevel@tonic-gate 	} else {
3950Sstevel@tonic-gate 		prof->prof_cyclic = cyclic_add_omni(&omni);
3960Sstevel@tonic-gate 	}
3970Sstevel@tonic-gate }
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate /*ARGSUSED*/
4000Sstevel@tonic-gate static void
4010Sstevel@tonic-gate profile_disable(void *arg, dtrace_id_t id, void *parg)
4020Sstevel@tonic-gate {
4030Sstevel@tonic-gate 	profile_probe_t *prof = parg;
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	ASSERT(prof->prof_cyclic != CYCLIC_NONE);
4060Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	cyclic_remove(prof->prof_cyclic);
4090Sstevel@tonic-gate 	prof->prof_cyclic = CYCLIC_NONE;
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate /*ARGSUSED*/
4130Sstevel@tonic-gate static int
4140Sstevel@tonic-gate profile_usermode(void *arg, dtrace_id_t id, void *parg)
4150Sstevel@tonic-gate {
4160Sstevel@tonic-gate 	return (CPU->cpu_profile_pc == 0);
4170Sstevel@tonic-gate }
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate static dtrace_pattr_t profile_attr = {
4200Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
4210Sstevel@tonic-gate { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_UNKNOWN },
4220Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
4230Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
4240Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
4250Sstevel@tonic-gate };
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate static dtrace_pops_t profile_pops = {
4280Sstevel@tonic-gate 	profile_provide,
4290Sstevel@tonic-gate 	NULL,
4300Sstevel@tonic-gate 	profile_enable,
4310Sstevel@tonic-gate 	profile_disable,
4320Sstevel@tonic-gate 	NULL,
4330Sstevel@tonic-gate 	NULL,
4340Sstevel@tonic-gate 	NULL,
4350Sstevel@tonic-gate 	NULL,
4360Sstevel@tonic-gate 	profile_usermode,
4370Sstevel@tonic-gate 	profile_destroy
4380Sstevel@tonic-gate };
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate static int
4410Sstevel@tonic-gate profile_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
4420Sstevel@tonic-gate {
4430Sstevel@tonic-gate 	switch (cmd) {
4440Sstevel@tonic-gate 	case DDI_ATTACH:
4450Sstevel@tonic-gate 		break;
4460Sstevel@tonic-gate 	case DDI_RESUME:
4470Sstevel@tonic-gate 		return (DDI_SUCCESS);
4480Sstevel@tonic-gate 	default:
4490Sstevel@tonic-gate 		return (DDI_FAILURE);
4500Sstevel@tonic-gate 	}
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "profile", S_IFCHR, 0,
4530Sstevel@tonic-gate 	    DDI_PSEUDO, NULL) == DDI_FAILURE ||
4540Sstevel@tonic-gate 	    dtrace_register("profile", &profile_attr,
455*1677Sdp 	    DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER, NULL,
4560Sstevel@tonic-gate 	    &profile_pops, NULL, &profile_id) != 0) {
4570Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
4580Sstevel@tonic-gate 		return (DDI_FAILURE);
4590Sstevel@tonic-gate 	}
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 	profile_max = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
4620Sstevel@tonic-gate 	    "profile-max-probes", PROFILE_MAX_DEFAULT);
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	ddi_report_dev(devi);
4650Sstevel@tonic-gate 	profile_devi = devi;
4660Sstevel@tonic-gate 	return (DDI_SUCCESS);
4670Sstevel@tonic-gate }
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate static int
4700Sstevel@tonic-gate profile_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
4710Sstevel@tonic-gate {
4720Sstevel@tonic-gate 	switch (cmd) {
4730Sstevel@tonic-gate 	case DDI_DETACH:
4740Sstevel@tonic-gate 		break;
4750Sstevel@tonic-gate 	case DDI_SUSPEND:
4760Sstevel@tonic-gate 		return (DDI_SUCCESS);
4770Sstevel@tonic-gate 	default:
4780Sstevel@tonic-gate 		return (DDI_FAILURE);
4790Sstevel@tonic-gate 	}
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	if (dtrace_unregister(profile_id) != 0)
4820Sstevel@tonic-gate 		return (DDI_FAILURE);
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
4850Sstevel@tonic-gate 	return (DDI_SUCCESS);
4860Sstevel@tonic-gate }
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate /*ARGSUSED*/
4890Sstevel@tonic-gate static int
4900Sstevel@tonic-gate profile_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
4910Sstevel@tonic-gate {
4920Sstevel@tonic-gate 	int error;
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	switch (infocmd) {
4950Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
4960Sstevel@tonic-gate 		*result = (void *)profile_devi;
4970Sstevel@tonic-gate 		error = DDI_SUCCESS;
4980Sstevel@tonic-gate 		break;
4990Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
5000Sstevel@tonic-gate 		*result = (void *)0;
5010Sstevel@tonic-gate 		error = DDI_SUCCESS;
5020Sstevel@tonic-gate 		break;
5030Sstevel@tonic-gate 	default:
5040Sstevel@tonic-gate 		error = DDI_FAILURE;
5050Sstevel@tonic-gate 	}
5060Sstevel@tonic-gate 	return (error);
5070Sstevel@tonic-gate }
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate /*ARGSUSED*/
5100Sstevel@tonic-gate static int
5110Sstevel@tonic-gate profile_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
5120Sstevel@tonic-gate {
5130Sstevel@tonic-gate 	return (0);
5140Sstevel@tonic-gate }
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate static struct cb_ops profile_cb_ops = {
5170Sstevel@tonic-gate 	profile_open,		/* open */
5180Sstevel@tonic-gate 	nodev,			/* close */
5190Sstevel@tonic-gate 	nulldev,		/* strategy */
5200Sstevel@tonic-gate 	nulldev,		/* print */
5210Sstevel@tonic-gate 	nodev,			/* dump */
5220Sstevel@tonic-gate 	nodev,			/* read */
5230Sstevel@tonic-gate 	nodev,			/* write */
5240Sstevel@tonic-gate 	nodev,			/* ioctl */
5250Sstevel@tonic-gate 	nodev,			/* devmap */
5260Sstevel@tonic-gate 	nodev,			/* mmap */
5270Sstevel@tonic-gate 	nodev,			/* segmap */
5280Sstevel@tonic-gate 	nochpoll,		/* poll */
5290Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
5300Sstevel@tonic-gate 	0,			/* streamtab  */
5310Sstevel@tonic-gate 	D_NEW | D_MP		/* Driver compatibility flag */
5320Sstevel@tonic-gate };
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate static struct dev_ops profile_ops = {
5350Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
5360Sstevel@tonic-gate 	0,			/* refcnt  */
5370Sstevel@tonic-gate 	profile_info,		/* get_dev_info */
5380Sstevel@tonic-gate 	nulldev,		/* identify */
5390Sstevel@tonic-gate 	nulldev,		/* probe */
5400Sstevel@tonic-gate 	profile_attach,		/* attach */
5410Sstevel@tonic-gate 	profile_detach,		/* detach */
5420Sstevel@tonic-gate 	nodev,			/* reset */
5430Sstevel@tonic-gate 	&profile_cb_ops,	/* driver operations */
5440Sstevel@tonic-gate 	NULL,			/* bus operations */
5450Sstevel@tonic-gate 	nodev			/* dev power */
5460Sstevel@tonic-gate };
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate /*
5490Sstevel@tonic-gate  * Module linkage information for the kernel.
5500Sstevel@tonic-gate  */
5510Sstevel@tonic-gate static struct modldrv modldrv = {
5520Sstevel@tonic-gate 	&mod_driverops,		/* module type (this is a pseudo driver) */
5530Sstevel@tonic-gate 	"Profile Interrupt Tracing",	/* name of module */
5540Sstevel@tonic-gate 	&profile_ops,		/* driver ops */
5550Sstevel@tonic-gate };
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate static struct modlinkage modlinkage = {
5580Sstevel@tonic-gate 	MODREV_1,
5590Sstevel@tonic-gate 	(void *)&modldrv,
5600Sstevel@tonic-gate 	NULL
5610Sstevel@tonic-gate };
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate int
5640Sstevel@tonic-gate _init(void)
5650Sstevel@tonic-gate {
5660Sstevel@tonic-gate 	return (mod_install(&modlinkage));
5670Sstevel@tonic-gate }
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate int
5700Sstevel@tonic-gate _info(struct modinfo *modinfop)
5710Sstevel@tonic-gate {
5720Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
5730Sstevel@tonic-gate }
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate int
5760Sstevel@tonic-gate _fini(void)
5770Sstevel@tonic-gate {
5780Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
5790Sstevel@tonic-gate }
580