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_test_verify.h"
12 #include "comp_perf.h"
13 #include "comp_perf_test_common.h"
15 #define NUM_MAX_XFORMS 16
16 #define NUM_MAX_INFLIGHT_OPS 512
19 const char *cperf_test_type_strs[] = {
20 [CPERF_TEST_TYPE_BENCHMARK] = "benchmark",
21 [CPERF_TEST_TYPE_VERIFY] = "verify"
25 static const struct cperf_test cperf_testmap[] = {
26 [CPERF_TEST_TYPE_BENCHMARK] = {
27 cperf_benchmark_test_constructor,
28 cperf_benchmark_test_runner,
29 cperf_benchmark_test_destructor
31 [CPERF_TEST_TYPE_VERIFY] = {
32 cperf_verify_test_constructor,
33 cperf_verify_test_runner,
34 cperf_verify_test_destructor
39 comp_perf_check_capabilities(struct comp_test_data *test_data, uint8_t cdev_id)
41 const struct rte_compressdev_capabilities *cap;
43 cap = rte_compressdev_capability_get(cdev_id,
44 RTE_COMP_ALGO_DEFLATE);
48 "Compress device does not support DEFLATE\n");
52 uint64_t comp_flags = cap->comp_feature_flags;
54 /* Huffman enconding */
55 if (test_data->huffman_enc == RTE_COMP_HUFFMAN_FIXED &&
56 (comp_flags & RTE_COMP_FF_HUFFMAN_FIXED) == 0) {
58 "Compress device does not supported Fixed Huffman\n");
62 if (test_data->huffman_enc == RTE_COMP_HUFFMAN_DYNAMIC &&
63 (comp_flags & RTE_COMP_FF_HUFFMAN_DYNAMIC) == 0) {
65 "Compress device does not supported Dynamic Huffman\n");
70 if (test_data->window_sz != -1) {
71 if (param_range_check(test_data->window_sz, &cap->window_size)
74 "Compress device does not support "
75 "this window size\n");
79 /* Set window size to PMD maximum if none was specified */
80 test_data->window_sz = cap->window_size.max;
82 /* Check if chained mbufs is supported */
83 if (test_data->max_sgl_segs > 1 &&
84 (comp_flags & RTE_COMP_FF_OOP_SGL_IN_SGL_OUT) == 0) {
85 RTE_LOG(INFO, USER1, "Compress device does not support "
86 "chained mbufs. Max SGL segments set to 1\n");
87 test_data->max_sgl_segs = 1;
91 if (test_data->level_lst.min == 0 &&
92 (comp_flags & RTE_COMP_FF_NONCOMPRESSED_BLOCKS) == 0) {
93 RTE_LOG(ERR, USER1, "Compress device does not support "
94 "level 0 (no compression)\n");
102 comp_perf_initialize_compressdev(struct comp_test_data *test_data,
103 uint8_t *enabled_cdevs)
105 uint8_t enabled_cdev_count, nb_lcores, cdev_id;
109 enabled_cdev_count = rte_compressdev_devices_get(test_data->driver_name,
110 enabled_cdevs, RTE_COMPRESS_MAX_DEVS);
111 if (enabled_cdev_count == 0) {
112 RTE_LOG(ERR, USER1, "No compress devices type %s available\n",
113 test_data->driver_name);
117 nb_lcores = rte_lcore_count() - 1;
120 * if there are more available than cores.
122 if (enabled_cdev_count > nb_lcores) {
123 enabled_cdev_count = nb_lcores;
125 " There's more available devices than cores!"
126 " The number of devices has been aligned to %d cores\n",
131 * Calculate number of needed queue pairs, based on the amount
132 * of available number of logical cores and compression devices.
133 * For instance, if there are 4 cores and 2 compression devices,
134 * 2 queue pairs will be set up per device.
135 * One queue pair per one core.
136 * if e.g.: there're 3 cores and 2 compression devices,
137 * 2 queue pairs will be set up per device but one queue pair
138 * will left unused in the last one device
140 test_data->nb_qps = (nb_lcores % enabled_cdev_count) ?
141 (nb_lcores / enabled_cdev_count) + 1 :
142 nb_lcores / enabled_cdev_count;
144 for (i = 0; i < enabled_cdev_count &&
145 i < RTE_COMPRESS_MAX_DEVS; i++,
146 nb_lcores -= test_data->nb_qps) {
147 cdev_id = enabled_cdevs[i];
149 struct rte_compressdev_info cdev_info;
150 uint8_t socket_id = rte_compressdev_socket_id(cdev_id);
152 rte_compressdev_info_get(cdev_id, &cdev_info);
153 if (cdev_info.max_nb_queue_pairs &&
154 test_data->nb_qps > cdev_info.max_nb_queue_pairs) {
156 "Number of needed queue pairs is higher "
157 "than the maximum number of queue pairs "
160 "Lower the number of cores or increase "
161 "the number of crypto devices\n");
165 if (comp_perf_check_capabilities(test_data, cdev_id) < 0)
168 /* Configure compressdev */
169 struct rte_compressdev_config config = {
170 .socket_id = socket_id,
171 .nb_queue_pairs = nb_lcores > test_data->nb_qps
172 ? test_data->nb_qps : nb_lcores,
173 .max_nb_priv_xforms = NUM_MAX_XFORMS,
177 if (rte_compressdev_configure(cdev_id, &config) < 0) {
178 RTE_LOG(ERR, USER1, "Device configuration failed\n");
182 for (j = 0; j < test_data->nb_qps; j++) {
183 ret = rte_compressdev_queue_pair_setup(cdev_id, j,
184 NUM_MAX_INFLIGHT_OPS, socket_id);
187 "Failed to setup queue pair %u on compressdev %u",
193 ret = rte_compressdev_start(cdev_id);
196 "Failed to start device %u: error %d\n",
202 return enabled_cdev_count;
206 comp_perf_dump_input_data(struct comp_test_data *test_data)
208 FILE *f = fopen(test_data->input_file, "r");
212 RTE_LOG(ERR, USER1, "Input file could not be opened\n");
216 if (fseek(f, 0, SEEK_END) != 0) {
217 RTE_LOG(ERR, USER1, "Size of input could not be calculated\n");
220 size_t actual_file_sz = ftell(f);
221 /* If extended input data size has not been set,
222 * input data size = file size
225 if (test_data->input_data_sz == 0)
226 test_data->input_data_sz = actual_file_sz;
228 if (test_data->input_data_sz <= 0 || actual_file_sz <= 0 ||
229 fseek(f, 0, SEEK_SET) != 0) {
230 RTE_LOG(ERR, USER1, "Size of input could not be calculated\n");
234 test_data->input_data = rte_zmalloc_socket(NULL,
235 test_data->input_data_sz, 0, rte_socket_id());
237 if (test_data->input_data == NULL) {
238 RTE_LOG(ERR, USER1, "Memory to hold the data from the input "
239 "file could not be allocated\n");
243 size_t remaining_data = test_data->input_data_sz;
244 uint8_t *data = test_data->input_data;
246 while (remaining_data > 0) {
247 size_t data_to_read = RTE_MIN(remaining_data, actual_file_sz);
249 if (fread(data, data_to_read, 1, f) != 1) {
250 RTE_LOG(ERR, USER1, "Input file could not be read\n");
253 if (fseek(f, 0, SEEK_SET) != 0) {
255 "Size of input could not be calculated\n");
258 remaining_data -= data_to_read;
259 data += data_to_read;
262 if (test_data->input_data_sz > actual_file_sz)
264 "%zu bytes read from file %s, extending the file %.2f times\n",
265 test_data->input_data_sz, test_data->input_file,
266 (double)test_data->input_data_sz/actual_file_sz);
269 "%zu bytes read from file %s\n",
270 test_data->input_data_sz, test_data->input_file);
280 main(int argc, char **argv)
282 uint8_t level_idx = 0;
284 struct comp_test_data *test_data;
285 void *ctx[RTE_MAX_LCORE] = {};
286 uint8_t enabled_cdevs[RTE_COMPRESS_MAX_DEVS];
287 int nb_compressdevs = 0;
288 uint16_t total_nb_qps = 0;
292 /* Initialise DPDK EAL */
293 ret = rte_eal_init(argc, argv);
295 rte_exit(EXIT_FAILURE, "Invalid EAL arguments!\n");
299 test_data = rte_zmalloc_socket(NULL, sizeof(struct comp_test_data),
302 if (test_data == NULL)
303 rte_exit(EXIT_FAILURE, "Cannot reserve memory in socket %d\n",
307 test_data->cleanup = ST_TEST_DATA;
308 comp_perf_options_default(test_data);
310 if (comp_perf_options_parse(test_data, argc, argv) < 0) {
312 "Parsing one or more user options failed\n");
317 if (comp_perf_options_check(test_data) < 0) {
323 comp_perf_initialize_compressdev(test_data, enabled_cdevs);
325 if (nb_compressdevs < 1) {
330 test_data->cleanup = ST_COMPDEV;
331 if (comp_perf_dump_input_data(test_data) < 0) {
336 test_data->cleanup = ST_INPUT_DATA;
338 if (test_data->level_lst.inc != 0)
339 test_data->level = test_data->level_lst.min;
341 test_data->level = test_data->level_lst.list[0];
343 printf("App uses socket: %u\n", rte_socket_id());
344 printf("Burst size = %u\n", test_data->burst_sz);
345 printf("File size = %zu\n", test_data->input_data_sz);
347 test_data->cleanup = ST_DURING_TEST;
348 total_nb_qps = nb_compressdevs * test_data->nb_qps;
351 uint8_t qp_id = 0, cdev_index = 0;
353 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
355 if (i == total_nb_qps)
358 cdev_id = enabled_cdevs[cdev_index];
359 ctx[i] = cperf_testmap[test_data->test].constructor(
362 if (ctx[i] == NULL) {
363 RTE_LOG(ERR, USER1, "Test run constructor failed\n");
366 qp_id = (qp_id + 1) % test_data->nb_qps;
372 while (test_data->level <= test_data->level_lst.max) {
375 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
377 if (i == total_nb_qps)
380 rte_eal_remote_launch(
381 cperf_testmap[test_data->test].runner,
386 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
388 if (i == total_nb_qps)
390 ret |= rte_eal_wait_lcore(lcore_id);
394 if (ret != EXIT_SUCCESS)
397 if (test_data->level_lst.inc != 0)
398 test_data->level += test_data->level_lst.inc;
400 if (++level_idx == test_data->level_lst.count)
402 test_data->level = test_data->level_lst.list[level_idx];
407 switch (test_data->cleanup) {
411 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
412 if (i == total_nb_qps)
415 if (ctx[i] && cperf_testmap[test_data->test].destructor)
416 cperf_testmap[test_data->test].destructor(
422 rte_free(test_data->input_data);
425 for (i = 0; i < nb_compressdevs &&
426 i < RTE_COMPRESS_MAX_DEVS; i++)
427 rte_compressdev_stop(enabled_cdevs[i]);
434 i = rte_eal_cleanup();
437 "Error from rte_eal_cleanup(), %d\n", i);
446 cperf_benchmark_test_constructor(uint8_t dev_id __rte_unused,
447 uint16_t qp_id __rte_unused,
448 struct comp_test_data *options __rte_unused)
450 RTE_LOG(INFO, USER1, "Benchmark test is not supported yet\n");
455 cperf_benchmark_test_destructor(void *arg __rte_unused)
461 cperf_benchmark_test_runner(void *test_ctx __rte_unused)
466 cperf_verify_test_constructor(uint8_t dev_id __rte_unused,
467 uint16_t qp_id __rte_unused,
468 struct comp_test_data *options __rte_unused)
470 RTE_LOG(INFO, USER1, "Verify test is not supported yet\n");
475 cperf_verify_test_destructor(void *arg __rte_unused)
481 cperf_verify_test_runner(void *test_ctx __rte_unused)