2 * perfmon.c - skeleton vpp engine plug-in
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 <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
20 #include <perfmon/perfmon.h>
22 #include <vlibapi/api.h>
23 #include <vlibmemory/api.h>
24 #include <vpp/app/version.h>
25 #include <linux/limits.h>
27 perfmon_main_t perfmon_main;
29 static char *perfmon_json_path = "/usr/share/vpp/plugins/perfmon";
32 set_perfmon_json_path ()
34 char *p, path[PATH_MAX];
38 /* find executable path */
39 if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1)
42 /* readlink doesn't provide null termination */
46 if ((p = strrchr (path, '/')) == 0)
51 if ((p = strrchr (path, '/')) == 0)
55 /* cons up the .json file path */
56 s = format (0, "%s/share/vpp/plugins/perfmon", path);
58 perfmon_json_path = (char *) s;
61 #define foreach_cpuid_table \
62 _(0x0306C3, haswell_core_v28.json) \
63 _(0x0306F2, haswell_core_v28.json) \
64 _(0x0406E3, skylake_core_v42.json) \
65 _(0x0506E3, skylake_core_v42.json)
70 #if defined(__x86_64__)
72 asm volatile ("mov $1, %%eax; cpuid; mov %%eax, %0":"=r" (cpuid)::"%eax",
73 "%edx", "%ecx", "%rbx");
81 perfmon_init (vlib_main_t * vm)
83 perfmon_main_t *pm = &perfmon_main;
84 clib_error_t *error = 0;
87 int found_a_table = 0;
90 pm->vnet_main = vnet_get_main ();
92 pm->capture_by_thread_and_node_name =
93 hash_create_string (0, sizeof (uword));
95 pm->log_class = vlib_log_register_class ("perfmon", 0);
97 /* Default data collection interval */
98 pm->timeout_interval = 3.0;
99 vec_validate (pm->pm_fds, vec_len (vlib_mains) - 1);
100 vec_validate (pm->perf_event_pages, vec_len (vlib_mains) - 1);
101 vec_validate (pm->rdpmc_indices, vec_len (vlib_mains) - 1);
102 pm->page_size = getpagesize ();
104 ht = pm->perfmon_table = 0;
106 set_perfmon_json_path ();
108 cpuid = get_cpuid ();
113 #define _(id,table) \
114 else if (cpuid == id) \
116 vlib_log_debug (pm->log_class, "Found table %s", #table); \
117 ht = perfmon_parse_table (pm, perfmon_json_path, #table); \
123 pm->perfmon_table = ht;
125 if (found_a_table == 0)
126 vlib_log_err (pm->log_class, "No table for cpuid %x", cpuid);
131 VLIB_INIT_FUNCTION (perfmon_init);
134 VLIB_PLUGIN_REGISTER () =
136 .version = VPP_BUILD_VER,
137 .description = "Performance monitor plugin",
138 #if !defined(__x86_64__)
139 .default_disabled = 1,
151 if (*s >= '0' && *s <= '9')
152 rv = (rv << 4) | (*s - '0');
153 else if (*s >= 'a' && *s <= 'f')
154 rv = (rv << 4) | (*s - 'a' + 10);
155 else if (*s >= 'A' && *s <= 'A')
156 rv = (rv << 4) | (*s - 'A' + 10);
167 unformat_processor_event (unformat_input_t * input, va_list * args)
169 perfmon_main_t *pm = va_arg (*args, perfmon_main_t *);
170 perfmon_event_config_t *ep = va_arg (*args, perfmon_event_config_t *);
172 name_value_pair_t **nvps, *nvp;
178 if (pm->perfmon_table == 0)
181 if (!unformat (input, "%s", &s))
184 hp = hash_get_pair_mem (pm->perfmon_table, s);
191 nvps = (name_value_pair_t **) (hp->value[0]);
193 for (i = 0; i < vec_len (nvps); i++)
196 if (!strncmp ((char *) nvp->name, "EventCode", 9))
198 pe_config |= atox (nvp->value);
201 else if (!strncmp ((char *) nvp->name, "UMask", 5))
203 pe_config |= (atox (nvp->value) << 8);
212 clib_warning ("BUG: only found %d values", set_values);
216 ep->name = (char *) hp->key;
217 ep->pe_type = PERF_TYPE_RAW;
218 ep->pe_config = pe_config;
222 static clib_error_t *
223 set_pmc_command_fn (vlib_main_t * vm,
224 unformat_input_t * input, vlib_cli_command_t * cmd)
226 perfmon_main_t *pm = &perfmon_main;
227 unformat_input_t _line_input, *line_input = &_line_input;
228 perfmon_event_config_t ec;
232 vec_reset_length (pm->events_to_collect);
233 pm->ipc_event_index = ~0;
234 pm->mispredict_event_index = ~0;
236 if (!unformat_user (input, unformat_line_input, line_input))
237 return clib_error_return (0, "counter names required...");
239 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
241 if (unformat (line_input, "timeout %u", &timeout_seconds))
242 pm->timeout_interval = (f64) timeout_seconds;
243 else if (unformat (line_input, "instructions-per-clock"))
245 ec.name = "instructions";
246 ec.pe_type = PERF_TYPE_HARDWARE;
247 ec.pe_config = PERF_COUNT_HW_INSTRUCTIONS;
248 pm->ipc_event_index = vec_len (pm->events_to_collect);
249 vec_add1 (pm->events_to_collect, ec);
250 ec.name = "cpu-cycles";
251 ec.pe_type = PERF_TYPE_HARDWARE;
252 ec.pe_config = PERF_COUNT_HW_CPU_CYCLES;
253 vec_add1 (pm->events_to_collect, ec);
255 else if (unformat (line_input, "branch-mispredict-rate"))
257 ec.name = "branch-misses";
258 ec.pe_type = PERF_TYPE_HARDWARE;
259 ec.pe_config = PERF_COUNT_HW_BRANCH_MISSES;
260 pm->mispredict_event_index = vec_len (pm->events_to_collect);
261 vec_add1 (pm->events_to_collect, ec);
262 ec.name = "branches";
263 ec.pe_type = PERF_TYPE_HARDWARE;
264 ec.pe_config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
265 vec_add1 (pm->events_to_collect, ec);
267 else if (unformat (line_input, "%U", unformat_processor_event, pm, &ec))
269 vec_add1 (pm->events_to_collect, ec);
271 #define _(type,event,str) \
272 else if (unformat (line_input, str)) \
276 ec.pe_config = event; \
277 vec_add1 (pm->events_to_collect, ec); \
279 foreach_perfmon_event
282 return clib_error_return (0, "unknown input '%U'",
283 format_unformat_error, line_input);
286 if (vec_len (pm->events_to_collect) == 0)
287 return clib_error_return (0, "no events specified...");
289 vlib_cli_output (vm, "Start collection for %d events, wait %.2f seconds",
290 vec_len (pm->events_to_collect),
291 (f64) (vec_len (pm->events_to_collect))
292 * pm->timeout_interval);
294 vlib_process_signal_event (pm->vlib_main, perfmon_periodic_node.index,
297 /* Coarse-grained wait */
298 vlib_process_suspend (vm,
299 ((f64) (vec_len (pm->events_to_collect)
300 * pm->timeout_interval)));
303 /* Reasonable to guess that collection may not be quite done... */
304 while (pm->state == PERFMON_STATE_RUNNING)
306 vlib_process_suspend (vm, 10e-3);
309 vlib_cli_output (vm, "DEADMAN: collection still running...");
314 vlib_cli_output (vm, "Data collection complete...");
319 VLIB_CLI_COMMAND (set_pmc_command, static) =
322 .short_help = "set pmc c1 [..., use \"show pmc events\"]",
323 .function = set_pmc_command_fn,
329 capture_name_sort (void *a1, void *a2)
331 perfmon_capture_t *c1 = a1;
332 perfmon_capture_t *c2 = a2;
334 return strcmp ((char *) c1->thread_and_node_name,
335 (char *) c2->thread_and_node_name);
339 format_capture (u8 * s, va_list * args)
341 perfmon_main_t *pm = va_arg (*args, perfmon_main_t *);
342 perfmon_capture_t *c = va_arg (*args, perfmon_capture_t *);
343 int verbose __attribute__ ((unused)) = va_arg (*args, int);
349 s = format (s, "%=40s%=20s%=16s%=16s%=16s",
350 "Name", "Counter", "Count", "Pkts", "Counts/Pkt");
354 for (i = 0; i < vec_len (c->counter_names); i++)
359 name = c->thread_and_node_name;
366 /* Deal with synthetic events right here */
367 if (i == pm->ipc_event_index)
370 ASSERT (i + 1 < vec_len (c->counter_names));
372 if (c->counter_values[i + 1] > 0)
373 ipc_rate = (f64) c->counter_values[i]
374 / (f64) c->counter_values[i + 1];
378 s = format (s, "%-40s%+20s%+16llu%+16llu%+16.2e\n",
379 name, "instructions-per-clock",
380 c->counter_values[i],
381 c->counter_values[i + 1], ipc_rate);
385 if (i == pm->mispredict_event_index)
388 ASSERT (i + 1 < vec_len (c->counter_names));
390 if (c->counter_values[i + 1] > 0)
391 mispredict_rate = (f64) c->counter_values[i]
392 / (f64) c->counter_values[i + 1];
394 mispredict_rate = 0.0;
396 s = format (s, "%-40s%+20s%+16llu%+16llu%+16.2e\n",
397 name, "branch-mispredict-rate",
398 c->counter_values[i],
399 c->counter_values[i + 1], mispredict_rate);
403 if (c->vectors_this_counter[i])
405 ((f64) c->counter_values[i]) / ((f64) c->vectors_this_counter[i]);
409 s = format (s, "%-40s%+20s%+16llu%+16llu%+16.2e",
410 name, c->counter_names[i],
411 c->counter_values[i],
412 c->vectors_this_counter[i], ticks_per_pkt);
418 format_generic_events (u8 * s, va_list * args)
420 int verbose = va_arg (*args, int);
422 #define _(type,config,name) \
424 s = format (s, "\n %s", name); \
426 s = format (s, "\n %s (%d, %d)", name, type, config);
427 foreach_perfmon_event;
435 name_value_pair_t **nvps;
439 sort_nvps_by_name (void *a1, void *a2)
441 sort_nvp_t *nvp1 = a1;
442 sort_nvp_t *nvp2 = a2;
444 return strcmp ((char *) nvp1->name, (char *) nvp2->name);
448 format_processor_events (u8 * s, va_list * args)
450 perfmon_main_t *pm = va_arg (*args, perfmon_main_t *);
451 int verbose = va_arg (*args, int);
453 sort_nvp_t *sort_nvps = 0;
456 name_value_pair_t **value;
459 hash_foreach_mem (key, value, pm->perfmon_table,
461 vec_add2 (sort_nvps, sn, 1);
466 vec_sort_with_function (sort_nvps, sort_nvps_by_name);
470 for (i = 0; i < vec_len (sort_nvps); i++)
471 s = format (s, "\n %s ", sort_nvps[i].name);
475 for (i = 0; i < vec_len (sort_nvps); i++)
477 name_value_pair_t **nvps;
478 s = format (s, "\n %s:", sort_nvps[i].name);
480 nvps = sort_nvps[i].nvps;
482 for (j = 0; j < vec_len (nvps); j++)
483 s = format (s, "\n %s = %s", nvps[j]->name, nvps[j]->value);
486 vec_free (sort_nvps);
491 static clib_error_t *
492 show_pmc_command_fn (vlib_main_t * vm,
493 unformat_input_t * input, vlib_cli_command_t * cmd)
495 perfmon_main_t *pm = &perfmon_main;
499 perfmon_capture_t *c;
500 perfmon_capture_t *captures = 0;
502 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
504 if (unformat (input, "events"))
506 else if (unformat (input, "verbose"))
514 vlib_cli_output (vm, "Generic Events %U",
515 format_generic_events, verbose);
516 vlib_cli_output (vm, "Synthetic Events");
517 vlib_cli_output (vm, " instructions-per-clock");
518 vlib_cli_output (vm, " branch-mispredict-rate");
519 if (pm->perfmon_table)
520 vlib_cli_output (vm, "Processor Events %U",
521 format_processor_events, pm, verbose);
525 if (pm->state == PERFMON_STATE_RUNNING)
527 vlib_cli_output (vm, "Data collection in progress...");
531 if (pool_elts (pm->capture_pool) == 0)
533 vlib_cli_output (vm, "No data...");
538 pool_foreach (c, pm->capture_pool,
540 vec_add1 (captures, *c);
544 vec_sort_with_function (captures, capture_name_sort);
546 vlib_cli_output (vm, "%U", format_capture, pm, 0 /* header */ ,
549 for (i = 0; i < vec_len (captures); i++)
553 vlib_cli_output (vm, "%U", format_capture, pm, c, verbose);
562 VLIB_CLI_COMMAND (show_pmc_command, static) =
565 .short_help = "show pmc [verbose]",
566 .function = show_pmc_command_fn,
571 static clib_error_t *
572 clear_pmc_command_fn (vlib_main_t * vm,
573 unformat_input_t * input, vlib_cli_command_t * cmd)
575 perfmon_main_t *pm = &perfmon_main;
579 if (pm->state == PERFMON_STATE_RUNNING)
581 vlib_cli_output (vm, "Performance monitor is still running...");
585 pool_free (pm->capture_pool);
588 hash_foreach_mem (key, value, pm->capture_by_thread_and_node_name,
593 hash_free (pm->capture_by_thread_and_node_name);
594 pm->capture_by_thread_and_node_name =
595 hash_create_string (0, sizeof (uword));
600 VLIB_CLI_COMMAND (clear_pmc_command, static) =
603 .short_help = "clear the performance monitor counters",
604 .function = clear_pmc_command_fn,
610 * fd.io coding-style-patch-verification: ON
613 * eval: (c-set-style "gnu")