Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
395ec5797504783e8ba4a6586b29a45f08c66313
[simgrid.git] / src / xbt / mmalloc / mm_diff.c
1 /* mm_diff - Memory snapshooting and comparison                             */
2
3 /* Copyright (c) 2008-2012. The SimGrid Team. All rights reserved.          */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "xbt/ex_interface.h" /* internals of backtrace setup */
9 #include "xbt/str.h"
10 #include "mc/mc.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mm_diff, xbt,
13                                 "Logging specific to mm_diff in mmalloc");
14
15 extern char *xbt_binary_name;
16
17 xbt_dynar_t mmalloc_ignore;
18
19 typedef struct s_heap_area_pair{
20   int block1;
21   int fragment1;
22   int block2;
23   int fragment2;
24 }s_heap_area_pair_t, *heap_area_pair_t;
25
26 static void heap_area_pair_free(heap_area_pair_t pair);
27 static void heap_area_pair_free_voidp(void *d);
28 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2);
29 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2);
30
31 static int compare_area(void *area1, void* area2, size_t size, xbt_dynar_t previous);
32 static void match_equals(xbt_dynar_t list);
33
34 static size_t heap_comparison_ignore(void *address);
35
36 void mmalloc_backtrace_block_display(void* heapinfo, int block){
37
38   xbt_ex_t e;
39
40   if (((malloc_info *)heapinfo)[block].busy_block.bt_size == 0) {
41     XBT_DEBUG("No backtrace available for that block, sorry.");
42     return;
43   }
44
45   memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE);
46   e.used = ((malloc_info *)heapinfo)[block].busy_block.bt_size;
47
48   xbt_ex_setup_backtrace(&e);
49   if (e.used == 0) {
50     XBT_DEBUG("(backtrace not set)");
51   } else if (e.bt_strings == NULL) {
52     XBT_DEBUG("(backtrace not ready to be computed. %s)",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
53   } else {
54     int i;
55
56     XBT_DEBUG("Backtrace of where the block %d was malloced (%d frames):", block ,e.used);
57     for (i = 0; i < e.used; i++)       /* no need to display "xbt_backtrace_display" */{
58       XBT_DEBUG("%d ---> %s",i, e.bt_strings[i] + 4);
59     }
60   }
61
62 }
63
64 void mmalloc_backtrace_fragment_display(void* heapinfo, int block, int frag){
65
66   xbt_ex_t e;
67
68   memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_frag.bt[frag]),sizeof(void*)*XBT_BACKTRACE_SIZE);
69   e.used = XBT_BACKTRACE_SIZE;
70
71   xbt_ex_setup_backtrace(&e);
72   if (e.used == 0) {
73     XBT_DEBUG("(backtrace not set)");
74   } else if (e.bt_strings == NULL) {
75     XBT_DEBUG("(backtrace not ready to be computed. %s)",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
76   } else {
77     int i;
78
79     XBT_DEBUG("Backtrace of where the fragment %d in block %d was malloced (%d frames):", frag, block ,e.used);
80     for (i = 0; i < e.used; i++)       /* no need to display "xbt_backtrace_display" */{
81       XBT_DEBUG("%d ---> %s",i, e.bt_strings[i] + 4);
82     }
83   }
84
85 }
86
87 void *s_heap, *heapbase1, *heapbase2;
88 malloc_info *heapinfo1, *heapinfo2;
89 size_t heaplimit, heapsize1, heapsize2;
90
91 int ignore_done;
92
93 int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
94
95   if(heap1 == NULL && heap1 == NULL){
96     XBT_DEBUG("Malloc descriptors null");
97     return 0;
98   }
99
100   if(heap1->heaplimit != heap2->heaplimit){
101     XBT_DEBUG("Different limit of valid info table indices");
102     return 1;
103   }
104
105   /* Heap information */
106   heaplimit = ((struct mdesc *)heap1)->heaplimit;
107
108   s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
109
110   heapbase1 = (char *)heap1 + BLOCKSIZE;
111   heapbase2 = (char *)heap2 + BLOCKSIZE;
112
113   heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
114   heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
115
116   heapsize1 = heap1->heapsize;
117   heapsize2 = heap2->heapsize;
118
119   /* Start comparison */
120   size_t i1, i2, j1, j2, k;
121   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
122   size_t frag_size1, frag_size2;
123
124   xbt_dynar_t previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
125
126   int equal;
127
128   ignore_done = 0;
129
130   /* Check busy blocks*/
131
132   i1 = 1;
133
134   while(i1 < heaplimit){
135
136     i2 = 1;
137     equal = 0;
138
139     if(heapinfo1[i1].type == -1){ /* Free block */
140       i1++;
141       continue;
142     }
143
144     addr_block1 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)heapbase1));
145
146     if(heapinfo1[i1].type == 0){  /* Large block */
147
148       while(i2 <= heaplimit && !equal){
149
150         if(heapinfo2[i2].type != 0){
151           i2++;
152           continue;
153         }
154
155         if(heapinfo2[i2].busy_block.equal_to == 1){         
156           i2++;
157           continue;
158         }
159         
160         if(heapinfo1[i1].busy_block.size != heapinfo2[i2].busy_block.size){
161           i2++;
162           continue;
163         }
164         
165         if(heapinfo1[i1].busy_block.busy_size != heapinfo2[i2].busy_block.busy_size){
166           i2++;
167           continue;
168         }
169
170         addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)heapbase2));
171         
172         /* Comparison */
173         add_heap_area_pair(previous, i1, -1, i2, -1);
174         if(!compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous)){
175           for(k=0; k < heapinfo2[i2].busy_block.size; k++)
176             heapinfo2[i2+k].busy_block.equal_to = 1;
177           for(k=0; k < heapinfo1[i1].busy_block.size; k++)
178             heapinfo1[i1+k].busy_block.equal_to = 1;
179           equal = 1;
180           match_equals(previous);
181         }
182         xbt_dynar_reset(previous);
183
184         i2++;
185
186       }
187
188     }else{ /* Fragmented block */
189
190       frag_size1 = 1 << heapinfo1[i1].type;
191
192       for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
193
194         if(heapinfo1[i1].busy_frag.frag_size[j1] == 0) /* Free fragment */
195           continue;
196         
197         addr_frag1 = (void*) ((char *)addr_block1 + (j1 * frag_size1));
198         
199         i2 = 1;
200         equal = 0;
201         
202         while(i2 <= heaplimit && !equal){
203           
204           if(heapinfo2[i2].type <= 0){
205             i2++;
206             continue;
207           }
208           
209           frag_size2 = 1 << heapinfo2[i2].type;
210
211           for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
212
213             if(heapinfo2[i2].busy_frag.equal_to[j2] == 1){                 
214               continue;              
215             }
216              
217             if(heapinfo1[i1].busy_frag.frag_size[j1] != heapinfo2[i2].busy_frag.frag_size[j2]){ /* Different size_used */    
218               continue;
219             }
220              
221             addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)heapbase2));
222             addr_frag2 = (void*) ((char *)addr_block2 + (j2 * frag_size2));
223              
224             /* Comparison */
225             add_heap_area_pair(previous, i1, j1, i2, j2);
226             if(!compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous)){
227               heapinfo2[i2].busy_frag.equal_to[j2] = 1;
228               heapinfo1[i1].busy_frag.equal_to[j1] = 1;
229               equal = 1;
230               match_equals(previous);
231               break;
232             }
233             xbt_dynar_reset(previous);
234
235           }
236
237           i2++;
238
239         }
240
241       }
242
243     }
244
245     i1++;
246
247   }
248
249   /* All blocks/fragments are equal to another block/fragment ? */
250   size_t i = 1, j = 0;
251   int nb_diff1 = 0, nb_diff2 = 0;
252   size_t frag_size = 0;
253  
254   while(i<heaplimit){
255     if(heapinfo1[i].type == 0){
256       if(heapinfo1[i].busy_block.busy_size > 0){
257         if(heapinfo1[i].busy_block.equal_to == -1){
258           if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
259             addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
260             XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block1, heapinfo1[i].busy_block.busy_size);
261             mmalloc_backtrace_block_display((void*)heapinfo1, i);
262           }
263           nb_diff1++;
264         }
265       }
266     }
267     if(heapinfo1[i].type > 0){
268       frag_size = 1 << heapinfo1[i].type;
269       for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
270         if(heapinfo1[i].busy_frag.frag_size[j] > 0){
271           if(heapinfo1[i].busy_frag.equal_to[j] == -1){
272             if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
273               addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
274               addr_frag1 = (void*) ((char *)addr_block1 + (j * frag_size));
275               XBT_DEBUG("Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag1, heapinfo1[i].busy_frag.frag_size[j]);
276               mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
277             }
278             nb_diff1++;
279           }
280         }
281       }
282     }
283     
284     i++; 
285   }
286
287   XBT_DEBUG("Different blocks or fragments in heap1 : %d\n", nb_diff1);
288
289   i = 1;
290
291   while(i<heaplimit){
292     if(heapinfo2[i].type == 0){
293       if(heapinfo2[i].busy_block.busy_size > 0){
294         if(heapinfo2[i].busy_block.equal_to == -1){
295           if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
296             addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
297             XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block2, heapinfo2[i].busy_block.busy_size);
298             mmalloc_backtrace_block_display((void*)heapinfo2, i);
299           }
300           nb_diff2++;
301         }
302       }
303     }
304     if(heapinfo2[i].type > 0){
305       frag_size = 1 << heapinfo2[i].type;
306       for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
307         if(heapinfo2[i].busy_frag.frag_size[j] > 0){
308           if(heapinfo2[i].busy_frag.equal_to[j] == -1){
309             if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
310               addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
311               addr_frag2 = (void*) ((char *)addr_block2 + (j * frag_size));
312               XBT_DEBUG( "Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag2, heapinfo2[i].busy_frag.frag_size[j]);
313               mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
314             }
315             nb_diff2++;
316           }
317         }
318       }
319     }
320     i++; 
321   }
322
323   XBT_DEBUG("Different blocks or fragments in heap2 : %d\n", nb_diff2);
324   
325   
326   /* Reset equal information */
327   i = 1;
328
329   while(i<heaplimit){
330     if(heapinfo1[i].type == 0){
331       heapinfo1[i].busy_block.equal_to = -1;
332     }
333     if(heapinfo1[i].type > 0){
334       for(j=0; j < MAX_FRAGMENT_PER_BLOCK; j++){
335         heapinfo1[i].busy_frag.equal_to[j] = -1;
336       }
337     }
338     i++; 
339   }
340
341   i = 1;
342
343   while(i<heaplimit){
344     if(heapinfo2[i].type == 0){
345       heapinfo2[i].busy_block.equal_to = -1;
346     }
347     if(heapinfo2[i].type > 0){
348       for(j=0; j < MAX_FRAGMENT_PER_BLOCK; j++){
349         heapinfo2[i].busy_frag.equal_to[j] = -1;
350       }
351     }
352     i++; 
353   }
354
355   xbt_dynar_free(&previous);
356  
357   return ((nb_diff1 > 0) || (nb_diff2 > 0));
358
359 }
360
361 static size_t heap_comparison_ignore(void *address){
362   unsigned int cursor = 0;
363   int start = 0;
364   int end = xbt_dynar_length(mmalloc_ignore) - 1;
365   mc_ignore_region_t region;
366
367   while(start <= end){
368     cursor = (start + end) / 2;
369     region = (mc_ignore_region_t)xbt_dynar_get_as(mmalloc_ignore, cursor, mc_ignore_region_t);
370     if(region->address == address)
371       return region->size;
372     if(region->address < address)
373       start = cursor + 1;
374     if(region->address > address)
375       end = cursor - 1;   
376   }
377
378   return 0;
379 }
380
381
382 static int compare_area(void *area1, void* area2, size_t size, xbt_dynar_t previous){
383
384   size_t i = 0, pointer_align = 0, ignore1 = 0, ignore2 = 0;
385   void *address_pointed1, *address_pointed2, *addr_block_pointed1, *addr_block_pointed2, *addr_frag_pointed1, *addr_frag_pointed2;
386   size_t block_pointed1, block_pointed2, frag_pointed1, frag_pointed2;
387   size_t frag_size, frag_size1, frag_size2;
388   int res_compare;
389   void *current_area1, *current_area2;
390  
391   while(i<size){
392
393     if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
394
395       current_area1 = (char*)((xbt_mheap_t)s_heap)->heapbase + ((((char *)area1) + i) - (char *)heapbase1);
396       if((ignore1 = heap_comparison_ignore(current_area1)) > 0){
397         current_area2 = (char*)((xbt_mheap_t)s_heap)->heapbase + ((((char *)area2) + i) - (char *)heapbase2);
398         if((ignore2 = heap_comparison_ignore(current_area2))  == ignore1){
399           i = i + ignore2;
400           ignore_done++;
401           continue;
402         }
403       }
404
405     }
406    
407     if(memcmp(((char *)area1) + i, ((char *)area2) + i, 1) != 0){
408
409       /* Check pointer difference */
410       pointer_align = (i / sizeof(void*)) * sizeof(void*);
411       address_pointed1 = *((void **)((char *)area1 + pointer_align));
412       address_pointed2 = *((void **)((char *)area2 + pointer_align));
413
414       /* Get pointed blocks number */ 
415       block_pointed1 = ((char*)address_pointed1 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
416       block_pointed2 = ((char*)address_pointed2 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
417
418       /* Check if valid blocks number */
419       if((char *)address_pointed1 < (char*)((xbt_mheap_t)s_heap)->heapbase || block_pointed1 > heapsize1 || block_pointed1 < 1 || (char *)address_pointed2 < (char*)((xbt_mheap_t)s_heap)->heapbase || block_pointed2 > heapsize2 || block_pointed2 < 1)
420         return 1;
421
422       if(heapinfo1[block_pointed1].type == heapinfo2[block_pointed2].type){ /* Same type of block (large or fragmented) */
423
424         addr_block_pointed1 = ((void*) (((ADDR2UINT(block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1));
425         addr_block_pointed2 = ((void*) (((ADDR2UINT(block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2));
426         
427         if(heapinfo1[block_pointed1].type == 0){ /* Large block */
428
429           if(heapinfo1[block_pointed1].busy_block.size != heapinfo2[block_pointed2].busy_block.size){
430             return 1;
431           }
432
433           if(heapinfo1[block_pointed1].busy_block.busy_size != heapinfo2[block_pointed2].busy_block.busy_size){
434             return 1;
435           }
436
437           if(add_heap_area_pair(previous, block_pointed1, -1, block_pointed2, -1)){
438             
439             res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous);
440             
441             if(res_compare)    
442               return 1;
443            
444           }
445           
446         }else{ /* Fragmented block */
447
448            /* Get pointed fragments number */ 
449           frag_pointed1 = ((uintptr_t) (ADDR2UINT (address_pointed1) % (BLOCKSIZE))) >> heapinfo1[block_pointed1].type;
450           frag_pointed2 = ((uintptr_t) (ADDR2UINT (address_pointed2) % (BLOCKSIZE))) >> heapinfo2[block_pointed2].type;
451          
452           if(heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1] != heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]) /* Different size_used */    
453             return 1;
454
455           frag_size = 1 << heapinfo1[block_pointed1].type;
456             
457           addr_frag_pointed1 = (void*) ((char *)addr_block_pointed1 + (frag_pointed1 * frag_size));
458           addr_frag_pointed2 = (void*) ((char *)addr_block_pointed2 + (frag_pointed2 * frag_size));
459
460           if(add_heap_area_pair(previous, block_pointed1, frag_pointed1, block_pointed2, frag_pointed2)){
461
462             res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous);
463             
464             if(res_compare)
465               return 1;
466            
467           }
468           
469         }
470           
471       }else{
472
473         if((heapinfo1[block_pointed1].type > 0) && (heapinfo2[block_pointed2].type > 0)){
474           
475           addr_block_pointed1 = ((void*) (((ADDR2UINT(block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1));
476           addr_block_pointed2 = ((void*) (((ADDR2UINT(block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2));
477        
478           frag_pointed1 = ((uintptr_t) (ADDR2UINT (address_pointed1) % (BLOCKSIZE))) >> heapinfo1[block_pointed1].type;
479           frag_pointed2 = ((uintptr_t) (ADDR2UINT (address_pointed2) % (BLOCKSIZE))) >> heapinfo2[block_pointed2].type;
480
481           if(heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1] != heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]) /* Different size_used */    
482             return 1;
483           
484           frag_size1 = 1 << heapinfo1[block_pointed1].type;
485           frag_size2 = 1 << heapinfo1[block_pointed2].type;
486
487           addr_frag_pointed1 = (void*) ((char *)addr_block_pointed1 + (frag_pointed1 * frag_size1));
488           addr_frag_pointed2 = (void*) ((char *)addr_block_pointed2 + (frag_pointed2 * frag_size2));
489
490           if(add_heap_area_pair(previous, block_pointed1, frag_pointed1, block_pointed2, frag_pointed2)){
491
492             res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous);
493             
494             if(res_compare)
495               return 1;
496            
497           }
498
499         }else{
500           return 1;
501         }
502
503       }
504
505       i = pointer_align + sizeof(void *);
506       
507     }else{
508
509       i++;
510
511     }
512   }
513
514   return 0;
515   
516
517 }
518
519 static void heap_area_pair_free(heap_area_pair_t pair){
520   if (pair){
521     free(pair);
522     pair = NULL;
523   }
524 }
525
526 static void heap_area_pair_free_voidp(void *d)
527 {
528   heap_area_pair_free((heap_area_pair_t) * (void **) d);
529 }
530
531 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
532
533   if(is_new_heap_area_pair(list, block1, fragment1, block2, fragment2)){
534     heap_area_pair_t pair = NULL;
535     pair = xbt_new0(s_heap_area_pair_t, 1);
536     pair->block1 = block1;
537     pair->fragment1 = fragment1;
538     pair->block2 = block2;
539     pair->fragment2 = fragment2;
540     
541     xbt_dynar_push(list, &pair); 
542
543     return 1;
544   }
545
546   return 0;
547 }
548  
549 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
550   
551   unsigned int cursor = 0;
552   heap_area_pair_t current_pair;
553
554   xbt_dynar_foreach(list, cursor, current_pair){
555     if(current_pair->block1 == block1 && current_pair->block2 == block2 && current_pair->fragment1 == fragment1 && current_pair->fragment2 == fragment2)
556       return 0; 
557   }
558   
559   return 1;
560 }
561
562 static void match_equals(xbt_dynar_t list){
563
564   unsigned int cursor = 0;
565   heap_area_pair_t current_pair;
566
567   xbt_dynar_foreach(list, cursor, current_pair){
568     if(current_pair->fragment1 != -1){
569       heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] = 1;
570       heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] = 1;
571     }else{
572       heapinfo1[current_pair->block1].busy_block.equal_to = 1;
573       heapinfo2[current_pair->block2].busy_block.equal_to = 1;
574     }
575   }
576
577 }
578