1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 #include <rte_random.h>
12 #include <rte_cycles.h>
13 #include <rte_memory.h>
14 #include <rte_memzone.h>
16 #include <rte_eal_memconfig.h>
17 #include <rte_common.h>
18 #include <rte_string_fns.h>
19 #include <rte_errno.h>
20 #include <rte_malloc.h>
21 #include "../../lib/librte_eal/common/malloc_elem.h"
29 * - Search for three reserved zones or reserve them if they do not exist:
31 * - One is on any socket id.
32 * - The second is on socket 0.
33 * - The last one is on socket 1 (if socket 1 exists).
35 * - Check that the zones exist.
37 * - Check that the zones are cache-aligned.
39 * - Check that zones do not overlap.
41 * - Check that the zones are on the correct socket id.
43 * - Check that a lookup of the first zone returns the same pointer.
45 * - Check that it is not possible to create another zone with the
46 * same name as an existing zone.
48 * - Check flags for specific huge page size reservation
51 #define TEST_MEMZONE_NAME(suffix) "MZ_TEST_" suffix
53 /* Test if memory overlaps: return 1 if true, or 0 if false. */
55 is_memory_overlap(rte_iova_t ptr1, size_t len1, rte_iova_t ptr2, size_t len2)
57 if (ptr2 >= ptr1 && (ptr2 - ptr1) < len1)
59 else if (ptr2 < ptr1 && (ptr1 - ptr2) < len2)
65 test_memzone_invalid_alignment(void)
67 const struct rte_memzone * mz;
69 mz = rte_memzone_lookup(TEST_MEMZONE_NAME("invalid_alignment"));
71 printf("Zone with invalid alignment has been reserved\n");
75 mz = rte_memzone_reserve_aligned(TEST_MEMZONE_NAME("invalid_alignment"),
76 100, SOCKET_ID_ANY, 0, 100);
78 printf("Zone with invalid alignment has been reserved\n");
85 test_memzone_reserving_zone_size_bigger_than_the_maximum(void)
87 const struct rte_memzone * mz;
89 mz = rte_memzone_lookup(
90 TEST_MEMZONE_NAME("zone_size_bigger_than_the_maximum"));
92 printf("zone_size_bigger_than_the_maximum has been reserved\n");
96 mz = rte_memzone_reserve(
97 TEST_MEMZONE_NAME("zone_size_bigger_than_the_maximum"),
98 (size_t)-1, SOCKET_ID_ANY, 0);
100 printf("It is impossible to reserve such big a memzone\n");
108 test_memzone_reserve_flags(void)
110 const struct rte_memzone *mz;
111 const struct rte_memseg *ms;
112 int hugepage_2MB_avail = 0;
113 int hugepage_1GB_avail = 0;
114 int hugepage_16MB_avail = 0;
115 int hugepage_16GB_avail = 0;
116 const size_t size = 100;
118 ms = rte_eal_get_physmem_layout();
119 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
120 if (ms[i].hugepage_sz == RTE_PGSIZE_2M)
121 hugepage_2MB_avail = 1;
122 if (ms[i].hugepage_sz == RTE_PGSIZE_1G)
123 hugepage_1GB_avail = 1;
124 if (ms[i].hugepage_sz == RTE_PGSIZE_16M)
125 hugepage_16MB_avail = 1;
126 if (ms[i].hugepage_sz == RTE_PGSIZE_16G)
127 hugepage_16GB_avail = 1;
129 /* Display the availability of 2MB ,1GB, 16MB, 16GB pages */
130 if (hugepage_2MB_avail)
131 printf("2MB Huge pages available\n");
132 if (hugepage_1GB_avail)
133 printf("1GB Huge pages available\n");
134 if (hugepage_16MB_avail)
135 printf("16MB Huge pages available\n");
136 if (hugepage_16GB_avail)
137 printf("16GB Huge pages available\n");
139 * If 2MB pages available, check that a small memzone is correctly
140 * reserved from 2MB huge pages when requested by the RTE_MEMZONE_2MB flag.
141 * Also check that RTE_MEMZONE_SIZE_HINT_ONLY flag only defaults to an
142 * available page size (i.e 1GB ) when 2MB pages are unavailable.
144 if (hugepage_2MB_avail) {
145 mz = rte_memzone_reserve(TEST_MEMZONE_NAME("flag_zone_2M"),
146 size, SOCKET_ID_ANY, RTE_MEMZONE_2MB);
148 printf("MEMZONE FLAG 2MB\n");
151 if (mz->hugepage_sz != RTE_PGSIZE_2M) {
152 printf("hugepage_sz not equal 2M\n");
155 if (rte_memzone_free(mz)) {
156 printf("Fail memzone free\n");
160 mz = rte_memzone_reserve(TEST_MEMZONE_NAME("flag_zone_2M_HINT"),
162 RTE_MEMZONE_2MB|RTE_MEMZONE_SIZE_HINT_ONLY);
164 printf("MEMZONE FLAG 2MB\n");
167 if (mz->hugepage_sz != RTE_PGSIZE_2M) {
168 printf("hugepage_sz not equal 2M\n");
171 if (rte_memzone_free(mz)) {
172 printf("Fail memzone free\n");
176 /* Check if 1GB huge pages are unavailable, that function fails unless
177 * HINT flag is indicated
179 if (!hugepage_1GB_avail) {
180 mz = rte_memzone_reserve(
181 TEST_MEMZONE_NAME("flag_zone_1G_HINT"),
183 RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY);
185 printf("MEMZONE FLAG 1GB & HINT\n");
188 if (mz->hugepage_sz != RTE_PGSIZE_2M) {
189 printf("hugepage_sz not equal 2M\n");
192 if (rte_memzone_free(mz)) {
193 printf("Fail memzone free\n");
197 mz = rte_memzone_reserve(
198 TEST_MEMZONE_NAME("flag_zone_1G"), size,
199 SOCKET_ID_ANY, RTE_MEMZONE_1GB);
201 printf("MEMZONE FLAG 1GB\n");
207 /*As with 2MB tests above for 1GB huge page requests*/
208 if (hugepage_1GB_avail) {
209 mz = rte_memzone_reserve(TEST_MEMZONE_NAME("flag_zone_1G"),
210 size, SOCKET_ID_ANY, RTE_MEMZONE_1GB);
212 printf("MEMZONE FLAG 1GB\n");
215 if (mz->hugepage_sz != RTE_PGSIZE_1G) {
216 printf("hugepage_sz not equal 1G\n");
219 if (rte_memzone_free(mz)) {
220 printf("Fail memzone free\n");
224 mz = rte_memzone_reserve(TEST_MEMZONE_NAME("flag_zone_1G_HINT"),
226 RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY);
228 printf("MEMZONE FLAG 1GB\n");
231 if (mz->hugepage_sz != RTE_PGSIZE_1G) {
232 printf("hugepage_sz not equal 1G\n");
235 if (rte_memzone_free(mz)) {
236 printf("Fail memzone free\n");
240 /* Check if 1GB huge pages are unavailable, that function fails unless
241 * HINT flag is indicated
243 if (!hugepage_2MB_avail) {
244 mz = rte_memzone_reserve(
245 TEST_MEMZONE_NAME("flag_zone_2M_HINT"),
247 RTE_MEMZONE_2MB|RTE_MEMZONE_SIZE_HINT_ONLY);
249 printf("MEMZONE FLAG 2MB & HINT\n");
252 if (mz->hugepage_sz != RTE_PGSIZE_1G) {
253 printf("hugepage_sz not equal 1G\n");
256 if (rte_memzone_free(mz)) {
257 printf("Fail memzone free\n");
260 mz = rte_memzone_reserve(
261 TEST_MEMZONE_NAME("flag_zone_2M"), size,
262 SOCKET_ID_ANY, RTE_MEMZONE_2MB);
264 printf("MEMZONE FLAG 2MB\n");
269 if (hugepage_2MB_avail && hugepage_1GB_avail) {
270 mz = rte_memzone_reserve(
271 TEST_MEMZONE_NAME("flag_zone_2M_HINT"),
273 RTE_MEMZONE_2MB|RTE_MEMZONE_1GB);
275 printf("BOTH SIZES SET\n");
278 if (mz->hugepage_sz != RTE_PGSIZE_1G &&
279 mz->hugepage_sz != RTE_PGSIZE_2M) {
280 printf("Wrong size when both sizes set\n");
283 if (rte_memzone_free(mz)) {
284 printf("Fail memzone free\n");
290 * This option is for IBM Power. If 16MB pages available, check
291 * that a small memzone is correctly reserved from 16MB huge pages
292 * when requested by the RTE_MEMZONE_16MB flag. Also check that
293 * RTE_MEMZONE_SIZE_HINT_ONLY flag only defaults to an available
294 * page size (i.e 16GB ) when 16MB pages are unavailable.
296 if (hugepage_16MB_avail) {
297 mz = rte_memzone_reserve(TEST_MEMZONE_NAME("flag_zone_16M"),
298 size, SOCKET_ID_ANY, RTE_MEMZONE_16MB);
300 printf("MEMZONE FLAG 16MB\n");
303 if (mz->hugepage_sz != RTE_PGSIZE_16M) {
304 printf("hugepage_sz not equal 16M\n");
307 if (rte_memzone_free(mz)) {
308 printf("Fail memzone free\n");
312 mz = rte_memzone_reserve(
313 TEST_MEMZONE_NAME("flag_zone_16M_HINT"), size,
315 RTE_MEMZONE_16MB|RTE_MEMZONE_SIZE_HINT_ONLY);
317 printf("MEMZONE FLAG 16MB\n");
320 if (mz->hugepage_sz != RTE_PGSIZE_16M) {
321 printf("hugepage_sz not equal 16M\n");
324 if (rte_memzone_free(mz)) {
325 printf("Fail memzone free\n");
329 /* Check if 1GB huge pages are unavailable, that function fails
330 * unless HINT flag is indicated
332 if (!hugepage_16GB_avail) {
333 mz = rte_memzone_reserve(
334 TEST_MEMZONE_NAME("flag_zone_16G_HINT"),
337 RTE_MEMZONE_SIZE_HINT_ONLY);
339 printf("MEMZONE FLAG 16GB & HINT\n");
342 if (mz->hugepage_sz != RTE_PGSIZE_16M) {
343 printf("hugepage_sz not equal 16M\n");
346 if (rte_memzone_free(mz)) {
347 printf("Fail memzone free\n");
351 mz = rte_memzone_reserve(
352 TEST_MEMZONE_NAME("flag_zone_16G"),
354 SOCKET_ID_ANY, RTE_MEMZONE_16GB);
356 printf("MEMZONE FLAG 16GB\n");
361 /*As with 16MB tests above for 16GB huge page requests*/
362 if (hugepage_16GB_avail) {
363 mz = rte_memzone_reserve(TEST_MEMZONE_NAME("flag_zone_16G"),
364 size, SOCKET_ID_ANY, RTE_MEMZONE_16GB);
366 printf("MEMZONE FLAG 16GB\n");
369 if (mz->hugepage_sz != RTE_PGSIZE_16G) {
370 printf("hugepage_sz not equal 16G\n");
373 if (rte_memzone_free(mz)) {
374 printf("Fail memzone free\n");
378 mz = rte_memzone_reserve(
379 TEST_MEMZONE_NAME("flag_zone_16G_HINT"), size,
381 RTE_MEMZONE_16GB|RTE_MEMZONE_SIZE_HINT_ONLY);
383 printf("MEMZONE FLAG 16GB\n");
386 if (mz->hugepage_sz != RTE_PGSIZE_16G) {
387 printf("hugepage_sz not equal 16G\n");
390 if (rte_memzone_free(mz)) {
391 printf("Fail memzone free\n");
395 /* Check if 1GB huge pages are unavailable, that function fails
396 * unless HINT flag is indicated
398 if (!hugepage_16MB_avail) {
399 mz = rte_memzone_reserve(
400 TEST_MEMZONE_NAME("flag_zone_16M_HINT"),
403 RTE_MEMZONE_SIZE_HINT_ONLY);
405 printf("MEMZONE FLAG 16MB & HINT\n");
408 if (mz->hugepage_sz != RTE_PGSIZE_16G) {
409 printf("hugepage_sz not equal 16G\n");
412 if (rte_memzone_free(mz)) {
413 printf("Fail memzone free\n");
416 mz = rte_memzone_reserve(
417 TEST_MEMZONE_NAME("flag_zone_16M"),
418 size, SOCKET_ID_ANY, RTE_MEMZONE_16MB);
420 printf("MEMZONE FLAG 16MB\n");
425 if (hugepage_16MB_avail && hugepage_16GB_avail) {
426 mz = rte_memzone_reserve(
427 TEST_MEMZONE_NAME("flag_zone_16M_HINT"),
429 RTE_MEMZONE_16MB|RTE_MEMZONE_16GB);
431 printf("BOTH SIZES SET\n");
434 if (mz->hugepage_sz != RTE_PGSIZE_16G &&
435 mz->hugepage_sz != RTE_PGSIZE_16M) {
436 printf("Wrong size when both sizes set\n");
439 if (rte_memzone_free(mz)) {
440 printf("Fail memzone free\n");
449 /* Find the heap with the greatest free block size */
451 find_max_block_free_size(const unsigned _align)
453 struct rte_malloc_socket_stats stats;
454 unsigned i, align = _align;
457 for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
458 rte_malloc_get_socket_stats(i, &stats);
459 if (stats.greatest_free_size > len)
460 len = stats.greatest_free_size;
463 if (align < RTE_CACHE_LINE_SIZE)
464 align = RTE_CACHE_LINE_ROUNDUP(align+1);
466 if (len <= MALLOC_ELEM_OVERHEAD + align)
469 return len - MALLOC_ELEM_OVERHEAD - align;
473 test_memzone_reserve_max(void)
475 const struct rte_memzone *mz;
478 maxlen = find_max_block_free_size(0);
481 printf("There is no space left!\n");
485 mz = rte_memzone_reserve(TEST_MEMZONE_NAME("max_zone"), 0,
488 printf("Failed to reserve a big chunk of memory - %s\n",
489 rte_strerror(rte_errno));
490 rte_dump_physmem_layout(stdout);
491 rte_memzone_dump(stdout);
495 if (mz->len != maxlen) {
496 printf("Memzone reserve with 0 size did not return bigest block\n");
497 printf("Expected size = %zu, actual size = %zu\n", maxlen, mz->len);
498 rte_dump_physmem_layout(stdout);
499 rte_memzone_dump(stdout);
503 if (rte_memzone_free(mz)) {
504 printf("Fail memzone free\n");
512 test_memzone_reserve_max_aligned(void)
514 const struct rte_memzone *mz;
517 /* random alignment */
518 rte_srand((unsigned)rte_rdtsc());
519 const unsigned align = 1 << ((rte_rand() % 8) + 5); /* from 128 up to 4k alignment */
521 maxlen = find_max_block_free_size(align);
524 printf("There is no space left for biggest %u-aligned memzone!\n", align);
528 mz = rte_memzone_reserve_aligned(TEST_MEMZONE_NAME("max_zone_aligned"),
529 0, SOCKET_ID_ANY, 0, align);
531 printf("Failed to reserve a big chunk of memory - %s\n",
532 rte_strerror(rte_errno));
533 rte_dump_physmem_layout(stdout);
534 rte_memzone_dump(stdout);
538 if (mz->len != maxlen) {
539 printf("Memzone reserve with 0 size and alignment %u did not return"
540 " bigest block\n", align);
541 printf("Expected size = %zu, actual size = %zu\n",
543 rte_dump_physmem_layout(stdout);
544 rte_memzone_dump(stdout);
548 if (rte_memzone_free(mz)) {
549 printf("Fail memzone free\n");
557 test_memzone_aligned(void)
559 const struct rte_memzone *memzone_aligned_32;
560 const struct rte_memzone *memzone_aligned_128;
561 const struct rte_memzone *memzone_aligned_256;
562 const struct rte_memzone *memzone_aligned_512;
563 const struct rte_memzone *memzone_aligned_1024;
565 /* memzone that should automatically be adjusted to align on 64 bytes */
566 memzone_aligned_32 = rte_memzone_reserve_aligned(
567 TEST_MEMZONE_NAME("aligned_32"), 100, SOCKET_ID_ANY, 0,
570 /* memzone that is supposed to be aligned on a 128 byte boundary */
571 memzone_aligned_128 = rte_memzone_reserve_aligned(
572 TEST_MEMZONE_NAME("aligned_128"), 100, SOCKET_ID_ANY, 0,
575 /* memzone that is supposed to be aligned on a 256 byte boundary */
576 memzone_aligned_256 = rte_memzone_reserve_aligned(
577 TEST_MEMZONE_NAME("aligned_256"), 100, SOCKET_ID_ANY, 0,
580 /* memzone that is supposed to be aligned on a 512 byte boundary */
581 memzone_aligned_512 = rte_memzone_reserve_aligned(
582 TEST_MEMZONE_NAME("aligned_512"), 100, SOCKET_ID_ANY, 0,
585 /* memzone that is supposed to be aligned on a 1024 byte boundary */
586 memzone_aligned_1024 = rte_memzone_reserve_aligned(
587 TEST_MEMZONE_NAME("aligned_1024"), 100, SOCKET_ID_ANY,
590 printf("check alignments and lengths\n");
591 if (memzone_aligned_32 == NULL) {
592 printf("Unable to reserve 64-byte aligned memzone!\n");
595 if ((memzone_aligned_32->iova & RTE_CACHE_LINE_MASK) != 0)
597 if (((uintptr_t) memzone_aligned_32->addr & RTE_CACHE_LINE_MASK) != 0)
599 if ((memzone_aligned_32->len & RTE_CACHE_LINE_MASK) != 0)
602 if (memzone_aligned_128 == NULL) {
603 printf("Unable to reserve 128-byte aligned memzone!\n");
606 if ((memzone_aligned_128->iova & 127) != 0)
608 if (((uintptr_t) memzone_aligned_128->addr & 127) != 0)
610 if ((memzone_aligned_128->len & RTE_CACHE_LINE_MASK) != 0)
613 if (memzone_aligned_256 == NULL) {
614 printf("Unable to reserve 256-byte aligned memzone!\n");
617 if ((memzone_aligned_256->iova & 255) != 0)
619 if (((uintptr_t) memzone_aligned_256->addr & 255) != 0)
621 if ((memzone_aligned_256->len & RTE_CACHE_LINE_MASK) != 0)
624 if (memzone_aligned_512 == NULL) {
625 printf("Unable to reserve 512-byte aligned memzone!\n");
628 if ((memzone_aligned_512->iova & 511) != 0)
630 if (((uintptr_t) memzone_aligned_512->addr & 511) != 0)
632 if ((memzone_aligned_512->len & RTE_CACHE_LINE_MASK) != 0)
635 if (memzone_aligned_1024 == NULL) {
636 printf("Unable to reserve 1024-byte aligned memzone!\n");
639 if ((memzone_aligned_1024->iova & 1023) != 0)
641 if (((uintptr_t) memzone_aligned_1024->addr & 1023) != 0)
643 if ((memzone_aligned_1024->len & RTE_CACHE_LINE_MASK) != 0)
646 /* check that zones don't overlap */
647 printf("check overlapping\n");
648 if (is_memory_overlap(memzone_aligned_32->iova, memzone_aligned_32->len,
649 memzone_aligned_128->iova, memzone_aligned_128->len))
651 if (is_memory_overlap(memzone_aligned_32->iova, memzone_aligned_32->len,
652 memzone_aligned_256->iova, memzone_aligned_256->len))
654 if (is_memory_overlap(memzone_aligned_32->iova, memzone_aligned_32->len,
655 memzone_aligned_512->iova, memzone_aligned_512->len))
657 if (is_memory_overlap(memzone_aligned_32->iova, memzone_aligned_32->len,
658 memzone_aligned_1024->iova, memzone_aligned_1024->len))
660 if (is_memory_overlap(memzone_aligned_128->iova, memzone_aligned_128->len,
661 memzone_aligned_256->iova, memzone_aligned_256->len))
663 if (is_memory_overlap(memzone_aligned_128->iova, memzone_aligned_128->len,
664 memzone_aligned_512->iova, memzone_aligned_512->len))
666 if (is_memory_overlap(memzone_aligned_128->iova, memzone_aligned_128->len,
667 memzone_aligned_1024->iova, memzone_aligned_1024->len))
669 if (is_memory_overlap(memzone_aligned_256->iova, memzone_aligned_256->len,
670 memzone_aligned_512->iova, memzone_aligned_512->len))
672 if (is_memory_overlap(memzone_aligned_256->iova, memzone_aligned_256->len,
673 memzone_aligned_1024->iova, memzone_aligned_1024->len))
675 if (is_memory_overlap(memzone_aligned_512->iova, memzone_aligned_512->len,
676 memzone_aligned_1024->iova, memzone_aligned_1024->len))
679 /* free all used zones */
680 if (rte_memzone_free(memzone_aligned_32)) {
681 printf("Fail memzone free\n");
684 if (rte_memzone_free(memzone_aligned_128)) {
685 printf("Fail memzone free\n");
688 if (rte_memzone_free(memzone_aligned_256)) {
689 printf("Fail memzone free\n");
692 if (rte_memzone_free(memzone_aligned_512)) {
693 printf("Fail memzone free\n");
696 if (rte_memzone_free(memzone_aligned_1024)) {
697 printf("Fail memzone free\n");
704 check_memzone_bounded(const char *name, uint32_t len, uint32_t align,
707 const struct rte_memzone *mz;
710 bmask = ~((rte_iova_t)bound - 1);
712 if ((mz = rte_memzone_reserve_bounded(name, len, SOCKET_ID_ANY, 0,
713 align, bound)) == NULL) {
714 printf("%s(%s): memzone creation failed\n",
719 if ((mz->iova & ((rte_iova_t)align - 1)) != 0) {
720 printf("%s(%s): invalid phys addr alignment\n",
725 if (((uintptr_t) mz->addr & ((uintptr_t)align - 1)) != 0) {
726 printf("%s(%s): invalid virtual addr alignment\n",
731 if ((mz->len & RTE_CACHE_LINE_MASK) != 0 || mz->len < len ||
732 mz->len < RTE_CACHE_LINE_SIZE) {
733 printf("%s(%s): invalid length\n",
738 if ((mz->iova & bmask) !=
739 ((mz->iova + mz->len - 1) & bmask)) {
740 printf("%s(%s): invalid memzone boundary %u crossed\n",
741 __func__, mz->name, bound);
745 if (rte_memzone_free(mz)) {
746 printf("Fail memzone free\n");
754 test_memzone_bounded(void)
756 const struct rte_memzone *memzone_err;
759 /* should fail as boundary is not power of two */
760 memzone_err = rte_memzone_reserve_bounded(
761 TEST_MEMZONE_NAME("bounded_error_31"), 100,
762 SOCKET_ID_ANY, 0, 32, UINT32_MAX);
763 if (memzone_err != NULL) {
764 printf("%s(%s)created a memzone with invalid boundary "
765 "conditions\n", __func__, memzone_err->name);
769 /* should fail as len is greater then boundary */
770 memzone_err = rte_memzone_reserve_bounded(
771 TEST_MEMZONE_NAME("bounded_error_32"), 100,
772 SOCKET_ID_ANY, 0, 32, 32);
773 if (memzone_err != NULL) {
774 printf("%s(%s)created a memzone with invalid boundary "
775 "conditions\n", __func__, memzone_err->name);
779 rc = check_memzone_bounded(TEST_MEMZONE_NAME("bounded_128"), 100, 128,
784 rc = check_memzone_bounded(TEST_MEMZONE_NAME("bounded_256"), 100, 256,
789 rc = check_memzone_bounded(TEST_MEMZONE_NAME("bounded_1K"), 100, 64,
794 rc = check_memzone_bounded(TEST_MEMZONE_NAME("bounded_1K_MAX"), 0, 64,
803 test_memzone_free(void)
805 const struct rte_memzone *mz[RTE_MAX_MEMZONE + 1];
809 mz[0] = rte_memzone_reserve(TEST_MEMZONE_NAME("tempzone0"), 2000,
811 mz[1] = rte_memzone_reserve(TEST_MEMZONE_NAME("tempzone1"), 4000,
816 if (!rte_memzone_lookup(TEST_MEMZONE_NAME("tempzone0")))
818 if (!rte_memzone_lookup(TEST_MEMZONE_NAME("tempzone1")))
821 if (rte_memzone_free(mz[0])) {
822 printf("Fail memzone free - tempzone0\n");
825 if (rte_memzone_lookup(TEST_MEMZONE_NAME("tempzone0"))) {
826 printf("Found previously free memzone - tempzone0\n");
829 mz[2] = rte_memzone_reserve(TEST_MEMZONE_NAME("tempzone2"), 2000,
833 printf("tempzone2 should have gotten the free entry from tempzone0\n");
836 if (rte_memzone_free(mz[2])) {
837 printf("Fail memzone free - tempzone2\n");
840 if (rte_memzone_lookup(TEST_MEMZONE_NAME("tempzone2"))) {
841 printf("Found previously free memzone - tempzone2\n");
844 if (rte_memzone_free(mz[1])) {
845 printf("Fail memzone free - tempzone1\n");
848 if (rte_memzone_lookup(TEST_MEMZONE_NAME("tempzone1"))) {
849 printf("Found previously free memzone - tempzone1\n");
855 snprintf(name, sizeof(name), TEST_MEMZONE_NAME("tempzone%u"),
857 mz[i] = rte_memzone_reserve(name, 1, SOCKET_ID_ANY, 0);
858 } while (mz[i++] != NULL);
860 if (rte_memzone_free(mz[0])) {
861 printf("Fail memzone free - tempzone0\n");
864 mz[0] = rte_memzone_reserve(TEST_MEMZONE_NAME("tempzone0new"), 0,
868 printf("Fail to create memzone - tempzone0new - when MAX memzones were "
869 "created and one was free\n");
873 for (i = i - 2; i >= 0; i--) {
874 if (rte_memzone_free(mz[i])) {
875 printf("Fail memzone free - tempzone%d\n", i);
884 test_memzone_basic(void)
886 const struct rte_memzone *memzone1;
887 const struct rte_memzone *memzone2;
888 const struct rte_memzone *memzone3;
889 const struct rte_memzone *memzone4;
890 const struct rte_memzone *mz;
891 int memzone_cnt_after, memzone_cnt_expected;
892 int memzone_cnt_before =
893 rte_eal_get_configuration()->mem_config->memzone_cnt;
895 memzone1 = rte_memzone_reserve(TEST_MEMZONE_NAME("testzone1"), 100,
898 memzone2 = rte_memzone_reserve(TEST_MEMZONE_NAME("testzone2"), 1000,
901 memzone3 = rte_memzone_reserve(TEST_MEMZONE_NAME("testzone3"), 1000,
904 memzone4 = rte_memzone_reserve(TEST_MEMZONE_NAME("testzone4"), 1024,
907 /* memzone3 may be NULL if we don't have NUMA */
908 if (memzone1 == NULL || memzone2 == NULL || memzone4 == NULL)
911 /* check how many memzones we are expecting */
912 memzone_cnt_expected = memzone_cnt_before +
913 (memzone1 != NULL) + (memzone2 != NULL) +
914 (memzone3 != NULL) + (memzone4 != NULL);
917 rte_eal_get_configuration()->mem_config->memzone_cnt;
919 if (memzone_cnt_after != memzone_cnt_expected)
923 rte_memzone_dump(stdout);
925 /* check cache-line alignments */
926 printf("check alignments and lengths\n");
928 if ((memzone1->iova & RTE_CACHE_LINE_MASK) != 0)
930 if ((memzone2->iova & RTE_CACHE_LINE_MASK) != 0)
932 if (memzone3 != NULL && (memzone3->iova & RTE_CACHE_LINE_MASK) != 0)
934 if ((memzone1->len & RTE_CACHE_LINE_MASK) != 0 || memzone1->len == 0)
936 if ((memzone2->len & RTE_CACHE_LINE_MASK) != 0 || memzone2->len == 0)
938 if (memzone3 != NULL && ((memzone3->len & RTE_CACHE_LINE_MASK) != 0 ||
941 if (memzone4->len != 1024)
944 /* check that zones don't overlap */
945 printf("check overlapping\n");
947 if (is_memory_overlap(memzone1->iova, memzone1->len,
948 memzone2->iova, memzone2->len))
950 if (memzone3 != NULL &&
951 is_memory_overlap(memzone1->iova, memzone1->len,
952 memzone3->iova, memzone3->len))
954 if (memzone3 != NULL &&
955 is_memory_overlap(memzone2->iova, memzone2->len,
956 memzone3->iova, memzone3->len))
959 printf("check socket ID\n");
961 /* memzone2 must be on socket id 0 and memzone3 on socket 1 */
962 if (memzone2->socket_id != 0)
964 if (memzone3 != NULL && memzone3->socket_id != 1)
967 printf("test zone lookup\n");
968 mz = rte_memzone_lookup(TEST_MEMZONE_NAME("testzone1"));
972 printf("test duplcate zone name\n");
973 mz = rte_memzone_reserve(TEST_MEMZONE_NAME("testzone1"), 100,
978 if (rte_memzone_free(memzone1)) {
979 printf("Fail memzone free - memzone1\n");
982 if (rte_memzone_free(memzone2)) {
983 printf("Fail memzone free - memzone2\n");
986 if (memzone3 && rte_memzone_free(memzone3)) {
987 printf("Fail memzone free - memzone3\n");
990 if (rte_memzone_free(memzone4)) {
991 printf("Fail memzone free - memzone4\n");
996 rte_eal_get_configuration()->mem_config->memzone_cnt;
997 if (memzone_cnt_after != memzone_cnt_before)
1003 static int test_memzones_left;
1004 static int memzone_walk_cnt;
1005 static void memzone_walk_clb(const struct rte_memzone *mz,
1006 void *arg __rte_unused)
1009 if (!strncmp(TEST_MEMZONE_NAME(""), mz->name, RTE_MEMZONE_NAMESIZE))
1010 test_memzones_left++;
1016 /* take note of how many memzones were allocated before running */
1017 int memzone_cnt = rte_eal_get_configuration()->mem_config->memzone_cnt;
1019 printf("test basic memzone API\n");
1020 if (test_memzone_basic() < 0)
1023 printf("test free memzone\n");
1024 if (test_memzone_free() < 0)
1027 printf("test reserving memzone with bigger size than the maximum\n");
1028 if (test_memzone_reserving_zone_size_bigger_than_the_maximum() < 0)
1031 printf("test memzone_reserve flags\n");
1032 if (test_memzone_reserve_flags() < 0)
1035 printf("test alignment for memzone_reserve\n");
1036 if (test_memzone_aligned() < 0)
1039 printf("test boundary alignment for memzone_reserve\n");
1040 if (test_memzone_bounded() < 0)
1043 printf("test invalid alignment for memzone_reserve\n");
1044 if (test_memzone_invalid_alignment() < 0)
1047 printf("test reserving the largest size memzone possible\n");
1048 if (test_memzone_reserve_max() < 0)
1051 printf("test reserving the largest size aligned memzone possible\n");
1052 if (test_memzone_reserve_max_aligned() < 0)
1055 printf("check memzone cleanup\n");
1056 memzone_walk_cnt = 0;
1057 test_memzones_left = 0;
1058 rte_memzone_walk(memzone_walk_clb, NULL);
1059 if (memzone_walk_cnt != memzone_cnt || test_memzones_left > 0) {
1060 printf("there are some memzones left after test\n");
1061 rte_memzone_dump(stdout);
1068 REGISTER_TEST_COMMAND(memzone_autotest, test_memzone);