1 /* Copyright (c) 2012-2013. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "mc_private.h"
9 #include "xbt/mmalloc.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, mc,
12 "Logging specific to mc_compare");
14 typedef struct s_pointers_pair{
17 }s_pointers_pair_t, *pointers_pair_t;
19 __thread xbt_dynar_t compared_pointers;
21 /************************** Free functions ****************************/
22 /********************************************************************/
24 static void stack_region_free(stack_region_t s){
26 xbt_free(s->process_name);
31 static void stack_region_free_voidp(void *s){
32 stack_region_free((stack_region_t) * (void **) s);
35 static void pointers_pair_free(pointers_pair_t p){
39 static void pointers_pair_free_voidp(void *p){
40 pointers_pair_free((pointers_pair_t) * (void **)p);
43 /************************** Snapshot comparison *******************************/
44 /******************************************************************************/
46 static int already_compared_pointers(void *p1, void *p2){
48 if(xbt_dynar_is_empty(compared_pointers))
51 unsigned int cursor = 0;
53 int end = xbt_dynar_length(compared_pointers) - 1;
57 cursor = (start + end) / 2;
58 pair = (pointers_pair_t)xbt_dynar_get_as(compared_pointers, cursor, pointers_pair_t);
62 else if(pair->p2 < p2)
66 }else if(pair->p1 < p1){
77 static void add_compared_pointers(void *p1, void *p2){
79 pointers_pair_t new_pair = xbt_new0(s_pointers_pair_t, 1);
83 if(xbt_dynar_is_empty(compared_pointers)){
84 xbt_dynar_push(compared_pointers, &new_pair);
88 unsigned int cursor = 0;
90 int end = xbt_dynar_length(compared_pointers) - 1;
91 pointers_pair_t pair = NULL;
94 cursor = (start + end) / 2;
95 pair = (pointers_pair_t)xbt_dynar_get_as(compared_pointers, cursor, pointers_pair_t);
98 pointers_pair_free(new_pair);
100 }else if(pair->p2 < p2)
104 }else if(pair->p1 < p1){
113 xbt_dynar_insert_at(compared_pointers, cursor + 1, &new_pair);
115 xbt_dynar_insert_at(compared_pointers, cursor, &new_pair);
118 xbt_dynar_insert_at(compared_pointers, cursor + 1, &new_pair);
120 xbt_dynar_insert_at(compared_pointers, cursor, &new_pair);
125 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){
127 dw_type_t type = xbt_dict_get_or_null(types, type_id);;
128 unsigned int cursor = 0;
129 dw_type_t member, subtype, subsubtype;
130 int elm_size, i, res, switch_types = 0;
131 void *addr_pointed1, *addr_pointed2;
135 case e_dw_enumeration_type:
136 case e_dw_union_type:
137 return (memcmp(area1, area2, type->size) != 0);
140 case e_dw_volatile_type:
141 return compare_areas_with_type(area1, area2, types, other_types, type->dw_type_id, region_size, region_type, start_data, pointer_level);
143 case e_dw_const_type: /* Const variable cannot be modified */
146 case e_dw_array_type:
147 subtype = xbt_dict_get_or_null(types, type->dw_type_id);
148 switch(subtype->type){
150 case e_dw_enumeration_type:
151 case e_dw_pointer_type:
152 case e_dw_structure_type:
153 case e_dw_union_type:
154 if(subtype->size == 0){ /*declaration of the type, need the complete description */
155 subtype = xbt_dict_get_or_null(types, get_type_description(types, subtype->name));
157 subtype = xbt_dict_get_or_null(other_types, get_type_description(other_types, subtype->name));
161 elm_size = subtype->size;
164 case e_dw_volatile_type:
165 subsubtype = xbt_dict_get_or_null(types, subtype->dw_type_id);
166 if(subsubtype->size == 0){ /*declaration of the type, need the complete description */
167 subsubtype = xbt_dict_get_or_null(types, get_type_description(types, subsubtype->name));
168 if(subsubtype == NULL){
169 subsubtype = xbt_dict_get_or_null(other_types, get_type_description(other_types, subsubtype->name));
173 elm_size = subsubtype->size;
179 for(i=0; i<type->size; i++){
181 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);
183 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);
188 case e_dw_pointer_type:
189 if(type->dw_type_id && ((dw_type_t)xbt_dict_get_or_null(types, type->dw_type_id))->type == e_dw_subroutine_type){
190 addr_pointed1 = *((void **)(area1));
191 addr_pointed2 = *((void **)(area2));
192 return (addr_pointed1 != addr_pointed2);
194 addr_pointed1 = *((void **)(area1));
195 addr_pointed2 = *((void **)(area2));
197 if(addr_pointed1 == NULL && addr_pointed2 == NULL)
199 if(already_compared_pointers(addr_pointed1, addr_pointed2) != -1)
201 add_compared_pointers(addr_pointed1, addr_pointed2);
205 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){
206 return compare_heap_area(addr_pointed1, addr_pointed2, NULL, types, other_types, type->dw_type_id, pointer_level);
207 }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){
208 if(type->dw_type_id == NULL)
209 return (addr_pointed1 != addr_pointed2);
211 return compare_areas_with_type(addr_pointed1, addr_pointed2, types, other_types, type->dw_type_id, region_size, region_type, start_data, pointer_level);
213 return (addr_pointed1 != addr_pointed2);
217 case e_dw_structure_type:
218 xbt_dynar_foreach(type->members, cursor, member){
219 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);
224 case e_dw_subroutine_type:
228 XBT_VERB("Unknown case : %d", type->type);
235 static int compare_global_variables(int region_type, mc_mem_region_t r1, mc_mem_region_t r2){
237 if(!compared_pointers){
238 compared_pointers = xbt_dynar_new(sizeof(pointers_pair_t), pointers_pair_free_voidp);
239 MC_ignore_global_variable("compared_pointers");
241 xbt_dynar_reset(compared_pointers);
244 xbt_dynar_t variables;
245 xbt_dict_t types, other_types;
247 unsigned int cursor = 0;
248 dw_variable_t current_var;
252 if(region_type == 2){
253 variables = mc_global_variables_binary;
254 types = mc_variables_type_binary;
255 other_types = mc_variables_type_libsimgrid;
256 start_data = start_data_binary;
258 variables = mc_global_variables_libsimgrid;
259 types = mc_variables_type_libsimgrid;
260 other_types = mc_variables_type_binary;
261 start_data = start_data_libsimgrid;
264 xbt_dynar_foreach(variables, cursor, current_var){
267 offset = (char *)current_var->address.address - (char *)start_data_binary;
269 offset = (char *)current_var->address.address - (char *)start_data_libsimgrid;
271 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);
273 XBT_VERB("Global variable %s (%p - %p) is different between snapshots", current_var->name, (char *)r1->data + offset, (char *)r2->data + offset);
274 xbt_dynar_free(&compared_pointers);
275 compared_pointers = NULL;
281 xbt_dynar_free(&compared_pointers);
282 compared_pointers = NULL;
288 static int compare_local_variables(mc_snapshot_stack_t stack1, mc_snapshot_stack_t stack2, void *heap1, void *heap2){
290 if(!compared_pointers){
291 compared_pointers = xbt_dynar_new(sizeof(pointers_pair_t), pointers_pair_free_voidp);
292 MC_ignore_global_variable("compared_pointers");
294 xbt_dynar_reset(compared_pointers);
297 if(xbt_dynar_length(stack1->local_variables) != xbt_dynar_length(stack2->local_variables)){
298 XBT_VERB("Different number of local variables");
299 xbt_dynar_free(&compared_pointers);
300 compared_pointers = NULL;
303 unsigned int cursor = 0;
304 local_variable_t current_var1, current_var2;
305 int offset1, offset2, res;
306 while(cursor < xbt_dynar_length(stack1->local_variables)){
307 current_var1 = (local_variable_t)xbt_dynar_get_as(stack1->local_variables, cursor, local_variable_t);
308 current_var2 = (local_variable_t)xbt_dynar_get_as(stack2->local_variables, cursor, local_variable_t);
309 if(strcmp(current_var1->name, current_var2->name) != 0 || strcmp(current_var1->frame, current_var2->frame) != 0 || current_var1->ip != current_var2->ip){
310 xbt_dynar_free(&compared_pointers);
313 offset1 = (char *)current_var1->address - (char *)std_heap;
314 offset2 = (char *)current_var2->address - (char *)std_heap;
315 if(current_var1->region == 1)
316 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);
318 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);
320 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);
321 xbt_dynar_free(&compared_pointers);
322 compared_pointers = NULL;
327 xbt_dynar_free(&compared_pointers);
328 compared_pointers = NULL;
333 int snapshot_compare(void *state1, void *state2){
335 mc_snapshot_t s1, s2;
338 if(_sg_mc_property_file && _sg_mc_property_file[0] != '\0'){ /* Liveness MC */
339 s1 = ((mc_pair_t)state1)->graph_state->system_state;
340 s2 = ((mc_pair_t)state2)->graph_state->system_state;
341 num1 = ((mc_pair_t)state1)->num;
342 num2 = ((mc_pair_t)state2)->num;
343 /* Firstly compare automaton state */
344 if(xbt_automaton_state_compare(((mc_pair_t)state1)->automaton_state, ((mc_pair_t)state2)->automaton_state) != 0)
346 if(xbt_automaton_propositional_symbols_compare_value(((mc_pair_t)state1)->atomic_propositions, ((mc_pair_t)state2)->atomic_propositions) != 0)
348 }else{ /* Safety MC */
349 s1 = ((mc_visited_state_t)state1)->system_state;
350 s2 = ((mc_visited_state_t)state2)->system_state;
351 num1 = ((mc_visited_state_t)state1)->num;
352 num2 = ((mc_visited_state_t)state2)->num;
358 xbt_os_timer_t global_timer = xbt_os_timer_new();
359 xbt_os_timer_t timer = xbt_os_timer_new();
361 xbt_os_walltimer_start(global_timer);
364 xbt_os_walltimer_start(timer);
367 /* Compare size of stacks */
369 size_t size_used1, size_used2;
371 while(i < xbt_dynar_length(s1->stacks)){
372 size_used1 = s1->stack_sizes[i];
373 size_used2 = s2->stack_sizes[i];
374 if(size_used1 != size_used2){
377 xbt_os_walltimer_stop(timer);
378 mc_comp_times->stacks_sizes_comparison_time = xbt_os_timer_elapsed(timer);
380 XBT_DEBUG("(%d - %d) Different size used in stacks : %zu - %zu", num1, num2, size_used1, size_used2);
385 XBT_VERB("(%d - %d) Different size used in stacks : %zu - %zu", num1, num2, size_used1, size_used2);
388 xbt_os_walltimer_stop(timer);
389 xbt_os_timer_free(timer);
390 xbt_os_walltimer_stop(global_timer);
391 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
392 xbt_os_timer_free(global_timer);
402 xbt_os_walltimer_stop(timer);
403 xbt_os_walltimer_start(timer);
406 /* Compare hash of global variables */
407 if(s1->hash_global != NULL && s2->hash_global != NULL){
408 if(strcmp(s1->hash_global, s2->hash_global) != 0){
410 xbt_os_walltimer_stop(timer);
411 mc_comp_times->hash_global_variables_comparison_time = xbt_os_timer_elapsed(timer);
412 XBT_DEBUG("Different hash of global variables : %s - %s", s1->hash_global, s2->hash_global);
416 XBT_VERB("Different hash of global variables : %s - %s", s1->hash_global, s2->hash_global);
419 xbt_os_walltimer_stop(timer);
420 xbt_os_timer_free(timer);
421 xbt_os_walltimer_stop(global_timer);
422 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
423 xbt_os_timer_free(global_timer);
431 xbt_os_walltimer_start(timer);
434 /* Compare hash of local variables */
435 if(s1->hash_local != NULL && s2->hash_local != NULL){
436 if(strcmp(s1->hash_local, s2->hash_local) != 0){
438 xbt_os_walltimer_stop(timer);
439 mc_comp_times->hash_local_variables_comparison_time = xbt_os_timer_elapsed(timer);
440 XBT_DEBUG("Different hash of local variables : %s - %s", s1->hash_local, s2->hash_local);
444 XBT_VERB("Different hash of local variables : %s - %s", s1->hash_local, s2->hash_local);
447 xbt_os_walltimer_stop(timer);
448 xbt_os_timer_free(timer);
449 xbt_os_walltimer_stop(global_timer);
450 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
451 xbt_os_timer_free(global_timer);
459 xbt_os_walltimer_start(timer);
462 /* Init heap information used in heap comparison algorithm */
463 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);
466 XBT_DEBUG("(%d - %d) Different heap information", num1, num2);
470 XBT_VERB("(%d - %d) Different heap information", num1, num2);
473 xbt_os_walltimer_stop(global_timer);
474 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
475 xbt_os_timer_free(global_timer);
482 xbt_os_walltimer_start(timer);
485 /* Stacks comparison */
486 unsigned int cursor = 0;
489 mc_snapshot_stack_t stack1, stack2;
491 while(cursor < xbt_dynar_length(s1->stacks)){
492 stack1 = (mc_snapshot_stack_t)xbt_dynar_get_as(s1->stacks, cursor, mc_snapshot_stack_t);
493 stack2 = (mc_snapshot_stack_t)xbt_dynar_get_as(s2->stacks, cursor, mc_snapshot_stack_t);
494 diff_local = compare_local_variables(stack1, stack2, s1->regions[0]->data, s2->regions[0]->data);
498 xbt_os_walltimer_stop(timer);
499 mc_comp_times->stacks_comparison_time = xbt_os_timer_elapsed(timer);
501 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1, num2, cursor + 1);
507 XBT_VERB("(%d - %d) Different local variables between stacks %d", num1, num2, cursor + 1);
510 reset_heap_information();
511 xbt_os_walltimer_stop(timer);
512 xbt_os_timer_free(timer);
513 xbt_os_walltimer_stop(global_timer);
514 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
515 xbt_os_timer_free(global_timer);
525 xbt_os_walltimer_stop(timer);
526 xbt_os_walltimer_start(timer);
529 /* Compare binary global variables */
530 is_diff = compare_global_variables(2, s1->regions[2], s2->regions[2]);
533 xbt_os_walltimer_stop(timer);
534 mc_comp_times->binary_global_variables_comparison_time = xbt_os_timer_elapsed(timer);
535 XBT_DEBUG("(%d - %d) Different global variables in binary", num1, num2);
539 XBT_VERB("(%d - %d) Different global variables in binary", num1, num2);
542 reset_heap_information();
543 xbt_os_walltimer_stop(timer);
544 xbt_os_timer_free(timer);
545 xbt_os_walltimer_stop(global_timer);
546 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
547 xbt_os_timer_free(global_timer);
555 xbt_os_walltimer_stop(timer);
556 xbt_os_walltimer_start(timer);
559 /* Compare libsimgrid global variables */
560 is_diff = compare_global_variables(1, s1->regions[1], s2->regions[1]);
563 xbt_os_walltimer_stop(timer);
564 mc_comp_times->libsimgrid_global_variables_comparison_time = xbt_os_timer_elapsed(timer);
565 XBT_DEBUG("(%d - %d) Different global variables in libsimgrid", num1, num2);
569 XBT_VERB("(%d - %d) Different global variables in libsimgrid", num1, num2);
572 reset_heap_information();
573 xbt_os_walltimer_stop(timer);
574 xbt_os_timer_free(timer);
575 xbt_os_walltimer_stop(global_timer);
576 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
577 xbt_os_timer_free(global_timer);
584 xbt_os_walltimer_start(timer);
588 if(mmalloc_compare_heap((xbt_mheap_t)s1->regions[0]->data, (xbt_mheap_t)s2->regions[0]->data) > 0){
591 xbt_os_walltimer_stop(timer);
592 mc_comp_times->heap_comparison_time = xbt_os_timer_elapsed(timer);
593 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
598 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
601 reset_heap_information();
602 xbt_os_walltimer_stop(timer);
603 xbt_os_timer_free(timer);
604 xbt_os_walltimer_stop(global_timer);
605 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
606 xbt_os_timer_free(global_timer);
612 xbt_os_walltimer_stop(timer);
616 reset_heap_information();
618 xbt_os_walltimer_stop(timer);
619 xbt_os_timer_free(timer);
622 xbt_os_walltimer_stop(global_timer);
623 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
626 xbt_os_timer_free(global_timer);
629 print_comparison_times();
636 /***************************** Statistics *****************************/
637 /*******************************************************************/
639 void print_comparison_times(){
640 XBT_DEBUG("*** Comparison times ***");
641 XBT_DEBUG("- Nb processes : %f", mc_comp_times->nb_processes_comparison_time);
642 XBT_DEBUG("- Nb bytes used : %f", mc_comp_times->bytes_used_comparison_time);
643 XBT_DEBUG("- Stacks sizes : %f", mc_comp_times->stacks_sizes_comparison_time);
644 XBT_DEBUG("- Binary global variables : %f", mc_comp_times->binary_global_variables_comparison_time);
645 XBT_DEBUG("- Libsimgrid global variables : %f", mc_comp_times->libsimgrid_global_variables_comparison_time);
646 XBT_DEBUG("- Heap : %f", mc_comp_times->heap_comparison_time);
647 XBT_DEBUG("- Stacks : %f", mc_comp_times->stacks_comparison_time);
650 /**************************** MC snapshot compare simcall **************************/
651 /***********************************************************************************/
653 int SIMIX_pre_mc_compare_snapshots(smx_simcall_t simcall,
654 mc_snapshot_t s1, mc_snapshot_t s2){
655 return snapshot_compare(s1, s2);
658 int MC_compare_snapshots(void *s1, void *s2){
660 MC_ignore_local_variable("self", "simcall_BODY_mc_snapshot");
661 return simcall_mc_compare_snapshots(s1, s2);