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 _(0x0106E5, NehalemEP_core_V2.json) /* Intel(R) Xeon(R) CPU X3430 @ 2.40GHz */ \
63 _(0x0306C3, haswell_core_v28.json) /* Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz */ \
64 _(0x0306F2, haswell_core_v28.json) /* Intel(R) Xeon(R) CPU E5-2640 v3 @ 2.60GHz */ \
65 _(0x040661, haswell_core_v28.json) /* Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz */ \
66 _(0x0406D8, Silvermont_core_V14.json) /* Intel(R) Atom(TM) CPU C2758 @ 2.40GHz */ \
67 _(0x0406E3, skylake_core_v42.json) /* Intel(R) Core(TM) i7-6500U CPU @ 2.50GHz */ \
68 _(0x0506E3, skylake_core_v42.json) /* Intel(R) Core(TM) i5-6600 CPU @ 3.30GHz */
73 #if defined(__x86_64__)
75 asm volatile ("mov $1, %%eax; cpuid; mov %%eax, %0":"=r" (cpuid)::"%eax",
76 "%edx", "%ecx", "%rbx");
84 perfmon_init (vlib_main_t * vm)
86 perfmon_main_t *pm = &perfmon_main;
87 clib_error_t *error = 0;
90 int found_a_table = 0;
93 pm->vnet_main = vnet_get_main ();
95 pm->capture_by_thread_and_node_name =
96 hash_create_string (0, sizeof (uword));
98 pm->log_class = vlib_log_register_class ("perfmon", 0);
100 /* Default data collection interval */
101 pm->timeout_interval = 3.0;
102 vec_validate (pm->pm_fds, vec_len (vlib_mains) - 1);
103 vec_validate (pm->perf_event_pages, vec_len (vlib_mains) - 1);
104 vec_validate (pm->rdpmc_indices, vec_len (vlib_mains) - 1);
105 pm->page_size = getpagesize ();
107 ht = pm->perfmon_table = 0;
109 set_perfmon_json_path ();
111 cpuid = get_cpuid ();
116 #define _(id,table) \
117 else if (cpuid == id) \
119 vlib_log_debug (pm->log_class, "Found table %s", #table); \
120 ht = perfmon_parse_table (pm, perfmon_json_path, #table); \
126 pm->perfmon_table = ht;
128 if (found_a_table == 0)
129 vlib_log_err (pm->log_class, "No table for cpuid %x", cpuid);
134 VLIB_INIT_FUNCTION (perfmon_init);
137 VLIB_PLUGIN_REGISTER () =
139 .version = VPP_BUILD_VER,
140 .description = "Performance monitor plugin",
141 #if !defined(__x86_64__)
142 .default_disabled = 1,
154 if (*s >= '0' && *s <= '9')
155 rv = (rv << 4) | (*s - '0');
156 else if (*s >= 'a' && *s <= 'f')
157 rv = (rv << 4) | (*s - 'a' + 10);
158 else if (*s >= 'A' && *s <= 'A')
159 rv = (rv << 4) | (*s - 'A' + 10);
170 unformat_processor_event (unformat_input_t * input, va_list * args)
172 perfmon_main_t *pm = va_arg (*args, perfmon_main_t *);
173 perfmon_event_config_t *ep = va_arg (*args, perfmon_event_config_t *);
175 name_value_pair_t **nvps, *nvp;
181 if (pm->perfmon_table == 0)
184 if (!unformat (input, "%s", &s))
187 hp = hash_get_pair_mem (pm->perfmon_table, s);
194 nvps = (name_value_pair_t **) (hp->value[0]);
196 for (i = 0; i < vec_len (nvps); i++)
199 if (!strncmp ((char *) nvp->name, "EventCode", 9))
201 pe_config |= atox (nvp->value);
204 else if (!strncmp ((char *) nvp->name, "UMask", 5))
206 pe_config |= (atox (nvp->value) << 8);
215 clib_warning ("BUG: only found %d values", set_values);
219 ep->name = (char *) hp->key;
220 ep->pe_type = PERF_TYPE_RAW;
221 ep->pe_config = pe_config;
225 static clib_error_t *
226 set_pmc_command_fn (vlib_main_t * vm,
227 unformat_input_t * input, vlib_cli_command_t * cmd)
229 perfmon_main_t *pm = &perfmon_main;
230 unformat_input_t _line_input, *line_input = &_line_input;
231 perfmon_event_config_t ec;
235 vec_reset_length (pm->events_to_collect);
236 pm->ipc_event_index = ~0;
237 pm->mispredict_event_index = ~0;
239 if (!unformat_user (input, unformat_line_input, line_input))
240 return clib_error_return (0, "counter names required...");
242 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
244 if (unformat (line_input, "timeout %u", &timeout_seconds))
245 pm->timeout_interval = (f64) timeout_seconds;
246 else if (unformat (line_input, "instructions-per-clock"))
248 ec.name = "instructions";
249 ec.pe_type = PERF_TYPE_HARDWARE;
250 ec.pe_config = PERF_COUNT_HW_INSTRUCTIONS;
251 pm->ipc_event_index = vec_len (pm->events_to_collect);
252 vec_add1 (pm->events_to_collect, ec);
253 ec.name = "cpu-cycles";
254 ec.pe_type = PERF_TYPE_HARDWARE;
255 ec.pe_config = PERF_COUNT_HW_CPU_CYCLES;
256 vec_add1 (pm->events_to_collect, ec);
258 else if (unformat (line_input, "branch-mispredict-rate"))
260 ec.name = "branch-misses";
261 ec.pe_type = PERF_TYPE_HARDWARE;
262 ec.pe_config = PERF_COUNT_HW_BRANCH_MISSES;
263 pm->mispredict_event_index = vec_len (pm->events_to_collect);
264 vec_add1 (pm->events_to_collect, ec);
265 ec.name = "branches";
266 ec.pe_type = PERF_TYPE_HARDWARE;
267 ec.pe_config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
268 vec_add1 (pm->events_to_collect, ec);
270 else if (unformat (line_input, "%U", unformat_processor_event, pm, &ec))
272 vec_add1 (pm->events_to_collect, ec);
274 #define _(type,event,str) \
275 else if (unformat (line_input, str)) \
279 ec.pe_config = event; \
280 vec_add1 (pm->events_to_collect, ec); \
282 foreach_perfmon_event
285 return clib_error_return (0, "unknown input '%U'",
286 format_unformat_error, line_input);
289 if (vec_len (pm->events_to_collect) == 0)
290 return clib_error_return (0, "no events specified...");
292 vlib_cli_output (vm, "Start collection for %d events, wait %.2f seconds",
293 vec_len (pm->events_to_collect),
294 (f64) (vec_len (pm->events_to_collect))
295 * pm->timeout_interval);
297 vlib_process_signal_event (pm->vlib_main, perfmon_periodic_node.index,
300 /* Coarse-grained wait */
301 vlib_process_suspend (vm,
302 ((f64) (vec_len (pm->events_to_collect)
303 * pm->timeout_interval)));
306 /* Reasonable to guess that collection may not be quite done... */
307 while (pm->state == PERFMON_STATE_RUNNING)
309 vlib_process_suspend (vm, 10e-3);
312 vlib_cli_output (vm, "DEADMAN: collection still running...");
317 vlib_cli_output (vm, "Data collection complete...");
322 VLIB_CLI_COMMAND (set_pmc_command, static) =
325 .short_help = "set pmc c1 [..., use \"show pmc events\"]",
326 .function = set_pmc_command_fn,
332 capture_name_sort (void *a1, void *a2)
334 perfmon_capture_t *c1 = a1;
335 perfmon_capture_t *c2 = a2;
337 return strcmp ((char *) c1->thread_and_node_name,
338 (char *) c2->thread_and_node_name);
342 format_capture (u8 * s, va_list * args)
344 perfmon_main_t *pm = va_arg (*args, perfmon_main_t *);
345 perfmon_capture_t *c = va_arg (*args, perfmon_capture_t *);
346 int verbose __attribute__ ((unused)) = va_arg (*args, int);
352 s = format (s, "%=40s%=20s%=16s%=16s%=16s",
353 "Name", "Counter", "Count", "Pkts", "Counts/Pkt");
357 for (i = 0; i < vec_len (c->counter_names); i++)
362 name = c->thread_and_node_name;
369 /* Deal with synthetic events right here */
370 if (i == pm->ipc_event_index)
373 ASSERT (i + 1 < vec_len (c->counter_names));
375 if (c->counter_values[i + 1] > 0)
376 ipc_rate = (f64) c->counter_values[i]
377 / (f64) c->counter_values[i + 1];
381 s = format (s, "%-40s%+20s%+16llu%+16llu%+16.2e\n",
382 name, "instructions-per-clock",
383 c->counter_values[i],
384 c->counter_values[i + 1], ipc_rate);
388 if (i == pm->mispredict_event_index)
391 ASSERT (i + 1 < vec_len (c->counter_names));
393 if (c->counter_values[i + 1] > 0)
394 mispredict_rate = (f64) c->counter_values[i]
395 / (f64) c->counter_values[i + 1];
397 mispredict_rate = 0.0;
399 s = format (s, "%-40s%+20s%+16llu%+16llu%+16.2e\n",
400 name, "branch-mispredict-rate",
401 c->counter_values[i],
402 c->counter_values[i + 1], mispredict_rate);
406 if (c->vectors_this_counter[i])
408 ((f64) c->counter_values[i]) / ((f64) c->vectors_this_counter[i]);
412 s = format (s, "%-40s%+20s%+16llu%+16llu%+16.2e",
413 name, c->counter_names[i],
414 c->counter_values[i],
415 c->vectors_this_counter[i], ticks_per_pkt);
421 format_generic_events (u8 * s, va_list * args)
423 int verbose = va_arg (*args, int);
425 #define _(type,config,name) \
427 s = format (s, "\n %s", name); \
429 s = format (s, "\n %s (%d, %d)", name, type, config);
430 foreach_perfmon_event;
438 name_value_pair_t **nvps;
442 sort_nvps_by_name (void *a1, void *a2)
444 sort_nvp_t *nvp1 = a1;
445 sort_nvp_t *nvp2 = a2;
447 return strcmp ((char *) nvp1->name, (char *) nvp2->name);
451 format_processor_events (u8 * s, va_list * args)
453 perfmon_main_t *pm = va_arg (*args, perfmon_main_t *);
454 int verbose = va_arg (*args, int);
456 sort_nvp_t *sort_nvps = 0;
459 name_value_pair_t **value;
462 hash_foreach_mem (key, value, pm->perfmon_table,
464 vec_add2 (sort_nvps, sn, 1);
469 vec_sort_with_function (sort_nvps, sort_nvps_by_name);
473 for (i = 0; i < vec_len (sort_nvps); i++)
474 s = format (s, "\n %s ", sort_nvps[i].name);
478 for (i = 0; i < vec_len (sort_nvps); i++)
480 name_value_pair_t **nvps;
481 s = format (s, "\n %s:", sort_nvps[i].name);
483 nvps = sort_nvps[i].nvps;
485 for (j = 0; j < vec_len (nvps); j++)
486 s = format (s, "\n %s = %s", nvps[j]->name, nvps[j]->value);
489 vec_free (sort_nvps);
494 static clib_error_t *
495 show_pmc_command_fn (vlib_main_t * vm,
496 unformat_input_t * input, vlib_cli_command_t * cmd)
498 perfmon_main_t *pm = &perfmon_main;
502 perfmon_capture_t *c;
503 perfmon_capture_t *captures = 0;
505 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
507 if (unformat (input, "events"))
509 else if (unformat (input, "verbose"))
517 vlib_cli_output (vm, "Generic Events %U",
518 format_generic_events, verbose);
519 vlib_cli_output (vm, "Synthetic Events");
520 vlib_cli_output (vm, " instructions-per-clock");
521 vlib_cli_output (vm, " branch-mispredict-rate");
522 if (pm->perfmon_table)
523 vlib_cli_output (vm, "Processor Events %U",
524 format_processor_events, pm, verbose);
528 if (pm->state == PERFMON_STATE_RUNNING)
530 vlib_cli_output (vm, "Data collection in progress...");
534 if (pool_elts (pm->capture_pool) == 0)
536 vlib_cli_output (vm, "No data...");
541 pool_foreach (c, pm->capture_pool,
543 vec_add1 (captures, *c);
547 vec_sort_with_function (captures, capture_name_sort);
549 vlib_cli_output (vm, "%U", format_capture, pm, 0 /* header */ ,
552 for (i = 0; i < vec_len (captures); i++)
556 vlib_cli_output (vm, "%U", format_capture, pm, c, verbose);
565 VLIB_CLI_COMMAND (show_pmc_command, static) =
568 .short_help = "show pmc [verbose]",
569 .function = show_pmc_command_fn,
574 static clib_error_t *
575 clear_pmc_command_fn (vlib_main_t * vm,
576 unformat_input_t * input, vlib_cli_command_t * cmd)
578 perfmon_main_t *pm = &perfmon_main;
582 if (pm->state == PERFMON_STATE_RUNNING)
584 vlib_cli_output (vm, "Performance monitor is still running...");
588 pool_free (pm->capture_pool);
591 hash_foreach_mem (key, value, pm->capture_by_thread_and_node_name,
596 hash_free (pm->capture_by_thread_and_node_name);
597 pm->capture_by_thread_and_node_name =
598 hash_create_string (0, sizeof (uword));
603 VLIB_CLI_COMMAND (clear_pmc_command, static) =
606 .short_help = "clear the performance monitor counters",
607 .function = clear_pmc_command_fn,
613 * fd.io coding-style-patch-verification: ON
616 * eval: (c-set-style "gnu")