c6a80224e0efa324fb26bd2f3ca2caae9917e992
[vpp.git] / src / plugins / perfmon / perfmon.c
1 /*
2  * perfmon.c - skeleton vpp engine plug-in
3  *
4  * Copyright (c) <current-year> <your-organization>
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
20 #include <perfmon/perfmon.h>
21
22 #include <vlibapi/api.h>
23 #include <vlibmemory/api.h>
24 #include <vpp/app/version.h>
25 #include <linux/limits.h>
26
27 perfmon_main_t perfmon_main;
28
29 static char *perfmon_json_path = "/usr/share/vpp/plugins/perfmon";
30
31 typedef struct
32 {
33   u8 model;
34   u8 stepping;
35   u8 has_stepping;
36   char *filename;
37 } file_by_model_and_stepping_t;
38
39 /* Created by parsing mapfile.csv, see mapfile_tool.c */
40
41 static const file_by_model_and_stepping_t fms_table[] = {
42   /* model, stepping, stepping valid, file */
43   {0x2E, 0x0, 0, "NehalemEX_core_V2.json"},
44   {0x1E, 0x0, 0, "NehalemEP_core_V2.json"},
45   {0x1F, 0x0, 0, "NehalemEP_core_V2.json"},
46   {0x1A, 0x0, 0, "NehalemEP_core_V2.json"},
47   {0x2F, 0x0, 0, "WestmereEX_core_V2.json"},
48   {0x25, 0x0, 0, "WestmereEP-SP_core_V2.json"},
49   {0x2C, 0x0, 0, "WestmereEP-DP_core_V2.json"},
50   {0x37, 0x0, 0, "Silvermont_core_V14.json"},
51   {0x4D, 0x0, 0, "Silvermont_core_V14.json"},
52   {0x4C, 0x0, 0, "Silvermont_core_V14.json"},
53   {0x5C, 0x0, 0, "goldmont_core_v13.json"},
54   {0x5F, 0x0, 0, "goldmont_core_v13.json"},
55   {0x1C, 0x0, 0, "Bonnell_core_V4.json"},
56   {0x26, 0x0, 0, "Bonnell_core_V4.json"},
57   {0x27, 0x0, 0, "Bonnell_core_V4.json"},
58   {0x36, 0x0, 0, "Bonnell_core_V4.json"},
59   {0x35, 0x0, 0, "Bonnell_core_V4.json"},
60   {0x2A, 0x0, 0, "sandybridge_core_v16.json"},
61   {0x2D, 0x0, 0, "Jaketown_core_V20.json"},
62   {0x3A, 0x0, 0, "ivybridge_core_v21.json"},
63   {0x3E, 0x0, 0, "ivytown_core_v20.json"},
64   {0x3C, 0x0, 0, "haswell_core_v28.json"},
65   {0x45, 0x0, 0, "haswell_core_v28.json"},
66   {0x46, 0x0, 0, "haswell_core_v28.json"},
67   {0x3F, 0x0, 0, "haswellx_core_v20.json"},
68   {0x3D, 0x0, 0, "broadwell_core_v23.json"},
69   {0x47, 0x0, 0, "broadwell_core_v23.json"},
70   {0x4F, 0x0, 0, "broadwellx_core_v14.json"},
71   {0x56, 0x0, 0, "broadwellde_core_v7.json"},
72   {0x4E, 0x0, 0, "skylake_core_v42.json"},
73   {0x5E, 0x0, 0, "skylake_core_v42.json"},
74   {0x8E, 0x0, 0, "skylake_core_v42.json"},
75   {0x9E, 0x0, 0, "skylake_core_v42.json"},
76   {0x57, 0x0, 0, "KnightsLanding_core_V9.json"},
77   {0x85, 0x0, 0, "KnightsLanding_core_V9.json"},
78   {0x55, 0x0, 1, "skylakex_core_v1.12.json"},
79   {0x55, 0x1, 1, "skylakex_core_v1.12.json"},
80   {0x55, 0x2, 1, "skylakex_core_v1.12.json"},
81   {0x55, 0x3, 1, "skylakex_core_v1.12.json"},
82   {0x55, 0x4, 1, "skylakex_core_v1.12.json"},
83   {0x55, 0x5, 1, "cascadelakex_core_v1.00.json"},
84   {0x55, 0x6, 1, "cascadelakex_core_v1.00.json"},
85   {0x55, 0x7, 1, "cascadelakex_core_v1.00.json"},
86   {0x55, 0x8, 1, "cascadelakex_core_v1.00.json"},
87   {0x55, 0x9, 1, "cascadelakex_core_v1.00.json"},
88   {0x55, 0xA, 1, "cascadelakex_core_v1.00.json"},
89   {0x55, 0xB, 1, "cascadelakex_core_v1.00.json"},
90   {0x55, 0xC, 1, "cascadelakex_core_v1.00.json"},
91   {0x55, 0xD, 1, "cascadelakex_core_v1.00.json"},
92   {0x55, 0xE, 1, "cascadelakex_core_v1.00.json"},
93   {0x55, 0xF, 1, "cascadelakex_core_v1.00.json"},
94   {0x7A, 0x0, 0, "goldmontplus_core_v1.01.json"},
95 };
96
97 static void
98 set_perfmon_json_path ()
99 {
100   char *p, path[PATH_MAX];
101   int rv;
102   u8 *s;
103
104   /* find executable path */
105   if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1)
106     return;
107
108   /* readlink doesn't provide null termination */
109   path[rv] = 0;
110
111   /* strip filename */
112   if ((p = strrchr (path, '/')) == 0)
113     return;
114   *p = 0;
115
116   /* strip bin/ */
117   if ((p = strrchr (path, '/')) == 0)
118     return;
119   *p = 0;
120
121   /* cons up the .json file path */
122   s = format (0, "%s/share/vpp/plugins/perfmon", path);
123   vec_add1 (s, 0);
124   perfmon_json_path = (char *) s;
125 }
126
127 static inline u32
128 get_cpuid (void)
129 {
130 #if defined(__x86_64__)
131   u32 cpuid;
132   asm volatile ("mov $1, %%eax; cpuid; mov %%eax, %0":"=r" (cpuid)::"%eax",
133                 "%edx", "%ecx", "%rbx");
134   return cpuid;
135 #else
136   return 0;
137 #endif
138 }
139
140 static clib_error_t *
141 perfmon_init (vlib_main_t * vm)
142 {
143   perfmon_main_t *pm = &perfmon_main;
144   clib_error_t *error = 0;
145   u32 cpuid;
146   uword *ht;
147   int found_a_table = 0;
148   int i;
149   u8 model, stepping;
150
151   pm->vlib_main = vm;
152   pm->vnet_main = vnet_get_main ();
153
154   pm->capture_by_thread_and_node_name =
155     hash_create_string (0, sizeof (uword));
156
157   pm->log_class = vlib_log_register_class ("perfmon", 0);
158
159   /* Default data collection interval */
160   pm->timeout_interval = 3.0;
161   vec_validate (pm->pm_fds, vec_len (vlib_mains) - 1);
162   vec_validate (pm->perf_event_pages, vec_len (vlib_mains) - 1);
163   vec_validate (pm->rdpmc_indices, vec_len (vlib_mains) - 1);
164   pm->page_size = getpagesize ();
165
166   ht = pm->perfmon_table = 0;
167
168   set_perfmon_json_path ();
169
170   cpuid = get_cpuid ();
171
172   for (i = 0; i < ARRAY_LEN (fms_table); i++)
173     {
174       model = ((cpuid >> 12) & 0xf0) | ((cpuid >> 4) & 0xf);
175       stepping = cpuid & 0xf;
176
177       if (fms_table[i].model != model)
178         continue;
179
180       if (fms_table[i].has_stepping)
181         {
182           if (fms_table[i].stepping != stepping)
183             continue;
184         }
185
186       found_a_table = 1;
187       ht = perfmon_parse_table (pm, perfmon_json_path, fms_table[i].filename);
188       break;
189     }
190   pm->perfmon_table = ht;
191
192   if (found_a_table == 0 || pm->perfmon_table == 0 || hash_elts (ht) == 0)
193     {
194       vlib_log_err (pm->log_class, "No table for cpuid %x", cpuid);
195       vlib_log_err (pm->log_class, "  model %x, stepping %x",
196                     model, stepping);
197     }
198
199   return error;
200 }
201
202 VLIB_INIT_FUNCTION (perfmon_init);
203
204 /* *INDENT-OFF* */
205 VLIB_PLUGIN_REGISTER () =
206 {
207   .version = VPP_BUILD_VER,
208   .description = "Performance monitor plugin",
209 #if !defined(__x86_64__)
210   .default_disabled = 1,
211 #endif
212 };
213 /* *INDENT-ON* */
214
215 static uword
216 atox (u8 * s)
217 {
218   uword rv = 0;
219
220   while (*s)
221     {
222       if (*s >= '0' && *s <= '9')
223         rv = (rv << 4) | (*s - '0');
224       else if (*s >= 'a' && *s <= 'f')
225         rv = (rv << 4) | (*s - 'a' + 10);
226       else if (*s >= 'A' && *s <= 'A')
227         rv = (rv << 4) | (*s - 'A' + 10);
228       else if (*s == 'x')
229         ;
230       else
231         break;
232       s++;
233     }
234   return rv;
235 }
236
237 static uword
238 unformat_processor_event (unformat_input_t * input, va_list * args)
239 {
240   perfmon_main_t *pm = va_arg (*args, perfmon_main_t *);
241   perfmon_event_config_t *ep = va_arg (*args, perfmon_event_config_t *);
242   u8 *s = 0;
243   name_value_pair_t **nvps, *nvp;
244   hash_pair_t *hp;
245   int i;
246   int set_values = 0;
247   u32 pe_config = 0;
248
249   if (pm->perfmon_table == 0)
250     return 0;
251
252   if (!unformat (input, "%s", &s))
253     return 0;
254
255   hp = hash_get_pair_mem (pm->perfmon_table, s);
256
257   vec_free (s);
258
259   if (hp == 0)
260     return 0;
261
262   nvps = (name_value_pair_t **) (hp->value[0]);
263
264   for (i = 0; i < vec_len (nvps); i++)
265     {
266       nvp = nvps[i];
267       if (!strncmp ((char *) nvp->name, "EventCode", 9))
268         {
269           pe_config |= atox (nvp->value);
270           set_values++;
271         }
272       else if (!strncmp ((char *) nvp->name, "UMask", 5))
273         {
274           pe_config |= (atox (nvp->value) << 8);
275           set_values++;
276         }
277       if (set_values == 2)
278         break;
279     }
280
281   if (set_values != 2)
282     {
283       clib_warning ("BUG: only found %d values", set_values);
284       return 0;
285     }
286
287   ep->name = (char *) hp->key;
288   ep->pe_type = PERF_TYPE_RAW;
289   ep->pe_config = pe_config;
290   return 1;
291 }
292
293 static clib_error_t *
294 set_pmc_command_fn (vlib_main_t * vm,
295                     unformat_input_t * input, vlib_cli_command_t * cmd)
296 {
297   perfmon_main_t *pm = &perfmon_main;
298   unformat_input_t _line_input, *line_input = &_line_input;
299   perfmon_event_config_t ec;
300   u32 timeout_seconds;
301   u32 deadman;
302
303   vec_reset_length (pm->events_to_collect);
304   pm->ipc_event_index = ~0;
305   pm->mispredict_event_index = ~0;
306
307   if (!unformat_user (input, unformat_line_input, line_input))
308     return clib_error_return (0, "counter names required...");
309
310   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
311     {
312       if (unformat (line_input, "timeout %u", &timeout_seconds))
313         pm->timeout_interval = (f64) timeout_seconds;
314       else if (unformat (line_input, "instructions-per-clock"))
315         {
316           ec.name = "instructions";
317           ec.pe_type = PERF_TYPE_HARDWARE;
318           ec.pe_config = PERF_COUNT_HW_INSTRUCTIONS;
319           pm->ipc_event_index = vec_len (pm->events_to_collect);
320           vec_add1 (pm->events_to_collect, ec);
321           ec.name = "cpu-cycles";
322           ec.pe_type = PERF_TYPE_HARDWARE;
323           ec.pe_config = PERF_COUNT_HW_CPU_CYCLES;
324           vec_add1 (pm->events_to_collect, ec);
325         }
326       else if (unformat (line_input, "branch-mispredict-rate"))
327         {
328           ec.name = "branch-misses";
329           ec.pe_type = PERF_TYPE_HARDWARE;
330           ec.pe_config = PERF_COUNT_HW_BRANCH_MISSES;
331           pm->mispredict_event_index = vec_len (pm->events_to_collect);
332           vec_add1 (pm->events_to_collect, ec);
333           ec.name = "branches";
334           ec.pe_type = PERF_TYPE_HARDWARE;
335           ec.pe_config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
336           vec_add1 (pm->events_to_collect, ec);
337         }
338       else if (unformat (line_input, "%U", unformat_processor_event, pm, &ec))
339         {
340           vec_add1 (pm->events_to_collect, ec);
341         }
342 #define _(type,event,str)                       \
343       else if (unformat (line_input, str))      \
344         {                                       \
345           ec.name = str;                        \
346           ec.pe_type = type;                    \
347           ec.pe_config = event;                 \
348           vec_add1 (pm->events_to_collect, ec); \
349         }
350       foreach_perfmon_event
351 #undef _
352         else
353         return clib_error_return (0, "unknown input '%U'",
354                                   format_unformat_error, line_input);
355     }
356
357   if (vec_len (pm->events_to_collect) == 0)
358     return clib_error_return (0, "no events specified...");
359
360   vlib_cli_output (vm, "Start collection for %d events, wait %.2f seconds",
361                    vec_len (pm->events_to_collect),
362                    (f64) (vec_len (pm->events_to_collect))
363                    * pm->timeout_interval);
364
365   vlib_process_signal_event (pm->vlib_main, perfmon_periodic_node.index,
366                              PERFMON_START, 0);
367
368   /* Coarse-grained wait */
369   vlib_process_suspend (vm,
370                         ((f64) (vec_len (pm->events_to_collect)
371                                 * pm->timeout_interval)));
372
373   deadman = 0;
374   /* Reasonable to guess that collection may not be quite done... */
375   while (pm->state == PERFMON_STATE_RUNNING)
376     {
377       vlib_process_suspend (vm, 10e-3);
378       if (deadman++ > 200)
379         {
380           vlib_cli_output (vm, "DEADMAN: collection still running...");
381           break;
382         }
383     }
384
385   vlib_cli_output (vm, "Data collection complete...");
386   return 0;
387 }
388
389 /* *INDENT-OFF* */
390 VLIB_CLI_COMMAND (set_pmc_command, static) =
391 {
392   .path = "set pmc",
393   .short_help = "set pmc c1 [..., use \"show pmc events\"]",
394   .function = set_pmc_command_fn,
395   .is_mp_safe = 1,
396 };
397 /* *INDENT-ON* */
398
399 static int
400 capture_name_sort (void *a1, void *a2)
401 {
402   perfmon_capture_t *c1 = a1;
403   perfmon_capture_t *c2 = a2;
404
405   return strcmp ((char *) c1->thread_and_node_name,
406                  (char *) c2->thread_and_node_name);
407 }
408
409 static u8 *
410 format_capture (u8 * s, va_list * args)
411 {
412   perfmon_main_t *pm = va_arg (*args, perfmon_main_t *);
413   perfmon_capture_t *c = va_arg (*args, perfmon_capture_t *);
414   int verbose __attribute__ ((unused)) = va_arg (*args, int);
415   f64 ticks_per_pkt;
416   int i;
417
418   if (c == 0)
419     {
420       s = format (s, "%=40s%=20s%=16s%=16s%=16s",
421                   "Name", "Counter", "Count", "Pkts", "Counts/Pkt");
422       return s;
423     }
424
425   for (i = 0; i < vec_len (c->counter_names); i++)
426     {
427       u8 *name;
428
429       if (i == 0)
430         name = c->thread_and_node_name;
431       else
432         {
433           vec_add1 (s, '\n');
434           name = (u8 *) "";
435         }
436
437       /* Deal with synthetic events right here */
438       if (i == pm->ipc_event_index)
439         {
440           f64 ipc_rate;
441           ASSERT (i + 1 < vec_len (c->counter_names));
442
443           if (c->counter_values[i + 1] > 0)
444             ipc_rate = (f64) c->counter_values[i]
445               / (f64) c->counter_values[i + 1];
446           else
447             ipc_rate = 0.0;
448
449           s = format (s, "%-40s%+20s%+16llu%+16llu%+16.2e\n",
450                       name, "instructions-per-clock",
451                       c->counter_values[i],
452                       c->counter_values[i + 1], ipc_rate);
453           name = (u8 *) "";
454         }
455
456       if (i == pm->mispredict_event_index)
457         {
458           f64 mispredict_rate;
459           ASSERT (i + 1 < vec_len (c->counter_names));
460
461           if (c->counter_values[i + 1] > 0)
462             mispredict_rate = (f64) c->counter_values[i]
463               / (f64) c->counter_values[i + 1];
464           else
465             mispredict_rate = 0.0;
466
467           s = format (s, "%-40s%+20s%+16llu%+16llu%+16.2e\n",
468                       name, "branch-mispredict-rate",
469                       c->counter_values[i],
470                       c->counter_values[i + 1], mispredict_rate);
471           name = (u8 *) "";
472         }
473
474       if (c->vectors_this_counter[i])
475         ticks_per_pkt =
476           ((f64) c->counter_values[i]) / ((f64) c->vectors_this_counter[i]);
477       else
478         ticks_per_pkt = 0.0;
479
480       s = format (s, "%-40s%+20s%+16llu%+16llu%+16.2e",
481                   name, c->counter_names[i],
482                   c->counter_values[i],
483                   c->vectors_this_counter[i], ticks_per_pkt);
484     }
485   return s;
486 }
487
488 static u8 *
489 format_generic_events (u8 * s, va_list * args)
490 {
491   int verbose = va_arg (*args, int);
492
493 #define _(type,config,name)                             \
494   if (verbose == 0)                                     \
495     s = format (s, "\n  %s", name);                     \
496   else                                                  \
497     s = format (s, "\n  %s (%d, %d)", name, type, config);
498   foreach_perfmon_event;
499 #undef _
500   return s;
501 }
502
503 typedef struct
504 {
505   u8 *name;
506   name_value_pair_t **nvps;
507 } sort_nvp_t;
508
509 static int
510 sort_nvps_by_name (void *a1, void *a2)
511 {
512   sort_nvp_t *nvp1 = a1;
513   sort_nvp_t *nvp2 = a2;
514
515   return strcmp ((char *) nvp1->name, (char *) nvp2->name);
516 }
517
518 static u8 *
519 format_processor_events (u8 * s, va_list * args)
520 {
521   perfmon_main_t *pm = va_arg (*args, perfmon_main_t *);
522   int verbose = va_arg (*args, int);
523   int i, j;
524   sort_nvp_t *sort_nvps = 0;
525   sort_nvp_t *sn;
526   u8 *key;
527   name_value_pair_t **value;
528
529   /* *INDENT-OFF* */
530   hash_foreach_mem (key, value, pm->perfmon_table,
531   ({
532     vec_add2 (sort_nvps, sn, 1);
533     sn->name = key;
534     sn->nvps = value;
535   }));
536
537   vec_sort_with_function (sort_nvps, sort_nvps_by_name);
538
539   if (verbose == 0)
540     {
541       for (i = 0; i < vec_len (sort_nvps); i++)
542         s = format (s, "\n  %s ", sort_nvps[i].name);
543     }
544   else
545     {
546       for (i = 0; i < vec_len (sort_nvps); i++)
547         {
548           name_value_pair_t **nvps;
549           s = format (s, "\n  %s:", sort_nvps[i].name);
550
551           nvps = sort_nvps[i].nvps;
552
553           for (j = 0; j < vec_len (nvps); j++)
554             s = format (s, "\n    %s = %s", nvps[j]->name, nvps[j]->value);
555         }
556     }
557   vec_free (sort_nvps);
558   return s;
559 }
560
561
562 static clib_error_t *
563 show_pmc_command_fn (vlib_main_t * vm,
564                      unformat_input_t * input, vlib_cli_command_t * cmd)
565 {
566   perfmon_main_t *pm = &perfmon_main;
567   int verbose = 0;
568   int events = 0;
569   int i;
570   perfmon_capture_t *c;
571   perfmon_capture_t *captures = 0;
572
573   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
574     {
575       if (unformat (input, "events"))
576         events = 1;
577       else if (unformat (input, "verbose"))
578         verbose = 1;
579       else
580         break;
581     }
582
583   if (events)
584     {
585       vlib_cli_output (vm, "Generic Events %U",
586                        format_generic_events, verbose);
587       vlib_cli_output (vm, "Synthetic Events");
588       vlib_cli_output (vm, "  instructions-per-clock");
589       vlib_cli_output (vm, "  branch-mispredict-rate");
590       if (pm->perfmon_table)
591         vlib_cli_output (vm, "Processor Events %U",
592                          format_processor_events, pm, verbose);
593       return 0;
594     }
595
596   if (pm->state == PERFMON_STATE_RUNNING)
597     {
598       vlib_cli_output (vm, "Data collection in progress...");
599       return 0;
600     }
601
602   if (pool_elts (pm->capture_pool) == 0)
603     {
604       vlib_cli_output (vm, "No data...");
605       return 0;
606     }
607
608   /* *INDENT-OFF* */
609   pool_foreach (c, pm->capture_pool,
610   ({
611     vec_add1 (captures, *c);
612   }));
613   /* *INDENT-ON* */
614
615   vec_sort_with_function (captures, capture_name_sort);
616
617   vlib_cli_output (vm, "%U", format_capture, pm, 0 /* header */ ,
618                    0 /* verbose */ );
619
620   for (i = 0; i < vec_len (captures); i++)
621     {
622       c = captures + i;
623
624       vlib_cli_output (vm, "%U", format_capture, pm, c, verbose);
625     }
626
627   vec_free (captures);
628
629   return 0;
630 }
631
632 /* *INDENT-OFF* */
633 VLIB_CLI_COMMAND (show_pmc_command, static) =
634 {
635   .path = "show pmc",
636   .short_help = "show pmc [verbose]",
637   .function = show_pmc_command_fn,
638   .is_mp_safe = 1,
639 };
640 /* *INDENT-ON* */
641
642 static clib_error_t *
643 clear_pmc_command_fn (vlib_main_t * vm,
644                       unformat_input_t * input, vlib_cli_command_t * cmd)
645 {
646   perfmon_main_t *pm = &perfmon_main;
647   u8 *key;
648   u32 *value;
649
650   if (pm->state == PERFMON_STATE_RUNNING)
651     {
652       vlib_cli_output (vm, "Performance monitor is still running...");
653       return 0;
654     }
655
656   pool_free (pm->capture_pool);
657
658   /* *INDENT-OFF* */
659   hash_foreach_mem (key, value, pm->capture_by_thread_and_node_name,
660   ({
661     vec_free (key);
662   }));
663   /* *INDENT-ON* */
664   hash_free (pm->capture_by_thread_and_node_name);
665   pm->capture_by_thread_and_node_name =
666     hash_create_string (0, sizeof (uword));
667   return 0;
668 }
669
670 /* *INDENT-OFF* */
671 VLIB_CLI_COMMAND (clear_pmc_command, static) =
672 {
673   .path = "clear pmc",
674   .short_help = "clear the performance monitor counters",
675   .function = clear_pmc_command_fn,
676 };
677 /* *INDENT-ON* */
678
679
680 /*
681  * fd.io coding-style-patch-verification: ON
682  *
683  * Local Variables:
684  * eval: (c-set-style "gnu")
685  * End:
686  */