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)){
82 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);
98 XBT_INFO("Different global variable (%p, %p) : %s at addresses %p - %p (size = %zu)", current_var->address, addr_pointed1, current_var->name, (char *)d1+offset, (char *)d2+offset, current_var->size);
104 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);
106 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);
110 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);
112 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);
114 XBT_INFO("Different global variable (%p, %p) : %s at addresses %p - %p (size = %zu)", current_var->address, addr_pointed1, current_var->name, (char *)d1+offset, (char *)d2+offset, current_var->size);
127 static int heap_region_compare(void *d1, void *d2, size_t size){
131 for(i=0; i<size; i++){
132 if(memcmp(((char *)d1) + i, ((char *)d2) + i, 1) != 0){
133 if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_verbose)){
134 XBT_VERB("Different byte (offset=%zu) (%p - %p) in heap region", i, (char *)d1 + i, (char *)d2 + i);
143 static void stack_region_free(stack_region_t s){
145 xbt_free(s->process_name);
150 void stack_region_free_voidp(void *s){
151 stack_region_free((stack_region_t) * (void **) s);
154 static void heap_equality_free(heap_equality_t e){
158 void heap_equality_free_voidp(void *e){
159 heap_equality_free((heap_equality_t) * (void **) e);
162 int SIMIX_pre_mc_compare_snapshots(smx_simcall_t simcall,
163 mc_snapshot_t s1, mc_snapshot_t s2){
164 return snapshot_compare(s1, s2);
167 int snapshot_compare(mc_snapshot_t s1, mc_snapshot_t s2){
169 int raw_mem = (mmalloc_get_current_heap() == raw_heap);
175 xbt_os_timer_t global_timer = xbt_os_timer_new();
176 xbt_os_timer_t timer = xbt_os_timer_new();
178 xbt_os_timer_start(global_timer);
181 xbt_os_timer_start(timer);
184 /* Compare number of processes */
185 if(s1->nb_processes != s2->nb_processes){
187 xbt_os_timer_stop(timer);
188 mc_comp_times->nb_processes_comparison_time = xbt_os_timer_elapsed(timer);
189 XBT_DEBUG("Different number of processes : %d - %d", s1->nb_processes, s2->nb_processes);
193 XBT_VERB("Different number of processes : %d - %d", s1->nb_processes, s2->nb_processes);
196 xbt_os_timer_free(timer);
197 xbt_os_timer_stop(global_timer);
198 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
199 xbt_os_timer_free(global_timer);
209 xbt_os_timer_start(timer);
212 /* Compare number of bytes used in each heap */
213 if(s1->heap_bytes_used != s2->heap_bytes_used){
215 xbt_os_timer_stop(timer);
216 mc_comp_times->bytes_used_comparison_time = xbt_os_timer_elapsed(timer);
217 XBT_DEBUG("Different number of bytes used in each heap : %zu - %zu", s1->heap_bytes_used, s2->heap_bytes_used);
221 XBT_VERB("Different number of bytes used in each heap : %zu - %zu", s1->heap_bytes_used, s2->heap_bytes_used);
224 xbt_os_timer_free(timer);
225 xbt_os_timer_stop(global_timer);
226 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
227 xbt_os_timer_free(global_timer);
236 xbt_os_timer_stop(timer);
241 xbt_os_timer_start(timer);
244 /* Compare size of stacks */
246 size_t size_used1, size_used2;
248 while(i < xbt_dynar_length(s1->stacks)){
249 size_used1 = s1->stack_sizes[i];
250 size_used2 = s2->stack_sizes[i];
251 if(size_used1 != size_used2){
254 xbt_os_timer_stop(timer);
255 mc_comp_times->stacks_sizes_comparison_time = xbt_os_timer_elapsed(timer);
257 XBT_DEBUG("Different size used in stacks : %zu - %zu", size_used1, size_used2);
262 XBT_VERB("Different size used in stacks : %zu - %zu", size_used1, size_used2);
265 xbt_os_timer_free(timer);
266 xbt_os_timer_stop(global_timer);
267 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
268 xbt_os_timer_free(global_timer);
281 xbt_os_timer_stop(timer);
282 xbt_os_timer_start(timer);
285 /* Compare hash of global variables */
286 if(s1->hash_global != NULL && s2->hash_global != NULL){
287 if(strcmp(s1->hash_global, s2->hash_global) != 0){
289 xbt_os_timer_stop(timer);
290 mc_comp_times->hash_global_variables_comparison_time = xbt_os_timer_elapsed(timer);
291 XBT_DEBUG("Different hash of global variables : %s - %s", s1->hash_global, s2->hash_global);
295 XBT_VERB("Different hash of global variables : %s - %s", s1->hash_global, s2->hash_global);
298 xbt_os_timer_free(timer);
299 xbt_os_timer_stop(global_timer);
300 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
301 xbt_os_timer_free(global_timer);
312 xbt_os_timer_start(timer);
315 /* Compare hash of local variables */
316 if(s1->hash_local != NULL && s2->hash_local != NULL){
317 if(strcmp(s1->hash_local, s2->hash_local) != 0){
319 xbt_os_timer_stop(timer);
320 mc_comp_times->hash_local_variables_comparison_time = xbt_os_timer_elapsed(timer);
321 XBT_DEBUG("Different hash of local variables : %s - %s", s1->hash_local, s2->hash_local);
325 XBT_VERB("Different hash of local variables : %s - %s", s1->hash_local, s2->hash_local);
328 xbt_os_timer_free(timer);
329 xbt_os_timer_stop(global_timer);
330 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
331 xbt_os_timer_free(global_timer);
342 xbt_os_timer_start(timer);
345 /* Init heap information used in heap comparison algorithm */
346 init_heap_information((xbt_mheap_t)s1->regions[0]->data, (xbt_mheap_t)s2->regions[0]->data, s1->to_ignore, s2->to_ignore);
348 /* Compare binary global variables */
349 is_diff = compare_global_variables(2, s1->regions[2]->data, s2->regions[2]->data);
352 xbt_os_timer_stop(timer);
353 mc_comp_times->binary_global_variables_comparison_time = xbt_os_timer_elapsed(timer);
354 XBT_DEBUG("Different global variables in binary");
358 XBT_VERB("Different global variables in binary");
361 reset_heap_information();
362 xbt_os_timer_free(timer);
363 xbt_os_timer_stop(global_timer);
364 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
365 xbt_os_timer_free(global_timer);
376 xbt_os_timer_stop(timer);
377 xbt_os_timer_start(timer);
380 /* Compare libsimgrid global variables */
381 is_diff = compare_global_variables(1, s1->regions[1]->data, s2->regions[1]->data);
384 xbt_os_timer_stop(timer);
385 mc_comp_times->libsimgrid_global_variables_comparison_time = xbt_os_timer_elapsed(timer);
386 XBT_DEBUG("Different global variables in libsimgrid");
390 XBT_VERB("Different global variables in libsimgrid");
393 reset_heap_information();
394 xbt_os_timer_free(timer);
395 xbt_os_timer_stop(global_timer);
396 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
397 xbt_os_timer_free(global_timer);
408 xbt_os_timer_stop(timer);
409 xbt_os_timer_start(timer);
412 /* Stacks comparison */
413 unsigned int cursor = 0;
417 while(cursor < xbt_dynar_length(s1->stacks)){
418 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);
422 xbt_os_timer_stop(timer);
423 mc_comp_times->stacks_comparison_time = xbt_os_timer_elapsed(timer);
425 XBT_DEBUG("Different local variables between stacks %d", cursor + 1);
431 XBT_VERB("Different local variables between stacks %d", cursor + 1);
434 reset_heap_information();
435 xbt_os_timer_free(timer);
436 xbt_os_timer_stop(global_timer);
437 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
438 xbt_os_timer_free(global_timer);
450 xbt_os_timer_start(timer);
455 if(mmalloc_compare_heap((xbt_mheap_t)s1->regions[0]->data, (xbt_mheap_t)s2->regions[0]->data)){
458 xbt_os_timer_stop(timer);
459 mc_comp_times->heap_comparison_time = xbt_os_timer_elapsed(timer);
460 XBT_DEBUG("Different heap (mmalloc_compare)");
464 xbt_os_timer_free(timer);
467 XBT_VERB("Different heap (mmalloc_compare)");
470 reset_heap_information();
471 xbt_os_timer_stop(global_timer);
472 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
473 xbt_os_timer_free(global_timer);
482 xbt_os_timer_stop(timer);
486 reset_heap_information();
488 xbt_os_timer_free(timer);
491 xbt_os_timer_stop(global_timer);
492 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
495 xbt_os_timer_free(global_timer);
498 print_comparison_times();
508 int is_stack_ignore_variable(char *frame, char *var_name){
510 unsigned int cursor = 0;
512 int end = xbt_dynar_length(mc_stack_comparison_ignore) - 1;
513 mc_stack_ignore_variable_t current_var;
516 cursor = (start + end) / 2;
517 current_var = (mc_stack_ignore_variable_t)xbt_dynar_get_as(mc_stack_comparison_ignore, cursor, mc_stack_ignore_variable_t);
518 if(strcmp(current_var->frame, frame) == 0 || strcmp(current_var->frame, "*") == 0){
519 if(strcmp(current_var->var_name, var_name) == 0)
521 if(strcmp(current_var->var_name, var_name) < 0)
523 if(strcmp(current_var->var_name, var_name) > 0)
525 }else if(strcmp(current_var->frame, frame) < 0){
527 }else if(strcmp(current_var->frame, frame) > 0){
535 static int compare_local_variables(char *s1, char *s2){
537 xbt_dynar_t tokens1 = xbt_str_split(s1, NULL);
538 xbt_dynar_t tokens2 = xbt_str_split(s2, NULL);
540 xbt_dynar_t s_tokens1, s_tokens2;
541 unsigned int cursor = 0;
543 char *frame_name1 = NULL, *frame_name2 = NULL;
546 #if defined MC_VERBOSE || defined MC_DEBUG
550 while(cursor < xbt_dynar_length(tokens1)){
551 s_tokens1 = xbt_str_split(xbt_dynar_get_as(tokens1, cursor, char *), "=");
552 s_tokens2 = xbt_str_split(xbt_dynar_get_as(tokens2, cursor, char *), "=");
553 if(xbt_dynar_length(s_tokens1) > 1 && xbt_dynar_length(s_tokens2) > 1){
554 #if defined MC_VERBOSE || defined MC_DEBUG
555 var_name = xbt_dynar_get_as(s_tokens1, 0, char *);
557 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)){
558 xbt_free(frame_name1);
559 xbt_free(frame_name2);
560 frame_name1 = strdup(xbt_dynar_get_as(s_tokens1, 1, char *));
561 frame_name2 = strdup(xbt_dynar_get_as(s_tokens2, 1, char *));
563 addr1 = (void *) strtoul(xbt_dynar_get_as(s_tokens1, 1, char *), NULL, 16);
564 addr2 = (void *) strtoul(xbt_dynar_get_as(s_tokens2, 1, char *), NULL, 16);
565 if(addr1 > std_heap && (char *)addr1 <= (char *)std_heap + STD_HEAP_SIZE && addr2 > std_heap && (char *)addr2 <= (char *)std_heap + STD_HEAP_SIZE){
566 res_compare = compare_area(addr1, addr2, NULL);
567 if(res_compare == 1){
568 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 *))){
569 xbt_dynar_free(&s_tokens1);
570 xbt_dynar_free(&s_tokens2);
575 XBT_VERB("Different local variable : %s at addresses %p - %p", var_name, addr1, addr2);
578 XBT_DEBUG("Different local variable : %s at addresses %p - %p", var_name, addr1, addr2);
580 xbt_dynar_free(&s_tokens1);
581 xbt_dynar_free(&s_tokens2);
582 xbt_dynar_free(&tokens1);
583 xbt_dynar_free(&tokens2);
584 xbt_free(frame_name1);
585 xbt_free(frame_name2);
590 if(strcmp(xbt_dynar_get_as(s_tokens1, 1, char *), xbt_dynar_get_as(s_tokens2, 1, char *)) != 0){
591 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 *))){
592 xbt_dynar_free(&s_tokens1);
593 xbt_dynar_free(&s_tokens2);
598 XBT_VERB("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_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 *));
604 xbt_dynar_free(&s_tokens1);
605 xbt_dynar_free(&s_tokens2);
606 xbt_dynar_free(&tokens1);
607 xbt_dynar_free(&tokens2);
608 xbt_free(frame_name1);
609 xbt_free(frame_name2);
615 xbt_dynar_free(&s_tokens1);
616 xbt_dynar_free(&s_tokens2);
621 xbt_free(frame_name1);
622 xbt_free(frame_name2);
623 xbt_dynar_free(&tokens1);
624 xbt_dynar_free(&tokens2);
629 static int is_heap_equality(xbt_dynar_t equals, void *a1, void *a2){
631 unsigned int cursor = 0;
633 int end = xbt_dynar_length(equals) - 1;
634 heap_equality_t current_equality;
637 cursor = (start + end) / 2;
638 current_equality = (heap_equality_t)xbt_dynar_get_as(equals, cursor, heap_equality_t);
639 if(current_equality->address1 == a1){
640 if(current_equality->address2 == a2)
642 if(current_equality->address2 < a2)
644 if(current_equality->address2 > a2)
647 if(current_equality->address1 < a1)
649 if(current_equality->address1 > a1)
657 int MC_compare_snapshots(void *s1, void *s2){
659 MC_ignore_stack("self", "simcall_BODY_mc_snapshot");
661 return simcall_mc_compare_snapshots(s1, s2);
665 void print_comparison_times(){
666 XBT_DEBUG("*** Comparison times ***");
667 XBT_DEBUG("- Nb processes : %f", mc_comp_times->nb_processes_comparison_time);
668 XBT_DEBUG("- Nb bytes used : %f", mc_comp_times->bytes_used_comparison_time);
669 XBT_DEBUG("- Stacks sizes : %f", mc_comp_times->stacks_sizes_comparison_time);
670 XBT_DEBUG("- Binary global variables : %f", mc_comp_times->binary_global_variables_comparison_time);
671 XBT_DEBUG("- Libsimgrid global variables : %f", mc_comp_times->libsimgrid_global_variables_comparison_time);
672 XBT_DEBUG("- Heap : %f", mc_comp_times->heap_comparison_time);
673 XBT_DEBUG("- Stacks : %f", mc_comp_times->stacks_comparison_time);