2 * Copyright (c) 2020 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vnet/vnet.h>
17 #include <perfmon/perfmon.h>
18 #include <vppinfra/format_table.h>
21 unformat_perfmon_bundle_name (unformat_input_t *input, va_list *args)
23 perfmon_main_t *pm = &perfmon_main;
24 perfmon_bundle_t **b = va_arg (*args, perfmon_bundle_t **);
28 if (unformat (input, "%s", &str) == 0)
31 p = hash_get_mem (pm->bundle_by_name, str);
34 b[0] = (perfmon_bundle_t *) p[0];
41 unformat_perfmon_active_type (unformat_input_t *input, va_list *args)
43 perfmon_bundle_t *b = va_arg (*args, perfmon_bundle_t *);
44 perfmon_bundle_type_t *bundle_type = va_arg (*args, perfmon_bundle_type_t *);
47 char *_str_types[PERFMON_BUNDLE_TYPE_MAX];
49 #define _(type, pstr) _str_types[type] = (char *) pstr;
51 foreach_perfmon_bundle_type
56 if (unformat (input, "%s", &str) == 0)
59 for (int i = PERFMON_BUNDLE_TYPE_NODE; i < PERFMON_BUNDLE_TYPE_MAX; i++)
61 /* match the name and confirm it is available on this cpu */
62 if (strncmp (str, _str_types[i], strlen (_str_types[i])) == 0 &&
63 (b->type_flags & 1 << i))
75 unformat_perfmon_source_name (unformat_input_t *input, va_list *args)
77 perfmon_main_t *pm = &perfmon_main;
78 perfmon_source_t **b = va_arg (*args, perfmon_source_t **);
82 if (unformat (input, "%s", &str) == 0)
85 p = hash_get_mem (pm->source_by_name, str);
88 b[0] = (perfmon_source_t *) p[0];
96 FORMAT_PERFMON_BUNDLE_NONE = 0,
97 FORMAT_PERFMON_BUNDLE_VERBOSE = 1,
98 FORMAT_PERFMON_BUNDLE_SHOW_CONFIG = 2
99 } format_perfmon_bundle_args_t;
102 format_perfmon_bundle (u8 *s, va_list *args)
104 perfmon_bundle_t *b = va_arg (*args, perfmon_bundle_t *);
105 format_perfmon_bundle_args_t cfg =
106 va_arg (*args, format_perfmon_bundle_args_t);
110 u8 *_bundle_type = 0;
111 const char *bundle_type[PERFMON_BUNDLE_TYPE_MAX];
112 #define _(type, pstr) bundle_type[type] = (const char *) pstr;
114 foreach_perfmon_bundle_type
117 if (b == 0) return format (s, "%-20s%-20s%-20s%s", "Name", "Type(s)",
118 "Source", "Description");
120 if (cfg != FORMAT_PERFMON_BUNDLE_NONE)
122 s = format (s, "name: %s\n", b->name);
123 s = format (s, "description: %s\n", b->description);
124 s = format (s, "source: %s\n", b->src->name);
125 for (int i = 0; i < b->n_events; i++)
127 perfmon_event_t *e = b->src->events + b->events[i];
128 s = format (s, "event %u: %s", i, e->name);
130 format_function_t *format_config = b->src->format_config;
132 if (format_config && cfg == FORMAT_PERFMON_BUNDLE_SHOW_CONFIG)
133 s = format (s, " (%U)", format_config, e->config);
135 s = format (s, "\n");
140 s = format (s, "%-20s", b->name);
141 for (int i = PERFMON_BUNDLE_TYPE_NODE; i < PERFMON_BUNDLE_TYPE_MAX; i++)
143 /* check the type is available on this uarch*/
144 if (b->type_flags & 1 << i)
145 _bundle_type = format (_bundle_type, "%s,", bundle_type[i]);
147 /* remove any stray commas */
148 if ((vl = vec_len (_bundle_type)))
149 _bundle_type[vl - 1] = 0;
152 format (s, "%-20s%-20s%s", _bundle_type, b->src->name, b->description);
155 vec_free (_bundle_type);
161 bundle_name_sort_cmp (void *a1, void *a2)
163 perfmon_bundle_t **n1 = a1;
164 perfmon_bundle_t **n2 = a2;
166 return clib_strcmp ((char *) (*n1)->name, (char *) (*n2)->name);
169 static clib_error_t *
170 show_perfmon_bundle_command_fn (vlib_main_t *vm, unformat_input_t *input,
171 vlib_cli_command_t *cmd)
173 perfmon_main_t *pm = &perfmon_main;
174 unformat_input_t _line_input, *line_input = &_line_input;
175 perfmon_bundle_t *b = 0, **vb = 0;
177 format_perfmon_bundle_args_t cfg = FORMAT_PERFMON_BUNDLE_NONE;
179 if (unformat_user (input, unformat_line_input, line_input))
181 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
183 if (unformat (line_input, "verbose"))
185 else if (unformat (line_input, "%U", unformat_perfmon_bundle_name,
189 return clib_error_return (0, "unknown input `%U'",
190 format_unformat_error, line_input);
192 unformat_free (line_input);
195 if (verbose) /* if verbose is specified */
196 cfg = FORMAT_PERFMON_BUNDLE_VERBOSE;
200 if (verbose) /* if verbose is specified with a bundle */
201 cfg = FORMAT_PERFMON_BUNDLE_SHOW_CONFIG;
203 cfg = FORMAT_PERFMON_BUNDLE_VERBOSE;
208 hash_foreach_mem (key, b, pm->bundle_by_name, vec_add (vb, &b, 1););
211 if (cfg == FORMAT_PERFMON_BUNDLE_NONE)
212 vlib_cli_output (vm, "%U\n", format_perfmon_bundle, 0, cfg);
214 vec_sort_with_function (vb, bundle_name_sort_cmp);
216 for (int i = 0; i < vec_len (vb); i++)
217 /* bundle type will be unknown if no cpu_supports matched */
218 if (vb[i]->type_flags)
219 vlib_cli_output (vm, "%U\n", format_perfmon_bundle, vb[i], cfg);
225 VLIB_CLI_COMMAND (show_perfmon_bundle_command, static) = {
226 .path = "show perfmon bundle",
227 .short_help = "show perfmon bundle [<bundle-name>] [verbose]",
228 .function = show_perfmon_bundle_command_fn,
233 format_perfmon_source (u8 *s, va_list *args)
235 perfmon_source_t *src = va_arg (*args, perfmon_source_t *);
236 int verbose = va_arg (*args, int);
239 return format (s, "%-20s%-9s %s", "Name", "NumEvents", "Description");
243 s = format (s, "name: %s\n", src->name);
244 s = format (s, "description: %s\n", src->description);
245 s = format (s, "Events:\n");
246 for (int i = 0; i < src->n_events; i++)
248 perfmon_event_t *e = src->events + i;
249 s = format (s, " %s", e->name);
250 if (src->format_config)
251 s = format (s, " (%U)\n", src->format_config, e->config);
253 s = format (s, " (0x%x)\n", e->config);
255 s = format (s, " %s\n", e->description);
258 if (src->instances_by_type)
260 s = format (s, "Instances:\n");
261 for (int i = 0; i < vec_len (src->instances_by_type); i++)
263 perfmon_instance_type_t *it;
264 it = vec_elt_at_index (src->instances_by_type, i);
265 if (vec_len (it->instances) == 0)
267 s = format (s, " %s:\n ", it->name);
268 for (int j = 0; j < vec_len (it->instances); j++)
270 perfmon_instance_t *in = vec_elt_at_index (it->instances, j);
271 s = format (s, " %s", in->name);
273 s = format (s, "\n");
278 s = format (s, "%-20s%9u %s", src->name, src->n_events, src->description);
283 static clib_error_t *
284 show_perfmon_source_command_fn (vlib_main_t *vm, unformat_input_t *input,
285 vlib_cli_command_t *cmd)
287 perfmon_main_t *pm = &perfmon_main;
288 unformat_input_t _line_input, *line_input = &_line_input;
289 perfmon_source_t *s = 0, **vs = 0;
292 if (unformat_user (input, unformat_line_input, line_input))
294 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
296 if (unformat (line_input, "verbose"))
298 else if (unformat (line_input, "%U", unformat_perfmon_source_name,
302 return clib_error_return (0, "unknown input `%U'",
303 format_unformat_error, line_input);
305 unformat_free (line_input);
311 hash_foreach_mem (key, s, pm->source_by_name, vec_add (vs, &s, 1););
317 vlib_cli_output (vm, "%U\n", format_perfmon_source, 0, 0);
319 for (int i = 0; i < vec_len (vs); i++)
320 vlib_cli_output (vm, "%U\n", format_perfmon_source, vs[i], verbose);
326 VLIB_CLI_COMMAND (show_perfmon_source_command, static) = {
327 .path = "show perfmon source",
328 .short_help = "show perfmon source [<source-name>] [verbose]",
329 .function = show_perfmon_source_command_fn,
333 static clib_error_t *
334 show_perfmon_active_bundle_command_fn (vlib_main_t *vm,
335 unformat_input_t *input,
336 vlib_cli_command_t *cmd)
338 perfmon_main_t *pm = &perfmon_main;
340 vlib_cli_output (vm, "%U\n", format_perfmon_bundle, pm->active_bundle, 1);
344 VLIB_CLI_COMMAND (show_perfmon_active_bundle_command, static) = {
345 .path = "show perfmon active-bundle",
346 .short_help = "show perfmon active-bundle",
347 .function = show_perfmon_active_bundle_command_fn,
351 static clib_error_t *
352 show_perfmon_stats_command_fn (vlib_main_t *vm, unformat_input_t *input,
353 vlib_cli_command_t *cmd)
355 perfmon_main_t *pm = &perfmon_main;
356 perfmon_bundle_t *b = pm->active_bundle;
357 clib_error_t *err = 0;
358 table_t table = {}, *t = &table;
360 perfmon_reading_t *r, *readings = 0;
361 perfmon_instance_type_t *it = pm->active_instance_type;
362 perfmon_instance_t *in;
367 return clib_error_return (0, "no bundle selected");
369 n_instances = vec_len (it->instances);
370 vec_validate (readings, n_instances - 1);
372 /*Only perform read() for THREAD or SYSTEM bundles*/
374 i < n_instances && b->active_type != PERFMON_BUNDLE_TYPE_NODE; i++)
376 in = vec_elt_at_index (it->instances, i);
377 r = vec_elt_at_index (readings, i);
379 if (read (pm->group_fds[i], r, (b->n_events + 3) * sizeof (u64)) == -1)
381 err = clib_error_return_unix (0, "read");
386 table_format_title (t, "%s", b->description);
388 table_add_header_col (t, 0);
389 table_add_header_row (t, 0);
391 if (b->column_headers)
393 char **hdr = b->column_headers;
395 table_format_cell (t, -1, n_row++, "%s", hdr++[0]);
399 for (int i = 0; i < n_instances; i++)
401 in = vec_elt_at_index (it->instances, i);
402 r = vec_elt_at_index (readings, i);
403 table_format_cell (t, col, -1, "%s", in->name, b->active_type);
404 if (b->active_type == PERFMON_BUNDLE_TYPE_NODE)
406 perfmon_thread_runtime_t *tr;
407 tr = vec_elt_at_index (pm->thread_runtimes, i);
408 for (int j = 0; j < tr->n_nodes; j++)
409 if (tr->node_stats[j].n_calls)
411 perfmon_node_stats_t ns;
412 table_format_cell (t, ++col, -1, "%U", format_vlib_node_name,
413 vm, j, b->active_type);
414 table_set_cell_align (t, col, -1, TTAA_RIGHT);
415 table_set_cell_fg_color (t, col, -1, TTAC_CYAN);
417 if (PREDICT_TRUE (clib_bitmap_is_zero (b->event_disabled)))
418 clib_memcpy_fast (&ns, tr->node_stats + j, sizeof (ns));
419 /* if some events are not implemented, we need to realign these
420 to display under the correct column headers */
423 perfmon_node_stats_t *tr_ns = tr->node_stats + j;
424 ns.n_calls = tr_ns->n_calls;
425 ns.n_packets = tr_ns->n_packets;
426 /* loop through all events in bundle + manually copy into
427 the correct place, until we've read all values that are
429 int num_enabled_events =
431 clib_bitmap_count_set_bits (b->event_disabled);
432 for (int i = 0, k = 0; k < num_enabled_events; i++)
434 if (!clib_bitmap_get (b->event_disabled, i))
436 ns.value[i] = tr_ns->value[k];
442 for (int j = 0; j < n_row; j++)
444 if (clib_bitmap_get (b->column_disabled, j))
445 table_format_cell (t, col, j, "-");
447 table_format_cell (t, col, j, "%U", b->format_fn, &ns, j,
452 else /* b->type != PERFMON_BUNDLE_TYPE_NODE */
454 if (PREDICT_TRUE (clib_bitmap_is_zero (b->event_disabled)))
456 for (int j = 0; j < n_row; j++)
457 table_format_cell (t, i, j, "%U", b->format_fn, r, j,
460 /* similarly for THREAD/SYSTEM bundles, if some events are not
461 implemented, we need to realign readings under column headings */
464 perfmon_reading_t aligned_r[b->n_events];
465 aligned_r->nr = r->nr;
466 aligned_r->time_enabled = r->time_enabled;
467 aligned_r->time_running = r->time_running;
468 int num_enabled_events =
469 b->n_events - clib_bitmap_count_set_bits (b->event_disabled);
470 for (int i = 0, k = 0; k < num_enabled_events; i++)
472 if (!clib_bitmap_get (b->event_disabled, i))
474 aligned_r->value[i] = r->value[k];
478 for (int j = 0; j < n_row; j++)
480 if (clib_bitmap_get (b->column_disabled, j))
481 table_format_cell (t, col, j, "-");
483 table_format_cell (t, i, j, "%U", b->format_fn, aligned_r,
491 vlib_cli_output (vm, "%U\n", format_table, t);
495 vlib_cli_output (vm, "\n%s\n", b->footer);
503 VLIB_CLI_COMMAND (show_perfmon_stats_command, static) = {
504 .path = "show perfmon statistics",
505 .short_help = "show perfmon statistics [raw]",
506 .function = show_perfmon_stats_command_fn,
510 static clib_error_t *
511 perfmon_reset_command_fn (vlib_main_t *vm, unformat_input_t *input,
512 vlib_cli_command_t *cmd)
518 VLIB_CLI_COMMAND (perfmon_reset_command, static) = {
519 .path = "perfmon reset",
520 .short_help = "perfmon reset",
521 .function = perfmon_reset_command_fn,
525 static clib_error_t *
526 perfmon_start_command_fn (vlib_main_t *vm, unformat_input_t *input,
527 vlib_cli_command_t *cmd)
529 perfmon_main_t *pm = &perfmon_main;
530 unformat_input_t _line_input, *line_input = &_line_input;
531 perfmon_bundle_t *b = 0;
532 perfmon_bundle_type_t bundle_type = PERFMON_BUNDLE_TYPE_UNKNOWN;
535 return clib_error_return (0, "please stop first");
537 if (unformat_user (input, unformat_line_input, line_input) == 0)
538 return clib_error_return (0, "please specify bundle name");
540 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
542 if (unformat (line_input, "bundle %U", unformat_perfmon_bundle_name, &b))
544 else if (unformat (line_input, "type %U", unformat_perfmon_active_type,
548 return clib_error_return (0, "unknown input '%U'",
549 format_unformat_error, line_input);
551 unformat_free (line_input);
554 return clib_error_return (0, "please specify bundle name");
556 /* if there is more than one valid mode */
557 if (count_set_bits (b->type_flags) > 1)
559 /* what did the user indicate */
561 return clib_error_return (0, "please specify a valid type");
563 /* otherwise just use the default */
564 else if (!bundle_type)
567 (perfmon_bundle_type_t) count_trailing_zeros (b->type_flags);
570 b->active_type = bundle_type;
572 return perfmon_start (vm, b);
575 VLIB_CLI_COMMAND (perfmon_start_command, static) = {
576 .path = "perfmon start",
577 .short_help = "perfmon start bundle [<bundle-name>] type [<node|thread>]",
578 .function = perfmon_start_command_fn,
582 static clib_error_t *
583 perfmon_stop_command_fn (vlib_main_t *vm, unformat_input_t *input,
584 vlib_cli_command_t *cmd)
586 return perfmon_stop (vm);
589 VLIB_CLI_COMMAND (perfmon_stop_command, static) = {
590 .path = "perfmon stop",
591 .short_help = "perfmon stop",
592 .function = perfmon_stop_command_fn,