perfmon: new perfmon plugin
[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 <vlib/vlib.h>
24
25 #define PERF_MAX_EVENTS 7 /* 3 fixed and 4 programmable */
26
27 typedef enum
28 {
29   PERFMON_BUNDLE_TYPE_UNKNOWN,
30   PERFMON_BUNDLE_TYPE_NODE,
31   PERFMON_BUNDLE_TYPE_THREAD,
32   PERFMON_BUNDLE_TYPE_SYSTEM,
33 } perfmon_bundle_type_t;
34
35 typedef struct
36 {
37   u32 type_from_instance : 1;
38   u32 exclude_kernel : 1;
39   union
40   {
41     u32 type;
42     u32 instance_type;
43   };
44   u64 config;
45   char *name;
46   char *description;
47 } perfmon_event_t;
48
49 typedef struct
50 {
51   u32 type;
52   int cpu;
53   pid_t pid;
54   char *name;
55 } perfmon_instance_t;
56
57 typedef struct
58 {
59   char *name;
60   perfmon_instance_t *instances;
61 } perfmon_instance_type_t;
62
63 struct perfmon_source;
64 vlib_node_function_t perfmon_dispatch_wrapper;
65
66 typedef clib_error_t *(perfmon_source_init_fn_t) (vlib_main_t *vm,
67                                                   struct perfmon_source *);
68 typedef struct perfmon_source
69 {
70   char *name;
71   char *description;
72   struct perfmon_source *next;
73   perfmon_event_t *events;
74   u32 n_events;
75   perfmon_instance_type_t *instances_by_type;
76   format_function_t *format_config;
77   perfmon_source_init_fn_t *init_fn;
78 } perfmon_source_t;
79
80 struct perfmon_bundle;
81 typedef clib_error_t *(perfmon_bundle_init_fn_t) (vlib_main_t *vm,
82                                                   struct perfmon_bundle *);
83 typedef struct perfmon_bundle
84 {
85   char *name;
86   char *description;
87   char *source;
88   char *footer;
89   perfmon_bundle_type_t type;
90   u32 events[PERF_MAX_EVENTS];
91   u32 n_events;
92
93   perfmon_bundle_init_fn_t *init_fn;
94
95   char **column_headers;
96   format_function_t *format_fn;
97
98   /* do not set manually */
99   perfmon_source_t *src;
100   struct perfmon_bundle *next;
101 } perfmon_bundle_t;
102
103 typedef struct
104 {
105   u64 nr;
106   u64 time_enabled;
107   u64 time_running;
108   u64 value[PERF_MAX_EVENTS];
109 } perfmon_reading_t;
110
111 typedef struct
112 {
113   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
114   u64 n_calls;
115   u64 n_packets;
116   u64 value[PERF_MAX_EVENTS];
117 } perfmon_node_stats_t;
118
119 typedef struct
120 {
121   u8 n_events;
122   u16 n_nodes;
123   perfmon_node_stats_t *node_stats;
124   struct perf_event_mmap_page *mmap_pages[PERF_MAX_EVENTS];
125 } perfmon_thread_runtime_t;
126
127 typedef struct
128 {
129   perfmon_thread_runtime_t *thread_runtimes;
130   perfmon_bundle_t *bundles;
131   uword *bundle_by_name;
132   perfmon_source_t *sources;
133   uword *source_by_name;
134   perfmon_bundle_t *active_bundle;
135   int is_running;
136   int *group_fds;
137   int *fds_to_close;
138   perfmon_instance_type_t *default_instance_type;
139   perfmon_instance_type_t *active_instance_type;
140 } perfmon_main_t;
141
142 extern perfmon_main_t perfmon_main;
143
144 #define PERFMON_REGISTER_SOURCE(x)                                            \
145   perfmon_source_t __perfmon_source_##x;                                      \
146   static void __clib_constructor __perfmon_source_registration_##x (void)     \
147   {                                                                           \
148     perfmon_main_t *pm = &perfmon_main;                                       \
149     __perfmon_source_##x.next = pm->sources;                                  \
150     pm->sources = &__perfmon_source_##x;                                      \
151   }                                                                           \
152   perfmon_source_t __perfmon_source_##x
153
154 #define PERFMON_REGISTER_BUNDLE(x)                                            \
155   perfmon_bundle_t __perfmon_bundle_##x;                                      \
156   static void __clib_constructor __perfmon_bundle_registration_##x (void)     \
157   {                                                                           \
158     perfmon_main_t *pm = &perfmon_main;                                       \
159     __perfmon_bundle_##x.next = pm->bundles;                                  \
160     pm->bundles = &__perfmon_bundle_##x;                                      \
161   }                                                                           \
162   perfmon_bundle_t __perfmon_bundle_##x
163
164 void perfmon_reset (vlib_main_t *vm);
165 clib_error_t *perfmon_set (vlib_main_t *vm, perfmon_bundle_t *);
166 clib_error_t *perfmon_start (vlib_main_t *vm);
167 clib_error_t *perfmon_stop (vlib_main_t *vm);
168
169 #define PERFMON_STRINGS(...)                                                  \
170   (char *[]) { __VA_ARGS__, 0 }
171
172 #endif