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/cpu.h>
56 #include <machine/stdarg.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 10
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 struct profile_probe_percpu;
131 typedef struct profile_probe {
132 char prof_name[PROF_NAMELEN];
136 hrtime_t prof_interval;
137 cyclic_id_t prof_cyclic;
139 sbintime_t prof_interval;
140 struct callout prof_cyclic;
141 sbintime_t prof_expected;
142 struct profile_probe_percpu **prof_pcpus;
146 typedef struct profile_probe_percpu {
147 hrtime_t profc_expected;
148 hrtime_t profc_interval;
149 profile_probe_t *profc_probe;
151 struct callout profc_cyclic;
153 } profile_probe_percpu_t;
155 static d_open_t profile_open;
156 static int profile_unload(void);
157 static void profile_create(hrtime_t, char *, int);
158 static void profile_destroy(void *, dtrace_id_t, void *);
159 static void profile_enable(void *, dtrace_id_t, void *);
160 static void profile_disable(void *, dtrace_id_t, void *);
161 static void profile_load(void *);
162 static void profile_provide(void *, dtrace_probedesc_t *);
164 static int profile_rates[] = {
165 97, 199, 499, 997, 1999,
171 static int profile_ticks[] = {
172 1, 10, 100, 500, 1000,
178 * profile_max defines the upper bound on the number of profile probes that
179 * can exist (this is to prevent malicious or clumsy users from exhausing
180 * system resources by creating a slew of profile probes). At mod load time,
181 * this gets its value from PROFILE_MAX_DEFAULT or profile-max-probes if it's
182 * present in the profile.conf file.
184 #define PROFILE_MAX_DEFAULT 1000 /* default max. number of probes */
185 static uint32_t profile_max = PROFILE_MAX_DEFAULT;
186 /* maximum number of profile probes */
187 static uint32_t profile_total; /* current number of profile probes */
189 static struct cdevsw profile_cdevsw = {
190 .d_version = D_VERSION,
191 .d_open = profile_open,
195 static dtrace_pattr_t profile_attr = {
196 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
197 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
198 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
199 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
200 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
203 static dtrace_pops_t profile_pops = {
216 static struct cdev *profile_cdev;
217 static dtrace_provider_id_t profile_id;
218 static hrtime_t profile_interval_min = NANOSEC / 5000; /* 5000 hz */
219 static int profile_aframes = 0; /* override */
222 nsec_to_sbt(hrtime_t nsec)
227 * We need to calculate nsec * 2^32 / 10^9
228 * Seconds and nanoseconds are split to avoid overflow.
230 sec = nsec / NANOSEC;
231 nsec = nsec % NANOSEC;
232 return (((sbintime_t)sec << 32) | ((sbintime_t)nsec << 32) / NANOSEC);
236 sbt_to_nsec(sbintime_t sbt)
239 return ((sbt >> 32) * NANOSEC +
240 (((uint32_t)sbt * (hrtime_t)NANOSEC) >> 32));
244 profile_fire(void *arg)
246 profile_probe_percpu_t *pcpu = arg;
247 profile_probe_t *prof = pcpu->profc_probe;
249 struct trapframe *frame;
253 late = gethrtime() - pcpu->profc_expected;
255 late = sbt_to_nsec(sbinuptime() - pcpu->profc_expected);
262 * td_intr_frame can be unset if this is a catch up event
263 * after waking up from idle sleep.
264 * This can only happen on a CPU idle thread.
266 frame = curthread->td_intr_frame;
268 if (TRAPF_USERMODE(frame))
269 upc = TRAPF_PC(frame);
271 pc = TRAPF_PC(frame);
273 dtrace_probe(prof->prof_id, pc, upc, late, 0, 0);
275 pcpu->profc_expected += pcpu->profc_interval;
276 callout_schedule_sbt_curcpu(&pcpu->profc_cyclic,
277 pcpu->profc_expected, 0, C_DIRECT_EXEC | C_ABSOLUTE);
281 profile_tick(void *arg)
283 profile_probe_t *prof = arg;
284 struct trapframe *frame;
291 * td_intr_frame can be unset if this is a catch up event
292 * after waking up from idle sleep.
293 * This can only happen on a CPU idle thread.
295 frame = curthread->td_intr_frame;
297 if (TRAPF_USERMODE(frame))
298 upc = TRAPF_PC(frame);
300 pc = TRAPF_PC(frame);
302 dtrace_probe(prof->prof_id, pc, upc, 0, 0, 0);
304 prof->prof_expected += prof->prof_interval;
305 callout_schedule_sbt(&prof->prof_cyclic,
306 prof->prof_expected, 0, C_DIRECT_EXEC | C_ABSOLUTE);
310 profile_create(hrtime_t interval, char *name, int kind)
312 profile_probe_t *prof;
314 if (interval < profile_interval_min)
317 if (dtrace_probe_lookup(profile_id, NULL, NULL, name) != 0)
320 atomic_add_32(&profile_total, 1);
321 if (profile_total > profile_max) {
322 atomic_add_32(&profile_total, -1);
326 prof = kmem_zalloc(sizeof (profile_probe_t), KM_SLEEP);
327 (void) strcpy(prof->prof_name, name);
329 prof->prof_interval = interval;
330 prof->prof_cyclic = CYCLIC_NONE;
332 prof->prof_interval = nsec_to_sbt(interval);
333 callout_init(&prof->prof_cyclic, CALLOUT_MPSAFE);
335 prof->prof_kind = kind;
336 prof->prof_id = dtrace_probe_create(profile_id,
338 profile_aframes ? profile_aframes : PROF_ARTIFICIAL_FRAMES, prof);
343 profile_provide(void *arg, dtrace_probedesc_t *desc)
345 int i, j, rate, kind;
346 hrtime_t val = 0, mult = 1, len = 0;
347 char *name, *suffix = NULL;
353 { PROF_PREFIX_PROFILE, PROF_PROFILE },
354 { PROF_PREFIX_TICK, PROF_TICK },
362 { "ns", NANOSEC / NANOSEC },
363 { "nsec", NANOSEC / NANOSEC },
364 { "us", NANOSEC / MICROSEC },
365 { "usec", NANOSEC / MICROSEC },
366 { "ms", NANOSEC / MILLISEC },
367 { "msec", NANOSEC / MILLISEC },
368 { "s", NANOSEC / SEC },
369 { "sec", NANOSEC / SEC },
370 { "m", NANOSEC * (hrtime_t)60 },
371 { "min", NANOSEC * (hrtime_t)60 },
372 { "h", NANOSEC * (hrtime_t)(60 * 60) },
373 { "hour", NANOSEC * (hrtime_t)(60 * 60) },
374 { "d", NANOSEC * (hrtime_t)(24 * 60 * 60) },
375 { "day", NANOSEC * (hrtime_t)(24 * 60 * 60) },
381 char n[PROF_NAMELEN];
384 * If no description was provided, provide all of our probes.
386 for (i = 0; i < sizeof (profile_rates) / sizeof (int); i++) {
387 if ((rate = profile_rates[i]) == 0)
390 (void) snprintf(n, PROF_NAMELEN, "%s%d",
391 PROF_PREFIX_PROFILE, rate);
392 profile_create(NANOSEC / rate, n, PROF_PROFILE);
395 for (i = 0; i < sizeof (profile_ticks) / sizeof (int); i++) {
396 if ((rate = profile_ticks[i]) == 0)
399 (void) snprintf(n, PROF_NAMELEN, "%s%d",
400 PROF_PREFIX_TICK, rate);
401 profile_create(NANOSEC / rate, n, PROF_TICK);
407 name = desc->dtpd_name;
409 for (i = 0; types[i].prefix != NULL; i++) {
410 len = strlen(types[i].prefix);
412 if (strncmp(name, types[i].prefix, len) != 0)
417 if (types[i].prefix == NULL)
420 kind = types[i].kind;
421 j = strlen(name) - len;
424 * We need to start before any time suffix.
426 for (j = strlen(name); j >= len; j--) {
427 if (name[j] >= '0' && name[j] <= '9')
432 ASSERT(suffix != NULL);
435 * Now determine the numerical value present in the probe name.
437 for (; j >= len; j--) {
438 if (name[j] < '0' || name[j] > '9')
441 val += (name[j] - '0') * mult;
442 mult *= (hrtime_t)10;
449 * Look-up the suffix to determine the multiplier.
451 for (i = 0, mult = 0; suffixes[i].name != NULL; i++) {
452 if (strcasecmp(suffixes[i].name, suffix) == 0) {
453 mult = suffixes[i].mult;
458 if (suffixes[i].name == NULL && *suffix != '\0')
463 * The default is frequency-per-second.
470 profile_create(val, name, kind);
475 profile_destroy(void *arg, dtrace_id_t id, void *parg)
477 profile_probe_t *prof = parg;
480 ASSERT(prof->prof_cyclic == CYCLIC_NONE);
482 ASSERT(!callout_active(&prof->prof_cyclic) && prof->prof_pcpus == NULL);
484 kmem_free(prof, sizeof (profile_probe_t));
486 ASSERT(profile_total >= 1);
487 atomic_add_32(&profile_total, -1);
493 profile_online(void *arg, cpu_t *cpu, cyc_handler_t *hdlr, cyc_time_t *when)
495 profile_probe_t *prof = arg;
496 profile_probe_percpu_t *pcpu;
498 pcpu = kmem_zalloc(sizeof (profile_probe_percpu_t), KM_SLEEP);
499 pcpu->profc_probe = prof;
501 hdlr->cyh_func = profile_fire;
502 hdlr->cyh_arg = pcpu;
504 when->cyt_interval = prof->prof_interval;
505 when->cyt_when = gethrtime() + when->cyt_interval;
507 pcpu->profc_expected = when->cyt_when;
508 pcpu->profc_interval = when->cyt_interval;
513 profile_offline(void *arg, cpu_t *cpu, void *oarg)
515 profile_probe_percpu_t *pcpu = oarg;
517 ASSERT(pcpu->profc_probe == arg);
518 kmem_free(pcpu, sizeof (profile_probe_percpu_t));
523 profile_enable(void *arg, dtrace_id_t id, void *parg)
525 profile_probe_t *prof = parg;
526 cyc_omni_handler_t omni;
530 ASSERT(prof->prof_interval != 0);
531 ASSERT(MUTEX_HELD(&cpu_lock));
533 if (prof->prof_kind == PROF_TICK) {
534 hdlr.cyh_func = profile_tick;
537 when.cyt_interval = prof->prof_interval;
538 when.cyt_when = gethrtime() + when.cyt_interval;
540 ASSERT(prof->prof_kind == PROF_PROFILE);
541 omni.cyo_online = profile_online;
542 omni.cyo_offline = profile_offline;
546 if (prof->prof_kind == PROF_TICK) {
547 prof->prof_cyclic = cyclic_add(&hdlr, &when);
549 prof->prof_cyclic = cyclic_add_omni(&omni);
555 profile_disable(void *arg, dtrace_id_t id, void *parg)
557 profile_probe_t *prof = parg;
559 ASSERT(prof->prof_cyclic != CYCLIC_NONE);
560 ASSERT(MUTEX_HELD(&cpu_lock));
562 cyclic_remove(prof->prof_cyclic);
563 prof->prof_cyclic = CYCLIC_NONE;
569 profile_enable_omni(profile_probe_t *prof)
571 profile_probe_percpu_t *pcpu;
574 prof->prof_pcpus = kmem_zalloc((mp_maxid + 1) * sizeof(pcpu), KM_SLEEP);
576 pcpu = kmem_zalloc(sizeof(profile_probe_percpu_t), KM_SLEEP);
577 prof->prof_pcpus[cpu] = pcpu;
578 pcpu->profc_probe = prof;
579 pcpu->profc_expected = sbinuptime() + prof->prof_interval;
580 pcpu->profc_interval = prof->prof_interval;
581 callout_init(&pcpu->profc_cyclic, CALLOUT_MPSAFE);
582 callout_reset_sbt_on(&pcpu->profc_cyclic,
583 pcpu->profc_expected, 0, profile_fire, pcpu,
584 cpu, C_DIRECT_EXEC | C_ABSOLUTE);
589 profile_disable_omni(profile_probe_t *prof)
591 profile_probe_percpu_t *pcpu;
594 ASSERT(prof->prof_pcpus != NULL);
596 pcpu = prof->prof_pcpus[cpu];
597 ASSERT(pcpu->profc_probe == prof);
598 ASSERT(callout_active(&pcpu->profc_cyclic));
599 callout_stop(&pcpu->profc_cyclic);
600 callout_drain(&pcpu->profc_cyclic);
601 kmem_free(pcpu, sizeof(profile_probe_percpu_t));
603 kmem_free(prof->prof_pcpus, (mp_maxid + 1) * sizeof(pcpu));
604 prof->prof_pcpus = NULL;
609 profile_enable(void *arg, dtrace_id_t id, void *parg)
611 profile_probe_t *prof = parg;
613 if (prof->prof_kind == PROF_TICK) {
614 prof->prof_expected = sbinuptime() + prof->prof_interval;
615 callout_reset_sbt(&prof->prof_cyclic,
616 prof->prof_expected, 0, profile_tick, prof,
617 C_DIRECT_EXEC | C_ABSOLUTE);
619 ASSERT(prof->prof_kind == PROF_PROFILE);
620 profile_enable_omni(prof);
626 profile_disable(void *arg, dtrace_id_t id, void *parg)
628 profile_probe_t *prof = parg;
630 if (prof->prof_kind == PROF_TICK) {
631 ASSERT(callout_active(&prof->prof_cyclic));
632 callout_stop(&prof->prof_cyclic);
633 callout_drain(&prof->prof_cyclic);
635 ASSERT(prof->prof_kind == PROF_PROFILE);
636 profile_disable_omni(prof);
642 profile_load(void *dummy)
644 /* Create the /dev/dtrace/profile entry. */
645 profile_cdev = make_dev(&profile_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
648 if (dtrace_register("profile", &profile_attr, DTRACE_PRIV_USER,
649 NULL, &profile_pops, NULL, &profile_id) != 0)
659 if ((error = dtrace_unregister(profile_id)) != 0)
662 destroy_dev(profile_cdev);
669 profile_modevent(module_t mod __unused, int type, void *data __unused)
693 profile_open(struct cdev *dev __unused, int oflags __unused, int devtype __unused, struct thread *td __unused)
698 SYSINIT(profile_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, profile_load, NULL);
699 SYSUNINIT(profile_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, profile_unload, NULL);
701 DEV_MODULE(profile, profile_modevent, NULL);
702 MODULE_VERSION(profile, 1);
703 MODULE_DEPEND(profile, dtrace, 1, 1, 1);
704 MODULE_DEPEND(profile, opensolaris, 1, 1, 1);