perfmon: compile dispatch wrapper once for each number of counters
[vpp.git] / src / plugins / perfmon / dispatch_wrapper.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 "vppinfra/string.h"
17 #include <vnet/vnet.h>
18
19 #include <vlibapi/api.h>
20 #include <vlibmemory/api.h>
21 #include <vnet/plugin/plugin.h>
22 #include <vpp/app/version.h>
23 #include <linux/limits.h>
24 #include <sys/ioctl.h>
25
26 #include <perfmon/perfmon.h>
27
28 static_always_inline void
29 perfmon_read_pmcs (u64 *counters, u32 *indexes, u8 n_counters)
30 {
31   for (int i = 0; i < n_counters; i++)
32     counters[i] = _rdpmc (indexes[i] - 1);
33 }
34
35 static_always_inline uword
36 perfmon_dispatch_wrapper_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
37                                  vlib_frame_t *frame, u8 n_events)
38 {
39   perfmon_main_t *pm = &perfmon_main;
40   perfmon_thread_runtime_t *rt =
41     vec_elt_at_index (pm->thread_runtimes, vm->thread_index);
42   perfmon_node_stats_t *s =
43     vec_elt_at_index (rt->node_stats, node->node_index);
44
45   struct
46   {
47     u64 t[2][PERF_MAX_EVENTS];
48   } samples;
49   uword rv;
50
51   clib_prefetch_load (s);
52
53   perfmon_read_pmcs (&samples.t[0][0], &rt->indexes[0], n_events);
54   rv = node->function (vm, node, frame);
55   perfmon_read_pmcs (&samples.t[1][0], &rt->indexes[0], n_events);
56
57   if (rv == 0)
58     return rv;
59
60   s->n_calls += 1;
61   s->n_packets += rv;
62
63   for (int i = 0; i < n_events; i++)
64     {
65       if (!(rt->preserve_samples & 1 << i))
66         {
67           s->value[i] += samples.t[1][i] - samples.t[0][i];
68         }
69       else
70         {
71           s->t[0].value[i] = samples.t[0][i];
72           s->t[1].value[i] = samples.t[1][i];
73         }
74     }
75
76   return rv;
77 }
78
79 #define foreach_n_events                                                      \
80   _ (1) _ (2) _ (3) _ (4) _ (5) _ (6) _ (7) _ (8) _ (9) _ (10) _ (11) _ (12)
81
82 #define _(x)                                                                  \
83   static uword perfmon_dispatch_wrapper##x (                                  \
84     vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)          \
85   {                                                                           \
86     return perfmon_dispatch_wrapper_inline (vm, node, frame, x);              \
87   }
88
89 foreach_n_events
90 #undef _
91
92   vlib_node_function_t *perfmon_dispatch_wrappers[PERF_MAX_EVENTS + 1] = {
93 #define _(x) [x] = &perfmon_dispatch_wrapper##x,
94     foreach_n_events
95 #undef _
96   };