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"
22 #include "../../lib/librte_eal/common/eal_memcfg.h"
30 * - Search for three reserved zones or reserve them if they do not exist:
32 * - One is on any socket id.
33 * - The second is on socket 0.
34 * - The last one is on socket 1 (if socket 1 exists).
36 * - Check that the zones exist.
38 * - Check that the zones are cache-aligned.
40 * - Check that zones do not overlap.
42 * - Check that the zones are on the correct socket id.
44 * - Check that a lookup of the first zone returns the same pointer.
46 * - Check that it is not possible to create another zone with the
47 * same name as an existing zone.
49 * - Check flags for specific huge page size reservation
52 #define TEST_MEMZONE_NAME(suffix) "MZ_TEST_" suffix
54 /* Test if memory overlaps: return 1 if true, or 0 if false. */
56 is_memory_overlap(rte_iova_t ptr1, size_t len1, rte_iova_t ptr2, size_t len2)
58 if (ptr2 >= ptr1 && (ptr2 - ptr1) < len1)
60 else if (ptr2 < ptr1 && (ptr1 - ptr2) < len2)
66 test_memzone_invalid_alignment(void)
68 const struct rte_memzone * mz;
70 mz = rte_memzone_lookup(TEST_MEMZONE_NAME("invalid_alignment"));
72 printf("Zone with invalid alignment has been reserved\n");
76 mz = rte_memzone_reserve_aligned(TEST_MEMZONE_NAME("invalid_alignment"),
77 100, SOCKET_ID_ANY, 0, 100);
79 printf("Zone with invalid alignment has been reserved\n");
86 test_memzone_reserving_zone_size_bigger_than_the_maximum(void)
88 const struct rte_memzone * mz;
90 mz = rte_memzone_lookup(
91 TEST_MEMZONE_NAME("zone_size_bigger_than_the_maximum"));
93 printf("zone_size_bigger_than_the_maximum has been reserved\n");
97 mz = rte_memzone_reserve(
98 TEST_MEMZONE_NAME("zone_size_bigger_than_the_maximum"),
99 (size_t)-1, SOCKET_ID_ANY, 0);
101 printf("It is impossible to reserve such big a memzone\n");
109 int hugepage_2MB_avail;
110 int hugepage_1GB_avail;
111 int hugepage_16MB_avail;
112 int hugepage_16GB_avail;
115 find_available_pagesz(const struct rte_memseg_list *msl, void *arg)
117 struct walk_arg *wa = arg;
122 if (msl->page_sz == RTE_PGSIZE_2M)
123 wa->hugepage_2MB_avail = 1;
124 if (msl->page_sz == RTE_PGSIZE_1G)
125 wa->hugepage_1GB_avail = 1;
126 if (msl->page_sz == RTE_PGSIZE_16M)
127 wa->hugepage_16MB_avail = 1;
128 if (msl->page_sz == RTE_PGSIZE_16G)
129 wa->hugepage_16GB_avail = 1;
135 test_memzone_reserve_flags(void)
137 const struct rte_memzone *mz;
139 int hugepage_2MB_avail, hugepage_1GB_avail;
140 int hugepage_16MB_avail, hugepage_16GB_avail;
141 const size_t size = 100;
143 memset(&wa, 0, sizeof(wa));
145 rte_memseg_list_walk(find_available_pagesz, &wa);
147 hugepage_2MB_avail = wa.hugepage_2MB_avail;
148 hugepage_1GB_avail = wa.hugepage_1GB_avail;
149 hugepage_16MB_avail = wa.hugepage_16MB_avail;
150 hugepage_16GB_avail = wa.hugepage_16GB_avail;
152 /* Display the availability of 2MB ,1GB, 16MB, 16GB pages */
153 if (hugepage_2MB_avail)
154 printf("2MB Huge pages available\n");
155 if (hugepage_1GB_avail)
156 printf("1GB Huge pages available\n");
157 if (hugepage_16MB_avail)
158 printf("16MB Huge pages available\n");
159 if (hugepage_16GB_avail)
160 printf("16GB Huge pages available\n");
162 * If 2MB pages available, check that a small memzone is correctly
163 * reserved from 2MB huge pages when requested by the RTE_MEMZONE_2MB flag.
164 * Also check that RTE_MEMZONE_SIZE_HINT_ONLY flag only defaults to an
165 * available page size (i.e 1GB ) when 2MB pages are unavailable.
167 if (hugepage_2MB_avail) {
168 mz = rte_memzone_reserve(TEST_MEMZONE_NAME("flag_zone_2M"),
169 size, SOCKET_ID_ANY, RTE_MEMZONE_2MB);
171 printf("MEMZONE FLAG 2MB\n");
174 if (mz->hugepage_sz != RTE_PGSIZE_2M) {
175 printf("hugepage_sz not equal 2M\n");
178 if (rte_memzone_free(mz)) {
179 printf("Fail memzone free\n");
183 mz = rte_memzone_reserve(TEST_MEMZONE_NAME("flag_zone_2M_HINT"),
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(
204 TEST_MEMZONE_NAME("flag_zone_1G_HINT"),
206 RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY);
208 printf("MEMZONE FLAG 1GB & HINT\n");
211 if (mz->hugepage_sz != RTE_PGSIZE_2M) {
212 printf("hugepage_sz not equal 2M\n");
215 if (rte_memzone_free(mz)) {
216 printf("Fail memzone free\n");
220 mz = rte_memzone_reserve(
221 TEST_MEMZONE_NAME("flag_zone_1G"), size,
222 SOCKET_ID_ANY, RTE_MEMZONE_1GB);
224 printf("MEMZONE FLAG 1GB\n");
230 /*As with 2MB tests above for 1GB huge page requests*/
231 if (hugepage_1GB_avail) {
232 mz = rte_memzone_reserve(TEST_MEMZONE_NAME("flag_zone_1G"),
233 size, SOCKET_ID_ANY, RTE_MEMZONE_1GB);
235 printf("MEMZONE FLAG 1GB\n");
238 if (mz->hugepage_sz != RTE_PGSIZE_1G) {
239 printf("hugepage_sz not equal 1G\n");
242 if (rte_memzone_free(mz)) {
243 printf("Fail memzone free\n");
247 mz = rte_memzone_reserve(TEST_MEMZONE_NAME("flag_zone_1G_HINT"),
249 RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY);
251 printf("MEMZONE FLAG 1GB\n");
254 if (mz->hugepage_sz != RTE_PGSIZE_1G) {
255 printf("hugepage_sz not equal 1G\n");
258 if (rte_memzone_free(mz)) {
259 printf("Fail memzone free\n");
263 /* Check if 1GB huge pages are unavailable, that function fails unless
264 * HINT flag is indicated
266 if (!hugepage_2MB_avail) {
267 mz = rte_memzone_reserve(
268 TEST_MEMZONE_NAME("flag_zone_2M_HINT"),
270 RTE_MEMZONE_2MB|RTE_MEMZONE_SIZE_HINT_ONLY);
272 printf("MEMZONE FLAG 2MB & HINT\n");
275 if (mz->hugepage_sz != RTE_PGSIZE_1G) {
276 printf("hugepage_sz not equal 1G\n");
279 if (rte_memzone_free(mz)) {
280 printf("Fail memzone free\n");
283 mz = rte_memzone_reserve(
284 TEST_MEMZONE_NAME("flag_zone_2M"), size,
285 SOCKET_ID_ANY, RTE_MEMZONE_2MB);
287 printf("MEMZONE FLAG 2MB\n");
292 if (hugepage_2MB_avail && hugepage_1GB_avail) {
293 mz = rte_memzone_reserve(
294 TEST_MEMZONE_NAME("flag_zone_2M_HINT"),
296 RTE_MEMZONE_2MB|RTE_MEMZONE_1GB);
298 printf("BOTH SIZES SET\n");
301 if (mz->hugepage_sz != RTE_PGSIZE_1G &&
302 mz->hugepage_sz != RTE_PGSIZE_2M) {
303 printf("Wrong size when both sizes set\n");
306 if (rte_memzone_free(mz)) {
307 printf("Fail memzone free\n");
313 * This option is for IBM Power. If 16MB pages available, check
314 * that a small memzone is correctly reserved from 16MB huge pages
315 * when requested by the RTE_MEMZONE_16MB flag. Also check that
316 * RTE_MEMZONE_SIZE_HINT_ONLY flag only defaults to an available
317 * page size (i.e 16GB ) when 16MB pages are unavailable.
319 if (hugepage_16MB_avail) {
320 mz = rte_memzone_reserve(TEST_MEMZONE_NAME("flag_zone_16M"),
321 size, SOCKET_ID_ANY, RTE_MEMZONE_16MB);
323 printf("MEMZONE FLAG 16MB\n");
326 if (mz->hugepage_sz != RTE_PGSIZE_16M) {
327 printf("hugepage_sz not equal 16M\n");
330 if (rte_memzone_free(mz)) {
331 printf("Fail memzone free\n");
335 mz = rte_memzone_reserve(
336 TEST_MEMZONE_NAME("flag_zone_16M_HINT"), size,
338 RTE_MEMZONE_16MB|RTE_MEMZONE_SIZE_HINT_ONLY);
340 printf("MEMZONE FLAG 16MB\n");
343 if (mz->hugepage_sz != RTE_PGSIZE_16M) {
344 printf("hugepage_sz not equal 16M\n");
347 if (rte_memzone_free(mz)) {
348 printf("Fail memzone free\n");
352 /* Check if 1GB huge pages are unavailable, that function fails
353 * unless HINT flag is indicated
355 if (!hugepage_16GB_avail) {
356 mz = rte_memzone_reserve(
357 TEST_MEMZONE_NAME("flag_zone_16G_HINT"),
360 RTE_MEMZONE_SIZE_HINT_ONLY);
362 printf("MEMZONE FLAG 16GB & HINT\n");
365 if (mz->hugepage_sz != RTE_PGSIZE_16M) {
366 printf("hugepage_sz not equal 16M\n");
369 if (rte_memzone_free(mz)) {
370 printf("Fail memzone free\n");
374 mz = rte_memzone_reserve(
375 TEST_MEMZONE_NAME("flag_zone_16G"),
377 SOCKET_ID_ANY, RTE_MEMZONE_16GB);
379 printf("MEMZONE FLAG 16GB\n");
384 /*As with 16MB tests above for 16GB huge page requests*/
385 if (hugepage_16GB_avail) {
386 mz = rte_memzone_reserve(TEST_MEMZONE_NAME("flag_zone_16G"),
387 size, SOCKET_ID_ANY, RTE_MEMZONE_16GB);
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 mz = rte_memzone_reserve(
402 TEST_MEMZONE_NAME("flag_zone_16G_HINT"), size,
404 RTE_MEMZONE_16GB|RTE_MEMZONE_SIZE_HINT_ONLY);
406 printf("MEMZONE FLAG 16GB\n");
409 if (mz->hugepage_sz != RTE_PGSIZE_16G) {
410 printf("hugepage_sz not equal 16G\n");
413 if (rte_memzone_free(mz)) {
414 printf("Fail memzone free\n");
418 /* Check if 1GB huge pages are unavailable, that function fails
419 * unless HINT flag is indicated
421 if (!hugepage_16MB_avail) {
422 mz = rte_memzone_reserve(
423 TEST_MEMZONE_NAME("flag_zone_16M_HINT"),
426 RTE_MEMZONE_SIZE_HINT_ONLY);
428 printf("MEMZONE FLAG 16MB & HINT\n");
431 if (mz->hugepage_sz != RTE_PGSIZE_16G) {
432 printf("hugepage_sz not equal 16G\n");
435 if (rte_memzone_free(mz)) {
436 printf("Fail memzone free\n");
439 mz = rte_memzone_reserve(
440 TEST_MEMZONE_NAME("flag_zone_16M"),
441 size, SOCKET_ID_ANY, RTE_MEMZONE_16MB);
443 printf("MEMZONE FLAG 16MB\n");
448 if (hugepage_16MB_avail && hugepage_16GB_avail) {
449 mz = rte_memzone_reserve(
450 TEST_MEMZONE_NAME("flag_zone_16M_HINT"),
452 RTE_MEMZONE_16MB|RTE_MEMZONE_16GB);
454 printf("BOTH SIZES SET\n");
457 if (mz->hugepage_sz != RTE_PGSIZE_16G &&
458 mz->hugepage_sz != RTE_PGSIZE_16M) {
459 printf("Wrong size when both sizes set\n");
462 if (rte_memzone_free(mz)) {
463 printf("Fail memzone free\n");
472 /* Find the heap with the greatest free block size */
474 find_max_block_free_size(unsigned int align, unsigned int socket_id)
476 struct rte_malloc_socket_stats stats;
477 size_t len, overhead;
479 rte_malloc_get_socket_stats(socket_id, &stats);
481 len = stats.greatest_free_size;
482 overhead = MALLOC_ELEM_OVERHEAD;
487 align = RTE_CACHE_LINE_ROUNDUP(align);
493 return len - overhead;
497 test_memzone_reserve_max(void)
501 for (i = 0; i < rte_socket_count(); i++) {
502 const struct rte_memzone *mz;
506 socket = rte_socket_id_by_idx(i);
507 maxlen = find_max_block_free_size(0, socket);
510 printf("There is no space left!\n");
514 mz = rte_memzone_reserve(TEST_MEMZONE_NAME("max_zone"), 0,
517 printf("Failed to reserve a big chunk of memory - %s\n",
518 rte_strerror(rte_errno));
519 rte_dump_physmem_layout(stdout);
520 rte_memzone_dump(stdout);
524 if (mz->len != maxlen) {
525 printf("Memzone reserve with 0 size did not return bigest block\n");
526 printf("Expected size = %zu, actual size = %zu\n",
528 rte_dump_physmem_layout(stdout);
529 rte_memzone_dump(stdout);
533 if (rte_memzone_free(mz)) {
534 printf("Fail memzone free\n");
543 test_memzone_reserve_max_aligned(void)
547 for (i = 0; i < rte_socket_count(); i++) {
548 const struct rte_memzone *mz;
549 size_t maxlen, minlen = 0;
552 socket = rte_socket_id_by_idx(i);
554 /* random alignment */
555 rte_srand((unsigned int)rte_rdtsc());
556 const unsigned int align = 1 << ((rte_rand() % 8) + 5); /* from 128 up to 4k alignment */
558 /* memzone size may be between size and size - align */
559 minlen = find_max_block_free_size(align, socket);
560 maxlen = find_max_block_free_size(0, socket);
562 if (minlen == 0 || maxlen == 0) {
563 printf("There is no space left for biggest %u-aligned memzone!\n",
568 mz = rte_memzone_reserve_aligned(
569 TEST_MEMZONE_NAME("max_zone_aligned"),
570 0, socket, 0, align);
572 printf("Failed to reserve a big chunk of memory - %s\n",
573 rte_strerror(rte_errno));
574 rte_dump_physmem_layout(stdout);
575 rte_memzone_dump(stdout);
578 if (mz->addr != RTE_PTR_ALIGN(mz->addr, align)) {
579 printf("Memzone reserve with 0 size and alignment %u did not return aligned block\n",
581 rte_dump_physmem_layout(stdout);
582 rte_memzone_dump(stdout);
586 if (mz->len < minlen || mz->len > maxlen) {
587 printf("Memzone reserve with 0 size and alignment %u did not return"
588 " bigest block\n", align);
589 printf("Expected size = %zu-%zu, actual size = %zu\n",
590 minlen, maxlen, mz->len);
591 rte_dump_physmem_layout(stdout);
592 rte_memzone_dump(stdout);
596 if (rte_memzone_free(mz)) {
597 printf("Fail memzone free\n");
605 test_memzone_aligned(void)
607 const struct rte_memzone *memzone_aligned_32;
608 const struct rte_memzone *memzone_aligned_128;
609 const struct rte_memzone *memzone_aligned_256;
610 const struct rte_memzone *memzone_aligned_512;
611 const struct rte_memzone *memzone_aligned_1024;
613 /* memzone that should automatically be adjusted to align on 64 bytes */
614 memzone_aligned_32 = rte_memzone_reserve_aligned(
615 TEST_MEMZONE_NAME("aligned_32"), 100, SOCKET_ID_ANY, 0,
618 /* memzone that is supposed to be aligned on a 128 byte boundary */
619 memzone_aligned_128 = rte_memzone_reserve_aligned(
620 TEST_MEMZONE_NAME("aligned_128"), 100, SOCKET_ID_ANY, 0,
623 /* memzone that is supposed to be aligned on a 256 byte boundary */
624 memzone_aligned_256 = rte_memzone_reserve_aligned(
625 TEST_MEMZONE_NAME("aligned_256"), 100, SOCKET_ID_ANY, 0,
628 /* memzone that is supposed to be aligned on a 512 byte boundary */
629 memzone_aligned_512 = rte_memzone_reserve_aligned(
630 TEST_MEMZONE_NAME("aligned_512"), 100, SOCKET_ID_ANY, 0,
633 /* memzone that is supposed to be aligned on a 1024 byte boundary */
634 memzone_aligned_1024 = rte_memzone_reserve_aligned(
635 TEST_MEMZONE_NAME("aligned_1024"), 100, SOCKET_ID_ANY,
638 printf("check alignments and lengths\n");
639 if (memzone_aligned_32 == NULL) {
640 printf("Unable to reserve 64-byte aligned memzone!\n");
643 if ((memzone_aligned_32->iova & RTE_CACHE_LINE_MASK) != 0)
645 if (((uintptr_t) memzone_aligned_32->addr & RTE_CACHE_LINE_MASK) != 0)
647 if ((memzone_aligned_32->len & RTE_CACHE_LINE_MASK) != 0)
650 if (memzone_aligned_128 == NULL) {
651 printf("Unable to reserve 128-byte aligned memzone!\n");
654 if ((memzone_aligned_128->iova & 127) != 0)
656 if (((uintptr_t) memzone_aligned_128->addr & 127) != 0)
658 if ((memzone_aligned_128->len & RTE_CACHE_LINE_MASK) != 0)
661 if (memzone_aligned_256 == NULL) {
662 printf("Unable to reserve 256-byte aligned memzone!\n");
665 if ((memzone_aligned_256->iova & 255) != 0)
667 if (((uintptr_t) memzone_aligned_256->addr & 255) != 0)
669 if ((memzone_aligned_256->len & RTE_CACHE_LINE_MASK) != 0)
672 if (memzone_aligned_512 == NULL) {
673 printf("Unable to reserve 512-byte aligned memzone!\n");
676 if ((memzone_aligned_512->iova & 511) != 0)
678 if (((uintptr_t) memzone_aligned_512->addr & 511) != 0)
680 if ((memzone_aligned_512->len & RTE_CACHE_LINE_MASK) != 0)
683 if (memzone_aligned_1024 == NULL) {
684 printf("Unable to reserve 1024-byte aligned memzone!\n");
687 if ((memzone_aligned_1024->iova & 1023) != 0)
689 if (((uintptr_t) memzone_aligned_1024->addr & 1023) != 0)
691 if ((memzone_aligned_1024->len & RTE_CACHE_LINE_MASK) != 0)
694 /* check that zones don't overlap */
695 printf("check overlapping\n");
696 if (is_memory_overlap(memzone_aligned_32->iova, memzone_aligned_32->len,
697 memzone_aligned_128->iova, memzone_aligned_128->len))
699 if (is_memory_overlap(memzone_aligned_32->iova, memzone_aligned_32->len,
700 memzone_aligned_256->iova, memzone_aligned_256->len))
702 if (is_memory_overlap(memzone_aligned_32->iova, memzone_aligned_32->len,
703 memzone_aligned_512->iova, memzone_aligned_512->len))
705 if (is_memory_overlap(memzone_aligned_32->iova, memzone_aligned_32->len,
706 memzone_aligned_1024->iova, memzone_aligned_1024->len))
708 if (is_memory_overlap(memzone_aligned_128->iova, memzone_aligned_128->len,
709 memzone_aligned_256->iova, memzone_aligned_256->len))
711 if (is_memory_overlap(memzone_aligned_128->iova, memzone_aligned_128->len,
712 memzone_aligned_512->iova, memzone_aligned_512->len))
714 if (is_memory_overlap(memzone_aligned_128->iova, memzone_aligned_128->len,
715 memzone_aligned_1024->iova, memzone_aligned_1024->len))
717 if (is_memory_overlap(memzone_aligned_256->iova, memzone_aligned_256->len,
718 memzone_aligned_512->iova, memzone_aligned_512->len))
720 if (is_memory_overlap(memzone_aligned_256->iova, memzone_aligned_256->len,
721 memzone_aligned_1024->iova, memzone_aligned_1024->len))
723 if (is_memory_overlap(memzone_aligned_512->iova, memzone_aligned_512->len,
724 memzone_aligned_1024->iova, memzone_aligned_1024->len))
727 /* free all used zones */
728 if (rte_memzone_free(memzone_aligned_32)) {
729 printf("Fail memzone free\n");
732 if (rte_memzone_free(memzone_aligned_128)) {
733 printf("Fail memzone free\n");
736 if (rte_memzone_free(memzone_aligned_256)) {
737 printf("Fail memzone free\n");
740 if (rte_memzone_free(memzone_aligned_512)) {
741 printf("Fail memzone free\n");
744 if (rte_memzone_free(memzone_aligned_1024)) {
745 printf("Fail memzone free\n");
752 check_memzone_bounded(const char *name, uint32_t len, uint32_t align,
755 const struct rte_memzone *mz;
758 bmask = ~((rte_iova_t)bound - 1);
760 if ((mz = rte_memzone_reserve_bounded(name, len, SOCKET_ID_ANY, 0,
761 align, bound)) == NULL) {
762 printf("%s(%s): memzone creation failed\n",
767 if ((mz->iova & ((rte_iova_t)align - 1)) != 0) {
768 printf("%s(%s): invalid phys addr alignment\n",
773 if (((uintptr_t) mz->addr & ((uintptr_t)align - 1)) != 0) {
774 printf("%s(%s): invalid virtual addr alignment\n",
779 if ((mz->len & RTE_CACHE_LINE_MASK) != 0 || mz->len < len ||
780 mz->len < RTE_CACHE_LINE_SIZE) {
781 printf("%s(%s): invalid length\n",
786 if ((mz->iova & bmask) !=
787 ((mz->iova + mz->len - 1) & bmask)) {
788 printf("%s(%s): invalid memzone boundary %u crossed\n",
789 __func__, mz->name, bound);
793 if (rte_memzone_free(mz)) {
794 printf("Fail memzone free\n");
802 test_memzone_bounded(void)
804 const struct rte_memzone *memzone_err;
807 /* should fail as boundary is not power of two */
808 memzone_err = rte_memzone_reserve_bounded(
809 TEST_MEMZONE_NAME("bounded_error_31"), 100,
810 SOCKET_ID_ANY, 0, 32, UINT32_MAX);
811 if (memzone_err != NULL) {
812 printf("%s(%s)created a memzone with invalid boundary "
813 "conditions\n", __func__, memzone_err->name);
817 /* should fail as len is greater then boundary */
818 memzone_err = rte_memzone_reserve_bounded(
819 TEST_MEMZONE_NAME("bounded_error_32"), 100,
820 SOCKET_ID_ANY, 0, 32, 32);
821 if (memzone_err != NULL) {
822 printf("%s(%s)created a memzone with invalid boundary "
823 "conditions\n", __func__, memzone_err->name);
827 rc = check_memzone_bounded(TEST_MEMZONE_NAME("bounded_128"), 100, 128,
832 rc = check_memzone_bounded(TEST_MEMZONE_NAME("bounded_256"), 100, 256,
837 rc = check_memzone_bounded(TEST_MEMZONE_NAME("bounded_1K"), 100, 64,
842 rc = check_memzone_bounded(TEST_MEMZONE_NAME("bounded_1K_MAX"), 0, 64,
851 test_memzone_free(void)
853 const struct rte_memzone *mz[RTE_MAX_MEMZONE + 1];
857 mz[0] = rte_memzone_reserve(TEST_MEMZONE_NAME("tempzone0"), 2000,
859 mz[1] = rte_memzone_reserve(TEST_MEMZONE_NAME("tempzone1"), 4000,
864 if (!rte_memzone_lookup(TEST_MEMZONE_NAME("tempzone0")))
866 if (!rte_memzone_lookup(TEST_MEMZONE_NAME("tempzone1")))
869 if (rte_memzone_free(mz[0])) {
870 printf("Fail memzone free - tempzone0\n");
873 if (rte_memzone_lookup(TEST_MEMZONE_NAME("tempzone0"))) {
874 printf("Found previously free memzone - tempzone0\n");
877 mz[2] = rte_memzone_reserve(TEST_MEMZONE_NAME("tempzone2"), 2000,
881 printf("tempzone2 should have gotten the free entry from tempzone0\n");
884 if (rte_memzone_free(mz[2])) {
885 printf("Fail memzone free - tempzone2\n");
888 if (rte_memzone_lookup(TEST_MEMZONE_NAME("tempzone2"))) {
889 printf("Found previously free memzone - tempzone2\n");
892 if (rte_memzone_free(mz[1])) {
893 printf("Fail memzone free - tempzone1\n");
896 if (rte_memzone_lookup(TEST_MEMZONE_NAME("tempzone1"))) {
897 printf("Found previously free memzone - tempzone1\n");
903 snprintf(name, sizeof(name), TEST_MEMZONE_NAME("tempzone%u"),
905 mz[i] = rte_memzone_reserve(name, 1, SOCKET_ID_ANY, 0);
906 } while (mz[i++] != NULL);
908 if (rte_memzone_free(mz[0])) {
909 printf("Fail memzone free - tempzone0\n");
912 mz[0] = rte_memzone_reserve(TEST_MEMZONE_NAME("tempzone0new"), 0,
916 printf("Fail to create memzone - tempzone0new - when MAX memzones were "
917 "created and one was free\n");
921 for (i = i - 2; i >= 0; i--) {
922 if (rte_memzone_free(mz[i])) {
923 printf("Fail memzone free - tempzone%d\n", i);
932 test_memzone_basic(void)
934 const struct rte_memzone *memzone1;
935 const struct rte_memzone *memzone2;
936 const struct rte_memzone *memzone3;
937 const struct rte_memzone *memzone4;
938 const struct rte_memzone *mz;
939 int memzone_cnt_after, memzone_cnt_expected;
940 int memzone_cnt_before =
941 rte_eal_get_configuration()->mem_config->memzones.count;
943 memzone1 = rte_memzone_reserve(TEST_MEMZONE_NAME("testzone1"), 100,
946 memzone2 = rte_memzone_reserve(TEST_MEMZONE_NAME("testzone2"), 1000,
949 memzone3 = rte_memzone_reserve(TEST_MEMZONE_NAME("testzone3"), 1000,
952 memzone4 = rte_memzone_reserve(TEST_MEMZONE_NAME("testzone4"), 1024,
955 /* memzone3 may be NULL if we don't have NUMA */
956 if (memzone1 == NULL || memzone2 == NULL || memzone4 == NULL)
959 /* check how many memzones we are expecting */
960 memzone_cnt_expected = memzone_cnt_before +
961 (memzone1 != NULL) + (memzone2 != NULL) +
962 (memzone3 != NULL) + (memzone4 != NULL);
965 rte_eal_get_configuration()->mem_config->memzones.count;
967 if (memzone_cnt_after != memzone_cnt_expected)
971 rte_memzone_dump(stdout);
973 /* check cache-line alignments */
974 printf("check alignments and lengths\n");
976 if ((memzone1->iova & RTE_CACHE_LINE_MASK) != 0)
978 if ((memzone2->iova & RTE_CACHE_LINE_MASK) != 0)
980 if (memzone3 != NULL && (memzone3->iova & RTE_CACHE_LINE_MASK) != 0)
982 if ((memzone1->len & RTE_CACHE_LINE_MASK) != 0 || memzone1->len == 0)
984 if ((memzone2->len & RTE_CACHE_LINE_MASK) != 0 || memzone2->len == 0)
986 if (memzone3 != NULL && ((memzone3->len & RTE_CACHE_LINE_MASK) != 0 ||
989 if (memzone4->len != 1024)
992 /* check that zones don't overlap */
993 printf("check overlapping\n");
995 if (is_memory_overlap(memzone1->iova, memzone1->len,
996 memzone2->iova, memzone2->len))
998 if (memzone3 != NULL &&
999 is_memory_overlap(memzone1->iova, memzone1->len,
1000 memzone3->iova, memzone3->len))
1002 if (memzone3 != NULL &&
1003 is_memory_overlap(memzone2->iova, memzone2->len,
1004 memzone3->iova, memzone3->len))
1007 printf("check socket ID\n");
1009 /* memzone2 must be on socket id 0 and memzone3 on socket 1 */
1010 if (memzone2->socket_id != 0)
1012 if (memzone3 != NULL && memzone3->socket_id != 1)
1015 printf("test zone lookup\n");
1016 mz = rte_memzone_lookup(TEST_MEMZONE_NAME("testzone1"));
1020 printf("test duplcate zone name\n");
1021 mz = rte_memzone_reserve(TEST_MEMZONE_NAME("testzone1"), 100,
1026 if (rte_memzone_free(memzone1)) {
1027 printf("Fail memzone free - memzone1\n");
1030 if (rte_memzone_free(memzone2)) {
1031 printf("Fail memzone free - memzone2\n");
1034 if (memzone3 && rte_memzone_free(memzone3)) {
1035 printf("Fail memzone free - memzone3\n");
1038 if (rte_memzone_free(memzone4)) {
1039 printf("Fail memzone free - memzone4\n");
1044 rte_eal_get_configuration()->mem_config->memzones.count;
1045 if (memzone_cnt_after != memzone_cnt_before)
1051 static int test_memzones_left;
1052 static int memzone_walk_cnt;
1053 static void memzone_walk_clb(const struct rte_memzone *mz,
1054 void *arg __rte_unused)
1057 if (!strncmp(TEST_MEMZONE_NAME(""), mz->name, RTE_MEMZONE_NAMESIZE))
1058 test_memzones_left++;
1064 /* take note of how many memzones were allocated before running */
1066 rte_eal_get_configuration()->mem_config->memzones.count;
1068 printf("test basic memzone API\n");
1069 if (test_memzone_basic() < 0)
1072 printf("test free memzone\n");
1073 if (test_memzone_free() < 0)
1076 printf("test reserving memzone with bigger size than the maximum\n");
1077 if (test_memzone_reserving_zone_size_bigger_than_the_maximum() < 0)
1080 printf("test memzone_reserve flags\n");
1081 if (test_memzone_reserve_flags() < 0)
1084 printf("test alignment for memzone_reserve\n");
1085 if (test_memzone_aligned() < 0)
1088 printf("test boundary alignment for memzone_reserve\n");
1089 if (test_memzone_bounded() < 0)
1092 printf("test invalid alignment for memzone_reserve\n");
1093 if (test_memzone_invalid_alignment() < 0)
1096 printf("test reserving the largest size memzone possible\n");
1097 if (test_memzone_reserve_max() < 0)
1100 printf("test reserving the largest size aligned memzone possible\n");
1101 if (test_memzone_reserve_max_aligned() < 0)
1104 printf("check memzone cleanup\n");
1105 memzone_walk_cnt = 0;
1106 test_memzones_left = 0;
1107 rte_memzone_walk(memzone_walk_clb, NULL);
1108 if (memzone_walk_cnt != memzone_cnt || test_memzones_left > 0) {
1109 printf("there are some memzones left after test\n");
1110 rte_memzone_dump(stdout);
1117 REGISTER_TEST_COMMAND(memzone_autotest, test_memzone);