1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2021 NVIDIA Corporation & Affiliates
14 #include <rte_common.h>
15 #include <rte_malloc.h>
16 #include <rte_memory.h>
18 #include <rte_ether.h>
19 #include <rte_ethdev.h>
20 #include <rte_mempool.h>
23 #include <rte_gpudev.h>
31 usage(const char *prog_name)
33 printf("%s [EAL options] --\n",
38 args_parse(int argc, char **argv)
44 static struct option lgopts[] = {
45 { "help", 0, 0, ARG_HELP},
51 while ((opt = getopt_long(argc, argvopt, "",
52 lgopts, &opt_idx)) != EOF) {
59 rte_exit(EXIT_FAILURE, "Invalid option: %s\n", argv[optind]);
66 alloc_gpu_memory(uint16_t gpu_id)
70 size_t buf_bytes = 1024;
73 printf("\n=======> TEST: Allocate GPU memory\n\n");
75 /* Alloc memory on GPU 0 */
76 ptr_1 = rte_gpu_mem_alloc(gpu_id, buf_bytes);
78 fprintf(stderr, "rte_gpu_mem_alloc GPU memory returned error\n");
81 printf("GPU memory allocated at 0x%p size is %zd bytes\n",
84 ptr_2 = rte_gpu_mem_alloc(gpu_id, buf_bytes);
86 fprintf(stderr, "rte_gpu_mem_alloc GPU memory returned error\n");
89 printf("GPU memory allocated at 0x%p size is %zd bytes\n",
92 ret = rte_gpu_mem_free(gpu_id, (uint8_t *)(ptr_1)+0x700);
94 printf("GPU memory 0x%p NOT freed: GPU driver didn't find this memory address internally.\n",
95 (uint8_t *)(ptr_1)+0x700);
97 fprintf(stderr, "ERROR: rte_gpu_mem_free freed GPU memory 0x%p\n",
98 (uint8_t *)(ptr_1)+0x700);
102 ret = rte_gpu_mem_free(gpu_id, ptr_2);
104 fprintf(stderr, "rte_gpu_mem_free returned error %d\n", ret);
107 printf("GPU memory 0x%p freed\n", ptr_2);
109 ret = rte_gpu_mem_free(gpu_id, ptr_1);
111 fprintf(stderr, "rte_gpu_mem_free returned error %d\n", ret);
114 printf("GPU memory 0x%p freed\n", ptr_1);
116 printf("\n=======> TEST: PASSED\n");
121 rte_gpu_mem_free(gpu_id, ptr_1);
122 rte_gpu_mem_free(gpu_id, ptr_2);
124 printf("\n=======> TEST: FAILED\n");
129 register_cpu_memory(uint16_t gpu_id)
132 size_t buf_bytes = 1024;
135 printf("\n=======> TEST: Register CPU memory\n\n");
137 /* Alloc memory on CPU visible from GPU 0 */
138 ptr = rte_zmalloc(NULL, buf_bytes, 0);
140 fprintf(stderr, "Failed to allocate CPU memory.\n");
144 ret = rte_gpu_mem_register(gpu_id, buf_bytes, ptr);
146 fprintf(stderr, "rte_gpu_mem_register CPU memory returned error %d\n", ret);
149 printf("CPU memory registered at 0x%p %zdB\n", ptr, buf_bytes);
151 ret = rte_gpu_mem_unregister(gpu_id, (uint8_t *)(ptr)+0x700);
153 printf("CPU memory 0x%p NOT unregistered: GPU driver didn't find this memory address internally\n",
154 (uint8_t *)(ptr)+0x700);
156 fprintf(stderr, "ERROR: rte_gpu_mem_unregister unregistered GPU memory 0x%p\n",
157 (uint8_t *)(ptr)+0x700);
161 ret = rte_gpu_mem_unregister(gpu_id, ptr);
163 fprintf(stderr, "rte_gpu_mem_unregister returned error %d\n", ret);
166 printf("CPU memory 0x%p unregistered\n", ptr);
170 printf("\n=======> TEST: PASSED\n");
175 rte_gpu_mem_unregister(gpu_id, ptr);
177 printf("\n=======> TEST: FAILED\n");
182 create_update_comm_flag(uint16_t gpu_id)
184 struct rte_gpu_comm_flag devflag;
189 printf("\n=======> TEST: Communication flag\n\n");
191 ret = rte_gpu_comm_create_flag(gpu_id, &devflag, RTE_GPU_COMM_FLAG_CPU);
193 fprintf(stderr, "rte_gpu_comm_create_flag returned error %d\n", ret);
198 ret = rte_gpu_comm_set_flag(&devflag, set_val);
200 fprintf(stderr, "rte_gpu_comm_set_flag returned error %d\n", ret);
204 ret = rte_gpu_comm_get_flag_value(&devflag, &get_val);
206 fprintf(stderr, "rte_gpu_comm_get_flag_value returned error %d\n", ret);
210 printf("Communication flag value at 0x%p was set to %d and current value is %d\n",
211 devflag.ptr, set_val, get_val);
214 ret = rte_gpu_comm_set_flag(&devflag, set_val);
216 fprintf(stderr, "rte_gpu_comm_set_flag returned error %d\n", ret);
220 ret = rte_gpu_comm_get_flag_value(&devflag, &get_val);
222 fprintf(stderr, "rte_gpu_comm_get_flag_value returned error %d\n", ret);
226 printf("Communication flag value at 0x%p was set to %d and current value is %d\n",
227 devflag.ptr, set_val, get_val);
229 ret = rte_gpu_comm_destroy_flag(&devflag);
231 fprintf(stderr, "rte_gpu_comm_destroy_flags returned error %d\n", ret);
235 printf("\n=======> TEST: PASSED\n");
240 rte_gpu_comm_destroy_flag(&devflag);
241 printf("\n=======> TEST: FAILED\n");
246 simulate_gpu_task(struct rte_gpu_comm_list *comm_list_item, int num_pkts)
250 if (comm_list_item == NULL)
253 for (idx = 0; idx < num_pkts; idx++) {
255 * consume(comm_list_item->pkt_list[idx].addr);
258 comm_list_item->status = RTE_GPU_COMM_LIST_DONE;
264 create_update_comm_list(uint16_t gpu_id)
268 struct rte_gpu_comm_list *comm_list = NULL;
269 uint32_t num_comm_items = 1024;
270 struct rte_mbuf *mbufs[10];
272 printf("\n=======> TEST: Communication list\n\n");
274 comm_list = rte_gpu_comm_create_list(gpu_id, num_comm_items);
275 if (comm_list == NULL) {
276 fprintf(stderr, "rte_gpu_comm_create_list returned error %d\n", ret);
281 * Simulate DPDK receive functions like rte_eth_rx_burst()
283 for (i = 0; i < 10; i++) {
284 mbufs[i] = rte_zmalloc(NULL, sizeof(struct rte_mbuf), 0);
285 if (mbufs[i] == NULL) {
286 fprintf(stderr, "Failed to allocate fake mbufs in CPU memory.\n");
290 memset(mbufs[i], 0, sizeof(struct rte_mbuf));
294 * Populate just the first item of the list
296 ret = rte_gpu_comm_populate_list_pkts(&(comm_list[0]), mbufs, 10);
298 fprintf(stderr, "rte_gpu_comm_populate_list_pkts returned error %d\n", ret);
302 ret = rte_gpu_comm_cleanup_list(&(comm_list[0]));
304 fprintf(stderr, "rte_gpu_comm_cleanup_list erroneously cleaned the list even if packets have not been consumed yet\n");
307 printf("Communication list not cleaned because packets have not been consumed yet.\n");
310 * Simulate a GPU tasks going through the packet list to consume
311 * mbufs packets and release them
313 printf("Consuming packets...\n");
314 simulate_gpu_task(&(comm_list[0]), 10);
317 * Packets have been consumed, now the communication item
318 * and the related mbufs can be all released
320 ret = rte_gpu_comm_cleanup_list(&(comm_list[0]));
322 fprintf(stderr, "rte_gpu_comm_cleanup_list returned error %d\n", ret);
326 printf("Communication list cleaned because packets have been consumed now.\n");
328 ret = rte_gpu_comm_destroy_list(comm_list, num_comm_items);
330 fprintf(stderr, "rte_gpu_comm_destroy_list returned error %d\n", ret);
334 for (i = 0; i < 10; i++)
337 printf("\n=======> TEST: PASSED\n");
342 rte_gpu_comm_destroy_list(comm_list, num_comm_items);
343 for (i = 0; i < 10; i++)
345 printf("\n=======> TEST: FAILED\n");
350 main(int argc, char **argv)
355 struct rte_gpu_info ginfo;
358 ret = rte_eal_init(argc, argv);
360 rte_exit(EXIT_FAILURE, "EAL init failed\n");
364 args_parse(argc, argv);
368 nb_gpus = rte_gpu_count_avail();
369 printf("\n\nDPDK found %d GPUs:\n", nb_gpus);
370 RTE_GPU_FOREACH(gpu_id)
372 if (rte_gpu_info_get(gpu_id, &ginfo))
373 rte_exit(EXIT_FAILURE, "rte_gpu_info_get error - bye\n");
375 printf("\tGPU ID %d\n\t\tparent ID %d GPU Bus ID %s NUMA node %d Tot memory %.02f MB, Tot processors %d\n",
380 (((float)ginfo.total_memory)/(float)1024)/(float)1024,
381 ginfo.processor_count
387 fprintf(stderr, "Need at least one GPU on the system to run the example\n");
396 alloc_gpu_memory(gpu_id);
397 register_cpu_memory(gpu_id);
400 * Communication items test
402 create_update_comm_flag(gpu_id);
403 create_update_comm_list(gpu_id);
405 /* clean up the EAL */