perfmon: Fix typo in debug log messages
[vpp.git] / src / plugins / perfmon / cli.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <vnet/vnet.h>
17 #include <perfmon/perfmon.h>
18 #include <vppinfra/format_table.h>
19
20 uword
21 unformat_perfmon_bundle_name (unformat_input_t *input, va_list *args)
22 {
23   perfmon_main_t *pm = &perfmon_main;
24   perfmon_bundle_t **b = va_arg (*args, perfmon_bundle_t **);
25   uword *p;
26   u8 *str = 0;
27
28   if (unformat (input, "%s", &str) == 0)
29     return 0;
30
31   p = hash_get_mem (pm->bundle_by_name, str);
32
33   if (p)
34     b[0] = (perfmon_bundle_t *) p[0];
35
36   vec_free (str);
37   return p ? 1 : 0;
38 }
39
40 uword
41 unformat_perfmon_active_type (unformat_input_t *input, va_list *args)
42 {
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 *);
45   char *str = 0;
46
47   char *_str_types[PERFMON_BUNDLE_TYPE_MAX];
48
49 #define _(type, pstr) _str_types[type] = (char *) pstr;
50
51   foreach_perfmon_bundle_type
52 #undef _
53
54     if (!b) return 0;
55
56   if (unformat (input, "%s", &str) == 0)
57     return 0;
58
59   for (int i = PERFMON_BUNDLE_TYPE_NODE; i < PERFMON_BUNDLE_TYPE_MAX; i++)
60     {
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))
64         {
65           *bundle_type = i;
66           break;
67         }
68     }
69
70   vec_free (str);
71   return bundle_type ? 1 : 0;
72 }
73
74 uword
75 unformat_perfmon_source_name (unformat_input_t *input, va_list *args)
76 {
77   perfmon_main_t *pm = &perfmon_main;
78   perfmon_source_t **b = va_arg (*args, perfmon_source_t **);
79   uword *p;
80   u8 *str = 0;
81
82   if (unformat (input, "%s", &str) == 0)
83     return 0;
84
85   p = hash_get_mem (pm->source_by_name, str);
86
87   if (p)
88     b[0] = (perfmon_source_t *) p[0];
89
90   vec_free (str);
91   return p ? 1 : 0;
92 }
93
94 typedef enum
95 {
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;
100
101 u8 *
102 format_perfmon_bundle (u8 *s, va_list *args)
103 {
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);
107
108   int vl = 0;
109
110   u8 *_bundle_type = 0;
111   const char *bundle_type[PERFMON_BUNDLE_TYPE_MAX];
112 #define _(type, pstr) bundle_type[type] = (const char *) pstr;
113
114   foreach_perfmon_bundle_type
115 #undef _
116
117     if (b == 0) return format (s, "%-20s%-20s%-20s%s", "Name", "Type(s)",
118                                "Source", "Description");
119
120   if (cfg != FORMAT_PERFMON_BUNDLE_NONE)
121     {
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++)
126         {
127           perfmon_event_t *e = b->src->events + b->events[i];
128           s = format (s, "event %u: %s", i, e->name);
129
130           format_function_t *format_config = b->src->format_config;
131
132           if (format_config && cfg == FORMAT_PERFMON_BUNDLE_SHOW_CONFIG)
133             s = format (s, " (%U)", format_config, e->config);
134
135           s = format (s, "\n");
136         }
137     }
138   else
139     {
140       s = format (s, "%-20s", b->name);
141       for (int i = PERFMON_BUNDLE_TYPE_NODE; i < PERFMON_BUNDLE_TYPE_MAX; i++)
142         {
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]);
146         }
147       /* remove any stray commas */
148       if ((vl = vec_len (_bundle_type)))
149         _bundle_type[vl - 1] = 0;
150
151       s =
152         format (s, "%-20s%-20s%s", _bundle_type, b->src->name, b->description);
153     }
154
155   vec_free (_bundle_type);
156
157   return s;
158 }
159
160 static int
161 bundle_name_sort_cmp (void *a1, void *a2)
162 {
163   perfmon_bundle_t **n1 = a1;
164   perfmon_bundle_t **n2 = a2;
165
166   return clib_strcmp ((char *) (*n1)->name, (char *) (*n2)->name);
167 }
168
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)
172 {
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;
176   int verbose = 0;
177   format_perfmon_bundle_args_t cfg = FORMAT_PERFMON_BUNDLE_NONE;
178
179   if (unformat_user (input, unformat_line_input, line_input))
180     {
181       while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
182         {
183           if (unformat (line_input, "verbose"))
184             verbose = 1;
185           else if (unformat (line_input, "%U", unformat_perfmon_bundle_name,
186                              &b))
187             vec_add (vb, &b, 1);
188           else
189             return clib_error_return (0, "unknown input `%U'",
190                                       format_unformat_error, line_input);
191         }
192       unformat_free (line_input);
193     }
194
195   if (verbose) /* if verbose is specified */
196     cfg = FORMAT_PERFMON_BUNDLE_VERBOSE;
197
198   if (vb)
199     {
200       if (verbose) /* if verbose is specified with a bundle */
201         cfg = FORMAT_PERFMON_BUNDLE_SHOW_CONFIG;
202       else
203         cfg = FORMAT_PERFMON_BUNDLE_VERBOSE;
204     }
205   else
206     {
207       char *key;
208       hash_foreach_mem (key, b, pm->bundle_by_name, vec_add (vb, &b, 1););
209     }
210
211   if (cfg == FORMAT_PERFMON_BUNDLE_NONE)
212     vlib_cli_output (vm, "%U\n", format_perfmon_bundle, 0, cfg);
213
214   vec_sort_with_function (vb, bundle_name_sort_cmp);
215
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);
220
221   vec_free (vb);
222   return 0;
223 }
224
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,
229   .is_mp_safe = 1,
230 };
231
232 u8 *
233 format_perfmon_source (u8 *s, va_list *args)
234 {
235   perfmon_source_t *src = va_arg (*args, perfmon_source_t *);
236   int verbose = va_arg (*args, int);
237
238   if (src == 0)
239     return format (s, "%-20s%-9s %s", "Name", "NumEvents", "Description");
240
241   if (verbose)
242     {
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++)
247         {
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);
252           else
253             s = format (s, " (0x%x)\n", e->config);
254           if (e->description)
255             s = format (s, "    %s\n", e->description);
256         }
257
258       if (src->instances_by_type)
259         {
260           s = format (s, "Instances:\n");
261           for (int i = 0; i < vec_len (src->instances_by_type); i++)
262             {
263               perfmon_instance_type_t *it;
264               it = vec_elt_at_index (src->instances_by_type, i);
265               if (vec_len (it->instances) == 0)
266                 continue;
267               s = format (s, "  %s:\n   ", it->name);
268               for (int j = 0; j < vec_len (it->instances); j++)
269                 {
270                   perfmon_instance_t *in = vec_elt_at_index (it->instances, j);
271                   s = format (s, " %s", in->name);
272                 }
273               s = format (s, "\n");
274             }
275         }
276     }
277   else
278     s = format (s, "%-20s%9u %s", src->name, src->n_events, src->description);
279
280   return s;
281 }
282
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)
286 {
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;
290   int verbose = 0;
291
292   if (unformat_user (input, unformat_line_input, line_input))
293     {
294       while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
295         {
296           if (unformat (line_input, "verbose"))
297             verbose = 1;
298           else if (unformat (line_input, "%U", unformat_perfmon_source_name,
299                              &s))
300             vec_add (vs, &s, 1);
301           else
302             return clib_error_return (0, "unknown input `%U'",
303                                       format_unformat_error, line_input);
304         }
305       unformat_free (line_input);
306     }
307
308   if (vs == 0)
309     {
310       char *key;
311       hash_foreach_mem (key, s, pm->source_by_name, vec_add (vs, &s, 1););
312     }
313   else
314     verbose = 1;
315
316   if (verbose == 0)
317     vlib_cli_output (vm, "%U\n", format_perfmon_source, 0, 0);
318
319   for (int i = 0; i < vec_len (vs); i++)
320     vlib_cli_output (vm, "%U\n", format_perfmon_source, vs[i], verbose);
321
322   vec_free (vs);
323   return 0;
324 }
325
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,
330   .is_mp_safe = 1,
331 };
332
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)
337 {
338   perfmon_main_t *pm = &perfmon_main;
339
340   vlib_cli_output (vm, "%U\n", format_perfmon_bundle, pm->active_bundle, 1);
341   return 0;
342 }
343
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,
348   .is_mp_safe = 1,
349 };
350
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)
354 {
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;
359   u32 n_instances;
360   perfmon_reading_t *r, *readings = 0;
361   perfmon_instance_type_t *it = pm->active_instance_type;
362   perfmon_instance_t *in;
363   u8 *s = 0;
364   int n_row = 0;
365
366   if (b == 0)
367     return clib_error_return (0, "no bundle selected");
368
369   n_instances = vec_len (it->instances);
370   vec_validate (readings, n_instances - 1);
371
372   /*Only perform read() for THREAD or SYSTEM bundles*/
373   for (int i = 0;
374        i < n_instances && b->active_type != PERFMON_BUNDLE_TYPE_NODE; i++)
375     {
376       in = vec_elt_at_index (it->instances, i);
377       r = vec_elt_at_index (readings, i);
378
379       if (read (pm->group_fds[i], r, (b->n_events + 3) * sizeof (u64)) == -1)
380         {
381           err = clib_error_return_unix (0, "read");
382           goto done;
383         }
384     }
385
386   table_format_title (t, "%s", b->description);
387
388   table_add_header_col (t, 0);
389   table_add_header_row (t, 0);
390
391   if (b->column_headers)
392     {
393       char **hdr = b->column_headers;
394       while (hdr[0])
395         table_format_cell (t, -1, n_row++, "%s", hdr++[0]);
396     }
397
398   int col = 0;
399   for (int i = 0; i < n_instances; i++)
400     {
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)
405         {
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)
410               {
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);
416                 clib_memcpy_fast (&ns, tr->node_stats + j, sizeof (ns));
417
418                 for (int j = 0; j < n_row; j++)
419                   table_format_cell (t, col, j, "%U", b->format_fn, &ns, j,
420                                      b->active_type);
421               }
422         }
423       else
424         {
425           for (int j = 0; j < n_row; j++)
426             table_format_cell (t, i, j, "%U", b->format_fn, r, j,
427                                b->active_type);
428         }
429       col++;
430     }
431
432   vlib_cli_output (vm, "%U\n", format_table, t);
433   table_free (t);
434
435   if (b->footer)
436     vlib_cli_output (vm, "\n%s\n", b->footer);
437
438 done:
439   vec_free (readings);
440   vec_free (s);
441   return err;
442 }
443
444 VLIB_CLI_COMMAND (show_perfmon_stats_command, static) = {
445   .path = "show perfmon statistics",
446   .short_help = "show perfmon statistics [raw]",
447   .function = show_perfmon_stats_command_fn,
448   .is_mp_safe = 1,
449 };
450
451 static clib_error_t *
452 perfmon_reset_command_fn (vlib_main_t *vm, unformat_input_t *input,
453                           vlib_cli_command_t *cmd)
454 {
455   perfmon_reset (vm);
456   return 0;
457 }
458
459 VLIB_CLI_COMMAND (perfmon_reset_command, static) = {
460   .path = "perfmon reset",
461   .short_help = "perfmon reset",
462   .function = perfmon_reset_command_fn,
463   .is_mp_safe = 1,
464 };
465
466 static clib_error_t *
467 perfmon_start_command_fn (vlib_main_t *vm, unformat_input_t *input,
468                           vlib_cli_command_t *cmd)
469 {
470   perfmon_main_t *pm = &perfmon_main;
471   unformat_input_t _line_input, *line_input = &_line_input;
472   perfmon_bundle_t *b = 0;
473   perfmon_bundle_type_t bundle_type = PERFMON_BUNDLE_TYPE_UNKNOWN;
474
475   if (pm->is_running)
476     return clib_error_return (0, "please stop first");
477
478   if (unformat_user (input, unformat_line_input, line_input) == 0)
479     return clib_error_return (0, "please specify bundle name");
480
481   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
482     {
483       if (unformat (line_input, "bundle %U", unformat_perfmon_bundle_name, &b))
484         ;
485       else if (unformat (line_input, "type %U", unformat_perfmon_active_type,
486                          b, &bundle_type))
487         ;
488       else
489         return clib_error_return (0, "unknown input '%U'",
490                                   format_unformat_error, line_input);
491     }
492   unformat_free (line_input);
493
494   if (b == 0)
495     return clib_error_return (0, "please specify bundle name");
496
497   /* if there is more than one valid mode */
498   if (count_set_bits (b->type_flags) > 1)
499     {
500       /* what did the user indicate */
501       if (!bundle_type)
502         return clib_error_return (0, "please specify a valid type");
503     }
504   else /* otherwise just use the default  */
505     {
506       if (bundle_type && !(b->type_flags & bundle_type))
507         return clib_error_return (0, "please specify a valid type");
508
509       bundle_type =
510         (perfmon_bundle_type_t) count_trailing_zeros (b->type_flags);
511     }
512
513   b->active_type = bundle_type;
514
515   return perfmon_start (vm, b);
516 }
517
518 VLIB_CLI_COMMAND (perfmon_start_command, static) = {
519   .path = "perfmon start",
520   .short_help = "perfmon start bundle [<bundle-name>] type [<node|thread>]",
521   .function = perfmon_start_command_fn,
522   .is_mp_safe = 1,
523 };
524
525 static clib_error_t *
526 perfmon_stop_command_fn (vlib_main_t *vm, unformat_input_t *input,
527                          vlib_cli_command_t *cmd)
528 {
529   return perfmon_stop (vm);
530 }
531
532 VLIB_CLI_COMMAND (perfmon_stop_command, static) = {
533   .path = "perfmon stop",
534   .short_help = "perfmon stop",
535   .function = perfmon_stop_command_fn,
536   .is_mp_safe = 1,
537 };