vpp_lite: add cpu pinning support (VPP-467)
[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           switch (lcore_config[lcore].state)
113             {
114             case WAIT:
115               line = format (line, "wait");
116               break;
117             case RUNNING:
118               line = format (line, "running");
119               break;
120             case FINISHED:
121               line = format (line, "finished");
122               break;
123             default:
124               line = format (line, "unknown");
125             }
126 #endif
127         }
128       else
129         {
130           line =
131             format (line, "%-7s%-7s%-7s%", (lcore == -2) ? "M" : "n/a", "n/a",
132                     "n/a");
133         }
134
135       vlib_cli_output (vm, "%v", line);
136       vec_free (line);
137     }
138 #endif
139
140   return 0;
141 }
142
143
144 /* *INDENT-OFF* */
145 VLIB_CLI_COMMAND (show_threads_command, static) = {
146   .path = "show threads",
147   .short_help = "Show threads",
148   .function = show_threads_fn,
149 };
150 /* *INDENT-ON* */
151
152 /*
153  * Trigger threads to grab frame queue trace data
154  */
155 static clib_error_t *
156 trace_frame_queue (vlib_main_t * vm, unformat_input_t * input,
157                    vlib_cli_command_t * cmd)
158 {
159   clib_error_t *error = NULL;
160   frame_queue_trace_t *fqt;
161   frame_queue_nelt_counter_t *fqh;
162   vlib_thread_main_t *tm = vlib_get_thread_main ();
163   u32 num_fq;
164   u32 fqix;
165   u32 enable = 0;
166
167   if (unformat (input, "on"))
168     {
169       enable = 1;
170     }
171   else if (unformat (input, "off"))
172     {
173       enable = 0;
174     }
175   else
176     {
177       return clib_error_return (0, "expecting on or off");
178     }
179
180   num_fq = vec_len (vlib_frame_queues);
181   if (num_fq == 0)
182     {
183       vlib_cli_output (vm, "No frame queues exist\n");
184       return error;
185     }
186
187   // Allocate storage for trace if necessary
188   vec_validate_aligned (tm->frame_queue_traces, num_fq - 1,
189                         CLIB_CACHE_LINE_BYTES);
190   vec_validate_aligned (tm->frame_queue_histogram, num_fq - 1,
191                         CLIB_CACHE_LINE_BYTES);
192
193   for (fqix = 0; fqix < num_fq; fqix++)
194     {
195       fqt = &tm->frame_queue_traces[fqix];
196       fqh = &tm->frame_queue_histogram[fqix];
197
198       memset (fqt->n_vectors, 0xff, sizeof (fqt->n_vectors));
199       fqt->written = 0;
200       memset (fqh, 0, sizeof (*fqh));
201       vlib_frame_queues[fqix]->trace = enable;
202     }
203   return error;
204 }
205
206 /* *INDENT-OFF* */
207 VLIB_CLI_COMMAND (cmd_trace_frame_queue,static) = {
208     .path = "trace frame-queue",
209     .short_help = "trace frame-queue (on|off)",
210     .function = trace_frame_queue,
211     .is_mp_safe = 1,
212 };
213 /* *INDENT-ON* */
214
215
216 /*
217  * Adding two counters and compute percent of total
218  * Round up, e.g. 0.000001 => 1%
219  */
220 static u32
221 compute_percent (u64 * two_counters, u64 total)
222 {
223   if (total == 0)
224     {
225       return 0;
226     }
227   else
228     {
229       return (((two_counters[0] + two_counters[1]) * 100) +
230               (total - 1)) / total;
231     }
232 }
233
234 /*
235  * Display frame queue trace data gathered by threads.
236  */
237 static clib_error_t *
238 show_frame_queue_internal (vlib_main_t * vm, u32 histogram)
239 {
240   vlib_thread_main_t *tm = vlib_get_thread_main ();
241   clib_error_t *error = NULL;
242   frame_queue_trace_t *fqt;
243   frame_queue_nelt_counter_t *fqh;
244   u32 num_fq;
245   u32 fqix;
246
247   num_fq = vec_len (tm->frame_queue_traces);
248   if (num_fq == 0)
249     {
250       vlib_cli_output (vm, "No trace data for frame queues\n");
251       return error;
252     }
253
254   if (histogram)
255     {
256       vlib_cli_output (vm, "0-1   2-3   4-5   6-7   8-9   10-11 12-13 14-15 "
257                        "16-17 18-19 20-21 22-23 24-25 26-27 28-29 30-31\n");
258     }
259
260   for (fqix = 0; fqix < num_fq; fqix++)
261     {
262       fqt = &(tm->frame_queue_traces[fqix]);
263
264       vlib_cli_output (vm, "Thread %d %v\n", fqix,
265                        vlib_worker_threads[fqix].name);
266
267       if (fqt->written == 0)
268         {
269           vlib_cli_output (vm, "  no trace data\n");
270           continue;
271         }
272
273       if (histogram)
274         {
275           fqh = &(tm->frame_queue_histogram[fqix]);
276           u32 nelt;
277           u64 total = 0;
278
279           for (nelt = 0; nelt < FRAME_QUEUE_MAX_NELTS; nelt++)
280             {
281               total += fqh->count[nelt];
282             }
283
284           /*
285            * Print in pairs to condense the output.
286            * Allow entries with 0 counts to be clearly identified, by rounding up.
287            * Any non-zero value will be displayed as at least one percent. This
288            * also means the sum of percentages can be > 100, but that is fine. The
289            * histogram is counted from the last time "trace frame on" was issued.
290            */
291           vlib_cli_output (vm,
292                            "%3d%%  %3d%%  %3d%%  %3d%%  %3d%%  %3d%%  %3d%%  %3d%%  "
293                            "%3d%%  %3d%%  %3d%%  %3d%%  %3d%%  %3d%%  %3d%%  %3d%%\n",
294                            compute_percent (&fqh->count[0], total),
295                            compute_percent (&fqh->count[2], total),
296                            compute_percent (&fqh->count[4], total),
297                            compute_percent (&fqh->count[6], total),
298                            compute_percent (&fqh->count[8], total),
299                            compute_percent (&fqh->count[10], total),
300                            compute_percent (&fqh->count[12], total),
301                            compute_percent (&fqh->count[14], total),
302                            compute_percent (&fqh->count[16], total),
303                            compute_percent (&fqh->count[18], total),
304                            compute_percent (&fqh->count[20], total),
305                            compute_percent (&fqh->count[22], total),
306                            compute_percent (&fqh->count[24], total),
307                            compute_percent (&fqh->count[26], total),
308                            compute_percent (&fqh->count[28], total),
309                            compute_percent (&fqh->count[30], total));
310         }
311       else
312         {
313           vlib_cli_output (vm,
314                            "  vector-threshold %d  ring size %d  in use %d\n",
315                            fqt->threshold, fqt->nelts, fqt->n_in_use);
316           vlib_cli_output (vm, "  head %12d  head_hint %12d  tail %12d\n",
317                            fqt->head, fqt->head_hint, fqt->tail);
318           vlib_cli_output (vm,
319                            "  %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
320                            fqt->n_vectors[0], fqt->n_vectors[1],
321                            fqt->n_vectors[2], fqt->n_vectors[3],
322                            fqt->n_vectors[4], fqt->n_vectors[5],
323                            fqt->n_vectors[6], fqt->n_vectors[7],
324                            fqt->n_vectors[8], fqt->n_vectors[9],
325                            fqt->n_vectors[10], fqt->n_vectors[11],
326                            fqt->n_vectors[12], fqt->n_vectors[13],
327                            fqt->n_vectors[14], fqt->n_vectors[15]);
328
329           if (fqt->nelts > 16)
330             {
331               vlib_cli_output (vm,
332                                "  %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
333                                fqt->n_vectors[16], fqt->n_vectors[17],
334                                fqt->n_vectors[18], fqt->n_vectors[19],
335                                fqt->n_vectors[20], fqt->n_vectors[21],
336                                fqt->n_vectors[22], fqt->n_vectors[23],
337                                fqt->n_vectors[24], fqt->n_vectors[25],
338                                fqt->n_vectors[26], fqt->n_vectors[27],
339                                fqt->n_vectors[28], fqt->n_vectors[29],
340                                fqt->n_vectors[30], fqt->n_vectors[31]);
341             }
342         }
343
344     }
345   return error;
346 }
347
348 static clib_error_t *
349 show_frame_queue_trace (vlib_main_t * vm, unformat_input_t * input,
350                         vlib_cli_command_t * cmd)
351 {
352   return show_frame_queue_internal (vm, 0);
353 }
354
355 static clib_error_t *
356 show_frame_queue_histogram (vlib_main_t * vm, unformat_input_t * input,
357                             vlib_cli_command_t * cmd)
358 {
359   return show_frame_queue_internal (vm, 1);
360 }
361
362 /* *INDENT-OFF* */
363 VLIB_CLI_COMMAND (cmd_show_frame_queue_trace,static) = {
364     .path = "show frame-queue",
365     .short_help = "show frame-queue trace",
366     .function = show_frame_queue_trace,
367 };
368 /* *INDENT-ON* */
369
370 /* *INDENT-OFF* */
371 VLIB_CLI_COMMAND (cmd_show_frame_queue_histogram,static) = {
372     .path = "show frame-queue histogram",
373     .short_help = "show frame-queue histogram",
374     .function = show_frame_queue_histogram,
375 };
376 /* *INDENT-ON* */
377
378
379 /*
380  * Modify the number of elements on the frame_queues
381  */
382 static clib_error_t *
383 test_frame_queue_nelts (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 nelts = 0;
390
391   if ((unformat (input, "%d", &nelts) != 1) ||
392       ((nelts != 4) && (nelts != 8) && (nelts != 16) && (nelts != 32)))
393     {
394       return clib_error_return (0, "expecting 4,8,16,32");
395     }
396
397   num_fq = vec_len (vlib_frame_queues);
398   if (num_fq == 0)
399     {
400       vlib_cli_output (vm, "No frame queues exist\n");
401       return error;
402     }
403
404   for (fqix = 0; fqix < num_fq; fqix++)
405     {
406       vlib_frame_queues[fqix]->nelts = nelts;
407     }
408
409   return error;
410 }
411
412 /* *INDENT-OFF* */
413 VLIB_CLI_COMMAND (cmd_test_frame_queue_nelts,static) = {
414     .path = "test frame-queue nelts",
415     .short_help = "test frame-queue nelts (4,8,16,32)",
416     .function = test_frame_queue_nelts,
417 };
418 /* *INDENT-ON* */
419
420
421 /*
422  * Modify the max number of packets pulled off the frame queues
423  */
424 static clib_error_t *
425 test_frame_queue_threshold (vlib_main_t * vm, unformat_input_t * input,
426                             vlib_cli_command_t * cmd)
427 {
428   clib_error_t *error = NULL;
429   u32 num_fq;
430   u32 fqix;
431   u32 threshold = 0;
432
433   if (unformat (input, "%d", &threshold))
434     {
435     }
436   else
437     {
438       vlib_cli_output (vm, "expecting threshold value\n");
439       return error;
440     }
441
442   if (threshold == 0)
443     threshold = ~0;
444
445   num_fq = vec_len (vlib_frame_queues);
446   if (num_fq == 0)
447     {
448       vlib_cli_output (vm, "No frame queues exist\n");
449       return error;
450     }
451
452   for (fqix = 0; fqix < num_fq; fqix++)
453     {
454       vlib_frame_queues[fqix]->vector_threshold = threshold;
455     }
456
457   return error;
458 }
459
460 /* *INDENT-OFF* */
461 VLIB_CLI_COMMAND (cmd_test_frame_queue_threshold,static) = {
462     .path = "test frame-queue threshold",
463     .short_help = "test frame-queue threshold N (0=no limit)",
464     .function = test_frame_queue_threshold,
465 };
466 /* *INDENT-ON* */
467
468
469 /*
470  * fd.io coding-style-patch-verification: ON
471  *
472  * Local Variables:
473  * eval: (c-set-style "gnu")
474  * End:
475  */