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 /* Init heap information used in heap comparison algorithm */
291 init_heap_information((xbt_mheap_t)s1->regions[0]->data, (xbt_mheap_t)s2->regions[0]->data, s1->to_ignore, s2->to_ignore);
293 /* Compare binary global variables */
294 is_diff = compare_global_variables(2, s1->regions[2]->data, s2->regions[2]->data);
297 xbt_os_timer_stop(timer);
298 mc_comp_times->binary_global_variables_comparison_time = xbt_os_timer_elapsed(timer);
299 XBT_DEBUG("Different global variables in binary");
303 XBT_VERB("Different global variables in binary");
306 reset_heap_information();
307 xbt_os_timer_free(timer);
308 xbt_os_timer_stop(global_timer);
309 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
310 xbt_os_timer_free(global_timer);
321 xbt_os_timer_stop(timer);
322 xbt_os_timer_start(timer);
325 /* Compare libsimgrid global variables */
326 is_diff = compare_global_variables(1, s1->regions[1]->data, s2->regions[1]->data);
329 xbt_os_timer_stop(timer);
330 mc_comp_times->libsimgrid_global_variables_comparison_time = xbt_os_timer_elapsed(timer);
331 XBT_DEBUG("Different global variables in libsimgrid");
335 XBT_VERB("Different global variables in libsimgrid");
338 reset_heap_information();
339 xbt_os_timer_free(timer);
340 xbt_os_timer_stop(global_timer);
341 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
342 xbt_os_timer_free(global_timer);
353 xbt_os_timer_stop(timer);
354 xbt_os_timer_start(timer);
357 /* Stacks comparison */
358 unsigned int cursor = 0;
362 while(cursor < xbt_dynar_length(s1->stacks)){
363 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);
367 xbt_os_timer_stop(timer);
368 mc_comp_times->stacks_comparison_time = xbt_os_timer_elapsed(timer);
370 XBT_DEBUG("Different local variables between stacks %d", cursor + 1);
376 XBT_VERB("Different local variables between stacks %d", cursor + 1);
379 reset_heap_information();
380 xbt_os_timer_free(timer);
381 xbt_os_timer_stop(global_timer);
382 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
383 xbt_os_timer_free(global_timer);
395 xbt_os_timer_start(timer);
400 if(mmalloc_compare_heap((xbt_mheap_t)s1->regions[0]->data, (xbt_mheap_t)s2->regions[0]->data)){
403 xbt_os_timer_stop(timer);
404 mc_comp_times->heap_comparison_time = xbt_os_timer_elapsed(timer);
405 XBT_DEBUG("Different heap (mmalloc_compare)");
409 xbt_os_timer_free(timer);
412 XBT_VERB("Different heap (mmalloc_compare)");
415 reset_heap_information();
416 xbt_os_timer_stop(global_timer);
417 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
418 xbt_os_timer_free(global_timer);
427 xbt_os_timer_stop(timer);
431 reset_heap_information();
433 xbt_os_timer_free(timer);
436 xbt_os_timer_stop(global_timer);
437 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
440 xbt_os_timer_free(global_timer);
443 print_comparison_times();
453 static int is_stack_ignore_variable(char *frame, char *var_name){
455 unsigned int cursor = 0;
457 int end = xbt_dynar_length(mc_stack_comparison_ignore) - 1;
458 mc_stack_ignore_variable_t current_var;
461 cursor = (start + end) / 2;
462 current_var = (mc_stack_ignore_variable_t)xbt_dynar_get_as(mc_stack_comparison_ignore, cursor, mc_stack_ignore_variable_t);
463 if(strcmp(current_var->frame, frame) == 0 || strcmp(current_var->frame, "*") == 0){
464 if(strcmp(current_var->var_name, var_name) == 0)
466 if(strcmp(current_var->var_name, var_name) < 0)
468 if(strcmp(current_var->var_name, var_name) > 0)
470 }else if(strcmp(current_var->frame, frame) < 0){
472 }else if(strcmp(current_var->frame, frame) > 0){
480 static int compare_local_variables(char *s1, char *s2){
482 xbt_dynar_t tokens1 = xbt_str_split(s1, NULL);
483 xbt_dynar_t tokens2 = xbt_str_split(s2, NULL);
485 xbt_dynar_t s_tokens1, s_tokens2;
486 unsigned int cursor = 0;
488 char *frame_name1 = NULL, *frame_name2 = NULL;
495 while(cursor < xbt_dynar_length(tokens1)){
496 s_tokens1 = xbt_str_split(xbt_dynar_get_as(tokens1, cursor, char *), "=");
497 s_tokens2 = xbt_str_split(xbt_dynar_get_as(tokens2, cursor, char *), "=");
498 if(xbt_dynar_length(s_tokens1) > 1 && xbt_dynar_length(s_tokens2) > 1){
500 var_name = xbt_dynar_get_as(s_tokens1, 0, char *);
502 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)){
503 xbt_free(frame_name1);
504 xbt_free(frame_name2);
505 frame_name1 = strdup(xbt_dynar_get_as(s_tokens1, 1, char *));
506 frame_name2 = strdup(xbt_dynar_get_as(s_tokens2, 1, char *));
508 addr1 = (void *) strtoul(xbt_dynar_get_as(s_tokens1, 1, char *), NULL, 16);
509 addr2 = (void *) strtoul(xbt_dynar_get_as(s_tokens2, 1, char *), NULL, 16);
510 if(addr1 > std_heap && (char *)addr1 <= (char *)std_heap + STD_HEAP_SIZE && addr2 > std_heap && (char *)addr2 <= (char *)std_heap + STD_HEAP_SIZE){
511 res_compare = compare_area(addr1, addr2, NULL);
512 if(res_compare == 1){
513 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 *))){
514 xbt_dynar_free(&s_tokens1);
515 xbt_dynar_free(&s_tokens2);
520 XBT_VERB("Different local variable : %s at addresses %p - %p", var_name, addr1, addr2);
522 xbt_dynar_free(&s_tokens1);
523 xbt_dynar_free(&s_tokens2);
524 xbt_dynar_free(&tokens1);
525 xbt_dynar_free(&tokens2);
526 xbt_free(frame_name1);
527 xbt_free(frame_name2);
532 if(strcmp(xbt_dynar_get_as(s_tokens1, 1, char *), xbt_dynar_get_as(s_tokens2, 1, char *)) != 0){
533 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 *))){
534 xbt_dynar_free(&s_tokens1);
535 xbt_dynar_free(&s_tokens2);
540 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 *));
542 xbt_dynar_free(&s_tokens1);
543 xbt_dynar_free(&s_tokens2);
544 xbt_dynar_free(&tokens1);
545 xbt_dynar_free(&tokens2);
546 xbt_free(frame_name1);
547 xbt_free(frame_name2);
553 xbt_dynar_free(&s_tokens1);
554 xbt_dynar_free(&s_tokens2);
559 xbt_free(frame_name1);
560 xbt_free(frame_name2);
561 xbt_dynar_free(&tokens1);
562 xbt_dynar_free(&tokens2);
567 static int is_heap_equality(xbt_dynar_t equals, void *a1, void *a2){
569 unsigned int cursor = 0;
571 int end = xbt_dynar_length(equals) - 1;
572 heap_equality_t current_equality;
575 cursor = (start + end) / 2;
576 current_equality = (heap_equality_t)xbt_dynar_get_as(equals, cursor, heap_equality_t);
577 if(current_equality->address1 == a1){
578 if(current_equality->address2 == a2)
580 if(current_equality->address2 < a2)
582 if(current_equality->address2 > a2)
585 if(current_equality->address1 < a1)
587 if(current_equality->address1 > a1)
595 int MC_compare_snapshots(void *s1, void *s2){
597 MC_ignore_stack("self", "simcall_BODY_mc_snapshot");
599 return simcall_mc_compare_snapshots(s1, s2);
603 void print_comparison_times(){
604 XBT_DEBUG("*** Comparison times ***");
605 XBT_DEBUG("- Nb processes : %f", mc_comp_times->nb_processes_comparison_time);
606 XBT_DEBUG("- Nb bytes used : %f", mc_comp_times->bytes_used_comparison_time);
607 XBT_DEBUG("- Stacks sizes : %f", mc_comp_times->stacks_sizes_comparison_time);
608 XBT_DEBUG("- Binary global variables : %f", mc_comp_times->binary_global_variables_comparison_time);
609 XBT_DEBUG("- Libsimgrid global variables : %f", mc_comp_times->libsimgrid_global_variables_comparison_time);
610 XBT_DEBUG("- Heap : %f", mc_comp_times->heap_comparison_time);
611 XBT_DEBUG("- Stacks : %f", mc_comp_times->stacks_comparison_time);