Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill the sbrk-based morecore: we'll never use less than two heaps when using mmalloc
[simgrid.git] / src / xbt / mmalloc / mm_legacy.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 /* Redefine the classical malloc/free/realloc functions so that they fit well in the mmalloc framework */
8
9 #include "mmprivate.h"
10 #include "gras_config.h"
11 #include <math.h>
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_mm_legacy, xbt,
14                                 "Logging specific to mm_legacy in mmalloc");
15
16 /* The mmalloc() package can use a single implicit malloc descriptor
17    for mmalloc/mrealloc/mfree operations which do not supply an explicit
18    descriptor.  This allows mmalloc() to provide
19    backwards compatibility with the non-mmap'd version. */
20 struct mdesc *__mmalloc_default_mdp;
21
22
23 static void *__mmalloc_current_heap = NULL;     /* The heap we are currently using. */
24
25 #include "xbt_modinter.h"
26
27 void *mmalloc_get_current_heap(void)
28 {
29   return __mmalloc_current_heap;
30 }
31
32 void mmalloc_set_current_heap(void *new_heap)
33 {
34   __mmalloc_current_heap = new_heap;
35 }
36
37 #ifdef MMALLOC_WANT_OVERIDE_LEGACY
38 void *malloc(size_t n)
39 {
40   void *mdp = __mmalloc_current_heap;
41 #ifdef HAVE_MMAP
42   if (!mdp)
43     mmalloc_preinit();
44 #endif
45   LOCK(mdp);
46   void *ret = mmalloc(mdp, n);
47   UNLOCK(mdp);
48
49   return ret;
50 }
51
52 void *calloc(size_t nmemb, size_t size)
53 {
54   size_t total_size = nmemb * size;
55   void *mdp = __mmalloc_current_heap;
56 #ifdef HAVE_MMAP
57   if (!mdp)
58     mmalloc_preinit();
59 #endif
60   LOCK(mdp);
61   void *ret = mmalloc(mdp, total_size);
62   UNLOCK(mdp);
63
64   /* Fill the allocated memory with zeroes to mimic calloc behaviour */
65   memset(ret, '\0', total_size);
66
67   return ret;
68 }
69
70 void *realloc(void *p, size_t s)
71 {
72   void *ret = NULL;
73   void *mdp = __mmalloc_current_heap;
74 #ifdef HAVE_MMAP
75   if (!mdp)
76     mmalloc_preinit();
77 #endif
78   LOCK(mdp);
79   if (s) {
80     if (p)
81       ret = mrealloc(mdp, p, s);
82     else
83       ret = mmalloc(mdp, s);
84   } else {
85     mfree(mdp, p);
86   }
87   UNLOCK(mdp);
88
89   return ret;
90 }
91
92 void free(void *p)
93 {
94   void *mdp = __mmalloc_current_heap;
95 #ifdef HAVE_GTNETS
96   if(!mdp) return;
97 #endif
98   LOCK(mdp);
99   mfree(mdp, p);
100   UNLOCK(mdp);
101 }
102 #endif
103
104 /* Make sure it works with md==NULL */
105
106 /* Safety gap from the heap's break address.
107  * Try to increase this first if you experience strange errors under
108  * valgrind. */
109 #define HEAP_OFFSET   (128UL<<20)
110
111 void *mmalloc_get_default_md(void)
112 {
113   xbt_assert(__mmalloc_default_mdp);
114   return __mmalloc_default_mdp;
115 }
116
117 static void mmalloc_fork_prepare(void)
118 {
119   struct mdesc* mdp = NULL;
120   if ((mdp =__mmalloc_default_mdp)){
121     while(mdp){
122       LOCK(mdp);
123       if(mdp->fd >= 0){
124         mdp->refcount++;
125       }
126       mdp = mdp->next_mdesc;
127     }
128   }
129 }
130
131 static void mmalloc_fork_parent(void)
132 {
133   struct mdesc* mdp = NULL;
134   if ((mdp =__mmalloc_default_mdp)){
135     while(mdp){
136       if(mdp->fd < 0)
137         UNLOCK(mdp);
138       mdp = mdp->next_mdesc;
139     }
140   }
141 }
142
143 static void mmalloc_fork_child(void)
144 {
145   struct mdesc* mdp = NULL;
146   if ((mdp =__mmalloc_default_mdp)){
147     while(mdp){
148       UNLOCK(mdp);
149       mdp = mdp->next_mdesc;
150     }
151   }
152 }
153
154 /* Initialize the default malloc descriptor. */
155 void mmalloc_preinit(void)
156 {
157   int res;
158   if (!__mmalloc_default_mdp) {
159     unsigned long mask = ~((unsigned long)getpagesize() - 1);
160     void *addr = (void*)(((unsigned long)sbrk(0) + HEAP_OFFSET) & mask);
161     __mmalloc_default_mdp = mmalloc_attach(-1, addr);
162     /* Fixme? only the default mdp in protected against forks */
163     res = xbt_os_thread_atfork(mmalloc_fork_prepare,
164                                mmalloc_fork_parent, mmalloc_fork_child);
165     if (res != 0)
166       THROWF(system_error,0,"xbt_os_thread_atfork() failed: return value %d",res);
167   }
168   xbt_assert(__mmalloc_default_mdp != NULL);
169 }
170
171 void mmalloc_postexit(void)
172 {
173   /* Do not detach the default mdp or ldl won't be able to free the memory it allocated since we're in memory */
174   //  mmalloc_detach(__mmalloc_default_mdp);
175   mmalloc_pre_detach(__mmalloc_default_mdp);
176 }
177
178 int mmalloc_compare_heap(void *h1, void *h2, void *std_heap_addr){
179
180   if(h1 == NULL && h2 == NULL){
181     XBT_DEBUG("Malloc descriptors null");
182     return 0;
183   }
184
185   /* Heapstats */
186
187   int errors = 0;
188
189   struct mstats ms1 = mmstats(h1);
190   struct mstats ms2 = mmstats(h2);
191
192   if(ms1.chunks_used !=  ms2.chunks_used){
193     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
194       XBT_DEBUG("Different chunks allocated by the user : %zu - %zu", ms1.chunks_used, ms2.chunks_used);
195       errors++;
196     }else{
197       return 1;
198     }
199   }
200
201   if(ms1.bytes_used !=  ms2.bytes_used){
202     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
203       XBT_DEBUG("Different byte total of user-allocated chunks : %zu - %zu", ms1.bytes_used, ms2.bytes_used);
204       errors++;
205     }else{
206       return 1;
207     }
208   }
209
210   if(ms1.bytes_free !=  ms2.bytes_free){
211     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
212       XBT_DEBUG("Different byte total of chunks in the free list : %zu - %zu", ms1.bytes_free, ms2.bytes_free);
213       errors++;
214     }else{
215       return 1;
216     }
217   }
218
219   if(ms1.chunks_free !=  ms2.chunks_free){
220     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
221       XBT_DEBUG("Different chunks in the free list : %zu - %zu", ms1.chunks_free, ms2.chunks_free);
222       errors++;
223     }else{
224       return 1;
225     }
226   }
227
228   struct mdesc *mdp1, *mdp2;
229   mdp1 = MD_TO_MDP(h1);
230   mdp2 = MD_TO_MDP(h2);
231
232   int res = mmalloc_compare_mdesc(mdp1, mdp2, std_heap_addr);
233
234   return ((errors + res ) > 0);
235
236 }
237
238 int mmalloc_compare_mdesc(struct mdesc *mdp1, struct mdesc *mdp2, void *std_heap_addr){
239
240   int errors = 0;
241
242   if(mdp1->headersize != mdp2->headersize){
243     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
244       XBT_DEBUG("Different size of the file header for the mapped files");
245       errors++;
246     }else{
247       return 1;
248     }
249   }
250
251   if(mdp1->refcount != mdp2->refcount){
252     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
253       XBT_DEBUG("Different number of processes that attached the heap");
254       errors++;
255     }else{
256       return 1;
257     }
258   }
259  
260   if(strcmp(mdp1->magic, mdp2->magic) != 0){
261     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
262       XBT_DEBUG("Different magic number");
263       errors++;
264     }else{
265       return 1;
266     }
267   }
268
269   if(mdp1->flags != mdp2->flags){
270     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
271       XBT_DEBUG("Different flags");
272       errors++;
273     }else{
274       return 1;
275     }
276   }
277
278   if(mdp1->heapsize != mdp2->heapsize){
279     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
280       XBT_DEBUG("Different number of info entries");
281       errors++;
282     }else{
283       return 1;
284     }
285   }
286
287   //XBT_DEBUG("Heap size : %zu", mdp1->heapsize);
288
289   if(mdp1->heapbase != mdp2->heapbase){
290     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
291       XBT_DEBUG("Different first block of the heap");
292       errors++;
293     }else{
294       return 1;
295     }
296   }
297
298
299   if(mdp1->heapindex != mdp2->heapindex){
300     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
301       XBT_DEBUG("Different index for the heap table : %zu - %zu", mdp1->heapindex, mdp2->heapindex);
302       errors++;
303     }else{
304       return 1;
305     }
306   }
307
308   //XBT_DEBUG("Heap index : %zu", mdp1->heapindex);
309
310   if(mdp1->base != mdp2->base){
311     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
312       XBT_DEBUG("Different base address of the memory region");
313       errors++;
314     }else{
315       return 1;
316     }
317   }
318
319   if(mdp1->breakval != mdp2->breakval){
320     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
321       XBT_DEBUG("Different current location in the memory region");
322       errors++;
323     }else{
324       return 1;
325     }
326   }
327
328   if(mdp1->top != mdp2->top){
329     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
330       XBT_DEBUG("Different end of the current location in the memory region");
331       errors++;
332     }else{
333       return 1;
334     }
335   }
336   
337   if(mdp1->heaplimit != mdp2->heaplimit){
338     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
339       XBT_DEBUG("Different limit of valid info table indices");
340       errors++;
341     }else{
342       return 1;
343     }
344   }
345
346   if(mdp1->fd != mdp2->fd){
347     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
348       XBT_DEBUG("Different file descriptor for the file to which this malloc heap is mapped");
349       errors++;
350     }else{
351       return 1;
352     }
353   }
354
355   if(mdp1->saved_errno != mdp2->saved_errno){
356     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
357       XBT_DEBUG("Different errno");
358       errors++;
359     }else{
360       return 1;
361     }
362   }
363
364   if(mdp1->version != mdp2->version){
365     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
366       XBT_DEBUG("Different version of the mmalloc package");
367       errors++;
368     }else{
369       return 1;
370     }
371   }
372
373  
374   size_t block_free1, block_free2 , next_block_free, first_block_free, block_free ;
375   size_t i, j;
376   void *addr_block1, *addr_block2;
377   size_t frag_size;
378  
379
380   /* Search index of the first free block */
381
382   block_free1 = mdp1->heapindex; 
383   block_free2 = mdp2->heapindex;
384
385   while(mdp1->heapinfo[block_free1].free.prev != 0){
386     block_free1 = mdp1->heapinfo[block_free1].free.prev;
387   }
388
389   while(mdp2->heapinfo[block_free2].free.prev != 0){
390     block_free2 = mdp1->heapinfo[block_free2].free.prev;
391   }
392
393   if(block_free1 !=  block_free2){
394     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
395       XBT_DEBUG("Different first free block");
396       errors++;
397     }else{
398       return 1;
399     }
400   }
401
402   first_block_free = block_free1;
403
404   if(mdp1->heapinfo[first_block_free].free.size != mdp2->heapinfo[first_block_free].free.size){ 
405     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
406       XBT_DEBUG("Different size (in blocks) of the first free cluster");
407       errors++;
408     }else{
409       return 1;
410     }
411   }
412
413   /* Check busy blocks (circular checking)*/
414
415   i = first_block_free + mdp1->heapinfo[first_block_free].free.size;
416
417   if(mdp1->heapinfo[first_block_free].free.next != mdp2->heapinfo[first_block_free].free.next){
418     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
419       XBT_DEBUG("Different next block free");
420       errors++;
421     }else{
422       return 1;
423     }
424   }
425   
426   block_free = first_block_free;
427   next_block_free = mdp1->heapinfo[first_block_free].free.next;
428
429   if(next_block_free == 0)
430     next_block_free = mdp1->heaplimit;
431
432   while(i != first_block_free){
433
434     while(i<next_block_free){
435
436       if(mdp1->heapinfo[i].busy.type != mdp2->heapinfo[i].busy.type){
437         if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
438           XBT_DEBUG("Different type of busy block");
439           errors++;
440         }else{
441           return 1;
442         }
443       }else{
444
445         addr_block1 = (char *)mdp1 + sizeof(struct mdesc) + ((i-1) * BLOCKSIZE); 
446         addr_block2 = (char *)mdp2 + sizeof(struct mdesc) + ((i-1) * BLOCKSIZE); 
447         
448         switch(mdp1->heapinfo[i].busy.type){
449         case 0 :
450           if(mdp1->heapinfo[i].busy.info.block.size != mdp2->heapinfo[i].busy.info.block.size){
451             if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
452               XBT_DEBUG("Different size of a large cluster");
453               errors++;
454             }else{
455               return 1;
456             }
457           }else{
458             if(memcmp(addr_block1, addr_block2, (mdp1->heapinfo[i].busy.info.block.size * BLOCKSIZE)) != 0){
459               if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){           
460                 XBT_DEBUG("Different data in block %zu (size = %zu) (addr_block1 = %p (current = %p) - addr_block2 = %p)", i, mdp1->heapinfo[i].busy.info.block.size, addr_block1, (char *)std_heap_addr + sizeof(struct mdesc) + ((i-1) * BLOCKSIZE), addr_block2);
461                 errors++;
462               }else{
463                 return 1;
464               }
465             } 
466           }
467           i = i+mdp1->heapinfo[i].busy.info.block.size;
468
469           break;
470         default :         
471           if(mdp1->heapinfo[i].busy.info.frag.nfree != mdp2->heapinfo[i].busy.info.frag.nfree){
472             if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
473               XBT_DEBUG("Different free fragments in the fragmented block %zu", i);
474               errors++;
475             }else{
476               return 1;
477             }
478           }else{
479             if(mdp1->heapinfo[i].busy.info.frag.first != mdp2->heapinfo[i].busy.info.frag.first){
480               if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
481                 XBT_DEBUG("Different first free fragments in the block %zu", i);
482                 errors++;
483               }else{
484                 return 1;
485               } 
486             }else{
487               frag_size = pow(2,mdp1->heapinfo[i].busy.type);
488               for(j=0 ; j< (BLOCKSIZE/frag_size); j++){
489                 if(memcmp((char *)addr_block1 + (j * frag_size), (char *)addr_block2 + (j * frag_size), frag_size) != 0){
490                   if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
491                     XBT_DEBUG("Different data in fragment %zu (addr_frag1 = %p - addr_frag2 = %p) of block %zu", j + 1, (char *)addr_block1 + (j * frag_size), (char *)addr_block2 + (j * frag_size),  i);
492                     errors++;
493                   }else{
494                     return 1;
495                   }
496                 } 
497               }
498             }
499           }
500
501           i++;
502
503           break;
504         }
505
506       }
507     }
508
509     if( i != first_block_free){
510
511       if(mdp1->heapinfo[block_free].free.next != mdp2->heapinfo[block_free].free.next){
512         if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
513           XBT_DEBUG("Different next block free");
514           errors++;
515         }else{
516           return 1;
517         }
518       }
519      
520       block_free = mdp1->heapinfo[block_free].free.next;
521       next_block_free = mdp1->heapinfo[block_free].free.next;
522
523       i = block_free + mdp1->heapinfo[block_free].free.size;
524
525       if((next_block_free == 0) && (i != mdp1->heaplimit)){
526
527         while(i < mdp1->heaplimit){
528
529           if(mdp1->heapinfo[i].busy.type != mdp2->heapinfo[i].busy.type){
530             if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
531               XBT_DEBUG("Different type of busy block");
532               errors++;
533             }else{
534               return 1;
535             }
536           }else{
537
538             addr_block1 = (char *)mdp1 + sizeof(struct mdesc) + ((i-1) * BLOCKSIZE); 
539             addr_block2 = (char *)mdp2 + sizeof(struct mdesc) + ((i-1) * BLOCKSIZE); 
540         
541             switch(mdp1->heapinfo[i].busy.type){
542             case 0 :
543               if(mdp1->heapinfo[i].busy.info.block.size != mdp2->heapinfo[i].busy.info.block.size){
544                 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
545                   XBT_DEBUG("Different size of a large cluster");
546                   errors++;
547                 }else{
548                   return 1;
549                 }
550               }else{
551                 if(memcmp(addr_block1, addr_block2, (mdp1->heapinfo[i].busy.info.block.size * BLOCKSIZE)) != 0){
552                   if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){       
553                     XBT_DEBUG("Different data in block %zu (addr_block1 = %p (current = %p) - addr_block2 = %p)", i, addr_block1, (char *)std_heap_addr + sizeof(struct mdesc) + ((i-1) * BLOCKSIZE), addr_block2);
554                     errors++;
555                   }else{
556                     return 1;
557                   }
558                 } 
559               }
560             
561               i = i+mdp1->heapinfo[i].busy.info.block.size;
562
563               break;
564             default :     
565               if(mdp1->heapinfo[i].busy.info.frag.nfree != mdp2->heapinfo[i].busy.info.frag.nfree){
566                 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
567                   XBT_DEBUG("Different free fragments in the fragmented block %zu", i);
568                   errors++;
569                 }else{
570                   return 1;
571                 }
572               }else{
573                 if(mdp1->heapinfo[i].busy.info.frag.first != mdp2->heapinfo[i].busy.info.frag.first){
574                   if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
575                     XBT_DEBUG("Different first free fragments in the block %zu", i);
576                     errors++;
577                   }else{
578                     return 1;
579                   } 
580                 }else{
581                   frag_size = pow(2,mdp1->heapinfo[i].busy.type);
582                   for(j=0 ; j< (BLOCKSIZE/frag_size); j++){
583                     if(memcmp((char *)addr_block1 + (j * frag_size), (char *)addr_block2 + (j * frag_size), frag_size) != 0){
584                       if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
585                         XBT_DEBUG("Different data in fragment %zu (addr_frag1 = %p - addr_frag2 = %p) of block %zu", j + 1, (char *)addr_block1 + (j * frag_size), (char *)addr_block2 + (j * frag_size),  i);
586                         errors++;
587                       }else{
588                         return 1;
589                       }
590                     } 
591                   }
592                 }
593               }
594
595               i++;
596
597               break;
598             }
599           }
600         }
601
602       }
603
604     }
605   }
606   
607   return (errors>0);    
608 }
609
610  
611 void mmalloc_display_info_heap(void *h){
612
613 }  
614   
615