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)
67 || (addr_pointed2 > start_plt_libsimgrid && addr_pointed2 < end_plt_libsimgrid)){
68 i = current_var->size;
71 if((addr_pointed1 > std_heap) && ((char *)addr_pointed1 < (char *)std_heap + STD_HEAP_SIZE)
72 && (addr_pointed2 > std_heap) && ((char *)addr_pointed2 < (char *)std_heap + STD_HEAP_SIZE)){
73 res_compare = compare_area(addr_pointed1, addr_pointed2, NULL);
76 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);
82 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);
93 xbt_dynar_foreach(mc_global_variables, cursor, current_var){
94 if(current_var->address > start_data_libsimgrid)
96 offset = (char *)current_var->address - (char *)start_data_binary;
98 while(i < current_var->size){
99 if(memcmp((char*)d1 + offset + i, (char*)d2 + offset + i, 1) != 0){
100 pointer_align = (i / sizeof(void*)) * sizeof(void*);
101 addr_pointed1 = *((void **)((char *)d1 + offset + pointer_align));
102 addr_pointed2 = *((void **)((char *)d2 + offset + pointer_align));
103 if((addr_pointed1 > start_plt_binary && addr_pointed1 < end_plt_binary) || (addr_pointed2 > start_plt_binary && addr_pointed2 < end_plt_binary)){
104 i = current_var->size;
107 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)){
108 res_compare = compare_area(addr_pointed1, addr_pointed2, NULL);
109 if(res_compare == 1){
111 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);
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){
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 snapshot_compare(mc_snapshot_t s1, mc_snapshot_t s2){
174 int raw_mem = (mmalloc_get_current_heap() == raw_heap);
180 xbt_os_timer_t global_timer = xbt_os_timer_new();
181 xbt_os_timer_t timer = xbt_os_timer_new();
183 xbt_os_timer_start(global_timer);
186 xbt_os_timer_start(timer);
189 /* Compare number of processes */
190 if(s1->nb_processes != s2->nb_processes){
192 xbt_os_timer_stop(timer);
193 mc_comp_times->nb_processes_comparison_time = xbt_os_timer_elapsed(timer);
194 XBT_DEBUG("Different number of processes : %d - %d", s1->nb_processes, s2->nb_processes);
198 XBT_VERB("Different number of processes : %d - %d", s1->nb_processes, s2->nb_processes);
201 xbt_os_timer_free(timer);
202 xbt_os_timer_stop(global_timer);
203 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
204 xbt_os_timer_free(global_timer);
214 xbt_os_timer_start(timer);
217 /* Compare number of bytes used in each heap */
218 if(s1->heap_bytes_used != s2->heap_bytes_used){
220 xbt_os_timer_stop(timer);
221 mc_comp_times->bytes_used_comparison_time = xbt_os_timer_elapsed(timer);
222 XBT_DEBUG("Different number of bytes used in each heap : %zu - %zu", s1->heap_bytes_used, s2->heap_bytes_used);
226 XBT_VERB("Different number of bytes used in each heap : %zu - %zu", s1->heap_bytes_used, s2->heap_bytes_used);
229 xbt_os_timer_free(timer);
230 xbt_os_timer_stop(global_timer);
231 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
232 xbt_os_timer_free(global_timer);
241 xbt_os_timer_stop(timer);
246 xbt_os_timer_start(timer);
249 /* Compare size of stacks */
251 size_t size_used1, size_used2;
253 while(i < xbt_dynar_length(s1->stacks)){
254 size_used1 = s1->stack_sizes[i];
255 size_used2 = s2->stack_sizes[i];
256 if(size_used1 != size_used2){
259 xbt_os_timer_stop(timer);
260 mc_comp_times->stacks_sizes_comparison_time = xbt_os_timer_elapsed(timer);
262 XBT_DEBUG("Different size used in stacks : %zu - %zu", size_used1, size_used2);
267 XBT_VERB("Different size used in stacks : %zu - %zu", size_used1, size_used2);
270 xbt_os_timer_free(timer);
271 xbt_os_timer_stop(global_timer);
272 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
273 xbt_os_timer_free(global_timer);
286 xbt_os_timer_stop(timer);
287 xbt_os_timer_start(timer);
290 /* Compare hash of global variables */
291 if(s1->hash_global != NULL && s2->hash_global != NULL){
292 if(strcmp(s1->hash_global, s2->hash_global) != 0){
294 xbt_os_timer_stop(timer);
295 mc_comp_times->hash_global_variables_comparison_time = xbt_os_timer_elapsed(timer);
296 XBT_DEBUG("Different hash of global variables : %s - %s", s1->hash_global, s2->hash_global);
300 XBT_VERB("Different hash of global variables : %s - %s", s1->hash_global, s2->hash_global);
303 xbt_os_timer_free(timer);
304 xbt_os_timer_stop(global_timer);
305 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
306 xbt_os_timer_free(global_timer);
317 xbt_os_timer_start(timer);
320 /* Compare hash of local variables */
321 if(s1->hash_local != NULL && s2->hash_local != NULL){
322 if(strcmp(s1->hash_local, s2->hash_local) != 0){
324 xbt_os_timer_stop(timer);
325 mc_comp_times->hash_local_variables_comparison_time = xbt_os_timer_elapsed(timer);
326 XBT_DEBUG("Different hash of local variables : %s - %s", s1->hash_local, s2->hash_local);
330 XBT_VERB("Different hash of local variables : %s - %s", s1->hash_local, s2->hash_local);
333 xbt_os_timer_free(timer);
334 xbt_os_timer_stop(global_timer);
335 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
336 xbt_os_timer_free(global_timer);
347 xbt_os_timer_start(timer);
350 /* Init heap information used in heap comparison algorithm */
351 init_heap_information((xbt_mheap_t)s1->regions[0]->data, (xbt_mheap_t)s2->regions[0]->data, s1->to_ignore, s2->to_ignore);
353 /* Compare binary global variables */
354 is_diff = compare_global_variables(2, s1->regions[2]->data, s2->regions[2]->data);
357 xbt_os_timer_stop(timer);
358 mc_comp_times->binary_global_variables_comparison_time = xbt_os_timer_elapsed(timer);
359 XBT_DEBUG("Different global variables in binary");
363 XBT_VERB("Different global variables in binary");
366 reset_heap_information();
367 xbt_os_timer_free(timer);
368 xbt_os_timer_stop(global_timer);
369 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
370 xbt_os_timer_free(global_timer);
381 xbt_os_timer_stop(timer);
382 xbt_os_timer_start(timer);
385 /* Compare libsimgrid global variables */
386 is_diff = compare_global_variables(1, s1->regions[1]->data, s2->regions[1]->data);
389 xbt_os_timer_stop(timer);
390 mc_comp_times->libsimgrid_global_variables_comparison_time = xbt_os_timer_elapsed(timer);
391 XBT_DEBUG("Different global variables in libsimgrid");
395 XBT_VERB("Different global variables in libsimgrid");
398 reset_heap_information();
399 xbt_os_timer_free(timer);
400 xbt_os_timer_stop(global_timer);
401 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
402 xbt_os_timer_free(global_timer);
413 xbt_os_timer_stop(timer);
414 xbt_os_timer_start(timer);
417 /* Stacks comparison */
418 unsigned int cursor = 0;
422 while(cursor < xbt_dynar_length(s1->stacks)){
423 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);
427 xbt_os_timer_stop(timer);
428 mc_comp_times->stacks_comparison_time = xbt_os_timer_elapsed(timer);
430 XBT_DEBUG("Different local variables between stacks %d", cursor + 1);
436 XBT_VERB("Different local variables between stacks %d", cursor + 1);
439 reset_heap_information();
440 xbt_os_timer_free(timer);
441 xbt_os_timer_stop(global_timer);
442 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
443 xbt_os_timer_free(global_timer);
455 xbt_os_timer_start(timer);
460 if(mmalloc_compare_heap((xbt_mheap_t)s1->regions[0]->data, (xbt_mheap_t)s2->regions[0]->data)){
463 xbt_os_timer_stop(timer);
464 mc_comp_times->heap_comparison_time = xbt_os_timer_elapsed(timer);
465 XBT_DEBUG("Different heap (mmalloc_compare)");
469 xbt_os_timer_free(timer);
472 XBT_VERB("Different heap (mmalloc_compare)");
475 reset_heap_information();
476 xbt_os_timer_stop(global_timer);
477 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
478 xbt_os_timer_free(global_timer);
487 xbt_os_timer_stop(timer);
491 reset_heap_information();
493 xbt_os_timer_free(timer);
496 xbt_os_timer_stop(global_timer);
497 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
500 xbt_os_timer_free(global_timer);
503 print_comparison_times();
513 static int is_stack_ignore_variable(char *frame, char *var_name){
515 unsigned int cursor = 0;
517 int end = xbt_dynar_length(mc_stack_comparison_ignore) - 1;
518 mc_stack_ignore_variable_t current_var;
521 cursor = (start + end) / 2;
522 current_var = (mc_stack_ignore_variable_t)xbt_dynar_get_as(mc_stack_comparison_ignore, cursor, mc_stack_ignore_variable_t);
523 if(strcmp(current_var->frame, frame) == 0 || strcmp(current_var->frame, "*") == 0){
524 if(strcmp(current_var->var_name, var_name) == 0)
526 if(strcmp(current_var->var_name, var_name) < 0)
528 if(strcmp(current_var->var_name, var_name) > 0)
530 }else if(strcmp(current_var->frame, frame) < 0){
532 }else if(strcmp(current_var->frame, frame) > 0){
540 static int compare_local_variables(char *s1, char *s2){
542 xbt_dynar_t tokens1 = xbt_str_split(s1, NULL);
543 xbt_dynar_t tokens2 = xbt_str_split(s2, NULL);
545 xbt_dynar_t s_tokens1, s_tokens2;
546 unsigned int cursor = 0;
548 char *frame_name1 = NULL, *frame_name2 = NULL;
555 while(cursor < xbt_dynar_length(tokens1)){
556 s_tokens1 = xbt_str_split(xbt_dynar_get_as(tokens1, cursor, char *), "=");
557 s_tokens2 = xbt_str_split(xbt_dynar_get_as(tokens2, cursor, char *), "=");
558 if(xbt_dynar_length(s_tokens1) > 1 && xbt_dynar_length(s_tokens2) > 1){
560 var_name = xbt_dynar_get_as(s_tokens1, 0, char *);
562 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)){
563 xbt_free(frame_name1);
564 xbt_free(frame_name2);
565 frame_name1 = strdup(xbt_dynar_get_as(s_tokens1, 1, char *));
566 frame_name2 = strdup(xbt_dynar_get_as(s_tokens2, 1, char *));
568 addr1 = (void *) strtoul(xbt_dynar_get_as(s_tokens1, 1, char *), NULL, 16);
569 addr2 = (void *) strtoul(xbt_dynar_get_as(s_tokens2, 1, char *), NULL, 16);
570 if(addr1 > std_heap && (char *)addr1 <= (char *)std_heap + STD_HEAP_SIZE && addr2 > std_heap && (char *)addr2 <= (char *)std_heap + STD_HEAP_SIZE){
571 res_compare = compare_area(addr1, addr2, NULL);
572 if(res_compare == 1){
573 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 *))){
574 xbt_dynar_free(&s_tokens1);
575 xbt_dynar_free(&s_tokens2);
580 XBT_VERB("Different local variable : %s at addresses %p - %p", var_name, addr1, addr2);
582 xbt_dynar_free(&s_tokens1);
583 xbt_dynar_free(&s_tokens2);
584 xbt_dynar_free(&tokens1);
585 xbt_dynar_free(&tokens2);
586 xbt_free(frame_name1);
587 xbt_free(frame_name2);
592 if(strcmp(xbt_dynar_get_as(s_tokens1, 1, char *), xbt_dynar_get_as(s_tokens2, 1, char *)) != 0){
593 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 *))){
594 xbt_dynar_free(&s_tokens1);
595 xbt_dynar_free(&s_tokens2);
600 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 *));
602 xbt_dynar_free(&s_tokens1);
603 xbt_dynar_free(&s_tokens2);
604 xbt_dynar_free(&tokens1);
605 xbt_dynar_free(&tokens2);
606 xbt_free(frame_name1);
607 xbt_free(frame_name2);
613 xbt_dynar_free(&s_tokens1);
614 xbt_dynar_free(&s_tokens2);
619 xbt_free(frame_name1);
620 xbt_free(frame_name2);
621 xbt_dynar_free(&tokens1);
622 xbt_dynar_free(&tokens2);
627 static int is_heap_equality(xbt_dynar_t equals, void *a1, void *a2){
629 unsigned int cursor = 0;
631 int end = xbt_dynar_length(equals) - 1;
632 heap_equality_t current_equality;
635 cursor = (start + end) / 2;
636 current_equality = (heap_equality_t)xbt_dynar_get_as(equals, cursor, heap_equality_t);
637 if(current_equality->address1 == a1){
638 if(current_equality->address2 == a2)
640 if(current_equality->address2 < a2)
642 if(current_equality->address2 > a2)
645 if(current_equality->address1 < a1)
647 if(current_equality->address1 > a1)
655 int MC_compare_snapshots(void *s1, void *s2){
657 MC_ignore_stack("self", "simcall_BODY_mc_snapshot");
659 return simcall_mc_compare_snapshots(s1, s2);
663 void print_comparison_times(){
664 XBT_DEBUG("*** Comparison times ***");
665 XBT_DEBUG("- Nb processes : %f", mc_comp_times->nb_processes_comparison_time);
666 XBT_DEBUG("- Nb bytes used : %f", mc_comp_times->bytes_used_comparison_time);
667 XBT_DEBUG("- Stacks sizes : %f", mc_comp_times->stacks_sizes_comparison_time);
668 XBT_DEBUG("- Binary global variables : %f", mc_comp_times->binary_global_variables_comparison_time);
669 XBT_DEBUG("- Libsimgrid global variables : %f", mc_comp_times->libsimgrid_global_variables_comparison_time);
670 XBT_DEBUG("- Heap : %f", mc_comp_times->heap_comparison_time);
671 XBT_DEBUG("- Stacks : %f", mc_comp_times->stacks_comparison_time);