]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/pmckern.h
epoch(9): Make epochs non-preemptible by default
[FreeBSD/FreeBSD.git] / sys / sys / pmckern.h
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2003-2007, Joseph Koshy
5  * Copyright (c) 2007 The FreeBSD Foundation
6  * All rights reserved.
7  *
8  * Portions of this software were developed by A. Joseph Koshy under
9  * sponsorship from the FreeBSD Foundation and Google, Inc.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD$
33  */
34
35 /*
36  * PMC interface used by the base kernel.
37  */
38
39 #ifndef _SYS_PMCKERN_H_
40 #define _SYS_PMCKERN_H_
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/proc.h>
47 #include <sys/sx.h>
48 #include <sys/pmc.h>
49
50 #include <machine/cpufunc.h>
51
52 #define PMC_FN_PROCESS_EXEC             1
53 #define PMC_FN_CSW_IN                   2
54 #define PMC_FN_CSW_OUT                  3
55 #define PMC_FN_DO_SAMPLES               4
56 #define PMC_FN_UNUSED1                  5
57 #define PMC_FN_UNUSED2                  6
58 #define PMC_FN_MMAP                     7
59 #define PMC_FN_MUNMAP                   8
60 #define PMC_FN_USER_CALLCHAIN           9
61 #define PMC_FN_USER_CALLCHAIN_SOFT      10
62 #define PMC_FN_SOFT_SAMPLING            11
63 #define PMC_FN_THR_CREATE               12
64 #define PMC_FN_THR_EXIT                 13
65
66 #define PMC_HR  0       /* Hardware ring buffer */
67 #define PMC_SR  1       /* Software ring buffer */
68
69 struct pmckern_procexec {
70         int             pm_credentialschanged;
71         uintfptr_t      pm_entryaddr;
72 };
73
74 struct pmckern_map_in {
75         void            *pm_file;       /* filename or vnode pointer */
76         uintfptr_t      pm_address;     /* address object is loaded at */
77 };
78
79 struct pmckern_map_out {
80         uintfptr_t      pm_address;     /* start address of region */
81         size_t          pm_size;        /* size of unmapped region */
82 };
83
84 struct pmckern_soft {
85         enum pmc_event          pm_ev;
86         int                     pm_cpu;
87         struct trapframe        *pm_tf;
88 };
89
90 /*
91  * Soft PMC.
92  */
93
94 #define PMC_SOFT_DEFINE_EX(prov, mod, func, name, alloc, release)               \
95         struct pmc_soft pmc_##prov##_##mod##_##func##_##name =                  \
96             { 0, alloc, release, { #prov "_" #mod "_" #func "." #name, 0 } };   \
97         SYSINIT(pmc_##prov##_##mod##_##func##_##name##_init, SI_SUB_KDTRACE,    \
98             SI_ORDER_SECOND + 1, pmc_soft_ev_register,                          \
99             &pmc_##prov##_##mod##_##func##_##name );                            \
100         SYSUNINIT(pmc_##prov##_##mod##_##func##_##name##_uninit,                \
101             SI_SUB_KDTRACE, SI_ORDER_SECOND + 1, pmc_soft_ev_deregister,        \
102             &pmc_##prov##_##mod##_##func##_##name )
103
104 #define PMC_SOFT_DEFINE(prov, mod, func, name)                                  \
105         PMC_SOFT_DEFINE_EX(prov, mod, func, name, NULL, NULL)
106
107 #define PMC_SOFT_DECLARE(prov, mod, func, name)                                 \
108         extern struct pmc_soft pmc_##prov##_##mod##_##func##_##name
109
110 /*
111  * PMC_SOFT_CALL can be used anywhere in the kernel.
112  * Require md defined PMC_FAKE_TRAPFRAME.
113  */
114 #ifdef PMC_FAKE_TRAPFRAME
115 #define PMC_SOFT_CALL(pr, mo, fu, na)                                           \
116 do {                                                                            \
117         if (__predict_false(pmc_##pr##_##mo##_##fu##_##na.ps_running)) {        \
118                 struct pmckern_soft ks;                                         \
119                 register_t intr;                                                \
120                 intr = intr_disable();                                          \
121                 PMC_FAKE_TRAPFRAME(&pmc_tf[curcpu]);                            \
122                 ks.pm_ev = pmc_##pr##_##mo##_##fu##_##na.ps_ev.pm_ev_code;      \
123                 ks.pm_cpu = PCPU_GET(cpuid);                                    \
124                 ks.pm_tf = &pmc_tf[curcpu];                                     \
125                 PMC_CALL_HOOK_UNLOCKED(curthread,                               \
126                     PMC_FN_SOFT_SAMPLING, (void *) &ks);                        \
127                 intr_restore(intr);                                             \
128         }                                                                       \
129 } while (0)
130 #else
131 #define PMC_SOFT_CALL(pr, mo, fu, na)                                           \
132 do {                                                                            \
133 } while (0)
134 #endif
135
136 /*
137  * PMC_SOFT_CALL_TF need to be used carefully.
138  * Userland capture will be done during AST processing.
139  */
140 #define PMC_SOFT_CALL_TF(pr, mo, fu, na, tf)                                    \
141 do {                                                                            \
142         if (__predict_false(pmc_##pr##_##mo##_##fu##_##na.ps_running)) {        \
143                 struct pmckern_soft ks;                                         \
144                 register_t intr;                                                \
145                 intr = intr_disable();                                          \
146                 ks.pm_ev = pmc_##pr##_##mo##_##fu##_##na.ps_ev.pm_ev_code;      \
147                 ks.pm_cpu = PCPU_GET(cpuid);                                    \
148                 ks.pm_tf = tf;                                                  \
149                 PMC_CALL_HOOK_UNLOCKED(curthread,                               \
150                     PMC_FN_SOFT_SAMPLING, (void *) &ks);                        \
151                 intr_restore(intr);                                             \
152         }                                                                       \
153 } while (0)
154
155 struct pmc_soft {
156         int                             ps_running;
157         void                            (*ps_alloc)(void);
158         void                            (*ps_release)(void);
159         struct pmc_dyn_event_descr      ps_ev;
160 };
161
162 struct pmclog_buffer;
163
164 struct pmc_domain_buffer_header {
165         struct mtx pdbh_mtx;
166         TAILQ_HEAD(, pmclog_buffer) pdbh_head;
167         struct pmclog_buffer *pdbh_plbs;
168         int pdbh_ncpus;
169 } __aligned(CACHE_LINE_SIZE);
170
171 /* hook */
172 extern int (*pmc_hook)(struct thread *_td, int _function, void *_arg);
173 extern int (*pmc_intr)(int _cpu, struct trapframe *_frame);
174
175 /* SX lock protecting the hook */
176 extern struct sx pmc_sx;
177
178 /* Per-cpu flags indicating availability of sampling data */
179 DPCPU_DECLARE(uint8_t, pmc_sampled);
180
181 /* Count of system-wide sampling PMCs in existence */
182 extern volatile int pmc_ss_count;
183
184 /* kernel version number */
185 extern const int pmc_kernel_version;
186
187 /* PMC soft per cpu trapframe */
188 extern struct trapframe pmc_tf[MAXCPU];
189
190 /* per domain buffer header list */
191 extern struct pmc_domain_buffer_header *pmc_dom_hdrs[MAXMEMDOM];
192
193 /* Quick check if preparatory work is necessary */
194 #define PMC_HOOK_INSTALLED(cmd) __predict_false(pmc_hook != NULL)
195
196 /* Hook invocation; for use within the kernel */
197 #define PMC_CALL_HOOK(t, cmd, arg)              \
198 do {                                            \
199         epoch_enter_preempt(global_epoch_preempt);              \
200         if (pmc_hook != NULL)                   \
201                 (pmc_hook)((t), (cmd), (arg));  \
202         epoch_exit_preempt(global_epoch_preempt);                       \
203 } while (0)
204
205 /* Hook invocation that needs an exclusive lock */
206 #define PMC_CALL_HOOK_X(t, cmd, arg)            \
207 do {                                            \
208         sx_xlock(&pmc_sx);                      \
209         if (pmc_hook != NULL)                   \
210                 (pmc_hook)((t), (cmd), (arg));  \
211         sx_xunlock(&pmc_sx);                    \
212 } while (0)
213
214 /*
215  * Some hook invocations (e.g., from context switch and clock handling
216  * code) need to be lock-free.
217  */
218 #define PMC_CALL_HOOK_UNLOCKED(t, cmd, arg)     \
219 do {                                            \
220         if (pmc_hook != NULL)                   \
221                 (pmc_hook)((t), (cmd), (arg));  \
222 } while (0)
223
224 #define PMC_SWITCH_CONTEXT(t,cmd)       PMC_CALL_HOOK_UNLOCKED(t,cmd,NULL)
225
226 /* Check if a process is using HWPMCs.*/
227 #define PMC_PROC_IS_USING_PMCS(p)                               \
228         (__predict_false(p->p_flag & P_HWPMC))
229
230 /* Check if a thread have pending user capture. */
231 #define PMC_IS_PENDING_CALLCHAIN(p)                             \
232         (__predict_false((p)->td_pflags & TDP_CALLCHAIN))
233
234 #define PMC_SYSTEM_SAMPLING_ACTIVE()            (pmc_ss_count > 0)
235
236 /* Check if a CPU has recorded samples. */
237 #define PMC_CPU_HAS_SAMPLES(C)  (__predict_false(DPCPU_ID_GET((C), pmc_sampled)))
238
239 /*
240  * Helper functions.
241  */
242 int             pmc_cpu_is_disabled(int _cpu);  /* deprecated */
243 int             pmc_cpu_is_active(int _cpu);
244 int             pmc_cpu_is_present(int _cpu);
245 int             pmc_cpu_is_primary(int _cpu);
246 unsigned int    pmc_cpu_max(void);
247
248 #ifdef  INVARIANTS
249 int             pmc_cpu_max_active(void);
250 #endif
251
252 /*
253  * Soft events functions.
254  */
255 void pmc_soft_ev_register(struct pmc_soft *ps);
256 void pmc_soft_ev_deregister(struct pmc_soft *ps);
257 struct pmc_soft *pmc_soft_ev_acquire(enum pmc_event ev);
258 void pmc_soft_ev_release(struct pmc_soft *ps);
259
260 #endif /* _SYS_PMCKERN_H_ */