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