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