summaryrefslogtreecommitdiffstats
path: root/src/spdk/dpdk/app/test/test_graph_perf.c
blob: 296d99a9d3fa3afcbab26932b8bbf69953dae805 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(C) 2020 Marvell International Ltd.
 */
#include <inttypes.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>

#include <rte_common.h>
#include <rte_cycles.h>
#include <rte_errno.h>
#include <rte_graph.h>
#include <rte_graph_worker.h>
#include <rte_lcore.h>
#include <rte_malloc.h>
#include <rte_mbuf.h>

#include "test.h"

#define TEST_GRAPH_PERF_MZ	     "graph_perf_data"
#define TEST_GRAPH_SRC_NAME	     "test_graph_perf_source"
#define TEST_GRAPH_SRC_BRST_ONE_NAME "test_graph_perf_source_one"
#define TEST_GRAPH_WRK_NAME	     "test_graph_perf_worker"
#define TEST_GRAPH_SNK_NAME	     "test_graph_perf_sink"

#define SOURCES(map)	     RTE_DIM(map)
#define STAGES(map)	     RTE_DIM(map)
#define NODES_PER_STAGE(map) RTE_DIM(map[0])
#define SINKS(map)	     RTE_DIM(map[0])

#define MAX_EDGES_PER_NODE 7

struct test_node_data {
	uint8_t node_id;
	uint8_t is_sink;
	uint8_t next_nodes[MAX_EDGES_PER_NODE];
	uint8_t next_percentage[MAX_EDGES_PER_NODE];
};

struct test_graph_perf {
	uint16_t nb_nodes;
	rte_graph_t graph_id;
	struct test_node_data *node_data;
};

struct graph_lcore_data {
	uint8_t done;
	rte_graph_t graph_id;
};

static struct test_node_data *
graph_get_node_data(struct test_graph_perf *graph_data, rte_node_t id)
{
	struct test_node_data *node_data = NULL;
	int i;

	for (i = 0; i < graph_data->nb_nodes; i++)
		if (graph_data->node_data[i].node_id == id) {
			node_data = &graph_data->node_data[i];
			break;
		}

	return node_data;
}

static int
test_node_ctx_init(const struct rte_graph *graph, struct rte_node *node)
{
	struct test_graph_perf *graph_data;
	struct test_node_data *node_data;
	const struct rte_memzone *mz;
	rte_node_t nid = node->id;
	rte_edge_t edge = 0;
	int i;

	RTE_SET_USED(graph);

	mz = rte_memzone_lookup(TEST_GRAPH_PERF_MZ);
	if (mz == NULL)
		return -ENOMEM;
	graph_data = mz->addr;
	node_data = graph_get_node_data(graph_data, nid);
	node->ctx[0] = node->nb_edges;
	for (i = 0; i < node->nb_edges && !node_data->is_sink; i++, edge++) {
		node->ctx[i + 1] = edge;
		node->ctx[i + 9] = node_data->next_percentage[i];
	}

	return 0;
}

/* Source node function */
static uint16_t
test_perf_node_worker_source(struct rte_graph *graph, struct rte_node *node,
			     void **objs, uint16_t nb_objs)
{
	uint16_t count;
	int i;

	RTE_SET_USED(objs);
	RTE_SET_USED(nb_objs);

	/* Create a proportional stream for every next */
	for (i = 0; i < node->ctx[0]; i++) {
		count = (node->ctx[i + 9] * RTE_GRAPH_BURST_SIZE) / 100;
		rte_node_next_stream_get(graph, node, node->ctx[i + 1], count);
		rte_node_next_stream_put(graph, node, node->ctx[i + 1], count);
	}

	return RTE_GRAPH_BURST_SIZE;
}

static struct rte_node_register test_graph_perf_source = {
	.name = TEST_GRAPH_SRC_NAME,
	.process = test_perf_node_worker_source,
	.flags = RTE_NODE_SOURCE_F,
	.init = test_node_ctx_init,
};

RTE_NODE_REGISTER(test_graph_perf_source);

static uint16_t
test_perf_node_worker_source_burst_one(struct rte_graph *graph,
				       struct rte_node *node, void **objs,
				       uint16_t nb_objs)
{
	uint16_t count;
	int i;

	RTE_SET_USED(objs);
	RTE_SET_USED(nb_objs);

	/* Create a proportional stream for every next */
	for (i = 0; i < node->ctx[0]; i++) {
		count = (node->ctx[i + 9]) / 100;
		rte_node_next_stream_get(graph, node, node->ctx[i + 1], count);
		rte_node_next_stream_put(graph, node, node->ctx[i + 1], count);
	}

	return 1;
}

static struct rte_node_register test_graph_perf_source_burst_one = {
	.name = TEST_GRAPH_SRC_BRST_ONE_NAME,
	.process = test_perf_node_worker_source_burst_one,
	.flags = RTE_NODE_SOURCE_F,
	.init = test_node_ctx_init,
};

RTE_NODE_REGISTER(test_graph_perf_source_burst_one);

/* Worker node function */
static uint16_t
test_perf_node_worker(struct rte_graph *graph, struct rte_node *node,
		      void **objs, uint16_t nb_objs)
{
	uint16_t next = 0;
	uint16_t enq = 0;
	uint16_t count;
	int i;

	/* Move stream for single next node */
	if (node->ctx[0] == 1) {
		rte_node_next_stream_move(graph, node, node->ctx[1]);
		return nb_objs;
	}

	/* Enqueue objects to next nodes proportionally */
	for (i = 0; i < node->ctx[0]; i++) {
		next = node->ctx[i + 1];
		count = (node->ctx[i + 9] * nb_objs) / 100;
		enq += count;
		while (count) {
			switch (count & (4 - 1)) {
			case 0:
				rte_node_enqueue_x4(graph, node, next, objs[0],
						    objs[1], objs[2], objs[3]);
				objs += 4;
				count -= 4;
				break;
			case 1:
				rte_node_enqueue_x1(graph, node, next, objs[0]);
				objs += 1;
				count -= 1;
				break;
			case 2:
				rte_node_enqueue_x2(graph, node, next, objs[0],
						    objs[1]);
				objs += 2;
				count -= 2;
				break;
			case 3:
				rte_node_enqueue_x2(graph, node, next, objs[0],
						    objs[1]);
				rte_node_enqueue_x1(graph, node, next, objs[0]);
				objs += 3;
				count -= 3;
				break;
			}
		}
	}

	if (enq != nb_objs)
		rte_node_enqueue(graph, node, next, objs, nb_objs - enq);

	return nb_objs;
}

static struct rte_node_register test_graph_perf_worker = {
	.name = TEST_GRAPH_WRK_NAME,
	.process = test_perf_node_worker,
	.init = test_node_ctx_init,
};

RTE_NODE_REGISTER(test_graph_perf_worker);

/* Last node in graph a.k.a sink node */
static uint16_t
test_perf_node_sink(struct rte_graph *graph, struct rte_node *node, void **objs,
		    uint16_t nb_objs)
{
	RTE_SET_USED(graph);
	RTE_SET_USED(node);
	RTE_SET_USED(objs);
	RTE_SET_USED(nb_objs);

	return nb_objs;
}

static struct rte_node_register test_graph_perf_sink = {
	.name = TEST_GRAPH_SNK_NAME,
	.process = test_perf_node_sink,
	.init = test_node_ctx_init,
};

RTE_NODE_REGISTER(test_graph_perf_sink);

static int
graph_perf_setup(void)
{
	if (rte_lcore_count() < 2) {
		printf("Test requires at least 2 lcores\n");
		return TEST_SKIPPED;
	}

	return 0;
}

static void
graph_perf_teardown(void)
{
}

static inline rte_node_t
graph_node_get(const char *pname, char *nname)
{
	rte_node_t pnode_id = rte_node_from_name(pname);
	char lookup_name[RTE_NODE_NAMESIZE];
	rte_node_t node_id;

	snprintf(lookup_name, RTE_NODE_NAMESIZE, "%s-%s", pname, nname);
	node_id = rte_node_from_name(lookup_name);

	if (node_id != RTE_NODE_ID_INVALID) {
		if (rte_node_edge_count(node_id))
			rte_node_edge_shrink(node_id, 0);
		return node_id;
	}

	return rte_node_clone(pnode_id, nname);
}

static uint16_t
graph_node_count_edges(uint32_t stage, uint16_t node, uint16_t nodes_per_stage,
		       uint8_t edge_map[][nodes_per_stage][nodes_per_stage],
		       char *ename[], struct test_node_data *node_data,
		       rte_node_t **node_map)
{
	uint8_t total_percent = 0;
	uint16_t edges = 0;
	int i;

	for (i = 0; i < nodes_per_stage && edges < MAX_EDGES_PER_NODE; i++) {
		if (edge_map[stage + 1][i][node]) {
			ename[edges] = malloc(sizeof(char) * RTE_NODE_NAMESIZE);
			snprintf(ename[edges], RTE_NODE_NAMESIZE, "%s",
				 rte_node_id_to_name(node_map[stage + 1][i]));
			node_data->next_nodes[edges] = node_map[stage + 1][i];
			node_data->next_percentage[edges] =
				edge_map[stage + 1][i][node];
			edges++;
			total_percent += edge_map[stage + 1][i][node];
		}
	}

	if (edges >= MAX_EDGES_PER_NODE || (edges && total_percent != 100)) {
		for (i = 0; i < edges; i++)
			free(ename[i]);
		return RTE_EDGE_ID_INVALID;
	}

	return edges;
}

static int
graph_init(const char *gname, uint8_t nb_srcs, uint8_t nb_sinks,
	   uint32_t stages, uint16_t nodes_per_stage,
	   uint8_t src_map[][nodes_per_stage], uint8_t snk_map[][nb_sinks],
	   uint8_t edge_map[][nodes_per_stage][nodes_per_stage],
	   uint8_t burst_one)
{
	struct test_graph_perf *graph_data;
	char nname[RTE_NODE_NAMESIZE / 2];
	struct test_node_data *node_data;
	char *ename[nodes_per_stage];
	struct rte_graph_param gconf;
	const struct rte_memzone *mz;
	uint8_t total_percent = 0;
	rte_node_t *src_nodes;
	rte_node_t *snk_nodes;
	rte_node_t **node_map;
	char **node_patterns;
	rte_graph_t graph_id;
	rte_edge_t edges;
	rte_edge_t count;
	uint32_t i, j, k;

	mz = rte_memzone_reserve(TEST_GRAPH_PERF_MZ,
				 sizeof(struct test_graph_perf), 0, 0);
	if (mz == NULL) {
		printf("Failed to allocate graph common memory\n");
		return -ENOMEM;
	}

	graph_data = mz->addr;
	graph_data->nb_nodes = 0;
	graph_data->node_data =
		malloc(sizeof(struct test_node_data) *
		       (nb_srcs + nb_sinks + stages * nodes_per_stage));
	if (graph_data->node_data == NULL) {
		printf("Failed to reserve memzone for graph data\n");
		goto memzone_free;
	}

	node_patterns = malloc(sizeof(char *) *
			       (nb_srcs + nb_sinks + stages * nodes_per_stage));
	if (node_patterns == NULL) {
		printf("Failed to reserve memory for node patterns\n");
		goto data_free;
	}

	src_nodes = malloc(sizeof(rte_node_t) * nb_srcs);
	if (src_nodes == NULL) {
		printf("Failed to reserve memory for src nodes\n");
		goto pattern_free;
	}

	snk_nodes = malloc(sizeof(rte_node_t) * nb_sinks);
	if (snk_nodes == NULL) {
		printf("Failed to reserve memory for snk nodes\n");
		goto src_free;
	}

	node_map = malloc(sizeof(rte_node_t *) * stages +
			  sizeof(rte_node_t) * nodes_per_stage * stages);
	if (node_map == NULL) {
		printf("Failed to reserve memory for node map\n");
		goto snk_free;
	}

	/* Setup the Graph */
	for (i = 0; i < stages; i++) {
		node_map[i] =
			(rte_node_t *)(node_map + stages) + nodes_per_stage * i;
		for (j = 0; j < nodes_per_stage; j++) {
			total_percent = 0;
			for (k = 0; k < nodes_per_stage; k++)
				total_percent += edge_map[i][j][k];
			if (!total_percent)
				continue;
			node_patterns[graph_data->nb_nodes] =
				malloc(RTE_NODE_NAMESIZE);
			if (node_patterns[graph_data->nb_nodes] == NULL) {
				printf("Failed to create memory for pattern\n");
				goto pattern_name_free;
			}

			/* Clone a worker node */
			snprintf(nname, sizeof(nname), "%d-%d", i, j);
			node_map[i][j] =
				graph_node_get(TEST_GRAPH_WRK_NAME, nname);
			if (node_map[i][j] == RTE_NODE_ID_INVALID) {
				printf("Failed to create node[%s]\n", nname);
				graph_data->nb_nodes++;
				goto pattern_name_free;
			}
			snprintf(node_patterns[graph_data->nb_nodes],
				 RTE_NODE_NAMESIZE, "%s",
				 rte_node_id_to_name(node_map[i][j]));
			node_data =
				&graph_data->node_data[graph_data->nb_nodes];
			node_data->node_id = node_map[i][j];
			node_data->is_sink = false;
			graph_data->nb_nodes++;
		}
	}

	for (i = 0; i < stages - 1; i++) {
		for (j = 0; j < nodes_per_stage; j++) {
			/* Count edges i.e connections of worker node to next */
			node_data =
				graph_get_node_data(graph_data, node_map[i][j]);
			edges = graph_node_count_edges(i, j, nodes_per_stage,
						       edge_map, ename,
						       node_data, node_map);
			if (edges == RTE_EDGE_ID_INVALID) {
				printf("Invalid edge configuration\n");
				goto pattern_name_free;
			}
			if (!edges)
				continue;

			/* Connect a node in stage 'i' to nodes
			 * in stage 'i + 1' with edges.
			 */
			count = rte_node_edge_update(
				node_map[i][j], 0,
				(const char **)(uintptr_t)ename, edges);
			for (k = 0; k < edges; k++)
				free(ename[k]);
			if (count != edges) {
				printf("Couldn't add edges %d %d\n", edges,
				       count);
				goto pattern_name_free;
			}
		}
	}

	/* Setup Source nodes */
	for (i = 0; i < nb_srcs; i++) {
		edges = 0;
		total_percent = 0;
		node_patterns[graph_data->nb_nodes] = malloc(RTE_NODE_NAMESIZE);
		if (node_patterns[graph_data->nb_nodes] == NULL) {
			printf("Failed to create memory for pattern\n");
			goto pattern_name_free;
		}
		/* Clone a source node */
		snprintf(nname, sizeof(nname), "%d", i);
		src_nodes[i] =
			graph_node_get(burst_one ? TEST_GRAPH_SRC_BRST_ONE_NAME
						 : TEST_GRAPH_SRC_NAME,
				       nname);
		if (src_nodes[i] == RTE_NODE_ID_INVALID) {
			printf("Failed to create node[%s]\n", nname);
			graph_data->nb_nodes++;
			goto pattern_name_free;
		}
		snprintf(node_patterns[graph_data->nb_nodes], RTE_NODE_NAMESIZE,
			 "%s", rte_node_id_to_name(src_nodes[i]));
		node_data = &graph_data->node_data[graph_data->nb_nodes];
		node_data->node_id = src_nodes[i];
		node_data->is_sink = false;
		graph_data->nb_nodes++;

		/* Prepare next node list  to connect to */
		for (j = 0; j < nodes_per_stage; j++) {
			if (!src_map[i][j])
				continue;
			ename[edges] = malloc(sizeof(char) * RTE_NODE_NAMESIZE);
			snprintf(ename[edges], RTE_NODE_NAMESIZE, "%s",
				 rte_node_id_to_name(node_map[0][j]));
			node_data->next_nodes[edges] = node_map[0][j];
			node_data->next_percentage[edges] = src_map[i][j];
			edges++;
			total_percent += src_map[i][j];
		}

		if (!edges)
			continue;
		if (edges >= MAX_EDGES_PER_NODE || total_percent != 100) {
			printf("Invalid edge configuration\n");
			for (j = 0; j < edges; j++)
				free(ename[j]);
			goto pattern_name_free;
		}

		/* Connect to list of next nodes using edges */
		count = rte_node_edge_update(src_nodes[i], 0,
					     (const char **)(uintptr_t)ename,
					     edges);
		for (k = 0; k < edges; k++)
			free(ename[k]);
		if (count != edges) {
			printf("Couldn't add edges %d %d\n", edges, count);
			goto pattern_name_free;
		}
	}

	/* Setup Sink nodes */
	for (i = 0; i < nb_sinks; i++) {
		node_patterns[graph_data->nb_nodes] = malloc(RTE_NODE_NAMESIZE);
		if (node_patterns[graph_data->nb_nodes] == NULL) {
			printf("Failed to create memory for pattern\n");
			goto pattern_name_free;
		}

		/* Clone a sink node */
		snprintf(nname, sizeof(nname), "%d", i);
		snk_nodes[i] = graph_node_get(TEST_GRAPH_SNK_NAME, nname);
		if (snk_nodes[i] == RTE_NODE_ID_INVALID) {
			printf("Failed to create node[%s]\n", nname);
			graph_data->nb_nodes++;
			goto pattern_name_free;
		}
		snprintf(node_patterns[graph_data->nb_nodes], RTE_NODE_NAMESIZE,
			 "%s", rte_node_id_to_name(snk_nodes[i]));
		node_data = &graph_data->node_data[graph_data->nb_nodes];
		node_data->node_id = snk_nodes[i];
		node_data->is_sink = true;
		graph_data->nb_nodes++;
	}

	/* Connect last stage worker nodes to sink nodes */
	for (i = 0; i < nodes_per_stage; i++) {
		edges = 0;
		total_percent = 0;
		node_data = graph_get_node_data(graph_data,
						node_map[stages - 1][i]);
		/* Prepare list of sink nodes to connect to */
		for (j = 0; j < nb_sinks; j++) {
			if (!snk_map[i][j])
				continue;
			ename[edges] = malloc(sizeof(char) * RTE_NODE_NAMESIZE);
			snprintf(ename[edges], RTE_NODE_NAMESIZE, "%s",
				 rte_node_id_to_name(snk_nodes[j]));
			node_data->next_nodes[edges] = snk_nodes[j];
			node_data->next_percentage[edges] = snk_map[i][j];
			edges++;
			total_percent += snk_map[i][j];
		}
		if (!edges)
			continue;
		if (edges >= MAX_EDGES_PER_NODE || total_percent != 100) {
			printf("Invalid edge configuration\n");
			for (j = 0; j < edges; j++)
				free(ename[i]);
			goto pattern_name_free;
		}

		/* Connect a worker node to a list of sink nodes */
		count = rte_node_edge_update(node_map[stages - 1][i], 0,
					     (const char **)(uintptr_t)ename,
					     edges);
		for (k = 0; k < edges; k++)
			free(ename[k]);
		if (count != edges) {
			printf("Couldn't add edges %d %d\n", edges, count);
			goto pattern_name_free;
		}
	}

	/* Create a Graph */
	gconf.socket_id = SOCKET_ID_ANY;
	gconf.nb_node_patterns = graph_data->nb_nodes;
	gconf.node_patterns = (const char **)(uintptr_t)node_patterns;

	graph_id = rte_graph_create(gname, &gconf);
	if (graph_id == RTE_GRAPH_ID_INVALID) {
		printf("Graph creation failed with error = %d\n", rte_errno);
		goto pattern_name_free;
	}
	graph_data->graph_id = graph_id;

	free(node_map);
	for (i = 0; i < graph_data->nb_nodes; i++)
		free(node_patterns[i]);
	free(snk_nodes);
	free(src_nodes);
	free(node_patterns);
	return 0;

pattern_name_free:
	free(node_map);
	for (i = 0; i < graph_data->nb_nodes; i++)
		free(node_patterns[i]);
snk_free:
	free(snk_nodes);
src_free:
	free(src_nodes);
pattern_free:
	free(node_patterns);
data_free:
	free(graph_data->node_data);
memzone_free:
	rte_memzone_free(mz);
	return -ENOMEM;
}

/* Worker thread function */
static int
_graph_perf_wrapper(void *args)
{
	struct graph_lcore_data *data = args;
	struct rte_graph *graph;

	/* Lookup graph */
	graph = rte_graph_lookup(rte_graph_id_to_name(data->graph_id));

	/* Graph walk until done */
	while (!data->done)
		rte_graph_walk(graph);

	return 0;
}

static int
measure_perf_get(rte_graph_t graph_id)
{
	const char *pattern = rte_graph_id_to_name(graph_id);
	uint32_t lcore_id = rte_get_next_lcore(-1, 1, 0);
	struct rte_graph_cluster_stats_param param;
	struct rte_graph_cluster_stats *stats;
	struct graph_lcore_data *data;

	data = rte_zmalloc("Graph_perf", sizeof(struct graph_lcore_data),
			   RTE_CACHE_LINE_SIZE);
	data->graph_id = graph_id;
	data->done = 0;

	/* Run graph worker thread function */
	rte_eal_remote_launch(_graph_perf_wrapper, data, lcore_id);

	/* Collect stats for few msecs */
	if (rte_graph_has_stats_feature()) {
		memset(&param, 0, sizeof(param));
		param.f = stdout;
		param.socket_id = SOCKET_ID_ANY;
		param.graph_patterns = &pattern;
		param.nb_graph_patterns = 1;

		stats = rte_graph_cluster_stats_create(&param);
		if (stats == NULL) {
			printf("Failed to create stats\n");
			return -ENOMEM;
		}

		rte_delay_ms(3E2);
		rte_graph_cluster_stats_get(stats, true);
		rte_delay_ms(1E3);
		rte_graph_cluster_stats_get(stats, false);
		rte_graph_cluster_stats_destroy(stats);
	} else
		rte_delay_ms(1E3);

	data->done = 1;
	rte_eal_wait_lcore(lcore_id);

	return 0;
}

static inline void
graph_fini(void)
{
	const struct rte_memzone *mz = rte_memzone_lookup(TEST_GRAPH_PERF_MZ);
	struct test_graph_perf *graph_data;

	if (mz == NULL)
		return;
	graph_data = mz->addr;

	rte_graph_destroy(graph_data->graph_id);
	free(graph_data->node_data);
	rte_memzone_free(rte_memzone_lookup(TEST_GRAPH_PERF_MZ));
}

static int
measure_perf(void)
{
	const struct rte_memzone *mz;
	struct test_graph_perf *graph_data;

	mz = rte_memzone_lookup(TEST_GRAPH_PERF_MZ);
	if (mz == NULL)
		return -ENOMEM;
	graph_data = mz->addr;

	return measure_perf_get(graph_data->graph_id);
}

static inline int
graph_hr_4s_1n_1src_1snk(void)
{
	return measure_perf();
}

static inline int
graph_hr_4s_1n_1src_1snk_brst_one(void)
{
	return measure_perf();
}

static inline int
graph_hr_4s_1n_2src_1snk(void)
{
	return measure_perf();
}

static inline int
graph_hr_4s_1n_1src_2snk(void)
{
	return measure_perf();
}

static inline int
graph_tree_4s_4n_1src_4snk(void)
{
	return measure_perf();
}

static inline int
graph_reverse_tree_3s_4n_1src_1snk(void)
{
	return measure_perf();
}

static inline int
graph_parallel_tree_5s_4n_4src_4snk(void)
{
	return measure_perf();
}

/* Graph Topology
 * nodes per stage:	1
 * stages:		4
 * src:			1
 * sink:		1
 */
static inline int
graph_init_hr(void)
{
	uint8_t edge_map[][1][1] = {
		{ {100} },
		{ {100} },
		{ {100} },
		{ {100} },
	};
	uint8_t src_map[][1] = { {100} };
	uint8_t snk_map[][1] = { {100} };

	return graph_init("graph_hr", SOURCES(src_map), SINKS(snk_map),
			  STAGES(edge_map), NODES_PER_STAGE(edge_map), src_map,
			  snk_map, edge_map, 0);
}

/* Graph Topology
 * nodes per stage:	1
 * stages:		4
 * src:			1
 * sink:		1
 */
static inline int
graph_init_hr_brst_one(void)
{
	uint8_t edge_map[][1][1] = {
		{ {100} },
		{ {100} },
		{ {100} },
		{ {100} },
	};
	uint8_t src_map[][1] = { {100} };
	uint8_t snk_map[][1] = { {100} };

	return graph_init("graph_hr", SOURCES(src_map), SINKS(snk_map),
			  STAGES(edge_map), NODES_PER_STAGE(edge_map), src_map,
			  snk_map, edge_map, 1);
}

/* Graph Topology
 * nodes per stage:	1
 * stages:		4
 * src:			2
 * sink:		1
 */
static inline int
graph_init_hr_multi_src(void)
{
	uint8_t edge_map[][1][1] = {
		{ {100} },
		{ {100} },
		{ {100} },
		{ {100} },
	};
	uint8_t src_map[][1] = {
		{100}, {100}
	};
	uint8_t snk_map[][1] = { {100} };

	return graph_init("graph_hr", SOURCES(src_map), SINKS(snk_map),
			  STAGES(edge_map), NODES_PER_STAGE(edge_map), src_map,
			  snk_map, edge_map, 0);
}

/* Graph Topology
 * nodes per stage:	1
 * stages:		4
 * src:			1
 * sink:		2
 */
static inline int
graph_init_hr_multi_snk(void)
{
	uint8_t edge_map[][1][1] = {
		{ {100} },
		{ {100} },
		{ {100} },
		{ {100} },
	};
	uint8_t src_map[][1] = { {100} };
	uint8_t snk_map[][2] = { {50, 50} };

	return graph_init("graph_hr", SOURCES(src_map), SINKS(snk_map),
			  STAGES(edge_map), NODES_PER_STAGE(edge_map), src_map,
			  snk_map, edge_map, 0);
}

/* Graph Topology
 * nodes per stage:	4
 * stages:		4
 * src:			1
 * sink:		4
 */
static inline int
graph_init_tree(void)
{
	uint8_t edge_map[][4][4] = {
		{
			{100, 0, 0, 0},
			{0, 0, 0, 0},
			{0, 0, 0, 0},
			{0, 0, 0, 0}
		},
		{
			{50, 0, 0, 0},
			{50, 0, 0, 0},
			{0, 0, 0, 0},
			{0, 0, 0, 0}
		},
		{
			{33, 33, 0, 0},
			{34, 34, 0, 0},
			{33, 33, 0, 0},
			{0, 0, 0, 0}
		},
		{
			{25, 25, 25, 0},
			{25, 25, 25, 0},
			{25, 25, 25, 0},
			{25, 25, 25, 0}
		}
	};
	uint8_t src_map[][4] = { {100, 0, 0, 0} };
	uint8_t snk_map[][4] = {
		{100, 0, 0, 0},
		{0, 100, 0, 0},
		{0, 0, 100, 0},
		{0, 0, 0, 100}
	};

	return graph_init("graph_full_split", SOURCES(src_map), SINKS(snk_map),
			  STAGES(edge_map), NODES_PER_STAGE(edge_map), src_map,
			  snk_map, edge_map, 0);
}

/* Graph Topology
 * nodes per stage:	4
 * stages:		3
 * src:			1
 * sink:		1
 */
static inline int
graph_init_reverse_tree(void)
{
	uint8_t edge_map[][4][4] = {
		{
			{25, 25, 25, 25},
			{25, 25, 25, 25},
			{25, 25, 25, 25},
			{25, 25, 25, 25}
		},
		{
			{33, 33, 33, 33},
			{33, 33, 33, 33},
			{34, 34, 34, 34},
			{0, 0, 0, 0}
		},
		{
			{50, 50, 50, 0},
			{50, 50, 50, 0},
			{0, 0, 0, 0},
			{0, 0, 0, 0}
		},
	};
	uint8_t src_map[][4] = { {25, 25, 25, 25} };
	uint8_t snk_map[][1] = { {100}, {100}, {0}, {0} };

	return graph_init("graph_full_split", SOURCES(src_map), SINKS(snk_map),
			  STAGES(edge_map), NODES_PER_STAGE(edge_map), src_map,
			  snk_map, edge_map, 0);
}

/* Graph Topology
 * nodes per stage:	4
 * stages:		5
 * src:			4
 * sink:		4
 */
static inline int
graph_init_parallel_tree(void)
{
	uint8_t edge_map[][4][4] = {
		{
			{100, 0, 0, 0},
			{0, 100, 0, 0},
			{0, 0, 100, 0},
			{0, 0, 0, 100}
		},
		{
			{100, 0, 0, 0},
			{0, 100, 0, 0},
			{0, 0, 100, 0},
			{0, 0, 0, 100}
		},
		{
			{100, 0, 0, 0},
			{0, 100, 0, 0},
			{0, 0, 100, 0},
			{0, 0, 0, 100}
		},
		{
			{100, 0, 0, 0},
			{0, 100, 0, 0},
			{0, 0, 100, 0},
			{0, 0, 0, 100}
		},
		{
			{100, 0, 0, 0},
			{0, 100, 0, 0},
			{0, 0, 100, 0},
			{0, 0, 0, 100}
		},
	};
	uint8_t src_map[][4] = {
		{100, 0, 0, 0},
		{0, 100, 0, 0},
		{0, 0, 100, 0},
		{0, 0, 0, 100}
	};
	uint8_t snk_map[][4] = {
		{100, 0, 0, 0},
		{0, 100, 0, 0},
		{0, 0, 100, 0},
		{0, 0, 0, 100}
	};

	return graph_init("graph_parallel", SOURCES(src_map), SINKS(snk_map),
			  STAGES(edge_map), NODES_PER_STAGE(edge_map), src_map,
			  snk_map, edge_map, 0);
}

/** Graph Creation cheat sheet
 *  edge_map -> dictates graph flow from worker stage 0 to worker stage n-1.
 *  src_map  -> dictates source nodes enqueue percentage to worker stage 0.
 *  snk_map  -> dictates stage n-1 enqueue percentage to sink.
 *
 *  Layout:
 *  edge_map[<nb_stages>][<nodes_per_stg>][<nodes_in_nxt_stg = nodes_per_stg>]
 *  src_map[<nb_sources>][<nodes_in_stage0 = nodes_per_stage>]
 *  snk_map[<nodes_in_stage(n-1) = nodes_per_stage>][<nb_sinks>]
 *
 *  The last array dictates the percentage of received objs to enqueue to next
 *  stage.
 *
 *  Note: edge_map[][0][] will always be unused as it will receive from source
 *
 *  Example:
 *	Graph:
 *	http://bit.ly/2PqbqOy
 *	Each stage(n) connects to all nodes in the next stage in decreasing
 *	order.
 *	Since we can't resize the edge_map dynamically we get away by creating
 *	dummy nodes and assigning 0 percentages.
 *	Max nodes across all stages = 4
 *	stages = 3
 *	nb_src = 1
 *	nb_snk = 1
 *			   // Stages
 *	edge_map[][4][4] = {
 *		// Nodes per stage
 *		{
 *		    {25, 25, 25, 25},
 *		    {25, 25, 25, 25},
 *		    {25, 25, 25, 25},
 *		    {25, 25, 25, 25}
 *		},	// This will be unused.
 *		{
 *		    // Nodes enabled in current stage + prev stage enq %
 *		    {33, 33, 33, 33},
 *		    {33, 33, 33, 33},
 *		    {34, 34, 34, 34},
 *		    {0, 0, 0, 0}
 *		},
 *		{
 *		    {50, 50, 50, 0},
 *		    {50, 50, 50, 0},
 *		    {0, 0, 0, 0},
 *		    {0, 0, 0, 0}
 *		},
 *	};
 *	Above, each stage tells how much it should receive from previous except
 *	from stage_0.
 *
 *	src_map[][4] = { {25, 25, 25, 25} };
 *	Here, we tell each source the % it has to send to stage_0 nodes. In
 *	case we want 2 source node we can declare as
 *	src_map[][4] = { {25, 25, 25, 25}, {25, 25, 25, 25} };
 *
 *	snk_map[][1] = { {100}, {100}, {0}, {0} }
 *	Here, we tell stage - 1 nodes how much to enqueue to sink_0.
 *	If we have 2 sinks we can do as follows
 *	snk_map[][2] = { {50, 50}, {50, 50}, {0, 0}, {0, 0} }
 */

static struct unit_test_suite graph_perf_testsuite = {
	.suite_name = "Graph library performance test suite",
	.setup = graph_perf_setup,
	.teardown = graph_perf_teardown,
	.unit_test_cases = {
		TEST_CASE_ST(graph_init_hr, graph_fini,
			     graph_hr_4s_1n_1src_1snk),
		TEST_CASE_ST(graph_init_hr_brst_one, graph_fini,
			     graph_hr_4s_1n_1src_1snk_brst_one),
		TEST_CASE_ST(graph_init_hr_multi_src, graph_fini,
			     graph_hr_4s_1n_2src_1snk),
		TEST_CASE_ST(graph_init_hr_multi_snk, graph_fini,
			     graph_hr_4s_1n_1src_2snk),
		TEST_CASE_ST(graph_init_tree, graph_fini,
			     graph_tree_4s_4n_1src_4snk),
		TEST_CASE_ST(graph_init_reverse_tree, graph_fini,
			     graph_reverse_tree_3s_4n_1src_1snk),
		TEST_CASE_ST(graph_init_parallel_tree, graph_fini,
			     graph_parallel_tree_5s_4n_4src_4snk),
		TEST_CASES_END(), /**< NULL terminate unit test array */
	},
};

static int
test_graph_perf_func(void)
{
	return unit_test_suite_runner(&graph_perf_testsuite);
}

REGISTER_TEST_COMMAND(graph_perf_autotest, test_graph_perf_func);