[v5,2/2] app/test-gpudev: test cpu_map/cpu_unmap functions

Message ID 20220127035029.12920-2-eagostini@nvidia.com (mailing list archive)
State Accepted, archived
Delegated to: Thomas Monjalon
Headers
Series [v5,1/2] gpudev: expose GPU memory to CPU |

Checks

Context Check Description
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/github-robot: build success github build: passed
ci/iol-aarch64-unit-testing success Testing PASS
ci/iol-broadcom-Functional success Functional Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-aarch64-compile-testing success Testing PASS
ci/iol-abi-testing warning Testing issues

Commit Message

Elena Agostini Jan. 27, 2022, 3:50 a.m. UTC
  From: Elena Agostini <eagostini@nvidia.com>

New test case added to test the gpudev cpu_map/cpu_unmap functions.

Signed-off-by: Elena Agostini <eagostini@nvidia.com>
---
 app/test-gpudev/main.c | 63 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)
  

Patch

diff --git a/app/test-gpudev/main.c b/app/test-gpudev/main.c
index 4500a8660b..417f2d78b7 100644
--- a/app/test-gpudev/main.c
+++ b/app/test-gpudev/main.c
@@ -185,6 +185,68 @@  register_cpu_memory(uint16_t gpu_id)
 	return -1;
 }
 
+static int
+gpu_mem_cpu_map(uint16_t gpu_id)
+{
+	void *ptr_gpu = NULL;
+	void *ptr_cpu = NULL;
+	size_t buf_bytes = 1024;
+	unsigned int align = 4096;
+	int ret;
+
+	printf("\n=======> TEST: Map GPU memory for CPU visibility\n\n");
+
+	/* Alloc memory on GPU 0 with 4kB alignment */
+	ptr_gpu = rte_gpu_mem_alloc(gpu_id, buf_bytes, align);
+	if (ptr_gpu == NULL) {
+		fprintf(stderr, "rte_gpu_mem_alloc GPU memory returned error\n");
+		goto error;
+	}
+	printf("GPU memory allocated at 0x%p size is %zd bytes\n",
+			ptr_gpu, buf_bytes);
+
+	ptr_cpu = rte_gpu_mem_cpu_map(gpu_id, buf_bytes, ptr_gpu);
+	if (ptr_cpu == NULL) {
+		fprintf(stderr, "rte_gpu_mem_cpu_map returned error\n");
+		goto error;
+	}
+	printf("GPU memory mapped for CPU access at 0x%p\n", ptr_cpu);
+
+	((uint8_t *)ptr_cpu)[0] = 0x4;
+	((uint8_t *)ptr_cpu)[1] = 0x5;
+	((uint8_t *)ptr_cpu)[2] = 0x6;
+
+	printf("GPU memory first 3 bytes set from CPU: %x %x %x\n",
+			((uint8_t *)ptr_cpu)[0],
+			((uint8_t *)ptr_cpu)[1],
+			((uint8_t *)ptr_cpu)[2]);
+
+	ret = rte_gpu_mem_cpu_unmap(gpu_id, ptr_cpu);
+	if (ret < 0) {
+		fprintf(stderr, "rte_gpu_mem_cpu_unmap returned error %d\n", ret);
+		goto error;
+	}
+	printf("GPU memory mapped for CPU access at 0x%p\n", ptr_cpu);
+
+	ret = rte_gpu_mem_free(gpu_id, ptr_gpu);
+	if (ret < 0) {
+		fprintf(stderr, "rte_gpu_mem_free returned error %d\n", ret);
+		goto error;
+	}
+	printf("GPU memory 0x%p freed\n", ptr_gpu);
+
+	printf("\n=======> TEST: PASSED\n");
+	return 0;
+
+error:
+
+	rte_gpu_mem_cpu_unmap(gpu_id, ptr_cpu);
+	rte_gpu_mem_free(gpu_id, ptr_gpu);
+
+	printf("\n=======> TEST: FAILED\n");
+	return -1;
+}
+
 static int
 create_update_comm_flag(uint16_t gpu_id)
 {
@@ -402,6 +464,7 @@  main(int argc, char **argv)
 	 */
 	alloc_gpu_memory(gpu_id);
 	register_cpu_memory(gpu_id);
+	gpu_mem_cpu_map(gpu_id);
 
 	/**
 	 * Communication items test