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 compare_stack(stack_region_t s1, stack_region_t s2, void *sp1, void *sp2, void *heap1, void *heap2, xbt_dynar_t equals);
16 static int is_heap_equality(xbt_dynar_t equals, void *a1, void *a2);
17 static size_t heap_ignore_size(void *address);
19 static void stack_region_free(stack_region_t s);
20 static void heap_equality_free(heap_equality_t e);
22 static int is_stack_ignore_variable(char *frame, char *var_name);
23 static int compare_local_variables(char *s1, char *s2, xbt_dynar_t heap_equals);
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, xbt_dynar_t equals){
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, equals);
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, equals);
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);
113 XBT_VERB("False pointer differences for variable : %s", current_var->name);
117 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);
132 static int heap_region_compare(void *d1, void *d2, size_t size){
136 for(i=0; i<size; i++){
137 if(memcmp(((char *)d1) + i, ((char *)d2) + i, 1) != 0){
138 if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_verbose)){
139 XBT_VERB("Different byte (offset=%zu) (%p - %p) in heap region", i, (char *)d1 + i, (char *)d2 + i);
148 static void stack_region_free(stack_region_t s){
150 xbt_free(s->process_name);
155 void stack_region_free_voidp(void *s){
156 stack_region_free((stack_region_t) * (void **) s);
159 static void heap_equality_free(heap_equality_t e){
165 void heap_equality_free_voidp(void *e){
166 heap_equality_free((heap_equality_t) * (void **) e);
169 int SIMIX_pre_mc_compare_snapshots(smx_simcall_t simcall,
170 mc_snapshot_t s1, mc_snapshot_t s2){
171 return snapshot_compare(s1, s2);
174 int get_heap_region_index(mc_snapshot_t s){
176 while(i < s->num_reg){
177 switch(s->regions[i]->type){
189 int snapshot_compare(mc_snapshot_t s1, mc_snapshot_t s2){
191 int raw_mem = (mmalloc_get_current_heap() == raw_heap);
197 xbt_os_timer_t global_timer = xbt_os_timer_new();
198 xbt_os_timer_t timer = xbt_os_timer_new();
200 xbt_os_timer_start(global_timer);
203 xbt_os_timer_start(timer);
206 /* Compare number of processes */
207 if(s1->nb_processes != s2->nb_processes){
209 xbt_os_timer_stop(timer);
210 mc_comp_times->nb_processes_comparison_time = xbt_os_timer_elapsed(timer);
211 XBT_DEBUG("Different number of processes : %d - %d", s1->nb_processes, s2->nb_processes);
215 XBT_VERB("Different number of processes : %d - %d", s1->nb_processes, s2->nb_processes);
218 xbt_os_timer_free(timer);
219 xbt_os_timer_stop(global_timer);
220 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
221 xbt_os_timer_free(global_timer);
230 /* Compare number of blocks/fragments used in each heap */
231 if(s1->heap_chunks_used != s2->heap_chunks_used){
233 xbt_os_timer_stop(timer);
234 mc_comp_times->chunks_used_comparison_time = xbt_os_timer_elapsed(timer);
235 XBT_DEBUG("Different number of chunks used in each heap : %zu - %zu", s1->heap_chunks_used, s2->heap_chunks_used);
239 XBT_VERB("Different number of chunks used in each heap : %zu - %zu", s1->heap_chunks_used, s2->heap_chunks_used);
242 xbt_os_timer_free(timer);
243 xbt_os_timer_stop(global_timer);
244 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
245 xbt_os_timer_free(global_timer);
254 xbt_os_timer_stop(timer);
259 xbt_os_timer_start(timer);
262 /* Compare size of stacks */
264 size_t size_used1, size_used2;
266 while(i < xbt_dynar_length(s1->stacks)){
267 size_used1 = s1->stack_sizes[i];
268 size_used2 = s2->stack_sizes[i];
269 if(size_used1 != size_used2){
272 xbt_os_timer_stop(timer);
273 mc_comp_times->stacks_sizes_comparison_time = xbt_os_timer_elapsed(timer);
275 XBT_DEBUG("Different size used in stacks : %zu - %zu", size_used1, size_used2);
280 XBT_VERB("Different size used in stacks : %zu - %zu", size_used1, size_used2);
283 xbt_os_timer_free(timer);
284 xbt_os_timer_stop(global_timer);
285 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
286 xbt_os_timer_free(global_timer);
299 xbt_os_timer_stop(timer);
300 xbt_os_timer_start(timer);
303 int heap_index = 0, data_libsimgrid_index = 0, data_program_index = 0;
306 /* Get index of regions */
307 while(i < s1->num_reg){
308 switch(s1->region_type[i]){
314 data_libsimgrid_index = i;
316 while( i < s1->num_reg && s1->region_type[i] == 1)
320 data_program_index = i;
322 while( i < s1->num_reg && s1->region_type[i] == 2)
328 /* Init heap information used in heap comparison algorithm */
329 init_heap_information((xbt_mheap_t)s1->regions[heap_index]->data, (xbt_mheap_t)s2->regions[heap_index]->data);
331 xbt_dynar_t equals = xbt_dynar_new(sizeof(heap_equality_t), heap_equality_free_voidp);
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, equals);
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, equals);
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);
396 xbt_dynar_t stacks1 = xbt_dynar_new(sizeof(stack_region_t), stack_region_free_voidp);
397 xbt_dynar_t stacks2 = xbt_dynar_new(sizeof(stack_region_t), stack_region_free_voidp);
399 void *heap1 = s1->regions[heap_index]->data, *heap2 = s2->regions[heap_index]->data;
401 if(mmalloc_compare_heap((xbt_mheap_t)s1->regions[heap_index]->data, (xbt_mheap_t)s2->regions[heap_index]->data, &stacks1, &stacks2, equals)){
404 xbt_os_timer_stop(timer);
405 mc_comp_times->heap_comparison_time = xbt_os_timer_elapsed(timer);
406 XBT_DEBUG("Different heap (mmalloc_compare)");
410 xbt_os_timer_free(timer);
411 xbt_dynar_free(&stacks1);
412 xbt_dynar_free(&stacks2);
413 xbt_dynar_free(&equals);
416 XBT_VERB("Different heap (mmalloc_compare)");
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);
430 xbt_os_timer_stop(timer);
435 xbt_os_timer_start(timer);
438 /* Stacks comparison */
439 unsigned int cursor = 0;
440 stack_region_t stack_region1, stack_region2;
441 int diff = 0, diff_local = 0;
445 while(cursor < xbt_dynar_length(stacks1)){
446 stack_region1 = (stack_region_t)(xbt_dynar_get_as(stacks1, cursor, stack_region_t));
447 stack_region2 = (stack_region_t)(xbt_dynar_get_as(stacks2, cursor, stack_region_t));
448 sp1 = ((mc_snapshot_stack_t)xbt_dynar_get_as(s1->stacks, cursor, mc_snapshot_stack_t))->stack_pointer;
449 sp2 = ((mc_snapshot_stack_t)xbt_dynar_get_as(s2->stacks, cursor, mc_snapshot_stack_t))->stack_pointer;
450 diff = compare_stack(stack_region1, stack_region2, sp1, sp2, heap1, heap2, equals);
452 if(diff > 0){ /* Differences may be due to padding */
453 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, equals);
457 xbt_os_timer_stop(timer);
458 mc_comp_times->stacks_comparison_time = xbt_os_timer_elapsed(timer);
460 XBT_DEBUG("Different local variables between stacks %d", cursor + 1);
464 xbt_dynar_free(&stacks1);
465 xbt_dynar_free(&stacks2);
466 xbt_dynar_free(&equals);
469 XBT_VERB("Different local variables between stacks %d", cursor + 1);
472 xbt_os_timer_free(timer);
473 xbt_os_timer_stop(global_timer);
474 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
475 xbt_os_timer_free(global_timer);
487 xbt_dynar_free(&stacks1);
488 xbt_dynar_free(&stacks2);
489 xbt_dynar_free(&equals);
491 xbt_os_timer_free(timer);
494 xbt_os_timer_stop(global_timer);
495 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
498 xbt_os_timer_free(global_timer);
501 print_comparison_times();
511 static int is_stack_ignore_variable(char *frame, char *var_name){
513 unsigned int cursor = 0;
515 int end = xbt_dynar_length(mc_stack_comparison_ignore) - 1;
516 mc_stack_ignore_variable_t current_var;
519 cursor = (start + end) / 2;
520 current_var = (mc_stack_ignore_variable_t)xbt_dynar_get_as(mc_stack_comparison_ignore, cursor, mc_stack_ignore_variable_t);
521 if(strcmp(current_var->frame, frame) == 0 || strcmp(current_var->frame, "*") == 0){
522 if(strcmp(current_var->var_name, var_name) == 0)
524 if(strcmp(current_var->var_name, var_name) < 0)
526 if(strcmp(current_var->var_name, var_name) > 0)
528 }else if(strcmp(current_var->frame, frame) < 0){
530 }else if(strcmp(current_var->frame, frame) > 0){
538 static int compare_local_variables(char *s1, char *s2, xbt_dynar_t heap_equals){
540 xbt_dynar_t tokens1 = xbt_str_split(s1, NULL);
541 xbt_dynar_t tokens2 = xbt_str_split(s2, NULL);
543 xbt_dynar_t s_tokens1, s_tokens2;
544 unsigned int cursor = 0;
546 char *ip1 = NULL, *ip2 = NULL;
548 while(cursor < xbt_dynar_length(tokens1)){
549 s_tokens1 = xbt_str_split(xbt_dynar_get_as(tokens1, cursor, char *), "=");
550 s_tokens2 = xbt_str_split(xbt_dynar_get_as(tokens2, cursor, char *), "=");
551 if(xbt_dynar_length(s_tokens1) > 1 && xbt_dynar_length(s_tokens2) > 1){
552 if((strcmp(xbt_dynar_get_as(s_tokens1, 0, char *), "ip") == 0) && (strcmp(xbt_dynar_get_as(s_tokens2, 0, char *), "ip") == 0)){
555 ip1 = strdup(xbt_dynar_get_as(s_tokens1, 1, char *));
556 ip2 = strdup(xbt_dynar_get_as(s_tokens2, 1, char *));
558 if(strcmp(xbt_dynar_get_as(s_tokens1, 1, char *), xbt_dynar_get_as(s_tokens2, 1, char *)) != 0){
559 /* Ignore this variable ? */
560 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 *))){
561 xbt_dynar_free(&s_tokens1);
562 xbt_dynar_free(&s_tokens2);
566 addr1 = (void *) strtoul(xbt_dynar_get_as(s_tokens1, 1, char *), NULL, 16);
567 addr2 = (void *) strtoul(xbt_dynar_get_as(s_tokens2, 1, char *), NULL, 16);
568 if(is_heap_equality(heap_equals, addr1, addr2) == 0){
569 if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_verbose))
570 XBT_VERB("Variable %s is different between stacks in %s : %s - %s", xbt_dynar_get_as(s_tokens1, 0, char *), ip1, xbt_dynar_get_as(s_tokens1, 1, char *), xbt_dynar_get_as(s_tokens2, 1, char *));
571 xbt_dynar_free(&s_tokens1);
572 xbt_dynar_free(&s_tokens2);
573 xbt_dynar_free(&tokens1);
574 xbt_dynar_free(&tokens2);
581 xbt_dynar_free(&s_tokens1);
582 xbt_dynar_free(&s_tokens2);
589 xbt_dynar_free(&tokens1);
590 xbt_dynar_free(&tokens2);
595 static int is_heap_equality(xbt_dynar_t equals, void *a1, void *a2){
597 unsigned int cursor = 0;
599 int end = xbt_dynar_length(equals) - 1;
600 heap_equality_t current_equality;
603 cursor = (start + end) / 2;
604 current_equality = (heap_equality_t)xbt_dynar_get_as(equals, cursor, heap_equality_t);
605 if(current_equality->address1 == a1){
606 if(current_equality->address2 == a2)
608 if(current_equality->address2 < a2)
610 if(current_equality->address2 > a2)
613 if(current_equality->address1 < a1)
615 if(current_equality->address1 > a1)
624 static int compare_stack(stack_region_t s1, stack_region_t s2, void *sp1, void *sp2, void *heap1, void *heap2, xbt_dynar_t equals){
627 size_t size_used1 = s1->size - ((char*)sp1 - (char*)s1->address);
628 size_t size_used2 = s2->size - ((char*)sp2 - (char*)s2->address);
631 void *addr_pointed1 = NULL, *addr_pointed2 = NULL;
633 while(k < size_used1){
634 if(memcmp((char *)s1->address + s1->size - k, (char *)s2->address + s2->size - k, 1) != 0){
635 pointer_align = ((size_used1 - k) / sizeof(void*)) * sizeof(void*);
636 addr_pointed1 = *((void **)(((char*)s1->address + (s1->size - size_used1)) + pointer_align));
637 addr_pointed2 = *((void **)(((char*)s2->address + (s2->size - size_used2)) + pointer_align));
638 if(is_heap_equality(equals, addr_pointed1, addr_pointed2) == 0){
639 if((addr_pointed1 > std_heap) && (addr_pointed1 < (void *)((char *)std_heap + STD_HEAP_SIZE)) && (addr_pointed2 > std_heap) && (addr_pointed2 < (void *)((char *)std_heap + STD_HEAP_SIZE))){
640 if(is_free_area(addr_pointed1, (xbt_mheap_t)heap1) == 0 || is_free_area(addr_pointed2, (xbt_mheap_t)heap2) == 0){
654 int MC_compare_snapshots(void *s1, void *s2){
656 MC_ignore_stack("self", "simcall_BODY_mc_snapshot");
658 return simcall_mc_compare_snapshots(s1, s2);
662 void print_comparison_times(){
663 XBT_DEBUG("*** Comparison times ***");
664 XBT_DEBUG("- Nb processes : %f", mc_comp_times->nb_processes_comparison_time);
665 XBT_DEBUG("- Nb chunks used : %f", mc_comp_times->chunks_used_comparison_time);
666 XBT_DEBUG("- Stacks sizes : %f", mc_comp_times->stacks_sizes_comparison_time);
667 XBT_DEBUG("- Binary global variables : %f", mc_comp_times->binary_global_variables_comparison_time);
668 XBT_DEBUG("- Libsimgrid global variables : %f", mc_comp_times->libsimgrid_global_variables_comparison_time);
669 XBT_DEBUG("- Heap : %f", mc_comp_times->heap_comparison_time);
670 XBT_DEBUG("- Stacks : %f", mc_comp_times->stacks_comparison_time);