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 <perfmon/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_source_name (unformat_input_t *input, va_list *args)
43 perfmon_main_t *pm = &perfmon_main;
44 perfmon_source_t **b = va_arg (*args, perfmon_source_t **);
48 if (unformat (input, "%s", &str) == 0)
51 p = hash_get_mem (pm->source_by_name, str);
54 b[0] = (perfmon_source_t *) p[0];
61 format_perfmon_bundle (u8 *s, va_list *args)
63 perfmon_bundle_t *b = va_arg (*args, perfmon_bundle_t *);
64 int verbose = va_arg (*args, int);
66 const char *bundle_type[] = {
67 [PERFMON_BUNDLE_TYPE_NODE] = "node",
68 [PERFMON_BUNDLE_TYPE_THREAD] = "thread",
69 [PERFMON_BUNDLE_TYPE_SYSTEM] = "system",
73 return format (s, "%-20s%-10s%-20s%s", "Name", "Type", "Source",
78 s = format (s, "name: %s\n", b->name);
79 s = format (s, "description: %s\n", b->description);
80 s = format (s, "source: %s\n", b->src->name);
81 for (int i = 0; i < b->n_events; i++)
83 perfmon_event_t *e = b->src->events + b->events[i];
84 s = format (s, "event %u: %s\n", i, e->name);
88 s = format (s, "%-20s%-10s%-20s%s", b->name, bundle_type[b->type],
89 b->src->name, b->description);
95 show_perfmon_bundle_command_fn (vlib_main_t *vm, unformat_input_t *input,
96 vlib_cli_command_t *cmd)
98 perfmon_main_t *pm = &perfmon_main;
99 unformat_input_t _line_input, *line_input = &_line_input;
100 perfmon_bundle_t *b = 0, **vb = 0;
103 if (unformat_user (input, unformat_line_input, line_input))
105 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
107 if (unformat (line_input, "verbose"))
109 else if (unformat (line_input, "%U", unformat_perfmon_bundle_name,
113 return clib_error_return (0, "unknown input `%U'",
114 format_unformat_error, line_input);
116 unformat_free (line_input);
122 hash_foreach_mem (key, b, pm->bundle_by_name, vec_add (vb, &b, 1););
128 vlib_cli_output (vm, "%U\n", format_perfmon_bundle, 0, 0);
130 for (int i = 0; i < vec_len (vb); i++)
131 vlib_cli_output (vm, "%U\n", format_perfmon_bundle, vb[i], verbose);
137 VLIB_CLI_COMMAND (show_perfmon_bundle_command, static) = {
138 .path = "show perfmon bundle",
139 .short_help = "show perfmon bundle [<bundle-name>] [verbose]",
140 .function = show_perfmon_bundle_command_fn,
145 format_perfmon_source (u8 *s, va_list *args)
147 perfmon_source_t *src = va_arg (*args, perfmon_source_t *);
148 int verbose = va_arg (*args, int);
151 return format (s, "%-20s%-9s %s", "Name", "NumEvents", "Description");
155 s = format (s, "name: %s\n", src->name);
156 s = format (s, "description: %s\n", src->description);
157 s = format (s, "Events:\n");
158 for (int i = 0; i < src->n_events; i++)
160 perfmon_event_t *e = src->events + i;
161 s = format (s, " %s", e->name);
162 if (src->format_config)
163 s = format (s, " (%U)\n", src->format_config, e->config);
165 s = format (s, " (0x%x)\n", e->config);
167 s = format (s, " %s\n", e->description);
170 if (src->instances_by_type)
172 s = format (s, "Instances:\n");
173 for (int i = 0; i < vec_len (src->instances_by_type); i++)
175 perfmon_instance_type_t *it;
176 it = vec_elt_at_index (src->instances_by_type, i);
177 if (vec_len (it->instances) == 0)
179 s = format (s, " %s:\n ", it->name);
180 for (int j = 0; j < vec_len (it->instances); j++)
182 perfmon_instance_t *in = vec_elt_at_index (it->instances, j);
183 s = format (s, " %s", in->name);
185 s = format (s, "\n");
190 s = format (s, "%-20s%9u %s", src->name, src->n_events, src->description);
195 static clib_error_t *
196 show_perfmon_source_command_fn (vlib_main_t *vm, unformat_input_t *input,
197 vlib_cli_command_t *cmd)
199 perfmon_main_t *pm = &perfmon_main;
200 unformat_input_t _line_input, *line_input = &_line_input;
201 perfmon_source_t *s = 0, **vs = 0;
204 if (unformat_user (input, unformat_line_input, line_input))
206 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
208 if (unformat (line_input, "verbose"))
210 else if (unformat (line_input, "%U", unformat_perfmon_source_name,
214 return clib_error_return (0, "unknown input `%U'",
215 format_unformat_error, line_input);
217 unformat_free (line_input);
223 hash_foreach_mem (key, s, pm->source_by_name, vec_add (vs, &s, 1););
229 vlib_cli_output (vm, "%U\n", format_perfmon_source, 0, 0);
231 for (int i = 0; i < vec_len (vs); i++)
232 vlib_cli_output (vm, "%U\n", format_perfmon_source, vs[i], verbose);
238 VLIB_CLI_COMMAND (show_perfmon_source_command, static) = {
239 .path = "show perfmon source",
240 .short_help = "show perfmon source [<source-name>] [verbose]",
241 .function = show_perfmon_source_command_fn,
245 static clib_error_t *
246 show_perfmon_active_bundle_command_fn (vlib_main_t *vm,
247 unformat_input_t *input,
248 vlib_cli_command_t *cmd)
250 perfmon_main_t *pm = &perfmon_main;
252 vlib_cli_output (vm, "%U\n", format_perfmon_bundle, pm->active_bundle, 1);
257 VLIB_CLI_COMMAND (show_perfmon_active_bundle_command, static) = {
258 .path = "show perfmon active-bundle",
259 .short_help = "show perfmon active-bundle",
260 .function = show_perfmon_active_bundle_command_fn,
264 static clib_error_t *
265 show_perfmon_stats_command_fn (vlib_main_t *vm, unformat_input_t *input,
266 vlib_cli_command_t *cmd)
268 perfmon_main_t *pm = &perfmon_main;
269 perfmon_bundle_t *b = pm->active_bundle;
270 clib_error_t *err = 0;
271 table_t table = {}, *t = &table;
273 perfmon_reading_t *r, *readings = 0;
274 perfmon_instance_type_t *it = pm->active_instance_type;
275 perfmon_instance_t *in;
280 return clib_error_return (0, "no budle selected");
282 n_instances = vec_len (it->instances);
283 vec_validate (readings, n_instances - 1);
285 for (int i = 0; i < n_instances; i++)
287 in = vec_elt_at_index (it->instances, i);
288 r = vec_elt_at_index (readings, i);
290 if (read (pm->group_fds[i], r, (b->n_events + 3) * sizeof (u64)) == -1)
292 err = clib_error_return_unix (0, "read");
297 table_format_title (t, "%s", b->description);
299 table_add_header_col (t, 0);
300 table_add_header_row (t, 0);
302 if (b->column_headers)
304 char **hdr = b->column_headers;
306 table_format_cell (t, -1, n_row++, "%s", hdr++[0]);
310 for (int i = 0; i < n_instances; i++)
312 in = vec_elt_at_index (it->instances, i);
313 r = vec_elt_at_index (readings, i);
314 table_format_cell (t, col, -1, "%s", in->name);
315 if (b->type == PERFMON_BUNDLE_TYPE_NODE)
317 perfmon_thread_runtime_t *tr;
318 tr = vec_elt_at_index (pm->thread_runtimes, i);
319 for (int j = 0; j < tr->n_nodes; j++)
320 if (tr->node_stats[j].n_calls)
322 perfmon_node_stats_t ns;
323 table_format_cell (t, ++col, -1, "%U", format_vlib_node_name,
325 table_set_cell_align (t, col, -1, TTAA_RIGHT);
326 table_set_cell_fg_color (t, col, -1, TTAC_CYAN);
327 clib_memcpy_fast (&ns, tr->node_stats + j, sizeof (ns));
328 for (int j = 0; j < n_row; j++)
329 table_format_cell (t, col, j, "%U", b->format_fn, &ns, j);
334 for (int j = 0; j < n_row; j++)
335 table_format_cell (t, i, j, "%U", b->format_fn, r, j);
340 vlib_cli_output (vm, "%U\n", format_table, t);
344 vlib_cli_output (vm, "\n%s\n", b->footer);
352 VLIB_CLI_COMMAND (show_perfmon_stats_command, static) = {
353 .path = "show perfmon statistics",
354 .short_help = "show perfmon statistics",
355 .function = show_perfmon_stats_command_fn,
359 static clib_error_t *
360 set_perfmon_bundle_command_fn (vlib_main_t *vm, unformat_input_t *input,
361 vlib_cli_command_t *cmd)
363 perfmon_main_t *pm = &perfmon_main;
364 unformat_input_t _line_input, *line_input = &_line_input;
365 perfmon_bundle_t *b = 0;
367 if (unformat_user (input, unformat_line_input, line_input) == 0)
368 return clib_error_return (0, "please specify bundle name");
370 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
372 if (unformat (line_input, "%U", unformat_perfmon_bundle_name, &b))
375 return clib_error_return (0, "unknown input '%U'",
376 format_unformat_error, line_input);
378 unformat_free (line_input);
381 return clib_error_return (0, "please specify bundle name");
384 return clib_error_return (0, "please stop first");
386 return perfmon_set (vm, b);
389 VLIB_CLI_COMMAND (set_perfmon_bundle_command, static) = {
390 .path = "set perfmon bundle",
391 .short_help = "set perfmon bundle [<bundle-name>]",
392 .function = set_perfmon_bundle_command_fn,
396 static clib_error_t *
397 perfmon_reset_command_fn (vlib_main_t *vm, unformat_input_t *input,
398 vlib_cli_command_t *cmd)
404 VLIB_CLI_COMMAND (perfmon_reset_command, static) = {
405 .path = "perfmon reset",
406 .short_help = "perfmon reset",
407 .function = perfmon_reset_command_fn,
411 static clib_error_t *
412 perfmon_start_command_fn (vlib_main_t *vm, unformat_input_t *input,
413 vlib_cli_command_t *cmd)
415 return perfmon_start (vm);
418 VLIB_CLI_COMMAND (perfmon_start_command, static) = {
419 .path = "perfmon start",
420 .short_help = "perfmon start",
421 .function = perfmon_start_command_fn,
425 static clib_error_t *
426 perfmon_stop_command_fn (vlib_main_t *vm, unformat_input_t *input,
427 vlib_cli_command_t *cmd)
429 return perfmon_stop (vm);
432 VLIB_CLI_COMMAND (perfmon_stop_command, static) = {
433 .path = "perfmon stop",
434 .short_help = "perfmon stop",
435 .function = perfmon_stop_command_fn,