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 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){
46 unsigned int cursor = 0;
49 global_variable_t current_var;
51 void *addr_pointed1 = NULL, *addr_pointed2 = NULL;
54 if(region_type == 1){ /* libsimgrid */
55 xbt_dynar_foreach(mc_global_variables, cursor, current_var){
56 if(current_var->address < start_data_libsimgrid)
58 offset = (char *)current_var->address - (char *)start_data_libsimgrid;
60 while(i < current_var->size){
61 if(memcmp((char*)d1 + offset + i, (char*)d2 + offset + i, 1) != 0){
62 pointer_align = (i / sizeof(void*)) * sizeof(void*);
63 addr_pointed1 = *((void **)((char *)d1 + offset + pointer_align));
64 addr_pointed2 = *((void **)((char *)d2 + offset + pointer_align));
65 if((addr_pointed1 > start_plt_libsimgrid && addr_pointed1 < end_plt_libsimgrid)
66 || (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)
71 && (addr_pointed2 > std_heap) && ((char *)addr_pointed2 < (char *)std_heap + STD_HEAP_SIZE)){
72 res_compare = compare_area(addr_pointed1, addr_pointed2, NULL);
75 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);
81 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);
92 xbt_dynar_foreach(mc_global_variables, cursor, current_var){
93 if(current_var->address > start_data_libsimgrid)
95 offset = (char *)current_var->address - (char *)start_data_binary;
97 while(i < current_var->size){
98 if(memcmp((char*)d1 + offset + i, (char*)d2 + offset + i, 1) != 0){
99 pointer_align = (i / sizeof(void*)) * sizeof(void*);
100 addr_pointed1 = *((void **)((char *)d1 + offset + pointer_align));
101 addr_pointed2 = *((void **)((char *)d2 + offset + pointer_align));
102 if((addr_pointed1 > start_plt_binary && addr_pointed1 < end_plt_binary) || (addr_pointed2 > start_plt_binary && addr_pointed2 < end_plt_binary)){
103 i = current_var->size;
106 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)){
107 res_compare = compare_area(addr_pointed1, addr_pointed2, NULL);
108 if(res_compare == 1){
110 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);
116 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);
131 static int heap_region_compare(void *d1, void *d2, size_t size){
135 for(i=0; i<size; i++){
136 if(memcmp(((char *)d1) + i, ((char *)d2) + i, 1) != 0){
137 if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_verbose)){
138 XBT_VERB("Different byte (offset=%zu) (%p - %p) in heap region", i, (char *)d1 + i, (char *)d2 + i);
147 static void stack_region_free(stack_region_t s){
149 xbt_free(s->process_name);
154 void stack_region_free_voidp(void *s){
155 stack_region_free((stack_region_t) * (void **) s);
158 static void heap_equality_free(heap_equality_t e){
162 void heap_equality_free_voidp(void *e){
163 heap_equality_free((heap_equality_t) * (void **) e);
166 int SIMIX_pre_mc_compare_snapshots(smx_simcall_t simcall,
167 mc_snapshot_t s1, mc_snapshot_t s2){
168 return snapshot_compare(s1, s2);
171 int snapshot_compare(mc_snapshot_t s1, mc_snapshot_t s2){
173 int raw_mem = (mmalloc_get_current_heap() == raw_heap);
179 xbt_os_timer_t global_timer = xbt_os_timer_new();
180 xbt_os_timer_t timer = xbt_os_timer_new();
182 xbt_os_timer_start(global_timer);
185 xbt_os_timer_start(timer);
188 /* Compare number of processes */
189 if(s1->nb_processes != s2->nb_processes){
191 xbt_os_timer_stop(timer);
192 mc_comp_times->nb_processes_comparison_time = xbt_os_timer_elapsed(timer);
193 XBT_DEBUG("Different number of processes : %d - %d", s1->nb_processes, s2->nb_processes);
197 XBT_VERB("Different number of processes : %d - %d", s1->nb_processes, s2->nb_processes);
200 xbt_os_timer_free(timer);
201 xbt_os_timer_stop(global_timer);
202 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
203 xbt_os_timer_free(global_timer);
213 xbt_os_timer_start(timer);
216 /* Compare number of bytes used in each heap */
217 if(s1->heap_bytes_used != s2->heap_bytes_used){
219 xbt_os_timer_stop(timer);
220 mc_comp_times->bytes_used_comparison_time = xbt_os_timer_elapsed(timer);
221 XBT_DEBUG("Different number of bytes used in each heap : %zu - %zu", s1->heap_bytes_used, s2->heap_bytes_used);
225 XBT_VERB("Different number of bytes used in each heap : %zu - %zu", s1->heap_bytes_used, s2->heap_bytes_used);
228 xbt_os_timer_free(timer);
229 xbt_os_timer_stop(global_timer);
230 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
231 xbt_os_timer_free(global_timer);
240 xbt_os_timer_stop(timer);
245 xbt_os_timer_start(timer);
248 /* Compare size of stacks */
250 size_t size_used1, size_used2;
252 while(i < xbt_dynar_length(s1->stacks)){
253 size_used1 = s1->stack_sizes[i];
254 size_used2 = s2->stack_sizes[i];
255 if(size_used1 != size_used2){
258 xbt_os_timer_stop(timer);
259 mc_comp_times->stacks_sizes_comparison_time = xbt_os_timer_elapsed(timer);
261 XBT_DEBUG("Different size used in stacks : %zu - %zu", size_used1, size_used2);
266 XBT_VERB("Different size used in stacks : %zu - %zu", size_used1, size_used2);
269 xbt_os_timer_free(timer);
270 xbt_os_timer_stop(global_timer);
271 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
272 xbt_os_timer_free(global_timer);
285 xbt_os_timer_stop(timer);
286 xbt_os_timer_start(timer);
289 /* Compare hash of global variables */
290 if(s1->hash_global != NULL && s2->hash_global != NULL){
291 if(strcmp(s1->hash_global, s2->hash_global) != 0){
293 xbt_os_timer_stop(timer);
294 mc_comp_times->hash_global_variables_comparison_time = xbt_os_timer_elapsed(timer);
295 XBT_DEBUG("Different hash of global variables : %s - %s", s1->hash_global, s2->hash_global);
299 XBT_VERB("Different hash of global variables : %s - %s", s1->hash_global, s2->hash_global);
302 xbt_os_timer_free(timer);
303 xbt_os_timer_stop(global_timer);
304 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
305 xbt_os_timer_free(global_timer);
316 xbt_os_timer_start(timer);
319 /* Compare hash of local variables */
320 if(s1->hash_local != NULL && s2->hash_local != NULL){
321 if(strcmp(s1->hash_local, s2->hash_local) != 0){
323 xbt_os_timer_stop(timer);
324 mc_comp_times->hash_local_variables_comparison_time = xbt_os_timer_elapsed(timer);
325 XBT_DEBUG("Different hash of local variables : %s - %s", s1->hash_local, s2->hash_local);
329 XBT_VERB("Different hash of local variables : %s - %s", s1->hash_local, s2->hash_local);
332 xbt_os_timer_free(timer);
333 xbt_os_timer_stop(global_timer);
334 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
335 xbt_os_timer_free(global_timer);
346 xbt_os_timer_start(timer);
349 /* Init heap information used in heap comparison algorithm */
350 init_heap_information((xbt_mheap_t)s1->regions[0]->data, (xbt_mheap_t)s2->regions[0]->data, s1->to_ignore, s2->to_ignore);
352 /* Compare binary global variables */
353 is_diff = compare_global_variables(2, s1->regions[2]->data, s2->regions[2]->data);
356 xbt_os_timer_stop(timer);
357 mc_comp_times->binary_global_variables_comparison_time = xbt_os_timer_elapsed(timer);
358 XBT_DEBUG("Different global variables in binary");
362 XBT_VERB("Different global variables in binary");
365 reset_heap_information();
366 xbt_os_timer_free(timer);
367 xbt_os_timer_stop(global_timer);
368 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
369 xbt_os_timer_free(global_timer);
380 xbt_os_timer_stop(timer);
381 xbt_os_timer_start(timer);
384 /* Compare libsimgrid global variables */
385 is_diff = compare_global_variables(1, s1->regions[1]->data, s2->regions[1]->data);
388 xbt_os_timer_stop(timer);
389 mc_comp_times->libsimgrid_global_variables_comparison_time = xbt_os_timer_elapsed(timer);
390 XBT_DEBUG("Different global variables in libsimgrid");
394 XBT_VERB("Different global variables in libsimgrid");
397 reset_heap_information();
398 xbt_os_timer_free(timer);
399 xbt_os_timer_stop(global_timer);
400 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
401 xbt_os_timer_free(global_timer);
412 xbt_os_timer_stop(timer);
413 xbt_os_timer_start(timer);
416 /* Stacks comparison */
417 unsigned int cursor = 0;
421 while(cursor < xbt_dynar_length(s1->stacks)){
422 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);
426 xbt_os_timer_stop(timer);
427 mc_comp_times->stacks_comparison_time = xbt_os_timer_elapsed(timer);
429 XBT_DEBUG("Different local variables between stacks %d", cursor + 1);
435 XBT_VERB("Different local variables between stacks %d", cursor + 1);
438 reset_heap_information();
439 xbt_os_timer_free(timer);
440 xbt_os_timer_stop(global_timer);
441 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
442 xbt_os_timer_free(global_timer);
454 xbt_os_timer_start(timer);
459 if(mmalloc_compare_heap((xbt_mheap_t)s1->regions[0]->data, (xbt_mheap_t)s2->regions[0]->data)){
462 xbt_os_timer_stop(timer);
463 mc_comp_times->heap_comparison_time = xbt_os_timer_elapsed(timer);
464 XBT_DEBUG("Different heap (mmalloc_compare)");
468 xbt_os_timer_free(timer);
471 XBT_VERB("Different heap (mmalloc_compare)");
474 reset_heap_information();
475 xbt_os_timer_stop(global_timer);
476 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
477 xbt_os_timer_free(global_timer);
486 xbt_os_timer_stop(timer);
490 reset_heap_information();
492 xbt_os_timer_free(timer);
495 xbt_os_timer_stop(global_timer);
496 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
499 xbt_os_timer_free(global_timer);
502 print_comparison_times();
512 int is_stack_ignore_variable(char *frame, char *var_name){
514 unsigned int cursor = 0;
516 int end = xbt_dynar_length(mc_stack_comparison_ignore) - 1;
517 mc_stack_ignore_variable_t current_var;
520 cursor = (start + end) / 2;
521 current_var = (mc_stack_ignore_variable_t)xbt_dynar_get_as(mc_stack_comparison_ignore, cursor, mc_stack_ignore_variable_t);
522 if(strcmp(current_var->frame, frame) == 0 || strcmp(current_var->frame, "*") == 0){
523 if(strcmp(current_var->var_name, var_name) == 0)
525 if(strcmp(current_var->var_name, var_name) < 0)
527 if(strcmp(current_var->var_name, var_name) > 0)
529 }else if(strcmp(current_var->frame, frame) < 0){
531 }else if(strcmp(current_var->frame, frame) > 0){
539 static int compare_local_variables(char *s1, char *s2){
541 xbt_dynar_t tokens1 = xbt_str_split(s1, NULL);
542 xbt_dynar_t tokens2 = xbt_str_split(s2, NULL);
544 xbt_dynar_t s_tokens1, s_tokens2;
545 unsigned int cursor = 0;
547 char *frame_name1 = NULL, *frame_name2 = NULL;
554 while(cursor < xbt_dynar_length(tokens1)){
555 s_tokens1 = xbt_str_split(xbt_dynar_get_as(tokens1, cursor, char *), "=");
556 s_tokens2 = xbt_str_split(xbt_dynar_get_as(tokens2, cursor, char *), "=");
557 if(xbt_dynar_length(s_tokens1) > 1 && xbt_dynar_length(s_tokens2) > 1){
559 var_name = xbt_dynar_get_as(s_tokens1, 0, char *);
561 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)){
562 xbt_free(frame_name1);
563 xbt_free(frame_name2);
564 frame_name1 = strdup(xbt_dynar_get_as(s_tokens1, 1, char *));
565 frame_name2 = strdup(xbt_dynar_get_as(s_tokens2, 1, char *));
567 addr1 = (void *) strtoul(xbt_dynar_get_as(s_tokens1, 1, char *), NULL, 16);
568 addr2 = (void *) strtoul(xbt_dynar_get_as(s_tokens2, 1, char *), NULL, 16);
569 if(addr1 > std_heap && (char *)addr1 <= (char *)std_heap + STD_HEAP_SIZE && addr2 > std_heap && (char *)addr2 <= (char *)std_heap + STD_HEAP_SIZE){
570 res_compare = compare_area(addr1, addr2, NULL);
571 if(res_compare == 1){
572 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 *))){
573 xbt_dynar_free(&s_tokens1);
574 xbt_dynar_free(&s_tokens2);
579 XBT_VERB("Different local variable : %s at addresses %p - %p", var_name, addr1, addr2);
581 xbt_dynar_free(&s_tokens1);
582 xbt_dynar_free(&s_tokens2);
583 xbt_dynar_free(&tokens1);
584 xbt_dynar_free(&tokens2);
585 xbt_free(frame_name1);
586 xbt_free(frame_name2);
591 if(strcmp(xbt_dynar_get_as(s_tokens1, 1, char *), xbt_dynar_get_as(s_tokens2, 1, char *)) != 0){
592 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 *))){
593 xbt_dynar_free(&s_tokens1);
594 xbt_dynar_free(&s_tokens2);
599 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_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);