FD.io VPP  v20.01-48-g3e0dafb74
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  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  {
133  vec_validate (pm->pm_fds[i], vec_len (vlib_mains) - 1);
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  pm->perf_event_pages[i][my_thread_index] = (void *) p;
188  pm->pm_fds[i][my_thread_index] = 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 =
198  (struct perf_event_mmap_page *)
199  pm->perf_event_pages[i][my_thread_index];
200 
201  /*
202  * Software event counters - and others not capable of being
203  * read via the "rdpmc" instruction - will be read
204  * by system calls.
205  */
206  if (p == 0 || p->cap_user_rdpmc == 0)
207  index = ~0;
208  else
209  index = p->index - 1;
210 
211  pm->rdpmc_indices[i][my_thread_index] = index;
212  }
213 
214  pm->n_active = i;
215  /* Enable the main loop counter snapshot mechanism */
220  read_current_perf_counters, 1 /* enable */ );
221 }
222 
223 static void
225 {
227  u32 my_thread_index = vm->thread_index;
228  int i;
229 
230  /* Stop main loop collection */
235  read_current_perf_counters, 0 /* enable */ );
236 
237  for (i = 0; i < pm->n_active; i++)
238  {
239  if (pm->pm_fds[i][my_thread_index] == 0)
240  continue;
241 
242  if (ioctl (pm->pm_fds[i][my_thread_index], PERF_EVENT_IOC_DISABLE, 0) <
243  0)
244  clib_unix_warning ("disable ioctl");
245 
246  if (pm->perf_event_pages[i][my_thread_index])
247  {
248  if (munmap (pm->perf_event_pages[i][my_thread_index],
249  pm->page_size) < 0)
250  clib_unix_warning ("munmap");
251  CLIB_MEM_POISON (pm->perf_event_pages[i][my_thread_index],
252  pm->page_size);
253  pm->perf_event_pages[i][my_thread_index] = 0;
254  }
255 
256  (void) close (pm->pm_fds[i][my_thread_index]);
257  pm->pm_fds[i][my_thread_index] = 0;
258 
259  }
260 }
261 
262 static void
264 {
266 
270  worker_thread_start_event, 0 /* enable */ );
272 }
273 
274 static void
276 {
281  worker_thread_stop_event, 0 /* enable */ );
282  disable_events (pm);
283 }
284 
285 static void
286 start_event (perfmon_main_t * pm, f64 now, uword event_data)
287 {
288  int i;
289  int last_set;
290  int all = 0;
291  pm->current_event = 0;
292 
293  if (vec_len (pm->single_events_to_collect) == 0)
294  {
295  pm->state = PERFMON_STATE_OFF;
296  return;
297  }
298 
299  last_set = clib_bitmap_last_set (pm->thread_bitmap);
300  all = (last_set == ~0);
301 
303  clear_counters (pm);
304 
305  /* Start collection on thread 0? */
306  if (all || clib_bitmap_get (pm->thread_bitmap, 0))
307  {
308  /* Start collection on this thread */
310  }
311 
312  /* And also on worker threads */
313  for (i = 1; i < vec_len (vlib_mains); i++)
314  {
315  if (vlib_mains[i] == 0)
316  continue;
317 
318  if (all || clib_bitmap_get (pm->thread_bitmap, i))
320  (vlib_mains[i]->worker_thread_main_loop_callbacks,
321  vlib_mains[i]->worker_thread_main_loop_callback_tmp,
322  vlib_mains[i]->worker_thread_main_loop_callback_lock,
323  (void *) worker_thread_start_event, 1 /* enable */ );
324  }
325 }
326 
327 void
329 {
330  int i, j, k;
331  vlib_main_t *vm = pm->vlib_main;
332  vlib_main_t *stat_vm;
333  vlib_node_main_t *nm;
334  vlib_node_t ***node_dups = 0;
335  vlib_node_t **nodes;
336  vlib_node_t *n;
338  perfmon_event_config_t *current_event;
339  uword *p;
340  u8 *counter_name;
341  u64 vectors_this_counter;
342 
343  /* snapshoot the nodes, including pm counters */
345 
346  for (j = 0; j < vec_len (vlib_mains); j++)
347  {
348  stat_vm = vlib_mains[j];
349  if (stat_vm == 0)
350  continue;
351 
352  nm = &stat_vm->node_main;
353 
354  for (i = 0; i < vec_len (nm->nodes); i++)
355  {
356  n = nm->nodes[i];
357  vlib_node_sync_stats (stat_vm, n);
358  }
359 
360  nodes = 0;
361  vec_validate (nodes, vec_len (nm->nodes) - 1);
362  vec_add1 (node_dups, nodes);
363 
364  /* Snapshoot and clear the per-node perfmon counters */
365  for (i = 0; i < vec_len (nm->nodes); i++)
366  {
367  n = nm->nodes[i];
368  nodes[i] = clib_mem_alloc (sizeof (*n));
369  clib_memcpy_fast (nodes[i], n, sizeof (*n));
376  }
377  }
378 
380 
381  for (j = 0; j < vec_len (vlib_mains); j++)
382  {
383  stat_vm = vlib_mains[j];
384  if (stat_vm == 0)
385  continue;
386 
387  nodes = node_dups[j];
388 
389  for (i = 0; i < vec_len (nodes); i++)
390  {
391  u8 *capture_name;
392 
393  n = nodes[i];
394 
395  if (n->stats_total.perf_counter0_ticks == 0 &&
397  goto skip_this_node;
398 
399  for (k = 0; k < 2; k++)
400  {
401  u64 counter_value, counter_last_clear;
402 
403  /*
404  * We collect 2 counters at once, except for the
405  * last counter when the user asks for an odd number of
406  * counters
407  */
408  if ((pm->current_event + k)
410  break;
411 
412  if (k == 0)
413  {
414  counter_value = n->stats_total.perf_counter0_ticks;
415  counter_last_clear =
417  }
418  else
419  {
420  counter_value = n->stats_total.perf_counter1_ticks;
421  counter_last_clear =
423  }
424 
425  capture_name = format (0, "t%d-%v%c", j, n->name, 0);
426 
428  capture_name);
429 
430  if (p == 0)
431  {
432  pool_get (pm->capture_pool, c);
433  memset (c, 0, sizeof (*c));
434  c->thread_and_node_name = capture_name;
436  capture_name, c - pm->capture_pool);
437  }
438  else
439  {
440  c = pool_elt_at_index (pm->capture_pool, p[0]);
441  vec_free (capture_name);
442  }
443 
444  /* Snapshoot counters, etc. into the capture */
445  current_event = pm->single_events_to_collect
446  + pm->current_event + k;
447  counter_name = (u8 *) current_event->name;
448  vectors_this_counter = n->stats_total.perf_counter_vectors -
450 
451  vec_add1 (c->counter_names, counter_name);
453  counter_value - counter_last_clear);
454  vec_add1 (c->vectors_this_counter, vectors_this_counter);
455  }
456  skip_this_node:
457  clib_mem_free (n);
458  }
459  vec_free (nodes);
460  }
461  vec_free (node_dups);
462 }
463 
464 static void
466 {
467  int i;
468  int last_set, all;
469 
470  last_set = clib_bitmap_last_set (pm->thread_bitmap);
471  all = (last_set == ~0);
472 
473  if (all || clib_bitmap_get (pm->thread_bitmap, 0))
474  disable_events (pm);
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  (void *) worker_thread_stop_event, 1 /* enable */ );
487  }
488 
489  /* Make sure workers have stopped collection */
490  if (i > 1)
491  {
492  f64 deadman = vlib_time_now (vm) + 1.0;
493 
494  for (i = 1; i < vec_len (vlib_mains); i++)
495  {
496  /* Has the worker actually stopped collecting data? */
497  while (clib_callback_is_set
498  (vlib_mains[i]->worker_thread_main_loop_callbacks,
499  vlib_mains[i]->worker_thread_main_loop_callback_lock,
501  {
502  if (vlib_time_now (vm) > deadman)
503  {
504  clib_warning ("Thread %d deadman timeout!", i);
505  break;
506  }
507  vlib_process_suspend (pm->vlib_main, 1e-3);
508  }
509  }
510  }
512  pm->current_event += pm->n_active;
513  if (pm->current_event >= vec_len (pm->single_events_to_collect))
514  {
515  pm->current_event = 0;
516  pm->state = PERFMON_STATE_OFF;
517  return;
518  }
519 
520  if (all || clib_bitmap_get (pm->thread_bitmap, 0))
522 
523  /* And also on worker threads */
524  for (i = 1; i < vec_len (vlib_mains); i++)
525  {
526  if (vlib_mains[i] == 0)
527  continue;
528  if (all || clib_bitmap_get (pm->thread_bitmap, i))
530  (vlib_mains[i]->worker_thread_main_loop_callbacks,
531  vlib_mains[i]->worker_thread_main_loop_callback_tmp,
532  vlib_mains[i]->worker_thread_main_loop_callback_lock,
533  worker_thread_start_event, 1 /* enable */ );
534  }
535 }
536 
537 static uword
540 {
542  f64 now;
543  uword *event_data = 0;
544  uword event_type;
545  int i;
546 
547  while (1)
548  {
549  if (pm->state == PERFMON_STATE_RUNNING)
551  else
553 
554  now = vlib_time_now (vm);
555 
556  event_type = vlib_process_get_events (vm, (uword **) & event_data);
557 
558  switch (event_type)
559  {
560  case PERFMON_START:
561  for (i = 0; i < vec_len (event_data); i++)
562  start_event (pm, now, event_data[i]);
563  break;
564 
565  /* Handle timeout */
566  case ~0:
567  handle_timeout (vm, pm, now);
568  break;
569 
570  default:
571  clib_warning ("Unexpected event %d", event_type);
572  break;
573  }
574  vec_reset_length (event_data);
575  }
576  return 0; /* or not */
577 }
578 
579 /* *INDENT-OFF* */
581 {
582  .function = perfmon_periodic_process,
583  .type = VLIB_NODE_TYPE_PROCESS,
584  .name = "perfmon-periodic-process",
585 };
586 /* *INDENT-ON* */
587 
588 /*
589  * fd.io coding-style-patch-verification: ON
590  *
591  * Local Variables:
592  * eval: (c-set-style "gnu")
593  * End:
594  */
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:440
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
#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
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:523
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:237
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:519
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
vlib_main_t * vm
Definition: in2out_ed.c:1810
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
u32 flags
Definition: vhost_user.h:141
svmdb_client_t * c
static void handle_timeout(vlib_main_t *vm, perfmon_main_t *pm, f64 now)
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:342
#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
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1810
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
u32 pid
Definition: dhcp.api:164
#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:1496
static uword perfmon_periodic_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: in2out_ed.c:1811
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)
#define CLIB_MEM_POISON(a, s)
Definition: sanitizer.h:46