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");
174 pm->perf_event_pages[i][my_thread_index] = (void *) p;
175 pm->pm_fds[i][my_thread_index] = fd;
179 * Hardware events must be all opened and enabled before aquiring
180 * pmc indices, otherwise the pmc indices might be out-dated.
182 for (i = 0; i < limit; i++)
185 (struct perf_event_mmap_page *)
186 pm->perf_event_pages[i][my_thread_index];
189 * Software event counters - and others not capable of being
190 * read via the "rdpmc" instruction - will be read
193 if (p == 0 || p->cap_user_rdpmc == 0)
196 index = p->index - 1;
198 pm->rdpmc_indices[i][my_thread_index] = index;
202 /* Enable the main loop counter snapshot mechanism */
203 vm->vlib_node_runtime_perf_counter_cb = read_current_perf_counters;
207 disable_events (perfmon_main_t * pm)
209 vlib_main_t *vm = vlib_get_main ();
210 u32 my_thread_index = vm->thread_index;
213 /* Stop main loop collection */
214 vm->vlib_node_runtime_perf_counter_cb = 0;
216 for (i = 0; i < pm->n_active; i++)
218 if (pm->pm_fds[i][my_thread_index] == 0)
221 if (ioctl (pm->pm_fds[i][my_thread_index], PERF_EVENT_IOC_DISABLE, 0) <
223 clib_unix_warning ("disable ioctl");
225 if (pm->perf_event_pages[i][my_thread_index])
226 if (munmap (pm->perf_event_pages[i][my_thread_index],
228 clib_unix_warning ("munmap");
230 (void) close (pm->pm_fds[i][my_thread_index]);
231 pm->pm_fds[i][my_thread_index] = 0;
236 worker_thread_start_event (vlib_main_t * vm)
238 perfmon_main_t *pm = &perfmon_main;
240 enable_current_events (pm);
241 vm->worker_thread_main_loop_callback = 0;
245 worker_thread_stop_event (vlib_main_t * vm)
247 perfmon_main_t *pm = &perfmon_main;
249 vm->worker_thread_main_loop_callback = 0;
253 start_event (perfmon_main_t * pm, f64 now, uword event_data)
258 pm->current_event = 0;
260 if (vec_len (pm->single_events_to_collect) == 0)
262 pm->state = PERFMON_STATE_OFF;
266 last_set = clib_bitmap_last_set (pm->thread_bitmap);
267 all = (last_set == ~0);
269 pm->state = PERFMON_STATE_RUNNING;
272 /* Start collection on thread 0? */
273 if (all || clib_bitmap_get (pm->thread_bitmap, 0))
275 /* Start collection on this thread */
276 enable_current_events (pm);
279 /* And also on worker threads */
280 for (i = 1; i < vec_len (vlib_mains); i++)
282 if (vlib_mains[i] == 0)
285 if (all || clib_bitmap_get (pm->thread_bitmap, i))
286 vlib_mains[i]->worker_thread_main_loop_callback = (void *)
287 worker_thread_start_event;
292 scrape_and_clear_counters (perfmon_main_t * pm)
295 vlib_main_t *vm = pm->vlib_main;
296 vlib_main_t *stat_vm;
297 vlib_node_main_t *nm;
298 vlib_node_t ***node_dups = 0;
301 perfmon_capture_t *c;
302 perfmon_event_config_t *current_event;
305 u64 vectors_this_counter;
307 /* snapshoot the nodes, including pm counters */
308 vlib_worker_thread_barrier_sync (vm);
310 for (j = 0; j < vec_len (vlib_mains); j++)
312 stat_vm = vlib_mains[j];
316 nm = &stat_vm->node_main;
318 for (i = 0; i < vec_len (nm->nodes); i++)
321 vlib_node_sync_stats (stat_vm, n);
325 vec_validate (nodes, vec_len (nm->nodes) - 1);
326 vec_add1 (node_dups, nodes);
328 /* Snapshoot and clear the per-node perfmon counters */
329 for (i = 0; i < vec_len (nm->nodes); i++)
332 nodes[i] = clib_mem_alloc (sizeof (*n));
333 clib_memcpy_fast (nodes[i], n, sizeof (*n));
334 n->stats_total.perf_counter0_ticks = 0;
335 n->stats_total.perf_counter1_ticks = 0;
336 n->stats_total.perf_counter_vectors = 0;
337 n->stats_last_clear.perf_counter0_ticks = 0;
338 n->stats_last_clear.perf_counter1_ticks = 0;
339 n->stats_last_clear.perf_counter_vectors = 0;
343 vlib_worker_thread_barrier_release (vm);
345 for (j = 0; j < vec_len (vlib_mains); j++)
347 stat_vm = vlib_mains[j];
351 nodes = node_dups[j];
353 for (i = 0; i < vec_len (nodes); i++)
359 if (n->stats_total.perf_counter0_ticks == 0 &&
360 n->stats_total.perf_counter1_ticks == 0)
363 for (k = 0; k < 2; k++)
365 u64 counter_value, counter_last_clear;
368 * We collect 2 counters at once, except for the
369 * last counter when the user asks for an odd number of
372 if ((pm->current_event + k)
373 >= vec_len (pm->single_events_to_collect))
378 counter_value = n->stats_total.perf_counter0_ticks;
380 n->stats_last_clear.perf_counter0_ticks;
384 counter_value = n->stats_total.perf_counter1_ticks;
386 n->stats_last_clear.perf_counter1_ticks;
389 capture_name = format (0, "t%d-%v%c", j, n->name, 0);
391 p = hash_get_mem (pm->capture_by_thread_and_node_name,
396 pool_get (pm->capture_pool, c);
397 memset (c, 0, sizeof (*c));
398 c->thread_and_node_name = capture_name;
399 hash_set_mem (pm->capture_by_thread_and_node_name,
400 capture_name, c - pm->capture_pool);
404 c = pool_elt_at_index (pm->capture_pool, p[0]);
405 vec_free (capture_name);
408 /* Snapshoot counters, etc. into the capture */
409 current_event = pm->single_events_to_collect
410 + pm->current_event + k;
411 counter_name = (u8 *) current_event->name;
412 vectors_this_counter = n->stats_total.perf_counter_vectors -
413 n->stats_last_clear.perf_counter_vectors;
415 vec_add1 (c->counter_names, counter_name);
416 vec_add1 (c->counter_values,
417 counter_value - counter_last_clear);
418 vec_add1 (c->vectors_this_counter, vectors_this_counter);
425 vec_free (node_dups);
429 handle_timeout (vlib_main_t * vm, perfmon_main_t * pm, f64 now)
434 last_set = clib_bitmap_last_set (pm->thread_bitmap);
435 all = (last_set == ~0);
437 if (all || clib_bitmap_get (pm->thread_bitmap, 0))
440 /* And also on worker threads */
441 for (i = 1; i < vec_len (vlib_mains); i++)
443 if (vlib_mains[i] == 0)
445 if (all || clib_bitmap_get (pm->thread_bitmap, i))
446 vlib_mains[i]->worker_thread_main_loop_callback = (void *)
447 worker_thread_stop_event;
450 /* Make sure workers have stopped collection */
453 f64 deadman = vlib_time_now (vm) + 1.0;
455 for (i = 1; i < vec_len (vlib_mains); i++)
457 /* Has the worker actually stopped collecting data? */
458 while (vlib_mains[i]->worker_thread_main_loop_callback)
460 if (vlib_time_now (vm) > deadman)
462 clib_warning ("Thread %d deadman timeout!", i);
465 vlib_process_suspend (pm->vlib_main, 1e-3);
469 scrape_and_clear_counters (pm);
470 pm->current_event += pm->n_active;
471 if (pm->current_event >= vec_len (pm->single_events_to_collect))
473 pm->current_event = 0;
474 pm->state = PERFMON_STATE_OFF;
478 if (all || clib_bitmap_get (pm->thread_bitmap, 0))
479 enable_current_events (pm);
481 /* And also on worker threads */
482 for (i = 1; i < vec_len (vlib_mains); i++)
484 if (vlib_mains[i] == 0)
486 if (all || clib_bitmap_get (pm->thread_bitmap, i))
487 vlib_mains[i]->worker_thread_main_loop_callback = (void *)
488 worker_thread_start_event;
493 perfmon_periodic_process (vlib_main_t * vm,
494 vlib_node_runtime_t * rt, vlib_frame_t * f)
496 perfmon_main_t *pm = &perfmon_main;
498 uword *event_data = 0;
504 if (pm->state == PERFMON_STATE_RUNNING)
505 vlib_process_wait_for_event_or_clock (vm, pm->timeout_interval);
507 vlib_process_wait_for_event (vm);
509 now = vlib_time_now (vm);
511 event_type = vlib_process_get_events (vm, (uword **) & event_data);
516 for (i = 0; i < vec_len (event_data); i++)
517 start_event (pm, now, event_data[i]);
522 handle_timeout (vm, pm, now);
526 clib_warning ("Unexpected event %d", event_type);
529 vec_reset_length (event_data);
531 return 0; /* or not */
535 VLIB_REGISTER_NODE (perfmon_periodic_node) =
537 .function = perfmon_periodic_process,
538 .type = VLIB_NODE_TYPE_PROCESS,
539 .name = "perfmon-periodic-process",
544 * fd.io coding-style-patch-verification: ON
547 * eval: (c-set-style "gnu")