doc: add Meson coding style to contributors guide
[dpdk.git] / lib / librte_graph / graph.c
index f4e02fd..7224b00 100644 (file)
@@ -18,7 +18,6 @@
 static struct graph_head graph_list = STAILQ_HEAD_INITIALIZER(graph_list);
 static rte_spinlock_t graph_lock = RTE_SPINLOCK_INITIALIZER;
 static rte_graph_t graph_id;
-int rte_graph_logtype;
 
 #define GRAPH_ID_CHECK(id) ID_CHECK(id, graph_id)
 
@@ -210,6 +209,54 @@ graph_node_fini(struct graph *graph)
                                                       graph_node->node->name));
 }
 
+static struct rte_graph *
+graph_mem_fixup_node_ctx(struct rte_graph *graph)
+{
+       struct rte_node *node;
+       struct node *node_db;
+       rte_graph_off_t off;
+       rte_node_t count;
+       const char *name;
+
+       rte_graph_foreach_node(count, off, graph, node) {
+               if (node->parent_id == RTE_NODE_ID_INVALID) /* Static node */
+                       name = node->name;
+               else /* Cloned node */
+                       name = node->parent;
+
+               node_db = node_from_name(name);
+               if (node_db == NULL)
+                       SET_ERR_JMP(ENOLINK, fail, "Node %s not found", name);
+               node->process = node_db->process;
+       }
+
+       return graph;
+fail:
+       return NULL;
+}
+
+static struct rte_graph *
+graph_mem_fixup_secondary(struct rte_graph *graph)
+{
+       if (graph == NULL || rte_eal_process_type() == RTE_PROC_PRIMARY)
+               return graph;
+
+       return graph_mem_fixup_node_ctx(graph);
+}
+
+struct rte_graph *
+rte_graph_lookup(const char *name)
+{
+       const struct rte_memzone *mz;
+       struct rte_graph *rc = NULL;
+
+       mz = rte_memzone_lookup(name);
+       if (mz)
+               rc = mz->addr;
+
+       return graph_mem_fixup_secondary(rc);
+}
+
 rte_graph_t
 rte_graph_create(const char *name, struct rte_graph_param *prm)
 {
@@ -340,6 +387,76 @@ done:
        return rc;
 }
 
+rte_graph_t
+rte_graph_from_name(const char *name)
+{
+       struct graph *graph;
+
+       STAILQ_FOREACH(graph, &graph_list, next)
+               if (strncmp(graph->name, name, RTE_GRAPH_NAMESIZE) == 0)
+                       return graph->id;
+
+       return RTE_GRAPH_ID_INVALID;
+}
+
+char *
+rte_graph_id_to_name(rte_graph_t id)
+{
+       struct graph *graph;
+
+       GRAPH_ID_CHECK(id);
+       STAILQ_FOREACH(graph, &graph_list, next)
+               if (graph->id == id)
+                       return graph->name;
+
+fail:
+       return NULL;
+}
+
+struct rte_node *
+rte_graph_node_get(rte_graph_t gid, uint32_t nid)
+{
+       struct rte_node *node;
+       struct graph *graph;
+       rte_graph_off_t off;
+       rte_node_t count;
+
+       GRAPH_ID_CHECK(gid);
+       STAILQ_FOREACH(graph, &graph_list, next)
+               if (graph->id == gid) {
+                       rte_graph_foreach_node(count, off, graph->graph,
+                                               node) {
+                               if (node->id == nid)
+                                       return node;
+                       }
+                       break;
+               }
+fail:
+       return NULL;
+}
+
+struct rte_node *
+rte_graph_node_get_by_name(const char *graph_name, const char *node_name)
+{
+       struct rte_node *node;
+       struct graph *graph;
+       rte_graph_off_t off;
+       rte_node_t count;
+
+       STAILQ_FOREACH(graph, &graph_list, next)
+               if (!strncmp(graph->name, graph_name, RTE_GRAPH_NAMESIZE)) {
+                       rte_graph_foreach_node(count, off, graph->graph,
+                                               node) {
+                               if (!strncmp(node->name, node_name,
+                                            RTE_NODE_NAMESIZE))
+                                       return node;
+                       }
+                       break;
+               }
+
+       return NULL;
+}
+
 void __rte_noinline
 __rte_node_stream_alloc(struct rte_graph *graph, struct rte_node *node)
 {
@@ -354,3 +471,111 @@ __rte_node_stream_alloc(struct rte_graph *graph, struct rte_node *node)
        node->size = size;
        node->realloc_count++;
 }
+
+void __rte_noinline
+__rte_node_stream_alloc_size(struct rte_graph *graph, struct rte_node *node,
+                            uint16_t req_size)
+{
+       uint16_t size = node->size;
+
+       RTE_VERIFY(size != UINT16_MAX);
+       /* Allocate double amount of size to avoid immediate realloc */
+       size = RTE_MIN(UINT16_MAX, RTE_MAX(RTE_GRAPH_BURST_SIZE, req_size * 2));
+       node->objs = rte_realloc_socket(node->objs, size * sizeof(void *),
+                                       RTE_CACHE_LINE_SIZE, graph->socket);
+       RTE_VERIFY(node->objs);
+       node->size = size;
+       node->realloc_count++;
+}
+
+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);
+       if (rc < 0)
+               goto end;
+
+       STAILQ_FOREACH(graph_node, &graph->node_list, next) {
+               node_name = graph_node->node->name;
+               for (i = 0; i < graph_node->node->nb_edges; i++) {
+                       rc = fprintf(f, "\t\"%s\"->\"%s\"%s", node_name,
+                                    graph_node->adjacency_list[i]->node->name,
+                                    graph_node->node->flags & RTE_NODE_SOURCE_F
+                                            ? src_edge_color
+                                            : edge_color);
+                       if (rc < 0)
+                               goto end;
+               }
+       }
+       rc = fprintf(f, "}\n");
+       if (rc < 0)
+               goto end;
+
+       return 0;
+end:
+       rte_errno = EBADF;
+       return -rte_errno;
+}
+
+int
+rte_graph_export(const char *name, FILE *f)
+{
+       struct graph *graph;
+       int rc = ENOENT;
+
+       STAILQ_FOREACH(graph, &graph_list, next) {
+               if (strncmp(graph->name, name, RTE_GRAPH_NAMESIZE) == 0) {
+                       rc = graph_to_dot(f, graph);
+                       goto end;
+               }
+       }
+end:
+       return -rc;
+}
+
+static void
+graph_scan_dump(FILE *f, rte_graph_t id, bool all)
+{
+       struct graph *graph;
+
+       RTE_VERIFY(f);
+       GRAPH_ID_CHECK(id);
+
+       STAILQ_FOREACH(graph, &graph_list, next) {
+               if (all == true) {
+                       graph_dump(f, graph);
+               } else if (graph->id == id) {
+                       graph_dump(f, graph);
+                       return;
+               }
+       }
+fail:
+       return;
+}
+
+void
+rte_graph_dump(FILE *f, rte_graph_t id)
+{
+       graph_scan_dump(f, id, false);
+}
+
+void
+rte_graph_list_dump(FILE *f)
+{
+       graph_scan_dump(f, 0, true);
+}
+
+rte_graph_t
+rte_graph_max_count(void)
+{
+       return graph_id;
+}
+
+RTE_LOG_REGISTER(rte_graph_logtype, lib.graph, INFO);