Add node, frame to vlib main loop perf analysis callback arguments
[vpp.git] / src / plugins / perfmon / perfmon_periodic.c
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
36 read_current_perf_counters (vlib_main_t * vm, u64 * c0, u64 * c1,
37                             vlib_node_runtime_t * node,
38                             vlib_frame_t * frame, int before_or_after)
39 {
40   int i;
41   u64 *cc;
42   perfmon_main_t *pm = &perfmon_main;
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           if (read (pm->pm_fds[i][my_thread_index], &sw_value,
56                     sizeof (sw_value)) != sizeof (sw_value))
57             {
58               clib_unix_warning
59                 ("counter read failed, disable collection...");
60               vm->vlib_node_runtime_perf_counter_cb = 0;
61               return;
62             }
63           *cc = sw_value;
64         }
65     }
66 }
67
68 static void
69 clear_counters (perfmon_main_t * pm)
70 {
71   int i, j;
72   vlib_main_t *vm = pm->vlib_main;
73   vlib_main_t *stat_vm;
74   vlib_node_main_t *nm;
75   vlib_node_t *n;
76
77   vlib_worker_thread_barrier_sync (vm);
78
79   for (j = 0; j < vec_len (vlib_mains); j++)
80     {
81       stat_vm = vlib_mains[j];
82       if (stat_vm == 0)
83         continue;
84
85       nm = &stat_vm->node_main;
86
87       /* Clear the node runtime perfmon counters */
88       for (i = 0; i < vec_len (nm->nodes); i++)
89         {
90           n = nm->nodes[i];
91           vlib_node_sync_stats (stat_vm, n);
92         }
93
94       /* And clear the node perfmon counters */
95       for (i = 0; i < vec_len (nm->nodes); i++)
96         {
97           n = nm->nodes[i];
98           n->stats_total.perf_counter0_ticks = 0;
99           n->stats_total.perf_counter1_ticks = 0;
100           n->stats_total.perf_counter_vectors = 0;
101           n->stats_last_clear.perf_counter0_ticks = 0;
102           n->stats_last_clear.perf_counter1_ticks = 0;
103           n->stats_last_clear.perf_counter_vectors = 0;
104         }
105     }
106   vlib_worker_thread_barrier_release (vm);
107 }
108
109 static void
110 enable_current_events (perfmon_main_t * pm)
111 {
112   struct perf_event_attr pe;
113   int fd;
114   struct perf_event_mmap_page *p = 0;
115   perfmon_event_config_t *c;
116   vlib_main_t *vm = vlib_get_main ();
117   u32 my_thread_index = vm->thread_index;
118   u32 index;
119   int i, limit = 1;
120   int cpu;
121
122   if ((pm->current_event + 1) < vec_len (pm->single_events_to_collect))
123     limit = 2;
124
125   for (i = 0; i < limit; i++)
126     {
127       c = vec_elt_at_index (pm->single_events_to_collect,
128                             pm->current_event + i);
129
130       memset (&pe, 0, sizeof (struct perf_event_attr));
131       pe.type = c->pe_type;
132       pe.size = sizeof (struct perf_event_attr);
133       pe.config = c->pe_config;
134       pe.disabled = 1;
135       pe.pinned = 1;
136       /*
137        * Note: excluding the kernel makes the
138        * (software) context-switch counter read 0...
139        */
140       if (pe.type != PERF_TYPE_SOFTWARE)
141         {
142           /* Exclude kernel and hypervisor */
143           pe.exclude_kernel = 1;
144           pe.exclude_hv = 1;
145         }
146
147       cpu = vm->cpu_id;
148
149       fd = perf_event_open (&pe, 0, cpu, -1, 0);
150       if (fd == -1)
151         {
152           clib_unix_warning ("event open: type %d config %d", c->pe_type,
153                              c->pe_config);
154           return;
155         }
156
157       if (pe.type != PERF_TYPE_SOFTWARE)
158         {
159           p = mmap (0, pm->page_size, PROT_READ, MAP_SHARED, fd, 0);
160           if (p == MAP_FAILED)
161             {
162               clib_unix_warning ("mmap");
163               close (fd);
164               return;
165             }
166         }
167       else
168         p = 0;
169
170       if (ioctl (fd, PERF_EVENT_IOC_RESET, 0) < 0)
171         clib_unix_warning ("reset ioctl");
172
173       if (ioctl (fd, PERF_EVENT_IOC_ENABLE, 0) < 0)
174         clib_unix_warning ("enable ioctl");
175
176       pm->perf_event_pages[i][my_thread_index] = (void *) p;
177       pm->pm_fds[i][my_thread_index] = fd;
178     }
179
180   /*
181    * Hardware events must be all opened and enabled before aquiring
182    * pmc indices, otherwise the pmc indices might be out-dated.
183    */
184   for (i = 0; i < limit; i++)
185     {
186       p =
187         (struct perf_event_mmap_page *)
188         pm->perf_event_pages[i][my_thread_index];
189
190       /*
191        * Software event counters - and others not capable of being
192        * read via the "rdpmc" instruction - will be read
193        * by system calls.
194        */
195       if (p == 0 || p->cap_user_rdpmc == 0)
196         index = ~0;
197       else
198         index = p->index - 1;
199
200       pm->rdpmc_indices[i][my_thread_index] = index;
201     }
202
203   pm->n_active = i;
204   /* Enable the main loop counter snapshot mechanism */
205   vm->vlib_node_runtime_perf_counter_cb = read_current_perf_counters;
206 }
207
208 static void
209 disable_events (perfmon_main_t * pm)
210 {
211   vlib_main_t *vm = vlib_get_main ();
212   u32 my_thread_index = vm->thread_index;
213   int i;
214
215   /* Stop main loop collection */
216   vm->vlib_node_runtime_perf_counter_cb = 0;
217
218   for (i = 0; i < pm->n_active; i++)
219     {
220       if (pm->pm_fds[i][my_thread_index] == 0)
221         continue;
222
223       if (ioctl (pm->pm_fds[i][my_thread_index], PERF_EVENT_IOC_DISABLE, 0) <
224           0)
225         clib_unix_warning ("disable ioctl");
226
227       if (pm->perf_event_pages[i][my_thread_index])
228         if (munmap (pm->perf_event_pages[i][my_thread_index],
229                     pm->page_size) < 0)
230           clib_unix_warning ("munmap");
231
232       (void) close (pm->pm_fds[i][my_thread_index]);
233       pm->pm_fds[i][my_thread_index] = 0;
234     }
235 }
236
237 static void
238 worker_thread_start_event (vlib_main_t * vm)
239 {
240   perfmon_main_t *pm = &perfmon_main;
241
242   enable_current_events (pm);
243   vm->worker_thread_main_loop_callback = 0;
244 }
245
246 static void
247 worker_thread_stop_event (vlib_main_t * vm)
248 {
249   perfmon_main_t *pm = &perfmon_main;
250   disable_events (pm);
251   vm->worker_thread_main_loop_callback = 0;
252 }
253
254 static void
255 start_event (perfmon_main_t * pm, f64 now, uword event_data)
256 {
257   int i;
258   int last_set;
259   int all = 0;
260   pm->current_event = 0;
261
262   if (vec_len (pm->single_events_to_collect) == 0)
263     {
264       pm->state = PERFMON_STATE_OFF;
265       return;
266     }
267
268   last_set = clib_bitmap_last_set (pm->thread_bitmap);
269   all = (last_set == ~0);
270
271   pm->state = PERFMON_STATE_RUNNING;
272   clear_counters (pm);
273
274   /* Start collection on thread 0? */
275   if (all || clib_bitmap_get (pm->thread_bitmap, 0))
276     {
277       /* Start collection on this thread */
278       enable_current_events (pm);
279     }
280
281   /* And also on worker threads */
282   for (i = 1; i < vec_len (vlib_mains); i++)
283     {
284       if (vlib_mains[i] == 0)
285         continue;
286
287       if (all || clib_bitmap_get (pm->thread_bitmap, i))
288         vlib_mains[i]->worker_thread_main_loop_callback = (void *)
289           worker_thread_start_event;
290     }
291 }
292
293 void
294 scrape_and_clear_counters (perfmon_main_t * pm)
295 {
296   int i, j, k;
297   vlib_main_t *vm = pm->vlib_main;
298   vlib_main_t *stat_vm;
299   vlib_node_main_t *nm;
300   vlib_node_t ***node_dups = 0;
301   vlib_node_t **nodes;
302   vlib_node_t *n;
303   perfmon_capture_t *c;
304   perfmon_event_config_t *current_event;
305   uword *p;
306   u8 *counter_name;
307   u64 vectors_this_counter;
308
309   /* snapshoot the nodes, including pm counters */
310   vlib_worker_thread_barrier_sync (vm);
311
312   for (j = 0; j < vec_len (vlib_mains); j++)
313     {
314       stat_vm = vlib_mains[j];
315       if (stat_vm == 0)
316         continue;
317
318       nm = &stat_vm->node_main;
319
320       for (i = 0; i < vec_len (nm->nodes); i++)
321         {
322           n = nm->nodes[i];
323           vlib_node_sync_stats (stat_vm, n);
324         }
325
326       nodes = 0;
327       vec_validate (nodes, vec_len (nm->nodes) - 1);
328       vec_add1 (node_dups, nodes);
329
330       /* Snapshoot and clear the per-node perfmon counters */
331       for (i = 0; i < vec_len (nm->nodes); i++)
332         {
333           n = nm->nodes[i];
334           nodes[i] = clib_mem_alloc (sizeof (*n));
335           clib_memcpy_fast (nodes[i], n, sizeof (*n));
336           n->stats_total.perf_counter0_ticks = 0;
337           n->stats_total.perf_counter1_ticks = 0;
338           n->stats_total.perf_counter_vectors = 0;
339           n->stats_last_clear.perf_counter0_ticks = 0;
340           n->stats_last_clear.perf_counter1_ticks = 0;
341           n->stats_last_clear.perf_counter_vectors = 0;
342         }
343     }
344
345   vlib_worker_thread_barrier_release (vm);
346
347   for (j = 0; j < vec_len (vlib_mains); j++)
348     {
349       stat_vm = vlib_mains[j];
350       if (stat_vm == 0)
351         continue;
352
353       nodes = node_dups[j];
354
355       for (i = 0; i < vec_len (nodes); i++)
356         {
357           u8 *capture_name;
358
359           n = nodes[i];
360
361           if (n->stats_total.perf_counter0_ticks == 0 &&
362               n->stats_total.perf_counter1_ticks == 0)
363             goto skip_this_node;
364
365           for (k = 0; k < 2; k++)
366             {
367               u64 counter_value, counter_last_clear;
368
369               /*
370                * We collect 2 counters at once, except for the
371                * last counter when the user asks for an odd number of
372                * counters
373                */
374               if ((pm->current_event + k)
375                   >= vec_len (pm->single_events_to_collect))
376                 break;
377
378               if (k == 0)
379                 {
380                   counter_value = n->stats_total.perf_counter0_ticks;
381                   counter_last_clear =
382                     n->stats_last_clear.perf_counter0_ticks;
383                 }
384               else
385                 {
386                   counter_value = n->stats_total.perf_counter1_ticks;
387                   counter_last_clear =
388                     n->stats_last_clear.perf_counter1_ticks;
389                 }
390
391               capture_name = format (0, "t%d-%v%c", j, n->name, 0);
392
393               p = hash_get_mem (pm->capture_by_thread_and_node_name,
394                                 capture_name);
395
396               if (p == 0)
397                 {
398                   pool_get (pm->capture_pool, c);
399                   memset (c, 0, sizeof (*c));
400                   c->thread_and_node_name = capture_name;
401                   hash_set_mem (pm->capture_by_thread_and_node_name,
402                                 capture_name, c - pm->capture_pool);
403                 }
404               else
405                 {
406                   c = pool_elt_at_index (pm->capture_pool, p[0]);
407                   vec_free (capture_name);
408                 }
409
410               /* Snapshoot counters, etc. into the capture */
411               current_event = pm->single_events_to_collect
412                 + pm->current_event + k;
413               counter_name = (u8 *) current_event->name;
414               vectors_this_counter = n->stats_total.perf_counter_vectors -
415                 n->stats_last_clear.perf_counter_vectors;
416
417               vec_add1 (c->counter_names, counter_name);
418               vec_add1 (c->counter_values,
419                         counter_value - counter_last_clear);
420               vec_add1 (c->vectors_this_counter, vectors_this_counter);
421             }
422         skip_this_node:
423           clib_mem_free (n);
424         }
425       vec_free (nodes);
426     }
427   vec_free (node_dups);
428 }
429
430 static void
431 handle_timeout (vlib_main_t * vm, perfmon_main_t * pm, f64 now)
432 {
433   int i;
434   int last_set, all;
435
436   last_set = clib_bitmap_last_set (pm->thread_bitmap);
437   all = (last_set == ~0);
438
439   if (all || clib_bitmap_get (pm->thread_bitmap, 0))
440     disable_events (pm);
441
442   /* And also on worker threads */
443   for (i = 1; i < vec_len (vlib_mains); i++)
444     {
445       if (vlib_mains[i] == 0)
446         continue;
447       if (all || clib_bitmap_get (pm->thread_bitmap, i))
448         vlib_mains[i]->worker_thread_main_loop_callback = (void *)
449           worker_thread_stop_event;
450     }
451
452   /* Make sure workers have stopped collection */
453   if (i > 1)
454     {
455       f64 deadman = vlib_time_now (vm) + 1.0;
456
457       for (i = 1; i < vec_len (vlib_mains); i++)
458         {
459           /* Has the worker actually stopped collecting data? */
460           while (vlib_mains[i]->worker_thread_main_loop_callback)
461             {
462               if (vlib_time_now (vm) > deadman)
463                 {
464                   clib_warning ("Thread %d deadman timeout!", i);
465                   break;
466                 }
467               vlib_process_suspend (pm->vlib_main, 1e-3);
468             }
469         }
470     }
471   scrape_and_clear_counters (pm);
472   pm->current_event += pm->n_active;
473   if (pm->current_event >= vec_len (pm->single_events_to_collect))
474     {
475       pm->current_event = 0;
476       pm->state = PERFMON_STATE_OFF;
477       return;
478     }
479
480   if (all || clib_bitmap_get (pm->thread_bitmap, 0))
481     enable_current_events (pm);
482
483   /* And also on worker threads */
484   for (i = 1; i < vec_len (vlib_mains); i++)
485     {
486       if (vlib_mains[i] == 0)
487         continue;
488       if (all || clib_bitmap_get (pm->thread_bitmap, i))
489         vlib_mains[i]->worker_thread_main_loop_callback = (void *)
490           worker_thread_start_event;
491     }
492 }
493
494 static uword
495 perfmon_periodic_process (vlib_main_t * vm,
496                           vlib_node_runtime_t * rt, vlib_frame_t * f)
497 {
498   perfmon_main_t *pm = &perfmon_main;
499   f64 now;
500   uword *event_data = 0;
501   uword event_type;
502   int i;
503
504   while (1)
505     {
506       if (pm->state == PERFMON_STATE_RUNNING)
507         vlib_process_wait_for_event_or_clock (vm, pm->timeout_interval);
508       else
509         vlib_process_wait_for_event (vm);
510
511       now = vlib_time_now (vm);
512
513       event_type = vlib_process_get_events (vm, (uword **) & event_data);
514
515       switch (event_type)
516         {
517         case PERFMON_START:
518           for (i = 0; i < vec_len (event_data); i++)
519             start_event (pm, now, event_data[i]);
520           break;
521
522           /* Handle timeout */
523         case ~0:
524           handle_timeout (vm, pm, now);
525           break;
526
527         default:
528           clib_warning ("Unexpected event %d", event_type);
529           break;
530         }
531       vec_reset_length (event_data);
532     }
533   return 0;                     /* or not */
534 }
535
536 /* *INDENT-OFF* */
537 VLIB_REGISTER_NODE (perfmon_periodic_node) =
538 {
539   .function = perfmon_periodic_process,
540   .type = VLIB_NODE_TYPE_PROCESS,
541   .name = "perfmon-periodic-process",
542 };
543 /* *INDENT-ON* */
544
545 /*
546  * fd.io coding-style-patch-verification: ON
547  *
548  * Local Variables:
549  * eval: (c-set-style "gnu")
550  * End:
551  */