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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "vppinfra/string.h"
17 #include <vnet/vnet.h>
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>
26 #include <perfmon/perfmon.h>
28 static_always_inline u64
29 perfmon_mmap_read_pmc1 (const struct perf_event_mmap_page *mmap_page)
34 /* See documentation in /usr/include/linux/perf_event.h, for more details
35 * but the 2 main important things are:
36 * 1) if seq != mmap_page->lock, it means the kernel is currently updating
37 * the user page and we need to read it again
38 * 2) if idx == 0, it means the perf event is currently turned off and we
39 * just need to read the kernel-updated 'offset', otherwise we must also
40 * add the current hw value (hence rdmpc) */
45 seq = mmap_page->lock;
46 CLIB_COMPILER_BARRIER ();
48 idx = mmap_page->index;
49 count = mmap_page->offset;
51 count += _rdpmc (idx - 1);
53 CLIB_COMPILER_BARRIER ();
55 while (mmap_page->lock != seq);
60 static_always_inline void
61 perfmon_mmap_read_pmcs (u64 *counters,
62 struct perf_event_mmap_page **mmap_pages,
69 counters[11] = perfmon_mmap_read_pmc1 (mmap_pages[11]);
71 counters[10] = perfmon_mmap_read_pmc1 (mmap_pages[10]);
73 counters[9] = perfmon_mmap_read_pmc1 (mmap_pages[9]);
75 counters[8] = perfmon_mmap_read_pmc1 (mmap_pages[8]);
77 counters[7] = perfmon_mmap_read_pmc1 (mmap_pages[7]);
79 counters[6] = perfmon_mmap_read_pmc1 (mmap_pages[6]);
81 counters[5] = perfmon_mmap_read_pmc1 (mmap_pages[5]);
83 counters[4] = perfmon_mmap_read_pmc1 (mmap_pages[4]);
85 counters[3] = perfmon_mmap_read_pmc1 (mmap_pages[3]);
87 counters[2] = perfmon_mmap_read_pmc1 (mmap_pages[2]);
89 counters[1] = perfmon_mmap_read_pmc1 (mmap_pages[1]);
91 counters[0] = perfmon_mmap_read_pmc1 (mmap_pages[0]);
97 perfmon_dispatch_wrapper_mmap (vlib_main_t *vm, vlib_node_runtime_t *node,
100 perfmon_main_t *pm = &perfmon_main;
101 perfmon_thread_runtime_t *rt =
102 vec_elt_at_index (pm->thread_runtimes, vm->thread_index);
103 perfmon_node_stats_t *s =
104 vec_elt_at_index (rt->node_stats, node->node_index);
106 u8 n_events = rt->n_events;
108 u64 before[PERF_MAX_EVENTS];
109 u64 after[PERF_MAX_EVENTS];
112 clib_prefetch_load (s);
114 perfmon_mmap_read_pmcs (&before[0], rt->mmap_pages, n_events);
115 rv = node->function (vm, node, frame);
116 perfmon_mmap_read_pmcs (&after[0], rt->mmap_pages, n_events);
124 for (int i = 0; i < n_events; i++)
125 s->value[i] += after[i] - before[i];
130 static_always_inline void
131 perfmon_metric_read_pmcs (u64 *counters, int *pmc_index, u8 n_counters)
137 counters[11] = _rdpmc (pmc_index[11]);
139 counters[10] = _rdpmc (pmc_index[10]);
141 counters[9] = _rdpmc (pmc_index[9]);
143 counters[8] = _rdpmc (pmc_index[8]);
145 counters[7] = _rdpmc (pmc_index[7]);
147 counters[6] = _rdpmc (pmc_index[6]);
149 counters[5] = _rdpmc (pmc_index[5]);
151 counters[4] = _rdpmc (pmc_index[4]);
153 counters[3] = _rdpmc (pmc_index[3]);
155 counters[2] = _rdpmc (pmc_index[2]);
157 counters[1] = _rdpmc (pmc_index[1]);
159 counters[0] = _rdpmc (pmc_index[0]);
164 static_always_inline int
165 perfmon_metric_index (perfmon_bundle_t *b, u8 i)
167 return (int) (b->metrics[i]);
171 perfmon_dispatch_wrapper_metrics (vlib_main_t *vm, vlib_node_runtime_t *node,
174 perfmon_main_t *pm = &perfmon_main;
175 perfmon_thread_runtime_t *rt =
176 vec_elt_at_index (pm->thread_runtimes, vm->thread_index);
177 perfmon_node_stats_t *s =
178 vec_elt_at_index (rt->node_stats, node->node_index);
180 u8 n_events = rt->n_events;
182 u64 before[PERF_MAX_EVENTS];
183 int pmc_index[PERF_MAX_EVENTS];
186 clib_prefetch_load (s);
192 pmc_index[11] = perfmon_metric_index (rt->bundle, 11);
194 pmc_index[10] = perfmon_metric_index (rt->bundle, 10);
196 pmc_index[9] = perfmon_metric_index (rt->bundle, 9);
198 pmc_index[8] = perfmon_metric_index (rt->bundle, 8);
200 pmc_index[7] = perfmon_metric_index (rt->bundle, 7);
202 pmc_index[6] = perfmon_metric_index (rt->bundle, 6);
204 pmc_index[5] = perfmon_metric_index (rt->bundle, 5);
206 pmc_index[4] = perfmon_metric_index (rt->bundle, 4);
208 pmc_index[3] = perfmon_metric_index (rt->bundle, 3);
210 pmc_index[2] = perfmon_metric_index (rt->bundle, 2);
212 pmc_index[1] = perfmon_metric_index (rt->bundle, 1);
214 pmc_index[0] = perfmon_metric_index (rt->bundle, 0);
218 perfmon_metric_read_pmcs (&before[0], pmc_index, n_events);
219 rv = node->function (vm, node, frame);
221 clib_memcpy_fast (&s->t[0].value[0], &before, sizeof (before));
222 perfmon_metric_read_pmcs (&s->t[1].value[0], pmc_index, n_events);