test: use memseg walk instead of iteration
authorAnatoly Burakov <anatoly.burakov@intel.com>
Wed, 11 Apr 2018 12:30:06 +0000 (13:30 +0100)
committerThomas Monjalon <thomas@monjalon.net>
Wed, 11 Apr 2018 17:53:02 +0000 (19:53 +0200)
Reduce dependency on internal details of EAL memory subsystem, and
simplify code.

Signed-off-by: Anatoly Burakov <anatoly.burakov@intel.com>
Tested-by: Santosh Shukla <santosh.shukla@caviumnetworks.com>
Tested-by: Hemant Agrawal <hemant.agrawal@nxp.com>
Tested-by: Gowrishankar Muthukrishnan <gowrishankar.m@linux.vnet.ibm.com>
test/test/test_malloc.c
test/test/test_memory.c
test/test/test_memzone.c

index ccc5fea..28c241f 100644 (file)
@@ -705,16 +705,34 @@ err_return:
        return -1;
 }
 
+static int
+check_socket_mem(const struct rte_memseg *ms, void *arg)
+{
+       int32_t *socket = arg;
+
+       return *socket == ms->socket_id;
+}
+
 /* Check if memory is available on a specific socket */
 static int
 is_mem_on_socket(int32_t socket)
 {
-       const struct rte_memseg *ms = rte_eal_get_physmem_layout();
-       unsigned i;
+       return rte_memseg_walk(check_socket_mem, &socket);
+}
 
-       for (i = 0; i < RTE_MAX_MEMSEG; i++) {
-               if (socket == ms[i].socket_id)
-                       return 1;
+struct walk_param {
+       void *addr;
+       int32_t socket;
+};
+static int
+find_socket(const struct rte_memseg *ms, void *arg)
+{
+       struct walk_param *param = arg;
+
+       if (param->addr >= ms->addr &&
+                       param->addr < RTE_PTR_ADD(ms->addr, ms->len)) {
+               param->socket = ms->socket_id;
+               return 1;
        }
        return 0;
 }
@@ -726,15 +744,9 @@ is_mem_on_socket(int32_t socket)
 static int32_t
 addr_to_socket(void * addr)
 {
-       const struct rte_memseg *ms = rte_eal_get_physmem_layout();
-       unsigned i;
-
-       for (i = 0; i < RTE_MAX_MEMSEG; i++) {
-               if ((ms[i].addr <= addr) &&
-                               ((uintptr_t)addr <
-                               ((uintptr_t)ms[i].addr + (uintptr_t)ms[i].len)))
-                       return ms[i].socket_id;
-       }
+       struct walk_param param = {.addr = addr, .socket = 0};
+       if (rte_memseg_walk(find_socket, &param) > 0)
+               return param.socket;
        return -1;
 }
 
index 972321f..c9b287c 100644 (file)
  * - Try to read all memory; it should not segfault.
  */
 
+static int
+check_mem(const struct rte_memseg *ms, void *arg __rte_unused)
+{
+       volatile uint8_t *mem = (volatile uint8_t *) ms->addr;
+       size_t i;
+
+       for (i = 0; i < ms->len; i++, mem++)
+               *mem;
+       return 0;
+}
+
 static int
 test_memory(void)
 {
        uint64_t s;
-       unsigned i;
-       size_t j;
-       const struct rte_memseg *mem;
 
        /*
         * dump the mapped memory: the python-expect script checks
@@ -45,14 +53,7 @@ test_memory(void)
        }
 
        /* try to read memory (should not segfault) */
-       mem = rte_eal_get_physmem_layout();
-       for (i = 0; i < RTE_MAX_MEMSEG && mem[i].addr != NULL ; i++) {
-
-               /* check memory */
-               for (j = 0; j<mem[i].len; j++) {
-                       *((volatile uint8_t *) mem[i].addr + j);
-               }
-       }
+       rte_memseg_walk(check_mem, NULL);
 
        return 0;
 }
index 8ece1ac..cbf0cfa 100644 (file)
@@ -104,28 +104,47 @@ test_memzone_reserving_zone_size_bigger_than_the_maximum(void)
        return 0;
 }
 
+struct walk_arg {
+       int hugepage_2MB_avail;
+       int hugepage_1GB_avail;
+       int hugepage_16MB_avail;
+       int hugepage_16GB_avail;
+};
+static int
+find_available_pagesz(const struct rte_memseg *ms, void *arg)
+{
+       struct walk_arg *wa = arg;
+
+       if (ms->hugepage_sz == RTE_PGSIZE_2M)
+               wa->hugepage_2MB_avail = 1;
+       if (ms->hugepage_sz == RTE_PGSIZE_1G)
+               wa->hugepage_1GB_avail = 1;
+       if (ms->hugepage_sz == RTE_PGSIZE_16M)
+               wa->hugepage_16MB_avail = 1;
+       if (ms->hugepage_sz == RTE_PGSIZE_16G)
+               wa->hugepage_16GB_avail = 1;
+
+       return 0;
+}
+
 static int
 test_memzone_reserve_flags(void)
 {
        const struct rte_memzone *mz;
-       const struct rte_memseg *ms;
-       int hugepage_2MB_avail = 0;
-       int hugepage_1GB_avail = 0;
-       int hugepage_16MB_avail = 0;
-       int hugepage_16GB_avail = 0;
+       struct walk_arg wa;
+       int hugepage_2MB_avail, hugepage_1GB_avail;
+       int hugepage_16MB_avail, hugepage_16GB_avail;
        const size_t size = 100;
-       int i = 0;
-       ms = rte_eal_get_physmem_layout();
-       for (i = 0; i < RTE_MAX_MEMSEG; i++) {
-               if (ms[i].hugepage_sz == RTE_PGSIZE_2M)
-                       hugepage_2MB_avail = 1;
-               if (ms[i].hugepage_sz == RTE_PGSIZE_1G)
-                       hugepage_1GB_avail = 1;
-               if (ms[i].hugepage_sz == RTE_PGSIZE_16M)
-                       hugepage_16MB_avail = 1;
-               if (ms[i].hugepage_sz == RTE_PGSIZE_16G)
-                       hugepage_16GB_avail = 1;
-       }
+
+       memset(&wa, 0, sizeof(wa));
+
+       rte_memseg_walk(find_available_pagesz, &wa);
+
+       hugepage_2MB_avail = wa.hugepage_2MB_avail;
+       hugepage_1GB_avail = wa.hugepage_1GB_avail;
+       hugepage_16MB_avail = wa.hugepage_16MB_avail;
+       hugepage_16GB_avail = wa.hugepage_16GB_avail;
+
        /* Display the availability of 2MB ,1GB, 16MB, 16GB pages */
        if (hugepage_2MB_avail)
                printf("2MB Huge pages available\n");