1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2020 Marvell International Ltd.
8 #include <rte_common.h>
10 #include <rte_malloc.h>
12 #include "graph_private.h"
14 /* Capture all graphs of cluster */
16 rte_graph_t nb_graphs;
19 struct graph **graphs;
22 /* Capture same node ID across cluster */
24 struct rte_graph_cluster_node_stats stat;
27 struct rte_node *nodes[];
30 struct rte_graph_cluster_stats {
32 rte_graph_cluster_stats_cb_t fn;
33 uint32_t cluster_node_size; /* Size of struct cluster_node */
39 struct cluster_node clusters[];
40 } __rte_cache_aligned;
43 fprintf(f, "+-------------------------------+---------------+--------" \
44 "-------+---------------+---------------+---------------+-" \
51 fprintf(f, "%-32s%-16s%-16s%-16s%-16s%-16s%-16s\n", "|Node", "|calls",
52 "|objs", "|realloc_count", "|objs/call", "|objs/sec(10E6)",
58 print_node(FILE *f, const struct rte_graph_cluster_node_stats *stat)
60 double objs_per_call, objs_per_sec, cycles_per_call, ts_per_hz;
61 const uint64_t prev_calls = stat->prev_calls;
62 const uint64_t prev_objs = stat->prev_objs;
63 const uint64_t cycles = stat->cycles;
64 const uint64_t calls = stat->calls;
65 const uint64_t objs = stat->objs;
68 call_delta = calls - prev_calls;
70 call_delta ? (double)((objs - prev_objs) / call_delta) : 0;
72 call_delta ? (double)((cycles - stat->prev_cycles) / call_delta)
74 ts_per_hz = (double)((stat->ts - stat->prev_ts) / stat->hz);
75 objs_per_sec = ts_per_hz ? (objs - prev_objs) / ts_per_hz : 0;
76 objs_per_sec /= 1000000;
79 "|%-31s|%-15" PRIu64 "|%-15" PRIu64 "|%-15" PRIu64
80 "|%-15.3f|%-15.6f|%-11.4f|\n",
81 stat->name, calls, objs, stat->realloc_count, objs_per_call,
82 objs_per_sec, cycles_per_call);
86 graph_cluster_stats_cb(bool is_first, bool is_last, void *cookie,
87 const struct rte_graph_cluster_node_stats *stat)
91 if (unlikely(is_first))
95 if (unlikely(is_last))
101 static struct rte_graph_cluster_stats *
102 stats_mem_init(struct cluster *cluster,
103 const struct rte_graph_cluster_stats_param *prm)
105 size_t sz = sizeof(struct rte_graph_cluster_stats);
106 struct rte_graph_cluster_stats *stats;
107 rte_graph_cluster_stats_cb_t fn;
108 int socket_id = prm->socket_id;
109 uint32_t cluster_node_size;
111 /* Fix up callback */
114 fn = graph_cluster_stats_cb;
116 cluster_node_size = sizeof(struct cluster_node);
117 /* For a given cluster, max nodes will be the max number of graphs */
118 cluster_node_size += cluster->nb_graphs * sizeof(struct rte_node *);
119 cluster_node_size = RTE_ALIGN(cluster_node_size, RTE_CACHE_LINE_SIZE);
121 stats = realloc(NULL, sz);
123 memset(stats, 0, sz);
125 stats->cluster_node_size = cluster_node_size;
126 stats->max_nodes = 0;
127 stats->socket_id = socket_id;
128 stats->cookie = prm->cookie;
136 stats_mem_populate(struct rte_graph_cluster_stats **stats_in,
137 struct rte_graph *graph, struct graph_node *graph_node)
139 struct rte_graph_cluster_stats *stats = *stats_in;
140 rte_node_t id = graph_node->node->id;
141 struct cluster_node *cluster;
142 struct rte_node *node;
145 cluster = stats->clusters;
147 /* Iterate over cluster node array to find node ID match */
148 for (count = 0; count < stats->max_nodes; count++) {
149 /* Found an existing node in the reel */
150 if (cluster->stat.id == id) {
151 node = graph_node_id_to_ptr(graph, id);
155 "Failed to find node %s in graph %s",
156 graph_node->node->name, graph->name);
158 cluster->nodes[cluster->nb_nodes++] = node;
161 cluster = RTE_PTR_ADD(cluster, stats->cluster_node_size);
164 /* Hey, it is a new node, allocate space for it in the reel */
165 stats = realloc(stats, stats->sz + stats->cluster_node_size);
167 SET_ERR_JMP(ENOMEM, err, "Realloc failed");
170 /* Clear the new struct cluster_node area */
171 cluster = RTE_PTR_ADD(stats, stats->sz),
172 memset(cluster, 0, stats->cluster_node_size);
173 memcpy(cluster->stat.name, graph_node->node->name, RTE_NODE_NAMESIZE);
174 cluster->stat.id = graph_node->node->id;
175 cluster->stat.hz = rte_get_timer_hz();
176 node = graph_node_id_to_ptr(graph, id);
178 SET_ERR_JMP(ENOENT, free, "Failed to find node %s in graph %s",
179 graph_node->node->name, graph->name);
180 cluster->nodes[cluster->nb_nodes++] = node;
182 stats->sz += stats->cluster_node_size;
194 stats_mem_fini(struct rte_graph_cluster_stats *stats)
200 cluster_init(struct cluster *cluster)
202 memset(cluster, 0, sizeof(*cluster));
206 cluster_add(struct cluster *cluster, struct graph *graph)
211 /* Skip the if graph is already added to cluster */
212 for (count = 0; count < cluster->nb_graphs; count++)
213 if (cluster->graphs[count] == graph)
216 /* Expand the cluster if required to store graph objects */
217 if (cluster->nb_graphs + 1 > cluster->size) {
218 cluster->size = RTE_MAX(1, cluster->size * 2);
219 sz = sizeof(struct graph *) * cluster->size;
220 cluster->graphs = realloc(cluster->graphs, sz);
221 if (cluster->graphs == NULL)
222 SET_ERR_JMP(ENOMEM, free, "Failed to realloc");
225 /* Add graph to cluster */
226 cluster->graphs[cluster->nb_graphs++] = graph;
234 cluster_fini(struct cluster *cluster)
236 free(cluster->graphs);
240 expand_pattern_to_cluster(struct cluster *cluster, const char *pattern)
242 struct graph_head *graph_head = graph_list_head_get();
246 /* Check for pattern match */
247 STAILQ_FOREACH(graph, graph_head, next) {
248 if (fnmatch(pattern, graph->name, 0) == 0) {
249 if (cluster_add(cluster, graph))
255 SET_ERR_JMP(EFAULT, fail, "Pattern %s graph not found",
263 struct rte_graph_cluster_stats *
264 rte_graph_cluster_stats_create(const struct rte_graph_cluster_stats_param *prm)
266 struct rte_graph_cluster_stats *stats, *rc = NULL;
267 struct graph_node *graph_node;
268 struct cluster cluster;
274 if (!rte_graph_has_stats_feature())
275 SET_ERR_JMP(EINVAL, fail, "Stats feature is not enabled");
278 SET_ERR_JMP(EINVAL, fail, "Invalid param");
280 if (prm->graph_patterns == NULL || prm->nb_graph_patterns == 0)
281 SET_ERR_JMP(EINVAL, fail, "Invalid graph param");
283 cluster_init(&cluster);
285 graph_spinlock_lock();
286 /* Expand graph pattern and add the graph to the cluster */
287 for (i = 0; i < prm->nb_graph_patterns; i++) {
288 pattern = prm->graph_patterns[i];
289 if (expand_pattern_to_cluster(&cluster, pattern))
293 /* Alloc the stats memory */
294 stats = stats_mem_init(&cluster, prm);
296 SET_ERR_JMP(ENOMEM, bad_pattern, "Failed alloc stats memory");
298 /* Iterate over M(Graph) x N (Nodes in graph) */
299 for (i = 0; i < cluster.nb_graphs; i++) {
300 graph = cluster.graphs[i];
301 STAILQ_FOREACH(graph_node, &graph->node_list, next) {
302 struct rte_graph *graph_fp = graph->graph;
303 if (stats_mem_populate(&stats, graph_fp, graph_node))
308 /* Finally copy to hugepage memory to avoid pressure on rte_realloc */
309 rc = rte_malloc_socket(NULL, stats->sz, 0, stats->socket_id);
311 rte_memcpy(rc, stats, stats->sz);
313 SET_ERR_JMP(ENOMEM, realloc_fail, "rte_malloc failed");
316 stats_mem_fini(stats);
318 graph_spinlock_unlock();
319 cluster_fini(&cluster);
325 rte_graph_cluster_stats_destroy(struct rte_graph_cluster_stats *stat)
327 return rte_free(stat);
331 cluster_node_arregate_stats(struct cluster_node *cluster)
333 uint64_t calls = 0, cycles = 0, objs = 0, realloc_count = 0;
334 struct rte_graph_cluster_node_stats *stat = &cluster->stat;
335 struct rte_node *node;
338 for (count = 0; count < cluster->nb_nodes; count++) {
339 node = cluster->nodes[count];
341 calls += node->total_calls;
342 objs += node->total_objs;
343 cycles += node->total_cycles;
344 realloc_count += node->realloc_count;
349 stat->cycles = cycles;
350 stat->ts = rte_get_timer_cycles();
351 stat->realloc_count = realloc_count;
355 cluster_node_store_prev_stats(struct cluster_node *cluster)
357 struct rte_graph_cluster_node_stats *stat = &cluster->stat;
359 stat->prev_ts = stat->ts;
360 stat->prev_calls = stat->calls;
361 stat->prev_objs = stat->objs;
362 stat->prev_cycles = stat->cycles;
366 rte_graph_cluster_stats_get(struct rte_graph_cluster_stats *stat, bool skip_cb)
368 struct cluster_node *cluster;
372 cluster = stat->clusters;
374 for (count = 0; count < stat->max_nodes; count++) {
375 cluster_node_arregate_stats(cluster);
377 rc = stat->fn(!count, (count == stat->max_nodes - 1),
378 stat->cookie, &cluster->stat);
379 cluster_node_store_prev_stats(cluster);
382 cluster = RTE_PTR_ADD(cluster, stat->cluster_node_size);
387 rte_graph_cluster_stats_reset(struct rte_graph_cluster_stats *stat)
389 struct cluster_node *cluster;
392 cluster = stat->clusters;
394 for (count = 0; count < stat->max_nodes; count++) {
395 struct rte_graph_cluster_node_stats *node = &cluster->stat;
402 node->prev_calls = 0;
404 node->prev_cycles = 0;
405 node->realloc_count = 0;
406 cluster = RTE_PTR_ADD(cluster, stat->cluster_node_size);