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
117 * This value is bogus just to make module compilable on mips
119 #define PROF_ARTIFICIAL_FRAMES 3
124 * This value is bogus just to make module compilable on powerpc
126 #define PROF_ARTIFICIAL_FRAMES 3
129 typedef struct profile_probe {
130 char prof_name[PROF_NAMELEN];
133 hrtime_t prof_interval;
134 cyclic_id_t prof_cyclic;
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;
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 *);
152 static int profile_rates[] = {
153 97, 199, 499, 997, 1999,
159 static int profile_ticks[] = {
160 1, 10, 100, 500, 1000,
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.
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 */
177 static struct cdevsw profile_cdevsw = {
178 .d_version = D_VERSION,
179 .d_open = profile_open,
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 },
191 static dtrace_pops_t profile_pops = {
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 */
210 profile_fire(void *arg)
212 profile_probe_percpu_t *pcpu = arg;
213 profile_probe_t *prof = pcpu->profc_probe;
215 solaris_cpu_t *c = &solaris_cpu[curcpu];
217 late = gethrtime() - pcpu->profc_expected;
218 pcpu->profc_expected += pcpu->profc_interval;
220 dtrace_probe(prof->prof_id, c->cpu_profile_pc,
221 c->cpu_profile_upc, late, 0, 0);
225 profile_tick(void *arg)
227 profile_probe_t *prof = arg;
228 solaris_cpu_t *c = &solaris_cpu[curcpu];
230 dtrace_probe(prof->prof_id, c->cpu_profile_pc,
231 c->cpu_profile_upc, 0, 0, 0);
235 profile_create(hrtime_t interval, char *name, int kind)
237 profile_probe_t *prof;
239 if (interval < profile_interval_min)
242 if (dtrace_probe_lookup(profile_id, NULL, NULL, name) != 0)
245 atomic_add_32(&profile_total, 1);
246 if (profile_total > profile_max) {
247 atomic_add_32(&profile_total, -1);
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,
258 profile_aframes ? profile_aframes : PROF_ARTIFICIAL_FRAMES, prof);
263 profile_provide(void *arg, dtrace_probedesc_t *desc)
265 int i, j, rate, kind;
266 hrtime_t val = 0, mult = 1, len = 0;
267 char *name, *suffix = NULL;
273 { PROF_PREFIX_PROFILE, PROF_PROFILE },
274 { PROF_PREFIX_TICK, PROF_TICK },
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) },
301 char n[PROF_NAMELEN];
304 * If no description was provided, provide all of our probes.
306 for (i = 0; i < sizeof (profile_rates) / sizeof (int); i++) {
307 if ((rate = profile_rates[i]) == 0)
310 (void) snprintf(n, PROF_NAMELEN, "%s%d",
311 PROF_PREFIX_PROFILE, rate);
312 profile_create(NANOSEC / rate, n, PROF_PROFILE);
315 for (i = 0; i < sizeof (profile_ticks) / sizeof (int); i++) {
316 if ((rate = profile_ticks[i]) == 0)
319 (void) snprintf(n, PROF_NAMELEN, "%s%d",
320 PROF_PREFIX_TICK, rate);
321 profile_create(NANOSEC / rate, n, PROF_TICK);
327 name = desc->dtpd_name;
329 for (i = 0; types[i].prefix != NULL; i++) {
330 len = strlen(types[i].prefix);
332 if (strncmp(name, types[i].prefix, len) != 0)
337 if (types[i].prefix == NULL)
340 kind = types[i].kind;
341 j = strlen(name) - len;
344 * We need to start before any time suffix.
346 for (j = strlen(name); j >= len; j--) {
347 if (name[j] >= '0' && name[j] <= '9')
352 ASSERT(suffix != NULL);
355 * Now determine the numerical value present in the probe name.
357 for (; j >= len; j--) {
358 if (name[j] < '0' || name[j] > '9')
361 val += (name[j] - '0') * mult;
362 mult *= (hrtime_t)10;
369 * Look-up the suffix to determine the multiplier.
371 for (i = 0, mult = 0; suffixes[i].name != NULL; i++) {
372 if (strcasecmp(suffixes[i].name, suffix) == 0) {
373 mult = suffixes[i].mult;
378 if (suffixes[i].name == NULL && *suffix != '\0')
383 * The default is frequency-per-second.
390 profile_create(val, name, kind);
395 profile_destroy(void *arg, dtrace_id_t id, void *parg)
397 profile_probe_t *prof = parg;
399 ASSERT(prof->prof_cyclic == CYCLIC_NONE);
400 kmem_free(prof, sizeof (profile_probe_t));
402 ASSERT(profile_total >= 1);
403 atomic_add_32(&profile_total, -1);
408 profile_online(void *arg, cpu_t *cpu, cyc_handler_t *hdlr, cyc_time_t *when)
410 profile_probe_t *prof = arg;
411 profile_probe_percpu_t *pcpu;
413 pcpu = kmem_zalloc(sizeof (profile_probe_percpu_t), KM_SLEEP);
414 pcpu->profc_probe = prof;
416 hdlr->cyh_func = profile_fire;
417 hdlr->cyh_arg = pcpu;
419 when->cyt_interval = prof->prof_interval;
420 when->cyt_when = gethrtime() + when->cyt_interval;
422 pcpu->profc_expected = when->cyt_when;
423 pcpu->profc_interval = when->cyt_interval;
428 profile_offline(void *arg, cpu_t *cpu, void *oarg)
430 profile_probe_percpu_t *pcpu = oarg;
432 ASSERT(pcpu->profc_probe == arg);
433 kmem_free(pcpu, sizeof (profile_probe_percpu_t));
438 profile_enable(void *arg, dtrace_id_t id, void *parg)
440 profile_probe_t *prof = parg;
441 cyc_omni_handler_t omni;
445 ASSERT(prof->prof_interval != 0);
446 ASSERT(MUTEX_HELD(&cpu_lock));
448 if (prof->prof_kind == PROF_TICK) {
449 hdlr.cyh_func = profile_tick;
452 when.cyt_interval = prof->prof_interval;
453 when.cyt_when = gethrtime() + when.cyt_interval;
455 ASSERT(prof->prof_kind == PROF_PROFILE);
456 omni.cyo_online = profile_online;
457 omni.cyo_offline = profile_offline;
461 if (prof->prof_kind == PROF_TICK) {
462 prof->prof_cyclic = cyclic_add(&hdlr, &when);
464 prof->prof_cyclic = cyclic_add_omni(&omni);
470 profile_disable(void *arg, dtrace_id_t id, void *parg)
472 profile_probe_t *prof = parg;
474 ASSERT(prof->prof_cyclic != CYCLIC_NONE);
475 ASSERT(MUTEX_HELD(&cpu_lock));
477 cyclic_remove(prof->prof_cyclic);
478 prof->prof_cyclic = CYCLIC_NONE;
482 profile_load(void *dummy)
484 /* Create the /dev/dtrace/profile entry. */
485 profile_cdev = make_dev(&profile_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
488 if (dtrace_register("profile", &profile_attr, DTRACE_PRIV_USER,
489 NULL, &profile_pops, NULL, &profile_id) != 0)
499 if ((error = dtrace_unregister(profile_id)) != 0)
502 destroy_dev(profile_cdev);
509 profile_modevent(module_t mod __unused, int type, void *data __unused)
533 profile_open(struct cdev *dev __unused, int oflags __unused, int devtype __unused, struct thread *td __unused)
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);
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);