graph: enhance export to graphviz

Message ID 20240320171120.255142-2-rjarry@redhat.com (mailing list archive)
State New
Delegated to: Thomas Monjalon
Headers
Series graph: enhance export to graphviz |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/loongarch-compilation success Compilation OK
ci/loongarch-unit-testing success Unit Testing PASS
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/github-robot: build fail github build: failed
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-abi-testing success Testing PASS
ci/intel-Functional success Functional PASS
ci/iol-compile-amd64-testing success Testing PASS
ci/iol-unit-arm64-testing success Testing PASS
ci/iol-unit-amd64-testing success Testing PASS
ci/iol-compile-arm64-testing success Testing PASS
ci/iol-sample-apps-testing success Testing PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/iol-broadcom-Functional success Functional Testing PASS

Commit Message

Robin Jarry March 20, 2024, 5:11 p.m. UTC
  * Quote graph name to avoid parsing errors when it contains a dash.
* Use fixed margin and smaller font for a more compact layout.
* Use sans-serif font, the default is times new roman which is not the
  best choice for a packet processing generated graph.
* Force bold blue border and arrows for source nodes.
* Force dark orange borders for sink nodes (nodes without next nodes).

Link: https://f.jarry.cc/rte-graph-dot/before.svg
Link: https://f.jarry.cc/rte-graph-dot/after.svg
Signed-off-by: Robin Jarry <rjarry@redhat.com>
---
 lib/graph/graph.c | 32 +++++++++++++++++++++++++-------
 1 file changed, 25 insertions(+), 7 deletions(-)
  

Comments

Kiran Kumar Kokkilagadda March 22, 2024, 8:02 a.m. UTC | #1
> -----Original Message-----
> From: Robin Jarry <rjarry@redhat.com>
> Sent: Wednesday, March 20, 2024 10:41 PM
> To: dev@dpdk.org; Jerin Jacob <jerinj@marvell.com>; Kiran Kumar
> Kokkilagadda <kirankumark@marvell.com>; Nithin Kumar Dabilpuram
> <ndabilpuram@marvell.com>; Zhirun Yan <yanzhirun_163@163.com>
> Subject: [EXTERNAL] [PATCH] graph: enhance export to graphviz
> 
> Prioritize security for external emails: Confirm sender and content safety
> before clicking links or opening attachments
> 
> ----------------------------------------------------------------------
> * Quote graph name to avoid parsing errors when it contains a dash.
> * Use fixed margin and smaller font for a more compact layout.
> * Use sans-serif font, the default is times new roman which is not the
>   best choice for a packet processing generated graph.
> * Force bold blue border and arrows for source nodes.
> * Force dark orange borders for sink nodes (nodes without next nodes).
> 
> Link: https://urldefense.proofpoint.com/v2/url?u=https-3A__f.jarry.cc_rte-
> 2Dgraph-
> 2Ddot_before.svg&d=DwIDAg&c=nKjWec2b6R0mOyPaz7xtfQ&r=owEKckYY4FT
> mil1Z6oBURwkTThyuRbLAY9LdfiaT6HA&m=pyYDQxlqMplh30E36wiXH6LLReml
> Q19SXfdBaWFn9w76vzq1CZdH-
> MC9xnFF1F73&s=j08NkMjMiHpSAVfucSwMN7c769_JbCqmTlEv-O0LyeQ&e=
> Link: https://urldefense.proofpoint.com/v2/url?u=https-3A__f.jarry.cc_rte-
> 2Dgraph-
> 2Ddot_after.svg&d=DwIDAg&c=nKjWec2b6R0mOyPaz7xtfQ&r=owEKckYY4FTmi
> l1Z6oBURwkTThyuRbLAY9LdfiaT6HA&m=pyYDQxlqMplh30E36wiXH6LLRemlQ1
> 9SXfdBaWFn9w76vzq1CZdH-MC9xnFF1F73&s=dQTT3aUgxo-
> oZVMsw0KJR1UT2Gn3oi9r50TAJdIgJLs&e=
> Signed-off-by: Robin Jarry <rjarry@redhat.com>

Acked-by: Kiran Kumar Kokkilagadda <kirankumark@marvell.com>

> ---
>  lib/graph/graph.c | 32 +++++++++++++++++++++++++-------
>  1 file changed, 25 insertions(+), 7 deletions(-)
> 
> diff --git a/lib/graph/graph.c b/lib/graph/graph.c index
> 26f0968a978d..147bc6c685c5 100644
> --- a/lib/graph/graph.c
> +++ b/lib/graph/graph.c
> @@ -674,25 +674,43 @@ __rte_node_stream_alloc_size(struct rte_graph
> *graph, struct rte_node *node,  static int  graph_to_dot(FILE *f, struct graph
> *graph)  {
> -	const char *src_edge_color = " [color=blue]\n";
> -	const char *edge_color = "\n";
>  	struct graph_node *graph_node;
>  	char *node_name;
>  	rte_edge_t i;
>  	int rc;
> 
> -	rc = fprintf(f, "Digraph %s {\n\trankdir=LR;\n", graph->name);
> +	rc = fprintf(f, "digraph \"%s\" {\n\trankdir=LR;\n", graph->name);
> +	if (rc < 0)
> +		goto end;
> +
> +	rc = fprintf(f, "\tnode [margin=0.02 fontsize=11 fontname=sans];\n");
>  	if (rc < 0)
>  		goto end;
> 
>  	STAILQ_FOREACH(graph_node, &graph->node_list, next) {
> +		const char *attrs = "";
>  		node_name = graph_node->node->name;
> +
> +		rc = fprintf(f, "\t\"%s\"", node_name);
> +		if (rc < 0)
> +			goto end;
> +		if (graph_node->node->flags & RTE_NODE_SOURCE_F) {
> +			attrs = " [color=blue style=bold]";
> +			rc = fprintf(f, "%s", attrs);
> +			if (rc < 0)
> +				goto end;
> +		} else if (graph_node->node->nb_edges == 0) {
> +			rc = fprintf(f, " [color=darkorange]");
> +			if (rc < 0)
> +				goto end;
> +		}
> +		rc = fprintf(f, ";\n");
> +		if (rc < 0)
> +			goto end;
>  		for (i = 0; i < graph_node->node->nb_edges; i++) {
> -			rc = fprintf(f, "\t\"%s\"->\"%s\"%s", node_name,
> +			rc = fprintf(f, "\t\"%s\" -> \"%s\"%s;\n", node_name,
>  				     graph_node->adjacency_list[i]->node-
> >name,
> -				     graph_node->node->flags &
> RTE_NODE_SOURCE_F
> -					     ? src_edge_color
> -					     : edge_color);
> +				     attrs);
>  			if (rc < 0)
>  				goto end;
>  		}
> --
> 2.44.0
  

Patch

diff --git a/lib/graph/graph.c b/lib/graph/graph.c
index 26f0968a978d..147bc6c685c5 100644
--- a/lib/graph/graph.c
+++ b/lib/graph/graph.c
@@ -674,25 +674,43 @@  __rte_node_stream_alloc_size(struct rte_graph *graph, struct rte_node *node,
 static int
 graph_to_dot(FILE *f, struct graph *graph)
 {
-	const char *src_edge_color = " [color=blue]\n";
-	const char *edge_color = "\n";
 	struct graph_node *graph_node;
 	char *node_name;
 	rte_edge_t i;
 	int rc;
 
-	rc = fprintf(f, "Digraph %s {\n\trankdir=LR;\n", graph->name);
+	rc = fprintf(f, "digraph \"%s\" {\n\trankdir=LR;\n", graph->name);
+	if (rc < 0)
+		goto end;
+
+	rc = fprintf(f, "\tnode [margin=0.02 fontsize=11 fontname=sans];\n");
 	if (rc < 0)
 		goto end;
 
 	STAILQ_FOREACH(graph_node, &graph->node_list, next) {
+		const char *attrs = "";
 		node_name = graph_node->node->name;
+
+		rc = fprintf(f, "\t\"%s\"", node_name);
+		if (rc < 0)
+			goto end;
+		if (graph_node->node->flags & RTE_NODE_SOURCE_F) {
+			attrs = " [color=blue style=bold]";
+			rc = fprintf(f, "%s", attrs);
+			if (rc < 0)
+				goto end;
+		} else if (graph_node->node->nb_edges == 0) {
+			rc = fprintf(f, " [color=darkorange]");
+			if (rc < 0)
+				goto end;
+		}
+		rc = fprintf(f, ";\n");
+		if (rc < 0)
+			goto end;
 		for (i = 0; i < graph_node->node->nb_edges; i++) {
-			rc = fprintf(f, "\t\"%s\"->\"%s\"%s", node_name,
+			rc = fprintf(f, "\t\"%s\" -> \"%s\"%s;\n", node_name,
 				     graph_node->adjacency_list[i]->node->name,
-				     graph_node->node->flags & RTE_NODE_SOURCE_F
-					     ? src_edge_color
-					     : edge_color);
+				     attrs);
 			if (rc < 0)
 				goto end;
 		}