4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <sys/queue.h>
39 #include <rte_random.h>
40 #include <rte_cycles.h>
41 #include <rte_memory.h>
42 #include <rte_memzone.h>
44 #include <rte_eal_memconfig.h>
45 #include <rte_common.h>
46 #include <rte_string_fns.h>
47 #include <rte_errno.h>
48 #include <rte_malloc.h>
49 #include "../../lib/librte_eal/common/malloc_elem.h"
57 * - Search for three reserved zones or reserve them if they do not exist:
59 * - One is on any socket id.
60 * - The second is on socket 0.
61 * - The last one is on socket 1 (if socket 1 exists).
63 * - Check that the zones exist.
65 * - Check that the zones are cache-aligned.
67 * - Check that zones do not overlap.
69 * - Check that the zones are on the correct socket id.
71 * - Check that a lookup of the first zone returns the same pointer.
73 * - Check that it is not possible to create another zone with the
74 * same name as an existing zone.
76 * - Check flags for specific huge page size reservation
79 /* Test if memory overlaps: return 1 if true, or 0 if false. */
81 is_memory_overlap(rte_iova_t ptr1, size_t len1, rte_iova_t ptr2, size_t len2)
83 if (ptr2 >= ptr1 && (ptr2 - ptr1) < len1)
85 else if (ptr2 < ptr1 && (ptr1 - ptr2) < len2)
91 test_memzone_invalid_alignment(void)
93 const struct rte_memzone * mz;
95 mz = rte_memzone_lookup("invalid_alignment");
97 printf("Zone with invalid alignment has been reserved\n");
101 mz = rte_memzone_reserve_aligned("invalid_alignment", 100,
102 SOCKET_ID_ANY, 0, 100);
104 printf("Zone with invalid alignment has been reserved\n");
111 test_memzone_reserving_zone_size_bigger_than_the_maximum(void)
113 const struct rte_memzone * mz;
115 mz = rte_memzone_lookup("zone_size_bigger_than_the_maximum");
117 printf("zone_size_bigger_than_the_maximum has been reserved\n");
121 mz = rte_memzone_reserve("zone_size_bigger_than_the_maximum", (size_t)-1,
124 printf("It is impossible to reserve such big a memzone\n");
132 test_memzone_reserve_flags(void)
134 const struct rte_memzone *mz;
135 const struct rte_memseg *ms;
136 int hugepage_2MB_avail = 0;
137 int hugepage_1GB_avail = 0;
138 int hugepage_16MB_avail = 0;
139 int hugepage_16GB_avail = 0;
140 const size_t size = 100;
142 ms = rte_eal_get_physmem_layout();
143 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
144 if (ms[i].hugepage_sz == RTE_PGSIZE_2M)
145 hugepage_2MB_avail = 1;
146 if (ms[i].hugepage_sz == RTE_PGSIZE_1G)
147 hugepage_1GB_avail = 1;
148 if (ms[i].hugepage_sz == RTE_PGSIZE_16M)
149 hugepage_16MB_avail = 1;
150 if (ms[i].hugepage_sz == RTE_PGSIZE_16G)
151 hugepage_16GB_avail = 1;
153 /* Display the availability of 2MB ,1GB, 16MB, 16GB pages */
154 if (hugepage_2MB_avail)
155 printf("2MB Huge pages available\n");
156 if (hugepage_1GB_avail)
157 printf("1GB Huge pages available\n");
158 if (hugepage_16MB_avail)
159 printf("16MB Huge pages available\n");
160 if (hugepage_16GB_avail)
161 printf("16GB Huge pages available\n");
163 * If 2MB pages available, check that a small memzone is correctly
164 * reserved from 2MB huge pages when requested by the RTE_MEMZONE_2MB flag.
165 * Also check that RTE_MEMZONE_SIZE_HINT_ONLY flag only defaults to an
166 * available page size (i.e 1GB ) when 2MB pages are unavailable.
168 if (hugepage_2MB_avail) {
169 mz = rte_memzone_reserve("flag_zone_2M", size, SOCKET_ID_ANY,
172 printf("MEMZONE FLAG 2MB\n");
175 if (mz->hugepage_sz != RTE_PGSIZE_2M) {
176 printf("hugepage_sz not equal 2M\n");
179 if (rte_memzone_free(mz)) {
180 printf("Fail memzone free\n");
184 mz = rte_memzone_reserve("flag_zone_2M_HINT", size, SOCKET_ID_ANY,
185 RTE_MEMZONE_2MB|RTE_MEMZONE_SIZE_HINT_ONLY);
187 printf("MEMZONE FLAG 2MB\n");
190 if (mz->hugepage_sz != RTE_PGSIZE_2M) {
191 printf("hugepage_sz not equal 2M\n");
194 if (rte_memzone_free(mz)) {
195 printf("Fail memzone free\n");
199 /* Check if 1GB huge pages are unavailable, that function fails unless
200 * HINT flag is indicated
202 if (!hugepage_1GB_avail) {
203 mz = rte_memzone_reserve("flag_zone_1G_HINT", size, SOCKET_ID_ANY,
204 RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY);
206 printf("MEMZONE FLAG 1GB & HINT\n");
209 if (mz->hugepage_sz != RTE_PGSIZE_2M) {
210 printf("hugepage_sz not equal 2M\n");
213 if (rte_memzone_free(mz)) {
214 printf("Fail memzone free\n");
218 mz = rte_memzone_reserve("flag_zone_1G", size, SOCKET_ID_ANY,
221 printf("MEMZONE FLAG 1GB\n");
227 /*As with 2MB tests above for 1GB huge page requests*/
228 if (hugepage_1GB_avail) {
229 mz = rte_memzone_reserve("flag_zone_1G", size, SOCKET_ID_ANY,
232 printf("MEMZONE FLAG 1GB\n");
235 if (mz->hugepage_sz != RTE_PGSIZE_1G) {
236 printf("hugepage_sz not equal 1G\n");
239 if (rte_memzone_free(mz)) {
240 printf("Fail memzone free\n");
244 mz = rte_memzone_reserve("flag_zone_1G_HINT", size, SOCKET_ID_ANY,
245 RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY);
247 printf("MEMZONE FLAG 1GB\n");
250 if (mz->hugepage_sz != RTE_PGSIZE_1G) {
251 printf("hugepage_sz not equal 1G\n");
254 if (rte_memzone_free(mz)) {
255 printf("Fail memzone free\n");
259 /* Check if 1GB huge pages are unavailable, that function fails unless
260 * HINT flag is indicated
262 if (!hugepage_2MB_avail) {
263 mz = rte_memzone_reserve("flag_zone_2M_HINT", size, SOCKET_ID_ANY,
264 RTE_MEMZONE_2MB|RTE_MEMZONE_SIZE_HINT_ONLY);
266 printf("MEMZONE FLAG 2MB & HINT\n");
269 if (mz->hugepage_sz != RTE_PGSIZE_1G) {
270 printf("hugepage_sz not equal 1G\n");
273 if (rte_memzone_free(mz)) {
274 printf("Fail memzone free\n");
277 mz = rte_memzone_reserve("flag_zone_2M", size, SOCKET_ID_ANY,
280 printf("MEMZONE FLAG 2MB\n");
283 if (rte_memzone_free(mz)) {
284 printf("Fail memzone free\n");
289 if (hugepage_2MB_avail && hugepage_1GB_avail) {
290 mz = rte_memzone_reserve("flag_zone_2M_HINT", size, SOCKET_ID_ANY,
291 RTE_MEMZONE_2MB|RTE_MEMZONE_1GB);
293 printf("BOTH SIZES SET\n");
299 * This option is for IBM Power. If 16MB pages available, check
300 * that a small memzone is correctly reserved from 16MB huge pages
301 * when requested by the RTE_MEMZONE_16MB flag. Also check that
302 * RTE_MEMZONE_SIZE_HINT_ONLY flag only defaults to an available
303 * page size (i.e 16GB ) when 16MB pages are unavailable.
305 if (hugepage_16MB_avail) {
306 mz = rte_memzone_reserve("flag_zone_16M", size, SOCKET_ID_ANY,
309 printf("MEMZONE FLAG 16MB\n");
312 if (mz->hugepage_sz != RTE_PGSIZE_16M) {
313 printf("hugepage_sz not equal 16M\n");
316 if (rte_memzone_free(mz)) {
317 printf("Fail memzone free\n");
321 mz = rte_memzone_reserve("flag_zone_16M_HINT", size,
322 SOCKET_ID_ANY, RTE_MEMZONE_16MB|RTE_MEMZONE_SIZE_HINT_ONLY);
324 printf("MEMZONE FLAG 2MB\n");
327 if (mz->hugepage_sz != RTE_PGSIZE_16M) {
328 printf("hugepage_sz not equal 16M\n");
331 if (rte_memzone_free(mz)) {
332 printf("Fail memzone free\n");
336 /* Check if 1GB huge pages are unavailable, that function fails
337 * unless HINT flag is indicated
339 if (!hugepage_16GB_avail) {
340 mz = rte_memzone_reserve("flag_zone_16G_HINT", size,
342 RTE_MEMZONE_16GB|RTE_MEMZONE_SIZE_HINT_ONLY);
344 printf("MEMZONE FLAG 16GB & HINT\n");
347 if (mz->hugepage_sz != RTE_PGSIZE_16M) {
348 printf("hugepage_sz not equal 16M\n");
351 if (rte_memzone_free(mz)) {
352 printf("Fail memzone free\n");
356 mz = rte_memzone_reserve("flag_zone_16G", size,
357 SOCKET_ID_ANY, RTE_MEMZONE_16GB);
359 printf("MEMZONE FLAG 16GB\n");
364 /*As with 16MB tests above for 16GB huge page requests*/
365 if (hugepage_16GB_avail) {
366 mz = rte_memzone_reserve("flag_zone_16G", size, SOCKET_ID_ANY,
369 printf("MEMZONE FLAG 16GB\n");
372 if (mz->hugepage_sz != RTE_PGSIZE_16G) {
373 printf("hugepage_sz not equal 16G\n");
376 if (rte_memzone_free(mz)) {
377 printf("Fail memzone free\n");
381 mz = rte_memzone_reserve("flag_zone_16G_HINT", size,
382 SOCKET_ID_ANY, RTE_MEMZONE_16GB|RTE_MEMZONE_SIZE_HINT_ONLY);
384 printf("MEMZONE FLAG 16GB\n");
387 if (mz->hugepage_sz != RTE_PGSIZE_16G) {
388 printf("hugepage_sz not equal 16G\n");
391 if (rte_memzone_free(mz)) {
392 printf("Fail memzone free\n");
396 /* Check if 1GB huge pages are unavailable, that function fails
397 * unless HINT flag is indicated
399 if (!hugepage_16MB_avail) {
400 mz = rte_memzone_reserve("flag_zone_16M_HINT", size,
402 RTE_MEMZONE_16MB|RTE_MEMZONE_SIZE_HINT_ONLY);
404 printf("MEMZONE FLAG 16MB & HINT\n");
407 if (mz->hugepage_sz != RTE_PGSIZE_16G) {
408 printf("hugepage_sz not equal 16G\n");
411 if (rte_memzone_free(mz)) {
412 printf("Fail memzone free\n");
415 mz = rte_memzone_reserve("flag_zone_16M", size,
416 SOCKET_ID_ANY, RTE_MEMZONE_16MB);
418 printf("MEMZONE FLAG 16MB\n");
423 if (hugepage_16MB_avail && hugepage_16GB_avail) {
424 mz = rte_memzone_reserve("flag_zone_16M_HINT", size,
426 RTE_MEMZONE_16MB|RTE_MEMZONE_16GB);
428 printf("BOTH SIZES SET\n");
437 /* Find the heap with the greatest free block size */
439 find_max_block_free_size(const unsigned _align)
441 struct rte_malloc_socket_stats stats;
442 unsigned i, align = _align;
445 for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
446 rte_malloc_get_socket_stats(i, &stats);
447 if (stats.greatest_free_size > len)
448 len = stats.greatest_free_size;
451 if (align < RTE_CACHE_LINE_SIZE)
452 align = RTE_CACHE_LINE_ROUNDUP(align+1);
454 if (len <= MALLOC_ELEM_OVERHEAD + align)
457 return len - MALLOC_ELEM_OVERHEAD - align;
461 test_memzone_reserve_max(void)
463 const struct rte_memzone *mz;
466 maxlen = find_max_block_free_size(0);
469 printf("There is no space left!\n");
473 mz = rte_memzone_reserve("max_zone", 0, SOCKET_ID_ANY, 0);
475 printf("Failed to reserve a big chunk of memory - %s\n",
476 rte_strerror(rte_errno));
477 rte_dump_physmem_layout(stdout);
478 rte_memzone_dump(stdout);
482 if (mz->len != maxlen) {
483 printf("Memzone reserve with 0 size did not return bigest block\n");
484 printf("Expected size = %zu, actual size = %zu\n", maxlen, mz->len);
485 rte_dump_physmem_layout(stdout);
486 rte_memzone_dump(stdout);
490 if (rte_memzone_free(mz)) {
491 printf("Fail memzone free\n");
499 test_memzone_reserve_max_aligned(void)
501 const struct rte_memzone *mz;
504 /* random alignment */
505 rte_srand((unsigned)rte_rdtsc());
506 const unsigned align = 1 << ((rte_rand() % 8) + 5); /* from 128 up to 4k alignment */
508 maxlen = find_max_block_free_size(align);
511 printf("There is no space left for biggest %u-aligned memzone!\n", align);
515 mz = rte_memzone_reserve_aligned("max_zone_aligned", 0,
516 SOCKET_ID_ANY, 0, align);
518 printf("Failed to reserve a big chunk of memory - %s\n",
519 rte_strerror(rte_errno));
520 rte_dump_physmem_layout(stdout);
521 rte_memzone_dump(stdout);
525 if (mz->len != maxlen) {
526 printf("Memzone reserve with 0 size and alignment %u did not return"
527 " bigest block\n", align);
528 printf("Expected size = %zu, actual size = %zu\n",
530 rte_dump_physmem_layout(stdout);
531 rte_memzone_dump(stdout);
535 if (rte_memzone_free(mz)) {
536 printf("Fail memzone free\n");
544 test_memzone_aligned(void)
546 const struct rte_memzone *memzone_aligned_32;
547 const struct rte_memzone *memzone_aligned_128;
548 const struct rte_memzone *memzone_aligned_256;
549 const struct rte_memzone *memzone_aligned_512;
550 const struct rte_memzone *memzone_aligned_1024;
552 /* memzone that should automatically be adjusted to align on 64 bytes */
553 memzone_aligned_32 = rte_memzone_reserve_aligned("aligned_32", 100,
554 SOCKET_ID_ANY, 0, 32);
556 /* memzone that is supposed to be aligned on a 128 byte boundary */
557 memzone_aligned_128 = rte_memzone_reserve_aligned("aligned_128", 100,
558 SOCKET_ID_ANY, 0, 128);
560 /* memzone that is supposed to be aligned on a 256 byte boundary */
561 memzone_aligned_256 = rte_memzone_reserve_aligned("aligned_256", 100,
562 SOCKET_ID_ANY, 0, 256);
564 /* memzone that is supposed to be aligned on a 512 byte boundary */
565 memzone_aligned_512 = rte_memzone_reserve_aligned("aligned_512", 100,
566 SOCKET_ID_ANY, 0, 512);
568 /* memzone that is supposed to be aligned on a 1024 byte boundary */
569 memzone_aligned_1024 = rte_memzone_reserve_aligned("aligned_1024", 100,
570 SOCKET_ID_ANY, 0, 1024);
572 printf("check alignments and lengths\n");
573 if (memzone_aligned_32 == NULL) {
574 printf("Unable to reserve 64-byte aligned memzone!\n");
577 if ((memzone_aligned_32->iova & RTE_CACHE_LINE_MASK) != 0)
579 if (((uintptr_t) memzone_aligned_32->addr & RTE_CACHE_LINE_MASK) != 0)
581 if ((memzone_aligned_32->len & RTE_CACHE_LINE_MASK) != 0)
584 if (memzone_aligned_128 == NULL) {
585 printf("Unable to reserve 128-byte aligned memzone!\n");
588 if ((memzone_aligned_128->iova & 127) != 0)
590 if (((uintptr_t) memzone_aligned_128->addr & 127) != 0)
592 if ((memzone_aligned_128->len & RTE_CACHE_LINE_MASK) != 0)
595 if (memzone_aligned_256 == NULL) {
596 printf("Unable to reserve 256-byte aligned memzone!\n");
599 if ((memzone_aligned_256->iova & 255) != 0)
601 if (((uintptr_t) memzone_aligned_256->addr & 255) != 0)
603 if ((memzone_aligned_256->len & RTE_CACHE_LINE_MASK) != 0)
606 if (memzone_aligned_512 == NULL) {
607 printf("Unable to reserve 512-byte aligned memzone!\n");
610 if ((memzone_aligned_512->iova & 511) != 0)
612 if (((uintptr_t) memzone_aligned_512->addr & 511) != 0)
614 if ((memzone_aligned_512->len & RTE_CACHE_LINE_MASK) != 0)
617 if (memzone_aligned_1024 == NULL) {
618 printf("Unable to reserve 1024-byte aligned memzone!\n");
621 if ((memzone_aligned_1024->iova & 1023) != 0)
623 if (((uintptr_t) memzone_aligned_1024->addr & 1023) != 0)
625 if ((memzone_aligned_1024->len & RTE_CACHE_LINE_MASK) != 0)
628 /* check that zones don't overlap */
629 printf("check overlapping\n");
630 if (is_memory_overlap(memzone_aligned_32->iova, memzone_aligned_32->len,
631 memzone_aligned_128->iova, memzone_aligned_128->len))
633 if (is_memory_overlap(memzone_aligned_32->iova, memzone_aligned_32->len,
634 memzone_aligned_256->iova, memzone_aligned_256->len))
636 if (is_memory_overlap(memzone_aligned_32->iova, memzone_aligned_32->len,
637 memzone_aligned_512->iova, memzone_aligned_512->len))
639 if (is_memory_overlap(memzone_aligned_32->iova, memzone_aligned_32->len,
640 memzone_aligned_1024->iova, memzone_aligned_1024->len))
642 if (is_memory_overlap(memzone_aligned_128->iova, memzone_aligned_128->len,
643 memzone_aligned_256->iova, memzone_aligned_256->len))
645 if (is_memory_overlap(memzone_aligned_128->iova, memzone_aligned_128->len,
646 memzone_aligned_512->iova, memzone_aligned_512->len))
648 if (is_memory_overlap(memzone_aligned_128->iova, memzone_aligned_128->len,
649 memzone_aligned_1024->iova, memzone_aligned_1024->len))
651 if (is_memory_overlap(memzone_aligned_256->iova, memzone_aligned_256->len,
652 memzone_aligned_512->iova, memzone_aligned_512->len))
654 if (is_memory_overlap(memzone_aligned_256->iova, memzone_aligned_256->len,
655 memzone_aligned_1024->iova, memzone_aligned_1024->len))
657 if (is_memory_overlap(memzone_aligned_512->iova, memzone_aligned_512->len,
658 memzone_aligned_1024->iova, memzone_aligned_1024->len))
661 /* free all used zones */
662 if (rte_memzone_free(memzone_aligned_32)) {
663 printf("Fail memzone free\n");
666 if (rte_memzone_free(memzone_aligned_128)) {
667 printf("Fail memzone free\n");
670 if (rte_memzone_free(memzone_aligned_256)) {
671 printf("Fail memzone free\n");
674 if (rte_memzone_free(memzone_aligned_512)) {
675 printf("Fail memzone free\n");
678 if (rte_memzone_free(memzone_aligned_1024)) {
679 printf("Fail memzone free\n");
686 check_memzone_bounded(const char *name, uint32_t len, uint32_t align,
689 const struct rte_memzone *mz;
692 bmask = ~((rte_iova_t)bound - 1);
694 if ((mz = rte_memzone_reserve_bounded(name, len, SOCKET_ID_ANY, 0,
695 align, bound)) == NULL) {
696 printf("%s(%s): memzone creation failed\n",
701 if ((mz->iova & ((rte_iova_t)align - 1)) != 0) {
702 printf("%s(%s): invalid phys addr alignment\n",
707 if (((uintptr_t) mz->addr & ((uintptr_t)align - 1)) != 0) {
708 printf("%s(%s): invalid virtual addr alignment\n",
713 if ((mz->len & RTE_CACHE_LINE_MASK) != 0 || mz->len < len ||
714 mz->len < RTE_CACHE_LINE_SIZE) {
715 printf("%s(%s): invalid length\n",
720 if ((mz->iova & bmask) !=
721 ((mz->iova + mz->len - 1) & bmask)) {
722 printf("%s(%s): invalid memzone boundary %u crossed\n",
723 __func__, mz->name, bound);
727 if (rte_memzone_free(mz)) {
728 printf("Fail memzone free\n");
736 test_memzone_bounded(void)
738 const struct rte_memzone *memzone_err;
742 /* should fail as boundary is not power of two */
743 name = "bounded_error_31";
744 if ((memzone_err = rte_memzone_reserve_bounded(name,
745 100, SOCKET_ID_ANY, 0, 32, UINT32_MAX)) != NULL) {
746 printf("%s(%s)created a memzone with invalid boundary "
747 "conditions\n", __func__, memzone_err->name);
751 /* should fail as len is greater then boundary */
752 name = "bounded_error_32";
753 if ((memzone_err = rte_memzone_reserve_bounded(name,
754 100, SOCKET_ID_ANY, 0, 32, 32)) != NULL) {
755 printf("%s(%s)created a memzone with invalid boundary "
756 "conditions\n", __func__, memzone_err->name);
760 if ((rc = check_memzone_bounded("bounded_128", 100, 128, 128)) != 0)
763 if ((rc = check_memzone_bounded("bounded_256", 100, 256, 128)) != 0)
766 if ((rc = check_memzone_bounded("bounded_1K", 100, 64, 1024)) != 0)
769 if ((rc = check_memzone_bounded("bounded_1K_MAX", 0, 64, 1024)) != 0)
776 test_memzone_free(void)
778 const struct rte_memzone *mz[RTE_MAX_MEMZONE];
782 mz[0] = rte_memzone_reserve("tempzone0", 2000, SOCKET_ID_ANY, 0);
783 mz[1] = rte_memzone_reserve("tempzone1", 4000, SOCKET_ID_ANY, 0);
787 if (!rte_memzone_lookup("tempzone0"))
789 if (!rte_memzone_lookup("tempzone1"))
792 if (rte_memzone_free(mz[0])) {
793 printf("Fail memzone free - tempzone0\n");
796 if (rte_memzone_lookup("tempzone0")) {
797 printf("Found previously free memzone - tempzone0\n");
800 mz[2] = rte_memzone_reserve("tempzone2", 2000, SOCKET_ID_ANY, 0);
803 printf("tempzone2 should have gotten the free entry from tempzone0\n");
806 if (rte_memzone_free(mz[2])) {
807 printf("Fail memzone free - tempzone2\n");
810 if (rte_memzone_lookup("tempzone2")) {
811 printf("Found previously free memzone - tempzone2\n");
814 if (rte_memzone_free(mz[1])) {
815 printf("Fail memzone free - tempzone1\n");
818 if (rte_memzone_lookup("tempzone1")) {
819 printf("Found previously free memzone - tempzone1\n");
825 snprintf(name, sizeof(name), "tempzone%u", i);
826 mz[i] = rte_memzone_reserve(name, 1, SOCKET_ID_ANY, 0);
827 } while (mz[i++] != NULL);
829 if (rte_memzone_free(mz[0])) {
830 printf("Fail memzone free - tempzone0\n");
833 mz[0] = rte_memzone_reserve("tempzone0new", 0, SOCKET_ID_ANY, 0);
836 printf("Fail to create memzone - tempzone0new - when MAX memzones were "
837 "created and one was free\n");
841 for (i = i - 2; i >= 0; i--) {
842 if (rte_memzone_free(mz[i])) {
843 printf("Fail memzone free - tempzone%d\n", i);
852 test_memzone_basic(void)
854 const struct rte_memzone *memzone1;
855 const struct rte_memzone *memzone2;
856 const struct rte_memzone *memzone3;
857 const struct rte_memzone *memzone4;
858 const struct rte_memzone *mz;
860 memzone1 = rte_memzone_reserve("testzone1", 100,
863 memzone2 = rte_memzone_reserve("testzone2", 1000,
866 memzone3 = rte_memzone_reserve("testzone3", 1000,
869 memzone4 = rte_memzone_reserve("testzone4", 1024,
872 /* memzone3 may be NULL if we don't have NUMA */
873 if (memzone1 == NULL || memzone2 == NULL || memzone4 == NULL)
876 rte_memzone_dump(stdout);
878 /* check cache-line alignments */
879 printf("check alignments and lengths\n");
881 if ((memzone1->iova & RTE_CACHE_LINE_MASK) != 0)
883 if ((memzone2->iova & RTE_CACHE_LINE_MASK) != 0)
885 if (memzone3 != NULL && (memzone3->iova & RTE_CACHE_LINE_MASK) != 0)
887 if ((memzone1->len & RTE_CACHE_LINE_MASK) != 0 || memzone1->len == 0)
889 if ((memzone2->len & RTE_CACHE_LINE_MASK) != 0 || memzone2->len == 0)
891 if (memzone3 != NULL && ((memzone3->len & RTE_CACHE_LINE_MASK) != 0 ||
894 if (memzone4->len != 1024)
897 /* check that zones don't overlap */
898 printf("check overlapping\n");
900 if (is_memory_overlap(memzone1->iova, memzone1->len,
901 memzone2->iova, memzone2->len))
903 if (memzone3 != NULL &&
904 is_memory_overlap(memzone1->iova, memzone1->len,
905 memzone3->iova, memzone3->len))
907 if (memzone3 != NULL &&
908 is_memory_overlap(memzone2->iova, memzone2->len,
909 memzone3->iova, memzone3->len))
912 printf("check socket ID\n");
914 /* memzone2 must be on socket id 0 and memzone3 on socket 1 */
915 if (memzone2->socket_id != 0)
917 if (memzone3 != NULL && memzone3->socket_id != 1)
920 printf("test zone lookup\n");
921 mz = rte_memzone_lookup("testzone1");
925 printf("test duplcate zone name\n");
926 mz = rte_memzone_reserve("testzone1", 100,
931 if (rte_memzone_free(memzone1)) {
932 printf("Fail memzone free - memzone1\n");
935 if (rte_memzone_free(memzone2)) {
936 printf("Fail memzone free - memzone2\n");
939 if (memzone3 && rte_memzone_free(memzone3)) {
940 printf("Fail memzone free - memzone3\n");
943 if (rte_memzone_free(memzone4)) {
944 printf("Fail memzone free - memzone4\n");
951 static int memzone_calk_called;
952 static void memzone_walk_clb(const struct rte_memzone *mz __rte_unused,
953 void *arg __rte_unused)
955 memzone_calk_called = 1;
961 printf("test basic memzone API\n");
962 if (test_memzone_basic() < 0)
965 printf("test free memzone\n");
966 if (test_memzone_free() < 0)
969 printf("test reserving memzone with bigger size than the maximum\n");
970 if (test_memzone_reserving_zone_size_bigger_than_the_maximum() < 0)
973 printf("test memzone_reserve flags\n");
974 if (test_memzone_reserve_flags() < 0)
977 printf("test alignment for memzone_reserve\n");
978 if (test_memzone_aligned() < 0)
981 printf("test boundary alignment for memzone_reserve\n");
982 if (test_memzone_bounded() < 0)
985 printf("test invalid alignment for memzone_reserve\n");
986 if (test_memzone_invalid_alignment() < 0)
989 printf("test reserving the largest size memzone possible\n");
990 if (test_memzone_reserve_max() < 0)
993 printf("test reserving the largest size aligned memzone possible\n");
994 if (test_memzone_reserve_max_aligned() < 0)
997 printf("check memzone cleanup\n");
998 rte_memzone_walk(memzone_walk_clb, NULL);
999 if (memzone_calk_called) {
1000 printf("there are some memzones left after test\n");
1001 rte_memzone_dump(stdout);
1008 REGISTER_TEST_COMMAND(memzone_autotest, test_memzone);