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_test_benchmark.h"
14 #define NUM_MAX_XFORMS 16
15 #define NUM_MAX_INFLIGHT_OPS 512
16 #define EXPANSE_RATIO 1.05
17 #define MIN_COMPRESSED_BUF_SIZE 8
19 #define DIV_CEIL(a, b) ((a) / (b) + ((a) % (b) != 0))
21 /* Cleanup state machine */
22 static enum cleanup_st {
33 param_range_check(uint16_t size, const struct rte_param_log2_range *range)
35 unsigned int next_size;
37 /* Check lower/upper bounds */
38 if (size < range->min)
41 if (size > range->max)
44 /* If range is actually only one value, size is correct */
45 if (range->increment == 0)
48 /* Check if value is one of the supported sizes */
49 for (next_size = range->min; next_size <= range->max;
50 next_size += range->increment)
51 if (size == next_size)
58 comp_perf_check_capabilities(struct comp_test_data *test_data)
60 const struct rte_compressdev_capabilities *cap;
62 cap = rte_compressdev_capability_get(test_data->cdev_id,
63 RTE_COMP_ALGO_DEFLATE);
67 "Compress device does not support DEFLATE\n");
71 uint64_t comp_flags = cap->comp_feature_flags;
73 /* Huffman enconding */
74 if (test_data->huffman_enc == RTE_COMP_HUFFMAN_FIXED &&
75 (comp_flags & RTE_COMP_FF_HUFFMAN_FIXED) == 0) {
77 "Compress device does not supported Fixed Huffman\n");
81 if (test_data->huffman_enc == RTE_COMP_HUFFMAN_DYNAMIC &&
82 (comp_flags & RTE_COMP_FF_HUFFMAN_DYNAMIC) == 0) {
84 "Compress device does not supported Dynamic Huffman\n");
89 if (test_data->window_sz != -1) {
90 if (param_range_check(test_data->window_sz, &cap->window_size)
93 "Compress device does not support "
94 "this window size\n");
98 /* Set window size to PMD maximum if none was specified */
99 test_data->window_sz = cap->window_size.max;
101 /* Check if chained mbufs is supported */
102 if (test_data->max_sgl_segs > 1 &&
103 (comp_flags & RTE_COMP_FF_OOP_SGL_IN_SGL_OUT) == 0) {
104 RTE_LOG(INFO, USER1, "Compress device does not support "
105 "chained mbufs. Max SGL segments set to 1\n");
106 test_data->max_sgl_segs = 1;
109 /* Level 0 support */
110 if (test_data->level.min == 0 &&
111 (comp_flags & RTE_COMP_FF_NONCOMPRESSED_BLOCKS) == 0) {
112 RTE_LOG(ERR, USER1, "Compress device does not support "
113 "level 0 (no compression)\n");
121 comp_perf_allocate_memory(struct comp_test_data *test_data)
123 /* Number of segments for input and output
124 * (compression and decompression)
126 uint32_t total_segs = DIV_CEIL(test_data->input_data_sz,
128 test_data->comp_buf_pool = rte_pktmbuf_pool_create("comp_buf_pool",
130 0, 0, test_data->seg_sz + RTE_PKTMBUF_HEADROOM,
132 if (test_data->comp_buf_pool == NULL) {
133 RTE_LOG(ERR, USER1, "Mbuf mempool could not be created\n");
137 cleanup = ST_MEMORY_ALLOC;
138 test_data->decomp_buf_pool = rte_pktmbuf_pool_create("decomp_buf_pool",
140 0, 0, test_data->seg_sz + RTE_PKTMBUF_HEADROOM,
142 if (test_data->decomp_buf_pool == NULL) {
143 RTE_LOG(ERR, USER1, "Mbuf mempool could not be created\n");
147 test_data->total_bufs = DIV_CEIL(total_segs, test_data->max_sgl_segs);
149 test_data->op_pool = rte_comp_op_pool_create("op_pool",
150 test_data->total_bufs,
151 0, 0, rte_socket_id());
152 if (test_data->op_pool == NULL) {
153 RTE_LOG(ERR, USER1, "Comp op mempool could not be created\n");
158 * Compressed data might be a bit larger than input data,
159 * if data cannot be compressed
161 test_data->compressed_data = rte_zmalloc_socket(NULL,
162 test_data->input_data_sz * EXPANSE_RATIO
163 + MIN_COMPRESSED_BUF_SIZE, 0,
165 if (test_data->compressed_data == NULL) {
166 RTE_LOG(ERR, USER1, "Memory to hold the data from the input "
167 "file could not be allocated\n");
171 test_data->decompressed_data = rte_zmalloc_socket(NULL,
172 test_data->input_data_sz, 0,
174 if (test_data->decompressed_data == NULL) {
175 RTE_LOG(ERR, USER1, "Memory to hold the data from the input "
176 "file could not be allocated\n");
180 test_data->comp_bufs = rte_zmalloc_socket(NULL,
181 test_data->total_bufs * sizeof(struct rte_mbuf *),
183 if (test_data->comp_bufs == NULL) {
184 RTE_LOG(ERR, USER1, "Memory to hold the compression mbufs"
185 " could not be allocated\n");
189 test_data->decomp_bufs = rte_zmalloc_socket(NULL,
190 test_data->total_bufs * sizeof(struct rte_mbuf *),
192 if (test_data->decomp_bufs == NULL) {
193 RTE_LOG(ERR, USER1, "Memory to hold the decompression mbufs"
194 " could not be allocated\n");
201 comp_perf_dump_input_data(struct comp_test_data *test_data)
203 FILE *f = fopen(test_data->input_file, "r");
207 RTE_LOG(ERR, USER1, "Input file could not be opened\n");
211 if (fseek(f, 0, SEEK_END) != 0) {
212 RTE_LOG(ERR, USER1, "Size of input could not be calculated\n");
215 size_t actual_file_sz = ftell(f);
216 /* If extended input data size has not been set,
217 * input data size = file size
220 if (test_data->input_data_sz == 0)
221 test_data->input_data_sz = actual_file_sz;
223 if (fseek(f, 0, SEEK_SET) != 0) {
224 RTE_LOG(ERR, USER1, "Size of input could not be calculated\n");
228 test_data->input_data = rte_zmalloc_socket(NULL,
229 test_data->input_data_sz, 0, rte_socket_id());
231 if (test_data->input_data == NULL) {
232 RTE_LOG(ERR, USER1, "Memory to hold the data from the input "
233 "file could not be allocated\n");
237 size_t remaining_data = test_data->input_data_sz;
238 uint8_t *data = test_data->input_data;
240 while (remaining_data > 0) {
241 size_t data_to_read = RTE_MIN(remaining_data, actual_file_sz);
243 if (fread(data, data_to_read, 1, f) != 1) {
244 RTE_LOG(ERR, USER1, "Input file could not be read\n");
247 if (fseek(f, 0, SEEK_SET) != 0) {
249 "Size of input could not be calculated\n");
252 remaining_data -= data_to_read;
253 data += data_to_read;
256 if (test_data->input_data_sz > actual_file_sz)
258 "%zu bytes read from file %s, extending the file %.2f times\n",
259 test_data->input_data_sz, test_data->input_file,
260 (double)test_data->input_data_sz/actual_file_sz);
263 "%zu bytes read from file %s\n",
264 test_data->input_data_sz, test_data->input_file);
274 comp_perf_initialize_compressdev(struct comp_test_data *test_data)
276 uint8_t enabled_cdev_count;
277 uint8_t enabled_cdevs[RTE_COMPRESS_MAX_DEVS];
279 enabled_cdev_count = rte_compressdev_devices_get(test_data->driver_name,
280 enabled_cdevs, RTE_COMPRESS_MAX_DEVS);
281 if (enabled_cdev_count == 0) {
282 RTE_LOG(ERR, USER1, "No compress devices type %s available\n",
283 test_data->driver_name);
287 if (enabled_cdev_count > 1)
289 "Only the first compress device will be used\n");
291 test_data->cdev_id = enabled_cdevs[0];
293 if (comp_perf_check_capabilities(test_data) < 0)
296 /* Configure compressdev (one device, one queue pair) */
297 struct rte_compressdev_config config = {
298 .socket_id = rte_socket_id(),
300 .max_nb_priv_xforms = NUM_MAX_XFORMS,
304 if (rte_compressdev_configure(test_data->cdev_id, &config) < 0) {
305 RTE_LOG(ERR, USER1, "Device configuration failed\n");
309 if (rte_compressdev_queue_pair_setup(test_data->cdev_id, 0,
310 NUM_MAX_INFLIGHT_OPS, rte_socket_id()) < 0) {
311 RTE_LOG(ERR, USER1, "Queue pair setup failed\n");
315 if (rte_compressdev_start(test_data->cdev_id) < 0) {
316 RTE_LOG(ERR, USER1, "Device could not be started\n");
324 prepare_bufs(struct comp_test_data *test_data)
326 uint32_t remaining_data = test_data->input_data_sz;
327 uint8_t *input_data_ptr = test_data->input_data;
332 for (i = 0; i < test_data->total_bufs; i++) {
333 /* Allocate data in input mbuf and copy data from input file */
334 test_data->decomp_bufs[i] =
335 rte_pktmbuf_alloc(test_data->decomp_buf_pool);
336 if (test_data->decomp_bufs[i] == NULL) {
337 RTE_LOG(ERR, USER1, "Could not allocate mbuf\n");
341 cleanup = ST_PREPARE_BUF;
342 data_sz = RTE_MIN(remaining_data, test_data->seg_sz);
343 data_addr = (uint8_t *) rte_pktmbuf_append(
344 test_data->decomp_bufs[i], data_sz);
345 if (data_addr == NULL) {
346 RTE_LOG(ERR, USER1, "Could not append data\n");
349 rte_memcpy(data_addr, input_data_ptr, data_sz);
351 input_data_ptr += data_sz;
352 remaining_data -= data_sz;
354 /* Already one segment in the mbuf */
355 uint16_t segs_per_mbuf = 1;
357 /* Chain mbufs if needed for input mbufs */
358 while (segs_per_mbuf < test_data->max_sgl_segs
359 && remaining_data > 0) {
360 struct rte_mbuf *next_seg =
361 rte_pktmbuf_alloc(test_data->decomp_buf_pool);
363 if (next_seg == NULL) {
365 "Could not allocate mbuf\n");
369 data_sz = RTE_MIN(remaining_data, test_data->seg_sz);
370 data_addr = (uint8_t *)rte_pktmbuf_append(next_seg,
373 if (data_addr == NULL) {
374 RTE_LOG(ERR, USER1, "Could not append data\n");
378 rte_memcpy(data_addr, input_data_ptr, data_sz);
379 input_data_ptr += data_sz;
380 remaining_data -= data_sz;
382 if (rte_pktmbuf_chain(test_data->decomp_bufs[i],
384 RTE_LOG(ERR, USER1, "Could not chain mbufs\n");
390 /* Allocate data in output mbuf */
391 test_data->comp_bufs[i] =
392 rte_pktmbuf_alloc(test_data->comp_buf_pool);
393 if (test_data->comp_bufs[i] == NULL) {
394 RTE_LOG(ERR, USER1, "Could not allocate mbuf\n");
397 data_addr = (uint8_t *) rte_pktmbuf_append(
398 test_data->comp_bufs[i],
400 if (data_addr == NULL) {
401 RTE_LOG(ERR, USER1, "Could not append data\n");
405 /* Chain mbufs if needed for output mbufs */
406 for (j = 1; j < segs_per_mbuf; j++) {
407 struct rte_mbuf *next_seg =
408 rte_pktmbuf_alloc(test_data->comp_buf_pool);
410 if (next_seg == NULL) {
412 "Could not allocate mbuf\n");
416 data_addr = (uint8_t *)rte_pktmbuf_append(next_seg,
419 if (data_addr == NULL) {
420 RTE_LOG(ERR, USER1, "Could not append data\n");
424 if (rte_pktmbuf_chain(test_data->comp_bufs[i],
426 RTE_LOG(ERR, USER1, "Could not chain mbufs\n");
436 free_bufs(struct comp_test_data *test_data)
440 for (i = 0; i < test_data->total_bufs; i++) {
441 rte_pktmbuf_free(test_data->comp_bufs[i]);
442 rte_pktmbuf_free(test_data->decomp_bufs[i]);
449 main(int argc, char **argv)
451 uint8_t level, level_idx = 0;
453 struct comp_test_data *test_data;
455 /* Initialise DPDK EAL */
456 ret = rte_eal_init(argc, argv);
458 rte_exit(EXIT_FAILURE, "Invalid EAL arguments!\n");
462 test_data = rte_zmalloc_socket(NULL, sizeof(struct comp_test_data),
465 if (test_data == NULL)
466 rte_exit(EXIT_FAILURE, "Cannot reserve memory in socket %d\n",
470 cleanup = ST_TEST_DATA;
471 comp_perf_options_default(test_data);
473 if (comp_perf_options_parse(test_data, argc, argv) < 0) {
475 "Parsing one or more user options failed\n");
480 if (comp_perf_options_check(test_data) < 0) {
485 if (comp_perf_initialize_compressdev(test_data) < 0) {
490 cleanup = ST_COMPDEV;
491 if (comp_perf_dump_input_data(test_data) < 0) {
496 cleanup = ST_INPUT_DATA;
497 if (comp_perf_allocate_memory(test_data) < 0) {
502 if (prepare_bufs(test_data) < 0) {
507 if (test_data->level.inc != 0)
508 level = test_data->level.min;
510 level = test_data->level.list[0];
512 printf("Burst size = %u\n", test_data->burst_sz);
513 printf("File size = %zu\n", test_data->input_data_sz);
515 printf("%6s%12s%17s%19s%21s%15s%21s%23s%16s\n",
516 "Level", "Comp size", "Comp ratio [%]",
517 "Comp [Cycles/it]", "Comp [Cycles/Byte]", "Comp [Gbps]",
518 "Decomp [Cycles/it]", "Decomp [Cycles/Byte]", "Decomp [Gbps]");
520 cleanup = ST_DURING_TEST;
521 while (level <= test_data->level.max) {
524 * Run a first iteration, to verify compression and
525 * get the compression ratio for the level
527 if (cperf_verification(test_data, level) != EXIT_SUCCESS)
531 * Run benchmarking test
533 if (cperf_benchmark(test_data, level) != EXIT_SUCCESS)
536 printf("%6u%12zu%17.2f%19"PRIu64"%21.2f"
537 "%15.2f%21"PRIu64"%23.2f%16.2f\n",
538 level, test_data->comp_data_sz, test_data->ratio,
539 test_data->comp_tsc_duration[level],
540 test_data->comp_tsc_byte, test_data->comp_gbps,
541 test_data->decomp_tsc_duration[level],
542 test_data->decomp_tsc_byte, test_data->decomp_gbps);
544 if (test_data->level.inc != 0)
545 level += test_data->level.inc;
547 if (++level_idx == test_data->level.count)
549 level = test_data->level.list[level_idx];
558 free_bufs(test_data);
560 case ST_MEMORY_ALLOC:
561 rte_free(test_data->decomp_bufs);
562 rte_free(test_data->comp_bufs);
563 rte_free(test_data->decompressed_data);
564 rte_free(test_data->compressed_data);
565 rte_mempool_free(test_data->op_pool);
566 rte_mempool_free(test_data->decomp_buf_pool);
567 rte_mempool_free(test_data->comp_buf_pool);
570 rte_free(test_data->input_data);
573 if (test_data->cdev_id != -1)
574 rte_compressdev_stop(test_data->cdev_id);
581 i = rte_eal_cleanup();
584 "Error from rte_eal_cleanup(), %d\n", i);