update Intel copyright years to 2014
[dpdk.git] / app / test / test_memzone.c
index 17e5588..f735597 100644 (file)
@@ -1,35 +1,34 @@
 /*-
  *   BSD LICENSE
  * 
- *   Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
+ *   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 
+ *   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 
+ *     * 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 
- *       notice, this list of conditions and the following disclaimer in 
- *       the documentation and/or other materials provided with the 
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
  *       distribution.
- *     * Neither the name of Intel Corporation nor the names of its 
- *       contributors may be used to endorse or promote products derived 
+ *     * 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 
- *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
- *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
- *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
- *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
- *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
- *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
- *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
+ *   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
+ *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- * 
  */
 
 #include <stdio.h>
 
 #include <cmdline_parse.h>
 
+#include <rte_random.h>
+#include <rte_cycles.h>
 #include <rte_memory.h>
 #include <rte_memzone.h>
 #include <rte_tailq.h>
 #include <rte_eal.h>
+#include <rte_eal_memconfig.h>
 #include <rte_common.h>
 
 #include "test.h"
@@ -115,7 +117,7 @@ test_memzone_reserving_zone_size_bigger_than_the_maximum(void)
                return -1;
        }
 
-       mz = rte_memzone_reserve("zone_size_bigger_than_the_maximum", 0x1900000000ULL,
+       mz = rte_memzone_reserve("zone_size_bigger_than_the_maximum", (size_t)-1,
                        SOCKET_ID_ANY, 0);
        if (mz != NULL) {
                printf("It is impossible to reserve such big a memzone\n");
@@ -265,9 +267,9 @@ test_memzone_reserve_max(void)
        const struct rte_memseg *ms;
        int memseg_idx = 0;
        int memzone_idx = 0;
-       uint64_t len = 0;
+       size_t len = 0;
        void* last_addr;
-       uint64_t maxlen = 0;
+       size_t maxlen = 0;
 
        /* get pointer to global configuration */
        config = rte_eal_get_configuration();
@@ -279,8 +281,10 @@ test_memzone_reserve_max(void)
                if (ms[memseg_idx].len < maxlen)
                        continue;
 
-               len = ms[memseg_idx].len;
-               last_addr = ms[memseg_idx].addr;
+               /* align everything */
+               last_addr = RTE_PTR_ALIGN_CEIL(ms[memseg_idx].addr, CACHE_LINE_SIZE);
+               len = ms[memseg_idx].len - RTE_PTR_DIFF(last_addr, ms[memseg_idx].addr);
+               len &= ~((size_t) CACHE_LINE_MASK);
 
                /* cycle through all memzones */
                for (memzone_idx = 0; memzone_idx < RTE_MAX_MEMZONE; memzone_idx++) {
@@ -292,21 +296,19 @@ 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,
-                                       (size_t)ms[memseg_idx].len)))) {
+                                       (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
                                 * are allocated sequentially so we don't need to worry about
                                 * them being in the right order.
                                 */
-                               len -= (uintptr_t) RTE_PTR_SUB(
+                               len -= RTE_PTR_DIFF(
                                                config->mem_config->memzone[memzone_idx].addr,
-                                               (uintptr_t) last_addr);
+                                               last_addr);
                                len -= config->mem_config->memzone[memzone_idx].len;
-                               last_addr =
-                                               RTE_PTR_ADD(config->mem_config->memzone[memzone_idx].addr,
+                               last_addr = RTE_PTR_ADD(config->mem_config->memzone[memzone_idx].addr,
                                                (size_t) config->mem_config->memzone[memzone_idx].len);
                        }
                }
@@ -317,6 +319,11 @@ test_memzone_reserve_max(void)
                        maxlen = len;
        }
 
+       if (maxlen == 0) {
+               printf("There is no space left!\n");
+               return 0;
+       }
+
        mz = rte_memzone_reserve("max_zone", 0, SOCKET_ID_ANY, 0);
        if (mz == NULL){
                printf("Failed to reserve a big chunk of memory\n");
@@ -327,7 +334,7 @@ test_memzone_reserve_max(void)
 
        if (mz->len != maxlen) {
                printf("Memzone reserve with 0 size did not return bigest block\n");
-               printf("Expected size = %" PRIu64 ", actual size = %" PRIu64 "\n",
+               printf("Expected size = %zu, actual size = %zu\n",
                                maxlen, mz->len);
                rte_dump_physmem_layout();
                rte_memzone_dump();
@@ -345,9 +352,14 @@ test_memzone_reserve_max_aligned(void)
        const struct rte_memseg *ms;
        int memseg_idx = 0;
        int memzone_idx = 0;
-       uint64_t addr_offset, len = 0;
+       uintptr_t addr_offset;
+       size_t len = 0;
        void* last_addr;
-       uint64_t maxlen = 0;
+       size_t maxlen = 0;
+
+       /* random alignment */
+       rte_srand((unsigned)rte_rdtsc());
+       const unsigned align = 1 << ((rte_rand() % 8) + 5); /* from 128 up to 4k alignment */
 
        /* get pointer to global configuration */
        config = rte_eal_get_configuration();
@@ -362,8 +374,10 @@ test_memzone_reserve_max_aligned(void)
                if (ms[memseg_idx].len < maxlen)
                        continue;
 
-               len = ms[memseg_idx].len;
-               last_addr = ms[memseg_idx].addr;
+               /* align everything */
+               last_addr = RTE_PTR_ALIGN_CEIL(ms[memseg_idx].addr, CACHE_LINE_SIZE);
+               len = ms[memseg_idx].len - RTE_PTR_DIFF(last_addr, ms[memseg_idx].addr);
+               len &= ~((size_t) CACHE_LINE_MASK);
 
                /* cycle through all memzones */
                for (memzone_idx = 0; memzone_idx < RTE_MAX_MEMZONE; memzone_idx++) {
@@ -375,9 +389,8 @@ test_memzone_reserve_max_aligned(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,
-                                       (size_t) ms[memseg_idx].len)))) {
+                                       (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.
@@ -394,15 +407,20 @@ test_memzone_reserve_max_aligned(void)
 
                /* make sure we get the alignment offset */
                if (len > maxlen) {
-                       addr_offset = RTE_ALIGN_CEIL((uintptr_t) last_addr, 512) - (uintptr_t) last_addr;
+                       addr_offset = RTE_PTR_ALIGN_CEIL((uintptr_t) last_addr, align) - (uintptr_t) last_addr;
                        maxlen = len;
                }
        }
 
+       if (maxlen == 0 || maxlen == addr_offset) {
+               printf("There is no space left for biggest %u-aligned memzone!\n", align);
+               return 0;
+       }
+
        maxlen -= addr_offset;
 
        mz = rte_memzone_reserve_aligned("max_zone_aligned", 0,
-                       SOCKET_ID_ANY, 0, 512);
+                       SOCKET_ID_ANY, 0, align);
        if (mz == NULL){
                printf("Failed to reserve a big chunk of memory\n");
                rte_dump_physmem_layout();
@@ -411,9 +429,9 @@ test_memzone_reserve_max_aligned(void)
        }
 
        if (mz->len != maxlen) {
-               printf("Memzone reserve with 0 size and alignment 512 did not return"
-                               " bigest block\n");
-               printf("Expected size = %" PRIu64 ", actual size = %" PRIu64 "\n",
+               printf("Memzone reserve with 0 size and alignment %u did not return"
+                               " bigest block\n", align);
+               printf("Expected size = %zu, actual size = %zu\n",
                                maxlen, mz->len);
                rte_dump_physmem_layout();
                rte_memzone_dump();
@@ -463,30 +481,50 @@ test_memzone_aligned(void)
                                SOCKET_ID_ANY, 0, 1024);
 
        printf("check alignments and lengths\n");
+       if (memzone_aligned_32 == NULL) {
+               printf("Unable to reserve 64-byte aligned memzone!\n");
+               return -1;
+       }
        if ((memzone_aligned_32->phys_addr & CACHE_LINE_MASK) != 0)
                return -1;
        if (((uintptr_t) memzone_aligned_32->addr & CACHE_LINE_MASK) != 0)
                return -1;
        if ((memzone_aligned_32->len & CACHE_LINE_MASK) != 0)
                return -1;
+       if (memzone_aligned_128 == NULL) {
+               printf("Unable to reserve 128-byte aligned memzone!\n");
+               return -1;
+       }
        if ((memzone_aligned_128->phys_addr & 127) != 0)
                return -1;
        if (((uintptr_t) memzone_aligned_128->addr & 127) != 0)
                return -1;
        if ((memzone_aligned_128->len & CACHE_LINE_MASK) != 0)
                return -1;
+       if (memzone_aligned_256 == NULL) {
+               printf("Unable to reserve 256-byte aligned memzone!\n");
+               return -1;
+       }
        if ((memzone_aligned_256->phys_addr & 255) != 0)
                return -1;
        if (((uintptr_t) memzone_aligned_256->addr & 255) != 0)
                return -1;
        if ((memzone_aligned_256->len & CACHE_LINE_MASK) != 0)
                return -1;
+       if (memzone_aligned_512 == NULL) {
+               printf("Unable to reserve 512-byte aligned memzone!\n");
+               return -1;
+       }
        if ((memzone_aligned_512->phys_addr & 511) != 0)
                return -1;
        if (((uintptr_t) memzone_aligned_512->addr & 511) != 0)
                return -1;
        if ((memzone_aligned_512->len & CACHE_LINE_MASK) != 0)
                return -1;
+       if (memzone_aligned_1024 == NULL) {
+               printf("Unable to reserve 1024-byte aligned memzone!\n");
+               return -1;
+       }
        if ((memzone_aligned_1024->phys_addr & 1023) != 0)
                return -1;
        if (((uintptr_t) memzone_aligned_1024->addr & 1023) != 0)
@@ -494,7 +532,6 @@ test_memzone_aligned(void)
        if ((memzone_aligned_1024->len & CACHE_LINE_MASK) != 0)
                return -1;
 
-
        /* check that zones don't overlap */
        printf("check overlapping\n");
        if (is_memory_overlap(memzone_aligned_32->phys_addr, memzone_aligned_32->len,
@@ -614,10 +651,6 @@ test_memzone(void)
        if (test_memzone_reserving_zone_size_bigger_than_the_maximum() < 0)
                return -1;
 
-       printf("test reserving the largest size memzone possible\n");
-       if (test_memzone_reserve_max() < 0)
-               return -1;
-
        printf("test memzone_reserve flags\n");
        if (test_memzone_reserve_flags() < 0)
                return -1;
@@ -630,6 +663,10 @@ test_memzone(void)
        if (test_memzone_invalid_alignment() < 0)
                return -1;
 
+       printf("test reserving the largest size memzone possible\n");
+       if (test_memzone_reserve_max() < 0)
+               return -1;
+
        printf("test reserving the largest size aligned memzone possible\n");
        if (test_memzone_reserve_max_aligned() < 0)
                return -1;