]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cddl/dev/profile/profile.c
dtrace/powerpc: Adjust AFRAMES for fbt and profile
[FreeBSD/FreeBSD.git] / sys / cddl / dev / profile / profile.c
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 jb@freebsd.org
22  *
23  * $FreeBSD$
24  *
25  */
26
27 /*
28  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
29  * Use is subject to license terms.
30  */
31
32 #include <sys/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/conf.h>
36 #include <sys/cpuvar.h>
37 #include <sys/endian.h>
38 #include <sys/fcntl.h>
39 #include <sys/filio.h>
40 #include <sys/kdb.h>
41 #include <sys/kernel.h>
42 #include <sys/kmem.h>
43 #include <sys/kthread.h>
44 #include <sys/limits.h>
45 #include <sys/linker.h>
46 #include <sys/lock.h>
47 #include <sys/malloc.h>
48 #include <sys/module.h>
49 #include <sys/mutex.h>
50 #include <sys/poll.h>
51 #include <sys/proc.h>
52 #include <sys/selinfo.h>
53 #include <sys/smp.h>
54 #include <sys/sysctl.h>
55 #include <sys/uio.h>
56 #include <sys/unistd.h>
57 #include <machine/cpu.h>
58 #include <machine/stdarg.h>
59
60 #include <sys/dtrace.h>
61 #include <sys/dtrace_bsd.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
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
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
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         } else if (TD_IS_IDLETHREAD(td))
254                 pc = (uintfptr_t)&cpu_idle;
255
256         dtrace_probe(prof->prof_id, pc, upc, late, 0, 0);
257 }
258
259 static void
260 profile_fire(void *arg)
261 {
262         profile_probe_percpu_t *pcpu = arg;
263         profile_probe_t *prof = pcpu->profc_probe;
264         hrtime_t late;
265
266         late = sbt_to_nsec(sbinuptime() - pcpu->profc_expected);
267
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);
272 }
273
274 static void
275 profile_tick(void *arg)
276 {
277         profile_probe_t *prof = arg;
278
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);
283 }
284
285 static void
286 profile_create(hrtime_t interval, char *name, int kind)
287 {
288         profile_probe_t *prof;
289
290         if (interval < profile_interval_min)
291                 return;
292
293         if (dtrace_probe_lookup(profile_id, NULL, NULL, name) != 0)
294                 return;
295
296         atomic_add_32(&profile_total, 1);
297         if (profile_total > profile_max) {
298                 atomic_add_32(&profile_total, -1);
299                 return;
300         }
301
302         prof = kmem_zalloc(sizeof (profile_probe_t), KM_SLEEP);
303         (void) strcpy(prof->prof_name, name);
304 #ifdef illumos
305         prof->prof_interval = interval;
306         prof->prof_cyclic = CYCLIC_NONE;
307 #else
308         prof->prof_interval = nsec_to_sbt(interval);
309         callout_init(&prof->prof_cyclic, 1);
310 #endif
311         prof->prof_kind = kind;
312         prof->prof_id = dtrace_probe_create(profile_id,
313             NULL, NULL, name,
314             profile_aframes, prof);
315 }
316
317 /*ARGSUSED*/
318 static void
319 profile_provide(void *arg, dtrace_probedesc_t *desc)
320 {
321         int i, j, rate, kind;
322         hrtime_t val = 0, mult = 1, len = 0;
323         char *name, *suffix = NULL;
324
325         const struct {
326                 char *prefix;
327                 int kind;
328         } types[] = {
329                 { PROF_PREFIX_PROFILE, PROF_PROFILE },
330                 { PROF_PREFIX_TICK, PROF_TICK },
331                 { 0, 0 }
332         };
333
334         const struct {
335                 char *name;
336                 hrtime_t mult;
337         } suffixes[] = {
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) },
352                 { "hz",         0 },
353                 { NULL }
354         };
355
356         if (desc == NULL) {
357                 char n[PROF_NAMELEN];
358
359                 /*
360                  * If no description was provided, provide all of our probes.
361                  */
362                 for (i = 0; i < sizeof (profile_rates) / sizeof (int); i++) {
363                         if ((rate = profile_rates[i]) == 0)
364                                 continue;
365
366                         (void) snprintf(n, PROF_NAMELEN, "%s%d",
367                             PROF_PREFIX_PROFILE, rate);
368                         profile_create(NANOSEC / rate, n, PROF_PROFILE);
369                 }
370
371                 for (i = 0; i < sizeof (profile_ticks) / sizeof (int); i++) {
372                         if ((rate = profile_ticks[i]) == 0)
373                                 continue;
374
375                         (void) snprintf(n, PROF_NAMELEN, "%s%d",
376                             PROF_PREFIX_TICK, rate);
377                         profile_create(NANOSEC / rate, n, PROF_TICK);
378                 }
379
380                 return;
381         }
382
383         name = desc->dtpd_name;
384
385         for (i = 0; types[i].prefix != NULL; i++) {
386                 len = strlen(types[i].prefix);
387
388                 if (strncmp(name, types[i].prefix, len) != 0)
389                         continue;
390                 break;
391         }
392
393         if (types[i].prefix == NULL)
394                 return;
395
396         kind = types[i].kind;
397         j = strlen(name) - len;
398
399         /*
400          * We need to start before any time suffix.
401          */
402         for (j = strlen(name); j >= len; j--) {
403                 if (name[j] >= '0' && name[j] <= '9')
404                         break;
405                 suffix = &name[j];
406         }
407
408         ASSERT(suffix != NULL);
409
410         /*
411          * Now determine the numerical value present in the probe name.
412          */
413         for (; j >= len; j--) {
414                 if (name[j] < '0' || name[j] > '9')
415                         return;
416
417                 val += (name[j] - '0') * mult;
418                 mult *= (hrtime_t)10;
419         }
420
421         if (val == 0)
422                 return;
423
424         /*
425          * Look-up the suffix to determine the multiplier.
426          */
427         for (i = 0, mult = 0; suffixes[i].name != NULL; i++) {
428                 if (strcasecmp(suffixes[i].name, suffix) == 0) {
429                         mult = suffixes[i].mult;
430                         break;
431                 }
432         }
433
434         if (suffixes[i].name == NULL && *suffix != '\0')
435                 return;
436
437         if (mult == 0) {
438                 /*
439                  * The default is frequency-per-second.
440                  */
441                 val = NANOSEC / val;
442         } else {
443                 val *= mult;
444         }
445
446         profile_create(val, name, kind);
447 }
448
449 /* ARGSUSED */
450 static void
451 profile_destroy(void *arg, dtrace_id_t id, void *parg)
452 {
453         profile_probe_t *prof = parg;
454
455 #ifdef illumos
456         ASSERT(prof->prof_cyclic == CYCLIC_NONE);
457 #else
458         ASSERT(!callout_active(&prof->prof_cyclic) && prof->prof_pcpus == NULL);
459 #endif
460         kmem_free(prof, sizeof (profile_probe_t));
461
462         ASSERT(profile_total >= 1);
463         atomic_add_32(&profile_total, -1);
464 }
465
466 #ifdef illumos
467 /*ARGSUSED*/
468 static void
469 profile_online(void *arg, cpu_t *cpu, cyc_handler_t *hdlr, cyc_time_t *when)
470 {
471         profile_probe_t *prof = arg;
472         profile_probe_percpu_t *pcpu;
473
474         pcpu = kmem_zalloc(sizeof (profile_probe_percpu_t), KM_SLEEP);
475         pcpu->profc_probe = prof;
476
477         hdlr->cyh_func = profile_fire;
478         hdlr->cyh_arg = pcpu;
479
480         when->cyt_interval = prof->prof_interval;
481         when->cyt_when = gethrtime() + when->cyt_interval;
482
483         pcpu->profc_expected = when->cyt_when;
484         pcpu->profc_interval = when->cyt_interval;
485 }
486
487 /*ARGSUSED*/
488 static void
489 profile_offline(void *arg, cpu_t *cpu, void *oarg)
490 {
491         profile_probe_percpu_t *pcpu = oarg;
492
493         ASSERT(pcpu->profc_probe == arg);
494         kmem_free(pcpu, sizeof (profile_probe_percpu_t));
495 }
496
497 /* ARGSUSED */
498 static void
499 profile_enable(void *arg, dtrace_id_t id, void *parg)
500 {
501         profile_probe_t *prof = parg;
502         cyc_omni_handler_t omni;
503         cyc_handler_t hdlr;
504         cyc_time_t when;
505
506         ASSERT(prof->prof_interval != 0);
507         ASSERT(MUTEX_HELD(&cpu_lock));
508
509         if (prof->prof_kind == PROF_TICK) {
510                 hdlr.cyh_func = profile_tick;
511                 hdlr.cyh_arg = prof;
512
513                 when.cyt_interval = prof->prof_interval;
514                 when.cyt_when = gethrtime() + when.cyt_interval;
515         } else {
516                 ASSERT(prof->prof_kind == PROF_PROFILE);
517                 omni.cyo_online = profile_online;
518                 omni.cyo_offline = profile_offline;
519                 omni.cyo_arg = prof;
520         }
521
522         if (prof->prof_kind == PROF_TICK) {
523                 prof->prof_cyclic = cyclic_add(&hdlr, &when);
524         } else {
525                 prof->prof_cyclic = cyclic_add_omni(&omni);
526         }
527 }
528
529 /* ARGSUSED */
530 static void
531 profile_disable(void *arg, dtrace_id_t id, void *parg)
532 {
533         profile_probe_t *prof = parg;
534
535         ASSERT(prof->prof_cyclic != CYCLIC_NONE);
536         ASSERT(MUTEX_HELD(&cpu_lock));
537
538         cyclic_remove(prof->prof_cyclic);
539         prof->prof_cyclic = CYCLIC_NONE;
540 }
541
542 #else
543
544 static void
545 profile_enable_omni(profile_probe_t *prof)
546 {
547         profile_probe_percpu_t *pcpu;
548         int cpu;
549
550         prof->prof_pcpus = kmem_zalloc((mp_maxid + 1) * sizeof(pcpu), KM_SLEEP);
551         CPU_FOREACH(cpu) {
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);
561         }
562 }
563
564 static void
565 profile_disable_omni(profile_probe_t *prof)
566 {
567         profile_probe_percpu_t *pcpu;
568         int cpu;
569
570         ASSERT(prof->prof_pcpus != NULL);
571         CPU_FOREACH(cpu) {
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));
578         }
579         kmem_free(prof->prof_pcpus, (mp_maxid + 1) * sizeof(pcpu));
580         prof->prof_pcpus = NULL;
581 }
582
583 /* ARGSUSED */
584 static void
585 profile_enable(void *arg, dtrace_id_t id, void *parg)
586 {
587         profile_probe_t *prof = parg;
588
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);
594         } else {
595                 ASSERT(prof->prof_kind == PROF_PROFILE);
596                 profile_enable_omni(prof);
597         }
598 }
599
600 /* ARGSUSED */
601 static void
602 profile_disable(void *arg, dtrace_id_t id, void *parg)
603 {
604         profile_probe_t *prof = parg;
605
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);
610         } else {
611                 ASSERT(prof->prof_kind == PROF_PROFILE);
612                 profile_disable_omni(prof);
613         }
614 }
615 #endif
616
617 static void
618 profile_load(void *dummy)
619 {
620         if (dtrace_register("profile", &profile_attr, DTRACE_PRIV_USER,
621             NULL, &profile_pops, NULL, &profile_id) != 0)
622                 return;
623 }
624
625
626 static int
627 profile_unload(void)
628 {
629         int error = 0;
630
631         if ((error = dtrace_unregister(profile_id)) != 0)
632                 return (error);
633
634         return (error);
635 }
636
637 /* ARGSUSED */
638 static int
639 profile_modevent(module_t mod __unused, int type, void *data __unused)
640 {
641         int error = 0;
642
643         switch (type) {
644         case MOD_LOAD:
645                 break;
646
647         case MOD_UNLOAD:
648                 break;
649
650         case MOD_SHUTDOWN:
651                 break;
652
653         default:
654                 error = EOPNOTSUPP;
655                 break;
656
657         }
658         return (error);
659 }
660
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);
663
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);