xref: /illumos-gate/usr/src/uts/common/dtrace/profile.c (revision 1aa5f892)
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 /*
22b9e93c10SJonathan Haslam  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267d5c9b5fSBryan Cantrill /*
277d5c9b5fSBryan Cantrill  * Copyright (c) 2011, Joyent, Inc. All rights reserved.
287d5c9b5fSBryan Cantrill  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/errno.h>
317c478bd9Sstevel@tonic-gate #include <sys/stat.h>
327c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
337c478bd9Sstevel@tonic-gate #include <sys/conf.h>
347c478bd9Sstevel@tonic-gate #include <sys/systm.h>
357c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
367c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
377c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
387c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
397c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
407c478bd9Sstevel@tonic-gate #include <sys/dtrace.h>
417c478bd9Sstevel@tonic-gate #include <sys/cyclic.h>
427c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate static dev_info_t *profile_devi;
457c478bd9Sstevel@tonic-gate static dtrace_provider_id_t profile_id;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
48ae115bc7Smrj  * Regardless of platform, the stack frames look like this in the case of the
497c478bd9Sstevel@tonic-gate  * profile provider:
507c478bd9Sstevel@tonic-gate  *
517c478bd9Sstevel@tonic-gate  *	profile_fire
527c478bd9Sstevel@tonic-gate  *	cyclic_expire
537c478bd9Sstevel@tonic-gate  *	cyclic_fire
547c478bd9Sstevel@tonic-gate  *	[ cbe ]
55ae115bc7Smrj  *	[ interrupt code ]
567c478bd9Sstevel@tonic-gate  *
57ae115bc7Smrj  * On x86, there are five frames from the generic interrupt code; further, the
58ae115bc7Smrj  * interrupted instruction appears as its own stack frame, giving us a total of
59ae115bc7Smrj  * 10.
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  *
64*1aa5f892SToomas Soome  *	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
71ae115bc7Smrj  * provider should ideally figure this out on-the-fly by hitting 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  */
76ae115bc7Smrj #ifdef __x86
77ae115bc7Smrj #define	PROF_ARTIFICIAL_FRAMES	10
787c478bd9Sstevel@tonic-gate #else
797c478bd9Sstevel@tonic-gate #ifdef __sparc
807c478bd9Sstevel@tonic-gate #ifdef DEBUG
817c478bd9Sstevel@tonic-gate #define	PROF_ARTIFICIAL_FRAMES	4
827c478bd9Sstevel@tonic-gate #else
837c478bd9Sstevel@tonic-gate #define	PROF_ARTIFICIAL_FRAMES	3
847c478bd9Sstevel@tonic-gate #endif
857c478bd9Sstevel@tonic-gate #endif
867c478bd9Sstevel@tonic-gate #endif
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #define	PROF_NAMELEN		15
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate #define	PROF_PROFILE		0
917c478bd9Sstevel@tonic-gate #define	PROF_TICK		1
927c478bd9Sstevel@tonic-gate #define	PROF_PREFIX_PROFILE	"profile-"
937c478bd9Sstevel@tonic-gate #define	PROF_PREFIX_TICK	"tick-"
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate typedef struct profile_probe {
967c478bd9Sstevel@tonic-gate 	char		prof_name[PROF_NAMELEN];
977c478bd9Sstevel@tonic-gate 	dtrace_id_t	prof_id;
987c478bd9Sstevel@tonic-gate 	int		prof_kind;
997c478bd9Sstevel@tonic-gate 	hrtime_t	prof_interval;
1007c478bd9Sstevel@tonic-gate 	cyclic_id_t	prof_cyclic;
1017c478bd9Sstevel@tonic-gate } profile_probe_t;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate typedef struct profile_probe_percpu {
1047c478bd9Sstevel@tonic-gate 	hrtime_t	profc_expected;
1057c478bd9Sstevel@tonic-gate 	hrtime_t	profc_interval;
1067c478bd9Sstevel@tonic-gate 	profile_probe_t	*profc_probe;
1077c478bd9Sstevel@tonic-gate } profile_probe_percpu_t;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate hrtime_t	profile_interval_min = NANOSEC / 5000;		/* 5000 hz */
1107c478bd9Sstevel@tonic-gate int		profile_aframes = 0;				/* override */
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate static int profile_rates[] = {
1137c478bd9Sstevel@tonic-gate     97, 199, 499, 997, 1999,
1147c478bd9Sstevel@tonic-gate     4001, 4999, 0, 0, 0,
1157c478bd9Sstevel@tonic-gate     0, 0, 0, 0, 0,
1167c478bd9Sstevel@tonic-gate     0, 0, 0, 0, 0
1177c478bd9Sstevel@tonic-gate };
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate static int profile_ticks[] = {
1207c478bd9Sstevel@tonic-gate     1, 10, 100, 500, 1000,
1217c478bd9Sstevel@tonic-gate     5000, 0, 0, 0, 0,
1227c478bd9Sstevel@tonic-gate     0, 0, 0, 0, 0
1237c478bd9Sstevel@tonic-gate };
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate /*
1267c478bd9Sstevel@tonic-gate  * profile_max defines the upper bound on the number of profile probes that
1277c478bd9Sstevel@tonic-gate  * can exist (this is to prevent malicious or clumsy users from exhausing
1287c478bd9Sstevel@tonic-gate  * system resources by creating a slew of profile probes). At mod load time,
1297c478bd9Sstevel@tonic-gate  * this gets its value from PROFILE_MAX_DEFAULT or profile-max-probes if it's
1307c478bd9Sstevel@tonic-gate  * present in the profile.conf file.
1317c478bd9Sstevel@tonic-gate  */
1327c478bd9Sstevel@tonic-gate #define	PROFILE_MAX_DEFAULT	1000	/* default max. number of probes */
1337c478bd9Sstevel@tonic-gate static uint32_t profile_max;		/* maximum number of profile probes */
1347c478bd9Sstevel@tonic-gate static uint32_t profile_total;	/* current number of profile probes */
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate static void
profile_fire(void * arg)1377c478bd9Sstevel@tonic-gate profile_fire(void *arg)
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate 	profile_probe_percpu_t *pcpu = arg;
1407c478bd9Sstevel@tonic-gate 	profile_probe_t *prof = pcpu->profc_probe;
1417c478bd9Sstevel@tonic-gate 	hrtime_t late;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	late = dtrace_gethrtime() - pcpu->profc_expected;
1447c478bd9Sstevel@tonic-gate 	pcpu->profc_expected += pcpu->profc_interval;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	dtrace_probe(prof->prof_id, CPU->cpu_profile_pc,
1477c478bd9Sstevel@tonic-gate 	    CPU->cpu_profile_upc, late, 0, 0);
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate static void
profile_tick(void * arg)1517c478bd9Sstevel@tonic-gate profile_tick(void *arg)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate 	profile_probe_t *prof = arg;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	dtrace_probe(prof->prof_id, CPU->cpu_profile_pc,
1567c478bd9Sstevel@tonic-gate 	    CPU->cpu_profile_upc, 0, 0, 0);
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate static void
profile_create(hrtime_t interval,const char * name,int kind)1607c478bd9Sstevel@tonic-gate profile_create(hrtime_t interval, const char *name, int kind)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	profile_probe_t *prof;
163ae115bc7Smrj 	int nr_frames = PROF_ARTIFICIAL_FRAMES + dtrace_mach_aframes();
164ae115bc7Smrj 
165ae115bc7Smrj 	if (profile_aframes)
166ae115bc7Smrj 		nr_frames = profile_aframes;
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 
1741a5e258fSJosef 'Jeff' Sipek 	atomic_inc_32(&profile_total);
1757c478bd9Sstevel@tonic-gate 	if (profile_total > profile_max) {
1761a5e258fSJosef 'Jeff' Sipek 		atomic_dec_32(&profile_total);
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,
186ae115bc7Smrj 	    NULL, NULL, name, nr_frames, prof);
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1907c478bd9Sstevel@tonic-gate static void
profile_provide(void * arg,const dtrace_probedesc_t * desc)1917c478bd9Sstevel@tonic-gate profile_provide(void *arg, const dtrace_probedesc_t *desc)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate 	int i, j, rate, kind;
1947c478bd9Sstevel@tonic-gate 	hrtime_t val = 0, mult = 1, len;
1957c478bd9Sstevel@tonic-gate 	const char *name, *suffix = NULL;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	const struct {
1987c478bd9Sstevel@tonic-gate 		char *prefix;
1997c478bd9Sstevel@tonic-gate 		int kind;
2007c478bd9Sstevel@tonic-gate 	} types[] = {
2017c478bd9Sstevel@tonic-gate 		{ PROF_PREFIX_PROFILE, PROF_PROFILE },
2027c478bd9Sstevel@tonic-gate 		{ PROF_PREFIX_TICK, PROF_TICK },
203*1aa5f892SToomas Soome 		{ NULL, 0 }
2047c478bd9Sstevel@tonic-gate 	};
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	const struct {
2077c478bd9Sstevel@tonic-gate 		char *name;
2087c478bd9Sstevel@tonic-gate 		hrtime_t mult;
2097c478bd9Sstevel@tonic-gate 	} suffixes[] = {
210*1aa5f892SToomas Soome 		{ "ns",		NANOSEC / NANOSEC },
2117c478bd9Sstevel@tonic-gate 		{ "nsec",	NANOSEC / NANOSEC },
2127c478bd9Sstevel@tonic-gate 		{ "us",		NANOSEC / MICROSEC },
2137c478bd9Sstevel@tonic-gate 		{ "usec",	NANOSEC / MICROSEC },
2147c478bd9Sstevel@tonic-gate 		{ "ms",		NANOSEC / MILLISEC },
2157c478bd9Sstevel@tonic-gate 		{ "msec",	NANOSEC / MILLISEC },
2167c478bd9Sstevel@tonic-gate 		{ "s",		NANOSEC / SEC },
2177c478bd9Sstevel@tonic-gate 		{ "sec",	NANOSEC / SEC },
2187c478bd9Sstevel@tonic-gate 		{ "m",		NANOSEC * (hrtime_t)60 },
2197c478bd9Sstevel@tonic-gate 		{ "min",	NANOSEC * (hrtime_t)60 },
2207c478bd9Sstevel@tonic-gate 		{ "h",		NANOSEC * (hrtime_t)(60 * 60) },
2217c478bd9Sstevel@tonic-gate 		{ "hour",	NANOSEC * (hrtime_t)(60 * 60) },
2227c478bd9Sstevel@tonic-gate 		{ "d",		NANOSEC * (hrtime_t)(24 * 60 * 60) },
2237c478bd9Sstevel@tonic-gate 		{ "day",	NANOSEC * (hrtime_t)(24 * 60 * 60) },
2247c478bd9Sstevel@tonic-gate 		{ "hz",		0 },
2257c478bd9Sstevel@tonic-gate 		{ NULL }
2267c478bd9Sstevel@tonic-gate 	};
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	if (desc == NULL) {
2297c478bd9Sstevel@tonic-gate 		char n[PROF_NAMELEN];
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 		/*
2327c478bd9Sstevel@tonic-gate 		 * If no description was provided, provide all of our probes.
2337c478bd9Sstevel@tonic-gate 		 */
2347c478bd9Sstevel@tonic-gate 		for (i = 0; i < sizeof (profile_rates) / sizeof (int); i++) {
2357c478bd9Sstevel@tonic-gate 			if ((rate = profile_rates[i]) == 0)
2367c478bd9Sstevel@tonic-gate 				continue;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 			(void) snprintf(n, PROF_NAMELEN, "%s%d",
2397c478bd9Sstevel@tonic-gate 			    PROF_PREFIX_PROFILE, rate);
2407c478bd9Sstevel@tonic-gate 			profile_create(NANOSEC / rate, n, PROF_PROFILE);
2417c478bd9Sstevel@tonic-gate 		}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 		for (i = 0; i < sizeof (profile_ticks) / sizeof (int); i++) {
2447c478bd9Sstevel@tonic-gate 			if ((rate = profile_ticks[i]) == 0)
2457c478bd9Sstevel@tonic-gate 				continue;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 			(void) snprintf(n, PROF_NAMELEN, "%s%d",
2487c478bd9Sstevel@tonic-gate 			    PROF_PREFIX_TICK, rate);
2497c478bd9Sstevel@tonic-gate 			profile_create(NANOSEC / rate, n, PROF_TICK);
2507c478bd9Sstevel@tonic-gate 		}
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 		return;
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	name = desc->dtpd_name;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	for (i = 0; types[i].prefix != NULL; i++) {
2587c478bd9Sstevel@tonic-gate 		len = strlen(types[i].prefix);
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 		if (strncmp(name, types[i].prefix, len) != 0)
2617c478bd9Sstevel@tonic-gate 			continue;
2627c478bd9Sstevel@tonic-gate 		break;
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	if (types[i].prefix == NULL)
2667c478bd9Sstevel@tonic-gate 		return;
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	kind = types[i].kind;
2697c478bd9Sstevel@tonic-gate 	j = strlen(name) - len;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	/*
2727c478bd9Sstevel@tonic-gate 	 * We need to start before any time suffix.
2737c478bd9Sstevel@tonic-gate 	 */
2747c478bd9Sstevel@tonic-gate 	for (j = strlen(name); j >= len; j--) {
2757c478bd9Sstevel@tonic-gate 		if (name[j] >= '0' && name[j] <= '9')
2767c478bd9Sstevel@tonic-gate 			break;
2777c478bd9Sstevel@tonic-gate 		suffix = &name[j];
2787c478bd9Sstevel@tonic-gate 	}
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	ASSERT(suffix != NULL);
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	/*
2837c478bd9Sstevel@tonic-gate 	 * Now determine the numerical value present in the probe name.
2847c478bd9Sstevel@tonic-gate 	 */
2857c478bd9Sstevel@tonic-gate 	for (; j >= len; j--) {
2867c478bd9Sstevel@tonic-gate 		if (name[j] < '0' || name[j] > '9')
2877c478bd9Sstevel@tonic-gate 			return;
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 		val += (name[j] - '0') * mult;
2907c478bd9Sstevel@tonic-gate 		mult *= (hrtime_t)10;
2917c478bd9Sstevel@tonic-gate 	}
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	if (val == 0)
2947c478bd9Sstevel@tonic-gate 		return;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	/*
2977c478bd9Sstevel@tonic-gate 	 * Look-up the suffix to determine the multiplier.
2987c478bd9Sstevel@tonic-gate 	 */
2997c478bd9Sstevel@tonic-gate 	for (i = 0, mult = 0; suffixes[i].name != NULL; i++) {
3007c478bd9Sstevel@tonic-gate 		if (strcasecmp(suffixes[i].name, suffix) == 0) {
3017c478bd9Sstevel@tonic-gate 			mult = suffixes[i].mult;
3027c478bd9Sstevel@tonic-gate 			break;
3037c478bd9Sstevel@tonic-gate 		}
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	if (suffixes[i].name == NULL && *suffix != '\0')
3077c478bd9Sstevel@tonic-gate 		return;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	if (mult == 0) {
3107c478bd9Sstevel@tonic-gate 		/*
3117c478bd9Sstevel@tonic-gate 		 * The default is frequency-per-second.
3127c478bd9Sstevel@tonic-gate 		 */
3137c478bd9Sstevel@tonic-gate 		val = NANOSEC / val;
3147c478bd9Sstevel@tonic-gate 	} else {
3157c478bd9Sstevel@tonic-gate 		val *= mult;
3167c478bd9Sstevel@tonic-gate 	}
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	profile_create(val, name, kind);
3197c478bd9Sstevel@tonic-gate }
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3227c478bd9Sstevel@tonic-gate static void
profile_destroy(void * arg,dtrace_id_t id,void * parg)3237c478bd9Sstevel@tonic-gate profile_destroy(void *arg, dtrace_id_t id, void *parg)
3247c478bd9Sstevel@tonic-gate {
3257c478bd9Sstevel@tonic-gate 	profile_probe_t *prof = parg;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	ASSERT(prof->prof_cyclic == CYCLIC_NONE);
3287c478bd9Sstevel@tonic-gate 	kmem_free(prof, sizeof (profile_probe_t));
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	ASSERT(profile_total >= 1);
3311a5e258fSJosef 'Jeff' Sipek 	atomic_dec_32(&profile_total);
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3357c478bd9Sstevel@tonic-gate static void
profile_online(void * arg,cpu_t * cpu,cyc_handler_t * hdlr,cyc_time_t * when)3367c478bd9Sstevel@tonic-gate profile_online(void *arg, cpu_t *cpu, cyc_handler_t *hdlr, cyc_time_t *when)
3377c478bd9Sstevel@tonic-gate {
3387c478bd9Sstevel@tonic-gate 	profile_probe_t *prof = arg;
3397c478bd9Sstevel@tonic-gate 	profile_probe_percpu_t *pcpu;
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	pcpu = kmem_zalloc(sizeof (profile_probe_percpu_t), KM_SLEEP);
3427c478bd9Sstevel@tonic-gate 	pcpu->profc_probe = prof;
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	hdlr->cyh_func = profile_fire;
3457c478bd9Sstevel@tonic-gate 	hdlr->cyh_arg = pcpu;
3467c478bd9Sstevel@tonic-gate 	hdlr->cyh_level = CY_HIGH_LEVEL;
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	when->cyt_interval = prof->prof_interval;
3497c478bd9Sstevel@tonic-gate 	when->cyt_when = dtrace_gethrtime() + when->cyt_interval;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	pcpu->profc_expected = when->cyt_when;
3527c478bd9Sstevel@tonic-gate 	pcpu->profc_interval = when->cyt_interval;
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3567c478bd9Sstevel@tonic-gate static void
profile_offline(void * arg,cpu_t * cpu,void * oarg)3577c478bd9Sstevel@tonic-gate profile_offline(void *arg, cpu_t *cpu, void *oarg)
3587c478bd9Sstevel@tonic-gate {
3597c478bd9Sstevel@tonic-gate 	profile_probe_percpu_t *pcpu = oarg;
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	ASSERT(pcpu->profc_probe == arg);
3627c478bd9Sstevel@tonic-gate 	kmem_free(pcpu, sizeof (profile_probe_percpu_t));
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate /*ARGSUSED*/
366b9e93c10SJonathan Haslam static int
profile_enable(void * arg,dtrace_id_t id,void * parg)3677c478bd9Sstevel@tonic-gate profile_enable(void *arg, dtrace_id_t id, void *parg)
3687c478bd9Sstevel@tonic-gate {
3697c478bd9Sstevel@tonic-gate 	profile_probe_t *prof = parg;
3707c478bd9Sstevel@tonic-gate 	cyc_omni_handler_t omni;
3717c478bd9Sstevel@tonic-gate 	cyc_handler_t hdlr;
3727c478bd9Sstevel@tonic-gate 	cyc_time_t when;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	ASSERT(prof->prof_interval != 0);
3757c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	if (prof->prof_kind == PROF_TICK) {
3787c478bd9Sstevel@tonic-gate 		hdlr.cyh_func = profile_tick;
3797c478bd9Sstevel@tonic-gate 		hdlr.cyh_arg = prof;
3807c478bd9Sstevel@tonic-gate 		hdlr.cyh_level = CY_HIGH_LEVEL;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 		when.cyt_interval = prof->prof_interval;
3837c478bd9Sstevel@tonic-gate 		when.cyt_when = dtrace_gethrtime() + when.cyt_interval;
3847c478bd9Sstevel@tonic-gate 	} else {
3857c478bd9Sstevel@tonic-gate 		ASSERT(prof->prof_kind == PROF_PROFILE);
3867c478bd9Sstevel@tonic-gate 		omni.cyo_online = profile_online;
3877c478bd9Sstevel@tonic-gate 		omni.cyo_offline = profile_offline;
3887c478bd9Sstevel@tonic-gate 		omni.cyo_arg = prof;
3897c478bd9Sstevel@tonic-gate 	}
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	if (prof->prof_kind == PROF_TICK) {
3927c478bd9Sstevel@tonic-gate 		prof->prof_cyclic = cyclic_add(&hdlr, &when);
3937c478bd9Sstevel@tonic-gate 	} else {
3947c478bd9Sstevel@tonic-gate 		prof->prof_cyclic = cyclic_add_omni(&omni);
3957c478bd9Sstevel@tonic-gate 	}
396b9e93c10SJonathan Haslam 	return (0);
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4007c478bd9Sstevel@tonic-gate static void
profile_disable(void * arg,dtrace_id_t id,void * parg)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
profile_mode(void * arg,dtrace_id_t id,void * parg)4147d5c9b5fSBryan Cantrill profile_mode(void *arg, dtrace_id_t id, void *parg)
4157c478bd9Sstevel@tonic-gate {
4167d5c9b5fSBryan Cantrill 	profile_probe_t *prof = parg;
4177d5c9b5fSBryan Cantrill 	int mode;
4187d5c9b5fSBryan Cantrill 
4197d5c9b5fSBryan Cantrill 	if (CPU->cpu_profile_pc != 0) {
4207d5c9b5fSBryan Cantrill 		mode = DTRACE_MODE_KERNEL;
4217d5c9b5fSBryan Cantrill 	} else {
4227d5c9b5fSBryan Cantrill 		mode = DTRACE_MODE_USER;
4237d5c9b5fSBryan Cantrill 	}
4247d5c9b5fSBryan Cantrill 
4257d5c9b5fSBryan Cantrill 	if (prof->prof_kind == PROF_TICK) {
4267d5c9b5fSBryan Cantrill 		mode |= DTRACE_MODE_NOPRIV_RESTRICT;
4277d5c9b5fSBryan Cantrill 	} else {
4287d5c9b5fSBryan Cantrill 		ASSERT(prof->prof_kind == PROF_PROFILE);
4297d5c9b5fSBryan Cantrill 		mode |= DTRACE_MODE_NOPRIV_DROP;
4307d5c9b5fSBryan Cantrill 	}
4317d5c9b5fSBryan Cantrill 
4327d5c9b5fSBryan Cantrill 	return (mode);
4337c478bd9Sstevel@tonic-gate }
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate static dtrace_pattr_t profile_attr = {
4367c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
4377c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_UNKNOWN },
4387c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
4397c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
4407c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
4417c478bd9Sstevel@tonic-gate };
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate static dtrace_pops_t profile_pops = {
4447c478bd9Sstevel@tonic-gate 	profile_provide,
4457c478bd9Sstevel@tonic-gate 	NULL,
4467c478bd9Sstevel@tonic-gate 	profile_enable,
4477c478bd9Sstevel@tonic-gate 	profile_disable,
4487c478bd9Sstevel@tonic-gate 	NULL,
4497c478bd9Sstevel@tonic-gate 	NULL,
4507c478bd9Sstevel@tonic-gate 	NULL,
4517c478bd9Sstevel@tonic-gate 	NULL,
4527d5c9b5fSBryan Cantrill 	profile_mode,
4537c478bd9Sstevel@tonic-gate 	profile_destroy
4547c478bd9Sstevel@tonic-gate };
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate static int
profile_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)4577c478bd9Sstevel@tonic-gate profile_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
4587c478bd9Sstevel@tonic-gate {
4597c478bd9Sstevel@tonic-gate 	switch (cmd) {
4607c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
4617c478bd9Sstevel@tonic-gate 		break;
4627c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
4637c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
4647c478bd9Sstevel@tonic-gate 	default:
4657c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4667c478bd9Sstevel@tonic-gate 	}
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "profile", S_IFCHR, 0,
469*1aa5f892SToomas Soome 	    DDI_PSEUDO, 0) == DDI_FAILURE ||
4707c478bd9Sstevel@tonic-gate 	    dtrace_register("profile", &profile_attr,
471ad4023c4Sdp 	    DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER, NULL,
4727c478bd9Sstevel@tonic-gate 	    &profile_pops, NULL, &profile_id) != 0) {
4737c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
4747c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4757c478bd9Sstevel@tonic-gate 	}
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	profile_max = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
4787c478bd9Sstevel@tonic-gate 	    "profile-max-probes", PROFILE_MAX_DEFAULT);
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	ddi_report_dev(devi);
4817c478bd9Sstevel@tonic-gate 	profile_devi = devi;
4827c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
4837c478bd9Sstevel@tonic-gate }
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate static int
profile_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)4867c478bd9Sstevel@tonic-gate profile_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
4877c478bd9Sstevel@tonic-gate {
4887c478bd9Sstevel@tonic-gate 	switch (cmd) {
4897c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
4907c478bd9Sstevel@tonic-gate 		break;
4917c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
4927c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
4937c478bd9Sstevel@tonic-gate 	default:
4947c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4957c478bd9Sstevel@tonic-gate 	}
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	if (dtrace_unregister(profile_id) != 0)
4987c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
5017c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
5027c478bd9Sstevel@tonic-gate }
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5057c478bd9Sstevel@tonic-gate static int
profile_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)5067c478bd9Sstevel@tonic-gate profile_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
5077c478bd9Sstevel@tonic-gate {
5087c478bd9Sstevel@tonic-gate 	int error;
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	switch (infocmd) {
5117c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
5127c478bd9Sstevel@tonic-gate 		*result = (void *)profile_devi;
5137c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
5147c478bd9Sstevel@tonic-gate 		break;
5157c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
5167c478bd9Sstevel@tonic-gate 		*result = (void *)0;
5177c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
5187c478bd9Sstevel@tonic-gate 		break;
5197c478bd9Sstevel@tonic-gate 	default:
5207c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
5217c478bd9Sstevel@tonic-gate 	}
5227c478bd9Sstevel@tonic-gate 	return (error);
5237c478bd9Sstevel@tonic-gate }
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5267c478bd9Sstevel@tonic-gate static int
profile_open(dev_t * devp,int flag,int otyp,cred_t * cred_p)5277c478bd9Sstevel@tonic-gate profile_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
5287c478bd9Sstevel@tonic-gate {
5297c478bd9Sstevel@tonic-gate 	return (0);
5307c478bd9Sstevel@tonic-gate }
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate static struct cb_ops profile_cb_ops = {
5337c478bd9Sstevel@tonic-gate 	profile_open,		/* open */
5347c478bd9Sstevel@tonic-gate 	nodev,			/* close */
5357c478bd9Sstevel@tonic-gate 	nulldev,		/* strategy */
5367c478bd9Sstevel@tonic-gate 	nulldev,		/* print */
5377c478bd9Sstevel@tonic-gate 	nodev,			/* dump */
5387c478bd9Sstevel@tonic-gate 	nodev,			/* read */
5397c478bd9Sstevel@tonic-gate 	nodev,			/* write */
5407c478bd9Sstevel@tonic-gate 	nodev,			/* ioctl */
5417c478bd9Sstevel@tonic-gate 	nodev,			/* devmap */
5427c478bd9Sstevel@tonic-gate 	nodev,			/* mmap */
5437c478bd9Sstevel@tonic-gate 	nodev,			/* segmap */
5447c478bd9Sstevel@tonic-gate 	nochpoll,		/* poll */
5457c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
5467c478bd9Sstevel@tonic-gate 	0,			/* streamtab  */
5477c478bd9Sstevel@tonic-gate 	D_NEW | D_MP		/* Driver compatibility flag */
5487c478bd9Sstevel@tonic-gate };
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate static struct dev_ops profile_ops = {
5517c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
5527c478bd9Sstevel@tonic-gate 	0,			/* refcnt  */
5537c478bd9Sstevel@tonic-gate 	profile_info,		/* get_dev_info */
5547c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
5557c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
5567c478bd9Sstevel@tonic-gate 	profile_attach,		/* attach */
5577c478bd9Sstevel@tonic-gate 	profile_detach,		/* detach */
5587c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
5597c478bd9Sstevel@tonic-gate 	&profile_cb_ops,	/* driver operations */
5607c478bd9Sstevel@tonic-gate 	NULL,			/* bus operations */
56119397407SSherry Moore 	nodev,			/* dev power */
56219397407SSherry Moore 	ddi_quiesce_not_needed,		/* quiesce */
5637c478bd9Sstevel@tonic-gate };
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate /*
5667c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
5677c478bd9Sstevel@tonic-gate  */
5687c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
5697c478bd9Sstevel@tonic-gate 	&mod_driverops,		/* module type (this is a pseudo driver) */
5707c478bd9Sstevel@tonic-gate 	"Profile Interrupt Tracing",	/* name of module */
5717c478bd9Sstevel@tonic-gate 	&profile_ops,		/* driver ops */
5727c478bd9Sstevel@tonic-gate };
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
5757c478bd9Sstevel@tonic-gate 	MODREV_1,
5767c478bd9Sstevel@tonic-gate 	(void *)&modldrv,
5777c478bd9Sstevel@tonic-gate 	NULL
5787c478bd9Sstevel@tonic-gate };
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate int
_init(void)5817c478bd9Sstevel@tonic-gate _init(void)
5827c478bd9Sstevel@tonic-gate {
5837c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
5847c478bd9Sstevel@tonic-gate }
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)5877c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
5887c478bd9Sstevel@tonic-gate {
5897c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
5907c478bd9Sstevel@tonic-gate }
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate int
_fini(void)5937c478bd9Sstevel@tonic-gate _fini(void)
5947c478bd9Sstevel@tonic-gate {
5957c478bd9Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
5967c478bd9Sstevel@tonic-gate }
597