1 /* Copyright (c) 2008-2012 Da SimGrid Team. All rights reserved. */
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. */
6 #include "mc_private.h"
8 #include "xbt/mmalloc.h"
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, mc,
11 "Logging specific to mc_compare");
13 static int heap_region_compare(void *d1, void *d2, size_t size);
15 static int is_heap_equality(xbt_dynar_t equals, void *a1, void *a2);
16 static size_t heap_ignore_size(void *address);
18 static void stack_region_free(stack_region_t s);
19 static void heap_equality_free(heap_equality_t e);
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);
25 static size_t heap_ignore_size(void *address){
26 unsigned int cursor = 0;
28 int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
29 mc_heap_ignore_region_t region;
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)
36 if(region->address < address)
38 if(region->address > address)
45 static int compare_global_variables(int region_type, void *d1, void *d2){
47 unsigned int cursor = 0;
50 global_variable_t current_var;
52 void *addr_pointed1 = NULL, *addr_pointed2 = NULL;
55 if(region_type == 1){ /* libsimgrid */
56 xbt_dynar_foreach(mc_global_variables, cursor, current_var){
57 if(current_var->address < start_data_libsimgrid)
59 offset = (char *)current_var->address - (char *)start_data_libsimgrid;
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;
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);
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);
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);
91 xbt_dynar_foreach(mc_global_variables, cursor, current_var){
92 if(current_var->address > start_data_libsimgrid)
94 offset = (char *)current_var->address - (char *)start_data_binary;
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;
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){
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);
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);
130 static int heap_region_compare(void *d1, void *d2, size_t size){
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);
146 static void stack_region_free(stack_region_t s){
148 xbt_free(s->process_name);
153 void stack_region_free_voidp(void *s){
154 stack_region_free((stack_region_t) * (void **) s);
157 static void heap_equality_free(heap_equality_t e){
163 void heap_equality_free_voidp(void *e){
164 heap_equality_free((heap_equality_t) * (void **) e);
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);
172 int get_heap_region_index(mc_snapshot_t s){
174 while(i < s->num_reg){
175 switch(s->regions[i]->type){
187 int snapshot_compare(mc_snapshot_t s1, mc_snapshot_t s2){
189 int raw_mem = (mmalloc_get_current_heap() == raw_heap);
195 xbt_os_timer_t global_timer = xbt_os_timer_new();
196 xbt_os_timer_t timer = xbt_os_timer_new();
198 xbt_os_timer_start(global_timer);
201 xbt_os_timer_start(timer);
204 /* Compare number of processes */
205 if(s1->nb_processes != s2->nb_processes){
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);
213 XBT_VERB("Different number of processes : %d - %d", s1->nb_processes, s2->nb_processes);
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);
229 xbt_os_timer_start(timer);
232 /* Compare number of bytes used in each heap */
233 if(s1->heap_bytes_used != s2->heap_bytes_used){
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);
241 XBT_VERB("Different number of bytes used in each heap : %zu - %zu", s1->heap_bytes_used, s2->heap_bytes_used);
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);
256 xbt_os_timer_stop(timer);
261 xbt_os_timer_start(timer);
264 /* Compare size of stacks */
266 size_t size_used1, size_used2;
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){
274 xbt_os_timer_stop(timer);
275 mc_comp_times->stacks_sizes_comparison_time = xbt_os_timer_elapsed(timer);
277 XBT_DEBUG("Different size used in stacks : %zu - %zu", size_used1, size_used2);
282 XBT_VERB("Different size used in stacks : %zu - %zu", size_used1, size_used2);
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);
301 xbt_os_timer_stop(timer);
302 xbt_os_timer_start(timer);
305 int heap_index = 0, data_libsimgrid_index = 0, data_program_index = 0;
308 /* Get index of regions */
309 while(i < s1->num_reg){
310 switch(s1->region_type[i]){
316 data_libsimgrid_index = i;
318 while( i < s1->num_reg && s1->region_type[i] == 1)
322 data_program_index = i;
324 while( i < s1->num_reg && s1->region_type[i] == 2)
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);
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);
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");
343 XBT_VERB("Different global variables in binary");
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);
360 xbt_os_timer_stop(timer);
361 xbt_os_timer_start(timer);
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);
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");
374 XBT_VERB("Different global variables in libsimgrid");
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);
391 xbt_os_timer_stop(timer);
392 xbt_os_timer_start(timer);
395 /* Stacks comparison */
396 unsigned int cursor = 0;
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);
405 xbt_os_timer_stop(timer);
406 mc_comp_times->stacks_comparison_time = xbt_os_timer_elapsed(timer);
408 XBT_DEBUG("Different local variables between stacks %d", cursor + 1);
414 XBT_VERB("Different local variables between stacks %d", cursor + 1);
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);
433 xbt_os_timer_start(timer);
438 if(mmalloc_compare_heap((xbt_mheap_t)s1->regions[heap_index]->data, (xbt_mheap_t)s2->regions[heap_index]->data)){
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)");
447 xbt_os_timer_free(timer);
450 XBT_VERB("Different heap (mmalloc_compare)");
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);
465 xbt_os_timer_stop(timer);
469 reset_heap_information();
471 xbt_os_timer_free(timer);
474 xbt_os_timer_stop(global_timer);
475 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
478 xbt_os_timer_free(global_timer);
481 print_comparison_times();
491 static int is_stack_ignore_variable(char *frame, char *var_name){
493 unsigned int cursor = 0;
495 int end = xbt_dynar_length(mc_stack_comparison_ignore) - 1;
496 mc_stack_ignore_variable_t current_var;
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)
504 if(strcmp(current_var->var_name, var_name) < 0)
506 if(strcmp(current_var->var_name, var_name) > 0)
508 }else if(strcmp(current_var->frame, frame) < 0){
510 }else if(strcmp(current_var->frame, frame) > 0){
518 static int compare_local_variables(char *s1, char *s2){
520 xbt_dynar_t tokens1 = xbt_str_split(s1, NULL);
521 xbt_dynar_t tokens2 = xbt_str_split(s2, NULL);
523 xbt_dynar_t s_tokens1, s_tokens2;
524 unsigned int cursor = 0;
526 char *ip1 = NULL, *ip2 = NULL;
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){
538 var_name = xbt_dynar_get_as(s_tokens1, 0, char *);
540 if((strcmp(xbt_dynar_get_as(s_tokens1, 0, char *), "ip") == 0) && (strcmp(xbt_dynar_get_as(s_tokens2, 0, char *), "ip") == 0)){
543 ip1 = strdup(xbt_dynar_get_as(s_tokens1, 1, char *));
544 ip2 = strdup(xbt_dynar_get_as(s_tokens2, 1, char *));
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);
558 XBT_VERB("Different local variable : %s at addresses %p - %p", var_name, addr1, addr2);
560 xbt_dynar_free(&s_tokens1);
561 xbt_dynar_free(&s_tokens2);
562 xbt_dynar_free(&tokens1);
563 xbt_dynar_free(&tokens2);
571 xbt_dynar_free(&s_tokens1);
572 xbt_dynar_free(&s_tokens2);
579 xbt_dynar_free(&tokens1);
580 xbt_dynar_free(&tokens2);
585 static int is_heap_equality(xbt_dynar_t equals, void *a1, void *a2){
587 unsigned int cursor = 0;
589 int end = xbt_dynar_length(equals) - 1;
590 heap_equality_t current_equality;
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)
598 if(current_equality->address2 < a2)
600 if(current_equality->address2 > a2)
603 if(current_equality->address1 < a1)
605 if(current_equality->address1 > a1)
613 int MC_compare_snapshots(void *s1, void *s2){
615 MC_ignore_stack("self", "simcall_BODY_mc_snapshot");
617 return simcall_mc_compare_snapshots(s1, s2);
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);