4 * Copyright(c) 2010-2012 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.
38 #include <sys/queue.h>
40 #include <cmdline_parse.h>
42 #include <rte_random.h>
43 #include <rte_cycles.h>
44 #include <rte_memory.h>
45 #include <rte_memzone.h>
46 #include <rte_tailq.h>
48 #include <rte_eal_memconfig.h>
49 #include <rte_common.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", 0x1900000000ULL,
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 int 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 &= ~((uint64_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,
302 (size_t)ms[memseg_idx].len)))) {
303 /* since the zones can now be aligned and occasionally skip
304 * some space, we should calculate the length based on
305 * reported length and start addresses difference. Addresses
306 * are allocated sequentially so we don't need to worry about
307 * them being in the right order.
310 config->mem_config->memzone[memzone_idx].addr,
312 len -= config->mem_config->memzone[memzone_idx].len;
313 last_addr = RTE_PTR_ADD(config->mem_config->memzone[memzone_idx].addr,
314 (size_t) config->mem_config->memzone[memzone_idx].len);
318 /* we don't need to calculate offset here since length
319 * is always cache-aligned */
325 printf("There is no space left!\n");
329 mz = rte_memzone_reserve("max_zone", 0, SOCKET_ID_ANY, 0);
331 printf("Failed to reserve a big chunk of memory\n");
332 rte_dump_physmem_layout();
337 if (mz->len != maxlen) {
338 printf("Memzone reserve with 0 size did not return bigest block\n");
339 printf("Expected size = %" PRIu64 ", actual size = %" PRIu64 "\n",
341 rte_dump_physmem_layout();
350 test_memzone_reserve_max_aligned(void)
352 const struct rte_memzone *mz;
353 const struct rte_config *config;
354 const struct rte_memseg *ms;
357 uint64_t addr_offset, len = 0;
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 &= ~((uint64_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,
395 (size_t) ms[memseg_idx].len)))) {
396 /* since the zones can now be aligned and occasionally skip
397 * some space, we should calculate the length based on
398 * reported length and start addresses difference.
400 len -= (uintptr_t) RTE_PTR_SUB(
401 config->mem_config->memzone[memzone_idx].addr,
402 (uintptr_t) last_addr);
403 len -= config->mem_config->memzone[memzone_idx].len;
405 RTE_PTR_ADD(config->mem_config->memzone[memzone_idx].addr,
406 (size_t) config->mem_config->memzone[memzone_idx].len);
410 /* make sure we get the alignment offset */
412 addr_offset = RTE_PTR_ALIGN_CEIL((uintptr_t) last_addr, align) - (uintptr_t) last_addr;
417 if (maxlen == 0 || maxlen == addr_offset) {
418 printf("There is no space left for biggest %u-aligned memzone!\n", align);
422 maxlen -= addr_offset;
424 mz = rte_memzone_reserve_aligned("max_zone_aligned", 0,
425 SOCKET_ID_ANY, 0, align);
427 printf("Failed to reserve a big chunk of memory\n");
428 rte_dump_physmem_layout();
433 if (mz->len != maxlen) {
434 printf("Memzone reserve with 0 size and alignment %u did not return"
435 " bigest block\n", align);
436 printf("Expected size = %" PRIu64 ", actual size = %" PRIu64 "\n",
438 rte_dump_physmem_layout();
447 test_memzone_aligned(void)
449 const struct rte_memzone *memzone_aligned_32;
450 const struct rte_memzone *memzone_aligned_128;
451 const struct rte_memzone *memzone_aligned_256;
452 const struct rte_memzone *memzone_aligned_512;
453 const struct rte_memzone *memzone_aligned_1024;
455 /* memzone that should automatically be adjusted to align on 64 bytes */
456 memzone_aligned_32 = rte_memzone_lookup("aligned_32");
457 if (memzone_aligned_32 == NULL)
458 memzone_aligned_32 = rte_memzone_reserve_aligned("aligned_32", 100,
459 SOCKET_ID_ANY, 0, 32);
461 /* memzone that is supposed to be aligned on a 128 byte boundary */
462 memzone_aligned_128 = rte_memzone_lookup("aligned_128");
463 if (memzone_aligned_128 == NULL)
464 memzone_aligned_128 = rte_memzone_reserve_aligned("aligned_128", 100,
465 SOCKET_ID_ANY, 0, 128);
467 /* memzone that is supposed to be aligned on a 256 byte boundary */
468 memzone_aligned_256 = rte_memzone_lookup("aligned_256");
469 if (memzone_aligned_256 == NULL)
470 memzone_aligned_256 = rte_memzone_reserve_aligned("aligned_256", 100,
471 SOCKET_ID_ANY, 0, 256);
473 /* memzone that is supposed to be aligned on a 512 byte boundary */
474 memzone_aligned_512 = rte_memzone_lookup("aligned_512");
475 if (memzone_aligned_512 == NULL)
476 memzone_aligned_512 = rte_memzone_reserve_aligned("aligned_512", 100,
477 SOCKET_ID_ANY, 0, 512);
479 /* memzone that is supposed to be aligned on a 1024 byte boundary */
480 memzone_aligned_1024 = rte_memzone_lookup("aligned_1024");
481 if (memzone_aligned_1024 == NULL)
482 memzone_aligned_1024 = rte_memzone_reserve_aligned("aligned_1024", 100,
483 SOCKET_ID_ANY, 0, 1024);
485 printf("check alignments and lengths\n");
486 if (memzone_aligned_32 == NULL) {
487 printf("Unable to reserve 64-byte aligned memzone!\n");
490 if ((memzone_aligned_32->phys_addr & CACHE_LINE_MASK) != 0)
492 if (((uintptr_t) memzone_aligned_32->addr & CACHE_LINE_MASK) != 0)
494 if ((memzone_aligned_32->len & CACHE_LINE_MASK) != 0)
496 if (memzone_aligned_128 == NULL) {
497 printf("Unable to reserve 128-byte aligned memzone!\n");
500 if ((memzone_aligned_128->phys_addr & 127) != 0)
502 if (((uintptr_t) memzone_aligned_128->addr & 127) != 0)
504 if ((memzone_aligned_128->len & CACHE_LINE_MASK) != 0)
506 if (memzone_aligned_256 == NULL) {
507 printf("Unable to reserve 256-byte aligned memzone!\n");
510 if ((memzone_aligned_256->phys_addr & 255) != 0)
512 if (((uintptr_t) memzone_aligned_256->addr & 255) != 0)
514 if ((memzone_aligned_256->len & CACHE_LINE_MASK) != 0)
516 if (memzone_aligned_512 == NULL) {
517 printf("Unable to reserve 512-byte aligned memzone!\n");
520 if ((memzone_aligned_512->phys_addr & 511) != 0)
522 if (((uintptr_t) memzone_aligned_512->addr & 511) != 0)
524 if ((memzone_aligned_512->len & CACHE_LINE_MASK) != 0)
526 if (memzone_aligned_1024 == NULL) {
527 printf("Unable to reserve 1024-byte aligned memzone!\n");
530 if ((memzone_aligned_1024->phys_addr & 1023) != 0)
532 if (((uintptr_t) memzone_aligned_1024->addr & 1023) != 0)
534 if ((memzone_aligned_1024->len & CACHE_LINE_MASK) != 0)
537 /* check that zones don't overlap */
538 printf("check overlapping\n");
539 if (is_memory_overlap(memzone_aligned_32->phys_addr, memzone_aligned_32->len,
540 memzone_aligned_128->phys_addr, memzone_aligned_128->len))
542 if (is_memory_overlap(memzone_aligned_32->phys_addr, memzone_aligned_32->len,
543 memzone_aligned_256->phys_addr, memzone_aligned_256->len))
545 if (is_memory_overlap(memzone_aligned_32->phys_addr, memzone_aligned_32->len,
546 memzone_aligned_512->phys_addr, memzone_aligned_512->len))
548 if (is_memory_overlap(memzone_aligned_32->phys_addr, memzone_aligned_32->len,
549 memzone_aligned_1024->phys_addr, memzone_aligned_1024->len))
551 if (is_memory_overlap(memzone_aligned_128->phys_addr, memzone_aligned_128->len,
552 memzone_aligned_256->phys_addr, memzone_aligned_256->len))
554 if (is_memory_overlap(memzone_aligned_128->phys_addr, memzone_aligned_128->len,
555 memzone_aligned_512->phys_addr, memzone_aligned_512->len))
557 if (is_memory_overlap(memzone_aligned_128->phys_addr, memzone_aligned_128->len,
558 memzone_aligned_1024->phys_addr, memzone_aligned_1024->len))
560 if (is_memory_overlap(memzone_aligned_256->phys_addr, memzone_aligned_256->len,
561 memzone_aligned_512->phys_addr, memzone_aligned_512->len))
563 if (is_memory_overlap(memzone_aligned_256->phys_addr, memzone_aligned_256->len,
564 memzone_aligned_1024->phys_addr, memzone_aligned_1024->len))
566 if (is_memory_overlap(memzone_aligned_512->phys_addr, memzone_aligned_512->len,
567 memzone_aligned_1024->phys_addr, memzone_aligned_1024->len))
575 const struct rte_memzone *memzone1;
576 const struct rte_memzone *memzone2;
577 const struct rte_memzone *memzone3;
578 const struct rte_memzone *mz;
580 memzone1 = rte_memzone_lookup("testzone1");
581 if (memzone1 == NULL)
582 memzone1 = rte_memzone_reserve("testzone1", 100,
585 memzone2 = rte_memzone_lookup("testzone2");
586 if (memzone2 == NULL)
587 memzone2 = rte_memzone_reserve("testzone2", 1000,
590 memzone3 = rte_memzone_lookup("testzone3");
591 if (memzone3 == NULL)
592 memzone3 = rte_memzone_reserve("testzone3", 1000,
595 /* memzone3 may be NULL if we don't have NUMA */
596 if (memzone1 == NULL || memzone2 == NULL)
601 /* check cache-line alignments */
602 printf("check alignments and lengths\n");
604 if ((memzone1->phys_addr & CACHE_LINE_MASK) != 0)
606 if ((memzone2->phys_addr & CACHE_LINE_MASK) != 0)
608 if (memzone3 != NULL && (memzone3->phys_addr & CACHE_LINE_MASK) != 0)
610 if ((memzone1->len & CACHE_LINE_MASK) != 0 || memzone1->len == 0)
612 if ((memzone2->len & CACHE_LINE_MASK) != 0 || memzone2->len == 0)
614 if (memzone3 != NULL && ((memzone3->len & CACHE_LINE_MASK) != 0 ||
618 /* check that zones don't overlap */
619 printf("check overlapping\n");
621 if (is_memory_overlap(memzone1->phys_addr, memzone1->len,
622 memzone2->phys_addr, memzone2->len))
624 if (memzone3 != NULL &&
625 is_memory_overlap(memzone1->phys_addr, memzone1->len,
626 memzone3->phys_addr, memzone3->len))
628 if (memzone3 != NULL &&
629 is_memory_overlap(memzone2->phys_addr, memzone2->len,
630 memzone3->phys_addr, memzone3->len))
633 printf("check socket ID\n");
635 /* memzone2 must be on socket id 0 and memzone3 on socket 1 */
636 if (memzone2->socket_id != 0)
638 if (memzone3 != NULL && memzone3->socket_id != 1)
641 printf("test zone lookup\n");
642 mz = rte_memzone_lookup("testzone1");
646 printf("test duplcate zone name\n");
647 mz = rte_memzone_reserve("testzone1", 100,
652 printf("test reserving memzone with bigger size than the maximum\n");
653 if (test_memzone_reserving_zone_size_bigger_than_the_maximum() < 0)
656 printf("test memzone_reserve flags\n");
657 if (test_memzone_reserve_flags() < 0)
660 printf("test alignment for memzone_reserve\n");
661 if (test_memzone_aligned() < 0)
664 printf("test invalid alignment for memzone_reserve\n");
665 if (test_memzone_invalid_alignment() < 0)
668 printf("test reserving the largest size memzone possible\n");
669 if (test_memzone_reserve_max() < 0)
672 printf("test reserving the largest size aligned memzone possible\n");
673 if (test_memzone_reserve_max_aligned() < 0)