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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef __perfmon_perfmon_h
17 #define __perfmon_perfmon_h
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>
26 #if defined(__x86_64__)
27 #define PERF_MAX_EVENTS 12 /* 4 fixed and 8 programable on ICX */
28 #elif defined(__aarch64__)
29 #define PERF_MAX_EVENTS 7 /* 6 events + 1 CPU cycle counter */
34 PERFMON_EVENT_TYPE_GENERAL,
35 PERFMON_EVENT_TYPE_FIXED,
36 PERFMON_EVENT_TYPE_PSEUDO,
37 PERFMON_EVENT_TYPE_MAX,
38 } perfmon_event_type_t;
42 PERFMON_BUNDLE_TYPE_UNKNOWN,
43 PERFMON_BUNDLE_TYPE_NODE,
44 PERFMON_BUNDLE_TYPE_THREAD,
45 PERFMON_BUNDLE_TYPE_SYSTEM,
46 PERFMON_BUNDLE_TYPE_MAX,
47 PERFMON_BUNDLE_TYPE_NODE_OR_THREAD,
48 } perfmon_bundle_type_t;
50 #define foreach_perfmon_bundle_type \
51 _ (PERFMON_BUNDLE_TYPE_UNKNOWN, "not supported") \
52 _ (PERFMON_BUNDLE_TYPE_NODE, "node") \
53 _ (PERFMON_BUNDLE_TYPE_THREAD, "thread") \
54 _ (PERFMON_BUNDLE_TYPE_SYSTEM, "system")
58 #define _(e, str) e##_FLAG = 1 << e,
59 foreach_perfmon_bundle_type
62 } perfmon_bundle_type_flag_t;
66 u32 type_from_instance : 1;
67 u32 exclude_kernel : 1;
91 perfmon_instance_t *instances;
92 } perfmon_instance_type_t;
94 struct perfmon_source;
95 typedef struct perfmon_bundle perfmon_bundle_t;
97 typedef clib_error_t *(perfmon_source_init_fn_t) (vlib_main_t *vm,
98 struct perfmon_source *);
99 typedef perfmon_event_type_t (perfmon_source_get_event_type) (u32 event);
100 typedef u8 (perfmon_source_bundle_support_t) (perfmon_bundle_t *);
101 typedef clib_error_t *(perfmon_source_config_dispatch_wrapper_t) (
102 perfmon_bundle_t *b, vlib_node_function_t **dispatch_wrapper);
104 typedef struct perfmon_source
108 struct perfmon_source *next;
109 perfmon_event_t *events;
111 perfmon_instance_type_t *instances_by_type;
112 format_function_t *format_config;
113 perfmon_source_get_event_type *get_event_type;
114 perfmon_source_init_fn_t *init_fn;
115 perfmon_source_bundle_support_t *bundle_support;
116 perfmon_source_config_dispatch_wrapper_t *config_dispatch_wrapper;
119 typedef clib_error_t *(perfmon_bundle_init_fn_t) (vlib_main_t *vm,
120 struct perfmon_bundle *);
124 clib_cpu_supports_func_t cpu_supports;
125 perfmon_bundle_type_t bundle_type;
126 } perfmon_cpu_supports_t;
128 typedef struct perfmon_bundle
137 perfmon_bundle_type_flag_t type_flags;
138 perfmon_bundle_type_t type;
140 perfmon_bundle_type_t active_type;
142 u32 events[PERF_MAX_EVENTS];
146 uword *event_disabled;
147 uword *column_disabled;
150 u16 preserve_samples;
152 perfmon_cpu_supports_t *cpu_supports;
155 perfmon_bundle_init_fn_t *init_fn;
157 char **column_headers;
158 format_function_t *format_fn;
160 /* do not set manually */
161 perfmon_source_t *src;
162 struct perfmon_bundle *next;
170 u64 value[PERF_MAX_EVENTS];
175 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
182 u64 value[PERF_MAX_EVENTS];
184 u64 value[PERF_MAX_EVENTS * 2];
186 } perfmon_node_stats_t;
192 perfmon_node_stats_t *node_stats;
193 perfmon_bundle_t *bundle;
194 u32 indexes[PERF_MAX_EVENTS];
195 u16 preserve_samples;
196 struct perf_event_mmap_page *mmap_pages[PERF_MAX_EVENTS];
197 } perfmon_thread_runtime_t;
201 perfmon_thread_runtime_t *thread_runtimes;
202 perfmon_bundle_t *bundles;
203 uword *bundle_by_name;
204 perfmon_source_t *sources;
205 uword *source_by_name;
206 perfmon_bundle_t *active_bundle;
211 perfmon_instance_type_t *default_instance_type;
212 perfmon_instance_type_t *active_instance_type;
215 extern perfmon_main_t perfmon_main;
217 #define PERFMON_BUNDLE_TYPE_TO_FLAGS(type) \
220 if (type == PERFMON_BUNDLE_TYPE_NODE_OR_THREAD) \
222 1 << PERFMON_BUNDLE_TYPE_THREAD | 1 << PERFMON_BUNDLE_TYPE_NODE; \
229 perfmon_cpu_update_bundle_type (perfmon_bundle_t *b)
231 perfmon_cpu_supports_t *supports = b->cpu_supports;
234 /* either supports or b->type should be set, but not both */
235 ASSERT (!!supports ^ !!b->type);
237 /* if nothing specific for this bundle, go with the defaults */
239 type = PERFMON_BUNDLE_TYPE_TO_FLAGS (b->type);
242 /* more than one type may be supported by a given bundle */
243 for (int i = 0; i < b->n_cpu_supports; ++i)
244 if (supports[i].cpu_supports ())
245 type |= PERFMON_BUNDLE_TYPE_TO_FLAGS (supports[i].bundle_type);
250 #undef PERFMON_BUNDLE_TYPE_TO_FLAGS
252 #define PERFMON_REGISTER_SOURCE(x) \
253 perfmon_source_t __perfmon_source_##x; \
254 static void __clib_constructor __perfmon_source_registration_##x (void) \
256 perfmon_main_t *pm = &perfmon_main; \
257 __perfmon_source_##x.next = pm->sources; \
258 pm->sources = &__perfmon_source_##x; \
260 perfmon_source_t __perfmon_source_##x
262 #define PERFMON_REGISTER_BUNDLE(x) \
263 perfmon_bundle_t __perfmon_bundle_##x; \
264 static void __clib_constructor __perfmon_bundle_registration_##x (void) \
266 perfmon_main_t *pm = &perfmon_main; \
267 __perfmon_bundle_##x.next = pm->bundles; \
268 __perfmon_bundle_##x.type_flags = \
269 perfmon_cpu_update_bundle_type (&__perfmon_bundle_##x); \
270 pm->bundles = &__perfmon_bundle_##x; \
272 perfmon_bundle_t __perfmon_bundle_##x
274 void perfmon_reset (vlib_main_t *vm);
275 clib_error_t *perfmon_start (vlib_main_t *vm, perfmon_bundle_t *);
276 clib_error_t *perfmon_stop (vlib_main_t *vm);
278 #define PERFMON_STRINGS(...) \
279 (char *[]) { __VA_ARGS__, 0 }
281 #define PERFMON_COLUMN_EVENTS(...) \
282 (u8[]) { __VA_ARGS__ }