net/virtio: unmap PCI device in secondary process
[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 <signal.h>
6 #include <sys/types.h>
7 #include <unistd.h>
8
9 #include <rte_malloc.h>
10 #include <rte_eal.h>
11 #include <rte_log.h>
12 #include <rte_compressdev.h>
13
14 #include "comp_perf.h"
15 #include "comp_perf_options.h"
16 #include "comp_perf_test_common.h"
17 #include "comp_perf_test_cyclecount.h"
18 #include "comp_perf_test_throughput.h"
19 #include "comp_perf_test_verify.h"
20
21 #define NUM_MAX_XFORMS 16
22 #define NUM_MAX_INFLIGHT_OPS 512
23
24 __extension__
25 const char *comp_perf_test_type_strs[] = {
26         [CPERF_TEST_TYPE_THROUGHPUT] = "throughput",
27         [CPERF_TEST_TYPE_VERIFY] = "verify",
28         [CPERF_TEST_TYPE_PMDCC] = "pmd-cyclecount"
29 };
30
31 __extension__
32 static const struct cperf_test cperf_testmap[] = {
33         [CPERF_TEST_TYPE_THROUGHPUT] = {
34                         cperf_throughput_test_constructor,
35                         cperf_throughput_test_runner,
36                         cperf_throughput_test_destructor
37
38         },
39         [CPERF_TEST_TYPE_VERIFY] = {
40                         cperf_verify_test_constructor,
41                         cperf_verify_test_runner,
42                         cperf_verify_test_destructor
43         },
44
45         [CPERF_TEST_TYPE_PMDCC] = {
46                         cperf_cyclecount_test_constructor,
47                         cperf_cyclecount_test_runner,
48                         cperf_cyclecount_test_destructor
49         }
50 };
51
52 static struct comp_test_data *test_data;
53
54 static int
55 comp_perf_check_capabilities(struct comp_test_data *test_data, uint8_t cdev_id)
56 {
57         const struct rte_compressdev_capabilities *cap;
58
59         cap = rte_compressdev_capability_get(cdev_id,
60                                              RTE_COMP_ALGO_DEFLATE);
61
62         if (cap == NULL) {
63                 RTE_LOG(ERR, USER1,
64                         "Compress device does not support DEFLATE\n");
65                 return -1;
66         }
67
68         uint64_t comp_flags = cap->comp_feature_flags;
69
70         /* Huffman encoding */
71         if (test_data->huffman_enc == RTE_COMP_HUFFMAN_FIXED &&
72                         (comp_flags & RTE_COMP_FF_HUFFMAN_FIXED) == 0) {
73                 RTE_LOG(ERR, USER1,
74                         "Compress device does not supported Fixed Huffman\n");
75                 return -1;
76         }
77
78         if (test_data->huffman_enc == RTE_COMP_HUFFMAN_DYNAMIC &&
79                         (comp_flags & RTE_COMP_FF_HUFFMAN_DYNAMIC) == 0) {
80                 RTE_LOG(ERR, USER1,
81                         "Compress device does not supported Dynamic Huffman\n");
82                 return -1;
83         }
84
85         /* Window size */
86         if (test_data->window_sz != -1) {
87                 if (param_range_check(test_data->window_sz, &cap->window_size)
88                                 < 0) {
89                         RTE_LOG(ERR, USER1,
90                                 "Compress device does not support "
91                                 "this window size\n");
92                         return -1;
93                 }
94         } else
95                 /* Set window size to PMD maximum if none was specified */
96                 test_data->window_sz = cap->window_size.max;
97
98         /* Check if chained mbufs is supported */
99         if (test_data->max_sgl_segs > 1  &&
100                         (comp_flags & RTE_COMP_FF_OOP_SGL_IN_SGL_OUT) == 0) {
101                 RTE_LOG(INFO, USER1, "Compress device does not support "
102                                 "chained mbufs. Max SGL segments set to 1\n");
103                 test_data->max_sgl_segs = 1;
104         }
105
106         /* Level 0 support */
107         if (test_data->level_lst.min == 0 &&
108                         (comp_flags & RTE_COMP_FF_NONCOMPRESSED_BLOCKS) == 0) {
109                 RTE_LOG(ERR, USER1, "Compress device does not support "
110                                 "level 0 (no compression)\n");
111                 return -1;
112         }
113
114         return 0;
115 }
116
117 static int
118 comp_perf_initialize_compressdev(struct comp_test_data *test_data,
119                                  uint8_t *enabled_cdevs)
120 {
121         uint8_t enabled_cdev_count, nb_lcores, cdev_id;
122         unsigned int i, j;
123         int ret;
124
125         enabled_cdev_count = rte_compressdev_devices_get(test_data->driver_name,
126                         enabled_cdevs, RTE_COMPRESS_MAX_DEVS);
127         if (enabled_cdev_count == 0) {
128                 RTE_LOG(ERR, USER1, "No compress devices type %s available,"
129                                     " please check the list of specified devices in EAL section\n",
130                                 test_data->driver_name);
131                 return -EINVAL;
132         }
133
134         nb_lcores = rte_lcore_count() - 1;
135         /*
136          * Use fewer devices,
137          * if there are more available than cores.
138          */
139         if (enabled_cdev_count > nb_lcores) {
140                 if (nb_lcores == 0) {
141                         RTE_LOG(ERR, USER1, "Cannot run with 0 cores! Increase the number of cores\n");
142                         return -EINVAL;
143                 }
144                 enabled_cdev_count = nb_lcores;
145                 RTE_LOG(INFO, USER1,
146                         "There's more available devices than cores!"
147                         " The number of devices has been aligned to %d cores\n",
148                         nb_lcores);
149         }
150
151         /*
152          * Calculate number of needed queue pairs, based on the amount
153          * of available number of logical cores and compression devices.
154          * For instance, if there are 4 cores and 2 compression devices,
155          * 2 queue pairs will be set up per device.
156          * One queue pair per one core.
157          * if e.g.: there're 3 cores and 2 compression devices,
158          * 2 queue pairs will be set up per device but one queue pair
159          * will left unused in the last one device
160          */
161         test_data->nb_qps = (nb_lcores % enabled_cdev_count) ?
162                                 (nb_lcores / enabled_cdev_count) + 1 :
163                                 nb_lcores / enabled_cdev_count;
164
165         for (i = 0; i < enabled_cdev_count &&
166                         i < RTE_COMPRESS_MAX_DEVS; i++,
167                                         nb_lcores -= test_data->nb_qps) {
168                 cdev_id = enabled_cdevs[i];
169
170                 struct rte_compressdev_info cdev_info;
171                 int socket_id = rte_compressdev_socket_id(cdev_id);
172
173                 rte_compressdev_info_get(cdev_id, &cdev_info);
174                 if (cdev_info.max_nb_queue_pairs &&
175                         test_data->nb_qps > cdev_info.max_nb_queue_pairs) {
176                         RTE_LOG(ERR, USER1,
177                                 "Number of needed queue pairs is higher "
178                                 "than the maximum number of queue pairs "
179                                 "per device.\n");
180                         RTE_LOG(ERR, USER1,
181                                 "Lower the number of cores or increase "
182                                 "the number of crypto devices\n");
183                         return -EINVAL;
184                 }
185
186                 if (comp_perf_check_capabilities(test_data, cdev_id) < 0)
187                         return -EINVAL;
188
189                 /* Configure compressdev */
190                 struct rte_compressdev_config config = {
191                         .socket_id = socket_id,
192                         .nb_queue_pairs = nb_lcores > test_data->nb_qps
193                                         ? test_data->nb_qps : nb_lcores,
194                         .max_nb_priv_xforms = NUM_MAX_XFORMS,
195                         .max_nb_streams = 0
196                 };
197                 test_data->nb_qps = config.nb_queue_pairs;
198
199                 if (rte_compressdev_configure(cdev_id, &config) < 0) {
200                         RTE_LOG(ERR, USER1, "Device configuration failed\n");
201                         return -EINVAL;
202                 }
203
204                 for (j = 0; j < test_data->nb_qps; j++) {
205                         ret = rte_compressdev_queue_pair_setup(cdev_id, j,
206                                         NUM_MAX_INFLIGHT_OPS, socket_id);
207                         if (ret < 0) {
208                                 RTE_LOG(ERR, USER1,
209                               "Failed to setup queue pair %u on compressdev %u",
210                                         j, cdev_id);
211                                 return -EINVAL;
212                         }
213                 }
214
215                 ret = rte_compressdev_start(cdev_id);
216                 if (ret < 0) {
217                         RTE_LOG(ERR, USER1,
218                                 "Failed to start device %u: error %d\n",
219                                 cdev_id, ret);
220                         return -EPERM;
221                 }
222         }
223
224         return enabled_cdev_count;
225 }
226
227 static int
228 comp_perf_dump_input_data(struct comp_test_data *test_data)
229 {
230         FILE *f = fopen(test_data->input_file, "r");
231         int ret = -1;
232
233         if (f == NULL) {
234                 RTE_LOG(ERR, USER1, "Input file could not be opened\n");
235                 return -1;
236         }
237
238         if (fseek(f, 0, SEEK_END) != 0) {
239                 RTE_LOG(ERR, USER1, "Size of input could not be calculated\n");
240                 goto end;
241         }
242         size_t actual_file_sz = ftell(f);
243         /* If extended input data size has not been set,
244          * input data size = file size
245          */
246
247         if (test_data->input_data_sz == 0)
248                 test_data->input_data_sz = actual_file_sz;
249
250         if (test_data->input_data_sz <= 0 || actual_file_sz <= 0 ||
251                         fseek(f, 0, SEEK_SET) != 0) {
252                 RTE_LOG(ERR, USER1, "Size of input could not be calculated\n");
253                 goto end;
254         }
255
256         test_data->input_data = rte_zmalloc_socket(NULL,
257                                 test_data->input_data_sz, 0, rte_socket_id());
258
259         if (test_data->input_data == NULL) {
260                 RTE_LOG(ERR, USER1, "Memory to hold the data from the input "
261                                 "file could not be allocated\n");
262                 goto end;
263         }
264
265         size_t remaining_data = test_data->input_data_sz;
266         uint8_t *data = test_data->input_data;
267
268         while (remaining_data > 0) {
269                 size_t data_to_read = RTE_MIN(remaining_data, actual_file_sz);
270
271                 if (fread(data, data_to_read, 1, f) != 1) {
272                         RTE_LOG(ERR, USER1, "Input file could not be read\n");
273                         goto end;
274                 }
275                 if (fseek(f, 0, SEEK_SET) != 0) {
276                         RTE_LOG(ERR, USER1,
277                                 "Size of input could not be calculated\n");
278                         goto end;
279                 }
280                 remaining_data -= data_to_read;
281                 data += data_to_read;
282         }
283
284         printf("\n");
285         if (test_data->input_data_sz > actual_file_sz)
286                 RTE_LOG(INFO, USER1,
287                   "%zu bytes read from file %s, extending the file %.2f times\n",
288                         test_data->input_data_sz, test_data->input_file,
289                         (double)test_data->input_data_sz/actual_file_sz);
290         else
291                 RTE_LOG(INFO, USER1,
292                         "%zu bytes read from file %s\n",
293                         test_data->input_data_sz, test_data->input_file);
294
295         ret = 0;
296
297 end:
298         fclose(f);
299         return ret;
300 }
301
302 static void
303 comp_perf_cleanup_on_signal(int signalNumber __rte_unused)
304 {
305         test_data->perf_comp_force_stop = 1;
306 }
307
308 static void
309 comp_perf_register_cleanup_on_signal(void)
310 {
311         signal(SIGTERM, comp_perf_cleanup_on_signal);
312         signal(SIGINT, comp_perf_cleanup_on_signal);
313 }
314
315 int
316 main(int argc, char **argv)
317 {
318         uint8_t level_idx = 0;
319         int ret, i;
320         void *ctx[RTE_MAX_LCORE] = {};
321         uint8_t enabled_cdevs[RTE_COMPRESS_MAX_DEVS];
322         int nb_compressdevs = 0;
323         uint16_t total_nb_qps = 0;
324         uint8_t cdev_id;
325         uint32_t lcore_id;
326
327         /* Initialise DPDK EAL */
328         ret = rte_eal_init(argc, argv);
329         if (ret < 0)
330                 rte_exit(EXIT_FAILURE, "Invalid EAL arguments!\n");
331         argc -= ret;
332         argv += ret;
333
334         test_data = rte_zmalloc_socket(NULL, sizeof(struct comp_test_data),
335                                         0, rte_socket_id());
336
337         if (test_data == NULL)
338                 rte_exit(EXIT_FAILURE, "Cannot reserve memory in socket %d\n",
339                                 rte_socket_id());
340
341         comp_perf_register_cleanup_on_signal();
342
343         ret = EXIT_SUCCESS;
344         test_data->cleanup = ST_TEST_DATA;
345         comp_perf_options_default(test_data);
346
347         if (comp_perf_options_parse(test_data, argc, argv) < 0) {
348                 RTE_LOG(ERR, USER1,
349                         "Parsing one or more user options failed\n");
350                 ret = EXIT_FAILURE;
351                 goto end;
352         }
353
354         if (comp_perf_options_check(test_data) < 0) {
355                 ret = EXIT_FAILURE;
356                 goto end;
357         }
358
359         nb_compressdevs =
360                 comp_perf_initialize_compressdev(test_data, enabled_cdevs);
361
362         if (nb_compressdevs < 1) {
363                 ret = EXIT_FAILURE;
364                 goto end;
365         }
366
367         test_data->cleanup = ST_COMPDEV;
368         if (comp_perf_dump_input_data(test_data) < 0) {
369                 ret = EXIT_FAILURE;
370                 goto end;
371         }
372
373         test_data->cleanup = ST_INPUT_DATA;
374
375         if (test_data->level_lst.inc != 0)
376                 test_data->level = test_data->level_lst.min;
377         else
378                 test_data->level = test_data->level_lst.list[0];
379
380         printf("\nApp uses socket: %u\n", rte_socket_id());
381         printf("Burst size = %u\n", test_data->burst_sz);
382         printf("Input data size = %zu\n", test_data->input_data_sz);
383         if (test_data->test == CPERF_TEST_TYPE_PMDCC)
384                 printf("Cycle-count delay = %u [us]\n",
385                        test_data->cyclecount_delay);
386
387         test_data->cleanup = ST_DURING_TEST;
388         total_nb_qps = nb_compressdevs * test_data->nb_qps;
389
390         i = 0;
391         uint8_t qp_id = 0, cdev_index = 0;
392
393         RTE_LCORE_FOREACH_WORKER(lcore_id) {
394
395                 if (i == total_nb_qps)
396                         break;
397
398                 cdev_id = enabled_cdevs[cdev_index];
399                 ctx[i] = cperf_testmap[test_data->test].constructor(
400                                                         cdev_id, qp_id,
401                                                         test_data);
402                 if (ctx[i] == NULL) {
403                         RTE_LOG(ERR, USER1, "Test run constructor failed\n");
404                         goto end;
405                 }
406                 qp_id = (qp_id + 1) % test_data->nb_qps;
407                 if (qp_id == 0)
408                         cdev_index++;
409                 i++;
410         }
411
412         print_test_dynamics(test_data);
413
414         while (test_data->level <= test_data->level_lst.max) {
415
416                 i = 0;
417                 RTE_LCORE_FOREACH_WORKER(lcore_id) {
418
419                         if (i == total_nb_qps)
420                                 break;
421
422                         rte_eal_remote_launch(
423                                         cperf_testmap[test_data->test].runner,
424                                         ctx[i], lcore_id);
425                         i++;
426                 }
427                 i = 0;
428                 RTE_LCORE_FOREACH_WORKER(lcore_id) {
429
430                         if (i == total_nb_qps)
431                                 break;
432                         ret |= rte_eal_wait_lcore(lcore_id);
433                         i++;
434                 }
435
436                 if (ret != EXIT_SUCCESS)
437                         break;
438
439                 if (test_data->level_lst.inc != 0)
440                         test_data->level += test_data->level_lst.inc;
441                 else {
442                         if (++level_idx == test_data->level_lst.count)
443                                 break;
444                         test_data->level = test_data->level_lst.list[level_idx];
445                 }
446         }
447
448 end:
449         switch (test_data->cleanup) {
450
451         case ST_DURING_TEST:
452                 i = 0;
453                 RTE_LCORE_FOREACH_WORKER(lcore_id) {
454                         if (i == total_nb_qps)
455                                 break;
456
457                         if (ctx[i] && cperf_testmap[test_data->test].destructor)
458                                 cperf_testmap[test_data->test].destructor(
459                                                                         ctx[i]);
460                         i++;
461                 }
462                 /* fallthrough */
463         case ST_INPUT_DATA:
464                 rte_free(test_data->input_data);
465                 /* fallthrough */
466         case ST_COMPDEV:
467                 for (i = 0; i < nb_compressdevs &&
468                      i < RTE_COMPRESS_MAX_DEVS; i++) {
469                         rte_compressdev_stop(enabled_cdevs[i]);
470                         rte_compressdev_close(enabled_cdevs[i]);
471                 }
472                 /* fallthrough */
473         case ST_TEST_DATA:
474                 rte_free(test_data);
475                 /* fallthrough */
476         case ST_CLEAR:
477         default:
478                 i = rte_eal_cleanup();
479                 if (i) {
480                         RTE_LOG(ERR, USER1,
481                                 "Error from rte_eal_cleanup(), %d\n", i);
482                         ret = i;
483                 }
484                 break;
485         }
486         return ret;
487 }
488
489 __rte_weak void *
490 cperf_cyclecount_test_constructor(uint8_t dev_id __rte_unused,
491                                  uint16_t qp_id __rte_unused,
492                                  struct comp_test_data *options __rte_unused)
493 {
494         RTE_LOG(INFO, USER1, "Cycle count test is not supported yet\n");
495         return NULL;
496 }
497
498 __rte_weak void
499 cperf_cyclecount_test_destructor(void *arg __rte_unused)
500 {
501         RTE_LOG(INFO, USER1, "Something wrong happened!!!\n");
502 }
503
504 __rte_weak int
505 cperf_cyclecount_test_runner(void *test_ctx __rte_unused)
506 {
507         return 0;
508 }
509
510 __rte_weak void *
511 cperf_throughput_test_constructor(uint8_t dev_id __rte_unused,
512                                  uint16_t qp_id __rte_unused,
513                                  struct comp_test_data *options __rte_unused)
514 {
515         RTE_LOG(INFO, USER1, "Benchmark test is not supported yet\n");
516         return NULL;
517 }
518
519 __rte_weak void
520 cperf_throughput_test_destructor(void *arg __rte_unused)
521 {
522
523 }
524
525 __rte_weak int
526 cperf_throughput_test_runner(void *test_ctx __rte_unused)
527 {
528         return 0;
529 }
530 __rte_weak void *
531 cperf_verify_test_constructor(uint8_t dev_id __rte_unused,
532                                  uint16_t qp_id __rte_unused,
533                                  struct comp_test_data *options __rte_unused)
534 {
535         RTE_LOG(INFO, USER1, "Verify test is not supported yet\n");
536         return NULL;
537 }
538
539 __rte_weak void
540 cperf_verify_test_destructor(void *arg __rte_unused)
541 {
542
543 }
544
545 __rte_weak int
546 cperf_verify_test_runner(void *test_ctx __rte_unused)
547 {
548         return 0;
549 }