Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2018 Intel Corporation
3 : : */
4 : :
5 : : #include <stdlib.h>
6 : :
7 : : #include <rte_malloc.h>
8 : : #include <rte_eal.h>
9 : : #include <rte_log.h>
10 : : #include <rte_cycles.h>
11 : : #include <rte_compressdev.h>
12 : :
13 : : #include "comp_perf_test_throughput.h"
14 : :
15 : : void
16 : 0 : cperf_throughput_test_destructor(void *arg)
17 : : {
18 : 0 : if (arg) {
19 : 0 : comp_perf_free_memory(
20 : : ((struct cperf_benchmark_ctx *)arg)->ver.options,
21 : : &((struct cperf_benchmark_ctx *)arg)->ver.mem);
22 : 0 : rte_free(arg);
23 : : }
24 : 0 : }
25 : :
26 : : void *
27 : 0 : cperf_throughput_test_constructor(uint8_t dev_id, uint16_t qp_id,
28 : : struct comp_test_data *options)
29 : : {
30 : : struct cperf_benchmark_ctx *ctx = NULL;
31 : :
32 : 0 : ctx = rte_malloc(NULL, sizeof(struct cperf_benchmark_ctx), 0);
33 : :
34 : 0 : if (ctx == NULL)
35 : : return NULL;
36 : :
37 : 0 : ctx->ver.mem.dev_id = dev_id;
38 : 0 : ctx->ver.mem.qp_id = qp_id;
39 : 0 : ctx->ver.options = options;
40 : 0 : ctx->ver.silent = 1; /* ver. part will be silent */
41 : :
42 : 0 : if (!comp_perf_allocate_memory(ctx->ver.options, &ctx->ver.mem)
43 : 0 : && !prepare_bufs(ctx->ver.options, &ctx->ver.mem))
44 : : return ctx;
45 : :
46 : 0 : cperf_throughput_test_destructor(ctx);
47 : 0 : return NULL;
48 : : }
49 : :
50 : : static int
51 : 0 : main_loop(struct cperf_benchmark_ctx *ctx, enum rte_comp_xform_type type)
52 : : {
53 : 0 : struct comp_test_data *test_data = ctx->ver.options;
54 : : struct cperf_mem_resources *mem = &ctx->ver.mem;
55 : 0 : uint8_t dev_id = mem->dev_id;
56 : : uint32_t i, iter, num_iter;
57 : : struct rte_comp_op **ops, **deq_ops;
58 : 0 : void *priv_xform = NULL;
59 : : struct rte_comp_xform xform;
60 : : struct rte_mbuf **input_bufs, **output_bufs;
61 : : int res = 0;
62 : : int allocated = 0;
63 : : uint32_t out_seg_sz;
64 : :
65 : 0 : if (test_data == NULL || !test_data->burst_sz) {
66 : 0 : RTE_LOG(ERR, USER1,
67 : : "Unknown burst size\n");
68 : 0 : return -1;
69 : : }
70 : :
71 : 0 : ops = rte_zmalloc_socket(NULL,
72 : 0 : 2 * mem->total_bufs * sizeof(struct rte_comp_op *),
73 : 0 : 0, rte_socket_id());
74 : :
75 : 0 : if (ops == NULL) {
76 : 0 : RTE_LOG(ERR, USER1,
77 : : "Can't allocate memory for ops structures\n");
78 : 0 : return -1;
79 : : }
80 : :
81 : 0 : deq_ops = &ops[mem->total_bufs];
82 : :
83 : 0 : if (type == RTE_COMP_COMPRESS) {
84 : 0 : xform = (struct rte_comp_xform) {
85 : : .type = RTE_COMP_COMPRESS,
86 : : .compress = {
87 : 0 : .algo = test_data->test_algo,
88 : 0 : .level = test_data->level,
89 : 0 : .window_size = test_data->window_sz,
90 : : .chksum = RTE_COMP_CHECKSUM_NONE,
91 : : .hash_algo = RTE_COMP_HASH_ALGO_NONE
92 : : }
93 : : };
94 : 0 : if (test_data->test_algo == RTE_COMP_ALGO_DEFLATE)
95 : 0 : xform.compress.deflate.huffman = test_data->huffman_enc;
96 : 0 : else if (test_data->test_algo == RTE_COMP_ALGO_LZ4)
97 : 0 : xform.compress.lz4.flags = test_data->lz4_flags;
98 : 0 : input_bufs = mem->decomp_bufs;
99 : 0 : output_bufs = mem->comp_bufs;
100 : 0 : out_seg_sz = test_data->out_seg_sz;
101 : : } else {
102 : 0 : xform = (struct rte_comp_xform) {
103 : : .type = RTE_COMP_DECOMPRESS,
104 : : .decompress = {
105 : 0 : .algo = test_data->test_algo,
106 : : .chksum = RTE_COMP_CHECKSUM_NONE,
107 : 0 : .window_size = test_data->window_sz,
108 : : .hash_algo = RTE_COMP_HASH_ALGO_NONE
109 : : }
110 : : };
111 : 0 : if (test_data->test_algo == RTE_COMP_ALGO_LZ4)
112 : 0 : xform.decompress.lz4.flags = test_data->lz4_flags;
113 : 0 : input_bufs = mem->comp_bufs;
114 : 0 : output_bufs = mem->decomp_bufs;
115 : 0 : out_seg_sz = test_data->seg_sz;
116 : : }
117 : :
118 : : /* Create private xform */
119 : 0 : if (rte_compressdev_private_xform_create(dev_id, &xform,
120 : : &priv_xform) < 0) {
121 : 0 : RTE_LOG(ERR, USER1, "Private xform could not be created\n");
122 : : res = -1;
123 : 0 : goto end;
124 : : }
125 : :
126 : : uint64_t tsc_start, tsc_end, tsc_duration;
127 : :
128 : 0 : num_iter = test_data->num_iter;
129 : : tsc_start = tsc_end = tsc_duration = 0;
130 : : tsc_start = rte_rdtsc_precise();
131 : :
132 : 0 : for (iter = 0; iter < num_iter; iter++) {
133 : 0 : uint32_t total_ops = mem->total_bufs;
134 : : uint32_t remaining_ops = mem->total_bufs;
135 : : uint32_t total_deq_ops = 0;
136 : : uint32_t total_enq_ops = 0;
137 : : uint16_t ops_unused = 0;
138 : : uint16_t num_enq = 0;
139 : : uint16_t num_deq = 0;
140 : :
141 : 0 : while (remaining_ops > 0) {
142 : 0 : uint16_t num_ops = RTE_MIN(remaining_ops,
143 : : test_data->burst_sz);
144 : 0 : uint16_t ops_needed = num_ops - ops_unused;
145 : :
146 : : /*
147 : : * Move the unused operations from the previous
148 : : * enqueue_burst call to the front, to maintain order
149 : : */
150 : 0 : if ((ops_unused > 0) && (num_enq > 0)) {
151 : 0 : size_t nb_b_to_mov =
152 : 0 : ops_unused * sizeof(struct rte_comp_op *);
153 : :
154 : 0 : memmove(ops, &ops[num_enq], nb_b_to_mov);
155 : : }
156 : :
157 : : /* Allocate compression operations */
158 : 0 : if (ops_needed && !rte_comp_op_bulk_alloc(
159 : : mem->op_pool,
160 : 0 : &ops[ops_unused],
161 : : ops_needed)) {
162 : 0 : RTE_LOG(ERR, USER1,
163 : : "Could not allocate enough operations\n");
164 : : res = -1;
165 : 0 : goto end;
166 : : }
167 : 0 : allocated += ops_needed;
168 : :
169 : 0 : for (i = 0; i < ops_needed; i++) {
170 : : /*
171 : : * Calculate next buffer to attach to operation
172 : : */
173 : 0 : uint32_t buf_id = total_enq_ops + i +
174 : : ops_unused;
175 : 0 : uint16_t op_id = ops_unused + i;
176 : : /* Reset all data in output buffers */
177 : 0 : struct rte_mbuf *m = output_bufs[buf_id];
178 : :
179 : 0 : m->pkt_len = out_seg_sz * m->nb_segs;
180 : 0 : while (m) {
181 : 0 : m->data_len = m->buf_len - m->data_off;
182 : 0 : m = m->next;
183 : : }
184 : 0 : ops[op_id]->m_src = input_bufs[buf_id];
185 : 0 : ops[op_id]->m_dst = output_bufs[buf_id];
186 : 0 : ops[op_id]->src.offset = 0;
187 : 0 : ops[op_id]->src.length =
188 : 0 : rte_pktmbuf_pkt_len(input_bufs[buf_id]);
189 : 0 : ops[op_id]->dst.offset = 0;
190 : 0 : ops[op_id]->flush_flag = RTE_COMP_FLUSH_FINAL;
191 : 0 : ops[op_id]->input_chksum = buf_id;
192 : 0 : ops[op_id]->private_xform = priv_xform;
193 : : }
194 : :
195 : 0 : if (unlikely(test_data->perf_comp_force_stop))
196 : 0 : goto end;
197 : :
198 : 0 : num_enq = rte_compressdev_enqueue_burst(dev_id,
199 : 0 : mem->qp_id, ops,
200 : : num_ops);
201 : 0 : if (num_enq == 0) {
202 : : struct rte_compressdev_stats stats;
203 : :
204 : 0 : rte_compressdev_stats_get(dev_id, &stats);
205 : 0 : if (stats.enqueue_err_count) {
206 : : res = -1;
207 : 0 : goto end;
208 : : }
209 : : }
210 : :
211 : 0 : ops_unused = num_ops - num_enq;
212 : 0 : remaining_ops -= num_enq;
213 : 0 : total_enq_ops += num_enq;
214 : :
215 : 0 : num_deq = rte_compressdev_dequeue_burst(dev_id,
216 : 0 : mem->qp_id,
217 : : deq_ops,
218 : 0 : test_data->burst_sz);
219 : 0 : total_deq_ops += num_deq;
220 : :
221 : 0 : if (iter == num_iter - 1) {
222 : 0 : for (i = 0; i < num_deq; i++) {
223 : 0 : struct rte_comp_op *op = deq_ops[i];
224 : :
225 : 0 : if (op->status !=
226 : : RTE_COMP_OP_STATUS_SUCCESS) {
227 : 0 : RTE_LOG(ERR, USER1,
228 : : "Some operations were not successful\n");
229 : 0 : goto end;
230 : : }
231 : :
232 : 0 : struct rte_mbuf *m = op->m_dst;
233 : :
234 : 0 : m->pkt_len = op->produced;
235 : : uint32_t remaining_data = op->produced;
236 : : uint16_t data_to_append;
237 : :
238 : 0 : while (remaining_data > 0) {
239 : 0 : data_to_append =
240 : 0 : RTE_MIN(remaining_data,
241 : : out_seg_sz);
242 : 0 : m->data_len = data_to_append;
243 : 0 : remaining_data -=
244 : : data_to_append;
245 : 0 : m = m->next;
246 : : }
247 : : }
248 : : }
249 : 0 : rte_mempool_put_bulk(mem->op_pool,
250 : : (void **)deq_ops, num_deq);
251 : 0 : allocated -= num_deq;
252 : : }
253 : :
254 : : /* Dequeue the last operations */
255 : 0 : while (total_deq_ops < total_ops) {
256 : 0 : if (unlikely(test_data->perf_comp_force_stop))
257 : 0 : goto end;
258 : :
259 : 0 : num_deq = rte_compressdev_dequeue_burst(dev_id,
260 : 0 : mem->qp_id,
261 : : deq_ops,
262 : 0 : test_data->burst_sz);
263 : 0 : if (num_deq == 0) {
264 : : struct rte_compressdev_stats stats;
265 : :
266 : 0 : rte_compressdev_stats_get(dev_id, &stats);
267 : 0 : if (stats.dequeue_err_count) {
268 : : res = -1;
269 : 0 : goto end;
270 : : }
271 : : }
272 : :
273 : 0 : total_deq_ops += num_deq;
274 : :
275 : 0 : if (iter == num_iter - 1) {
276 : 0 : for (i = 0; i < num_deq; i++) {
277 : 0 : struct rte_comp_op *op = deq_ops[i];
278 : :
279 : 0 : if (op->status !=
280 : : RTE_COMP_OP_STATUS_SUCCESS) {
281 : 0 : RTE_LOG(ERR, USER1,
282 : : "Some operations were not successful\n");
283 : 0 : goto end;
284 : : }
285 : :
286 : 0 : struct rte_mbuf *m = op->m_dst;
287 : :
288 : 0 : m->pkt_len = op->produced;
289 : : uint32_t remaining_data = op->produced;
290 : : uint16_t data_to_append;
291 : :
292 : 0 : while (remaining_data > 0) {
293 : 0 : data_to_append =
294 : 0 : RTE_MIN(remaining_data,
295 : : out_seg_sz);
296 : 0 : m->data_len = data_to_append;
297 : 0 : remaining_data -=
298 : : data_to_append;
299 : 0 : m = m->next;
300 : : }
301 : : }
302 : : }
303 : 0 : rte_mempool_put_bulk(mem->op_pool,
304 : : (void **)deq_ops, num_deq);
305 : 0 : allocated -= num_deq;
306 : : }
307 : : }
308 : :
309 : : tsc_end = rte_rdtsc_precise();
310 : 0 : tsc_duration = tsc_end - tsc_start;
311 : :
312 : 0 : if (type == RTE_COMP_COMPRESS)
313 : 0 : ctx->comp_tsc_duration[test_data->level] =
314 : 0 : tsc_duration / num_iter;
315 : : else
316 : 0 : ctx->decomp_tsc_duration[test_data->level] =
317 : 0 : tsc_duration / num_iter;
318 : :
319 : 0 : end:
320 : 0 : rte_mempool_put_bulk(mem->op_pool, (void **)ops, allocated);
321 : 0 : rte_compressdev_private_xform_free(dev_id, priv_xform);
322 : 0 : rte_free(ops);
323 : :
324 : 0 : if (test_data->perf_comp_force_stop) {
325 : 0 : RTE_LOG(ERR, USER1,
326 : : "lcore: %d Perf. test has been aborted by user\n",
327 : : mem->lcore_id);
328 : : res = -1;
329 : : }
330 : : return res;
331 : : }
332 : :
333 : : int
334 : 0 : cperf_throughput_test_runner(void *test_ctx)
335 : : {
336 : : struct cperf_benchmark_ctx *ctx = test_ctx;
337 : 0 : struct comp_test_data *test_data = ctx->ver.options;
338 : : uint32_t lcore = rte_lcore_id();
339 : : static RTE_ATOMIC(uint16_t) display_once;
340 : : int i, ret = EXIT_SUCCESS;
341 : :
342 : 0 : ctx->ver.mem.lcore_id = lcore;
343 : :
344 : : uint16_t exp = 0;
345 : : /*
346 : : * printing information about current compression thread
347 : : */
348 : 0 : if (rte_atomic_compare_exchange_strong_explicit(&ctx->ver.mem.print_info_once, &exp,
349 : : 1, rte_memory_order_relaxed, rte_memory_order_relaxed))
350 : 0 : printf(" lcore: %u,"
351 : : " driver name: %s,"
352 : : " device name: %s,"
353 : : " device id: %u,"
354 : : " socket id: %u,"
355 : : " queue pair id: %u\n",
356 : : lcore,
357 : 0 : ctx->ver.options->driver_name,
358 : : rte_compressdev_name_get(ctx->ver.mem.dev_id),
359 : 0 : ctx->ver.mem.dev_id,
360 : 0 : rte_compressdev_socket_id(ctx->ver.mem.dev_id),
361 : 0 : ctx->ver.mem.qp_id);
362 : :
363 : : /*
364 : : * First the verification part is needed
365 : : */
366 : 0 : if (cperf_verify_test_runner(&ctx->ver)) {
367 : : ret = EXIT_FAILURE;
368 : 0 : goto end;
369 : : }
370 : :
371 : 0 : if (test_data->test_op & COMPRESS) {
372 : : /*
373 : : * Run the test twice, discarding the first performance
374 : : * results, before the cache is warmed up
375 : : */
376 : 0 : for (i = 0; i < 2; i++) {
377 : 0 : if (main_loop(ctx, RTE_COMP_COMPRESS) < 0) {
378 : : ret = EXIT_FAILURE;
379 : 0 : goto end;
380 : : }
381 : : }
382 : :
383 : 0 : ctx->comp_tsc_byte =
384 : 0 : (double)(ctx->comp_tsc_duration[test_data->level]) /
385 : 0 : test_data->input_data_sz;
386 : 0 : ctx->comp_gbps = rte_get_tsc_hz() / ctx->comp_tsc_byte * 8 /
387 : : 1000000000;
388 : : } else {
389 : 0 : ctx->comp_tsc_byte = 0;
390 : 0 : ctx->comp_gbps = 0;
391 : : }
392 : :
393 : 0 : if (test_data->test_op & DECOMPRESS) {
394 : : /*
395 : : * Run the test twice, discarding the first performance
396 : : * results, before the cache is warmed up
397 : : */
398 : 0 : for (i = 0; i < 2; i++) {
399 : 0 : if (main_loop(ctx, RTE_COMP_DECOMPRESS) < 0) {
400 : : ret = EXIT_FAILURE;
401 : 0 : goto end;
402 : : }
403 : : }
404 : :
405 : 0 : ctx->decomp_tsc_byte =
406 : 0 : (double)(ctx->decomp_tsc_duration[test_data->level]) /
407 : 0 : test_data->input_data_sz;
408 : 0 : ctx->decomp_gbps = rte_get_tsc_hz() / ctx->decomp_tsc_byte * 8 /
409 : : 1000000000;
410 : : } else {
411 : 0 : ctx->decomp_tsc_byte = 0;
412 : 0 : ctx->decomp_gbps = 0;
413 : : }
414 : :
415 : : exp = 0;
416 : 0 : if (rte_atomic_compare_exchange_strong_explicit(&display_once, &exp, 1,
417 : : rte_memory_order_relaxed, rte_memory_order_relaxed)) {
418 : : printf("\n%12s%6s%12s%17s%15s%16s\n",
419 : : "lcore id", "Level", "Comp size", "Comp ratio [%]",
420 : : "Comp [Gbps]", "Decomp [Gbps]");
421 : : }
422 : :
423 : 0 : printf("%12u%6u%12zu%17.2f%15.2f%16.2f\n",
424 : 0 : ctx->ver.mem.lcore_id,
425 : 0 : test_data->level, ctx->ver.comp_data_sz, ctx->ver.ratio,
426 : : ctx->comp_gbps,
427 : : ctx->decomp_gbps);
428 : :
429 : 0 : end:
430 : 0 : return ret;
431 : : }
|