4 * Copyright(c) 2010-2013 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", (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 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 &= ~((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_lookup("aligned_32");
456 if (memzone_aligned_32 == NULL)
457 memzone_aligned_32 = rte_memzone_reserve_aligned("aligned_32", 100,
458 SOCKET_ID_ANY, 0, 32);
460 /* memzone that is supposed to be aligned on a 128 byte boundary */
461 memzone_aligned_128 = rte_memzone_lookup("aligned_128");
462 if (memzone_aligned_128 == NULL)
463 memzone_aligned_128 = rte_memzone_reserve_aligned("aligned_128", 100,
464 SOCKET_ID_ANY, 0, 128);
466 /* memzone that is supposed to be aligned on a 256 byte boundary */
467 memzone_aligned_256 = rte_memzone_lookup("aligned_256");
468 if (memzone_aligned_256 == NULL)
469 memzone_aligned_256 = rte_memzone_reserve_aligned("aligned_256", 100,
470 SOCKET_ID_ANY, 0, 256);
472 /* memzone that is supposed to be aligned on a 512 byte boundary */
473 memzone_aligned_512 = rte_memzone_lookup("aligned_512");
474 if (memzone_aligned_512 == NULL)
475 memzone_aligned_512 = rte_memzone_reserve_aligned("aligned_512", 100,
476 SOCKET_ID_ANY, 0, 512);
478 /* memzone that is supposed to be aligned on a 1024 byte boundary */
479 memzone_aligned_1024 = rte_memzone_lookup("aligned_1024");
480 if (memzone_aligned_1024 == NULL)
481 memzone_aligned_1024 = rte_memzone_reserve_aligned("aligned_1024", 100,
482 SOCKET_ID_ANY, 0, 1024);
484 printf("check alignments and lengths\n");
485 if (memzone_aligned_32 == NULL) {
486 printf("Unable to reserve 64-byte aligned memzone!\n");
489 if ((memzone_aligned_32->phys_addr & CACHE_LINE_MASK) != 0)
491 if (((uintptr_t) memzone_aligned_32->addr & CACHE_LINE_MASK) != 0)
493 if ((memzone_aligned_32->len & CACHE_LINE_MASK) != 0)
495 if (memzone_aligned_128 == NULL) {
496 printf("Unable to reserve 128-byte aligned memzone!\n");
499 if ((memzone_aligned_128->phys_addr & 127) != 0)
501 if (((uintptr_t) memzone_aligned_128->addr & 127) != 0)
503 if ((memzone_aligned_128->len & CACHE_LINE_MASK) != 0)
505 if (memzone_aligned_256 == NULL) {
506 printf("Unable to reserve 256-byte aligned memzone!\n");
509 if ((memzone_aligned_256->phys_addr & 255) != 0)
511 if (((uintptr_t) memzone_aligned_256->addr & 255) != 0)
513 if ((memzone_aligned_256->len & CACHE_LINE_MASK) != 0)
515 if (memzone_aligned_512 == NULL) {
516 printf("Unable to reserve 512-byte aligned memzone!\n");
519 if ((memzone_aligned_512->phys_addr & 511) != 0)
521 if (((uintptr_t) memzone_aligned_512->addr & 511) != 0)
523 if ((memzone_aligned_512->len & CACHE_LINE_MASK) != 0)
525 if (memzone_aligned_1024 == NULL) {
526 printf("Unable to reserve 1024-byte aligned memzone!\n");
529 if ((memzone_aligned_1024->phys_addr & 1023) != 0)
531 if (((uintptr_t) memzone_aligned_1024->addr & 1023) != 0)
533 if ((memzone_aligned_1024->len & CACHE_LINE_MASK) != 0)
536 /* check that zones don't overlap */
537 printf("check overlapping\n");
538 if (is_memory_overlap(memzone_aligned_32->phys_addr, memzone_aligned_32->len,
539 memzone_aligned_128->phys_addr, memzone_aligned_128->len))
541 if (is_memory_overlap(memzone_aligned_32->phys_addr, memzone_aligned_32->len,
542 memzone_aligned_256->phys_addr, memzone_aligned_256->len))
544 if (is_memory_overlap(memzone_aligned_32->phys_addr, memzone_aligned_32->len,
545 memzone_aligned_512->phys_addr, memzone_aligned_512->len))
547 if (is_memory_overlap(memzone_aligned_32->phys_addr, memzone_aligned_32->len,
548 memzone_aligned_1024->phys_addr, memzone_aligned_1024->len))
550 if (is_memory_overlap(memzone_aligned_128->phys_addr, memzone_aligned_128->len,
551 memzone_aligned_256->phys_addr, memzone_aligned_256->len))
553 if (is_memory_overlap(memzone_aligned_128->phys_addr, memzone_aligned_128->len,
554 memzone_aligned_512->phys_addr, memzone_aligned_512->len))
556 if (is_memory_overlap(memzone_aligned_128->phys_addr, memzone_aligned_128->len,
557 memzone_aligned_1024->phys_addr, memzone_aligned_1024->len))
559 if (is_memory_overlap(memzone_aligned_256->phys_addr, memzone_aligned_256->len,
560 memzone_aligned_512->phys_addr, memzone_aligned_512->len))
562 if (is_memory_overlap(memzone_aligned_256->phys_addr, memzone_aligned_256->len,
563 memzone_aligned_1024->phys_addr, memzone_aligned_1024->len))
565 if (is_memory_overlap(memzone_aligned_512->phys_addr, memzone_aligned_512->len,
566 memzone_aligned_1024->phys_addr, memzone_aligned_1024->len))
574 const struct rte_memzone *memzone1;
575 const struct rte_memzone *memzone2;
576 const struct rte_memzone *memzone3;
577 const struct rte_memzone *mz;
579 memzone1 = rte_memzone_lookup("testzone1");
580 if (memzone1 == NULL)
581 memzone1 = rte_memzone_reserve("testzone1", 100,
584 memzone2 = rte_memzone_lookup("testzone2");
585 if (memzone2 == NULL)
586 memzone2 = rte_memzone_reserve("testzone2", 1000,
589 memzone3 = rte_memzone_lookup("testzone3");
590 if (memzone3 == NULL)
591 memzone3 = rte_memzone_reserve("testzone3", 1000,
594 /* memzone3 may be NULL if we don't have NUMA */
595 if (memzone1 == NULL || memzone2 == NULL)
600 /* check cache-line alignments */
601 printf("check alignments and lengths\n");
603 if ((memzone1->phys_addr & CACHE_LINE_MASK) != 0)
605 if ((memzone2->phys_addr & CACHE_LINE_MASK) != 0)
607 if (memzone3 != NULL && (memzone3->phys_addr & CACHE_LINE_MASK) != 0)
609 if ((memzone1->len & CACHE_LINE_MASK) != 0 || memzone1->len == 0)
611 if ((memzone2->len & CACHE_LINE_MASK) != 0 || memzone2->len == 0)
613 if (memzone3 != NULL && ((memzone3->len & CACHE_LINE_MASK) != 0 ||
617 /* check that zones don't overlap */
618 printf("check overlapping\n");
620 if (is_memory_overlap(memzone1->phys_addr, memzone1->len,
621 memzone2->phys_addr, memzone2->len))
623 if (memzone3 != NULL &&
624 is_memory_overlap(memzone1->phys_addr, memzone1->len,
625 memzone3->phys_addr, memzone3->len))
627 if (memzone3 != NULL &&
628 is_memory_overlap(memzone2->phys_addr, memzone2->len,
629 memzone3->phys_addr, memzone3->len))
632 printf("check socket ID\n");
634 /* memzone2 must be on socket id 0 and memzone3 on socket 1 */
635 if (memzone2->socket_id != 0)
637 if (memzone3 != NULL && memzone3->socket_id != 1)
640 printf("test zone lookup\n");
641 mz = rte_memzone_lookup("testzone1");
645 printf("test duplcate zone name\n");
646 mz = rte_memzone_reserve("testzone1", 100,
651 printf("test reserving memzone with bigger size than the maximum\n");
652 if (test_memzone_reserving_zone_size_bigger_than_the_maximum() < 0)
655 printf("test memzone_reserve flags\n");
656 if (test_memzone_reserve_flags() < 0)
659 printf("test alignment for memzone_reserve\n");
660 if (test_memzone_aligned() < 0)
663 printf("test invalid alignment for memzone_reserve\n");
664 if (test_memzone_invalid_alignment() < 0)
667 printf("test reserving the largest size memzone possible\n");
668 if (test_memzone_reserve_max() < 0)
671 printf("test reserving the largest size aligned memzone possible\n");
672 if (test_memzone_reserve_max_aligned() < 0)