1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019 Intel Corporation
5 #include <rte_malloc.h>
8 #include <rte_compressdev.h>
10 #include "comp_perf.h"
11 #include "comp_perf_options.h"
12 #include "comp_perf_test_benchmark.h"
13 #include "comp_perf_test_common.h"
14 #include "comp_perf_test_verify.h"
17 #define DIV_CEIL(a, b) ((a) / (b) + ((a) % (b) != 0))
19 struct cperf_buffer_info {
20 uint16_t total_segments;
22 uint16_t last_segment_sz;
23 uint32_t total_buffs; /*number of buffers = number of ops*/
24 uint16_t segments_per_buff;
25 uint16_t segments_per_last_buff;
29 static struct cperf_buffer_info buffer_info;
32 param_range_check(uint16_t size, const struct rte_param_log2_range *range)
34 unsigned int next_size;
36 /* Check lower/upper bounds */
37 if (size < range->min)
40 if (size > range->max)
43 /* If range is actually only one value, size is correct */
44 if (range->increment == 0)
47 /* Check if value is one of the supported sizes */
48 for (next_size = range->min; next_size <= range->max;
49 next_size += range->increment)
50 if (size == next_size)
57 find_buf_size(uint32_t input_size)
61 /* From performance point of view the buffer size should be a
62 * power of 2 but also should be enough to store incompressible data
65 /* We're looking for nearest power of 2 buffer size, which is greater
69 !input_size ? MIN_COMPRESSED_BUF_SIZE : (input_size << 1);
71 for (i = UINT16_MAX + 1; !(i & size); i >>= 1)
74 return i > ((UINT16_MAX + 1) >> 1)
75 ? (uint32_t)((float)input_size * EXPANSE_RATIO)
80 comp_perf_free_memory(struct comp_test_data *test_data,
81 struct cperf_mem_resources *mem)
85 if (mem->decomp_bufs != NULL)
86 for (i = 0; i < mem->total_bufs; i++)
87 rte_pktmbuf_free(mem->decomp_bufs[i]);
89 if (mem->comp_bufs != NULL)
90 for (i = 0; i < mem->total_bufs; i++)
91 rte_pktmbuf_free(mem->comp_bufs[i]);
93 rte_free(mem->decomp_bufs);
94 rte_free(mem->comp_bufs);
95 rte_free(mem->decompressed_data);
96 rte_free(mem->compressed_data);
97 rte_mempool_free(mem->op_pool);
98 rte_mempool_free(mem->decomp_buf_pool);
99 rte_mempool_free(mem->comp_buf_pool);
101 /* external mbuf support */
102 if (mem->decomp_memzones != NULL) {
103 for (i = 0; i < test_data->total_segs; i++)
104 rte_memzone_free(mem->decomp_memzones[i]);
105 rte_free(mem->decomp_memzones);
107 if (mem->comp_memzones != NULL) {
108 for (i = 0; i < test_data->total_segs; i++)
109 rte_memzone_free(mem->comp_memzones[i]);
110 rte_free(mem->comp_memzones);
112 rte_free(mem->decomp_buf_infos);
113 rte_free(mem->comp_buf_infos);
117 comp_perf_extbuf_free_cb(void *addr __rte_unused, void *opaque __rte_unused)
121 static const struct rte_memzone *
122 comp_perf_make_memzone(const char *name, struct cperf_mem_resources *mem,
123 unsigned int number, size_t size)
125 unsigned int socket_id = rte_socket_id();
126 char mz_name[RTE_MEMZONE_NAMESIZE];
127 const struct rte_memzone *memzone;
129 snprintf(mz_name, RTE_MEMZONE_NAMESIZE, "%s_s%u_d%u_q%u_%d", name,
130 socket_id, mem->dev_id, mem->qp_id, number);
131 memzone = rte_memzone_lookup(mz_name);
132 if (memzone != NULL && memzone->len != size) {
133 rte_memzone_free(memzone);
136 if (memzone == NULL) {
137 memzone = rte_memzone_reserve_aligned(mz_name, size, socket_id,
138 RTE_MEMZONE_IOVA_CONTIG, RTE_CACHE_LINE_SIZE);
140 RTE_LOG(ERR, USER1, "Can't allocate memory zone %s\n",
147 comp_perf_allocate_external_mbufs(struct comp_test_data *test_data,
148 struct cperf_mem_resources *mem)
152 mem->comp_memzones = rte_zmalloc_socket(NULL,
153 test_data->total_segs * sizeof(struct rte_memzone *),
156 if (mem->comp_memzones == NULL) {
158 "Memory to hold the compression memzones could not be allocated\n");
162 mem->decomp_memzones = rte_zmalloc_socket(NULL,
163 test_data->total_segs * sizeof(struct rte_memzone *),
166 if (mem->decomp_memzones == NULL) {
168 "Memory to hold the decompression memzones could not be allocated\n");
172 mem->comp_buf_infos = rte_zmalloc_socket(NULL,
173 test_data->total_segs * sizeof(struct rte_mbuf_ext_shared_info),
176 if (mem->comp_buf_infos == NULL) {
178 "Memory to hold the compression buf infos could not be allocated\n");
182 mem->decomp_buf_infos = rte_zmalloc_socket(NULL,
183 test_data->total_segs * sizeof(struct rte_mbuf_ext_shared_info),
186 if (mem->decomp_buf_infos == NULL) {
188 "Memory to hold the decompression buf infos could not be allocated\n");
192 for (i = 0; i < test_data->total_segs; i++) {
193 mem->comp_memzones[i] = comp_perf_make_memzone("comp", mem,
194 i, test_data->out_seg_sz);
195 if (mem->comp_memzones[i] == NULL) {
197 "Memory to hold the compression memzone could not be allocated\n");
201 mem->decomp_memzones[i] = comp_perf_make_memzone("decomp", mem,
202 i, test_data->seg_sz);
203 if (mem->decomp_memzones[i] == NULL) {
205 "Memory to hold the decompression memzone could not be allocated\n");
209 mem->comp_buf_infos[i].free_cb =
210 comp_perf_extbuf_free_cb;
211 mem->comp_buf_infos[i].fcb_opaque = NULL;
212 rte_mbuf_ext_refcnt_set(&mem->comp_buf_infos[i], 1);
214 mem->decomp_buf_infos[i].free_cb =
215 comp_perf_extbuf_free_cb;
216 mem->decomp_buf_infos[i].fcb_opaque = NULL;
217 rte_mbuf_ext_refcnt_set(&mem->decomp_buf_infos[i], 1);
224 comp_perf_allocate_memory(struct comp_test_data *test_data,
225 struct cperf_mem_resources *mem)
227 uint16_t comp_mbuf_size;
228 uint16_t decomp_mbuf_size;
230 test_data->out_seg_sz = find_buf_size(test_data->seg_sz);
232 /* Number of segments for input and output
233 * (compression and decompression)
235 test_data->total_segs = DIV_CEIL(test_data->input_data_sz,
238 if (test_data->use_external_mbufs != 0) {
239 if (comp_perf_allocate_external_mbufs(test_data, mem) < 0)
242 decomp_mbuf_size = 0;
244 comp_mbuf_size = test_data->out_seg_sz + RTE_PKTMBUF_HEADROOM;
245 decomp_mbuf_size = test_data->seg_sz + RTE_PKTMBUF_HEADROOM;
248 char pool_name[32] = "";
250 snprintf(pool_name, sizeof(pool_name), "comp_buf_pool_%u_qp_%u",
251 mem->dev_id, mem->qp_id);
252 mem->comp_buf_pool = rte_pktmbuf_pool_create(pool_name,
253 test_data->total_segs,
257 if (mem->comp_buf_pool == NULL) {
258 RTE_LOG(ERR, USER1, "Mbuf mempool could not be created\n");
262 snprintf(pool_name, sizeof(pool_name), "decomp_buf_pool_%u_qp_%u",
263 mem->dev_id, mem->qp_id);
264 mem->decomp_buf_pool = rte_pktmbuf_pool_create(pool_name,
265 test_data->total_segs,
269 if (mem->decomp_buf_pool == NULL) {
270 RTE_LOG(ERR, USER1, "Mbuf mempool could not be created\n");
274 mem->total_bufs = DIV_CEIL(test_data->total_segs,
275 test_data->max_sgl_segs);
277 snprintf(pool_name, sizeof(pool_name), "op_pool_%u_qp_%u",
278 mem->dev_id, mem->qp_id);
279 mem->op_pool = rte_comp_op_pool_create(pool_name,
281 0, 0, rte_socket_id());
282 if (mem->op_pool == NULL) {
283 RTE_LOG(ERR, USER1, "Comp op mempool could not be created\n");
288 * Compressed data might be a bit larger than input data,
289 * if data cannot be compressed
291 mem->compressed_data = rte_zmalloc_socket(NULL,
293 (size_t) test_data->out_seg_sz *
294 test_data->total_segs,
295 (size_t) MIN_COMPRESSED_BUF_SIZE),
298 if (mem->compressed_data == NULL) {
299 RTE_LOG(ERR, USER1, "Memory to hold the data from the input "
300 "file could not be allocated\n");
304 mem->decompressed_data = rte_zmalloc_socket(NULL,
305 test_data->input_data_sz, 0,
307 if (mem->decompressed_data == NULL) {
308 RTE_LOG(ERR, USER1, "Memory to hold the data from the input "
309 "file could not be allocated\n");
313 mem->comp_bufs = rte_zmalloc_socket(NULL,
314 mem->total_bufs * sizeof(struct rte_mbuf *),
316 if (mem->comp_bufs == NULL) {
317 RTE_LOG(ERR, USER1, "Memory to hold the compression mbufs"
318 " could not be allocated\n");
322 mem->decomp_bufs = rte_zmalloc_socket(NULL,
323 mem->total_bufs * sizeof(struct rte_mbuf *),
325 if (mem->decomp_bufs == NULL) {
326 RTE_LOG(ERR, USER1, "Memory to hold the decompression mbufs"
327 " could not be allocated\n");
331 buffer_info.total_segments = test_data->total_segs;
332 buffer_info.segment_sz = test_data->seg_sz;
333 buffer_info.total_buffs = mem->total_bufs;
334 buffer_info.segments_per_buff = test_data->max_sgl_segs;
335 buffer_info.input_data_sz = test_data->input_data_sz;
341 prepare_bufs(struct comp_test_data *test_data, struct cperf_mem_resources *mem)
343 uint32_t remaining_data = test_data->input_data_sz;
344 uint8_t *input_data_ptr = test_data->input_data;
348 uint16_t segs_per_mbuf = 0;
352 for (i = 0; i < mem->total_bufs; i++) {
353 /* Allocate data in input mbuf and copy data from input file */
354 mem->decomp_bufs[i] =
355 rte_pktmbuf_alloc(mem->decomp_buf_pool);
356 if (mem->decomp_bufs[i] == NULL) {
357 RTE_LOG(ERR, USER1, "Could not allocate mbuf\n");
361 data_sz = RTE_MIN(remaining_data, test_data->seg_sz);
363 if (test_data->use_external_mbufs != 0) {
364 rte_pktmbuf_attach_extbuf(mem->decomp_bufs[i],
365 mem->decomp_memzones[dmz]->addr,
366 mem->decomp_memzones[dmz]->iova,
368 &mem->decomp_buf_infos[dmz]);
372 data_addr = (uint8_t *) rte_pktmbuf_append(
373 mem->decomp_bufs[i], data_sz);
374 if (data_addr == NULL) {
375 RTE_LOG(ERR, USER1, "Could not append data\n");
378 rte_memcpy(data_addr, input_data_ptr, data_sz);
380 input_data_ptr += data_sz;
381 remaining_data -= data_sz;
383 /* Already one segment in the mbuf */
386 /* Chain mbufs if needed for input mbufs */
387 while (segs_per_mbuf < test_data->max_sgl_segs
388 && remaining_data > 0) {
389 struct rte_mbuf *next_seg =
390 rte_pktmbuf_alloc(mem->decomp_buf_pool);
392 if (next_seg == NULL) {
394 "Could not allocate mbuf\n");
398 data_sz = RTE_MIN(remaining_data, test_data->seg_sz);
400 if (test_data->use_external_mbufs != 0) {
401 rte_pktmbuf_attach_extbuf(
403 mem->decomp_memzones[dmz]->addr,
404 mem->decomp_memzones[dmz]->iova,
406 &mem->decomp_buf_infos[dmz]);
410 data_addr = (uint8_t *)rte_pktmbuf_append(next_seg,
413 if (data_addr == NULL) {
414 RTE_LOG(ERR, USER1, "Could not append data\n");
418 rte_memcpy(data_addr, input_data_ptr, data_sz);
419 input_data_ptr += data_sz;
420 remaining_data -= data_sz;
422 if (rte_pktmbuf_chain(mem->decomp_bufs[i],
424 RTE_LOG(ERR, USER1, "Could not chain mbufs\n");
430 /* Allocate data in output mbuf */
432 rte_pktmbuf_alloc(mem->comp_buf_pool);
433 if (mem->comp_bufs[i] == NULL) {
434 RTE_LOG(ERR, USER1, "Could not allocate mbuf\n");
438 if (test_data->use_external_mbufs != 0) {
439 rte_pktmbuf_attach_extbuf(mem->comp_bufs[i],
440 mem->comp_memzones[cmz]->addr,
441 mem->comp_memzones[cmz]->iova,
442 test_data->out_seg_sz,
443 &mem->comp_buf_infos[cmz]);
447 data_addr = (uint8_t *) rte_pktmbuf_append(
449 test_data->out_seg_sz);
450 if (data_addr == NULL) {
451 RTE_LOG(ERR, USER1, "Could not append data\n");
455 /* Chain mbufs if needed for output mbufs */
456 for (j = 1; j < segs_per_mbuf; j++) {
457 struct rte_mbuf *next_seg =
458 rte_pktmbuf_alloc(mem->comp_buf_pool);
460 if (next_seg == NULL) {
462 "Could not allocate mbuf\n");
466 if (test_data->use_external_mbufs != 0) {
467 rte_pktmbuf_attach_extbuf(
469 mem->comp_memzones[cmz]->addr,
470 mem->comp_memzones[cmz]->iova,
471 test_data->out_seg_sz,
472 &mem->comp_buf_infos[cmz]);
476 data_addr = (uint8_t *)rte_pktmbuf_append(next_seg,
477 test_data->out_seg_sz);
478 if (data_addr == NULL) {
479 RTE_LOG(ERR, USER1, "Could not append data\n");
483 if (rte_pktmbuf_chain(mem->comp_bufs[i],
485 RTE_LOG(ERR, USER1, "Could not chain mbufs\n");
491 buffer_info.segments_per_last_buff = segs_per_mbuf;
492 buffer_info.last_segment_sz = data_sz;
498 print_test_dynamics(void)
500 uint32_t opt_total_segs = DIV_CEIL(buffer_info.input_data_sz,
503 if (buffer_info.total_buffs > 1) {
504 printf("\nWarning: for the current input parameters, number"
505 " of ops is higher than one, which may result"
506 " in sub-optimal performance.\n");
507 printf("To improve the performance (for the current"
508 " input data) following parameters are"
510 printf(" * Segment size: %d\n", MAX_SEG_SIZE);
511 printf(" * Number of segments: %u\n", opt_total_segs);
512 } else if (buffer_info.total_buffs == 1) {
513 printf("\nInfo: there is only one op with %u segments -"
514 " the compression ratio is the best.\n",
515 buffer_info.segments_per_last_buff);
516 if (buffer_info.segment_sz < MAX_SEG_SIZE)
517 printf("To reduce compression time, please use"
518 " bigger segment size: %d.\n",
520 else if (buffer_info.segment_sz == MAX_SEG_SIZE)
521 printf("Segment size is optimal for the best"
524 printf("Warning: something wrong happened!!\n");
526 printf("\nFor the current input parameters (segment size = %u,"
527 " maximum segments per SGL = %u):\n",
528 buffer_info.segment_sz,
529 buffer_info.segments_per_buff);
530 printf(" * Total number of buffers: %d\n",
531 buffer_info.total_segments);
532 printf(" * %u buffer(s) %u bytes long, last buffer %u"
534 buffer_info.total_segments - 1,
535 buffer_info.segment_sz,
536 buffer_info.last_segment_sz);
537 printf(" * Number of ops: %u\n", buffer_info.total_buffs);
538 printf(" * Total memory allocation: %u\n",
539 (buffer_info.total_segments - 1) * buffer_info.segment_sz
540 + buffer_info.last_segment_sz);
541 if (buffer_info.total_buffs > 1)
542 printf(" * %u ops: %u segment(s) in each,"
543 " segment size %u\n",
544 buffer_info.total_buffs - 1,
545 buffer_info.segments_per_buff,
546 buffer_info.segment_sz);
547 if (buffer_info.segments_per_last_buff > 1) {
548 printf(" * 1 op %u segments:\n",
549 buffer_info.segments_per_last_buff);
550 printf(" o %u segment size %u\n",
551 buffer_info.segments_per_last_buff - 1,
552 buffer_info.segment_sz);
553 printf(" o last segment size %u\n",
554 buffer_info.last_segment_sz);
555 } else if (buffer_info.segments_per_last_buff == 1) {
556 printf(" * 1 op (the last one): %u segment %u"
558 buffer_info.segments_per_last_buff,
559 buffer_info.last_segment_sz);