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