From 156158f06d725d9301940783dff8ccdcc4a01b9b Mon Sep 17 00:00:00 2001 From: Arthur de Kerhor Date: Thu, 18 Feb 2021 03:09:42 -0800 Subject: vlib: graphviz upgrade to allow filters Possibility to draw only the active nodes on the graph. These are scaled and colored according to their utilization. Type: improvement Signed-off-by: Arthur de Kerhor Change-Id: I7ddb7b62b3a141cb03750dca24f044138fcc577f --- src/vlib/main.c | 18 +++-- src/vlib/main.h | 1 - src/vlib/node_cli.c | 210 ++++++++++++++++++++++++++++++++++++++++++++------ src/vlib/node_funcs.h | 6 ++ src/vlib/threads.c | 5 -- 5 files changed, 206 insertions(+), 34 deletions(-) diff --git a/src/vlib/main.c b/src/vlib/main.c index 02fdc89ad99..41f74b9bdf6 100644 --- a/src/vlib/main.c +++ b/src/vlib/main.c @@ -570,13 +570,11 @@ vlib_put_next_frame (vlib_main_t * vm, } /* Sync up runtime (32 bit counters) and main node stats (64 bit counters). */ -never_inline void -vlib_node_runtime_sync_stats (vlib_main_t * vm, - vlib_node_runtime_t * r, - uword n_calls, uword n_vectors, uword n_clocks) +void +vlib_node_runtime_sync_stats_node (vlib_node_t *n, vlib_node_runtime_t *r, + uword n_calls, uword n_vectors, + uword n_clocks) { - vlib_node_t *n = vlib_get_node (vm, r->node_index); - n->stats_total.calls += n_calls + r->calls_since_last_overflow; n->stats_total.vectors += n_vectors + r->vectors_since_last_overflow; n->stats_total.clocks += n_clocks + r->clocks_since_last_overflow; @@ -588,6 +586,14 @@ vlib_node_runtime_sync_stats (vlib_main_t * vm, r->clocks_since_last_overflow = 0; } +void +vlib_node_runtime_sync_stats (vlib_main_t *vm, vlib_node_runtime_t *r, + uword n_calls, uword n_vectors, uword n_clocks) +{ + vlib_node_t *n = vlib_get_node (vm, r->node_index); + vlib_node_runtime_sync_stats_node (n, r, n_calls, n_vectors, n_clocks); +} + always_inline void __attribute__ ((unused)) vlib_process_sync_stats (vlib_main_t * vm, vlib_process_t * p, diff --git a/src/vlib/main.h b/src/vlib/main.h index 3e3590abab0..860192c1aa3 100644 --- a/src/vlib/main.h +++ b/src/vlib/main.h @@ -450,7 +450,6 @@ extern u8 **vlib_thread_stacks; /* Number of thread stacks that the application needs */ u32 vlib_app_num_thread_stacks_needed (void) __attribute__ ((weak)); -extern void vlib_node_sync_stats (vlib_main_t * vm, vlib_node_t * n); void vlib_add_del_post_mortem_callback (void *cb, int is_add); vlib_main_t *vlib_get_main_not_inline (void); diff --git a/src/vlib/node_cli.c b/src/vlib/node_cli.c index 1c0780517b5..39fca6ea582 100644 --- a/src/vlib/node_cli.c +++ b/src/vlib/node_cli.c @@ -42,6 +42,7 @@ #include #include #include +#include static int node_cmp (void *a1, void *a2) @@ -97,21 +98,42 @@ show_node_graphviz (vlib_main_t * vm, { clib_error_t *error = 0; vlib_node_main_t *nm = &vm->node_main; + vlib_node_t **nodes = nm->nodes; u8 *chroot_filename = 0; int fd; - vlib_node_t **nodes = 0; - uword i, j; + uword *active = 0; + u32 i, j; + unformat_input_t _line_input, *line_input = &_line_input; + u8 filter = 0, calls_filter = 0, vectors_filter = 0, both = 0; - if (!unformat_user (input, unformat_vlib_tmpfile, &chroot_filename)) - { - fd = -1; - } - else + fd = -1; + /* Get a line of input. */ + if (unformat_user (input, unformat_line_input, line_input)) { - fd = - open ((char *) chroot_filename, O_CREAT | O_TRUNC | O_WRONLY, 0664); + while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) + { + if (unformat (line_input, "filter")) + filter = 1; + else if (unformat (line_input, "calls") && filter) + calls_filter = 1; + else if (unformat (line_input, "vectors") && filter) + vectors_filter = 1; + else if (unformat (line_input, "file %U", unformat_vlib_tmpfile, + &chroot_filename)) + { + fd = open ((char *) chroot_filename, + O_CREAT | O_TRUNC | O_WRONLY, 0664); + } + else + return clib_error_return (0, "unknown input `%U'", + format_unformat_error, input); + } + unformat_free (line_input); } + /*both is set to true if calls_filter and vectors_filter are, or neither */ + both = filter & (!(calls_filter ^ vectors_filter)); + #define format__(vm__, fd__, ...) \ if ((fd) < 0) \ { \ @@ -124,45 +146,189 @@ show_node_graphviz (vlib_main_t * vm, format__ (vm, fd, "%s", "digraph {\n"); - nodes = vec_dup (nm->nodes); - vec_sort_with_function (nodes, node_cmp); + clib_bitmap_alloc (active, vec_len (nodes)); + clib_bitmap_set_region (active, 0, 1, vec_len (nodes)); + if (filter) + { + /*Adding the legend to the dot file*/ + format__ (vm, fd, "%s", + " rankdir=\"LR\"\n nodesep=2\n subgraph cluster_legend {\n " + " label=\"Legend\"\n style=\"solid\"\n labelloc = b\n " + " subgraph cluster_colors {\n label=\"Packets/Call\"\n " + " style=\"solid\"\n labelloc = b\n"); + format__ (vm, fd, "%s", + " 0 [label=\"No packet\", fixedsize=true shape=circle " + "width=2 fontsize=17]\n" + " 1 [label=\"1-32\", fillcolor=1 style=filled " + "colorscheme=ylorrd8 fixedsize=true shape=circle width=2 " + "fontsize=17]\n" + " 2 [label=\"33-64\", fillcolor=2 style=filled " + "colorscheme=ylorrd8 fixedsize=true shape=circle width=2 " + "fontsize=17]\n" + " 3 [label=\"65-96\", fillcolor=3 style=filled " + "colorscheme=ylorrd8 fixedsize=true shape=circle width=2 " + "fontsize=17]\n" + " 4 [label=\"97-128\", fillcolor=4 style=filled " + "colorscheme=ylorrd8 fixedsize=true shape=circle width=2 " + "fontsize=17]\n" + " 5 [label=\"129-160\", fillcolor=5 style=filled " + "colorscheme=ylorrd8 fixedsize=true shape=circle width=2 " + "fontsize=17]\n" + " 6 [label=\"161-192\", fillcolor=6 style=filled " + "colorscheme=ylorrd8 fixedsize=true shape=circle width=2 " + "fontsize=17]\n" + " 7 [label=\"193-224\", fillcolor=7 style=filled " + "colorscheme=ylorrd8 fixedsize=true shape=circle width=2 " + "fontsize=17]\n" + " 8 [label=\"224+\", fillcolor=8 style=filled " + "colorscheme=ylorrd8 fixedsize=true shape=circle width=2 " + "fontsize=17]\n"); + format__ (vm, fd, "%s", + " 0 -> 1 -> 2 -> 3 -> 4 [style=\"invis\",weight =100]\n " + " 5 -> 6 -> 7 -> 8 [style=\"invis\",weight =100]\n }\n " + " subgraph cluster_size {\n label=\"Cycles/Packet\"\n " + " style=\"solid\"\n labelloc = b\n"); + format__ ( + vm, fd, "%s", + " a[label=\"0\",fixedsize=true shape=circle width=1] \n" + " b[label=\"10\",fixedsize=true shape=circle width=2 " + "fontsize=17]\n" + " c[label=\"100\",fixedsize=true shape=circle width=3 " + "fontsize=20]\n" + " d[label=\"1000\",fixedsize=true shape=circle width=4 " + "fontsize=23]\n" + " a -> b -> c -> d [style=\"invis\",weight =100]\n }\n }\n"); - for (i = 0; i < vec_len (nodes); i++) + vlib_worker_thread_barrier_sync (vm); + for (j = 0; j < vec_len (nm->nodes); j++) + { + vlib_node_t *n; + n = nm->nodes[j]; + vlib_node_sync_stats (vm, n); + } + + /* Updating the stats for multithreaded use cases. + * We need to dup the nodes to sum the stats from all threads.*/ + nodes = vec_dup (nm->nodes); + for (i = 1; i < vec_len (vlib_mains); i++) + { + vlib_node_main_t *nm_clone; + vlib_main_t *vm_clone; + vlib_node_runtime_t *rt; + vlib_node_t *n; + + vm_clone = vlib_mains[i]; + nm_clone = &vm_clone->node_main; + + for (j = 0; j < vec_len (nm_clone->nodes); j++) + { + n = nm_clone->nodes[j]; + + rt = vlib_node_get_runtime (vm_clone, n->index); + /* Sync the stats directly in the duplicated node.*/ + vlib_node_runtime_sync_stats_node (nodes[j], rt, 0, 0, 0); + } + } + vlib_worker_thread_barrier_release (vm); + + for (i = 0; i < vec_len (nodes); i++) + { + u64 p, c, l; + c = nodes[i]->stats_total.calls - nodes[i]->stats_last_clear.calls; + p = + nodes[i]->stats_total.vectors - nodes[i]->stats_last_clear.vectors; + l = nodes[i]->stats_total.clocks - nodes[i]->stats_last_clear.clocks; + + if ((both && c > 0 && p > 0) || (calls_filter && c > 0) || + (vectors_filter && p > 0)) + { + format__ (vm, fd, " \"%v\" [shape=circle", nodes[i]->name); + /*Changing the size and the font of nodes that receive packets*/ + if (p > 0) + { + f64 x = (f64) l / (f64) p; + f64 size_ratio = (1 + log10 (x + 1)); + format__ (vm, fd, " width=%.2f fontsize=%.2f fixedsize=true", + size_ratio, 11 + 3 * size_ratio); + /*Coloring nodes that are indeed called*/ + if (c > 0) + { + u64 color = ((p - 1) / (32 * c)) + 1; + color = clib_min (color, 8); + format__ ( + vm, fd, + " fillcolor=%u style=filled colorscheme=ylorrd8", + color); + } + } + format__ (vm, fd, "]\n"); + } + else + { + clib_bitmap_set (active, i, 0); + } + } + } + + clib_bitmap_foreach (i, active) { for (j = 0; j < vec_len (nodes[i]->next_nodes); j++) { - vlib_node_t *x; - if (nodes[i]->next_nodes[j] == VLIB_INVALID_NODE_INDEX) continue; - x = vec_elt (nm->nodes, nodes[i]->next_nodes[j]); - format__ (vm, fd, " \"%v\" -> \"%v\"\n", nodes[i]->name, x->name); + if (!filter || clib_bitmap_get (active, nodes[i]->next_nodes[j])) + { + format__ (vm, fd, " \"%v\" -> \"%v\"\n", nodes[i]->name, + nodes[nodes[i]->next_nodes[j]]->name); + } } } - format__ (vm, fd, "%s", "}"); + format__ (vm, fd, "}\n"); if (fd >= 0) { - vlib_cli_output (vm, - "vlib graph dumped into `%s'. Run eg. `fdp -Tsvg -O %s'.", - chroot_filename, chroot_filename); + /*Dumping all the nodes saturates dot capacities to render a directed + * graph. In this case, prefer using he fdp command to generate an + * undirected graph. */ + const char *soft = filter ? "dot" : "fdp"; + vlib_cli_output ( + vm, "vlib graph dumped into `%s'. Run eg. `%s -Tsvg -O %s'.", + chroot_filename, soft, chroot_filename); } - vec_free (nodes); + clib_bitmap_free (active); vec_free (chroot_filename); - vec_free (nodes); + if (filter) + vec_free (nodes); if (fd >= 0) close (fd); return error; } +/*? + * Dump dot files data to draw a graph of all the nodes. + * If the argument 'filter' is provided, only the active nodes (since the last + * "clear run" comand) are selected and they are scaled and colored according + * to their utilization. You can choose to filter nodes that are called, + * nodes that receive vectors or both (default). + * The 'file' option allows to save data in a temp file. + * + * @cliexpar + * @clistart + * show vlib graphviz + * show vlib graphviz filter file tmpfile + * show vlib graphviz filter calls file tmpfile + * @cliend + * @cliexcmd{show vlib graphviz [filter][calls][vectors][file ]} +?*/ /* *INDENT-OFF* */ VLIB_CLI_COMMAND (show_node_graphviz_command, static) = { .path = "show vlib graphviz", .short_help = "Dump packet processing node graph as a graphviz dotfile", .function = show_node_graphviz, + .is_mp_safe = 1, }; /* *INDENT-ON* */ diff --git a/src/vlib/node_funcs.h b/src/vlib/node_funcs.h index 386e9168fa0..d65fd2e061d 100644 --- a/src/vlib/node_funcs.h +++ b/src/vlib/node_funcs.h @@ -1200,6 +1200,12 @@ void vlib_start_process (vlib_main_t * vm, uword process_index); /* Sync up runtime and main node stats. */ void vlib_node_sync_stats (vlib_main_t * vm, vlib_node_t * n); +void vlib_node_runtime_sync_stats (vlib_main_t *vm, vlib_node_runtime_t *r, + uword n_calls, uword n_vectors, + uword n_clocks); +void vlib_node_runtime_sync_stats_node (vlib_node_t *n, vlib_node_runtime_t *r, + uword n_calls, uword n_vectors, + uword n_clocks); /* Node graph initialization function. */ clib_error_t *vlib_node_main_init (vlib_main_t * vm); diff --git a/src/vlib/threads.c b/src/vlib/threads.c index 48e1bab8a66..dd7de73ad8c 100644 --- a/src/vlib/threads.c +++ b/src/vlib/threads.c @@ -998,11 +998,6 @@ worker_thread_node_runtime_update_internal (void) vlib_node_main_t *nm, *nm_clone; vlib_main_t *vm_clone; vlib_node_runtime_t *rt; - never_inline void - vlib_node_runtime_sync_stats (vlib_main_t * vm, - vlib_node_runtime_t * r, - uword n_calls, - uword n_vectors, uword n_clocks); ASSERT (vlib_get_thread_index () == 0); -- cgit 1.2.3-korg