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)
40 perfmon_main_t *pm = &perfmon_main;
41 uword my_thread_index = vm->thread_index;
45 for (i = 0; i < pm->n_active; i++)
47 cc = (i == 0) ? c0 : c1;
48 if (pm->rdpmc_indices[i][my_thread_index] != ~0)
49 *cc = clib_rdpmc ((int) pm->rdpmc_indices[i][my_thread_index]);
53 if (read (pm->pm_fds[i][my_thread_index], &sw_value,
54 sizeof (sw_value)) != sizeof (sw_value))
57 ("counter read failed, disable collection...");
58 vm->vlib_node_runtime_perf_counter_cb = 0;
67 clear_counters (perfmon_main_t * pm)
70 vlib_main_t *vm = pm->vlib_main;
75 vlib_worker_thread_barrier_sync (vm);
77 for (j = 0; j < vec_len (vlib_mains); j++)
79 stat_vm = vlib_mains[j];
83 nm = &stat_vm->node_main;
85 /* Clear the node runtime perfmon counters */
86 for (i = 0; i < vec_len (nm->nodes); i++)
89 vlib_node_sync_stats (stat_vm, n);
92 /* And clear the node perfmon counters */
93 for (i = 0; i < vec_len (nm->nodes); i++)
96 n->stats_total.perf_counter0_ticks = 0;
97 n->stats_total.perf_counter1_ticks = 0;
98 n->stats_total.perf_counter_vectors = 0;
99 n->stats_last_clear.perf_counter0_ticks = 0;
100 n->stats_last_clear.perf_counter1_ticks = 0;
101 n->stats_last_clear.perf_counter_vectors = 0;
104 vlib_worker_thread_barrier_release (vm);
108 enable_current_events (perfmon_main_t * pm)
110 struct perf_event_attr pe;
112 struct perf_event_mmap_page *p = 0;
113 perfmon_event_config_t *c;
114 vlib_main_t *vm = vlib_get_main ();
115 u32 my_thread_index = vm->thread_index;
120 if ((pm->current_event + 1) < vec_len (pm->single_events_to_collect))
123 for (i = 0; i < limit; i++)
125 c = vec_elt_at_index (pm->single_events_to_collect,
126 pm->current_event + i);
128 memset (&pe, 0, sizeof (struct perf_event_attr));
129 pe.type = c->pe_type;
130 pe.size = sizeof (struct perf_event_attr);
131 pe.config = c->pe_config;
135 * Note: excluding the kernel makes the
136 * (software) context-switch counter read 0...
138 if (pe.type != PERF_TYPE_SOFTWARE)
140 /* Exclude kernel and hypervisor */
141 pe.exclude_kernel = 1;
147 fd = perf_event_open (&pe, 0, cpu, -1, 0);
150 clib_unix_warning ("event open: type %d config %d", c->pe_type,
155 if (pe.type != PERF_TYPE_SOFTWARE)
157 p = mmap (0, pm->page_size, PROT_READ, MAP_SHARED, fd, 0);
160 clib_unix_warning ("mmap");
168 if (ioctl (fd, PERF_EVENT_IOC_RESET, 0) < 0)
169 clib_unix_warning ("reset ioctl");
171 if (ioctl (fd, PERF_EVENT_IOC_ENABLE, 0) < 0)
172 clib_unix_warning ("enable ioctl");
175 * Software event counters - and others not capable of being
176 * read via the "rdpmc" instruction - will be read
179 if (pe.type == PERF_TYPE_SOFTWARE || p->cap_user_rdpmc == 0)
182 index = p->index - 1;
184 pm->rdpmc_indices[i][my_thread_index] = index;
185 pm->perf_event_pages[i][my_thread_index] = (void *) p;
186 pm->pm_fds[i][my_thread_index] = fd;
190 /* Enable the main loop counter snapshot mechanism */
191 vm->vlib_node_runtime_perf_counter_cb = read_current_perf_counters;
195 disable_events (perfmon_main_t * pm)
197 vlib_main_t *vm = vlib_get_main ();
198 u32 my_thread_index = vm->thread_index;
201 /* Stop main loop collection */
202 vm->vlib_node_runtime_perf_counter_cb = 0;
204 for (i = 0; i < pm->n_active; i++)
206 if (pm->pm_fds[i][my_thread_index] == 0)
209 if (ioctl (pm->pm_fds[i][my_thread_index], PERF_EVENT_IOC_DISABLE, 0) <
211 clib_unix_warning ("disable ioctl");
213 if (pm->perf_event_pages[i][my_thread_index])
214 if (munmap (pm->perf_event_pages[i][my_thread_index],
216 clib_unix_warning ("munmap");
218 (void) close (pm->pm_fds[i][my_thread_index]);
219 pm->pm_fds[i][my_thread_index] = 0;
224 worker_thread_start_event (vlib_main_t * vm)
226 perfmon_main_t *pm = &perfmon_main;
228 enable_current_events (pm);
229 vm->worker_thread_main_loop_callback = 0;
233 worker_thread_stop_event (vlib_main_t * vm)
235 perfmon_main_t *pm = &perfmon_main;
237 vm->worker_thread_main_loop_callback = 0;
241 start_event (perfmon_main_t * pm, f64 now, uword event_data)
246 pm->current_event = 0;
248 if (vec_len (pm->single_events_to_collect) == 0)
250 pm->state = PERFMON_STATE_OFF;
254 last_set = clib_bitmap_last_set (pm->thread_bitmap);
255 all = (last_set == ~0);
257 pm->state = PERFMON_STATE_RUNNING;
260 /* Start collection on thread 0? */
261 if (all || clib_bitmap_get (pm->thread_bitmap, 0))
263 /* Start collection on this thread */
264 enable_current_events (pm);
267 /* And also on worker threads */
268 for (i = 1; i < vec_len (vlib_mains); i++)
270 if (vlib_mains[i] == 0)
273 if (all || clib_bitmap_get (pm->thread_bitmap, i))
274 vlib_mains[i]->worker_thread_main_loop_callback = (void *)
275 worker_thread_start_event;
280 scrape_and_clear_counters (perfmon_main_t * pm)
283 vlib_main_t *vm = pm->vlib_main;
284 vlib_main_t *stat_vm;
285 vlib_node_main_t *nm;
286 vlib_node_t ***node_dups = 0;
289 perfmon_capture_t *c;
290 perfmon_event_config_t *current_event;
293 u64 vectors_this_counter;
295 /* snapshoot the nodes, including pm counters */
296 vlib_worker_thread_barrier_sync (vm);
298 for (j = 0; j < vec_len (vlib_mains); j++)
300 stat_vm = vlib_mains[j];
304 nm = &stat_vm->node_main;
306 for (i = 0; i < vec_len (nm->nodes); i++)
309 vlib_node_sync_stats (stat_vm, n);
313 vec_validate (nodes, vec_len (nm->nodes) - 1);
314 vec_add1 (node_dups, nodes);
316 /* Snapshoot and clear the per-node perfmon counters */
317 for (i = 0; i < vec_len (nm->nodes); i++)
320 nodes[i] = clib_mem_alloc (sizeof (*n));
321 clib_memcpy_fast (nodes[i], n, sizeof (*n));
322 n->stats_total.perf_counter0_ticks = 0;
323 n->stats_total.perf_counter1_ticks = 0;
324 n->stats_total.perf_counter_vectors = 0;
325 n->stats_last_clear.perf_counter0_ticks = 0;
326 n->stats_last_clear.perf_counter1_ticks = 0;
327 n->stats_last_clear.perf_counter_vectors = 0;
331 vlib_worker_thread_barrier_release (vm);
333 for (j = 0; j < vec_len (vlib_mains); j++)
335 stat_vm = vlib_mains[j];
339 nodes = node_dups[j];
341 for (i = 0; i < vec_len (nodes); i++)
347 if (n->stats_total.perf_counter0_ticks == 0 &&
348 n->stats_total.perf_counter1_ticks == 0)
351 for (k = 0; k < 2; k++)
353 u64 counter_value, counter_last_clear;
356 * We collect 2 counters at once, except for the
357 * last counter when the user asks for an odd number of
360 if ((pm->current_event + k)
361 >= vec_len (pm->single_events_to_collect))
366 counter_value = n->stats_total.perf_counter0_ticks;
368 n->stats_last_clear.perf_counter0_ticks;
372 counter_value = n->stats_total.perf_counter1_ticks;
374 n->stats_last_clear.perf_counter1_ticks;
377 capture_name = format (0, "t%d-%v%c", j, n->name, 0);
379 p = hash_get_mem (pm->capture_by_thread_and_node_name,
384 pool_get (pm->capture_pool, c);
385 memset (c, 0, sizeof (*c));
386 c->thread_and_node_name = capture_name;
387 hash_set_mem (pm->capture_by_thread_and_node_name,
388 capture_name, c - pm->capture_pool);
392 c = pool_elt_at_index (pm->capture_pool, p[0]);
393 vec_free (capture_name);
396 /* Snapshoot counters, etc. into the capture */
397 current_event = pm->single_events_to_collect
398 + pm->current_event + k;
399 counter_name = (u8 *) current_event->name;
400 vectors_this_counter = n->stats_total.perf_counter_vectors -
401 n->stats_last_clear.perf_counter_vectors;
403 vec_add1 (c->counter_names, counter_name);
404 vec_add1 (c->counter_values,
405 counter_value - counter_last_clear);
406 vec_add1 (c->vectors_this_counter, vectors_this_counter);
413 vec_free (node_dups);
417 handle_timeout (vlib_main_t * vm, perfmon_main_t * pm, f64 now)
422 last_set = clib_bitmap_last_set (pm->thread_bitmap);
423 all = (last_set == ~0);
425 if (all || clib_bitmap_get (pm->thread_bitmap, 0))
428 /* And also on worker threads */
429 for (i = 1; i < vec_len (vlib_mains); i++)
431 if (vlib_mains[i] == 0)
433 if (all || clib_bitmap_get (pm->thread_bitmap, i))
434 vlib_mains[i]->worker_thread_main_loop_callback = (void *)
435 worker_thread_stop_event;
438 /* Make sure workers have stopped collection */
441 f64 deadman = vlib_time_now (vm) + 1.0;
443 for (i = 1; i < vec_len (vlib_mains); i++)
445 /* Has the worker actually stopped collecting data? */
446 while (vlib_mains[i]->worker_thread_main_loop_callback)
448 if (vlib_time_now (vm) > deadman)
450 clib_warning ("Thread %d deadman timeout!", i);
453 vlib_process_suspend (pm->vlib_main, 1e-3);
457 scrape_and_clear_counters (pm);
458 pm->current_event += pm->n_active;
459 if (pm->current_event >= vec_len (pm->single_events_to_collect))
461 pm->current_event = 0;
462 pm->state = PERFMON_STATE_OFF;
466 if (all || clib_bitmap_get (pm->thread_bitmap, 0))
467 enable_current_events (pm);
469 /* And also on worker threads */
470 for (i = 1; i < vec_len (vlib_mains); i++)
472 if (vlib_mains[i] == 0)
474 if (all || clib_bitmap_get (pm->thread_bitmap, i))
475 vlib_mains[i]->worker_thread_main_loop_callback = (void *)
476 worker_thread_start_event;
481 perfmon_periodic_process (vlib_main_t * vm,
482 vlib_node_runtime_t * rt, vlib_frame_t * f)
484 perfmon_main_t *pm = &perfmon_main;
486 uword *event_data = 0;
492 if (pm->state == PERFMON_STATE_RUNNING)
493 vlib_process_wait_for_event_or_clock (vm, pm->timeout_interval);
495 vlib_process_wait_for_event (vm);
497 now = vlib_time_now (vm);
499 event_type = vlib_process_get_events (vm, (uword **) & event_data);
504 for (i = 0; i < vec_len (event_data); i++)
505 start_event (pm, now, event_data[i]);
510 handle_timeout (vm, pm, now);
514 clib_warning ("Unexpected event %d", event_type);
517 vec_reset_length (event_data);
519 return 0; /* or not */
523 VLIB_REGISTER_NODE (perfmon_periodic_node) =
525 .function = perfmon_periodic_process,
526 .type = VLIB_NODE_TYPE_PROCESS,
527 .name = "perfmon-periodic-process",
532 * fd.io coding-style-patch-verification: ON
535 * eval: (c-set-style "gnu")