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) || (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) && (addr_pointed2 > std_heap) && ((char *)addr_pointed2 < (char *)std_heap + STD_HEAP_SIZE)){
71 res_compare = compare_area(addr_pointed1, addr_pointed2, NULL);
74 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);
80 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);
91 xbt_dynar_foreach(mc_global_variables, cursor, current_var){
92 if(current_var->address > start_data_libsimgrid)
94 offset = (char *)current_var->address - (char *)start_data_binary;
96 while(i < current_var->size){
97 if(memcmp((char*)d1 + offset + i, (char*)d2 + offset + i, 1) != 0){
98 pointer_align = (i / sizeof(void*)) * sizeof(void*);
99 addr_pointed1 = *((void **)((char *)d1 + offset + pointer_align));
100 addr_pointed2 = *((void **)((char *)d2 + offset + pointer_align));
101 if((addr_pointed1 > start_plt_binary && addr_pointed1 < end_plt_binary) || (addr_pointed2 > start_plt_binary && addr_pointed2 < end_plt_binary)){
102 i = current_var->size;
105 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)){
106 res_compare = compare_area(addr_pointed1, addr_pointed2, NULL);
107 if(res_compare == 1){
109 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);
115 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);
130 static int heap_region_compare(void *d1, void *d2, size_t size){
134 for(i=0; i<size; i++){
135 if(memcmp(((char *)d1) + i, ((char *)d2) + i, 1) != 0){
136 if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_verbose)){
137 XBT_VERB("Different byte (offset=%zu) (%p - %p) in heap region", i, (char *)d1 + i, (char *)d2 + i);
146 static void stack_region_free(stack_region_t s){
148 xbt_free(s->process_name);
153 void stack_region_free_voidp(void *s){
154 stack_region_free((stack_region_t) * (void **) s);
157 static void heap_equality_free(heap_equality_t e){
161 void heap_equality_free_voidp(void *e){
162 heap_equality_free((heap_equality_t) * (void **) e);
165 int SIMIX_pre_mc_compare_snapshots(smx_simcall_t simcall,
166 mc_snapshot_t s1, mc_snapshot_t s2){
167 return snapshot_compare(s1, s2);
170 int snapshot_compare(mc_snapshot_t s1, mc_snapshot_t s2){
172 int raw_mem = (mmalloc_get_current_heap() == raw_heap);
178 xbt_os_timer_t global_timer = xbt_os_timer_new();
179 xbt_os_timer_t timer = xbt_os_timer_new();
181 xbt_os_timer_start(global_timer);
184 xbt_os_timer_start(timer);
187 /* Compare number of processes */
188 if(s1->nb_processes != s2->nb_processes){
190 xbt_os_timer_stop(timer);
191 mc_comp_times->nb_processes_comparison_time = xbt_os_timer_elapsed(timer);
192 XBT_DEBUG("Different number of processes : %d - %d", s1->nb_processes, s2->nb_processes);
196 XBT_VERB("Different number of processes : %d - %d", s1->nb_processes, s2->nb_processes);
199 xbt_os_timer_free(timer);
200 xbt_os_timer_stop(global_timer);
201 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
202 xbt_os_timer_free(global_timer);
212 xbt_os_timer_start(timer);
215 /* Compare number of bytes used in each heap */
216 if(s1->heap_bytes_used != s2->heap_bytes_used){
218 xbt_os_timer_stop(timer);
219 mc_comp_times->bytes_used_comparison_time = xbt_os_timer_elapsed(timer);
220 XBT_DEBUG("Different number of bytes used in each heap : %zu - %zu", s1->heap_bytes_used, s2->heap_bytes_used);
224 XBT_VERB("Different number of bytes used in each heap : %zu - %zu", s1->heap_bytes_used, s2->heap_bytes_used);
227 xbt_os_timer_free(timer);
228 xbt_os_timer_stop(global_timer);
229 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
230 xbt_os_timer_free(global_timer);
239 xbt_os_timer_stop(timer);
244 xbt_os_timer_start(timer);
247 /* Compare size of stacks */
249 size_t size_used1, size_used2;
251 while(i < xbt_dynar_length(s1->stacks)){
252 size_used1 = s1->stack_sizes[i];
253 size_used2 = s2->stack_sizes[i];
254 if(size_used1 != size_used2){
257 xbt_os_timer_stop(timer);
258 mc_comp_times->stacks_sizes_comparison_time = xbt_os_timer_elapsed(timer);
260 XBT_DEBUG("Different size used in stacks : %zu - %zu", size_used1, size_used2);
265 XBT_VERB("Different size used in stacks : %zu - %zu", size_used1, size_used2);
268 xbt_os_timer_free(timer);
269 xbt_os_timer_stop(global_timer);
270 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
271 xbt_os_timer_free(global_timer);
284 xbt_os_timer_stop(timer);
285 xbt_os_timer_start(timer);
288 /* Init heap information used in heap comparison algorithm */
289 init_heap_information((xbt_mheap_t)s1->regions[0]->data, (xbt_mheap_t)s2->regions[0]->data, s1->to_ignore, s2->to_ignore);
291 /* Compare binary global variables */
292 is_diff = compare_global_variables(2, s1->regions[2]->data, s2->regions[2]->data);
295 xbt_os_timer_stop(timer);
296 mc_comp_times->binary_global_variables_comparison_time = xbt_os_timer_elapsed(timer);
297 XBT_DEBUG("Different global variables in binary");
301 XBT_VERB("Different global variables in binary");
304 xbt_os_timer_free(timer);
305 xbt_os_timer_stop(global_timer);
306 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
307 xbt_os_timer_free(global_timer);
318 xbt_os_timer_stop(timer);
319 xbt_os_timer_start(timer);
322 /* Compare libsimgrid global variables */
323 is_diff = compare_global_variables(1, s1->regions[1]->data, s2->regions[1]->data);
326 xbt_os_timer_stop(timer);
327 mc_comp_times->libsimgrid_global_variables_comparison_time = xbt_os_timer_elapsed(timer);
328 XBT_DEBUG("Different global variables in libsimgrid");
332 XBT_VERB("Different global variables in libsimgrid");
335 xbt_os_timer_free(timer);
336 xbt_os_timer_stop(global_timer);
337 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
338 xbt_os_timer_free(global_timer);
349 xbt_os_timer_stop(timer);
350 xbt_os_timer_start(timer);
353 /* Stacks comparison */
354 unsigned int cursor = 0;
358 while(cursor < xbt_dynar_length(s1->stacks)){
359 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);
363 xbt_os_timer_stop(timer);
364 mc_comp_times->stacks_comparison_time = xbt_os_timer_elapsed(timer);
366 XBT_DEBUG("Different local variables between stacks %d", cursor + 1);
372 XBT_VERB("Different local variables between stacks %d", cursor + 1);
375 reset_heap_information();
376 xbt_os_timer_free(timer);
377 xbt_os_timer_stop(global_timer);
378 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
379 xbt_os_timer_free(global_timer);
391 xbt_os_timer_start(timer);
396 if(mmalloc_compare_heap((xbt_mheap_t)s1->regions[0]->data, (xbt_mheap_t)s2->regions[0]->data)){
399 xbt_os_timer_stop(timer);
400 mc_comp_times->heap_comparison_time = xbt_os_timer_elapsed(timer);
401 XBT_DEBUG("Different heap (mmalloc_compare)");
405 xbt_os_timer_free(timer);
408 XBT_VERB("Different heap (mmalloc_compare)");
411 reset_heap_information();
412 xbt_os_timer_stop(global_timer);
413 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
414 xbt_os_timer_free(global_timer);
423 xbt_os_timer_stop(timer);
427 reset_heap_information();
429 xbt_os_timer_free(timer);
432 xbt_os_timer_stop(global_timer);
433 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
436 xbt_os_timer_free(global_timer);
439 print_comparison_times();
449 static int is_stack_ignore_variable(char *frame, char *var_name){
451 unsigned int cursor = 0;
453 int end = xbt_dynar_length(mc_stack_comparison_ignore) - 1;
454 mc_stack_ignore_variable_t current_var;
457 cursor = (start + end) / 2;
458 current_var = (mc_stack_ignore_variable_t)xbt_dynar_get_as(mc_stack_comparison_ignore, cursor, mc_stack_ignore_variable_t);
459 if(strcmp(current_var->frame, frame) == 0 || strcmp(current_var->frame, "*") == 0){
460 if(strcmp(current_var->var_name, var_name) == 0)
462 if(strcmp(current_var->var_name, var_name) < 0)
464 if(strcmp(current_var->var_name, var_name) > 0)
466 }else if(strcmp(current_var->frame, frame) < 0){
468 }else if(strcmp(current_var->frame, frame) > 0){
476 static int compare_local_variables(char *s1, char *s2){
478 xbt_dynar_t tokens1 = xbt_str_split(s1, NULL);
479 xbt_dynar_t tokens2 = xbt_str_split(s2, NULL);
481 xbt_dynar_t s_tokens1, s_tokens2;
482 unsigned int cursor = 0;
484 char *frame_name1 = NULL, *frame_name2 = NULL;
491 while(cursor < xbt_dynar_length(tokens1)){
492 s_tokens1 = xbt_str_split(xbt_dynar_get_as(tokens1, cursor, char *), "=");
493 s_tokens2 = xbt_str_split(xbt_dynar_get_as(tokens2, cursor, char *), "=");
494 if(xbt_dynar_length(s_tokens1) > 1 && xbt_dynar_length(s_tokens2) > 1){
496 var_name = xbt_dynar_get_as(s_tokens1, 0, char *);
498 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)){
499 xbt_free(frame_name1);
500 xbt_free(frame_name2);
501 frame_name1 = strdup(xbt_dynar_get_as(s_tokens1, 1, char *));
502 frame_name2 = strdup(xbt_dynar_get_as(s_tokens2, 1, char *));
504 addr1 = (void *) strtoul(xbt_dynar_get_as(s_tokens1, 1, char *), NULL, 16);
505 addr2 = (void *) strtoul(xbt_dynar_get_as(s_tokens2, 1, char *), NULL, 16);
506 if(addr1 > std_heap && (char *)addr1 <= (char *)std_heap + STD_HEAP_SIZE && addr2 > std_heap && (char *)addr2 <= (char *)std_heap + STD_HEAP_SIZE){
507 res_compare = compare_area(addr1, addr2, NULL);
508 if(res_compare == 1){
509 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 *))){
510 xbt_dynar_free(&s_tokens1);
511 xbt_dynar_free(&s_tokens2);
516 XBT_VERB("Different local variable : %s at addresses %p - %p", var_name, addr1, addr2);
518 xbt_dynar_free(&s_tokens1);
519 xbt_dynar_free(&s_tokens2);
520 xbt_dynar_free(&tokens1);
521 xbt_dynar_free(&tokens2);
522 xbt_free(frame_name1);
523 xbt_free(frame_name2);
528 if(strcmp(xbt_dynar_get_as(s_tokens1, 1, char *), xbt_dynar_get_as(s_tokens2, 1, char *)) != 0){
529 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 *))){
530 xbt_dynar_free(&s_tokens1);
531 xbt_dynar_free(&s_tokens2);
536 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 *));
538 xbt_dynar_free(&s_tokens1);
539 xbt_dynar_free(&s_tokens2);
540 xbt_dynar_free(&tokens1);
541 xbt_dynar_free(&tokens2);
542 xbt_free(frame_name1);
543 xbt_free(frame_name2);
549 xbt_dynar_free(&s_tokens1);
550 xbt_dynar_free(&s_tokens2);
555 xbt_free(frame_name1);
556 xbt_free(frame_name2);
557 xbt_dynar_free(&tokens1);
558 xbt_dynar_free(&tokens2);
563 static int is_heap_equality(xbt_dynar_t equals, void *a1, void *a2){
565 unsigned int cursor = 0;
567 int end = xbt_dynar_length(equals) - 1;
568 heap_equality_t current_equality;
571 cursor = (start + end) / 2;
572 current_equality = (heap_equality_t)xbt_dynar_get_as(equals, cursor, heap_equality_t);
573 if(current_equality->address1 == a1){
574 if(current_equality->address2 == a2)
576 if(current_equality->address2 < a2)
578 if(current_equality->address2 > a2)
581 if(current_equality->address1 < a1)
583 if(current_equality->address1 > a1)
591 int MC_compare_snapshots(void *s1, void *s2){
593 MC_ignore_stack("self", "simcall_BODY_mc_snapshot");
595 return simcall_mc_compare_snapshots(s1, s2);
599 void print_comparison_times(){
600 XBT_DEBUG("*** Comparison times ***");
601 XBT_DEBUG("- Nb processes : %f", mc_comp_times->nb_processes_comparison_time);
602 XBT_DEBUG("- Nb bytes used : %f", mc_comp_times->bytes_used_comparison_time);
603 XBT_DEBUG("- Stacks sizes : %f", mc_comp_times->stacks_sizes_comparison_time);
604 XBT_DEBUG("- Binary global variables : %f", mc_comp_times->binary_global_variables_comparison_time);
605 XBT_DEBUG("- Libsimgrid global variables : %f", mc_comp_times->libsimgrid_global_variables_comparison_time);
606 XBT_DEBUG("- Heap : %f", mc_comp_times->heap_comparison_time);
607 XBT_DEBUG("- Stacks : %f", mc_comp_times->stacks_comparison_time);