2 * perfmon_periodic.c - skeleton plug-in periodic function
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
26 perf_event_open (struct perf_event_attr *hw_event, pid_t pid, int cpu,
27 int group_fd, unsigned long flags)
31 ret = syscall (__NR_perf_event_open, hw_event, pid, cpu, group_fd, flags);
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)
42 perfmon_main_t *pm = &perfmon_main;
43 uword my_thread_index = vm->thread_index;
47 for (i = 0; i < pm->n_active; i++)
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]);
56 if ((read_result = read (pm->pm_fds[i][my_thread_index], &sw_value,
57 sizeof (sw_value)) != sizeof (sw_value)))
60 ("counter read returned %d, expected %d",
61 read_result, sizeof (sw_value));
62 clib_callback_enable_disable
63 (vm->vlib_node_runtime_perf_counter_cbs,
64 vm->vlib_node_runtime_perf_counter_cb_tmp,
65 vm->worker_thread_main_loop_callback_lock,
66 read_current_perf_counters, 0 /* enable */ );
75 clear_counters (perfmon_main_t * pm)
78 vlib_main_t *vm = pm->vlib_main;
83 vlib_worker_thread_barrier_sync (vm);
85 for (j = 0; j < vec_len (vlib_mains); j++)
87 stat_vm = vlib_mains[j];
91 nm = &stat_vm->node_main;
93 /* Clear the node runtime perfmon counters */
94 for (i = 0; i < vec_len (nm->nodes); i++)
97 vlib_node_sync_stats (stat_vm, n);
100 /* And clear the node perfmon counters */
101 for (i = 0; i < vec_len (nm->nodes); i++)
104 n->stats_total.perf_counter0_ticks = 0;
105 n->stats_total.perf_counter1_ticks = 0;
106 n->stats_total.perf_counter_vectors = 0;
107 n->stats_last_clear.perf_counter0_ticks = 0;
108 n->stats_last_clear.perf_counter1_ticks = 0;
109 n->stats_last_clear.perf_counter_vectors = 0;
112 vlib_worker_thread_barrier_release (vm);
116 enable_current_events (perfmon_main_t * pm)
118 struct perf_event_attr pe;
120 struct perf_event_mmap_page *p = 0;
121 perfmon_event_config_t *c;
122 vlib_main_t *vm = vlib_get_main ();
123 u32 my_thread_index = vm->thread_index;
128 if ((pm->current_event + 1) < vec_len (pm->single_events_to_collect))
131 for (i = 0; i < limit; i++)
133 vec_validate (pm->pm_fds[i], vec_len (vlib_mains) - 1);
134 vec_validate (pm->perf_event_pages[i], vec_len (vlib_mains) - 1);
135 vec_validate (pm->rdpmc_indices[i], vec_len (vlib_mains) - 1);
137 c = vec_elt_at_index (pm->single_events_to_collect,
138 pm->current_event + i);
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;
147 * Note: excluding the kernel makes the
148 * (software) context-switch counter read 0...
150 if (pe.type != PERF_TYPE_SOFTWARE)
152 /* Exclude kernel and hypervisor */
153 pe.exclude_kernel = 1;
159 fd = perf_event_open (&pe, 0, cpu, -1, 0);
162 clib_unix_warning ("event open: type %d config %d", c->pe_type,
167 if (pe.type != PERF_TYPE_SOFTWARE)
169 p = mmap (0, pm->page_size, PROT_READ, MAP_SHARED, fd, 0);
172 clib_unix_warning ("mmap");
176 CLIB_MEM_UNPOISON (p, pm->page_size);
181 if (ioctl (fd, PERF_EVENT_IOC_RESET, 0) < 0)
182 clib_unix_warning ("reset ioctl");
184 if (ioctl (fd, PERF_EVENT_IOC_ENABLE, 0) < 0)
185 clib_unix_warning ("enable ioctl");
187 pm->perf_event_pages[i][my_thread_index] = (void *) p;
188 pm->pm_fds[i][my_thread_index] = fd;
192 * Hardware events must be all opened and enabled before aquiring
193 * pmc indices, otherwise the pmc indices might be out-dated.
195 for (i = 0; i < limit; i++)
198 (struct perf_event_mmap_page *)
199 pm->perf_event_pages[i][my_thread_index];
202 * Software event counters - and others not capable of being
203 * read via the "rdpmc" instruction - will be read
206 if (p == 0 || p->cap_user_rdpmc == 0)
209 index = p->index - 1;
211 pm->rdpmc_indices[i][my_thread_index] = index;
215 /* Enable the main loop counter snapshot mechanism */
216 clib_callback_enable_disable
217 (vm->vlib_node_runtime_perf_counter_cbs,
218 vm->vlib_node_runtime_perf_counter_cb_tmp,
219 vm->worker_thread_main_loop_callback_lock,
220 read_current_perf_counters, 1 /* enable */ );
224 disable_events (perfmon_main_t * pm)
226 vlib_main_t *vm = vlib_get_main ();
227 u32 my_thread_index = vm->thread_index;
230 /* Stop main loop collection */
231 clib_callback_enable_disable
232 (vm->vlib_node_runtime_perf_counter_cbs,
233 vm->vlib_node_runtime_perf_counter_cb_tmp,
234 vm->worker_thread_main_loop_callback_lock,
235 read_current_perf_counters, 0 /* enable */ );
237 for (i = 0; i < pm->n_active; i++)
239 if (pm->pm_fds[i][my_thread_index] == 0)
242 if (ioctl (pm->pm_fds[i][my_thread_index], PERF_EVENT_IOC_DISABLE, 0) <
244 clib_unix_warning ("disable ioctl");
246 if (pm->perf_event_pages[i][my_thread_index])
248 if (munmap (pm->perf_event_pages[i][my_thread_index],
250 clib_unix_warning ("munmap");
251 CLIB_MEM_POISON (pm->perf_event_pages[i][my_thread_index],
253 pm->perf_event_pages[i][my_thread_index] = 0;
256 (void) close (pm->pm_fds[i][my_thread_index]);
257 pm->pm_fds[i][my_thread_index] = 0;
263 worker_thread_start_event (vlib_main_t * vm)
265 perfmon_main_t *pm = &perfmon_main;
267 clib_callback_enable_disable (vm->worker_thread_main_loop_callbacks,
268 vm->worker_thread_main_loop_callback_tmp,
269 vm->worker_thread_main_loop_callback_lock,
270 worker_thread_start_event, 0 /* enable */ );
271 enable_current_events (pm);
275 worker_thread_stop_event (vlib_main_t * vm)
277 perfmon_main_t *pm = &perfmon_main;
278 clib_callback_enable_disable (vm->worker_thread_main_loop_callbacks,
279 vm->worker_thread_main_loop_callback_tmp,
280 vm->worker_thread_main_loop_callback_lock,
281 worker_thread_stop_event, 0 /* enable */ );
286 start_event (perfmon_main_t * pm, f64 now, uword event_data)
291 pm->current_event = 0;
293 if (vec_len (pm->single_events_to_collect) == 0)
295 pm->state = PERFMON_STATE_OFF;
299 last_set = clib_bitmap_last_set (pm->thread_bitmap);
300 all = (last_set == ~0);
302 pm->state = PERFMON_STATE_RUNNING;
305 /* Start collection on thread 0? */
306 if (all || clib_bitmap_get (pm->thread_bitmap, 0))
308 /* Start collection on this thread */
309 enable_current_events (pm);
312 /* And also on worker threads */
313 for (i = 1; i < vec_len (vlib_mains); i++)
315 if (vlib_mains[i] == 0)
318 if (all || clib_bitmap_get (pm->thread_bitmap, i))
319 clib_callback_enable_disable
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 */ );
328 scrape_and_clear_counters (perfmon_main_t * pm)
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;
337 perfmon_capture_t *c;
338 perfmon_event_config_t *current_event;
341 u64 vectors_this_counter;
343 /* snapshoot the nodes, including pm counters */
344 vlib_worker_thread_barrier_sync (vm);
346 for (j = 0; j < vec_len (vlib_mains); j++)
348 stat_vm = vlib_mains[j];
352 nm = &stat_vm->node_main;
354 for (i = 0; i < vec_len (nm->nodes); i++)
357 vlib_node_sync_stats (stat_vm, n);
361 vec_validate (nodes, vec_len (nm->nodes) - 1);
362 vec_add1 (node_dups, nodes);
364 /* Snapshoot and clear the per-node perfmon counters */
365 for (i = 0; i < vec_len (nm->nodes); i++)
368 nodes[i] = clib_mem_alloc (sizeof (*n));
369 clib_memcpy_fast (nodes[i], n, sizeof (*n));
370 n->stats_total.perf_counter0_ticks = 0;
371 n->stats_total.perf_counter1_ticks = 0;
372 n->stats_total.perf_counter_vectors = 0;
373 n->stats_last_clear.perf_counter0_ticks = 0;
374 n->stats_last_clear.perf_counter1_ticks = 0;
375 n->stats_last_clear.perf_counter_vectors = 0;
379 vlib_worker_thread_barrier_release (vm);
381 for (j = 0; j < vec_len (vlib_mains); j++)
383 stat_vm = vlib_mains[j];
387 nodes = node_dups[j];
389 for (i = 0; i < vec_len (nodes); i++)
395 if (n->stats_total.perf_counter0_ticks == 0 &&
396 n->stats_total.perf_counter1_ticks == 0)
399 for (k = 0; k < 2; k++)
401 u64 counter_value, counter_last_clear;
404 * We collect 2 counters at once, except for the
405 * last counter when the user asks for an odd number of
408 if ((pm->current_event + k)
409 >= vec_len (pm->single_events_to_collect))
414 counter_value = n->stats_total.perf_counter0_ticks;
416 n->stats_last_clear.perf_counter0_ticks;
420 counter_value = n->stats_total.perf_counter1_ticks;
422 n->stats_last_clear.perf_counter1_ticks;
425 capture_name = format (0, "t%d-%v%c", j, n->name, 0);
427 p = hash_get_mem (pm->capture_by_thread_and_node_name,
432 pool_get (pm->capture_pool, c);
433 memset (c, 0, sizeof (*c));
434 c->thread_and_node_name = capture_name;
435 hash_set_mem (pm->capture_by_thread_and_node_name,
436 capture_name, c - pm->capture_pool);
440 c = pool_elt_at_index (pm->capture_pool, p[0]);
441 vec_free (capture_name);
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 -
449 n->stats_last_clear.perf_counter_vectors;
451 vec_add1 (c->counter_names, counter_name);
452 vec_add1 (c->counter_values,
453 counter_value - counter_last_clear);
454 vec_add1 (c->vectors_this_counter, vectors_this_counter);
461 vec_free (node_dups);
465 handle_timeout (vlib_main_t * vm, perfmon_main_t * pm, f64 now)
470 last_set = clib_bitmap_last_set (pm->thread_bitmap);
471 all = (last_set == ~0);
473 if (all || clib_bitmap_get (pm->thread_bitmap, 0))
476 /* And also on worker threads */
477 for (i = 1; i < vec_len (vlib_mains); i++)
479 if (vlib_mains[i] == 0)
481 if (all || clib_bitmap_get (pm->thread_bitmap, i))
482 clib_callback_enable_disable
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 */ );
489 /* Make sure workers have stopped collection */
492 f64 deadman = vlib_time_now (vm) + 1.0;
494 for (i = 1; i < vec_len (vlib_mains); i++)
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,
500 read_current_perf_counters))
502 if (vlib_time_now (vm) > deadman)
504 clib_warning ("Thread %d deadman timeout!", i);
507 vlib_process_suspend (pm->vlib_main, 1e-3);
511 scrape_and_clear_counters (pm);
512 pm->current_event += pm->n_active;
513 if (pm->current_event >= vec_len (pm->single_events_to_collect))
515 pm->current_event = 0;
516 pm->state = PERFMON_STATE_OFF;
520 if (all || clib_bitmap_get (pm->thread_bitmap, 0))
521 enable_current_events (pm);
523 /* And also on worker threads */
524 for (i = 1; i < vec_len (vlib_mains); i++)
526 if (vlib_mains[i] == 0)
528 if (all || clib_bitmap_get (pm->thread_bitmap, i))
529 clib_callback_enable_disable
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 */ );
538 perfmon_periodic_process (vlib_main_t * vm,
539 vlib_node_runtime_t * rt, vlib_frame_t * f)
541 perfmon_main_t *pm = &perfmon_main;
543 uword *event_data = 0;
549 if (pm->state == PERFMON_STATE_RUNNING)
550 vlib_process_wait_for_event_or_clock (vm, pm->timeout_interval);
552 vlib_process_wait_for_event (vm);
554 now = vlib_time_now (vm);
556 event_type = vlib_process_get_events (vm, (uword **) & event_data);
561 for (i = 0; i < vec_len (event_data); i++)
562 start_event (pm, now, event_data[i]);
567 handle_timeout (vm, pm, now);
571 clib_warning ("Unexpected event %d", event_type);
574 vec_reset_length (event_data);
576 return 0; /* or not */
580 VLIB_REGISTER_NODE (perfmon_periodic_node) =
582 .function = perfmon_periodic_process,
583 .type = VLIB_NODE_TYPE_PROCESS,
584 .name = "perfmon-periodic-process",
589 * fd.io coding-style-patch-verification: ON
592 * eval: (c-set-style "gnu")