1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23 * Use is subject to license terms.
24 */
25
26#pragma ident	"%Z%%M%	%I%	%E% SMI"
27
28#include <sys/errno.h>
29#include <sys/stat.h>
30#include <sys/modctl.h>
31#include <sys/conf.h>
32#include <sys/systm.h>
33#include <sys/ddi.h>
34#include <sys/sunddi.h>
35#include <sys/cpuvar.h>
36#include <sys/kmem.h>
37#include <sys/strsubr.h>
38#include <sys/dtrace.h>
39#include <sys/cyclic.h>
40#include <sys/atomic.h>
41
42static dev_info_t *profile_devi;
43static dtrace_provider_id_t profile_id;
44
45/*
46 * Regardless of platform, the stack frames look like this in the case of the
47 * profile provider:
48 *
49 *	profile_fire
50 *	cyclic_expire
51 *	cyclic_fire
52 *	[ cbe ]
53 *	[ interrupt code ]
54 *
55 * On x86, there are five frames from the generic interrupt code; further, the
56 * interrupted instruction appears as its own stack frame, giving us a total of
57 * 10.
58 *
59 * On SPARC, the picture is further complicated because the compiler
60 * optimizes away tail-calls -- so the following frames are optimized away:
61 *
62 * 	profile_fire
63 *	cyclic_expire
64 *
65 * This gives three frames.  However, on DEBUG kernels, the cyclic_expire
66 * frame cannot be tail-call eliminated, yielding four frames in this case.
67 *
68 * All of the above constraints lead to the mess below.  Yes, the profile
69 * provider should ideally figure this out on-the-fly by hitting one of its own
70 * probes and then walking its own stack trace.  This is complicated, however,
71 * and the static definition doesn't seem to be overly brittle.  Still, we
72 * allow for a manual override in case we get it completely wrong.
73 */
74#ifdef __x86
75#define	PROF_ARTIFICIAL_FRAMES	10
76#else
77#ifdef __sparc
78#ifdef DEBUG
79#define	PROF_ARTIFICIAL_FRAMES	4
80#else
81#define	PROF_ARTIFICIAL_FRAMES	3
82#endif
83#endif
84#endif
85
86#define	PROF_NAMELEN		15
87
88#define	PROF_PROFILE		0
89#define	PROF_TICK		1
90#define	PROF_PREFIX_PROFILE	"profile-"
91#define	PROF_PREFIX_TICK	"tick-"
92
93typedef struct profile_probe {
94	char		prof_name[PROF_NAMELEN];
95	dtrace_id_t	prof_id;
96	int		prof_kind;
97	hrtime_t	prof_interval;
98	cyclic_id_t	prof_cyclic;
99} profile_probe_t;
100
101typedef struct profile_probe_percpu {
102	hrtime_t	profc_expected;
103	hrtime_t	profc_interval;
104	profile_probe_t	*profc_probe;
105} profile_probe_percpu_t;
106
107hrtime_t	profile_interval_min = NANOSEC / 5000;		/* 5000 hz */
108int		profile_aframes = 0;				/* override */
109
110static int profile_rates[] = {
111    97, 199, 499, 997, 1999,
112    4001, 4999, 0, 0, 0,
113    0, 0, 0, 0, 0,
114    0, 0, 0, 0, 0
115};
116
117static int profile_ticks[] = {
118    1, 10, 100, 500, 1000,
119    5000, 0, 0, 0, 0,
120    0, 0, 0, 0, 0
121};
122
123/*
124 * profile_max defines the upper bound on the number of profile probes that
125 * can exist (this is to prevent malicious or clumsy users from exhausing
126 * system resources by creating a slew of profile probes). At mod load time,
127 * this gets its value from PROFILE_MAX_DEFAULT or profile-max-probes if it's
128 * present in the profile.conf file.
129 */
130#define	PROFILE_MAX_DEFAULT	1000	/* default max. number of probes */
131static uint32_t profile_max;		/* maximum number of profile probes */
132static uint32_t profile_total;	/* current number of profile probes */
133
134static void
135profile_fire(void *arg)
136{
137	profile_probe_percpu_t *pcpu = arg;
138	profile_probe_t *prof = pcpu->profc_probe;
139	hrtime_t late;
140
141	late = dtrace_gethrtime() - pcpu->profc_expected;
142	pcpu->profc_expected += pcpu->profc_interval;
143
144	dtrace_probe(prof->prof_id, CPU->cpu_profile_pc,
145	    CPU->cpu_profile_upc, late, 0, 0);
146}
147
148static void
149profile_tick(void *arg)
150{
151	profile_probe_t *prof = arg;
152
153	dtrace_probe(prof->prof_id, CPU->cpu_profile_pc,
154	    CPU->cpu_profile_upc, 0, 0, 0);
155}
156
157static void
158profile_create(hrtime_t interval, const char *name, int kind)
159{
160	profile_probe_t *prof;
161	int nr_frames = PROF_ARTIFICIAL_FRAMES + dtrace_mach_aframes();
162
163	if (profile_aframes)
164		nr_frames = profile_aframes;
165
166	if (interval < profile_interval_min)
167		return;
168
169	if (dtrace_probe_lookup(profile_id, NULL, NULL, name) != 0)
170		return;
171
172	atomic_add_32(&profile_total, 1);
173	if (profile_total > profile_max) {
174		atomic_add_32(&profile_total, -1);
175		return;
176	}
177
178	prof = kmem_zalloc(sizeof (profile_probe_t), KM_SLEEP);
179	(void) strcpy(prof->prof_name, name);
180	prof->prof_interval = interval;
181	prof->prof_cyclic = CYCLIC_NONE;
182	prof->prof_kind = kind;
183	prof->prof_id = dtrace_probe_create(profile_id,
184	    NULL, NULL, name, nr_frames, prof);
185}
186
187/*ARGSUSED*/
188static void
189profile_provide(void *arg, const dtrace_probedesc_t *desc)
190{
191	int i, j, rate, kind;
192	hrtime_t val = 0, mult = 1, len;
193	const char *name, *suffix = NULL;
194
195	const struct {
196		char *prefix;
197		int kind;
198	} types[] = {
199		{ PROF_PREFIX_PROFILE, PROF_PROFILE },
200		{ PROF_PREFIX_TICK, PROF_TICK },
201		{ NULL, NULL }
202	};
203
204	const struct {
205		char *name;
206		hrtime_t mult;
207	} suffixes[] = {
208		{ "ns", 	NANOSEC / NANOSEC },
209		{ "nsec",	NANOSEC / NANOSEC },
210		{ "us",		NANOSEC / MICROSEC },
211		{ "usec",	NANOSEC / MICROSEC },
212		{ "ms",		NANOSEC / MILLISEC },
213		{ "msec",	NANOSEC / MILLISEC },
214		{ "s",		NANOSEC / SEC },
215		{ "sec",	NANOSEC / SEC },
216		{ "m",		NANOSEC * (hrtime_t)60 },
217		{ "min",	NANOSEC * (hrtime_t)60 },
218		{ "h",		NANOSEC * (hrtime_t)(60 * 60) },
219		{ "hour",	NANOSEC * (hrtime_t)(60 * 60) },
220		{ "d",		NANOSEC * (hrtime_t)(24 * 60 * 60) },
221		{ "day",	NANOSEC * (hrtime_t)(24 * 60 * 60) },
222		{ "hz",		0 },
223		{ NULL }
224	};
225
226	if (desc == NULL) {
227		char n[PROF_NAMELEN];
228
229		/*
230		 * If no description was provided, provide all of our probes.
231		 */
232		for (i = 0; i < sizeof (profile_rates) / sizeof (int); i++) {
233			if ((rate = profile_rates[i]) == 0)
234				continue;
235
236			(void) snprintf(n, PROF_NAMELEN, "%s%d",
237			    PROF_PREFIX_PROFILE, rate);
238			profile_create(NANOSEC / rate, n, PROF_PROFILE);
239		}
240
241		for (i = 0; i < sizeof (profile_ticks) / sizeof (int); i++) {
242			if ((rate = profile_ticks[i]) == 0)
243				continue;
244
245			(void) snprintf(n, PROF_NAMELEN, "%s%d",
246			    PROF_PREFIX_TICK, rate);
247			profile_create(NANOSEC / rate, n, PROF_TICK);
248		}
249
250		return;
251	}
252
253	name = desc->dtpd_name;
254
255	for (i = 0; types[i].prefix != NULL; i++) {
256		len = strlen(types[i].prefix);
257
258		if (strncmp(name, types[i].prefix, len) != 0)
259			continue;
260		break;
261	}
262
263	if (types[i].prefix == NULL)
264		return;
265
266	kind = types[i].kind;
267	j = strlen(name) - len;
268
269	/*
270	 * We need to start before any time suffix.
271	 */
272	for (j = strlen(name); j >= len; j--) {
273		if (name[j] >= '0' && name[j] <= '9')
274			break;
275		suffix = &name[j];
276	}
277
278	ASSERT(suffix != NULL);
279
280	/*
281	 * Now determine the numerical value present in the probe name.
282	 */
283	for (; j >= len; j--) {
284		if (name[j] < '0' || name[j] > '9')
285			return;
286
287		val += (name[j] - '0') * mult;
288		mult *= (hrtime_t)10;
289	}
290
291	if (val == 0)
292		return;
293
294	/*
295	 * Look-up the suffix to determine the multiplier.
296	 */
297	for (i = 0, mult = 0; suffixes[i].name != NULL; i++) {
298		if (strcasecmp(suffixes[i].name, suffix) == 0) {
299			mult = suffixes[i].mult;
300			break;
301		}
302	}
303
304	if (suffixes[i].name == NULL && *suffix != '\0')
305		return;
306
307	if (mult == 0) {
308		/*
309		 * The default is frequency-per-second.
310		 */
311		val = NANOSEC / val;
312	} else {
313		val *= mult;
314	}
315
316	profile_create(val, name, kind);
317}
318
319/*ARGSUSED*/
320static void
321profile_destroy(void *arg, dtrace_id_t id, void *parg)
322{
323	profile_probe_t *prof = parg;
324
325	ASSERT(prof->prof_cyclic == CYCLIC_NONE);
326	kmem_free(prof, sizeof (profile_probe_t));
327
328	ASSERT(profile_total >= 1);
329	atomic_add_32(&profile_total, -1);
330}
331
332/*ARGSUSED*/
333static void
334profile_online(void *arg, cpu_t *cpu, cyc_handler_t *hdlr, cyc_time_t *when)
335{
336	profile_probe_t *prof = arg;
337	profile_probe_percpu_t *pcpu;
338
339	pcpu = kmem_zalloc(sizeof (profile_probe_percpu_t), KM_SLEEP);
340	pcpu->profc_probe = prof;
341
342	hdlr->cyh_func = profile_fire;
343	hdlr->cyh_arg = pcpu;
344	hdlr->cyh_level = CY_HIGH_LEVEL;
345
346	when->cyt_interval = prof->prof_interval;
347	when->cyt_when = dtrace_gethrtime() + when->cyt_interval;
348
349	pcpu->profc_expected = when->cyt_when;
350	pcpu->profc_interval = when->cyt_interval;
351}
352
353/*ARGSUSED*/
354static void
355profile_offline(void *arg, cpu_t *cpu, void *oarg)
356{
357	profile_probe_percpu_t *pcpu = oarg;
358
359	ASSERT(pcpu->profc_probe == arg);
360	kmem_free(pcpu, sizeof (profile_probe_percpu_t));
361}
362
363/*ARGSUSED*/
364static int
365profile_enable(void *arg, dtrace_id_t id, void *parg)
366{
367	profile_probe_t *prof = parg;
368	cyc_omni_handler_t omni;
369	cyc_handler_t hdlr;
370	cyc_time_t when;
371
372	ASSERT(prof->prof_interval != 0);
373	ASSERT(MUTEX_HELD(&cpu_lock));
374
375	if (prof->prof_kind == PROF_TICK) {
376		hdlr.cyh_func = profile_tick;
377		hdlr.cyh_arg = prof;
378		hdlr.cyh_level = CY_HIGH_LEVEL;
379
380		when.cyt_interval = prof->prof_interval;
381		when.cyt_when = dtrace_gethrtime() + when.cyt_interval;
382	} else {
383		ASSERT(prof->prof_kind == PROF_PROFILE);
384		omni.cyo_online = profile_online;
385		omni.cyo_offline = profile_offline;
386		omni.cyo_arg = prof;
387	}
388
389	if (prof->prof_kind == PROF_TICK) {
390		prof->prof_cyclic = cyclic_add(&hdlr, &when);
391	} else {
392		prof->prof_cyclic = cyclic_add_omni(&omni);
393	}
394	return (0);
395}
396
397/*ARGSUSED*/
398static void
399profile_disable(void *arg, dtrace_id_t id, void *parg)
400{
401	profile_probe_t *prof = parg;
402
403	ASSERT(prof->prof_cyclic != CYCLIC_NONE);
404	ASSERT(MUTEX_HELD(&cpu_lock));
405
406	cyclic_remove(prof->prof_cyclic);
407	prof->prof_cyclic = CYCLIC_NONE;
408}
409
410/*ARGSUSED*/
411static int
412profile_usermode(void *arg, dtrace_id_t id, void *parg)
413{
414	return (CPU->cpu_profile_pc == 0);
415}
416
417static dtrace_pattr_t profile_attr = {
418{ DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
419{ DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_UNKNOWN },
420{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
421{ DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
422{ DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
423};
424
425static dtrace_pops_t profile_pops = {
426	profile_provide,
427	NULL,
428	profile_enable,
429	profile_disable,
430	NULL,
431	NULL,
432	NULL,
433	NULL,
434	profile_usermode,
435	profile_destroy
436};
437
438static int
439profile_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
440{
441	switch (cmd) {
442	case DDI_ATTACH:
443		break;
444	case DDI_RESUME:
445		return (DDI_SUCCESS);
446	default:
447		return (DDI_FAILURE);
448	}
449
450	if (ddi_create_minor_node(devi, "profile", S_IFCHR, 0,
451	    DDI_PSEUDO, NULL) == DDI_FAILURE ||
452	    dtrace_register("profile", &profile_attr,
453	    DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER, NULL,
454	    &profile_pops, NULL, &profile_id) != 0) {
455		ddi_remove_minor_node(devi, NULL);
456		return (DDI_FAILURE);
457	}
458
459	profile_max = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
460	    "profile-max-probes", PROFILE_MAX_DEFAULT);
461
462	ddi_report_dev(devi);
463	profile_devi = devi;
464	return (DDI_SUCCESS);
465}
466
467static int
468profile_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
469{
470	switch (cmd) {
471	case DDI_DETACH:
472		break;
473	case DDI_SUSPEND:
474		return (DDI_SUCCESS);
475	default:
476		return (DDI_FAILURE);
477	}
478
479	if (dtrace_unregister(profile_id) != 0)
480		return (DDI_FAILURE);
481
482	ddi_remove_minor_node(devi, NULL);
483	return (DDI_SUCCESS);
484}
485
486/*ARGSUSED*/
487static int
488profile_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
489{
490	int error;
491
492	switch (infocmd) {
493	case DDI_INFO_DEVT2DEVINFO:
494		*result = (void *)profile_devi;
495		error = DDI_SUCCESS;
496		break;
497	case DDI_INFO_DEVT2INSTANCE:
498		*result = (void *)0;
499		error = DDI_SUCCESS;
500		break;
501	default:
502		error = DDI_FAILURE;
503	}
504	return (error);
505}
506
507/*ARGSUSED*/
508static int
509profile_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
510{
511	return (0);
512}
513
514static struct cb_ops profile_cb_ops = {
515	profile_open,		/* open */
516	nodev,			/* close */
517	nulldev,		/* strategy */
518	nulldev,		/* print */
519	nodev,			/* dump */
520	nodev,			/* read */
521	nodev,			/* write */
522	nodev,			/* ioctl */
523	nodev,			/* devmap */
524	nodev,			/* mmap */
525	nodev,			/* segmap */
526	nochpoll,		/* poll */
527	ddi_prop_op,		/* cb_prop_op */
528	0,			/* streamtab  */
529	D_NEW | D_MP		/* Driver compatibility flag */
530};
531
532static struct dev_ops profile_ops = {
533	DEVO_REV,		/* devo_rev, */
534	0,			/* refcnt  */
535	profile_info,		/* get_dev_info */
536	nulldev,		/* identify */
537	nulldev,		/* probe */
538	profile_attach,		/* attach */
539	profile_detach,		/* detach */
540	nodev,			/* reset */
541	&profile_cb_ops,	/* driver operations */
542	NULL,			/* bus operations */
543	nodev			/* dev power */
544};
545
546/*
547 * Module linkage information for the kernel.
548 */
549static struct modldrv modldrv = {
550	&mod_driverops,		/* module type (this is a pseudo driver) */
551	"Profile Interrupt Tracing",	/* name of module */
552	&profile_ops,		/* driver ops */
553};
554
555static struct modlinkage modlinkage = {
556	MODREV_1,
557	(void *)&modldrv,
558	NULL
559};
560
561int
562_init(void)
563{
564	return (mod_install(&modlinkage));
565}
566
567int
568_info(struct modinfo *modinfop)
569{
570	return (mod_info(&modlinkage, modinfop));
571}
572
573int
574_fini(void)
575{
576	return (mod_remove(&modlinkage));
577}
578