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.
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.
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]
21 * Portions Copyright 2006-2008 John Birrell jb@freebsd.org
28 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
29 * Use is subject to license terms.
32 #include <sys/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/cpuvar.h>
37 #include <sys/fcntl.h>
38 #include <sys/filio.h>
40 #include <sys/kernel.h>
42 #include <sys/kthread.h>
43 #include <sys/limits.h>
44 #include <sys/linker.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/mutex.h>
51 #include <sys/selinfo.h>
54 #include <sys/unistd.h>
55 #include <machine/stdarg.h>
57 #include <sys/cyclic.h>
58 #include <sys/dtrace.h>
59 #include <sys/dtrace_bsd.h>
61 #define PROF_NAMELEN 15
63 #define PROF_PROFILE 0
65 #define PROF_PREFIX_PROFILE "profile-"
66 #define PROF_PREFIX_TICK "tick-"
69 * Regardless of platform, there are five artificial frames in the case of the
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.
84 * On SPARC, the picture is further complicated because the compiler
85 * optimizes away tail-calls -- so the following frames are optimized away:
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.
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.
100 #define PROF_ARTIFICIAL_FRAMES 7
103 #define PROF_ARTIFICIAL_FRAMES 6
107 #define PROF_ARTIFICIAL_FRAMES 4
109 #define PROF_ARTIFICIAL_FRAMES 3
115 typedef struct profile_probe {
116 char prof_name[PROF_NAMELEN];
119 hrtime_t prof_interval;
120 cyclic_id_t prof_cyclic;
123 typedef struct profile_probe_percpu {
124 hrtime_t profc_expected;
125 hrtime_t profc_interval;
126 profile_probe_t *profc_probe;
127 } profile_probe_percpu_t;
129 static d_open_t profile_open;
130 static int profile_unload(void);
131 static void profile_create(hrtime_t, char *, int);
132 static void profile_destroy(void *, dtrace_id_t, void *);
133 static void profile_enable(void *, dtrace_id_t, void *);
134 static void profile_disable(void *, dtrace_id_t, void *);
135 static void profile_load(void *);
136 static void profile_provide(void *, dtrace_probedesc_t *);
138 static int profile_rates[] = {
139 97, 199, 499, 997, 1999,
145 static int profile_ticks[] = {
146 1, 10, 100, 500, 1000,
152 * profile_max defines the upper bound on the number of profile probes that
153 * can exist (this is to prevent malicious or clumsy users from exhausing
154 * system resources by creating a slew of profile probes). At mod load time,
155 * this gets its value from PROFILE_MAX_DEFAULT or profile-max-probes if it's
156 * present in the profile.conf file.
158 #define PROFILE_MAX_DEFAULT 1000 /* default max. number of probes */
159 static uint32_t profile_max = PROFILE_MAX_DEFAULT;
160 /* maximum number of profile probes */
161 static uint32_t profile_total; /* current number of profile probes */
163 static struct cdevsw profile_cdevsw = {
164 .d_version = D_VERSION,
165 .d_open = profile_open,
169 static dtrace_pattr_t profile_attr = {
170 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
171 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
172 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
173 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
174 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
177 static dtrace_pops_t profile_pops = {
190 static struct cdev *profile_cdev;
191 static dtrace_provider_id_t profile_id;
192 static hrtime_t profile_interval_min = NANOSEC / 5000; /* 5000 hz */
193 static int profile_aframes = 0; /* override */
196 profile_fire(void *arg)
198 profile_probe_percpu_t *pcpu = arg;
199 profile_probe_t *prof = pcpu->profc_probe;
201 solaris_cpu_t *c = &solaris_cpu[curcpu];
203 late = gethrtime() - pcpu->profc_expected;
204 pcpu->profc_expected += pcpu->profc_interval;
206 dtrace_probe(prof->prof_id, c->cpu_profile_pc,
207 c->cpu_profile_upc, late, 0, 0);
211 profile_tick(void *arg)
213 profile_probe_t *prof = arg;
214 solaris_cpu_t *c = &solaris_cpu[curcpu];
216 dtrace_probe(prof->prof_id, c->cpu_profile_pc,
217 c->cpu_profile_upc, 0, 0, 0);
221 profile_create(hrtime_t interval, char *name, int kind)
223 profile_probe_t *prof;
225 if (interval < profile_interval_min)
228 if (dtrace_probe_lookup(profile_id, NULL, NULL, name) != 0)
231 atomic_add_32(&profile_total, 1);
232 if (profile_total > profile_max) {
233 atomic_add_32(&profile_total, -1);
237 prof = kmem_zalloc(sizeof (profile_probe_t), KM_SLEEP);
238 (void) strcpy(prof->prof_name, name);
239 prof->prof_interval = interval;
240 prof->prof_cyclic = CYCLIC_NONE;
241 prof->prof_kind = kind;
242 prof->prof_id = dtrace_probe_create(profile_id,
244 profile_aframes ? profile_aframes : PROF_ARTIFICIAL_FRAMES, prof);
249 profile_provide(void *arg, dtrace_probedesc_t *desc)
251 int i, j, rate, kind;
252 hrtime_t val = 0, mult = 1, len = 0;
253 char *name, *suffix = NULL;
259 { PROF_PREFIX_PROFILE, PROF_PROFILE },
260 { PROF_PREFIX_TICK, PROF_TICK },
268 { "ns", NANOSEC / NANOSEC },
269 { "nsec", NANOSEC / NANOSEC },
270 { "us", NANOSEC / MICROSEC },
271 { "usec", NANOSEC / MICROSEC },
272 { "ms", NANOSEC / MILLISEC },
273 { "msec", NANOSEC / MILLISEC },
274 { "s", NANOSEC / SEC },
275 { "sec", NANOSEC / SEC },
276 { "m", NANOSEC * (hrtime_t)60 },
277 { "min", NANOSEC * (hrtime_t)60 },
278 { "h", NANOSEC * (hrtime_t)(60 * 60) },
279 { "hour", NANOSEC * (hrtime_t)(60 * 60) },
280 { "d", NANOSEC * (hrtime_t)(24 * 60 * 60) },
281 { "day", NANOSEC * (hrtime_t)(24 * 60 * 60) },
287 char n[PROF_NAMELEN];
290 * If no description was provided, provide all of our probes.
292 for (i = 0; i < sizeof (profile_rates) / sizeof (int); i++) {
293 if ((rate = profile_rates[i]) == 0)
296 (void) snprintf(n, PROF_NAMELEN, "%s%d",
297 PROF_PREFIX_PROFILE, rate);
298 profile_create(NANOSEC / rate, n, PROF_PROFILE);
301 for (i = 0; i < sizeof (profile_ticks) / sizeof (int); i++) {
302 if ((rate = profile_ticks[i]) == 0)
305 (void) snprintf(n, PROF_NAMELEN, "%s%d",
306 PROF_PREFIX_TICK, rate);
307 profile_create(NANOSEC / rate, n, PROF_TICK);
313 name = desc->dtpd_name;
315 for (i = 0; types[i].prefix != NULL; i++) {
316 len = strlen(types[i].prefix);
318 if (strncmp(name, types[i].prefix, len) != 0)
323 if (types[i].prefix == NULL)
326 kind = types[i].kind;
327 j = strlen(name) - len;
330 * We need to start before any time suffix.
332 for (j = strlen(name); j >= len; j--) {
333 if (name[j] >= '0' && name[j] <= '9')
338 ASSERT(suffix != NULL);
341 * Now determine the numerical value present in the probe name.
343 for (; j >= len; j--) {
344 if (name[j] < '0' || name[j] > '9')
347 val += (name[j] - '0') * mult;
348 mult *= (hrtime_t)10;
355 * Look-up the suffix to determine the multiplier.
357 for (i = 0, mult = 0; suffixes[i].name != NULL; i++) {
358 if (strcasecmp(suffixes[i].name, suffix) == 0) {
359 mult = suffixes[i].mult;
364 if (suffixes[i].name == NULL && *suffix != '\0')
369 * The default is frequency-per-second.
376 profile_create(val, name, kind);
381 profile_destroy(void *arg, dtrace_id_t id, void *parg)
383 profile_probe_t *prof = parg;
385 ASSERT(prof->prof_cyclic == CYCLIC_NONE);
386 kmem_free(prof, sizeof (profile_probe_t));
388 ASSERT(profile_total >= 1);
389 atomic_add_32(&profile_total, -1);
394 profile_online(void *arg, cpu_t *cpu, cyc_handler_t *hdlr, cyc_time_t *when)
396 profile_probe_t *prof = arg;
397 profile_probe_percpu_t *pcpu;
399 pcpu = kmem_zalloc(sizeof (profile_probe_percpu_t), KM_SLEEP);
400 pcpu->profc_probe = prof;
402 hdlr->cyh_func = profile_fire;
403 hdlr->cyh_arg = pcpu;
405 when->cyt_interval = prof->prof_interval;
406 when->cyt_when = gethrtime() + when->cyt_interval;
408 pcpu->profc_expected = when->cyt_when;
409 pcpu->profc_interval = when->cyt_interval;
414 profile_offline(void *arg, cpu_t *cpu, void *oarg)
416 profile_probe_percpu_t *pcpu = oarg;
418 ASSERT(pcpu->profc_probe == arg);
419 kmem_free(pcpu, sizeof (profile_probe_percpu_t));
424 profile_enable(void *arg, dtrace_id_t id, void *parg)
426 profile_probe_t *prof = parg;
427 cyc_omni_handler_t omni;
431 ASSERT(prof->prof_interval != 0);
432 ASSERT(MUTEX_HELD(&cpu_lock));
434 if (prof->prof_kind == PROF_TICK) {
435 hdlr.cyh_func = profile_tick;
438 when.cyt_interval = prof->prof_interval;
439 when.cyt_when = gethrtime() + when.cyt_interval;
441 ASSERT(prof->prof_kind == PROF_PROFILE);
442 omni.cyo_online = profile_online;
443 omni.cyo_offline = profile_offline;
447 if (prof->prof_kind == PROF_TICK) {
448 prof->prof_cyclic = cyclic_add(&hdlr, &when);
450 prof->prof_cyclic = cyclic_add_omni(&omni);
456 profile_disable(void *arg, dtrace_id_t id, void *parg)
458 profile_probe_t *prof = parg;
460 ASSERT(prof->prof_cyclic != CYCLIC_NONE);
461 ASSERT(MUTEX_HELD(&cpu_lock));
463 cyclic_remove(prof->prof_cyclic);
464 prof->prof_cyclic = CYCLIC_NONE;
468 profile_load(void *dummy)
470 /* Create the /dev/dtrace/profile entry. */
471 profile_cdev = make_dev(&profile_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
474 if (dtrace_register("profile", &profile_attr, DTRACE_PRIV_USER,
475 NULL, &profile_pops, NULL, &profile_id) != 0)
485 if ((error = dtrace_unregister(profile_id)) != 0)
488 destroy_dev(profile_cdev);
495 profile_modevent(module_t mod __unused, int type, void *data __unused)
519 profile_open(struct cdev *dev __unused, int oflags __unused, int devtype __unused, struct thread *td __unused)
524 SYSINIT(profile_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, profile_load, NULL);
525 SYSUNINIT(profile_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, profile_unload, NULL);
527 DEV_MODULE(profile, profile_modevent, NULL);
528 MODULE_VERSION(profile, 1);
529 MODULE_DEPEND(profile, dtrace, 1, 1, 1);
530 MODULE_DEPEND(profile, cyclic, 1, 1, 1);
531 MODULE_DEPEND(profile, opensolaris, 1, 1, 1);