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