FD.io VPP  v21.01.1
Vector Packet Processing
perfmon_periodic.c
Go to the documentation of this file.
1 /*
2  * perfmon_periodic.c - skeleton plug-in periodic function
3  *
4  * Copyright (c) <current-year> <your-organization>
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include <vlib/vlib.h>
19 #include <vppinfra/error.h>
20 #include <perfmon/perfmon.h>
21 #include <asm/unistd.h>
22 #include <sys/ioctl.h>
23 
24 /* "not in glibc" */
25 static long
26 perf_event_open (struct perf_event_attr *hw_event, pid_t pid, int cpu,
27  int group_fd, unsigned long flags)
28 {
29  int ret;
30 
31  ret = syscall (__NR_perf_event_open, hw_event, pid, cpu, group_fd, flags);
32  return ret;
33 }
34 
35 static void
38 {
39  int i;
41  perfmon_thread_t *pt = data->u[0].v;
42  u64 c[2] = { 0, 0 };
43  u64 *cc;
44 
46  return;
47 
49  cc = pt->c;
50  else
51  cc = c;
52 
53  for (i = 0; i < pm->n_active; i++)
54  {
55  if (pt->rdpmc_indices[i] != ~0)
56  cc[i] = clib_rdpmc ((int) pt->rdpmc_indices[i]);
57  else
58  {
59  u64 sw_value;
60  int read_result;
61  if ((read_result = read (pt->pm_fds[i], &sw_value,
62  sizeof (sw_value))) != sizeof (sw_value))
63  {
65  ("counter read returned %d, expected %d",
66  read_result, sizeof (sw_value));
69  read_current_perf_counters, 0 /* enable */ );
70  return;
71  }
72  cc[i] = sw_value;
73  }
74  }
75 
77  {
78  u32 node_index = args->node->node_index;
79  vec_validate (pt->counters, node_index);
80  pt->counters[node_index].ticks[0] += c[0] - pt->c[0];
81  pt->counters[node_index].ticks[1] += c[1] - pt->c[1];
82  pt->counters[node_index].vectors += args->packets;
83  }
84 }
85 
86 static void
88 {
89  int j;
90  vlib_main_t *vm = pm->vlib_main;
91  vlib_main_t *stat_vm;
92  perfmon_thread_t *pt;
93  u32 len;
94 
95 
97 
98  for (j = 0; j < vec_len (vlib_mains); j++)
99  {
100  stat_vm = vlib_mains[j];
101  if (stat_vm == 0)
102  continue;
103 
104  pt = pm->threads[j];
105  len = vec_len (pt->counters);
106  if (!len)
107  continue;
108 
109  clib_memset (pt->counters, 0, len * sizeof (pt->counters[0]));
110  }
112 }
113 
114 static void
116 {
117  struct perf_event_attr pe;
118  int fd;
119  struct perf_event_mmap_page *p = 0;
122  u32 my_thread_index = vm->thread_index;
123  perfmon_thread_t *pt = pm->threads[my_thread_index];
124  u32 index;
125  int i, limit = 1;
126  int cpu;
129  cbdata.u[0].v = pt;
130  cbdata.u[1].v = vm;
131 
132  if ((pm->current_event + 1) < vec_len (pm->single_events_to_collect))
133  limit = 2;
134 
135  for (i = 0; i < limit; i++)
136  {
138  pm->current_event + i);
139 
140  memset (&pe, 0, sizeof (struct perf_event_attr));
141  pe.type = c->pe_type;
142  pe.size = sizeof (struct perf_event_attr);
143  pe.config = c->pe_config;
144  pe.disabled = 1;
145  pe.pinned = 1;
146  /*
147  * Note: excluding the kernel makes the
148  * (software) context-switch counter read 0...
149  */
150  if (pe.type != PERF_TYPE_SOFTWARE)
151  {
152  /* Exclude kernel and hypervisor */
153  pe.exclude_kernel = 1;
154  pe.exclude_hv = 1;
155  }
156 
157  cpu = vm->cpu_id;
158 
159  fd = perf_event_open (&pe, 0, cpu, -1, 0);
160  if (fd == -1)
161  {
162  clib_unix_warning ("event open: type %d config %d", c->pe_type,
163  c->pe_config);
164  return;
165  }
166 
167  if (pe.type != PERF_TYPE_SOFTWARE)
168  {
169  p = mmap (0, pm->page_size, PROT_READ, MAP_SHARED, fd, 0);
170  if (p == MAP_FAILED)
171  {
172  clib_unix_warning ("mmap");
173  close (fd);
174  return;
175  }
176  CLIB_MEM_UNPOISON (p, pm->page_size);
177  }
178  else
179  p = 0;
180 
181  if (ioctl (fd, PERF_EVENT_IOC_RESET, 0) < 0)
182  clib_unix_warning ("reset ioctl");
183 
184  if (ioctl (fd, PERF_EVENT_IOC_ENABLE, 0) < 0)
185  clib_unix_warning ("enable ioctl");
186 
187  pt->perf_event_pages[i] = (void *) p;
188  pt->pm_fds[i] = fd;
189  }
190 
191  /*
192  * Hardware events must be all opened and enabled before aquiring
193  * pmc indices, otherwise the pmc indices might be out-dated.
194  */
195  for (i = 0; i < limit; i++)
196  {
197  p = (struct perf_event_mmap_page *) pt->perf_event_pages[i];
198 
199  /*
200  * Software event counters - and others not capable of being
201  * read via the "rdpmc" instruction - will be read
202  * by system calls.
203  */
204  if (p == 0 || p->cap_user_rdpmc == 0)
205  index = ~0;
206  else
207  index = p->index - 1;
208 
209  pt->rdpmc_indices[i] = index;
210  }
211 
212  pm->n_active = i;
213  /* Enable the main loop counter snapshot mechanism */
215 }
216 
217 static void
219 {
221  u32 my_thread_index = vm->thread_index;
222  perfmon_thread_t *pt = pm->threads[my_thread_index];
223  int i;
224 
225  /* Stop main loop collection */
228 
229  for (i = 0; i < pm->n_active; i++)
230  {
231  if (pt->pm_fds[i] == 0)
232  continue;
233 
234  if (ioctl (pt->pm_fds[i], PERF_EVENT_IOC_DISABLE, 0) < 0)
235  clib_unix_warning ("disable ioctl");
236 
237  if (pt->perf_event_pages[i])
238  {
239  if (munmap (pt->perf_event_pages[i], pm->page_size) < 0)
240  clib_unix_warning ("munmap");
241  pt->perf_event_pages[i] = 0;
242  }
243 
244  (void) close (pt->pm_fds[i]);
245  pt->pm_fds[i] = 0;
246  }
247 }
248 
249 static void
251 {
253 
257  worker_thread_start_event, 0 /* disable */ );
259 }
260 
261 static void
263 {
268  worker_thread_stop_event, 0 /* disable */ );
269  disable_events (pm);
270 }
271 
272 static void
273 start_event (perfmon_main_t * pm, f64 now, uword event_data)
274 {
275  int i;
276  int last_set;
277  int all = 0;
278  pm->current_event = 0;
279 
280  if (vec_len (pm->single_events_to_collect) == 0)
281  {
282  pm->state = PERFMON_STATE_OFF;
283  return;
284  }
285 
286  last_set = clib_bitmap_last_set (pm->thread_bitmap);
287  all = (last_set == ~0);
288 
290  clear_counters (pm);
291 
292  /* Start collection on thread 0? */
293  if (all || clib_bitmap_get (pm->thread_bitmap, 0))
294  {
295  /* Start collection on this thread */
297  }
298 
299  /* And also on worker threads */
300  for (i = 1; i < vec_len (vlib_mains); i++)
301  {
302  if (vlib_mains[i] == 0)
303  continue;
304 
305  if (all || clib_bitmap_get (pm->thread_bitmap, i))
307  (vlib_mains[i]->worker_thread_main_loop_callbacks,
308  vlib_mains[i]->worker_thread_main_loop_callback_tmp,
309  vlib_mains[i]->worker_thread_main_loop_callback_lock,
310  (void *) worker_thread_start_event, 1 /* enable */ );
311  }
312 }
313 
314 void
316 {
317  int i, j, k;
318  vlib_main_t *vm = pm->vlib_main;
319  vlib_main_t *stat_vm;
320  vlib_node_main_t *nm;
321  perfmon_counters_t *ctr;
322  perfmon_counters_t *ctrs;
323  perfmon_counters_t **ctr_dups = 0;
324  perfmon_thread_t *pt;
326  perfmon_event_config_t *current_event;
327  uword *p;
328  u8 *counter_name;
329  u32 len;
330 
331  /* snapshoot the nodes, including pm counters */
333 
334  for (j = 0; j < vec_len (vlib_mains); j++)
335  {
336  stat_vm = vlib_mains[j];
337  if (stat_vm == 0)
338  continue;
339 
340  pt = pm->threads[j];
341  len = vec_len (pt->counters);
342  ctrs = 0;
343  if (len)
344  {
345  vec_validate (ctrs, len - 1);
346  clib_memcpy (ctrs, pt->counters, len * sizeof (pt->counters[0]));
347  clib_memset (pt->counters, 0, len * sizeof (pt->counters[0]));
348  }
349  vec_add1 (ctr_dups, ctrs);
350  }
351 
353 
354  for (j = 0; j < vec_len (vlib_mains); j++)
355  {
356  stat_vm = vlib_mains[j];
357  if (stat_vm == 0)
358  continue;
359 
360  pt = pm->threads[j];
361  ctrs = ctr_dups[j];
362 
363  for (i = 0; i < vec_len (ctrs); i++)
364  {
365  u8 *capture_name;
366 
367  ctr = &ctrs[i];
368  nm = &stat_vm->node_main;
369 
370  if (ctr->ticks[0] == 0 && ctr->ticks[1] == 0)
371  continue;
372 
373  for (k = 0; k < 2; k++)
374  {
375  /*
376  * We collect 2 counters at once, except for the
377  * last counter when the user asks for an odd number of
378  * counters
379  */
380  if ((pm->current_event + k)
382  break;
383 
384  capture_name = format (0, "t%d-%v%c", j, nm->nodes[i]->name, 0);
385 
387  capture_name);
388 
389  if (p == 0)
390  {
391  pool_get (pm->capture_pool, c);
392  memset (c, 0, sizeof (*c));
393  c->thread_and_node_name = capture_name;
395  capture_name, c - pm->capture_pool);
396  }
397  else
398  {
399  c = pool_elt_at_index (pm->capture_pool, p[0]);
400  vec_free (capture_name);
401  }
402 
403  /* Snapshoot counters, etc. into the capture */
404  current_event = pm->single_events_to_collect
405  + pm->current_event + k;
406  counter_name = (u8 *) current_event->name;
407 
408  vec_add1 (c->counter_names, counter_name);
409  vec_add1 (c->counter_values, ctr->ticks[k]);
411  }
412  }
413  vec_free (ctrs);
414  }
415  vec_free (ctr_dups);
416 }
417 
418 static void
420 {
421  int i;
422  int last_set, all;
423 
424  last_set = clib_bitmap_last_set (pm->thread_bitmap);
425  all = (last_set == ~0);
426 
427  if (all || clib_bitmap_get (pm->thread_bitmap, 0))
428  disable_events (pm);
429 
430  /* And also on worker threads */
431  for (i = 1; i < vec_len (vlib_mains); i++)
432  {
433  if (vlib_mains[i] == 0)
434  continue;
435  if (all || clib_bitmap_get (pm->thread_bitmap, i))
437  (vlib_mains[i]->worker_thread_main_loop_callbacks,
438  vlib_mains[i]->worker_thread_main_loop_callback_tmp,
439  vlib_mains[i]->worker_thread_main_loop_callback_lock,
440  (void *) worker_thread_stop_event, 1 /* enable */ );
441  }
442 
443  /* Make sure workers have stopped collection */
444  if (i > 1)
445  {
446  f64 deadman = vlib_time_now (vm) + 1.0;
447 
448  for (i = 1; i < vec_len (vlib_mains); i++)
449  {
450  /* Has the worker actually stopped collecting data? */
454  {
455  if (vlib_time_now (vm) > deadman)
456  {
457  clib_warning ("Thread %d deadman timeout!", i);
458  break;
459  }
460  vlib_process_suspend (pm->vlib_main, 1e-3);
461  }
462  }
463  }
465  pm->current_event += pm->n_active;
466  if (pm->current_event >= vec_len (pm->single_events_to_collect))
467  {
468  pm->current_event = 0;
469  pm->state = PERFMON_STATE_OFF;
470  return;
471  }
472 
473  if (all || clib_bitmap_get (pm->thread_bitmap, 0))
475 
476  /* And also on worker threads */
477  for (i = 1; i < vec_len (vlib_mains); i++)
478  {
479  if (vlib_mains[i] == 0)
480  continue;
481  if (all || clib_bitmap_get (pm->thread_bitmap, i))
483  (vlib_mains[i]->worker_thread_main_loop_callbacks,
484  vlib_mains[i]->worker_thread_main_loop_callback_tmp,
485  vlib_mains[i]->worker_thread_main_loop_callback_lock,
486  worker_thread_start_event, 0 /* disable */ );
487  }
488 }
489 
490 static uword
493 {
495  f64 now;
496  uword *event_data = 0;
497  uword event_type;
498  int i;
499 
500  while (1)
501  {
502  if (pm->state == PERFMON_STATE_RUNNING)
504  else
506 
507  now = vlib_time_now (vm);
508 
509  event_type = vlib_process_get_events (vm, (uword **) & event_data);
510 
511  switch (event_type)
512  {
513  case PERFMON_START:
514  for (i = 0; i < vec_len (event_data); i++)
515  start_event (pm, now, event_data[i]);
516  break;
517 
518  /* Handle timeout */
519  case ~0:
520  handle_timeout (vm, pm, now);
521  break;
522 
523  default:
524  clib_warning ("Unexpected event %d", event_type);
525  break;
526  }
527  vec_reset_length (event_data);
528  }
529  return 0; /* or not */
530 }
531 
532 /* *INDENT-OFF* */
534 {
535  .function = perfmon_periodic_process,
536  .type = VLIB_NODE_TYPE_PROCESS,
537  .name = "perfmon-periodic-process",
538 };
539 /* *INDENT-ON* */
540 
541 /*
542  * fd.io coding-style-patch-verification: ON
543  *
544  * Local Variables:
545  * eval: (c-set-style "gnu")
546  * End:
547  */
perfmon_capture_t * capture_pool
Definition: perfmon.h:114
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:509
volatile u8 state
Definition: perfmon.h:111
u32 current_event
Definition: perfmon.h:139
f64 timeout_interval
Definition: perfmon.h:136
#define CLIB_MEM_UNPOISON(a, s)
Definition: sanitizer.h:47
static void worker_thread_start_event(vlib_main_t *vm)
u64 * vectors_this_counter
Definition: perfmon.h:70
perfmon_thread_t ** threads
Definition: perfmon.h:148
static f64 vlib_process_wait_for_event_or_clock(vlib_main_t *vm, f64 dt)
Suspend a cooperative multi-tasking thread Waits for an event, or for the indicated number of seconds...
Definition: node_funcs.h:751
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:105
static uword * vlib_process_wait_for_event(vlib_main_t *vm)
Definition: node_funcs.h:656
static void clear_counters(perfmon_main_t *pm)
unsigned long u64
Definition: types.h:89
static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags)
clib_spinlock_t worker_thread_main_loop_callback_lock
Definition: main.h:273
#define clib_callback_data_add(set_, value_)
Add a callback to the specified callback set.
Definition: callback_data.h:57
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static void disable_events(perfmon_main_t *pm)
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:334
u32 thread_index
Definition: main.h:250
struct vlib_main_t * vm
Definition: main.h:92
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:592
static void enable_current_events(perfmon_main_t *pm)
#define hash_set_mem(h, key, value)
Definition: hash.h:275
vlib_main_t * vm
Definition: in2out_ed.c:1580
vlib_main_t * vlib_main
Definition: perfmon.h:154
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:251
vlib_main_t ** vlib_mains
Definition: buffer.c:332
unsigned char u8
Definition: types.h:56
u8 data[128]
Definition: ipsec_types.api:90
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
double f64
Definition: types.h:142
#define vlib_worker_thread_barrier_sync(X)
Definition: threads.h:205
#define clib_memcpy(d, s, n)
Definition: string.h:180
static uword vlib_process_suspend(vlib_main_t *vm, f64 dt)
Suspend a vlib cooperative multi-tasking thread for a period of time.
Definition: node_funcs.h:482
static uword vlib_process_get_events(vlib_main_t *vm, uword **data_vector)
Return the first event type which has occurred and a vector of per-event data of that type...
Definition: node_funcs.h:579
vlib_node_t ** nodes
Definition: node.h:680
description fragment has unexpected format
Definition: map.api:433
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
u32 cpu_id
Definition: main.h:251
u8 * thread_and_node_name
Definition: perfmon.h:67
unsigned int u32
Definition: types.h:88
void scrape_and_clear_counters(perfmon_main_t *pm)
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:546
u8 * perf_event_pages[2]
Definition: perfmon.h:96
void(**volatile worker_thread_main_loop_callback_tmp)(struct vlib_main_t *, u64 t)
Definition: main.h:272
u8 ** counter_names
Definition: perfmon.h:68
static uword clib_bitmap_last_set(uword *ai)
Return the higest numbered set bit in a bitmap.
Definition: bitmap.h:429
vlib_node_runtime_perf_callback_fp_t fp
Definition: main.h:108
#define clib_callback_data_remove(set_, fp_)
Remove a callback from the specified callback set.
Definition: callback_data.h:84
#define PREDICT_FALSE(x)
Definition: clib.h:121
#define clib_callback_data_enable_disable(set_, fp_, ena_)
Enable/Disable the specified callback.
u32 node_index
Node index.
Definition: node.h:488
u32 rdpmc_indices[2]
Definition: perfmon.h:98
u8 len
Definition: ip_types.api:103
u8 * name
Definition: node.h:264
static void worker_thread_stop_event(vlib_main_t *vm)
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:170
svmdb_client_t * c
static void handle_timeout(vlib_main_t *vm, perfmon_main_t *pm, f64 now)
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
perfmon_counters_t * counters
Definition: perfmon.h:101
#define clib_warning(format, args...)
Definition: error.h:59
void(**volatile worker_thread_main_loop_callbacks)(struct vlib_main_t *, u64 t)
Definition: main.h:270
static void read_current_perf_counters(vlib_node_runtime_perf_callback_data_t *data, vlib_node_runtime_perf_callback_args_t *args)
static uword clib_bitmap_get(uword *ai, uword i)
Gets the ith bit value from a bitmap.
Definition: bitmap.h:197
vlib_node_runtime_perf_callback_set_t vlib_node_runtime_perf_callbacks
Definition: main.h:151
#define clib_callback_enable_disable(h, tmp, l, f, enable)
Add or remove a callback to the specified callback set.
Definition: callback.h:38
perfmon_event_config_t * single_events_to_collect
Definition: perfmon.h:126
static u64 clib_rdpmc(int counter_id)
Definition: pmc.h:22
perfmon_main_t perfmon_main
Definition: perfmon.c:27
#define clib_callback_data_is_set(set_, fp_)
Check if callback is set.
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
uword * capture_by_thread_and_node_name
Definition: perfmon.h:115
static void start_event(perfmon_main_t *pm, f64 now, uword event_data)
uword * thread_bitmap
Definition: perfmon.h:145
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u64 * counter_values
Definition: perfmon.h:69
vlib_node_main_t node_main
Definition: main.h:188
u64 uword
Definition: types.h:112
u32 pid
Definition: dhcp.api:164
#define clib_unix_warning(format, args...)
Definition: error.h:68
u32 index
Definition: flow_types.api:221
vlib_node_runtime_perf_call_type_t call_type
Definition: main.h:97
#define hash_get_mem(h, key)
Definition: hash.h:269
void vlib_worker_thread_barrier_release(vlib_main_t *vm)
Definition: threads.c:1561
vlib_node_runtime_t * node
Definition: main.h:93
static uword perfmon_periodic_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
int pm_fds[2]
Definition: perfmon.h:93
vlib_node_registration_t perfmon_periodic_node
(constructor) VLIB_REGISTER_NODE (perfmon_periodic_node)
#define PERFMON_START
Definition: perfmon.h:167