xref: /dpdk/app/test-compress-perf/main.c (revision 72c64a3492ba7b41bc1074e016684643c30be04d)
1e0b6287cSTomasz Jozwiak /* SPDX-License-Identifier: BSD-3-Clause
2e0b6287cSTomasz Jozwiak  * Copyright(c) 2018 Intel Corporation
3e0b6287cSTomasz Jozwiak  */
4e0b6287cSTomasz Jozwiak 
572b452c5SDmitry Kozlyuk #include <stdlib.h>
6d6cec113STomasz Jozwiak #include <signal.h>
7d6cec113STomasz Jozwiak #include <sys/types.h>
8d6cec113STomasz Jozwiak #include <unistd.h>
9d6cec113STomasz Jozwiak 
10e0b6287cSTomasz Jozwiak #include <rte_malloc.h>
11e0b6287cSTomasz Jozwiak #include <rte_eal.h>
12e0b6287cSTomasz Jozwiak #include <rte_log.h>
13e0b6287cSTomasz Jozwiak #include <rte_compressdev.h>
14e0b6287cSTomasz Jozwiak 
15424dd6c8STomasz Jozwiak #include "comp_perf.h"
162695db95SArtur Trybula #include "comp_perf_options.h"
17424dd6c8STomasz Jozwiak #include "comp_perf_test_common.h"
182695db95SArtur Trybula #include "comp_perf_test_cyclecount.h"
192695db95SArtur Trybula #include "comp_perf_test_throughput.h"
202695db95SArtur Trybula #include "comp_perf_test_verify.h"
21e0b6287cSTomasz Jozwiak 
22b68a8242STomasz Jozwiak #define NUM_MAX_XFORMS 16
23b68a8242STomasz Jozwiak #define NUM_MAX_INFLIGHT_OPS 512
24b68a8242STomasz Jozwiak 
25424dd6c8STomasz Jozwiak __extension__
264fba6df9SLavanya Govindarajan const char *comp_perf_test_type_strs[] = {
272695db95SArtur Trybula 	[CPERF_TEST_TYPE_THROUGHPUT] = "throughput",
282695db95SArtur Trybula 	[CPERF_TEST_TYPE_VERIFY] = "verify",
292695db95SArtur Trybula 	[CPERF_TEST_TYPE_PMDCC] = "pmd-cyclecount"
30424dd6c8STomasz Jozwiak };
31b68a8242STomasz Jozwiak 
32424dd6c8STomasz Jozwiak __extension__
33424dd6c8STomasz Jozwiak static const struct cperf_test cperf_testmap[] = {
342695db95SArtur Trybula 	[CPERF_TEST_TYPE_THROUGHPUT] = {
352695db95SArtur Trybula 			cperf_throughput_test_constructor,
362695db95SArtur Trybula 			cperf_throughput_test_runner,
372695db95SArtur Trybula 			cperf_throughput_test_destructor
382695db95SArtur Trybula 
39424dd6c8STomasz Jozwiak 	},
40424dd6c8STomasz Jozwiak 	[CPERF_TEST_TYPE_VERIFY] = {
41424dd6c8STomasz Jozwiak 			cperf_verify_test_constructor,
42424dd6c8STomasz Jozwiak 			cperf_verify_test_runner,
43424dd6c8STomasz Jozwiak 			cperf_verify_test_destructor
442695db95SArtur Trybula 	},
452695db95SArtur Trybula 
462695db95SArtur Trybula 	[CPERF_TEST_TYPE_PMDCC] = {
472695db95SArtur Trybula 			cperf_cyclecount_test_constructor,
482695db95SArtur Trybula 			cperf_cyclecount_test_runner,
492695db95SArtur Trybula 			cperf_cyclecount_test_destructor
50b68a8242STomasz Jozwiak 	}
51424dd6c8STomasz Jozwiak };
52b68a8242STomasz Jozwiak 
53d6cec113STomasz Jozwiak static struct comp_test_data *test_data;
54d6cec113STomasz Jozwiak 
55b68a8242STomasz Jozwiak static int
comp_perf_check_capabilities(struct comp_test_data * test_data,uint8_t cdev_id)56424dd6c8STomasz Jozwiak comp_perf_check_capabilities(struct comp_test_data *test_data, uint8_t cdev_id)
57b68a8242STomasz Jozwiak {
58b68a8242STomasz Jozwiak 	const struct rte_compressdev_capabilities *cap;
59b68a8242STomasz Jozwiak 
60*72c64a34SMichael Baum 	cap = rte_compressdev_capability_get(cdev_id, test_data->test_algo);
61b68a8242STomasz Jozwiak 
62b68a8242STomasz Jozwiak 	if (cap == NULL) {
63b68a8242STomasz Jozwiak 		RTE_LOG(ERR, USER1,
64*72c64a34SMichael Baum 			"Compress device does not support %u algorithm\n",
65*72c64a34SMichael Baum 			test_data->test_algo);
66b68a8242STomasz Jozwiak 		return -1;
67b68a8242STomasz Jozwiak 	}
68b68a8242STomasz Jozwiak 
69b68a8242STomasz Jozwiak 	uint64_t comp_flags = cap->comp_feature_flags;
70b68a8242STomasz Jozwiak 
71*72c64a34SMichael Baum 	/* Algorithm type */
72*72c64a34SMichael Baum 	switch (test_data->test_algo) {
73*72c64a34SMichael Baum 	case RTE_COMP_ALGO_DEFLATE:
747be78d02SJosh Soref 		/* Huffman encoding */
75b68a8242STomasz Jozwiak 		if (test_data->huffman_enc == RTE_COMP_HUFFMAN_FIXED &&
76b68a8242STomasz Jozwiak 		    (comp_flags & RTE_COMP_FF_HUFFMAN_FIXED) == 0) {
77b68a8242STomasz Jozwiak 			RTE_LOG(ERR, USER1,
78b68a8242STomasz Jozwiak 				"Compress device does not supported Fixed Huffman\n");
79b68a8242STomasz Jozwiak 			return -1;
80b68a8242STomasz Jozwiak 		}
81b68a8242STomasz Jozwiak 
82b68a8242STomasz Jozwiak 		if (test_data->huffman_enc == RTE_COMP_HUFFMAN_DYNAMIC &&
83b68a8242STomasz Jozwiak 		    (comp_flags & RTE_COMP_FF_HUFFMAN_DYNAMIC) == 0) {
84b68a8242STomasz Jozwiak 			RTE_LOG(ERR, USER1,
85b68a8242STomasz Jozwiak 				"Compress device does not supported Dynamic Huffman\n");
86b68a8242STomasz Jozwiak 			return -1;
87b68a8242STomasz Jozwiak 		}
88*72c64a34SMichael Baum 		break;
89*72c64a34SMichael Baum 	case RTE_COMP_ALGO_LZ4:
90*72c64a34SMichael Baum 		/* LZ4 flags */
91*72c64a34SMichael Baum 		if ((test_data->lz4_flags & RTE_COMP_LZ4_FLAG_BLOCK_CHECKSUM) &&
92*72c64a34SMichael Baum 		    (comp_flags & RTE_COMP_FF_LZ4_BLOCK_WITH_CHECKSUM) == 0) {
93*72c64a34SMichael Baum 			RTE_LOG(ERR, USER1,
94*72c64a34SMichael Baum 				"Compress device does not support LZ4 block with checksum\n");
95*72c64a34SMichael Baum 			return -1;
96*72c64a34SMichael Baum 		}
97*72c64a34SMichael Baum 
98*72c64a34SMichael Baum 		if ((test_data->lz4_flags &
99*72c64a34SMichael Baum 		     RTE_COMP_LZ4_FLAG_BLOCK_INDEPENDENCE) &&
100*72c64a34SMichael Baum 		    (comp_flags & RTE_COMP_FF_LZ4_BLOCK_INDEPENDENCE) == 0) {
101*72c64a34SMichael Baum 			RTE_LOG(ERR, USER1,
102*72c64a34SMichael Baum 				"Compress device does not support LZ4 independent blocks\n");
103*72c64a34SMichael Baum 			return -1;
104*72c64a34SMichael Baum 		}
105*72c64a34SMichael Baum 		break;
106*72c64a34SMichael Baum 	case RTE_COMP_ALGO_LZS:
107*72c64a34SMichael Baum 	case RTE_COMP_ALGO_NULL:
108*72c64a34SMichael Baum 		break;
109*72c64a34SMichael Baum 	default:
110*72c64a34SMichael Baum 		return -1;
111*72c64a34SMichael Baum 	}
112b68a8242STomasz Jozwiak 
113b68a8242STomasz Jozwiak 	/* Window size */
114b68a8242STomasz Jozwiak 	if (test_data->window_sz != -1) {
115b68a8242STomasz Jozwiak 		if (param_range_check(test_data->window_sz, &cap->window_size)
116b68a8242STomasz Jozwiak 				< 0) {
117b68a8242STomasz Jozwiak 			RTE_LOG(ERR, USER1,
118b68a8242STomasz Jozwiak 				"Compress device does not support "
119b68a8242STomasz Jozwiak 				"this window size\n");
120b68a8242STomasz Jozwiak 			return -1;
121b68a8242STomasz Jozwiak 		}
122b68a8242STomasz Jozwiak 	} else
123b68a8242STomasz Jozwiak 		/* Set window size to PMD maximum if none was specified */
124b68a8242STomasz Jozwiak 		test_data->window_sz = cap->window_size.max;
125b68a8242STomasz Jozwiak 
126b68a8242STomasz Jozwiak 	/* Check if chained mbufs is supported */
127b68a8242STomasz Jozwiak 	if (test_data->max_sgl_segs > 1  &&
128b68a8242STomasz Jozwiak 			(comp_flags & RTE_COMP_FF_OOP_SGL_IN_SGL_OUT) == 0) {
129b68a8242STomasz Jozwiak 		RTE_LOG(INFO, USER1, "Compress device does not support "
130b68a8242STomasz Jozwiak 				"chained mbufs. Max SGL segments set to 1\n");
131b68a8242STomasz Jozwiak 		test_data->max_sgl_segs = 1;
132b68a8242STomasz Jozwiak 	}
133b68a8242STomasz Jozwiak 
134b68a8242STomasz Jozwiak 	/* Level 0 support */
135424dd6c8STomasz Jozwiak 	if (test_data->level_lst.min == 0 &&
136b68a8242STomasz Jozwiak 			(comp_flags & RTE_COMP_FF_NONCOMPRESSED_BLOCKS) == 0) {
137b68a8242STomasz Jozwiak 		RTE_LOG(ERR, USER1, "Compress device does not support "
138b68a8242STomasz Jozwiak 				"level 0 (no compression)\n");
139b68a8242STomasz Jozwiak 		return -1;
140b68a8242STomasz Jozwiak 	}
141b68a8242STomasz Jozwiak 
142b68a8242STomasz Jozwiak 	return 0;
143b68a8242STomasz Jozwiak }
144b68a8242STomasz Jozwiak 
145b68a8242STomasz Jozwiak static int
comp_perf_initialize_compressdev(struct comp_test_data * test_data,uint8_t * enabled_cdevs)146424dd6c8STomasz Jozwiak comp_perf_initialize_compressdev(struct comp_test_data *test_data,
147424dd6c8STomasz Jozwiak 				 uint8_t *enabled_cdevs)
148b68a8242STomasz Jozwiak {
149424dd6c8STomasz Jozwiak 	uint8_t enabled_cdev_count, nb_lcores, cdev_id;
150424dd6c8STomasz Jozwiak 	unsigned int i, j;
151424dd6c8STomasz Jozwiak 	int ret;
15227cee417STomasz Jozwiak 
153424dd6c8STomasz Jozwiak 	enabled_cdev_count = rte_compressdev_devices_get(test_data->driver_name,
154424dd6c8STomasz Jozwiak 			enabled_cdevs, RTE_COMPRESS_MAX_DEVS);
155424dd6c8STomasz Jozwiak 	if (enabled_cdev_count == 0) {
1562695db95SArtur Trybula 		RTE_LOG(ERR, USER1, "No compress devices type %s available,"
1572695db95SArtur Trybula 				    " please check the list of specified devices in EAL section\n",
158424dd6c8STomasz Jozwiak 				test_data->driver_name);
159424dd6c8STomasz Jozwiak 		return -EINVAL;
160424dd6c8STomasz Jozwiak 	}
161424dd6c8STomasz Jozwiak 
162424dd6c8STomasz Jozwiak 	nb_lcores = rte_lcore_count() - 1;
163424dd6c8STomasz Jozwiak 	/*
164424dd6c8STomasz Jozwiak 	 * Use fewer devices,
165424dd6c8STomasz Jozwiak 	 * if there are more available than cores.
166b68a8242STomasz Jozwiak 	 */
167424dd6c8STomasz Jozwiak 	if (enabled_cdev_count > nb_lcores) {
16820972557SAdam Dybkowski 		if (nb_lcores == 0) {
16920972557SAdam Dybkowski 			RTE_LOG(ERR, USER1, "Cannot run with 0 cores! Increase the number of cores\n");
17020972557SAdam Dybkowski 			return -EINVAL;
17120972557SAdam Dybkowski 		}
172424dd6c8STomasz Jozwiak 		enabled_cdev_count = nb_lcores;
173424dd6c8STomasz Jozwiak 		RTE_LOG(INFO, USER1,
174424dd6c8STomasz Jozwiak 			"There's more available devices than cores!"
175424dd6c8STomasz Jozwiak 			" The number of devices has been aligned to %d cores\n",
176424dd6c8STomasz Jozwiak 			nb_lcores);
177b68a8242STomasz Jozwiak 	}
178b68a8242STomasz Jozwiak 
179b68a8242STomasz Jozwiak 	/*
180424dd6c8STomasz Jozwiak 	 * Calculate number of needed queue pairs, based on the amount
181424dd6c8STomasz Jozwiak 	 * of available number of logical cores and compression devices.
182424dd6c8STomasz Jozwiak 	 * For instance, if there are 4 cores and 2 compression devices,
183424dd6c8STomasz Jozwiak 	 * 2 queue pairs will be set up per device.
184424dd6c8STomasz Jozwiak 	 * One queue pair per one core.
185424dd6c8STomasz Jozwiak 	 * if e.g.: there're 3 cores and 2 compression devices,
186424dd6c8STomasz Jozwiak 	 * 2 queue pairs will be set up per device but one queue pair
187424dd6c8STomasz Jozwiak 	 * will left unused in the last one device
188b68a8242STomasz Jozwiak 	 */
189424dd6c8STomasz Jozwiak 	test_data->nb_qps = (nb_lcores % enabled_cdev_count) ?
190424dd6c8STomasz Jozwiak 				(nb_lcores / enabled_cdev_count) + 1 :
191424dd6c8STomasz Jozwiak 				nb_lcores / enabled_cdev_count;
192424dd6c8STomasz Jozwiak 
193424dd6c8STomasz Jozwiak 	for (i = 0; i < enabled_cdev_count &&
194424dd6c8STomasz Jozwiak 			i < RTE_COMPRESS_MAX_DEVS; i++,
195424dd6c8STomasz Jozwiak 					nb_lcores -= test_data->nb_qps) {
196424dd6c8STomasz Jozwiak 		cdev_id = enabled_cdevs[i];
197424dd6c8STomasz Jozwiak 
198424dd6c8STomasz Jozwiak 		struct rte_compressdev_info cdev_info;
199220b51f7SRaja Zidane 		int socket_id = rte_compressdev_socket_id(cdev_id);
200424dd6c8STomasz Jozwiak 
201424dd6c8STomasz Jozwiak 		rte_compressdev_info_get(cdev_id, &cdev_info);
202424dd6c8STomasz Jozwiak 		if (cdev_info.max_nb_queue_pairs &&
203424dd6c8STomasz Jozwiak 			test_data->nb_qps > cdev_info.max_nb_queue_pairs) {
204424dd6c8STomasz Jozwiak 			RTE_LOG(ERR, USER1,
205424dd6c8STomasz Jozwiak 				"Number of needed queue pairs is higher "
206424dd6c8STomasz Jozwiak 				"than the maximum number of queue pairs "
207424dd6c8STomasz Jozwiak 				"per device.\n");
208424dd6c8STomasz Jozwiak 			RTE_LOG(ERR, USER1,
209424dd6c8STomasz Jozwiak 				"Lower the number of cores or increase "
210424dd6c8STomasz Jozwiak 				"the number of crypto devices\n");
211424dd6c8STomasz Jozwiak 			return -EINVAL;
212b68a8242STomasz Jozwiak 		}
213b68a8242STomasz Jozwiak 
214424dd6c8STomasz Jozwiak 		if (comp_perf_check_capabilities(test_data, cdev_id) < 0)
215424dd6c8STomasz Jozwiak 			return -EINVAL;
216424dd6c8STomasz Jozwiak 
217424dd6c8STomasz Jozwiak 		/* Configure compressdev */
218424dd6c8STomasz Jozwiak 		struct rte_compressdev_config config = {
219424dd6c8STomasz Jozwiak 			.socket_id = socket_id,
220424dd6c8STomasz Jozwiak 			.nb_queue_pairs = nb_lcores > test_data->nb_qps
221424dd6c8STomasz Jozwiak 					? test_data->nb_qps : nb_lcores,
222424dd6c8STomasz Jozwiak 			.max_nb_priv_xforms = NUM_MAX_XFORMS,
223424dd6c8STomasz Jozwiak 			.max_nb_streams = 0
224424dd6c8STomasz Jozwiak 		};
225e77e8b66SRaja Zidane 		test_data->nb_qps = config.nb_queue_pairs;
226424dd6c8STomasz Jozwiak 
227424dd6c8STomasz Jozwiak 		if (rte_compressdev_configure(cdev_id, &config) < 0) {
228424dd6c8STomasz Jozwiak 			RTE_LOG(ERR, USER1, "Device configuration failed\n");
229424dd6c8STomasz Jozwiak 			return -EINVAL;
230b68a8242STomasz Jozwiak 		}
231b68a8242STomasz Jozwiak 
232424dd6c8STomasz Jozwiak 		for (j = 0; j < test_data->nb_qps; j++) {
233424dd6c8STomasz Jozwiak 			ret = rte_compressdev_queue_pair_setup(cdev_id, j,
234424dd6c8STomasz Jozwiak 					NUM_MAX_INFLIGHT_OPS, socket_id);
235424dd6c8STomasz Jozwiak 			if (ret < 0) {
236424dd6c8STomasz Jozwiak 				RTE_LOG(ERR, USER1,
237424dd6c8STomasz Jozwiak 			      "Failed to setup queue pair %u on compressdev %u",
238424dd6c8STomasz Jozwiak 					j, cdev_id);
239424dd6c8STomasz Jozwiak 				return -EINVAL;
240424dd6c8STomasz Jozwiak 			}
241b68a8242STomasz Jozwiak 		}
242b68a8242STomasz Jozwiak 
243424dd6c8STomasz Jozwiak 		ret = rte_compressdev_start(cdev_id);
244424dd6c8STomasz Jozwiak 		if (ret < 0) {
245424dd6c8STomasz Jozwiak 			RTE_LOG(ERR, USER1,
246424dd6c8STomasz Jozwiak 				"Failed to start device %u: error %d\n",
247424dd6c8STomasz Jozwiak 				cdev_id, ret);
248424dd6c8STomasz Jozwiak 			return -EPERM;
249b68a8242STomasz Jozwiak 		}
250424dd6c8STomasz Jozwiak 	}
251424dd6c8STomasz Jozwiak 
252424dd6c8STomasz Jozwiak 	return enabled_cdev_count;
253b68a8242STomasz Jozwiak }
254b68a8242STomasz Jozwiak 
255b68a8242STomasz Jozwiak static int
comp_perf_dump_input_data(struct comp_test_data * test_data)256b68a8242STomasz Jozwiak comp_perf_dump_input_data(struct comp_test_data *test_data)
257b68a8242STomasz Jozwiak {
258b68a8242STomasz Jozwiak 	FILE *f = fopen(test_data->input_file, "r");
259b68a8242STomasz Jozwiak 	int ret = -1;
260b68a8242STomasz Jozwiak 
261b68a8242STomasz Jozwiak 	if (f == NULL) {
262b68a8242STomasz Jozwiak 		RTE_LOG(ERR, USER1, "Input file could not be opened\n");
263b68a8242STomasz Jozwiak 		return -1;
264b68a8242STomasz Jozwiak 	}
265b68a8242STomasz Jozwiak 
266b68a8242STomasz Jozwiak 	if (fseek(f, 0, SEEK_END) != 0) {
267b68a8242STomasz Jozwiak 		RTE_LOG(ERR, USER1, "Size of input could not be calculated\n");
268b68a8242STomasz Jozwiak 		goto end;
269b68a8242STomasz Jozwiak 	}
270b68a8242STomasz Jozwiak 	size_t actual_file_sz = ftell(f);
271b68a8242STomasz Jozwiak 	/* If extended input data size has not been set,
272b68a8242STomasz Jozwiak 	 * input data size = file size
273b68a8242STomasz Jozwiak 	 */
274b68a8242STomasz Jozwiak 
275b68a8242STomasz Jozwiak 	if (test_data->input_data_sz == 0)
276b68a8242STomasz Jozwiak 		test_data->input_data_sz = actual_file_sz;
277b68a8242STomasz Jozwiak 
278e378bd76STomasz Jozwiak 	if (test_data->input_data_sz <= 0 || actual_file_sz <= 0 ||
279e378bd76STomasz Jozwiak 			fseek(f, 0, SEEK_SET) != 0) {
280b68a8242STomasz Jozwiak 		RTE_LOG(ERR, USER1, "Size of input could not be calculated\n");
281b68a8242STomasz Jozwiak 		goto end;
282b68a8242STomasz Jozwiak 	}
283b68a8242STomasz Jozwiak 
28483cc3b90SMichael Baum 	if (!(test_data->test_op & COMPRESS) &&
28583cc3b90SMichael Baum 	    test_data->input_data_sz >
28683cc3b90SMichael Baum 	    (size_t) test_data->seg_sz * (size_t) test_data->max_sgl_segs) {
28783cc3b90SMichael Baum 		RTE_LOG(ERR, USER1,
28883cc3b90SMichael Baum 			"Size of input must be less than total segments\n");
28983cc3b90SMichael Baum 		goto end;
29083cc3b90SMichael Baum 	}
29183cc3b90SMichael Baum 
292b68a8242STomasz Jozwiak 	test_data->input_data = rte_zmalloc_socket(NULL,
293b68a8242STomasz Jozwiak 				test_data->input_data_sz, 0, rte_socket_id());
294b68a8242STomasz Jozwiak 
295b68a8242STomasz Jozwiak 	if (test_data->input_data == NULL) {
296b68a8242STomasz Jozwiak 		RTE_LOG(ERR, USER1, "Memory to hold the data from the input "
297b68a8242STomasz Jozwiak 				"file could not be allocated\n");
298b68a8242STomasz Jozwiak 		goto end;
299b68a8242STomasz Jozwiak 	}
300b68a8242STomasz Jozwiak 
301b68a8242STomasz Jozwiak 	size_t remaining_data = test_data->input_data_sz;
302b68a8242STomasz Jozwiak 	uint8_t *data = test_data->input_data;
303b68a8242STomasz Jozwiak 
304b68a8242STomasz Jozwiak 	while (remaining_data > 0) {
305b68a8242STomasz Jozwiak 		size_t data_to_read = RTE_MIN(remaining_data, actual_file_sz);
306b68a8242STomasz Jozwiak 
307b68a8242STomasz Jozwiak 		if (fread(data, data_to_read, 1, f) != 1) {
308b68a8242STomasz Jozwiak 			RTE_LOG(ERR, USER1, "Input file could not be read\n");
309b68a8242STomasz Jozwiak 			goto end;
310b68a8242STomasz Jozwiak 		}
311b68a8242STomasz Jozwiak 		if (fseek(f, 0, SEEK_SET) != 0) {
312b68a8242STomasz Jozwiak 			RTE_LOG(ERR, USER1,
313b68a8242STomasz Jozwiak 				"Size of input could not be calculated\n");
314b68a8242STomasz Jozwiak 			goto end;
315b68a8242STomasz Jozwiak 		}
316b68a8242STomasz Jozwiak 		remaining_data -= data_to_read;
317b68a8242STomasz Jozwiak 		data += data_to_read;
318b68a8242STomasz Jozwiak 	}
319b68a8242STomasz Jozwiak 
3202695db95SArtur Trybula 	printf("\n");
321b68a8242STomasz Jozwiak 	if (test_data->input_data_sz > actual_file_sz)
322b68a8242STomasz Jozwiak 		RTE_LOG(INFO, USER1,
323b68a8242STomasz Jozwiak 		  "%zu bytes read from file %s, extending the file %.2f times\n",
324b68a8242STomasz Jozwiak 			test_data->input_data_sz, test_data->input_file,
325b68a8242STomasz Jozwiak 			(double)test_data->input_data_sz/actual_file_sz);
326b68a8242STomasz Jozwiak 	else
327b68a8242STomasz Jozwiak 		RTE_LOG(INFO, USER1,
328b68a8242STomasz Jozwiak 			"%zu bytes read from file %s\n",
329b68a8242STomasz Jozwiak 			test_data->input_data_sz, test_data->input_file);
330b68a8242STomasz Jozwiak 
331b68a8242STomasz Jozwiak 	ret = 0;
332b68a8242STomasz Jozwiak 
333b68a8242STomasz Jozwiak end:
334b68a8242STomasz Jozwiak 	fclose(f);
335b68a8242STomasz Jozwiak 	return ret;
336b68a8242STomasz Jozwiak }
337b68a8242STomasz Jozwiak 
338d6cec113STomasz Jozwiak static void
comp_perf_cleanup_on_signal(int signalNumber __rte_unused)339d6cec113STomasz Jozwiak comp_perf_cleanup_on_signal(int signalNumber __rte_unused)
340d6cec113STomasz Jozwiak {
341d6cec113STomasz Jozwiak 	test_data->perf_comp_force_stop = 1;
342d6cec113STomasz Jozwiak }
343d6cec113STomasz Jozwiak 
344d6cec113STomasz Jozwiak static void
comp_perf_register_cleanup_on_signal(void)345d6cec113STomasz Jozwiak comp_perf_register_cleanup_on_signal(void)
346d6cec113STomasz Jozwiak {
347d6cec113STomasz Jozwiak 	signal(SIGTERM, comp_perf_cleanup_on_signal);
348d6cec113STomasz Jozwiak 	signal(SIGINT, comp_perf_cleanup_on_signal);
349d6cec113STomasz Jozwiak }
350d6cec113STomasz Jozwiak 
351e0b6287cSTomasz Jozwiak int
main(int argc,char ** argv)352e0b6287cSTomasz Jozwiak main(int argc, char **argv)
353e0b6287cSTomasz Jozwiak {
354424dd6c8STomasz Jozwiak 	uint8_t level_idx = 0;
355b68a8242STomasz Jozwiak 	int ret, i;
356424dd6c8STomasz Jozwiak 	void *ctx[RTE_MAX_LCORE] = {};
357424dd6c8STomasz Jozwiak 	uint8_t enabled_cdevs[RTE_COMPRESS_MAX_DEVS];
358424dd6c8STomasz Jozwiak 	int nb_compressdevs = 0;
359424dd6c8STomasz Jozwiak 	uint16_t total_nb_qps = 0;
360424dd6c8STomasz Jozwiak 	uint8_t cdev_id;
361424dd6c8STomasz Jozwiak 	uint32_t lcore_id;
362e0b6287cSTomasz Jozwiak 
363e0b6287cSTomasz Jozwiak 	/* Initialise DPDK EAL */
364e0b6287cSTomasz Jozwiak 	ret = rte_eal_init(argc, argv);
365e0b6287cSTomasz Jozwiak 	if (ret < 0)
366e0b6287cSTomasz Jozwiak 		rte_exit(EXIT_FAILURE, "Invalid EAL arguments!\n");
367e0b6287cSTomasz Jozwiak 	argc -= ret;
368e0b6287cSTomasz Jozwiak 	argv += ret;
369e0b6287cSTomasz Jozwiak 
370e0b6287cSTomasz Jozwiak 	test_data = rte_zmalloc_socket(NULL, sizeof(struct comp_test_data),
371e0b6287cSTomasz Jozwiak 					0, rte_socket_id());
372e0b6287cSTomasz Jozwiak 
373e0b6287cSTomasz Jozwiak 	if (test_data == NULL)
374e0b6287cSTomasz Jozwiak 		rte_exit(EXIT_FAILURE, "Cannot reserve memory in socket %d\n",
375e0b6287cSTomasz Jozwiak 				rte_socket_id());
376e0b6287cSTomasz Jozwiak 
377d6cec113STomasz Jozwiak 	comp_perf_register_cleanup_on_signal();
378d6cec113STomasz Jozwiak 
3790bf1e98fSTomasz Jozwiak 	ret = EXIT_SUCCESS;
380424dd6c8STomasz Jozwiak 	test_data->cleanup = ST_TEST_DATA;
381e0b6287cSTomasz Jozwiak 	comp_perf_options_default(test_data);
382e0b6287cSTomasz Jozwiak 
383e0b6287cSTomasz Jozwiak 	if (comp_perf_options_parse(test_data, argc, argv) < 0) {
384e0b6287cSTomasz Jozwiak 		RTE_LOG(ERR, USER1,
385e0b6287cSTomasz Jozwiak 			"Parsing one or more user options failed\n");
386e0b6287cSTomasz Jozwiak 		ret = EXIT_FAILURE;
387b68a8242STomasz Jozwiak 		goto end;
388e0b6287cSTomasz Jozwiak 	}
389e0b6287cSTomasz Jozwiak 
390e0b6287cSTomasz Jozwiak 	if (comp_perf_options_check(test_data) < 0) {
391e0b6287cSTomasz Jozwiak 		ret = EXIT_FAILURE;
392b68a8242STomasz Jozwiak 		goto end;
393b68a8242STomasz Jozwiak 	}
394b68a8242STomasz Jozwiak 
395424dd6c8STomasz Jozwiak 	nb_compressdevs =
396424dd6c8STomasz Jozwiak 		comp_perf_initialize_compressdev(test_data, enabled_cdevs);
397424dd6c8STomasz Jozwiak 
398424dd6c8STomasz Jozwiak 	if (nb_compressdevs < 1) {
399b68a8242STomasz Jozwiak 		ret = EXIT_FAILURE;
400b68a8242STomasz Jozwiak 		goto end;
401b68a8242STomasz Jozwiak 	}
402b68a8242STomasz Jozwiak 
403424dd6c8STomasz Jozwiak 	test_data->cleanup = ST_COMPDEV;
404b68a8242STomasz Jozwiak 	if (comp_perf_dump_input_data(test_data) < 0) {
405b68a8242STomasz Jozwiak 		ret = EXIT_FAILURE;
406b68a8242STomasz Jozwiak 		goto end;
407b68a8242STomasz Jozwiak 	}
408b68a8242STomasz Jozwiak 
409424dd6c8STomasz Jozwiak 	test_data->cleanup = ST_INPUT_DATA;
410b68a8242STomasz Jozwiak 
411424dd6c8STomasz Jozwiak 	if (test_data->level_lst.inc != 0)
412424dd6c8STomasz Jozwiak 		test_data->level = test_data->level_lst.min;
413b68a8242STomasz Jozwiak 	else
414424dd6c8STomasz Jozwiak 		test_data->level = test_data->level_lst.list[0];
415b68a8242STomasz Jozwiak 
4162695db95SArtur Trybula 	printf("\nApp uses socket: %u\n", rte_socket_id());
417b68a8242STomasz Jozwiak 	printf("Burst size = %u\n", test_data->burst_sz);
4186f1e5d80SArtur Trybula 	printf("Input data size = %zu\n", test_data->input_data_sz);
4192695db95SArtur Trybula 	if (test_data->test == CPERF_TEST_TYPE_PMDCC)
4202695db95SArtur Trybula 		printf("Cycle-count delay = %u [us]\n",
4212695db95SArtur Trybula 		       test_data->cyclecount_delay);
422b68a8242STomasz Jozwiak 
423424dd6c8STomasz Jozwiak 	test_data->cleanup = ST_DURING_TEST;
424424dd6c8STomasz Jozwiak 	total_nb_qps = nb_compressdevs * test_data->nb_qps;
425b68a8242STomasz Jozwiak 
426424dd6c8STomasz Jozwiak 	i = 0;
427424dd6c8STomasz Jozwiak 	uint8_t qp_id = 0, cdev_index = 0;
4280bf1e98fSTomasz Jozwiak 
429cb056611SStephen Hemminger 	RTE_LCORE_FOREACH_WORKER(lcore_id) {
430424dd6c8STomasz Jozwiak 
431424dd6c8STomasz Jozwiak 		if (i == total_nb_qps)
4320bf1e98fSTomasz Jozwiak 			break;
433b68a8242STomasz Jozwiak 
434424dd6c8STomasz Jozwiak 		cdev_id = enabled_cdevs[cdev_index];
435424dd6c8STomasz Jozwiak 		ctx[i] = cperf_testmap[test_data->test].constructor(
436424dd6c8STomasz Jozwiak 							cdev_id, qp_id,
437424dd6c8STomasz Jozwiak 							test_data);
438424dd6c8STomasz Jozwiak 		if (ctx[i] == NULL) {
439424dd6c8STomasz Jozwiak 			RTE_LOG(ERR, USER1, "Test run constructor failed\n");
440424dd6c8STomasz Jozwiak 			goto end;
441424dd6c8STomasz Jozwiak 		}
442424dd6c8STomasz Jozwiak 		qp_id = (qp_id + 1) % test_data->nb_qps;
443424dd6c8STomasz Jozwiak 		if (qp_id == 0)
444424dd6c8STomasz Jozwiak 			cdev_index++;
445424dd6c8STomasz Jozwiak 		i++;
446424dd6c8STomasz Jozwiak 	}
447424dd6c8STomasz Jozwiak 
4482695db95SArtur Trybula 	print_test_dynamics(test_data);
4496f1e5d80SArtur Trybula 
450424dd6c8STomasz Jozwiak 	while (test_data->level <= test_data->level_lst.max) {
451424dd6c8STomasz Jozwiak 
452424dd6c8STomasz Jozwiak 		i = 0;
453cb056611SStephen Hemminger 		RTE_LCORE_FOREACH_WORKER(lcore_id) {
454424dd6c8STomasz Jozwiak 
455424dd6c8STomasz Jozwiak 			if (i == total_nb_qps)
4560bf1e98fSTomasz Jozwiak 				break;
457b68a8242STomasz Jozwiak 
458424dd6c8STomasz Jozwiak 			rte_eal_remote_launch(
459424dd6c8STomasz Jozwiak 					cperf_testmap[test_data->test].runner,
460424dd6c8STomasz Jozwiak 					ctx[i], lcore_id);
461424dd6c8STomasz Jozwiak 			i++;
462424dd6c8STomasz Jozwiak 		}
463424dd6c8STomasz Jozwiak 		i = 0;
464cb056611SStephen Hemminger 		RTE_LCORE_FOREACH_WORKER(lcore_id) {
465b68a8242STomasz Jozwiak 
466424dd6c8STomasz Jozwiak 			if (i == total_nb_qps)
467424dd6c8STomasz Jozwiak 				break;
468424dd6c8STomasz Jozwiak 			ret |= rte_eal_wait_lcore(lcore_id);
469424dd6c8STomasz Jozwiak 			i++;
470424dd6c8STomasz Jozwiak 		}
471424dd6c8STomasz Jozwiak 
472424dd6c8STomasz Jozwiak 		if (ret != EXIT_SUCCESS)
473424dd6c8STomasz Jozwiak 			break;
474424dd6c8STomasz Jozwiak 
475424dd6c8STomasz Jozwiak 		if (test_data->level_lst.inc != 0)
476424dd6c8STomasz Jozwiak 			test_data->level += test_data->level_lst.inc;
477b68a8242STomasz Jozwiak 		else {
478424dd6c8STomasz Jozwiak 			if (++level_idx == test_data->level_lst.count)
479b68a8242STomasz Jozwiak 				break;
480424dd6c8STomasz Jozwiak 			test_data->level = test_data->level_lst.list[level_idx];
481b68a8242STomasz Jozwiak 		}
482e0b6287cSTomasz Jozwiak 	}
483e0b6287cSTomasz Jozwiak 
484b68a8242STomasz Jozwiak end:
485424dd6c8STomasz Jozwiak 	switch (test_data->cleanup) {
486e0b6287cSTomasz Jozwiak 
487b68a8242STomasz Jozwiak 	case ST_DURING_TEST:
488424dd6c8STomasz Jozwiak 		i = 0;
489cb056611SStephen Hemminger 		RTE_LCORE_FOREACH_WORKER(lcore_id) {
490424dd6c8STomasz Jozwiak 			if (i == total_nb_qps)
491424dd6c8STomasz Jozwiak 				break;
492424dd6c8STomasz Jozwiak 
493424dd6c8STomasz Jozwiak 			if (ctx[i] && cperf_testmap[test_data->test].destructor)
494424dd6c8STomasz Jozwiak 				cperf_testmap[test_data->test].destructor(
495424dd6c8STomasz Jozwiak 									ctx[i]);
496424dd6c8STomasz Jozwiak 			i++;
497424dd6c8STomasz Jozwiak 		}
498b68a8242STomasz Jozwiak 		/* fallthrough */
499b68a8242STomasz Jozwiak 	case ST_INPUT_DATA:
500b68a8242STomasz Jozwiak 		rte_free(test_data->input_data);
501b68a8242STomasz Jozwiak 		/* fallthrough */
502b68a8242STomasz Jozwiak 	case ST_COMPDEV:
503424dd6c8STomasz Jozwiak 		for (i = 0; i < nb_compressdevs &&
504d6cec113STomasz Jozwiak 		     i < RTE_COMPRESS_MAX_DEVS; i++) {
505424dd6c8STomasz Jozwiak 			rte_compressdev_stop(enabled_cdevs[i]);
506d6cec113STomasz Jozwiak 			rte_compressdev_close(enabled_cdevs[i]);
507d6cec113STomasz Jozwiak 		}
508b68a8242STomasz Jozwiak 		/* fallthrough */
509b68a8242STomasz Jozwiak 	case ST_TEST_DATA:
510b68a8242STomasz Jozwiak 		rte_free(test_data);
511b68a8242STomasz Jozwiak 		/* fallthrough */
512b68a8242STomasz Jozwiak 	case ST_CLEAR:
513b68a8242STomasz Jozwiak 	default:
514b68a8242STomasz Jozwiak 		i = rte_eal_cleanup();
515b68a8242STomasz Jozwiak 		if (i) {
516b68a8242STomasz Jozwiak 			RTE_LOG(ERR, USER1,
517b68a8242STomasz Jozwiak 				"Error from rte_eal_cleanup(), %d\n", i);
518b68a8242STomasz Jozwiak 			ret = i;
519b68a8242STomasz Jozwiak 		}
520b68a8242STomasz Jozwiak 		break;
521b68a8242STomasz Jozwiak 	}
522e0b6287cSTomasz Jozwiak 	return ret;
523e0b6287cSTomasz Jozwiak }
524424dd6c8STomasz Jozwiak 
525424dd6c8STomasz Jozwiak __rte_weak void *
cperf_cyclecount_test_constructor(uint8_t dev_id __rte_unused,uint16_t qp_id __rte_unused,struct comp_test_data * options __rte_unused)5262695db95SArtur Trybula cperf_cyclecount_test_constructor(uint8_t dev_id __rte_unused,
5272695db95SArtur Trybula 				 uint16_t qp_id __rte_unused,
5282695db95SArtur Trybula 				 struct comp_test_data *options __rte_unused)
5292695db95SArtur Trybula {
5302695db95SArtur Trybula 	RTE_LOG(INFO, USER1, "Cycle count test is not supported yet\n");
5312695db95SArtur Trybula 	return NULL;
5322695db95SArtur Trybula }
5332695db95SArtur Trybula 
5342695db95SArtur Trybula __rte_weak void
cperf_cyclecount_test_destructor(void * arg __rte_unused)5352695db95SArtur Trybula cperf_cyclecount_test_destructor(void *arg __rte_unused)
5362695db95SArtur Trybula {
5372695db95SArtur Trybula 	RTE_LOG(INFO, USER1, "Something wrong happened!!!\n");
5382695db95SArtur Trybula }
5392695db95SArtur Trybula 
5402695db95SArtur Trybula __rte_weak int
cperf_cyclecount_test_runner(void * test_ctx __rte_unused)5412695db95SArtur Trybula cperf_cyclecount_test_runner(void *test_ctx __rte_unused)
5422695db95SArtur Trybula {
5432695db95SArtur Trybula 	return 0;
5442695db95SArtur Trybula }
5452695db95SArtur Trybula 
5462695db95SArtur Trybula __rte_weak void *
cperf_throughput_test_constructor(uint8_t dev_id __rte_unused,uint16_t qp_id __rte_unused,struct comp_test_data * options __rte_unused)5472695db95SArtur Trybula cperf_throughput_test_constructor(uint8_t dev_id __rte_unused,
548424dd6c8STomasz Jozwiak 				 uint16_t qp_id __rte_unused,
549424dd6c8STomasz Jozwiak 				 struct comp_test_data *options __rte_unused)
550424dd6c8STomasz Jozwiak {
551424dd6c8STomasz Jozwiak 	RTE_LOG(INFO, USER1, "Benchmark test is not supported yet\n");
552424dd6c8STomasz Jozwiak 	return NULL;
553424dd6c8STomasz Jozwiak }
554424dd6c8STomasz Jozwiak 
555424dd6c8STomasz Jozwiak __rte_weak void
cperf_throughput_test_destructor(void * arg __rte_unused)5562695db95SArtur Trybula cperf_throughput_test_destructor(void *arg __rte_unused)
557424dd6c8STomasz Jozwiak {
558424dd6c8STomasz Jozwiak 
559424dd6c8STomasz Jozwiak }
560424dd6c8STomasz Jozwiak 
561424dd6c8STomasz Jozwiak __rte_weak int
cperf_throughput_test_runner(void * test_ctx __rte_unused)5622695db95SArtur Trybula cperf_throughput_test_runner(void *test_ctx __rte_unused)
563424dd6c8STomasz Jozwiak {
564424dd6c8STomasz Jozwiak 	return 0;
565424dd6c8STomasz Jozwiak }
566424dd6c8STomasz Jozwiak __rte_weak void *
cperf_verify_test_constructor(uint8_t dev_id __rte_unused,uint16_t qp_id __rte_unused,struct comp_test_data * options __rte_unused)567424dd6c8STomasz Jozwiak cperf_verify_test_constructor(uint8_t dev_id __rte_unused,
568424dd6c8STomasz Jozwiak 				 uint16_t qp_id __rte_unused,
569424dd6c8STomasz Jozwiak 				 struct comp_test_data *options __rte_unused)
570424dd6c8STomasz Jozwiak {
571424dd6c8STomasz Jozwiak 	RTE_LOG(INFO, USER1, "Verify test is not supported yet\n");
572424dd6c8STomasz Jozwiak 	return NULL;
573424dd6c8STomasz Jozwiak }
574424dd6c8STomasz Jozwiak 
575424dd6c8STomasz Jozwiak __rte_weak void
cperf_verify_test_destructor(void * arg __rte_unused)576424dd6c8STomasz Jozwiak cperf_verify_test_destructor(void *arg __rte_unused)
577424dd6c8STomasz Jozwiak {
578424dd6c8STomasz Jozwiak 
579424dd6c8STomasz Jozwiak }
580424dd6c8STomasz Jozwiak 
581424dd6c8STomasz Jozwiak __rte_weak int
cperf_verify_test_runner(void * test_ctx __rte_unused)582424dd6c8STomasz Jozwiak cperf_verify_test_runner(void *test_ctx __rte_unused)
583424dd6c8STomasz Jozwiak {
584424dd6c8STomasz Jozwiak 	return 0;
585424dd6c8STomasz Jozwiak }
586