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_memory.h>
43 #include <rte_memzone.h>
44 #include <rte_tailq.h>
46 #include <rte_common.h>
54 * - Search for three reserved zones or reserve them if they do not exist:
56 * - One is on any socket id.
57 * - The second is on socket 0.
58 * - The last one is on socket 1 (if socket 1 exists).
60 * - Check that the zones exist.
62 * - Check that the zones are cache-aligned.
64 * - Check that zones do not overlap.
66 * - Check that the zones are on the correct socket id.
68 * - Check that a lookup of the first zone returns the same pointer.
70 * - Check that it is not possible to create another zone with the
71 * same name as an existing zone.
73 * - Check flags for specific huge page size reservation
76 /* Test if memory overlaps: return 1 if true, or 0 if false. */
78 is_memory_overlap(phys_addr_t ptr1, size_t len1, phys_addr_t ptr2, size_t len2)
80 if (ptr2 >= ptr1 && (ptr2 - ptr1) < len1)
82 else if (ptr2 < ptr1 && (ptr1 - ptr2) < len2)
88 test_memzone_invalid_alignment(void)
90 const struct rte_memzone * mz;
92 mz = rte_memzone_lookup("invalid_alignment");
94 printf("Zone with invalid alignment has been reserved\n");
98 mz = rte_memzone_reserve_aligned("invalid_alignment", 100,
99 SOCKET_ID_ANY, 0, 100);
101 printf("Zone with invalid alignment has been reserved\n");
108 test_memzone_reserving_zone_size_bigger_than_the_maximum(void)
110 const struct rte_memzone * mz;
112 mz = rte_memzone_lookup("zone_size_bigger_than_the_maximum");
114 printf("zone_size_bigger_than_the_maximum has been reserved\n");
118 mz = rte_memzone_reserve("zone_size_bigger_than_the_maximum", 0x1900000000ULL,
121 printf("It is impossible to reserve such big a memzone\n");
129 test_memzone_reserve_flags(void)
131 const struct rte_memzone *mz;
132 const struct rte_memseg *ms;
133 int hugepage_2MB_avail = 0;
134 int hugepage_1GB_avail = 0;
135 const int size = 100;
137 ms = rte_eal_get_physmem_layout();
138 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
139 if (ms[i].hugepage_sz == RTE_PGSIZE_2M)
140 hugepage_2MB_avail = 1;
141 if (ms[i].hugepage_sz == RTE_PGSIZE_1G)
142 hugepage_1GB_avail = 1;
144 /* Display the availability of 2MB and 1GB pages */
145 if (hugepage_2MB_avail)
146 printf("2MB Huge pages available\n");
147 if (hugepage_1GB_avail)
148 printf("1GB Huge pages available\n");
150 * If 2MB pages available, check that a small memzone is correctly
151 * reserved from 2MB huge pages when requested by the RTE_MEMZONE_2MB flag.
152 * Also check that RTE_MEMZONE_SIZE_HINT_ONLY flag only defaults to an
153 * available page size (i.e 1GB ) when 2MB pages are unavailable.
155 if (hugepage_2MB_avail) {
156 mz = rte_memzone_reserve("flag_zone_2M", size, SOCKET_ID_ANY,
159 printf("MEMZONE FLAG 2MB\n");
162 if (mz->hugepage_sz != RTE_PGSIZE_2M) {
163 printf("hugepage_sz not equal 2M\n");
167 mz = rte_memzone_reserve("flag_zone_2M_HINT", size, SOCKET_ID_ANY,
168 RTE_MEMZONE_2MB|RTE_MEMZONE_SIZE_HINT_ONLY);
170 printf("MEMZONE FLAG 2MB\n");
173 if (mz->hugepage_sz != RTE_PGSIZE_2M) {
174 printf("hugepage_sz not equal 2M\n");
178 /* Check if 1GB huge pages are unavailable, that function fails unless
179 * HINT flag is indicated
181 if (!hugepage_1GB_avail) {
182 mz = rte_memzone_reserve("flag_zone_1G_HINT", size, SOCKET_ID_ANY,
183 RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY);
185 printf("MEMZONE FLAG 1GB & HINT\n");
188 if (mz->hugepage_sz != RTE_PGSIZE_2M) {
189 printf("hugepage_sz not equal 2M\n");
193 mz = rte_memzone_reserve("flag_zone_1G", size, SOCKET_ID_ANY,
196 printf("MEMZONE FLAG 1GB\n");
202 /*As with 2MB tests above for 1GB huge page requests*/
203 if (hugepage_1GB_avail) {
204 mz = rte_memzone_reserve("flag_zone_1G", size, SOCKET_ID_ANY,
207 printf("MEMZONE FLAG 1GB\n");
210 if (mz->hugepage_sz != RTE_PGSIZE_1G) {
211 printf("hugepage_sz not equal 1G\n");
215 mz = rte_memzone_reserve("flag_zone_1G_HINT", size, SOCKET_ID_ANY,
216 RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY);
218 printf("MEMZONE FLAG 1GB\n");
221 if (mz->hugepage_sz != RTE_PGSIZE_1G) {
222 printf("hugepage_sz not equal 1G\n");
226 /* Check if 1GB huge pages are unavailable, that function fails unless
227 * HINT flag is indicated
229 if (!hugepage_2MB_avail) {
230 mz = rte_memzone_reserve("flag_zone_2M_HINT", size, SOCKET_ID_ANY,
231 RTE_MEMZONE_2MB|RTE_MEMZONE_SIZE_HINT_ONLY);
233 printf("MEMZONE FLAG 2MB & HINT\n");
236 if (mz->hugepage_sz != RTE_PGSIZE_1G) {
237 printf("hugepage_sz not equal 1G\n");
240 mz = rte_memzone_reserve("flag_zone_2M", size, SOCKET_ID_ANY,
243 printf("MEMZONE FLAG 2MB\n");
248 if (hugepage_2MB_avail && hugepage_1GB_avail) {
249 mz = rte_memzone_reserve("flag_zone_2M_HINT", size, SOCKET_ID_ANY,
250 RTE_MEMZONE_2MB|RTE_MEMZONE_1GB);
252 printf("BOTH SIZES SET\n");
261 test_memzone_reserve_max(void)
263 const struct rte_memzone *mz;
264 const struct rte_config *config;
265 const struct rte_memseg *ms;
272 /* get pointer to global configuration */
273 config = rte_eal_get_configuration();
275 ms = rte_eal_get_physmem_layout();
277 for (memseg_idx = 0; memseg_idx < RTE_MAX_MEMSEG; memseg_idx++){
278 /* ignore smaller memsegs as they can only get smaller */
279 if (ms[memseg_idx].len < maxlen)
282 len = ms[memseg_idx].len;
283 last_addr = ms[memseg_idx].addr;
285 /* cycle through all memzones */
286 for (memzone_idx = 0; memzone_idx < RTE_MAX_MEMZONE; memzone_idx++) {
288 /* stop when reaching last allocated memzone */
289 if (config->mem_config->memzone[memzone_idx].addr == NULL)
292 /* check if the memzone is in our memseg and subtract length */
293 if ((config->mem_config->memzone[memzone_idx].addr >=
294 ms[memseg_idx].addr) &&
295 (config->mem_config->memzone[memzone_idx].addr <=
296 (RTE_PTR_ADD(ms[memseg_idx].addr,
297 (size_t)ms[memseg_idx].len)))) {
298 /* since the zones can now be aligned and occasionally skip
299 * some space, we should calculate the length based on
300 * reported length and start addresses difference. Addresses
301 * are allocated sequentially so we don't need to worry about
302 * them being in the right order.
304 len -= (uintptr_t) RTE_PTR_SUB(
305 config->mem_config->memzone[memzone_idx].addr,
306 (uintptr_t) last_addr);
307 len -= config->mem_config->memzone[memzone_idx].len;
309 RTE_PTR_ADD(config->mem_config->memzone[memzone_idx].addr,
310 (size_t) config->mem_config->memzone[memzone_idx].len);
314 /* we don't need to calculate offset here since length
315 * is always cache-aligned */
320 mz = rte_memzone_reserve("max_zone", 0, SOCKET_ID_ANY, 0);
322 printf("Failed to reserve a big chunk of memory\n");
323 rte_dump_physmem_layout();
328 if (mz->len != maxlen) {
329 printf("Memzone reserve with 0 size did not return bigest block\n");
330 printf("Expected size = %" PRIu64 ", actual size = %" PRIu64 "\n",
332 rte_dump_physmem_layout();
341 test_memzone_reserve_max_aligned(void)
343 const struct rte_memzone *mz;
344 const struct rte_config *config;
345 const struct rte_memseg *ms;
348 uint64_t addr_offset, len = 0;
352 /* get pointer to global configuration */
353 config = rte_eal_get_configuration();
355 ms = rte_eal_get_physmem_layout();
359 for (memseg_idx = 0; memseg_idx < RTE_MAX_MEMSEG; memseg_idx++){
361 /* ignore smaller memsegs as they can only get smaller */
362 if (ms[memseg_idx].len < maxlen)
365 len = ms[memseg_idx].len;
366 last_addr = ms[memseg_idx].addr;
368 /* cycle through all memzones */
369 for (memzone_idx = 0; memzone_idx < RTE_MAX_MEMZONE; memzone_idx++) {
371 /* stop when reaching last allocated memzone */
372 if (config->mem_config->memzone[memzone_idx].addr == NULL)
375 /* check if the memzone is in our memseg and subtract length */
376 if ((config->mem_config->memzone[memzone_idx].addr >=
377 ms[memseg_idx].addr) &&
378 (config->mem_config->memzone[memzone_idx].addr <=
379 (RTE_PTR_ADD(ms[memseg_idx].addr,
380 (size_t) ms[memseg_idx].len)))) {
381 /* since the zones can now be aligned and occasionally skip
382 * some space, we should calculate the length based on
383 * reported length and start addresses difference.
385 len -= (uintptr_t) RTE_PTR_SUB(
386 config->mem_config->memzone[memzone_idx].addr,
387 (uintptr_t) last_addr);
388 len -= config->mem_config->memzone[memzone_idx].len;
390 RTE_PTR_ADD(config->mem_config->memzone[memzone_idx].addr,
391 (size_t) config->mem_config->memzone[memzone_idx].len);
395 /* make sure we get the alignment offset */
397 addr_offset = RTE_ALIGN_CEIL((uintptr_t) last_addr, 512) - (uintptr_t) last_addr;
402 maxlen -= addr_offset;
404 mz = rte_memzone_reserve_aligned("max_zone_aligned", 0,
405 SOCKET_ID_ANY, 0, 512);
407 printf("Failed to reserve a big chunk of memory\n");
408 rte_dump_physmem_layout();
413 if (mz->len != maxlen) {
414 printf("Memzone reserve with 0 size and alignment 512 did not return"
416 printf("Expected size = %" PRIu64 ", actual size = %" PRIu64 "\n",
418 rte_dump_physmem_layout();
427 test_memzone_aligned(void)
429 const struct rte_memzone *memzone_aligned_32;
430 const struct rte_memzone *memzone_aligned_128;
431 const struct rte_memzone *memzone_aligned_256;
432 const struct rte_memzone *memzone_aligned_512;
433 const struct rte_memzone *memzone_aligned_1024;
435 /* memzone that should automatically be adjusted to align on 64 bytes */
436 memzone_aligned_32 = rte_memzone_lookup("aligned_32");
437 if (memzone_aligned_32 == NULL)
438 memzone_aligned_32 = rte_memzone_reserve_aligned("aligned_32", 100,
439 SOCKET_ID_ANY, 0, 32);
441 /* memzone that is supposed to be aligned on a 128 byte boundary */
442 memzone_aligned_128 = rte_memzone_lookup("aligned_128");
443 if (memzone_aligned_128 == NULL)
444 memzone_aligned_128 = rte_memzone_reserve_aligned("aligned_128", 100,
445 SOCKET_ID_ANY, 0, 128);
447 /* memzone that is supposed to be aligned on a 256 byte boundary */
448 memzone_aligned_256 = rte_memzone_lookup("aligned_256");
449 if (memzone_aligned_256 == NULL)
450 memzone_aligned_256 = rte_memzone_reserve_aligned("aligned_256", 100,
451 SOCKET_ID_ANY, 0, 256);
453 /* memzone that is supposed to be aligned on a 512 byte boundary */
454 memzone_aligned_512 = rte_memzone_lookup("aligned_512");
455 if (memzone_aligned_512 == NULL)
456 memzone_aligned_512 = rte_memzone_reserve_aligned("aligned_512", 100,
457 SOCKET_ID_ANY, 0, 512);
459 /* memzone that is supposed to be aligned on a 1024 byte boundary */
460 memzone_aligned_1024 = rte_memzone_lookup("aligned_1024");
461 if (memzone_aligned_1024 == NULL)
462 memzone_aligned_1024 = rte_memzone_reserve_aligned("aligned_1024", 100,
463 SOCKET_ID_ANY, 0, 1024);
465 printf("check alignments and lengths\n");
466 if ((memzone_aligned_32->phys_addr & CACHE_LINE_MASK) != 0)
468 if (((uintptr_t) memzone_aligned_32->addr & CACHE_LINE_MASK) != 0)
470 if ((memzone_aligned_32->len & CACHE_LINE_MASK) != 0)
472 if ((memzone_aligned_128->phys_addr & 127) != 0)
474 if (((uintptr_t) memzone_aligned_128->addr & 127) != 0)
476 if ((memzone_aligned_128->len & CACHE_LINE_MASK) != 0)
478 if ((memzone_aligned_256->phys_addr & 255) != 0)
480 if (((uintptr_t) memzone_aligned_256->addr & 255) != 0)
482 if ((memzone_aligned_256->len & CACHE_LINE_MASK) != 0)
484 if ((memzone_aligned_512->phys_addr & 511) != 0)
486 if (((uintptr_t) memzone_aligned_512->addr & 511) != 0)
488 if ((memzone_aligned_512->len & CACHE_LINE_MASK) != 0)
490 if ((memzone_aligned_1024->phys_addr & 1023) != 0)
492 if (((uintptr_t) memzone_aligned_1024->addr & 1023) != 0)
494 if ((memzone_aligned_1024->len & CACHE_LINE_MASK) != 0)
498 /* check that zones don't overlap */
499 printf("check overlapping\n");
500 if (is_memory_overlap(memzone_aligned_32->phys_addr, memzone_aligned_32->len,
501 memzone_aligned_128->phys_addr, memzone_aligned_128->len))
503 if (is_memory_overlap(memzone_aligned_32->phys_addr, memzone_aligned_32->len,
504 memzone_aligned_256->phys_addr, memzone_aligned_256->len))
506 if (is_memory_overlap(memzone_aligned_32->phys_addr, memzone_aligned_32->len,
507 memzone_aligned_512->phys_addr, memzone_aligned_512->len))
509 if (is_memory_overlap(memzone_aligned_32->phys_addr, memzone_aligned_32->len,
510 memzone_aligned_1024->phys_addr, memzone_aligned_1024->len))
512 if (is_memory_overlap(memzone_aligned_128->phys_addr, memzone_aligned_128->len,
513 memzone_aligned_256->phys_addr, memzone_aligned_256->len))
515 if (is_memory_overlap(memzone_aligned_128->phys_addr, memzone_aligned_128->len,
516 memzone_aligned_512->phys_addr, memzone_aligned_512->len))
518 if (is_memory_overlap(memzone_aligned_128->phys_addr, memzone_aligned_128->len,
519 memzone_aligned_1024->phys_addr, memzone_aligned_1024->len))
521 if (is_memory_overlap(memzone_aligned_256->phys_addr, memzone_aligned_256->len,
522 memzone_aligned_512->phys_addr, memzone_aligned_512->len))
524 if (is_memory_overlap(memzone_aligned_256->phys_addr, memzone_aligned_256->len,
525 memzone_aligned_1024->phys_addr, memzone_aligned_1024->len))
527 if (is_memory_overlap(memzone_aligned_512->phys_addr, memzone_aligned_512->len,
528 memzone_aligned_1024->phys_addr, memzone_aligned_1024->len))
536 const struct rte_memzone *memzone1;
537 const struct rte_memzone *memzone2;
538 const struct rte_memzone *memzone3;
539 const struct rte_memzone *mz;
541 memzone1 = rte_memzone_lookup("testzone1");
542 if (memzone1 == NULL)
543 memzone1 = rte_memzone_reserve("testzone1", 100,
546 memzone2 = rte_memzone_lookup("testzone2");
547 if (memzone2 == NULL)
548 memzone2 = rte_memzone_reserve("testzone2", 1000,
551 memzone3 = rte_memzone_lookup("testzone3");
552 if (memzone3 == NULL)
553 memzone3 = rte_memzone_reserve("testzone3", 1000,
556 /* memzone3 may be NULL if we don't have NUMA */
557 if (memzone1 == NULL || memzone2 == NULL)
562 /* check cache-line alignments */
563 printf("check alignments and lengths\n");
565 if ((memzone1->phys_addr & CACHE_LINE_MASK) != 0)
567 if ((memzone2->phys_addr & CACHE_LINE_MASK) != 0)
569 if (memzone3 != NULL && (memzone3->phys_addr & CACHE_LINE_MASK) != 0)
571 if ((memzone1->len & CACHE_LINE_MASK) != 0 || memzone1->len == 0)
573 if ((memzone2->len & CACHE_LINE_MASK) != 0 || memzone2->len == 0)
575 if (memzone3 != NULL && ((memzone3->len & CACHE_LINE_MASK) != 0 ||
579 /* check that zones don't overlap */
580 printf("check overlapping\n");
582 if (is_memory_overlap(memzone1->phys_addr, memzone1->len,
583 memzone2->phys_addr, memzone2->len))
585 if (memzone3 != NULL &&
586 is_memory_overlap(memzone1->phys_addr, memzone1->len,
587 memzone3->phys_addr, memzone3->len))
589 if (memzone3 != NULL &&
590 is_memory_overlap(memzone2->phys_addr, memzone2->len,
591 memzone3->phys_addr, memzone3->len))
594 printf("check socket ID\n");
596 /* memzone2 must be on socket id 0 and memzone3 on socket 1 */
597 if (memzone2->socket_id != 0)
599 if (memzone3 != NULL && memzone3->socket_id != 1)
602 printf("test zone lookup\n");
603 mz = rte_memzone_lookup("testzone1");
607 printf("test duplcate zone name\n");
608 mz = rte_memzone_reserve("testzone1", 100,
613 printf("test reserving memzone with bigger size than the maximum\n");
614 if (test_memzone_reserving_zone_size_bigger_than_the_maximum() < 0)
617 printf("test reserving the largest size memzone possible\n");
618 if (test_memzone_reserve_max() < 0)
621 printf("test memzone_reserve flags\n");
622 if (test_memzone_reserve_flags() < 0)
625 printf("test alignment for memzone_reserve\n");
626 if (test_memzone_aligned() < 0)
629 printf("test invalid alignment for memzone_reserve\n");
630 if (test_memzone_invalid_alignment() < 0)
633 printf("test reserving the largest size aligned memzone possible\n");
634 if (test_memzone_reserve_max_aligned() < 0)