gpudev: add child device representing a device context
[dpdk.git] / app / test-gpudev / main.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2021 NVIDIA Corporation & Affiliates
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <stdint.h>
9 #include <inttypes.h>
10 #include <stdarg.h>
11 #include <errno.h>
12 #include <getopt.h>
13
14 #include <rte_common.h>
15 #include <rte_malloc.h>
16 #include <rte_memory.h>
17 #include <rte_eal.h>
18 #include <rte_ether.h>
19 #include <rte_ethdev.h>
20 #include <rte_mempool.h>
21 #include <rte_mbuf.h>
22
23 #include <rte_gpudev.h>
24
25 enum app_args {
26         ARG_HELP,
27         ARG_MEMPOOL
28 };
29
30 static void
31 usage(const char *prog_name)
32 {
33         printf("%s [EAL options] --\n",
34                 prog_name);
35 }
36
37 static void
38 args_parse(int argc, char **argv)
39 {
40         char **argvopt;
41         int opt;
42         int opt_idx;
43
44         static struct option lgopts[] = {
45                 { "help", 0, 0, ARG_HELP},
46                 /* End of options */
47                 { 0, 0, 0, 0 }
48         };
49
50         argvopt = argv;
51         while ((opt = getopt_long(argc, argvopt, "",
52                                 lgopts, &opt_idx)) != EOF) {
53                 switch (opt) {
54                 case ARG_HELP:
55                         usage(argv[0]);
56                         break;
57                 default:
58                         usage(argv[0]);
59                         rte_exit(EXIT_FAILURE, "Invalid option: %s\n", argv[optind]);
60                         break;
61                 }
62         }
63 }
64
65 int
66 main(int argc, char **argv)
67 {
68         int ret;
69         int nb_gpus = 0;
70         int16_t gpu_id = 0;
71         struct rte_gpu_info ginfo;
72
73         /* Init EAL. */
74         ret = rte_eal_init(argc, argv);
75         if (ret < 0)
76                 rte_exit(EXIT_FAILURE, "EAL init failed\n");
77         argc -= ret;
78         argv += ret;
79         if (argc > 1)
80                 args_parse(argc, argv);
81         argc -= ret;
82         argv += ret;
83
84         nb_gpus = rte_gpu_count_avail();
85         printf("\n\nDPDK found %d GPUs:\n", nb_gpus);
86         RTE_GPU_FOREACH(gpu_id)
87         {
88                 if (rte_gpu_info_get(gpu_id, &ginfo))
89                         rte_exit(EXIT_FAILURE, "rte_gpu_info_get error - bye\n");
90
91                 printf("\tGPU ID %d\n\t\tparent ID %d GPU Bus ID %s NUMA node %d Tot memory %.02f MB, Tot processors %d\n",
92                                 ginfo.dev_id,
93                                 ginfo.parent,
94                                 ginfo.name,
95                                 ginfo.numa_node,
96                                 (((float)ginfo.total_memory)/(float)1024)/(float)1024,
97                                 ginfo.processor_count
98                         );
99         }
100         printf("\n\n");
101
102         /* clean up the EAL */
103         rte_eal_cleanup();
104         printf("Bye...\n");
105
106         return EXIT_SUCCESS;
107 }