New upstream version 18.02
[deb_dpdk.git] / test / test / test_malloc.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <stdint.h>
7 #include <string.h>
8 #include <stdarg.h>
9 #include <errno.h>
10 #include <stdlib.h>
11 #include <sys/queue.h>
12
13 #include <rte_common.h>
14 #include <rte_memory.h>
15 #include <rte_per_lcore.h>
16 #include <rte_launch.h>
17 #include <rte_eal.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>
23
24 #include "test.h"
25
26 #define N 10000
27
28 /*
29  * Malloc
30  * ======
31  *
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.
35  */
36
37 /* Test if memory overlaps: return 1 if true, or 0 if false. */
38 static int
39 is_memory_overlap(void *p1, size_t len1, void *p2, size_t len2)
40 {
41         unsigned long ptr1 = (unsigned long)p1;
42         unsigned long ptr2 = (unsigned long)p2;
43
44         if (ptr2 >= ptr1 && (ptr2 - ptr1) < len1)
45                 return 1;
46         else if (ptr2 < ptr1 && (ptr1 - ptr2) < len2)
47                 return 1;
48         return 0;
49 }
50
51 static int
52 is_aligned(void *p, int align)
53 {
54         unsigned long addr = (unsigned long)p;
55         unsigned mask = align - 1;
56
57         if (addr & mask)
58                 return 0;
59         return 1;
60 }
61
62 static int
63 test_align_overlap_per_lcore(__attribute__((unused)) void *arg)
64 {
65         const unsigned align1 = 8,
66                         align2 = 64,
67                         align3 = 2048;
68         unsigned i,j;
69         void *p1 = NULL, *p2 = NULL, *p3 = NULL;
70         int ret = 0;
71
72         for (i = 0; i < N; i++) {
73                 p1 = rte_zmalloc("dummy", 1000, align1);
74                 if (!p1){
75                         printf("rte_zmalloc returned NULL (i=%u)\n", i);
76                         ret = -1;
77                         break;
78                 }
79                 for(j = 0; j < 1000 ; j++) {
80                         if( *(char *)p1 != 0) {
81                                 printf("rte_zmalloc didn't zero the allocated memory\n");
82                                 ret = -1;
83                         }
84                 }
85                 p2 = rte_malloc("dummy", 1000, align2);
86                 if (!p2){
87                         printf("rte_malloc returned NULL (i=%u)\n", i);
88                         ret = -1;
89                         rte_free(p1);
90                         break;
91                 }
92                 p3 = rte_malloc("dummy", 1000, align3);
93                 if (!p3){
94                         printf("rte_malloc returned NULL (i=%u)\n", i);
95                         ret = -1;
96                         rte_free(p1);
97                         rte_free(p2);
98                         break;
99                 }
100                 if (is_memory_overlap(p1, 1000, p2, 1000)) {
101                         printf("p1 and p2 overlaps\n");
102                         ret = -1;
103                 }
104                 if (is_memory_overlap(p2, 1000, p3, 1000)) {
105                         printf("p2 and p3 overlaps\n");
106                         ret = -1;
107                 }
108                 if (is_memory_overlap(p1, 1000, p3, 1000)) {
109                         printf("p1 and p3 overlaps\n");
110                         ret = -1;
111                 }
112                 if (!is_aligned(p1, align1)) {
113                         printf("p1 is not aligned\n");
114                         ret = -1;
115                 }
116                 if (!is_aligned(p2, align2)) {
117                         printf("p2 is not aligned\n");
118                         ret = -1;
119                 }
120                 if (!is_aligned(p3, align3)) {
121                         printf("p3 is not aligned\n");
122                         ret = -1;
123                 }
124                 rte_free(p1);
125                 rte_free(p2);
126                 rte_free(p3);
127         }
128         rte_malloc_dump_stats(stdout, "dummy");
129
130         return ret;
131 }
132
133 static int
134 test_reordered_free_per_lcore(__attribute__((unused)) void *arg)
135 {
136         const unsigned align1 = 8,
137                         align2 = 64,
138                         align3 = 2048;
139         unsigned i,j;
140         void *p1, *p2, *p3;
141         int ret = 0;
142
143         for (i = 0; i < 30; i++) {
144                 p1 = rte_zmalloc("dummy", 1000, align1);
145                 if (!p1){
146                         printf("rte_zmalloc returned NULL (i=%u)\n", i);
147                         ret = -1;
148                         break;
149                 }
150                 for(j = 0; j < 1000 ; j++) {
151                         if( *(char *)p1 != 0) {
152                                 printf("rte_zmalloc didn't zero the allocated memory\n");
153                                 ret = -1;
154                         }
155                 }
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);
160                 if (!p2 || !p3){
161                         printf("rte_malloc returned NULL (i=%u)\n", i);
162                         ret = -1;
163                         break;
164                 }
165                 if (is_memory_overlap(p1, 1000, p2, 1000)) {
166                         printf("p1 and p2 overlaps\n");
167                         ret = -1;
168                 }
169                 if (is_memory_overlap(p2, 1000, p3, 1000)) {
170                         printf("p2 and p3 overlaps\n");
171                         ret = -1;
172                 }
173                 if (is_memory_overlap(p1, 1000, p3, 1000)) {
174                         printf("p1 and p3 overlaps\n");
175                         ret = -1;
176                 }
177                 if (!is_aligned(p1, align1)) {
178                         printf("p1 is not aligned\n");
179                         ret = -1;
180                 }
181                 if (!is_aligned(p2, align2)) {
182                         printf("p2 is not aligned\n");
183                         ret = -1;
184                 }
185                 if (!is_aligned(p3, align3)) {
186                         printf("p3 is not aligned\n");
187                         ret = -1;
188                 }
189                 /* try freeing in every possible order */
190                 switch (i%6){
191                 case 0:
192                         rte_free(p1);
193                         rte_free(p2);
194                         rte_free(p3);
195                         break;
196                 case 1:
197                         rte_free(p1);
198                         rte_free(p3);
199                         rte_free(p2);
200                         break;
201                 case 2:
202                         rte_free(p2);
203                         rte_free(p1);
204                         rte_free(p3);
205                         break;
206                 case 3:
207                         rte_free(p2);
208                         rte_free(p3);
209                         rte_free(p1);
210                         break;
211                 case 4:
212                         rte_free(p3);
213                         rte_free(p1);
214                         rte_free(p2);
215                         break;
216                 case 5:
217                         rte_free(p3);
218                         rte_free(p2);
219                         rte_free(p1);
220                         break;
221                 }
222         }
223         rte_malloc_dump_stats(stdout, "dummy");
224
225         return ret;
226 }
227
228 /* test function inside the malloc lib*/
229 static int
230 test_str_to_size(void)
231 {
232         struct {
233                 const char *str;
234                 uint64_t value;
235         } test_values[] =
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},
240                         {"8K", 8 * 1024},
241                         {"15k", 15 * 1024},
242                         {"0200", 0200},
243                         {"0x103", 0x103},
244                         {"432", 432},
245                         {"-1", 0}, /* negative values return 0 */
246                         {"  -2", 0},
247                         {"  -3MB", 0},
248                         {"18446744073709551616", 0} /* ULLONG_MAX + 1 == out of range*/
249         };
250         unsigned i;
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)
253                         return -1;
254         return 0;
255 }
256
257 static int
258 test_multi_alloc_statistics(void)
259 {
260         int socket = 0;
261         struct rte_malloc_socket_stats pre_stats, post_stats ,first_stats, second_stats;
262         size_t size = 2048;
263         int align = 1024;
264 #ifndef RTE_MALLOC_DEBUG
265         int trailer_size = 0;
266 #else
267         int trailer_size = RTE_CACHE_LINE_SIZE;
268 #endif
269         int overhead = RTE_CACHE_LINE_SIZE + trailer_size;
270
271         rte_malloc_get_socket_stats(socket, &pre_stats);
272
273         void *p1 = rte_malloc_socket("stats", size , align, socket);
274         if (!p1)
275                 return -1;
276         rte_free(p1);
277         rte_malloc_dump_stats(stdout, "stats");
278
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");
288                 return -1;
289         }
290         /* Check two consecutive allocations */
291         size = 1024;
292         align = 0;
293         rte_malloc_get_socket_stats(socket,&pre_stats);
294         void *p2 = rte_malloc_socket("add", size ,align, socket);
295         if (!p2)
296                 return -1;
297         rte_malloc_get_socket_stats(socket,&first_stats);
298
299         void *p3 = rte_malloc_socket("add2", size,align, socket);
300         if (!p3)
301                 return -1;
302
303         rte_malloc_get_socket_stats(socket,&second_stats);
304
305         rte_free(p2);
306         rte_free(p3);
307
308         /* After freeing both allocations check stats return to original */
309         rte_malloc_get_socket_stats(socket, &post_stats);
310
311         if(second_stats.heap_totalsz_bytes != first_stats.heap_totalsz_bytes) {
312                 printf("Incorrect heap statistics: Total size \n");
313                 return -1;
314         }
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");
319                 return -1;
320         }
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");
324                 return -1;
325         }
326
327         if (second_stats.free_count != first_stats.free_count){
328                 printf("Incorrect heap statistics: Free count \n");
329                 return -1;
330         }
331
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");
335                 return -1;
336         }
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");
340                 return -1;
341         }
342
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");
349                 return -1;
350         }
351         return 0;
352 }
353
354 static int
355 test_rte_malloc_type_limits(void)
356 {
357         /* The type-limits functionality is not yet implemented,
358          * so always return 0 no matter what the retval.
359          */
360         const char *typename = "limit_test";
361         rte_malloc_set_limit(typename, 64 * 1024);
362         rte_malloc_dump_stats(stdout, typename);
363         return 0;
364 }
365
366 static int
367 test_realloc(void)
368 {
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;
374
375         /* test data is the same even if element is moved*/
376         char *ptr1 = rte_zmalloc(NULL, size1, RTE_CACHE_LINE_SIZE);
377         if (!ptr1){
378                 printf("NULL pointer returned from rte_zmalloc\n");
379                 return -1;
380         }
381         snprintf(ptr1, size1, "%s" ,hello_str);
382         char *ptr2 = rte_realloc(ptr1, size2, RTE_CACHE_LINE_SIZE);
383         if (!ptr2){
384                 rte_free(ptr1);
385                 printf("NULL pointer returned from rte_realloc\n");
386                 return -1;
387         }
388         if (ptr1 == ptr2){
389                 printf("unexpected - ptr1 == ptr2\n");
390         }
391         if (strcmp(ptr2, hello_str) != 0){
392                 printf("Error - lost data from pointed area\n");
393                 rte_free(ptr2);
394                 return -1;
395         }
396         unsigned i;
397         for (i = strnlen(hello_str, sizeof(hello_str)); i < size1; i++)
398                 if (ptr2[i] != 0){
399                         printf("Bad data in realloc\n");
400                         rte_free(ptr2);
401                         return -1;
402                 }
403         /* now allocate third element, free the second
404          * and resize third. It should not move. (ptr1 is now invalid)
405          */
406         char *ptr3 = rte_zmalloc(NULL, size3, RTE_CACHE_LINE_SIZE);
407         if (!ptr3){
408                 printf("NULL pointer returned from rte_zmalloc\n");
409                 rte_free(ptr2);
410                 return -1;
411         }
412         for (i = 0; i < size3; i++)
413                 if (ptr3[i] != 0){
414                         printf("Bad data in zmalloc\n");
415                         rte_free(ptr3);
416                         rte_free(ptr2);
417                         return -1;
418                 }
419         rte_free(ptr2);
420         /* first resize to half the size of the freed block */
421         char *ptr4 = rte_realloc(ptr3, size4, RTE_CACHE_LINE_SIZE);
422         if (!ptr4){
423                 printf("NULL pointer returned from rte_realloc\n");
424                 rte_free(ptr3);
425                 return -1;
426         }
427         if (ptr3 != ptr4){
428                 printf("Unexpected - ptr4 != ptr3\n");
429                 rte_free(ptr4);
430                 return -1;
431         }
432         /* now resize again to the full size of the freed block */
433         ptr4 = rte_realloc(ptr3, size3 + size2 + size1, RTE_CACHE_LINE_SIZE);
434         if (ptr3 != ptr4){
435                 printf("Unexpected - ptr4 != ptr3 on second resize\n");
436                 rte_free(ptr4);
437                 return -1;
438         }
439         rte_free(ptr4);
440
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);
445         if (!ptr5){
446                 printf("NULL pointer returned from rte_malloc\n");
447                 return -1;
448         }
449         char *ptr6 = rte_realloc(ptr5, size6, RTE_CACHE_LINE_SIZE);
450         if (!ptr6){
451                 printf("NULL pointer returned from rte_realloc\n");
452                 rte_free(ptr5);
453                 return -1;
454         }
455         if (ptr5 != ptr6){
456                 printf("Error, resizing to a smaller size moved data\n");
457                 rte_free(ptr6);
458                 return -1;
459         }
460         rte_free(ptr6);
461
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);
467         if (!ptr7){
468                 printf("NULL pointer returned from rte_malloc\n");
469                 return -1;
470         }
471         /* calc an alignment we don't already have */
472         while(RTE_PTR_ALIGN(ptr7, new_align) == ptr7)
473                 new_align *= 2;
474         char *ptr8 = rte_realloc(ptr7, size7, new_align);
475         if (!ptr8){
476                 printf("NULL pointer returned from rte_realloc\n");
477                 rte_free(ptr7);
478                 return -1;
479         }
480         if (RTE_PTR_ALIGN(ptr8, new_align) != ptr8){
481                 printf("Failure to re-align data\n");
482                 rte_free(ptr8);
483                 return -1;
484         }
485         rte_free(ptr8);
486
487         /* test behaviour when there is a free block after current one,
488          * but its not big enough
489          */
490         unsigned size9 = 1024, size10 = 1024;
491         unsigned size11 = size9 + size10 + 256;
492         char *ptr9 = rte_malloc(NULL, size9, RTE_CACHE_LINE_SIZE);
493         if (!ptr9){
494                 printf("NULL pointer returned from rte_malloc\n");
495                 return -1;
496         }
497         char *ptr10 = rte_malloc(NULL, size10, RTE_CACHE_LINE_SIZE);
498         if (!ptr10){
499                 printf("NULL pointer returned from rte_malloc\n");
500                 return -1;
501         }
502         rte_free(ptr9);
503         char *ptr11 = rte_realloc(ptr10, size11, RTE_CACHE_LINE_SIZE);
504         if (!ptr11){
505                 printf("NULL pointer returned from rte_realloc\n");
506                 rte_free(ptr10);
507                 return -1;
508         }
509         if (ptr11 == ptr10){
510                 printf("Error, unexpected that realloc has not created new buffer\n");
511                 rte_free(ptr11);
512                 return -1;
513         }
514         rte_free(ptr11);
515
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;
519         size_t size12_check;
520         char *ptr12 = rte_realloc(NULL, size12, RTE_CACHE_LINE_SIZE);
521         if (!ptr12){
522                 printf("NULL pointer returned from rte_realloc\n");
523                 return -1;
524         }
525         if (rte_malloc_validate(ptr12, &size12_check) < 0 ||
526                         size12_check != size12){
527                 rte_free(ptr12);
528                 return -1;
529         }
530         rte_free(ptr12);
531         return 0;
532 }
533
534 static int
535 test_random_alloc_free(void *_ __attribute__((unused)))
536 {
537         struct mem_list {
538                 struct mem_list *next;
539                 char data[0];
540         } *list_head = NULL;
541         unsigned i;
542         unsigned count = 0;
543
544         rte_srand((unsigned)rte_rdtsc());
545
546         for (i = 0; i < N; i++){
547                 unsigned free_mem = 0;
548                 size_t allocated_size;
549                 while (!free_mem){
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,
554                                         mem_size, align);
555                         if (entry == NULL)
556                                 return -1;
557                         if (RTE_PTR_ALIGN(entry, align)!= entry)
558                                 return -1;
559                         if (rte_malloc_validate(entry, &allocated_size) == -1
560                                         || allocated_size < mem_size)
561                                 return -1;
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)
566                                 return -1;
567                         list_head = entry;
568
569                         count++;
570                         /* switch to freeing the memory with a 20% probability */
571                         free_mem = ((rte_rand() % 10) >= 8);
572                 }
573                 while (list_head){
574                         struct mem_list *entry = list_head;
575                         list_head = list_head->next;
576                         rte_free(entry);
577                 }
578         }
579         printf("Lcore %u allocated/freed %u blocks\n", rte_lcore_id(), count);
580         return 0;
581 }
582
583 #define err_return() do { \
584         printf("%s: %d - Error\n", __func__, __LINE__); \
585         goto err_return; \
586 } while (0)
587
588 static int
589 test_rte_malloc_validate(void)
590 {
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
595         int retval;
596         char *over_write_vals = NULL;
597 #endif
598
599         if (data_ptr == NULL) {
600                 printf("%s: %d - Allocation error\n", __func__, __LINE__);
601                 return -1;
602         }
603
604         /* check that a null input returns -1 */
605         if (rte_malloc_validate(NULL, NULL) != -1)
606                 err_return();
607
608         /* check that we get ok on a valid pointer */
609         if (rte_malloc_validate(data_ptr, &allocated_size) < 0)
610                 err_return();
611
612         /* check that the returned size is ok */
613         if (allocated_size < request_size)
614                 err_return();
615
616 #ifdef RTE_MALLOC_DEBUG
617
618         /****** change the header to be bad */
619         char save_buf[64];
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 */
629         if (retval != -1)
630                 err_return();
631
632         /* check all ok again */
633         if (rte_malloc_validate(data_ptr, &allocated_size) < 0)
634                 err_return();
635
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));
645         if (retval != -1)
646                 err_return();
647
648         /* check all ok again */
649         if (rte_malloc_validate(data_ptr, &allocated_size) < 0)
650                 err_return();
651 #endif
652
653         rte_free(data_ptr);
654         return 0;
655
656 err_return:
657         /*clean up */
658         rte_free(data_ptr);
659         return -1;
660 }
661
662 static int
663 test_zero_aligned_alloc(void)
664 {
665         char *p1 = rte_malloc(NULL,1024, 0);
666         if (!p1)
667                 goto err_return;
668         if (!rte_is_aligned(p1, RTE_CACHE_LINE_SIZE))
669                 goto err_return;
670         rte_free(p1);
671         return 0;
672
673 err_return:
674         /*clean up */
675         if (p1) rte_free(p1);
676         return -1;
677 }
678
679 static int
680 test_malloc_bad_params(void)
681 {
682         const char *type = NULL;
683         size_t size = 0;
684         unsigned align = RTE_CACHE_LINE_SIZE;
685
686         /* rte_malloc expected to return null with inappropriate size */
687         char *bad_ptr = rte_malloc(type, size, align);
688         if (bad_ptr != NULL)
689                 goto err_return;
690
691         /* rte_malloc expected to return null with inappropriate alignment */
692         align = 17;
693         size = 1024;
694
695         bad_ptr = rte_malloc(type, size, align);
696         if (bad_ptr != NULL)
697                 goto err_return;
698
699         return 0;
700
701 err_return:
702         /* clean up pointer */
703         if (bad_ptr)
704                 rte_free(bad_ptr);
705         return -1;
706 }
707
708 /* Check if memory is available on a specific socket */
709 static int
710 is_mem_on_socket(int32_t socket)
711 {
712         const struct rte_memseg *ms = rte_eal_get_physmem_layout();
713         unsigned i;
714
715         for (i = 0; i < RTE_MAX_MEMSEG; i++) {
716                 if (socket == ms[i].socket_id)
717                         return 1;
718         }
719         return 0;
720 }
721
722 /*
723  * Find what socket a memory address is on. Only works for addresses within
724  * memsegs, not heap or stack...
725  */
726 static int32_t
727 addr_to_socket(void * addr)
728 {
729         const struct rte_memseg *ms = rte_eal_get_physmem_layout();
730         unsigned i;
731
732         for (i = 0; i < RTE_MAX_MEMSEG; i++) {
733                 if ((ms[i].addr <= addr) &&
734                                 ((uintptr_t)addr <
735                                 ((uintptr_t)ms[i].addr + (uintptr_t)ms[i].len)))
736                         return ms[i].socket_id;
737         }
738         return -1;
739 }
740
741 /* Test using rte_[c|m|zm]alloc_socket() on a specific socket */
742 static int
743 test_alloc_single_socket(int32_t socket)
744 {
745         const char *type = NULL;
746         const size_t size = 10;
747         const unsigned align = 0;
748         char *mem = NULL;
749         int32_t desired_socket = (socket == SOCKET_ID_ANY) ?
750                         (int32_t)rte_socket_id() : socket;
751
752         /* Test rte_calloc_socket() */
753         mem = rte_calloc_socket(type, size, sizeof(char), align, socket);
754         if (mem == NULL)
755                 return -1;
756         if (addr_to_socket(mem) != desired_socket) {
757                 rte_free(mem);
758                 return -1;
759         }
760         rte_free(mem);
761
762         /* Test rte_malloc_socket() */
763         mem = rte_malloc_socket(type, size, align, socket);
764         if (mem == NULL)
765                 return -1;
766         if (addr_to_socket(mem) != desired_socket) {
767                 return -1;
768         }
769         rte_free(mem);
770
771         /* Test rte_zmalloc_socket() */
772         mem = rte_zmalloc_socket(type, size, align, socket);
773         if (mem == NULL)
774                 return -1;
775         if (addr_to_socket(mem) != desired_socket) {
776                 rte_free(mem);
777                 return -1;
778         }
779         rte_free(mem);
780
781         return 0;
782 }
783
784 static int
785 test_alloc_socket(void)
786 {
787         unsigned socket_count = 0;
788         unsigned i;
789
790         if (test_alloc_single_socket(SOCKET_ID_ANY) < 0)
791                 return -1;
792
793         for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
794                 if (is_mem_on_socket(i)) {
795                         socket_count++;
796                         if (test_alloc_single_socket(i) < 0) {
797                                 printf("Fail: rte_malloc_socket(..., %u) did not succeed\n",
798                                                 i);
799                                 return -1;
800                         }
801                 }
802                 else {
803                         if (test_alloc_single_socket(i) == 0) {
804                                 printf("Fail: rte_malloc_socket(..., %u) succeeded\n",
805                                                 i);
806                                 return -1;
807                         }
808                 }
809         }
810
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");
814         }
815
816         return 0;
817 }
818
819 static int
820 test_malloc(void)
821 {
822         unsigned lcore_id;
823         int ret = 0;
824
825         if (test_str_to_size() < 0){
826                 printf("test_str_to_size() failed\n");
827                 return -1;
828         }
829         else printf("test_str_to_size() passed\n");
830
831         if (test_zero_aligned_alloc() < 0){
832                 printf("test_zero_aligned_alloc() failed\n");
833                 return -1;
834         }
835         else printf("test_zero_aligned_alloc() passed\n");
836
837         if (test_malloc_bad_params() < 0){
838                 printf("test_malloc_bad_params() failed\n");
839                 return -1;
840         }
841         else printf("test_malloc_bad_params() passed\n");
842
843         if (test_realloc() < 0){
844                 printf("test_realloc() failed\n");
845                 return -1;
846         }
847         else printf("test_realloc() passed\n");
848
849         /*----------------------------*/
850         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
851                 rte_eal_remote_launch(test_align_overlap_per_lcore, NULL, lcore_id);
852         }
853
854         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
855                 if (rte_eal_wait_lcore(lcore_id) < 0)
856                         ret = -1;
857         }
858         if (ret < 0){
859                 printf("test_align_overlap_per_lcore() failed\n");
860                 return ret;
861         }
862         else printf("test_align_overlap_per_lcore() passed\n");
863
864         /*----------------------------*/
865         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
866                 rte_eal_remote_launch(test_reordered_free_per_lcore, NULL, lcore_id);
867         }
868
869         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
870                 if (rte_eal_wait_lcore(lcore_id) < 0)
871                         ret = -1;
872         }
873         if (ret < 0){
874                 printf("test_reordered_free_per_lcore() failed\n");
875                 return ret;
876         }
877         else printf("test_reordered_free_per_lcore() passed\n");
878
879         /*----------------------------*/
880         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
881                 rte_eal_remote_launch(test_random_alloc_free, NULL, lcore_id);
882         }
883
884         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
885                 if (rte_eal_wait_lcore(lcore_id) < 0)
886                         ret = -1;
887         }
888         if (ret < 0){
889                 printf("test_random_alloc_free() failed\n");
890                 return ret;
891         }
892         else printf("test_random_alloc_free() passed\n");
893
894         /*----------------------------*/
895         ret = test_rte_malloc_type_limits();
896         if (ret < 0){
897                 printf("test_rte_malloc_type_limits() failed\n");
898                 return ret;
899         }
900         /* TODO: uncomment following line once type limits are valid */
901         /*else printf("test_rte_malloc_type_limits() passed\n");*/
902
903         /*----------------------------*/
904         ret = test_rte_malloc_validate();
905         if (ret < 0){
906                 printf("test_rte_malloc_validate() failed\n");
907                 return ret;
908         }
909         else printf("test_rte_malloc_validate() passed\n");
910
911         ret = test_alloc_socket();
912         if (ret < 0){
913                 printf("test_alloc_socket() failed\n");
914                 return ret;
915         }
916         else printf("test_alloc_socket() passed\n");
917
918         ret = test_multi_alloc_statistics();
919         if (ret < 0) {
920                 printf("test_multi_alloc_statistics() failed\n");
921                 return ret;
922         }
923         else
924                 printf("test_multi_alloc_statistics() passed\n");
925
926         return 0;
927 }
928
929 REGISTER_TEST_COMMAND(malloc_autotest, test_malloc);