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 <cmdline_parse.h>
41 #include <rte_random.h>
42 #include <rte_cycles.h>
43 #include <rte_memory.h>
44 #include <rte_memzone.h>
45 #include <rte_tailq.h>
47 #include <rte_eal_memconfig.h>
48 #include <rte_common.h>
49 #include <rte_string_fns.h>
57 * - Search for three reserved zones or reserve them if they do not exist:
59 * - One is on any socket id.
60 * - The second is on socket 0.
61 * - The last one is on socket 1 (if socket 1 exists).
63 * - Check that the zones exist.
65 * - Check that the zones are cache-aligned.
67 * - Check that zones do not overlap.
69 * - Check that the zones are on the correct socket id.
71 * - Check that a lookup of the first zone returns the same pointer.
73 * - Check that it is not possible to create another zone with the
74 * same name as an existing zone.
76 * - Check flags for specific huge page size reservation
79 /* Test if memory overlaps: return 1 if true, or 0 if false. */
81 is_memory_overlap(phys_addr_t ptr1, size_t len1, phys_addr_t ptr2, size_t len2)
83 if (ptr2 >= ptr1 && (ptr2 - ptr1) < len1)
85 else if (ptr2 < ptr1 && (ptr1 - ptr2) < len2)
91 test_memzone_invalid_alignment(void)
93 const struct rte_memzone * mz;
95 mz = rte_memzone_lookup("invalid_alignment");
97 printf("Zone with invalid alignment has been reserved\n");
101 mz = rte_memzone_reserve_aligned("invalid_alignment", 100,
102 SOCKET_ID_ANY, 0, 100);
104 printf("Zone with invalid alignment has been reserved\n");
111 test_memzone_reserving_zone_size_bigger_than_the_maximum(void)
113 const struct rte_memzone * mz;
115 mz = rte_memzone_lookup("zone_size_bigger_than_the_maximum");
117 printf("zone_size_bigger_than_the_maximum has been reserved\n");
121 mz = rte_memzone_reserve("zone_size_bigger_than_the_maximum", (size_t)-1,
124 printf("It is impossible to reserve such big a memzone\n");
132 test_memzone_reserve_flags(void)
134 const struct rte_memzone *mz;
135 const struct rte_memseg *ms;
136 int hugepage_2MB_avail = 0;
137 int hugepage_1GB_avail = 0;
138 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;
147 /* Display the availability of 2MB and 1GB pages */
148 if (hugepage_2MB_avail)
149 printf("2MB Huge pages available\n");
150 if (hugepage_1GB_avail)
151 printf("1GB Huge pages available\n");
153 * If 2MB pages available, check that a small memzone is correctly
154 * reserved from 2MB huge pages when requested by the RTE_MEMZONE_2MB flag.
155 * Also check that RTE_MEMZONE_SIZE_HINT_ONLY flag only defaults to an
156 * available page size (i.e 1GB ) when 2MB pages are unavailable.
158 if (hugepage_2MB_avail) {
159 mz = rte_memzone_reserve("flag_zone_2M", size, SOCKET_ID_ANY,
162 printf("MEMZONE FLAG 2MB\n");
165 if (mz->hugepage_sz != RTE_PGSIZE_2M) {
166 printf("hugepage_sz not equal 2M\n");
170 mz = rte_memzone_reserve("flag_zone_2M_HINT", size, SOCKET_ID_ANY,
171 RTE_MEMZONE_2MB|RTE_MEMZONE_SIZE_HINT_ONLY);
173 printf("MEMZONE FLAG 2MB\n");
176 if (mz->hugepage_sz != RTE_PGSIZE_2M) {
177 printf("hugepage_sz not equal 2M\n");
181 /* Check if 1GB huge pages are unavailable, that function fails unless
182 * HINT flag is indicated
184 if (!hugepage_1GB_avail) {
185 mz = rte_memzone_reserve("flag_zone_1G_HINT", size, SOCKET_ID_ANY,
186 RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY);
188 printf("MEMZONE FLAG 1GB & HINT\n");
191 if (mz->hugepage_sz != RTE_PGSIZE_2M) {
192 printf("hugepage_sz not equal 2M\n");
196 mz = rte_memzone_reserve("flag_zone_1G", size, SOCKET_ID_ANY,
199 printf("MEMZONE FLAG 1GB\n");
205 /*As with 2MB tests above for 1GB huge page requests*/
206 if (hugepage_1GB_avail) {
207 mz = rte_memzone_reserve("flag_zone_1G", size, SOCKET_ID_ANY,
210 printf("MEMZONE FLAG 1GB\n");
213 if (mz->hugepage_sz != RTE_PGSIZE_1G) {
214 printf("hugepage_sz not equal 1G\n");
218 mz = rte_memzone_reserve("flag_zone_1G_HINT", size, SOCKET_ID_ANY,
219 RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY);
221 printf("MEMZONE FLAG 1GB\n");
224 if (mz->hugepage_sz != RTE_PGSIZE_1G) {
225 printf("hugepage_sz not equal 1G\n");
229 /* Check if 1GB huge pages are unavailable, that function fails unless
230 * HINT flag is indicated
232 if (!hugepage_2MB_avail) {
233 mz = rte_memzone_reserve("flag_zone_2M_HINT", size, SOCKET_ID_ANY,
234 RTE_MEMZONE_2MB|RTE_MEMZONE_SIZE_HINT_ONLY);
236 printf("MEMZONE FLAG 2MB & HINT\n");
239 if (mz->hugepage_sz != RTE_PGSIZE_1G) {
240 printf("hugepage_sz not equal 1G\n");
243 mz = rte_memzone_reserve("flag_zone_2M", size, SOCKET_ID_ANY,
246 printf("MEMZONE FLAG 2MB\n");
251 if (hugepage_2MB_avail && hugepage_1GB_avail) {
252 mz = rte_memzone_reserve("flag_zone_2M_HINT", size, SOCKET_ID_ANY,
253 RTE_MEMZONE_2MB|RTE_MEMZONE_1GB);
255 printf("BOTH SIZES SET\n");
264 test_memzone_reserve_max(void)
266 const struct rte_memzone *mz;
267 const struct rte_config *config;
268 const struct rte_memseg *ms;
275 /* get pointer to global configuration */
276 config = rte_eal_get_configuration();
278 ms = rte_eal_get_physmem_layout();
280 for (memseg_idx = 0; memseg_idx < RTE_MAX_MEMSEG; memseg_idx++){
281 /* ignore smaller memsegs as they can only get smaller */
282 if (ms[memseg_idx].len < maxlen)
285 /* align everything */
286 last_addr = RTE_PTR_ALIGN_CEIL(ms[memseg_idx].addr, CACHE_LINE_SIZE);
287 len = ms[memseg_idx].len - RTE_PTR_DIFF(last_addr, ms[memseg_idx].addr);
288 len &= ~((size_t) CACHE_LINE_MASK);
290 /* cycle through all memzones */
291 for (memzone_idx = 0; memzone_idx < RTE_MAX_MEMZONE; memzone_idx++) {
293 /* stop when reaching last allocated memzone */
294 if (config->mem_config->memzone[memzone_idx].addr == NULL)
297 /* check if the memzone is in our memseg and subtract length */
298 if ((config->mem_config->memzone[memzone_idx].addr >=
299 ms[memseg_idx].addr) &&
300 (config->mem_config->memzone[memzone_idx].addr <
301 (RTE_PTR_ADD(ms[memseg_idx].addr, ms[memseg_idx].len)))) {
302 /* since the zones can now be aligned and occasionally skip
303 * some space, we should calculate the length based on
304 * reported length and start addresses difference. Addresses
305 * are allocated sequentially so we don't need to worry about
306 * them being in the right order.
309 config->mem_config->memzone[memzone_idx].addr,
311 len -= config->mem_config->memzone[memzone_idx].len;
312 last_addr = RTE_PTR_ADD(config->mem_config->memzone[memzone_idx].addr,
313 (size_t) config->mem_config->memzone[memzone_idx].len);
317 /* we don't need to calculate offset here since length
318 * is always cache-aligned */
324 printf("There is no space left!\n");
328 mz = rte_memzone_reserve("max_zone", 0, SOCKET_ID_ANY, 0);
330 printf("Failed to reserve a big chunk of memory\n");
331 rte_dump_physmem_layout();
336 if (mz->len != maxlen) {
337 printf("Memzone reserve with 0 size did not return bigest block\n");
338 printf("Expected size = %zu, actual size = %zu\n",
340 rte_dump_physmem_layout();
349 test_memzone_reserve_max_aligned(void)
351 const struct rte_memzone *mz;
352 const struct rte_config *config;
353 const struct rte_memseg *ms;
356 uintptr_t addr_offset;
361 /* random alignment */
362 rte_srand((unsigned)rte_rdtsc());
363 const unsigned align = 1 << ((rte_rand() % 8) + 5); /* from 128 up to 4k alignment */
365 /* get pointer to global configuration */
366 config = rte_eal_get_configuration();
368 ms = rte_eal_get_physmem_layout();
372 for (memseg_idx = 0; memseg_idx < RTE_MAX_MEMSEG; memseg_idx++){
374 /* ignore smaller memsegs as they can only get smaller */
375 if (ms[memseg_idx].len < maxlen)
378 /* align everything */
379 last_addr = RTE_PTR_ALIGN_CEIL(ms[memseg_idx].addr, CACHE_LINE_SIZE);
380 len = ms[memseg_idx].len - RTE_PTR_DIFF(last_addr, ms[memseg_idx].addr);
381 len &= ~((size_t) CACHE_LINE_MASK);
383 /* cycle through all memzones */
384 for (memzone_idx = 0; memzone_idx < RTE_MAX_MEMZONE; memzone_idx++) {
386 /* stop when reaching last allocated memzone */
387 if (config->mem_config->memzone[memzone_idx].addr == NULL)
390 /* check if the memzone is in our memseg and subtract length */
391 if ((config->mem_config->memzone[memzone_idx].addr >=
392 ms[memseg_idx].addr) &&
393 (config->mem_config->memzone[memzone_idx].addr <
394 (RTE_PTR_ADD(ms[memseg_idx].addr, ms[memseg_idx].len)))) {
395 /* since the zones can now be aligned and occasionally skip
396 * some space, we should calculate the length based on
397 * reported length and start addresses difference.
399 len -= (uintptr_t) RTE_PTR_SUB(
400 config->mem_config->memzone[memzone_idx].addr,
401 (uintptr_t) last_addr);
402 len -= config->mem_config->memzone[memzone_idx].len;
404 RTE_PTR_ADD(config->mem_config->memzone[memzone_idx].addr,
405 (size_t) config->mem_config->memzone[memzone_idx].len);
409 /* make sure we get the alignment offset */
411 addr_offset = RTE_PTR_ALIGN_CEIL((uintptr_t) last_addr, align) - (uintptr_t) last_addr;
416 if (maxlen == 0 || maxlen == addr_offset) {
417 printf("There is no space left for biggest %u-aligned memzone!\n", align);
421 maxlen -= addr_offset;
423 mz = rte_memzone_reserve_aligned("max_zone_aligned", 0,
424 SOCKET_ID_ANY, 0, align);
426 printf("Failed to reserve a big chunk of memory\n");
427 rte_dump_physmem_layout();
432 if (mz->len != maxlen) {
433 printf("Memzone reserve with 0 size and alignment %u did not return"
434 " bigest block\n", align);
435 printf("Expected size = %zu, actual size = %zu\n",
437 rte_dump_physmem_layout();
446 test_memzone_aligned(void)
448 const struct rte_memzone *memzone_aligned_32;
449 const struct rte_memzone *memzone_aligned_128;
450 const struct rte_memzone *memzone_aligned_256;
451 const struct rte_memzone *memzone_aligned_512;
452 const struct rte_memzone *memzone_aligned_1024;
454 /* memzone that should automatically be adjusted to align on 64 bytes */
455 memzone_aligned_32 = rte_memzone_reserve_aligned("aligned_32", 100,
456 SOCKET_ID_ANY, 0, 32);
458 /* memzone that is supposed to be aligned on a 128 byte boundary */
459 memzone_aligned_128 = rte_memzone_reserve_aligned("aligned_128", 100,
460 SOCKET_ID_ANY, 0, 128);
462 /* memzone that is supposed to be aligned on a 256 byte boundary */
463 memzone_aligned_256 = rte_memzone_reserve_aligned("aligned_256", 100,
464 SOCKET_ID_ANY, 0, 256);
466 /* memzone that is supposed to be aligned on a 512 byte boundary */
467 memzone_aligned_512 = rte_memzone_reserve_aligned("aligned_512", 100,
468 SOCKET_ID_ANY, 0, 512);
470 /* memzone that is supposed to be aligned on a 1024 byte boundary */
471 memzone_aligned_1024 = rte_memzone_reserve_aligned("aligned_1024", 100,
472 SOCKET_ID_ANY, 0, 1024);
474 printf("check alignments and lengths\n");
475 if (memzone_aligned_32 == NULL) {
476 printf("Unable to reserve 64-byte aligned memzone!\n");
479 if ((memzone_aligned_32->phys_addr & CACHE_LINE_MASK) != 0)
481 if (((uintptr_t) memzone_aligned_32->addr & CACHE_LINE_MASK) != 0)
483 if ((memzone_aligned_32->len & CACHE_LINE_MASK) != 0)
486 if (memzone_aligned_128 == NULL) {
487 printf("Unable to reserve 128-byte aligned memzone!\n");
490 if ((memzone_aligned_128->phys_addr & 127) != 0)
492 if (((uintptr_t) memzone_aligned_128->addr & 127) != 0)
494 if ((memzone_aligned_128->len & CACHE_LINE_MASK) != 0)
497 if (memzone_aligned_256 == NULL) {
498 printf("Unable to reserve 256-byte aligned memzone!\n");
501 if ((memzone_aligned_256->phys_addr & 255) != 0)
503 if (((uintptr_t) memzone_aligned_256->addr & 255) != 0)
505 if ((memzone_aligned_256->len & CACHE_LINE_MASK) != 0)
508 if (memzone_aligned_512 == NULL) {
509 printf("Unable to reserve 512-byte aligned memzone!\n");
512 if ((memzone_aligned_512->phys_addr & 511) != 0)
514 if (((uintptr_t) memzone_aligned_512->addr & 511) != 0)
516 if ((memzone_aligned_512->len & CACHE_LINE_MASK) != 0)
519 if (memzone_aligned_1024 == NULL) {
520 printf("Unable to reserve 1024-byte aligned memzone!\n");
523 if ((memzone_aligned_1024->phys_addr & 1023) != 0)
525 if (((uintptr_t) memzone_aligned_1024->addr & 1023) != 0)
527 if ((memzone_aligned_1024->len & CACHE_LINE_MASK) != 0)
530 /* check that zones don't overlap */
531 printf("check overlapping\n");
532 if (is_memory_overlap(memzone_aligned_32->phys_addr, memzone_aligned_32->len,
533 memzone_aligned_128->phys_addr, memzone_aligned_128->len))
535 if (is_memory_overlap(memzone_aligned_32->phys_addr, memzone_aligned_32->len,
536 memzone_aligned_256->phys_addr, memzone_aligned_256->len))
538 if (is_memory_overlap(memzone_aligned_32->phys_addr, memzone_aligned_32->len,
539 memzone_aligned_512->phys_addr, memzone_aligned_512->len))
541 if (is_memory_overlap(memzone_aligned_32->phys_addr, memzone_aligned_32->len,
542 memzone_aligned_1024->phys_addr, memzone_aligned_1024->len))
544 if (is_memory_overlap(memzone_aligned_128->phys_addr, memzone_aligned_128->len,
545 memzone_aligned_256->phys_addr, memzone_aligned_256->len))
547 if (is_memory_overlap(memzone_aligned_128->phys_addr, memzone_aligned_128->len,
548 memzone_aligned_512->phys_addr, memzone_aligned_512->len))
550 if (is_memory_overlap(memzone_aligned_128->phys_addr, memzone_aligned_128->len,
551 memzone_aligned_1024->phys_addr, memzone_aligned_1024->len))
553 if (is_memory_overlap(memzone_aligned_256->phys_addr, memzone_aligned_256->len,
554 memzone_aligned_512->phys_addr, memzone_aligned_512->len))
556 if (is_memory_overlap(memzone_aligned_256->phys_addr, memzone_aligned_256->len,
557 memzone_aligned_1024->phys_addr, memzone_aligned_1024->len))
559 if (is_memory_overlap(memzone_aligned_512->phys_addr, memzone_aligned_512->len,
560 memzone_aligned_1024->phys_addr, memzone_aligned_1024->len))
566 check_memzone_bounded(const char *name, uint32_t len, uint32_t align,
569 const struct rte_memzone *mz;
572 bmask = ~((phys_addr_t)bound - 1);
574 if ((mz = rte_memzone_reserve_bounded(name, len, SOCKET_ID_ANY, 0,
575 align, bound)) == NULL) {
576 printf("%s(%s): memzone creation failed\n",
581 if ((mz->phys_addr & ((phys_addr_t)align - 1)) != 0) {
582 printf("%s(%s): invalid phys addr alignment\n",
587 if (((uintptr_t) mz->addr & ((uintptr_t)align - 1)) != 0) {
588 printf("%s(%s): invalid virtual addr alignment\n",
593 if ((mz->len & CACHE_LINE_MASK) != 0 || mz->len < len ||
594 mz->len < CACHE_LINE_SIZE) {
595 printf("%s(%s): invalid length\n",
600 if ((mz->phys_addr & bmask) !=
601 ((mz->phys_addr + mz->len - 1) & bmask)) {
602 printf("%s(%s): invalid memzone boundary %u crossed\n",
603 __func__, mz->name, bound);
611 test_memzone_bounded(void)
613 const struct rte_memzone *memzone_err;
617 /* should fail as boundary is not power of two */
618 name = "bounded_error_31";
619 if ((memzone_err = rte_memzone_reserve_bounded(name,
620 100, SOCKET_ID_ANY, 0, 32, UINT32_MAX)) != NULL) {
621 printf("%s(%s)created a memzone with invalid boundary "
622 "conditions\n", __func__, memzone_err->name);
626 /* should fail as len is greater then boundary */
627 name = "bounded_error_32";
628 if ((memzone_err = rte_memzone_reserve_bounded(name,
629 100, SOCKET_ID_ANY, 0, 32, 32)) != NULL) {
630 printf("%s(%s)created a memzone with invalid boundary "
631 "conditions\n", __func__, memzone_err->name);
635 if ((rc = check_memzone_bounded("bounded_128", 100, 128, 128)) != 0)
638 if ((rc = check_memzone_bounded("bounded_256", 100, 256, 128)) != 0)
641 if ((rc = check_memzone_bounded("bounded_1K", 100, 64, 1024)) != 0)
644 if ((rc = check_memzone_bounded("bounded_1K_MAX", 0, 64, 1024)) != 0)
651 test_memzone_reserve_memory_in_smallest_segment(void)
653 const struct rte_memzone *mz;
654 const struct rte_memseg *ms, *min_ms, *prev_min_ms;
655 size_t min_len, prev_min_len;
656 const struct rte_config *config;
659 config = rte_eal_get_configuration();
661 min_ms = NULL; /*< smallest segment */
662 prev_min_ms = NULL; /*< second smallest segment */
664 /* find two smallest segments */
665 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
666 ms = &config->mem_config->free_memseg[i];
668 if (ms->addr == NULL)
675 else if (min_ms->len > ms->len) {
676 /* set last smallest to second last */
677 prev_min_ms = min_ms;
679 /* set new smallest */
682 else if (prev_min_ms == NULL) {
687 if (min_ms == NULL || prev_min_ms == NULL) {
688 printf("Smallest segments not found!\n");
692 min_len = min_ms->len;
693 prev_min_len = prev_min_ms->len;
695 /* try reserving a memzone in the smallest memseg */
696 mz = rte_memzone_reserve("smallest_mz", CACHE_LINE_SIZE,
699 printf("Failed to reserve memory from smallest memseg!\n");
702 if (prev_min_ms->len != prev_min_len &&
703 min_ms->len != min_len - CACHE_LINE_SIZE) {
704 printf("Reserved memory from wrong memseg!\n");
711 /* this test is a bit tricky, and thus warrants explanation.
713 * first, we find two smallest memsegs to conduct our experiments on.
715 * then, we bring them within alignment from each other: if second segment is
716 * twice+ as big as the first, reserve memory from that segment; if second
717 * segment is comparable in length to the first, then cut the first segment
718 * down until it becomes less than half of second segment, and then cut down
719 * the second segment to be within alignment of the first.
721 * then, we have to pass the following test: if segments are within alignment
722 * of each other (that is, the difference is less than 256 bytes, which is what
723 * our alignment will be), segment with smallest offset should be picked.
725 * we know that min_ms will be our smallest segment, so we need to make sure
726 * that we adjust the alignments so that the bigger segment has smallest
727 * alignment (in our case, smallest segment will have 64-byte alignment, while
728 * bigger segment will have 128-byte alignment).
731 test_memzone_reserve_memory_with_smallest_offset(void)
733 const struct rte_memseg *ms, *min_ms, *prev_min_ms;
734 size_t len, min_len, prev_min_len;
735 const struct rte_config *config;
738 config = rte_eal_get_configuration();
740 min_ms = NULL; /*< smallest segment */
741 prev_min_ms = NULL; /*< second smallest segment */
742 align = CACHE_LINE_SIZE * 4;
744 /* find two smallest segments */
745 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
746 ms = &config->mem_config->free_memseg[i];
748 if (ms->addr == NULL)
755 else if (min_ms->len > ms->len) {
756 /* set last smallest to second last */
757 prev_min_ms = min_ms;
759 /* set new smallest */
762 else if (prev_min_ms == NULL) {
767 if (min_ms == NULL || prev_min_ms == NULL) {
768 printf("Smallest segments not found!\n");
772 prev_min_len = prev_min_ms->len;
773 min_len = min_ms->len;
775 /* if smallest segment is bigger than half of bigger segment */
776 if (prev_min_ms->len - min_ms->len <= min_ms->len) {
778 len = (min_ms->len * 2) - prev_min_ms->len;
780 /* make sure final length is *not* aligned */
781 while (((min_ms->addr_64 + len) & (align-1)) == 0)
782 len += CACHE_LINE_SIZE;
784 if (rte_memzone_reserve("dummy_mz1", len, SOCKET_ID_ANY, 0) == NULL) {
785 printf("Cannot reserve memory!\n");
789 /* check if we got memory from correct segment */
790 if (min_ms->len != min_len - len) {
791 printf("Reserved memory from wrong segment!\n");
795 /* if we don't need to touch smallest segment but it's aligned */
796 else if ((min_ms->addr_64 & (align-1)) == 0) {
797 if (rte_memzone_reserve("align_mz1", CACHE_LINE_SIZE,
798 SOCKET_ID_ANY, 0) == NULL) {
799 printf("Cannot reserve memory!\n");
802 if (min_ms->len != min_len - CACHE_LINE_SIZE) {
803 printf("Reserved memory from wrong segment!\n");
808 /* if smallest segment is less than half of bigger segment */
809 if (prev_min_ms->len - min_ms->len > min_ms->len) {
810 len = prev_min_ms->len - min_ms->len - align;
812 /* make sure final length is aligned */
813 while (((prev_min_ms->addr_64 + len) & (align-1)) != 0)
814 len += CACHE_LINE_SIZE;
816 if (rte_memzone_reserve("dummy_mz2", len, SOCKET_ID_ANY, 0) == NULL) {
817 printf("Cannot reserve memory!\n");
821 /* check if we got memory from correct segment */
822 if (prev_min_ms->len != prev_min_len - len) {
823 printf("Reserved memory from wrong segment!\n");
827 len = CACHE_LINE_SIZE;
831 prev_min_len = prev_min_ms->len;
832 min_len = min_ms->len;
834 if (min_len >= prev_min_len || prev_min_len - min_len > (unsigned) align) {
835 printf("Segments are of wrong lengths!\n");
839 /* try reserving from a bigger segment */
840 if (rte_memzone_reserve_aligned("smallest_offset", len, SOCKET_ID_ANY, 0, align) ==
842 printf("Cannot reserve memory!\n");
846 /* check if we got memory from correct segment */
847 if (min_ms->len != min_len && prev_min_ms->len != (prev_min_len - len)) {
848 printf("Reserved memory from segment with smaller offset!\n");
856 test_memzone_reserve_remainder(void)
858 const struct rte_memzone *mz1, *mz2;
859 const struct rte_memseg *ms, *min_ms = NULL;
861 const struct rte_config *config;
865 align = CACHE_LINE_SIZE;
867 config = rte_eal_get_configuration();
869 /* find minimum free contiguous length */
870 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
871 ms = &config->mem_config->free_memseg[i];
873 if (ms->addr == NULL)
878 if (min_len == 0 || ms->len < min_len) {
882 /* find maximum alignment this segment is able to hold */
883 align = CACHE_LINE_SIZE;
884 while ((ms->addr_64 & (align-1)) == 0) {
890 if (min_ms == NULL) {
891 printf("Minimal sized segment not found!\n");
895 /* try reserving min_len bytes with alignment - this should not affect our
896 * memseg, the memory will be taken from a different one.
898 mz1 = rte_memzone_reserve_aligned("reserve_remainder_1", min_len,
899 SOCKET_ID_ANY, 0, align);
901 printf("Failed to reserve %zu bytes aligned on %i bytes\n", min_len,
905 if (min_ms->len != min_len) {
906 printf("Memseg memory should not have been reserved!\n");
910 /* try reserving min_len bytes with less alignment - this should fill up
913 mz2 = rte_memzone_reserve("reserve_remainder_2", min_len,
916 printf("Failed to reserve %zu bytes\n", min_len);
919 if (min_ms->len != 0) {
920 printf("Memseg memory should have been reserved!\n");
930 const struct rte_memzone *memzone1;
931 const struct rte_memzone *memzone2;
932 const struct rte_memzone *memzone3;
933 const struct rte_memzone *memzone4;
934 const struct rte_memzone *mz;
936 memzone1 = rte_memzone_reserve("testzone1", 100,
939 memzone2 = rte_memzone_reserve("testzone2", 1000,
942 memzone3 = rte_memzone_reserve("testzone3", 1000,
945 memzone4 = rte_memzone_reserve("testzone4", 1024,
948 /* memzone3 may be NULL if we don't have NUMA */
949 if (memzone1 == NULL || memzone2 == NULL || memzone4 == NULL)
954 /* check cache-line alignments */
955 printf("check alignments and lengths\n");
957 if ((memzone1->phys_addr & CACHE_LINE_MASK) != 0)
959 if ((memzone2->phys_addr & CACHE_LINE_MASK) != 0)
961 if (memzone3 != NULL && (memzone3->phys_addr & CACHE_LINE_MASK) != 0)
963 if ((memzone1->len & CACHE_LINE_MASK) != 0 || memzone1->len == 0)
965 if ((memzone2->len & CACHE_LINE_MASK) != 0 || memzone2->len == 0)
967 if (memzone3 != NULL && ((memzone3->len & CACHE_LINE_MASK) != 0 ||
970 if (memzone4->len != 1024)
973 /* check that zones don't overlap */
974 printf("check overlapping\n");
976 if (is_memory_overlap(memzone1->phys_addr, memzone1->len,
977 memzone2->phys_addr, memzone2->len))
979 if (memzone3 != NULL &&
980 is_memory_overlap(memzone1->phys_addr, memzone1->len,
981 memzone3->phys_addr, memzone3->len))
983 if (memzone3 != NULL &&
984 is_memory_overlap(memzone2->phys_addr, memzone2->len,
985 memzone3->phys_addr, memzone3->len))
988 printf("check socket ID\n");
990 /* memzone2 must be on socket id 0 and memzone3 on socket 1 */
991 if (memzone2->socket_id != 0)
993 if (memzone3 != NULL && memzone3->socket_id != 1)
996 printf("test zone lookup\n");
997 mz = rte_memzone_lookup("testzone1");
1001 printf("test duplcate zone name\n");
1002 mz = rte_memzone_reserve("testzone1", 100,
1007 printf("test reserving memzone with bigger size than the maximum\n");
1008 if (test_memzone_reserving_zone_size_bigger_than_the_maximum() < 0)
1011 printf("test reserving memory in smallest segments\n");
1012 if (test_memzone_reserve_memory_in_smallest_segment() < 0)
1015 printf("test reserving memory in segments with smallest offsets\n");
1016 if (test_memzone_reserve_memory_with_smallest_offset() < 0)
1019 printf("test memzone_reserve flags\n");
1020 if (test_memzone_reserve_flags() < 0)
1023 printf("test alignment for memzone_reserve\n");
1024 if (test_memzone_aligned() < 0)
1027 printf("test boundary alignment for memzone_reserve\n");
1028 if (test_memzone_bounded() < 0)
1031 printf("test invalid alignment for memzone_reserve\n");
1032 if (test_memzone_invalid_alignment() < 0)
1035 printf("test reserving amounts of memory equal to segment's length\n");
1036 if (test_memzone_reserve_remainder() < 0)
1039 printf("test reserving the largest size memzone possible\n");
1040 if (test_memzone_reserve_max() < 0)
1043 printf("test reserving the largest size aligned memzone possible\n");
1044 if (test_memzone_reserve_max_aligned() < 0)