xref: /dpdk/app/test/test_memory.c (revision 1cde1b9a9b4dbf31cb5e5ccdfc5da3cb079f43a2)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 
5 #include <stdio.h>
6 #include <stdint.h>
7 
8 #include <rte_eal.h>
9 #include <rte_memory.h>
10 #include <rte_common.h>
11 #include <rte_memzone.h>
12 
13 #include "test.h"
14 
15 /*
16  * Memory
17  * ======
18  *
19  * - Dump the mapped memory. The python-expect script checks that at
20  *   least one line is dumped.
21  *
22  * - Check that memory size is different than 0.
23  *
24  * - Try to read all memory; it should not segfault.
25  */
26 
27 static int
28 check_mem(const struct rte_memseg_list *msl __rte_unused,
29 		const struct rte_memseg *ms, void *arg __rte_unused)
30 {
31 	volatile uint8_t *mem = (volatile uint8_t *) ms->addr;
32 	size_t i, max = ms->len;
33 
34 	for (i = 0; i < max; i++, mem++)
35 		*mem;
36 	return 0;
37 }
38 
39 static int
40 check_seg_fds(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
41 		void *arg __rte_unused)
42 {
43 	size_t offset;
44 	int ret;
45 
46 	/* skip external segments */
47 	if (msl->external)
48 		return 0;
49 
50 	/* try segment fd first. we're in a callback, so thread-unsafe */
51 	ret = rte_memseg_get_fd_thread_unsafe(ms);
52 	if (ret < 0) {
53 		/* ENOTSUP means segment is valid, but there is not support for
54 		 * segment fd API (e.g. on FreeBSD).
55 		 */
56 		if (errno == ENOTSUP)
57 			return 1;
58 		/* all other errors are treated as failures */
59 		return -1;
60 	}
61 
62 	/* we're able to get memseg fd - try getting its offset */
63 	ret = rte_memseg_get_fd_offset_thread_unsafe(ms, &offset);
64 	if (ret < 0) {
65 		if (errno == ENOTSUP)
66 			return 1;
67 		return -1;
68 	}
69 	return 0;
70 }
71 
72 static int
73 test_memory(void)
74 {
75 	uint64_t s;
76 	int ret;
77 
78 	/*
79 	 * dump the mapped memory: the python-expect script checks
80 	 * that at least one line is dumped
81 	 */
82 	printf("Dump memory layout\n");
83 	rte_dump_physmem_layout(stdout);
84 
85 	/* check that memory size is != 0 */
86 	s = rte_eal_get_physmem_size();
87 	if (s == 0) {
88 		printf("No memory detected\n");
89 		return -1;
90 	}
91 
92 	/* try to read memory (should not segfault) */
93 	rte_memseg_walk(check_mem, NULL);
94 
95 	/* check segment fd support */
96 	ret = rte_memseg_walk(check_seg_fds, NULL);
97 	if (ret == 1) {
98 		printf("Segment fd API is unsupported\n");
99 	} else if (ret == -1) {
100 		printf("Error getting segment fd's\n");
101 		return -1;
102 	}
103 
104 	return 0;
105 }
106 
107 REGISTER_TEST_COMMAND(memory_autotest, test_memory);
108