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 bundle_name_sort_cmp (void *a1, void *a2)
97 perfmon_bundle_t **n1 = a1;
98 perfmon_bundle_t **n2 = a2;
100 return clib_strcmp ((char *) (*n1)->name, (char *) (*n2)->name);
103 static clib_error_t *
104 show_perfmon_bundle_command_fn (vlib_main_t *vm, unformat_input_t *input,
105 vlib_cli_command_t *cmd)
107 perfmon_main_t *pm = &perfmon_main;
108 unformat_input_t _line_input, *line_input = &_line_input;
109 perfmon_bundle_t *b = 0, **vb = 0;
112 if (unformat_user (input, unformat_line_input, line_input))
114 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
116 if (unformat (line_input, "verbose"))
118 else if (unformat (line_input, "%U", unformat_perfmon_bundle_name,
122 return clib_error_return (0, "unknown input `%U'",
123 format_unformat_error, line_input);
125 unformat_free (line_input);
131 hash_foreach_mem (key, b, pm->bundle_by_name, vec_add (vb, &b, 1););
137 vlib_cli_output (vm, "%U\n", format_perfmon_bundle, 0, 0);
139 vec_sort_with_function (vb, bundle_name_sort_cmp);
141 for (int i = 0; i < vec_len (vb); i++)
142 /* bundle type will be unknown if no cpu_supports matched */
143 if (vb[i]->type != PERFMON_BUNDLE_TYPE_UNKNOWN)
144 vlib_cli_output (vm, "%U\n", format_perfmon_bundle, vb[i], verbose);
150 VLIB_CLI_COMMAND (show_perfmon_bundle_command, static) = {
151 .path = "show perfmon bundle",
152 .short_help = "show perfmon bundle [<bundle-name>] [verbose]",
153 .function = show_perfmon_bundle_command_fn,
158 format_perfmon_source (u8 *s, va_list *args)
160 perfmon_source_t *src = va_arg (*args, perfmon_source_t *);
161 int verbose = va_arg (*args, int);
164 return format (s, "%-20s%-9s %s", "Name", "NumEvents", "Description");
168 s = format (s, "name: %s\n", src->name);
169 s = format (s, "description: %s\n", src->description);
170 s = format (s, "Events:\n");
171 for (int i = 0; i < src->n_events; i++)
173 perfmon_event_t *e = src->events + i;
174 s = format (s, " %s", e->name);
175 if (src->format_config)
176 s = format (s, " (%U)\n", src->format_config, e->config);
178 s = format (s, " (0x%x)\n", e->config);
180 s = format (s, " %s\n", e->description);
183 if (src->instances_by_type)
185 s = format (s, "Instances:\n");
186 for (int i = 0; i < vec_len (src->instances_by_type); i++)
188 perfmon_instance_type_t *it;
189 it = vec_elt_at_index (src->instances_by_type, i);
190 if (vec_len (it->instances) == 0)
192 s = format (s, " %s:\n ", it->name);
193 for (int j = 0; j < vec_len (it->instances); j++)
195 perfmon_instance_t *in = vec_elt_at_index (it->instances, j);
196 s = format (s, " %s", in->name);
198 s = format (s, "\n");
203 s = format (s, "%-20s%9u %s", src->name, src->n_events, src->description);
208 static clib_error_t *
209 show_perfmon_source_command_fn (vlib_main_t *vm, unformat_input_t *input,
210 vlib_cli_command_t *cmd)
212 perfmon_main_t *pm = &perfmon_main;
213 unformat_input_t _line_input, *line_input = &_line_input;
214 perfmon_source_t *s = 0, **vs = 0;
217 if (unformat_user (input, unformat_line_input, line_input))
219 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
221 if (unformat (line_input, "verbose"))
223 else if (unformat (line_input, "%U", unformat_perfmon_source_name,
227 return clib_error_return (0, "unknown input `%U'",
228 format_unformat_error, line_input);
230 unformat_free (line_input);
236 hash_foreach_mem (key, s, pm->source_by_name, vec_add (vs, &s, 1););
242 vlib_cli_output (vm, "%U\n", format_perfmon_source, 0, 0);
244 for (int i = 0; i < vec_len (vs); i++)
245 vlib_cli_output (vm, "%U\n", format_perfmon_source, vs[i], verbose);
251 VLIB_CLI_COMMAND (show_perfmon_source_command, static) = {
252 .path = "show perfmon source",
253 .short_help = "show perfmon source [<source-name>] [verbose]",
254 .function = show_perfmon_source_command_fn,
258 static clib_error_t *
259 show_perfmon_active_bundle_command_fn (vlib_main_t *vm,
260 unformat_input_t *input,
261 vlib_cli_command_t *cmd)
263 perfmon_main_t *pm = &perfmon_main;
265 vlib_cli_output (vm, "%U\n", format_perfmon_bundle, pm->active_bundle, 1);
269 VLIB_CLI_COMMAND (show_perfmon_active_bundle_command, static) = {
270 .path = "show perfmon active-bundle",
271 .short_help = "show perfmon active-bundle",
272 .function = show_perfmon_active_bundle_command_fn,
276 static clib_error_t *
277 show_perfmon_stats_command_fn (vlib_main_t *vm, unformat_input_t *input,
278 vlib_cli_command_t *cmd)
280 perfmon_main_t *pm = &perfmon_main;
281 perfmon_bundle_t *b = pm->active_bundle;
282 clib_error_t *err = 0;
283 table_t table = {}, *t = &table;
285 perfmon_reading_t *r, *readings = 0;
286 perfmon_instance_type_t *it = pm->active_instance_type;
287 perfmon_instance_t *in;
292 return clib_error_return (0, "no bundle selected");
294 n_instances = vec_len (it->instances);
295 vec_validate (readings, n_instances - 1);
297 /*Only perform read() for THREAD or SYSTEM bundles*/
298 for (int i = 0; i < n_instances && b->type != PERFMON_BUNDLE_TYPE_NODE; i++)
300 in = vec_elt_at_index (it->instances, i);
301 r = vec_elt_at_index (readings, i);
303 if (read (pm->group_fds[i], r, (b->n_events + 3) * sizeof (u64)) == -1)
305 err = clib_error_return_unix (0, "read");
310 table_format_title (t, "%s", b->description);
312 table_add_header_col (t, 0);
313 table_add_header_row (t, 0);
315 if (b->column_headers)
317 char **hdr = b->column_headers;
319 table_format_cell (t, -1, n_row++, "%s", hdr++[0]);
323 for (int i = 0; i < n_instances; i++)
325 in = vec_elt_at_index (it->instances, i);
326 r = vec_elt_at_index (readings, i);
327 table_format_cell (t, col, -1, "%s", in->name, b->type);
328 if (b->type == PERFMON_BUNDLE_TYPE_NODE)
330 perfmon_thread_runtime_t *tr;
331 tr = vec_elt_at_index (pm->thread_runtimes, i);
332 for (int j = 0; j < tr->n_nodes; j++)
333 if (tr->node_stats[j].n_calls)
335 perfmon_node_stats_t ns;
336 table_format_cell (t, ++col, -1, "%U", format_vlib_node_name,
338 table_set_cell_align (t, col, -1, TTAA_RIGHT);
339 table_set_cell_fg_color (t, col, -1, TTAC_CYAN);
340 clib_memcpy_fast (&ns, tr->node_stats + j, sizeof (ns));
342 for (int j = 0; j < n_row; j++)
343 table_format_cell (t, col, j, "%U", b->format_fn, &ns, j,
349 for (int j = 0; j < n_row; j++)
350 table_format_cell (t, i, j, "%U", b->format_fn, r, j, b->type);
355 vlib_cli_output (vm, "%U\n", format_table, t);
359 vlib_cli_output (vm, "\n%s\n", b->footer);
367 VLIB_CLI_COMMAND (show_perfmon_stats_command, static) = {
368 .path = "show perfmon statistics",
369 .short_help = "show perfmon statistics [raw]",
370 .function = show_perfmon_stats_command_fn,
374 static clib_error_t *
375 perfmon_reset_command_fn (vlib_main_t *vm, unformat_input_t *input,
376 vlib_cli_command_t *cmd)
382 VLIB_CLI_COMMAND (perfmon_reset_command, static) = {
383 .path = "perfmon reset",
384 .short_help = "perfmon reset",
385 .function = perfmon_reset_command_fn,
389 static clib_error_t *
390 perfmon_start_command_fn (vlib_main_t *vm, unformat_input_t *input,
391 vlib_cli_command_t *cmd)
393 perfmon_main_t *pm = &perfmon_main;
394 unformat_input_t _line_input, *line_input = &_line_input;
395 perfmon_bundle_t *b = 0;
398 return clib_error_return (0, "please stop first");
400 if (unformat_user (input, unformat_line_input, line_input) == 0)
401 return clib_error_return (0, "please specify bundle name");
403 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
405 if (unformat (line_input, "bundle %U", unformat_perfmon_bundle_name, &b))
408 return clib_error_return (0, "unknown input '%U'",
409 format_unformat_error, line_input);
411 unformat_free (line_input);
414 return clib_error_return (0, "please specify bundle name");
416 return perfmon_start (vm, b);
419 VLIB_CLI_COMMAND (perfmon_start_command, static) = {
420 .path = "perfmon start",
421 .short_help = "perfmon start bundle [<bundle-name>]",
422 .function = perfmon_start_command_fn,
426 static clib_error_t *
427 perfmon_stop_command_fn (vlib_main_t *vm, unformat_input_t *input,
428 vlib_cli_command_t *cmd)
430 return perfmon_stop (vm);
433 VLIB_CLI_COMMAND (perfmon_stop_command, static) = {
434 .path = "perfmon stop",
435 .short_help = "perfmon stop",
436 .function = perfmon_stop_command_fn,