1 /* Copyright (c) 2012-2013 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 compare_local_variables(char *s1, char *s2);
22 static int compare_global_variables(int region_type, void *d1, void *d2);
24 static size_t heap_ignore_size(void *address){
25 unsigned int cursor = 0;
27 int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
28 mc_heap_ignore_region_t region;
31 cursor = (start + end) / 2;
32 region = (mc_heap_ignore_region_t)xbt_dynar_get_as(mc_heap_comparison_ignore, cursor, mc_heap_ignore_region_t);
33 if(region->address == address)
35 if(region->address < address)
37 if(region->address > address)
44 static int compare_global_variables(int region_type, void *d1, void *d2){ /* region_type = 1 -> libsimgrid, region_type = 2 -> binary */
46 unsigned int cursor = 0;
49 global_variable_t current_var;
51 void *addr_pointed1 = NULL, *addr_pointed2 = NULL;
53 void *plt_start, *plt_end;
56 plt_start = start_plt_binary;
57 plt_end = end_plt_binary;
59 plt_start = start_plt_libsimgrid;
60 plt_end = end_plt_libsimgrid;
63 xbt_dynar_foreach(mc_global_variables, cursor, current_var){
64 if(current_var->address < start_data_libsimgrid){ /* binary global variable */
67 offset = (char *)current_var->address - (char *)start_data_binary;
68 }else{ /* libsimgrid global variable */
71 offset = (char *)current_var->address - (char *)start_data_libsimgrid;
74 while(i < current_var->size){
75 if(memcmp((char *)d1 + offset + i, (char *)d2 + offset + i, 1) != 0){
76 pointer_align = (i / sizeof(void*)) * sizeof(void*);
77 addr_pointed1 = *((void **)((char *)d1 + offset + pointer_align));
78 addr_pointed2 = *((void **)((char *)d2 + offset + pointer_align));
79 if((addr_pointed1 > plt_start && addr_pointed1 < plt_end) || (addr_pointed2 > plt_start && addr_pointed2 < plt_end)){
80 i = pointer_align + sizeof(void*);
82 }else if((addr_pointed1 > std_heap) && ((char *)addr_pointed1 < (char *)std_heap + STD_HEAP_SIZE)
83 && (addr_pointed2 > std_heap) && ((char *)addr_pointed2 < (char *)std_heap + STD_HEAP_SIZE)){
84 res_compare = compare_area(addr_pointed1, addr_pointed2, NULL);
88 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);
90 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);
94 XBT_DEBUG("Different global variable in binary : %s at addresses %p - %p (size = %zu)", current_var->name, (char *)d1+offset, (char *)d2+offset, current_var->size);
96 XBT_DEBUG("Different global variable in libsimgrid : %s at addresses %p - %p (size = %zu)", current_var->name, (char *)d1+offset, (char *)d2+offset, current_var->size);
100 i = pointer_align + sizeof(void*);
105 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);
107 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);
111 XBT_DEBUG("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_DEBUG("Different global variable in libsimgrid : %s at addresses %p - %p (size = %zu)", current_var->name, (char *)d1+offset, (char *)d2+offset, current_var->size);
125 static int heap_region_compare(void *d1, void *d2, size_t size){
129 for(i=0; i<size; i++){
130 if(memcmp(((char *)d1) + i, ((char *)d2) + i, 1) != 0){
131 if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_verbose)){
132 XBT_VERB("Different byte (offset=%zu) (%p - %p) in heap region", i, (char *)d1 + i, (char *)d2 + i);
141 static void stack_region_free(stack_region_t s){
143 xbt_free(s->process_name);
148 void stack_region_free_voidp(void *s){
149 stack_region_free((stack_region_t) * (void **) s);
152 static void heap_equality_free(heap_equality_t e){
156 void heap_equality_free_voidp(void *e){
157 heap_equality_free((heap_equality_t) * (void **) e);
160 int SIMIX_pre_mc_compare_snapshots(smx_simcall_t simcall,
161 mc_snapshot_t s1, mc_snapshot_t s2){
162 return snapshot_compare(s1, s2);
165 int snapshot_compare(mc_snapshot_t s1, mc_snapshot_t s2){
167 int raw_mem = (mmalloc_get_current_heap() == raw_heap);
173 xbt_os_timer_t global_timer = xbt_os_timer_new();
174 xbt_os_timer_t timer = xbt_os_timer_new();
176 xbt_os_walltimer_start(global_timer);
179 xbt_os_walltimer_start(timer);
182 /* Compare number of processes */
183 if(s1->nb_processes != s2->nb_processes){
185 xbt_os_walltimer_stop(timer);
186 mc_comp_times->nb_processes_comparison_time = xbt_os_timer_elapsed(timer);
187 XBT_DEBUG("Different number of processes : %d - %d", s1->nb_processes, s2->nb_processes);
191 XBT_VERB("Different number of processes : %d - %d", s1->nb_processes, s2->nb_processes);
194 xbt_os_timer_free(timer);
195 xbt_os_walltimer_stop(global_timer);
196 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
197 xbt_os_timer_free(global_timer);
207 xbt_os_walltimer_start(timer);
210 /* Compare number of bytes used in each heap */
211 if(s1->heap_bytes_used != s2->heap_bytes_used){
213 xbt_os_walltimer_stop(timer);
214 mc_comp_times->bytes_used_comparison_time = xbt_os_timer_elapsed(timer);
215 XBT_DEBUG("Different number of bytes used in each heap : %zu - %zu", s1->heap_bytes_used, s2->heap_bytes_used);
219 XBT_VERB("Different number of bytes used in each heap : %zu - %zu", s1->heap_bytes_used, s2->heap_bytes_used);
222 xbt_os_timer_free(timer);
223 xbt_os_walltimer_stop(global_timer);
224 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
225 xbt_os_timer_free(global_timer);
234 xbt_os_walltimer_stop(timer);
239 xbt_os_walltimer_start(timer);
242 /* Compare size of stacks */
244 size_t size_used1, size_used2;
246 while(i < xbt_dynar_length(s1->stacks)){
247 size_used1 = s1->stack_sizes[i];
248 size_used2 = s2->stack_sizes[i];
249 if(size_used1 != size_used2){
252 xbt_os_walltimer_stop(timer);
253 mc_comp_times->stacks_sizes_comparison_time = xbt_os_timer_elapsed(timer);
255 XBT_DEBUG("Different size used in stacks : %zu - %zu", size_used1, size_used2);
260 XBT_VERB("Different size used in stacks : %zu - %zu", size_used1, size_used2);
263 xbt_os_timer_free(timer);
264 xbt_os_walltimer_stop(global_timer);
265 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
266 xbt_os_timer_free(global_timer);
279 xbt_os_walltimer_stop(timer);
280 xbt_os_walltimer_start(timer);
283 /* Compare hash of global variables */
284 if(s1->hash_global != NULL && s2->hash_global != NULL){
285 if(strcmp(s1->hash_global, s2->hash_global) != 0){
287 xbt_os_walltimer_stop(timer);
288 mc_comp_times->hash_global_variables_comparison_time = xbt_os_timer_elapsed(timer);
289 XBT_DEBUG("Different hash of global variables : %s - %s", s1->hash_global, s2->hash_global);
293 XBT_VERB("Different hash of global variables : %s - %s", s1->hash_global, s2->hash_global);
296 xbt_os_timer_free(timer);
297 xbt_os_walltimer_stop(global_timer);
298 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
299 xbt_os_timer_free(global_timer);
310 xbt_os_walltimer_start(timer);
313 /* Compare hash of local variables */
314 if(s1->hash_local != NULL && s2->hash_local != NULL){
315 if(strcmp(s1->hash_local, s2->hash_local) != 0){
317 xbt_os_walltimer_stop(timer);
318 mc_comp_times->hash_local_variables_comparison_time = xbt_os_timer_elapsed(timer);
319 XBT_DEBUG("Different hash of local variables : %s - %s", s1->hash_local, s2->hash_local);
323 XBT_VERB("Different hash of local variables : %s - %s", s1->hash_local, s2->hash_local);
326 xbt_os_timer_free(timer);
327 xbt_os_walltimer_stop(global_timer);
328 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
329 xbt_os_timer_free(global_timer);
340 xbt_os_walltimer_start(timer);
343 /* Init heap information used in heap comparison algorithm */
344 init_heap_information((xbt_mheap_t)s1->regions[0]->data, (xbt_mheap_t)s2->regions[0]->data, s1->to_ignore, s2->to_ignore);
346 /* Compare binary global variables */
347 is_diff = compare_global_variables(2, s1->regions[2]->data, s2->regions[2]->data);
350 xbt_os_walltimer_stop(timer);
351 mc_comp_times->binary_global_variables_comparison_time = xbt_os_timer_elapsed(timer);
352 XBT_DEBUG("Different global variables in binary");
356 XBT_VERB("Different global variables in binary");
359 reset_heap_information();
360 xbt_os_timer_free(timer);
361 xbt_os_walltimer_stop(global_timer);
362 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
363 xbt_os_timer_free(global_timer);
374 xbt_os_walltimer_stop(timer);
375 xbt_os_walltimer_start(timer);
378 /* Compare libsimgrid global variables */
379 is_diff = compare_global_variables(1, s1->regions[1]->data, s2->regions[1]->data);
382 xbt_os_walltimer_stop(timer);
383 mc_comp_times->libsimgrid_global_variables_comparison_time = xbt_os_timer_elapsed(timer);
384 XBT_DEBUG("Different global variables in libsimgrid");
388 XBT_VERB("Different global variables in libsimgrid");
391 reset_heap_information();
392 xbt_os_timer_free(timer);
393 xbt_os_walltimer_stop(global_timer);
394 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
395 xbt_os_timer_free(global_timer);
406 xbt_os_walltimer_stop(timer);
407 xbt_os_walltimer_start(timer);
410 /* Stacks comparison */
411 unsigned int cursor = 0;
415 while(cursor < xbt_dynar_length(s1->stacks)){
416 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);
420 xbt_os_walltimer_stop(timer);
421 mc_comp_times->stacks_comparison_time = xbt_os_timer_elapsed(timer);
423 XBT_DEBUG("Different local variables between stacks %d", cursor + 1);
429 XBT_VERB("Different local variables between stacks %d", cursor + 1);
432 reset_heap_information();
433 xbt_os_timer_free(timer);
434 xbt_os_walltimer_stop(global_timer);
435 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
436 xbt_os_timer_free(global_timer);
448 xbt_os_walltimer_start(timer);
452 if(mmalloc_compare_heap((xbt_mheap_t)s1->regions[0]->data, (xbt_mheap_t)s2->regions[0]->data)){
455 xbt_os_walltimer_stop(timer);
456 mc_comp_times->heap_comparison_time = xbt_os_timer_elapsed(timer);
457 XBT_DEBUG("Different heap (mmalloc_compare)");
461 xbt_os_timer_free(timer);
464 XBT_VERB("Different heap (mmalloc_compare)");
467 reset_heap_information();
468 xbt_os_walltimer_stop(global_timer);
469 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
470 xbt_os_timer_free(global_timer);
479 xbt_os_walltimer_stop(timer);
483 reset_heap_information();
485 xbt_os_timer_free(timer);
488 xbt_os_walltimer_stop(global_timer);
489 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
492 xbt_os_timer_free(global_timer);
495 print_comparison_times();
505 int is_stack_ignore_variable(char *frame, char *var_name){
507 unsigned int cursor = 0;
509 int end = xbt_dynar_length(mc_stack_comparison_ignore) - 1;
510 mc_stack_ignore_variable_t current_var;
513 cursor = (start + end) / 2;
514 current_var = (mc_stack_ignore_variable_t)xbt_dynar_get_as(mc_stack_comparison_ignore, cursor, mc_stack_ignore_variable_t);
515 if(strcmp(current_var->frame, frame) == 0 || strcmp(current_var->frame, "*") == 0){
516 if(strcmp(current_var->var_name, var_name) == 0)
518 if(strcmp(current_var->var_name, var_name) < 0)
520 if(strcmp(current_var->var_name, var_name) > 0)
522 }else if(strcmp(current_var->frame, frame) < 0){
524 }else if(strcmp(current_var->frame, frame) > 0){
532 static int compare_local_variables(char *s1, char *s2){
534 xbt_dynar_t tokens1 = xbt_str_split(s1, NULL);
535 xbt_dynar_t tokens2 = xbt_str_split(s2, NULL);
537 xbt_dynar_t s_tokens1, s_tokens2;
538 unsigned int cursor = 0;
540 char *frame_name1 = NULL, *frame_name2 = NULL;
543 #if defined MC_VERBOSE || defined MC_DEBUG
547 while(cursor < xbt_dynar_length(tokens1)){
548 s_tokens1 = xbt_str_split(xbt_dynar_get_as(tokens1, cursor, char *), "=");
549 s_tokens2 = xbt_str_split(xbt_dynar_get_as(tokens2, cursor, char *), "=");
550 if(xbt_dynar_length(s_tokens1) > 1 && xbt_dynar_length(s_tokens2) > 1){
551 #if defined MC_VERBOSE || defined MC_DEBUG
552 var_name = xbt_dynar_get_as(s_tokens1, 0, char *);
554 if((strcmp(xbt_dynar_get_as(s_tokens1, 0, char *), "frame_name") == 0) && (strcmp(xbt_dynar_get_as(s_tokens2, 0, char *), "frame_name") == 0)){
555 xbt_free(frame_name1);
556 xbt_free(frame_name2);
557 frame_name1 = strdup(xbt_dynar_get_as(s_tokens1, 1, char *));
558 frame_name2 = strdup(xbt_dynar_get_as(s_tokens2, 1, char *));
560 addr1 = (void *) strtoul(xbt_dynar_get_as(s_tokens1, 1, char *), NULL, 16);
561 addr2 = (void *) strtoul(xbt_dynar_get_as(s_tokens2, 1, char *), NULL, 16);
562 if(addr1 > std_heap && (char *)addr1 <= (char *)std_heap + STD_HEAP_SIZE && addr2 > std_heap && (char *)addr2 <= (char *)std_heap + STD_HEAP_SIZE){
563 res_compare = compare_area(addr1, addr2, NULL);
564 if(res_compare == 1){
565 if(is_stack_ignore_variable(frame_name1, xbt_dynar_get_as(s_tokens1, 0, char *)) && is_stack_ignore_variable(frame_name2, xbt_dynar_get_as(s_tokens2, 0, char *))){
566 xbt_dynar_free(&s_tokens1);
567 xbt_dynar_free(&s_tokens2);
572 XBT_VERB("Different local variable : %s at addresses %p - %p in frame %s", var_name, addr1, addr2, frame_name1);
575 XBT_DEBUG("Different local variable : %s at addresses %p - %p", var_name, addr1, addr2);
577 xbt_dynar_free(&s_tokens1);
578 xbt_dynar_free(&s_tokens2);
579 xbt_dynar_free(&tokens1);
580 xbt_dynar_free(&tokens2);
581 xbt_free(frame_name1);
582 xbt_free(frame_name2);
587 if(strcmp(xbt_dynar_get_as(s_tokens1, 1, char *), xbt_dynar_get_as(s_tokens2, 1, char *)) != 0){
588 if(is_stack_ignore_variable(frame_name1, xbt_dynar_get_as(s_tokens1, 0, char *)) && is_stack_ignore_variable(frame_name2, xbt_dynar_get_as(s_tokens2, 0, char *))){
589 xbt_dynar_free(&s_tokens1);
590 xbt_dynar_free(&s_tokens2);
595 XBT_VERB("Different local variable : %s (%s - %s) in frame %s", var_name, xbt_dynar_get_as(s_tokens1, 1, char *), xbt_dynar_get_as(s_tokens2, 1, char *), frame_name1);
598 XBT_DEBUG("Different local variable : %s (%s - %s)", var_name, xbt_dynar_get_as(s_tokens1, 1, char *), xbt_dynar_get_as(s_tokens2, 1, char *));
601 xbt_dynar_free(&s_tokens1);
602 xbt_dynar_free(&s_tokens2);
603 xbt_dynar_free(&tokens1);
604 xbt_dynar_free(&tokens2);
605 xbt_free(frame_name1);
606 xbt_free(frame_name2);
612 xbt_dynar_free(&s_tokens1);
613 xbt_dynar_free(&s_tokens2);
618 xbt_free(frame_name1);
619 xbt_free(frame_name2);
620 xbt_dynar_free(&tokens1);
621 xbt_dynar_free(&tokens2);
626 static int is_heap_equality(xbt_dynar_t equals, void *a1, void *a2){
628 unsigned int cursor = 0;
630 int end = xbt_dynar_length(equals) - 1;
631 heap_equality_t current_equality;
634 cursor = (start + end) / 2;
635 current_equality = (heap_equality_t)xbt_dynar_get_as(equals, cursor, heap_equality_t);
636 if(current_equality->address1 == a1){
637 if(current_equality->address2 == a2)
639 if(current_equality->address2 < a2)
641 if(current_equality->address2 > a2)
644 if(current_equality->address1 < a1)
646 if(current_equality->address1 > a1)
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 bytes used : %f", mc_comp_times->bytes_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);