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