perfmon: top down level 1 support
[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 <perfmon/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_source_name (unformat_input_t *input, va_list *args)
42 {
43   perfmon_main_t *pm = &perfmon_main;
44   perfmon_source_t **b = va_arg (*args, perfmon_source_t **);
45   uword *p;
46   u8 *str = 0;
47
48   if (unformat (input, "%s", &str) == 0)
49     return 0;
50
51   p = hash_get_mem (pm->source_by_name, str);
52
53   if (p)
54     b[0] = (perfmon_source_t *) p[0];
55
56   vec_free (str);
57   return p ? 1 : 0;
58 }
59
60 u8 *
61 format_perfmon_bundle (u8 *s, va_list *args)
62 {
63   perfmon_bundle_t *b = va_arg (*args, perfmon_bundle_t *);
64   int verbose = va_arg (*args, int);
65
66   const char *bundle_type[] = {
67     [PERFMON_BUNDLE_TYPE_NODE] = "node",
68     [PERFMON_BUNDLE_TYPE_THREAD] = "thread",
69     [PERFMON_BUNDLE_TYPE_SYSTEM] = "system",
70   };
71
72   if (b == 0)
73     return format (s, "%-20s%-10s%-20s%s", "Name", "Type", "Source",
74                    "Description");
75
76   if (verbose)
77     {
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++)
82         {
83           perfmon_event_t *e = b->src->events + b->events[i];
84           s = format (s, "event %u: %s\n", i, e->name);
85         }
86     }
87   else
88     s = format (s, "%-20s%-10s%-20s%s", b->name, bundle_type[b->type],
89                 b->src->name, b->description);
90
91   return s;
92 }
93
94 static clib_error_t *
95 show_perfmon_bundle_command_fn (vlib_main_t *vm, unformat_input_t *input,
96                                 vlib_cli_command_t *cmd)
97 {
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;
101   int verbose = 0;
102
103   if (unformat_user (input, unformat_line_input, line_input))
104     {
105       while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
106         {
107           if (unformat (line_input, "verbose"))
108             verbose = 1;
109           else if (unformat (line_input, "%U", unformat_perfmon_bundle_name,
110                              &b))
111             vec_add (vb, &b, 1);
112           else
113             return clib_error_return (0, "unknown input `%U'",
114                                       format_unformat_error, line_input);
115         }
116       unformat_free (line_input);
117     }
118
119   if (vb == 0)
120     {
121       char *key;
122       hash_foreach_mem (key, b, pm->bundle_by_name, vec_add (vb, &b, 1););
123     }
124   else
125     verbose = 1;
126
127   if (verbose == 0)
128     vlib_cli_output (vm, "%U\n", format_perfmon_bundle, 0, 0);
129
130   for (int i = 0; i < vec_len (vb); i++)
131     if (!vb[i]->cpu_supports || vb[i]->cpu_supports ())
132       vlib_cli_output (vm, "%U\n", format_perfmon_bundle, vb[i], verbose);
133
134   vec_free (vb);
135   return 0;
136 }
137
138 VLIB_CLI_COMMAND (show_perfmon_bundle_command, static) = {
139   .path = "show perfmon bundle",
140   .short_help = "show perfmon bundle [<bundle-name>] [verbose]",
141   .function = show_perfmon_bundle_command_fn,
142   .is_mp_safe = 1,
143 };
144
145 u8 *
146 format_perfmon_source (u8 *s, va_list *args)
147 {
148   perfmon_source_t *src = va_arg (*args, perfmon_source_t *);
149   int verbose = va_arg (*args, int);
150
151   if (src == 0)
152     return format (s, "%-20s%-9s %s", "Name", "NumEvents", "Description");
153
154   if (verbose)
155     {
156       s = format (s, "name:        %s\n", src->name);
157       s = format (s, "description: %s\n", src->description);
158       s = format (s, "Events:\n");
159       for (int i = 0; i < src->n_events; i++)
160         {
161           perfmon_event_t *e = src->events + i;
162           s = format (s, "  %s", e->name);
163           if (src->format_config)
164             s = format (s, " (%U)\n", src->format_config, e->config);
165           else
166             s = format (s, " (0x%x)\n", e->config);
167           if (e->description)
168             s = format (s, "    %s\n", e->description);
169         }
170
171       if (src->instances_by_type)
172         {
173           s = format (s, "Instances:\n");
174           for (int i = 0; i < vec_len (src->instances_by_type); i++)
175             {
176               perfmon_instance_type_t *it;
177               it = vec_elt_at_index (src->instances_by_type, i);
178               if (vec_len (it->instances) == 0)
179                 continue;
180               s = format (s, "  %s:\n   ", it->name);
181               for (int j = 0; j < vec_len (it->instances); j++)
182                 {
183                   perfmon_instance_t *in = vec_elt_at_index (it->instances, j);
184                   s = format (s, " %s", in->name);
185                 }
186               s = format (s, "\n");
187             }
188         }
189     }
190   else
191     s = format (s, "%-20s%9u %s", src->name, src->n_events, src->description);
192
193   return s;
194 }
195
196 static clib_error_t *
197 show_perfmon_source_command_fn (vlib_main_t *vm, unformat_input_t *input,
198                                 vlib_cli_command_t *cmd)
199 {
200   perfmon_main_t *pm = &perfmon_main;
201   unformat_input_t _line_input, *line_input = &_line_input;
202   perfmon_source_t *s = 0, **vs = 0;
203   int verbose = 0;
204
205   if (unformat_user (input, unformat_line_input, line_input))
206     {
207       while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
208         {
209           if (unformat (line_input, "verbose"))
210             verbose = 1;
211           else if (unformat (line_input, "%U", unformat_perfmon_source_name,
212                              &s))
213             vec_add (vs, &s, 1);
214           else
215             return clib_error_return (0, "unknown input `%U'",
216                                       format_unformat_error, line_input);
217         }
218       unformat_free (line_input);
219     }
220
221   if (vs == 0)
222     {
223       char *key;
224       hash_foreach_mem (key, s, pm->source_by_name, vec_add (vs, &s, 1););
225     }
226   else
227     verbose = 1;
228
229   if (verbose == 0)
230     vlib_cli_output (vm, "%U\n", format_perfmon_source, 0, 0);
231
232   for (int i = 0; i < vec_len (vs); i++)
233     vlib_cli_output (vm, "%U\n", format_perfmon_source, vs[i], verbose);
234
235   vec_free (vs);
236   return 0;
237 }
238
239 VLIB_CLI_COMMAND (show_perfmon_source_command, static) = {
240   .path = "show perfmon source",
241   .short_help = "show perfmon source [<source-name>] [verbose]",
242   .function = show_perfmon_source_command_fn,
243   .is_mp_safe = 1,
244 };
245
246 static clib_error_t *
247 show_perfmon_active_bundle_command_fn (vlib_main_t *vm,
248                                        unformat_input_t *input,
249                                        vlib_cli_command_t *cmd)
250 {
251   perfmon_main_t *pm = &perfmon_main;
252
253   vlib_cli_output (vm, "%U\n", format_perfmon_bundle, pm->active_bundle, 1);
254   return 0;
255 }
256
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,
261   .is_mp_safe = 1,
262 };
263
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)
267 {
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;
272   u32 n_instances;
273   perfmon_reading_t *r, *readings = 0;
274   perfmon_instance_type_t *it = pm->active_instance_type;
275   perfmon_instance_t *in;
276   u8 *s = 0;
277   int n_row = 0;
278   u8 raw = 0;
279
280   if (b == 0)
281     return clib_error_return (0, "no bundle selected");
282
283   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
284     {
285       if (unformat (input, "raw"))
286         raw = 1;
287       else
288         break;
289     }
290
291   n_instances = vec_len (it->instances);
292   vec_validate (readings, n_instances - 1);
293
294   /*Only perform read() for THREAD or SYSTEM bundles*/
295   for (int i = 0; i < n_instances && b->type != PERFMON_BUNDLE_TYPE_NODE; i++)
296     {
297       in = vec_elt_at_index (it->instances, i);
298       r = vec_elt_at_index (readings, i);
299
300       if (read (pm->group_fds[i], r, (b->n_events + 3) * sizeof (u64)) == -1)
301         {
302           err = clib_error_return_unix (0, "read");
303           goto done;
304         }
305     }
306
307   table_format_title (t, "%s", b->description);
308
309   table_add_header_col (t, 0);
310   table_add_header_row (t, 0);
311
312   if (b->column_headers)
313     {
314       char **hdr = b->column_headers;
315       while (hdr[0])
316         table_format_cell (t, -1, n_row++, "%s", hdr++[0]);
317
318       if (b->raw_column_headers && raw)
319         {
320           hdr = b->raw_column_headers;
321           while (hdr[0])
322             table_format_cell (t, -1, n_row++, "%s", hdr++[0]);
323         }
324     }
325
326   int col = 0;
327   for (int i = 0; i < n_instances; i++)
328     {
329       in = vec_elt_at_index (it->instances, i);
330       r = vec_elt_at_index (readings, i);
331       table_format_cell (t, col, -1, "%s", in->name);
332       if (b->type == PERFMON_BUNDLE_TYPE_NODE)
333         {
334           perfmon_thread_runtime_t *tr;
335           tr = vec_elt_at_index (pm->thread_runtimes, i);
336           for (int j = 0; j < tr->n_nodes; j++)
337             if (tr->node_stats[j].n_calls)
338               {
339                 perfmon_node_stats_t ns;
340                 table_format_cell (t, ++col, -1, "%U", format_vlib_node_name,
341                                    vm, j);
342                 table_set_cell_align (t, col, -1, TTAA_RIGHT);
343                 table_set_cell_fg_color (t, col, -1, TTAC_CYAN);
344                 clib_memcpy_fast (&ns, tr->node_stats + j, sizeof (ns));
345
346                 for (int j = 0; j < n_row; j++)
347                   table_format_cell (t, col, j, "%U", b->format_fn, &ns, j);
348               }
349         }
350       else
351         {
352           for (int j = 0; j < n_row; j++)
353             table_format_cell (t, i, j, "%U", b->format_fn, r, j);
354         }
355       col++;
356     }
357
358   vlib_cli_output (vm, "%U\n", format_table, t);
359   table_free (t);
360
361   if (raw)
362     vlib_cli_output (vm, "Sample time is %.4f seconds \n", pm->sample_time);
363
364   if (b->footer)
365     vlib_cli_output (vm, "\n%s\n", b->footer);
366
367 done:
368   vec_free (readings);
369   vec_free (s);
370   return err;
371 }
372
373 VLIB_CLI_COMMAND (show_perfmon_stats_command, static) = {
374   .path = "show perfmon statistics",
375   .short_help = "show perfmon statistics [raw]",
376   .function = show_perfmon_stats_command_fn,
377   .is_mp_safe = 1,
378 };
379
380 static clib_error_t *
381 perfmon_reset_command_fn (vlib_main_t *vm, unformat_input_t *input,
382                           vlib_cli_command_t *cmd)
383 {
384   perfmon_reset (vm);
385   return 0;
386 }
387
388 VLIB_CLI_COMMAND (perfmon_reset_command, static) = {
389   .path = "perfmon reset",
390   .short_help = "perfmon reset",
391   .function = perfmon_reset_command_fn,
392   .is_mp_safe = 1,
393 };
394
395 static clib_error_t *
396 perfmon_start_command_fn (vlib_main_t *vm, unformat_input_t *input,
397                           vlib_cli_command_t *cmd)
398 {
399   perfmon_main_t *pm = &perfmon_main;
400   unformat_input_t _line_input, *line_input = &_line_input;
401   perfmon_bundle_t *b = 0;
402
403   if (pm->is_running)
404     return clib_error_return (0, "please stop first");
405
406   if (unformat_user (input, unformat_line_input, line_input) == 0)
407     return clib_error_return (0, "please specify bundle name");
408
409   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
410     {
411       if (unformat (line_input, "bundle %U", unformat_perfmon_bundle_name, &b))
412         ;
413       else
414         return clib_error_return (0, "unknown input '%U'",
415                                   format_unformat_error, line_input);
416     }
417   unformat_free (line_input);
418
419   if (b == 0)
420     return clib_error_return (0, "please specify bundle name");
421
422   return perfmon_start (vm, b);
423 }
424
425 VLIB_CLI_COMMAND (perfmon_start_command, static) = {
426   .path = "perfmon start",
427   .short_help = "perfmon start bundle [<bundle-name>]",
428   .function = perfmon_start_command_fn,
429   .is_mp_safe = 1,
430 };
431
432 static clib_error_t *
433 perfmon_stop_command_fn (vlib_main_t *vm, unformat_input_t *input,
434                          vlib_cli_command_t *cmd)
435 {
436   return perfmon_stop (vm);
437 }
438
439 VLIB_CLI_COMMAND (perfmon_stop_command, static) = {
440   .path = "perfmon stop",
441   .short_help = "perfmon stop",
442   .function = perfmon_stop_command_fn,
443   .is_mp_safe = 1,
444 };