4 * Copyright(c) 2010-2014 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.
40 #include <sys/queue.h>
42 #include <rte_common.h>
43 #include <rte_memory.h>
44 #include <rte_memzone.h>
45 #include <rte_per_lcore.h>
46 #include <rte_launch.h>
48 #include <rte_lcore.h>
49 #include <rte_malloc.h>
50 #include <rte_cycles.h>
51 #include <rte_random.h>
52 #include <rte_string_fns.h>
62 * Allocate some dynamic memory from heap (3 areas). Check that areas
63 * don't overlap and that alignment constraints match. This test is
64 * done many times on different lcores simultaneously.
67 /* Test if memory overlaps: return 1 if true, or 0 if false. */
69 is_memory_overlap(void *p1, size_t len1, void *p2, size_t len2)
71 unsigned long ptr1 = (unsigned long)p1;
72 unsigned long ptr2 = (unsigned long)p2;
74 if (ptr2 >= ptr1 && (ptr2 - ptr1) < len1)
76 else if (ptr2 < ptr1 && (ptr1 - ptr2) < len2)
82 is_aligned(void *p, int align)
84 unsigned long addr = (unsigned long)p;
85 unsigned mask = align - 1;
93 test_align_overlap_per_lcore(__attribute__((unused)) void *arg)
95 const unsigned align1 = 8,
99 void *p1 = NULL, *p2 = NULL, *p3 = NULL;
102 for (i = 0; i < N; i++) {
103 p1 = rte_zmalloc("dummy", 1000, align1);
105 printf("rte_zmalloc returned NULL (i=%u)\n", i);
109 for(j = 0; j < 1000 ; j++) {
110 if( *(char *)p1 != 0) {
111 printf("rte_zmalloc didn't zero"
112 "the allocated memory\n");
116 p2 = rte_malloc("dummy", 1000, align2);
118 printf("rte_malloc returned NULL (i=%u)\n", i);
123 p3 = rte_malloc("dummy", 1000, align3);
125 printf("rte_malloc returned NULL (i=%u)\n", i);
131 if (is_memory_overlap(p1, 1000, p2, 1000)) {
132 printf("p1 and p2 overlaps\n");
135 if (is_memory_overlap(p2, 1000, p3, 1000)) {
136 printf("p2 and p3 overlaps\n");
139 if (is_memory_overlap(p1, 1000, p3, 1000)) {
140 printf("p1 and p3 overlaps\n");
143 if (!is_aligned(p1, align1)) {
144 printf("p1 is not aligned\n");
147 if (!is_aligned(p2, align2)) {
148 printf("p2 is not aligned\n");
151 if (!is_aligned(p3, align3)) {
152 printf("p3 is not aligned\n");
159 rte_malloc_dump_stats(stdout, "dummy");
165 test_reordered_free_per_lcore(__attribute__((unused)) void *arg)
167 const unsigned align1 = 8,
174 for (i = 0; i < 30; i++) {
175 p1 = rte_zmalloc("dummy", 1000, align1);
177 printf("rte_zmalloc returned NULL (i=%u)\n", i);
181 for(j = 0; j < 1000 ; j++) {
182 if( *(char *)p1 != 0) {
183 printf("rte_zmalloc didn't zero"
184 "the allocated memory\n");
188 /* use calloc to allocate 1000 16-byte items this time */
189 p2 = rte_calloc("dummy", 1000, 16, align2);
190 /* for third request use regular malloc again */
191 p3 = rte_malloc("dummy", 1000, align3);
193 printf("rte_malloc returned NULL (i=%u)\n", i);
197 if (is_memory_overlap(p1, 1000, p2, 1000)) {
198 printf("p1 and p2 overlaps\n");
201 if (is_memory_overlap(p2, 1000, p3, 1000)) {
202 printf("p2 and p3 overlaps\n");
205 if (is_memory_overlap(p1, 1000, p3, 1000)) {
206 printf("p1 and p3 overlaps\n");
209 if (!is_aligned(p1, align1)) {
210 printf("p1 is not aligned\n");
213 if (!is_aligned(p2, align2)) {
214 printf("p2 is not aligned\n");
217 if (!is_aligned(p3, align3)) {
218 printf("p3 is not aligned\n");
221 /* try freeing in every possible order */
255 rte_malloc_dump_stats(stdout, "dummy");
260 /* test function inside the malloc lib*/
262 test_str_to_size(void)
268 {{ "5G", (uint64_t)5 * 1024 * 1024 *1024 },
269 {"0x20g", (uint64_t)0x20 * 1024 * 1024 *1024},
270 {"10M", 10 * 1024 * 1024},
271 {"050m", 050 * 1024 * 1024},
277 {"-1", 0}, /* negative values return 0 */
280 {"18446744073709551616", 0} /* ULLONG_MAX + 1 == out of range*/
283 for (i = 0; i < sizeof(test_values)/sizeof(test_values[0]); i++)
284 if (rte_str_to_size(test_values[i].str) != test_values[i].value)
290 test_multi_alloc_statistics(void)
293 struct rte_malloc_socket_stats pre_stats, post_stats ,first_stats, second_stats;
296 #ifndef RTE_LIBRTE_MALLOC_DEBUG
297 int trailer_size = 0;
299 int trailer_size = RTE_CACHE_LINE_SIZE;
301 int overhead = RTE_CACHE_LINE_SIZE + trailer_size;
303 rte_malloc_get_socket_stats(socket, &pre_stats);
305 void *p1 = rte_malloc_socket("stats", size , align, socket);
309 rte_malloc_dump_stats(stdout, "stats");
311 rte_malloc_get_socket_stats(socket,&post_stats);
312 /* Check statistics reported are correct */
313 /* All post stats should be equal to pre stats after alloc freed */
314 if ((post_stats.heap_totalsz_bytes != pre_stats.heap_totalsz_bytes) &&
315 (post_stats.heap_freesz_bytes!=pre_stats.heap_freesz_bytes) &&
316 (post_stats.heap_allocsz_bytes!=pre_stats.heap_allocsz_bytes)&&
317 (post_stats.alloc_count!=pre_stats.alloc_count)&&
318 (post_stats.free_count!=pre_stats.free_count)) {
319 printf("Malloc statistics are incorrect - freed alloc\n");
322 /* Check two consecutive allocations */
325 rte_malloc_get_socket_stats(socket,&pre_stats);
326 void *p2 = rte_malloc_socket("add", size ,align, socket);
329 rte_malloc_get_socket_stats(socket,&first_stats);
331 void *p3 = rte_malloc_socket("add2", size,align, socket);
335 rte_malloc_get_socket_stats(socket,&second_stats);
340 /* After freeing both allocations check stats return to original */
341 rte_malloc_get_socket_stats(socket, &post_stats);
343 if(second_stats.heap_totalsz_bytes != first_stats.heap_totalsz_bytes) {
344 printf("Incorrect heap statistics: Total size \n");
347 /* Check allocated size is equal to two additions plus overhead */
348 if(second_stats.heap_allocsz_bytes !=
349 size + overhead + first_stats.heap_allocsz_bytes) {
350 printf("Incorrect heap statistics: Allocated size \n");
353 /* Check that allocation count increments correctly i.e. +1 */
354 if (second_stats.alloc_count != first_stats.alloc_count + 1) {
355 printf("Incorrect heap statistics: Allocated count \n");
359 if (second_stats.free_count != first_stats.free_count){
360 printf("Incorrect heap statistics: Free count \n");
364 /* Make sure that we didn't touch our greatest chunk: 2 * 11M) */
365 if (post_stats.greatest_free_size != pre_stats.greatest_free_size) {
366 printf("Incorrect heap statistics: Greatest free size \n");
369 /* Free size must equal the original free size minus the new allocation*/
370 if (first_stats.heap_freesz_bytes <= second_stats.heap_freesz_bytes) {
371 printf("Incorrect heap statistics: Free size \n");
375 if ((post_stats.heap_totalsz_bytes != pre_stats.heap_totalsz_bytes) &&
376 (post_stats.heap_freesz_bytes!=pre_stats.heap_freesz_bytes) &&
377 (post_stats.heap_allocsz_bytes!=pre_stats.heap_allocsz_bytes)&&
378 (post_stats.alloc_count!=pre_stats.alloc_count)&&
379 (post_stats.free_count!=pre_stats.free_count)) {
380 printf("Malloc statistics are incorrect - freed alloc\n");
387 test_rte_malloc_type_limits(void)
389 /* The type-limits functionality is not yet implemented,
390 * so always return 0 no matter what the retval.
392 const char *typename = "limit_test";
393 rte_malloc_set_limit(typename, 64 * 1024);
394 rte_malloc_dump_stats(stdout, typename);
401 const char hello_str[] = "Hello, world!";
402 const unsigned size1 = 1024;
403 const unsigned size2 = size1 + 1024;
404 const unsigned size3 = size2;
405 const unsigned size4 = size3 + 1024;
407 /* test data is the same even if element is moved*/
408 char *ptr1 = rte_zmalloc(NULL, size1, RTE_CACHE_LINE_SIZE);
410 printf("NULL pointer returned from rte_zmalloc\n");
413 snprintf(ptr1, size1, "%s" ,hello_str);
414 char *ptr2 = rte_realloc(ptr1, size2, RTE_CACHE_LINE_SIZE);
417 printf("NULL pointer returned from rte_realloc\n");
421 printf("unexpected - ptr1 == ptr2\n");
423 if (strcmp(ptr2, hello_str) != 0){
424 printf("Error - lost data from pointed area\n");
429 for (i = strnlen(hello_str, sizeof(hello_str)); i < size1; i++)
431 printf("Bad data in realloc\n");
435 /* now allocate third element, free the second
436 * and resize third. It should not move. (ptr1 is now invalid)
438 char *ptr3 = rte_zmalloc(NULL, size3, RTE_CACHE_LINE_SIZE);
440 printf("NULL pointer returned from rte_zmalloc\n");
444 for (i = 0; i < size3; i++)
446 printf("Bad data in zmalloc\n");
452 /* first resize to half the size of the freed block */
453 char *ptr4 = rte_realloc(ptr3, size4, RTE_CACHE_LINE_SIZE);
455 printf("NULL pointer returned from rte_realloc\n");
460 printf("Unexpected - ptr4 != ptr3\n");
464 /* now resize again to the full size of the freed block */
465 ptr4 = rte_realloc(ptr3, size3 + size2 + size1, RTE_CACHE_LINE_SIZE);
467 printf("Unexpected - ptr4 != ptr3 on second resize\n");
473 /* now try a resize to a smaller size, see if it works */
474 const unsigned size5 = 1024;
475 const unsigned size6 = size5 / 2;
476 char *ptr5 = rte_malloc(NULL, size5, RTE_CACHE_LINE_SIZE);
478 printf("NULL pointer returned from rte_malloc\n");
481 char *ptr6 = rte_realloc(ptr5, size6, RTE_CACHE_LINE_SIZE);
483 printf("NULL pointer returned from rte_realloc\n");
488 printf("Error, resizing to a smaller size moved data\n");
494 /* check for behaviour changing alignment */
495 const unsigned size7 = 1024;
496 const unsigned orig_align = RTE_CACHE_LINE_SIZE;
497 unsigned new_align = RTE_CACHE_LINE_SIZE * 2;
498 char *ptr7 = rte_malloc(NULL, size7, orig_align);
500 printf("NULL pointer returned from rte_malloc\n");
503 /* calc an alignment we don't already have */
504 while(RTE_PTR_ALIGN(ptr7, new_align) == ptr7)
506 char *ptr8 = rte_realloc(ptr7, size7, new_align);
508 printf("NULL pointer returned from rte_realloc\n");
512 if (RTE_PTR_ALIGN(ptr8, new_align) != ptr8){
513 printf("Failure to re-align data\n");
519 /* test behaviour when there is a free block after current one,
520 * but its not big enough
522 unsigned size9 = 1024, size10 = 1024;
523 unsigned size11 = size9 + size10 + 256;
524 char *ptr9 = rte_malloc(NULL, size9, RTE_CACHE_LINE_SIZE);
526 printf("NULL pointer returned from rte_malloc\n");
529 char *ptr10 = rte_malloc(NULL, size10, RTE_CACHE_LINE_SIZE);
531 printf("NULL pointer returned from rte_malloc\n");
535 char *ptr11 = rte_realloc(ptr10, size11, RTE_CACHE_LINE_SIZE);
537 printf("NULL pointer returned from rte_realloc\n");
542 printf("Error, unexpected that realloc has not created new buffer\n");
548 /* check we don't crash if we pass null to realloc
549 * We should get a malloc of the size requested*/
550 const size_t size12 = 1024;
552 char *ptr12 = rte_realloc(NULL, size12, RTE_CACHE_LINE_SIZE);
554 printf("NULL pointer returned from rte_realloc\n");
557 if (rte_malloc_validate(ptr12, &size12_check) < 0 ||
558 size12_check != size12){
567 test_random_alloc_free(void *_ __attribute__((unused)))
570 struct mem_list *next;
576 rte_srand((unsigned)rte_rdtsc());
578 for (i = 0; i < N; i++){
579 unsigned free_mem = 0;
580 size_t allocated_size;
582 const unsigned mem_size = sizeof(struct mem_list) + \
583 rte_rand() % (64 * 1024);
584 const unsigned align = 1 << (rte_rand() % 12); /* up to 4k alignment */
585 struct mem_list *entry = rte_malloc(NULL,
589 if (RTE_PTR_ALIGN(entry, align)!= entry)
591 if (rte_malloc_validate(entry, &allocated_size) == -1
592 || allocated_size < mem_size)
594 memset(entry->data, rte_lcore_id(),
595 mem_size - sizeof(*entry));
596 entry->next = list_head;
597 if (rte_malloc_validate(entry, NULL) == -1)
602 /* switch to freeing the memory with a 20% probability */
603 free_mem = ((rte_rand() % 10) >= 8);
606 struct mem_list *entry = list_head;
607 list_head = list_head->next;
611 printf("Lcore %u allocated/freed %u blocks\n", rte_lcore_id(), count);
615 #define err_return() do { \
616 printf("%s: %d - Error\n", __func__, __LINE__); \
621 test_rte_malloc_validate(void)
623 const size_t request_size = 1024;
624 size_t allocated_size;
625 char *data_ptr = rte_malloc(NULL, request_size, RTE_CACHE_LINE_SIZE);
626 #ifdef RTE_LIBRTE_MALLOC_DEBUG
628 char *over_write_vals = NULL;
631 if (data_ptr == NULL) {
632 printf("%s: %d - Allocation error\n", __func__, __LINE__);
636 /* check that a null input returns -1 */
637 if (rte_malloc_validate(NULL, NULL) != -1)
640 /* check that we get ok on a valid pointer */
641 if (rte_malloc_validate(data_ptr, &allocated_size) < 0)
644 /* check that the returned size is ok */
645 if (allocated_size < request_size)
648 #ifdef RTE_LIBRTE_MALLOC_DEBUG
650 /****** change the header to be bad */
652 over_write_vals = (char *)((uintptr_t)data_ptr - sizeof(save_buf));
653 /* first save the data as a backup before overwriting it */
654 memcpy(save_buf, over_write_vals, sizeof(save_buf));
655 memset(over_write_vals, 1, sizeof(save_buf));
656 /* then run validate */
657 retval = rte_malloc_validate(data_ptr, NULL);
658 /* finally restore the data again */
659 memcpy(over_write_vals, save_buf, sizeof(save_buf));
660 /* check we previously had an error */
664 /* check all ok again */
665 if (rte_malloc_validate(data_ptr, &allocated_size) < 0)
668 /**** change the trailer to be bad */
669 over_write_vals = (char *)((uintptr_t)data_ptr + allocated_size);
670 /* first save the data as a backup before overwriting it */
671 memcpy(save_buf, over_write_vals, sizeof(save_buf));
672 memset(over_write_vals, 1, sizeof(save_buf));
673 /* then run validate */
674 retval = rte_malloc_validate(data_ptr, NULL);
675 /* finally restore the data again */
676 memcpy(over_write_vals, save_buf, sizeof(save_buf));
680 /* check all ok again */
681 if (rte_malloc_validate(data_ptr, &allocated_size) < 0)
695 test_zero_aligned_alloc(void)
697 char *p1 = rte_malloc(NULL,1024, 0);
700 if (!rte_is_aligned(p1, RTE_CACHE_LINE_SIZE))
707 if (p1) rte_free(p1);
712 test_malloc_bad_params(void)
714 const char *type = NULL;
716 unsigned align = RTE_CACHE_LINE_SIZE;
718 /* rte_malloc expected to return null with inappropriate size */
719 char *bad_ptr = rte_malloc(type, size, align);
723 /* rte_malloc expected to return null with inappropriate alignment */
727 bad_ptr = rte_malloc(type, size, align);
734 /* clean up pointer */
740 /* Check if memory is available on a specific socket */
742 is_mem_on_socket(int32_t socket)
744 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
747 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
748 if (socket == ms[i].socket_id)
755 * Find what socket a memory address is on. Only works for addresses within
756 * memsegs, not heap or stack...
759 addr_to_socket(void * addr)
761 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
764 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
765 if ((ms[i].addr <= addr) &&
767 ((uintptr_t)ms[i].addr + (uintptr_t)ms[i].len)))
768 return ms[i].socket_id;
773 /* Test using rte_[c|m|zm]alloc_socket() on a specific socket */
775 test_alloc_single_socket(int32_t socket)
777 const char *type = NULL;
778 const size_t size = 10;
779 const unsigned align = 0;
781 int32_t desired_socket = (socket == SOCKET_ID_ANY) ?
782 (int32_t)rte_socket_id() : socket;
784 /* Test rte_calloc_socket() */
785 mem = rte_calloc_socket(type, size, sizeof(char), align, socket);
788 if (addr_to_socket(mem) != desired_socket) {
794 /* Test rte_malloc_socket() */
795 mem = rte_malloc_socket(type, size, align, socket);
798 if (addr_to_socket(mem) != desired_socket) {
803 /* Test rte_zmalloc_socket() */
804 mem = rte_zmalloc_socket(type, size, align, socket);
807 if (addr_to_socket(mem) != desired_socket) {
817 test_alloc_socket(void)
819 unsigned socket_count = 0;
822 if (test_alloc_single_socket(SOCKET_ID_ANY) < 0)
825 for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
826 if (is_mem_on_socket(i)) {
828 if (test_alloc_single_socket(i) < 0) {
829 printf("Fail: rte_malloc_socket(..., %u) did not succeed\n",
835 if (test_alloc_single_socket(i) == 0) {
836 printf("Fail: rte_malloc_socket(..., %u) succeeded\n",
843 /* Print warnign if only a single socket, but don't fail the test */
844 if (socket_count < 2) {
845 printf("WARNING: alloc_socket test needs memory on multiple sockets!\n");
857 if (test_str_to_size() < 0){
858 printf("test_str_to_size() failed\n");
861 else printf("test_str_to_size() passed\n");
863 if (test_zero_aligned_alloc() < 0){
864 printf("test_zero_aligned_alloc() failed\n");
867 else printf("test_zero_aligned_alloc() passed\n");
869 if (test_malloc_bad_params() < 0){
870 printf("test_malloc_bad_params() failed\n");
873 else printf("test_malloc_bad_params() passed\n");
875 if (test_realloc() < 0){
876 printf("test_realloc() failed\n");
879 else printf("test_realloc() passed\n");
881 /*----------------------------*/
882 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
883 rte_eal_remote_launch(test_align_overlap_per_lcore, NULL, lcore_id);
886 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
887 if (rte_eal_wait_lcore(lcore_id) < 0)
891 printf("test_align_overlap_per_lcore() failed\n");
894 else printf("test_align_overlap_per_lcore() passed\n");
896 /*----------------------------*/
897 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
898 rte_eal_remote_launch(test_reordered_free_per_lcore, NULL, lcore_id);
901 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
902 if (rte_eal_wait_lcore(lcore_id) < 0)
906 printf("test_reordered_free_per_lcore() failed\n");
909 else printf("test_reordered_free_per_lcore() passed\n");
911 /*----------------------------*/
912 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
913 rte_eal_remote_launch(test_random_alloc_free, NULL, lcore_id);
916 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
917 if (rte_eal_wait_lcore(lcore_id) < 0)
921 printf("test_random_alloc_free() failed\n");
924 else printf("test_random_alloc_free() passed\n");
926 /*----------------------------*/
927 ret = test_rte_malloc_type_limits();
929 printf("test_rte_malloc_type_limits() failed\n");
932 /* TODO: uncomment following line once type limits are valid */
933 /*else printf("test_rte_malloc_type_limits() passed\n");*/
935 /*----------------------------*/
936 ret = test_rte_malloc_validate();
938 printf("test_rte_malloc_validate() failed\n");
941 else printf("test_rte_malloc_validate() passed\n");
943 ret = test_alloc_socket();
945 printf("test_alloc_socket() failed\n");
948 else printf("test_alloc_socket() passed\n");
950 ret = test_multi_alloc_statistics();
952 printf("test_multi_alloc_statistics() failed\n");
956 printf("test_multi_alloc_statistics() passed\n");
961 REGISTER_TEST_COMMAND(malloc_autotest, test_malloc);