1 /*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 *
21 * Portions Copyright 2006-2008 John Birrell [email protected]
22 *
23 */
24
25 /*
26 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
28 */
29
30 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/conf.h>
34 #include <sys/cpuvar.h>
35 #include <sys/endian.h>
36 #include <sys/fcntl.h>
37 #include <sys/filio.h>
38 #include <sys/kdb.h>
39 #include <sys/kernel.h>
40 #include <sys/kmem.h>
41 #include <sys/kthread.h>
42 #include <sys/limits.h>
43 #include <sys/linker.h>
44 #include <sys/lock.h>
45 #include <sys/malloc.h>
46 #include <sys/module.h>
47 #include <sys/mutex.h>
48 #include <sys/poll.h>
49 #include <sys/proc.h>
50 #include <sys/selinfo.h>
51 #include <sys/smp.h>
52 #include <sys/sysctl.h>
53 #include <sys/uio.h>
54 #include <sys/unistd.h>
55 #include <machine/cpu.h>
56 #include <machine/stdarg.h>
57
58 #include <sys/dtrace.h>
59 #include <sys/dtrace_bsd.h>
60
61 #include <cddl/dev/dtrace/dtrace_cddl.h>
62
63 #define PROF_NAMELEN 15
64
65 #define PROF_PROFILE 0
66 #define PROF_TICK 1
67 #define PROF_PREFIX_PROFILE "profile-"
68 #define PROF_PREFIX_TICK "tick-"
69
70 /*
71 * Regardless of platform, there are five artificial frames in the case of the
72 * profile provider:
73 *
74 * profile_fire
75 * cyclic_expire
76 * cyclic_fire
77 * [ cbe ]
78 * [ locore ]
79 *
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.
85 *
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.
91 */
92 #ifdef __amd64
93 #define PROF_ARTIFICIAL_FRAMES 10
94 #else
95 #ifdef __i386
96 #define PROF_ARTIFICIAL_FRAMES 6
97 #endif
98 #endif
99
100 #ifdef __powerpc__
101 /*
102 * This value is bogus just to make module compilable on powerpc
103 */
104 #define PROF_ARTIFICIAL_FRAMES 8
105 #endif
106
107 struct profile_probe_percpu;
108
109 #ifdef __arm__
110 #define PROF_ARTIFICIAL_FRAMES 3
111 #endif
112
113 #ifdef __aarch64__
114 #define PROF_ARTIFICIAL_FRAMES 12
115 #endif
116
117 #ifdef __riscv
118 #define PROF_ARTIFICIAL_FRAMES 12
119 #endif
120
121 typedef struct profile_probe {
122 char prof_name[PROF_NAMELEN];
123 dtrace_id_t prof_id;
124 int prof_kind;
125 #ifdef illumos
126 hrtime_t prof_interval;
127 cyclic_id_t prof_cyclic;
128 #else
129 sbintime_t prof_interval;
130 struct callout prof_cyclic;
131 sbintime_t prof_expected;
132 struct profile_probe_percpu **prof_pcpus;
133 #endif
134 } profile_probe_t;
135
136 typedef struct profile_probe_percpu {
137 hrtime_t profc_expected;
138 hrtime_t profc_interval;
139 profile_probe_t *profc_probe;
140 #ifdef __FreeBSD__
141 struct callout profc_cyclic;
142 #endif
143 } profile_probe_percpu_t;
144
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 *);
152
153 static int profile_rates[] = {
154 97, 199, 499, 997, 1999,
155 4001, 4999, 0, 0, 0,
156 0, 0, 0, 0, 0,
157 0, 0, 0, 0, 0
158 };
159
160 static int profile_ticks[] = {
161 1, 10, 100, 500, 1000,
162 5000, 0, 0, 0, 0,
163 0, 0, 0, 0, 0
164 };
165
166 /*
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.
172 */
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 */
177
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 },
184 };
185
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,
192 .dtps_resume = NULL,
193 .dtps_getargdesc = NULL,
194 .dtps_getargval = NULL,
195 .dtps_usermode = NULL,
196 .dtps_destroy = profile_destroy
197 };
198
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;
202
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");
208
209 static sbintime_t
nsec_to_sbt(hrtime_t nsec)210 nsec_to_sbt(hrtime_t nsec)
211 {
212 time_t sec;
213
214 /*
215 * We need to calculate nsec * 2^32 / 10^9
216 * Seconds and nanoseconds are split to avoid overflow.
217 */
218 sec = nsec / NANOSEC;
219 nsec = nsec % NANOSEC;
220 return (((sbintime_t)sec << 32) | ((sbintime_t)nsec << 32) / NANOSEC);
221 }
222
223 static hrtime_t
sbt_to_nsec(sbintime_t sbt)224 sbt_to_nsec(sbintime_t sbt)
225 {
226
227 return ((sbt >> 32) * NANOSEC +
228 (((uint32_t)sbt * (hrtime_t)NANOSEC) >> 32));
229 }
230
231 static void
profile_probe(profile_probe_t * prof,hrtime_t late)232 profile_probe(profile_probe_t *prof, hrtime_t late)
233 {
234 struct thread *td;
235 struct trapframe *frame;
236 uintfptr_t pc, upc;
237
238 td = curthread;
239 pc = upc = 0;
240
241 /*
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.
246 */
247 frame = td->td_intr_frame;
248 if (frame != NULL) {
249 if (TRAPF_USERMODE(frame))
250 upc = TRAPF_PC(frame);
251 else {
252 pc = TRAPF_PC(frame);
253 td->t_dtrace_trapframe = frame;
254 }
255 } else if (TD_IS_IDLETHREAD(td))
256 pc = (uintfptr_t)&cpu_idle;
257
258 dtrace_probe(prof->prof_id, pc, upc, late, 0, 0);
259 td->t_dtrace_trapframe = NULL;
260 }
261
262 static void
profile_fire(void * arg)263 profile_fire(void *arg)
264 {
265 profile_probe_percpu_t *pcpu = arg;
266 profile_probe_t *prof = pcpu->profc_probe;
267 hrtime_t late;
268
269 late = sbt_to_nsec(sbinuptime() - pcpu->profc_expected);
270
271 profile_probe(prof, late);
272 pcpu->profc_expected += pcpu->profc_interval;
273 callout_schedule_sbt_curcpu(&pcpu->profc_cyclic,
274 pcpu->profc_expected, 0, C_DIRECT_EXEC | C_ABSOLUTE);
275 }
276
277 static void
profile_tick(void * arg)278 profile_tick(void *arg)
279 {
280 profile_probe_t *prof = arg;
281
282 profile_probe(prof, 0);
283 prof->prof_expected += prof->prof_interval;
284 callout_schedule_sbt(&prof->prof_cyclic,
285 prof->prof_expected, 0, C_DIRECT_EXEC | C_ABSOLUTE);
286 }
287
288 static void
profile_create(hrtime_t interval,char * name,int kind)289 profile_create(hrtime_t interval, char *name, int kind)
290 {
291 profile_probe_t *prof;
292
293 if (interval < profile_interval_min)
294 return;
295
296 if (dtrace_probe_lookup(profile_id, NULL, NULL, name) != 0)
297 return;
298
299 atomic_add_32(&profile_total, 1);
300 if (profile_total > profile_max) {
301 atomic_add_32(&profile_total, -1);
302 return;
303 }
304
305 prof = kmem_zalloc(sizeof (profile_probe_t), KM_SLEEP);
306 (void) strcpy(prof->prof_name, name);
307 #ifdef illumos
308 prof->prof_interval = interval;
309 prof->prof_cyclic = CYCLIC_NONE;
310 #else
311 prof->prof_interval = nsec_to_sbt(interval);
312 callout_init(&prof->prof_cyclic, 1);
313 #endif
314 prof->prof_kind = kind;
315 prof->prof_id = dtrace_probe_create(profile_id,
316 NULL, NULL, name,
317 profile_aframes, prof);
318 }
319
320 /*ARGSUSED*/
321 static void
profile_provide(void * arg,dtrace_probedesc_t * desc)322 profile_provide(void *arg, dtrace_probedesc_t *desc)
323 {
324 int i, j, rate, kind;
325 hrtime_t val = 0, mult = 1, len = 0;
326 char *name, *suffix = NULL;
327
328 const struct {
329 char *prefix;
330 int kind;
331 } types[] = {
332 { PROF_PREFIX_PROFILE, PROF_PROFILE },
333 { PROF_PREFIX_TICK, PROF_TICK },
334 { 0, 0 }
335 };
336
337 const struct {
338 char *name;
339 hrtime_t mult;
340 } suffixes[] = {
341 { "ns", NANOSEC / NANOSEC },
342 { "nsec", NANOSEC / NANOSEC },
343 { "us", NANOSEC / MICROSEC },
344 { "usec", NANOSEC / MICROSEC },
345 { "ms", NANOSEC / MILLISEC },
346 { "msec", NANOSEC / MILLISEC },
347 { "s", NANOSEC / SEC },
348 { "sec", NANOSEC / SEC },
349 { "m", NANOSEC * (hrtime_t)60 },
350 { "min", NANOSEC * (hrtime_t)60 },
351 { "h", NANOSEC * (hrtime_t)(60 * 60) },
352 { "hour", NANOSEC * (hrtime_t)(60 * 60) },
353 { "d", NANOSEC * (hrtime_t)(24 * 60 * 60) },
354 { "day", NANOSEC * (hrtime_t)(24 * 60 * 60) },
355 { "hz", 0 },
356 { NULL }
357 };
358
359 if (desc == NULL) {
360 char n[PROF_NAMELEN];
361
362 /*
363 * If no description was provided, provide all of our probes.
364 */
365 for (i = 0; i < sizeof (profile_rates) / sizeof (int); i++) {
366 if ((rate = profile_rates[i]) == 0)
367 continue;
368
369 (void) snprintf(n, PROF_NAMELEN, "%s%d",
370 PROF_PREFIX_PROFILE, rate);
371 profile_create(NANOSEC / rate, n, PROF_PROFILE);
372 }
373
374 for (i = 0; i < sizeof (profile_ticks) / sizeof (int); i++) {
375 if ((rate = profile_ticks[i]) == 0)
376 continue;
377
378 (void) snprintf(n, PROF_NAMELEN, "%s%d",
379 PROF_PREFIX_TICK, rate);
380 profile_create(NANOSEC / rate, n, PROF_TICK);
381 }
382
383 return;
384 }
385
386 name = desc->dtpd_name;
387
388 for (i = 0; types[i].prefix != NULL; i++) {
389 len = strlen(types[i].prefix);
390
391 if (strncmp(name, types[i].prefix, len) != 0)
392 continue;
393 break;
394 }
395
396 if (types[i].prefix == NULL)
397 return;
398
399 kind = types[i].kind;
400 j = strlen(name) - len;
401
402 /*
403 * We need to start before any time suffix.
404 */
405 for (j = strlen(name); j >= len; j--) {
406 if (name[j] >= '0' && name[j] <= '9')
407 break;
408 suffix = &name[j];
409 }
410
411 ASSERT(suffix != NULL);
412
413 /*
414 * Now determine the numerical value present in the probe name.
415 */
416 for (; j >= len; j--) {
417 if (name[j] < '0' || name[j] > '9')
418 return;
419
420 val += (name[j] - '0') * mult;
421 mult *= (hrtime_t)10;
422 }
423
424 if (val == 0)
425 return;
426
427 /*
428 * Look-up the suffix to determine the multiplier.
429 */
430 for (i = 0, mult = 0; suffixes[i].name != NULL; i++) {
431 if (strcasecmp(suffixes[i].name, suffix) == 0) {
432 mult = suffixes[i].mult;
433 break;
434 }
435 }
436
437 if (suffixes[i].name == NULL && *suffix != '\0')
438 return;
439
440 if (mult == 0) {
441 /*
442 * The default is frequency-per-second.
443 */
444 val = NANOSEC / val;
445 } else {
446 val *= mult;
447 }
448
449 profile_create(val, name, kind);
450 }
451
452 /* ARGSUSED */
453 static void
profile_destroy(void * arg,dtrace_id_t id,void * parg)454 profile_destroy(void *arg, dtrace_id_t id, void *parg)
455 {
456 profile_probe_t *prof = parg;
457
458 #ifdef illumos
459 ASSERT(prof->prof_cyclic == CYCLIC_NONE);
460 #else
461 ASSERT(!callout_active(&prof->prof_cyclic) && prof->prof_pcpus == NULL);
462 #endif
463 kmem_free(prof, sizeof (profile_probe_t));
464
465 ASSERT(profile_total >= 1);
466 atomic_add_32(&profile_total, -1);
467 }
468
469 #ifdef illumos
470 /*ARGSUSED*/
471 static void
profile_online(void * arg,cpu_t * cpu,cyc_handler_t * hdlr,cyc_time_t * when)472 profile_online(void *arg, cpu_t *cpu, cyc_handler_t *hdlr, cyc_time_t *when)
473 {
474 profile_probe_t *prof = arg;
475 profile_probe_percpu_t *pcpu;
476
477 pcpu = kmem_zalloc(sizeof (profile_probe_percpu_t), KM_SLEEP);
478 pcpu->profc_probe = prof;
479
480 hdlr->cyh_func = profile_fire;
481 hdlr->cyh_arg = pcpu;
482
483 when->cyt_interval = prof->prof_interval;
484 when->cyt_when = gethrtime() + when->cyt_interval;
485
486 pcpu->profc_expected = when->cyt_when;
487 pcpu->profc_interval = when->cyt_interval;
488 }
489
490 /*ARGSUSED*/
491 static void
profile_offline(void * arg,cpu_t * cpu,void * oarg)492 profile_offline(void *arg, cpu_t *cpu, void *oarg)
493 {
494 profile_probe_percpu_t *pcpu = oarg;
495
496 ASSERT(pcpu->profc_probe == arg);
497 kmem_free(pcpu, sizeof (profile_probe_percpu_t));
498 }
499
500 /* ARGSUSED */
501 static void
profile_enable(void * arg,dtrace_id_t id,void * parg)502 profile_enable(void *arg, dtrace_id_t id, void *parg)
503 {
504 profile_probe_t *prof = parg;
505 cyc_omni_handler_t omni;
506 cyc_handler_t hdlr;
507 cyc_time_t when;
508
509 ASSERT(prof->prof_interval != 0);
510 ASSERT(MUTEX_HELD(&cpu_lock));
511
512 if (prof->prof_kind == PROF_TICK) {
513 hdlr.cyh_func = profile_tick;
514 hdlr.cyh_arg = prof;
515
516 when.cyt_interval = prof->prof_interval;
517 when.cyt_when = gethrtime() + when.cyt_interval;
518 } else {
519 ASSERT(prof->prof_kind == PROF_PROFILE);
520 omni.cyo_online = profile_online;
521 omni.cyo_offline = profile_offline;
522 omni.cyo_arg = prof;
523 }
524
525 if (prof->prof_kind == PROF_TICK) {
526 prof->prof_cyclic = cyclic_add(&hdlr, &when);
527 } else {
528 prof->prof_cyclic = cyclic_add_omni(&omni);
529 }
530 }
531
532 /* ARGSUSED */
533 static void
profile_disable(void * arg,dtrace_id_t id,void * parg)534 profile_disable(void *arg, dtrace_id_t id, void *parg)
535 {
536 profile_probe_t *prof = parg;
537
538 ASSERT(prof->prof_cyclic != CYCLIC_NONE);
539 ASSERT(MUTEX_HELD(&cpu_lock));
540
541 cyclic_remove(prof->prof_cyclic);
542 prof->prof_cyclic = CYCLIC_NONE;
543 }
544
545 #else
546
547 static void
profile_enable_omni(profile_probe_t * prof)548 profile_enable_omni(profile_probe_t *prof)
549 {
550 profile_probe_percpu_t *pcpu;
551 int cpu;
552
553 prof->prof_pcpus = kmem_zalloc((mp_maxid + 1) * sizeof(pcpu), KM_SLEEP);
554 CPU_FOREACH(cpu) {
555 pcpu = kmem_zalloc(sizeof(profile_probe_percpu_t), KM_SLEEP);
556 prof->prof_pcpus[cpu] = pcpu;
557 pcpu->profc_probe = prof;
558 pcpu->profc_expected = sbinuptime() + prof->prof_interval;
559 pcpu->profc_interval = prof->prof_interval;
560 callout_init(&pcpu->profc_cyclic, 1);
561 callout_reset_sbt_on(&pcpu->profc_cyclic,
562 pcpu->profc_expected, 0, profile_fire, pcpu,
563 cpu, C_DIRECT_EXEC | C_ABSOLUTE);
564 }
565 }
566
567 static void
profile_disable_omni(profile_probe_t * prof)568 profile_disable_omni(profile_probe_t *prof)
569 {
570 profile_probe_percpu_t *pcpu;
571 int cpu;
572
573 ASSERT(prof->prof_pcpus != NULL);
574 CPU_FOREACH(cpu) {
575 pcpu = prof->prof_pcpus[cpu];
576 ASSERT(pcpu->profc_probe == prof);
577 ASSERT(callout_active(&pcpu->profc_cyclic));
578 callout_stop(&pcpu->profc_cyclic);
579 callout_drain(&pcpu->profc_cyclic);
580 kmem_free(pcpu, sizeof(profile_probe_percpu_t));
581 }
582 kmem_free(prof->prof_pcpus, (mp_maxid + 1) * sizeof(pcpu));
583 prof->prof_pcpus = NULL;
584 }
585
586 /* ARGSUSED */
587 static void
profile_enable(void * arg,dtrace_id_t id,void * parg)588 profile_enable(void *arg, dtrace_id_t id, void *parg)
589 {
590 profile_probe_t *prof = parg;
591
592 if (prof->prof_kind == PROF_TICK) {
593 prof->prof_expected = sbinuptime() + prof->prof_interval;
594 callout_reset_sbt(&prof->prof_cyclic,
595 prof->prof_expected, 0, profile_tick, prof,
596 C_DIRECT_EXEC | C_ABSOLUTE);
597 } else {
598 ASSERT(prof->prof_kind == PROF_PROFILE);
599 profile_enable_omni(prof);
600 }
601 }
602
603 /* ARGSUSED */
604 static void
profile_disable(void * arg,dtrace_id_t id,void * parg)605 profile_disable(void *arg, dtrace_id_t id, void *parg)
606 {
607 profile_probe_t *prof = parg;
608
609 if (prof->prof_kind == PROF_TICK) {
610 ASSERT(callout_active(&prof->prof_cyclic));
611 callout_stop(&prof->prof_cyclic);
612 callout_drain(&prof->prof_cyclic);
613 } else {
614 ASSERT(prof->prof_kind == PROF_PROFILE);
615 profile_disable_omni(prof);
616 }
617 }
618 #endif
619
620 static void
profile_load(void * dummy)621 profile_load(void *dummy)
622 {
623 if (dtrace_register("profile", &profile_attr, DTRACE_PRIV_USER,
624 NULL, &profile_pops, NULL, &profile_id) != 0)
625 return;
626 }
627
628
629 static int
profile_unload(void)630 profile_unload(void)
631 {
632 int error = 0;
633
634 if ((error = dtrace_unregister(profile_id)) != 0)
635 return (error);
636
637 return (error);
638 }
639
640 /* ARGSUSED */
641 static int
profile_modevent(module_t mod __unused,int type,void * data __unused)642 profile_modevent(module_t mod __unused, int type, void *data __unused)
643 {
644 int error = 0;
645
646 switch (type) {
647 case MOD_LOAD:
648 break;
649
650 case MOD_UNLOAD:
651 break;
652
653 case MOD_SHUTDOWN:
654 break;
655
656 default:
657 error = EOPNOTSUPP;
658 break;
659
660 }
661 return (error);
662 }
663
664 SYSINIT(profile_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, profile_load, NULL);
665 SYSUNINIT(profile_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, profile_unload, NULL);
666
667 DEV_MODULE(profile, profile_modevent, NULL);
668 MODULE_VERSION(profile, 1);
669 MODULE_DEPEND(profile, dtrace, 1, 1, 1);
670 MODULE_DEPEND(profile, opensolaris, 1, 1, 1);
671