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