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/endian.h>
38 #include <sys/fcntl.h>
39 #include <sys/filio.h>
41 #include <sys/kernel.h>
43 #include <sys/kthread.h>
44 #include <sys/limits.h>
45 #include <sys/linker.h>
47 #include <sys/malloc.h>
48 #include <sys/module.h>
49 #include <sys/mutex.h>
52 #include <sys/selinfo.h>
54 #include <sys/sysctl.h>
56 #include <sys/unistd.h>
57 #include <machine/cpu.h>
58 #include <machine/stdarg.h>
60 #include <sys/dtrace.h>
61 #include <sys/dtrace_bsd.h>
63 #define PROF_NAMELEN 15
65 #define PROF_PROFILE 0
67 #define PROF_PREFIX_PROFILE "profile-"
68 #define PROF_PREFIX_TICK "tick-"
71 * Regardless of platform, there are five artificial frames in the case of the
80 * On amd64, there are two frames associated with locore: one in locore, and
81 * another in common interrupt dispatch code. (i386 has not been modified to
82 * use this common layer.) Further, on i386, the interrupted instruction
83 * appears as its own stack frame. All of this means that we need to add one
84 * frame for amd64, and then take one away for both amd64 and i386.
86 * All of the above constraints lead to the mess below. Yes, the profile
87 * provider should ideally figure this out on-the-fly by hiting one of its own
88 * probes and then walking its own stack trace. This is complicated, however,
89 * and the static definition doesn't seem to be overly brittle. Still, we
90 * allow for a manual override in case we get it completely wrong.
93 #define PROF_ARTIFICIAL_FRAMES 10
96 #define PROF_ARTIFICIAL_FRAMES 6
102 * This value is bogus just to make module compilable on powerpc
104 #define PROF_ARTIFICIAL_FRAMES 8
107 struct profile_probe_percpu;
110 #define PROF_ARTIFICIAL_FRAMES 3
114 #define PROF_ARTIFICIAL_FRAMES 12
118 #define PROF_ARTIFICIAL_FRAMES 12
121 typedef struct profile_probe {
122 char prof_name[PROF_NAMELEN];
126 hrtime_t prof_interval;
127 cyclic_id_t prof_cyclic;
129 sbintime_t prof_interval;
130 struct callout prof_cyclic;
131 sbintime_t prof_expected;
132 struct profile_probe_percpu **prof_pcpus;
136 typedef struct profile_probe_percpu {
137 hrtime_t profc_expected;
138 hrtime_t profc_interval;
139 profile_probe_t *profc_probe;
141 struct callout profc_cyclic;
143 } profile_probe_percpu_t;
145 static int profile_unload(void);
146 static void profile_create(hrtime_t, char *, int);
147 static void profile_destroy(void *, dtrace_id_t, void *);
148 static void profile_enable(void *, dtrace_id_t, void *);
149 static void profile_disable(void *, dtrace_id_t, void *);
150 static void profile_load(void *);
151 static void profile_provide(void *, dtrace_probedesc_t *);
153 static int profile_rates[] = {
154 97, 199, 499, 997, 1999,
160 static int profile_ticks[] = {
161 1, 10, 100, 500, 1000,
167 * profile_max defines the upper bound on the number of profile probes that
168 * can exist (this is to prevent malicious or clumsy users from exhausing
169 * system resources by creating a slew of profile probes). At mod load time,
170 * this gets its value from PROFILE_MAX_DEFAULT or profile-max-probes if it's
171 * present in the profile.conf file.
173 #define PROFILE_MAX_DEFAULT 1000 /* default max. number of probes */
174 static uint32_t profile_max = PROFILE_MAX_DEFAULT;
175 /* maximum number of profile probes */
176 static uint32_t profile_total; /* current number of profile probes */
178 static dtrace_pattr_t profile_attr = {
179 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
180 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
181 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
182 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
183 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
186 static dtrace_pops_t profile_pops = {
187 .dtps_provide = profile_provide,
188 .dtps_provide_module = NULL,
189 .dtps_enable = profile_enable,
190 .dtps_disable = profile_disable,
191 .dtps_suspend = NULL,
193 .dtps_getargdesc = NULL,
194 .dtps_getargval = NULL,
195 .dtps_usermode = NULL,
196 .dtps_destroy = profile_destroy
199 static dtrace_provider_id_t profile_id;
200 static hrtime_t profile_interval_min = NANOSEC / 5000; /* 5000 hz */
201 static int profile_aframes = PROF_ARTIFICIAL_FRAMES;
203 SYSCTL_DECL(_kern_dtrace);
204 SYSCTL_NODE(_kern_dtrace, OID_AUTO, profile, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
205 "DTrace profile parameters");
206 SYSCTL_INT(_kern_dtrace_profile, OID_AUTO, aframes, CTLFLAG_RW, &profile_aframes,
207 0, "Skipped frames for profile provider");
210 nsec_to_sbt(hrtime_t nsec)
215 * We need to calculate nsec * 2^32 / 10^9
216 * Seconds and nanoseconds are split to avoid overflow.
218 sec = nsec / NANOSEC;
219 nsec = nsec % NANOSEC;
220 return (((sbintime_t)sec << 32) | ((sbintime_t)nsec << 32) / NANOSEC);
224 sbt_to_nsec(sbintime_t sbt)
227 return ((sbt >> 32) * NANOSEC +
228 (((uint32_t)sbt * (hrtime_t)NANOSEC) >> 32));
232 profile_probe(profile_probe_t *prof, hrtime_t late)
235 struct trapframe *frame;
242 * td_intr_frame can be unset if this is a catch-up event upon waking up
243 * from idle sleep. This can only happen on a CPU idle thread. Use a
244 * representative arg0 value in this case so that one of the probe
245 * arguments is non-zero.
247 frame = td->td_intr_frame;
249 if (TRAPF_USERMODE(frame))
250 upc = TRAPF_PC(frame);
252 pc = TRAPF_PC(frame);
253 } else if (TD_IS_IDLETHREAD(td))
254 pc = (uintfptr_t)&cpu_idle;
256 dtrace_probe(prof->prof_id, pc, upc, late, 0, 0);
260 profile_fire(void *arg)
262 profile_probe_percpu_t *pcpu = arg;
263 profile_probe_t *prof = pcpu->profc_probe;
266 late = sbt_to_nsec(sbinuptime() - pcpu->profc_expected);
268 profile_probe(prof, late);
269 pcpu->profc_expected += pcpu->profc_interval;
270 callout_schedule_sbt_curcpu(&pcpu->profc_cyclic,
271 pcpu->profc_expected, 0, C_DIRECT_EXEC | C_ABSOLUTE);
275 profile_tick(void *arg)
277 profile_probe_t *prof = arg;
279 profile_probe(prof, 0);
280 prof->prof_expected += prof->prof_interval;
281 callout_schedule_sbt(&prof->prof_cyclic,
282 prof->prof_expected, 0, C_DIRECT_EXEC | C_ABSOLUTE);
286 profile_create(hrtime_t interval, char *name, int kind)
288 profile_probe_t *prof;
290 if (interval < profile_interval_min)
293 if (dtrace_probe_lookup(profile_id, NULL, NULL, name) != 0)
296 atomic_add_32(&profile_total, 1);
297 if (profile_total > profile_max) {
298 atomic_add_32(&profile_total, -1);
302 prof = kmem_zalloc(sizeof (profile_probe_t), KM_SLEEP);
303 (void) strcpy(prof->prof_name, name);
305 prof->prof_interval = interval;
306 prof->prof_cyclic = CYCLIC_NONE;
308 prof->prof_interval = nsec_to_sbt(interval);
309 callout_init(&prof->prof_cyclic, 1);
311 prof->prof_kind = kind;
312 prof->prof_id = dtrace_probe_create(profile_id,
314 profile_aframes, prof);
319 profile_provide(void *arg, dtrace_probedesc_t *desc)
321 int i, j, rate, kind;
322 hrtime_t val = 0, mult = 1, len = 0;
323 char *name, *suffix = NULL;
329 { PROF_PREFIX_PROFILE, PROF_PROFILE },
330 { PROF_PREFIX_TICK, PROF_TICK },
338 { "ns", NANOSEC / NANOSEC },
339 { "nsec", NANOSEC / NANOSEC },
340 { "us", NANOSEC / MICROSEC },
341 { "usec", NANOSEC / MICROSEC },
342 { "ms", NANOSEC / MILLISEC },
343 { "msec", NANOSEC / MILLISEC },
344 { "s", NANOSEC / SEC },
345 { "sec", NANOSEC / SEC },
346 { "m", NANOSEC * (hrtime_t)60 },
347 { "min", NANOSEC * (hrtime_t)60 },
348 { "h", NANOSEC * (hrtime_t)(60 * 60) },
349 { "hour", NANOSEC * (hrtime_t)(60 * 60) },
350 { "d", NANOSEC * (hrtime_t)(24 * 60 * 60) },
351 { "day", NANOSEC * (hrtime_t)(24 * 60 * 60) },
357 char n[PROF_NAMELEN];
360 * If no description was provided, provide all of our probes.
362 for (i = 0; i < sizeof (profile_rates) / sizeof (int); i++) {
363 if ((rate = profile_rates[i]) == 0)
366 (void) snprintf(n, PROF_NAMELEN, "%s%d",
367 PROF_PREFIX_PROFILE, rate);
368 profile_create(NANOSEC / rate, n, PROF_PROFILE);
371 for (i = 0; i < sizeof (profile_ticks) / sizeof (int); i++) {
372 if ((rate = profile_ticks[i]) == 0)
375 (void) snprintf(n, PROF_NAMELEN, "%s%d",
376 PROF_PREFIX_TICK, rate);
377 profile_create(NANOSEC / rate, n, PROF_TICK);
383 name = desc->dtpd_name;
385 for (i = 0; types[i].prefix != NULL; i++) {
386 len = strlen(types[i].prefix);
388 if (strncmp(name, types[i].prefix, len) != 0)
393 if (types[i].prefix == NULL)
396 kind = types[i].kind;
397 j = strlen(name) - len;
400 * We need to start before any time suffix.
402 for (j = strlen(name); j >= len; j--) {
403 if (name[j] >= '0' && name[j] <= '9')
408 ASSERT(suffix != NULL);
411 * Now determine the numerical value present in the probe name.
413 for (; j >= len; j--) {
414 if (name[j] < '0' || name[j] > '9')
417 val += (name[j] - '0') * mult;
418 mult *= (hrtime_t)10;
425 * Look-up the suffix to determine the multiplier.
427 for (i = 0, mult = 0; suffixes[i].name != NULL; i++) {
428 if (strcasecmp(suffixes[i].name, suffix) == 0) {
429 mult = suffixes[i].mult;
434 if (suffixes[i].name == NULL && *suffix != '\0')
439 * The default is frequency-per-second.
446 profile_create(val, name, kind);
451 profile_destroy(void *arg, dtrace_id_t id, void *parg)
453 profile_probe_t *prof = parg;
456 ASSERT(prof->prof_cyclic == CYCLIC_NONE);
458 ASSERT(!callout_active(&prof->prof_cyclic) && prof->prof_pcpus == NULL);
460 kmem_free(prof, sizeof (profile_probe_t));
462 ASSERT(profile_total >= 1);
463 atomic_add_32(&profile_total, -1);
469 profile_online(void *arg, cpu_t *cpu, cyc_handler_t *hdlr, cyc_time_t *when)
471 profile_probe_t *prof = arg;
472 profile_probe_percpu_t *pcpu;
474 pcpu = kmem_zalloc(sizeof (profile_probe_percpu_t), KM_SLEEP);
475 pcpu->profc_probe = prof;
477 hdlr->cyh_func = profile_fire;
478 hdlr->cyh_arg = pcpu;
480 when->cyt_interval = prof->prof_interval;
481 when->cyt_when = gethrtime() + when->cyt_interval;
483 pcpu->profc_expected = when->cyt_when;
484 pcpu->profc_interval = when->cyt_interval;
489 profile_offline(void *arg, cpu_t *cpu, void *oarg)
491 profile_probe_percpu_t *pcpu = oarg;
493 ASSERT(pcpu->profc_probe == arg);
494 kmem_free(pcpu, sizeof (profile_probe_percpu_t));
499 profile_enable(void *arg, dtrace_id_t id, void *parg)
501 profile_probe_t *prof = parg;
502 cyc_omni_handler_t omni;
506 ASSERT(prof->prof_interval != 0);
507 ASSERT(MUTEX_HELD(&cpu_lock));
509 if (prof->prof_kind == PROF_TICK) {
510 hdlr.cyh_func = profile_tick;
513 when.cyt_interval = prof->prof_interval;
514 when.cyt_when = gethrtime() + when.cyt_interval;
516 ASSERT(prof->prof_kind == PROF_PROFILE);
517 omni.cyo_online = profile_online;
518 omni.cyo_offline = profile_offline;
522 if (prof->prof_kind == PROF_TICK) {
523 prof->prof_cyclic = cyclic_add(&hdlr, &when);
525 prof->prof_cyclic = cyclic_add_omni(&omni);
531 profile_disable(void *arg, dtrace_id_t id, void *parg)
533 profile_probe_t *prof = parg;
535 ASSERT(prof->prof_cyclic != CYCLIC_NONE);
536 ASSERT(MUTEX_HELD(&cpu_lock));
538 cyclic_remove(prof->prof_cyclic);
539 prof->prof_cyclic = CYCLIC_NONE;
545 profile_enable_omni(profile_probe_t *prof)
547 profile_probe_percpu_t *pcpu;
550 prof->prof_pcpus = kmem_zalloc((mp_maxid + 1) * sizeof(pcpu), KM_SLEEP);
552 pcpu = kmem_zalloc(sizeof(profile_probe_percpu_t), KM_SLEEP);
553 prof->prof_pcpus[cpu] = pcpu;
554 pcpu->profc_probe = prof;
555 pcpu->profc_expected = sbinuptime() + prof->prof_interval;
556 pcpu->profc_interval = prof->prof_interval;
557 callout_init(&pcpu->profc_cyclic, 1);
558 callout_reset_sbt_on(&pcpu->profc_cyclic,
559 pcpu->profc_expected, 0, profile_fire, pcpu,
560 cpu, C_DIRECT_EXEC | C_ABSOLUTE);
565 profile_disable_omni(profile_probe_t *prof)
567 profile_probe_percpu_t *pcpu;
570 ASSERT(prof->prof_pcpus != NULL);
572 pcpu = prof->prof_pcpus[cpu];
573 ASSERT(pcpu->profc_probe == prof);
574 ASSERT(callout_active(&pcpu->profc_cyclic));
575 callout_stop(&pcpu->profc_cyclic);
576 callout_drain(&pcpu->profc_cyclic);
577 kmem_free(pcpu, sizeof(profile_probe_percpu_t));
579 kmem_free(prof->prof_pcpus, (mp_maxid + 1) * sizeof(pcpu));
580 prof->prof_pcpus = NULL;
585 profile_enable(void *arg, dtrace_id_t id, void *parg)
587 profile_probe_t *prof = parg;
589 if (prof->prof_kind == PROF_TICK) {
590 prof->prof_expected = sbinuptime() + prof->prof_interval;
591 callout_reset_sbt(&prof->prof_cyclic,
592 prof->prof_expected, 0, profile_tick, prof,
593 C_DIRECT_EXEC | C_ABSOLUTE);
595 ASSERT(prof->prof_kind == PROF_PROFILE);
596 profile_enable_omni(prof);
602 profile_disable(void *arg, dtrace_id_t id, void *parg)
604 profile_probe_t *prof = parg;
606 if (prof->prof_kind == PROF_TICK) {
607 ASSERT(callout_active(&prof->prof_cyclic));
608 callout_stop(&prof->prof_cyclic);
609 callout_drain(&prof->prof_cyclic);
611 ASSERT(prof->prof_kind == PROF_PROFILE);
612 profile_disable_omni(prof);
618 profile_load(void *dummy)
620 if (dtrace_register("profile", &profile_attr, DTRACE_PRIV_USER,
621 NULL, &profile_pops, NULL, &profile_id) != 0)
631 if ((error = dtrace_unregister(profile_id)) != 0)
639 profile_modevent(module_t mod __unused, int type, void *data __unused)
661 SYSINIT(profile_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, profile_load, NULL);
662 SYSUNINIT(profile_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, profile_unload, NULL);
664 DEV_MODULE(profile, profile_modevent, NULL);
665 MODULE_VERSION(profile, 1);
666 MODULE_DEPEND(profile, dtrace, 1, 1, 1);
667 MODULE_DEPEND(profile, opensolaris, 1, 1, 1);