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 void
29 perfmon_read_pmcs (u64 *counters, u32 *indexes, u8 n_counters)
31 for (int i = 0; i < n_counters; i++)
32 counters[i] = _rdpmc (indexes[i] - 1);
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)
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);
47 u64 t[2][PERF_MAX_EVENTS];
51 clib_prefetch_load (s);
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);
63 for (int i = 0; i < n_events; i++)
65 if (!(rt->preserve_samples & 1 << i))
67 s->value[i] += samples.t[1][i] - samples.t[0][i];
71 s->t[0].value[i] = samples.t[0][i];
72 s->t[1].value[i] = samples.t[1][i];
79 #define foreach_n_events \
80 _ (1) _ (2) _ (3) _ (4) _ (5) _ (6) _ (7) _ (8) _ (9) _ (10) _ (11) _ (12)
83 static uword perfmon_dispatch_wrapper##x ( \
84 vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame) \
86 return perfmon_dispatch_wrapper_inline (vm, node, frame, x); \
92 vlib_node_function_t *perfmon_dispatch_wrappers[PERF_MAX_EVENTS + 1] = {
93 #define _(x) [x] = &perfmon_dispatch_wrapper##x,