FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
perfmon.c
Go to the documentation of this file.
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 
28 
30  .version = VPP_BUILD_VER,
31  .description = "Performance Monitor",
32 };
33 
34 VLIB_REGISTER_LOG_CLASS (if_default_log, static) = {
35  .class_name = "perfmon",
36 };
37 
38 #define log_debug(fmt, ...) \
39  vlib_log_debug (if_default_log.class, fmt, __VA_ARGS__)
40 #define log_warn(fmt, ...) \
41  vlib_log_warn (if_default_log.class, fmt, __VA_ARGS__)
42 #define log_err(fmt, ...) vlib_log_err (if_default_log.class, fmt, __VA_ARGS__)
43 
44 void
46 {
48  uword page_size = clib_mem_get_page_size ();
49 
50  if (pm->is_running)
51  for (int i = 0; i < vlib_get_n_threads (); i++)
53 
54  for (int i = 0; i < vec_len (pm->fds_to_close); i++)
55  close (pm->fds_to_close[i]);
56  vec_free (pm->fds_to_close);
57  vec_free (pm->group_fds);
58  if (pm->default_instance_type)
59  {
61  for (int i = 0; i < vec_len (it->instances); i++)
62  vec_free (it->instances[i].name);
63  vec_free (it->instances);
65  }
66 
67  for (int i = 0; i < vec_len (pm->thread_runtimes); i++)
68  {
70  vec_free (tr->node_stats);
71  for (int j = 0; j < PERF_MAX_EVENTS; j++)
72  if (tr->mmap_pages[j])
73  munmap (tr->mmap_pages, page_size);
74  }
76 
77  pm->is_running = 0;
78  pm->active_instance_type = 0;
79  pm->active_bundle = 0;
80 }
81 
82 static clib_error_t *
84 {
85  clib_error_t *err = 0;
88  int is_node = 0;
89  int n_nodes = vec_len (vm->node_main.nodes);
90  uword page_size = clib_mem_get_page_size ();
91  u32 instance_type = 0;
92  perfmon_event_t *e;
94 
95  perfmon_reset (vm);
96 
97  s = b->src;
98  ASSERT (b->n_events);
99 
100  if (b->type == PERFMON_BUNDLE_TYPE_NODE)
101  is_node = 1;
102 
103  if (s->instances_by_type == 0)
104  {
106  it->name = is_node ? "Thread/Node" : "Thread";
107  for (int i = 0; i < vlib_get_n_threads (); i++)
108  {
110  perfmon_instance_t *in;
111  vec_add2 (it->instances, in, 1);
112  in->cpu = w->cpu_id;
113  in->pid = w->lwp;
114  in->name = (char *) format (0, "%s (%u)%c", w->name, i, 0);
115  }
116  if (is_node)
118  }
119  else
120  {
121  e = s->events + b->events[0];
122 
123  if (e->type_from_instance)
124  {
125  instance_type = e->instance_type;
126  for (int i = 1; i < b->n_events; i++)
127  {
128  e = s->events + b->events[i];
129  ASSERT (e->type_from_instance == 1 &&
130  e->instance_type == instance_type);
131  }
132  }
133  it = vec_elt_at_index (s->instances_by_type, instance_type);
134  }
135 
136  pm->active_instance_type = it;
137 
138  for (int i = 0; i < vec_len (it->instances); i++)
139  {
140  perfmon_instance_t *in = vec_elt_at_index (it->instances, i);
141 
142  vec_validate (pm->group_fds, i);
143  pm->group_fds[i] = -1;
144 
145  for (int j = 0; j < b->n_events; j++)
146  {
147  int fd;
148  perfmon_event_t *e = s->events + b->events[j];
149  struct perf_event_attr pe = {
150  .size = sizeof (struct perf_event_attr),
151  .type = e->type_from_instance ? in->type : e->type,
152  .config = e->config,
153  .exclude_kernel = e->exclude_kernel,
154  .read_format =
155  (PERF_FORMAT_GROUP | PERF_FORMAT_TOTAL_TIME_ENABLED |
156  PERF_FORMAT_TOTAL_TIME_RUNNING),
157  .disabled = 1,
158  };
159 
160  log_debug ("perf_event_open pe.type=%u pe.config=0x%x pid=%d "
161  "cpu=%d group_fd=%d",
162  pe.type, pe.config, in->pid, in->cpu, pm->group_fds[i]);
163  fd = syscall (__NR_perf_event_open, &pe, in->pid, in->cpu,
164  pm->group_fds[i], 0);
165 
166  if (fd == -1)
167  {
168  err = clib_error_return_unix (0, "perf_event_open");
169  goto error;
170  }
171 
172  vec_add1 (pm->fds_to_close, fd);
173 
174  if (pm->group_fds[i] == -1)
175  pm->group_fds[i] = fd;
176 
177  if (is_node)
178  {
180  tr = vec_elt_at_index (pm->thread_runtimes, i);
181  tr->mmap_pages[j] =
182  mmap (0, page_size, PROT_READ, MAP_SHARED, fd, 0);
183 
184  if (tr->mmap_pages[j] == MAP_FAILED)
185  {
186  err = clib_error_return_unix (0, "mmap");
187  goto error;
188  }
189  }
190  }
191 
192  if (is_node)
193  {
196  rt->bundle = b;
197  rt->n_events = b->n_events;
198  rt->n_nodes = n_nodes;
199  vec_validate_aligned (rt->node_stats, n_nodes - 1,
201  }
202  }
203 
204  pm->active_bundle = b;
205 
206 error:
207  if (err)
208  {
209  log_err ("%U", format_clib_error, err);
210  perfmon_reset (vm);
211  }
212  return err;
213 }
214 
215 clib_error_t *
217 {
218  clib_error_t *err = 0;
220  int n_groups;
221 
222  if (pm->is_running == 1)
223  return clib_error_return (0, "already running");
224 
225  if ((err = perfmon_set (vm, b)) != 0)
226  return err;
227 
228  n_groups = vec_len (pm->group_fds);
229 
230  for (int i = 0; i < n_groups; i++)
231  {
232  if (ioctl (pm->group_fds[i], PERF_EVENT_IOC_ENABLE,
233  PERF_IOC_FLAG_GROUP) == -1)
234  {
235  perfmon_reset (vm);
236  return clib_error_return_unix (0, "ioctl(PERF_EVENT_IOC_ENABLE)");
237  }
238  }
239  if (b->type == PERFMON_BUNDLE_TYPE_NODE)
240  {
241 
243 #define _(type, pfunc) funcs[type] = pfunc;
244 
246 #undef _
247 
248  ASSERT (funcs[b->offset_type]);
249 
250  for (int i = 0; i < vlib_get_n_threads (); i++)
252  funcs[b->offset_type]);
253  }
254 
255  pm->sample_time = vlib_time_now (vm);
256  pm->is_running = 1;
257 
258  return 0;
259 }
260 
261 clib_error_t *
263 {
265  int n_groups = vec_len (pm->group_fds);
266 
267  if (pm->is_running != 1)
268  return clib_error_return (0, "not running");
269 
271  {
272  for (int i = 0; i < vlib_get_n_threads (); i++)
274  }
275 
276  for (int i = 0; i < n_groups; i++)
277  {
278  if (ioctl (pm->group_fds[i], PERF_EVENT_IOC_DISABLE,
279  PERF_IOC_FLAG_GROUP) == -1)
280  {
281  perfmon_reset (vm);
282  return clib_error_return_unix (0, "ioctl(PERF_EVENT_IOC_DISABLE)");
283  }
284  }
285 
286  pm->is_running = 0;
287  pm->sample_time = vlib_time_now (vm) - pm->sample_time;
288  return 0;
289 }
290 
291 static clib_error_t *
293 {
295  perfmon_source_t *s = pm->sources;
296  perfmon_bundle_t *b = pm->bundles;
297 
298  pm->source_by_name = hash_create_string (0, sizeof (uword));
299  while (s)
300  {
301  clib_error_t *err;
302  if (hash_get_mem (pm->source_by_name, s->name) != 0)
303  clib_panic ("duplicate source name '%s'", s->name);
304  if (s->init_fn && ((err = (s->init_fn) (vm, s))))
305  {
306  log_warn ("skipping source '%s' - %U", s->name, format_clib_error,
307  err);
308  clib_error_free (err);
309  s = s->next;
310  continue;
311  }
312 
313  hash_set_mem (pm->source_by_name, s->name, s);
314  log_debug ("source '%s' regisrtered", s->name);
315  s = s->next;
316  }
317 
318  pm->bundle_by_name = hash_create_string (0, sizeof (uword));
319  while (b)
320  {
321  clib_error_t *err;
322  uword *p;
323  if (hash_get_mem (pm->bundle_by_name, b->name) != 0)
324  clib_panic ("duplicate bundle name '%s'", b->name);
325 
326  if ((p = hash_get_mem (pm->source_by_name, b->source)) == 0)
327  {
328  log_debug ("missing source '%s', skipping bundle '%s'", b->source,
329  b->name);
330  b = b->next;
331  continue;
332  }
333 
334  b->src = (perfmon_source_t *) p[0];
335  if (b->init_fn && ((err = (b->init_fn) (vm, b))))
336  {
337  log_warn ("skipping bundle '%s' - %U", b->name, format_clib_error,
338  err);
339  clib_error_free (err);
340  b = b->next;
341  continue;
342  }
343 
344  hash_set_mem (pm->bundle_by_name, b->name, b);
345  log_debug ("bundle '%s' regisrtered", b->name);
346 
347  b = b->next;
348  }
349 
350  return 0;
351 }
352 
perfmon_event_t::config
u64 config
Definition: perfmon.h:53
api.h
PERFMON_BUNDLE_TYPE_NODE
@ PERFMON_BUNDLE_TYPE_NODE
Definition: perfmon.h:31
perfmon_bundle::type
perfmon_bundle_type_t type
Definition: perfmon.h:111
perfmon_reset
void perfmon_reset(vlib_main_t *vm)
Definition: perfmon.c:45
perfmon_source::events
perfmon_event_t * events
Definition: perfmon.h:87
perfmon_main_t::active_bundle
perfmon_bundle_t * active_bundle
Definition: perfmon.h:171
perfmon_event_t
Definition: perfmon.h:44
perfmon_main_t
Definition: perfmon.h:164
perfmon_instance_type_t
Definition: perfmon.h:66
perfmon_main
perfmon_main_t perfmon_main
Definition: perfmon.c:27
hash_create_string
#define hash_create_string(elts, value_bytes)
Definition: hash.h:689
clib_error_return
#define clib_error_return(e, args...)
Definition: error.h:99
perfmon_main_t::bundles
perfmon_bundle_t * bundles
Definition: perfmon.h:167
vlib_main_t::node_main
vlib_node_main_t node_main
Definition: main.h:173
hash_set_mem
#define hash_set_mem(h, key, value)
Definition: hash.h:275
perfmon_bundle
Definition: perfmon.h:105
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
api.h
perfmon_source::instances_by_type
perfmon_instance_type_t * instances_by_type
Definition: perfmon.h:89
perfmon_event_t::instance_type
u32 instance_type
Definition: perfmon.h:51
perfmon_source::name
char * name
Definition: perfmon.h:84
vlib_node_function_t
uword() vlib_node_function_t(struct vlib_main_t *vm, struct vlib_node_runtime_t *node, struct vlib_frame_t *frame)
Definition: node.h:54
perfmon_main_t::active_instance_type
perfmon_instance_type_t * active_instance_type
Definition: perfmon.h:177
vlib_worker_threads
vlib_worker_thread_t * vlib_worker_threads
Definition: threads.c:35
perfmon_main_t::thread_runtimes
perfmon_thread_runtime_t * thread_runtimes
Definition: perfmon.h:166
error
Definition: cJSON.c:88
perfmon_main_t::source_by_name
uword * source_by_name
Definition: perfmon.h:170
VLIB_REGISTER_LOG_CLASS
VLIB_REGISTER_LOG_CLASS(if_default_log, static)
perfmon_source::init_fn
perfmon_source_init_fn_t * init_fn
Definition: perfmon.h:91
format_clib_error
__clib_export u8 * format_clib_error(u8 *s, va_list *va)
Definition: error.c:191
perfmon_source::next
struct perfmon_source * next
Definition: perfmon.h:86
perfmon_instance_t::type
u32 type
Definition: perfmon.h:60
perfmon_main_t::sources
perfmon_source_t * sources
Definition: perfmon.h:169
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
perfmon_event_t::type
u32 type
Definition: perfmon.h:50
perfmon_source
Definition: perfmon.h:82
vec_add2
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:644
vec_add1
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:606
vec_elt_at_index
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
Definition: vec_bootstrap.h:203
perfmon_main_t::sample_time
f64 sample_time
Definition: perfmon.h:173
vec_validate_aligned
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:534
uword
u64 uword
Definition: types.h:112
vlib_worker_thread_t::cpu_id
int cpu_id
Definition: threads.h:106
perfmon_main_t::fds_to_close
int * fds_to_close
Definition: perfmon.h:175
vlib_worker_thread_t::lwp
long lwp
Definition: threads.h:105
vec_validate
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment)
Definition: vec.h:523
it
save_rewrite_length must be aligned so that reass doesn t overwrite it
Definition: buffer.h:425
CLIB_CACHE_LINE_BYTES
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:58
pe
uword pe(void *v)
GDB callable function: pe - call pool_elts - number of elements in a pool.
Definition: gdb_funcs.c:64
perfmon_event_t::exclude_kernel
u32 exclude_kernel
Definition: perfmon.h:47
perfmon_init
static clib_error_t * perfmon_init(vlib_main_t *vm)
Definition: perfmon.c:292
perfmon_instance_t
Definition: perfmon.h:58
plugin.h
perfmon_thread_runtime_t::mmap_pages
struct perf_event_mmap_page * mmap_pages[PERF_MAX_EVENTS]
Definition: perfmon.h:161
PERFMON_OFFSET_TYPE_MAX
@ PERFMON_OFFSET_TYPE_MAX
Definition: perfmon.h:41
vlib_node_set_dispatch_wrapper
static int vlib_node_set_dispatch_wrapper(vlib_main_t *vm, vlib_node_function_t *fn)
Definition: node_funcs.h:1264
hash_get_mem
#define hash_get_mem(h, key)
Definition: hash.h:269
vec_free
#define vec_free(V)
Free vector's memory (no header).
Definition: vec.h:395
PERF_MAX_EVENTS
#define PERF_MAX_EVENTS
Definition: perfmon.h:26
perfmon_main_t::group_fds
int * group_fds
Definition: perfmon.h:174
format
description fragment has unexpected format
Definition: map.api:433
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
perfmon_main_t::bundle_by_name
uword * bundle_by_name
Definition: perfmon.h:168
u32
unsigned int u32
Definition: types.h:88
VLIB_INIT_FUNCTION
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:172
perfmon_main_t::default_instance_type
perfmon_instance_type_t * default_instance_type
Definition: perfmon.h:176
foreach_permon_offset_type
#define foreach_permon_offset_type
Definition: perfmon.h:76
perfmon_instance_t::pid
pid_t pid
Definition: perfmon.h:62
log_err
#define log_err(fmt,...)
Definition: perfmon.c:42
clib_mem_get_page_size
static_always_inline uword clib_mem_get_page_size(void)
Definition: mem.h:471
vlib_worker_thread_t
Definition: threads.h:81
perfmon_main_t::is_running
int is_running
Definition: perfmon.h:172
perfmon_instance_t::name
char * name
Definition: perfmon.h:63
vlib_get_main_by_index
static vlib_main_t * vlib_get_main_by_index(u32 thread_index)
Definition: global_funcs.h:29
clib_error_return_unix
#define clib_error_return_unix(e, args...)
Definition: error.h:102
perfmon_event_t::type_from_instance
u32 type_from_instance
Definition: perfmon.h:46
perfmon_thread_runtime_t
Definition: perfmon.h:155
perfmon_instance_t::cpu
int cpu
Definition: perfmon.h:61
vlib_main_t
Definition: main.h:102
vlib_get_n_threads
static u32 vlib_get_n_threads()
Definition: global_funcs.h:23
b
vlib_buffer_t ** b
Definition: nat44_ei_out2in.c:717
clib_error_t
Definition: clib_error.h:21
perfmon_set
static clib_error_t * perfmon_set(vlib_main_t *vm, perfmon_bundle_t *b)
Definition: perfmon.c:83
rt
vnet_interface_output_runtime_t * rt
Definition: interface_output.c:419
vlib_init_function_t
clib_error_t *() vlib_init_function_t(struct vlib_main_t *vm)
Definition: init.h:51
perfmon_thread_runtime_t::node_stats
perfmon_node_stats_t * node_stats
Definition: perfmon.h:159
i
int i
Definition: flowhash_template.h:376
vlib_worker_thread_t::name
u8 * name
Definition: threads.h:98
log_warn
#define log_warn(fmt,...)
Definition: perfmon.c:40
perfmon_stop
clib_error_t * perfmon_stop(vlib_main_t *vm)
Definition: perfmon.c:262
clib_error_free
#define clib_error_free(e)
Definition: error.h:86
perfmon_start
clib_error_t * perfmon_start(vlib_main_t *vm, perfmon_bundle_t *b)
Definition: perfmon.c:216
vlib_time_now
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:327
vnet.h
vlib_node_main_t::nodes
vlib_node_t ** nodes
Definition: node.h:668
clib_panic
#define clib_panic(format, args...)
Definition: error.h:72
VLIB_PLUGIN_REGISTER
VLIB_PLUGIN_REGISTER()
type
vl_api_fib_path_type_t type
Definition: fib_types.api:123
log_debug
#define log_debug(fmt,...)
Definition: perfmon.c:38