app/compress-perf: print socket id
[dpdk.git] / app / test-compress-perf / main.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_compressdev.h>
9
10 #include "comp_perf_options.h"
11 #include "comp_perf_test_verify.h"
12 #include "comp_perf_test_benchmark.h"
13
14 #define NUM_MAX_XFORMS 16
15 #define NUM_MAX_INFLIGHT_OPS 512
16
17 #define DIV_CEIL(a, b)  ((a) / (b) + ((a) % (b) != 0))
18
19 /* Cleanup state machine */
20 static enum cleanup_st {
21         ST_CLEAR = 0,
22         ST_TEST_DATA,
23         ST_COMPDEV,
24         ST_INPUT_DATA,
25         ST_MEMORY_ALLOC,
26         ST_PREPARE_BUF,
27         ST_DURING_TEST
28 } cleanup = ST_CLEAR;
29
30 static int
31 param_range_check(uint16_t size, const struct rte_param_log2_range *range)
32 {
33         unsigned int next_size;
34
35         /* Check lower/upper bounds */
36         if (size < range->min)
37                 return -1;
38
39         if (size > range->max)
40                 return -1;
41
42         /* If range is actually only one value, size is correct */
43         if (range->increment == 0)
44                 return 0;
45
46         /* Check if value is one of the supported sizes */
47         for (next_size = range->min; next_size <= range->max;
48                         next_size += range->increment)
49                 if (size == next_size)
50                         return 0;
51
52         return -1;
53 }
54
55 static int
56 comp_perf_check_capabilities(struct comp_test_data *test_data)
57 {
58         const struct rte_compressdev_capabilities *cap;
59
60         cap = rte_compressdev_capability_get(test_data->cdev_id,
61                                              RTE_COMP_ALGO_DEFLATE);
62
63         if (cap == NULL) {
64                 RTE_LOG(ERR, USER1,
65                         "Compress device does not support DEFLATE\n");
66                 return -1;
67         }
68
69         uint64_t comp_flags = cap->comp_feature_flags;
70
71         /* Huffman enconding */
72         if (test_data->huffman_enc == RTE_COMP_HUFFMAN_FIXED &&
73                         (comp_flags & RTE_COMP_FF_HUFFMAN_FIXED) == 0) {
74                 RTE_LOG(ERR, USER1,
75                         "Compress device does not supported Fixed Huffman\n");
76                 return -1;
77         }
78
79         if (test_data->huffman_enc == RTE_COMP_HUFFMAN_DYNAMIC &&
80                         (comp_flags & RTE_COMP_FF_HUFFMAN_DYNAMIC) == 0) {
81                 RTE_LOG(ERR, USER1,
82                         "Compress device does not supported Dynamic Huffman\n");
83                 return -1;
84         }
85
86         /* Window size */
87         if (test_data->window_sz != -1) {
88                 if (param_range_check(test_data->window_sz, &cap->window_size)
89                                 < 0) {
90                         RTE_LOG(ERR, USER1,
91                                 "Compress device does not support "
92                                 "this window size\n");
93                         return -1;
94                 }
95         } else
96                 /* Set window size to PMD maximum if none was specified */
97                 test_data->window_sz = cap->window_size.max;
98
99         /* Check if chained mbufs is supported */
100         if (test_data->max_sgl_segs > 1  &&
101                         (comp_flags & RTE_COMP_FF_OOP_SGL_IN_SGL_OUT) == 0) {
102                 RTE_LOG(INFO, USER1, "Compress device does not support "
103                                 "chained mbufs. Max SGL segments set to 1\n");
104                 test_data->max_sgl_segs = 1;
105         }
106
107         /* Level 0 support */
108         if (test_data->level.min == 0 &&
109                         (comp_flags & RTE_COMP_FF_NONCOMPRESSED_BLOCKS) == 0) {
110                 RTE_LOG(ERR, USER1, "Compress device does not support "
111                                 "level 0 (no compression)\n");
112                 return -1;
113         }
114
115         return 0;
116 }
117
118 static uint32_t
119 find_buf_size(uint32_t input_size)
120 {
121         uint32_t i;
122
123         /* From performance point of view the buffer size should be a
124          * power of 2 but also should be enough to store incompressible data
125          */
126
127         /* We're looking for nearest power of 2 buffer size, which is greather
128          * than input_size
129          */
130         uint32_t size =
131                 !input_size ? MIN_COMPRESSED_BUF_SIZE : (input_size << 1);
132
133         for (i = UINT16_MAX + 1; !(i & size); i >>= 1)
134                 ;
135
136         return i > ((UINT16_MAX + 1) >> 1)
137                         ? (uint32_t)((float)input_size * EXPANSE_RATIO)
138                         : i;
139 }
140
141 static int
142 comp_perf_allocate_memory(struct comp_test_data *test_data)
143 {
144
145         test_data->out_seg_sz = find_buf_size(test_data->seg_sz);
146         /* Number of segments for input and output
147          * (compression and decompression)
148          */
149         uint32_t total_segs = DIV_CEIL(test_data->input_data_sz,
150                         test_data->seg_sz);
151         test_data->comp_buf_pool = rte_pktmbuf_pool_create("comp_buf_pool",
152                                 total_segs,
153                                 0, 0,
154                                 test_data->out_seg_sz + RTE_PKTMBUF_HEADROOM,
155                                 rte_socket_id());
156         if (test_data->comp_buf_pool == NULL) {
157                 RTE_LOG(ERR, USER1, "Mbuf mempool could not be created\n");
158                 return -1;
159         }
160
161         cleanup = ST_MEMORY_ALLOC;
162         test_data->decomp_buf_pool = rte_pktmbuf_pool_create("decomp_buf_pool",
163                                 total_segs,
164                                 0, 0, test_data->seg_sz + RTE_PKTMBUF_HEADROOM,
165                                 rte_socket_id());
166         if (test_data->decomp_buf_pool == NULL) {
167                 RTE_LOG(ERR, USER1, "Mbuf mempool could not be created\n");
168                 return -1;
169         }
170
171         test_data->total_bufs = DIV_CEIL(total_segs, test_data->max_sgl_segs);
172
173         test_data->op_pool = rte_comp_op_pool_create("op_pool",
174                                   test_data->total_bufs,
175                                   0, 0, rte_socket_id());
176         if (test_data->op_pool == NULL) {
177                 RTE_LOG(ERR, USER1, "Comp op mempool could not be created\n");
178                 return -1;
179         }
180
181         /*
182          * Compressed data might be a bit larger than input data,
183          * if data cannot be compressed
184          */
185         test_data->compressed_data = rte_zmalloc_socket(NULL,
186                                 test_data->input_data_sz * EXPANSE_RATIO
187                                                 + MIN_COMPRESSED_BUF_SIZE, 0,
188                                 rte_socket_id());
189         if (test_data->compressed_data == NULL) {
190                 RTE_LOG(ERR, USER1, "Memory to hold the data from the input "
191                                 "file could not be allocated\n");
192                 return -1;
193         }
194
195         test_data->decompressed_data = rte_zmalloc_socket(NULL,
196                                 test_data->input_data_sz, 0,
197                                 rte_socket_id());
198         if (test_data->decompressed_data == NULL) {
199                 RTE_LOG(ERR, USER1, "Memory to hold the data from the input "
200                                 "file could not be allocated\n");
201                 return -1;
202         }
203
204         test_data->comp_bufs = rte_zmalloc_socket(NULL,
205                         test_data->total_bufs * sizeof(struct rte_mbuf *),
206                         0, rte_socket_id());
207         if (test_data->comp_bufs == NULL) {
208                 RTE_LOG(ERR, USER1, "Memory to hold the compression mbufs"
209                                 " could not be allocated\n");
210                 return -1;
211         }
212
213         test_data->decomp_bufs = rte_zmalloc_socket(NULL,
214                         test_data->total_bufs * sizeof(struct rte_mbuf *),
215                         0, rte_socket_id());
216         if (test_data->decomp_bufs == NULL) {
217                 RTE_LOG(ERR, USER1, "Memory to hold the decompression mbufs"
218                                 " could not be allocated\n");
219                 return -1;
220         }
221         return 0;
222 }
223
224 static int
225 comp_perf_dump_input_data(struct comp_test_data *test_data)
226 {
227         FILE *f = fopen(test_data->input_file, "r");
228         int ret = -1;
229
230         if (f == NULL) {
231                 RTE_LOG(ERR, USER1, "Input file could not be opened\n");
232                 return -1;
233         }
234
235         if (fseek(f, 0, SEEK_END) != 0) {
236                 RTE_LOG(ERR, USER1, "Size of input could not be calculated\n");
237                 goto end;
238         }
239         size_t actual_file_sz = ftell(f);
240         /* If extended input data size has not been set,
241          * input data size = file size
242          */
243
244         if (test_data->input_data_sz == 0)
245                 test_data->input_data_sz = actual_file_sz;
246
247         if (test_data->input_data_sz <= 0 || actual_file_sz <= 0 ||
248                         fseek(f, 0, SEEK_SET) != 0) {
249                 RTE_LOG(ERR, USER1, "Size of input could not be calculated\n");
250                 goto end;
251         }
252
253         test_data->input_data = rte_zmalloc_socket(NULL,
254                                 test_data->input_data_sz, 0, rte_socket_id());
255
256         if (test_data->input_data == NULL) {
257                 RTE_LOG(ERR, USER1, "Memory to hold the data from the input "
258                                 "file could not be allocated\n");
259                 goto end;
260         }
261
262         size_t remaining_data = test_data->input_data_sz;
263         uint8_t *data = test_data->input_data;
264
265         while (remaining_data > 0) {
266                 size_t data_to_read = RTE_MIN(remaining_data, actual_file_sz);
267
268                 if (fread(data, data_to_read, 1, f) != 1) {
269                         RTE_LOG(ERR, USER1, "Input file could not be read\n");
270                         goto end;
271                 }
272                 if (fseek(f, 0, SEEK_SET) != 0) {
273                         RTE_LOG(ERR, USER1,
274                                 "Size of input could not be calculated\n");
275                         goto end;
276                 }
277                 remaining_data -= data_to_read;
278                 data += data_to_read;
279         }
280
281         if (test_data->input_data_sz > actual_file_sz)
282                 RTE_LOG(INFO, USER1,
283                   "%zu bytes read from file %s, extending the file %.2f times\n",
284                         test_data->input_data_sz, test_data->input_file,
285                         (double)test_data->input_data_sz/actual_file_sz);
286         else
287                 RTE_LOG(INFO, USER1,
288                         "%zu bytes read from file %s\n",
289                         test_data->input_data_sz, test_data->input_file);
290
291         ret = 0;
292
293 end:
294         fclose(f);
295         return ret;
296 }
297
298 static int
299 comp_perf_initialize_compressdev(struct comp_test_data *test_data)
300 {
301         uint8_t enabled_cdev_count;
302         uint8_t enabled_cdevs[RTE_COMPRESS_MAX_DEVS];
303
304         enabled_cdev_count = rte_compressdev_devices_get(test_data->driver_name,
305                         enabled_cdevs, RTE_COMPRESS_MAX_DEVS);
306         if (enabled_cdev_count == 0) {
307                 RTE_LOG(ERR, USER1, "No compress devices type %s available\n",
308                                 test_data->driver_name);
309                 return -EINVAL;
310         }
311
312         if (enabled_cdev_count > 1)
313                 RTE_LOG(INFO, USER1,
314                         "Only the first compress device will be used\n");
315
316         test_data->cdev_id = enabled_cdevs[0];
317
318         if (comp_perf_check_capabilities(test_data) < 0)
319                 return -1;
320
321         /* Configure compressdev (one device, one queue pair) */
322         struct rte_compressdev_config config = {
323                 .socket_id = rte_socket_id(),
324                 .nb_queue_pairs = 1,
325                 .max_nb_priv_xforms = NUM_MAX_XFORMS,
326                 .max_nb_streams = 0
327         };
328
329         if (rte_compressdev_configure(test_data->cdev_id, &config) < 0) {
330                 RTE_LOG(ERR, USER1, "Device configuration failed\n");
331                 return -1;
332         }
333
334         if (rte_compressdev_queue_pair_setup(test_data->cdev_id, 0,
335                         NUM_MAX_INFLIGHT_OPS, rte_socket_id()) < 0) {
336                 RTE_LOG(ERR, USER1, "Queue pair setup failed\n");
337                 return -1;
338         }
339
340         if (rte_compressdev_start(test_data->cdev_id) < 0) {
341                 RTE_LOG(ERR, USER1, "Device could not be started\n");
342                 return -1;
343         }
344
345         return 0;
346 }
347
348 static int
349 prepare_bufs(struct comp_test_data *test_data)
350 {
351         uint32_t remaining_data = test_data->input_data_sz;
352         uint8_t *input_data_ptr = test_data->input_data;
353         size_t data_sz;
354         uint8_t *data_addr;
355         uint32_t i, j;
356
357         for (i = 0; i < test_data->total_bufs; i++) {
358                 /* Allocate data in input mbuf and copy data from input file */
359                 test_data->decomp_bufs[i] =
360                         rte_pktmbuf_alloc(test_data->decomp_buf_pool);
361                 if (test_data->decomp_bufs[i] == NULL) {
362                         RTE_LOG(ERR, USER1, "Could not allocate mbuf\n");
363                         return -1;
364                 }
365
366                 cleanup = ST_PREPARE_BUF;
367                 data_sz = RTE_MIN(remaining_data, test_data->seg_sz);
368                 data_addr = (uint8_t *) rte_pktmbuf_append(
369                                         test_data->decomp_bufs[i], data_sz);
370                 if (data_addr == NULL) {
371                         RTE_LOG(ERR, USER1, "Could not append data\n");
372                         return -1;
373                 }
374                 rte_memcpy(data_addr, input_data_ptr, data_sz);
375
376                 input_data_ptr += data_sz;
377                 remaining_data -= data_sz;
378
379                 /* Already one segment in the mbuf */
380                 uint16_t segs_per_mbuf = 1;
381
382                 /* Chain mbufs if needed for input mbufs */
383                 while (segs_per_mbuf < test_data->max_sgl_segs
384                                 && remaining_data > 0) {
385                         struct rte_mbuf *next_seg =
386                                 rte_pktmbuf_alloc(test_data->decomp_buf_pool);
387
388                         if (next_seg == NULL) {
389                                 RTE_LOG(ERR, USER1,
390                                         "Could not allocate mbuf\n");
391                                 return -1;
392                         }
393
394                         data_sz = RTE_MIN(remaining_data, test_data->seg_sz);
395                         data_addr = (uint8_t *)rte_pktmbuf_append(next_seg,
396                                 data_sz);
397
398                         if (data_addr == NULL) {
399                                 RTE_LOG(ERR, USER1, "Could not append data\n");
400                                 return -1;
401                         }
402
403                         rte_memcpy(data_addr, input_data_ptr, data_sz);
404                         input_data_ptr += data_sz;
405                         remaining_data -= data_sz;
406
407                         if (rte_pktmbuf_chain(test_data->decomp_bufs[i],
408                                         next_seg) < 0) {
409                                 RTE_LOG(ERR, USER1, "Could not chain mbufs\n");
410                                 return -1;
411                         }
412                         segs_per_mbuf++;
413                 }
414
415                 /* Allocate data in output mbuf */
416                 test_data->comp_bufs[i] =
417                         rte_pktmbuf_alloc(test_data->comp_buf_pool);
418                 if (test_data->comp_bufs[i] == NULL) {
419                         RTE_LOG(ERR, USER1, "Could not allocate mbuf\n");
420                         return -1;
421                 }
422                 data_addr = (uint8_t *) rte_pktmbuf_append(
423                                         test_data->comp_bufs[i],
424                                         test_data->out_seg_sz);
425                 if (data_addr == NULL) {
426                         RTE_LOG(ERR, USER1, "Could not append data\n");
427                         return -1;
428                 }
429
430                 /* Chain mbufs if needed for output mbufs */
431                 for (j = 1; j < segs_per_mbuf; j++) {
432                         struct rte_mbuf *next_seg =
433                                 rte_pktmbuf_alloc(test_data->comp_buf_pool);
434
435                         if (next_seg == NULL) {
436                                 RTE_LOG(ERR, USER1,
437                                         "Could not allocate mbuf\n");
438                                 return -1;
439                         }
440
441                         data_addr = (uint8_t *)rte_pktmbuf_append(next_seg,
442                                 test_data->out_seg_sz);
443
444                         if (data_addr == NULL) {
445                                 RTE_LOG(ERR, USER1, "Could not append data\n");
446                                 return -1;
447                         }
448
449                         if (rte_pktmbuf_chain(test_data->comp_bufs[i],
450                                         next_seg) < 0) {
451                                 RTE_LOG(ERR, USER1, "Could not chain mbufs\n");
452                                 return -1;
453                         }
454                 }
455         }
456
457         return 0;
458 }
459
460 static void
461 free_bufs(struct comp_test_data *test_data)
462 {
463         uint32_t i;
464
465         for (i = 0; i < test_data->total_bufs; i++) {
466                 rte_pktmbuf_free(test_data->comp_bufs[i]);
467                 rte_pktmbuf_free(test_data->decomp_bufs[i]);
468         }
469 }
470
471
472
473 int
474 main(int argc, char **argv)
475 {
476         uint8_t level, level_idx = 0;
477         int ret, i;
478         struct comp_test_data *test_data;
479
480         /* Initialise DPDK EAL */
481         ret = rte_eal_init(argc, argv);
482         if (ret < 0)
483                 rte_exit(EXIT_FAILURE, "Invalid EAL arguments!\n");
484         argc -= ret;
485         argv += ret;
486
487         test_data = rte_zmalloc_socket(NULL, sizeof(struct comp_test_data),
488                                         0, rte_socket_id());
489
490         if (test_data == NULL)
491                 rte_exit(EXIT_FAILURE, "Cannot reserve memory in socket %d\n",
492                                 rte_socket_id());
493
494         ret = EXIT_SUCCESS;
495         cleanup = ST_TEST_DATA;
496         comp_perf_options_default(test_data);
497
498         if (comp_perf_options_parse(test_data, argc, argv) < 0) {
499                 RTE_LOG(ERR, USER1,
500                         "Parsing one or more user options failed\n");
501                 ret = EXIT_FAILURE;
502                 goto end;
503         }
504
505         if (comp_perf_options_check(test_data) < 0) {
506                 ret = EXIT_FAILURE;
507                 goto end;
508         }
509
510         if (comp_perf_initialize_compressdev(test_data) < 0) {
511                 ret = EXIT_FAILURE;
512                 goto end;
513         }
514
515         cleanup = ST_COMPDEV;
516         if (comp_perf_dump_input_data(test_data) < 0) {
517                 ret = EXIT_FAILURE;
518                 goto end;
519         }
520
521         cleanup = ST_INPUT_DATA;
522         if (comp_perf_allocate_memory(test_data) < 0) {
523                 ret = EXIT_FAILURE;
524                 goto end;
525         }
526
527         if (prepare_bufs(test_data) < 0) {
528                 ret = EXIT_FAILURE;
529                 goto end;
530         }
531
532         if (test_data->level.inc != 0)
533                 level = test_data->level.min;
534         else
535                 level = test_data->level.list[0];
536
537         printf("App uses socket: %u\n", rte_socket_id());
538         printf("Driver uses socket: %u\n",
539                rte_compressdev_socket_id(test_data->cdev_id));
540         printf("Burst size = %u\n", test_data->burst_sz);
541         printf("File size = %zu\n", test_data->input_data_sz);
542
543         printf("%6s%12s%17s%19s%21s%15s%21s%23s%16s\n",
544                 "Level", "Comp size", "Comp ratio [%]",
545                 "Comp [Cycles/it]", "Comp [Cycles/Byte]", "Comp [Gbps]",
546                 "Decomp [Cycles/it]", "Decomp [Cycles/Byte]", "Decomp [Gbps]");
547
548         cleanup = ST_DURING_TEST;
549         while (level <= test_data->level.max) {
550
551                 /*
552                  * Run a first iteration, to verify compression and
553                  * get the compression ratio for the level
554                  */
555                 if (cperf_verification(test_data, level) != EXIT_SUCCESS)
556                         break;
557
558                 /*
559                  * Run benchmarking test
560                  */
561                 if (cperf_benchmark(test_data, level) != EXIT_SUCCESS)
562                         break;
563
564                 printf("%6u%12zu%17.2f%19"PRIu64"%21.2f"
565                                         "%15.2f%21"PRIu64"%23.2f%16.2f\n",
566                        level, test_data->comp_data_sz, test_data->ratio,
567                        test_data->comp_tsc_duration[level],
568                        test_data->comp_tsc_byte, test_data->comp_gbps,
569                        test_data->decomp_tsc_duration[level],
570                        test_data->decomp_tsc_byte, test_data->decomp_gbps);
571
572                 if (test_data->level.inc != 0)
573                         level += test_data->level.inc;
574                 else {
575                         if (++level_idx == test_data->level.count)
576                                 break;
577                         level = test_data->level.list[level_idx];
578                 }
579         }
580
581 end:
582         switch (cleanup) {
583
584         case ST_DURING_TEST:
585         case ST_PREPARE_BUF:
586                 free_bufs(test_data);
587                 /* fallthrough */
588         case ST_MEMORY_ALLOC:
589                 rte_free(test_data->decomp_bufs);
590                 rte_free(test_data->comp_bufs);
591                 rte_free(test_data->decompressed_data);
592                 rte_free(test_data->compressed_data);
593                 rte_mempool_free(test_data->op_pool);
594                 rte_mempool_free(test_data->decomp_buf_pool);
595                 rte_mempool_free(test_data->comp_buf_pool);
596                 /* fallthrough */
597         case ST_INPUT_DATA:
598                 rte_free(test_data->input_data);
599                 /* fallthrough */
600         case ST_COMPDEV:
601                 if (test_data->cdev_id != -1)
602                         rte_compressdev_stop(test_data->cdev_id);
603                 /* fallthrough */
604         case ST_TEST_DATA:
605                 rte_free(test_data);
606                 /* fallthrough */
607         case ST_CLEAR:
608         default:
609                 i = rte_eal_cleanup();
610                 if (i) {
611                         RTE_LOG(ERR, USER1,
612                                 "Error from rte_eal_cleanup(), %d\n", i);
613                         ret = i;
614                 }
615                 break;
616         }
617         return ret;
618 }