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_node_runtime_perf_callback_data_t * data,
37 vlib_node_runtime_perf_callback_args_t * args)
40 perfmon_main_t *pm = &perfmon_main;
41 perfmon_thread_t *pt = data->u[0].v;
45 if (PREDICT_FALSE (args->call_type == VLIB_NODE_RUNTIME_PERF_RESET))
48 if (args->call_type == VLIB_NODE_RUNTIME_PERF_BEFORE)
53 for (i = 0; i < pm->n_active; i++)
55 if (pt->rdpmc_indices[i] != ~0)
56 cc[i] = clib_rdpmc ((int) pt->rdpmc_indices[i]);
61 if ((read_result = read (pt->pm_fds[i], &sw_value,
62 sizeof (sw_value))) != sizeof (sw_value))
65 ("counter read returned %d, expected %d",
66 read_result, sizeof (sw_value));
67 clib_callback_data_enable_disable
68 (&args->vm->vlib_node_runtime_perf_callbacks,
69 read_current_perf_counters, 0 /* enable */ );
76 if (args->call_type == VLIB_NODE_RUNTIME_PERF_AFTER)
78 u32 node_index = args->node->node_index;
79 vec_validate (pt->counters, node_index);
80 pt->counters[node_index].ticks[0] += c[0] - pt->c[0];
81 pt->counters[node_index].ticks[1] += c[1] - pt->c[1];
82 pt->counters[node_index].vectors += args->packets;
87 clear_counters (perfmon_main_t * pm)
90 vlib_main_t *vm = pm->vlib_main;
96 vlib_worker_thread_barrier_sync (vm);
98 for (j = 0; j < vec_len (vlib_mains); j++)
100 stat_vm = vlib_mains[j];
105 len = vec_len (pt->counters);
109 clib_memset (pt->counters, 0, len * sizeof (pt->counters[0]));
111 vlib_worker_thread_barrier_release (vm);
115 enable_current_events (perfmon_main_t * pm)
117 struct perf_event_attr pe;
119 struct perf_event_mmap_page *p = 0;
120 perfmon_event_config_t *c;
121 vlib_main_t *vm = vlib_get_main ();
122 u32 my_thread_index = vm->thread_index;
123 perfmon_thread_t *pt = pm->threads[my_thread_index];
127 vlib_node_runtime_perf_callback_data_t cbdata = { 0 };
128 cbdata.fp = read_current_perf_counters;
132 if ((pm->current_event + 1) < vec_len (pm->single_events_to_collect))
135 for (i = 0; i < limit; i++)
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 pt->perf_event_pages[i] = (void *) p;
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++)
197 p = (struct perf_event_mmap_page *) pt->perf_event_pages[i];
200 * Software event counters - and others not capable of being
201 * read via the "rdpmc" instruction - will be read
204 if (p == 0 || p->cap_user_rdpmc == 0)
207 index = p->index - 1;
209 pt->rdpmc_indices[i] = index;
213 /* Enable the main loop counter snapshot mechanism */
214 clib_callback_data_add (&vm->vlib_node_runtime_perf_callbacks, cbdata);
218 disable_events (perfmon_main_t * pm)
220 vlib_main_t *vm = vlib_get_main ();
221 u32 my_thread_index = vm->thread_index;
222 perfmon_thread_t *pt = pm->threads[my_thread_index];
225 /* Stop main loop collection */
226 clib_callback_data_remove (&vm->vlib_node_runtime_perf_callbacks,
227 read_current_perf_counters);
229 for (i = 0; i < pm->n_active; i++)
231 if (pt->pm_fds[i] == 0)
234 if (ioctl (pt->pm_fds[i], PERF_EVENT_IOC_DISABLE, 0) < 0)
235 clib_unix_warning ("disable ioctl");
237 if (pt->perf_event_pages[i])
239 if (munmap (pt->perf_event_pages[i], pm->page_size) < 0)
240 clib_unix_warning ("munmap");
241 pt->perf_event_pages[i] = 0;
244 (void) close (pt->pm_fds[i]);
250 worker_thread_start_event (vlib_main_t * vm)
252 perfmon_main_t *pm = &perfmon_main;
254 clib_callback_enable_disable (vm->worker_thread_main_loop_callbacks,
255 vm->worker_thread_main_loop_callback_tmp,
256 vm->worker_thread_main_loop_callback_lock,
257 worker_thread_start_event, 0 /* disable */ );
258 enable_current_events (pm);
262 worker_thread_stop_event (vlib_main_t * vm)
264 perfmon_main_t *pm = &perfmon_main;
265 clib_callback_enable_disable (vm->worker_thread_main_loop_callbacks,
266 vm->worker_thread_main_loop_callback_tmp,
267 vm->worker_thread_main_loop_callback_lock,
268 worker_thread_stop_event, 0 /* disable */ );
273 start_event (perfmon_main_t * pm, f64 now, uword event_data)
278 pm->current_event = 0;
280 if (vec_len (pm->single_events_to_collect) == 0)
282 pm->state = PERFMON_STATE_OFF;
286 last_set = clib_bitmap_last_set (pm->thread_bitmap);
287 all = (last_set == ~0);
289 pm->state = PERFMON_STATE_RUNNING;
292 /* Start collection on thread 0? */
293 if (all || clib_bitmap_get (pm->thread_bitmap, 0))
295 /* Start collection on this thread */
296 enable_current_events (pm);
299 /* And also on worker threads */
300 for (i = 1; i < vec_len (vlib_mains); i++)
302 if (vlib_mains[i] == 0)
305 if (all || clib_bitmap_get (pm->thread_bitmap, i))
306 clib_callback_enable_disable
307 (vlib_mains[i]->worker_thread_main_loop_callbacks,
308 vlib_mains[i]->worker_thread_main_loop_callback_tmp,
309 vlib_mains[i]->worker_thread_main_loop_callback_lock,
310 (void *) worker_thread_start_event, 1 /* enable */ );
315 scrape_and_clear_counters (perfmon_main_t * pm)
318 vlib_main_t *vm = pm->vlib_main;
319 vlib_main_t *stat_vm;
320 vlib_node_main_t *nm;
321 perfmon_counters_t *ctr;
322 perfmon_counters_t *ctrs;
323 perfmon_counters_t **ctr_dups = 0;
324 perfmon_thread_t *pt;
325 perfmon_capture_t *c;
326 perfmon_event_config_t *current_event;
331 /* snapshoot the nodes, including pm counters */
332 vlib_worker_thread_barrier_sync (vm);
334 for (j = 0; j < vec_len (vlib_mains); j++)
336 stat_vm = vlib_mains[j];
341 len = vec_len (pt->counters);
345 vec_validate (ctrs, len - 1);
346 clib_memcpy (ctrs, pt->counters, len * sizeof (pt->counters[0]));
347 clib_memset (pt->counters, 0, len * sizeof (pt->counters[0]));
349 vec_add1 (ctr_dups, ctrs);
352 vlib_worker_thread_barrier_release (vm);
354 for (j = 0; j < vec_len (vlib_mains); j++)
356 stat_vm = vlib_mains[j];
363 for (i = 0; i < vec_len (ctrs); i++)
368 nm = &stat_vm->node_main;
370 if (ctr->ticks[0] == 0 && ctr->ticks[1] == 0)
373 for (k = 0; k < 2; k++)
376 * We collect 2 counters at once, except for the
377 * last counter when the user asks for an odd number of
380 if ((pm->current_event + k)
381 >= vec_len (pm->single_events_to_collect))
384 capture_name = format (0, "t%d-%v%c", j, nm->nodes[i]->name, 0);
386 p = hash_get_mem (pm->capture_by_thread_and_node_name,
391 pool_get (pm->capture_pool, c);
392 memset (c, 0, sizeof (*c));
393 c->thread_and_node_name = capture_name;
394 hash_set_mem (pm->capture_by_thread_and_node_name,
395 capture_name, c - pm->capture_pool);
399 c = pool_elt_at_index (pm->capture_pool, p[0]);
400 vec_free (capture_name);
403 /* Snapshoot counters, etc. into the capture */
404 current_event = pm->single_events_to_collect
405 + pm->current_event + k;
406 counter_name = (u8 *) current_event->name;
408 vec_add1 (c->counter_names, counter_name);
409 vec_add1 (c->counter_values, ctr->ticks[k]);
410 vec_add1 (c->vectors_this_counter, ctr->vectors);
419 handle_timeout (vlib_main_t * vm, perfmon_main_t * pm, f64 now)
424 last_set = clib_bitmap_last_set (pm->thread_bitmap);
425 all = (last_set == ~0);
427 if (all || clib_bitmap_get (pm->thread_bitmap, 0))
430 /* And also on worker threads */
431 for (i = 1; i < vec_len (vlib_mains); i++)
433 if (vlib_mains[i] == 0)
435 if (all || clib_bitmap_get (pm->thread_bitmap, i))
436 clib_callback_enable_disable
437 (vlib_mains[i]->worker_thread_main_loop_callbacks,
438 vlib_mains[i]->worker_thread_main_loop_callback_tmp,
439 vlib_mains[i]->worker_thread_main_loop_callback_lock,
440 (void *) worker_thread_stop_event, 1 /* enable */ );
443 /* Make sure workers have stopped collection */
446 f64 deadman = vlib_time_now (vm) + 1.0;
448 for (i = 1; i < vec_len (vlib_mains); i++)
450 /* Has the worker actually stopped collecting data? */
451 while (clib_callback_data_is_set
452 (&vm->vlib_node_runtime_perf_callbacks,
453 read_current_perf_counters))
455 if (vlib_time_now (vm) > deadman)
457 clib_warning ("Thread %d deadman timeout!", i);
460 vlib_process_suspend (pm->vlib_main, 1e-3);
464 scrape_and_clear_counters (pm);
465 pm->current_event += pm->n_active;
466 if (pm->current_event >= vec_len (pm->single_events_to_collect))
468 pm->current_event = 0;
469 pm->state = PERFMON_STATE_OFF;
473 if (all || clib_bitmap_get (pm->thread_bitmap, 0))
474 enable_current_events (pm);
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 worker_thread_start_event, 0 /* disable */ );
491 perfmon_periodic_process (vlib_main_t * vm,
492 vlib_node_runtime_t * rt, vlib_frame_t * f)
494 perfmon_main_t *pm = &perfmon_main;
496 uword *event_data = 0;
502 if (pm->state == PERFMON_STATE_RUNNING)
503 vlib_process_wait_for_event_or_clock (vm, pm->timeout_interval);
505 vlib_process_wait_for_event (vm);
507 now = vlib_time_now (vm);
509 event_type = vlib_process_get_events (vm, (uword **) & event_data);
514 for (i = 0; i < vec_len (event_data); i++)
515 start_event (pm, now, event_data[i]);
520 handle_timeout (vm, pm, now);
524 clib_warning ("Unexpected event %d", event_type);
527 vec_reset_length (event_data);
529 return 0; /* or not */
533 VLIB_REGISTER_NODE (perfmon_periodic_node) =
535 .function = perfmon_periodic_process,
536 .type = VLIB_NODE_TYPE_PROCESS,
537 .name = "perfmon-periodic-process",
542 * fd.io coding-style-patch-verification: ON
545 * eval: (c-set-style "gnu")