perfmon: new perfmon plugin
[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 <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 static_always_inline void
28 perfmon_read_pmcs (u64 *counters, int *pmc_index, u8 n_counters)
29 {
30   switch (n_counters)
31     {
32     default:
33     case 7:
34       counters[6] = _rdpmc (pmc_index[6]);
35     case 6:
36       counters[5] = _rdpmc (pmc_index[5]);
37     case 5:
38       counters[4] = _rdpmc (pmc_index[4]);
39     case 4:
40       counters[3] = _rdpmc (pmc_index[3]);
41     case 3:
42       counters[2] = _rdpmc (pmc_index[2]);
43     case 2:
44       counters[1] = _rdpmc (pmc_index[1]);
45     case 1:
46       counters[0] = _rdpmc (pmc_index[0]);
47       break;
48     }
49 }
50
51 static_always_inline int
52 perfmon_calc_pmc_index (perfmon_thread_runtime_t *tr, u8 i)
53 {
54   return (int) (tr->mmap_pages[i]->index + tr->mmap_pages[i]->offset);
55 }
56
57 uword
58 perfmon_dispatch_wrapper (vlib_main_t *vm, vlib_node_runtime_t *node,
59                           vlib_frame_t *frame)
60 {
61   perfmon_main_t *pm = &perfmon_main;
62   perfmon_thread_runtime_t *rt =
63     vec_elt_at_index (pm->thread_runtimes, vm->thread_index);
64   perfmon_node_stats_t *s =
65     vec_elt_at_index (rt->node_stats, node->node_index);
66   u8 n_events = rt->n_events;
67   int pmc_index[PERF_MAX_EVENTS];
68   u64 before[PERF_MAX_EVENTS];
69   u64 after[PERF_MAX_EVENTS];
70   uword rv;
71
72   clib_prefetch_load (s);
73
74   switch (n_events)
75     {
76     default:
77     case 7:
78       pmc_index[6] = perfmon_calc_pmc_index (rt, 6);
79     case 6:
80       pmc_index[5] = perfmon_calc_pmc_index (rt, 5);
81     case 5:
82       pmc_index[4] = perfmon_calc_pmc_index (rt, 4);
83     case 4:
84       pmc_index[3] = perfmon_calc_pmc_index (rt, 3);
85     case 3:
86       pmc_index[2] = perfmon_calc_pmc_index (rt, 2);
87     case 2:
88       pmc_index[1] = perfmon_calc_pmc_index (rt, 1);
89     case 1:
90       pmc_index[0] = perfmon_calc_pmc_index (rt, 0);
91       break;
92     }
93
94   perfmon_read_pmcs (before, pmc_index, n_events);
95   rv = node->function (vm, node, frame);
96   perfmon_read_pmcs (after, pmc_index, n_events);
97
98   if (rv == 0)
99     return rv;
100
101   s->n_calls += 1;
102   s->n_packets += rv;
103   for (int i = 0; i < n_events; i++)
104     s->value[i] += after[i] - before[i];
105
106   return rv;
107 }