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 cperf_mem_resources *mem)
84 for (i = 0; i < mem->total_bufs; i++) {
85 rte_pktmbuf_free(mem->comp_bufs[i]);
86 rte_pktmbuf_free(mem->decomp_bufs[i]);
89 rte_free(mem->decomp_bufs);
90 rte_free(mem->comp_bufs);
91 rte_free(mem->decompressed_data);
92 rte_free(mem->compressed_data);
93 rte_mempool_free(mem->op_pool);
94 rte_mempool_free(mem->decomp_buf_pool);
95 rte_mempool_free(mem->comp_buf_pool);
99 comp_perf_allocate_memory(struct comp_test_data *test_data,
100 struct cperf_mem_resources *mem)
102 test_data->out_seg_sz = find_buf_size(test_data->seg_sz);
103 /* Number of segments for input and output
104 * (compression and decompression)
106 uint32_t total_segs = DIV_CEIL(test_data->input_data_sz,
108 char pool_name[32] = "";
110 snprintf(pool_name, sizeof(pool_name), "comp_buf_pool_%u_qp_%u",
111 mem->dev_id, mem->qp_id);
112 mem->comp_buf_pool = rte_pktmbuf_pool_create(pool_name,
115 test_data->out_seg_sz + RTE_PKTMBUF_HEADROOM,
117 if (mem->comp_buf_pool == NULL) {
118 RTE_LOG(ERR, USER1, "Mbuf mempool could not be created\n");
122 snprintf(pool_name, sizeof(pool_name), "decomp_buf_pool_%u_qp_%u",
123 mem->dev_id, mem->qp_id);
124 mem->decomp_buf_pool = rte_pktmbuf_pool_create(pool_name,
126 0, 0, test_data->seg_sz + RTE_PKTMBUF_HEADROOM,
128 if (mem->decomp_buf_pool == NULL) {
129 RTE_LOG(ERR, USER1, "Mbuf mempool could not be created\n");
133 mem->total_bufs = DIV_CEIL(total_segs, test_data->max_sgl_segs);
135 snprintf(pool_name, sizeof(pool_name), "op_pool_%u_qp_%u",
136 mem->dev_id, mem->qp_id);
137 mem->op_pool = rte_comp_op_pool_create(pool_name,
139 0, 0, rte_socket_id());
140 if (mem->op_pool == NULL) {
141 RTE_LOG(ERR, USER1, "Comp op mempool could not be created\n");
146 * Compressed data might be a bit larger than input data,
147 * if data cannot be compressed
149 mem->compressed_data = rte_zmalloc_socket(NULL,
150 test_data->input_data_sz * EXPANSE_RATIO
151 + MIN_COMPRESSED_BUF_SIZE, 0,
153 if (mem->compressed_data == NULL) {
154 RTE_LOG(ERR, USER1, "Memory to hold the data from the input "
155 "file could not be allocated\n");
159 mem->decompressed_data = rte_zmalloc_socket(NULL,
160 test_data->input_data_sz, 0,
162 if (mem->decompressed_data == NULL) {
163 RTE_LOG(ERR, USER1, "Memory to hold the data from the input "
164 "file could not be allocated\n");
168 mem->comp_bufs = rte_zmalloc_socket(NULL,
169 mem->total_bufs * sizeof(struct rte_mbuf *),
171 if (mem->comp_bufs == NULL) {
172 RTE_LOG(ERR, USER1, "Memory to hold the compression mbufs"
173 " could not be allocated\n");
177 mem->decomp_bufs = rte_zmalloc_socket(NULL,
178 mem->total_bufs * sizeof(struct rte_mbuf *),
180 if (mem->decomp_bufs == NULL) {
181 RTE_LOG(ERR, USER1, "Memory to hold the decompression mbufs"
182 " could not be allocated\n");
186 buffer_info.total_segments = total_segs;
187 buffer_info.segment_sz = test_data->seg_sz;
188 buffer_info.total_buffs = mem->total_bufs;
189 buffer_info.segments_per_buff = test_data->max_sgl_segs;
190 buffer_info.input_data_sz = test_data->input_data_sz;
196 prepare_bufs(struct comp_test_data *test_data, struct cperf_mem_resources *mem)
198 uint32_t remaining_data = test_data->input_data_sz;
199 uint8_t *input_data_ptr = test_data->input_data;
203 uint16_t segs_per_mbuf = 0;
205 for (i = 0; i < mem->total_bufs; i++) {
206 /* Allocate data in input mbuf and copy data from input file */
207 mem->decomp_bufs[i] =
208 rte_pktmbuf_alloc(mem->decomp_buf_pool);
209 if (mem->decomp_bufs[i] == NULL) {
210 RTE_LOG(ERR, USER1, "Could not allocate mbuf\n");
214 data_sz = RTE_MIN(remaining_data, test_data->seg_sz);
215 data_addr = (uint8_t *) rte_pktmbuf_append(
216 mem->decomp_bufs[i], data_sz);
217 if (data_addr == NULL) {
218 RTE_LOG(ERR, USER1, "Could not append data\n");
221 rte_memcpy(data_addr, input_data_ptr, data_sz);
223 input_data_ptr += data_sz;
224 remaining_data -= data_sz;
226 /* Already one segment in the mbuf */
229 /* Chain mbufs if needed for input mbufs */
230 while (segs_per_mbuf < test_data->max_sgl_segs
231 && remaining_data > 0) {
232 struct rte_mbuf *next_seg =
233 rte_pktmbuf_alloc(mem->decomp_buf_pool);
235 if (next_seg == NULL) {
237 "Could not allocate mbuf\n");
241 data_sz = RTE_MIN(remaining_data, test_data->seg_sz);
242 data_addr = (uint8_t *)rte_pktmbuf_append(next_seg,
245 if (data_addr == NULL) {
246 RTE_LOG(ERR, USER1, "Could not append data\n");
250 rte_memcpy(data_addr, input_data_ptr, data_sz);
251 input_data_ptr += data_sz;
252 remaining_data -= data_sz;
254 if (rte_pktmbuf_chain(mem->decomp_bufs[i],
256 RTE_LOG(ERR, USER1, "Could not chain mbufs\n");
262 /* Allocate data in output mbuf */
264 rte_pktmbuf_alloc(mem->comp_buf_pool);
265 if (mem->comp_bufs[i] == NULL) {
266 RTE_LOG(ERR, USER1, "Could not allocate mbuf\n");
269 data_addr = (uint8_t *) rte_pktmbuf_append(
271 test_data->out_seg_sz);
272 if (data_addr == NULL) {
273 RTE_LOG(ERR, USER1, "Could not append data\n");
277 /* Chain mbufs if needed for output mbufs */
278 for (j = 1; j < segs_per_mbuf; j++) {
279 struct rte_mbuf *next_seg =
280 rte_pktmbuf_alloc(mem->comp_buf_pool);
282 if (next_seg == NULL) {
284 "Could not allocate mbuf\n");
288 data_addr = (uint8_t *)rte_pktmbuf_append(next_seg,
289 test_data->out_seg_sz);
291 if (data_addr == NULL) {
292 RTE_LOG(ERR, USER1, "Could not append data\n");
296 if (rte_pktmbuf_chain(mem->comp_bufs[i],
298 RTE_LOG(ERR, USER1, "Could not chain mbufs\n");
304 buffer_info.segments_per_last_buff = segs_per_mbuf;
305 buffer_info.last_segment_sz = data_sz;
311 print_test_dynamics(void)
313 uint32_t opt_total_segs = DIV_CEIL(buffer_info.input_data_sz,
316 if (buffer_info.total_buffs > 1) {
317 printf("\nWarning: for the current input parameters, number"
318 " of ops is higher than one, which may result"
319 " in sub-optimal performance.\n");
320 printf("To improve the performance (for the current"
321 " input data) following parameters are"
323 printf(" * Segment size: %d\n", MAX_SEG_SIZE);
324 printf(" * Number of segments: %u\n", opt_total_segs);
325 } else if (buffer_info.total_buffs == 1) {
326 printf("\nInfo: there is only one op with %u segments -"
327 " the compression ratio is the best.\n",
328 buffer_info.segments_per_last_buff);
329 if (buffer_info.segment_sz < MAX_SEG_SIZE)
330 printf("To reduce compression time, please use"
331 " bigger segment size: %d.\n",
333 else if (buffer_info.segment_sz == MAX_SEG_SIZE)
334 printf("Segment size is optimal for the best"
337 printf("Warning: something wrong happened!!\n");
339 printf("\nFor the current input parameters (segment size = %u,"
340 " maximum segments per SGL = %u):\n",
341 buffer_info.segment_sz,
342 buffer_info.segments_per_buff);
343 printf(" * Total number of buffers: %d\n",
344 buffer_info.total_segments);
345 printf(" * %u buffer(s) %u bytes long, last buffer %u"
347 buffer_info.total_segments - 1,
348 buffer_info.segment_sz,
349 buffer_info.last_segment_sz);
350 printf(" * Number of ops: %u\n", buffer_info.total_buffs);
351 printf(" * Total memory allocation: %u\n",
352 (buffer_info.total_segments - 1) * buffer_info.segment_sz
353 + buffer_info.last_segment_sz);
354 if (buffer_info.total_buffs > 1)
355 printf(" * %u ops: %u segment(s) in each,"
356 " segment size %u\n",
357 buffer_info.total_buffs - 1,
358 buffer_info.segments_per_buff,
359 buffer_info.segment_sz);
360 if (buffer_info.segments_per_last_buff > 1) {
361 printf(" * 1 op %u segments:\n",
362 buffer_info.segments_per_last_buff);
363 printf(" o %u segment size %u\n",
364 buffer_info.segments_per_last_buff - 1,
365 buffer_info.segment_sz);
366 printf(" o last segment size %u\n",
367 buffer_info.last_segment_sz);
368 } else if (buffer_info.segments_per_last_buff == 1) {
369 printf(" * 1 op (the last one): %u segment %u"
371 buffer_info.segments_per_last_buff,
372 buffer_info.last_segment_sz);