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