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)
34 counters[11] = _rdpmc (indexes[11] - 1);
36 counters[10] = _rdpmc (indexes[10] - 1);
38 counters[9] = _rdpmc (indexes[9] - 1);
40 counters[8] = _rdpmc (indexes[8] - 1);
42 counters[7] = _rdpmc (indexes[7] - 1);
44 counters[6] = _rdpmc (indexes[6] - 1);
46 counters[5] = _rdpmc (indexes[5] - 1);
48 counters[4] = _rdpmc (indexes[4] - 1);
50 counters[3] = _rdpmc (indexes[3] - 1);
52 counters[2] = _rdpmc (indexes[2] - 1);
54 counters[1] = _rdpmc (indexes[1] - 1);
56 counters[0] = _rdpmc (indexes[0] - 1);
61 perfmon_dispatch_wrapper (vlib_main_t *vm, vlib_node_runtime_t *node,
64 perfmon_main_t *pm = &perfmon_main;
65 perfmon_thread_runtime_t *rt =
66 vec_elt_at_index (pm->thread_runtimes, vm->thread_index);
67 perfmon_node_stats_t *s =
68 vec_elt_at_index (rt->node_stats, node->node_index);
70 u8 n_events = rt->n_events;
74 u64 t[2][PERF_MAX_EVENTS];
78 clib_prefetch_load (s);
80 perfmon_read_pmcs (&samples.t[0][0], &rt->indexes[0], n_events);
81 rv = node->function (vm, node, frame);
82 perfmon_read_pmcs (&samples.t[1][0], &rt->indexes[0], n_events);
90 for (int i = 0; i < n_events; i++)
92 if (!(rt->preserve_samples & 1 << i))
94 s->value[i] += samples.t[1][i] - samples.t[0][i];
98 s->t[0].value[i] = samples.t[0][i];
99 s->t[1].value[i] = samples.t[1][i];