FD.io VPP  v19.08.3-2-gbabecb413
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
37  vlib_node_runtime_t * node,
38  vlib_frame_t * frame, int before_or_after)
39 {
40  int i;
41  u64 *cc;
43  uword my_thread_index = vm->thread_index;
44 
45  *c0 = *c1 = 0;
46 
47  for (i = 0; i < pm->n_active; i++)
48  {
49  cc = (i == 0) ? c0 : c1;
50  if (pm->rdpmc_indices[i][my_thread_index] != ~0)
51  *cc = clib_rdpmc ((int) pm->rdpmc_indices[i][my_thread_index]);
52  else
53  {
54  u64 sw_value;
55  int read_result;
56  if ((read_result = read (pm->pm_fds[i][my_thread_index], &sw_value,
57  sizeof (sw_value)) != sizeof (sw_value)))
58  {
60  ("counter read returned %d, expected %d",
61  read_result, sizeof (sw_value));
66  read_current_perf_counters, 0 /* enable */ );
67  return;
68  }
69  *cc = sw_value;
70  }
71  }
72 }
73 
74 static void
76 {
77  int i, j;
78  vlib_main_t *vm = pm->vlib_main;
79  vlib_main_t *stat_vm;
80  vlib_node_main_t *nm;
81  vlib_node_t *n;
82 
84 
85  for (j = 0; j < vec_len (vlib_mains); j++)
86  {
87  stat_vm = vlib_mains[j];
88  if (stat_vm == 0)
89  continue;
90 
91  nm = &stat_vm->node_main;
92 
93  /* Clear the node runtime perfmon counters */
94  for (i = 0; i < vec_len (nm->nodes); i++)
95  {
96  n = nm->nodes[i];
97  vlib_node_sync_stats (stat_vm, n);
98  }
99 
100  /* And clear the node perfmon counters */
101  for (i = 0; i < vec_len (nm->nodes); i++)
102  {
103  n = nm->nodes[i];
110  }
111  }
113 }
114 
115 static void
117 {
118  struct perf_event_attr pe;
119  int fd;
120  struct perf_event_mmap_page *p = 0;
123  u32 my_thread_index = vm->thread_index;
124  u32 index;
125  int i, limit = 1;
126  int cpu;
127 
128  if ((pm->current_event + 1) < vec_len (pm->single_events_to_collect))
129  limit = 2;
130 
131  for (i = 0; i < limit; i++)
132  {
134  pm->current_event + i);
135 
136  memset (&pe, 0, sizeof (struct perf_event_attr));
137  pe.type = c->pe_type;
138  pe.size = sizeof (struct perf_event_attr);
139  pe.config = c->pe_config;
140  pe.disabled = 1;
141  pe.pinned = 1;
142  /*
143  * Note: excluding the kernel makes the
144  * (software) context-switch counter read 0...
145  */
146  if (pe.type != PERF_TYPE_SOFTWARE)
147  {
148  /* Exclude kernel and hypervisor */
149  pe.exclude_kernel = 1;
150  pe.exclude_hv = 1;
151  }
152 
153  cpu = vm->cpu_id;
154 
155  fd = perf_event_open (&pe, 0, cpu, -1, 0);
156  if (fd == -1)
157  {
158  clib_unix_warning ("event open: type %d config %d", c->pe_type,
159  c->pe_config);
160  return;
161  }
162 
163  if (pe.type != PERF_TYPE_SOFTWARE)
164  {
165  p = mmap (0, pm->page_size, PROT_READ, MAP_SHARED, fd, 0);
166  if (p == MAP_FAILED)
167  {
168  clib_unix_warning ("mmap");
169  close (fd);
170  return;
171  }
172  }
173  else
174  p = 0;
175 
176  if (ioctl (fd, PERF_EVENT_IOC_RESET, 0) < 0)
177  clib_unix_warning ("reset ioctl");
178 
179  if (ioctl (fd, PERF_EVENT_IOC_ENABLE, 0) < 0)
180  clib_unix_warning ("enable ioctl");
181 
182  pm->perf_event_pages[i][my_thread_index] = (void *) p;
183  pm->pm_fds[i][my_thread_index] = fd;
184  }
185 
186  /*
187  * Hardware events must be all opened and enabled before aquiring
188  * pmc indices, otherwise the pmc indices might be out-dated.
189  */
190  for (i = 0; i < limit; i++)
191  {
192  p =
193  (struct perf_event_mmap_page *)
194  pm->perf_event_pages[i][my_thread_index];
195 
196  /*
197  * Software event counters - and others not capable of being
198  * read via the "rdpmc" instruction - will be read
199  * by system calls.
200  */
201  if (p == 0 || p->cap_user_rdpmc == 0)
202  index = ~0;
203  else
204  index = p->index - 1;
205 
206  pm->rdpmc_indices[i][my_thread_index] = index;
207  }
208 
209  pm->n_active = i;
210  /* Enable the main loop counter snapshot mechanism */
215  read_current_perf_counters, 1 /* enable */ );
216 }
217 
218 static void
220 {
222  u32 my_thread_index = vm->thread_index;
223  int i;
224 
225  /* Stop main loop collection */
230  read_current_perf_counters, 0 /* enable */ );
231 
232  for (i = 0; i < pm->n_active; i++)
233  {
234  if (pm->pm_fds[i][my_thread_index] == 0)
235  continue;
236 
237  if (ioctl (pm->pm_fds[i][my_thread_index], PERF_EVENT_IOC_DISABLE, 0) <
238  0)
239  clib_unix_warning ("disable ioctl");
240 
241  if (pm->perf_event_pages[i][my_thread_index])
242  if (munmap (pm->perf_event_pages[i][my_thread_index],
243  pm->page_size) < 0)
244  clib_unix_warning ("munmap");
245 
246  (void) close (pm->pm_fds[i][my_thread_index]);
247  pm->pm_fds[i][my_thread_index] = 0;
248  }
249 }
250 
251 static void
253 {
255 
259  worker_thread_start_event, 0 /* enable */ );
261 }
262 
263 static void
265 {
270  worker_thread_stop_event, 0 /* enable */ );
271  disable_events (pm);
272 }
273 
274 static void
275 start_event (perfmon_main_t * pm, f64 now, uword event_data)
276 {
277  int i;
278  int last_set;
279  int all = 0;
280  pm->current_event = 0;
281 
282  if (vec_len (pm->single_events_to_collect) == 0)
283  {
284  pm->state = PERFMON_STATE_OFF;
285  return;
286  }
287 
288  last_set = clib_bitmap_last_set (pm->thread_bitmap);
289  all = (last_set == ~0);
290 
292  clear_counters (pm);
293 
294  /* Start collection on thread 0? */
295  if (all || clib_bitmap_get (pm->thread_bitmap, 0))
296  {
297  /* Start collection on this thread */
299  }
300 
301  /* And also on worker threads */
302  for (i = 1; i < vec_len (vlib_mains); i++)
303  {
304  if (vlib_mains[i] == 0)
305  continue;
306 
307  if (all || clib_bitmap_get (pm->thread_bitmap, i))
309  (vlib_mains[i]->worker_thread_main_loop_callbacks,
310  vlib_mains[i]->worker_thread_main_loop_callback_tmp,
311  vlib_mains[i]->worker_thread_main_loop_callback_lock,
312  (void *) worker_thread_start_event, 1 /* enable */ );
313  }
314 }
315 
316 void
318 {
319  int i, j, k;
320  vlib_main_t *vm = pm->vlib_main;
321  vlib_main_t *stat_vm;
322  vlib_node_main_t *nm;
323  vlib_node_t ***node_dups = 0;
324  vlib_node_t **nodes;
325  vlib_node_t *n;
327  perfmon_event_config_t *current_event;
328  uword *p;
329  u8 *counter_name;
330  u64 vectors_this_counter;
331 
332  /* snapshoot the nodes, including pm counters */
334 
335  for (j = 0; j < vec_len (vlib_mains); j++)
336  {
337  stat_vm = vlib_mains[j];
338  if (stat_vm == 0)
339  continue;
340 
341  nm = &stat_vm->node_main;
342 
343  for (i = 0; i < vec_len (nm->nodes); i++)
344  {
345  n = nm->nodes[i];
346  vlib_node_sync_stats (stat_vm, n);
347  }
348 
349  nodes = 0;
350  vec_validate (nodes, vec_len (nm->nodes) - 1);
351  vec_add1 (node_dups, nodes);
352 
353  /* Snapshoot and clear the per-node perfmon counters */
354  for (i = 0; i < vec_len (nm->nodes); i++)
355  {
356  n = nm->nodes[i];
357  nodes[i] = clib_mem_alloc (sizeof (*n));
358  clib_memcpy_fast (nodes[i], n, sizeof (*n));
365  }
366  }
367 
369 
370  for (j = 0; j < vec_len (vlib_mains); j++)
371  {
372  stat_vm = vlib_mains[j];
373  if (stat_vm == 0)
374  continue;
375 
376  nodes = node_dups[j];
377 
378  for (i = 0; i < vec_len (nodes); i++)
379  {
380  u8 *capture_name;
381 
382  n = nodes[i];
383 
384  if (n->stats_total.perf_counter0_ticks == 0 &&
386  goto skip_this_node;
387 
388  for (k = 0; k < 2; k++)
389  {
390  u64 counter_value, counter_last_clear;
391 
392  /*
393  * We collect 2 counters at once, except for the
394  * last counter when the user asks for an odd number of
395  * counters
396  */
397  if ((pm->current_event + k)
399  break;
400 
401  if (k == 0)
402  {
403  counter_value = n->stats_total.perf_counter0_ticks;
404  counter_last_clear =
406  }
407  else
408  {
409  counter_value = n->stats_total.perf_counter1_ticks;
410  counter_last_clear =
412  }
413 
414  capture_name = format (0, "t%d-%v%c", j, n->name, 0);
415 
417  capture_name);
418 
419  if (p == 0)
420  {
421  pool_get (pm->capture_pool, c);
422  memset (c, 0, sizeof (*c));
423  c->thread_and_node_name = capture_name;
425  capture_name, c - pm->capture_pool);
426  }
427  else
428  {
429  c = pool_elt_at_index (pm->capture_pool, p[0]);
430  vec_free (capture_name);
431  }
432 
433  /* Snapshoot counters, etc. into the capture */
434  current_event = pm->single_events_to_collect
435  + pm->current_event + k;
436  counter_name = (u8 *) current_event->name;
437  vectors_this_counter = n->stats_total.perf_counter_vectors -
439 
440  vec_add1 (c->counter_names, counter_name);
442  counter_value - counter_last_clear);
443  vec_add1 (c->vectors_this_counter, vectors_this_counter);
444  }
445  skip_this_node:
446  clib_mem_free (n);
447  }
448  vec_free (nodes);
449  }
450  vec_free (node_dups);
451 }
452 
453 static void
455 {
456  int i;
457  int last_set, all;
458 
459  last_set = clib_bitmap_last_set (pm->thread_bitmap);
460  all = (last_set == ~0);
461 
462  if (all || clib_bitmap_get (pm->thread_bitmap, 0))
463  disable_events (pm);
464 
465  /* And also on worker threads */
466  for (i = 1; i < vec_len (vlib_mains); i++)
467  {
468  if (vlib_mains[i] == 0)
469  continue;
470  if (all || clib_bitmap_get (pm->thread_bitmap, i))
472  (vlib_mains[i]->worker_thread_main_loop_callbacks,
473  vlib_mains[i]->worker_thread_main_loop_callback_tmp,
474  vlib_mains[i]->worker_thread_main_loop_callback_lock,
475  (void *) worker_thread_stop_event, 1 /* enable */ );
476  }
477 
478  /* Make sure workers have stopped collection */
479  if (i > 1)
480  {
481  f64 deadman = vlib_time_now (vm) + 1.0;
482 
483  for (i = 1; i < vec_len (vlib_mains); i++)
484  {
485  /* Has the worker actually stopped collecting data? */
486  while (clib_callback_is_set
487  (vlib_mains[i]->worker_thread_main_loop_callbacks,
488  vlib_mains[i]->worker_thread_main_loop_callback_lock,
490  {
491  if (vlib_time_now (vm) > deadman)
492  {
493  clib_warning ("Thread %d deadman timeout!", i);
494  break;
495  }
496  vlib_process_suspend (pm->vlib_main, 1e-3);
497  }
498  }
499  }
501  pm->current_event += pm->n_active;
502  if (pm->current_event >= vec_len (pm->single_events_to_collect))
503  {
504  pm->current_event = 0;
505  pm->state = PERFMON_STATE_OFF;
506  return;
507  }
508 
509  if (all || clib_bitmap_get (pm->thread_bitmap, 0))
511 
512  /* And also on worker threads */
513  for (i = 1; i < vec_len (vlib_mains); i++)
514  {
515  if (vlib_mains[i] == 0)
516  continue;
517  if (all || clib_bitmap_get (pm->thread_bitmap, i))
519  (vlib_mains[i]->worker_thread_main_loop_callbacks,
520  vlib_mains[i]->worker_thread_main_loop_callback_tmp,
521  vlib_mains[i]->worker_thread_main_loop_callback_lock,
522  worker_thread_start_event, 1 /* enable */ );
523  }
524 }
525 
526 static uword
529 {
531  f64 now;
532  uword *event_data = 0;
533  uword event_type;
534  int i;
535 
536  while (1)
537  {
538  if (pm->state == PERFMON_STATE_RUNNING)
540  else
542 
543  now = vlib_time_now (vm);
544 
545  event_type = vlib_process_get_events (vm, (uword **) & event_data);
546 
547  switch (event_type)
548  {
549  case PERFMON_START:
550  for (i = 0; i < vec_len (event_data); i++)
551  start_event (pm, now, event_data[i]);
552  break;
553 
554  /* Handle timeout */
555  case ~0:
556  handle_timeout (vm, pm, now);
557  break;
558 
559  default:
560  clib_warning ("Unexpected event %d", event_type);
561  break;
562  }
563  vec_reset_length (event_data);
564  }
565  return 0; /* or not */
566 }
567 
568 /* *INDENT-OFF* */
570 {
571  .function = perfmon_periodic_process,
572  .type = VLIB_NODE_TYPE_PROCESS,
573  .name = "perfmon-periodic-process",
574 };
575 /* *INDENT-ON* */
576 
577 /*
578  * fd.io coding-style-patch-verification: ON
579  *
580  * Local Variables:
581  * eval: (c-set-style "gnu")
582  * End:
583  */
perfmon_capture_t * capture_pool
Definition: perfmon.h:88
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:439
u32 ** rdpmc_indices
Definition: perfmon.h:115
volatile u8 state
Definition: perfmon.h:85
u32 current_event
Definition: perfmon.h:113
f64 timeout_interval
Definition: perfmon.h:110
static void worker_thread_start_event(vlib_main_t *vm)
u32 flags
Definition: vhost_user.h:141
u64 * vectors_this_counter
Definition: perfmon.h:70
int ** pm_fds
Definition: perfmon.h:121
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:673
static uword * vlib_process_wait_for_event(vlib_main_t *vm)
Definition: node_funcs.h:593
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:240
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
static void disable_events(perfmon_main_t *pm)
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:279
u32 thread_index
Definition: main.h:218
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:522
int i
static void enable_current_events(perfmon_main_t *pm)
#define hash_set_mem(h, key, value)
Definition: hash.h:275
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
vlib_main_t * vlib_main
Definition: perfmon.h:130
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:236
vlib_main_t ** vlib_mains
Definition: buffer.c:332
unsigned char u8
Definition: types.h:56
void(** vlib_node_runtime_perf_counter_cb_tmp)(struct vlib_main_t *, u64 *, u64 *, vlib_node_runtime_t *, vlib_frame_t *, int)
Definition: main.h:119
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
u64 perf_counter0_ticks
Definition: node.h:236
double f64
Definition: types.h:142
#define vlib_worker_thread_barrier_sync(X)
Definition: threads.h:204
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:422
vlib_node_stats_t stats_last_clear
Definition: node.h:274
void(** vlib_node_runtime_perf_counter_cbs)(struct vlib_main_t *, u64 *, u64 *, vlib_node_runtime_t *, vlib_frame_t *, int)
Definition: main.h:115
void(**volatile worker_thread_main_loop_callback_tmp)(struct vlib_main_t *)
Definition: main.h:239
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:516
vlib_node_t ** nodes
Definition: node.h:699
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
u64 perf_counter_vectors
Definition: node.h:238
u32 cpu_id
Definition: main.h:219
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:514
vlib_node_stats_t stats_total
Definition: node.h:270
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:423
#define clib_callback_is_set(h, l, f)
predicate function says whether the specified function is enabled
Definition: callback.h:88
u8 * name
Definition: node.h:264
static void worker_thread_stop_event(vlib_main_t *vm)
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
svmdb_client_t * c
static void handle_timeout(vlib_main_t *vm, perfmon_main_t *pm, f64 now)
vlib_main_t * vm
Definition: buffer.c:323
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
#define clib_warning(format, args...)
Definition: error.h:59
static uword clib_bitmap_get(uword *ai, uword i)
Gets the ith bit value from a bitmap.
Definition: bitmap.h:197
u64 perf_counter1_ticks
Definition: node.h:237
#define clib_callback_enable_disable(h, tmp, l, f, enable)
Add or remove a callback to the specified callback set.
Definition: callback.h:38
static void clib_mem_free(void *p)
Definition: mem.h:226
perfmon_event_config_t * single_events_to_collect
Definition: perfmon.h:100
static u64 clib_rdpmc(int counter_id)
Definition: pmc.h:22
perfmon_main_t perfmon_main
Definition: perfmon.c:28
static void * clib_mem_alloc(uword size)
Definition: mem.h:153
void(**volatile worker_thread_main_loop_callbacks)(struct vlib_main_t *)
Definition: main.h:237
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
uword * capture_by_thread_and_node_name
Definition: perfmon.h:89
static void start_event(perfmon_main_t *pm, f64 now, uword event_data)
void vlib_node_sync_stats(vlib_main_t *vm, vlib_node_t *n)
Definition: main.c:584
uword * thread_bitmap
Definition: perfmon.h:124
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u8 *** perf_event_pages
Definition: perfmon.h:117
u64 * counter_values
Definition: perfmon.h:69
vlib_node_main_t node_main
Definition: main.h:158
u64 uword
Definition: types.h:112
#define clib_unix_warning(format, args...)
Definition: error.h:68
#define hash_get_mem(h, key)
Definition: hash.h:269
void vlib_worker_thread_barrier_release(vlib_main_t *vm)
Definition: threads.c:1520
static uword perfmon_periodic_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
vlib_node_registration_t perfmon_periodic_node
(constructor) VLIB_REGISTER_NODE (perfmon_periodic_node)
#define PERFMON_START
Definition: perfmon.h:141
static void read_current_perf_counters(vlib_main_t *vm, u64 *c0, u64 *c1, vlib_node_runtime_t *node, vlib_frame_t *frame, int before_or_after)