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