perfmon: add support for raw and timestamps
[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   char **raw_column_headers;
97   format_function_t *format_fn;
98
99   /* do not set manually */
100   perfmon_source_t *src;
101   struct perfmon_bundle *next;
102 } perfmon_bundle_t;
103
104 typedef struct
105 {
106   u64 nr;
107   u64 time_enabled;
108   u64 time_running;
109   u64 value[PERF_MAX_EVENTS];
110 } perfmon_reading_t;
111
112 typedef struct
113 {
114   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
115   u64 n_calls;
116   u64 n_packets;
117   u64 value[PERF_MAX_EVENTS];
118 } perfmon_node_stats_t;
119
120 typedef struct
121 {
122   u8 n_events;
123   u16 n_nodes;
124   perfmon_node_stats_t *node_stats;
125   struct perf_event_mmap_page *mmap_pages[PERF_MAX_EVENTS];
126 } perfmon_thread_runtime_t;
127
128 typedef struct
129 {
130   perfmon_thread_runtime_t *thread_runtimes;
131   perfmon_bundle_t *bundles;
132   uword *bundle_by_name;
133   perfmon_source_t *sources;
134   uword *source_by_name;
135   perfmon_bundle_t *active_bundle;
136   int is_running;
137   f64 sample_time;
138   int *group_fds;
139   int *fds_to_close;
140   perfmon_instance_type_t *default_instance_type;
141   perfmon_instance_type_t *active_instance_type;
142 } perfmon_main_t;
143
144 extern perfmon_main_t perfmon_main;
145
146 #define PERFMON_REGISTER_SOURCE(x)                                            \
147   perfmon_source_t __perfmon_source_##x;                                      \
148   static void __clib_constructor __perfmon_source_registration_##x (void)     \
149   {                                                                           \
150     perfmon_main_t *pm = &perfmon_main;                                       \
151     __perfmon_source_##x.next = pm->sources;                                  \
152     pm->sources = &__perfmon_source_##x;                                      \
153   }                                                                           \
154   perfmon_source_t __perfmon_source_##x
155
156 #define PERFMON_REGISTER_BUNDLE(x)                                            \
157   perfmon_bundle_t __perfmon_bundle_##x;                                      \
158   static void __clib_constructor __perfmon_bundle_registration_##x (void)     \
159   {                                                                           \
160     perfmon_main_t *pm = &perfmon_main;                                       \
161     __perfmon_bundle_##x.next = pm->bundles;                                  \
162     pm->bundles = &__perfmon_bundle_##x;                                      \
163   }                                                                           \
164   perfmon_bundle_t __perfmon_bundle_##x
165
166 void perfmon_reset (vlib_main_t *vm);
167 clib_error_t *perfmon_set (vlib_main_t *vm, perfmon_bundle_t *);
168 clib_error_t *perfmon_start (vlib_main_t *vm);
169 clib_error_t *perfmon_stop (vlib_main_t *vm);
170
171 #define PERFMON_STRINGS(...)                                                  \
172   (char *[]) { __VA_ARGS__, 0 }
173
174 #endif