app/gpudev: improve output
[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 static int
66 alloc_gpu_memory(uint16_t gpu_id)
67 {
68         void *ptr_1 = NULL;
69         void *ptr_2 = NULL;
70         size_t buf_bytes = 1024;
71         int ret;
72
73         printf("\n=======> TEST: Allocate GPU memory\n\n");
74
75         /* Alloc memory on GPU 0 */
76         ptr_1 = rte_gpu_mem_alloc(gpu_id, buf_bytes);
77         if (ptr_1 == NULL) {
78                 fprintf(stderr, "rte_gpu_mem_alloc GPU memory returned error\n");
79                 goto error;
80         }
81         printf("GPU memory allocated at 0x%p size is %zd bytes\n",
82                         ptr_1, buf_bytes);
83
84         ptr_2 = rte_gpu_mem_alloc(gpu_id, buf_bytes);
85         if (ptr_2 == NULL) {
86                 fprintf(stderr, "rte_gpu_mem_alloc GPU memory returned error\n");
87                 goto error;
88         }
89         printf("GPU memory allocated at 0x%p size is %zd bytes\n",
90                         ptr_2, buf_bytes);
91
92         ret = rte_gpu_mem_free(gpu_id, (uint8_t *)(ptr_1)+0x700);
93         if (ret < 0) {
94                 printf("GPU memory 0x%p NOT freed: GPU driver didn't find this memory address internally.\n",
95                                 (uint8_t *)(ptr_1)+0x700);
96         } else {
97                 fprintf(stderr, "ERROR: rte_gpu_mem_free freed GPU memory 0x%p\n",
98                                 (uint8_t *)(ptr_1)+0x700);
99                 goto error;
100         }
101
102         ret = rte_gpu_mem_free(gpu_id, ptr_2);
103         if (ret < 0) {
104                 fprintf(stderr, "rte_gpu_mem_free returned error %d\n", ret);
105                 goto error;
106         }
107         printf("GPU memory 0x%p freed\n", ptr_2);
108
109         ret = rte_gpu_mem_free(gpu_id, ptr_1);
110         if (ret < 0) {
111                 fprintf(stderr, "rte_gpu_mem_free returned error %d\n", ret);
112                 goto error;
113         }
114         printf("GPU memory 0x%p freed\n", ptr_1);
115
116         printf("\n=======> TEST: PASSED\n");
117         return 0;
118
119 error:
120         printf("\n=======> TEST: FAILED\n");
121         return -1;
122 }
123
124 static int
125 register_cpu_memory(uint16_t gpu_id)
126 {
127         void *ptr = NULL;
128         size_t buf_bytes = 1024;
129         int ret;
130
131         printf("\n=======> TEST: Register CPU memory\n\n");
132
133         /* Alloc memory on CPU visible from GPU 0 */
134         ptr = rte_zmalloc(NULL, buf_bytes, 0);
135         if (ptr == NULL) {
136                 fprintf(stderr, "Failed to allocate CPU memory.\n");
137                 goto error;
138         }
139
140         ret = rte_gpu_mem_register(gpu_id, buf_bytes, ptr);
141         if (ret < 0) {
142                 fprintf(stderr, "rte_gpu_mem_register CPU memory returned error %d\n", ret);
143                 goto error;
144         }
145         printf("CPU memory registered at 0x%p %zdB\n", ptr, buf_bytes);
146
147         ret = rte_gpu_mem_unregister(gpu_id, (uint8_t *)(ptr)+0x700);
148         if (ret < 0) {
149                 printf("CPU memory 0x%p NOT unregistered: GPU driver didn't find this memory address internally\n",
150                                 (uint8_t *)(ptr)+0x700);
151         } else {
152                 fprintf(stderr, "ERROR: rte_gpu_mem_unregister unregistered GPU memory 0x%p\n",
153                                 (uint8_t *)(ptr)+0x700);
154                 goto error;
155         }
156
157         ret = rte_gpu_mem_unregister(gpu_id, ptr);
158         if (ret < 0) {
159                 fprintf(stderr, "rte_gpu_mem_unregister returned error %d\n", ret);
160                 goto error;
161         }
162         printf("CPU memory 0x%p unregistered\n", ptr);
163
164         printf("\n=======> TEST: PASSED\n");
165         return 0;
166
167 error:
168         printf("\n=======> TEST: FAILED\n");
169         return -1;
170 }
171
172 static int
173 create_update_comm_flag(uint16_t gpu_id)
174 {
175         struct rte_gpu_comm_flag devflag;
176         int ret = 0;
177         uint32_t set_val;
178         uint32_t get_val;
179
180         printf("\n=======> TEST: Communication flag\n\n");
181
182         ret = rte_gpu_comm_create_flag(gpu_id, &devflag, RTE_GPU_COMM_FLAG_CPU);
183         if (ret < 0) {
184                 fprintf(stderr, "rte_gpu_comm_create_flag returned error %d\n", ret);
185                 goto error;
186         }
187
188         set_val = 25;
189         ret = rte_gpu_comm_set_flag(&devflag, set_val);
190         if (ret < 0) {
191                 fprintf(stderr, "rte_gpu_comm_set_flag returned error %d\n", ret);
192                 goto error;
193         }
194
195         ret = rte_gpu_comm_get_flag_value(&devflag, &get_val);
196         if (ret < 0) {
197                 fprintf(stderr, "rte_gpu_comm_get_flag_value returned error %d\n", ret);
198                 goto error;
199         }
200
201         printf("Communication flag value at 0x%p was set to %d and current value is %d\n",
202                         devflag.ptr, set_val, get_val);
203
204         set_val = 38;
205         ret = rte_gpu_comm_set_flag(&devflag, set_val);
206         if (ret < 0) {
207                 fprintf(stderr, "rte_gpu_comm_set_flag returned error %d\n", ret);
208                 goto error;
209         }
210
211         ret = rte_gpu_comm_get_flag_value(&devflag, &get_val);
212         if (ret < 0) {
213                 fprintf(stderr, "rte_gpu_comm_get_flag_value returned error %d\n", ret);
214                 goto error;
215         }
216
217         printf("Communication flag value at 0x%p was set to %d and current value is %d\n",
218                         devflag.ptr, set_val, get_val);
219
220         ret = rte_gpu_comm_destroy_flag(&devflag);
221         if (ret < 0) {
222                 fprintf(stderr, "rte_gpu_comm_destroy_flags returned error %d\n", ret);
223                 goto error;
224         }
225
226         printf("\n=======> TEST: PASSED\n");
227         return 0;
228
229 error:
230         printf("\n=======> TEST: FAILED\n");
231         return -1;
232 }
233
234 static int
235 simulate_gpu_task(struct rte_gpu_comm_list *comm_list_item, int num_pkts)
236 {
237         int idx;
238
239         if (comm_list_item == NULL)
240                 return -1;
241
242         for (idx = 0; idx < num_pkts; idx++) {
243                 /**
244                  * consume(comm_list_item->pkt_list[idx].addr);
245                  */
246         }
247         comm_list_item->status = RTE_GPU_COMM_LIST_DONE;
248
249         return 0;
250 }
251
252 static int
253 create_update_comm_list(uint16_t gpu_id)
254 {
255         int ret = 0;
256         int i = 0;
257         struct rte_gpu_comm_list *comm_list;
258         uint32_t num_comm_items = 1024;
259         struct rte_mbuf *mbufs[10];
260
261         printf("\n=======> TEST: Communication list\n\n");
262
263         comm_list = rte_gpu_comm_create_list(gpu_id, num_comm_items);
264         if (comm_list == NULL) {
265                 fprintf(stderr, "rte_gpu_comm_create_list returned error %d\n", ret);
266                 goto error;
267         }
268
269         /**
270          * Simulate DPDK receive functions like rte_eth_rx_burst()
271          */
272         for (i = 0; i < 10; i++) {
273                 mbufs[i] = rte_zmalloc(NULL, sizeof(struct rte_mbuf), 0);
274                 if (mbufs[i] == NULL) {
275                         fprintf(stderr, "Failed to allocate fake mbufs in CPU memory.\n");
276                         goto error;
277                 }
278
279                 memset(mbufs[i], 0, sizeof(struct rte_mbuf));
280         }
281
282         /**
283          * Populate just the first item of  the list
284          */
285         ret = rte_gpu_comm_populate_list_pkts(&(comm_list[0]), mbufs, 10);
286         if (ret < 0) {
287                 fprintf(stderr, "rte_gpu_comm_populate_list_pkts returned error %d\n", ret);
288                 goto error;
289         }
290
291         ret = rte_gpu_comm_cleanup_list(&(comm_list[0]));
292         if (ret == 0) {
293                 fprintf(stderr, "rte_gpu_comm_cleanup_list erroneously cleaned the list even if packets have not been consumed yet\n");
294                 goto error;
295         }
296         printf("Communication list not cleaned because packets have not been consumed yet.\n");
297
298         /**
299          * Simulate a GPU tasks going through the packet list to consume
300          * mbufs packets and release them
301          */
302         printf("Consuming packets...\n");
303         simulate_gpu_task(&(comm_list[0]), 10);
304
305         /**
306          * Packets have been consumed, now the communication item
307          * and the related mbufs can be all released
308          */
309         ret = rte_gpu_comm_cleanup_list(&(comm_list[0]));
310         if (ret < 0) {
311                 fprintf(stderr, "rte_gpu_comm_cleanup_list returned error %d\n", ret);
312                 goto error;
313         }
314
315         printf("Communication list cleaned because packets have been consumed now.\n");
316
317         ret = rte_gpu_comm_destroy_list(comm_list, num_comm_items);
318         if (ret < 0) {
319                 fprintf(stderr, "rte_gpu_comm_destroy_list returned error %d\n", ret);
320                 goto error;
321         }
322
323         for (i = 0; i < 10; i++)
324                 rte_free(mbufs[i]);
325
326         printf("\n=======> TEST: PASSED\n");
327         return 0;
328
329 error:
330         printf("\n=======> TEST: FAILED\n");
331         return -1;
332 }
333
334 int
335 main(int argc, char **argv)
336 {
337         int ret;
338         int nb_gpus = 0;
339         int16_t gpu_id = 0;
340         struct rte_gpu_info ginfo;
341
342         /* Init EAL. */
343         ret = rte_eal_init(argc, argv);
344         if (ret < 0)
345                 rte_exit(EXIT_FAILURE, "EAL init failed\n");
346         argc -= ret;
347         argv += ret;
348         if (argc > 1)
349                 args_parse(argc, argv);
350         argc -= ret;
351         argv += ret;
352
353         nb_gpus = rte_gpu_count_avail();
354         printf("\n\nDPDK found %d GPUs:\n", nb_gpus);
355         RTE_GPU_FOREACH(gpu_id)
356         {
357                 if (rte_gpu_info_get(gpu_id, &ginfo))
358                         rte_exit(EXIT_FAILURE, "rte_gpu_info_get error - bye\n");
359
360                 printf("\tGPU ID %d\n\t\tparent ID %d GPU Bus ID %s NUMA node %d Tot memory %.02f MB, Tot processors %d\n",
361                                 ginfo.dev_id,
362                                 ginfo.parent,
363                                 ginfo.name,
364                                 ginfo.numa_node,
365                                 (((float)ginfo.total_memory)/(float)1024)/(float)1024,
366                                 ginfo.processor_count
367                         );
368         }
369         printf("\n\n");
370
371         if (nb_gpus == 0) {
372                 fprintf(stderr, "Need at least one GPU on the system to run the example\n");
373                 return EXIT_FAILURE;
374         }
375
376         gpu_id = 0;
377
378         /**
379          * Memory tests
380          */
381         alloc_gpu_memory(gpu_id);
382         register_cpu_memory(gpu_id);
383
384         /**
385          * Communication items test
386          */
387         create_update_comm_flag(gpu_id);
388         create_update_comm_list(gpu_id);
389
390         /* clean up the EAL */
391         rte_eal_cleanup();
392
393         return EXIT_SUCCESS;
394 }