FD.io VPP  v17.01.1-3-gc6833f8
Vector Packet Processing
node_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 /*
16  * node_cli.c: node CLI
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39 
40 #include <vlib/vlib.h>
41 #include <vlib/threads.h>
42 
43 static int
44 node_cmp (void *a1, void *a2)
45 {
46  vlib_node_t **n1 = a1;
47  vlib_node_t **n2 = a2;
48 
49  return vec_cmp (n1[0]->name, n2[0]->name);
50 }
51 
52 static clib_error_t *
54  unformat_input_t * input, vlib_cli_command_t * cmd)
55 {
56  vlib_node_main_t *nm = &vm->node_main;
57  vlib_node_t *n;
58  u32 node_index;
59 
60  vlib_cli_output (vm, "%U\n", format_vlib_node_graph, nm, 0);
61 
62  if (unformat (input, "%U", unformat_vlib_node, vm, &node_index))
63  {
64  n = vlib_get_node (vm, node_index);
65  vlib_cli_output (vm, "%U\n", format_vlib_node_graph, nm, n);
66  }
67  else
68  {
69  vlib_node_t **nodes = vec_dup (nm->nodes);
70  uword i;
71 
73 
74  for (i = 0; i < vec_len (nodes); i++)
75  vlib_cli_output (vm, "%U\n\n", format_vlib_node_graph, nm, nodes[i]);
76 
77  vec_free (nodes);
78  }
79 
80  return 0;
81 }
82 
83 /* *INDENT-OFF* */
84 VLIB_CLI_COMMAND (show_node_graph_command, static) = {
85  .path = "show vlib graph",
86  .short_help = "Show packet processing node graph",
87  .function = show_node_graph,
88 };
89 /* *INDENT-ON* */
90 
91 static u8 *
92 format_vlib_node_stats (u8 * s, va_list * va)
93 {
94  vlib_main_t *vm = va_arg (*va, vlib_main_t *);
95  vlib_node_t *n = va_arg (*va, vlib_node_t *);
96  int max = va_arg (*va, int);
97  f64 v;
98  char *state;
99  u8 *ns;
100  u8 *misc_info = 0;
101  u64 c, p, l, d;
102  f64 x;
103  f64 maxc, maxcn;
104  u32 maxn;
105  uword indent;
106 
107  if (!n)
108  {
109  if (max)
110  return format (s,
111  "%=30s%=17s%=16s%=16s%=16s%=16s",
112  "Name", "Max Node Clocks", "Vectors at Max",
113  "Max Clocks", "Avg Clocks", "Avg Vectors/Call");
114  else
115  return format (s,
116  "%=30s%=12s%=16s%=16s%=16s%=16s%=16s",
117  "Name", "State", "Calls", "Vectors", "Suspends",
118  "Clocks", "Vectors/Call");
119  }
120 
121  indent = format_get_indent (s);
122 
127  maxc = (f64) n->stats_total.max_clock;
128  maxn = n->stats_total.max_clock_n;
129  if (n->stats_total.max_clock_n)
130  maxcn = (f64) n->stats_total.max_clock / (f64) maxn;
131  else
132  maxcn = 0.0;
133 
134  /* Clocks per packet, per call or per suspend. */
135  x = 0;
136  if (p > 0)
137  x = (f64) l / (f64) p;
138  else if (c > 0)
139  x = (f64) l / (f64) c;
140  else if (d > 0)
141  x = (f64) l / (f64) d;
142 
143  if (c > 0)
144  v = (double) p / (double) c;
145  else
146  v = 0;
147 
148  state = "active";
149  if (n->type == VLIB_NODE_TYPE_PROCESS)
150  {
152 
153  /* Show processes with events pending. This helps spot bugs where events are not
154  being handled. */
156  misc_info = format (misc_info, "events pending, ");
157 
160  {
161  default:
162  if (!(p->flags & VLIB_PROCESS_IS_RUNNING))
163  state = "done";
164  break;
165 
167  state = "time wait";
168  break;
169 
171  state = "event wait";
172  break;
173 
175  state =
176  "any wait";
177  break;
178  }
179  }
180  else if (n->type != VLIB_NODE_TYPE_INTERNAL)
181  {
182  state = "polling";
183  if (n->state == VLIB_NODE_STATE_DISABLED)
184  state = "disabled";
185  else if (n->state == VLIB_NODE_STATE_INTERRUPT)
186  state = "interrupt wait";
187  }
188 
189  ns = n->name;
190 
191  if (max)
192  s = format (s, "%-30v%=17.2e%=16d%=16.2e%=16.2e%=16.2e",
193  ns, maxc, maxn, maxcn, x, v);
194  else
195  s = format (s, "%-30v%=12s%16Ld%16Ld%16Ld%16.2e%16.2f", ns, state,
196  c, p, d, x, v);
197 
198  if (ns != n->name)
199  vec_free (ns);
200 
201  if (misc_info)
202  {
203  s = format (s, "\n%U%v", format_white_space, indent + 4, misc_info);
204  vec_free (misc_info);
205  }
206 
207  return s;
208 }
209 
210 static clib_error_t *
212  unformat_input_t * input, vlib_cli_command_t * cmd)
213 {
214  vlib_node_main_t *nm = &vm->node_main;
215  vlib_node_t *n;
216  f64 time_now;
217  u32 node_index;
218  vlib_node_t ***node_dups = 0;
219  f64 *vectors_per_main_loop = 0;
220  f64 *last_vector_length_per_node = 0;
221 
222  time_now = vlib_time_now (vm);
223 
224  if (unformat (input, "%U", unformat_vlib_node, vm, &node_index))
225  {
226  n = vlib_get_node (vm, node_index);
227  vlib_node_sync_stats (vm, n);
228  vlib_cli_output (vm, "%U\n", format_vlib_node_stats, vm, 0, 0);
229  vlib_cli_output (vm, "%U\n", format_vlib_node_stats, vm, n, 0);
230  }
231  else
232  {
233  vlib_node_t **nodes;
234  uword i, j;
235  f64 dt;
236  u64 n_input, n_output, n_drop, n_punt;
237  u64 n_internal_vectors, n_internal_calls;
238  u64 n_clocks, l, v, c, d;
239  int brief = 1;
240  int max = 0;
241  vlib_main_t **stat_vms = 0, *stat_vm;
242 
243  /* Suppress nodes with zero calls since last clear */
244  if (unformat (input, "brief") || unformat (input, "b"))
245  brief = 1;
246  if (unformat (input, "verbose") || unformat (input, "v"))
247  brief = 0;
248  if (unformat (input, "max") || unformat (input, "m"))
249  max = 1;
250 
251  if (vec_len (vlib_mains) == 0)
252  vec_add1 (stat_vms, vm);
253  else
254  {
255  for (i = 0; i < vec_len (vlib_mains); i++)
256  {
257  stat_vm = vlib_mains[i];
258  if (stat_vm)
259  vec_add1 (stat_vms, stat_vm);
260  }
261  }
262 
263  /*
264  * Barrier sync across stats scraping.
265  * Otherwise, the counts will be grossly inaccurate.
266  */
268 
269  for (j = 0; j < vec_len (stat_vms); j++)
270  {
271  stat_vm = stat_vms[j];
272  nm = &stat_vm->node_main;
273 
274  for (i = 0; i < vec_len (nm->nodes); i++)
275  {
276  n = nm->nodes[i];
277  vlib_node_sync_stats (stat_vm, n);
278  }
279 
280  nodes = vec_dup (nm->nodes);
281 
282  vec_add1 (node_dups, nodes);
283  vec_add1 (vectors_per_main_loop,
285  vec_add1 (last_vector_length_per_node,
287  }
289 
290 
291  for (j = 0; j < vec_len (stat_vms); j++)
292  {
293  stat_vm = stat_vms[j];
294  nodes = node_dups[j];
295 
297 
298  n_input = n_output = n_drop = n_punt = n_clocks = 0;
299  n_internal_vectors = n_internal_calls = 0;
300  for (i = 0; i < vec_len (nodes); i++)
301  {
302  n = nodes[i];
303 
305  n_clocks += l;
306 
309 
310  switch (n->type)
311  {
312  default:
313  continue;
314 
316  n_output += (n->flags & VLIB_NODE_FLAG_IS_OUTPUT) ? v : 0;
317  n_drop += (n->flags & VLIB_NODE_FLAG_IS_DROP) ? v : 0;
318  n_punt += (n->flags & VLIB_NODE_FLAG_IS_PUNT) ? v : 0;
319  if (!(n->flags & VLIB_NODE_FLAG_IS_OUTPUT))
320  {
321  n_internal_vectors += v;
322  n_internal_calls += c;
323  }
325  n_input += v;
326  break;
327 
329  n_input += v;
330  break;
331  }
332  }
333 
334  if (vec_len (vlib_mains))
335  {
337  if (j > 0)
338  vlib_cli_output (vm, "---------------");
339 
340  if (w->lcore_id > -1)
341  vlib_cli_output (vm, "Thread %d %s (lcore %u)", j, w->name,
342  w->lcore_id);
343  else
344  vlib_cli_output (vm, "Thread %d %s", j, w->name);
345  }
346 
347  dt = time_now - nm->time_last_runtime_stats_clear;
349  (vm,
350  "Time %.1f, average vectors/node %.2f, last %d main loops %.2f per node %.2f"
351  "\n vector rates in %.4e, out %.4e, drop %.4e, punt %.4e",
352  dt,
353  (n_internal_calls > 0
354  ? (f64) n_internal_vectors / (f64) n_internal_calls
355  : 0),
357  vectors_per_main_loop[j],
358  last_vector_length_per_node[j],
359  (f64) n_input / dt,
360  (f64) n_output / dt, (f64) n_drop / dt, (f64) n_punt / dt);
361 
362  vlib_cli_output (vm, "%U", format_vlib_node_stats, stat_vm, 0, max);
363  for (i = 0; i < vec_len (nodes); i++)
364  {
365  c =
366  nodes[i]->stats_total.calls -
367  nodes[i]->stats_last_clear.calls;
368  d =
369  nodes[i]->stats_total.suspends -
370  nodes[i]->stats_last_clear.suspends;
371  if (c || d || !brief)
372  {
373  vlib_cli_output (vm, "%U", format_vlib_node_stats, stat_vm,
374  nodes[i], max);
375  }
376  }
377  vec_free (nodes);
378  }
379  vec_free (stat_vms);
380  vec_free (node_dups);
381  vec_free (vectors_per_main_loop);
382  vec_free (last_vector_length_per_node);
383  }
384 
385  return 0;
386 }
387 
388 /* *INDENT-OFF* */
389 VLIB_CLI_COMMAND (show_node_runtime_command, static) = {
390  .path = "show runtime",
391  .short_help = "Show packet processing runtime",
392  .function = show_node_runtime,
393  .is_mp_safe = 1,
394 };
395 /* *INDENT-ON* */
396 
397 static clib_error_t *
399  unformat_input_t * input, vlib_cli_command_t * cmd)
400 {
401  vlib_node_main_t *nm;
402  vlib_node_t *n;
403  int i, j;
404  vlib_main_t **stat_vms = 0, *stat_vm;
406 
407  if (vec_len (vlib_mains) == 0)
408  vec_add1 (stat_vms, vm);
409  else
410  {
411  for (i = 0; i < vec_len (vlib_mains); i++)
412  {
413  stat_vm = vlib_mains[i];
414  if (stat_vm)
415  vec_add1 (stat_vms, stat_vm);
416  }
417  }
418 
420 
421  for (j = 0; j < vec_len (stat_vms); j++)
422  {
423  stat_vm = stat_vms[j];
424  nm = &stat_vm->node_main;
425 
426  for (i = 0; i < vec_len (nm->nodes); i++)
427  {
428  n = nm->nodes[i];
429  vlib_node_sync_stats (stat_vm, n);
431 
432  r = vlib_node_get_runtime (stat_vm, n->index);
433  r->max_clock = 0;
434  }
435  /* Note: input/output rates computed using vlib_global_main */
437  }
438 
440 
441  vec_free (stat_vms);
442 
443  return 0;
444 }
445 
446 /* *INDENT-OFF* */
447 VLIB_CLI_COMMAND (clear_node_runtime_command, static) = {
448  .path = "clear runtime",
449  .short_help = "Clear packet processing runtime statistics",
450  .function = clear_node_runtime,
451 };
452 /* *INDENT-ON* */
453 
454 /* Dummy function to get us linked in. */
455 void
457 {
458 }
459 
460 /*
461  * fd.io coding-style-patch-verification: ON
462  *
463  * Local Variables:
464  * eval: (c-set-style "gnu")
465  * End:
466  */
static f64 vlib_last_vectors_per_main_loop_as_f64(vlib_main_t *vm)
Definition: main.h:264
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:966
u32 index
Definition: node.h:237
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:182
u16 flags
Definition: node.h:246
static int node_cmp(void *a1, void *a2)
Definition: node_cli.c:44
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:482
u8 state
Definition: node.h:265
static f64 vlib_last_vector_length_per_node(vlib_main_t *vm)
Definition: main.h:273
vlib_node_stats_t stats_last_clear
Definition: node.h:231
static clib_error_t * show_node_runtime(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: node_cli.c:211
#define VLIB_NODE_FLAG_IS_DROP
Definition: node.h:254
static uword clib_bitmap_is_zero(uword *ai)
predicate function; is an entire bitmap empty?
Definition: bitmap.h:57
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:113
vlib_node_t ** nodes
Definition: node.h:630
#define VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE
Definition: main.h:84
unsigned long u64
Definition: types.h:89
u64 max_clock_n
Definition: node.h:195
static u8 * format_vlib_node_stats(u8 *s, va_list *va)
Definition: node_cli.c:92
vlib_worker_thread_t * vlib_worker_threads
Definition: threads.c:54
vlib_node_stats_t stats_total
Definition: node.h:227
static uword format_get_indent(u8 *s)
Definition: format.h:72
#define VLIB_NODE_FLAG_IS_HANDOFF
Definition: node.h:256
f64 time_last_runtime_stats_clear
Definition: node.h:691
#define v
Definition: acl.c:314
#define vec_dup(V)
Return copy of vector (no header, no alignment)
Definition: vec.h:334
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:576
u8 * name
Definition: node.h:221
svmdb_client_t * c
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:300
static vlib_node_runtime_t * vlib_node_get_runtime(vlib_main_t *vm, u32 node_index)
Get node runtime by node index.
Definition: node_funcs.h:88
void vlib_worker_thread_barrier_sync(vlib_main_t *vm)
Definition: threads.c:1163
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
#define VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT
Definition: node.h:506
unsigned int u32
Definition: types.h:88
#define VLIB_NODE_FLAG_IS_PUNT
Definition: node.h:255
static clib_error_t * clear_node_runtime(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: node_cli.c:398
void vlib_node_cli_reference(void)
Definition: node_cli.c:456
vhost_vring_state_t state
Definition: vhost-user.h:80
#define vec_cmp(v1, v2)
Compare two vectors (only applicable to vectors of signed numbers).
Definition: vec.h:880
u64 uword
Definition: types.h:112
#define VLIB_NODE_FLAG_IS_OUTPUT
Definition: node.h:253
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
double f64
Definition: types.h:142
unsigned char u8
Definition: types.h:56
vlib_node_main_t node_main
Definition: main.h:115
#define vec_sort_with_function(vec, f)
Sort a vector using the supplied element comparison function.
Definition: vec.h:920
static clib_error_t * show_node_graph(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: node_cli.c:53
vlib_main_t ** vlib_mains
#define VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK
Definition: node.h:505
unformat_function_t unformat_vlib_node
Definition: node_funcs.h:1110
void vlib_node_sync_stats(vlib_main_t *vm, vlib_node_t *n)
Definition: main.c:575
u16 flags
Definition: node.h:504
vlib_node_type_t type
Definition: node.h:234
static vlib_process_t * vlib_get_process_from_node(vlib_main_t *vm, vlib_node_t *node)
Definition: node_funcs.h:190
void vlib_worker_thread_barrier_release(vlib_main_t *vm)
Definition: threads.c:1195
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
static vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Get vlib node by index.
Definition: node_funcs.h:58
struct _unformat_input_t unformat_input_t
format_function_t format_vlib_node_graph
Definition: node_funcs.h:1103
#define VLIB_PROCESS_IS_RUNNING
Definition: node.h:511
uword * non_empty_event_type_bitmap
Definition: node.h:525