dpdk: Add support for Mellanox ConnectX-4 devices
[vpp.git] / vlib / vlib / threads_cli.c
1 /*
2  * Copyright (c) 2015 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 #define _GNU_SOURCE
16
17 #include <vppinfra/format.h>
18 #include <vlib/vlib.h>
19
20 #include <vlib/threads.h>
21 #include <vlib/unix/unix.h>
22
23 #if DPDK==1
24 #include <rte_config.h>
25 #include <rte_common.h>
26 #include <rte_eal.h>
27 #include <rte_launch.h>
28 #include <rte_lcore.h>
29 #endif
30
31 static u8 *
32 format_sched_policy_and_priority (u8 * s, va_list * args)
33 {
34   long i = va_arg (*args, long);
35   struct sched_param sched_param;
36   u8 *t = 0;
37
38   switch (sched_getscheduler (i))
39     {
40 #define _(v,f,str) case SCHED_POLICY_##f: t = (u8 *) str; break;
41       foreach_sched_policy
42 #undef _
43     }
44   if (sched_getparam (i, &sched_param) == 0)
45     return format (s, "%s (%d)", t, sched_param.sched_priority);
46   else
47     return format (s, "%s (n/a)", t);
48 }
49
50 static clib_error_t *
51 show_threads_fn (vlib_main_t * vm,
52                  unformat_input_t * input, vlib_cli_command_t * cmd)
53 {
54   vlib_worker_thread_t *w;
55   int i;
56
57   vlib_cli_output (vm, "%-7s%-20s%-12s%-8s%-25s%-7s%-7s%-7s%-10s",
58                    "ID", "Name", "Type", "LWP", "Sched Policy (Priority)",
59                    "lcore", "Core", "Socket", "State");
60
61 #if !defined(__powerpc64__)
62   for (i = 0; i < vec_len (vlib_worker_threads); i++)
63     {
64       w = vlib_worker_threads + i;
65       u8 *line = NULL;
66
67       line = format (line, "%-7d%-20s%-12s%-8d",
68                      i,
69                      w->name ? w->name : (u8 *) "",
70                      w->registration ? w->registration->name : "", w->lwp);
71
72       line = format (line, "%-25U", format_sched_policy_and_priority, w->lwp);
73
74       int lcore = -1;
75       cpu_set_t cpuset;
76       CPU_ZERO (&cpuset);
77       int ret = -1;
78
79       ret =
80         pthread_getaffinity_np (w->thread_id, sizeof (cpu_set_t), &cpuset);
81       if (!ret)
82         {
83           int c;
84           for (c = 0; c < CPU_SETSIZE; c++)
85             if (CPU_ISSET (c, &cpuset))
86               {
87                 if (lcore > -1)
88                   {
89                     lcore = -2;
90                     break;
91                   }
92                 lcore = c;
93               }
94         }
95       else
96         {
97           lcore = w->lcore_id;
98         }
99
100       if (lcore > -1)
101         {
102           const char *sys_cpu_path = "/sys/devices/system/cpu/cpu";
103           int socket_id = -1;
104           int core_id = -1;
105           u8 *p = 0;
106
107           p = format (p, "%s%u/topology/core_id%c", sys_cpu_path, lcore, 0);
108           vlib_sysfs_read ((char *) p, "%d", &core_id);
109
110           vec_reset_length (p);
111           p =
112             format (p,
113                     "%s%u/topology/physical_package_id%c",
114                     sys_cpu_path, lcore, 0);
115           vlib_sysfs_read ((char *) p, "%d", &socket_id);
116           vec_free (p);
117
118           line = format (line, "%-7u%-7u%-7u%", lcore, core_id, socket_id);
119 #if DPDK==1
120           ASSERT (lcore <= RTE_MAX_LCORE);
121           switch (lcore_config[lcore].state)
122             {
123             case WAIT:
124               line = format (line, "wait");
125               break;
126             case RUNNING:
127               line = format (line, "running");
128               break;
129             case FINISHED:
130               line = format (line, "finished");
131               break;
132             default:
133               line = format (line, "unknown");
134             }
135 #endif
136         }
137       else
138         {
139           line =
140             format (line, "%-7s%-7s%-7s%", (lcore == -2) ? "M" : "n/a", "n/a",
141                     "n/a");
142         }
143
144       vlib_cli_output (vm, "%v", line);
145       vec_free (line);
146     }
147 #endif
148
149   return 0;
150 }
151
152
153 /* *INDENT-OFF* */
154 VLIB_CLI_COMMAND (show_threads_command, static) = {
155   .path = "show threads",
156   .short_help = "Show threads",
157   .function = show_threads_fn,
158 };
159 /* *INDENT-ON* */
160
161 /*
162  * Trigger threads to grab frame queue trace data
163  */
164 static clib_error_t *
165 trace_frame_queue (vlib_main_t * vm, unformat_input_t * input,
166                    vlib_cli_command_t * cmd)
167 {
168   unformat_input_t _line_input, *line_input = &_line_input;
169   clib_error_t *error = NULL;
170   frame_queue_trace_t *fqt;
171   frame_queue_nelt_counter_t *fqh;
172   vlib_thread_main_t *tm = vlib_get_thread_main ();
173   vlib_frame_queue_main_t *fqm;
174   u32 num_fq;
175   u32 fqix;
176   u32 enable = 2;
177   u32 index = ~(u32) 0;
178
179   if (!unformat_user (input, unformat_line_input, line_input))
180     return 0;
181
182   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
183     {
184       if (unformat (line_input, "on"))
185         enable = 1;
186       else if (unformat (line_input, "off"))
187         enable = 0;
188       else if (unformat (line_input, "index %u"), &index)
189         ;
190       else
191         return clib_error_return (0, "parse error: '%U'",
192                                   format_unformat_error, line_input);
193     }
194
195   unformat_free (line_input);
196
197   if (enable > 1)
198     return clib_error_return (0, "expecting on or off");
199
200   if (vec_len (tm->frame_queue_mains) == 0)
201     return clib_error_return (0, "no worker handoffs exist");
202
203   if (index > vec_len (tm->frame_queue_mains) - 1)
204     return clib_error_return (0,
205                               "expecting valid worker handoff queue index");
206
207   fqm = vec_elt_at_index (tm->frame_queue_mains, index);
208
209   num_fq = vec_len (fqm->vlib_frame_queues);
210   if (num_fq == 0)
211     {
212       vlib_cli_output (vm, "No frame queues exist\n");
213       return error;
214     }
215
216   // Allocate storage for trace if necessary
217   vec_validate_aligned (fqm->frame_queue_traces, num_fq - 1,
218                         CLIB_CACHE_LINE_BYTES);
219   vec_validate_aligned (fqm->frame_queue_histogram, num_fq - 1,
220                         CLIB_CACHE_LINE_BYTES);
221
222   for (fqix = 0; fqix < num_fq; fqix++)
223     {
224       fqt = &fqm->frame_queue_traces[fqix];
225       fqh = &fqm->frame_queue_histogram[fqix];
226
227       memset (fqt->n_vectors, 0xff, sizeof (fqt->n_vectors));
228       fqt->written = 0;
229       memset (fqh, 0, sizeof (*fqh));
230       fqm->vlib_frame_queues[fqix]->trace = enable;
231     }
232   return error;
233 }
234
235 /* *INDENT-OFF* */
236 VLIB_CLI_COMMAND (cmd_trace_frame_queue,static) = {
237     .path = "trace frame-queue",
238     .short_help = "trace frame-queue (on|off)",
239     .function = trace_frame_queue,
240     .is_mp_safe = 1,
241 };
242 /* *INDENT-ON* */
243
244
245 /*
246  * Adding two counters and compute percent of total
247  * Round up, e.g. 0.000001 => 1%
248  */
249 static u32
250 compute_percent (u64 * two_counters, u64 total)
251 {
252   if (total == 0)
253     {
254       return 0;
255     }
256   else
257     {
258       return (((two_counters[0] + two_counters[1]) * 100) +
259               (total - 1)) / total;
260     }
261 }
262
263 /*
264  * Display frame queue trace data gathered by threads.
265  */
266 static clib_error_t *
267 show_frame_queue_internal (vlib_main_t * vm,
268                            vlib_frame_queue_main_t * fqm, u32 histogram)
269 {
270   clib_error_t *error = NULL;
271   frame_queue_trace_t *fqt;
272   frame_queue_nelt_counter_t *fqh;
273   u32 num_fq;
274   u32 fqix;
275
276   num_fq = vec_len (fqm->frame_queue_traces);
277   if (num_fq == 0)
278     {
279       vlib_cli_output (vm, "No trace data for frame queues\n");
280       return error;
281     }
282
283   if (histogram)
284     {
285       vlib_cli_output (vm, "0-1   2-3   4-5   6-7   8-9   10-11 12-13 14-15 "
286                        "16-17 18-19 20-21 22-23 24-25 26-27 28-29 30-31\n");
287     }
288
289   for (fqix = 0; fqix < num_fq; fqix++)
290     {
291       fqt = &(fqm->frame_queue_traces[fqix]);
292
293       vlib_cli_output (vm, "Thread %d %v\n", fqix,
294                        vlib_worker_threads[fqix].name);
295
296       if (fqt->written == 0)
297         {
298           vlib_cli_output (vm, "  no trace data\n");
299           continue;
300         }
301
302       if (histogram)
303         {
304           fqh = &(fqm->frame_queue_histogram[fqix]);
305           u32 nelt;
306           u64 total = 0;
307
308           for (nelt = 0; nelt < FRAME_QUEUE_MAX_NELTS; nelt++)
309             {
310               total += fqh->count[nelt];
311             }
312
313           /*
314            * Print in pairs to condense the output.
315            * Allow entries with 0 counts to be clearly identified, by rounding up.
316            * Any non-zero value will be displayed as at least one percent. This
317            * also means the sum of percentages can be > 100, but that is fine. The
318            * histogram is counted from the last time "trace frame on" was issued.
319            */
320           vlib_cli_output (vm,
321                            "%3d%%  %3d%%  %3d%%  %3d%%  %3d%%  %3d%%  %3d%%  %3d%%  "
322                            "%3d%%  %3d%%  %3d%%  %3d%%  %3d%%  %3d%%  %3d%%  %3d%%\n",
323                            compute_percent (&fqh->count[0], total),
324                            compute_percent (&fqh->count[2], total),
325                            compute_percent (&fqh->count[4], total),
326                            compute_percent (&fqh->count[6], total),
327                            compute_percent (&fqh->count[8], total),
328                            compute_percent (&fqh->count[10], total),
329                            compute_percent (&fqh->count[12], total),
330                            compute_percent (&fqh->count[14], total),
331                            compute_percent (&fqh->count[16], total),
332                            compute_percent (&fqh->count[18], total),
333                            compute_percent (&fqh->count[20], total),
334                            compute_percent (&fqh->count[22], total),
335                            compute_percent (&fqh->count[24], total),
336                            compute_percent (&fqh->count[26], total),
337                            compute_percent (&fqh->count[28], total),
338                            compute_percent (&fqh->count[30], total));
339         }
340       else
341         {
342           vlib_cli_output (vm,
343                            "  vector-threshold %d  ring size %d  in use %d\n",
344                            fqt->threshold, fqt->nelts, fqt->n_in_use);
345           vlib_cli_output (vm, "  head %12d  head_hint %12d  tail %12d\n",
346                            fqt->head, fqt->head_hint, fqt->tail);
347           vlib_cli_output (vm,
348                            "  %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
349                            fqt->n_vectors[0], fqt->n_vectors[1],
350                            fqt->n_vectors[2], fqt->n_vectors[3],
351                            fqt->n_vectors[4], fqt->n_vectors[5],
352                            fqt->n_vectors[6], fqt->n_vectors[7],
353                            fqt->n_vectors[8], fqt->n_vectors[9],
354                            fqt->n_vectors[10], fqt->n_vectors[11],
355                            fqt->n_vectors[12], fqt->n_vectors[13],
356                            fqt->n_vectors[14], fqt->n_vectors[15]);
357
358           if (fqt->nelts > 16)
359             {
360               vlib_cli_output (vm,
361                                "  %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
362                                fqt->n_vectors[16], fqt->n_vectors[17],
363                                fqt->n_vectors[18], fqt->n_vectors[19],
364                                fqt->n_vectors[20], fqt->n_vectors[21],
365                                fqt->n_vectors[22], fqt->n_vectors[23],
366                                fqt->n_vectors[24], fqt->n_vectors[25],
367                                fqt->n_vectors[26], fqt->n_vectors[27],
368                                fqt->n_vectors[28], fqt->n_vectors[29],
369                                fqt->n_vectors[30], fqt->n_vectors[31]);
370             }
371         }
372
373     }
374   return error;
375 }
376
377 static clib_error_t *
378 show_frame_queue_trace (vlib_main_t * vm, unformat_input_t * input,
379                         vlib_cli_command_t * cmd)
380 {
381   vlib_thread_main_t *tm = vlib_get_thread_main ();
382   vlib_frame_queue_main_t *fqm;
383   clib_error_t *error;
384
385   vec_foreach (fqm, tm->frame_queue_mains)
386   {
387     vlib_cli_output (vm, "Worker handoff queue index %u (next node '%U'):",
388                      fqm - tm->frame_queue_mains,
389                      format_vlib_node_name, vm, fqm->node_index);
390     error = show_frame_queue_internal (vm, fqm, 0);
391     if (error)
392       return error;
393   }
394   return 0;
395 }
396
397 static clib_error_t *
398 show_frame_queue_histogram (vlib_main_t * vm, unformat_input_t * input,
399                             vlib_cli_command_t * cmd)
400 {
401   vlib_thread_main_t *tm = vlib_get_thread_main ();
402   vlib_frame_queue_main_t *fqm;
403   clib_error_t *error;
404
405   vec_foreach (fqm, tm->frame_queue_mains)
406   {
407     vlib_cli_output (vm, "Worker handoff queue index %u (next node '%U'):",
408                      fqm - tm->frame_queue_mains,
409                      format_vlib_node_name, vm, fqm->node_index);
410     error = show_frame_queue_internal (vm, fqm, 1);
411     if (error)
412       return error;
413   }
414   return 0;
415 }
416
417 /* *INDENT-OFF* */
418 VLIB_CLI_COMMAND (cmd_show_frame_queue_trace,static) = {
419     .path = "show frame-queue",
420     .short_help = "show frame-queue trace",
421     .function = show_frame_queue_trace,
422 };
423 /* *INDENT-ON* */
424
425 /* *INDENT-OFF* */
426 VLIB_CLI_COMMAND (cmd_show_frame_queue_histogram,static) = {
427     .path = "show frame-queue histogram",
428     .short_help = "show frame-queue histogram",
429     .function = show_frame_queue_histogram,
430 };
431 /* *INDENT-ON* */
432
433
434 /*
435  * Modify the number of elements on the frame_queues
436  */
437 static clib_error_t *
438 test_frame_queue_nelts (vlib_main_t * vm, unformat_input_t * input,
439                         vlib_cli_command_t * cmd)
440 {
441   unformat_input_t _line_input, *line_input = &_line_input;
442   vlib_thread_main_t *tm = vlib_get_thread_main ();
443   vlib_frame_queue_main_t *fqm;
444   clib_error_t *error = NULL;
445   u32 num_fq;
446   u32 fqix;
447   u32 nelts = 0;
448   u32 index = ~(u32) 0;
449
450   if (!unformat_user (input, unformat_line_input, line_input))
451     return 0;
452
453   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
454     {
455       if (unformat (line_input, "nelts %u", &nelts))
456         ;
457       else if (unformat (line_input, "index %u", &index))
458         ;
459       else
460         return clib_error_return (0, "parse error: '%U'",
461                                   format_unformat_error, line_input);
462     }
463
464   unformat_free (line_input);
465
466   if (index > vec_len (tm->frame_queue_mains) - 1)
467     return clib_error_return (0,
468                               "expecting valid worker handoff queue index");
469
470   fqm = vec_elt_at_index (tm->frame_queue_mains, index);
471
472   if ((nelts != 4) && (nelts != 8) && (nelts != 16) && (nelts != 32))
473     {
474       return clib_error_return (0, "expecting 4,8,16,32");
475     }
476
477   num_fq = vec_len (fqm->vlib_frame_queues);
478   if (num_fq == 0)
479     {
480       vlib_cli_output (vm, "No frame queues exist\n");
481       return error;
482     }
483
484   for (fqix = 0; fqix < num_fq; fqix++)
485     {
486       fqm->vlib_frame_queues[fqix]->nelts = nelts;
487     }
488
489   return error;
490 }
491
492 /* *INDENT-OFF* */
493 VLIB_CLI_COMMAND (cmd_test_frame_queue_nelts,static) = {
494     .path = "test frame-queue nelts",
495     .short_help = "test frame-queue nelts (4,8,16,32)",
496     .function = test_frame_queue_nelts,
497 };
498 /* *INDENT-ON* */
499
500
501 /*
502  * Modify the max number of packets pulled off the frame queues
503  */
504 static clib_error_t *
505 test_frame_queue_threshold (vlib_main_t * vm, unformat_input_t * input,
506                             vlib_cli_command_t * cmd)
507 {
508   unformat_input_t _line_input, *line_input = &_line_input;
509   vlib_thread_main_t *tm = vlib_get_thread_main ();
510   vlib_frame_queue_main_t *fqm;
511   clib_error_t *error = NULL;
512   u32 num_fq;
513   u32 fqix;
514   u32 threshold = ~(u32) 0;
515   u32 index = ~(u32) 0;
516
517   if (!unformat_user (input, unformat_line_input, line_input))
518     return 0;
519
520   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
521     {
522       if (unformat (line_input, "threshold %u", &threshold))
523         ;
524       else if (unformat (line_input, "index %u", &index))
525         ;
526       else
527         return clib_error_return (0, "parse error: '%U'",
528                                   format_unformat_error, line_input);
529     }
530
531   unformat_free (line_input);
532
533   if (index > vec_len (tm->frame_queue_mains) - 1)
534     return clib_error_return (0,
535                               "expecting valid worker handoff queue index");
536
537   fqm = vec_elt_at_index (tm->frame_queue_mains, index);
538
539
540   if (threshold == ~(u32) 0)
541     {
542       vlib_cli_output (vm, "expecting threshold value\n");
543       return error;
544     }
545
546   if (threshold == 0)
547     threshold = ~0;
548
549   num_fq = vec_len (fqm->vlib_frame_queues);
550   if (num_fq == 0)
551     {
552       vlib_cli_output (vm, "No frame queues exist\n");
553       return error;
554     }
555
556   for (fqix = 0; fqix < num_fq; fqix++)
557     {
558       fqm->vlib_frame_queues[fqix]->vector_threshold = threshold;
559     }
560
561   return error;
562 }
563
564 /* *INDENT-OFF* */
565 VLIB_CLI_COMMAND (cmd_test_frame_queue_threshold,static) = {
566     .path = "test frame-queue threshold",
567     .short_help = "test frame-queue threshold N (0=no limit)",
568     .function = test_frame_queue_threshold,
569 };
570 /* *INDENT-ON* */
571
572
573 /*
574  * fd.io coding-style-patch-verification: ON
575  *
576  * Local Variables:
577  * eval: (c-set-style "gnu")
578  * End:
579  */