add prefix to cache line macros
[dpdk.git] / app / test / test_memzone.c
index cebe375..5da6903 100644 (file)
@@ -1,13 +1,13 @@
 /*-
  *   BSD LICENSE
- * 
+ *
  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
  *   All rights reserved.
- * 
+ *
  *   Redistribution and use in source and binary forms, with or without
  *   modification, are permitted provided that the following conditions
  *   are met:
- * 
+ *
  *     * Redistributions of source code must retain the above copyright
  *       notice, this list of conditions and the following disclaimer.
  *     * Redistributions in binary form must reproduce the above copyright
@@ -17,7 +17,7 @@
  *     * Neither the name of Intel Corporation nor the names of its
  *       contributors may be used to endorse or promote products derived
  *       from this software without specific prior written permission.
- * 
+ *
  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@@ -36,8 +36,6 @@
 #include <inttypes.h>
 #include <sys/queue.h>
 
-#include <cmdline_parse.h>
-
 #include <rte_random.h>
 #include <rte_cycles.h>
 #include <rte_memory.h>
@@ -135,6 +133,8 @@ test_memzone_reserve_flags(void)
        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;
        const size_t size = 100;
        int i = 0;
        ms = rte_eal_get_physmem_layout();
@@ -143,12 +143,20 @@ test_memzone_reserve_flags(void)
                        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;
        }
-       /* Display the availability of 2MB and 1GB pages */
+       /* Display the availability of 2MB ,1GB, 16MB, 16GB pages */
        if (hugepage_2MB_avail)
                printf("2MB Huge pages available\n");
        if (hugepage_1GB_avail)
                printf("1GB Huge pages available\n");
+       if (hugepage_16MB_avail)
+               printf("16MB Huge pages available\n");
+       if (hugepage_16GB_avail)
+               printf("16GB Huge pages available\n");
        /*
         * If 2MB pages available, check that a small memzone is correctly
         * reserved from 2MB huge pages when requested by the RTE_MEMZONE_2MB flag.
@@ -257,6 +265,117 @@ test_memzone_reserve_flags(void)
                        }
                }
        }
+       /*
+        * This option is for IBM Power. If 16MB pages available, check
+        * that a small memzone is correctly reserved from 16MB huge pages
+        * when requested by the RTE_MEMZONE_16MB flag. Also check that
+        * RTE_MEMZONE_SIZE_HINT_ONLY flag only defaults to an available
+        * page size (i.e 16GB ) when 16MB pages are unavailable.
+        */
+       if (hugepage_16MB_avail) {
+               mz = rte_memzone_reserve("flag_zone_16M", size, SOCKET_ID_ANY,
+                               RTE_MEMZONE_16MB);
+               if (mz == NULL) {
+                       printf("MEMZONE FLAG 16MB\n");
+                       return -1;
+               }
+               if (mz->hugepage_sz != RTE_PGSIZE_16M) {
+                       printf("hugepage_sz not equal 16M\n");
+                       return -1;
+               }
+
+               mz = rte_memzone_reserve("flag_zone_16M_HINT", size,
+               SOCKET_ID_ANY, RTE_MEMZONE_16MB|RTE_MEMZONE_SIZE_HINT_ONLY);
+               if (mz == NULL) {
+                       printf("MEMZONE FLAG 2MB\n");
+                       return -1;
+               }
+               if (mz->hugepage_sz != RTE_PGSIZE_16M) {
+                       printf("hugepage_sz not equal 16M\n");
+                       return -1;
+               }
+
+               /* Check if 1GB huge pages are unavailable, that function fails
+                * unless HINT flag is indicated
+                */
+               if (!hugepage_16GB_avail) {
+                       mz = rte_memzone_reserve("flag_zone_16G_HINT", size,
+                               SOCKET_ID_ANY,
+                               RTE_MEMZONE_16GB|RTE_MEMZONE_SIZE_HINT_ONLY);
+                       if (mz == NULL) {
+                               printf("MEMZONE FLAG 16GB & HINT\n");
+                               return -1;
+                       }
+                       if (mz->hugepage_sz != RTE_PGSIZE_16M) {
+                               printf("hugepage_sz not equal 16M\n");
+                               return -1;
+                       }
+
+                       mz = rte_memzone_reserve("flag_zone_16G", size,
+                               SOCKET_ID_ANY, RTE_MEMZONE_16GB);
+                       if (mz != NULL) {
+                               printf("MEMZONE FLAG 16GB\n");
+                               return -1;
+                       }
+               }
+       }
+       /*As with 16MB tests above for 16GB huge page requests*/
+       if (hugepage_16GB_avail) {
+               mz = rte_memzone_reserve("flag_zone_16G", size, SOCKET_ID_ANY,
+                               RTE_MEMZONE_16GB);
+               if (mz == NULL) {
+                       printf("MEMZONE FLAG 16GB\n");
+                       return -1;
+               }
+               if (mz->hugepage_sz != RTE_PGSIZE_16G) {
+                       printf("hugepage_sz not equal 16G\n");
+                       return -1;
+               }
+
+               mz = rte_memzone_reserve("flag_zone_16G_HINT", size,
+               SOCKET_ID_ANY, RTE_MEMZONE_16GB|RTE_MEMZONE_SIZE_HINT_ONLY);
+               if (mz == NULL) {
+                       printf("MEMZONE FLAG 16GB\n");
+                       return -1;
+               }
+               if (mz->hugepage_sz != RTE_PGSIZE_16G) {
+                       printf("hugepage_sz not equal 16G\n");
+                       return -1;
+               }
+
+               /* Check if 1GB huge pages are unavailable, that function fails
+                * unless HINT flag is indicated
+                */
+               if (!hugepage_16MB_avail) {
+                       mz = rte_memzone_reserve("flag_zone_16M_HINT", size,
+                               SOCKET_ID_ANY,
+                               RTE_MEMZONE_16MB|RTE_MEMZONE_SIZE_HINT_ONLY);
+                       if (mz == NULL) {
+                               printf("MEMZONE FLAG 16MB & HINT\n");
+                               return -1;
+                       }
+                       if (mz->hugepage_sz != RTE_PGSIZE_16G) {
+                               printf("hugepage_sz not equal 16G\n");
+                               return -1;
+                       }
+                       mz = rte_memzone_reserve("flag_zone_16M", size,
+                               SOCKET_ID_ANY, RTE_MEMZONE_16MB);
+                       if (mz != NULL) {
+                               printf("MEMZONE FLAG 16MB\n");
+                               return -1;
+                       }
+               }
+
+               if (hugepage_16MB_avail && hugepage_16GB_avail) {
+                       mz = rte_memzone_reserve("flag_zone_16M_HINT", size,
+                               SOCKET_ID_ANY,
+                               RTE_MEMZONE_16MB|RTE_MEMZONE_16GB);
+                       if (mz != NULL) {
+                               printf("BOTH SIZES SET\n");
+                               return -1;
+                       }
+               }
+       }
        return 0;
 }
 
@@ -283,9 +402,9 @@ test_memzone_reserve_max(void)
                        continue;
 
                /* align everything */
-               last_addr = RTE_PTR_ALIGN_CEIL(ms[memseg_idx].addr, CACHE_LINE_SIZE);
+               last_addr = RTE_PTR_ALIGN_CEIL(ms[memseg_idx].addr, RTE_CACHE_LINE_SIZE);
                len = ms[memseg_idx].len - RTE_PTR_DIFF(last_addr, ms[memseg_idx].addr);
-               len &= ~((size_t) CACHE_LINE_MASK);
+               len &= ~((size_t) RTE_CACHE_LINE_MASK);
 
                /* cycle through all memzones */
                for (memzone_idx = 0; memzone_idx < RTE_MAX_MEMZONE; memzone_idx++) {
@@ -296,9 +415,9 @@ test_memzone_reserve_max(void)
 
                        /* check if the memzone is in our memseg and subtract length */
                        if ((config->mem_config->memzone[memzone_idx].addr >=
-                                       ms[memseg_idx].addr) &&
-                                       (config->mem_config->memzone[memzone_idx].addr <
-                                       (RTE_PTR_ADD(ms[memseg_idx].addr, ms[memseg_idx].len)))) {
+                            ms[memseg_idx].addr) &&
+                           (config->mem_config->memzone[memzone_idx].addr <
+                            (RTE_PTR_ADD(ms[memseg_idx].addr, ms[memseg_idx].len)))) {
                                /* since the zones can now be aligned and occasionally skip
                                 * some space, we should calculate the length based on
                                 * reported length and start addresses difference. Addresses
@@ -306,11 +425,11 @@ test_memzone_reserve_max(void)
                                 * them being in the right order.
                                 */
                                len -= RTE_PTR_DIFF(
-                                               config->mem_config->memzone[memzone_idx].addr,
-                                               last_addr);
+                                                   config->mem_config->memzone[memzone_idx].addr,
+                                                   last_addr);
                                len -= config->mem_config->memzone[memzone_idx].len;
                                last_addr = RTE_PTR_ADD(config->mem_config->memzone[memzone_idx].addr,
-                                               (size_t) config->mem_config->memzone[memzone_idx].len);
+                                                       (size_t) config->mem_config->memzone[memzone_idx].len);
                        }
                }
 
@@ -328,17 +447,17 @@ test_memzone_reserve_max(void)
        mz = rte_memzone_reserve("max_zone", 0, SOCKET_ID_ANY, 0);
        if (mz == NULL){
                printf("Failed to reserve a big chunk of memory\n");
-               rte_dump_physmem_layout();
-               rte_memzone_dump();
+               rte_dump_physmem_layout(stdout);
+               rte_memzone_dump(stdout);
                return -1;
        }
 
        if (mz->len != maxlen) {
                printf("Memzone reserve with 0 size did not return bigest block\n");
                printf("Expected size = %zu, actual size = %zu\n",
-                               maxlen, mz->len);
-               rte_dump_physmem_layout();
-               rte_memzone_dump();
+                      maxlen, mz->len);
+               rte_dump_physmem_layout(stdout);
+               rte_memzone_dump(stdout);
 
                return -1;
        }
@@ -376,9 +495,9 @@ test_memzone_reserve_max_aligned(void)
                        continue;
 
                /* align everything */
-               last_addr = RTE_PTR_ALIGN_CEIL(ms[memseg_idx].addr, CACHE_LINE_SIZE);
+               last_addr = RTE_PTR_ALIGN_CEIL(ms[memseg_idx].addr, RTE_CACHE_LINE_SIZE);
                len = ms[memseg_idx].len - RTE_PTR_DIFF(last_addr, ms[memseg_idx].addr);
-               len &= ~((size_t) CACHE_LINE_MASK);
+               len &= ~((size_t) RTE_CACHE_LINE_MASK);
 
                /* cycle through all memzones */
                for (memzone_idx = 0; memzone_idx < RTE_MAX_MEMZONE; memzone_idx++) {
@@ -424,8 +543,8 @@ test_memzone_reserve_max_aligned(void)
                        SOCKET_ID_ANY, 0, align);
        if (mz == NULL){
                printf("Failed to reserve a big chunk of memory\n");
-               rte_dump_physmem_layout();
-               rte_memzone_dump();
+               rte_dump_physmem_layout(stdout);
+               rte_memzone_dump(stdout);
                return -1;
        }
 
@@ -434,8 +553,8 @@ test_memzone_reserve_max_aligned(void)
                                " bigest block\n", align);
                printf("Expected size = %zu, actual size = %zu\n",
                                maxlen, mz->len);
-               rte_dump_physmem_layout();
-               rte_memzone_dump();
+               rte_dump_physmem_layout(stdout);
+               rte_memzone_dump(stdout);
 
                return -1;
        }
@@ -476,11 +595,11 @@ test_memzone_aligned(void)
                printf("Unable to reserve 64-byte aligned memzone!\n");
                return -1;
        }
-       if ((memzone_aligned_32->phys_addr & CACHE_LINE_MASK) != 0)
+       if ((memzone_aligned_32->phys_addr & RTE_CACHE_LINE_MASK) != 0)
                return -1;
-       if (((uintptr_t) memzone_aligned_32->addr & CACHE_LINE_MASK) != 0)
+       if (((uintptr_t) memzone_aligned_32->addr & RTE_CACHE_LINE_MASK) != 0)
                return -1;
-       if ((memzone_aligned_32->len & CACHE_LINE_MASK) != 0)
+       if ((memzone_aligned_32->len & RTE_CACHE_LINE_MASK) != 0)
                return -1;
 
        if (memzone_aligned_128 == NULL) {
@@ -491,7 +610,7 @@ test_memzone_aligned(void)
                return -1;
        if (((uintptr_t) memzone_aligned_128->addr & 127) != 0)
                return -1;
-       if ((memzone_aligned_128->len & CACHE_LINE_MASK) != 0)
+       if ((memzone_aligned_128->len & RTE_CACHE_LINE_MASK) != 0)
                return -1;
 
        if (memzone_aligned_256 == NULL) {
@@ -502,7 +621,7 @@ test_memzone_aligned(void)
                return -1;
        if (((uintptr_t) memzone_aligned_256->addr & 255) != 0)
                return -1;
-       if ((memzone_aligned_256->len & CACHE_LINE_MASK) != 0)
+       if ((memzone_aligned_256->len & RTE_CACHE_LINE_MASK) != 0)
                return -1;
 
        if (memzone_aligned_512 == NULL) {
@@ -513,7 +632,7 @@ test_memzone_aligned(void)
                return -1;
        if (((uintptr_t) memzone_aligned_512->addr & 511) != 0)
                return -1;
-       if ((memzone_aligned_512->len & CACHE_LINE_MASK) != 0)
+       if ((memzone_aligned_512->len & RTE_CACHE_LINE_MASK) != 0)
                return -1;
 
        if (memzone_aligned_1024 == NULL) {
@@ -524,7 +643,7 @@ test_memzone_aligned(void)
                return -1;
        if (((uintptr_t) memzone_aligned_1024->addr & 1023) != 0)
                return -1;
-       if ((memzone_aligned_1024->len & CACHE_LINE_MASK) != 0)
+       if ((memzone_aligned_1024->len & RTE_CACHE_LINE_MASK) != 0)
                return -1;
 
        /* check that zones don't overlap */
@@ -571,7 +690,7 @@ check_memzone_bounded(const char *name, uint32_t len,  uint32_t align,
 
        bmask = ~((phys_addr_t)bound - 1);
 
-       if ((mz = rte_memzone_reserve_bounded(name, len, SOCKET_ID_ANY, 0, 
+       if ((mz = rte_memzone_reserve_bounded(name, len, SOCKET_ID_ANY, 0,
                        align, bound)) == NULL) {
                printf("%s(%s): memzone creation failed\n",
                        __func__, name);
@@ -590,8 +709,8 @@ check_memzone_bounded(const char *name, uint32_t len,  uint32_t align,
                return (-1);
        }
 
-       if ((mz->len & CACHE_LINE_MASK) != 0 || mz->len < len ||
-                       mz->len < CACHE_LINE_SIZE) {
+       if ((mz->len & RTE_CACHE_LINE_MASK) != 0 || mz->len < len ||
+                       mz->len < RTE_CACHE_LINE_SIZE) {
                printf("%s(%s): invalid length\n",
                        __func__, mz->name);
                return (-1);
@@ -622,7 +741,7 @@ test_memzone_bounded(void)
                        "conditions\n", __func__, memzone_err->name);
                return (-1);
        }
-                               
+
        /* should fail as len is greater then boundary */
        name = "bounded_error_32";
        if ((memzone_err = rte_memzone_reserve_bounded(name,
@@ -678,10 +797,9 @@ test_memzone_reserve_memory_in_smallest_segment(void)
 
                        /* set new smallest */
                        min_ms = ms;
-               }
-               else if (prev_min_ms == NULL) {
+               } else if ((prev_min_ms == NULL)
+                       || (prev_min_ms->len > ms->len))
                        prev_min_ms = ms;
-               }
        }
 
        if (min_ms == NULL || prev_min_ms == NULL) {
@@ -693,14 +811,14 @@ test_memzone_reserve_memory_in_smallest_segment(void)
        prev_min_len = prev_min_ms->len;
 
        /* try reserving a memzone in the smallest memseg */
-       mz = rte_memzone_reserve("smallest_mz", CACHE_LINE_SIZE,
+       mz = rte_memzone_reserve("smallest_mz", RTE_CACHE_LINE_SIZE,
                        SOCKET_ID_ANY, 0);
        if (mz == NULL) {
                printf("Failed to reserve memory from smallest memseg!\n");
                return -1;
        }
        if (prev_min_ms->len != prev_min_len &&
-                       min_ms->len != min_len - CACHE_LINE_SIZE) {
+                       min_ms->len != min_len - RTE_CACHE_LINE_SIZE) {
                printf("Reserved memory from wrong memseg!\n");
                return -1;
        }
@@ -739,7 +857,7 @@ test_memzone_reserve_memory_with_smallest_offset(void)
 
        min_ms = NULL;  /*< smallest segment */
        prev_min_ms = NULL; /*< second smallest segment */
-       align = CACHE_LINE_SIZE * 4;
+       align = RTE_CACHE_LINE_SIZE * 4;
 
        /* find two smallest segments */
        for (i = 0; i < RTE_MAX_MEMSEG; i++) {
@@ -758,8 +876,8 @@ test_memzone_reserve_memory_with_smallest_offset(void)
 
                        /* set new smallest */
                        min_ms = ms;
-               }
-               else if (prev_min_ms == NULL) {
+               } else if ((prev_min_ms == NULL)
+                       || (prev_min_ms->len > ms->len)) {
                        prev_min_ms = ms;
                }
        }
@@ -779,7 +897,7 @@ test_memzone_reserve_memory_with_smallest_offset(void)
 
                /* make sure final length is *not* aligned */
                while (((min_ms->addr_64 + len) & (align-1)) == 0)
-                       len += CACHE_LINE_SIZE;
+                       len += RTE_CACHE_LINE_SIZE;
 
                if (rte_memzone_reserve("dummy_mz1", len, SOCKET_ID_ANY, 0) == NULL) {
                        printf("Cannot reserve memory!\n");
@@ -794,12 +912,12 @@ test_memzone_reserve_memory_with_smallest_offset(void)
        }
     /* if we don't need to touch smallest segment but it's aligned */
     else if ((min_ms->addr_64 & (align-1)) == 0) {
-            if (rte_memzone_reserve("align_mz1", CACHE_LINE_SIZE,
+            if (rte_memzone_reserve("align_mz1", RTE_CACHE_LINE_SIZE,
                     SOCKET_ID_ANY, 0) == NULL) {
                             printf("Cannot reserve memory!\n");
                             return -1;
             }
-            if (min_ms->len != min_len - CACHE_LINE_SIZE) {
+            if (min_ms->len != min_len - RTE_CACHE_LINE_SIZE) {
                     printf("Reserved memory from wrong segment!\n");
                     return -1;
             }
@@ -811,7 +929,7 @@ test_memzone_reserve_memory_with_smallest_offset(void)
 
                /* make sure final length is aligned */
                while (((prev_min_ms->addr_64 + len) & (align-1)) != 0)
-                       len += CACHE_LINE_SIZE;
+                       len += RTE_CACHE_LINE_SIZE;
 
                if (rte_memzone_reserve("dummy_mz2", len, SOCKET_ID_ANY, 0) == NULL) {
                        printf("Cannot reserve memory!\n");
@@ -824,7 +942,7 @@ test_memzone_reserve_memory_with_smallest_offset(void)
                        return -1;
                }
        }
-       len = CACHE_LINE_SIZE;
+       len = RTE_CACHE_LINE_SIZE;
 
 
 
@@ -862,7 +980,7 @@ test_memzone_reserve_remainder(void)
        int i, align;
 
        min_len = 0;
-       align = CACHE_LINE_SIZE;
+       align = RTE_CACHE_LINE_SIZE;
 
        config = rte_eal_get_configuration();
 
@@ -880,7 +998,7 @@ test_memzone_reserve_remainder(void)
                        min_ms = ms;
 
                        /* find maximum alignment this segment is able to hold */
-                       align = CACHE_LINE_SIZE;
+                       align = RTE_CACHE_LINE_SIZE;
                        while ((ms->addr_64 & (align-1)) == 0) {
                                align <<= 1;
                        }
@@ -924,7 +1042,7 @@ test_memzone_reserve_remainder(void)
        return 0;
 }
 
-int
+static int
 test_memzone(void)
 {
        const struct rte_memzone *memzone1;
@@ -949,22 +1067,22 @@ test_memzone(void)
        if (memzone1 == NULL || memzone2 == NULL || memzone4 == NULL)
                return -1;
 
-       rte_memzone_dump();
+       rte_memzone_dump(stdout);
 
        /* check cache-line alignments */
        printf("check alignments and lengths\n");
 
-       if ((memzone1->phys_addr & CACHE_LINE_MASK) != 0)
+       if ((memzone1->phys_addr & RTE_CACHE_LINE_MASK) != 0)
                return -1;
-       if ((memzone2->phys_addr & CACHE_LINE_MASK) != 0)
+       if ((memzone2->phys_addr & RTE_CACHE_LINE_MASK) != 0)
                return -1;
-       if (memzone3 != NULL && (memzone3->phys_addr & CACHE_LINE_MASK) != 0)
+       if (memzone3 != NULL && (memzone3->phys_addr & RTE_CACHE_LINE_MASK) != 0)
                return -1;
-       if ((memzone1->len & CACHE_LINE_MASK) != 0 || memzone1->len == 0)
+       if ((memzone1->len & RTE_CACHE_LINE_MASK) != 0 || memzone1->len == 0)
                return -1;
-       if ((memzone2->len & CACHE_LINE_MASK) != 0 || memzone2->len == 0)
+       if ((memzone2->len & RTE_CACHE_LINE_MASK) != 0 || memzone2->len == 0)
                return -1;
-       if (memzone3 != NULL && ((memzone3->len & CACHE_LINE_MASK) != 0 ||
+       if (memzone3 != NULL && ((memzone3->len & RTE_CACHE_LINE_MASK) != 0 ||
                        memzone3->len == 0))
                return -1;
        if (memzone4->len != 1024)
@@ -1046,3 +1164,9 @@ test_memzone(void)
 
        return 0;
 }
+
+static struct test_command memzone_cmd = {
+       .command = "memzone_autotest",
+       .callback = test_memzone,
+};
+REGISTER_TEST_COMMAND(memzone_cmd);