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