Initial commit of vpp code.
[vpp.git] / vlib / vlib / node_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  * 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 clib_error_t *
44 show_node_graph (vlib_main_t * vm,
45                  unformat_input_t * input,
46                  vlib_cli_command_t * cmd)
47 {
48   vlib_node_main_t * nm = &vm->node_main;
49   vlib_node_t * n;
50   u32 node_index;
51
52   vlib_cli_output (vm, "%U\n", format_vlib_node_graph, nm, 0);
53
54   if (unformat (input, "%U", unformat_vlib_node, vm, &node_index))
55     {
56       n = vlib_get_node (vm, node_index);
57       vlib_cli_output (vm, "%U\n", format_vlib_node_graph, nm, n);
58     }
59   else
60     {
61       vlib_node_t ** nodes = vec_dup (nm->nodes);
62       uword i;
63
64       vec_sort (nodes, n1, n2,
65                 vec_cmp (n1[0]->name, n2[0]->name));
66
67       for (i = 0; i < vec_len (nodes); i++)
68         vlib_cli_output (vm, "%U\n\n", format_vlib_node_graph, nm, nodes[i]);
69
70       vec_free (nodes);
71     }
72
73   return 0;
74 }
75
76 VLIB_CLI_COMMAND (show_node_graph_command, static) = {
77   .path = "show vlib graph",
78   .short_help = "Show packet processing node graph",
79   .function = show_node_graph,
80 };
81
82 static u8 * format_vlib_node_stats (u8 * s, va_list * va)
83 {
84   vlib_main_t * vm = va_arg (*va, vlib_main_t *);
85   vlib_node_t * n = va_arg (*va, vlib_node_t *);
86   int max = va_arg (*va, int);
87   f64 v;
88   char * state;
89   u8 * ns;
90   u8 * misc_info = 0;
91   u64 c, p, l, d;
92   f64 x;
93   f64 maxc, maxcn; 
94   u32 maxn;
95   uword indent;
96
97   if (! n)
98     {
99       if (max) 
100         return format (s,
101             "%=30s%=17s%=16s%=16s%=16s%=16s",
102             "Name", "Max Node Clocks", "Vectors at Max", "Max Clocks", "Avg Clocks", "Avg Vectors/Call");
103       else
104         return format (s,
105             "%=30s%=12s%=16s%=16s%=16s%=16s%=16s",
106             "Name", "State", "Calls", "Vectors", "Suspends", "Clocks", "Vectors/Call");
107     }
108
109   indent = format_get_indent (s);
110
111   l = n->stats_total.clocks - n->stats_last_clear.clocks;
112   c = n->stats_total.calls - n->stats_last_clear.calls;
113   p = n->stats_total.vectors - n->stats_last_clear.vectors;
114   d = n->stats_total.suspends - n->stats_last_clear.suspends;
115   maxc = (f64)n->stats_total.max_clock;
116   maxn = n->stats_total.max_clock_n;
117   if (n->stats_total.max_clock_n) 
118     maxcn = (f64)n->stats_total.max_clock / (f64)maxn;
119   else 
120     maxcn = 0.0;
121
122   /* Clocks per packet, per call or per suspend. */
123   x = 0;
124   if (p > 0)
125     x = (f64) l / (f64) p;
126   else if (c > 0)
127     x = (f64) l / (f64) c;
128   else if (d > 0)
129     x = (f64) l / (f64) d;
130     
131   if (c > 0)
132     v = (double)p / (double)c;
133   else
134     v = 0;
135
136   state = "active";
137   if (n->type == VLIB_NODE_TYPE_PROCESS)
138     {
139       vlib_process_t * p = vlib_get_process_from_node (vm, n);
140
141       /* Show processes with events pending.  This helps spot bugs where events are not
142          being handled. */
143       if (! clib_bitmap_is_zero (p->non_empty_event_type_bitmap))
144         misc_info = format (misc_info, "events pending, ");
145
146       switch (p->flags & (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK
147                           | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT))
148         {
149         default:
150           if (! (p->flags & VLIB_PROCESS_IS_RUNNING))
151             state = "done";
152           break;
153
154         case VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK:
155           state = "time wait";
156           break;
157
158         case VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT:
159           state = "event wait";
160           break;
161
162         case (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT
163               | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK):
164           state = "any wait";
165           break;
166         }
167     }      
168   else if (n->type != VLIB_NODE_TYPE_INTERNAL)
169     {
170       state = "polling";
171       if (n->state == VLIB_NODE_STATE_DISABLED)
172         state = "disabled";
173       else if (n->state == VLIB_NODE_STATE_INTERRUPT)
174         state = "interrupt wait";
175     }
176
177   ns = n->name;
178
179   if (max)
180     s = format (s, "%-30v%=17.2e%=16d%=16.2e%=16.2e%=16.2e",
181                    ns, maxc, maxn, maxcn, x, v);
182   else
183     s = format (s, "%-30v%=12s%16Ld%16Ld%16Ld%16.2e%16.2f", ns, state,
184                 c, p, d, x, v);
185
186   if (ns != n->name)
187     vec_free (ns);
188
189   if (misc_info)
190     {
191       s = format (s, "\n%U%v", format_white_space, indent + 4, misc_info);
192       vec_free (misc_info);
193     }
194
195   return s;
196 }
197
198 static clib_error_t *
199 show_node_runtime (vlib_main_t * vm,
200                    unformat_input_t * input,
201                    vlib_cli_command_t * cmd)
202 {
203   vlib_node_main_t * nm = &vm->node_main;
204   vlib_node_t * n;
205   f64 time_now;
206   u32 node_index;
207   vlib_node_t *** node_dups = 0;
208   f64 * vectors_per_main_loop = 0;
209   f64 * last_vector_length_per_node = 0;
210
211   time_now = vlib_time_now (vm);
212
213   if (unformat (input, "%U", unformat_vlib_node, vm, &node_index))
214     {
215       n = vlib_get_node (vm, node_index);
216       vlib_node_sync_stats (vm, n);
217       vlib_cli_output (vm, "%U\n", format_vlib_node_stats, vm, 0, 0);
218       vlib_cli_output (vm, "%U\n", format_vlib_node_stats, vm, n, 0);
219     }
220   else
221     {
222       vlib_node_t ** nodes;
223       uword i, j;
224       f64 dt;
225       u64 n_input, n_output, n_drop, n_punt;
226       u64 n_internal_vectors, n_internal_calls;
227       u64 n_clocks, l, v, c, d;
228       int brief = 1;
229       int max = 0;
230       vlib_main_t ** stat_vms = 0, *stat_vm;
231
232       /* Suppress nodes with zero calls since last clear */
233       if (unformat (input, "brief") || unformat (input, "b"))
234         brief = 1;
235       if (unformat (input, "verbose") || unformat(input, "v"))
236         brief = 0;
237       if (unformat (input, "max") || unformat(input, "m"))
238         max = 1;
239
240       if (vec_len(vlib_mains) == 0)
241         vec_add1 (stat_vms, vm);
242       else
243         {
244           for (i = 0; i < vec_len (vlib_mains); i++)
245             {
246               stat_vm = vlib_mains[i];
247               if (stat_vm)
248                 vec_add1 (stat_vms, stat_vm);
249             }
250         }
251
252       /* 
253        * Barrier sync across stats scraping.
254        * Otherwise, the counts will be grossly inaccurate.
255        */
256       vlib_worker_thread_barrier_sync(vm);
257
258       for (j = 0; j < vec_len (stat_vms); j++)
259         {
260           stat_vm = stat_vms[j];
261           nm = &stat_vm->node_main;
262
263           for (i = 0; i < vec_len (nm->nodes); i++)
264             {
265               n = nm->nodes[i];
266               vlib_node_sync_stats (stat_vm, n);
267             }
268
269           nodes = vec_dup (nm->nodes);
270
271           vec_add1(node_dups, nodes);
272           vec_add1 (vectors_per_main_loop, 
273                     vlib_last_vectors_per_main_loop_as_f64 (stat_vm));
274           vec_add1 (last_vector_length_per_node, 
275                     vlib_last_vector_length_per_node (stat_vm));
276         }
277       vlib_worker_thread_barrier_release(vm);
278
279
280       for (j = 0; j < vec_len (stat_vms); j++)
281         {
282           stat_vm = stat_vms[j];
283           nodes = node_dups[j];
284
285           vec_sort (nodes, n1, n2,
286                     vec_cmp (n1[0]->name, n2[0]->name));
287
288           n_input = n_output = n_drop = n_punt = n_clocks = 0;
289           n_internal_vectors = n_internal_calls = 0;
290           for (i = 0; i < vec_len (nodes); i++)
291             {
292               n = nodes[i];
293
294               l = n->stats_total.clocks - n->stats_last_clear.clocks;
295               n_clocks += l;
296
297               v = n->stats_total.vectors - n->stats_last_clear.vectors;
298               c = n->stats_total.calls - n->stats_last_clear.calls;
299
300               switch (n->type)
301                 {
302                 default:
303                   continue;
304
305                 case VLIB_NODE_TYPE_INTERNAL:
306                   n_output += (n->flags & VLIB_NODE_FLAG_IS_OUTPUT) ? v : 0;
307                   n_drop += (n->flags & VLIB_NODE_FLAG_IS_DROP) ? v : 0;
308                   n_punt += (n->flags & VLIB_NODE_FLAG_IS_PUNT) ? v : 0;
309                   if (! (n->flags & VLIB_NODE_FLAG_IS_OUTPUT))
310                     {
311                       n_internal_vectors += v;
312                       n_internal_calls += c;
313                     }
314                   if (n->flags & VLIB_NODE_FLAG_IS_HANDOFF)
315                       n_input += v;
316                   break;
317
318                 case VLIB_NODE_TYPE_INPUT:
319                   n_input += v;
320                   break;
321                 }
322             }
323
324           if (vec_len (vlib_mains))
325             {
326               vlib_worker_thread_t *w = vlib_worker_threads + j;
327               if (j > 0)
328                 vlib_cli_output (vm, "---------------");
329
330               if ( w->dpdk_lcore_id > -1)
331                 vlib_cli_output (vm, "Thread %d %v (lcore %u)", j, w->name,
332                                  w->dpdk_lcore_id);
333               else
334                 vlib_cli_output (vm, "Thread %d %v", j,
335                                  w->name);
336             }
337
338           dt = time_now - nm->time_last_runtime_stats_clear;
339           vlib_cli_output
340             (vm,
341              "Time %.1f, average vectors/node %.2f, last %d main loops %.2f per node %.2f"
342              "\n  vector rates in %.4e, out %.4e, drop %.4e, punt %.4e",
343              dt,
344              (n_internal_calls > 0
345               ? (f64) n_internal_vectors / (f64) n_internal_calls
346               : 0),
347              1 << VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE,
348              vectors_per_main_loop [j],
349              last_vector_length_per_node [j],
350              (f64) n_input / dt,
351              (f64) n_output / dt,
352              (f64) n_drop / dt,
353              (f64) n_punt / dt);
354
355           vlib_cli_output (vm, "%U", format_vlib_node_stats, stat_vm, 0, max);
356           for (i = 0; i < vec_len (nodes); i++)
357             {
358               c = nodes[i]->stats_total.calls - nodes[i]->stats_last_clear.calls;
359               d = nodes[i]->stats_total.suspends - nodes[i]->stats_last_clear.suspends;
360               if (c || d || ! brief)
361               {
362                 vlib_cli_output (vm, "%U", format_vlib_node_stats, stat_vm, 
363                                  nodes[i], max);
364               }
365             }
366           vec_free (nodes);
367         }
368       vec_free (stat_vms);
369       vec_free (node_dups);
370       vec_free (vectors_per_main_loop);
371       vec_free (last_vector_length_per_node);
372     }
373
374   return 0;
375 }
376
377 VLIB_CLI_COMMAND (show_node_runtime_command, static) = {
378   .path = "show runtime",
379   .short_help = "Show packet processing runtime",
380   .function = show_node_runtime,
381   .is_mp_safe = 1,
382 };
383
384 static clib_error_t *
385 clear_node_runtime (vlib_main_t * vm,
386                     unformat_input_t * input,
387                     vlib_cli_command_t * cmd)
388 {
389   vlib_node_main_t * nm;
390   vlib_node_t * n;
391   int i, j;
392   vlib_main_t ** stat_vms = 0, *stat_vm;
393   vlib_node_runtime_t * r;
394
395   if (vec_len(vlib_mains) == 0)
396     vec_add1 (stat_vms, vm);
397   else
398     {
399       for (i = 0; i < vec_len (vlib_mains); i++)
400         {
401           stat_vm = vlib_mains[i];
402           if (stat_vm)
403             vec_add1 (stat_vms, stat_vm);
404         }
405     }
406   
407   vlib_worker_thread_barrier_sync(vm);
408
409   for (j = 0; j < vec_len (stat_vms); j++)
410     {
411       stat_vm = stat_vms[j];
412       nm = &stat_vm->node_main;
413       
414       for (i = 0; i < vec_len (nm->nodes); i++)
415         {
416           n = nm->nodes[i];
417           vlib_node_sync_stats (stat_vm, n);
418           n->stats_last_clear = n->stats_total;
419
420           r = vlib_node_get_runtime (stat_vm, n->index);
421           r->max_clock = 0;
422         }
423       /* Note: input/output rates computed using vlib_global_main */
424       nm->time_last_runtime_stats_clear = vlib_time_now (vm);
425     }
426
427   vlib_worker_thread_barrier_release(vm);
428       
429   vec_free (stat_vms);
430
431   return 0;
432 }
433
434 VLIB_CLI_COMMAND (clear_node_runtime_command, static) = {
435   .path = "clear runtime",
436   .short_help = "Clear packet processing runtime statistics",
437   .function = clear_node_runtime,
438 };
439
440 /* Dummy function to get us linked in. */
441 void vlib_node_cli_reference (void) {}