1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
5 #include <rte_malloc.h>
8 #include <rte_compressdev.h>
10 #include "comp_perf_options.h"
11 #include "comp_perf.h"
12 #include "comp_perf_test_common.h"
14 #define NUM_MAX_XFORMS 16
15 #define NUM_MAX_INFLIGHT_OPS 512
18 const char *cperf_test_type_strs[] = {
19 [CPERF_TEST_TYPE_BENCHMARK] = "benchmark",
20 [CPERF_TEST_TYPE_VERIFY] = "verify"
24 static const struct cperf_test cperf_testmap[] = {
25 [CPERF_TEST_TYPE_BENCHMARK] = {
26 cperf_benchmark_test_constructor,
27 cperf_benchmark_test_runner,
28 cperf_benchmark_test_destructor
30 [CPERF_TEST_TYPE_VERIFY] = {
31 cperf_verify_test_constructor,
32 cperf_verify_test_runner,
33 cperf_verify_test_destructor
38 comp_perf_check_capabilities(struct comp_test_data *test_data, uint8_t cdev_id)
40 const struct rte_compressdev_capabilities *cap;
42 cap = rte_compressdev_capability_get(cdev_id,
43 RTE_COMP_ALGO_DEFLATE);
47 "Compress device does not support DEFLATE\n");
51 uint64_t comp_flags = cap->comp_feature_flags;
53 /* Huffman enconding */
54 if (test_data->huffman_enc == RTE_COMP_HUFFMAN_FIXED &&
55 (comp_flags & RTE_COMP_FF_HUFFMAN_FIXED) == 0) {
57 "Compress device does not supported Fixed Huffman\n");
61 if (test_data->huffman_enc == RTE_COMP_HUFFMAN_DYNAMIC &&
62 (comp_flags & RTE_COMP_FF_HUFFMAN_DYNAMIC) == 0) {
64 "Compress device does not supported Dynamic Huffman\n");
69 if (test_data->window_sz != -1) {
70 if (param_range_check(test_data->window_sz, &cap->window_size)
73 "Compress device does not support "
74 "this window size\n");
78 /* Set window size to PMD maximum if none was specified */
79 test_data->window_sz = cap->window_size.max;
81 /* Check if chained mbufs is supported */
82 if (test_data->max_sgl_segs > 1 &&
83 (comp_flags & RTE_COMP_FF_OOP_SGL_IN_SGL_OUT) == 0) {
84 RTE_LOG(INFO, USER1, "Compress device does not support "
85 "chained mbufs. Max SGL segments set to 1\n");
86 test_data->max_sgl_segs = 1;
90 if (test_data->level_lst.min == 0 &&
91 (comp_flags & RTE_COMP_FF_NONCOMPRESSED_BLOCKS) == 0) {
92 RTE_LOG(ERR, USER1, "Compress device does not support "
93 "level 0 (no compression)\n");
101 comp_perf_initialize_compressdev(struct comp_test_data *test_data,
102 uint8_t *enabled_cdevs)
104 uint8_t enabled_cdev_count, nb_lcores, cdev_id;
108 enabled_cdev_count = rte_compressdev_devices_get(test_data->driver_name,
109 enabled_cdevs, RTE_COMPRESS_MAX_DEVS);
110 if (enabled_cdev_count == 0) {
111 RTE_LOG(ERR, USER1, "No compress devices type %s available\n",
112 test_data->driver_name);
116 nb_lcores = rte_lcore_count() - 1;
119 * if there are more available than cores.
121 if (enabled_cdev_count > nb_lcores) {
122 enabled_cdev_count = nb_lcores;
124 " There's more available devices than cores!"
125 " The number of devices has been aligned to %d cores\n",
130 * Calculate number of needed queue pairs, based on the amount
131 * of available number of logical cores and compression devices.
132 * For instance, if there are 4 cores and 2 compression devices,
133 * 2 queue pairs will be set up per device.
134 * One queue pair per one core.
135 * if e.g.: there're 3 cores and 2 compression devices,
136 * 2 queue pairs will be set up per device but one queue pair
137 * will left unused in the last one device
139 test_data->nb_qps = (nb_lcores % enabled_cdev_count) ?
140 (nb_lcores / enabled_cdev_count) + 1 :
141 nb_lcores / enabled_cdev_count;
143 for (i = 0; i < enabled_cdev_count &&
144 i < RTE_COMPRESS_MAX_DEVS; i++,
145 nb_lcores -= test_data->nb_qps) {
146 cdev_id = enabled_cdevs[i];
148 struct rte_compressdev_info cdev_info;
149 uint8_t socket_id = rte_compressdev_socket_id(cdev_id);
151 rte_compressdev_info_get(cdev_id, &cdev_info);
152 if (cdev_info.max_nb_queue_pairs &&
153 test_data->nb_qps > cdev_info.max_nb_queue_pairs) {
155 "Number of needed queue pairs is higher "
156 "than the maximum number of queue pairs "
159 "Lower the number of cores or increase "
160 "the number of crypto devices\n");
164 if (comp_perf_check_capabilities(test_data, cdev_id) < 0)
167 /* Configure compressdev */
168 struct rte_compressdev_config config = {
169 .socket_id = socket_id,
170 .nb_queue_pairs = nb_lcores > test_data->nb_qps
171 ? test_data->nb_qps : nb_lcores,
172 .max_nb_priv_xforms = NUM_MAX_XFORMS,
176 if (rte_compressdev_configure(cdev_id, &config) < 0) {
177 RTE_LOG(ERR, USER1, "Device configuration failed\n");
181 for (j = 0; j < test_data->nb_qps; j++) {
182 ret = rte_compressdev_queue_pair_setup(cdev_id, j,
183 NUM_MAX_INFLIGHT_OPS, socket_id);
186 "Failed to setup queue pair %u on compressdev %u",
192 ret = rte_compressdev_start(cdev_id);
195 "Failed to start device %u: error %d\n",
201 return enabled_cdev_count;
205 comp_perf_dump_input_data(struct comp_test_data *test_data)
207 FILE *f = fopen(test_data->input_file, "r");
211 RTE_LOG(ERR, USER1, "Input file could not be opened\n");
215 if (fseek(f, 0, SEEK_END) != 0) {
216 RTE_LOG(ERR, USER1, "Size of input could not be calculated\n");
219 size_t actual_file_sz = ftell(f);
220 /* If extended input data size has not been set,
221 * input data size = file size
224 if (test_data->input_data_sz == 0)
225 test_data->input_data_sz = actual_file_sz;
227 if (test_data->input_data_sz <= 0 || actual_file_sz <= 0 ||
228 fseek(f, 0, SEEK_SET) != 0) {
229 RTE_LOG(ERR, USER1, "Size of input could not be calculated\n");
233 test_data->input_data = rte_zmalloc_socket(NULL,
234 test_data->input_data_sz, 0, rte_socket_id());
236 if (test_data->input_data == NULL) {
237 RTE_LOG(ERR, USER1, "Memory to hold the data from the input "
238 "file could not be allocated\n");
242 size_t remaining_data = test_data->input_data_sz;
243 uint8_t *data = test_data->input_data;
245 while (remaining_data > 0) {
246 size_t data_to_read = RTE_MIN(remaining_data, actual_file_sz);
248 if (fread(data, data_to_read, 1, f) != 1) {
249 RTE_LOG(ERR, USER1, "Input file could not be read\n");
252 if (fseek(f, 0, SEEK_SET) != 0) {
254 "Size of input could not be calculated\n");
257 remaining_data -= data_to_read;
258 data += data_to_read;
261 if (test_data->input_data_sz > actual_file_sz)
263 "%zu bytes read from file %s, extending the file %.2f times\n",
264 test_data->input_data_sz, test_data->input_file,
265 (double)test_data->input_data_sz/actual_file_sz);
268 "%zu bytes read from file %s\n",
269 test_data->input_data_sz, test_data->input_file);
279 main(int argc, char **argv)
281 uint8_t level_idx = 0;
283 struct comp_test_data *test_data;
284 void *ctx[RTE_MAX_LCORE] = {};
285 uint8_t enabled_cdevs[RTE_COMPRESS_MAX_DEVS];
286 int nb_compressdevs = 0;
287 uint16_t total_nb_qps = 0;
291 /* Initialise DPDK EAL */
292 ret = rte_eal_init(argc, argv);
294 rte_exit(EXIT_FAILURE, "Invalid EAL arguments!\n");
298 test_data = rte_zmalloc_socket(NULL, sizeof(struct comp_test_data),
301 if (test_data == NULL)
302 rte_exit(EXIT_FAILURE, "Cannot reserve memory in socket %d\n",
306 test_data->cleanup = ST_TEST_DATA;
307 comp_perf_options_default(test_data);
309 if (comp_perf_options_parse(test_data, argc, argv) < 0) {
311 "Parsing one or more user options failed\n");
316 if (comp_perf_options_check(test_data) < 0) {
322 comp_perf_initialize_compressdev(test_data, enabled_cdevs);
324 if (nb_compressdevs < 1) {
329 test_data->cleanup = ST_COMPDEV;
330 if (comp_perf_dump_input_data(test_data) < 0) {
335 test_data->cleanup = ST_INPUT_DATA;
337 if (test_data->level_lst.inc != 0)
338 test_data->level = test_data->level_lst.min;
340 test_data->level = test_data->level_lst.list[0];
342 printf("App uses socket: %u\n", rte_socket_id());
343 printf("Burst size = %u\n", test_data->burst_sz);
344 printf("File size = %zu\n", test_data->input_data_sz);
346 test_data->cleanup = ST_DURING_TEST;
347 total_nb_qps = nb_compressdevs * test_data->nb_qps;
350 uint8_t qp_id = 0, cdev_index = 0;
352 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
354 if (i == total_nb_qps)
357 cdev_id = enabled_cdevs[cdev_index];
358 ctx[i] = cperf_testmap[test_data->test].constructor(
361 if (ctx[i] == NULL) {
362 RTE_LOG(ERR, USER1, "Test run constructor failed\n");
365 qp_id = (qp_id + 1) % test_data->nb_qps;
371 while (test_data->level <= test_data->level_lst.max) {
374 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
376 if (i == total_nb_qps)
379 rte_eal_remote_launch(
380 cperf_testmap[test_data->test].runner,
385 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
387 if (i == total_nb_qps)
389 ret |= rte_eal_wait_lcore(lcore_id);
393 if (ret != EXIT_SUCCESS)
396 if (test_data->level_lst.inc != 0)
397 test_data->level += test_data->level_lst.inc;
399 if (++level_idx == test_data->level_lst.count)
401 test_data->level = test_data->level_lst.list[level_idx];
406 switch (test_data->cleanup) {
410 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
411 if (i == total_nb_qps)
414 if (ctx[i] && cperf_testmap[test_data->test].destructor)
415 cperf_testmap[test_data->test].destructor(
421 rte_free(test_data->input_data);
424 for (i = 0; i < nb_compressdevs &&
425 i < RTE_COMPRESS_MAX_DEVS; i++)
426 rte_compressdev_stop(enabled_cdevs[i]);
433 i = rte_eal_cleanup();
436 "Error from rte_eal_cleanup(), %d\n", i);
445 cperf_benchmark_test_constructor(uint8_t dev_id __rte_unused,
446 uint16_t qp_id __rte_unused,
447 struct comp_test_data *options __rte_unused)
449 RTE_LOG(INFO, USER1, "Benchmark test is not supported yet\n");
454 cperf_benchmark_test_destructor(void *arg __rte_unused)
460 cperf_benchmark_test_runner(void *test_ctx __rte_unused)
465 cperf_verify_test_constructor(uint8_t dev_id __rte_unused,
466 uint16_t qp_id __rte_unused,
467 struct comp_test_data *options __rte_unused)
469 RTE_LOG(INFO, USER1, "Verify test is not supported yet\n");
474 cperf_verify_test_destructor(void *arg __rte_unused)
480 cperf_verify_test_runner(void *test_ctx __rte_unused)