Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : memcmp on space really used in fragment
[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
56 void mmalloc_backtrace_block_display(xbt_mheap_t mdp, size_t block){
57
58   int type;
59   xbt_ex_t e;
60
61   type = mdp->heapinfo[block].type;
62
63   if (type != 0) {
64     fprintf(stderr,"Only full blocks are backtraced for now. Ignoring your request.\n");
65     return;
66   }
67   if (mdp->heapinfo[block].busy_block.bt_size == 0) {
68     fprintf(stderr,"No backtrace available for that block, sorry.\n");
69     return;
70   }
71
72   memcpy(&e.bt,&(mdp->heapinfo[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE);
73   e.used = mdp->heapinfo[block].busy_block.bt_size;
74
75   xbt_ex_setup_backtrace(&e);
76   if (e.used == 0) {
77     fprintf(stderr, "(backtrace not set)\n");
78   } else if (e.bt_strings == NULL) {
79     fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
80   } else {
81     int i;
82
83     fprintf(stderr, "Backtrace of where the block %zu where malloced (%d frames):\n", block ,e.used);
84     for (i = 0; i < e.used; i++)       /* no need to display "xbt_backtrace_display" */{
85       fprintf(stderr,"%d",i);fflush(NULL);
86       fprintf(stderr, "---> %s\n", e.bt_strings[i] + 4);
87     }
88   }
89 }
90
91 int mmalloc_compare_heap(xbt_mheap_t mdp1, xbt_mheap_t mdp2){
92
93   if(mdp1 == NULL && mdp2 == NULL){
94     XBT_DEBUG("Malloc descriptors null\n");
95     return 0;
96   }
97
98   int errors = mmalloc_compare_mdesc(mdp1, mdp2);
99
100   return (errors > 0);
101
102 }
103
104 int mmalloc_compare_mdesc(struct mdesc *mdp1, struct mdesc *mdp2){
105
106   int errors = 0;
107
108   if(mdp1->headersize != mdp2->headersize){
109     fprintf(stderr, "Different size of the file header for the mapped files\n");
110     return 1;
111   }
112
113   if(mdp1->refcount != mdp2->refcount){
114     fprintf(stderr, "Different number of processes that attached the heap\n");
115     return 1;
116   }
117
118   if(strcmp(mdp1->magic, mdp2->magic) != 0){
119     fprintf(stderr,"Different magic number\n");
120     return 1;
121   }
122
123   if(mdp1->flags != mdp2->flags){
124     fprintf(stderr,"Different flags\n");  
125     return 1;
126   }
127
128   if(mdp1->heapsize != mdp2->heapsize){
129     fprintf(stderr,"Different number of info entries\n");
130     return 1;
131   }
132   
133
134   if(mdp1->heapbase != mdp2->heapbase){
135     fprintf(stderr,"Different first block of the heap\n");
136     return 1;
137   }
138
139
140   if(mdp1->heapindex != mdp2->heapindex){
141     fprintf(stderr,"Different index for the heap table : %zu - %zu\n", mdp1->heapindex, mdp2->heapindex);
142     return 1;
143   }
144
145
146   if(mdp1->base != mdp2->base){
147     fprintf(stderr,"Different base address of the memory region\n");
148     return 1;
149   }
150
151   if(mdp1->breakval != mdp2->breakval){
152     fprintf(stderr,"Different current location in the memory region\n");
153     return 1;
154   }
155
156   if(mdp1->top != mdp2->top){
157     fprintf(stderr,"Different end of the current location in the memory region\n");
158     return 1;
159   }
160
161   if(mdp1->heaplimit != mdp2->heaplimit){
162     fprintf(stderr,"Different limit of valid info table indices\n");
163     return 1;
164   }
165
166   if(mdp1->fd != mdp2->fd){
167     fprintf(stderr,"Different file descriptor for the file to which this malloc heap is mapped\n");
168     return 1;
169   }
170
171   if(mdp1->version != mdp2->version){
172     fprintf(stderr,"Different version of the mmalloc package\n");
173     return 1;
174   }
175
176
177   size_t i, j;
178   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
179   size_t frag_size;
180
181   i = 0;
182
183   /* Check busy blocks*/
184
185   while(i < mdp1->heapindex){
186
187     if(mdp1->heapinfo[i].type != mdp2->heapinfo[i].type){
188       fprintf(stderr,"Different type of block : %d - %d\n", mdp1->heapinfo[i].type, mdp2->heapinfo[i].type);
189       errors++;
190     }
191
192     addr_block1 = (char *)mdp1 + sizeof(struct mdesc) + (i * BLOCKSIZE);
193     addr_block2 = (char *)mdp2 + sizeof(struct mdesc) + (i * BLOCKSIZE);
194
195     if(mdp1->heapinfo[i].type == 0){ /* busy large block */
196
197       if(mdp1->heapinfo[i].busy_block.size != mdp2->heapinfo[i].busy_block.size){
198         fprintf(stderr,"Different size of a large cluster : %zu - %zu\n", mdp1->heapinfo[i].busy_block.size, mdp2->heapinfo[i].busy_block.size);
199         errors++;
200       } 
201
202       if(mdp1->heapinfo[i].busy_block.busy_size != mdp2->heapinfo[i].busy_block.busy_size){
203         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);
204         errors++;
205       } 
206
207       if(memcmp(addr_block1, addr_block2, (mdp1->heapinfo[i].busy_block.busy_size)) != 0){
208         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);
209         //fprintf(stderr, "Backtrace size : %d\n", mdp1->heapinfo[i].busy_block.bt_size);
210         mmalloc_backtrace_block_display(mdp1, i);
211         errors++;
212       }
213
214       //fprintf(stderr, "Backtrace size : %d\n", mdp1->heapinfo[i].busy_block.bt_size);
215       //mmalloc_backtrace_block_display(mdp1, i);
216         
217       i = i + mdp1->heapinfo[i].busy_block.size;
218
219     }else{
220       
221       if(mdp1->heapinfo[i].type > 0){ /* busy fragmented block */
222
223         if(mdp1->heapinfo[i].type != mdp2->heapinfo[i].type){
224           fprintf(stderr,"Different size of fragments in fragmented block %zu : %d - %d\n", i, mdp1->heapinfo[i].type, mdp2->heapinfo[i].type);
225           errors++;
226         }
227
228         if(mdp1->heapinfo[i].busy_frag.nfree != mdp2->heapinfo[i].busy_frag.nfree){
229           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);
230           errors++;
231         } 
232         
233         if(mdp1->heapinfo[i].busy_frag.first != mdp2->heapinfo[i].busy_frag.first){
234           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);
235           errors++;
236         } 
237
238         frag_size = pow(2, mdp1->heapinfo[i].type);
239
240         for(j=0; j< (BLOCKSIZE/frag_size); j++){
241
242           if(mdp1->heapinfo[i].busy_frag.frag_size[j] != mdp2->heapinfo[i].busy_frag.frag_size[j]){
243             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]);
244             errors++;
245           }
246
247           if(mdp1->heapinfo[i].busy_frag.frag_size[j] > 0){
248             
249             addr_frag1 = (char *)addr_block1 + (j * frag_size);
250             addr_frag2 = (char *)addr_block2 + (j * frag_size);
251
252             if(memcmp(addr_frag1, addr_frag2, mdp1->heapinfo[i].busy_frag.frag_size[j]) != 0){
253               fprintf(stderr,"Different data in fragment %zu (size = %zu, size used = %u) in block %zu \n", j, frag_size, mdp1->heapinfo[i].busy_frag.frag_size[j], i);
254               errors++;
255             }
256
257           }
258         }
259
260         i++;
261
262       }else{ /* free block */
263
264         i++;
265
266       }
267       
268     }
269
270   }
271
272
273   return (errors);
274 }
275
276
277 void mmalloc_display_info_heap(xbt_mheap_t h){
278
279 }
280
281
282