Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d0b8fff820842c5453da0566da2c35b0a93dcb27
[simgrid.git] / src / mc / mc_compare.c
1 /* Copyright (c) 2008-2012 Da SimGrid Team. All rights reserved.            */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "mc_private.h"
7
8 #include "xbt/mmalloc.h"
9
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, mc,
11                                 "Logging specific to mc_compare");
12
13 static int heap_region_compare(void *d1, void *d2, size_t size);
14
15 static int is_heap_equality(xbt_dynar_t equals, void *a1, void *a2);
16 static size_t heap_ignore_size(void *address);
17
18 static void stack_region_free(stack_region_t s);
19 static void heap_equality_free(heap_equality_t e);
20
21 static int is_stack_ignore_variable(char *frame, char *var_name);
22 static int compare_local_variables(char *s1, char *s2);
23 static int compare_global_variables(int region_type, void *d1, void *d2);
24
25 static size_t heap_ignore_size(void *address){
26   unsigned int cursor = 0;
27   int start = 0;
28   int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
29   mc_heap_ignore_region_t region;
30
31   while(start <= end){
32     cursor = (start + end) / 2;
33     region = (mc_heap_ignore_region_t)xbt_dynar_get_as(mc_heap_comparison_ignore, cursor, mc_heap_ignore_region_t);
34     if(region->address == address)
35       return region->size;
36     if(region->address < address)
37       start = cursor + 1;
38     if(region->address > address)
39       end = cursor - 1;   
40   }
41
42   return 0;
43 }
44
45 static int compare_global_variables(int region_type, void *d1, void *d2){
46
47   unsigned int cursor = 0;
48   size_t offset; 
49   int i = 0;
50   global_variable_t current_var; 
51   int pointer_align; 
52   void *addr_pointed1 = NULL, *addr_pointed2 = NULL;
53   int res_compare = 0;
54
55   if(region_type == 1){ /* libsimgrid */
56     xbt_dynar_foreach(mc_global_variables, cursor, current_var){
57       if(current_var->address < start_data_libsimgrid)
58         continue;
59       offset = (char *)current_var->address - (char *)start_data_libsimgrid;
60       i = 0;
61       while(i < current_var->size){
62         if(memcmp((char*)d1 + offset + i, (char*)d2 + offset + i, 1) != 0){
63           pointer_align = (i / sizeof(void*)) * sizeof(void*); 
64           addr_pointed1 = *((void **)((char *)d1 + offset + pointer_align));
65           addr_pointed2 = *((void **)((char *)d2 + offset + pointer_align));
66           if((addr_pointed1 > start_plt_libsimgrid && addr_pointed1 < end_plt_libsimgrid) || (addr_pointed2 > start_plt_libsimgrid && addr_pointed2 < end_plt_libsimgrid)){
67             i = current_var->size;
68             continue;
69           }else{
70             if((addr_pointed1 > std_heap) && ((char *)addr_pointed1 < (char *)std_heap + STD_HEAP_SIZE) && (addr_pointed2 > std_heap) && ((char *)addr_pointed2 < (char *)std_heap + STD_HEAP_SIZE)){
71               res_compare = compare_area(addr_pointed1, addr_pointed2, NULL);
72               if(res_compare == 1){
73                 #ifdef MC_VERBOSE
74                   XBT_VERB("Different global variable in libsimgrid : %s at addresses %p - %p (size = %zu)", current_var->name, (char *)d1+offset, (char *)d2+offset, current_var->size);
75                 #endif
76                 return 1;
77               }
78             }else{
79               #ifdef MC_VERBOSE
80                 XBT_VERB("Different global variable in libsimgrid : %s at addresses %p - %p (size = %zu)", current_var->name, (char *)d1+offset, (char *)d2+offset, current_var->size);
81               #endif
82               return 1;
83             }
84            
85           }
86         } 
87         i++;
88       }
89     }
90   }else{ /* binary */
91     xbt_dynar_foreach(mc_global_variables, cursor, current_var){
92       if(current_var->address > start_data_libsimgrid)
93         break;
94       offset = (char *)current_var->address - (char *)start_data_binary;
95       i = 0;
96       while(i < current_var->size){
97         if(memcmp((char*)d1 + offset + i, (char*)d2 + offset + i, 1) != 0){
98           pointer_align = (i / sizeof(void*)) * sizeof(void*); 
99           addr_pointed1 = *((void **)((char *)d1 + offset + pointer_align));
100           addr_pointed2 = *((void **)((char *)d2 + offset + pointer_align));
101           if((addr_pointed1 > start_plt_binary && addr_pointed1 < end_plt_binary) || (addr_pointed2 > start_plt_binary && addr_pointed2 < end_plt_binary)){
102             i = current_var->size;
103             continue;
104           }else{
105             if((addr_pointed1 > std_heap) && ((char *)addr_pointed1 < (char *)std_heap + STD_HEAP_SIZE) && (addr_pointed2 > std_heap) && ((char *)addr_pointed2 < (char *)std_heap + STD_HEAP_SIZE)){
106               res_compare = compare_area(addr_pointed1, addr_pointed2, NULL);
107               if(res_compare == 1){
108                 #ifdef MC_VERBOSE
109                   XBT_VERB("Different global variable in binary : %s at addresses %p - %p (size = %zu)", current_var->name, (char *)d1+offset, (char *)d2+offset, current_var->size);
110                 #endif
111                 return 1;
112               }
113             }else{
114               #ifdef MC_VERBOSE
115                 XBT_VERB("Different global variable in binary : %s at addresses %p - %p (size = %zu)", current_var->name, (char *)d1+offset, (char *)d2+offset, current_var->size);
116               #endif
117               return 1;
118             }
119           }
120         } 
121         i++;
122       }
123       
124     }
125   }
126
127   return 0;
128 }
129
130 static int heap_region_compare(void *d1, void *d2, size_t size){
131   
132   size_t i = 0;
133   
134   for(i=0; i<size; i++){
135     if(memcmp(((char *)d1) + i, ((char *)d2) + i, 1) != 0){
136       if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_verbose)){
137         XBT_VERB("Different byte (offset=%zu) (%p - %p) in heap region", i, (char *)d1 + i, (char *)d2 + i);
138       }
139       return 1;
140     }
141   }
142   
143   return 0;
144 }
145
146 static void stack_region_free(stack_region_t s){
147   if(s){
148     xbt_free(s->process_name);
149     xbt_free(s);
150   }
151 }
152
153 void stack_region_free_voidp(void *s){
154   stack_region_free((stack_region_t) * (void **) s);
155 }
156
157 static void heap_equality_free(heap_equality_t e){
158   if(e){
159     xbt_free(e);
160   }
161 }
162
163 void heap_equality_free_voidp(void *e){
164   heap_equality_free((heap_equality_t) * (void **) e);
165 }
166
167 int SIMIX_pre_mc_compare_snapshots(smx_simcall_t simcall,
168                                    mc_snapshot_t s1, mc_snapshot_t s2){
169   return snapshot_compare(s1, s2);
170 }
171
172 int get_heap_region_index(mc_snapshot_t s){
173   int i = 0;
174   while(i < s->num_reg){
175     switch(s->regions[i]->type){
176     case 0:
177       return i;
178       break;
179     default:
180       i++;
181       break;
182     }
183   }
184   return -1;
185 }
186
187 int snapshot_compare(mc_snapshot_t s1, mc_snapshot_t s2){
188
189   int raw_mem = (mmalloc_get_current_heap() == raw_heap);
190   
191   MC_SET_RAW_MEM;
192      
193   int errors = 0;
194
195   xbt_os_timer_t global_timer = xbt_os_timer_new();
196   xbt_os_timer_t timer = xbt_os_timer_new();
197
198   xbt_os_timer_start(global_timer);
199
200   #ifdef MC_DEBUG
201     xbt_os_timer_start(timer);
202   #endif
203
204   /* Compare number of processes */
205   if(s1->nb_processes != s2->nb_processes){
206     #ifdef MC_DEBUG
207       xbt_os_timer_stop(timer);
208       mc_comp_times->nb_processes_comparison_time =  xbt_os_timer_elapsed(timer);
209       XBT_DEBUG("Different number of processes : %d - %d", s1->nb_processes, s2->nb_processes);
210       errors++;
211     #else
212       #ifdef MC_VERBOSE
213         XBT_VERB("Different number of processes : %d - %d", s1->nb_processes, s2->nb_processes);
214       #endif
215      
216       xbt_os_timer_free(timer);
217       xbt_os_timer_stop(global_timer);
218       mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
219       xbt_os_timer_free(global_timer);
220
221       if(!raw_mem)
222         MC_UNSET_RAW_MEM;
223
224       return 1;
225     #endif
226   }
227
228   #ifdef MC_DEBUG
229     xbt_os_timer_start(timer);
230   #endif
231
232   /* Compare number of bytes used in each heap */
233   if(s1->heap_bytes_used != s2->heap_bytes_used){
234     #ifdef MC_DEBUG
235       xbt_os_timer_stop(timer);
236       mc_comp_times->bytes_used_comparison_time = xbt_os_timer_elapsed(timer);
237       XBT_DEBUG("Different number of bytes used in each heap : %zu - %zu", s1->heap_bytes_used, s2->heap_bytes_used);
238       errors++;
239     #else
240       #ifdef MC_VERBOSE
241         XBT_VERB("Different number of bytes used in each heap : %zu - %zu", s1->heap_bytes_used, s2->heap_bytes_used);
242       #endif
243
244       xbt_os_timer_free(timer);
245       xbt_os_timer_stop(global_timer);
246       mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
247       xbt_os_timer_free(global_timer);
248
249       if(!raw_mem)
250         MC_UNSET_RAW_MEM;
251
252       return 1;
253     #endif
254   }else{
255     #ifdef MC_DEBUG
256       xbt_os_timer_stop(timer);
257     #endif
258   }
259   
260   #ifdef MC_DEBUG
261     xbt_os_timer_start(timer);
262   #endif
263   
264   /* Compare size of stacks */
265   int i = 0;
266   size_t size_used1, size_used2;
267   int is_diff = 0;
268   while(i < xbt_dynar_length(s1->stacks)){
269     size_used1 = s1->stack_sizes[i];
270     size_used2 = s2->stack_sizes[i];
271     if(size_used1 != size_used2){
272     #ifdef MC_DEBUG
273       if(is_diff == 0){
274         xbt_os_timer_stop(timer);
275         mc_comp_times->stacks_sizes_comparison_time = xbt_os_timer_elapsed(timer);
276       }
277       XBT_DEBUG("Different size used in stacks : %zu - %zu", size_used1, size_used2);
278       errors++;
279       is_diff = 1;
280     #else
281       #ifdef MC_VERBOSE
282         XBT_VERB("Different size used in stacks : %zu - %zu", size_used1, size_used2);
283       #endif
284  
285       xbt_os_timer_free(timer);
286       xbt_os_timer_stop(global_timer);
287       mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
288       xbt_os_timer_free(global_timer);
289
290       if(!raw_mem)
291         MC_UNSET_RAW_MEM;
292
293       return 1;
294     #endif  
295     }
296     i++;
297   }
298
299   #ifdef MC_DEBUG
300     if(is_diff == 0)
301       xbt_os_timer_stop(timer);
302     xbt_os_timer_start(timer);
303   #endif
304
305   int heap_index = 0, data_libsimgrid_index = 0, data_program_index = 0;
306   i = 0;
307   
308   /* Get index of regions */
309   while(i < s1->num_reg){
310     switch(s1->region_type[i]){
311     case 0:
312       heap_index = i;
313       i++;
314       break;
315     case 1:
316       data_libsimgrid_index = i;
317       i++;
318       while( i < s1->num_reg && s1->region_type[i] == 1)
319         i++;
320       break;
321     case 2:
322       data_program_index = i;
323       i++;
324       while( i < s1->num_reg && s1->region_type[i] == 2)
325         i++;
326       break;
327     }
328   }
329
330   /* Init heap information used in heap comparison algorithm */
331   init_heap_information((xbt_mheap_t)s1->regions[heap_index]->data, (xbt_mheap_t)s2->regions[heap_index]->data, s1->to_ignore, s2->to_ignore);
332
333   /* Compare binary global variables */
334   is_diff = compare_global_variables(s1->region_type[data_program_index], s1->regions[data_program_index]->data, s2->regions[data_program_index]->data);
335   if(is_diff != 0){
336     #ifdef MC_DEBUG
337       xbt_os_timer_stop(timer);
338       mc_comp_times->binary_global_variables_comparison_time = xbt_os_timer_elapsed(timer);
339       XBT_DEBUG("Different global variables in binary"); 
340       errors++; 
341     #else
342       #ifdef MC_VERBOSE
343         XBT_VERB("Different global variables in binary"); 
344       #endif
345     
346       xbt_os_timer_free(timer);
347       xbt_os_timer_stop(global_timer);
348       mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
349       xbt_os_timer_free(global_timer);
350     
351       if(!raw_mem)
352         MC_UNSET_RAW_MEM;
353
354       return 1;
355     #endif
356   }
357
358   #ifdef MC_DEBUG
359     if(is_diff == 0)
360       xbt_os_timer_stop(timer);
361     xbt_os_timer_start(timer);
362   #endif
363
364   /* Compare libsimgrid global variables */
365     is_diff = compare_global_variables(s1->region_type[data_libsimgrid_index], s1->regions[data_libsimgrid_index]->data, s2->regions[data_libsimgrid_index]->data);
366   if(is_diff != 0){
367     #ifdef MC_DEBUG
368       xbt_os_timer_stop(timer);
369       mc_comp_times->libsimgrid_global_variables_comparison_time = xbt_os_timer_elapsed(timer); 
370       XBT_DEBUG("Different global variables in libsimgrid"); 
371       errors++; 
372     #else
373       #ifdef MC_VERBOSE
374         XBT_VERB("Different global variables in libsimgrid"); 
375       #endif
376     
377       xbt_os_timer_free(timer);
378       xbt_os_timer_stop(global_timer);
379       mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
380       xbt_os_timer_free(global_timer);
381     
382       if(!raw_mem)
383         MC_UNSET_RAW_MEM;
384
385       return 1;
386     #endif
387   }  
388
389   #ifdef MC_DEBUG
390     if(is_diff == 0)
391       xbt_os_timer_stop(timer);
392     xbt_os_timer_start(timer);
393   #endif
394
395     /* Stacks comparison */
396     unsigned int  cursor = 0;
397     int diff_local = 0;
398     is_diff = 0;
399     
400     while(cursor < xbt_dynar_length(s1->stacks)){
401       diff_local = compare_local_variables(((mc_snapshot_stack_t)xbt_dynar_get_as(s1->stacks, cursor, mc_snapshot_stack_t))->local_variables->data, ((mc_snapshot_stack_t)xbt_dynar_get_as(s2->stacks, cursor, mc_snapshot_stack_t))->local_variables->data);
402       if(diff_local > 0){
403         #ifdef MC_DEBUG
404           if(is_diff == 0){
405             xbt_os_timer_stop(timer);
406             mc_comp_times->stacks_comparison_time = xbt_os_timer_elapsed(timer); 
407           }
408           XBT_DEBUG("Different local variables between stacks %d", cursor + 1);
409           errors++;
410           is_diff = 1;
411         #else
412         
413           #ifdef MC_VERBOSE
414             XBT_VERB("Different local variables between stacks %d", cursor + 1);
415           #endif
416           
417             reset_heap_information();
418             xbt_os_timer_free(timer);
419             xbt_os_timer_stop(global_timer);
420             mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
421             xbt_os_timer_free(global_timer);
422             
423             if(!raw_mem)
424               MC_UNSET_RAW_MEM;
425             
426             return 1;
427         #endif
428       }
429       cursor++;
430     }
431     
432     #ifdef MC_DEBUG
433       xbt_os_timer_start(timer);
434     #endif
435
436   /* Compare heap */
437  
438   if(mmalloc_compare_heap((xbt_mheap_t)s1->regions[heap_index]->data, (xbt_mheap_t)s2->regions[heap_index]->data)){
439
440     #ifdef MC_DEBUG
441       xbt_os_timer_stop(timer);
442       mc_comp_times->heap_comparison_time = xbt_os_timer_elapsed(timer); 
443       XBT_DEBUG("Different heap (mmalloc_compare)");
444       errors++;
445     #else
446
447       xbt_os_timer_free(timer);
448  
449       #ifdef MC_VERBOSE
450         XBT_VERB("Different heap (mmalloc_compare)");
451       #endif
452        
453       reset_heap_information();
454       xbt_os_timer_stop(global_timer);
455       mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
456       xbt_os_timer_free(global_timer);
457
458       if(!raw_mem)
459         MC_UNSET_RAW_MEM;
460
461       return 1;
462     #endif
463   }else{
464     #ifdef MC_DEBUG
465       xbt_os_timer_stop(timer);
466     #endif
467   }
468
469   reset_heap_information();
470    
471   xbt_os_timer_free(timer);
472
473   #ifdef MC_VERBOSE
474     xbt_os_timer_stop(global_timer);
475     mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
476   #endif
477
478   xbt_os_timer_free(global_timer);
479
480   #ifdef MC_DEBUG
481     print_comparison_times();
482   #endif
483
484   if(!raw_mem)
485     MC_UNSET_RAW_MEM;
486
487   return errors > 0;
488   
489 }
490
491 static int is_stack_ignore_variable(char *frame, char *var_name){
492
493   unsigned int cursor = 0;
494   int start = 0;
495   int end = xbt_dynar_length(mc_stack_comparison_ignore) - 1;
496   mc_stack_ignore_variable_t current_var;
497
498   while(start <= end){
499     cursor = (start + end) / 2;
500     current_var = (mc_stack_ignore_variable_t)xbt_dynar_get_as(mc_stack_comparison_ignore, cursor, mc_stack_ignore_variable_t);
501     if(strcmp(current_var->frame, frame) == 0 || strcmp(current_var->frame, "*") == 0){
502       if(strcmp(current_var->var_name, var_name) == 0)
503         return 1;
504       if(strcmp(current_var->var_name, var_name) < 0)
505         start = cursor + 1;
506       if(strcmp(current_var->var_name, var_name) > 0)
507         end = cursor - 1;
508     }else if(strcmp(current_var->frame, frame) < 0){
509       start = cursor + 1;
510     }else if(strcmp(current_var->frame, frame) > 0){
511       end = cursor - 1; 
512     }
513   }
514
515   return 0;
516 }
517
518 static int compare_local_variables(char *s1, char *s2){
519   
520   xbt_dynar_t tokens1 = xbt_str_split(s1, NULL);
521   xbt_dynar_t tokens2 = xbt_str_split(s2, NULL);
522
523   xbt_dynar_t s_tokens1, s_tokens2;
524   unsigned int cursor = 0;
525   void *addr1, *addr2;
526   char *ip1 = NULL, *ip2 = NULL;
527   int res_compare = 0;
528
529   #ifdef MC_VERBOSE
530     char *var_name;
531   #endif
532
533   while(cursor < xbt_dynar_length(tokens1)){
534     s_tokens1 = xbt_str_split(xbt_dynar_get_as(tokens1, cursor, char *), "=");
535     s_tokens2 = xbt_str_split(xbt_dynar_get_as(tokens2, cursor, char *), "=");
536     if(xbt_dynar_length(s_tokens1) > 1 && xbt_dynar_length(s_tokens2) > 1){
537       #ifdef MC_VERBOSE
538         var_name = xbt_dynar_get_as(s_tokens1, 0, char *);
539       #endif
540       if((strcmp(xbt_dynar_get_as(s_tokens1, 0, char *), "ip") == 0) && (strcmp(xbt_dynar_get_as(s_tokens2, 0, char *), "ip") == 0)){
541         xbt_free(ip1);
542         xbt_free(ip2);
543         ip1 = strdup(xbt_dynar_get_as(s_tokens1, 1, char *));
544         ip2 = strdup(xbt_dynar_get_as(s_tokens2, 1, char *));
545       }
546       addr1 = (void *) strtoul(xbt_dynar_get_as(s_tokens1, 1, char *), NULL, 16);
547       addr2 = (void *) strtoul(xbt_dynar_get_as(s_tokens2, 1, char *), NULL, 16);
548       if(addr1 > std_heap && (char *)addr1 <= (char *)std_heap + STD_HEAP_SIZE && addr2 > std_heap && (char *)addr2 <= (char *)std_heap + STD_HEAP_SIZE){
549         res_compare = compare_area(addr1, addr2, NULL);
550         if(res_compare == 1){
551           if(is_stack_ignore_variable(ip1, xbt_dynar_get_as(s_tokens1, 0, char *)) && is_stack_ignore_variable(ip2, xbt_dynar_get_as(s_tokens2, 0, char *))){
552             xbt_dynar_free(&s_tokens1);
553             xbt_dynar_free(&s_tokens2);
554             cursor++;
555             continue;
556           }else {
557             #ifdef MC_VERBOSE
558               XBT_VERB("Different local variable : %s at addresses %p - %p", var_name, addr1, addr2);
559             #endif
560             xbt_dynar_free(&s_tokens1);
561             xbt_dynar_free(&s_tokens2);
562             xbt_dynar_free(&tokens1);
563             xbt_dynar_free(&tokens2);
564             xbt_free(ip1);
565             xbt_free(ip2);
566             return 1;
567           }
568         }
569       }
570     }
571     xbt_dynar_free(&s_tokens1);
572     xbt_dynar_free(&s_tokens2);
573          
574     cursor++;
575   }
576
577   xbt_free(ip1);
578   xbt_free(ip2);
579   xbt_dynar_free(&tokens1);
580   xbt_dynar_free(&tokens2);
581   return 0;
582   
583 }
584
585 static int is_heap_equality(xbt_dynar_t equals, void *a1, void *a2){
586
587   unsigned int cursor = 0;
588   int start = 0;
589   int end = xbt_dynar_length(equals) - 1;
590   heap_equality_t current_equality;
591
592   while(start <= end){
593     cursor = (start + end) / 2;
594     current_equality = (heap_equality_t)xbt_dynar_get_as(equals, cursor, heap_equality_t);
595     if(current_equality->address1 == a1){
596       if(current_equality->address2 == a2)
597         return 1;
598       if(current_equality->address2 < a2)
599         start = cursor + 1;
600       if(current_equality->address2 > a2)
601         end = cursor - 1;
602     }
603     if(current_equality->address1 < a1)
604       start = cursor + 1;
605     if(current_equality->address1 > a1)
606       end = cursor - 1; 
607   }
608
609   return 0;
610
611 }
612
613 int MC_compare_snapshots(void *s1, void *s2){
614   
615   MC_ignore_stack("self", "simcall_BODY_mc_snapshot");
616
617   return simcall_mc_compare_snapshots(s1, s2);
618
619 }
620
621 void print_comparison_times(){
622   XBT_DEBUG("*** Comparison times ***");
623   XBT_DEBUG("- Nb processes : %f", mc_comp_times->nb_processes_comparison_time);
624   XBT_DEBUG("- Nb bytes used : %f", mc_comp_times->bytes_used_comparison_time);
625   XBT_DEBUG("- Stacks sizes : %f", mc_comp_times->stacks_sizes_comparison_time);
626   XBT_DEBUG("- Binary global variables : %f", mc_comp_times->binary_global_variables_comparison_time);
627   XBT_DEBUG("- Libsimgrid global variables : %f", mc_comp_times->libsimgrid_global_variables_comparison_time);
628   XBT_DEBUG("- Heap : %f", mc_comp_times->heap_comparison_time);
629   XBT_DEBUG("- Stacks : %f", mc_comp_times->stacks_comparison_time);
630 }