191eaf3e1SJohn Birrell /*
291eaf3e1SJohn Birrell * CDDL HEADER START
391eaf3e1SJohn Birrell *
491eaf3e1SJohn Birrell * The contents of this file are subject to the terms of the
591eaf3e1SJohn Birrell * Common Development and Distribution License (the "License").
691eaf3e1SJohn Birrell * You may not use this file except in compliance with the License.
791eaf3e1SJohn Birrell *
891eaf3e1SJohn Birrell * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
991eaf3e1SJohn Birrell * or http://www.opensolaris.org/os/licensing.
1091eaf3e1SJohn Birrell * See the License for the specific language governing permissions
1191eaf3e1SJohn Birrell * and limitations under the License.
1291eaf3e1SJohn Birrell *
1391eaf3e1SJohn Birrell * When distributing Covered Code, include this CDDL HEADER in each
1491eaf3e1SJohn Birrell * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1591eaf3e1SJohn Birrell * If applicable, add the following below this CDDL HEADER, with the
1691eaf3e1SJohn Birrell * fields enclosed by brackets "[]" replaced with your own identifying
1791eaf3e1SJohn Birrell * information: Portions Copyright [yyyy] [name of copyright owner]
1891eaf3e1SJohn Birrell *
1991eaf3e1SJohn Birrell * CDDL HEADER END
2091eaf3e1SJohn Birrell *
2191eaf3e1SJohn Birrell * Portions Copyright 2006-2008 John Birrell jb@freebsd.org
2291eaf3e1SJohn Birrell *
2391eaf3e1SJohn Birrell */
2491eaf3e1SJohn Birrell
2591eaf3e1SJohn Birrell /*
2691eaf3e1SJohn Birrell * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
2791eaf3e1SJohn Birrell * Use is subject to license terms.
2891eaf3e1SJohn Birrell */
2991eaf3e1SJohn Birrell
3091eaf3e1SJohn Birrell #include <sys/param.h>
3191eaf3e1SJohn Birrell #include <sys/systm.h>
3291eaf3e1SJohn Birrell #include <sys/conf.h>
3391eaf3e1SJohn Birrell #include <sys/cpuvar.h>
349e5787d2SMatt Macy #include <sys/endian.h>
3591eaf3e1SJohn Birrell #include <sys/fcntl.h>
3691eaf3e1SJohn Birrell #include <sys/filio.h>
3791eaf3e1SJohn Birrell #include <sys/kdb.h>
3891eaf3e1SJohn Birrell #include <sys/kernel.h>
3991eaf3e1SJohn Birrell #include <sys/kmem.h>
4091eaf3e1SJohn Birrell #include <sys/kthread.h>
4191eaf3e1SJohn Birrell #include <sys/limits.h>
4291eaf3e1SJohn Birrell #include <sys/linker.h>
4391eaf3e1SJohn Birrell #include <sys/lock.h>
4491eaf3e1SJohn Birrell #include <sys/malloc.h>
4591eaf3e1SJohn Birrell #include <sys/module.h>
4691eaf3e1SJohn Birrell #include <sys/mutex.h>
4791eaf3e1SJohn Birrell #include <sys/poll.h>
4891eaf3e1SJohn Birrell #include <sys/proc.h>
4991eaf3e1SJohn Birrell #include <sys/selinfo.h>
5091eaf3e1SJohn Birrell #include <sys/smp.h>
51*e453e498SBrooks Davis #include <sys/stdarg.h>
525cde34a0SAndrew Turner #include <sys/sysctl.h>
5391eaf3e1SJohn Birrell #include <sys/uio.h>
5491eaf3e1SJohn Birrell #include <sys/unistd.h>
55036a8c5dSAndriy Gapon #include <machine/cpu.h>
5691eaf3e1SJohn Birrell
5791eaf3e1SJohn Birrell #include <sys/dtrace.h>
5891eaf3e1SJohn Birrell #include <sys/dtrace_bsd.h>
5991eaf3e1SJohn Birrell
607d35b389SMark Johnston #include <cddl/dev/dtrace/dtrace_cddl.h>
617d35b389SMark Johnston
6291eaf3e1SJohn Birrell #define PROF_NAMELEN 15
6391eaf3e1SJohn Birrell
6491eaf3e1SJohn Birrell #define PROF_PROFILE 0
6591eaf3e1SJohn Birrell #define PROF_TICK 1
6691eaf3e1SJohn Birrell #define PROF_PREFIX_PROFILE "profile-"
6791eaf3e1SJohn Birrell #define PROF_PREFIX_TICK "tick-"
6891eaf3e1SJohn Birrell
6991eaf3e1SJohn Birrell /*
7091eaf3e1SJohn Birrell * Regardless of platform, there are five artificial frames in the case of the
7191eaf3e1SJohn Birrell * profile provider:
7291eaf3e1SJohn Birrell *
7391eaf3e1SJohn Birrell * profile_fire
7491eaf3e1SJohn Birrell * cyclic_expire
7591eaf3e1SJohn Birrell * cyclic_fire
7691eaf3e1SJohn Birrell * [ cbe ]
7791eaf3e1SJohn Birrell * [ locore ]
7891eaf3e1SJohn Birrell *
7991eaf3e1SJohn Birrell * On amd64, there are two frames associated with locore: one in locore, and
8091eaf3e1SJohn Birrell * another in common interrupt dispatch code. (i386 has not been modified to
8191eaf3e1SJohn Birrell * use this common layer.) Further, on i386, the interrupted instruction
8291eaf3e1SJohn Birrell * appears as its own stack frame. All of this means that we need to add one
8391eaf3e1SJohn Birrell * frame for amd64, and then take one away for both amd64 and i386.
8491eaf3e1SJohn Birrell *
8591eaf3e1SJohn Birrell * All of the above constraints lead to the mess below. Yes, the profile
8691eaf3e1SJohn Birrell * provider should ideally figure this out on-the-fly by hiting one of its own
8791eaf3e1SJohn Birrell * probes and then walking its own stack trace. This is complicated, however,
8891eaf3e1SJohn Birrell * and the static definition doesn't seem to be overly brittle. Still, we
8991eaf3e1SJohn Birrell * allow for a manual override in case we get it completely wrong.
9091eaf3e1SJohn Birrell */
9191eaf3e1SJohn Birrell #ifdef __amd64
92036a8c5dSAndriy Gapon #define PROF_ARTIFICIAL_FRAMES 10
9391eaf3e1SJohn Birrell #else
9491eaf3e1SJohn Birrell #ifdef __i386
9591eaf3e1SJohn Birrell #define PROF_ARTIFICIAL_FRAMES 6
9691eaf3e1SJohn Birrell #endif
9791eaf3e1SJohn Birrell #endif
9891eaf3e1SJohn Birrell
99c7570492SJustin Hibbits #ifdef __powerpc__
100c7570492SJustin Hibbits /*
101c7570492SJustin Hibbits * This value is bogus just to make module compilable on powerpc
102c7570492SJustin Hibbits */
103635ecbf4SJustin Hibbits #define PROF_ARTIFICIAL_FRAMES 8
104c7570492SJustin Hibbits #endif
105c7570492SJustin Hibbits
106036a8c5dSAndriy Gapon struct profile_probe_percpu;
107036a8c5dSAndriy Gapon
108fcb56067SGeorge V. Neville-Neil #ifdef __arm__
1095cde34a0SAndrew Turner #define PROF_ARTIFICIAL_FRAMES 3
110fcb56067SGeorge V. Neville-Neil #endif
111fcb56067SGeorge V. Neville-Neil
112b78ee15eSRuslan Bukin #ifdef __aarch64__
113599fb1d1SRobert Watson #define PROF_ARTIFICIAL_FRAMES 12
114b78ee15eSRuslan Bukin #endif
115b78ee15eSRuslan Bukin
116ca20f8ecSRuslan Bukin #ifdef __riscv
11740fdda02SMitchell Horne #define PROF_ARTIFICIAL_FRAMES 12
118fed1ca4bSRuslan Bukin #endif
119fed1ca4bSRuslan Bukin
12091eaf3e1SJohn Birrell typedef struct profile_probe {
12191eaf3e1SJohn Birrell dtrace_id_t prof_id;
12291eaf3e1SJohn Birrell int prof_kind;
123036a8c5dSAndriy Gapon #ifdef illumos
12491eaf3e1SJohn Birrell hrtime_t prof_interval;
12591eaf3e1SJohn Birrell cyclic_id_t prof_cyclic;
126036a8c5dSAndriy Gapon #else
127036a8c5dSAndriy Gapon sbintime_t prof_interval;
128036a8c5dSAndriy Gapon struct callout prof_cyclic;
129036a8c5dSAndriy Gapon sbintime_t prof_expected;
130036a8c5dSAndriy Gapon struct profile_probe_percpu **prof_pcpus;
131036a8c5dSAndriy Gapon #endif
13291eaf3e1SJohn Birrell } profile_probe_t;
13391eaf3e1SJohn Birrell
13491eaf3e1SJohn Birrell typedef struct profile_probe_percpu {
13591eaf3e1SJohn Birrell hrtime_t profc_expected;
13691eaf3e1SJohn Birrell hrtime_t profc_interval;
13791eaf3e1SJohn Birrell profile_probe_t *profc_probe;
138036a8c5dSAndriy Gapon #ifdef __FreeBSD__
139036a8c5dSAndriy Gapon struct callout profc_cyclic;
140036a8c5dSAndriy Gapon #endif
14191eaf3e1SJohn Birrell } profile_probe_percpu_t;
14291eaf3e1SJohn Birrell
14391eaf3e1SJohn Birrell static int profile_unload(void);
14491eaf3e1SJohn Birrell static void profile_create(hrtime_t, char *, int);
14591eaf3e1SJohn Birrell static void profile_destroy(void *, dtrace_id_t, void *);
14691eaf3e1SJohn Birrell static void profile_enable(void *, dtrace_id_t, void *);
14791eaf3e1SJohn Birrell static void profile_disable(void *, dtrace_id_t, void *);
14891eaf3e1SJohn Birrell static void profile_load(void *);
14991eaf3e1SJohn Birrell static void profile_provide(void *, dtrace_probedesc_t *);
15091eaf3e1SJohn Birrell
15191eaf3e1SJohn Birrell static int profile_rates[] = {
15291eaf3e1SJohn Birrell 97, 199, 499, 997, 1999,
15391eaf3e1SJohn Birrell 4001, 4999, 0, 0, 0,
15491eaf3e1SJohn Birrell 0, 0, 0, 0, 0,
15591eaf3e1SJohn Birrell 0, 0, 0, 0, 0
15691eaf3e1SJohn Birrell };
15791eaf3e1SJohn Birrell
15891eaf3e1SJohn Birrell static int profile_ticks[] = {
15991eaf3e1SJohn Birrell 1, 10, 100, 500, 1000,
16091eaf3e1SJohn Birrell 5000, 0, 0, 0, 0,
16191eaf3e1SJohn Birrell 0, 0, 0, 0, 0
16291eaf3e1SJohn Birrell };
16391eaf3e1SJohn Birrell
16491eaf3e1SJohn Birrell /*
16591eaf3e1SJohn Birrell * profile_max defines the upper bound on the number of profile probes that
16691eaf3e1SJohn Birrell * can exist (this is to prevent malicious or clumsy users from exhausing
16791eaf3e1SJohn Birrell * system resources by creating a slew of profile probes). At mod load time,
16891eaf3e1SJohn Birrell * this gets its value from PROFILE_MAX_DEFAULT or profile-max-probes if it's
16991eaf3e1SJohn Birrell * present in the profile.conf file.
17091eaf3e1SJohn Birrell */
17191eaf3e1SJohn Birrell #define PROFILE_MAX_DEFAULT 1000 /* default max. number of probes */
17291eaf3e1SJohn Birrell static uint32_t profile_max = PROFILE_MAX_DEFAULT;
17391eaf3e1SJohn Birrell /* maximum number of profile probes */
17491eaf3e1SJohn Birrell static uint32_t profile_total; /* current number of profile probes */
17591eaf3e1SJohn Birrell
17691eaf3e1SJohn Birrell static dtrace_pattr_t profile_attr = {
17791eaf3e1SJohn Birrell { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
17891eaf3e1SJohn Birrell { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
17991eaf3e1SJohn Birrell { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
18091eaf3e1SJohn Birrell { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
18191eaf3e1SJohn Birrell { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
18291eaf3e1SJohn Birrell };
18391eaf3e1SJohn Birrell
18491eaf3e1SJohn Birrell static dtrace_pops_t profile_pops = {
18547f11baaSMark Johnston .dtps_provide = profile_provide,
18647f11baaSMark Johnston .dtps_provide_module = NULL,
18747f11baaSMark Johnston .dtps_enable = profile_enable,
18847f11baaSMark Johnston .dtps_disable = profile_disable,
18947f11baaSMark Johnston .dtps_suspend = NULL,
19047f11baaSMark Johnston .dtps_resume = NULL,
19147f11baaSMark Johnston .dtps_getargdesc = NULL,
19247f11baaSMark Johnston .dtps_getargval = NULL,
19347f11baaSMark Johnston .dtps_usermode = NULL,
19447f11baaSMark Johnston .dtps_destroy = profile_destroy
19591eaf3e1SJohn Birrell };
19691eaf3e1SJohn Birrell
19791eaf3e1SJohn Birrell static dtrace_provider_id_t profile_id;
19891eaf3e1SJohn Birrell static hrtime_t profile_interval_min = NANOSEC / 5000; /* 5000 hz */
1995cde34a0SAndrew Turner static int profile_aframes = PROF_ARTIFICIAL_FRAMES;
2005cde34a0SAndrew Turner
2015cde34a0SAndrew Turner SYSCTL_DECL(_kern_dtrace);
2027029da5cSPawel Biernacki SYSCTL_NODE(_kern_dtrace, OID_AUTO, profile, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
2037029da5cSPawel Biernacki "DTrace profile parameters");
2045cde34a0SAndrew Turner SYSCTL_INT(_kern_dtrace_profile, OID_AUTO, aframes, CTLFLAG_RW, &profile_aframes,
2055cde34a0SAndrew Turner 0, "Skipped frames for profile provider");
20691eaf3e1SJohn Birrell
207036a8c5dSAndriy Gapon static sbintime_t
nsec_to_sbt(hrtime_t nsec)208036a8c5dSAndriy Gapon nsec_to_sbt(hrtime_t nsec)
209036a8c5dSAndriy Gapon {
210036a8c5dSAndriy Gapon time_t sec;
211036a8c5dSAndriy Gapon
212036a8c5dSAndriy Gapon /*
213036a8c5dSAndriy Gapon * We need to calculate nsec * 2^32 / 10^9
214036a8c5dSAndriy Gapon * Seconds and nanoseconds are split to avoid overflow.
215036a8c5dSAndriy Gapon */
216036a8c5dSAndriy Gapon sec = nsec / NANOSEC;
217036a8c5dSAndriy Gapon nsec = nsec % NANOSEC;
218036a8c5dSAndriy Gapon return (((sbintime_t)sec << 32) | ((sbintime_t)nsec << 32) / NANOSEC);
219036a8c5dSAndriy Gapon }
220036a8c5dSAndriy Gapon
221036a8c5dSAndriy Gapon static hrtime_t
sbt_to_nsec(sbintime_t sbt)222036a8c5dSAndriy Gapon sbt_to_nsec(sbintime_t sbt)
223036a8c5dSAndriy Gapon {
224036a8c5dSAndriy Gapon
225036a8c5dSAndriy Gapon return ((sbt >> 32) * NANOSEC +
226036a8c5dSAndriy Gapon (((uint32_t)sbt * (hrtime_t)NANOSEC) >> 32));
227036a8c5dSAndriy Gapon }
228036a8c5dSAndriy Gapon
22991eaf3e1SJohn Birrell static void
profile_probe(profile_probe_t * prof,hrtime_t late)230de3a96e3SMark Johnston profile_probe(profile_probe_t *prof, hrtime_t late)
23191eaf3e1SJohn Birrell {
232de3a96e3SMark Johnston struct thread *td;
233036a8c5dSAndriy Gapon struct trapframe *frame;
234036a8c5dSAndriy Gapon uintfptr_t pc, upc;
23591eaf3e1SJohn Birrell
236de3a96e3SMark Johnston td = curthread;
237de3a96e3SMark Johnston pc = upc = 0;
238036a8c5dSAndriy Gapon
239036a8c5dSAndriy Gapon /*
240de3a96e3SMark Johnston * td_intr_frame can be unset if this is a catch-up event upon waking up
241de3a96e3SMark Johnston * from idle sleep. This can only happen on a CPU idle thread. Use a
242de3a96e3SMark Johnston * representative arg0 value in this case so that one of the probe
243de3a96e3SMark Johnston * arguments is non-zero.
244036a8c5dSAndriy Gapon */
245de3a96e3SMark Johnston frame = td->td_intr_frame;
246036a8c5dSAndriy Gapon if (frame != NULL) {
247036a8c5dSAndriy Gapon if (TRAPF_USERMODE(frame))
248036a8c5dSAndriy Gapon upc = TRAPF_PC(frame);
2497d35b389SMark Johnston else {
250036a8c5dSAndriy Gapon pc = TRAPF_PC(frame);
2517d35b389SMark Johnston td->t_dtrace_trapframe = frame;
2527d35b389SMark Johnston }
253de3a96e3SMark Johnston } else if (TD_IS_IDLETHREAD(td))
254de3a96e3SMark Johnston pc = (uintfptr_t)&cpu_idle;
255036a8c5dSAndriy Gapon
256de3a96e3SMark Johnston dtrace_probe(prof->prof_id, pc, upc, late, 0, 0);
2577d35b389SMark Johnston td->t_dtrace_trapframe = NULL;
258de3a96e3SMark Johnston }
259de3a96e3SMark Johnston
260de3a96e3SMark Johnston static void
profile_fire(void * arg)261de3a96e3SMark Johnston profile_fire(void *arg)
262de3a96e3SMark Johnston {
263de3a96e3SMark Johnston profile_probe_percpu_t *pcpu = arg;
264de3a96e3SMark Johnston profile_probe_t *prof = pcpu->profc_probe;
265de3a96e3SMark Johnston hrtime_t late;
266de3a96e3SMark Johnston
267de3a96e3SMark Johnston late = sbt_to_nsec(sbinuptime() - pcpu->profc_expected);
268de3a96e3SMark Johnston
269de3a96e3SMark Johnston profile_probe(prof, late);
270036a8c5dSAndriy Gapon pcpu->profc_expected += pcpu->profc_interval;
271036a8c5dSAndriy Gapon callout_schedule_sbt_curcpu(&pcpu->profc_cyclic,
272036a8c5dSAndriy Gapon pcpu->profc_expected, 0, C_DIRECT_EXEC | C_ABSOLUTE);
27391eaf3e1SJohn Birrell }
27491eaf3e1SJohn Birrell
27591eaf3e1SJohn Birrell static void
profile_tick(void * arg)27691eaf3e1SJohn Birrell profile_tick(void *arg)
27791eaf3e1SJohn Birrell {
27891eaf3e1SJohn Birrell profile_probe_t *prof = arg;
27991eaf3e1SJohn Birrell
280de3a96e3SMark Johnston profile_probe(prof, 0);
281036a8c5dSAndriy Gapon prof->prof_expected += prof->prof_interval;
282036a8c5dSAndriy Gapon callout_schedule_sbt(&prof->prof_cyclic,
283036a8c5dSAndriy Gapon prof->prof_expected, 0, C_DIRECT_EXEC | C_ABSOLUTE);
28491eaf3e1SJohn Birrell }
28591eaf3e1SJohn Birrell
28691eaf3e1SJohn Birrell static void
profile_create(hrtime_t interval,char * name,int kind)28791eaf3e1SJohn Birrell profile_create(hrtime_t interval, char *name, int kind)
28891eaf3e1SJohn Birrell {
28991eaf3e1SJohn Birrell profile_probe_t *prof;
29091eaf3e1SJohn Birrell
29191eaf3e1SJohn Birrell if (interval < profile_interval_min)
29291eaf3e1SJohn Birrell return;
29391eaf3e1SJohn Birrell
29491eaf3e1SJohn Birrell if (dtrace_probe_lookup(profile_id, NULL, NULL, name) != 0)
29591eaf3e1SJohn Birrell return;
29691eaf3e1SJohn Birrell
29791eaf3e1SJohn Birrell atomic_add_32(&profile_total, 1);
29891eaf3e1SJohn Birrell if (profile_total > profile_max) {
29991eaf3e1SJohn Birrell atomic_add_32(&profile_total, -1);
30091eaf3e1SJohn Birrell return;
30191eaf3e1SJohn Birrell }
30291eaf3e1SJohn Birrell
30391eaf3e1SJohn Birrell prof = kmem_zalloc(sizeof (profile_probe_t), KM_SLEEP);
304036a8c5dSAndriy Gapon #ifdef illumos
30591eaf3e1SJohn Birrell prof->prof_interval = interval;
30691eaf3e1SJohn Birrell prof->prof_cyclic = CYCLIC_NONE;
307036a8c5dSAndriy Gapon #else
308036a8c5dSAndriy Gapon prof->prof_interval = nsec_to_sbt(interval);
309fd90e2edSJung-uk Kim callout_init(&prof->prof_cyclic, 1);
310036a8c5dSAndriy Gapon #endif
31191eaf3e1SJohn Birrell prof->prof_kind = kind;
31291eaf3e1SJohn Birrell prof->prof_id = dtrace_probe_create(profile_id,
31391eaf3e1SJohn Birrell NULL, NULL, name,
3145cde34a0SAndrew Turner profile_aframes, prof);
31591eaf3e1SJohn Birrell }
31691eaf3e1SJohn Birrell
31791eaf3e1SJohn Birrell /*ARGSUSED*/
31891eaf3e1SJohn Birrell static void
profile_provide(void * arg,dtrace_probedesc_t * desc)31991eaf3e1SJohn Birrell profile_provide(void *arg, dtrace_probedesc_t *desc)
32091eaf3e1SJohn Birrell {
32191eaf3e1SJohn Birrell int i, j, rate, kind;
32291eaf3e1SJohn Birrell hrtime_t val = 0, mult = 1, len = 0;
32391eaf3e1SJohn Birrell char *name, *suffix = NULL;
32491eaf3e1SJohn Birrell
32591eaf3e1SJohn Birrell const struct {
32691eaf3e1SJohn Birrell char *prefix;
32791eaf3e1SJohn Birrell int kind;
32891eaf3e1SJohn Birrell } types[] = {
32991eaf3e1SJohn Birrell { PROF_PREFIX_PROFILE, PROF_PROFILE },
33091eaf3e1SJohn Birrell { PROF_PREFIX_TICK, PROF_TICK },
33191eaf3e1SJohn Birrell { 0, 0 }
33291eaf3e1SJohn Birrell };
33391eaf3e1SJohn Birrell
33491eaf3e1SJohn Birrell const struct {
33591eaf3e1SJohn Birrell char *name;
33691eaf3e1SJohn Birrell hrtime_t mult;
33791eaf3e1SJohn Birrell } suffixes[] = {
33891eaf3e1SJohn Birrell { "ns", NANOSEC / NANOSEC },
33991eaf3e1SJohn Birrell { "nsec", NANOSEC / NANOSEC },
34091eaf3e1SJohn Birrell { "us", NANOSEC / MICROSEC },
34191eaf3e1SJohn Birrell { "usec", NANOSEC / MICROSEC },
34291eaf3e1SJohn Birrell { "ms", NANOSEC / MILLISEC },
34391eaf3e1SJohn Birrell { "msec", NANOSEC / MILLISEC },
34491eaf3e1SJohn Birrell { "s", NANOSEC / SEC },
34591eaf3e1SJohn Birrell { "sec", NANOSEC / SEC },
34691eaf3e1SJohn Birrell { "m", NANOSEC * (hrtime_t)60 },
34791eaf3e1SJohn Birrell { "min", NANOSEC * (hrtime_t)60 },
34891eaf3e1SJohn Birrell { "h", NANOSEC * (hrtime_t)(60 * 60) },
34991eaf3e1SJohn Birrell { "hour", NANOSEC * (hrtime_t)(60 * 60) },
35091eaf3e1SJohn Birrell { "d", NANOSEC * (hrtime_t)(24 * 60 * 60) },
35191eaf3e1SJohn Birrell { "day", NANOSEC * (hrtime_t)(24 * 60 * 60) },
35291eaf3e1SJohn Birrell { "hz", 0 },
35391eaf3e1SJohn Birrell { NULL }
35491eaf3e1SJohn Birrell };
35591eaf3e1SJohn Birrell
35691eaf3e1SJohn Birrell if (desc == NULL) {
35791eaf3e1SJohn Birrell char n[PROF_NAMELEN];
35891eaf3e1SJohn Birrell
35991eaf3e1SJohn Birrell /*
36091eaf3e1SJohn Birrell * If no description was provided, provide all of our probes.
36191eaf3e1SJohn Birrell */
36291eaf3e1SJohn Birrell for (i = 0; i < sizeof (profile_rates) / sizeof (int); i++) {
36391eaf3e1SJohn Birrell if ((rate = profile_rates[i]) == 0)
36491eaf3e1SJohn Birrell continue;
36591eaf3e1SJohn Birrell
36691eaf3e1SJohn Birrell (void) snprintf(n, PROF_NAMELEN, "%s%d",
36791eaf3e1SJohn Birrell PROF_PREFIX_PROFILE, rate);
36891eaf3e1SJohn Birrell profile_create(NANOSEC / rate, n, PROF_PROFILE);
36991eaf3e1SJohn Birrell }
37091eaf3e1SJohn Birrell
37191eaf3e1SJohn Birrell for (i = 0; i < sizeof (profile_ticks) / sizeof (int); i++) {
37291eaf3e1SJohn Birrell if ((rate = profile_ticks[i]) == 0)
37391eaf3e1SJohn Birrell continue;
37491eaf3e1SJohn Birrell
37591eaf3e1SJohn Birrell (void) snprintf(n, PROF_NAMELEN, "%s%d",
37691eaf3e1SJohn Birrell PROF_PREFIX_TICK, rate);
37791eaf3e1SJohn Birrell profile_create(NANOSEC / rate, n, PROF_TICK);
37891eaf3e1SJohn Birrell }
37991eaf3e1SJohn Birrell
38091eaf3e1SJohn Birrell return;
38191eaf3e1SJohn Birrell }
38291eaf3e1SJohn Birrell
38391eaf3e1SJohn Birrell name = desc->dtpd_name;
38491eaf3e1SJohn Birrell
38591eaf3e1SJohn Birrell for (i = 0; types[i].prefix != NULL; i++) {
38691eaf3e1SJohn Birrell len = strlen(types[i].prefix);
38791eaf3e1SJohn Birrell
38891eaf3e1SJohn Birrell if (strncmp(name, types[i].prefix, len) != 0)
38991eaf3e1SJohn Birrell continue;
39091eaf3e1SJohn Birrell break;
39191eaf3e1SJohn Birrell }
39291eaf3e1SJohn Birrell
39391eaf3e1SJohn Birrell if (types[i].prefix == NULL)
39491eaf3e1SJohn Birrell return;
39591eaf3e1SJohn Birrell
39691eaf3e1SJohn Birrell kind = types[i].kind;
39791eaf3e1SJohn Birrell j = strlen(name) - len;
39891eaf3e1SJohn Birrell
39991eaf3e1SJohn Birrell /*
40091eaf3e1SJohn Birrell * We need to start before any time suffix.
40191eaf3e1SJohn Birrell */
40291eaf3e1SJohn Birrell for (j = strlen(name); j >= len; j--) {
40391eaf3e1SJohn Birrell if (name[j] >= '0' && name[j] <= '9')
40491eaf3e1SJohn Birrell break;
40591eaf3e1SJohn Birrell suffix = &name[j];
40691eaf3e1SJohn Birrell }
40791eaf3e1SJohn Birrell
40891eaf3e1SJohn Birrell ASSERT(suffix != NULL);
40991eaf3e1SJohn Birrell
41091eaf3e1SJohn Birrell /*
41191eaf3e1SJohn Birrell * Now determine the numerical value present in the probe name.
41291eaf3e1SJohn Birrell */
41391eaf3e1SJohn Birrell for (; j >= len; j--) {
41491eaf3e1SJohn Birrell if (name[j] < '0' || name[j] > '9')
41591eaf3e1SJohn Birrell return;
41691eaf3e1SJohn Birrell
41791eaf3e1SJohn Birrell val += (name[j] - '0') * mult;
41891eaf3e1SJohn Birrell mult *= (hrtime_t)10;
41991eaf3e1SJohn Birrell }
42091eaf3e1SJohn Birrell
42191eaf3e1SJohn Birrell if (val == 0)
42291eaf3e1SJohn Birrell return;
42391eaf3e1SJohn Birrell
42491eaf3e1SJohn Birrell /*
42591eaf3e1SJohn Birrell * Look-up the suffix to determine the multiplier.
42691eaf3e1SJohn Birrell */
42791eaf3e1SJohn Birrell for (i = 0, mult = 0; suffixes[i].name != NULL; i++) {
42891eaf3e1SJohn Birrell if (strcasecmp(suffixes[i].name, suffix) == 0) {
42991eaf3e1SJohn Birrell mult = suffixes[i].mult;
43091eaf3e1SJohn Birrell break;
43191eaf3e1SJohn Birrell }
43291eaf3e1SJohn Birrell }
43391eaf3e1SJohn Birrell
43491eaf3e1SJohn Birrell if (suffixes[i].name == NULL && *suffix != '\0')
43591eaf3e1SJohn Birrell return;
43691eaf3e1SJohn Birrell
43791eaf3e1SJohn Birrell if (mult == 0) {
43891eaf3e1SJohn Birrell /*
43991eaf3e1SJohn Birrell * The default is frequency-per-second.
44091eaf3e1SJohn Birrell */
44191eaf3e1SJohn Birrell val = NANOSEC / val;
44291eaf3e1SJohn Birrell } else {
44391eaf3e1SJohn Birrell val *= mult;
44491eaf3e1SJohn Birrell }
44591eaf3e1SJohn Birrell
44691eaf3e1SJohn Birrell profile_create(val, name, kind);
44791eaf3e1SJohn Birrell }
44891eaf3e1SJohn Birrell
44991eaf3e1SJohn Birrell /* ARGSUSED */
45091eaf3e1SJohn Birrell static void
profile_destroy(void * arg,dtrace_id_t id,void * parg)45191eaf3e1SJohn Birrell profile_destroy(void *arg, dtrace_id_t id, void *parg)
45291eaf3e1SJohn Birrell {
45391eaf3e1SJohn Birrell profile_probe_t *prof = parg;
45491eaf3e1SJohn Birrell
455036a8c5dSAndriy Gapon #ifdef illumos
45691eaf3e1SJohn Birrell ASSERT(prof->prof_cyclic == CYCLIC_NONE);
457036a8c5dSAndriy Gapon #else
458036a8c5dSAndriy Gapon ASSERT(!callout_active(&prof->prof_cyclic) && prof->prof_pcpus == NULL);
459036a8c5dSAndriy Gapon #endif
46091eaf3e1SJohn Birrell kmem_free(prof, sizeof (profile_probe_t));
46191eaf3e1SJohn Birrell
46291eaf3e1SJohn Birrell ASSERT(profile_total >= 1);
46391eaf3e1SJohn Birrell atomic_add_32(&profile_total, -1);
46491eaf3e1SJohn Birrell }
46591eaf3e1SJohn Birrell
466036a8c5dSAndriy Gapon #ifdef illumos
46791eaf3e1SJohn Birrell /*ARGSUSED*/
46891eaf3e1SJohn Birrell static void
profile_online(void * arg,cpu_t * cpu,cyc_handler_t * hdlr,cyc_time_t * when)46991eaf3e1SJohn Birrell profile_online(void *arg, cpu_t *cpu, cyc_handler_t *hdlr, cyc_time_t *when)
47091eaf3e1SJohn Birrell {
47191eaf3e1SJohn Birrell profile_probe_t *prof = arg;
47291eaf3e1SJohn Birrell profile_probe_percpu_t *pcpu;
47391eaf3e1SJohn Birrell
47491eaf3e1SJohn Birrell pcpu = kmem_zalloc(sizeof (profile_probe_percpu_t), KM_SLEEP);
47591eaf3e1SJohn Birrell pcpu->profc_probe = prof;
47691eaf3e1SJohn Birrell
47791eaf3e1SJohn Birrell hdlr->cyh_func = profile_fire;
47891eaf3e1SJohn Birrell hdlr->cyh_arg = pcpu;
47991eaf3e1SJohn Birrell
48091eaf3e1SJohn Birrell when->cyt_interval = prof->prof_interval;
48191eaf3e1SJohn Birrell when->cyt_when = gethrtime() + when->cyt_interval;
48291eaf3e1SJohn Birrell
48391eaf3e1SJohn Birrell pcpu->profc_expected = when->cyt_when;
48491eaf3e1SJohn Birrell pcpu->profc_interval = when->cyt_interval;
48591eaf3e1SJohn Birrell }
48691eaf3e1SJohn Birrell
48791eaf3e1SJohn Birrell /*ARGSUSED*/
48891eaf3e1SJohn Birrell static void
profile_offline(void * arg,cpu_t * cpu,void * oarg)48991eaf3e1SJohn Birrell profile_offline(void *arg, cpu_t *cpu, void *oarg)
49091eaf3e1SJohn Birrell {
49191eaf3e1SJohn Birrell profile_probe_percpu_t *pcpu = oarg;
49291eaf3e1SJohn Birrell
49391eaf3e1SJohn Birrell ASSERT(pcpu->profc_probe == arg);
49491eaf3e1SJohn Birrell kmem_free(pcpu, sizeof (profile_probe_percpu_t));
49591eaf3e1SJohn Birrell }
49691eaf3e1SJohn Birrell
49791eaf3e1SJohn Birrell /* ARGSUSED */
49891eaf3e1SJohn Birrell static void
profile_enable(void * arg,dtrace_id_t id,void * parg)49991eaf3e1SJohn Birrell profile_enable(void *arg, dtrace_id_t id, void *parg)
50091eaf3e1SJohn Birrell {
50191eaf3e1SJohn Birrell profile_probe_t *prof = parg;
50291eaf3e1SJohn Birrell cyc_omni_handler_t omni;
50391eaf3e1SJohn Birrell cyc_handler_t hdlr;
50491eaf3e1SJohn Birrell cyc_time_t when;
50591eaf3e1SJohn Birrell
50691eaf3e1SJohn Birrell ASSERT(prof->prof_interval != 0);
50791eaf3e1SJohn Birrell ASSERT(MUTEX_HELD(&cpu_lock));
50891eaf3e1SJohn Birrell
50991eaf3e1SJohn Birrell if (prof->prof_kind == PROF_TICK) {
51091eaf3e1SJohn Birrell hdlr.cyh_func = profile_tick;
51191eaf3e1SJohn Birrell hdlr.cyh_arg = prof;
51291eaf3e1SJohn Birrell
51391eaf3e1SJohn Birrell when.cyt_interval = prof->prof_interval;
51491eaf3e1SJohn Birrell when.cyt_when = gethrtime() + when.cyt_interval;
51591eaf3e1SJohn Birrell } else {
51691eaf3e1SJohn Birrell ASSERT(prof->prof_kind == PROF_PROFILE);
51791eaf3e1SJohn Birrell omni.cyo_online = profile_online;
51891eaf3e1SJohn Birrell omni.cyo_offline = profile_offline;
51991eaf3e1SJohn Birrell omni.cyo_arg = prof;
52091eaf3e1SJohn Birrell }
52191eaf3e1SJohn Birrell
52291eaf3e1SJohn Birrell if (prof->prof_kind == PROF_TICK) {
52391eaf3e1SJohn Birrell prof->prof_cyclic = cyclic_add(&hdlr, &when);
52491eaf3e1SJohn Birrell } else {
52591eaf3e1SJohn Birrell prof->prof_cyclic = cyclic_add_omni(&omni);
52691eaf3e1SJohn Birrell }
52791eaf3e1SJohn Birrell }
52891eaf3e1SJohn Birrell
52991eaf3e1SJohn Birrell /* ARGSUSED */
53091eaf3e1SJohn Birrell static void
profile_disable(void * arg,dtrace_id_t id,void * parg)53191eaf3e1SJohn Birrell profile_disable(void *arg, dtrace_id_t id, void *parg)
53291eaf3e1SJohn Birrell {
53391eaf3e1SJohn Birrell profile_probe_t *prof = parg;
53491eaf3e1SJohn Birrell
53591eaf3e1SJohn Birrell ASSERT(prof->prof_cyclic != CYCLIC_NONE);
53691eaf3e1SJohn Birrell ASSERT(MUTEX_HELD(&cpu_lock));
53791eaf3e1SJohn Birrell
53891eaf3e1SJohn Birrell cyclic_remove(prof->prof_cyclic);
53991eaf3e1SJohn Birrell prof->prof_cyclic = CYCLIC_NONE;
54091eaf3e1SJohn Birrell }
54191eaf3e1SJohn Birrell
542036a8c5dSAndriy Gapon #else
543036a8c5dSAndriy Gapon
544036a8c5dSAndriy Gapon static void
profile_enable_omni(profile_probe_t * prof)545036a8c5dSAndriy Gapon profile_enable_omni(profile_probe_t *prof)
546036a8c5dSAndriy Gapon {
547036a8c5dSAndriy Gapon profile_probe_percpu_t *pcpu;
548036a8c5dSAndriy Gapon int cpu;
549036a8c5dSAndriy Gapon
550036a8c5dSAndriy Gapon prof->prof_pcpus = kmem_zalloc((mp_maxid + 1) * sizeof(pcpu), KM_SLEEP);
551036a8c5dSAndriy Gapon CPU_FOREACH(cpu) {
552036a8c5dSAndriy Gapon pcpu = kmem_zalloc(sizeof(profile_probe_percpu_t), KM_SLEEP);
553036a8c5dSAndriy Gapon prof->prof_pcpus[cpu] = pcpu;
554036a8c5dSAndriy Gapon pcpu->profc_probe = prof;
555036a8c5dSAndriy Gapon pcpu->profc_expected = sbinuptime() + prof->prof_interval;
556036a8c5dSAndriy Gapon pcpu->profc_interval = prof->prof_interval;
557fd90e2edSJung-uk Kim callout_init(&pcpu->profc_cyclic, 1);
558036a8c5dSAndriy Gapon callout_reset_sbt_on(&pcpu->profc_cyclic,
559036a8c5dSAndriy Gapon pcpu->profc_expected, 0, profile_fire, pcpu,
560036a8c5dSAndriy Gapon cpu, C_DIRECT_EXEC | C_ABSOLUTE);
561036a8c5dSAndriy Gapon }
562036a8c5dSAndriy Gapon }
563036a8c5dSAndriy Gapon
564036a8c5dSAndriy Gapon static void
profile_disable_omni(profile_probe_t * prof)565036a8c5dSAndriy Gapon profile_disable_omni(profile_probe_t *prof)
566036a8c5dSAndriy Gapon {
567036a8c5dSAndriy Gapon profile_probe_percpu_t *pcpu;
568036a8c5dSAndriy Gapon int cpu;
569036a8c5dSAndriy Gapon
570036a8c5dSAndriy Gapon ASSERT(prof->prof_pcpus != NULL);
571036a8c5dSAndriy Gapon CPU_FOREACH(cpu) {
572036a8c5dSAndriy Gapon pcpu = prof->prof_pcpus[cpu];
573036a8c5dSAndriy Gapon ASSERT(pcpu->profc_probe == prof);
574036a8c5dSAndriy Gapon ASSERT(callout_active(&pcpu->profc_cyclic));
575036a8c5dSAndriy Gapon callout_stop(&pcpu->profc_cyclic);
576036a8c5dSAndriy Gapon callout_drain(&pcpu->profc_cyclic);
577036a8c5dSAndriy Gapon kmem_free(pcpu, sizeof(profile_probe_percpu_t));
578036a8c5dSAndriy Gapon }
579036a8c5dSAndriy Gapon kmem_free(prof->prof_pcpus, (mp_maxid + 1) * sizeof(pcpu));
580036a8c5dSAndriy Gapon prof->prof_pcpus = NULL;
581036a8c5dSAndriy Gapon }
582036a8c5dSAndriy Gapon
583036a8c5dSAndriy Gapon /* ARGSUSED */
584036a8c5dSAndriy Gapon static void
profile_enable(void * arg,dtrace_id_t id,void * parg)585036a8c5dSAndriy Gapon profile_enable(void *arg, dtrace_id_t id, void *parg)
586036a8c5dSAndriy Gapon {
587036a8c5dSAndriy Gapon profile_probe_t *prof = parg;
588036a8c5dSAndriy Gapon
589036a8c5dSAndriy Gapon if (prof->prof_kind == PROF_TICK) {
590036a8c5dSAndriy Gapon prof->prof_expected = sbinuptime() + prof->prof_interval;
591036a8c5dSAndriy Gapon callout_reset_sbt(&prof->prof_cyclic,
592036a8c5dSAndriy Gapon prof->prof_expected, 0, profile_tick, prof,
593036a8c5dSAndriy Gapon C_DIRECT_EXEC | C_ABSOLUTE);
594036a8c5dSAndriy Gapon } else {
595036a8c5dSAndriy Gapon ASSERT(prof->prof_kind == PROF_PROFILE);
596036a8c5dSAndriy Gapon profile_enable_omni(prof);
597036a8c5dSAndriy Gapon }
598036a8c5dSAndriy Gapon }
599036a8c5dSAndriy Gapon
600036a8c5dSAndriy Gapon /* ARGSUSED */
601036a8c5dSAndriy Gapon static void
profile_disable(void * arg,dtrace_id_t id,void * parg)602036a8c5dSAndriy Gapon profile_disable(void *arg, dtrace_id_t id, void *parg)
603036a8c5dSAndriy Gapon {
604036a8c5dSAndriy Gapon profile_probe_t *prof = parg;
605036a8c5dSAndriy Gapon
606036a8c5dSAndriy Gapon if (prof->prof_kind == PROF_TICK) {
607036a8c5dSAndriy Gapon ASSERT(callout_active(&prof->prof_cyclic));
608036a8c5dSAndriy Gapon callout_stop(&prof->prof_cyclic);
609036a8c5dSAndriy Gapon callout_drain(&prof->prof_cyclic);
610036a8c5dSAndriy Gapon } else {
611036a8c5dSAndriy Gapon ASSERT(prof->prof_kind == PROF_PROFILE);
612036a8c5dSAndriy Gapon profile_disable_omni(prof);
613036a8c5dSAndriy Gapon }
614036a8c5dSAndriy Gapon }
615036a8c5dSAndriy Gapon #endif
616036a8c5dSAndriy Gapon
61791eaf3e1SJohn Birrell static void
profile_load(void * dummy)61891eaf3e1SJohn Birrell profile_load(void *dummy)
61991eaf3e1SJohn Birrell {
62091eaf3e1SJohn Birrell if (dtrace_register("profile", &profile_attr, DTRACE_PRIV_USER,
62191eaf3e1SJohn Birrell NULL, &profile_pops, NULL, &profile_id) != 0)
62291eaf3e1SJohn Birrell return;
62391eaf3e1SJohn Birrell }
62491eaf3e1SJohn Birrell
62591eaf3e1SJohn Birrell
62691eaf3e1SJohn Birrell static int
profile_unload(void)6272d03b58fSDimitry Andric profile_unload(void)
62891eaf3e1SJohn Birrell {
62991eaf3e1SJohn Birrell int error = 0;
63091eaf3e1SJohn Birrell
63191eaf3e1SJohn Birrell if ((error = dtrace_unregister(profile_id)) != 0)
63291eaf3e1SJohn Birrell return (error);
63391eaf3e1SJohn Birrell
63491eaf3e1SJohn Birrell return (error);
63591eaf3e1SJohn Birrell }
63691eaf3e1SJohn Birrell
63791eaf3e1SJohn Birrell /* ARGSUSED */
63891eaf3e1SJohn Birrell static int
profile_modevent(module_t mod __unused,int type,void * data __unused)63991eaf3e1SJohn Birrell profile_modevent(module_t mod __unused, int type, void *data __unused)
64091eaf3e1SJohn Birrell {
64191eaf3e1SJohn Birrell int error = 0;
64291eaf3e1SJohn Birrell
64391eaf3e1SJohn Birrell switch (type) {
64491eaf3e1SJohn Birrell case MOD_LOAD:
64591eaf3e1SJohn Birrell break;
64691eaf3e1SJohn Birrell
64791eaf3e1SJohn Birrell case MOD_UNLOAD:
64891eaf3e1SJohn Birrell break;
64991eaf3e1SJohn Birrell
65091eaf3e1SJohn Birrell case MOD_SHUTDOWN:
65191eaf3e1SJohn Birrell break;
65291eaf3e1SJohn Birrell
65391eaf3e1SJohn Birrell default:
65491eaf3e1SJohn Birrell error = EOPNOTSUPP;
65591eaf3e1SJohn Birrell break;
65691eaf3e1SJohn Birrell
65791eaf3e1SJohn Birrell }
65891eaf3e1SJohn Birrell return (error);
65991eaf3e1SJohn Birrell }
66091eaf3e1SJohn Birrell
66191eaf3e1SJohn Birrell SYSINIT(profile_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, profile_load, NULL);
66291eaf3e1SJohn Birrell SYSUNINIT(profile_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, profile_unload, NULL);
66391eaf3e1SJohn Birrell
66491eaf3e1SJohn Birrell DEV_MODULE(profile, profile_modevent, NULL);
66591eaf3e1SJohn Birrell MODULE_VERSION(profile, 1);
66691eaf3e1SJohn Birrell MODULE_DEPEND(profile, dtrace, 1, 1, 1);
66791eaf3e1SJohn Birrell MODULE_DEPEND(profile, opensolaris, 1, 1, 1);
668