perfmon: adding support for papi TMAM
[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     /* bundle type will be unknown if no cpu_supports matched */
132     if (vb[i]->type != PERFMON_BUNDLE_TYPE_UNKNOWN)
133       vlib_cli_output (vm, "%U\n", format_perfmon_bundle, vb[i], verbose);
134
135   vec_free (vb);
136   return 0;
137 }
138
139 VLIB_CLI_COMMAND (show_perfmon_bundle_command, static) = {
140   .path = "show perfmon bundle",
141   .short_help = "show perfmon bundle [<bundle-name>] [verbose]",
142   .function = show_perfmon_bundle_command_fn,
143   .is_mp_safe = 1,
144 };
145
146 u8 *
147 format_perfmon_source (u8 *s, va_list *args)
148 {
149   perfmon_source_t *src = va_arg (*args, perfmon_source_t *);
150   int verbose = va_arg (*args, int);
151
152   if (src == 0)
153     return format (s, "%-20s%-9s %s", "Name", "NumEvents", "Description");
154
155   if (verbose)
156     {
157       s = format (s, "name:        %s\n", src->name);
158       s = format (s, "description: %s\n", src->description);
159       s = format (s, "Events:\n");
160       for (int i = 0; i < src->n_events; i++)
161         {
162           perfmon_event_t *e = src->events + i;
163           s = format (s, "  %s", e->name);
164           if (src->format_config)
165             s = format (s, " (%U)\n", src->format_config, e->config);
166           else
167             s = format (s, " (0x%x)\n", e->config);
168           if (e->description)
169             s = format (s, "    %s\n", e->description);
170         }
171
172       if (src->instances_by_type)
173         {
174           s = format (s, "Instances:\n");
175           for (int i = 0; i < vec_len (src->instances_by_type); i++)
176             {
177               perfmon_instance_type_t *it;
178               it = vec_elt_at_index (src->instances_by_type, i);
179               if (vec_len (it->instances) == 0)
180                 continue;
181               s = format (s, "  %s:\n   ", it->name);
182               for (int j = 0; j < vec_len (it->instances); j++)
183                 {
184                   perfmon_instance_t *in = vec_elt_at_index (it->instances, j);
185                   s = format (s, " %s", in->name);
186                 }
187               s = format (s, "\n");
188             }
189         }
190     }
191   else
192     s = format (s, "%-20s%9u %s", src->name, src->n_events, src->description);
193
194   return s;
195 }
196
197 static clib_error_t *
198 show_perfmon_source_command_fn (vlib_main_t *vm, unformat_input_t *input,
199                                 vlib_cli_command_t *cmd)
200 {
201   perfmon_main_t *pm = &perfmon_main;
202   unformat_input_t _line_input, *line_input = &_line_input;
203   perfmon_source_t *s = 0, **vs = 0;
204   int verbose = 0;
205
206   if (unformat_user (input, unformat_line_input, line_input))
207     {
208       while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
209         {
210           if (unformat (line_input, "verbose"))
211             verbose = 1;
212           else if (unformat (line_input, "%U", unformat_perfmon_source_name,
213                              &s))
214             vec_add (vs, &s, 1);
215           else
216             return clib_error_return (0, "unknown input `%U'",
217                                       format_unformat_error, line_input);
218         }
219       unformat_free (line_input);
220     }
221
222   if (vs == 0)
223     {
224       char *key;
225       hash_foreach_mem (key, s, pm->source_by_name, vec_add (vs, &s, 1););
226     }
227   else
228     verbose = 1;
229
230   if (verbose == 0)
231     vlib_cli_output (vm, "%U\n", format_perfmon_source, 0, 0);
232
233   for (int i = 0; i < vec_len (vs); i++)
234     vlib_cli_output (vm, "%U\n", format_perfmon_source, vs[i], verbose);
235
236   vec_free (vs);
237   return 0;
238 }
239
240 VLIB_CLI_COMMAND (show_perfmon_source_command, static) = {
241   .path = "show perfmon source",
242   .short_help = "show perfmon source [<source-name>] [verbose]",
243   .function = show_perfmon_source_command_fn,
244   .is_mp_safe = 1,
245 };
246
247 static clib_error_t *
248 show_perfmon_active_bundle_command_fn (vlib_main_t *vm,
249                                        unformat_input_t *input,
250                                        vlib_cli_command_t *cmd)
251 {
252   perfmon_main_t *pm = &perfmon_main;
253
254   vlib_cli_output (vm, "%U\n", format_perfmon_bundle, pm->active_bundle, 1);
255   return 0;
256 }
257
258 VLIB_CLI_COMMAND (show_perfmon_active_bundle_command, static) = {
259   .path = "show perfmon active-bundle",
260   .short_help = "show perfmon active-bundle",
261   .function = show_perfmon_active_bundle_command_fn,
262   .is_mp_safe = 1,
263 };
264
265 static clib_error_t *
266 show_perfmon_stats_command_fn (vlib_main_t *vm, unformat_input_t *input,
267                                vlib_cli_command_t *cmd)
268 {
269   perfmon_main_t *pm = &perfmon_main;
270   perfmon_bundle_t *b = pm->active_bundle;
271   clib_error_t *err = 0;
272   table_t table = {}, *t = &table;
273   u32 n_instances;
274   perfmon_reading_t *r, *readings = 0;
275   perfmon_instance_type_t *it = pm->active_instance_type;
276   perfmon_instance_t *in;
277   u8 *s = 0;
278   int n_row = 0;
279
280   if (b == 0)
281     return clib_error_return (0, "no bundle selected");
282
283   n_instances = vec_len (it->instances);
284   vec_validate (readings, n_instances - 1);
285
286   /*Only perform read() for THREAD or SYSTEM bundles*/
287   for (int i = 0; i < n_instances && b->type != PERFMON_BUNDLE_TYPE_NODE; i++)
288     {
289       in = vec_elt_at_index (it->instances, i);
290       r = vec_elt_at_index (readings, i);
291
292       if (read (pm->group_fds[i], r, (b->n_events + 3) * sizeof (u64)) == -1)
293         {
294           err = clib_error_return_unix (0, "read");
295           goto done;
296         }
297     }
298
299   table_format_title (t, "%s", b->description);
300
301   table_add_header_col (t, 0);
302   table_add_header_row (t, 0);
303
304   if (b->column_headers)
305     {
306       char **hdr = b->column_headers;
307       while (hdr[0])
308         table_format_cell (t, -1, n_row++, "%s", hdr++[0]);
309     }
310
311   int col = 0;
312   for (int i = 0; i < n_instances; i++)
313     {
314       in = vec_elt_at_index (it->instances, i);
315       r = vec_elt_at_index (readings, i);
316       table_format_cell (t, col, -1, "%s", in->name, b->type);
317       if (b->type == PERFMON_BUNDLE_TYPE_NODE)
318         {
319           perfmon_thread_runtime_t *tr;
320           tr = vec_elt_at_index (pm->thread_runtimes, i);
321           for (int j = 0; j < tr->n_nodes; j++)
322             if (tr->node_stats[j].n_calls)
323               {
324                 perfmon_node_stats_t ns;
325                 table_format_cell (t, ++col, -1, "%U", format_vlib_node_name,
326                                    vm, j, b->type);
327                 table_set_cell_align (t, col, -1, TTAA_RIGHT);
328                 table_set_cell_fg_color (t, col, -1, TTAC_CYAN);
329                 clib_memcpy_fast (&ns, tr->node_stats + j, sizeof (ns));
330
331                 for (int j = 0; j < n_row; j++)
332                   table_format_cell (t, col, j, "%U", b->format_fn, &ns, j,
333                                      b->type);
334               }
335         }
336       else
337         {
338           for (int j = 0; j < n_row; j++)
339             table_format_cell (t, i, j, "%U", b->format_fn, r, j, b->type);
340         }
341       col++;
342     }
343
344   vlib_cli_output (vm, "%U\n", format_table, t);
345   table_free (t);
346
347   if (b->footer)
348     vlib_cli_output (vm, "\n%s\n", b->footer);
349
350 done:
351   vec_free (readings);
352   vec_free (s);
353   return err;
354 }
355
356 VLIB_CLI_COMMAND (show_perfmon_stats_command, static) = {
357   .path = "show perfmon statistics",
358   .short_help = "show perfmon statistics [raw]",
359   .function = show_perfmon_stats_command_fn,
360   .is_mp_safe = 1,
361 };
362
363 static clib_error_t *
364 perfmon_reset_command_fn (vlib_main_t *vm, unformat_input_t *input,
365                           vlib_cli_command_t *cmd)
366 {
367   perfmon_reset (vm);
368   return 0;
369 }
370
371 VLIB_CLI_COMMAND (perfmon_reset_command, static) = {
372   .path = "perfmon reset",
373   .short_help = "perfmon reset",
374   .function = perfmon_reset_command_fn,
375   .is_mp_safe = 1,
376 };
377
378 static clib_error_t *
379 perfmon_start_command_fn (vlib_main_t *vm, unformat_input_t *input,
380                           vlib_cli_command_t *cmd)
381 {
382   perfmon_main_t *pm = &perfmon_main;
383   unformat_input_t _line_input, *line_input = &_line_input;
384   perfmon_bundle_t *b = 0;
385
386   if (pm->is_running)
387     return clib_error_return (0, "please stop first");
388
389   if (unformat_user (input, unformat_line_input, line_input) == 0)
390     return clib_error_return (0, "please specify bundle name");
391
392   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
393     {
394       if (unformat (line_input, "bundle %U", unformat_perfmon_bundle_name, &b))
395         ;
396       else
397         return clib_error_return (0, "unknown input '%U'",
398                                   format_unformat_error, line_input);
399     }
400   unformat_free (line_input);
401
402   if (b == 0)
403     return clib_error_return (0, "please specify bundle name");
404
405   return perfmon_start (vm, b);
406 }
407
408 VLIB_CLI_COMMAND (perfmon_start_command, static) = {
409   .path = "perfmon start",
410   .short_help = "perfmon start bundle [<bundle-name>]",
411   .function = perfmon_start_command_fn,
412   .is_mp_safe = 1,
413 };
414
415 static clib_error_t *
416 perfmon_stop_command_fn (vlib_main_t *vm, unformat_input_t *input,
417                          vlib_cli_command_t *cmd)
418 {
419   return perfmon_stop (vm);
420 }
421
422 VLIB_CLI_COMMAND (perfmon_stop_command, static) = {
423   .path = "perfmon stop",
424   .short_help = "perfmon stop",
425   .function = perfmon_stop_command_fn,
426   .is_mp_safe = 1,
427 };