Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot//simgrid/simgrid
[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
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mm_diff, xbt,
11                                 "Logging specific to mm_diff in mmalloc");
12
13 extern char *xbt_binary_name;
14
15 void mmalloc_backtrace_display(xbt_mheap_t mdp, void *ptr){
16   size_t block = BLOCK(ptr);
17   int type;
18   xbt_ex_t e;
19
20   if ((char *) ptr < (char *) mdp->heapbase || block > mdp->heapsize) {
21     fprintf(stderr,"Ouch, this pointer is not mine. I cannot display its backtrace. I refuse it to death!!\n");
22     abort();
23   }
24
25   type = mdp->heapinfo[block].type;
26
27   if (type != 0) {
28     //fprintf(stderr,"Only full blocks are backtraced for now. Ignoring your request.\n");
29     return;
30   }
31   if (mdp->heapinfo[block].busy_block.bt_size == 0) {
32     fprintf(stderr,"No backtrace available for that block, sorry.\n");
33     return;
34   }
35
36   memcpy(&e.bt,&(mdp->heapinfo[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE);
37   e.used = mdp->heapinfo[block].busy_block.bt_size;
38
39   xbt_ex_setup_backtrace(&e);
40   if (e.used == 0) {
41     fprintf(stderr, "(backtrace not set)\n");
42   } else if (e.bt_strings == NULL) {
43     fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
44   } else {
45     int i;
46
47     fprintf(stderr, "Backtrace of where the block %p where malloced (%d frames):\n",ptr,e.used);
48     for (i = 0; i < e.used; i++)       /* no need to display "xbt_backtrace_display" */{
49       fprintf(stderr,"%d",i);fflush(NULL);
50       fprintf(stderr, "---> %s\n", e.bt_strings[i] + 4);
51     }
52   }
53 }
54
55 int mmalloc_compare_heap(xbt_mheap_t mdp1, xbt_mheap_t mdp2, void *std_heap_addr){
56
57   if(mdp1 == NULL && mdp2 == NULL){
58     XBT_DEBUG("Malloc descriptors null\n");
59     return 0;
60   }
61
62   int errors = mmalloc_compare_mdesc(mdp1, mdp2, std_heap_addr);
63
64   return (errors > 0);
65
66 }
67
68 int mmalloc_compare_mdesc(struct mdesc *mdp1, struct mdesc *mdp2, void *std_heap_addr){
69
70   if(mdp1->headersize != mdp2->headersize){
71     fprintf(stderr, "Different size of the file header for the mapped files\n");
72     return 1;
73   }
74
75   if(mdp1->refcount != mdp2->refcount){
76     fprintf(stderr, "Different number of processes that attached the heap\n");
77     return 1;
78   }
79
80   if(strcmp(mdp1->magic, mdp2->magic) != 0){
81     fprintf(stderr,"Different magic number\n");
82     return 1;
83   }
84
85   if(mdp1->flags != mdp2->flags){
86     fprintf(stderr,"Different flags\n");  
87     return 1;
88   }
89
90   if(mdp1->heapsize != mdp2->heapsize){
91     fprintf(stderr,"Different number of info entries\n");
92     return 1;
93   }
94   
95
96   if(mdp1->heapbase != mdp2->heapbase){
97     fprintf(stderr,"Different first block of the heap\n");
98     return 1;
99   }
100
101
102   if(mdp1->heapindex != mdp2->heapindex){
103     fprintf(stderr,"Different index for the heap table : %zu - %zu\n", mdp1->heapindex, mdp2->heapindex);
104     return 1;
105   }
106
107
108   if(mdp1->base != mdp2->base){
109     fprintf(stderr,"Different base address of the memory region\n");
110     return 1;
111   }
112
113   if(mdp1->breakval != mdp2->breakval){
114     fprintf(stderr,"Different current location in the memory region\n");
115     return 1;
116   }
117
118   if(mdp1->top != mdp2->top){
119     fprintf(stderr,"Different end of the current location in the memory region\n");
120     return 1;
121   }
122
123   if(mdp1->heaplimit != mdp2->heaplimit){
124     fprintf(stderr,"Different limit of valid info table indices\n");
125     return 1;
126   }
127
128   if(mdp1->fd != mdp2->fd){
129     fprintf(stderr,"Different file descriptor for the file to which this malloc heap is mapped\n");
130     return 1;
131   }
132
133   if(mdp1->version != mdp2->version){
134     fprintf(stderr,"Different version of the mmalloc package\n");
135     return 1;
136   }
137
138
139   size_t i, j;
140   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
141   size_t frag_size;
142
143   i = 0;
144
145   /* Check busy blocks*/
146
147   while(i < mdp1->heapindex){
148
149     if(mdp1->heapinfo[i].type != mdp2->heapinfo[i].type){
150       fprintf(stderr,"Different type of block : %d - %d\n", mdp1->heapinfo[i].type, mdp2->heapinfo[i].type);
151       return 1;
152     }
153
154     addr_block1 = (char *)mdp1 + sizeof(struct mdesc) + (i * BLOCKSIZE);
155     addr_block2 = (char *)mdp2 + sizeof(struct mdesc) + (i * BLOCKSIZE);
156
157     if(mdp1->heapinfo[i].type == 0){ /* busy large block */
158
159       if(mdp1->heapinfo[i].busy_block.size != mdp2->heapinfo[i].busy_block.size){
160         fprintf(stderr,"Different size of a large cluster : %zu - %zu\n", mdp1->heapinfo[i].busy_block.size, mdp2->heapinfo[i].busy_block.size);
161         return 1;
162       } 
163
164       if(mdp1->heapinfo[i].busy_block.busy_size != mdp2->heapinfo[i].busy_block.busy_size){
165         fprintf(stderr,"Different busy_size of a large cluster : %zu - %zu\n", mdp1->heapinfo[i].busy_block.busy_size, mdp2->heapinfo[i].busy_block.busy_size);
166         return 1;
167       } 
168
169       if(memcmp(addr_block1, addr_block2, (mdp1->heapinfo[i].busy_block.busy_size)) != 0){
170         fprintf(stderr,"Different data in large block %zu (size = %zu (in blocks), busy_size = %zu (in bytes))\n", i, mdp1->heapinfo[i].busy_block.size, mdp1->heapinfo[i].busy_block.busy_size);
171         return 1;
172       }
173
174       i = i + mdp1->heapinfo[i].busy_block.size;
175
176     }else{
177       
178       if(mdp1->heapinfo[i].type > 0){ /* busy fragmented block */
179
180         if(mdp1->heapinfo[i].type != mdp2->heapinfo[i].type){
181           fprintf(stderr,"Different size of fragments in fragmented block %zu : %d - %d\n", i, mdp1->heapinfo[i].type, mdp2->heapinfo[i].type);
182           return 1;
183         }
184
185         if(mdp1->heapinfo[i].busy_frag.nfree != mdp2->heapinfo[i].busy_frag.nfree){
186           fprintf(stderr,"Different free fragments in fragmented block %zu : %zu - %zu\n", i, mdp1->heapinfo[i].busy_frag.nfree, mdp2->heapinfo[i].busy_frag.nfree);
187           return 1;
188         } 
189         
190         if(mdp1->heapinfo[i].busy_frag.first != mdp2->heapinfo[i].busy_frag.first){
191           fprintf(stderr,"Different busy_size of a large cluster : %zu - %zu\n", mdp1->heapinfo[i].busy_block.busy_size, mdp2->heapinfo[i].busy_block.busy_size);
192           return 1;
193         } 
194
195         for(j=0; j< MAX_FRAGMENT_PER_BLOCK; j++){
196
197           if(mdp1->heapinfo[i].busy_frag.frag_size[j] != mdp2->heapinfo[i].busy_frag.frag_size[j]){
198             fprintf(stderr,"Different busy_size for fragment %zu in block %zu : %u - %u\n", j, i, mdp1->heapinfo[i].busy_frag.frag_size[j], mdp2->heapinfo[i].busy_frag.frag_size[j]);
199             return 1;
200           }
201
202           if(mdp1->heapinfo[i].busy_frag.frag_size[j] > 0){
203             
204             frag_size = pow(2, mdp1->heapinfo[i].type);
205             addr_frag1 = (char *)addr_block1 + (j * frag_size);
206             addr_frag2 = (char *)addr_block2 + (j * frag_size);
207
208             if(memcmp(addr_frag1, addr_frag2, frag_size) != 0){
209               fprintf(stderr,"Different data in fragment %zu in block %zu \n", j, i);
210               return 1;
211             }
212
213           }
214         }
215
216         i++;
217
218       }else{ /* free block */
219
220         i++;
221
222       }
223       
224     }
225
226   }
227
228
229   return 0;
230 }
231
232
233 void mmalloc_display_info_heap(xbt_mheap_t h){
234
235 }
236
237
238