nsim: add packet loss simulation, docs
[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 static void
32 set_perfmon_json_path ()
33 {
34   char *p, path[PATH_MAX];
35   int rv;
36   u8 *s;
37
38   /* find executable path */
39   if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1)
40     return;
41
42   /* readlink doesn't provide null termination */
43   path[rv] = 0;
44
45   /* strip filename */
46   if ((p = strrchr (path, '/')) == 0)
47     return;
48   *p = 0;
49
50   /* strip bin/ */
51   if ((p = strrchr (path, '/')) == 0)
52     return;
53   *p = 0;
54
55   /* cons up the .json file path */
56   s = format (0, "%s/share/vpp/plugins/perfmon", path);
57   vec_add1 (s, 0);
58   perfmon_json_path = (char *) s;
59 }
60
61 #define foreach_cpuid_table                     \
62 _(0x0106E5, NehalemEP_core_V2.json)     /* Intel(R) Xeon(R) CPU X3430  @ 2.40GHz     */        \
63 _(0x0306C3, haswell_core_v28.json)      /* Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz   */        \
64 _(0x0306F2, haswell_core_v28.json)      /* Intel(R) Xeon(R) CPU E5-2640 v3 @ 2.60GHz */        \
65 _(0x040661, haswell_core_v28.json)      /* Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz */        \
66 _(0x0406D8, Silvermont_core_V14.json)   /* Intel(R) Atom(TM) CPU  C2758  @ 2.40GHz   */        \
67 _(0x0406E3, skylake_core_v42.json)      /* Intel(R) Core(TM) i7-6500U CPU @ 2.50GHz  */        \
68 _(0x0506E3, skylake_core_v42.json)      /* Intel(R) Core(TM) i5-6600 CPU @ 3.30GHz   */
69
70 static inline u32
71 get_cpuid (void)
72 {
73 #if defined(__x86_64__)
74   u32 cpuid;
75   asm volatile ("mov $1, %%eax; cpuid; mov %%eax, %0":"=r" (cpuid)::"%eax",
76                 "%edx", "%ecx", "%rbx");
77   return cpuid;
78 #else
79   return 0;
80 #endif
81 }
82
83 static clib_error_t *
84 perfmon_init (vlib_main_t * vm)
85 {
86   perfmon_main_t *pm = &perfmon_main;
87   clib_error_t *error = 0;
88   u32 cpuid;
89   uword *ht;
90   int found_a_table = 0;
91
92   pm->vlib_main = vm;
93   pm->vnet_main = vnet_get_main ();
94
95   pm->capture_by_thread_and_node_name =
96     hash_create_string (0, sizeof (uword));
97
98   pm->log_class = vlib_log_register_class ("perfmon", 0);
99
100   /* Default data collection interval */
101   pm->timeout_interval = 3.0;
102   vec_validate (pm->pm_fds, vec_len (vlib_mains) - 1);
103   vec_validate (pm->perf_event_pages, vec_len (vlib_mains) - 1);
104   vec_validate (pm->rdpmc_indices, vec_len (vlib_mains) - 1);
105   pm->page_size = getpagesize ();
106
107   ht = pm->perfmon_table = 0;
108
109   set_perfmon_json_path ();
110
111   cpuid = get_cpuid ();
112
113   if (0)
114     {
115     }
116 #define _(id,table)                                             \
117   else if (cpuid == id)                                         \
118     {                                                           \
119       vlib_log_debug (pm->log_class, "Found table %s", #table); \
120       ht = perfmon_parse_table (pm, perfmon_json_path, #table); \
121       found_a_table = 1;                                        \
122     }
123   foreach_cpuid_table;
124 #undef _
125
126   pm->perfmon_table = ht;
127
128   if (found_a_table == 0)
129     vlib_log_err (pm->log_class, "No table for cpuid %x", cpuid);
130
131   return error;
132 }
133
134 VLIB_INIT_FUNCTION (perfmon_init);
135
136 /* *INDENT-OFF* */
137 VLIB_PLUGIN_REGISTER () =
138 {
139   .version = VPP_BUILD_VER,
140   .description = "Performance monitor plugin",
141 #if !defined(__x86_64__)
142   .default_disabled = 1,
143 #endif
144 };
145 /* *INDENT-ON* */
146
147 static uword
148 atox (u8 * s)
149 {
150   uword rv = 0;
151
152   while (*s)
153     {
154       if (*s >= '0' && *s <= '9')
155         rv = (rv << 4) | (*s - '0');
156       else if (*s >= 'a' && *s <= 'f')
157         rv = (rv << 4) | (*s - 'a' + 10);
158       else if (*s >= 'A' && *s <= 'A')
159         rv = (rv << 4) | (*s - 'A' + 10);
160       else if (*s == 'x')
161         ;
162       else
163         break;
164       s++;
165     }
166   return rv;
167 }
168
169 static uword
170 unformat_processor_event (unformat_input_t * input, va_list * args)
171 {
172   perfmon_main_t *pm = va_arg (*args, perfmon_main_t *);
173   perfmon_event_config_t *ep = va_arg (*args, perfmon_event_config_t *);
174   u8 *s = 0;
175   name_value_pair_t **nvps, *nvp;
176   hash_pair_t *hp;
177   int i;
178   int set_values = 0;
179   u32 pe_config = 0;
180
181   if (pm->perfmon_table == 0)
182     return 0;
183
184   if (!unformat (input, "%s", &s))
185     return 0;
186
187   hp = hash_get_pair_mem (pm->perfmon_table, s);
188
189   vec_free (s);
190
191   if (hp == 0)
192     return 0;
193
194   nvps = (name_value_pair_t **) (hp->value[0]);
195
196   for (i = 0; i < vec_len (nvps); i++)
197     {
198       nvp = nvps[i];
199       if (!strncmp ((char *) nvp->name, "EventCode", 9))
200         {
201           pe_config |= atox (nvp->value);
202           set_values++;
203         }
204       else if (!strncmp ((char *) nvp->name, "UMask", 5))
205         {
206           pe_config |= (atox (nvp->value) << 8);
207           set_values++;
208         }
209       if (set_values == 2)
210         break;
211     }
212
213   if (set_values != 2)
214     {
215       clib_warning ("BUG: only found %d values", set_values);
216       return 0;
217     }
218
219   ep->name = (char *) hp->key;
220   ep->pe_type = PERF_TYPE_RAW;
221   ep->pe_config = pe_config;
222   return 1;
223 }
224
225 static clib_error_t *
226 set_pmc_command_fn (vlib_main_t * vm,
227                     unformat_input_t * input, vlib_cli_command_t * cmd)
228 {
229   perfmon_main_t *pm = &perfmon_main;
230   unformat_input_t _line_input, *line_input = &_line_input;
231   perfmon_event_config_t ec;
232   u32 timeout_seconds;
233   u32 deadman;
234
235   vec_reset_length (pm->events_to_collect);
236   pm->ipc_event_index = ~0;
237   pm->mispredict_event_index = ~0;
238
239   if (!unformat_user (input, unformat_line_input, line_input))
240     return clib_error_return (0, "counter names required...");
241
242   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
243     {
244       if (unformat (line_input, "timeout %u", &timeout_seconds))
245         pm->timeout_interval = (f64) timeout_seconds;
246       else if (unformat (line_input, "instructions-per-clock"))
247         {
248           ec.name = "instructions";
249           ec.pe_type = PERF_TYPE_HARDWARE;
250           ec.pe_config = PERF_COUNT_HW_INSTRUCTIONS;
251           pm->ipc_event_index = vec_len (pm->events_to_collect);
252           vec_add1 (pm->events_to_collect, ec);
253           ec.name = "cpu-cycles";
254           ec.pe_type = PERF_TYPE_HARDWARE;
255           ec.pe_config = PERF_COUNT_HW_CPU_CYCLES;
256           vec_add1 (pm->events_to_collect, ec);
257         }
258       else if (unformat (line_input, "branch-mispredict-rate"))
259         {
260           ec.name = "branch-misses";
261           ec.pe_type = PERF_TYPE_HARDWARE;
262           ec.pe_config = PERF_COUNT_HW_BRANCH_MISSES;
263           pm->mispredict_event_index = vec_len (pm->events_to_collect);
264           vec_add1 (pm->events_to_collect, ec);
265           ec.name = "branches";
266           ec.pe_type = PERF_TYPE_HARDWARE;
267           ec.pe_config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
268           vec_add1 (pm->events_to_collect, ec);
269         }
270       else if (unformat (line_input, "%U", unformat_processor_event, pm, &ec))
271         {
272           vec_add1 (pm->events_to_collect, ec);
273         }
274 #define _(type,event,str)                       \
275       else if (unformat (line_input, str))      \
276         {                                       \
277           ec.name = str;                        \
278           ec.pe_type = type;                    \
279           ec.pe_config = event;                 \
280           vec_add1 (pm->events_to_collect, ec); \
281         }
282       foreach_perfmon_event
283 #undef _
284         else
285         return clib_error_return (0, "unknown input '%U'",
286                                   format_unformat_error, line_input);
287     }
288
289   if (vec_len (pm->events_to_collect) == 0)
290     return clib_error_return (0, "no events specified...");
291
292   vlib_cli_output (vm, "Start collection for %d events, wait %.2f seconds",
293                    vec_len (pm->events_to_collect),
294                    (f64) (vec_len (pm->events_to_collect))
295                    * pm->timeout_interval);
296
297   vlib_process_signal_event (pm->vlib_main, perfmon_periodic_node.index,
298                              PERFMON_START, 0);
299
300   /* Coarse-grained wait */
301   vlib_process_suspend (vm,
302                         ((f64) (vec_len (pm->events_to_collect)
303                                 * pm->timeout_interval)));
304
305   deadman = 0;
306   /* Reasonable to guess that collection may not be quite done... */
307   while (pm->state == PERFMON_STATE_RUNNING)
308     {
309       vlib_process_suspend (vm, 10e-3);
310       if (deadman++ > 200)
311         {
312           vlib_cli_output (vm, "DEADMAN: collection still running...");
313           break;
314         }
315     }
316
317   vlib_cli_output (vm, "Data collection complete...");
318   return 0;
319 }
320
321 /* *INDENT-OFF* */
322 VLIB_CLI_COMMAND (set_pmc_command, static) =
323 {
324   .path = "set pmc",
325   .short_help = "set pmc c1 [..., use \"show pmc events\"]",
326   .function = set_pmc_command_fn,
327   .is_mp_safe = 1,
328 };
329 /* *INDENT-ON* */
330
331 static int
332 capture_name_sort (void *a1, void *a2)
333 {
334   perfmon_capture_t *c1 = a1;
335   perfmon_capture_t *c2 = a2;
336
337   return strcmp ((char *) c1->thread_and_node_name,
338                  (char *) c2->thread_and_node_name);
339 }
340
341 static u8 *
342 format_capture (u8 * s, va_list * args)
343 {
344   perfmon_main_t *pm = va_arg (*args, perfmon_main_t *);
345   perfmon_capture_t *c = va_arg (*args, perfmon_capture_t *);
346   int verbose __attribute__ ((unused)) = va_arg (*args, int);
347   f64 ticks_per_pkt;
348   int i;
349
350   if (c == 0)
351     {
352       s = format (s, "%=40s%=20s%=16s%=16s%=16s",
353                   "Name", "Counter", "Count", "Pkts", "Counts/Pkt");
354       return s;
355     }
356
357   for (i = 0; i < vec_len (c->counter_names); i++)
358     {
359       u8 *name;
360
361       if (i == 0)
362         name = c->thread_and_node_name;
363       else
364         {
365           vec_add1 (s, '\n');
366           name = (u8 *) "";
367         }
368
369       /* Deal with synthetic events right here */
370       if (i == pm->ipc_event_index)
371         {
372           f64 ipc_rate;
373           ASSERT (i + 1 < vec_len (c->counter_names));
374
375           if (c->counter_values[i + 1] > 0)
376             ipc_rate = (f64) c->counter_values[i]
377               / (f64) c->counter_values[i + 1];
378           else
379             ipc_rate = 0.0;
380
381           s = format (s, "%-40s%+20s%+16llu%+16llu%+16.2e\n",
382                       name, "instructions-per-clock",
383                       c->counter_values[i],
384                       c->counter_values[i + 1], ipc_rate);
385           name = (u8 *) "";
386         }
387
388       if (i == pm->mispredict_event_index)
389         {
390           f64 mispredict_rate;
391           ASSERT (i + 1 < vec_len (c->counter_names));
392
393           if (c->counter_values[i + 1] > 0)
394             mispredict_rate = (f64) c->counter_values[i]
395               / (f64) c->counter_values[i + 1];
396           else
397             mispredict_rate = 0.0;
398
399           s = format (s, "%-40s%+20s%+16llu%+16llu%+16.2e\n",
400                       name, "branch-mispredict-rate",
401                       c->counter_values[i],
402                       c->counter_values[i + 1], mispredict_rate);
403           name = (u8 *) "";
404         }
405
406       if (c->vectors_this_counter[i])
407         ticks_per_pkt =
408           ((f64) c->counter_values[i]) / ((f64) c->vectors_this_counter[i]);
409       else
410         ticks_per_pkt = 0.0;
411
412       s = format (s, "%-40s%+20s%+16llu%+16llu%+16.2e",
413                   name, c->counter_names[i],
414                   c->counter_values[i],
415                   c->vectors_this_counter[i], ticks_per_pkt);
416     }
417   return s;
418 }
419
420 static u8 *
421 format_generic_events (u8 * s, va_list * args)
422 {
423   int verbose = va_arg (*args, int);
424
425 #define _(type,config,name)                             \
426   if (verbose == 0)                                     \
427     s = format (s, "\n  %s", name);                     \
428   else                                                  \
429     s = format (s, "\n  %s (%d, %d)", name, type, config);
430   foreach_perfmon_event;
431 #undef _
432   return s;
433 }
434
435 typedef struct
436 {
437   u8 *name;
438   name_value_pair_t **nvps;
439 } sort_nvp_t;
440
441 static int
442 sort_nvps_by_name (void *a1, void *a2)
443 {
444   sort_nvp_t *nvp1 = a1;
445   sort_nvp_t *nvp2 = a2;
446
447   return strcmp ((char *) nvp1->name, (char *) nvp2->name);
448 }
449
450 static u8 *
451 format_processor_events (u8 * s, va_list * args)
452 {
453   perfmon_main_t *pm = va_arg (*args, perfmon_main_t *);
454   int verbose = va_arg (*args, int);
455   int i, j;
456   sort_nvp_t *sort_nvps = 0;
457   sort_nvp_t *sn;
458   u8 *key;
459   name_value_pair_t **value;
460
461   /* *INDENT-OFF* */
462   hash_foreach_mem (key, value, pm->perfmon_table,
463   ({
464     vec_add2 (sort_nvps, sn, 1);
465     sn->name = key;
466     sn->nvps = value;
467   }));
468
469   vec_sort_with_function (sort_nvps, sort_nvps_by_name);
470
471   if (verbose == 0)
472     {
473       for (i = 0; i < vec_len (sort_nvps); i++)
474         s = format (s, "\n  %s ", sort_nvps[i].name);
475     }
476   else
477     {
478       for (i = 0; i < vec_len (sort_nvps); i++)
479         {
480           name_value_pair_t **nvps;
481           s = format (s, "\n  %s:", sort_nvps[i].name);
482
483           nvps = sort_nvps[i].nvps;
484
485           for (j = 0; j < vec_len (nvps); j++)
486             s = format (s, "\n    %s = %s", nvps[j]->name, nvps[j]->value);
487         }
488     }
489   vec_free (sort_nvps);
490   return s;
491 }
492
493
494 static clib_error_t *
495 show_pmc_command_fn (vlib_main_t * vm,
496                      unformat_input_t * input, vlib_cli_command_t * cmd)
497 {
498   perfmon_main_t *pm = &perfmon_main;
499   int verbose = 0;
500   int events = 0;
501   int i;
502   perfmon_capture_t *c;
503   perfmon_capture_t *captures = 0;
504
505   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
506     {
507       if (unformat (input, "events"))
508         events = 1;
509       else if (unformat (input, "verbose"))
510         verbose = 1;
511       else
512         break;
513     }
514
515   if (events)
516     {
517       vlib_cli_output (vm, "Generic Events %U",
518                        format_generic_events, verbose);
519       vlib_cli_output (vm, "Synthetic Events");
520       vlib_cli_output (vm, "  instructions-per-clock");
521       vlib_cli_output (vm, "  branch-mispredict-rate");
522       if (pm->perfmon_table)
523         vlib_cli_output (vm, "Processor Events %U",
524                          format_processor_events, pm, verbose);
525       return 0;
526     }
527
528   if (pm->state == PERFMON_STATE_RUNNING)
529     {
530       vlib_cli_output (vm, "Data collection in progress...");
531       return 0;
532     }
533
534   if (pool_elts (pm->capture_pool) == 0)
535     {
536       vlib_cli_output (vm, "No data...");
537       return 0;
538     }
539
540   /* *INDENT-OFF* */
541   pool_foreach (c, pm->capture_pool,
542   ({
543     vec_add1 (captures, *c);
544   }));
545   /* *INDENT-ON* */
546
547   vec_sort_with_function (captures, capture_name_sort);
548
549   vlib_cli_output (vm, "%U", format_capture, pm, 0 /* header */ ,
550                    0 /* verbose */ );
551
552   for (i = 0; i < vec_len (captures); i++)
553     {
554       c = captures + i;
555
556       vlib_cli_output (vm, "%U", format_capture, pm, c, verbose);
557     }
558
559   vec_free (captures);
560
561   return 0;
562 }
563
564 /* *INDENT-OFF* */
565 VLIB_CLI_COMMAND (show_pmc_command, static) =
566 {
567   .path = "show pmc",
568   .short_help = "show pmc [verbose]",
569   .function = show_pmc_command_fn,
570   .is_mp_safe = 1,
571 };
572 /* *INDENT-ON* */
573
574 static clib_error_t *
575 clear_pmc_command_fn (vlib_main_t * vm,
576                       unformat_input_t * input, vlib_cli_command_t * cmd)
577 {
578   perfmon_main_t *pm = &perfmon_main;
579   u8 *key;
580   u32 *value;
581
582   if (pm->state == PERFMON_STATE_RUNNING)
583     {
584       vlib_cli_output (vm, "Performance monitor is still running...");
585       return 0;
586     }
587
588   pool_free (pm->capture_pool);
589
590   /* *INDENT-OFF* */
591   hash_foreach_mem (key, value, pm->capture_by_thread_and_node_name,
592   ({
593     vec_free (key);
594   }));
595   /* *INDENT-ON* */
596   hash_free (pm->capture_by_thread_and_node_name);
597   pm->capture_by_thread_and_node_name =
598     hash_create_string (0, sizeof (uword));
599   return 0;
600 }
601
602 /* *INDENT-OFF* */
603 VLIB_CLI_COMMAND (clear_pmc_command, static) =
604 {
605   .path = "clear pmc",
606   .short_help = "clear the performance monitor counters",
607   .function = clear_pmc_command_fn,
608 };
609 /* *INDENT-ON* */
610
611
612 /*
613  * fd.io coding-style-patch-verification: ON
614  *
615  * Local Variables:
616  * eval: (c-set-style "gnu")
617  * End:
618  */