179906ba51bc4c8296f6cc86fba87ad8940b1611
[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
16 #include <vppinfra/format.h>
17 #include <vlib/vlib.h>
18
19 #include <vlib/threads.h>
20 #include <linux/sched.h>
21
22 u8 *
23 format_sched_policy (u8 * s, va_list * args)
24 {
25   u32 i = va_arg (*args, u32);
26   u8 *t = 0;
27
28   switch (i)
29     {
30 #define _(v,f,str) case SCHED_POLICY_##f: t = (u8 *) str; break;
31       foreach_sched_policy
32 #undef _
33     }
34   s = format (s, "%-6s ", t);
35   return s;
36 }
37
38 static clib_error_t *
39 show_threads_fn (vlib_main_t * vm,
40                  unformat_input_t * input, vlib_cli_command_t * cmd)
41 {
42   vlib_worker_thread_t *w;
43   int i;
44
45   vlib_cli_output (vm, "%-7s%-20s%-12s%-8s%-7s%-9s%-7s%-7s%-7s%-10s",
46                    "ID", "Name", "Type", "LWP", "Policy", "Priority",
47                    "lcore", "Core", "Socket", "State");
48
49 #if !defined(__powerpc64__)
50   for (i = 0; i < vec_len (vlib_worker_threads); i++)
51     {
52       w = vlib_worker_threads + i;
53       u8 *line = NULL;
54
55       line = format (line, "%-7d%-20s%-12s%-8d",
56                      i,
57                      w->name ? w->name : (u8 *) "",
58                      w->registration ? w->registration->name : "", w->lwp);
59
60       line =
61         format (line, "%U", format_sched_policy, sched_getscheduler (w->lwp));
62
63       struct sched_param sched_param;
64       line = format (line, "%8d ",
65                      (!sched_getparam (w->lwp, &sched_param)) ?
66                      sched_param.sched_priority : -1);
67
68 #if DPDK==1
69       int lcore = w->dpdk_lcore_id;
70       if (lcore > -1)
71         {
72           line = format (line, "%-7u%-7u%-7u",
73                          lcore,
74                          lcore_config[lcore].core_id,
75                          lcore_config[lcore].socket_id);
76
77           switch (lcore_config[lcore].state)
78             {
79             case WAIT:
80               line = format (line, "wait");
81               break;
82             case RUNNING:
83               line = format (line, "running");
84               break;
85             case FINISHED:
86               line = format (line, "finished");
87               break;
88             default:
89               line = format (line, "unknown");
90             }
91         }
92 #endif
93       vlib_cli_output (vm, "%v", line);
94       vec_free (line);
95     }
96 #endif
97
98   return 0;
99 }
100
101
102 /* *INDENT-OFF* */
103 VLIB_CLI_COMMAND (show_threads_command, static) = {
104   .path = "show threads",
105   .short_help = "Show threads",
106   .function = show_threads_fn,
107 };
108 /* *INDENT-ON* */
109
110 /*
111  * Trigger threads to grab frame queue trace data
112  */
113 static clib_error_t *
114 trace_frame_queue (vlib_main_t * vm, unformat_input_t * input,
115                    vlib_cli_command_t * cmd)
116 {
117   clib_error_t *error = NULL;
118   frame_queue_trace_t *fqt;
119   frame_queue_nelt_counter_t *fqh;
120   vlib_thread_main_t *tm = vlib_get_thread_main ();
121   u32 num_fq;
122   u32 fqix;
123   u32 enable = 0;
124
125   if (unformat (input, "on"))
126     {
127       enable = 1;
128     }
129   else if (unformat (input, "off"))
130     {
131       enable = 0;
132     }
133   else
134     {
135       return clib_error_return (0, "expecting on or off");
136     }
137
138   num_fq = vec_len (vlib_frame_queues);
139   if (num_fq == 0)
140     {
141       vlib_cli_output (vm, "No frame queues exist\n");
142       return error;
143     }
144
145   // Allocate storage for trace if necessary
146   vec_validate_aligned (tm->frame_queue_traces, num_fq - 1,
147                         CLIB_CACHE_LINE_BYTES);
148   vec_validate_aligned (tm->frame_queue_histogram, num_fq - 1,
149                         CLIB_CACHE_LINE_BYTES);
150
151   for (fqix = 0; fqix < num_fq; fqix++)
152     {
153       fqt = &tm->frame_queue_traces[fqix];
154       fqh = &tm->frame_queue_histogram[fqix];
155
156       memset (fqt->n_vectors, 0xff, sizeof (fqt->n_vectors));
157       fqt->written = 0;
158       memset (fqh, 0, sizeof (*fqh));
159       vlib_frame_queues[fqix]->trace = enable;
160     }
161   return error;
162 }
163
164 /* *INDENT-OFF* */
165 VLIB_CLI_COMMAND (cmd_trace_frame_queue,static) = {
166     .path = "trace frame-queue",
167     .short_help = "trace frame-queue (on|off)",
168     .function = trace_frame_queue,
169     .is_mp_safe = 1,
170 };
171 /* *INDENT-ON* */
172
173
174 /*
175  * Adding two counters and compute percent of total
176  * Round up, e.g. 0.000001 => 1%
177  */
178 static u32
179 compute_percent (u64 * two_counters, u64 total)
180 {
181   if (total == 0)
182     {
183       return 0;
184     }
185   else
186     {
187       return (((two_counters[0] + two_counters[1]) * 100) +
188               (total - 1)) / total;
189     }
190 }
191
192 /*
193  * Display frame queue trace data gathered by threads.
194  */
195 static clib_error_t *
196 show_frame_queue_internal (vlib_main_t * vm, u32 histogram)
197 {
198   vlib_thread_main_t *tm = vlib_get_thread_main ();
199   clib_error_t *error = NULL;
200   frame_queue_trace_t *fqt;
201   frame_queue_nelt_counter_t *fqh;
202   u32 num_fq;
203   u32 fqix;
204
205   num_fq = vec_len (tm->frame_queue_traces);
206   if (num_fq == 0)
207     {
208       vlib_cli_output (vm, "No trace data for frame queues\n");
209       return error;
210     }
211
212   if (histogram)
213     {
214       vlib_cli_output (vm, "0-1   2-3   4-5   6-7   8-9   10-11 12-13 14-15 "
215                        "16-17 18-19 20-21 22-23 24-25 26-27 28-29 30-31\n");
216     }
217
218   for (fqix = 0; fqix < num_fq; fqix++)
219     {
220       fqt = &(tm->frame_queue_traces[fqix]);
221
222       vlib_cli_output (vm, "Thread %d %v\n", fqix,
223                        vlib_worker_threads[fqix].name);
224
225       if (fqt->written == 0)
226         {
227           vlib_cli_output (vm, "  no trace data\n");
228           continue;
229         }
230
231       if (histogram)
232         {
233           fqh = &(tm->frame_queue_histogram[fqix]);
234           u32 nelt;
235           u64 total = 0;
236
237           for (nelt = 0; nelt < FRAME_QUEUE_MAX_NELTS; nelt++)
238             {
239               total += fqh->count[nelt];
240             }
241
242           /*
243            * Print in pairs to condense the output.
244            * Allow entries with 0 counts to be clearly identified, by rounding up.
245            * Any non-zero value will be displayed as at least one percent. This
246            * also means the sum of percentages can be > 100, but that is fine. The
247            * histogram is counted from the last time "trace frame on" was issued.
248            */
249           vlib_cli_output (vm,
250                            "%3d%%  %3d%%  %3d%%  %3d%%  %3d%%  %3d%%  %3d%%  %3d%%  "
251                            "%3d%%  %3d%%  %3d%%  %3d%%  %3d%%  %3d%%  %3d%%  %3d%%\n",
252                            compute_percent (&fqh->count[0], total),
253                            compute_percent (&fqh->count[2], total),
254                            compute_percent (&fqh->count[4], total),
255                            compute_percent (&fqh->count[6], total),
256                            compute_percent (&fqh->count[8], total),
257                            compute_percent (&fqh->count[10], total),
258                            compute_percent (&fqh->count[12], total),
259                            compute_percent (&fqh->count[14], total),
260                            compute_percent (&fqh->count[16], total),
261                            compute_percent (&fqh->count[18], total),
262                            compute_percent (&fqh->count[20], total),
263                            compute_percent (&fqh->count[22], total),
264                            compute_percent (&fqh->count[24], total),
265                            compute_percent (&fqh->count[26], total),
266                            compute_percent (&fqh->count[28], total),
267                            compute_percent (&fqh->count[30], total));
268         }
269       else
270         {
271           vlib_cli_output (vm,
272                            "  vector-threshold %d  ring size %d  in use %d\n",
273                            fqt->threshold, fqt->nelts, fqt->n_in_use);
274           vlib_cli_output (vm, "  head %12d  head_hint %12d  tail %12d\n",
275                            fqt->head, fqt->head_hint, fqt->tail);
276           vlib_cli_output (vm,
277                            "  %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
278                            fqt->n_vectors[0], fqt->n_vectors[1],
279                            fqt->n_vectors[2], fqt->n_vectors[3],
280                            fqt->n_vectors[4], fqt->n_vectors[5],
281                            fqt->n_vectors[6], fqt->n_vectors[7],
282                            fqt->n_vectors[8], fqt->n_vectors[9],
283                            fqt->n_vectors[10], fqt->n_vectors[11],
284                            fqt->n_vectors[12], fqt->n_vectors[13],
285                            fqt->n_vectors[14], fqt->n_vectors[15]);
286
287           if (fqt->nelts > 16)
288             {
289               vlib_cli_output (vm,
290                                "  %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
291                                fqt->n_vectors[16], fqt->n_vectors[17],
292                                fqt->n_vectors[18], fqt->n_vectors[19],
293                                fqt->n_vectors[20], fqt->n_vectors[21],
294                                fqt->n_vectors[22], fqt->n_vectors[23],
295                                fqt->n_vectors[24], fqt->n_vectors[25],
296                                fqt->n_vectors[26], fqt->n_vectors[27],
297                                fqt->n_vectors[28], fqt->n_vectors[29],
298                                fqt->n_vectors[30], fqt->n_vectors[31]);
299             }
300         }
301
302     }
303   return error;
304 }
305
306 static clib_error_t *
307 show_frame_queue_trace (vlib_main_t * vm, unformat_input_t * input,
308                         vlib_cli_command_t * cmd)
309 {
310   return show_frame_queue_internal (vm, 0);
311 }
312
313 static clib_error_t *
314 show_frame_queue_histogram (vlib_main_t * vm, unformat_input_t * input,
315                             vlib_cli_command_t * cmd)
316 {
317   return show_frame_queue_internal (vm, 1);
318 }
319
320 /* *INDENT-OFF* */
321 VLIB_CLI_COMMAND (cmd_show_frame_queue_trace,static) = {
322     .path = "show frame-queue",
323     .short_help = "show frame-queue trace",
324     .function = show_frame_queue_trace,
325 };
326 /* *INDENT-ON* */
327
328 /* *INDENT-OFF* */
329 VLIB_CLI_COMMAND (cmd_show_frame_queue_histogram,static) = {
330     .path = "show frame-queue histogram",
331     .short_help = "show frame-queue histogram",
332     .function = show_frame_queue_histogram,
333 };
334 /* *INDENT-ON* */
335
336
337 /*
338  * Modify the number of elements on the frame_queues
339  */
340 static clib_error_t *
341 test_frame_queue_nelts (vlib_main_t * vm, unformat_input_t * input,
342                         vlib_cli_command_t * cmd)
343 {
344   clib_error_t *error = NULL;
345   u32 num_fq;
346   u32 fqix;
347   u32 nelts = 0;
348
349   if ((unformat (input, "%d", &nelts) != 1) ||
350       ((nelts != 4) && (nelts != 8) && (nelts != 16) && (nelts != 32)))
351     {
352       return clib_error_return (0, "expecting 4,8,16,32");
353     }
354
355   num_fq = vec_len (vlib_frame_queues);
356   if (num_fq == 0)
357     {
358       vlib_cli_output (vm, "No frame queues exist\n");
359       return error;
360     }
361
362   for (fqix = 0; fqix < num_fq; fqix++)
363     {
364       vlib_frame_queues[fqix]->nelts = nelts;
365     }
366
367   return error;
368 }
369
370 /* *INDENT-OFF* */
371 VLIB_CLI_COMMAND (cmd_test_frame_queue_nelts,static) = {
372     .path = "test frame-queue nelts",
373     .short_help = "test frame-queue nelts (4,8,16,32)",
374     .function = test_frame_queue_nelts,
375 };
376 /* *INDENT-ON* */
377
378
379 /*
380  * Modify the max number of packets pulled off the frame queues
381  */
382 static clib_error_t *
383 test_frame_queue_threshold (vlib_main_t * vm, unformat_input_t * input,
384                             vlib_cli_command_t * cmd)
385 {
386   clib_error_t *error = NULL;
387   u32 num_fq;
388   u32 fqix;
389   u32 threshold = 0;
390
391   if (unformat (input, "%d", &threshold))
392     {
393     }
394   else
395     {
396       vlib_cli_output (vm, "expecting threshold value\n");
397       return error;
398     }
399
400   if (threshold == 0)
401     threshold = ~0;
402
403   num_fq = vec_len (vlib_frame_queues);
404   if (num_fq == 0)
405     {
406       vlib_cli_output (vm, "No frame queues exist\n");
407       return error;
408     }
409
410   for (fqix = 0; fqix < num_fq; fqix++)
411     {
412       vlib_frame_queues[fqix]->vector_threshold = threshold;
413     }
414
415   return error;
416 }
417
418 /* *INDENT-OFF* */
419 VLIB_CLI_COMMAND (cmd_test_frame_queue_threshold,static) = {
420     .path = "test frame-queue threshold",
421     .short_help = "test frame-queue threshold N (0=no limit)",
422     .function = test_frame_queue_threshold,
423 };
424 /* *INDENT-ON* */
425
426
427 /*
428  * fd.io coding-style-patch-verification: ON
429  *
430  * Local Variables:
431  * eval: (c-set-style "gnu")
432  * End:
433  */