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]
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
28 #include <sys/errno.h>
30 #include <sys/modctl.h>
32 #include <sys/systm.h>
34 #include <sys/sunddi.h>
35 #include <sys/cpuvar.h>
37 #include <sys/strsubr.h>
38 #include <sys/dtrace.h>
39 #include <sys/cyclic.h>
40 #include <sys/atomic.h>
42 static dev_info_t *profile_devi;
43 static dtrace_provider_id_t profile_id;
46 * Regardless of platform, the stack frames look like this in the case of the
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
59 * On SPARC, the picture is further complicated because the compiler
60 * optimizes away tail-calls -- so the following frames are optimized away:
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.
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.
75 #define PROF_ARTIFICIAL_FRAMES 10
79 #define PROF_ARTIFICIAL_FRAMES 4
81 #define PROF_ARTIFICIAL_FRAMES 3
86 #define PROF_NAMELEN 15
88 #define PROF_PROFILE 0
90 #define PROF_PREFIX_PROFILE "profile-"
91 #define PROF_PREFIX_TICK "tick-"
93 typedef struct profile_probe {
94 char prof_name[PROF_NAMELEN];
97 hrtime_t prof_interval;
98 cyclic_id_t prof_cyclic;
101 typedef 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;
107 hrtime_t profile_interval_min = NANOSEC / 5000; /* 5000 hz */
108 int profile_aframes = 0; /* override */
110 static int profile_rates[] = {
111 97, 199, 499, 997, 1999,
117 static int profile_ticks[] = {
118 1, 10, 100, 500, 1000,
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.
130 #define PROFILE_MAX_DEFAULT 1000 /* default max. number of probes */
131 static uint32_t profile_max; /* maximum number of profile probes */
132 static uint32_t profile_total; /* current number of profile probes */
135 profile_fire(void *arg)
137 profile_probe_percpu_t *pcpu = arg;
138 profile_probe_t *prof = pcpu->profc_probe;
141 late = dtrace_gethrtime() - pcpu->profc_expected;
142 pcpu->profc_expected += pcpu->profc_interval;
144 dtrace_probe(prof->prof_id, CPU->cpu_profile_pc,
145 CPU->cpu_profile_upc, late, 0, 0);
149 profile_tick(void *arg)
151 profile_probe_t *prof = arg;
153 dtrace_probe(prof->prof_id, CPU->cpu_profile_pc,
154 CPU->cpu_profile_upc, 0, 0, 0);
158 profile_create(hrtime_t interval, const char *name, int kind)
160 profile_probe_t *prof;
161 int nr_frames = PROF_ARTIFICIAL_FRAMES + dtrace_mach_aframes();
164 nr_frames = profile_aframes;
166 if (interval < profile_interval_min)
169 if (dtrace_probe_lookup(profile_id, NULL, NULL, name) != 0)
172 atomic_add_32(&profile_total, 1);
173 if (profile_total > profile_max) {
174 atomic_add_32(&profile_total, -1);
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);
189 profile_provide(void *arg, const dtrace_probedesc_t *desc)
191 int i, j, rate, kind;
192 hrtime_t val = 0, mult = 1, len;
193 const char *name, *suffix = NULL;
199 { PROF_PREFIX_PROFILE, PROF_PROFILE },
200 { PROF_PREFIX_TICK, PROF_TICK },
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) },
227 char n[PROF_NAMELEN];
230 * If no description was provided, provide all of our probes.
232 for (i = 0; i < sizeof (profile_rates) / sizeof (int); i++) {
233 if ((rate = profile_rates[i]) == 0)
236 (void) snprintf(n, PROF_NAMELEN, "%s%d",
237 PROF_PREFIX_PROFILE, rate);
238 profile_create(NANOSEC / rate, n, PROF_PROFILE);
241 for (i = 0; i < sizeof (profile_ticks) / sizeof (int); i++) {
242 if ((rate = profile_ticks[i]) == 0)
245 (void) snprintf(n, PROF_NAMELEN, "%s%d",
246 PROF_PREFIX_TICK, rate);
247 profile_create(NANOSEC / rate, n, PROF_TICK);
253 name = desc->dtpd_name;
255 for (i = 0; types[i].prefix != NULL; i++) {
256 len = strlen(types[i].prefix);
258 if (strncmp(name, types[i].prefix, len) != 0)
263 if (types[i].prefix == NULL)
266 kind = types[i].kind;
267 j = strlen(name) - len;
270 * We need to start before any time suffix.
272 for (j = strlen(name); j >= len; j--) {
273 if (name[j] >= '0' && name[j] <= '9')
278 ASSERT(suffix != NULL);
281 * Now determine the numerical value present in the probe name.
283 for (; j >= len; j--) {
284 if (name[j] < '0' || name[j] > '9')
287 val += (name[j] - '0') * mult;
288 mult *= (hrtime_t)10;
295 * Look-up the suffix to determine the multiplier.
297 for (i = 0, mult = 0; suffixes[i].name != NULL; i++) {
298 if (strcasecmp(suffixes[i].name, suffix) == 0) {
299 mult = suffixes[i].mult;
304 if (suffixes[i].name == NULL && *suffix != '\0')
309 * The default is frequency-per-second.
316 profile_create(val, name, kind);
321 profile_destroy(void *arg, dtrace_id_t id, void *parg)
323 profile_probe_t *prof = parg;
325 ASSERT(prof->prof_cyclic == CYCLIC_NONE);
326 kmem_free(prof, sizeof (profile_probe_t));
328 ASSERT(profile_total >= 1);
329 atomic_add_32(&profile_total, -1);
334 profile_online(void *arg, cpu_t *cpu, cyc_handler_t *hdlr, cyc_time_t *when)
336 profile_probe_t *prof = arg;
337 profile_probe_percpu_t *pcpu;
339 pcpu = kmem_zalloc(sizeof (profile_probe_percpu_t), KM_SLEEP);
340 pcpu->profc_probe = prof;
342 hdlr->cyh_func = profile_fire;
343 hdlr->cyh_arg = pcpu;
344 hdlr->cyh_level = CY_HIGH_LEVEL;
346 when->cyt_interval = prof->prof_interval;
347 when->cyt_when = dtrace_gethrtime() + when->cyt_interval;
349 pcpu->profc_expected = when->cyt_when;
350 pcpu->profc_interval = when->cyt_interval;
355 profile_offline(void *arg, cpu_t *cpu, void *oarg)
357 profile_probe_percpu_t *pcpu = oarg;
359 ASSERT(pcpu->profc_probe == arg);
360 kmem_free(pcpu, sizeof (profile_probe_percpu_t));
365 profile_enable(void *arg, dtrace_id_t id, void *parg)
367 profile_probe_t *prof = parg;
368 cyc_omni_handler_t omni;
372 ASSERT(prof->prof_interval != 0);
373 ASSERT(MUTEX_HELD(&cpu_lock));
375 if (prof->prof_kind == PROF_TICK) {
376 hdlr.cyh_func = profile_tick;
378 hdlr.cyh_level = CY_HIGH_LEVEL;
380 when.cyt_interval = prof->prof_interval;
381 when.cyt_when = dtrace_gethrtime() + when.cyt_interval;
383 ASSERT(prof->prof_kind == PROF_PROFILE);
384 omni.cyo_online = profile_online;
385 omni.cyo_offline = profile_offline;
389 if (prof->prof_kind == PROF_TICK) {
390 prof->prof_cyclic = cyclic_add(&hdlr, &when);
392 prof->prof_cyclic = cyclic_add_omni(&omni);
398 profile_disable(void *arg, dtrace_id_t id, void *parg)
400 profile_probe_t *prof = parg;
402 ASSERT(prof->prof_cyclic != CYCLIC_NONE);
403 ASSERT(MUTEX_HELD(&cpu_lock));
405 cyclic_remove(prof->prof_cyclic);
406 prof->prof_cyclic = CYCLIC_NONE;
411 profile_usermode(void *arg, dtrace_id_t id, void *parg)
413 return (CPU->cpu_profile_pc == 0);
416 static dtrace_pattr_t profile_attr = {
417 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
418 { DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_UNKNOWN },
419 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
420 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
421 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
424 static dtrace_pops_t profile_pops = {
438 profile_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
444 return (DDI_SUCCESS);
446 return (DDI_FAILURE);
449 if (ddi_create_minor_node(devi, "profile", S_IFCHR, 0,
450 DDI_PSEUDO, NULL) == DDI_FAILURE ||
451 dtrace_register("profile", &profile_attr,
452 DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER, NULL,
453 &profile_pops, NULL, &profile_id) != 0) {
454 ddi_remove_minor_node(devi, NULL);
455 return (DDI_FAILURE);
458 profile_max = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
459 "profile-max-probes", PROFILE_MAX_DEFAULT);
461 ddi_report_dev(devi);
463 return (DDI_SUCCESS);
467 profile_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
473 return (DDI_SUCCESS);
475 return (DDI_FAILURE);
478 if (dtrace_unregister(profile_id) != 0)
479 return (DDI_FAILURE);
481 ddi_remove_minor_node(devi, NULL);
482 return (DDI_SUCCESS);
487 profile_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
492 case DDI_INFO_DEVT2DEVINFO:
493 *result = (void *)profile_devi;
496 case DDI_INFO_DEVT2INSTANCE:
508 profile_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
513 static struct cb_ops profile_cb_ops = {
514 profile_open, /* open */
516 nulldev, /* strategy */
526 ddi_prop_op, /* cb_prop_op */
528 D_NEW | D_MP /* Driver compatibility flag */
531 static struct dev_ops profile_ops = {
532 DEVO_REV, /* devo_rev, */
534 profile_info, /* get_dev_info */
535 nulldev, /* identify */
537 profile_attach, /* attach */
538 profile_detach, /* detach */
540 &profile_cb_ops, /* driver operations */
541 NULL, /* bus operations */
542 nodev /* dev power */
546 * Module linkage information for the kernel.
548 static struct modldrv modldrv = {
549 &mod_driverops, /* module type (this is a pseudo driver) */
550 "Profile Interrupt Tracing", /* name of module */
551 &profile_ops, /* driver ops */
554 static struct modlinkage modlinkage = {
563 return (mod_install(&modlinkage));
567 _info(struct modinfo *modinfop)
569 return (mod_info(&modlinkage, modinfop));
575 return (mod_remove(&modlinkage));