xref: /freebsd/sys/cddl/dev/profile/profile.c (revision 8d20be1e22095c27faf8fe8b2f0d089739cc742e)
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  * Portions Copyright 2006-2008 John Birrell jb@freebsd.org
22  *
23  * $FreeBSD$
24  *
25  */
26 
27 /*
28  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
29  * Use is subject to license terms.
30  */
31 
32 #include <sys/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/conf.h>
36 #include <sys/cpuvar.h>
37 #include <sys/fcntl.h>
38 #include <sys/filio.h>
39 #include <sys/kdb.h>
40 #include <sys/kernel.h>
41 #include <sys/kmem.h>
42 #include <sys/kthread.h>
43 #include <sys/limits.h>
44 #include <sys/linker.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/mutex.h>
49 #include <sys/poll.h>
50 #include <sys/proc.h>
51 #include <sys/selinfo.h>
52 #include <sys/smp.h>
53 #include <sys/uio.h>
54 #include <sys/unistd.h>
55 #include <machine/stdarg.h>
56 
57 #include <sys/cyclic.h>
58 #include <sys/dtrace.h>
59 #include <sys/dtrace_bsd.h>
60 
61 #define	PROF_NAMELEN		15
62 
63 #define	PROF_PROFILE		0
64 #define	PROF_TICK		1
65 #define	PROF_PREFIX_PROFILE	"profile-"
66 #define	PROF_PREFIX_TICK	"tick-"
67 
68 /*
69  * Regardless of platform, there are five artificial frames in the case of the
70  * profile provider:
71  *
72  *	profile_fire
73  *	cyclic_expire
74  *	cyclic_fire
75  *	[ cbe ]
76  *	[ locore ]
77  *
78  * On amd64, there are two frames associated with locore:  one in locore, and
79  * another in common interrupt dispatch code.  (i386 has not been modified to
80  * use this common layer.)  Further, on i386, the interrupted instruction
81  * appears as its own stack frame.  All of this means that we need to add one
82  * frame for amd64, and then take one away for both amd64 and i386.
83  *
84  * On SPARC, the picture is further complicated because the compiler
85  * optimizes away tail-calls -- so the following frames are optimized away:
86  *
87  * 	profile_fire
88  *	cyclic_expire
89  *
90  * This gives three frames.  However, on DEBUG kernels, the cyclic_expire
91  * frame cannot be tail-call eliminated, yielding four frames in this case.
92  *
93  * All of the above constraints lead to the mess below.  Yes, the profile
94  * provider should ideally figure this out on-the-fly by hiting one of its own
95  * probes and then walking its own stack trace.  This is complicated, however,
96  * and the static definition doesn't seem to be overly brittle.  Still, we
97  * allow for a manual override in case we get it completely wrong.
98  */
99 #ifdef __amd64
100 #define	PROF_ARTIFICIAL_FRAMES	7
101 #else
102 #ifdef __i386
103 #define	PROF_ARTIFICIAL_FRAMES	6
104 #else
105 #ifdef __sparc
106 #ifdef DEBUG
107 #define	PROF_ARTIFICIAL_FRAMES	4
108 #else
109 #define	PROF_ARTIFICIAL_FRAMES	3
110 #endif
111 #endif
112 #endif
113 #endif
114 
115 #ifdef __mips
116 /*
117  * This value is bogus just to make module compilable on mips
118  */
119 #define	PROF_ARTIFICIAL_FRAMES	3
120 #endif
121 
122 #ifdef __powerpc__
123 /*
124  * This value is bogus just to make module compilable on powerpc
125  */
126 #define	PROF_ARTIFICIAL_FRAMES	3
127 #endif
128 
129 typedef struct profile_probe {
130 	char		prof_name[PROF_NAMELEN];
131 	dtrace_id_t	prof_id;
132 	int		prof_kind;
133 	hrtime_t	prof_interval;
134 	cyclic_id_t	prof_cyclic;
135 } profile_probe_t;
136 
137 typedef struct profile_probe_percpu {
138 	hrtime_t	profc_expected;
139 	hrtime_t	profc_interval;
140 	profile_probe_t	*profc_probe;
141 } profile_probe_percpu_t;
142 
143 static d_open_t	profile_open;
144 static int	profile_unload(void);
145 static void	profile_create(hrtime_t, char *, int);
146 static void	profile_destroy(void *, dtrace_id_t, void *);
147 static void	profile_enable(void *, dtrace_id_t, void *);
148 static void	profile_disable(void *, dtrace_id_t, void *);
149 static void	profile_load(void *);
150 static void	profile_provide(void *, dtrace_probedesc_t *);
151 
152 static int profile_rates[] = {
153     97, 199, 499, 997, 1999,
154     4001, 4999, 0, 0, 0,
155     0, 0, 0, 0, 0,
156     0, 0, 0, 0, 0
157 };
158 
159 static int profile_ticks[] = {
160     1, 10, 100, 500, 1000,
161     5000, 0, 0, 0, 0,
162     0, 0, 0, 0, 0
163 };
164 
165 /*
166  * profile_max defines the upper bound on the number of profile probes that
167  * can exist (this is to prevent malicious or clumsy users from exhausing
168  * system resources by creating a slew of profile probes). At mod load time,
169  * this gets its value from PROFILE_MAX_DEFAULT or profile-max-probes if it's
170  * present in the profile.conf file.
171  */
172 #define	PROFILE_MAX_DEFAULT	1000	/* default max. number of probes */
173 static uint32_t profile_max = PROFILE_MAX_DEFAULT;
174 					/* maximum number of profile probes */
175 static uint32_t profile_total;		/* current number of profile probes */
176 
177 static struct cdevsw profile_cdevsw = {
178 	.d_version	= D_VERSION,
179 	.d_open		= profile_open,
180 	.d_name		= "profile",
181 };
182 
183 static dtrace_pattr_t profile_attr = {
184 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
185 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
186 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
187 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
188 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
189 };
190 
191 static dtrace_pops_t profile_pops = {
192 	profile_provide,
193 	NULL,
194 	profile_enable,
195 	profile_disable,
196 	NULL,
197 	NULL,
198 	NULL,
199 	NULL,
200 	NULL,
201 	profile_destroy
202 };
203 
204 static struct cdev		*profile_cdev;
205 static dtrace_provider_id_t	profile_id;
206 static hrtime_t			profile_interval_min = NANOSEC / 5000;	/* 5000 hz */
207 static int			profile_aframes = 0;			/* override */
208 
209 static void
210 profile_fire(void *arg)
211 {
212 	profile_probe_percpu_t *pcpu = arg;
213 	profile_probe_t *prof = pcpu->profc_probe;
214 	hrtime_t late;
215 	solaris_cpu_t *c = &solaris_cpu[curcpu];
216 
217 	late = gethrtime() - pcpu->profc_expected;
218 	pcpu->profc_expected += pcpu->profc_interval;
219 
220 	dtrace_probe(prof->prof_id, c->cpu_profile_pc,
221 	    c->cpu_profile_upc, late, 0, 0);
222 }
223 
224 static void
225 profile_tick(void *arg)
226 {
227 	profile_probe_t *prof = arg;
228 	solaris_cpu_t *c = &solaris_cpu[curcpu];
229 
230 	dtrace_probe(prof->prof_id, c->cpu_profile_pc,
231 	    c->cpu_profile_upc, 0, 0, 0);
232 }
233 
234 static void
235 profile_create(hrtime_t interval, char *name, int kind)
236 {
237 	profile_probe_t *prof;
238 
239 	if (interval < profile_interval_min)
240 		return;
241 
242 	if (dtrace_probe_lookup(profile_id, NULL, NULL, name) != 0)
243 		return;
244 
245 	atomic_add_32(&profile_total, 1);
246 	if (profile_total > profile_max) {
247 		atomic_add_32(&profile_total, -1);
248 		return;
249 	}
250 
251 	prof = kmem_zalloc(sizeof (profile_probe_t), KM_SLEEP);
252 	(void) strcpy(prof->prof_name, name);
253 	prof->prof_interval = interval;
254 	prof->prof_cyclic = CYCLIC_NONE;
255 	prof->prof_kind = kind;
256 	prof->prof_id = dtrace_probe_create(profile_id,
257 	    NULL, NULL, name,
258 	    profile_aframes ? profile_aframes : PROF_ARTIFICIAL_FRAMES, prof);
259 }
260 
261 /*ARGSUSED*/
262 static void
263 profile_provide(void *arg, dtrace_probedesc_t *desc)
264 {
265 	int i, j, rate, kind;
266 	hrtime_t val = 0, mult = 1, len = 0;
267 	char *name, *suffix = NULL;
268 
269 	const struct {
270 		char *prefix;
271 		int kind;
272 	} types[] = {
273 		{ PROF_PREFIX_PROFILE, PROF_PROFILE },
274 		{ PROF_PREFIX_TICK, PROF_TICK },
275 		{ 0, 0 }
276 	};
277 
278 	const struct {
279 		char *name;
280 		hrtime_t mult;
281 	} suffixes[] = {
282 		{ "ns", 	NANOSEC / NANOSEC },
283 		{ "nsec",	NANOSEC / NANOSEC },
284 		{ "us",		NANOSEC / MICROSEC },
285 		{ "usec",	NANOSEC / MICROSEC },
286 		{ "ms",		NANOSEC / MILLISEC },
287 		{ "msec",	NANOSEC / MILLISEC },
288 		{ "s",		NANOSEC / SEC },
289 		{ "sec",	NANOSEC / SEC },
290 		{ "m",		NANOSEC * (hrtime_t)60 },
291 		{ "min",	NANOSEC * (hrtime_t)60 },
292 		{ "h",		NANOSEC * (hrtime_t)(60 * 60) },
293 		{ "hour",	NANOSEC * (hrtime_t)(60 * 60) },
294 		{ "d",		NANOSEC * (hrtime_t)(24 * 60 * 60) },
295 		{ "day",	NANOSEC * (hrtime_t)(24 * 60 * 60) },
296 		{ "hz",		0 },
297 		{ NULL }
298 	};
299 
300 	if (desc == NULL) {
301 		char n[PROF_NAMELEN];
302 
303 		/*
304 		 * If no description was provided, provide all of our probes.
305 		 */
306 		for (i = 0; i < sizeof (profile_rates) / sizeof (int); i++) {
307 			if ((rate = profile_rates[i]) == 0)
308 				continue;
309 
310 			(void) snprintf(n, PROF_NAMELEN, "%s%d",
311 			    PROF_PREFIX_PROFILE, rate);
312 			profile_create(NANOSEC / rate, n, PROF_PROFILE);
313 		}
314 
315 		for (i = 0; i < sizeof (profile_ticks) / sizeof (int); i++) {
316 			if ((rate = profile_ticks[i]) == 0)
317 				continue;
318 
319 			(void) snprintf(n, PROF_NAMELEN, "%s%d",
320 			    PROF_PREFIX_TICK, rate);
321 			profile_create(NANOSEC / rate, n, PROF_TICK);
322 		}
323 
324 		return;
325 	}
326 
327 	name = desc->dtpd_name;
328 
329 	for (i = 0; types[i].prefix != NULL; i++) {
330 		len = strlen(types[i].prefix);
331 
332 		if (strncmp(name, types[i].prefix, len) != 0)
333 			continue;
334 		break;
335 	}
336 
337 	if (types[i].prefix == NULL)
338 		return;
339 
340 	kind = types[i].kind;
341 	j = strlen(name) - len;
342 
343 	/*
344 	 * We need to start before any time suffix.
345 	 */
346 	for (j = strlen(name); j >= len; j--) {
347 		if (name[j] >= '0' && name[j] <= '9')
348 			break;
349 		suffix = &name[j];
350 	}
351 
352 	ASSERT(suffix != NULL);
353 
354 	/*
355 	 * Now determine the numerical value present in the probe name.
356 	 */
357 	for (; j >= len; j--) {
358 		if (name[j] < '0' || name[j] > '9')
359 			return;
360 
361 		val += (name[j] - '0') * mult;
362 		mult *= (hrtime_t)10;
363 	}
364 
365 	if (val == 0)
366 		return;
367 
368 	/*
369 	 * Look-up the suffix to determine the multiplier.
370 	 */
371 	for (i = 0, mult = 0; suffixes[i].name != NULL; i++) {
372 		if (strcasecmp(suffixes[i].name, suffix) == 0) {
373 			mult = suffixes[i].mult;
374 			break;
375 		}
376 	}
377 
378 	if (suffixes[i].name == NULL && *suffix != '\0')
379 		return;
380 
381 	if (mult == 0) {
382 		/*
383 		 * The default is frequency-per-second.
384 		 */
385 		val = NANOSEC / val;
386 	} else {
387 		val *= mult;
388 	}
389 
390 	profile_create(val, name, kind);
391 }
392 
393 /* ARGSUSED */
394 static void
395 profile_destroy(void *arg, dtrace_id_t id, void *parg)
396 {
397 	profile_probe_t *prof = parg;
398 
399 	ASSERT(prof->prof_cyclic == CYCLIC_NONE);
400 	kmem_free(prof, sizeof (profile_probe_t));
401 
402 	ASSERT(profile_total >= 1);
403 	atomic_add_32(&profile_total, -1);
404 }
405 
406 /*ARGSUSED*/
407 static void
408 profile_online(void *arg, cpu_t *cpu, cyc_handler_t *hdlr, cyc_time_t *when)
409 {
410 	profile_probe_t *prof = arg;
411 	profile_probe_percpu_t *pcpu;
412 
413 	pcpu = kmem_zalloc(sizeof (profile_probe_percpu_t), KM_SLEEP);
414 	pcpu->profc_probe = prof;
415 
416 	hdlr->cyh_func = profile_fire;
417 	hdlr->cyh_arg = pcpu;
418 
419 	when->cyt_interval = prof->prof_interval;
420 	when->cyt_when = gethrtime() + when->cyt_interval;
421 
422 	pcpu->profc_expected = when->cyt_when;
423 	pcpu->profc_interval = when->cyt_interval;
424 }
425 
426 /*ARGSUSED*/
427 static void
428 profile_offline(void *arg, cpu_t *cpu, void *oarg)
429 {
430 	profile_probe_percpu_t *pcpu = oarg;
431 
432 	ASSERT(pcpu->profc_probe == arg);
433 	kmem_free(pcpu, sizeof (profile_probe_percpu_t));
434 }
435 
436 /* ARGSUSED */
437 static void
438 profile_enable(void *arg, dtrace_id_t id, void *parg)
439 {
440 	profile_probe_t *prof = parg;
441 	cyc_omni_handler_t omni;
442 	cyc_handler_t hdlr;
443 	cyc_time_t when;
444 
445 	ASSERT(prof->prof_interval != 0);
446 	ASSERT(MUTEX_HELD(&cpu_lock));
447 
448 	if (prof->prof_kind == PROF_TICK) {
449 		hdlr.cyh_func = profile_tick;
450 		hdlr.cyh_arg = prof;
451 
452 		when.cyt_interval = prof->prof_interval;
453 		when.cyt_when = gethrtime() + when.cyt_interval;
454 	} else {
455 		ASSERT(prof->prof_kind == PROF_PROFILE);
456 		omni.cyo_online = profile_online;
457 		omni.cyo_offline = profile_offline;
458 		omni.cyo_arg = prof;
459 	}
460 
461 	if (prof->prof_kind == PROF_TICK) {
462 		prof->prof_cyclic = cyclic_add(&hdlr, &when);
463 	} else {
464 		prof->prof_cyclic = cyclic_add_omni(&omni);
465 	}
466 }
467 
468 /* ARGSUSED */
469 static void
470 profile_disable(void *arg, dtrace_id_t id, void *parg)
471 {
472 	profile_probe_t *prof = parg;
473 
474 	ASSERT(prof->prof_cyclic != CYCLIC_NONE);
475 	ASSERT(MUTEX_HELD(&cpu_lock));
476 
477 	cyclic_remove(prof->prof_cyclic);
478 	prof->prof_cyclic = CYCLIC_NONE;
479 }
480 
481 static void
482 profile_load(void *dummy)
483 {
484 	/* Create the /dev/dtrace/profile entry. */
485 	profile_cdev = make_dev(&profile_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
486 	    "dtrace/profile");
487 
488 	if (dtrace_register("profile", &profile_attr, DTRACE_PRIV_USER,
489 	    NULL, &profile_pops, NULL, &profile_id) != 0)
490 		return;
491 }
492 
493 
494 static int
495 profile_unload()
496 {
497 	int error = 0;
498 
499 	if ((error = dtrace_unregister(profile_id)) != 0)
500 		return (error);
501 
502 	destroy_dev(profile_cdev);
503 
504 	return (error);
505 }
506 
507 /* ARGSUSED */
508 static int
509 profile_modevent(module_t mod __unused, int type, void *data __unused)
510 {
511 	int error = 0;
512 
513 	switch (type) {
514 	case MOD_LOAD:
515 		break;
516 
517 	case MOD_UNLOAD:
518 		break;
519 
520 	case MOD_SHUTDOWN:
521 		break;
522 
523 	default:
524 		error = EOPNOTSUPP;
525 		break;
526 
527 	}
528 	return (error);
529 }
530 
531 /* ARGSUSED */
532 static int
533 profile_open(struct cdev *dev __unused, int oflags __unused, int devtype __unused, struct thread *td __unused)
534 {
535 	return (0);
536 }
537 
538 SYSINIT(profile_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, profile_load, NULL);
539 SYSUNINIT(profile_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, profile_unload, NULL);
540 
541 DEV_MODULE(profile, profile_modevent, NULL);
542 MODULE_VERSION(profile, 1);
543 MODULE_DEPEND(profile, dtrace, 1, 1, 1);
544 MODULE_DEPEND(profile, cyclic, 1, 1, 1);
545 MODULE_DEPEND(profile, opensolaris, 1, 1, 1);
546