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>
43 #include <rte_tailq.h>
45 #include <rte_eal_memconfig.h>
46 #include <rte_common.h>
47 #include <rte_string_fns.h>
55 * - Search for three reserved zones or reserve them if they do not exist:
57 * - One is on any socket id.
58 * - The second is on socket 0.
59 * - The last one is on socket 1 (if socket 1 exists).
61 * - Check that the zones exist.
63 * - Check that the zones are cache-aligned.
65 * - Check that zones do not overlap.
67 * - Check that the zones are on the correct socket id.
69 * - Check that a lookup of the first zone returns the same pointer.
71 * - Check that it is not possible to create another zone with the
72 * same name as an existing zone.
74 * - Check flags for specific huge page size reservation
77 /* Test if memory overlaps: return 1 if true, or 0 if false. */
79 is_memory_overlap(phys_addr_t ptr1, size_t len1, phys_addr_t ptr2, size_t len2)
81 if (ptr2 >= ptr1 && (ptr2 - ptr1) < len1)
83 else if (ptr2 < ptr1 && (ptr1 - ptr2) < len2)
89 test_memzone_invalid_alignment(void)
91 const struct rte_memzone * mz;
93 mz = rte_memzone_lookup("invalid_alignment");
95 printf("Zone with invalid alignment has been reserved\n");
99 mz = rte_memzone_reserve_aligned("invalid_alignment", 100,
100 SOCKET_ID_ANY, 0, 100);
102 printf("Zone with invalid alignment has been reserved\n");
109 test_memzone_reserving_zone_size_bigger_than_the_maximum(void)
111 const struct rte_memzone * mz;
113 mz = rte_memzone_lookup("zone_size_bigger_than_the_maximum");
115 printf("zone_size_bigger_than_the_maximum has been reserved\n");
119 mz = rte_memzone_reserve("zone_size_bigger_than_the_maximum", (size_t)-1,
122 printf("It is impossible to reserve such big a memzone\n");
130 test_memzone_reserve_flags(void)
132 const struct rte_memzone *mz;
133 const struct rte_memseg *ms;
134 int hugepage_2MB_avail = 0;
135 int hugepage_1GB_avail = 0;
136 int hugepage_16MB_avail = 0;
137 int hugepage_16GB_avail = 0;
138 const size_t size = 100;
140 ms = rte_eal_get_physmem_layout();
141 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
142 if (ms[i].hugepage_sz == RTE_PGSIZE_2M)
143 hugepage_2MB_avail = 1;
144 if (ms[i].hugepage_sz == RTE_PGSIZE_1G)
145 hugepage_1GB_avail = 1;
146 if (ms[i].hugepage_sz == RTE_PGSIZE_16M)
147 hugepage_16MB_avail = 1;
148 if (ms[i].hugepage_sz == RTE_PGSIZE_16G)
149 hugepage_16GB_avail = 1;
151 /* Display the availability of 2MB ,1GB, 16MB, 16GB pages */
152 if (hugepage_2MB_avail)
153 printf("2MB Huge pages available\n");
154 if (hugepage_1GB_avail)
155 printf("1GB Huge pages available\n");
156 if (hugepage_16MB_avail)
157 printf("16MB Huge pages available\n");
158 if (hugepage_16GB_avail)
159 printf("16GB Huge pages available\n");
161 * If 2MB pages available, check that a small memzone is correctly
162 * reserved from 2MB huge pages when requested by the RTE_MEMZONE_2MB flag.
163 * Also check that RTE_MEMZONE_SIZE_HINT_ONLY flag only defaults to an
164 * available page size (i.e 1GB ) when 2MB pages are unavailable.
166 if (hugepage_2MB_avail) {
167 mz = rte_memzone_reserve("flag_zone_2M", size, SOCKET_ID_ANY,
170 printf("MEMZONE FLAG 2MB\n");
173 if (mz->hugepage_sz != RTE_PGSIZE_2M) {
174 printf("hugepage_sz not equal 2M\n");
178 mz = rte_memzone_reserve("flag_zone_2M_HINT", size, SOCKET_ID_ANY,
179 RTE_MEMZONE_2MB|RTE_MEMZONE_SIZE_HINT_ONLY);
181 printf("MEMZONE FLAG 2MB\n");
184 if (mz->hugepage_sz != RTE_PGSIZE_2M) {
185 printf("hugepage_sz not equal 2M\n");
189 /* Check if 1GB huge pages are unavailable, that function fails unless
190 * HINT flag is indicated
192 if (!hugepage_1GB_avail) {
193 mz = rte_memzone_reserve("flag_zone_1G_HINT", size, SOCKET_ID_ANY,
194 RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY);
196 printf("MEMZONE FLAG 1GB & HINT\n");
199 if (mz->hugepage_sz != RTE_PGSIZE_2M) {
200 printf("hugepage_sz not equal 2M\n");
204 mz = rte_memzone_reserve("flag_zone_1G", size, SOCKET_ID_ANY,
207 printf("MEMZONE FLAG 1GB\n");
213 /*As with 2MB tests above for 1GB huge page requests*/
214 if (hugepage_1GB_avail) {
215 mz = rte_memzone_reserve("flag_zone_1G", size, SOCKET_ID_ANY,
218 printf("MEMZONE FLAG 1GB\n");
221 if (mz->hugepage_sz != RTE_PGSIZE_1G) {
222 printf("hugepage_sz not equal 1G\n");
226 mz = rte_memzone_reserve("flag_zone_1G_HINT", size, SOCKET_ID_ANY,
227 RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY);
229 printf("MEMZONE FLAG 1GB\n");
232 if (mz->hugepage_sz != RTE_PGSIZE_1G) {
233 printf("hugepage_sz not equal 1G\n");
237 /* Check if 1GB huge pages are unavailable, that function fails unless
238 * HINT flag is indicated
240 if (!hugepage_2MB_avail) {
241 mz = rte_memzone_reserve("flag_zone_2M_HINT", size, SOCKET_ID_ANY,
242 RTE_MEMZONE_2MB|RTE_MEMZONE_SIZE_HINT_ONLY);
244 printf("MEMZONE FLAG 2MB & HINT\n");
247 if (mz->hugepage_sz != RTE_PGSIZE_1G) {
248 printf("hugepage_sz not equal 1G\n");
251 mz = rte_memzone_reserve("flag_zone_2M", size, SOCKET_ID_ANY,
254 printf("MEMZONE FLAG 2MB\n");
259 if (hugepage_2MB_avail && hugepage_1GB_avail) {
260 mz = rte_memzone_reserve("flag_zone_2M_HINT", size, SOCKET_ID_ANY,
261 RTE_MEMZONE_2MB|RTE_MEMZONE_1GB);
263 printf("BOTH SIZES SET\n");
269 * This option is for IBM Power. If 16MB pages available, check
270 * that a small memzone is correctly reserved from 16MB huge pages
271 * when requested by the RTE_MEMZONE_16MB flag. Also check that
272 * RTE_MEMZONE_SIZE_HINT_ONLY flag only defaults to an available
273 * page size (i.e 16GB ) when 16MB pages are unavailable.
275 if (hugepage_16MB_avail) {
276 mz = rte_memzone_reserve("flag_zone_16M", size, SOCKET_ID_ANY,
279 printf("MEMZONE FLAG 16MB\n");
282 if (mz->hugepage_sz != RTE_PGSIZE_16M) {
283 printf("hugepage_sz not equal 16M\n");
287 mz = rte_memzone_reserve("flag_zone_16M_HINT", size,
288 SOCKET_ID_ANY, RTE_MEMZONE_16MB|RTE_MEMZONE_SIZE_HINT_ONLY);
290 printf("MEMZONE FLAG 2MB\n");
293 if (mz->hugepage_sz != RTE_PGSIZE_16M) {
294 printf("hugepage_sz not equal 16M\n");
298 /* Check if 1GB huge pages are unavailable, that function fails
299 * unless HINT flag is indicated
301 if (!hugepage_16GB_avail) {
302 mz = rte_memzone_reserve("flag_zone_16G_HINT", size,
304 RTE_MEMZONE_16GB|RTE_MEMZONE_SIZE_HINT_ONLY);
306 printf("MEMZONE FLAG 16GB & HINT\n");
309 if (mz->hugepage_sz != RTE_PGSIZE_16M) {
310 printf("hugepage_sz not equal 16M\n");
314 mz = rte_memzone_reserve("flag_zone_16G", size,
315 SOCKET_ID_ANY, RTE_MEMZONE_16GB);
317 printf("MEMZONE FLAG 16GB\n");
322 /*As with 16MB tests above for 16GB huge page requests*/
323 if (hugepage_16GB_avail) {
324 mz = rte_memzone_reserve("flag_zone_16G", size, SOCKET_ID_ANY,
327 printf("MEMZONE FLAG 16GB\n");
330 if (mz->hugepage_sz != RTE_PGSIZE_16G) {
331 printf("hugepage_sz not equal 16G\n");
335 mz = rte_memzone_reserve("flag_zone_16G_HINT", size,
336 SOCKET_ID_ANY, RTE_MEMZONE_16GB|RTE_MEMZONE_SIZE_HINT_ONLY);
338 printf("MEMZONE FLAG 16GB\n");
341 if (mz->hugepage_sz != RTE_PGSIZE_16G) {
342 printf("hugepage_sz not equal 16G\n");
346 /* Check if 1GB huge pages are unavailable, that function fails
347 * unless HINT flag is indicated
349 if (!hugepage_16MB_avail) {
350 mz = rte_memzone_reserve("flag_zone_16M_HINT", size,
352 RTE_MEMZONE_16MB|RTE_MEMZONE_SIZE_HINT_ONLY);
354 printf("MEMZONE FLAG 16MB & HINT\n");
357 if (mz->hugepage_sz != RTE_PGSIZE_16G) {
358 printf("hugepage_sz not equal 16G\n");
361 mz = rte_memzone_reserve("flag_zone_16M", size,
362 SOCKET_ID_ANY, RTE_MEMZONE_16MB);
364 printf("MEMZONE FLAG 16MB\n");
369 if (hugepage_16MB_avail && hugepage_16GB_avail) {
370 mz = rte_memzone_reserve("flag_zone_16M_HINT", size,
372 RTE_MEMZONE_16MB|RTE_MEMZONE_16GB);
374 printf("BOTH SIZES SET\n");
383 test_memzone_reserve_max(void)
385 const struct rte_memzone *mz;
386 const struct rte_config *config;
387 const struct rte_memseg *ms;
394 /* get pointer to global configuration */
395 config = rte_eal_get_configuration();
397 ms = rte_eal_get_physmem_layout();
399 for (memseg_idx = 0; memseg_idx < RTE_MAX_MEMSEG; memseg_idx++){
400 /* ignore smaller memsegs as they can only get smaller */
401 if (ms[memseg_idx].len < maxlen)
404 /* align everything */
405 last_addr = RTE_PTR_ALIGN_CEIL(ms[memseg_idx].addr, RTE_CACHE_LINE_SIZE);
406 len = ms[memseg_idx].len - RTE_PTR_DIFF(last_addr, ms[memseg_idx].addr);
407 len &= ~((size_t) RTE_CACHE_LINE_MASK);
409 /* cycle through all memzones */
410 for (memzone_idx = 0; memzone_idx < RTE_MAX_MEMZONE; memzone_idx++) {
412 /* stop when reaching last allocated memzone */
413 if (config->mem_config->memzone[memzone_idx].addr == NULL)
416 /* check if the memzone is in our memseg and subtract length */
417 if ((config->mem_config->memzone[memzone_idx].addr >=
418 ms[memseg_idx].addr) &&
419 (config->mem_config->memzone[memzone_idx].addr <
420 (RTE_PTR_ADD(ms[memseg_idx].addr, ms[memseg_idx].len)))) {
421 /* since the zones can now be aligned and occasionally skip
422 * some space, we should calculate the length based on
423 * reported length and start addresses difference. Addresses
424 * are allocated sequentially so we don't need to worry about
425 * them being in the right order.
428 config->mem_config->memzone[memzone_idx].addr,
430 len -= config->mem_config->memzone[memzone_idx].len;
431 last_addr = RTE_PTR_ADD(config->mem_config->memzone[memzone_idx].addr,
432 (size_t) config->mem_config->memzone[memzone_idx].len);
436 /* we don't need to calculate offset here since length
437 * is always cache-aligned */
443 printf("There is no space left!\n");
447 mz = rte_memzone_reserve("max_zone", 0, SOCKET_ID_ANY, 0);
449 printf("Failed to reserve a big chunk of memory\n");
450 rte_dump_physmem_layout(stdout);
451 rte_memzone_dump(stdout);
455 if (mz->len != maxlen) {
456 printf("Memzone reserve with 0 size did not return bigest block\n");
457 printf("Expected size = %zu, actual size = %zu\n",
459 rte_dump_physmem_layout(stdout);
460 rte_memzone_dump(stdout);
468 test_memzone_reserve_max_aligned(void)
470 const struct rte_memzone *mz;
471 const struct rte_config *config;
472 const struct rte_memseg *ms;
475 uintptr_t addr_offset;
480 /* random alignment */
481 rte_srand((unsigned)rte_rdtsc());
482 const unsigned align = 1 << ((rte_rand() % 8) + 5); /* from 128 up to 4k alignment */
484 /* get pointer to global configuration */
485 config = rte_eal_get_configuration();
487 ms = rte_eal_get_physmem_layout();
491 for (memseg_idx = 0; memseg_idx < RTE_MAX_MEMSEG; memseg_idx++){
493 /* ignore smaller memsegs as they can only get smaller */
494 if (ms[memseg_idx].len < maxlen)
497 /* align everything */
498 last_addr = RTE_PTR_ALIGN_CEIL(ms[memseg_idx].addr, RTE_CACHE_LINE_SIZE);
499 len = ms[memseg_idx].len - RTE_PTR_DIFF(last_addr, ms[memseg_idx].addr);
500 len &= ~((size_t) RTE_CACHE_LINE_MASK);
502 /* cycle through all memzones */
503 for (memzone_idx = 0; memzone_idx < RTE_MAX_MEMZONE; memzone_idx++) {
505 /* stop when reaching last allocated memzone */
506 if (config->mem_config->memzone[memzone_idx].addr == NULL)
509 /* check if the memzone is in our memseg and subtract length */
510 if ((config->mem_config->memzone[memzone_idx].addr >=
511 ms[memseg_idx].addr) &&
512 (config->mem_config->memzone[memzone_idx].addr <
513 (RTE_PTR_ADD(ms[memseg_idx].addr, ms[memseg_idx].len)))) {
514 /* since the zones can now be aligned and occasionally skip
515 * some space, we should calculate the length based on
516 * reported length and start addresses difference.
518 len -= (uintptr_t) RTE_PTR_SUB(
519 config->mem_config->memzone[memzone_idx].addr,
520 (uintptr_t) last_addr);
521 len -= config->mem_config->memzone[memzone_idx].len;
523 RTE_PTR_ADD(config->mem_config->memzone[memzone_idx].addr,
524 (size_t) config->mem_config->memzone[memzone_idx].len);
528 /* make sure we get the alignment offset */
530 addr_offset = RTE_PTR_ALIGN_CEIL((uintptr_t) last_addr, align) - (uintptr_t) last_addr;
535 if (maxlen == 0 || maxlen == addr_offset) {
536 printf("There is no space left for biggest %u-aligned memzone!\n", align);
540 maxlen -= addr_offset;
542 mz = rte_memzone_reserve_aligned("max_zone_aligned", 0,
543 SOCKET_ID_ANY, 0, align);
545 printf("Failed to reserve a big chunk of memory\n");
546 rte_dump_physmem_layout(stdout);
547 rte_memzone_dump(stdout);
551 if (mz->len != maxlen) {
552 printf("Memzone reserve with 0 size and alignment %u did not return"
553 " bigest block\n", align);
554 printf("Expected size = %zu, actual size = %zu\n",
556 rte_dump_physmem_layout(stdout);
557 rte_memzone_dump(stdout);
565 test_memzone_aligned(void)
567 const struct rte_memzone *memzone_aligned_32;
568 const struct rte_memzone *memzone_aligned_128;
569 const struct rte_memzone *memzone_aligned_256;
570 const struct rte_memzone *memzone_aligned_512;
571 const struct rte_memzone *memzone_aligned_1024;
573 /* memzone that should automatically be adjusted to align on 64 bytes */
574 memzone_aligned_32 = rte_memzone_reserve_aligned("aligned_32", 100,
575 SOCKET_ID_ANY, 0, 32);
577 /* memzone that is supposed to be aligned on a 128 byte boundary */
578 memzone_aligned_128 = rte_memzone_reserve_aligned("aligned_128", 100,
579 SOCKET_ID_ANY, 0, 128);
581 /* memzone that is supposed to be aligned on a 256 byte boundary */
582 memzone_aligned_256 = rte_memzone_reserve_aligned("aligned_256", 100,
583 SOCKET_ID_ANY, 0, 256);
585 /* memzone that is supposed to be aligned on a 512 byte boundary */
586 memzone_aligned_512 = rte_memzone_reserve_aligned("aligned_512", 100,
587 SOCKET_ID_ANY, 0, 512);
589 /* memzone that is supposed to be aligned on a 1024 byte boundary */
590 memzone_aligned_1024 = rte_memzone_reserve_aligned("aligned_1024", 100,
591 SOCKET_ID_ANY, 0, 1024);
593 printf("check alignments and lengths\n");
594 if (memzone_aligned_32 == NULL) {
595 printf("Unable to reserve 64-byte aligned memzone!\n");
598 if ((memzone_aligned_32->phys_addr & RTE_CACHE_LINE_MASK) != 0)
600 if (((uintptr_t) memzone_aligned_32->addr & RTE_CACHE_LINE_MASK) != 0)
602 if ((memzone_aligned_32->len & RTE_CACHE_LINE_MASK) != 0)
605 if (memzone_aligned_128 == NULL) {
606 printf("Unable to reserve 128-byte aligned memzone!\n");
609 if ((memzone_aligned_128->phys_addr & 127) != 0)
611 if (((uintptr_t) memzone_aligned_128->addr & 127) != 0)
613 if ((memzone_aligned_128->len & RTE_CACHE_LINE_MASK) != 0)
616 if (memzone_aligned_256 == NULL) {
617 printf("Unable to reserve 256-byte aligned memzone!\n");
620 if ((memzone_aligned_256->phys_addr & 255) != 0)
622 if (((uintptr_t) memzone_aligned_256->addr & 255) != 0)
624 if ((memzone_aligned_256->len & RTE_CACHE_LINE_MASK) != 0)
627 if (memzone_aligned_512 == NULL) {
628 printf("Unable to reserve 512-byte aligned memzone!\n");
631 if ((memzone_aligned_512->phys_addr & 511) != 0)
633 if (((uintptr_t) memzone_aligned_512->addr & 511) != 0)
635 if ((memzone_aligned_512->len & RTE_CACHE_LINE_MASK) != 0)
638 if (memzone_aligned_1024 == NULL) {
639 printf("Unable to reserve 1024-byte aligned memzone!\n");
642 if ((memzone_aligned_1024->phys_addr & 1023) != 0)
644 if (((uintptr_t) memzone_aligned_1024->addr & 1023) != 0)
646 if ((memzone_aligned_1024->len & RTE_CACHE_LINE_MASK) != 0)
649 /* check that zones don't overlap */
650 printf("check overlapping\n");
651 if (is_memory_overlap(memzone_aligned_32->phys_addr, memzone_aligned_32->len,
652 memzone_aligned_128->phys_addr, memzone_aligned_128->len))
654 if (is_memory_overlap(memzone_aligned_32->phys_addr, memzone_aligned_32->len,
655 memzone_aligned_256->phys_addr, memzone_aligned_256->len))
657 if (is_memory_overlap(memzone_aligned_32->phys_addr, memzone_aligned_32->len,
658 memzone_aligned_512->phys_addr, memzone_aligned_512->len))
660 if (is_memory_overlap(memzone_aligned_32->phys_addr, memzone_aligned_32->len,
661 memzone_aligned_1024->phys_addr, memzone_aligned_1024->len))
663 if (is_memory_overlap(memzone_aligned_128->phys_addr, memzone_aligned_128->len,
664 memzone_aligned_256->phys_addr, memzone_aligned_256->len))
666 if (is_memory_overlap(memzone_aligned_128->phys_addr, memzone_aligned_128->len,
667 memzone_aligned_512->phys_addr, memzone_aligned_512->len))
669 if (is_memory_overlap(memzone_aligned_128->phys_addr, memzone_aligned_128->len,
670 memzone_aligned_1024->phys_addr, memzone_aligned_1024->len))
672 if (is_memory_overlap(memzone_aligned_256->phys_addr, memzone_aligned_256->len,
673 memzone_aligned_512->phys_addr, memzone_aligned_512->len))
675 if (is_memory_overlap(memzone_aligned_256->phys_addr, memzone_aligned_256->len,
676 memzone_aligned_1024->phys_addr, memzone_aligned_1024->len))
678 if (is_memory_overlap(memzone_aligned_512->phys_addr, memzone_aligned_512->len,
679 memzone_aligned_1024->phys_addr, memzone_aligned_1024->len))
685 check_memzone_bounded(const char *name, uint32_t len, uint32_t align,
688 const struct rte_memzone *mz;
691 bmask = ~((phys_addr_t)bound - 1);
693 if ((mz = rte_memzone_reserve_bounded(name, len, SOCKET_ID_ANY, 0,
694 align, bound)) == NULL) {
695 printf("%s(%s): memzone creation failed\n",
700 if ((mz->phys_addr & ((phys_addr_t)align - 1)) != 0) {
701 printf("%s(%s): invalid phys addr alignment\n",
706 if (((uintptr_t) mz->addr & ((uintptr_t)align - 1)) != 0) {
707 printf("%s(%s): invalid virtual addr alignment\n",
712 if ((mz->len & RTE_CACHE_LINE_MASK) != 0 || mz->len < len ||
713 mz->len < RTE_CACHE_LINE_SIZE) {
714 printf("%s(%s): invalid length\n",
719 if ((mz->phys_addr & bmask) !=
720 ((mz->phys_addr + mz->len - 1) & bmask)) {
721 printf("%s(%s): invalid memzone boundary %u crossed\n",
722 __func__, mz->name, bound);
730 test_memzone_bounded(void)
732 const struct rte_memzone *memzone_err;
736 /* should fail as boundary is not power of two */
737 name = "bounded_error_31";
738 if ((memzone_err = rte_memzone_reserve_bounded(name,
739 100, SOCKET_ID_ANY, 0, 32, UINT32_MAX)) != NULL) {
740 printf("%s(%s)created a memzone with invalid boundary "
741 "conditions\n", __func__, memzone_err->name);
745 /* should fail as len is greater then boundary */
746 name = "bounded_error_32";
747 if ((memzone_err = rte_memzone_reserve_bounded(name,
748 100, SOCKET_ID_ANY, 0, 32, 32)) != NULL) {
749 printf("%s(%s)created a memzone with invalid boundary "
750 "conditions\n", __func__, memzone_err->name);
754 if ((rc = check_memzone_bounded("bounded_128", 100, 128, 128)) != 0)
757 if ((rc = check_memzone_bounded("bounded_256", 100, 256, 128)) != 0)
760 if ((rc = check_memzone_bounded("bounded_1K", 100, 64, 1024)) != 0)
763 if ((rc = check_memzone_bounded("bounded_1K_MAX", 0, 64, 1024)) != 0)
770 test_memzone_reserve_memory_in_smallest_segment(void)
772 const struct rte_memzone *mz;
773 const struct rte_memseg *ms, *min_ms, *prev_min_ms;
774 size_t min_len, prev_min_len;
775 const struct rte_config *config;
778 config = rte_eal_get_configuration();
780 min_ms = NULL; /*< smallest segment */
781 prev_min_ms = NULL; /*< second smallest segment */
783 /* find two smallest segments */
784 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
785 ms = &config->mem_config->free_memseg[i];
787 if (ms->addr == NULL)
794 else if (min_ms->len > ms->len) {
795 /* set last smallest to second last */
796 prev_min_ms = min_ms;
798 /* set new smallest */
800 } else if ((prev_min_ms == NULL)
801 || (prev_min_ms->len > ms->len))
805 if (min_ms == NULL || prev_min_ms == NULL) {
806 printf("Smallest segments not found!\n");
810 min_len = min_ms->len;
811 prev_min_len = prev_min_ms->len;
813 /* try reserving a memzone in the smallest memseg */
814 mz = rte_memzone_reserve("smallest_mz", RTE_CACHE_LINE_SIZE,
817 printf("Failed to reserve memory from smallest memseg!\n");
820 if (prev_min_ms->len != prev_min_len &&
821 min_ms->len != min_len - RTE_CACHE_LINE_SIZE) {
822 printf("Reserved memory from wrong memseg!\n");
829 /* this test is a bit tricky, and thus warrants explanation.
831 * first, we find two smallest memsegs to conduct our experiments on.
833 * then, we bring them within alignment from each other: if second segment is
834 * twice+ as big as the first, reserve memory from that segment; if second
835 * segment is comparable in length to the first, then cut the first segment
836 * down until it becomes less than half of second segment, and then cut down
837 * the second segment to be within alignment of the first.
839 * then, we have to pass the following test: if segments are within alignment
840 * of each other (that is, the difference is less than 256 bytes, which is what
841 * our alignment will be), segment with smallest offset should be picked.
843 * we know that min_ms will be our smallest segment, so we need to make sure
844 * that we adjust the alignments so that the bigger segment has smallest
845 * alignment (in our case, smallest segment will have 64-byte alignment, while
846 * bigger segment will have 128-byte alignment).
849 test_memzone_reserve_memory_with_smallest_offset(void)
851 const struct rte_memseg *ms, *min_ms, *prev_min_ms;
852 size_t len, min_len, prev_min_len;
853 const struct rte_config *config;
856 config = rte_eal_get_configuration();
858 min_ms = NULL; /*< smallest segment */
859 prev_min_ms = NULL; /*< second smallest segment */
860 align = RTE_CACHE_LINE_SIZE * 4;
862 /* find two smallest segments */
863 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
864 ms = &config->mem_config->free_memseg[i];
866 if (ms->addr == NULL)
873 else if (min_ms->len > ms->len) {
874 /* set last smallest to second last */
875 prev_min_ms = min_ms;
877 /* set new smallest */
879 } else if ((prev_min_ms == NULL)
880 || (prev_min_ms->len > ms->len)) {
885 if (min_ms == NULL || prev_min_ms == NULL) {
886 printf("Smallest segments not found!\n");
890 prev_min_len = prev_min_ms->len;
891 min_len = min_ms->len;
893 /* if smallest segment is bigger than half of bigger segment */
894 if (prev_min_ms->len - min_ms->len <= min_ms->len) {
896 len = (min_ms->len * 2) - prev_min_ms->len;
898 /* make sure final length is *not* aligned */
899 while (((min_ms->addr_64 + len) & (align-1)) == 0)
900 len += RTE_CACHE_LINE_SIZE;
902 if (rte_memzone_reserve("dummy_mz1", len, SOCKET_ID_ANY, 0) == NULL) {
903 printf("Cannot reserve memory!\n");
907 /* check if we got memory from correct segment */
908 if (min_ms->len != min_len - len) {
909 printf("Reserved memory from wrong segment!\n");
913 /* if we don't need to touch smallest segment but it's aligned */
914 else if ((min_ms->addr_64 & (align-1)) == 0) {
915 if (rte_memzone_reserve("align_mz1", RTE_CACHE_LINE_SIZE,
916 SOCKET_ID_ANY, 0) == NULL) {
917 printf("Cannot reserve memory!\n");
920 if (min_ms->len != min_len - RTE_CACHE_LINE_SIZE) {
921 printf("Reserved memory from wrong segment!\n");
926 /* if smallest segment is less than half of bigger segment */
927 if (prev_min_ms->len - min_ms->len > min_ms->len) {
928 len = prev_min_ms->len - min_ms->len - align;
930 /* make sure final length is aligned */
931 while (((prev_min_ms->addr_64 + len) & (align-1)) != 0)
932 len += RTE_CACHE_LINE_SIZE;
934 if (rte_memzone_reserve("dummy_mz2", len, SOCKET_ID_ANY, 0) == NULL) {
935 printf("Cannot reserve memory!\n");
939 /* check if we got memory from correct segment */
940 if (prev_min_ms->len != prev_min_len - len) {
941 printf("Reserved memory from wrong segment!\n");
945 len = RTE_CACHE_LINE_SIZE;
949 prev_min_len = prev_min_ms->len;
950 min_len = min_ms->len;
952 if (min_len >= prev_min_len || prev_min_len - min_len > (unsigned) align) {
953 printf("Segments are of wrong lengths!\n");
957 /* try reserving from a bigger segment */
958 if (rte_memzone_reserve_aligned("smallest_offset", len, SOCKET_ID_ANY, 0, align) ==
960 printf("Cannot reserve memory!\n");
964 /* check if we got memory from correct segment */
965 if (min_ms->len != min_len && prev_min_ms->len != (prev_min_len - len)) {
966 printf("Reserved memory from segment with smaller offset!\n");
974 test_memzone_reserve_remainder(void)
976 const struct rte_memzone *mz1, *mz2;
977 const struct rte_memseg *ms, *min_ms = NULL;
979 const struct rte_config *config;
983 align = RTE_CACHE_LINE_SIZE;
985 config = rte_eal_get_configuration();
987 /* find minimum free contiguous length */
988 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
989 ms = &config->mem_config->free_memseg[i];
991 if (ms->addr == NULL)
996 if (min_len == 0 || ms->len < min_len) {
1000 /* find maximum alignment this segment is able to hold */
1001 align = RTE_CACHE_LINE_SIZE;
1002 while ((ms->addr_64 & (align-1)) == 0) {
1008 if (min_ms == NULL) {
1009 printf("Minimal sized segment not found!\n");
1013 /* try reserving min_len bytes with alignment - this should not affect our
1014 * memseg, the memory will be taken from a different one.
1016 mz1 = rte_memzone_reserve_aligned("reserve_remainder_1", min_len,
1017 SOCKET_ID_ANY, 0, align);
1019 printf("Failed to reserve %zu bytes aligned on %i bytes\n", min_len,
1023 if (min_ms->len != min_len) {
1024 printf("Memseg memory should not have been reserved!\n");
1028 /* try reserving min_len bytes with less alignment - this should fill up
1031 mz2 = rte_memzone_reserve("reserve_remainder_2", min_len,
1034 printf("Failed to reserve %zu bytes\n", min_len);
1037 if (min_ms->len != 0) {
1038 printf("Memseg memory should have been reserved!\n");
1048 const struct rte_memzone *memzone1;
1049 const struct rte_memzone *memzone2;
1050 const struct rte_memzone *memzone3;
1051 const struct rte_memzone *memzone4;
1052 const struct rte_memzone *mz;
1054 memzone1 = rte_memzone_reserve("testzone1", 100,
1057 memzone2 = rte_memzone_reserve("testzone2", 1000,
1060 memzone3 = rte_memzone_reserve("testzone3", 1000,
1063 memzone4 = rte_memzone_reserve("testzone4", 1024,
1066 /* memzone3 may be NULL if we don't have NUMA */
1067 if (memzone1 == NULL || memzone2 == NULL || memzone4 == NULL)
1070 rte_memzone_dump(stdout);
1072 /* check cache-line alignments */
1073 printf("check alignments and lengths\n");
1075 if ((memzone1->phys_addr & RTE_CACHE_LINE_MASK) != 0)
1077 if ((memzone2->phys_addr & RTE_CACHE_LINE_MASK) != 0)
1079 if (memzone3 != NULL && (memzone3->phys_addr & RTE_CACHE_LINE_MASK) != 0)
1081 if ((memzone1->len & RTE_CACHE_LINE_MASK) != 0 || memzone1->len == 0)
1083 if ((memzone2->len & RTE_CACHE_LINE_MASK) != 0 || memzone2->len == 0)
1085 if (memzone3 != NULL && ((memzone3->len & RTE_CACHE_LINE_MASK) != 0 ||
1086 memzone3->len == 0))
1088 if (memzone4->len != 1024)
1091 /* check that zones don't overlap */
1092 printf("check overlapping\n");
1094 if (is_memory_overlap(memzone1->phys_addr, memzone1->len,
1095 memzone2->phys_addr, memzone2->len))
1097 if (memzone3 != NULL &&
1098 is_memory_overlap(memzone1->phys_addr, memzone1->len,
1099 memzone3->phys_addr, memzone3->len))
1101 if (memzone3 != NULL &&
1102 is_memory_overlap(memzone2->phys_addr, memzone2->len,
1103 memzone3->phys_addr, memzone3->len))
1106 printf("check socket ID\n");
1108 /* memzone2 must be on socket id 0 and memzone3 on socket 1 */
1109 if (memzone2->socket_id != 0)
1111 if (memzone3 != NULL && memzone3->socket_id != 1)
1114 printf("test zone lookup\n");
1115 mz = rte_memzone_lookup("testzone1");
1119 printf("test duplcate zone name\n");
1120 mz = rte_memzone_reserve("testzone1", 100,
1125 printf("test reserving memzone with bigger size than the maximum\n");
1126 if (test_memzone_reserving_zone_size_bigger_than_the_maximum() < 0)
1129 printf("test reserving memory in smallest segments\n");
1130 if (test_memzone_reserve_memory_in_smallest_segment() < 0)
1133 printf("test reserving memory in segments with smallest offsets\n");
1134 if (test_memzone_reserve_memory_with_smallest_offset() < 0)
1137 printf("test memzone_reserve flags\n");
1138 if (test_memzone_reserve_flags() < 0)
1141 printf("test alignment for memzone_reserve\n");
1142 if (test_memzone_aligned() < 0)
1145 printf("test boundary alignment for memzone_reserve\n");
1146 if (test_memzone_bounded() < 0)
1149 printf("test invalid alignment for memzone_reserve\n");
1150 if (test_memzone_invalid_alignment() < 0)
1153 printf("test reserving amounts of memory equal to segment's length\n");
1154 if (test_memzone_reserve_remainder() < 0)
1157 printf("test reserving the largest size memzone possible\n");
1158 if (test_memzone_reserve_max() < 0)
1161 printf("test reserving the largest size aligned memzone possible\n");
1162 if (test_memzone_reserve_max_aligned() < 0)
1168 static struct test_command memzone_cmd = {
1169 .command = "memzone_autotest",
1170 .callback = test_memzone,
1172 REGISTER_TEST_COMMAND(memzone_cmd);