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.
41 #include <sys/queue.h>
43 #include <cmdline_parse.h>
45 #include <rte_common.h>
46 #include <rte_memory.h>
47 #include <rte_memzone.h>
48 #include <rte_per_lcore.h>
49 #include <rte_launch.h>
50 #include <rte_tailq.h>
52 #include <rte_per_lcore.h>
53 #include <rte_lcore.h>
54 #include <rte_malloc.h>
55 #include <rte_cycles.h>
56 #include <rte_random.h>
57 #include <rte_string_fns.h>
64 #define QUOTE(x) QUOTE_(x)
65 #define MALLOC_MEMZONE_SIZE QUOTE(RTE_MALLOC_MEMZONE_SIZE)
71 * Allocate some dynamic memory from heap (3 areas). Check that areas
72 * don't overlap an that alignment constraints match. This test is
73 * done many times on different lcores simultaneously.
76 /* Test if memory overlaps: return 1 if true, or 0 if false. */
78 is_memory_overlap(void *p1, size_t len1, void *p2, size_t len2)
80 unsigned long ptr1 = (unsigned long)p1;
81 unsigned long ptr2 = (unsigned long)p2;
83 if (ptr2 >= ptr1 && (ptr2 - ptr1) < len1)
85 else if (ptr2 < ptr1 && (ptr1 - ptr2) < len2)
91 is_aligned(void *p, int align)
93 unsigned long addr = (unsigned long)p;
94 unsigned mask = align - 1;
102 test_align_overlap_per_lcore(__attribute__((unused)) void *arg)
104 const unsigned align1 = 8,
108 void *p1 = NULL, *p2 = NULL, *p3 = NULL;
111 for (i = 0; i < N; i++) {
112 p1 = rte_zmalloc("dummy", 1000, align1);
114 printf("rte_zmalloc returned NULL (i=%u)\n", i);
118 for(j = 0; j < 1000 ; j++) {
119 if( *(char *)p1 != 0) {
120 printf("rte_zmalloc didn't zeroed"
121 "the allocated memory\n");
125 p2 = rte_malloc("dummy", 1000, align2);
127 printf("rte_malloc returned NULL (i=%u)\n", i);
132 p3 = rte_malloc("dummy", 1000, align3);
134 printf("rte_malloc returned NULL (i=%u)\n", i);
140 if (is_memory_overlap(p1, 1000, p2, 1000)) {
141 printf("p1 and p2 overlaps\n");
144 if (is_memory_overlap(p2, 1000, p3, 1000)) {
145 printf("p2 and p3 overlaps\n");
148 if (is_memory_overlap(p1, 1000, p3, 1000)) {
149 printf("p1 and p3 overlaps\n");
152 if (!is_aligned(p1, align1)) {
153 printf("p1 is not aligned\n");
156 if (!is_aligned(p2, align2)) {
157 printf("p2 is not aligned\n");
160 if (!is_aligned(p3, align3)) {
161 printf("p3 is not aligned\n");
168 rte_malloc_dump_stats("dummy");
174 test_reordered_free_per_lcore(__attribute__((unused)) void *arg)
176 const unsigned align1 = 8,
183 for (i = 0; i < 30; i++) {
184 p1 = rte_zmalloc("dummy", 1000, align1);
186 printf("rte_zmalloc returned NULL (i=%u)\n", i);
190 for(j = 0; j < 1000 ; j++) {
191 if( *(char *)p1 != 0) {
192 printf("rte_zmalloc didn't zeroed"
193 "the allocated memory\n");
197 /* use calloc to allocate 1000 16-byte items this time */
198 p2 = rte_calloc("dummy", 1000, 16, align2);
199 /* for third request use regular malloc again */
200 p3 = rte_malloc("dummy", 1000, align3);
202 printf("rte_malloc returned NULL (i=%u)\n", i);
206 if (is_memory_overlap(p1, 1000, p2, 1000)) {
207 printf("p1 and p2 overlaps\n");
210 if (is_memory_overlap(p2, 1000, p3, 1000)) {
211 printf("p2 and p3 overlaps\n");
214 if (is_memory_overlap(p1, 1000, p3, 1000)) {
215 printf("p1 and p3 overlaps\n");
218 if (!is_aligned(p1, align1)) {
219 printf("p1 is not aligned\n");
222 if (!is_aligned(p2, align2)) {
223 printf("p2 is not aligned\n");
226 if (!is_aligned(p3, align3)) {
227 printf("p3 is not aligned\n");
230 /* try freeing in every possible order */
264 rte_malloc_dump_stats("dummy");
270 /* test function inside the malloc lib*/
272 test_str_to_size(void)
278 {{ "5G", (uint64_t)5 * 1024 * 1024 *1024 },
279 {"0x20g", (uint64_t)0x20 * 1024 * 1024 *1024},
280 {"10M", 10 * 1024 * 1024},
281 {"050m", 050 * 1024 * 1024},
287 {"-1", 0}, /* negative values return 0 */
290 {"18446744073709551616", 0} /* ULLONG_MAX + 1 == out of range*/
293 for (i = 0; i < sizeof(test_values)/sizeof(test_values[0]); i++)
294 if (rte_str_to_size(test_values[i].str) != test_values[i].value)
302 void *p1 = rte_malloc("BIG", rte_str_to_size(MALLOC_MEMZONE_SIZE) * 2, 1024);
310 test_memzone_size_alloc(void)
312 void *p1 = rte_malloc("BIG", rte_str_to_size(MALLOC_MEMZONE_SIZE) - 128, 64);
316 /* one extra check - check no crashes if free(NULL) */
322 test_rte_malloc_type_limits(void)
324 /* The type-limits functionality is not yet implemented,
325 * so always return 0 no matter what the retval.
327 const char *typename = "limit_test";
328 rte_malloc_set_limit(typename, 64 * 1024);
329 rte_malloc_dump_stats(typename);
336 const char hello_str[] = "Hello, world!";
337 const unsigned size1 = 1024;
338 const unsigned size2 = size1 + 1024;
339 const unsigned size3 = size2;
340 const unsigned size4 = size3 + 1024;
342 /* test data is the same even if element is moved*/
343 char *ptr1 = rte_zmalloc(NULL, size1, CACHE_LINE_SIZE);
345 printf("NULL pointer returned from rte_zmalloc\n");
348 rte_snprintf(ptr1, size1, "%s" ,hello_str);
349 char *ptr2 = rte_realloc(ptr1, size2, CACHE_LINE_SIZE);
352 printf("NULL pointer returned from rte_realloc\n");
356 printf("unexpected - ptr1 == ptr2\n");
358 if (strcmp(ptr2, hello_str) != 0){
359 printf("Error - lost data from pointed area\n");
364 for (i = strnlen(hello_str, sizeof(hello_str)); i < size1; i++)
366 printf("Bad data in realloc\n");
370 /* now allocate third element, free the second
371 * and resize third. It should not move. (ptr1 is now invalid)
373 char *ptr3 = rte_zmalloc(NULL, size3, CACHE_LINE_SIZE);
375 printf("NULL pointer returned from rte_zmalloc\n");
379 for (i = 0; i < size3; i++)
381 printf("Bad data in zmalloc\n");
387 /* first resize to half the size of the freed block */
388 char *ptr4 = rte_realloc(ptr3, size4, CACHE_LINE_SIZE);
390 printf("NULL pointer returned from rte_realloc\n");
395 printf("Unexpected - ptr4 != ptr3\n");
399 /* now resize again to the full size of the freed block */
400 ptr4 = rte_realloc(ptr3, size3 + size2 + size1, CACHE_LINE_SIZE);
402 printf("Unexpected - ptr4 != ptr3 on second resize\n");
408 /* now try a resize to a smaller size, see if it works */
409 const unsigned size5 = 1024;
410 const unsigned size6 = size5 / 2;
411 char *ptr5 = rte_malloc(NULL, size5, CACHE_LINE_SIZE);
413 printf("NULL pointer returned from rte_malloc\n");
416 char *ptr6 = rte_realloc(ptr5, size6, CACHE_LINE_SIZE);
418 printf("NULL pointer returned from rte_realloc\n");
423 printf("Error, resizing to a smaller size moved data\n");
429 /* check for behaviour changing alignment */
430 const unsigned size7 = 1024;
431 const unsigned orig_align = CACHE_LINE_SIZE;
432 unsigned new_align = CACHE_LINE_SIZE * 2;
433 char *ptr7 = rte_malloc(NULL, size7, orig_align);
435 printf("NULL pointer returned from rte_malloc\n");
438 /* calc an alignment we don't already have */
439 while(RTE_PTR_ALIGN(ptr7, new_align) == ptr7)
441 char *ptr8 = rte_realloc(ptr7, size7, new_align);
443 printf("NULL pointer returned from rte_realloc\n");
447 if (RTE_PTR_ALIGN(ptr8, new_align) != ptr8){
448 printf("Failure to re-align data\n");
454 /* test behaviour when there is a free block after current one,
455 * but its not big enough
457 unsigned size9 = 1024, size10 = 1024;
458 unsigned size11 = size9 + size10 + 256;
459 char *ptr9 = rte_malloc(NULL, size9, CACHE_LINE_SIZE);
461 printf("NULL pointer returned from rte_malloc\n");
464 char *ptr10 = rte_malloc(NULL, size10, CACHE_LINE_SIZE);
466 printf("NULL pointer returned from rte_malloc\n");
470 char *ptr11 = rte_realloc(ptr10, size11, CACHE_LINE_SIZE);
472 printf("NULL pointer returned from rte_realloc\n");
477 printf("Error, unexpected that realloc has not created new buffer\n");
483 /* check we don't crash if we pass null to realloc
484 * We should get a malloc of the size requested*/
485 const size_t size12 = 1024;
487 char *ptr12 = rte_realloc(NULL, size12, CACHE_LINE_SIZE);
489 printf("NULL pointer returned from rte_realloc\n");
492 if (rte_malloc_validate(ptr12, &size12_check) < 0 ||
493 size12_check != size12){
502 test_random_alloc_free(void *_ __attribute__((unused)))
505 struct mem_list *next;
511 rte_srand((unsigned)rte_rdtsc());
513 for (i = 0; i < N; i++){
514 unsigned free_mem = 0;
515 size_t allocated_size;
517 const unsigned mem_size = sizeof(struct mem_list) + \
518 rte_rand() % (64 * 1024);
519 const unsigned align = 1 << (rte_rand() % 12); /* up to 4k alignment */
520 struct mem_list *entry = rte_malloc(NULL,
524 if (RTE_PTR_ALIGN(entry, align)!= entry)
526 if (rte_malloc_validate(entry, &allocated_size) == -1
527 || allocated_size < mem_size)
529 memset(entry->data, rte_lcore_id(),
530 mem_size - sizeof(*entry));
531 entry->next = list_head;
532 if (rte_malloc_validate(entry, NULL) == -1)
537 /* switch to freeing the memory with a 20% probability */
538 free_mem = ((rte_rand() % 10) >= 8);
541 struct mem_list *entry = list_head;
542 list_head = list_head->next;
546 printf("Lcore %u allocated/freed %u blocks\n", rte_lcore_id(), count);
550 #define err_return() do { \
551 printf("%s: %d - Error\n", __func__, __LINE__); \
556 test_rte_malloc_validate(void)
558 const size_t request_size = 1024;
559 size_t allocated_size;
560 char *data_ptr = rte_malloc(NULL, request_size, CACHE_LINE_SIZE);
561 if (data_ptr == NULL) {
562 printf("%s: %d - Allocation error\n", __func__, __LINE__);
566 /* check that a null input returns -1 */
567 if (rte_malloc_validate(NULL, NULL) != -1)
570 /* check that we get ok on a valid pointer */
571 if (rte_malloc_validate(data_ptr, &allocated_size) < 0)
574 /* check that the returned size is ok */
575 if (allocated_size < request_size)
578 #ifdef RTE_LIBRTE_MALLOC_DEBUG
580 char *over_write_vals = NULL;
582 /****** change the header to be bad */
584 over_write_vals = (char *)((uintptr_t)data_ptr - sizeof(save_buf));
585 /* first save the data as a backup before overwriting it */
586 memcpy(save_buf, over_write_vals, sizeof(save_buf));
587 memset(over_write_vals, 1, sizeof(save_buf));
588 /* then run validate */
589 retval = rte_malloc_validate(data_ptr, NULL);
590 /* finally restore the data again */
591 memcpy(over_write_vals, save_buf, sizeof(save_buf));
592 /* check we previously had an error */
596 /* check all ok again */
597 if (rte_malloc_validate(data_ptr, &allocated_size) < 0)
600 /**** change the trailer to be bad */
601 over_write_vals = (char *)((uintptr_t)data_ptr + allocated_size);
602 /* first save the data as a backup before overwriting it */
603 memcpy(save_buf, over_write_vals, sizeof(save_buf));
604 memset(over_write_vals, 1, sizeof(save_buf));
605 /* then run validate */
606 retval = rte_malloc_validate(data_ptr, NULL);
607 /* finally restore the data again */
608 memcpy(over_write_vals, save_buf, sizeof(save_buf));
612 /* check all ok again */
613 if (rte_malloc_validate(data_ptr, &allocated_size) < 0)
627 test_zero_aligned_alloc(void)
629 char *p1 = rte_malloc(NULL,1024, 0);
632 if (!rte_is_aligned(p1, CACHE_LINE_SIZE))
639 if (p1) rte_free(p1);
644 test_malloc_bad_params(void)
646 const char *type = NULL;
648 unsigned align = CACHE_LINE_SIZE;
650 /* rte_malloc expected to return null with inappropriate size */
651 char *bad_ptr = rte_malloc(type, size, align);
655 /* rte_malloc expected to return null with inappropriate alignment */
659 bad_ptr = rte_malloc(type, size, align);
666 /* clean up pointer */
678 if (test_str_to_size() < 0){
679 printf("test_str_to_size() failed\n");
682 else printf("test_str_to_size() passed\n");
684 if (test_memzone_size_alloc() < 0){
685 printf("test_memzone_size_alloc() failed\n");
688 else printf("test_memzone_size_alloc() passed\n");
690 if (test_big_alloc() < 0){
691 printf("test_big_alloc() failed\n");
694 else printf("test_big_alloc() passed\n");
696 if (test_zero_aligned_alloc() < 0){
697 printf("test_zero_aligned_alloc() failed\n");
700 else printf("test_zero_aligned_alloc() passed\n");
702 if (test_malloc_bad_params() < 0){
703 printf("test_malloc_bad_params() failed\n");
706 else printf("test_malloc_bad_params() passed\n");
708 if (test_realloc() < 0){
709 printf("test_realloc() failed\n");
712 else printf("test_realloc() passed\n");
713 /*----------------------------*/
714 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
715 rte_eal_remote_launch(test_align_overlap_per_lcore, NULL, lcore_id);
718 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
719 if (rte_eal_wait_lcore(lcore_id) < 0)
723 printf("test_align_overlap_per_lcore() failed\n");
726 else printf("test_align_overlap_per_lcore() passed\n");
727 /*----------------------------*/
728 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
729 rte_eal_remote_launch(test_reordered_free_per_lcore, NULL, lcore_id);
732 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
733 if (rte_eal_wait_lcore(lcore_id) < 0)
737 printf("test_reordered_free_per_lcore() failed\n");
740 else printf("test_reordered_free_per_lcore() passed\n");
742 /*----------------------------*/
743 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
744 rte_eal_remote_launch(test_random_alloc_free, NULL, lcore_id);
747 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
748 if (rte_eal_wait_lcore(lcore_id) < 0)
752 printf("test_random_alloc_free() failed\n");
755 else printf("test_random_alloc_free() passed\n");
757 /*----------------------------*/
758 ret = test_rte_malloc_type_limits();
760 printf("test_rte_malloc_type_limits() failed\n");
763 /* TODO: uncomment following line once type limits are valid */
764 /*else printf("test_rte_malloc_type_limits() passed\n");*/
766 /*----------------------------*/
767 ret = test_rte_malloc_validate();
769 printf("test_rte_malloc_validate() failed\n");
772 else printf("test_rte_malloc_validate() passed\n");