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_lcore.h>
17 #include <rte_common.h>
18 #include <rte_string_fns.h>
19 #include <rte_errno.h>
20 #include <rte_malloc.h>
22 #include "malloc_elem.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_invalid_flags(void)
88 const struct rte_memzone *mz;
90 mz = rte_memzone_lookup(TEST_MEMZONE_NAME("invalid_flags"));
92 printf("Zone with invalid flags has been reserved\n");
96 mz = rte_memzone_reserve(TEST_MEMZONE_NAME("invalid_flags"),
97 100, SOCKET_ID_ANY, RTE_MEMZONE_IOVA_CONTIG << 1);
99 printf("Zone with invalid flags has been reserved\n");
106 test_memzone_reserving_zone_size_bigger_than_the_maximum(void)
108 const struct rte_memzone * mz;
110 mz = rte_memzone_lookup(
111 TEST_MEMZONE_NAME("zone_size_bigger_than_the_maximum"));
113 printf("zone_size_bigger_than_the_maximum has been reserved\n");
117 mz = rte_memzone_reserve(
118 TEST_MEMZONE_NAME("zone_size_bigger_than_the_maximum"),
119 (size_t)-1, SOCKET_ID_ANY, 0);
121 printf("It is impossible to reserve such big a memzone\n");
129 int hugepage_2MB_avail;
130 int hugepage_1GB_avail;
131 int hugepage_16MB_avail;
132 int hugepage_16GB_avail;
135 find_available_pagesz(const struct rte_memseg_list *msl, void *arg)
137 struct walk_arg *wa = arg;
142 if (msl->page_sz == RTE_PGSIZE_2M)
143 wa->hugepage_2MB_avail = 1;
144 if (msl->page_sz == RTE_PGSIZE_1G)
145 wa->hugepage_1GB_avail = 1;
146 if (msl->page_sz == RTE_PGSIZE_16M)
147 wa->hugepage_16MB_avail = 1;
148 if (msl->page_sz == RTE_PGSIZE_16G)
149 wa->hugepage_16GB_avail = 1;
155 test_memzone_reserve_flags(void)
157 const struct rte_memzone *mz;
159 int hugepage_2MB_avail, hugepage_1GB_avail;
160 int hugepage_16MB_avail, hugepage_16GB_avail;
161 const size_t size = 100;
163 memset(&wa, 0, sizeof(wa));
165 rte_memseg_list_walk(find_available_pagesz, &wa);
167 hugepage_2MB_avail = wa.hugepage_2MB_avail;
168 hugepage_1GB_avail = wa.hugepage_1GB_avail;
169 hugepage_16MB_avail = wa.hugepage_16MB_avail;
170 hugepage_16GB_avail = wa.hugepage_16GB_avail;
172 /* Display the availability of 2MB ,1GB, 16MB, 16GB pages */
173 if (hugepage_2MB_avail)
174 printf("2MB Huge pages available\n");
175 if (hugepage_1GB_avail)
176 printf("1GB Huge pages available\n");
177 if (hugepage_16MB_avail)
178 printf("16MB Huge pages available\n");
179 if (hugepage_16GB_avail)
180 printf("16GB Huge pages available\n");
182 * If 2MB pages available, check that a small memzone is correctly
183 * reserved from 2MB huge pages when requested by the RTE_MEMZONE_2MB flag.
184 * Also check that RTE_MEMZONE_SIZE_HINT_ONLY flag only defaults to an
185 * available page size (i.e 1GB ) when 2MB pages are unavailable.
187 if (hugepage_2MB_avail) {
188 mz = rte_memzone_reserve(TEST_MEMZONE_NAME("flag_zone_2M"),
189 size, SOCKET_ID_ANY, RTE_MEMZONE_2MB);
191 printf("MEMZONE FLAG 2MB\n");
194 if (mz->hugepage_sz != RTE_PGSIZE_2M) {
195 printf("hugepage_sz not equal 2M\n");
198 if (rte_memzone_free(mz)) {
199 printf("Fail memzone free\n");
203 mz = rte_memzone_reserve(TEST_MEMZONE_NAME("flag_zone_2M_HINT"),
205 RTE_MEMZONE_2MB|RTE_MEMZONE_SIZE_HINT_ONLY);
207 printf("MEMZONE FLAG 2MB\n");
210 if (mz->hugepage_sz != RTE_PGSIZE_2M) {
211 printf("hugepage_sz not equal 2M\n");
214 if (rte_memzone_free(mz)) {
215 printf("Fail memzone free\n");
219 /* Check if 1GB huge pages are unavailable, that function fails unless
220 * HINT flag is indicated
222 if (!hugepage_1GB_avail) {
223 mz = rte_memzone_reserve(
224 TEST_MEMZONE_NAME("flag_zone_1G_HINT"),
226 RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY);
228 printf("MEMZONE FLAG 1GB & HINT\n");
231 if (mz->hugepage_sz != RTE_PGSIZE_2M) {
232 printf("hugepage_sz not equal 2M\n");
235 if (rte_memzone_free(mz)) {
236 printf("Fail memzone free\n");
240 mz = rte_memzone_reserve(
241 TEST_MEMZONE_NAME("flag_zone_1G"), size,
242 SOCKET_ID_ANY, RTE_MEMZONE_1GB);
244 printf("MEMZONE FLAG 1GB\n");
250 /*As with 2MB tests above for 1GB huge page requests*/
251 if (hugepage_1GB_avail) {
252 mz = rte_memzone_reserve(TEST_MEMZONE_NAME("flag_zone_1G"),
253 size, SOCKET_ID_ANY, RTE_MEMZONE_1GB);
255 printf("MEMZONE FLAG 1GB\n");
258 if (mz->hugepage_sz != RTE_PGSIZE_1G) {
259 printf("hugepage_sz not equal 1G\n");
262 if (rte_memzone_free(mz)) {
263 printf("Fail memzone free\n");
267 mz = rte_memzone_reserve(TEST_MEMZONE_NAME("flag_zone_1G_HINT"),
269 RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY);
271 printf("MEMZONE FLAG 1GB\n");
274 if (mz->hugepage_sz != RTE_PGSIZE_1G) {
275 printf("hugepage_sz not equal 1G\n");
278 if (rte_memzone_free(mz)) {
279 printf("Fail memzone free\n");
283 /* Check if 1GB huge pages are unavailable, that function fails unless
284 * HINT flag is indicated
286 if (!hugepage_2MB_avail) {
287 mz = rte_memzone_reserve(
288 TEST_MEMZONE_NAME("flag_zone_2M_HINT"),
290 RTE_MEMZONE_2MB|RTE_MEMZONE_SIZE_HINT_ONLY);
292 printf("MEMZONE FLAG 2MB & HINT\n");
295 if (mz->hugepage_sz != RTE_PGSIZE_1G) {
296 printf("hugepage_sz not equal 1G\n");
299 if (rte_memzone_free(mz)) {
300 printf("Fail memzone free\n");
303 mz = rte_memzone_reserve(
304 TEST_MEMZONE_NAME("flag_zone_2M"), size,
305 SOCKET_ID_ANY, RTE_MEMZONE_2MB);
307 printf("MEMZONE FLAG 2MB\n");
312 if (hugepage_2MB_avail && hugepage_1GB_avail) {
313 mz = rte_memzone_reserve(
314 TEST_MEMZONE_NAME("flag_zone_2M_HINT"),
316 RTE_MEMZONE_2MB|RTE_MEMZONE_1GB);
318 printf("BOTH SIZES SET\n");
321 if (mz->hugepage_sz != RTE_PGSIZE_1G &&
322 mz->hugepage_sz != RTE_PGSIZE_2M) {
323 printf("Wrong size when both sizes set\n");
326 if (rte_memzone_free(mz)) {
327 printf("Fail memzone free\n");
333 * This option is for IBM Power. If 16MB pages available, check
334 * that a small memzone is correctly reserved from 16MB huge pages
335 * when requested by the RTE_MEMZONE_16MB flag. Also check that
336 * RTE_MEMZONE_SIZE_HINT_ONLY flag only defaults to an available
337 * page size (i.e 16GB ) when 16MB pages are unavailable.
339 if (hugepage_16MB_avail) {
340 mz = rte_memzone_reserve(TEST_MEMZONE_NAME("flag_zone_16M"),
341 size, SOCKET_ID_ANY, RTE_MEMZONE_16MB);
343 printf("MEMZONE FLAG 16MB\n");
346 if (mz->hugepage_sz != RTE_PGSIZE_16M) {
347 printf("hugepage_sz not equal 16M\n");
350 if (rte_memzone_free(mz)) {
351 printf("Fail memzone free\n");
355 mz = rte_memzone_reserve(
356 TEST_MEMZONE_NAME("flag_zone_16M_HINT"), size,
358 RTE_MEMZONE_16MB|RTE_MEMZONE_SIZE_HINT_ONLY);
360 printf("MEMZONE FLAG 16MB\n");
363 if (mz->hugepage_sz != RTE_PGSIZE_16M) {
364 printf("hugepage_sz not equal 16M\n");
367 if (rte_memzone_free(mz)) {
368 printf("Fail memzone free\n");
372 /* Check if 1GB huge pages are unavailable, that function fails
373 * unless HINT flag is indicated
375 if (!hugepage_16GB_avail) {
376 mz = rte_memzone_reserve(
377 TEST_MEMZONE_NAME("flag_zone_16G_HINT"),
380 RTE_MEMZONE_SIZE_HINT_ONLY);
382 printf("MEMZONE FLAG 16GB & HINT\n");
385 if (mz->hugepage_sz != RTE_PGSIZE_16M) {
386 printf("hugepage_sz not equal 16M\n");
389 if (rte_memzone_free(mz)) {
390 printf("Fail memzone free\n");
394 mz = rte_memzone_reserve(
395 TEST_MEMZONE_NAME("flag_zone_16G"),
397 SOCKET_ID_ANY, RTE_MEMZONE_16GB);
399 printf("MEMZONE FLAG 16GB\n");
404 /*As with 16MB tests above for 16GB huge page requests*/
405 if (hugepage_16GB_avail) {
406 mz = rte_memzone_reserve(TEST_MEMZONE_NAME("flag_zone_16G"),
407 size, SOCKET_ID_ANY, RTE_MEMZONE_16GB);
409 printf("MEMZONE FLAG 16GB\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");
421 mz = rte_memzone_reserve(
422 TEST_MEMZONE_NAME("flag_zone_16G_HINT"), size,
424 RTE_MEMZONE_16GB|RTE_MEMZONE_SIZE_HINT_ONLY);
426 printf("MEMZONE FLAG 16GB\n");
429 if (mz->hugepage_sz != RTE_PGSIZE_16G) {
430 printf("hugepage_sz not equal 16G\n");
433 if (rte_memzone_free(mz)) {
434 printf("Fail memzone free\n");
438 /* Check if 1GB huge pages are unavailable, that function fails
439 * unless HINT flag is indicated
441 if (!hugepage_16MB_avail) {
442 mz = rte_memzone_reserve(
443 TEST_MEMZONE_NAME("flag_zone_16M_HINT"),
446 RTE_MEMZONE_SIZE_HINT_ONLY);
448 printf("MEMZONE FLAG 16MB & HINT\n");
451 if (mz->hugepage_sz != RTE_PGSIZE_16G) {
452 printf("hugepage_sz not equal 16G\n");
455 if (rte_memzone_free(mz)) {
456 printf("Fail memzone free\n");
459 mz = rte_memzone_reserve(
460 TEST_MEMZONE_NAME("flag_zone_16M"),
461 size, SOCKET_ID_ANY, RTE_MEMZONE_16MB);
463 printf("MEMZONE FLAG 16MB\n");
468 if (hugepage_16MB_avail && hugepage_16GB_avail) {
469 mz = rte_memzone_reserve(
470 TEST_MEMZONE_NAME("flag_zone_16M_HINT"),
472 RTE_MEMZONE_16MB|RTE_MEMZONE_16GB);
474 printf("BOTH SIZES SET\n");
477 if (mz->hugepage_sz != RTE_PGSIZE_16G &&
478 mz->hugepage_sz != RTE_PGSIZE_16M) {
479 printf("Wrong size when both sizes set\n");
482 if (rte_memzone_free(mz)) {
483 printf("Fail memzone free\n");
492 /* Find the heap with the greatest free block size */
494 find_max_block_free_size(unsigned int align, unsigned int socket_id)
496 struct rte_malloc_socket_stats stats;
497 size_t len, overhead;
499 if (rte_malloc_get_socket_stats(socket_id, &stats) < 0)
502 len = stats.greatest_free_size;
503 overhead = MALLOC_ELEM_OVERHEAD;
508 align = RTE_CACHE_LINE_ROUNDUP(align);
514 return len - overhead;
518 test_memzone_reserve_max(void)
522 for (i = 0; i < rte_socket_count(); i++) {
523 const struct rte_memzone *mz;
527 socket = rte_socket_id_by_idx(i);
528 maxlen = find_max_block_free_size(0, socket);
531 printf("There is no space left!\n");
535 mz = rte_memzone_reserve(TEST_MEMZONE_NAME("max_zone"), 0,
538 printf("Failed to reserve a big chunk of memory - %s\n",
539 rte_strerror(rte_errno));
540 rte_dump_physmem_layout(stdout);
541 rte_memzone_dump(stdout);
545 if (mz->len != maxlen) {
546 printf("Memzone reserve with 0 size did not return bigest block\n");
547 printf("Expected size = %zu, actual size = %zu\n",
549 rte_dump_physmem_layout(stdout);
550 rte_memzone_dump(stdout);
554 if (rte_memzone_free(mz)) {
555 printf("Fail memzone free\n");
564 test_memzone_reserve_max_aligned(void)
568 for (i = 0; i < rte_socket_count(); i++) {
569 const struct rte_memzone *mz;
570 size_t maxlen, minlen = 0;
573 socket = rte_socket_id_by_idx(i);
575 /* random alignment */
576 rte_srand((unsigned int)rte_rdtsc());
577 const unsigned int align = 1 << ((rte_rand() % 8) + 5); /* from 128 up to 4k alignment */
579 /* memzone size may be between size and size - align */
580 minlen = find_max_block_free_size(align, socket);
581 maxlen = find_max_block_free_size(0, socket);
583 if (minlen == 0 || maxlen == 0) {
584 printf("There is no space left for biggest %u-aligned memzone!\n",
589 mz = rte_memzone_reserve_aligned(
590 TEST_MEMZONE_NAME("max_zone_aligned"),
591 0, socket, 0, align);
593 printf("Failed to reserve a big chunk of memory - %s\n",
594 rte_strerror(rte_errno));
595 rte_dump_physmem_layout(stdout);
596 rte_memzone_dump(stdout);
599 if (mz->addr != RTE_PTR_ALIGN(mz->addr, align)) {
600 printf("Memzone reserve with 0 size and alignment %u did not return aligned block\n",
602 rte_dump_physmem_layout(stdout);
603 rte_memzone_dump(stdout);
607 if (mz->len < minlen || mz->len > maxlen) {
608 printf("Memzone reserve with 0 size and alignment %u did not return"
609 " bigest block\n", align);
610 printf("Expected size = %zu-%zu, actual size = %zu\n",
611 minlen, maxlen, mz->len);
612 rte_dump_physmem_layout(stdout);
613 rte_memzone_dump(stdout);
617 if (rte_memzone_free(mz)) {
618 printf("Fail memzone free\n");
626 test_memzone_aligned(void)
628 const struct rte_memzone *memzone_aligned_32;
629 const struct rte_memzone *memzone_aligned_128;
630 const struct rte_memzone *memzone_aligned_256;
631 const struct rte_memzone *memzone_aligned_512;
632 const struct rte_memzone *memzone_aligned_1024;
634 /* memzone that should automatically be adjusted to align on 64 bytes */
635 memzone_aligned_32 = rte_memzone_reserve_aligned(
636 TEST_MEMZONE_NAME("aligned_32"), 100, SOCKET_ID_ANY, 0,
639 /* memzone that is supposed to be aligned on a 128 byte boundary */
640 memzone_aligned_128 = rte_memzone_reserve_aligned(
641 TEST_MEMZONE_NAME("aligned_128"), 100, SOCKET_ID_ANY, 0,
644 /* memzone that is supposed to be aligned on a 256 byte boundary */
645 memzone_aligned_256 = rte_memzone_reserve_aligned(
646 TEST_MEMZONE_NAME("aligned_256"), 100, SOCKET_ID_ANY, 0,
649 /* memzone that is supposed to be aligned on a 512 byte boundary */
650 memzone_aligned_512 = rte_memzone_reserve_aligned(
651 TEST_MEMZONE_NAME("aligned_512"), 100, SOCKET_ID_ANY, 0,
654 /* memzone that is supposed to be aligned on a 1024 byte boundary */
655 memzone_aligned_1024 = rte_memzone_reserve_aligned(
656 TEST_MEMZONE_NAME("aligned_1024"), 100, SOCKET_ID_ANY,
659 printf("check alignments and lengths\n");
660 if (memzone_aligned_32 == NULL) {
661 printf("Unable to reserve 64-byte aligned memzone!\n");
664 if ((memzone_aligned_32->iova & RTE_CACHE_LINE_MASK) != 0)
666 if (((uintptr_t) memzone_aligned_32->addr & RTE_CACHE_LINE_MASK) != 0)
668 if ((memzone_aligned_32->len & RTE_CACHE_LINE_MASK) != 0)
671 if (memzone_aligned_128 == NULL) {
672 printf("Unable to reserve 128-byte aligned memzone!\n");
675 if ((memzone_aligned_128->iova & 127) != 0)
677 if (((uintptr_t) memzone_aligned_128->addr & 127) != 0)
679 if ((memzone_aligned_128->len & RTE_CACHE_LINE_MASK) != 0)
682 if (memzone_aligned_256 == NULL) {
683 printf("Unable to reserve 256-byte aligned memzone!\n");
686 if ((memzone_aligned_256->iova & 255) != 0)
688 if (((uintptr_t) memzone_aligned_256->addr & 255) != 0)
690 if ((memzone_aligned_256->len & RTE_CACHE_LINE_MASK) != 0)
693 if (memzone_aligned_512 == NULL) {
694 printf("Unable to reserve 512-byte aligned memzone!\n");
697 if ((memzone_aligned_512->iova & 511) != 0)
699 if (((uintptr_t) memzone_aligned_512->addr & 511) != 0)
701 if ((memzone_aligned_512->len & RTE_CACHE_LINE_MASK) != 0)
704 if (memzone_aligned_1024 == NULL) {
705 printf("Unable to reserve 1024-byte aligned memzone!\n");
708 if ((memzone_aligned_1024->iova & 1023) != 0)
710 if (((uintptr_t) memzone_aligned_1024->addr & 1023) != 0)
712 if ((memzone_aligned_1024->len & RTE_CACHE_LINE_MASK) != 0)
715 /* check that zones don't overlap */
716 printf("check overlapping\n");
717 if (is_memory_overlap(memzone_aligned_32->iova, memzone_aligned_32->len,
718 memzone_aligned_128->iova, memzone_aligned_128->len))
720 if (is_memory_overlap(memzone_aligned_32->iova, memzone_aligned_32->len,
721 memzone_aligned_256->iova, memzone_aligned_256->len))
723 if (is_memory_overlap(memzone_aligned_32->iova, memzone_aligned_32->len,
724 memzone_aligned_512->iova, memzone_aligned_512->len))
726 if (is_memory_overlap(memzone_aligned_32->iova, memzone_aligned_32->len,
727 memzone_aligned_1024->iova, memzone_aligned_1024->len))
729 if (is_memory_overlap(memzone_aligned_128->iova, memzone_aligned_128->len,
730 memzone_aligned_256->iova, memzone_aligned_256->len))
732 if (is_memory_overlap(memzone_aligned_128->iova, memzone_aligned_128->len,
733 memzone_aligned_512->iova, memzone_aligned_512->len))
735 if (is_memory_overlap(memzone_aligned_128->iova, memzone_aligned_128->len,
736 memzone_aligned_1024->iova, memzone_aligned_1024->len))
738 if (is_memory_overlap(memzone_aligned_256->iova, memzone_aligned_256->len,
739 memzone_aligned_512->iova, memzone_aligned_512->len))
741 if (is_memory_overlap(memzone_aligned_256->iova, memzone_aligned_256->len,
742 memzone_aligned_1024->iova, memzone_aligned_1024->len))
744 if (is_memory_overlap(memzone_aligned_512->iova, memzone_aligned_512->len,
745 memzone_aligned_1024->iova, memzone_aligned_1024->len))
748 /* free all used zones */
749 if (rte_memzone_free(memzone_aligned_32)) {
750 printf("Fail memzone free\n");
753 if (rte_memzone_free(memzone_aligned_128)) {
754 printf("Fail memzone free\n");
757 if (rte_memzone_free(memzone_aligned_256)) {
758 printf("Fail memzone free\n");
761 if (rte_memzone_free(memzone_aligned_512)) {
762 printf("Fail memzone free\n");
765 if (rte_memzone_free(memzone_aligned_1024)) {
766 printf("Fail memzone free\n");
773 check_memzone_bounded(const char *name, uint32_t len, uint32_t align,
776 const struct rte_memzone *mz;
779 bmask = ~((rte_iova_t)bound - 1);
781 if ((mz = rte_memzone_reserve_bounded(name, len, SOCKET_ID_ANY, 0,
782 align, bound)) == NULL) {
783 printf("%s(%s): memzone creation failed\n",
788 if ((mz->iova & ((rte_iova_t)align - 1)) != 0) {
789 printf("%s(%s): invalid phys addr alignment\n",
794 if (((uintptr_t) mz->addr & ((uintptr_t)align - 1)) != 0) {
795 printf("%s(%s): invalid virtual addr alignment\n",
800 if ((mz->len & RTE_CACHE_LINE_MASK) != 0 || mz->len < len ||
801 mz->len < RTE_CACHE_LINE_SIZE) {
802 printf("%s(%s): invalid length\n",
807 if ((mz->iova & bmask) !=
808 ((mz->iova + mz->len - 1) & bmask)) {
809 printf("%s(%s): invalid memzone boundary %u crossed\n",
810 __func__, mz->name, bound);
814 if (rte_memzone_free(mz)) {
815 printf("Fail memzone free\n");
823 test_memzone_bounded(void)
825 const struct rte_memzone *memzone_err;
828 /* should fail as boundary is not power of two */
829 memzone_err = rte_memzone_reserve_bounded(
830 TEST_MEMZONE_NAME("bounded_error_31"), 100,
831 SOCKET_ID_ANY, 0, 32, UINT32_MAX);
832 if (memzone_err != NULL) {
833 printf("%s(%s)created a memzone with invalid boundary "
834 "conditions\n", __func__, memzone_err->name);
838 /* should fail as len is greater then boundary */
839 memzone_err = rte_memzone_reserve_bounded(
840 TEST_MEMZONE_NAME("bounded_error_32"), 100,
841 SOCKET_ID_ANY, 0, 32, 32);
842 if (memzone_err != NULL) {
843 printf("%s(%s)created a memzone with invalid boundary "
844 "conditions\n", __func__, memzone_err->name);
848 rc = check_memzone_bounded(TEST_MEMZONE_NAME("bounded_128"), 100, 128,
853 rc = check_memzone_bounded(TEST_MEMZONE_NAME("bounded_256"), 100, 256,
858 rc = check_memzone_bounded(TEST_MEMZONE_NAME("bounded_1K"), 100, 64,
863 rc = check_memzone_bounded(TEST_MEMZONE_NAME("bounded_1K_MAX"), 0, 64,
872 test_memzone_free(void)
874 const struct rte_memzone *mz[RTE_MAX_MEMZONE + 1];
878 mz[0] = rte_memzone_reserve(TEST_MEMZONE_NAME("tempzone0"), 2000,
880 mz[1] = rte_memzone_reserve(TEST_MEMZONE_NAME("tempzone1"), 4000,
885 if (!rte_memzone_lookup(TEST_MEMZONE_NAME("tempzone0")))
887 if (!rte_memzone_lookup(TEST_MEMZONE_NAME("tempzone1")))
890 if (rte_memzone_free(mz[0])) {
891 printf("Fail memzone free - tempzone0\n");
894 if (rte_memzone_lookup(TEST_MEMZONE_NAME("tempzone0"))) {
895 printf("Found previously free memzone - tempzone0\n");
898 mz[2] = rte_memzone_reserve(TEST_MEMZONE_NAME("tempzone2"), 2000,
902 printf("tempzone2 should have gotten the free entry from tempzone0\n");
905 if (rte_memzone_free(mz[2])) {
906 printf("Fail memzone free - tempzone2\n");
909 if (rte_memzone_lookup(TEST_MEMZONE_NAME("tempzone2"))) {
910 printf("Found previously free memzone - tempzone2\n");
913 if (rte_memzone_free(mz[1])) {
914 printf("Fail memzone free - tempzone1\n");
917 if (rte_memzone_lookup(TEST_MEMZONE_NAME("tempzone1"))) {
918 printf("Found previously free memzone - tempzone1\n");
924 snprintf(name, sizeof(name), TEST_MEMZONE_NAME("tempzone%u"),
926 mz[i] = rte_memzone_reserve(name, 1, SOCKET_ID_ANY, 0);
927 } while (mz[i++] != NULL);
929 if (rte_memzone_free(mz[0])) {
930 printf("Fail memzone free - tempzone0\n");
933 mz[0] = rte_memzone_reserve(TEST_MEMZONE_NAME("tempzone0new"), 0,
937 printf("Fail to create memzone - tempzone0new - when MAX memzones were "
938 "created and one was free\n");
942 for (i = i - 2; i >= 0; i--) {
943 if (rte_memzone_free(mz[i])) {
944 printf("Fail memzone free - tempzone%d\n", i);
952 static int test_memzones_left;
953 static int memzone_walk_cnt;
954 static void memzone_walk_clb(const struct rte_memzone *mz,
955 void *arg __rte_unused)
958 if (!strncmp(TEST_MEMZONE_NAME(""), mz->name, RTE_MEMZONE_NAMESIZE))
959 test_memzones_left++;
963 test_memzone_basic(void)
965 const struct rte_memzone *memzone1;
966 const struct rte_memzone *memzone2;
967 const struct rte_memzone *memzone3;
968 const struct rte_memzone *memzone4;
969 const struct rte_memzone *mz;
970 int memzone_cnt_after, memzone_cnt_expected;
971 int memzone_cnt_before;
973 memzone_walk_cnt = 0;
974 test_memzones_left = 0;
975 rte_memzone_walk(memzone_walk_clb, NULL);
976 memzone_cnt_before = memzone_walk_cnt;
978 memzone1 = rte_memzone_reserve(TEST_MEMZONE_NAME("testzone1"), 100,
981 memzone2 = rte_memzone_reserve(TEST_MEMZONE_NAME("testzone2"), 1000,
984 memzone3 = rte_memzone_reserve(TEST_MEMZONE_NAME("testzone3"), 1000,
987 memzone4 = rte_memzone_reserve(TEST_MEMZONE_NAME("testzone4"), 1024,
990 /* memzone3 may be NULL if we don't have NUMA */
991 if (memzone1 == NULL || memzone2 == NULL || memzone4 == NULL)
994 /* check how many memzones we are expecting */
995 memzone_cnt_expected = memzone_cnt_before +
996 (memzone1 != NULL) + (memzone2 != NULL) +
997 (memzone3 != NULL) + (memzone4 != NULL);
999 memzone_walk_cnt = 0;
1000 test_memzones_left = 0;
1001 rte_memzone_walk(memzone_walk_clb, NULL);
1002 memzone_cnt_after = memzone_walk_cnt;
1004 if (memzone_cnt_after != memzone_cnt_expected)
1008 rte_memzone_dump(stdout);
1010 /* check cache-line alignments */
1011 printf("check alignments and lengths\n");
1013 if ((memzone1->iova & RTE_CACHE_LINE_MASK) != 0)
1015 if ((memzone2->iova & RTE_CACHE_LINE_MASK) != 0)
1017 if (memzone3 != NULL && (memzone3->iova & RTE_CACHE_LINE_MASK) != 0)
1019 if ((memzone1->len & RTE_CACHE_LINE_MASK) != 0 || memzone1->len == 0)
1021 if ((memzone2->len & RTE_CACHE_LINE_MASK) != 0 || memzone2->len == 0)
1023 if (memzone3 != NULL && ((memzone3->len & RTE_CACHE_LINE_MASK) != 0 ||
1024 memzone3->len == 0))
1026 if (memzone4->len != 1024)
1029 /* check that zones don't overlap */
1030 printf("check overlapping\n");
1032 if (is_memory_overlap(memzone1->iova, memzone1->len,
1033 memzone2->iova, memzone2->len))
1035 if (memzone3 != NULL &&
1036 is_memory_overlap(memzone1->iova, memzone1->len,
1037 memzone3->iova, memzone3->len))
1039 if (memzone3 != NULL &&
1040 is_memory_overlap(memzone2->iova, memzone2->len,
1041 memzone3->iova, memzone3->len))
1044 printf("check socket ID\n");
1046 /* memzone2 must be on socket id 0 and memzone3 on socket 1 */
1047 if (memzone2->socket_id != 0)
1049 if (memzone3 != NULL && memzone3->socket_id != 1)
1052 printf("test zone lookup\n");
1053 mz = rte_memzone_lookup(TEST_MEMZONE_NAME("testzone1"));
1057 printf("test duplcate zone name\n");
1058 mz = rte_memzone_reserve(TEST_MEMZONE_NAME("testzone1"), 100,
1063 if (rte_memzone_free(memzone1)) {
1064 printf("Fail memzone free - memzone1\n");
1067 if (rte_memzone_free(memzone2)) {
1068 printf("Fail memzone free - memzone2\n");
1071 if (memzone3 && rte_memzone_free(memzone3)) {
1072 printf("Fail memzone free - memzone3\n");
1075 if (rte_memzone_free(memzone4)) {
1076 printf("Fail memzone free - memzone4\n");
1080 memzone_walk_cnt = 0;
1081 test_memzones_left = 0;
1082 rte_memzone_walk(memzone_walk_clb, NULL);
1083 memzone_cnt_after = memzone_walk_cnt;
1084 if (memzone_cnt_after != memzone_cnt_before)
1093 /* take note of how many memzones were allocated before running */
1096 memzone_walk_cnt = 0;
1097 test_memzones_left = 0;
1098 rte_memzone_walk(memzone_walk_clb, NULL);
1099 memzone_cnt = memzone_walk_cnt;
1101 printf("test basic memzone API\n");
1102 if (test_memzone_basic() < 0)
1105 printf("test free memzone\n");
1106 if (test_memzone_free() < 0)
1109 printf("test reserving memzone with bigger size than the maximum\n");
1110 if (test_memzone_reserving_zone_size_bigger_than_the_maximum() < 0)
1113 printf("test memzone_reserve flags\n");
1114 if (test_memzone_reserve_flags() < 0)
1117 printf("test alignment for memzone_reserve\n");
1118 if (test_memzone_aligned() < 0)
1121 printf("test boundary alignment for memzone_reserve\n");
1122 if (test_memzone_bounded() < 0)
1125 printf("test invalid alignment for memzone_reserve\n");
1126 if (test_memzone_invalid_alignment() < 0)
1129 printf("test invalid flags for memzone_reserve\n");
1130 if (test_memzone_invalid_flags() < 0)
1133 printf("test reserving the largest size memzone possible\n");
1134 if (test_memzone_reserve_max() < 0)
1137 printf("test reserving the largest size aligned memzone possible\n");
1138 if (test_memzone_reserve_max_aligned() < 0)
1141 printf("check memzone cleanup\n");
1142 memzone_walk_cnt = 0;
1143 test_memzones_left = 0;
1144 rte_memzone_walk(memzone_walk_clb, NULL);
1145 if (memzone_walk_cnt != memzone_cnt || test_memzones_left > 0) {
1146 printf("there are some memzones left after test\n");
1147 rte_memzone_dump(stdout);
1154 REGISTER_TEST_COMMAND(memzone_autotest, test_memzone);