perfmon: refactor perf metric support
[vpp.git] / src / plugins / perfmon / perfmon.c
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 #include <vnet/vnet.h>
17
18 #include <vlibapi/api.h>
19 #include <vlibmemory/api.h>
20 #include <vnet/plugin/plugin.h>
21 #include <vpp/app/version.h>
22 #include <linux/limits.h>
23 #include <sys/ioctl.h>
24
25 #include <perfmon/perfmon.h>
26
27 perfmon_main_t perfmon_main;
28
29 VLIB_PLUGIN_REGISTER () = {
30   .version = VPP_BUILD_VER,
31   .description = "Performance Monitor",
32 };
33
34 VLIB_REGISTER_LOG_CLASS (if_default_log, static) = {
35   .class_name = "perfmon",
36 };
37
38 #define log_debug(fmt, ...)                                                   \
39   vlib_log_debug (if_default_log.class, fmt, __VA_ARGS__)
40 #define log_warn(fmt, ...)                                                    \
41   vlib_log_warn (if_default_log.class, fmt, __VA_ARGS__)
42 #define log_err(fmt, ...) vlib_log_err (if_default_log.class, fmt, __VA_ARGS__)
43
44 void
45 perfmon_reset (vlib_main_t *vm)
46 {
47   perfmon_main_t *pm = &perfmon_main;
48   uword page_size = clib_mem_get_page_size ();
49
50   if (pm->is_running)
51     for (int i = 0; i < vlib_get_n_threads (); i++)
52       vlib_node_set_dispatch_wrapper (vlib_get_main_by_index (i), 0);
53
54   for (int i = 0; i < vec_len (pm->fds_to_close); i++)
55     close (pm->fds_to_close[i]);
56   vec_free (pm->fds_to_close);
57   vec_free (pm->group_fds);
58   if (pm->default_instance_type)
59     {
60       perfmon_instance_type_t *it = pm->default_instance_type;
61       for (int i = 0; i < vec_len (it->instances); i++)
62         vec_free (it->instances[i].name);
63       vec_free (it->instances);
64       vec_free (pm->default_instance_type);
65     }
66
67   for (int i = 0; i < vec_len (pm->thread_runtimes); i++)
68     {
69       perfmon_thread_runtime_t *tr = vec_elt_at_index (pm->thread_runtimes, i);
70       vec_free (tr->node_stats);
71       for (int j = 0; j < PERF_MAX_EVENTS; j++)
72         if (tr->mmap_pages[j])
73           munmap (tr->mmap_pages[j], page_size);
74     }
75   vec_free (pm->thread_runtimes);
76
77   pm->is_running = 0;
78   pm->active_instance_type = 0;
79   pm->active_bundle = 0;
80 }
81
82 static clib_error_t *
83 perfmon_set (vlib_main_t *vm, perfmon_bundle_t *b)
84 {
85   clib_error_t *err = 0;
86   perfmon_main_t *pm = &perfmon_main;
87   perfmon_source_t *s;
88   int is_node = 0;
89   int n_nodes = vec_len (vm->node_main.nodes);
90   uword page_size = clib_mem_get_page_size ();
91   u32 instance_type = 0;
92   perfmon_event_t *e;
93   perfmon_instance_type_t *it = 0;
94
95   perfmon_reset (vm);
96
97   s = b->src;
98   ASSERT (b->n_events);
99
100   if (b->active_type == PERFMON_BUNDLE_TYPE_NODE)
101     is_node = 1;
102
103   if (s->instances_by_type == 0)
104     {
105       vec_add2 (pm->default_instance_type, it, 1);
106       it->name = is_node ? "Thread/Node" : "Thread";
107       for (int i = 0; i < vlib_get_n_threads (); i++)
108         {
109           vlib_worker_thread_t *w = vlib_worker_threads + i;
110           perfmon_instance_t *in;
111           vec_add2 (it->instances, in, 1);
112           in->cpu = w->cpu_id;
113           in->pid = w->lwp;
114           in->name = (char *) format (0, "%s (%u)%c", w->name, i, 0);
115         }
116       if (is_node)
117         vec_validate (pm->thread_runtimes, vlib_get_n_threads () - 1);
118     }
119   else
120     {
121       e = s->events + b->events[0];
122
123       if (e->type_from_instance)
124         {
125           instance_type = e->instance_type;
126           for (int i = 1; i < b->n_events; i++)
127             {
128               e = s->events + b->events[i];
129               ASSERT (e->type_from_instance == 1 &&
130                       e->instance_type == instance_type);
131             }
132         }
133       it = vec_elt_at_index (s->instances_by_type, instance_type);
134     }
135
136   pm->active_instance_type = it;
137
138   for (int i = 0; i < vec_len (it->instances); i++)
139     {
140       perfmon_instance_t *in = vec_elt_at_index (it->instances, i);
141
142       vec_validate (pm->group_fds, i);
143       pm->group_fds[i] = -1;
144
145       for (int j = 0; j < b->n_events; j++)
146         {
147           int fd;
148           perfmon_event_t *e = s->events + b->events[j];
149           struct perf_event_attr pe = {
150             .size = sizeof (struct perf_event_attr),
151             .type = e->type_from_instance ? in->type : e->type,
152             .config = e->config,
153             .exclude_kernel = e->exclude_kernel,
154             .read_format =
155               (PERF_FORMAT_GROUP | PERF_FORMAT_TOTAL_TIME_ENABLED |
156                PERF_FORMAT_TOTAL_TIME_RUNNING),
157             .disabled = 1,
158           };
159
160           log_debug ("perf_event_open pe.type=%u pe.config=0x%x pid=%d "
161                      "cpu=%d group_fd=%d",
162                      pe.type, pe.config, in->pid, in->cpu, pm->group_fds[i]);
163           fd = syscall (__NR_perf_event_open, &pe, in->pid, in->cpu,
164                         pm->group_fds[i], 0);
165
166           if (fd == -1)
167             {
168               err = clib_error_return_unix (0, "perf_event_open");
169               goto error;
170             }
171
172           vec_add1 (pm->fds_to_close, fd);
173
174           if (pm->group_fds[i] == -1)
175             pm->group_fds[i] = fd;
176
177           if (is_node)
178             {
179               perfmon_thread_runtime_t *tr;
180               tr = vec_elt_at_index (pm->thread_runtimes, i);
181               tr->mmap_pages[j] =
182                 mmap (0, page_size, PROT_READ, MAP_SHARED, fd, 0);
183
184               if (tr->mmap_pages[j] == MAP_FAILED)
185                 {
186                   err = clib_error_return_unix (0, "mmap");
187                   goto error;
188                 }
189             }
190         }
191
192       if (is_node)
193         {
194           perfmon_thread_runtime_t *rt;
195           rt = vec_elt_at_index (pm->thread_runtimes, i);
196           rt->bundle = b;
197           rt->n_events = b->n_events;
198           rt->n_nodes = n_nodes;
199           rt->preserve_samples = b->preserve_samples;
200           vec_validate_aligned (rt->node_stats, n_nodes - 1,
201                                 CLIB_CACHE_LINE_BYTES);
202         }
203     }
204
205   pm->active_bundle = b;
206
207 error:
208   if (err)
209     {
210       log_err ("%U", format_clib_error, err);
211       perfmon_reset (vm);
212     }
213   return err;
214 }
215
216 static_always_inline u32
217 perfmon_mmap_read_index (const struct perf_event_mmap_page *mmap_page)
218 {
219   u32 idx;
220   u32 seq;
221
222   /* See documentation in /usr/include/linux/perf_event.h, for more details
223    * but the 2 main important things are:
224    *  1) if seq != mmap_page->lock, it means the kernel is currently updating
225    *     the user page and we need to read it again
226    *  2) if idx == 0, it means the perf event is currently turned off and we
227    *     just need to read the kernel-updated 'offset', otherwise we must also
228    *     add the current hw value (hence rdmpc) */
229   do
230     {
231       seq = mmap_page->lock;
232       CLIB_COMPILER_BARRIER ();
233
234       idx = mmap_page->index;
235
236       CLIB_COMPILER_BARRIER ();
237     }
238   while (mmap_page->lock != seq);
239
240   return idx;
241 }
242
243 clib_error_t *
244 perfmon_start (vlib_main_t *vm, perfmon_bundle_t *b)
245 {
246   clib_error_t *err = 0;
247   perfmon_main_t *pm = &perfmon_main;
248   int n_groups;
249
250   if (pm->is_running == 1)
251     return clib_error_return (0, "already running");
252
253   if ((err = perfmon_set (vm, b)) != 0)
254     return err;
255
256   n_groups = vec_len (pm->group_fds);
257
258   for (int i = 0; i < n_groups; i++)
259     {
260       if (ioctl (pm->group_fds[i], PERF_EVENT_IOC_ENABLE,
261                  PERF_IOC_FLAG_GROUP) == -1)
262         {
263           perfmon_reset (vm);
264           return clib_error_return_unix (0, "ioctl(PERF_EVENT_IOC_ENABLE)");
265         }
266     }
267   if (b->active_type == PERFMON_BUNDLE_TYPE_NODE)
268     {
269       for (int i = 0; i < vec_len (pm->thread_runtimes); i++)
270         {
271           perfmon_thread_runtime_t *tr;
272           tr = vec_elt_at_index (pm->thread_runtimes, i);
273
274           for (int j = 0; j < b->n_events; j++)
275             {
276               tr->indexes[j] = perfmon_mmap_read_index (tr->mmap_pages[j]);
277
278               /* if a zero index is returned generate error */
279               if (!tr->indexes[j])
280                 {
281                   perfmon_reset (vm);
282                   return clib_error_return (0, "invalid rdpmc index");
283                 }
284             }
285         }
286
287       for (int i = 0; i < vlib_get_n_threads (); i++)
288         vlib_node_set_dispatch_wrapper (vlib_get_main_by_index (i),
289                                         perfmon_dispatch_wrapper);
290     }
291   pm->sample_time = vlib_time_now (vm);
292   pm->is_running = 1;
293
294   return 0;
295 }
296
297 clib_error_t *
298 perfmon_stop (vlib_main_t *vm)
299 {
300   perfmon_main_t *pm = &perfmon_main;
301   int n_groups = vec_len (pm->group_fds);
302
303   if (pm->is_running != 1)
304     return clib_error_return (0, "not running");
305
306   if (pm->active_bundle->active_type == PERFMON_BUNDLE_TYPE_NODE)
307     {
308       for (int i = 0; i < vlib_get_n_threads (); i++)
309         vlib_node_set_dispatch_wrapper (vlib_get_main_by_index (i), 0);
310     }
311
312   for (int i = 0; i < n_groups; i++)
313     {
314       if (ioctl (pm->group_fds[i], PERF_EVENT_IOC_DISABLE,
315                  PERF_IOC_FLAG_GROUP) == -1)
316         {
317           perfmon_reset (vm);
318           return clib_error_return_unix (0, "ioctl(PERF_EVENT_IOC_DISABLE)");
319         }
320     }
321
322   pm->is_running = 0;
323   pm->sample_time = vlib_time_now (vm) - pm->sample_time;
324   return 0;
325 }
326
327 static_always_inline u8
328 is_bundle_supported (perfmon_bundle_t *b)
329 {
330   perfmon_cpu_supports_t *supports = b->cpu_supports;
331
332   if (!b->cpu_supports)
333     return 1;
334
335   for (int i = 0; i < b->n_cpu_supports; ++i)
336     if (supports[i].cpu_supports ())
337       return 1;
338
339   return 0;
340 }
341
342 static clib_error_t *
343 perfmon_init (vlib_main_t *vm)
344 {
345   perfmon_main_t *pm = &perfmon_main;
346   perfmon_source_t *s = pm->sources;
347   perfmon_bundle_t *b = pm->bundles;
348
349   pm->source_by_name = hash_create_string (0, sizeof (uword));
350   while (s)
351     {
352       clib_error_t *err;
353       if (hash_get_mem (pm->source_by_name, s->name) != 0)
354         clib_panic ("duplicate source name '%s'", s->name);
355       if (s->init_fn && ((err = (s->init_fn) (vm, s))))
356         {
357           log_warn ("skipping source '%s' - %U", s->name, format_clib_error,
358                     err);
359           clib_error_free (err);
360           s = s->next;
361           continue;
362         }
363
364       hash_set_mem (pm->source_by_name, s->name, s);
365       log_debug ("source '%s' regisrtered", s->name);
366       s = s->next;
367     }
368
369   pm->bundle_by_name = hash_create_string (0, sizeof (uword));
370   while (b)
371     {
372       clib_error_t *err;
373       uword *p;
374
375       if (!is_bundle_supported (b))
376         {
377           log_warn ("skipping bundle '%s' - not supported", b->name);
378           b = b->next;
379           continue;
380         }
381
382       if (hash_get_mem (pm->bundle_by_name, b->name) != 0)
383         clib_panic ("duplicate bundle name '%s'", b->name);
384
385       if ((p = hash_get_mem (pm->source_by_name, b->source)) == 0)
386         {
387           log_debug ("missing source '%s', skipping bundle '%s'", b->source,
388                      b->name);
389           b = b->next;
390           continue;
391         }
392
393       b->src = (perfmon_source_t *) p[0];
394       if (b->init_fn && ((err = (b->init_fn) (vm, b))))
395         {
396           log_warn ("skipping bundle '%s' - %U", b->name, format_clib_error,
397                     err);
398           clib_error_free (err);
399           b = b->next;
400           continue;
401         }
402
403       hash_set_mem (pm->bundle_by_name, b->name, b);
404       log_debug ("bundle '%s' regisrtered", b->name);
405
406       b = b->next;
407     }
408
409   return 0;
410 }
411
412 VLIB_INIT_FUNCTION (perfmon_init);