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.
33 * version: DPDK.L.1.2.3-3
42 #include <sys/queue.h>
44 #include <cmdline_parse.h>
46 #include <rte_common.h>
47 #include <rte_memory.h>
48 #include <rte_memzone.h>
49 #include <rte_per_lcore.h>
50 #include <rte_launch.h>
51 #include <rte_tailq.h>
53 #include <rte_per_lcore.h>
54 #include <rte_lcore.h>
55 #include <rte_malloc.h>
56 #include <rte_cycles.h>
57 #include <rte_random.h>
58 #include <rte_string_fns.h>
65 #define QUOTE(x) QUOTE_(x)
66 #define MALLOC_MEMZONE_SIZE QUOTE(RTE_MALLOC_MEMZONE_SIZE)
72 * Allocate some dynamic memory from heap (3 areas). Check that areas
73 * don't overlap an that alignment constraints match. This test is
74 * done many times on different lcores simultaneously.
77 /* Test if memory overlaps: return 1 if true, or 0 if false. */
79 is_memory_overlap(void *p1, size_t len1, void *p2, size_t len2)
81 unsigned long ptr1 = (unsigned long)p1;
82 unsigned long ptr2 = (unsigned long)p2;
84 if (ptr2 >= ptr1 && (ptr2 - ptr1) < len1)
86 else if (ptr2 < ptr1 && (ptr1 - ptr2) < len2)
92 is_aligned(void *p, int align)
94 unsigned long addr = (unsigned long)p;
95 unsigned mask = align - 1;
103 test_align_overlap_per_lcore(__attribute__((unused)) void *arg)
105 const unsigned align1 = 8,
109 void *p1 = NULL, *p2 = NULL, *p3 = NULL;
112 for (i = 0; i < N; i++) {
113 p1 = rte_zmalloc("dummy", 1000, align1);
115 printf("rte_zmalloc returned NULL (i=%u)\n", i);
119 for(j = 0; j < 1000 ; j++) {
120 if( *(char *)p1 != 0) {
121 printf("rte_zmalloc didn't zeroed"
122 "the allocated memory\n");
126 p2 = rte_malloc("dummy", 1000, align2);
128 printf("rte_malloc returned NULL (i=%u)\n", i);
133 p3 = rte_malloc("dummy", 1000, align3);
135 printf("rte_malloc returned NULL (i=%u)\n", i);
141 if (is_memory_overlap(p1, 1000, p2, 1000)) {
142 printf("p1 and p2 overlaps\n");
145 if (is_memory_overlap(p2, 1000, p3, 1000)) {
146 printf("p2 and p3 overlaps\n");
149 if (is_memory_overlap(p1, 1000, p3, 1000)) {
150 printf("p1 and p3 overlaps\n");
153 if (!is_aligned(p1, align1)) {
154 printf("p1 is not aligned\n");
157 if (!is_aligned(p2, align2)) {
158 printf("p2 is not aligned\n");
161 if (!is_aligned(p3, align3)) {
162 printf("p3 is not aligned\n");
169 rte_malloc_dump_stats("dummy");
175 test_reordered_free_per_lcore(__attribute__((unused)) void *arg)
177 const unsigned align1 = 8,
184 for (i = 0; i < 30; i++) {
185 p1 = rte_zmalloc("dummy", 1000, align1);
187 printf("rte_zmalloc returned NULL (i=%u)\n", i);
191 for(j = 0; j < 1000 ; j++) {
192 if( *(char *)p1 != 0) {
193 printf("rte_zmalloc didn't zeroed"
194 "the allocated memory\n");
198 /* use calloc to allocate 1000 16-byte items this time */
199 p2 = rte_calloc("dummy", 1000, 16, align2);
200 /* for third request use regular malloc again */
201 p3 = rte_malloc("dummy", 1000, align3);
203 printf("rte_malloc returned NULL (i=%u)\n", i);
207 if (is_memory_overlap(p1, 1000, p2, 1000)) {
208 printf("p1 and p2 overlaps\n");
211 if (is_memory_overlap(p2, 1000, p3, 1000)) {
212 printf("p2 and p3 overlaps\n");
215 if (is_memory_overlap(p1, 1000, p3, 1000)) {
216 printf("p1 and p3 overlaps\n");
219 if (!is_aligned(p1, align1)) {
220 printf("p1 is not aligned\n");
223 if (!is_aligned(p2, align2)) {
224 printf("p2 is not aligned\n");
227 if (!is_aligned(p3, align3)) {
228 printf("p3 is not aligned\n");
231 /* try freeing in every possible order */
265 rte_malloc_dump_stats("dummy");
271 /* test function inside the malloc lib*/
273 test_str_to_size(void)
279 {{ "5G", (uint64_t)5 * 1024 * 1024 *1024 },
280 {"0x20g", (uint64_t)0x20 * 1024 * 1024 *1024},
281 {"10M", 10 * 1024 * 1024},
282 {"050m", 050 * 1024 * 1024},
288 {"-1", 0}, /* negative values return 0 */
291 {"18446744073709551616", 0} /* ULLONG_MAX + 1 == out of range*/
294 for (i = 0; i < sizeof(test_values)/sizeof(test_values[0]); i++)
295 if (rte_str_to_size(test_values[i].str) != test_values[i].value)
303 void *p1 = rte_malloc("BIG", rte_str_to_size(MALLOC_MEMZONE_SIZE) * 2, 1024);
311 test_memzone_size_alloc(void)
313 void *p1 = rte_malloc("BIG", rte_str_to_size(MALLOC_MEMZONE_SIZE) - 128, 64);
317 /* one extra check - check no crashes if free(NULL) */
323 test_rte_malloc_type_limits(void)
325 /* The type-limits functionality is not yet implemented,
326 * so always return 0 no matter what the retval.
328 const char *typename = "limit_test";
329 rte_malloc_set_limit(typename, 64 * 1024);
330 rte_malloc_dump_stats(typename);
337 const char hello_str[] = "Hello, world!";
338 const unsigned size1 = 1024;
339 const unsigned size2 = size1 + 1024;
340 const unsigned size3 = size2;
341 const unsigned size4 = size3 + 1024;
343 /* test data is the same even if element is moved*/
344 char *ptr1 = rte_zmalloc(NULL, size1, CACHE_LINE_SIZE);
346 printf("NULL pointer returned from rte_zmalloc\n");
349 rte_snprintf(ptr1, size1, "%s" ,hello_str);
350 char *ptr2 = rte_realloc(ptr1, size2, CACHE_LINE_SIZE);
353 printf("NULL pointer returned from rte_realloc\n");
357 printf("unexpected - ptr1 == ptr2\n");
359 if (strcmp(ptr2, hello_str) != 0){
360 printf("Error - lost data from pointed area\n");
365 for (i = strnlen(hello_str, sizeof(hello_str)); i < size1; i++)
367 printf("Bad data in realloc\n");
371 /* now allocate third element, free the second
372 * and resize third. It should not move. (ptr1 is now invalid)
374 char *ptr3 = rte_zmalloc(NULL, size3, CACHE_LINE_SIZE);
376 printf("NULL pointer returned from rte_zmalloc\n");
380 for (i = 0; i < size3; i++)
382 printf("Bad data in zmalloc\n");
388 /* first resize to half the size of the freed block */
389 char *ptr4 = rte_realloc(ptr3, size4, CACHE_LINE_SIZE);
391 printf("NULL pointer returned from rte_realloc\n");
396 printf("Unexpected - ptr4 != ptr3\n");
400 /* now resize again to the full size of the freed block */
401 ptr4 = rte_realloc(ptr3, size3 + size2 + size1, CACHE_LINE_SIZE);
403 printf("Unexpected - ptr4 != ptr3 on second resize\n");
409 /* now try a resize to a smaller size, see if it works */
410 const unsigned size5 = 1024;
411 const unsigned size6 = size5 / 2;
412 char *ptr5 = rte_malloc(NULL, size5, CACHE_LINE_SIZE);
414 printf("NULL pointer returned from rte_malloc\n");
417 char *ptr6 = rte_realloc(ptr5, size6, CACHE_LINE_SIZE);
419 printf("NULL pointer returned from rte_realloc\n");
424 printf("Error, resizing to a smaller size moved data\n");
430 /* check for behaviour changing alignment */
431 const unsigned size7 = 1024;
432 const unsigned orig_align = CACHE_LINE_SIZE;
433 unsigned new_align = CACHE_LINE_SIZE * 2;
434 char *ptr7 = rte_malloc(NULL, size7, orig_align);
436 printf("NULL pointer returned from rte_malloc\n");
439 /* calc an alignment we don't already have */
440 while(RTE_ALIGN(ptr7, new_align) == ptr7)
442 char *ptr8 = rte_realloc(ptr7, size7, new_align);
444 printf("NULL pointer returned from rte_realloc\n");
448 if (RTE_ALIGN(ptr8, new_align) != ptr8){
449 printf("Failure to re-align data\n");
455 /* test behaviour when there is a free block after current one,
456 * but its not big enough
458 unsigned size9 = 1024, size10 = 1024;
459 unsigned size11 = size9 + size10 + 256;
460 char *ptr9 = rte_malloc(NULL, size9, CACHE_LINE_SIZE);
462 printf("NULL pointer returned from rte_malloc\n");
465 char *ptr10 = rte_malloc(NULL, size10, CACHE_LINE_SIZE);
467 printf("NULL pointer returned from rte_malloc\n");
471 char *ptr11 = rte_realloc(ptr10, size11, CACHE_LINE_SIZE);
473 printf("NULL pointer returned from rte_realloc\n");
478 printf("Error, unexpected that realloc has not created new buffer\n");
484 /* check we don't crash if we pass null to realloc
485 * We should get a malloc of the size requested*/
486 const size_t size12 = 1024;
488 char *ptr12 = rte_realloc(NULL, size12, CACHE_LINE_SIZE);
490 printf("NULL pointer returned from rte_realloc\n");
493 if (rte_malloc_validate(ptr12, &size12_check) < 0 ||
494 size12_check != size12){
503 test_random_alloc_free(void *_ __attribute__((unused)))
506 struct mem_list *next;
512 rte_srand((unsigned)rte_rdtsc());
514 for (i = 0; i < N; i++){
515 unsigned free_mem = 0;
516 size_t allocated_size;
518 const unsigned mem_size = sizeof(struct mem_list) + \
519 rte_rand() % (64 * 1024);
520 const unsigned align = 1 << (rte_rand() % 12); /* up to 4k alignment */
521 struct mem_list *entry = rte_malloc(NULL,
525 if (RTE_ALIGN(entry, align)!= entry)
527 if (rte_malloc_validate(entry, &allocated_size) == -1
528 || allocated_size < mem_size)
530 memset(entry->data, rte_lcore_id(),
531 mem_size - sizeof(*entry));
532 entry->next = list_head;
533 if (rte_malloc_validate(entry, NULL) == -1)
538 /* switch to freeing the memory with a 20% probability */
539 free_mem = ((rte_rand() % 10) >= 8);
542 struct mem_list *entry = list_head;
543 list_head = list_head->next;
547 printf("Lcore %u allocated/freed %u blocks\n", rte_lcore_id(), count);
551 #define err_return() do { \
552 printf("%s: %d - Error\n", __func__, __LINE__); \
557 test_rte_malloc_validate(void)
559 const size_t request_size = 1024;
560 size_t allocated_size;
561 char *data_ptr = rte_malloc(NULL, request_size, CACHE_LINE_SIZE);
562 if (data_ptr == NULL) {
563 printf("%s: %d - Allocation error\n", __func__, __LINE__);
567 /* check that a null input returns -1 */
568 if (rte_malloc_validate(NULL, NULL) != -1)
571 /* check that we get ok on a valid pointer */
572 if (rte_malloc_validate(data_ptr, &allocated_size) < 0)
575 /* check that the returned size is ok */
576 if (allocated_size < request_size)
579 #ifdef RTE_LIBRTE_MALLOC_DEBUG
581 char *over_write_vals = NULL;
583 /****** change the header to be bad */
585 over_write_vals = (char *)((uintptr_t)data_ptr - sizeof(save_buf));
586 /* first save the data as a backup before overwriting it */
587 memcpy(save_buf, over_write_vals, sizeof(save_buf));
588 memset(over_write_vals, 1, sizeof(save_buf));
589 /* then run validate */
590 retval = rte_malloc_validate(data_ptr, NULL);
591 /* finally restore the data again */
592 memcpy(over_write_vals, save_buf, sizeof(save_buf));
593 /* check we previously had an error */
597 /* check all ok again */
598 if (rte_malloc_validate(data_ptr, &allocated_size) < 0)
601 /**** change the trailer to be bad */
602 over_write_vals = (char *)((uintptr_t)data_ptr + allocated_size);
603 /* first save the data as a backup before overwriting it */
604 memcpy(save_buf, over_write_vals, sizeof(save_buf));
605 memset(over_write_vals, 1, sizeof(save_buf));
606 /* then run validate */
607 retval = rte_malloc_validate(data_ptr, NULL);
608 /* finally restore the data again */
609 memcpy(over_write_vals, save_buf, sizeof(save_buf));
613 /* check all ok again */
614 if (rte_malloc_validate(data_ptr, &allocated_size) < 0)
628 test_zero_aligned_alloc(void)
630 char *p1 = rte_malloc(NULL,1024, 0);
633 if (!rte_is_aligned(p1, CACHE_LINE_SIZE))
640 if (p1) rte_free(p1);
645 test_malloc_bad_params(void)
647 const char *type = NULL;
649 unsigned align = CACHE_LINE_SIZE;
651 /* rte_malloc expected to return null with inappropriate size */
652 char *bad_ptr = rte_malloc(type, size, align);
656 /* rte_malloc expected to return null with inappropriate alignment */
660 bad_ptr = rte_malloc(type, size, align);
667 /* clean up pointer */
679 if (test_str_to_size() < 0){
680 printf("test_str_to_size() failed\n");
683 else printf("test_str_to_size() passed\n");
685 if (test_memzone_size_alloc() < 0){
686 printf("test_memzone_size_alloc() failed\n");
689 else printf("test_memzone_size_alloc() passed\n");
691 if (test_big_alloc() < 0){
692 printf("test_big_alloc() failed\n");
695 else printf("test_big_alloc() passed\n");
697 if (test_zero_aligned_alloc() < 0){
698 printf("test_zero_aligned_alloc() failed\n");
701 else printf("test_zero_aligned_alloc() passed\n");
703 if (test_malloc_bad_params() < 0){
704 printf("test_malloc_bad_params() failed\n");
707 else printf("test_malloc_bad_params() passed\n");
709 if (test_realloc() < 0){
710 printf("test_realloc() failed\n");
713 else printf("test_realloc() passed\n");
714 /*----------------------------*/
715 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
716 rte_eal_remote_launch(test_align_overlap_per_lcore, NULL, lcore_id);
719 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
720 if (rte_eal_wait_lcore(lcore_id) < 0)
724 printf("test_align_overlap_per_lcore() failed\n");
727 else printf("test_align_overlap_per_lcore() passed\n");
728 /*----------------------------*/
729 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
730 rte_eal_remote_launch(test_reordered_free_per_lcore, NULL, lcore_id);
733 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
734 if (rte_eal_wait_lcore(lcore_id) < 0)
738 printf("test_reordered_free_per_lcore() failed\n");
741 else printf("test_reordered_free_per_lcore() passed\n");
743 /*----------------------------*/
744 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
745 rte_eal_remote_launch(test_random_alloc_free, NULL, lcore_id);
748 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
749 if (rte_eal_wait_lcore(lcore_id) < 0)
753 printf("test_random_alloc_free() failed\n");
756 else printf("test_random_alloc_free() passed\n");
758 /*----------------------------*/
759 ret = test_rte_malloc_type_limits();
761 printf("test_rte_malloc_type_limits() failed\n");
764 /* TODO: uncomment following line once type limits are valid */
765 /*else printf("test_rte_malloc_type_limits() passed\n");*/
767 /*----------------------------*/
768 ret = test_rte_malloc_validate();
770 printf("test_rte_malloc_validate() failed\n");
773 else printf("test_rte_malloc_validate() passed\n");