1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 #include <sys/queue.h>
13 #include <rte_common.h>
14 #include <rte_memory.h>
15 #include <rte_per_lcore.h>
16 #include <rte_launch.h>
18 #include <rte_lcore.h>
19 #include <rte_malloc.h>
20 #include <rte_cycles.h>
21 #include <rte_random.h>
22 #include <rte_string_fns.h>
32 * Allocate some dynamic memory from heap (3 areas). Check that areas
33 * don't overlap and that alignment constraints match. This test is
34 * done many times on different lcores simultaneously.
37 /* Test if memory overlaps: return 1 if true, or 0 if false. */
39 is_memory_overlap(void *p1, size_t len1, void *p2, size_t len2)
41 unsigned long ptr1 = (unsigned long)p1;
42 unsigned long ptr2 = (unsigned long)p2;
44 if (ptr2 >= ptr1 && (ptr2 - ptr1) < len1)
46 else if (ptr2 < ptr1 && (ptr1 - ptr2) < len2)
52 is_aligned(void *p, int align)
54 unsigned long addr = (unsigned long)p;
55 unsigned mask = align - 1;
63 test_align_overlap_per_lcore(__attribute__((unused)) void *arg)
65 const unsigned align1 = 8,
69 void *p1 = NULL, *p2 = NULL, *p3 = NULL;
72 for (i = 0; i < N; i++) {
73 p1 = rte_zmalloc("dummy", 1000, align1);
75 printf("rte_zmalloc returned NULL (i=%u)\n", i);
79 for(j = 0; j < 1000 ; j++) {
80 if( *(char *)p1 != 0) {
81 printf("rte_zmalloc didn't zero the allocated memory\n");
85 p2 = rte_malloc("dummy", 1000, align2);
87 printf("rte_malloc returned NULL (i=%u)\n", i);
92 p3 = rte_malloc("dummy", 1000, align3);
94 printf("rte_malloc returned NULL (i=%u)\n", i);
100 if (is_memory_overlap(p1, 1000, p2, 1000)) {
101 printf("p1 and p2 overlaps\n");
104 if (is_memory_overlap(p2, 1000, p3, 1000)) {
105 printf("p2 and p3 overlaps\n");
108 if (is_memory_overlap(p1, 1000, p3, 1000)) {
109 printf("p1 and p3 overlaps\n");
112 if (!is_aligned(p1, align1)) {
113 printf("p1 is not aligned\n");
116 if (!is_aligned(p2, align2)) {
117 printf("p2 is not aligned\n");
120 if (!is_aligned(p3, align3)) {
121 printf("p3 is not aligned\n");
128 rte_malloc_dump_stats(stdout, "dummy");
134 test_reordered_free_per_lcore(__attribute__((unused)) void *arg)
136 const unsigned align1 = 8,
143 for (i = 0; i < 30; i++) {
144 p1 = rte_zmalloc("dummy", 1000, align1);
146 printf("rte_zmalloc returned NULL (i=%u)\n", i);
150 for(j = 0; j < 1000 ; j++) {
151 if( *(char *)p1 != 0) {
152 printf("rte_zmalloc didn't zero the allocated memory\n");
156 /* use calloc to allocate 1000 16-byte items this time */
157 p2 = rte_calloc("dummy", 1000, 16, align2);
158 /* for third request use regular malloc again */
159 p3 = rte_malloc("dummy", 1000, align3);
161 printf("rte_malloc returned NULL (i=%u)\n", i);
165 if (is_memory_overlap(p1, 1000, p2, 1000)) {
166 printf("p1 and p2 overlaps\n");
169 if (is_memory_overlap(p2, 1000, p3, 1000)) {
170 printf("p2 and p3 overlaps\n");
173 if (is_memory_overlap(p1, 1000, p3, 1000)) {
174 printf("p1 and p3 overlaps\n");
177 if (!is_aligned(p1, align1)) {
178 printf("p1 is not aligned\n");
181 if (!is_aligned(p2, align2)) {
182 printf("p2 is not aligned\n");
185 if (!is_aligned(p3, align3)) {
186 printf("p3 is not aligned\n");
189 /* try freeing in every possible order */
223 rte_malloc_dump_stats(stdout, "dummy");
228 /* test function inside the malloc lib*/
230 test_str_to_size(void)
236 {{ "5G", (uint64_t)5 * 1024 * 1024 *1024 },
237 {"0x20g", (uint64_t)0x20 * 1024 * 1024 *1024},
238 {"10M", 10 * 1024 * 1024},
239 {"050m", 050 * 1024 * 1024},
245 {"-1", 0}, /* negative values return 0 */
248 {"18446744073709551616", 0} /* ULLONG_MAX + 1 == out of range*/
251 for (i = 0; i < sizeof(test_values)/sizeof(test_values[0]); i++)
252 if (rte_str_to_size(test_values[i].str) != test_values[i].value)
258 test_multi_alloc_statistics(void)
261 struct rte_malloc_socket_stats pre_stats, post_stats ,first_stats, second_stats;
264 #ifndef RTE_MALLOC_DEBUG
265 int trailer_size = 0;
267 int trailer_size = RTE_CACHE_LINE_SIZE;
269 int overhead = RTE_CACHE_LINE_SIZE + trailer_size;
271 rte_malloc_get_socket_stats(socket, &pre_stats);
273 void *p1 = rte_malloc_socket("stats", size , align, socket);
277 rte_malloc_dump_stats(stdout, "stats");
279 rte_malloc_get_socket_stats(socket,&post_stats);
280 /* Check statistics reported are correct */
281 /* All post stats should be equal to pre stats after alloc freed */
282 if ((post_stats.heap_totalsz_bytes != pre_stats.heap_totalsz_bytes) &&
283 (post_stats.heap_freesz_bytes!=pre_stats.heap_freesz_bytes) &&
284 (post_stats.heap_allocsz_bytes!=pre_stats.heap_allocsz_bytes)&&
285 (post_stats.alloc_count!=pre_stats.alloc_count)&&
286 (post_stats.free_count!=pre_stats.free_count)) {
287 printf("Malloc statistics are incorrect - freed alloc\n");
290 /* Check two consecutive allocations */
293 rte_malloc_get_socket_stats(socket,&pre_stats);
294 void *p2 = rte_malloc_socket("add", size ,align, socket);
297 rte_malloc_get_socket_stats(socket,&first_stats);
299 void *p3 = rte_malloc_socket("add2", size,align, socket);
303 rte_malloc_get_socket_stats(socket,&second_stats);
308 /* After freeing both allocations check stats return to original */
309 rte_malloc_get_socket_stats(socket, &post_stats);
311 if(second_stats.heap_totalsz_bytes != first_stats.heap_totalsz_bytes) {
312 printf("Incorrect heap statistics: Total size \n");
315 /* Check allocated size is equal to two additions plus overhead */
316 if(second_stats.heap_allocsz_bytes !=
317 size + overhead + first_stats.heap_allocsz_bytes) {
318 printf("Incorrect heap statistics: Allocated size \n");
321 /* Check that allocation count increments correctly i.e. +1 */
322 if (second_stats.alloc_count != first_stats.alloc_count + 1) {
323 printf("Incorrect heap statistics: Allocated count \n");
327 if (second_stats.free_count != first_stats.free_count){
328 printf("Incorrect heap statistics: Free count \n");
332 /* Make sure that we didn't touch our greatest chunk: 2 * 11M) */
333 if (post_stats.greatest_free_size != pre_stats.greatest_free_size) {
334 printf("Incorrect heap statistics: Greatest free size \n");
337 /* Free size must equal the original free size minus the new allocation*/
338 if (first_stats.heap_freesz_bytes <= second_stats.heap_freesz_bytes) {
339 printf("Incorrect heap statistics: Free size \n");
343 if ((post_stats.heap_totalsz_bytes != pre_stats.heap_totalsz_bytes) &&
344 (post_stats.heap_freesz_bytes!=pre_stats.heap_freesz_bytes) &&
345 (post_stats.heap_allocsz_bytes!=pre_stats.heap_allocsz_bytes)&&
346 (post_stats.alloc_count!=pre_stats.alloc_count)&&
347 (post_stats.free_count!=pre_stats.free_count)) {
348 printf("Malloc statistics are incorrect - freed alloc\n");
355 test_rte_malloc_type_limits(void)
357 /* The type-limits functionality is not yet implemented,
358 * so always return 0 no matter what the retval.
360 const char *typename = "limit_test";
361 rte_malloc_set_limit(typename, 64 * 1024);
362 rte_malloc_dump_stats(stdout, typename);
369 const char hello_str[] = "Hello, world!";
370 const unsigned size1 = 1024;
371 const unsigned size2 = size1 + 1024;
372 const unsigned size3 = size2;
373 const unsigned size4 = size3 + 1024;
375 /* test data is the same even if element is moved*/
376 char *ptr1 = rte_zmalloc(NULL, size1, RTE_CACHE_LINE_SIZE);
378 printf("NULL pointer returned from rte_zmalloc\n");
381 strlcpy(ptr1, hello_str, size1);
382 char *ptr2 = rte_realloc(ptr1, size2, RTE_CACHE_LINE_SIZE);
385 printf("NULL pointer returned from rte_realloc\n");
389 printf("unexpected - ptr1 == ptr2\n");
391 if (strcmp(ptr2, hello_str) != 0){
392 printf("Error - lost data from pointed area\n");
397 for (i = strnlen(hello_str, sizeof(hello_str)); i < size1; i++)
399 printf("Bad data in realloc\n");
403 /* now allocate third element, free the second
404 * and resize third. It should not move. (ptr1 is now invalid)
406 char *ptr3 = rte_zmalloc(NULL, size3, RTE_CACHE_LINE_SIZE);
408 printf("NULL pointer returned from rte_zmalloc\n");
412 for (i = 0; i < size3; i++)
414 printf("Bad data in zmalloc\n");
420 /* first resize to half the size of the freed block */
421 char *ptr4 = rte_realloc(ptr3, size4, RTE_CACHE_LINE_SIZE);
423 printf("NULL pointer returned from rte_realloc\n");
428 printf("Unexpected - ptr4 != ptr3\n");
432 /* now resize again to the full size of the freed block */
433 ptr4 = rte_realloc(ptr3, size3 + size2 + size1, RTE_CACHE_LINE_SIZE);
435 printf("Unexpected - ptr4 != ptr3 on second resize\n");
441 /* now try a resize to a smaller size, see if it works */
442 const unsigned size5 = 1024;
443 const unsigned size6 = size5 / 2;
444 char *ptr5 = rte_malloc(NULL, size5, RTE_CACHE_LINE_SIZE);
446 printf("NULL pointer returned from rte_malloc\n");
449 char *ptr6 = rte_realloc(ptr5, size6, RTE_CACHE_LINE_SIZE);
451 printf("NULL pointer returned from rte_realloc\n");
456 printf("Error, resizing to a smaller size moved data\n");
462 /* check for behaviour changing alignment */
463 const unsigned size7 = 1024;
464 const unsigned orig_align = RTE_CACHE_LINE_SIZE;
465 unsigned new_align = RTE_CACHE_LINE_SIZE * 2;
466 char *ptr7 = rte_malloc(NULL, size7, orig_align);
468 printf("NULL pointer returned from rte_malloc\n");
471 /* calc an alignment we don't already have */
472 while(RTE_PTR_ALIGN(ptr7, new_align) == ptr7)
474 char *ptr8 = rte_realloc(ptr7, size7, new_align);
476 printf("NULL pointer returned from rte_realloc\n");
480 if (RTE_PTR_ALIGN(ptr8, new_align) != ptr8){
481 printf("Failure to re-align data\n");
487 /* test behaviour when there is a free block after current one,
488 * but its not big enough
490 unsigned size9 = 1024, size10 = 1024;
491 unsigned size11 = size9 + size10 + 256;
492 char *ptr9 = rte_malloc(NULL, size9, RTE_CACHE_LINE_SIZE);
494 printf("NULL pointer returned from rte_malloc\n");
497 char *ptr10 = rte_malloc(NULL, size10, RTE_CACHE_LINE_SIZE);
499 printf("NULL pointer returned from rte_malloc\n");
503 char *ptr11 = rte_realloc(ptr10, size11, RTE_CACHE_LINE_SIZE);
505 printf("NULL pointer returned from rte_realloc\n");
510 printf("Error, unexpected that realloc has not created new buffer\n");
516 /* check we don't crash if we pass null to realloc
517 * We should get a malloc of the size requested*/
518 const size_t size12 = 1024;
520 char *ptr12 = rte_realloc(NULL, size12, RTE_CACHE_LINE_SIZE);
522 printf("NULL pointer returned from rte_realloc\n");
525 if (rte_malloc_validate(ptr12, &size12_check) < 0 ||
526 size12_check != size12){
535 test_random_alloc_free(void *_ __attribute__((unused)))
538 struct mem_list *next;
544 rte_srand((unsigned)rte_rdtsc());
546 for (i = 0; i < N; i++){
547 unsigned free_mem = 0;
548 size_t allocated_size;
550 const unsigned mem_size = sizeof(struct mem_list) + \
551 rte_rand() % (64 * 1024);
552 const unsigned align = 1 << (rte_rand() % 12); /* up to 4k alignment */
553 struct mem_list *entry = rte_malloc(NULL,
557 if (RTE_PTR_ALIGN(entry, align)!= entry)
559 if (rte_malloc_validate(entry, &allocated_size) == -1
560 || allocated_size < mem_size)
562 memset(entry->data, rte_lcore_id(),
563 mem_size - sizeof(*entry));
564 entry->next = list_head;
565 if (rte_malloc_validate(entry, NULL) == -1)
570 /* switch to freeing the memory with a 20% probability */
571 free_mem = ((rte_rand() % 10) >= 8);
574 struct mem_list *entry = list_head;
575 list_head = list_head->next;
579 printf("Lcore %u allocated/freed %u blocks\n", rte_lcore_id(), count);
583 #define err_return() do { \
584 printf("%s: %d - Error\n", __func__, __LINE__); \
589 test_rte_malloc_validate(void)
591 const size_t request_size = 1024;
592 size_t allocated_size;
593 char *data_ptr = rte_malloc(NULL, request_size, RTE_CACHE_LINE_SIZE);
594 #ifdef RTE_MALLOC_DEBUG
596 char *over_write_vals = NULL;
599 if (data_ptr == NULL) {
600 printf("%s: %d - Allocation error\n", __func__, __LINE__);
604 /* check that a null input returns -1 */
605 if (rte_malloc_validate(NULL, NULL) != -1)
608 /* check that we get ok on a valid pointer */
609 if (rte_malloc_validate(data_ptr, &allocated_size) < 0)
612 /* check that the returned size is ok */
613 if (allocated_size < request_size)
616 #ifdef RTE_MALLOC_DEBUG
618 /****** change the header to be bad */
620 over_write_vals = (char *)((uintptr_t)data_ptr - sizeof(save_buf));
621 /* first save the data as a backup before overwriting it */
622 memcpy(save_buf, over_write_vals, sizeof(save_buf));
623 memset(over_write_vals, 1, sizeof(save_buf));
624 /* then run validate */
625 retval = rte_malloc_validate(data_ptr, NULL);
626 /* finally restore the data again */
627 memcpy(over_write_vals, save_buf, sizeof(save_buf));
628 /* check we previously had an error */
632 /* check all ok again */
633 if (rte_malloc_validate(data_ptr, &allocated_size) < 0)
636 /**** change the trailer to be bad */
637 over_write_vals = (char *)((uintptr_t)data_ptr + allocated_size);
638 /* first save the data as a backup before overwriting it */
639 memcpy(save_buf, over_write_vals, sizeof(save_buf));
640 memset(over_write_vals, 1, sizeof(save_buf));
641 /* then run validate */
642 retval = rte_malloc_validate(data_ptr, NULL);
643 /* finally restore the data again */
644 memcpy(over_write_vals, save_buf, sizeof(save_buf));
648 /* check all ok again */
649 if (rte_malloc_validate(data_ptr, &allocated_size) < 0)
663 test_zero_aligned_alloc(void)
665 char *p1 = rte_malloc(NULL,1024, 0);
668 if (!rte_is_aligned(p1, RTE_CACHE_LINE_SIZE))
675 if (p1) rte_free(p1);
680 test_malloc_bad_params(void)
682 const char *type = NULL;
684 unsigned align = RTE_CACHE_LINE_SIZE;
686 /* rte_malloc expected to return null with inappropriate size */
687 char *bad_ptr = rte_malloc(type, size, align);
691 /* rte_malloc expected to return null with inappropriate alignment */
695 bad_ptr = rte_malloc(type, size, align);
702 /* clean up pointer */
708 /* Check if memory is available on a specific socket */
710 is_mem_on_socket(int32_t socket)
712 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
715 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
716 if (socket == ms[i].socket_id)
723 * Find what socket a memory address is on. Only works for addresses within
724 * memsegs, not heap or stack...
727 addr_to_socket(void * addr)
729 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
732 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
733 if ((ms[i].addr <= addr) &&
735 ((uintptr_t)ms[i].addr + (uintptr_t)ms[i].len)))
736 return ms[i].socket_id;
741 /* Test using rte_[c|m|zm]alloc_socket() on a specific socket */
743 test_alloc_single_socket(int32_t socket)
745 const char *type = NULL;
746 const size_t size = 10;
747 const unsigned align = 0;
749 int32_t desired_socket = (socket == SOCKET_ID_ANY) ?
750 (int32_t)rte_socket_id() : socket;
752 /* Test rte_calloc_socket() */
753 mem = rte_calloc_socket(type, size, sizeof(char), align, socket);
756 if (addr_to_socket(mem) != desired_socket) {
762 /* Test rte_malloc_socket() */
763 mem = rte_malloc_socket(type, size, align, socket);
766 if (addr_to_socket(mem) != desired_socket) {
771 /* Test rte_zmalloc_socket() */
772 mem = rte_zmalloc_socket(type, size, align, socket);
775 if (addr_to_socket(mem) != desired_socket) {
785 test_alloc_socket(void)
787 unsigned socket_count = 0;
790 if (test_alloc_single_socket(SOCKET_ID_ANY) < 0)
793 for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
794 if (is_mem_on_socket(i)) {
796 if (test_alloc_single_socket(i) < 0) {
797 printf("Fail: rte_malloc_socket(..., %u) did not succeed\n",
803 if (test_alloc_single_socket(i) == 0) {
804 printf("Fail: rte_malloc_socket(..., %u) succeeded\n",
811 /* Print warnign if only a single socket, but don't fail the test */
812 if (socket_count < 2) {
813 printf("WARNING: alloc_socket test needs memory on multiple sockets!\n");
825 if (test_str_to_size() < 0){
826 printf("test_str_to_size() failed\n");
829 else printf("test_str_to_size() passed\n");
831 if (test_zero_aligned_alloc() < 0){
832 printf("test_zero_aligned_alloc() failed\n");
835 else printf("test_zero_aligned_alloc() passed\n");
837 if (test_malloc_bad_params() < 0){
838 printf("test_malloc_bad_params() failed\n");
841 else printf("test_malloc_bad_params() passed\n");
843 if (test_realloc() < 0){
844 printf("test_realloc() failed\n");
847 else printf("test_realloc() passed\n");
849 /*----------------------------*/
850 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
851 rte_eal_remote_launch(test_align_overlap_per_lcore, NULL, lcore_id);
854 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
855 if (rte_eal_wait_lcore(lcore_id) < 0)
859 printf("test_align_overlap_per_lcore() failed\n");
862 else printf("test_align_overlap_per_lcore() passed\n");
864 /*----------------------------*/
865 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
866 rte_eal_remote_launch(test_reordered_free_per_lcore, NULL, lcore_id);
869 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
870 if (rte_eal_wait_lcore(lcore_id) < 0)
874 printf("test_reordered_free_per_lcore() failed\n");
877 else printf("test_reordered_free_per_lcore() passed\n");
879 /*----------------------------*/
880 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
881 rte_eal_remote_launch(test_random_alloc_free, NULL, lcore_id);
884 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
885 if (rte_eal_wait_lcore(lcore_id) < 0)
889 printf("test_random_alloc_free() failed\n");
892 else printf("test_random_alloc_free() passed\n");
894 /*----------------------------*/
895 ret = test_rte_malloc_type_limits();
897 printf("test_rte_malloc_type_limits() failed\n");
900 /* TODO: uncomment following line once type limits are valid */
901 /*else printf("test_rte_malloc_type_limits() passed\n");*/
903 /*----------------------------*/
904 ret = test_rte_malloc_validate();
906 printf("test_rte_malloc_validate() failed\n");
909 else printf("test_rte_malloc_validate() passed\n");
911 ret = test_alloc_socket();
913 printf("test_alloc_socket() failed\n");
916 else printf("test_alloc_socket() passed\n");
918 ret = test_multi_alloc_statistics();
920 printf("test_multi_alloc_statistics() failed\n");
924 printf("test_multi_alloc_statistics() passed\n");
929 REGISTER_TEST_COMMAND(malloc_autotest, test_malloc);