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