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]);
55 if (read (pm->pm_fds[i][my_thread_index], &sw_value,
56 sizeof (sw_value)) != sizeof (sw_value))
59 ("counter read failed, disable collection...");
60 vm->vlib_node_runtime_perf_counter_cb = 0;
69 clear_counters (perfmon_main_t * pm)
72 vlib_main_t *vm = pm->vlib_main;
77 vlib_worker_thread_barrier_sync (vm);
79 for (j = 0; j < vec_len (vlib_mains); j++)
81 stat_vm = vlib_mains[j];
85 nm = &stat_vm->node_main;
87 /* Clear the node runtime perfmon counters */
88 for (i = 0; i < vec_len (nm->nodes); i++)
91 vlib_node_sync_stats (stat_vm, n);
94 /* And clear the node perfmon counters */
95 for (i = 0; i < vec_len (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;
106 vlib_worker_thread_barrier_release (vm);
110 enable_current_events (perfmon_main_t * pm)
112 struct perf_event_attr pe;
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;
122 if ((pm->current_event + 1) < vec_len (pm->single_events_to_collect))
125 for (i = 0; i < limit; i++)
127 c = vec_elt_at_index (pm->single_events_to_collect,
128 pm->current_event + i);
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;
137 * Note: excluding the kernel makes the
138 * (software) context-switch counter read 0...
140 if (pe.type != PERF_TYPE_SOFTWARE)
142 /* Exclude kernel and hypervisor */
143 pe.exclude_kernel = 1;
149 fd = perf_event_open (&pe, 0, cpu, -1, 0);
152 clib_unix_warning ("event open: type %d config %d", c->pe_type,
157 if (pe.type != PERF_TYPE_SOFTWARE)
159 p = mmap (0, pm->page_size, PROT_READ, MAP_SHARED, fd, 0);
162 clib_unix_warning ("mmap");
170 if (ioctl (fd, PERF_EVENT_IOC_RESET, 0) < 0)
171 clib_unix_warning ("reset ioctl");
173 if (ioctl (fd, PERF_EVENT_IOC_ENABLE, 0) < 0)
174 clib_unix_warning ("enable ioctl");
176 pm->perf_event_pages[i][my_thread_index] = (void *) p;
177 pm->pm_fds[i][my_thread_index] = fd;
181 * Hardware events must be all opened and enabled before aquiring
182 * pmc indices, otherwise the pmc indices might be out-dated.
184 for (i = 0; i < limit; i++)
187 (struct perf_event_mmap_page *)
188 pm->perf_event_pages[i][my_thread_index];
191 * Software event counters - and others not capable of being
192 * read via the "rdpmc" instruction - will be read
195 if (p == 0 || p->cap_user_rdpmc == 0)
198 index = p->index - 1;
200 pm->rdpmc_indices[i][my_thread_index] = index;
204 /* Enable the main loop counter snapshot mechanism */
205 vm->vlib_node_runtime_perf_counter_cb = read_current_perf_counters;
209 disable_events (perfmon_main_t * pm)
211 vlib_main_t *vm = vlib_get_main ();
212 u32 my_thread_index = vm->thread_index;
215 /* Stop main loop collection */
216 vm->vlib_node_runtime_perf_counter_cb = 0;
218 for (i = 0; i < pm->n_active; i++)
220 if (pm->pm_fds[i][my_thread_index] == 0)
223 if (ioctl (pm->pm_fds[i][my_thread_index], PERF_EVENT_IOC_DISABLE, 0) <
225 clib_unix_warning ("disable ioctl");
227 if (pm->perf_event_pages[i][my_thread_index])
228 if (munmap (pm->perf_event_pages[i][my_thread_index],
230 clib_unix_warning ("munmap");
232 (void) close (pm->pm_fds[i][my_thread_index]);
233 pm->pm_fds[i][my_thread_index] = 0;
238 worker_thread_start_event (vlib_main_t * vm)
240 perfmon_main_t *pm = &perfmon_main;
242 enable_current_events (pm);
243 vm->worker_thread_main_loop_callback = 0;
247 worker_thread_stop_event (vlib_main_t * vm)
249 perfmon_main_t *pm = &perfmon_main;
251 vm->worker_thread_main_loop_callback = 0;
255 start_event (perfmon_main_t * pm, f64 now, uword event_data)
260 pm->current_event = 0;
262 if (vec_len (pm->single_events_to_collect) == 0)
264 pm->state = PERFMON_STATE_OFF;
268 last_set = clib_bitmap_last_set (pm->thread_bitmap);
269 all = (last_set == ~0);
271 pm->state = PERFMON_STATE_RUNNING;
274 /* Start collection on thread 0? */
275 if (all || clib_bitmap_get (pm->thread_bitmap, 0))
277 /* Start collection on this thread */
278 enable_current_events (pm);
281 /* And also on worker threads */
282 for (i = 1; i < vec_len (vlib_mains); i++)
284 if (vlib_mains[i] == 0)
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;
294 scrape_and_clear_counters (perfmon_main_t * pm)
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;
303 perfmon_capture_t *c;
304 perfmon_event_config_t *current_event;
307 u64 vectors_this_counter;
309 /* snapshoot the nodes, including pm counters */
310 vlib_worker_thread_barrier_sync (vm);
312 for (j = 0; j < vec_len (vlib_mains); j++)
314 stat_vm = vlib_mains[j];
318 nm = &stat_vm->node_main;
320 for (i = 0; i < vec_len (nm->nodes); i++)
323 vlib_node_sync_stats (stat_vm, n);
327 vec_validate (nodes, vec_len (nm->nodes) - 1);
328 vec_add1 (node_dups, nodes);
330 /* Snapshoot and clear the per-node perfmon counters */
331 for (i = 0; i < vec_len (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;
345 vlib_worker_thread_barrier_release (vm);
347 for (j = 0; j < vec_len (vlib_mains); j++)
349 stat_vm = vlib_mains[j];
353 nodes = node_dups[j];
355 for (i = 0; i < vec_len (nodes); i++)
361 if (n->stats_total.perf_counter0_ticks == 0 &&
362 n->stats_total.perf_counter1_ticks == 0)
365 for (k = 0; k < 2; k++)
367 u64 counter_value, counter_last_clear;
370 * We collect 2 counters at once, except for the
371 * last counter when the user asks for an odd number of
374 if ((pm->current_event + k)
375 >= vec_len (pm->single_events_to_collect))
380 counter_value = n->stats_total.perf_counter0_ticks;
382 n->stats_last_clear.perf_counter0_ticks;
386 counter_value = n->stats_total.perf_counter1_ticks;
388 n->stats_last_clear.perf_counter1_ticks;
391 capture_name = format (0, "t%d-%v%c", j, n->name, 0);
393 p = hash_get_mem (pm->capture_by_thread_and_node_name,
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);
406 c = pool_elt_at_index (pm->capture_pool, p[0]);
407 vec_free (capture_name);
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;
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);
427 vec_free (node_dups);
431 handle_timeout (vlib_main_t * vm, perfmon_main_t * pm, f64 now)
436 last_set = clib_bitmap_last_set (pm->thread_bitmap);
437 all = (last_set == ~0);
439 if (all || clib_bitmap_get (pm->thread_bitmap, 0))
442 /* And also on worker threads */
443 for (i = 1; i < vec_len (vlib_mains); i++)
445 if (vlib_mains[i] == 0)
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;
452 /* Make sure workers have stopped collection */
455 f64 deadman = vlib_time_now (vm) + 1.0;
457 for (i = 1; i < vec_len (vlib_mains); i++)
459 /* Has the worker actually stopped collecting data? */
460 while (vlib_mains[i]->worker_thread_main_loop_callback)
462 if (vlib_time_now (vm) > deadman)
464 clib_warning ("Thread %d deadman timeout!", i);
467 vlib_process_suspend (pm->vlib_main, 1e-3);
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))
475 pm->current_event = 0;
476 pm->state = PERFMON_STATE_OFF;
480 if (all || clib_bitmap_get (pm->thread_bitmap, 0))
481 enable_current_events (pm);
483 /* And also on worker threads */
484 for (i = 1; i < vec_len (vlib_mains); i++)
486 if (vlib_mains[i] == 0)
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;
495 perfmon_periodic_process (vlib_main_t * vm,
496 vlib_node_runtime_t * rt, vlib_frame_t * f)
498 perfmon_main_t *pm = &perfmon_main;
500 uword *event_data = 0;
506 if (pm->state == PERFMON_STATE_RUNNING)
507 vlib_process_wait_for_event_or_clock (vm, pm->timeout_interval);
509 vlib_process_wait_for_event (vm);
511 now = vlib_time_now (vm);
513 event_type = vlib_process_get_events (vm, (uword **) & event_data);
518 for (i = 0; i < vec_len (event_data); i++)
519 start_event (pm, now, event_data[i]);
524 handle_timeout (vm, pm, now);
528 clib_warning ("Unexpected event %d", event_type);
531 vec_reset_length (event_data);
533 return 0; /* or not */
537 VLIB_REGISTER_NODE (perfmon_periodic_node) =
539 .function = perfmon_periodic_process,
540 .type = VLIB_NODE_TYPE_PROCESS,
541 .name = "perfmon-periodic-process",
546 * fd.io coding-style-patch-verification: ON
549 * eval: (c-set-style "gnu")