xref: /dpdk/app/test-bbdev/main.c (revision bd03d3f1e4f1734c70bf6be32cdeb5e3ae6fa611)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Intel Corporation
3  */
4 
5 #include <getopt.h>
6 #include <inttypes.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <stdbool.h>
10 
11 #include <rte_eal.h>
12 #include <rte_common.h>
13 #include <rte_string_fns.h>
14 #include <rte_cycles.h>
15 #include <rte_lcore.h>
16 
17 #include "main.h"
18 
19 /* Defines how many testcases can be specified as cmdline args */
20 #define MAX_CMDLINE_TESTCASES 8
21 
22 static const char tc_sep = ',';
23 
24 static struct test_params {
25 	struct test_command *test_to_run[MAX_CMDLINE_TESTCASES];
26 	unsigned int num_tests;
27 	unsigned int num_ops;
28 	unsigned int burst_sz;
29 	unsigned int num_lcores;
30 	char test_vector_filename[PATH_MAX];
31 } test_params;
32 
33 static struct test_commands_list commands_list =
34 	TAILQ_HEAD_INITIALIZER(commands_list);
35 
36 void
37 add_test_command(struct test_command *t)
38 {
39 	TAILQ_INSERT_TAIL(&commands_list, t, next);
40 }
41 
42 int
43 unit_test_suite_runner(struct unit_test_suite *suite)
44 {
45 	int test_result = TEST_SUCCESS;
46 	unsigned int total = 0, skipped = 0, succeeded = 0, failed = 0;
47 	uint64_t start, end;
48 
49 	printf(
50 			"\n + ------------------------------------------------------- +\n");
51 	printf(" + Starting Test Suite : %s\n", suite->suite_name);
52 
53 	start = rte_rdtsc_precise();
54 
55 	if (suite->setup) {
56 		test_result = suite->setup();
57 		if (test_result == TEST_FAILED) {
58 			printf(" + Test suite setup %s failed!\n",
59 					suite->suite_name);
60 			printf(
61 					" + ------------------------------------------------------- +\n");
62 			return 1;
63 		}
64 		if (test_result == TEST_SKIPPED) {
65 			printf(" + Test suite setup %s skipped!\n",
66 					suite->suite_name);
67 			printf(
68 					" + ------------------------------------------------------- +\n");
69 			return 0;
70 		}
71 	}
72 
73 	while (suite->unit_test_cases[total].testcase) {
74 		if (suite->unit_test_cases[total].setup)
75 			test_result = suite->unit_test_cases[total].setup();
76 
77 		if (test_result == TEST_SUCCESS)
78 			test_result = suite->unit_test_cases[total].testcase();
79 
80 		if (suite->unit_test_cases[total].teardown)
81 			suite->unit_test_cases[total].teardown();
82 
83 		if (test_result == TEST_SUCCESS) {
84 			succeeded++;
85 			printf(" + TestCase [%2d] : %s passed\n", total,
86 					suite->unit_test_cases[total].name);
87 		} else if (test_result == TEST_SKIPPED) {
88 			skipped++;
89 			printf(" + TestCase [%2d] : %s skipped\n", total,
90 					suite->unit_test_cases[total].name);
91 		} else {
92 			failed++;
93 			printf(" + TestCase [%2d] : %s failed\n", total,
94 					suite->unit_test_cases[total].name);
95 		}
96 
97 		total++;
98 	}
99 
100 	/* Run test suite teardown */
101 	if (suite->teardown)
102 		suite->teardown();
103 
104 	end = rte_rdtsc_precise();
105 
106 	printf(" + ------------------------------------------------------- +\n");
107 	printf(" + Test Suite Summary : %s\n", suite->suite_name);
108 	printf(" + Tests Total :       %2d\n", total);
109 	printf(" + Tests Skipped :     %2d\n", skipped);
110 	printf(" + Tests Passed :      %2d\n", succeeded);
111 	printf(" + Tests Failed :      %2d\n", failed);
112 	printf(" + Tests Lasted :       %lg ms\n",
113 			((end - start) * 1000) / (double)rte_get_tsc_hz());
114 	printf(" + ------------------------------------------------------- +\n");
115 
116 	return (failed > 0) ? 1 : 0;
117 }
118 
119 const char *
120 get_vector_filename(void)
121 {
122 	return test_params.test_vector_filename;
123 }
124 
125 unsigned int
126 get_num_ops(void)
127 {
128 	return test_params.num_ops;
129 }
130 
131 unsigned int
132 get_burst_sz(void)
133 {
134 	return test_params.burst_sz;
135 }
136 
137 unsigned int
138 get_num_lcores(void)
139 {
140 	return test_params.num_lcores;
141 }
142 
143 static void
144 print_usage(const char *prog_name)
145 {
146 	struct test_command *t;
147 
148 	printf("Usage: %s [EAL params] [-- [-n/--num-ops NUM_OPS]\n"
149 			"\t[-b/--burst-size BURST_SIZE]\n"
150 			"\t[-v/--test-vector VECTOR_FILE]\n"
151 			"\t[-c/--test-cases TEST_CASE[,TEST_CASE,...]]]\n",
152 			prog_name);
153 
154 	printf("Available testcases: ");
155 	TAILQ_FOREACH(t, &commands_list, next)
156 		printf("%s ", t->command);
157 	printf("\n");
158 }
159 
160 static int
161 parse_args(int argc, char **argv, struct test_params *tp)
162 {
163 	int opt, option_index;
164 	unsigned int num_tests = 0;
165 	bool test_cases_present = false;
166 	bool test_vector_present = false;
167 	struct test_command *t;
168 	char *tokens[MAX_CMDLINE_TESTCASES];
169 	int tc, ret;
170 
171 	static struct option lgopts[] = {
172 		{ "num-ops", 1, 0, 'n' },
173 		{ "burst-size", 1, 0, 'b' },
174 		{ "test-cases", 1, 0, 'c' },
175 		{ "test-vector", 1, 0, 'v' },
176 		{ "lcores", 1, 0, 'l' },
177 		{ "help", 0, 0, 'h' },
178 		{ NULL,  0, 0, 0 }
179 	};
180 
181 	while ((opt = getopt_long(argc, argv, "hn:b:c:v:l:", lgopts,
182 			&option_index)) != EOF)
183 		switch (opt) {
184 		case 'n':
185 			TEST_ASSERT(strlen(optarg) > 0,
186 					"Num of operations is not provided");
187 			tp->num_ops = strtol(optarg, NULL, 10);
188 			break;
189 		case 'b':
190 			TEST_ASSERT(strlen(optarg) > 0,
191 					"Burst size is not provided");
192 			tp->burst_sz = strtol(optarg, NULL, 10);
193 			TEST_ASSERT(tp->burst_sz <= MAX_BURST,
194 					"Burst size mustn't be greater than %u",
195 					MAX_BURST);
196 			break;
197 		case 'c':
198 			TEST_ASSERT(test_cases_present == false,
199 					"Test cases provided more than once");
200 			test_cases_present = true;
201 
202 			ret = rte_strsplit(optarg, strlen(optarg),
203 					tokens, MAX_CMDLINE_TESTCASES, tc_sep);
204 
205 			TEST_ASSERT(ret <= MAX_CMDLINE_TESTCASES,
206 					"Too many test cases (max=%d)",
207 					MAX_CMDLINE_TESTCASES);
208 
209 			for (tc = 0; tc < ret; ++tc) {
210 				/* Find matching test case */
211 				TAILQ_FOREACH(t, &commands_list, next)
212 					if (!strcmp(tokens[tc], t->command))
213 						tp->test_to_run[num_tests] = t;
214 
215 				TEST_ASSERT(tp->test_to_run[num_tests] != NULL,
216 						"Unknown test case: %s",
217 						tokens[tc]);
218 				++num_tests;
219 			}
220 			break;
221 		case 'v':
222 			TEST_ASSERT(test_vector_present == false,
223 					"Test vector provided more than once");
224 			test_vector_present = true;
225 
226 			TEST_ASSERT(strlen(optarg) > 0,
227 					"Config file name is null");
228 
229 			snprintf(tp->test_vector_filename,
230 					sizeof(tp->test_vector_filename),
231 					"%s", optarg);
232 			break;
233 		case 'l':
234 			TEST_ASSERT(strlen(optarg) > 0,
235 					"Num of lcores is not provided");
236 			tp->num_lcores = strtol(optarg, NULL, 10);
237 			TEST_ASSERT(tp->num_lcores <= RTE_MAX_LCORE,
238 					"Num of lcores mustn't be greater than %u",
239 					RTE_MAX_LCORE);
240 			break;
241 		case 'h':
242 			print_usage(argv[0]);
243 			return 0;
244 		default:
245 			printf("ERROR: Unknown option: -%c\n", opt);
246 			return -1;
247 		}
248 
249 	if (tp->num_ops == 0) {
250 		printf(
251 			"WARNING: Num of operations was not provided or was set 0. Set to default (%u)\n",
252 			DEFAULT_OPS);
253 		tp->num_ops = DEFAULT_OPS;
254 	}
255 	if (tp->burst_sz == 0) {
256 		printf(
257 			"WARNING: Burst size was not provided or was set 0. Set to default (%u)\n",
258 			DEFAULT_BURST);
259 		tp->burst_sz = DEFAULT_BURST;
260 	}
261 	if (tp->num_lcores == 0) {
262 		printf(
263 			"WARNING: Num of lcores was not provided or was set 0. Set to value from RTE config (%u)\n",
264 			rte_lcore_count());
265 		tp->num_lcores = rte_lcore_count();
266 	}
267 
268 	TEST_ASSERT(tp->burst_sz <= tp->num_ops,
269 			"Burst size (%u) mustn't be greater than num ops (%u)",
270 			tp->burst_sz, tp->num_ops);
271 
272 	tp->num_tests = num_tests;
273 	return 0;
274 }
275 
276 static int
277 run_all_tests(void)
278 {
279 	int ret = TEST_SUCCESS;
280 	struct test_command *t;
281 
282 	TAILQ_FOREACH(t, &commands_list, next)
283 		ret |= t->callback();
284 
285 	return ret;
286 }
287 
288 static int
289 run_parsed_tests(struct test_params *tp)
290 {
291 	int ret = TEST_SUCCESS;
292 	unsigned int i;
293 
294 	for (i = 0; i < tp->num_tests; ++i)
295 		ret |= tp->test_to_run[i]->callback();
296 
297 	return ret;
298 }
299 
300 int
301 main(int argc, char **argv)
302 {
303 	int ret;
304 
305 	/* Init EAL */
306 	ret = rte_eal_init(argc, argv);
307 	if (ret < 0)
308 		return 1;
309 	argc -= ret;
310 	argv += ret;
311 
312 	/* Parse application arguments (after the EAL ones) */
313 	ret = parse_args(argc, argv, &test_params);
314 	if (ret < 0) {
315 		print_usage(argv[0]);
316 		return 1;
317 	}
318 
319 	/* If no argument provided - run all tests */
320 	if (test_params.num_tests == 0)
321 		return run_all_tests();
322 	else
323 		return run_parsed_tests(&test_params);
324 }
325