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 c = vec_elt_at_index (pm->single_events_to_collect,
134 pm->current_event + i);
136 memset (&pe, 0, sizeof (struct perf_event_attr));
137 pe.type = c->pe_type;
138 pe.size = sizeof (struct perf_event_attr);
139 pe.config = c->pe_config;
143 * Note: excluding the kernel makes the
144 * (software) context-switch counter read 0...
146 if (pe.type != PERF_TYPE_SOFTWARE)
148 /* Exclude kernel and hypervisor */
149 pe.exclude_kernel = 1;
155 fd = perf_event_open (&pe, 0, cpu, -1, 0);
158 clib_unix_warning ("event open: type %d config %d", c->pe_type,
163 if (pe.type != PERF_TYPE_SOFTWARE)
165 p = mmap (0, pm->page_size, PROT_READ, MAP_SHARED, fd, 0);
168 clib_unix_warning ("mmap");
176 if (ioctl (fd, PERF_EVENT_IOC_RESET, 0) < 0)
177 clib_unix_warning ("reset ioctl");
179 if (ioctl (fd, PERF_EVENT_IOC_ENABLE, 0) < 0)
180 clib_unix_warning ("enable ioctl");
182 pm->perf_event_pages[i][my_thread_index] = (void *) p;
183 pm->pm_fds[i][my_thread_index] = fd;
187 * Hardware events must be all opened and enabled before aquiring
188 * pmc indices, otherwise the pmc indices might be out-dated.
190 for (i = 0; i < limit; i++)
193 (struct perf_event_mmap_page *)
194 pm->perf_event_pages[i][my_thread_index];
197 * Software event counters - and others not capable of being
198 * read via the "rdpmc" instruction - will be read
201 if (p == 0 || p->cap_user_rdpmc == 0)
204 index = p->index - 1;
206 pm->rdpmc_indices[i][my_thread_index] = index;
210 /* Enable the main loop counter snapshot mechanism */
211 clib_callback_enable_disable
212 (vm->vlib_node_runtime_perf_counter_cbs,
213 vm->vlib_node_runtime_perf_counter_cb_tmp,
214 vm->worker_thread_main_loop_callback_lock,
215 read_current_perf_counters, 1 /* enable */ );
219 disable_events (perfmon_main_t * pm)
221 vlib_main_t *vm = vlib_get_main ();
222 u32 my_thread_index = vm->thread_index;
225 /* Stop main loop collection */
226 clib_callback_enable_disable
227 (vm->vlib_node_runtime_perf_counter_cbs,
228 vm->vlib_node_runtime_perf_counter_cb_tmp,
229 vm->worker_thread_main_loop_callback_lock,
230 read_current_perf_counters, 0 /* enable */ );
232 for (i = 0; i < pm->n_active; i++)
234 if (pm->pm_fds[i][my_thread_index] == 0)
237 if (ioctl (pm->pm_fds[i][my_thread_index], PERF_EVENT_IOC_DISABLE, 0) <
239 clib_unix_warning ("disable ioctl");
241 if (pm->perf_event_pages[i][my_thread_index])
242 if (munmap (pm->perf_event_pages[i][my_thread_index],
244 clib_unix_warning ("munmap");
246 (void) close (pm->pm_fds[i][my_thread_index]);
247 pm->pm_fds[i][my_thread_index] = 0;
252 worker_thread_start_event (vlib_main_t * vm)
254 perfmon_main_t *pm = &perfmon_main;
256 clib_callback_enable_disable (vm->worker_thread_main_loop_callbacks,
257 vm->worker_thread_main_loop_callback_tmp,
258 vm->worker_thread_main_loop_callback_lock,
259 worker_thread_start_event, 0 /* enable */ );
260 enable_current_events (pm);
264 worker_thread_stop_event (vlib_main_t * vm)
266 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_stop_event, 0 /* enable */ );
275 start_event (perfmon_main_t * pm, f64 now, uword event_data)
280 pm->current_event = 0;
282 if (vec_len (pm->single_events_to_collect) == 0)
284 pm->state = PERFMON_STATE_OFF;
288 last_set = clib_bitmap_last_set (pm->thread_bitmap);
289 all = (last_set == ~0);
291 pm->state = PERFMON_STATE_RUNNING;
294 /* Start collection on thread 0? */
295 if (all || clib_bitmap_get (pm->thread_bitmap, 0))
297 /* Start collection on this thread */
298 enable_current_events (pm);
301 /* And also on worker threads */
302 for (i = 1; i < vec_len (vlib_mains); i++)
304 if (vlib_mains[i] == 0)
307 if (all || clib_bitmap_get (pm->thread_bitmap, i))
308 clib_callback_enable_disable
309 (vlib_mains[i]->worker_thread_main_loop_callbacks,
310 vlib_mains[i]->worker_thread_main_loop_callback_tmp,
311 vlib_mains[i]->worker_thread_main_loop_callback_lock,
312 (void *) worker_thread_start_event, 1 /* enable */ );
317 scrape_and_clear_counters (perfmon_main_t * pm)
320 vlib_main_t *vm = pm->vlib_main;
321 vlib_main_t *stat_vm;
322 vlib_node_main_t *nm;
323 vlib_node_t ***node_dups = 0;
326 perfmon_capture_t *c;
327 perfmon_event_config_t *current_event;
330 u64 vectors_this_counter;
332 /* snapshoot the nodes, including pm counters */
333 vlib_worker_thread_barrier_sync (vm);
335 for (j = 0; j < vec_len (vlib_mains); j++)
337 stat_vm = vlib_mains[j];
341 nm = &stat_vm->node_main;
343 for (i = 0; i < vec_len (nm->nodes); i++)
346 vlib_node_sync_stats (stat_vm, n);
350 vec_validate (nodes, vec_len (nm->nodes) - 1);
351 vec_add1 (node_dups, nodes);
353 /* Snapshoot and clear the per-node perfmon counters */
354 for (i = 0; i < vec_len (nm->nodes); i++)
357 nodes[i] = clib_mem_alloc (sizeof (*n));
358 clib_memcpy_fast (nodes[i], n, sizeof (*n));
359 n->stats_total.perf_counter0_ticks = 0;
360 n->stats_total.perf_counter1_ticks = 0;
361 n->stats_total.perf_counter_vectors = 0;
362 n->stats_last_clear.perf_counter0_ticks = 0;
363 n->stats_last_clear.perf_counter1_ticks = 0;
364 n->stats_last_clear.perf_counter_vectors = 0;
368 vlib_worker_thread_barrier_release (vm);
370 for (j = 0; j < vec_len (vlib_mains); j++)
372 stat_vm = vlib_mains[j];
376 nodes = node_dups[j];
378 for (i = 0; i < vec_len (nodes); i++)
384 if (n->stats_total.perf_counter0_ticks == 0 &&
385 n->stats_total.perf_counter1_ticks == 0)
388 for (k = 0; k < 2; k++)
390 u64 counter_value, counter_last_clear;
393 * We collect 2 counters at once, except for the
394 * last counter when the user asks for an odd number of
397 if ((pm->current_event + k)
398 >= vec_len (pm->single_events_to_collect))
403 counter_value = n->stats_total.perf_counter0_ticks;
405 n->stats_last_clear.perf_counter0_ticks;
409 counter_value = n->stats_total.perf_counter1_ticks;
411 n->stats_last_clear.perf_counter1_ticks;
414 capture_name = format (0, "t%d-%v%c", j, n->name, 0);
416 p = hash_get_mem (pm->capture_by_thread_and_node_name,
421 pool_get (pm->capture_pool, c);
422 memset (c, 0, sizeof (*c));
423 c->thread_and_node_name = capture_name;
424 hash_set_mem (pm->capture_by_thread_and_node_name,
425 capture_name, c - pm->capture_pool);
429 c = pool_elt_at_index (pm->capture_pool, p[0]);
430 vec_free (capture_name);
433 /* Snapshoot counters, etc. into the capture */
434 current_event = pm->single_events_to_collect
435 + pm->current_event + k;
436 counter_name = (u8 *) current_event->name;
437 vectors_this_counter = n->stats_total.perf_counter_vectors -
438 n->stats_last_clear.perf_counter_vectors;
440 vec_add1 (c->counter_names, counter_name);
441 vec_add1 (c->counter_values,
442 counter_value - counter_last_clear);
443 vec_add1 (c->vectors_this_counter, vectors_this_counter);
450 vec_free (node_dups);
454 handle_timeout (vlib_main_t * vm, perfmon_main_t * pm, f64 now)
459 last_set = clib_bitmap_last_set (pm->thread_bitmap);
460 all = (last_set == ~0);
462 if (all || clib_bitmap_get (pm->thread_bitmap, 0))
465 /* And also on worker threads */
466 for (i = 1; i < vec_len (vlib_mains); i++)
468 if (vlib_mains[i] == 0)
470 if (all || clib_bitmap_get (pm->thread_bitmap, i))
471 clib_callback_enable_disable
472 (vlib_mains[i]->worker_thread_main_loop_callbacks,
473 vlib_mains[i]->worker_thread_main_loop_callback_tmp,
474 vlib_mains[i]->worker_thread_main_loop_callback_lock,
475 (void *) worker_thread_stop_event, 1 /* enable */ );
478 /* Make sure workers have stopped collection */
481 f64 deadman = vlib_time_now (vm) + 1.0;
483 for (i = 1; i < vec_len (vlib_mains); i++)
485 /* Has the worker actually stopped collecting data? */
486 while (clib_callback_is_set
487 (vlib_mains[i]->worker_thread_main_loop_callbacks,
488 vlib_mains[i]->worker_thread_main_loop_callback_lock,
489 read_current_perf_counters))
491 if (vlib_time_now (vm) > deadman)
493 clib_warning ("Thread %d deadman timeout!", i);
496 vlib_process_suspend (pm->vlib_main, 1e-3);
500 scrape_and_clear_counters (pm);
501 pm->current_event += pm->n_active;
502 if (pm->current_event >= vec_len (pm->single_events_to_collect))
504 pm->current_event = 0;
505 pm->state = PERFMON_STATE_OFF;
509 if (all || clib_bitmap_get (pm->thread_bitmap, 0))
510 enable_current_events (pm);
512 /* And also on worker threads */
513 for (i = 1; i < vec_len (vlib_mains); i++)
515 if (vlib_mains[i] == 0)
517 if (all || clib_bitmap_get (pm->thread_bitmap, i))
518 clib_callback_enable_disable
519 (vlib_mains[i]->worker_thread_main_loop_callbacks,
520 vlib_mains[i]->worker_thread_main_loop_callback_tmp,
521 vlib_mains[i]->worker_thread_main_loop_callback_lock,
522 worker_thread_start_event, 1 /* enable */ );
527 perfmon_periodic_process (vlib_main_t * vm,
528 vlib_node_runtime_t * rt, vlib_frame_t * f)
530 perfmon_main_t *pm = &perfmon_main;
532 uword *event_data = 0;
538 if (pm->state == PERFMON_STATE_RUNNING)
539 vlib_process_wait_for_event_or_clock (vm, pm->timeout_interval);
541 vlib_process_wait_for_event (vm);
543 now = vlib_time_now (vm);
545 event_type = vlib_process_get_events (vm, (uword **) & event_data);
550 for (i = 0; i < vec_len (event_data); i++)
551 start_event (pm, now, event_data[i]);
556 handle_timeout (vm, pm, now);
560 clib_warning ("Unexpected event %d", event_type);
563 vec_reset_length (event_data);
565 return 0; /* or not */
569 VLIB_REGISTER_NODE (perfmon_periodic_node) =
571 .function = perfmon_periodic_process,
572 .type = VLIB_NODE_TYPE_PROCESS,
573 .name = "perfmon-periodic-process",
578 * fd.io coding-style-patch-verification: ON
581 * eval: (c-set-style "gnu")