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 #define PERF_MAX_EVENTS 12 /* 4 fixed and 8 programable on ICX */
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;
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;
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")
54 #define _(e, str) e##_FLAG = 1 << e,
55 foreach_perfmon_bundle_type
58 } perfmon_bundle_type_flag_t;
62 u32 type_from_instance : 1;
63 u32 exclude_kernel : 1;
85 perfmon_instance_t *instances;
86 } perfmon_instance_type_t;
88 struct perfmon_source;
89 extern vlib_node_function_t *perfmon_dispatch_wrappers[PERF_MAX_EVENTS + 1];
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);
95 typedef struct perfmon_source
99 struct perfmon_source *next;
100 perfmon_event_t *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;
108 struct perfmon_bundle;
110 typedef clib_error_t *(perfmon_bundle_init_fn_t) (vlib_main_t *vm,
111 struct perfmon_bundle *);
115 clib_cpu_supports_func_t cpu_supports;
116 perfmon_bundle_type_t bundle_type;
117 } perfmon_cpu_supports_t;
119 typedef struct perfmon_bundle
128 perfmon_bundle_type_flag_t type_flags;
129 perfmon_bundle_type_t type;
131 perfmon_bundle_type_t active_type;
133 u32 events[PERF_MAX_EVENTS];
136 u16 preserve_samples;
138 perfmon_cpu_supports_t *cpu_supports;
141 perfmon_bundle_init_fn_t *init_fn;
143 char **column_headers;
144 format_function_t *format_fn;
146 /* do not set manually */
147 perfmon_source_t *src;
148 struct perfmon_bundle *next;
156 u64 value[PERF_MAX_EVENTS];
161 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
168 u64 value[PERF_MAX_EVENTS];
170 u64 value[PERF_MAX_EVENTS * 2];
172 } perfmon_node_stats_t;
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;
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;
197 perfmon_instance_type_t *default_instance_type;
198 perfmon_instance_type_t *active_instance_type;
201 extern perfmon_main_t perfmon_main;
203 #define PERFMON_BUNDLE_TYPE_TO_FLAGS(type) \
206 if (type == PERFMON_BUNDLE_TYPE_NODE_OR_THREAD) \
208 1 << PERFMON_BUNDLE_TYPE_THREAD | 1 << PERFMON_BUNDLE_TYPE_NODE; \
215 perfmon_cpu_update_bundle_type (perfmon_bundle_t *b)
217 perfmon_cpu_supports_t *supports = b->cpu_supports;
220 /* either supports or b->type should be set, but not both */
221 ASSERT (!!supports ^ !!b->type);
223 /* if nothing specific for this bundle, go with the defaults */
225 type = PERFMON_BUNDLE_TYPE_TO_FLAGS (b->type);
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);
236 #undef PERFMON_BUNDLE_TYPE_TO_FLAGS
238 #define PERFMON_REGISTER_SOURCE(x) \
239 perfmon_source_t __perfmon_source_##x; \
240 static void __clib_constructor __perfmon_source_registration_##x (void) \
242 perfmon_main_t *pm = &perfmon_main; \
243 __perfmon_source_##x.next = pm->sources; \
244 pm->sources = &__perfmon_source_##x; \
246 perfmon_source_t __perfmon_source_##x
248 #define PERFMON_REGISTER_BUNDLE(x) \
249 perfmon_bundle_t __perfmon_bundle_##x; \
250 static void __clib_constructor __perfmon_bundle_registration_##x (void) \
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; \
258 perfmon_bundle_t __perfmon_bundle_##x
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);
264 #define PERFMON_STRINGS(...) \
265 (char *[]) { __VA_ARGS__, 0 }