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