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 <vnet/vnet.h>
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>
25 #include <perfmon/perfmon.h>
27 static_always_inline void
28 perfmon_read_pmcs (u64 *counters, int *pmc_index, u8 n_counters)
34 counters[6] = _rdpmc (pmc_index[6]);
36 counters[5] = _rdpmc (pmc_index[5]);
38 counters[4] = _rdpmc (pmc_index[4]);
40 counters[3] = _rdpmc (pmc_index[3]);
42 counters[2] = _rdpmc (pmc_index[2]);
44 counters[1] = _rdpmc (pmc_index[1]);
46 counters[0] = _rdpmc (pmc_index[0]);
51 static_always_inline int
52 perfmon_calc_pmc_index (perfmon_thread_runtime_t *tr, u8 i)
54 return (int) (tr->mmap_pages[i]->index + tr->mmap_pages[i]->offset);
58 perfmon_dispatch_wrapper (vlib_main_t *vm, vlib_node_runtime_t *node,
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];
72 clib_prefetch_load (s);
78 pmc_index[6] = perfmon_calc_pmc_index (rt, 6);
80 pmc_index[5] = perfmon_calc_pmc_index (rt, 5);
82 pmc_index[4] = perfmon_calc_pmc_index (rt, 4);
84 pmc_index[3] = perfmon_calc_pmc_index (rt, 3);
86 pmc_index[2] = perfmon_calc_pmc_index (rt, 2);
88 pmc_index[1] = perfmon_calc_pmc_index (rt, 1);
90 pmc_index[0] = perfmon_calc_pmc_index (rt, 0);
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);
103 for (int i = 0; i < n_events; i++)
104 s->value[i] += after[i] - before[i];