app/compress-perf: add incompressible data handling
[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 static int
14 main_loop(struct comp_test_data *test_data, uint8_t level,
15                         enum rte_comp_xform_type type)
16 {
17         uint8_t dev_id = test_data->cdev_id;
18         uint32_t i, iter, num_iter;
19         struct rte_comp_op **ops, **deq_ops;
20         void *priv_xform = NULL;
21         struct rte_comp_xform xform;
22         struct rte_mbuf **input_bufs, **output_bufs;
23         int res = 0;
24         int allocated = 0;
25         uint32_t out_seg_sz;
26
27         if (test_data == NULL || !test_data->burst_sz) {
28                 RTE_LOG(ERR, USER1,
29                         "Unknown burst size\n");
30                 return -1;
31         }
32
33         ops = rte_zmalloc_socket(NULL,
34                 2 * test_data->total_bufs * sizeof(struct rte_comp_op *),
35                 0, rte_socket_id());
36
37         if (ops == NULL) {
38                 RTE_LOG(ERR, USER1,
39                         "Can't allocate memory for ops strucures\n");
40                 return -1;
41         }
42
43         deq_ops = &ops[test_data->total_bufs];
44
45         if (type == RTE_COMP_COMPRESS) {
46                 xform = (struct rte_comp_xform) {
47                         .type = RTE_COMP_COMPRESS,
48                         .compress = {
49                                 .algo = RTE_COMP_ALGO_DEFLATE,
50                                 .deflate.huffman = test_data->huffman_enc,
51                                 .level = level,
52                                 .window_size = test_data->window_sz,
53                                 .chksum = RTE_COMP_CHECKSUM_NONE,
54                                 .hash_algo = RTE_COMP_HASH_ALGO_NONE
55                         }
56                 };
57                 input_bufs = test_data->decomp_bufs;
58                 output_bufs = test_data->comp_bufs;
59                 out_seg_sz = test_data->out_seg_sz;
60         } else {
61                 xform = (struct rte_comp_xform) {
62                         .type = RTE_COMP_DECOMPRESS,
63                         .decompress = {
64                                 .algo = RTE_COMP_ALGO_DEFLATE,
65                                 .chksum = RTE_COMP_CHECKSUM_NONE,
66                                 .window_size = test_data->window_sz,
67                                 .hash_algo = RTE_COMP_HASH_ALGO_NONE
68                         }
69                 };
70                 input_bufs = test_data->comp_bufs;
71                 output_bufs = test_data->decomp_bufs;
72                 out_seg_sz = test_data->seg_sz;
73         }
74
75         /* Create private xform */
76         if (rte_compressdev_private_xform_create(dev_id, &xform,
77                         &priv_xform) < 0) {
78                 RTE_LOG(ERR, USER1, "Private xform could not be created\n");
79                 res = -1;
80                 goto end;
81         }
82
83         uint64_t tsc_start, tsc_end, tsc_duration;
84
85         tsc_start = tsc_end = tsc_duration = 0;
86         tsc_start = rte_rdtsc();
87         num_iter = test_data->num_iter;
88
89         for (iter = 0; iter < num_iter; iter++) {
90                 uint32_t total_ops = test_data->total_bufs;
91                 uint32_t remaining_ops = test_data->total_bufs;
92                 uint32_t total_deq_ops = 0;
93                 uint32_t total_enq_ops = 0;
94                 uint16_t ops_unused = 0;
95                 uint16_t num_enq = 0;
96                 uint16_t num_deq = 0;
97
98                 while (remaining_ops > 0) {
99                         uint16_t num_ops = RTE_MIN(remaining_ops,
100                                                    test_data->burst_sz);
101                         uint16_t ops_needed = num_ops - ops_unused;
102
103                         /*
104                          * Move the unused operations from the previous
105                          * enqueue_burst call to the front, to maintain order
106                          */
107                         if ((ops_unused > 0) && (num_enq > 0)) {
108                                 size_t nb_b_to_mov =
109                                       ops_unused * sizeof(struct rte_comp_op *);
110
111                                 memmove(ops, &ops[num_enq], nb_b_to_mov);
112                         }
113
114                         /* Allocate compression operations */
115                         if (ops_needed && !rte_comp_op_bulk_alloc(
116                                                 test_data->op_pool,
117                                                 &ops[ops_unused],
118                                                 ops_needed)) {
119                                 RTE_LOG(ERR, USER1,
120                                       "Could not allocate enough operations\n");
121                                 res = -1;
122                                 goto end;
123                         }
124                         allocated += ops_needed;
125
126                         for (i = 0; i < ops_needed; i++) {
127                                 /*
128                                  * Calculate next buffer to attach to operation
129                                  */
130                                 uint32_t buf_id = total_enq_ops + i +
131                                                 ops_unused;
132                                 uint16_t op_id = ops_unused + i;
133                                 /* Reset all data in output buffers */
134                                 struct rte_mbuf *m = output_bufs[buf_id];
135
136                                 m->pkt_len = out_seg_sz * m->nb_segs;
137                                 while (m) {
138                                         m->data_len = m->buf_len - m->data_off;
139                                         m = m->next;
140                                 }
141                                 ops[op_id]->m_src = input_bufs[buf_id];
142                                 ops[op_id]->m_dst = output_bufs[buf_id];
143                                 ops[op_id]->src.offset = 0;
144                                 ops[op_id]->src.length =
145                                         rte_pktmbuf_pkt_len(input_bufs[buf_id]);
146                                 ops[op_id]->dst.offset = 0;
147                                 ops[op_id]->flush_flag = RTE_COMP_FLUSH_FINAL;
148                                 ops[op_id]->input_chksum = buf_id;
149                                 ops[op_id]->private_xform = priv_xform;
150                         }
151
152                         num_enq = rte_compressdev_enqueue_burst(dev_id, 0, ops,
153                                                                 num_ops);
154                         if (num_enq == 0) {
155                                 struct rte_compressdev_stats stats;
156
157                                 rte_compressdev_stats_get(dev_id, &stats);
158                                 if (stats.enqueue_err_count) {
159                                         res = -1;
160                                         goto end;
161                                 }
162                         }
163
164                         ops_unused = num_ops - num_enq;
165                         remaining_ops -= num_enq;
166                         total_enq_ops += num_enq;
167
168                         num_deq = rte_compressdev_dequeue_burst(dev_id, 0,
169                                                            deq_ops,
170                                                            test_data->burst_sz);
171                         total_deq_ops += num_deq;
172
173                         if (iter == num_iter - 1) {
174                                 for (i = 0; i < num_deq; i++) {
175                                         struct rte_comp_op *op = deq_ops[i];
176
177                                         if (op->status !=
178                                                 RTE_COMP_OP_STATUS_SUCCESS) {
179                                                 RTE_LOG(ERR, USER1,
180                                                         "Some operations were not successful\n");
181                                                 goto end;
182                                         }
183
184                                         struct rte_mbuf *m = op->m_dst;
185
186                                         m->pkt_len = op->produced;
187                                         uint32_t remaining_data = op->produced;
188                                         uint16_t data_to_append;
189
190                                         while (remaining_data > 0) {
191                                                 data_to_append =
192                                                         RTE_MIN(remaining_data,
193                                                              out_seg_sz);
194                                                 m->data_len = data_to_append;
195                                                 remaining_data -=
196                                                                 data_to_append;
197                                                 m = m->next;
198                                         }
199                                 }
200                         }
201                         rte_mempool_put_bulk(test_data->op_pool,
202                                              (void **)deq_ops, num_deq);
203                         allocated -= num_deq;
204                 }
205
206                 /* Dequeue the last operations */
207                 while (total_deq_ops < total_ops) {
208                         num_deq = rte_compressdev_dequeue_burst(dev_id, 0,
209                                                 deq_ops, test_data->burst_sz);
210                         if (num_deq == 0) {
211                                 struct rte_compressdev_stats stats;
212
213                                 rte_compressdev_stats_get(dev_id, &stats);
214                                 if (stats.dequeue_err_count) {
215                                         res = -1;
216                                         goto end;
217                                 }
218                         }
219
220                         total_deq_ops += num_deq;
221
222                         if (iter == num_iter - 1) {
223                                 for (i = 0; i < num_deq; i++) {
224                                         struct rte_comp_op *op = deq_ops[i];
225
226                                         if (op->status !=
227                                                 RTE_COMP_OP_STATUS_SUCCESS) {
228                                                 RTE_LOG(ERR, USER1,
229                                                         "Some operations were not successful\n");
230                                                 goto end;
231                                         }
232
233                                         struct rte_mbuf *m = op->m_dst;
234
235                                         m->pkt_len = op->produced;
236                                         uint32_t remaining_data = op->produced;
237                                         uint16_t data_to_append;
238
239                                         while (remaining_data > 0) {
240                                                 data_to_append =
241                                                 RTE_MIN(remaining_data,
242                                                         out_seg_sz);
243                                                 m->data_len = data_to_append;
244                                                 remaining_data -=
245                                                                 data_to_append;
246                                                 m = m->next;
247                                         }
248                                 }
249                         }
250                         rte_mempool_put_bulk(test_data->op_pool,
251                                              (void **)deq_ops, num_deq);
252                         allocated -= num_deq;
253                 }
254         }
255
256         tsc_end = rte_rdtsc();
257         tsc_duration = tsc_end - tsc_start;
258
259         if (type == RTE_COMP_COMPRESS)
260                 test_data->comp_tsc_duration[level] =
261                                 tsc_duration / num_iter;
262         else
263                 test_data->decomp_tsc_duration[level] =
264                                 tsc_duration / num_iter;
265
266 end:
267         rte_mempool_put_bulk(test_data->op_pool, (void **)ops, allocated);
268         rte_compressdev_private_xform_free(dev_id, priv_xform);
269         rte_free(ops);
270         return res;
271 }
272
273 int
274 cperf_benchmark(struct comp_test_data *test_data, uint8_t level)
275 {
276         int i, ret = EXIT_SUCCESS;
277
278         /*
279          * Run the tests twice, discarding the first performance
280          * results, before the cache is warmed up
281          */
282         for (i = 0; i < 2; i++) {
283                 if (main_loop(test_data, level, RTE_COMP_COMPRESS) < 0) {
284                         ret = EXIT_FAILURE;
285                         goto end;
286                 }
287         }
288
289         for (i = 0; i < 2; i++) {
290                 if (main_loop(test_data, level, RTE_COMP_DECOMPRESS) < 0) {
291                         ret = EXIT_FAILURE;
292                         goto end;
293                 }
294         }
295
296         test_data->comp_tsc_byte =
297                         (double)(test_data->comp_tsc_duration[level]) /
298                                         test_data->input_data_sz;
299
300         test_data->decomp_tsc_byte =
301                         (double)(test_data->decomp_tsc_duration[level]) /
302                                         test_data->input_data_sz;
303
304         test_data->comp_gbps = rte_get_tsc_hz() / test_data->comp_tsc_byte * 8 /
305                         1000000000;
306
307         test_data->decomp_gbps = rte_get_tsc_hz() / test_data->decomp_tsc_byte
308                         * 8 / 1000000000;
309 end:
310         return ret;
311 }