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");
285 if (hugepage_2MB_avail && hugepage_1GB_avail) {
286 mz = rte_memzone_reserve("flag_zone_2M_HINT", size, SOCKET_ID_ANY,
287 RTE_MEMZONE_2MB|RTE_MEMZONE_1GB);
289 printf("BOTH SIZES SET\n");
292 if (mz->hugepage_sz != RTE_PGSIZE_1G &&
293 mz->hugepage_sz != RTE_PGSIZE_2M) {
294 printf("Wrong size when both sizes set\n");
297 if (rte_memzone_free(mz)) {
298 printf("Fail memzone free\n");
304 * This option is for IBM Power. If 16MB pages available, check
305 * that a small memzone is correctly reserved from 16MB huge pages
306 * when requested by the RTE_MEMZONE_16MB flag. Also check that
307 * RTE_MEMZONE_SIZE_HINT_ONLY flag only defaults to an available
308 * page size (i.e 16GB ) when 16MB pages are unavailable.
310 if (hugepage_16MB_avail) {
311 mz = rte_memzone_reserve("flag_zone_16M", size, SOCKET_ID_ANY,
314 printf("MEMZONE FLAG 16MB\n");
317 if (mz->hugepage_sz != RTE_PGSIZE_16M) {
318 printf("hugepage_sz not equal 16M\n");
321 if (rte_memzone_free(mz)) {
322 printf("Fail memzone free\n");
326 mz = rte_memzone_reserve("flag_zone_16M_HINT", size,
327 SOCKET_ID_ANY, RTE_MEMZONE_16MB|RTE_MEMZONE_SIZE_HINT_ONLY);
329 printf("MEMZONE FLAG 2MB\n");
332 if (mz->hugepage_sz != RTE_PGSIZE_16M) {
333 printf("hugepage_sz not equal 16M\n");
336 if (rte_memzone_free(mz)) {
337 printf("Fail memzone free\n");
341 /* Check if 1GB huge pages are unavailable, that function fails
342 * unless HINT flag is indicated
344 if (!hugepage_16GB_avail) {
345 mz = rte_memzone_reserve("flag_zone_16G_HINT", size,
347 RTE_MEMZONE_16GB|RTE_MEMZONE_SIZE_HINT_ONLY);
349 printf("MEMZONE FLAG 16GB & HINT\n");
352 if (mz->hugepage_sz != RTE_PGSIZE_16M) {
353 printf("hugepage_sz not equal 16M\n");
356 if (rte_memzone_free(mz)) {
357 printf("Fail memzone free\n");
361 mz = rte_memzone_reserve("flag_zone_16G", size,
362 SOCKET_ID_ANY, RTE_MEMZONE_16GB);
364 printf("MEMZONE FLAG 16GB\n");
369 /*As with 16MB tests above for 16GB huge page requests*/
370 if (hugepage_16GB_avail) {
371 mz = rte_memzone_reserve("flag_zone_16G", size, SOCKET_ID_ANY,
374 printf("MEMZONE FLAG 16GB\n");
377 if (mz->hugepage_sz != RTE_PGSIZE_16G) {
378 printf("hugepage_sz not equal 16G\n");
381 if (rte_memzone_free(mz)) {
382 printf("Fail memzone free\n");
386 mz = rte_memzone_reserve("flag_zone_16G_HINT", size,
387 SOCKET_ID_ANY, RTE_MEMZONE_16GB|RTE_MEMZONE_SIZE_HINT_ONLY);
389 printf("MEMZONE FLAG 16GB\n");
392 if (mz->hugepage_sz != RTE_PGSIZE_16G) {
393 printf("hugepage_sz not equal 16G\n");
396 if (rte_memzone_free(mz)) {
397 printf("Fail memzone free\n");
401 /* Check if 1GB huge pages are unavailable, that function fails
402 * unless HINT flag is indicated
404 if (!hugepage_16MB_avail) {
405 mz = rte_memzone_reserve("flag_zone_16M_HINT", size,
407 RTE_MEMZONE_16MB|RTE_MEMZONE_SIZE_HINT_ONLY);
409 printf("MEMZONE FLAG 16MB & HINT\n");
412 if (mz->hugepage_sz != RTE_PGSIZE_16G) {
413 printf("hugepage_sz not equal 16G\n");
416 if (rte_memzone_free(mz)) {
417 printf("Fail memzone free\n");
420 mz = rte_memzone_reserve("flag_zone_16M", size,
421 SOCKET_ID_ANY, RTE_MEMZONE_16MB);
423 printf("MEMZONE FLAG 16MB\n");
428 if (hugepage_16MB_avail && hugepage_16GB_avail) {
429 mz = rte_memzone_reserve("flag_zone_16M_HINT", size,
431 RTE_MEMZONE_16MB|RTE_MEMZONE_16GB);
433 printf("BOTH SIZES SET\n");
436 if (mz->hugepage_sz != RTE_PGSIZE_16G &&
437 mz->hugepage_sz != RTE_PGSIZE_16M) {
438 printf("Wrong size when both sizes set\n");
441 if (rte_memzone_free(mz)) {
442 printf("Fail memzone free\n");
451 /* Find the heap with the greatest free block size */
453 find_max_block_free_size(const unsigned _align)
455 struct rte_malloc_socket_stats stats;
456 unsigned i, align = _align;
459 for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
460 rte_malloc_get_socket_stats(i, &stats);
461 if (stats.greatest_free_size > len)
462 len = stats.greatest_free_size;
465 if (align < RTE_CACHE_LINE_SIZE)
466 align = RTE_CACHE_LINE_ROUNDUP(align+1);
468 if (len <= MALLOC_ELEM_OVERHEAD + align)
471 return len - MALLOC_ELEM_OVERHEAD - align;
475 test_memzone_reserve_max(void)
477 const struct rte_memzone *mz;
480 maxlen = find_max_block_free_size(0);
483 printf("There is no space left!\n");
487 mz = rte_memzone_reserve("max_zone", 0, SOCKET_ID_ANY, 0);
489 printf("Failed to reserve a big chunk of memory - %s\n",
490 rte_strerror(rte_errno));
491 rte_dump_physmem_layout(stdout);
492 rte_memzone_dump(stdout);
496 if (mz->len != maxlen) {
497 printf("Memzone reserve with 0 size did not return bigest block\n");
498 printf("Expected size = %zu, actual size = %zu\n", maxlen, mz->len);
499 rte_dump_physmem_layout(stdout);
500 rte_memzone_dump(stdout);
504 if (rte_memzone_free(mz)) {
505 printf("Fail memzone free\n");
513 test_memzone_reserve_max_aligned(void)
515 const struct rte_memzone *mz;
518 /* random alignment */
519 rte_srand((unsigned)rte_rdtsc());
520 const unsigned align = 1 << ((rte_rand() % 8) + 5); /* from 128 up to 4k alignment */
522 maxlen = find_max_block_free_size(align);
525 printf("There is no space left for biggest %u-aligned memzone!\n", align);
529 mz = rte_memzone_reserve_aligned("max_zone_aligned", 0,
530 SOCKET_ID_ANY, 0, align);
532 printf("Failed to reserve a big chunk of memory - %s\n",
533 rte_strerror(rte_errno));
534 rte_dump_physmem_layout(stdout);
535 rte_memzone_dump(stdout);
539 if (mz->len != maxlen) {
540 printf("Memzone reserve with 0 size and alignment %u did not return"
541 " bigest block\n", align);
542 printf("Expected size = %zu, actual size = %zu\n",
544 rte_dump_physmem_layout(stdout);
545 rte_memzone_dump(stdout);
549 if (rte_memzone_free(mz)) {
550 printf("Fail memzone free\n");
558 test_memzone_aligned(void)
560 const struct rte_memzone *memzone_aligned_32;
561 const struct rte_memzone *memzone_aligned_128;
562 const struct rte_memzone *memzone_aligned_256;
563 const struct rte_memzone *memzone_aligned_512;
564 const struct rte_memzone *memzone_aligned_1024;
566 /* memzone that should automatically be adjusted to align on 64 bytes */
567 memzone_aligned_32 = rte_memzone_reserve_aligned("aligned_32", 100,
568 SOCKET_ID_ANY, 0, 32);
570 /* memzone that is supposed to be aligned on a 128 byte boundary */
571 memzone_aligned_128 = rte_memzone_reserve_aligned("aligned_128", 100,
572 SOCKET_ID_ANY, 0, 128);
574 /* memzone that is supposed to be aligned on a 256 byte boundary */
575 memzone_aligned_256 = rte_memzone_reserve_aligned("aligned_256", 100,
576 SOCKET_ID_ANY, 0, 256);
578 /* memzone that is supposed to be aligned on a 512 byte boundary */
579 memzone_aligned_512 = rte_memzone_reserve_aligned("aligned_512", 100,
580 SOCKET_ID_ANY, 0, 512);
582 /* memzone that is supposed to be aligned on a 1024 byte boundary */
583 memzone_aligned_1024 = rte_memzone_reserve_aligned("aligned_1024", 100,
584 SOCKET_ID_ANY, 0, 1024);
586 printf("check alignments and lengths\n");
587 if (memzone_aligned_32 == NULL) {
588 printf("Unable to reserve 64-byte aligned memzone!\n");
591 if ((memzone_aligned_32->iova & RTE_CACHE_LINE_MASK) != 0)
593 if (((uintptr_t) memzone_aligned_32->addr & RTE_CACHE_LINE_MASK) != 0)
595 if ((memzone_aligned_32->len & RTE_CACHE_LINE_MASK) != 0)
598 if (memzone_aligned_128 == NULL) {
599 printf("Unable to reserve 128-byte aligned memzone!\n");
602 if ((memzone_aligned_128->iova & 127) != 0)
604 if (((uintptr_t) memzone_aligned_128->addr & 127) != 0)
606 if ((memzone_aligned_128->len & RTE_CACHE_LINE_MASK) != 0)
609 if (memzone_aligned_256 == NULL) {
610 printf("Unable to reserve 256-byte aligned memzone!\n");
613 if ((memzone_aligned_256->iova & 255) != 0)
615 if (((uintptr_t) memzone_aligned_256->addr & 255) != 0)
617 if ((memzone_aligned_256->len & RTE_CACHE_LINE_MASK) != 0)
620 if (memzone_aligned_512 == NULL) {
621 printf("Unable to reserve 512-byte aligned memzone!\n");
624 if ((memzone_aligned_512->iova & 511) != 0)
626 if (((uintptr_t) memzone_aligned_512->addr & 511) != 0)
628 if ((memzone_aligned_512->len & RTE_CACHE_LINE_MASK) != 0)
631 if (memzone_aligned_1024 == NULL) {
632 printf("Unable to reserve 1024-byte aligned memzone!\n");
635 if ((memzone_aligned_1024->iova & 1023) != 0)
637 if (((uintptr_t) memzone_aligned_1024->addr & 1023) != 0)
639 if ((memzone_aligned_1024->len & RTE_CACHE_LINE_MASK) != 0)
642 /* check that zones don't overlap */
643 printf("check overlapping\n");
644 if (is_memory_overlap(memzone_aligned_32->iova, memzone_aligned_32->len,
645 memzone_aligned_128->iova, memzone_aligned_128->len))
647 if (is_memory_overlap(memzone_aligned_32->iova, memzone_aligned_32->len,
648 memzone_aligned_256->iova, memzone_aligned_256->len))
650 if (is_memory_overlap(memzone_aligned_32->iova, memzone_aligned_32->len,
651 memzone_aligned_512->iova, memzone_aligned_512->len))
653 if (is_memory_overlap(memzone_aligned_32->iova, memzone_aligned_32->len,
654 memzone_aligned_1024->iova, memzone_aligned_1024->len))
656 if (is_memory_overlap(memzone_aligned_128->iova, memzone_aligned_128->len,
657 memzone_aligned_256->iova, memzone_aligned_256->len))
659 if (is_memory_overlap(memzone_aligned_128->iova, memzone_aligned_128->len,
660 memzone_aligned_512->iova, memzone_aligned_512->len))
662 if (is_memory_overlap(memzone_aligned_128->iova, memzone_aligned_128->len,
663 memzone_aligned_1024->iova, memzone_aligned_1024->len))
665 if (is_memory_overlap(memzone_aligned_256->iova, memzone_aligned_256->len,
666 memzone_aligned_512->iova, memzone_aligned_512->len))
668 if (is_memory_overlap(memzone_aligned_256->iova, memzone_aligned_256->len,
669 memzone_aligned_1024->iova, memzone_aligned_1024->len))
671 if (is_memory_overlap(memzone_aligned_512->iova, memzone_aligned_512->len,
672 memzone_aligned_1024->iova, memzone_aligned_1024->len))
675 /* free all used zones */
676 if (rte_memzone_free(memzone_aligned_32)) {
677 printf("Fail memzone free\n");
680 if (rte_memzone_free(memzone_aligned_128)) {
681 printf("Fail memzone free\n");
684 if (rte_memzone_free(memzone_aligned_256)) {
685 printf("Fail memzone free\n");
688 if (rte_memzone_free(memzone_aligned_512)) {
689 printf("Fail memzone free\n");
692 if (rte_memzone_free(memzone_aligned_1024)) {
693 printf("Fail memzone free\n");
700 check_memzone_bounded(const char *name, uint32_t len, uint32_t align,
703 const struct rte_memzone *mz;
706 bmask = ~((rte_iova_t)bound - 1);
708 if ((mz = rte_memzone_reserve_bounded(name, len, SOCKET_ID_ANY, 0,
709 align, bound)) == NULL) {
710 printf("%s(%s): memzone creation failed\n",
715 if ((mz->iova & ((rte_iova_t)align - 1)) != 0) {
716 printf("%s(%s): invalid phys addr alignment\n",
721 if (((uintptr_t) mz->addr & ((uintptr_t)align - 1)) != 0) {
722 printf("%s(%s): invalid virtual addr alignment\n",
727 if ((mz->len & RTE_CACHE_LINE_MASK) != 0 || mz->len < len ||
728 mz->len < RTE_CACHE_LINE_SIZE) {
729 printf("%s(%s): invalid length\n",
734 if ((mz->iova & bmask) !=
735 ((mz->iova + mz->len - 1) & bmask)) {
736 printf("%s(%s): invalid memzone boundary %u crossed\n",
737 __func__, mz->name, bound);
741 if (rte_memzone_free(mz)) {
742 printf("Fail memzone free\n");
750 test_memzone_bounded(void)
752 const struct rte_memzone *memzone_err;
756 /* should fail as boundary is not power of two */
757 name = "bounded_error_31";
758 if ((memzone_err = rte_memzone_reserve_bounded(name,
759 100, SOCKET_ID_ANY, 0, 32, UINT32_MAX)) != NULL) {
760 printf("%s(%s)created a memzone with invalid boundary "
761 "conditions\n", __func__, memzone_err->name);
765 /* should fail as len is greater then boundary */
766 name = "bounded_error_32";
767 if ((memzone_err = rte_memzone_reserve_bounded(name,
768 100, SOCKET_ID_ANY, 0, 32, 32)) != NULL) {
769 printf("%s(%s)created a memzone with invalid boundary "
770 "conditions\n", __func__, memzone_err->name);
774 if ((rc = check_memzone_bounded("bounded_128", 100, 128, 128)) != 0)
777 if ((rc = check_memzone_bounded("bounded_256", 100, 256, 128)) != 0)
780 if ((rc = check_memzone_bounded("bounded_1K", 100, 64, 1024)) != 0)
783 if ((rc = check_memzone_bounded("bounded_1K_MAX", 0, 64, 1024)) != 0)
790 test_memzone_free(void)
792 const struct rte_memzone *mz[RTE_MAX_MEMZONE + 1];
796 mz[0] = rte_memzone_reserve("tempzone0", 2000, SOCKET_ID_ANY, 0);
797 mz[1] = rte_memzone_reserve("tempzone1", 4000, SOCKET_ID_ANY, 0);
801 if (!rte_memzone_lookup("tempzone0"))
803 if (!rte_memzone_lookup("tempzone1"))
806 if (rte_memzone_free(mz[0])) {
807 printf("Fail memzone free - tempzone0\n");
810 if (rte_memzone_lookup("tempzone0")) {
811 printf("Found previously free memzone - tempzone0\n");
814 mz[2] = rte_memzone_reserve("tempzone2", 2000, SOCKET_ID_ANY, 0);
817 printf("tempzone2 should have gotten the free entry from tempzone0\n");
820 if (rte_memzone_free(mz[2])) {
821 printf("Fail memzone free - tempzone2\n");
824 if (rte_memzone_lookup("tempzone2")) {
825 printf("Found previously free memzone - tempzone2\n");
828 if (rte_memzone_free(mz[1])) {
829 printf("Fail memzone free - tempzone1\n");
832 if (rte_memzone_lookup("tempzone1")) {
833 printf("Found previously free memzone - tempzone1\n");
839 snprintf(name, sizeof(name), "tempzone%u", i);
840 mz[i] = rte_memzone_reserve(name, 1, SOCKET_ID_ANY, 0);
841 } while (mz[i++] != NULL);
843 if (rte_memzone_free(mz[0])) {
844 printf("Fail memzone free - tempzone0\n");
847 mz[0] = rte_memzone_reserve("tempzone0new", 0, SOCKET_ID_ANY, 0);
850 printf("Fail to create memzone - tempzone0new - when MAX memzones were "
851 "created and one was free\n");
855 for (i = i - 2; i >= 0; i--) {
856 if (rte_memzone_free(mz[i])) {
857 printf("Fail memzone free - tempzone%d\n", i);
866 test_memzone_basic(void)
868 const struct rte_memzone *memzone1;
869 const struct rte_memzone *memzone2;
870 const struct rte_memzone *memzone3;
871 const struct rte_memzone *memzone4;
872 const struct rte_memzone *mz;
874 memzone1 = rte_memzone_reserve("testzone1", 100,
877 memzone2 = rte_memzone_reserve("testzone2", 1000,
880 memzone3 = rte_memzone_reserve("testzone3", 1000,
883 memzone4 = rte_memzone_reserve("testzone4", 1024,
886 /* memzone3 may be NULL if we don't have NUMA */
887 if (memzone1 == NULL || memzone2 == NULL || memzone4 == NULL)
890 rte_memzone_dump(stdout);
892 /* check cache-line alignments */
893 printf("check alignments and lengths\n");
895 if ((memzone1->iova & RTE_CACHE_LINE_MASK) != 0)
897 if ((memzone2->iova & RTE_CACHE_LINE_MASK) != 0)
899 if (memzone3 != NULL && (memzone3->iova & RTE_CACHE_LINE_MASK) != 0)
901 if ((memzone1->len & RTE_CACHE_LINE_MASK) != 0 || memzone1->len == 0)
903 if ((memzone2->len & RTE_CACHE_LINE_MASK) != 0 || memzone2->len == 0)
905 if (memzone3 != NULL && ((memzone3->len & RTE_CACHE_LINE_MASK) != 0 ||
908 if (memzone4->len != 1024)
911 /* check that zones don't overlap */
912 printf("check overlapping\n");
914 if (is_memory_overlap(memzone1->iova, memzone1->len,
915 memzone2->iova, memzone2->len))
917 if (memzone3 != NULL &&
918 is_memory_overlap(memzone1->iova, memzone1->len,
919 memzone3->iova, memzone3->len))
921 if (memzone3 != NULL &&
922 is_memory_overlap(memzone2->iova, memzone2->len,
923 memzone3->iova, memzone3->len))
926 printf("check socket ID\n");
928 /* memzone2 must be on socket id 0 and memzone3 on socket 1 */
929 if (memzone2->socket_id != 0)
931 if (memzone3 != NULL && memzone3->socket_id != 1)
934 printf("test zone lookup\n");
935 mz = rte_memzone_lookup("testzone1");
939 printf("test duplcate zone name\n");
940 mz = rte_memzone_reserve("testzone1", 100,
945 if (rte_memzone_free(memzone1)) {
946 printf("Fail memzone free - memzone1\n");
949 if (rte_memzone_free(memzone2)) {
950 printf("Fail memzone free - memzone2\n");
953 if (memzone3 && rte_memzone_free(memzone3)) {
954 printf("Fail memzone free - memzone3\n");
957 if (rte_memzone_free(memzone4)) {
958 printf("Fail memzone free - memzone4\n");
965 static int memzone_calk_called;
966 static void memzone_walk_clb(const struct rte_memzone *mz __rte_unused,
967 void *arg __rte_unused)
969 memzone_calk_called = 1;
975 printf("test basic memzone API\n");
976 if (test_memzone_basic() < 0)
979 printf("test free memzone\n");
980 if (test_memzone_free() < 0)
983 printf("test reserving memzone with bigger size than the maximum\n");
984 if (test_memzone_reserving_zone_size_bigger_than_the_maximum() < 0)
987 printf("test memzone_reserve flags\n");
988 if (test_memzone_reserve_flags() < 0)
991 printf("test alignment for memzone_reserve\n");
992 if (test_memzone_aligned() < 0)
995 printf("test boundary alignment for memzone_reserve\n");
996 if (test_memzone_bounded() < 0)
999 printf("test invalid alignment for memzone_reserve\n");
1000 if (test_memzone_invalid_alignment() < 0)
1003 printf("test reserving the largest size memzone possible\n");
1004 if (test_memzone_reserve_max() < 0)
1007 printf("test reserving the largest size aligned memzone possible\n");
1008 if (test_memzone_reserve_max_aligned() < 0)
1011 printf("check memzone cleanup\n");
1012 rte_memzone_walk(memzone_walk_clb, NULL);
1013 if (memzone_calk_called) {
1014 printf("there are some memzones left after test\n");
1015 rte_memzone_dump(stdout);
1022 REGISTER_TEST_COMMAND(memzone_autotest, test_memzone);