app/compress-perf: remove magic numbers
[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                         if (unlikely(test_data->perf_comp_force_stop))
188                                 goto end;
189
190                         num_enq = rte_compressdev_enqueue_burst(dev_id,
191                                                                 mem->qp_id, ops,
192                                                                 num_ops);
193                         if (num_enq == 0) {
194                                 struct rte_compressdev_stats stats;
195
196                                 rte_compressdev_stats_get(dev_id, &stats);
197                                 if (stats.enqueue_err_count) {
198                                         res = -1;
199                                         goto end;
200                                 }
201                         }
202
203                         ops_unused = num_ops - num_enq;
204                         remaining_ops -= num_enq;
205                         total_enq_ops += num_enq;
206
207                         num_deq = rte_compressdev_dequeue_burst(dev_id,
208                                                            mem->qp_id,
209                                                            deq_ops,
210                                                            test_data->burst_sz);
211                         total_deq_ops += num_deq;
212
213                         if (iter == num_iter - 1) {
214                                 for (i = 0; i < num_deq; i++) {
215                                         struct rte_comp_op *op = deq_ops[i];
216
217                                         if (op->status !=
218                                                 RTE_COMP_OP_STATUS_SUCCESS) {
219                                                 RTE_LOG(ERR, USER1,
220                                        "Some operations were not successful\n");
221                                                 goto end;
222                                         }
223
224                                         struct rte_mbuf *m = op->m_dst;
225
226                                         m->pkt_len = op->produced;
227                                         uint32_t remaining_data = op->produced;
228                                         uint16_t data_to_append;
229
230                                         while (remaining_data > 0) {
231                                                 data_to_append =
232                                                         RTE_MIN(remaining_data,
233                                                              out_seg_sz);
234                                                 m->data_len = data_to_append;
235                                                 remaining_data -=
236                                                                 data_to_append;
237                                                 m = m->next;
238                                         }
239                                 }
240                         }
241                         rte_mempool_put_bulk(mem->op_pool,
242                                              (void **)deq_ops, num_deq);
243                         allocated -= num_deq;
244                 }
245
246                 /* Dequeue the last operations */
247                 while (total_deq_ops < total_ops) {
248                         if (unlikely(test_data->perf_comp_force_stop))
249                                 goto end;
250
251                         num_deq = rte_compressdev_dequeue_burst(dev_id,
252                                                            mem->qp_id,
253                                                            deq_ops,
254                                                            test_data->burst_sz);
255                         if (num_deq == 0) {
256                                 struct rte_compressdev_stats stats;
257
258                                 rte_compressdev_stats_get(dev_id, &stats);
259                                 if (stats.dequeue_err_count) {
260                                         res = -1;
261                                         goto end;
262                                 }
263                         }
264
265                         total_deq_ops += num_deq;
266
267                         if (iter == num_iter - 1) {
268                                 for (i = 0; i < num_deq; i++) {
269                                         struct rte_comp_op *op = deq_ops[i];
270
271                                         if (op->status !=
272                                                 RTE_COMP_OP_STATUS_SUCCESS) {
273                                                 RTE_LOG(ERR, USER1,
274                                        "Some operations were not successful\n");
275                                                 goto end;
276                                         }
277
278                                         struct rte_mbuf *m = op->m_dst;
279
280                                         m->pkt_len = op->produced;
281                                         uint32_t remaining_data = op->produced;
282                                         uint16_t data_to_append;
283
284                                         while (remaining_data > 0) {
285                                                 data_to_append =
286                                                 RTE_MIN(remaining_data,
287                                                         out_seg_sz);
288                                                 m->data_len = data_to_append;
289                                                 remaining_data -=
290                                                                 data_to_append;
291                                                 m = m->next;
292                                         }
293                                 }
294                         }
295                         rte_mempool_put_bulk(mem->op_pool,
296                                              (void **)deq_ops, num_deq);
297                         allocated -= num_deq;
298                 }
299         }
300
301         tsc_end = rte_rdtsc_precise();
302         tsc_duration = tsc_end - tsc_start;
303
304         if (type == RTE_COMP_COMPRESS)
305                 ctx->comp_tsc_duration[test_data->level] =
306                                 tsc_duration / num_iter;
307         else
308                 ctx->decomp_tsc_duration[test_data->level] =
309                                 tsc_duration / num_iter;
310
311 end:
312         rte_mempool_put_bulk(mem->op_pool, (void **)ops, allocated);
313         rte_compressdev_private_xform_free(dev_id, priv_xform);
314         rte_free(ops);
315
316         if (test_data->perf_comp_force_stop) {
317                 RTE_LOG(ERR, USER1,
318                       "lcore: %d Perf. test has been aborted by user\n",
319                         mem->lcore_id);
320                 res = -1;
321         }
322         return res;
323 }
324
325 int
326 cperf_benchmark_test_runner(void *test_ctx)
327 {
328         struct cperf_benchmark_ctx *ctx = test_ctx;
329         struct comp_test_data *test_data = ctx->ver.options;
330         uint32_t lcore = rte_lcore_id();
331         static rte_atomic16_t display_once = RTE_ATOMIC16_INIT(0);
332
333         ctx->ver.mem.lcore_id = lcore;
334         int i, ret = EXIT_SUCCESS;
335
336         /*
337          * First the verification part is needed
338          */
339         if (cperf_verify_test_runner(&ctx->ver)) {
340                 ret =  EXIT_FAILURE;
341                 goto end;
342         }
343
344         /*
345          * Run the tests twice, discarding the first performance
346          * results, before the cache is warmed up
347          */
348         for (i = 0; i < 2; i++) {
349                 if (main_loop(ctx, RTE_COMP_COMPRESS) < 0) {
350                         ret = EXIT_FAILURE;
351                         goto end;
352                 }
353         }
354
355         for (i = 0; i < 2; i++) {
356                 if (main_loop(ctx, RTE_COMP_DECOMPRESS) < 0) {
357                         ret = EXIT_FAILURE;
358                         goto end;
359                 }
360         }
361
362         ctx->comp_tsc_byte =
363                         (double)(ctx->comp_tsc_duration[test_data->level]) /
364                                         test_data->input_data_sz;
365
366         ctx->decomp_tsc_byte =
367                         (double)(ctx->decomp_tsc_duration[test_data->level]) /
368                                         test_data->input_data_sz;
369
370         ctx->comp_gbps = rte_get_tsc_hz() / ctx->comp_tsc_byte * 8 /
371                         1000000000;
372
373         ctx->decomp_gbps = rte_get_tsc_hz() / ctx->decomp_tsc_byte * 8 /
374                         1000000000;
375
376         if (rte_atomic16_test_and_set(&display_once)) {
377                 printf("%12s%6s%12s%17s%15s%16s\n",
378                         "lcore id", "Level", "Comp size", "Comp ratio [%]",
379                         "Comp [Gbps]", "Decomp [Gbps]");
380         }
381
382         printf("%12u%6u%12zu%17.2f%15.2f%16.2f\n",
383                 ctx->ver.mem.lcore_id,
384                 test_data->level, ctx->ver.comp_data_sz, ctx->ver.ratio,
385                 ctx->comp_gbps,
386                 ctx->decomp_gbps);
387
388 end:
389         return ret;
390 }