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