memif: memory leak when deleting memif interface
[vpp.git] / src / plugins / perfmon / perfmon.h
1 /*
2  * Copyright (c) 2020 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #ifndef __perfmon_perfmon_h
17 #define __perfmon_perfmon_h
18
19 #include <linux/perf_event.h>
20 #include <vppinfra/clib.h>
21 #include <vppinfra/format.h>
22 #include <vppinfra/error.h>
23 #include <vppinfra/cpu.h>
24 #include <vlib/vlib.h>
25
26 #define PERF_MAX_EVENTS 12 /* 4 fixed and 8 programable on ICX */
27
28 typedef enum
29 {
30   PERFMON_EVENT_TYPE_GENERAL,
31   PERFMON_EVENT_TYPE_FIXED,
32   PERFMON_EVENT_TYPE_PSEUDO,
33   PERFMON_EVENT_TYPE_MAX,
34 } perfmon_event_type_t;
35
36 typedef enum
37 {
38   PERFMON_BUNDLE_TYPE_UNKNOWN,
39   PERFMON_BUNDLE_TYPE_NODE,
40   PERFMON_BUNDLE_TYPE_THREAD,
41   PERFMON_BUNDLE_TYPE_SYSTEM,
42   PERFMON_BUNDLE_TYPE_MAX,
43   PERFMON_BUNDLE_TYPE_NODE_OR_THREAD,
44 } perfmon_bundle_type_t;
45
46 #define foreach_perfmon_bundle_type                                           \
47   _ (PERFMON_BUNDLE_TYPE_UNKNOWN, "not supported")                            \
48   _ (PERFMON_BUNDLE_TYPE_NODE, "node")                                        \
49   _ (PERFMON_BUNDLE_TYPE_THREAD, "thread")                                    \
50   _ (PERFMON_BUNDLE_TYPE_SYSTEM, "system")
51
52 typedef enum
53 {
54 #define _(e, str) e##_FLAG = 1 << e,
55   foreach_perfmon_bundle_type
56 #undef _
57
58 } perfmon_bundle_type_flag_t;
59
60 typedef struct
61 {
62   u32 type_from_instance : 1;
63   u32 exclude_kernel : 1;
64   union
65   {
66     u32 type;
67     u32 instance_type;
68   };
69   u64 config;
70   char *name;
71   char *description;
72 } perfmon_event_t;
73
74 typedef struct
75 {
76   u32 type;
77   int cpu;
78   pid_t pid;
79   char *name;
80 } perfmon_instance_t;
81
82 typedef struct
83 {
84   char *name;
85   perfmon_instance_t *instances;
86 } perfmon_instance_type_t;
87
88 struct perfmon_source;
89 extern vlib_node_function_t *perfmon_dispatch_wrappers[PERF_MAX_EVENTS + 1];
90
91 typedef clib_error_t *(perfmon_source_init_fn_t) (vlib_main_t *vm,
92                                                   struct perfmon_source *);
93 typedef perfmon_event_type_t (perfmon_source_get_event_type) (u32 event);
94
95 typedef struct perfmon_source
96 {
97   char *name;
98   char *description;
99   struct perfmon_source *next;
100   perfmon_event_t *events;
101   u32 n_events;
102   perfmon_instance_type_t *instances_by_type;
103   format_function_t *format_config;
104   perfmon_source_get_event_type *get_event_type;
105   perfmon_source_init_fn_t *init_fn;
106 } perfmon_source_t;
107
108 struct perfmon_bundle;
109
110 typedef clib_error_t *(perfmon_bundle_init_fn_t) (vlib_main_t *vm,
111                                                   struct perfmon_bundle *);
112
113 typedef struct
114 {
115   clib_cpu_supports_func_t cpu_supports;
116   perfmon_bundle_type_t bundle_type;
117 } perfmon_cpu_supports_t;
118
119 typedef struct perfmon_bundle
120 {
121   char *name;
122   char *description;
123   char *source;
124   char *footer;
125
126   union
127   {
128     perfmon_bundle_type_flag_t type_flags;
129     perfmon_bundle_type_t type;
130   };
131   perfmon_bundle_type_t active_type;
132
133   u32 events[PERF_MAX_EVENTS];
134   u32 n_events;
135
136   u16 preserve_samples;
137
138   perfmon_cpu_supports_t *cpu_supports;
139   u32 n_cpu_supports;
140
141   perfmon_bundle_init_fn_t *init_fn;
142
143   char **column_headers;
144   format_function_t *format_fn;
145
146   /* do not set manually */
147   perfmon_source_t *src;
148   struct perfmon_bundle *next;
149 } perfmon_bundle_t;
150
151 typedef struct
152 {
153   u64 nr;
154   u64 time_enabled;
155   u64 time_running;
156   u64 value[PERF_MAX_EVENTS];
157 } perfmon_reading_t;
158
159 typedef struct
160 {
161   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
162   u64 n_calls;
163   u64 n_packets;
164   union
165   {
166     struct
167     {
168       u64 value[PERF_MAX_EVENTS];
169     } t[2];
170     u64 value[PERF_MAX_EVENTS * 2];
171   };
172 } perfmon_node_stats_t;
173
174 typedef struct
175 {
176   u8 n_events;
177   u16 n_nodes;
178   perfmon_node_stats_t *node_stats;
179   perfmon_bundle_t *bundle;
180   u32 indexes[PERF_MAX_EVENTS];
181   u16 preserve_samples;
182   struct perf_event_mmap_page *mmap_pages[PERF_MAX_EVENTS];
183 } perfmon_thread_runtime_t;
184
185 typedef struct
186 {
187   perfmon_thread_runtime_t *thread_runtimes;
188   perfmon_bundle_t *bundles;
189   uword *bundle_by_name;
190   perfmon_source_t *sources;
191   uword *source_by_name;
192   perfmon_bundle_t *active_bundle;
193   int is_running;
194   f64 sample_time;
195   int *group_fds;
196   int *fds_to_close;
197   perfmon_instance_type_t *default_instance_type;
198   perfmon_instance_type_t *active_instance_type;
199 } perfmon_main_t;
200
201 extern perfmon_main_t perfmon_main;
202
203 #define PERFMON_BUNDLE_TYPE_TO_FLAGS(type)                                    \
204   ({                                                                          \
205     uword rtype = 0;                                                          \
206     if (type == PERFMON_BUNDLE_TYPE_NODE_OR_THREAD)                           \
207       rtype =                                                                 \
208         1 << PERFMON_BUNDLE_TYPE_THREAD | 1 << PERFMON_BUNDLE_TYPE_NODE;      \
209     else                                                                      \
210       rtype = 1 << type;                                                      \
211     rtype;                                                                    \
212   })
213
214 always_inline uword
215 perfmon_cpu_update_bundle_type (perfmon_bundle_t *b)
216 {
217   perfmon_cpu_supports_t *supports = b->cpu_supports;
218   uword type = 0;
219
220   /* either supports or b->type should be set, but not both */
221   ASSERT (!!supports ^ !!b->type);
222
223   /* if nothing specific for this bundle, go with the defaults */
224   if (!supports)
225     type = PERFMON_BUNDLE_TYPE_TO_FLAGS (b->type);
226   else
227     {
228       /* more than one type may be supported by a given bundle */
229       for (int i = 0; i < b->n_cpu_supports; ++i)
230         if (supports[i].cpu_supports ())
231           type |= PERFMON_BUNDLE_TYPE_TO_FLAGS (supports[i].bundle_type);
232     }
233
234   return type;
235 }
236 #undef PERFMON_BUNDLE_TYPE_TO_FLAGS
237
238 #define PERFMON_REGISTER_SOURCE(x)                                            \
239   perfmon_source_t __perfmon_source_##x;                                      \
240   static void __clib_constructor __perfmon_source_registration_##x (void)     \
241   {                                                                           \
242     perfmon_main_t *pm = &perfmon_main;                                       \
243     __perfmon_source_##x.next = pm->sources;                                  \
244     pm->sources = &__perfmon_source_##x;                                      \
245   }                                                                           \
246   perfmon_source_t __perfmon_source_##x
247
248 #define PERFMON_REGISTER_BUNDLE(x)                                            \
249   perfmon_bundle_t __perfmon_bundle_##x;                                      \
250   static void __clib_constructor __perfmon_bundle_registration_##x (void)     \
251   {                                                                           \
252     perfmon_main_t *pm = &perfmon_main;                                       \
253     __perfmon_bundle_##x.next = pm->bundles;                                  \
254     __perfmon_bundle_##x.type_flags =                                         \
255       perfmon_cpu_update_bundle_type (&__perfmon_bundle_##x);                 \
256     pm->bundles = &__perfmon_bundle_##x;                                      \
257   }                                                                           \
258   perfmon_bundle_t __perfmon_bundle_##x
259
260 void perfmon_reset (vlib_main_t *vm);
261 clib_error_t *perfmon_start (vlib_main_t *vm, perfmon_bundle_t *);
262 clib_error_t *perfmon_stop (vlib_main_t *vm);
263
264 #define PERFMON_STRINGS(...)                                                  \
265   (char *[]) { __VA_ARGS__, 0 }
266
267 #endif