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>
25 perf_event_open (struct perf_event_attr *hw_event, pid_t pid, int cpu,
26 int group_fd, unsigned long flags)
30 ret = syscall (__NR_perf_event_open, hw_event, pid, cpu, group_fd, flags);
35 read_current_perf_counters (vlib_main_t * vm, u64 * c0, u64 * c1)
39 perfmon_main_t *pm = &perfmon_main;
40 uword my_thread_index = vm->thread_index;
44 for (i = 0; i < pm->n_active; i++)
46 cc = (i == 0) ? c0 : c1;
47 if (pm->rdpmc_indices[i][my_thread_index] != ~0)
48 *cc = clib_rdpmc ((int) pm->rdpmc_indices[i][my_thread_index]);
52 if (read (pm->pm_fds[i][my_thread_index], &sw_value,
53 sizeof (sw_value)) != sizeof (sw_value))
56 ("counter read failed, disable collection...");
57 vm->vlib_node_runtime_perf_counter_cb = 0;
66 clear_counters (perfmon_main_t * pm)
69 vlib_main_t *vm = pm->vlib_main;
74 vlib_worker_thread_barrier_sync (vm);
76 for (j = 0; j < vec_len (vlib_mains); j++)
78 stat_vm = vlib_mains[j];
82 nm = &stat_vm->node_main;
84 /* Clear the node runtime perfmon counters */
85 for (i = 0; i < vec_len (nm->nodes); i++)
88 vlib_node_sync_stats (stat_vm, n);
91 /* And clear the node perfmon counters */
92 for (i = 0; i < vec_len (nm->nodes); i++)
95 n->stats_total.perf_counter0_ticks = 0;
96 n->stats_total.perf_counter1_ticks = 0;
97 n->stats_total.perf_counter_vectors = 0;
98 n->stats_last_clear.perf_counter0_ticks = 0;
99 n->stats_last_clear.perf_counter1_ticks = 0;
100 n->stats_last_clear.perf_counter_vectors = 0;
103 vlib_worker_thread_barrier_release (vm);
107 enable_current_events (perfmon_main_t * pm)
109 struct perf_event_attr pe;
111 struct perf_event_mmap_page *p = 0;
112 perfmon_event_config_t *c;
113 vlib_main_t *vm = vlib_get_main ();
114 u32 my_thread_index = vm->thread_index;
118 if ((pm->current_event + 1) < vec_len (pm->single_events_to_collect))
121 for (i = 0; i < limit; i++)
123 c = vec_elt_at_index (pm->single_events_to_collect,
124 pm->current_event + i);
126 memset (&pe, 0, sizeof (struct perf_event_attr));
127 pe.type = c->pe_type;
128 pe.size = sizeof (struct perf_event_attr);
129 pe.config = c->pe_config;
133 * Note: excluding the kernel makes the
134 * (software) context-switch counter read 0...
136 if (pe.type != PERF_TYPE_SOFTWARE)
138 /* Exclude kernel and hypervisor */
139 pe.exclude_kernel = 1;
143 fd = perf_event_open (&pe, 0, -1, -1, 0);
146 clib_unix_warning ("event open: type %d config %d", c->pe_type,
151 if (pe.type != PERF_TYPE_SOFTWARE)
153 p = mmap (0, pm->page_size, PROT_READ, MAP_SHARED, fd, 0);
156 clib_unix_warning ("mmap");
165 * Software event counters - and others not capable of being
166 * read via the "rdpmc" instruction - will be read
169 if (pe.type == PERF_TYPE_SOFTWARE || p->cap_user_rdpmc == 0)
172 index = p->index - 1;
174 if (ioctl (fd, PERF_EVENT_IOC_RESET, 0) < 0)
175 clib_unix_warning ("reset ioctl");
177 if (ioctl (fd, PERF_EVENT_IOC_ENABLE, 0) < 0)
178 clib_unix_warning ("enable ioctl");
180 pm->rdpmc_indices[i][my_thread_index] = index;
181 pm->perf_event_pages[i][my_thread_index] = (void *) p;
182 pm->pm_fds[i][my_thread_index] = fd;
186 /* Enable the main loop counter snapshot mechanism */
187 vm->vlib_node_runtime_perf_counter_cb = read_current_perf_counters;
191 disable_events (perfmon_main_t * pm)
193 vlib_main_t *vm = vlib_get_main ();
194 u32 my_thread_index = vm->thread_index;
197 /* Stop main loop collection */
198 vm->vlib_node_runtime_perf_counter_cb = 0;
200 for (i = 0; i < pm->n_active; i++)
202 if (pm->pm_fds[i][my_thread_index] == 0)
205 if (ioctl (pm->pm_fds[i][my_thread_index], PERF_EVENT_IOC_DISABLE, 0) <
207 clib_unix_warning ("disable ioctl");
209 if (pm->perf_event_pages[i][my_thread_index])
210 if (munmap (pm->perf_event_pages[i][my_thread_index],
212 clib_unix_warning ("munmap");
214 (void) close (pm->pm_fds[i][my_thread_index]);
215 pm->pm_fds[i][my_thread_index] = 0;
220 worker_thread_start_event (vlib_main_t * vm)
222 perfmon_main_t *pm = &perfmon_main;
224 enable_current_events (pm);
225 vm->worker_thread_main_loop_callback = 0;
229 worker_thread_stop_event (vlib_main_t * vm)
231 perfmon_main_t *pm = &perfmon_main;
233 vm->worker_thread_main_loop_callback = 0;
237 start_event (perfmon_main_t * pm, f64 now, uword event_data)
240 pm->current_event = 0;
241 if (vec_len (pm->single_events_to_collect) == 0)
243 pm->state = PERFMON_STATE_OFF;
246 pm->state = PERFMON_STATE_RUNNING;
249 /* Start collection on this thread */
250 enable_current_events (pm);
252 /* And also on worker threads */
253 for (i = 1; i < vec_len (vlib_mains); i++)
255 if (vlib_mains[i] == 0)
257 vlib_mains[i]->worker_thread_main_loop_callback = (void *)
258 worker_thread_start_event;
263 scrape_and_clear_counters (perfmon_main_t * pm)
266 vlib_main_t *vm = pm->vlib_main;
267 vlib_main_t *stat_vm;
268 vlib_node_main_t *nm;
269 vlib_node_t ***node_dups = 0;
272 perfmon_capture_t *c;
273 perfmon_event_config_t *current_event;
276 u64 vectors_this_counter;
278 /* snapshoot the nodes, including pm counters */
279 vlib_worker_thread_barrier_sync (vm);
281 for (j = 0; j < vec_len (vlib_mains); j++)
283 stat_vm = vlib_mains[j];
287 nm = &stat_vm->node_main;
289 for (i = 0; i < vec_len (nm->nodes); i++)
292 vlib_node_sync_stats (stat_vm, n);
296 vec_validate (nodes, vec_len (nm->nodes) - 1);
297 vec_add1 (node_dups, nodes);
299 /* Snapshoot and clear the per-node perfmon counters */
300 for (i = 0; i < vec_len (nm->nodes); i++)
303 nodes[i] = clib_mem_alloc (sizeof (*n));
304 clib_memcpy_fast (nodes[i], n, sizeof (*n));
305 n->stats_total.perf_counter0_ticks = 0;
306 n->stats_total.perf_counter1_ticks = 0;
307 n->stats_total.perf_counter_vectors = 0;
308 n->stats_last_clear.perf_counter0_ticks = 0;
309 n->stats_last_clear.perf_counter1_ticks = 0;
310 n->stats_last_clear.perf_counter_vectors = 0;
314 vlib_worker_thread_barrier_release (vm);
316 for (j = 0; j < vec_len (vlib_mains); j++)
318 stat_vm = vlib_mains[j];
322 nodes = node_dups[j];
324 for (i = 0; i < vec_len (nodes); i++)
330 if (n->stats_total.perf_counter0_ticks == 0 &&
331 n->stats_total.perf_counter1_ticks == 0)
334 for (k = 0; k < 2; k++)
336 u64 counter_value, counter_last_clear;
339 * We collect 2 counters at once, except for the
340 * last counter when the user asks for an odd number of
343 if ((pm->current_event + k)
344 >= vec_len (pm->single_events_to_collect))
349 counter_value = n->stats_total.perf_counter0_ticks;
351 n->stats_last_clear.perf_counter0_ticks;
355 counter_value = n->stats_total.perf_counter1_ticks;
357 n->stats_last_clear.perf_counter1_ticks;
360 capture_name = format (0, "t%d-%v%c", j, n->name, 0);
362 p = hash_get_mem (pm->capture_by_thread_and_node_name,
367 pool_get (pm->capture_pool, c);
368 memset (c, 0, sizeof (*c));
369 c->thread_and_node_name = capture_name;
370 hash_set_mem (pm->capture_by_thread_and_node_name,
371 capture_name, c - pm->capture_pool);
375 c = pool_elt_at_index (pm->capture_pool, p[0]);
376 vec_free (capture_name);
379 /* Snapshoot counters, etc. into the capture */
380 current_event = pm->single_events_to_collect
381 + pm->current_event + k;
382 counter_name = (u8 *) current_event->name;
383 vectors_this_counter = n->stats_total.perf_counter_vectors -
384 n->stats_last_clear.perf_counter_vectors;
386 vec_add1 (c->counter_names, counter_name);
387 vec_add1 (c->counter_values,
388 counter_value - counter_last_clear);
389 vec_add1 (c->vectors_this_counter, vectors_this_counter);
396 vec_free (node_dups);
400 handle_timeout (perfmon_main_t * pm, f64 now)
405 /* And also on worker threads */
406 for (i = 1; i < vec_len (vlib_mains); i++)
408 if (vlib_mains[i] == 0)
410 vlib_mains[i]->worker_thread_main_loop_callback = (void *)
411 worker_thread_stop_event;
414 /* Short delay to make sure workers have stopped collection */
416 vlib_process_suspend (pm->vlib_main, 1e-3);
417 scrape_and_clear_counters (pm);
418 pm->current_event += pm->n_active;
419 if (pm->current_event >= vec_len (pm->single_events_to_collect))
421 pm->current_event = 0;
422 pm->state = PERFMON_STATE_OFF;
425 enable_current_events (pm);
427 /* And also on worker threads */
428 for (i = 1; i < vec_len (vlib_mains); i++)
430 if (vlib_mains[i] == 0)
432 vlib_mains[i]->worker_thread_main_loop_callback = (void *)
433 worker_thread_start_event;
438 perfmon_periodic_process (vlib_main_t * vm,
439 vlib_node_runtime_t * rt, vlib_frame_t * f)
441 perfmon_main_t *pm = &perfmon_main;
443 uword *event_data = 0;
449 if (pm->state == PERFMON_STATE_RUNNING)
450 vlib_process_wait_for_event_or_clock (vm, pm->timeout_interval);
452 vlib_process_wait_for_event (vm);
454 now = vlib_time_now (vm);
456 event_type = vlib_process_get_events (vm, (uword **) & event_data);
461 for (i = 0; i < vec_len (event_data); i++)
462 start_event (pm, now, event_data[i]);
467 handle_timeout (pm, now);
471 clib_warning ("Unexpected event %d", event_type);
474 vec_reset_length (event_data);
476 return 0; /* or not */
480 VLIB_REGISTER_NODE (perfmon_periodic_node) =
482 .function = perfmon_periodic_process,
483 .type = VLIB_NODE_TYPE_PROCESS,
484 .name = "perfmon-periodic-process",
489 * fd.io coding-style-patch-verification: ON
492 * eval: (c-set-style "gnu")