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 typedef struct s_pointers_pair{
16 }s_pointers_pair_t, *pointers_pair_t;
18 __thread xbt_dynar_t compared_pointers;
20 /************************** Free functions ****************************/
21 /********************************************************************/
23 static void stack_region_free(stack_region_t s){
25 xbt_free(s->process_name);
30 static void stack_region_free_voidp(void *s){
31 stack_region_free((stack_region_t) * (void **) s);
34 static void pointers_pair_free(pointers_pair_t p){
38 static void pointers_pair_free_voidp(void *p){
39 pointers_pair_free((pointers_pair_t) * (void **)p);
42 /************************** Snapshot comparison *******************************/
43 /******************************************************************************/
45 static int already_compared_pointers(void *p1, void *p2){
47 if(xbt_dynar_is_empty(compared_pointers))
50 unsigned int cursor = 0;
52 int end = xbt_dynar_length(compared_pointers) - 1;
56 cursor = (start + end) / 2;
57 pair = (pointers_pair_t)xbt_dynar_get_as(compared_pointers, cursor, pointers_pair_t);
61 else if(pair->p2 < p2)
65 }else if(pair->p1 < p1){
76 static void add_compared_pointers(void *p1, void *p2){
78 pointers_pair_t new_pair = xbt_new0(s_pointers_pair_t, 1);
82 if(xbt_dynar_is_empty(compared_pointers)){
83 xbt_dynar_push(compared_pointers, &new_pair);
87 unsigned int cursor = 0;
89 int end = xbt_dynar_length(compared_pointers) - 1;
90 pointers_pair_t pair = NULL;
93 cursor = (start + end) / 2;
94 pair = (pointers_pair_t)xbt_dynar_get_as(compared_pointers, cursor, pointers_pair_t);
97 pointers_pair_free(new_pair);
99 }else if(pair->p2 < p2)
103 }else if(pair->p1 < p1){
112 xbt_dynar_insert_at(compared_pointers, cursor + 1, &new_pair);
114 xbt_dynar_insert_at(compared_pointers, cursor, &new_pair);
117 xbt_dynar_insert_at(compared_pointers, cursor + 1, &new_pair);
119 xbt_dynar_insert_at(compared_pointers, cursor, &new_pair);
124 static int compare_areas_with_type(void *area1, void *area2, xbt_dict_t types, xbt_dict_t other_types, char *type_id, int region_size, int region_type, void *start_data, int pointer_level){
126 dw_type_t type = xbt_dict_get_or_null(types, type_id);;
127 unsigned int cursor = 0;
128 dw_type_t member, subtype, subsubtype;
129 int elm_size, i, res, switch_types = 0;
130 void *addr_pointed1, *addr_pointed2;
134 case e_dw_enumeration_type:
135 case e_dw_union_type:
136 return (memcmp(area1, area2, type->size) != 0);
139 case e_dw_volatile_type:
140 return compare_areas_with_type(area1, area2, types, other_types, type->dw_type_id, region_size, region_type, start_data, pointer_level);
142 case e_dw_const_type: /* Const variable cannot be modified */
145 case e_dw_array_type:
146 subtype = xbt_dict_get_or_null(types, type->dw_type_id);
147 switch(subtype->type){
149 case e_dw_enumeration_type:
150 case e_dw_pointer_type:
151 case e_dw_structure_type:
152 case e_dw_union_type:
153 if(subtype->size == 0){ /*declaration of the type, need the complete description */
154 subtype = xbt_dict_get_or_null(types, get_type_description(types, subtype->name));
156 subtype = xbt_dict_get_or_null(other_types, get_type_description(other_types, subtype->name));
160 elm_size = subtype->size;
163 case e_dw_volatile_type:
164 subsubtype = xbt_dict_get_or_null(types, subtype->dw_type_id);
165 if(subsubtype->size == 0){ /*declaration of the type, need the complete description */
166 subsubtype = xbt_dict_get_or_null(types, get_type_description(types, subsubtype->name));
167 if(subsubtype == NULL){
168 subsubtype = xbt_dict_get_or_null(other_types, get_type_description(other_types, subsubtype->name));
172 elm_size = subsubtype->size;
178 for(i=0; i<type->size; i++){
180 res = compare_areas_with_type((char *)area1 + (i*elm_size), (char *)area2 + (i*elm_size), other_types, types, type->dw_type_id, region_size, region_type, start_data, pointer_level);
182 res = compare_areas_with_type((char *)area1 + (i*elm_size), (char *)area2 + (i*elm_size), types, other_types, type->dw_type_id, region_size, region_type, start_data, pointer_level);
187 case e_dw_pointer_type:
188 if(type->dw_type_id && ((dw_type_t)xbt_dict_get_or_null(types, type->dw_type_id))->type == e_dw_subroutine_type){
189 addr_pointed1 = *((void **)(area1));
190 addr_pointed2 = *((void **)(area2));
191 return (addr_pointed1 != addr_pointed2);
193 addr_pointed1 = *((void **)(area1));
194 addr_pointed2 = *((void **)(area2));
196 if(addr_pointed1 == NULL && addr_pointed2 == NULL)
198 if(already_compared_pointers(addr_pointed1, addr_pointed2) != -1)
200 add_compared_pointers(addr_pointed1, addr_pointed2);
204 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){
205 return compare_heap_area(addr_pointed1, addr_pointed2, NULL, types, other_types, type->dw_type_id, pointer_level);
206 }else if(addr_pointed1 > start_data && (char*)addr_pointed1 <= (char *)start_data + region_size && addr_pointed2 > start_data && (char*)addr_pointed2 <= (char *)start_data + region_size){
207 if(type->dw_type_id == NULL)
208 return (addr_pointed1 != addr_pointed2);
210 return compare_areas_with_type(addr_pointed1, addr_pointed2, types, other_types, type->dw_type_id, region_size, region_type, start_data, pointer_level);
212 return (addr_pointed1 != addr_pointed2);
216 case e_dw_structure_type:
217 xbt_dynar_foreach(type->members, cursor, member){
218 res = compare_areas_with_type((char *)area1 + member->offset, (char *)area2 + member->offset, types, other_types, member->dw_type_id, region_size, region_type, start_data, pointer_level);
223 case e_dw_subroutine_type:
227 XBT_VERB("Unknown case : %d", type->type);
234 static int compare_global_variables(int region_type, mc_mem_region_t r1, mc_mem_region_t r2){
236 if(!compared_pointers){
237 compared_pointers = xbt_dynar_new(sizeof(pointers_pair_t), pointers_pair_free_voidp);
238 MC_ignore_global_variable("compared_pointers");
240 xbt_dynar_reset(compared_pointers);
243 xbt_dynar_t variables;
244 xbt_dict_t types, other_types;
246 unsigned int cursor = 0;
247 dw_variable_t current_var;
251 if(region_type == 2){
252 variables = mc_global_variables_binary;
253 types = mc_variables_type_binary;
254 other_types = mc_variables_type_libsimgrid;
255 start_data = start_data_binary;
257 variables = mc_global_variables_libsimgrid;
258 types = mc_variables_type_libsimgrid;
259 other_types = mc_variables_type_binary;
260 start_data = start_data_libsimgrid;
263 xbt_dynar_foreach(variables, cursor, current_var){
266 offset = (char *)current_var->address.address - (char *)start_data_binary;
268 offset = (char *)current_var->address.address - (char *)start_data_libsimgrid;
270 res = compare_areas_with_type((char *)r1->data + offset, (char *)r2->data + offset, types, other_types, current_var->type_origin, r1->size, region_type, start_data, 0);
272 XBT_VERB("Global variable %s (%p - %p) is different between snapshots", current_var->name, (char *)r1->data + offset, (char *)r2->data + offset);
273 xbt_dynar_free(&compared_pointers);
274 compared_pointers = NULL;
280 xbt_dynar_free(&compared_pointers);
281 compared_pointers = NULL;
287 static int compare_local_variables(mc_snapshot_stack_t stack1, mc_snapshot_stack_t stack2, void *heap1, void *heap2){
289 if(!compared_pointers){
290 compared_pointers = xbt_dynar_new(sizeof(pointers_pair_t), pointers_pair_free_voidp);
291 MC_ignore_global_variable("compared_pointers");
293 xbt_dynar_reset(compared_pointers);
296 if(xbt_dynar_length(stack1->local_variables) != xbt_dynar_length(stack2->local_variables)){
297 XBT_VERB("Different number of local variables");
298 xbt_dynar_free(&compared_pointers);
299 compared_pointers = NULL;
302 unsigned int cursor = 0;
303 local_variable_t current_var1, current_var2;
304 int offset1, offset2, res;
305 while(cursor < xbt_dynar_length(stack1->local_variables)){
306 current_var1 = (local_variable_t)xbt_dynar_get_as(stack1->local_variables, cursor, local_variable_t);
307 current_var2 = (local_variable_t)xbt_dynar_get_as(stack2->local_variables, cursor, local_variable_t);
308 if(strcmp(current_var1->name, current_var2->name) != 0 || strcmp(current_var1->frame, current_var2->frame) != 0 || current_var1->ip != current_var2->ip){
309 xbt_dynar_free(&compared_pointers);
312 offset1 = (char *)current_var1->address - (char *)std_heap;
313 offset2 = (char *)current_var2->address - (char *)std_heap;
314 if(current_var1->region == 1)
315 res = compare_areas_with_type( (char *)heap1 + offset1, (char *)heap2 + offset2, mc_variables_type_libsimgrid, mc_variables_type_binary, current_var1->type, 0, 1, start_data_libsimgrid, 0);
317 res = compare_areas_with_type( (char *)heap1 + offset1, (char *)heap2 + offset2, mc_variables_type_binary, mc_variables_type_libsimgrid, current_var1->type, 0, 2, start_data_binary, 0);
319 XBT_VERB("Local variable %s (%p - %p) in frame %s is different between snapshots", current_var1->name,(char *)heap1 + offset1, (char *)heap2 + offset2, current_var1->frame);
320 xbt_dynar_free(&compared_pointers);
321 compared_pointers = NULL;
326 xbt_dynar_free(&compared_pointers);
327 compared_pointers = NULL;
332 int snapshot_compare(void *p1, void *p2){
334 mc_snapshot_t s1 = ((mc_pair_t)p1)->graph_state->system_state;
335 mc_snapshot_t s2 = ((mc_pair_t)p2)->graph_state->system_state;
340 xbt_os_timer_t global_timer = xbt_os_timer_new();
341 xbt_os_timer_t timer = xbt_os_timer_new();
343 xbt_os_walltimer_start(global_timer);
346 xbt_os_walltimer_start(timer);
349 /* Compare size of stacks */
351 size_t size_used1, size_used2;
353 while(i < xbt_dynar_length(s1->stacks)){
354 size_used1 = s1->stack_sizes[i];
355 size_used2 = s2->stack_sizes[i];
356 if(size_used1 != size_used2){
359 xbt_os_walltimer_stop(timer);
360 mc_comp_times->stacks_sizes_comparison_time = xbt_os_timer_elapsed(timer);
362 XBT_DEBUG("(%d - %d) Different size used in stacks : %zu - %zu", ((mc_pair_t)p1)->num, ((mc_pair_t)p2)->num, size_used1, size_used2);
367 XBT_VERB("(%d - %d) Different size used in stacks : %zu - %zu", ((mc_pair_t)p1)->num, ((mc_pair_t)p2)->num, size_used1, size_used2);
370 xbt_os_walltimer_stop(timer);
371 xbt_os_timer_free(timer);
372 xbt_os_walltimer_stop(global_timer);
373 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
374 xbt_os_timer_free(global_timer);
384 xbt_os_walltimer_stop(timer);
385 xbt_os_walltimer_start(timer);
388 /* Compare hash of global variables */
389 if(s1->hash_global != NULL && s2->hash_global != NULL){
390 if(strcmp(s1->hash_global, s2->hash_global) != 0){
392 xbt_os_walltimer_stop(timer);
393 mc_comp_times->hash_global_variables_comparison_time = xbt_os_timer_elapsed(timer);
394 XBT_DEBUG("Different hash of global variables : %s - %s", s1->hash_global, s2->hash_global);
398 XBT_VERB("Different hash of global variables : %s - %s", s1->hash_global, s2->hash_global);
401 xbt_os_walltimer_stop(timer);
402 xbt_os_timer_free(timer);
403 xbt_os_walltimer_stop(global_timer);
404 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
405 xbt_os_timer_free(global_timer);
413 xbt_os_walltimer_start(timer);
416 /* Compare hash of local variables */
417 if(s1->hash_local != NULL && s2->hash_local != NULL){
418 if(strcmp(s1->hash_local, s2->hash_local) != 0){
420 xbt_os_walltimer_stop(timer);
421 mc_comp_times->hash_local_variables_comparison_time = xbt_os_timer_elapsed(timer);
422 XBT_DEBUG("Different hash of local variables : %s - %s", s1->hash_local, s2->hash_local);
426 XBT_VERB("Different hash of local variables : %s - %s", s1->hash_local, s2->hash_local);
429 xbt_os_walltimer_stop(timer);
430 xbt_os_timer_free(timer);
431 xbt_os_walltimer_stop(global_timer);
432 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
433 xbt_os_timer_free(global_timer);
441 xbt_os_walltimer_start(timer);
444 /* Init heap information used in heap comparison algorithm */
445 res_init = init_heap_information((xbt_mheap_t)s1->regions[0]->data, (xbt_mheap_t)s2->regions[0]->data, s1->to_ignore, s2->to_ignore);
448 XBT_DEBUG("(%d - %d) Different heap information", ((mc_pair_t)p1)->num, ((mc_pair_t)p2)->num);
452 XBT_VERB("(%d - %d) Different heap information", ((mc_pair_t)p1)->num, ((mc_pair_t)p2)->num);
455 xbt_os_walltimer_stop(global_timer);
456 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
457 xbt_os_timer_free(global_timer);
464 xbt_os_walltimer_start(timer);
467 /* Stacks comparison */
468 unsigned int cursor = 0;
471 mc_snapshot_stack_t stack1, stack2;
473 while(cursor < xbt_dynar_length(s1->stacks)){
474 stack1 = (mc_snapshot_stack_t)xbt_dynar_get_as(s1->stacks, cursor, mc_snapshot_stack_t);
475 stack2 = (mc_snapshot_stack_t)xbt_dynar_get_as(s2->stacks, cursor, mc_snapshot_stack_t);
476 diff_local = compare_local_variables(stack1, stack2, s1->regions[0]->data, s2->regions[0]->data);
480 xbt_os_walltimer_stop(timer);
481 mc_comp_times->stacks_comparison_time = xbt_os_timer_elapsed(timer);
483 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", ((mc_pair_t)p1)->num, ((mc_pair_t)p2)->num, cursor + 1);
489 XBT_VERB("(%d - %d) Different local variables between stacks %d", ((mc_pair_t)p1)->num, ((mc_pair_t)p2)->num, cursor + 1);
492 reset_heap_information();
493 xbt_os_walltimer_stop(timer);
494 xbt_os_timer_free(timer);
495 xbt_os_walltimer_stop(global_timer);
496 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
497 xbt_os_timer_free(global_timer);
507 xbt_os_walltimer_stop(timer);
508 xbt_os_walltimer_start(timer);
511 /* Compare binary global variables */
512 is_diff = compare_global_variables(2, s1->regions[2], s2->regions[2]);
515 xbt_os_walltimer_stop(timer);
516 mc_comp_times->binary_global_variables_comparison_time = xbt_os_timer_elapsed(timer);
517 XBT_DEBUG("(%d - %d) Different global variables in binary", ((mc_pair_t)p1)->num, ((mc_pair_t)p2)->num);
521 XBT_VERB("(%d - %d) Different global variables in binary", ((mc_pair_t)p1)->num, ((mc_pair_t)p2)->num);
524 reset_heap_information();
525 xbt_os_walltimer_stop(timer);
526 xbt_os_timer_free(timer);
527 xbt_os_walltimer_stop(global_timer);
528 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
529 xbt_os_timer_free(global_timer);
537 xbt_os_walltimer_stop(timer);
538 xbt_os_walltimer_start(timer);
541 /* Compare libsimgrid global variables */
542 is_diff = compare_global_variables(1, s1->regions[1], s2->regions[1]);
545 xbt_os_walltimer_stop(timer);
546 mc_comp_times->libsimgrid_global_variables_comparison_time = xbt_os_timer_elapsed(timer);
547 XBT_DEBUG("(%d - %d) Different global variables in libsimgrid", ((mc_pair_t)p1)->num, ((mc_pair_t)p2)->num);
551 XBT_VERB("(%d - %d) Different global variables in libsimgrid", ((mc_pair_t)p1)->num, ((mc_pair_t)p2)->num);
554 reset_heap_information();
555 xbt_os_walltimer_stop(timer);
556 xbt_os_timer_free(timer);
557 xbt_os_walltimer_stop(global_timer);
558 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
559 xbt_os_timer_free(global_timer);
566 xbt_os_walltimer_start(timer);
570 if(mmalloc_compare_heap((xbt_mheap_t)s1->regions[0]->data, (xbt_mheap_t)s2->regions[0]->data) > 0){
573 xbt_os_walltimer_stop(timer);
574 mc_comp_times->heap_comparison_time = xbt_os_timer_elapsed(timer);
575 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", ((mc_pair_t)p1)->num, ((mc_pair_t)p2)->num);
580 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", ((mc_pair_t)p1)->num, ((mc_pair_t)p2)->num);
583 reset_heap_information();
584 xbt_os_walltimer_stop(timer);
585 xbt_os_timer_free(timer);
586 xbt_os_walltimer_stop(global_timer);
587 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
588 xbt_os_timer_free(global_timer);
594 xbt_os_walltimer_stop(timer);
598 reset_heap_information();
600 xbt_os_walltimer_stop(timer);
601 xbt_os_timer_free(timer);
604 xbt_os_walltimer_stop(global_timer);
605 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
608 xbt_os_timer_free(global_timer);
611 print_comparison_times();
618 /***************************** Statistics *****************************/
619 /*******************************************************************/
621 void print_comparison_times(){
622 XBT_DEBUG("*** Comparison times ***");
623 XBT_DEBUG("- Nb processes : %f", mc_comp_times->nb_processes_comparison_time);
624 XBT_DEBUG("- Nb bytes used : %f", mc_comp_times->bytes_used_comparison_time);
625 XBT_DEBUG("- Stacks sizes : %f", mc_comp_times->stacks_sizes_comparison_time);
626 XBT_DEBUG("- Binary global variables : %f", mc_comp_times->binary_global_variables_comparison_time);
627 XBT_DEBUG("- Libsimgrid global variables : %f", mc_comp_times->libsimgrid_global_variables_comparison_time);
628 XBT_DEBUG("- Heap : %f", mc_comp_times->heap_comparison_time);
629 XBT_DEBUG("- Stacks : %f", mc_comp_times->stacks_comparison_time);
632 /**************************** MC snapshot compare simcall **************************/
633 /***********************************************************************************/
635 int SIMIX_pre_mc_compare_snapshots(smx_simcall_t simcall,
636 mc_snapshot_t s1, mc_snapshot_t s2){
637 return snapshot_compare(s1, s2);
640 int MC_compare_snapshots(void *s1, void *s2){
642 MC_ignore_local_variable("self", "simcall_BODY_mc_snapshot");
643 return simcall_mc_compare_snapshots(s1, s2);