4 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_liveness, mc,
5 "Logging specific to algorithms for liveness properties verification");
7 xbt_dynar_t initial_pairs = NULL;
8 xbt_dynar_t reached_pairs;
9 extern mc_snapshot_t initial_snapshot;
11 mc_pair_t new_pair(mc_snapshot_t sn, mc_state_t sg, xbt_state_t st){
13 p = xbt_new0(s_mc_pair_t, 1);
15 p->automaton_state = st;
17 mc_stats_pair->expanded_pairs++;
22 int snapshot_compare(mc_snapshot_t s1, mc_snapshot_t s2){
24 if(s1->num_reg != s2->num_reg)
29 for(i=0 ; i< s1->num_reg ; i++){
31 if(s1->regions[i]->size != s2->regions[i]->size)
34 if(s1->regions[i]->start_addr != s2->regions[i]->start_addr)
37 if(s1->regions[i]->type != s2->regions[i]->type)
40 if(s1->regions[i]->type == 0){
41 if(mmalloc_compare_heap(s1->regions[i]->start_addr, s2->regions[i]->start_addr)){
42 XBT_DEBUG("Different heap (mmalloc_compare)");
47 if(memcmp(s1->regions[i]->data, s2->regions[i]->data, s1->regions[i]->size) != 0){
48 XBT_DEBUG("Different memcmp for data in libsimgrid (%d)", i);
61 int reached(xbt_automaton_t a, mc_snapshot_t s){
63 if(xbt_dynar_is_empty(reached_pairs)){
68 mc_pair_reached_t pair = NULL;
69 pair = xbt_new0(s_mc_pair_reached_t, 1);
70 pair->automaton_state = a->current_state;
71 pair->prop_ato = xbt_dynar_new(sizeof(int), NULL);
72 pair->system_state = s;
74 /* Get values of propositional symbols */
75 unsigned int cursor = 0;
76 xbt_propositional_symbol_t ps = NULL;
77 xbt_dynar_foreach(a->propositional_symbols, cursor, ps){
78 int (*f)() = ps->function;
80 xbt_dynar_push(pair->prop_ato, &res);
84 mc_pair_reached_t pair_test;
85 int (*compare_dynar)(const void*; const void*) = propositional_symbols_compare;
87 xbt_dynar_foreach(reached_pairs, cursor, pair_test){
88 if(automaton_state_compare(pair_test->automaton_state, pair->automaton_state) == 0){
89 if(xbt_dynar_compare(pair_test->prop_ato, pair->prop_ato, compare_dynar) == 0){
90 if(snapshot_compare(pair_test->system_state, pair->system_state) == 0){
104 int set_pair_reached(xbt_automaton_t a, mc_snapshot_t s){
106 if(reached(a, s) == 0){
110 mc_pair_reached_t pair = NULL;
111 pair = xbt_new0(s_mc_pair_reached_t, 1);
112 pair->automaton_state = a->current_state;
113 pair->prop_ato = xbt_dynar_new(sizeof(int), NULL);
114 pair->system_state = s;
116 /* Get values of propositional symbols */
117 unsigned int cursor = 0;
118 xbt_propositional_symbol_t ps = NULL;
119 xbt_dynar_foreach(a->propositional_symbols, cursor, ps){
120 int (*f)() = ps->function;
122 xbt_dynar_push(pair->prop_ato, &res);
125 xbt_dynar_push(reached_pairs, &pair);
136 void MC_pair_delete(mc_pair_t pair){
137 xbt_free(pair->graph_state->proc_status);
138 xbt_free(pair->graph_state);
139 //xbt_free(pair->automaton_state); -> FIXME : à implémenter
144 int MC_automaton_evaluate_label(xbt_automaton_t a, xbt_exp_label_t l){
148 int left_res = MC_automaton_evaluate_label(a, l->u.or_and.left_exp);
149 int right_res = MC_automaton_evaluate_label(a, l->u.or_and.right_exp);
150 return (left_res || right_res);
154 int left_res = MC_automaton_evaluate_label(a, l->u.or_and.left_exp);
155 int right_res = MC_automaton_evaluate_label(a, l->u.or_and.right_exp);
156 return (left_res && right_res);
160 int res = MC_automaton_evaluate_label(a, l->u.exp_not);
165 unsigned int cursor = 0;
166 xbt_propositional_symbol_t p = NULL;
167 xbt_dynar_foreach(a->propositional_symbols, cursor, p){
168 if(strcmp(p->pred, l->u.predicat) == 0){
169 int (*f)() = p->function;
189 /********************* Double-DFS stateless *******************/
191 void MC_pair_stateless_delete(mc_pair_stateless_t pair){
192 xbt_free(pair->graph_state->proc_status);
193 xbt_free(pair->graph_state);
194 //xbt_free(pair->automaton_state); -> FIXME : à implémenter
198 mc_pair_stateless_t new_pair_stateless(mc_state_t sg, xbt_state_t st){
199 mc_pair_stateless_t p = NULL;
200 p = xbt_new0(s_mc_pair_stateless_t, 1);
201 p->automaton_state = st;
203 mc_stats_pair->expanded_pairs++;
209 void MC_ddfs_stateless_init(xbt_automaton_t a){
211 XBT_DEBUG("**************************************************");
212 XBT_DEBUG("Double-DFS stateless init");
213 XBT_DEBUG("**************************************************");
215 mc_pair_stateless_t mc_initial_pair = NULL;
216 mc_state_t initial_graph_state = NULL;
217 smx_process_t process;
219 MC_wait_for_requests();
222 initial_graph_state = MC_state_pair_new();
223 xbt_swag_foreach(process, simix_global->process_list){
224 if(MC_process_is_enabled(process)){
225 MC_state_interleave_process(initial_graph_state, process);
229 reached_pairs = xbt_dynar_new(sizeof(mc_pair_reached_t), NULL);
231 initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
232 MC_take_snapshot(initial_snapshot);
236 unsigned int cursor = 0;
239 xbt_dynar_foreach(a->states, cursor, state){
240 if(state->type == -1){
243 mc_initial_pair = new_pair_stateless(initial_graph_state, state);
244 xbt_fifo_unshift(mc_stack_liveness_stateless, mc_initial_pair);
248 MC_ddfs_stateless(a, 0, 0);
250 MC_restore_snapshot(initial_snapshot);
252 MC_ddfs_stateless(a, 0, 0);
255 if(state->type == 2){
258 mc_initial_pair = new_pair_stateless(initial_graph_state, state);
259 xbt_fifo_unshift(mc_stack_liveness_stateless, mc_initial_pair);
263 MC_ddfs_stateless(a, 1, 0);
265 MC_restore_snapshot(initial_snapshot);
267 MC_ddfs_stateless(a, 1, 0);
276 void MC_ddfs_stateless(xbt_automaton_t a, int search_cycle, int replay){
278 smx_process_t process;
279 mc_pair_stateless_t current_pair = NULL;
281 if(xbt_fifo_size(mc_stack_liveness_stateless) == 0)
285 MC_replay_liveness(mc_stack_liveness_stateless);
286 current_pair = (mc_pair_stateless_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack_liveness_stateless));
287 xbt_swag_foreach(process, simix_global->process_list){
288 if(MC_process_is_enabled(process)){
289 MC_state_interleave_process(current_pair->graph_state, process);
294 /* Get current pair */
295 current_pair = (mc_pair_stateless_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack_liveness_stateless));
297 /* Update current state in buchi automaton */
298 a->current_state = current_pair->automaton_state;
300 XBT_DEBUG("********************* ( Depth = %d, search_cycle = %d )", xbt_fifo_size(mc_stack_liveness_stateless), search_cycle);
301 XBT_DEBUG("Pair : graph=%p, automaton=%p(%s), %u interleave", current_pair->graph_state, current_pair->automaton_state, current_pair->automaton_state->id,MC_state_interleave_size(current_pair->graph_state));
304 mc_stats_pair->visited_pairs++;
307 mc_state_t next_graph_state = NULL;
308 smx_req_t req = NULL;
311 xbt_transition_t transition_succ;
312 unsigned int cursor = 0;
315 mc_pair_stateless_t next_pair = NULL;
316 mc_pair_stateless_t pair_succ;
317 mc_snapshot_t next_snapshot = NULL;
318 mc_snapshot_t current_snapshot = NULL;
320 xbt_dynar_t successors = NULL;
323 successors = xbt_dynar_new(sizeof(mc_pair_stateless_t), NULL);
326 while((req = MC_state_get_request(current_pair->graph_state, &value)) != NULL){
329 current_snapshot = xbt_new0(s_mc_snapshot_t, 1);
330 MC_take_snapshot(current_snapshot);
333 /* Debug information */
334 if(XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)){
335 req_str = MC_request_to_string(req, value);
336 XBT_DEBUG("Execute: %s", req_str);
342 MC_state_set_executed_request(current_pair->graph_state, req, value);
344 /* Answer the request */
345 SIMIX_request_pre(req, value);
347 /* Wait for requests (schedules processes) */
348 MC_wait_for_requests();
353 /* Create the new expanded graph_state */
354 next_graph_state = MC_state_pair_new();
356 /* Get enabled process and insert it in the interleave set of the next graph_state */
357 xbt_swag_foreach(process, simix_global->process_list){
358 if(MC_process_is_enabled(process)){
359 MC_state_interleave_process(next_graph_state, process);
363 next_snapshot = xbt_new0(s_mc_snapshot_t, 1);
364 MC_take_snapshot(next_snapshot);
366 xbt_dynar_reset(successors);
368 if(snapshot_compare(current_snapshot,next_snapshot)){
369 XBT_DEBUG("Different snapshot");
378 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
380 res = MC_automaton_evaluate_label(a, transition_succ->label);
382 if(res == 1){ // enabled transition in automaton
384 next_pair = new_pair_stateless(next_graph_state, transition_succ->dst);
385 xbt_dynar_push(successors, &next_pair);
393 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
395 res = MC_automaton_evaluate_label(a, transition_succ->label);
397 if(res == 2){ // true transition in automaton
399 next_pair = new_pair_stateless(next_graph_state, transition_succ->dst);
400 xbt_dynar_push(successors, &next_pair);
407 if(xbt_dynar_length(successors) == 0){
409 next_pair = new_pair_stateless(next_graph_state, current_pair->automaton_state);
410 xbt_dynar_push(successors, &next_pair);
416 xbt_dynar_foreach(successors, cursor, pair_succ){
418 if((search_cycle == 1) && (reached(a, next_snapshot) == 1)){
419 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
420 XBT_INFO("| ACCEPTANCE CYCLE |");
421 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
422 XBT_INFO("Counter-example that violates formula :");
423 MC_show_stack_liveness_stateless(mc_stack_liveness_stateless);
424 MC_dump_stack_liveness_stateless(mc_stack_liveness_stateless);
425 MC_print_statistics_pairs(mc_stats_pair);
430 xbt_fifo_unshift(mc_stack_liveness_stateless, pair_succ);
433 MC_ddfs_stateless(a, search_cycle, 0);
436 if((search_cycle == 0) && ((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2))){
438 XBT_DEBUG("Acceptance pair %p : graph=%p, automaton=%p(%s)", pair_succ, pair_succ->graph_state, pair_succ->automaton_state, pair_succ->automaton_state->id);
439 int res = set_pair_reached(a, next_snapshot);
442 xbt_fifo_unshift(mc_stack_liveness_stateless, pair_succ);
445 MC_ddfs_stateless(a, 1, 1);
449 xbt_dynar_pop(reached_pairs, NULL);
455 if(MC_state_interleave_size(current_pair->graph_state) > 0){
456 XBT_DEBUG("Backtracking to depth %u", xbt_fifo_size(mc_stack_liveness_stateless));
457 MC_replay_liveness(mc_stack_liveness_stateless);
463 xbt_fifo_shift(mc_stack_liveness_stateless);
464 XBT_DEBUG("Pair (graph=%p, automaton =%p, search_cycle = %u) shifted in stack", current_pair->graph_state, current_pair->automaton_state, search_cycle);
469 /********************* Double-DFS stateful without visited state *******************/
472 void MC_ddfs_stateful_init(xbt_automaton_t a){
474 XBT_DEBUG("**************************************************");
475 XBT_DEBUG("Double-DFS stateful without visited state init");
476 XBT_DEBUG("**************************************************");
478 mc_pair_t mc_initial_pair;
479 mc_state_t initial_graph_state;
480 smx_process_t process;
481 mc_snapshot_t init_snapshot;
483 MC_wait_for_requests();
487 init_snapshot = xbt_new0(s_mc_snapshot_t, 1);
489 initial_graph_state = MC_state_pair_new();
490 xbt_swag_foreach(process, simix_global->process_list){
491 if(MC_process_is_enabled(process)){
492 MC_state_interleave_process(initial_graph_state, process);
496 reached_pairs = xbt_dynar_new(sizeof(mc_pair_reached_t), NULL);
498 MC_take_snapshot(init_snapshot);
502 unsigned int cursor = 0;
503 xbt_state_t state = NULL;
505 xbt_dynar_foreach(a->states, cursor, state){
506 if(state->type == -1){
509 mc_initial_pair = new_pair(init_snapshot, initial_graph_state, state);
510 xbt_fifo_unshift(mc_stack_liveness_stateful, mc_initial_pair);
514 MC_ddfs_stateful(a, 0, 0);
516 MC_restore_snapshot(init_snapshot);
518 MC_ddfs_stateful(a, 0, 0);
521 if(state->type == 2){
524 mc_initial_pair = new_pair(init_snapshot, initial_graph_state, state);
525 xbt_fifo_unshift(mc_stack_liveness_stateful, mc_initial_pair);
529 MC_ddfs_stateful(a, 1, 0);
531 MC_restore_snapshot(init_snapshot);
533 MC_ddfs_stateful(a, 1, 0);
541 void MC_ddfs_stateful(xbt_automaton_t a, int search_cycle, int restore){
543 smx_process_t process = NULL;
544 mc_pair_t current_pair = NULL;
546 if(xbt_fifo_size(mc_stack_liveness_stateful) == 0)
550 current_pair = (mc_pair_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack_liveness_stateful));
551 MC_restore_snapshot(current_pair->system_state);
552 xbt_swag_foreach(process, simix_global->process_list){
553 if(MC_process_is_enabled(process)){
554 MC_state_interleave_process(current_pair->graph_state, process);
560 /* Get current state */
561 current_pair = (mc_pair_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack_liveness_stateful));
564 XBT_DEBUG("********************* ( Depth = %d, search_cycle = %d )", xbt_fifo_size(mc_stack_liveness_stateful), search_cycle);
565 XBT_DEBUG("Pair : graph=%p, automaton=%p(%s), %u interleave", current_pair->graph_state, current_pair->automaton_state, current_pair->automaton_state->id,MC_state_interleave_size(current_pair->graph_state));
567 a->current_state = current_pair->automaton_state;
569 mc_stats_pair->visited_pairs++;
572 mc_state_t next_graph_state = NULL;
573 smx_req_t req = NULL;
577 xbt_transition_t transition_succ;
581 xbt_dynar_t successors = NULL;
583 mc_pair_t next_pair = NULL;
584 mc_snapshot_t next_snapshot = NULL;
585 mc_snapshot_t current_snapshot = NULL;
589 successors = xbt_dynar_new(sizeof(mc_pair_t), NULL);
592 while((req = MC_state_get_request(current_pair->graph_state, &value)) != NULL){
595 current_snapshot = xbt_new0(s_mc_snapshot_t, 1);
596 MC_take_snapshot(current_snapshot);
600 /* Debug information */
601 if(XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)){
602 req_str = MC_request_to_string(req, value);
603 XBT_DEBUG("Execute: %s", req_str);
607 MC_state_set_executed_request(current_pair->graph_state, req, value);
609 /* Answer the request */
610 SIMIX_request_pre(req, value);
612 /* Wait for requests (schedules processes) */
613 MC_wait_for_requests();
616 /* Create the new expanded graph_state */
619 next_graph_state = MC_state_pair_new();
621 /* Get enabled process and insert it in the interleave set of the next graph_state */
622 xbt_swag_foreach(process, simix_global->process_list){
623 if(MC_process_is_enabled(process)){
624 MC_state_interleave_process(next_graph_state, process);
628 next_snapshot = xbt_new0(s_mc_snapshot_t, 1);
629 MC_take_snapshot(next_snapshot);
631 xbt_dynar_reset(successors);
637 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
639 res = MC_automaton_evaluate_label(a, transition_succ->label);
641 if(res == 1){ // enabled transition in automaton
643 next_pair = new_pair(next_snapshot,next_graph_state, transition_succ->dst);
644 xbt_dynar_push(successors, &next_pair);
651 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
653 res = MC_automaton_evaluate_label(a, transition_succ->label);
655 if(res == 2){ // transition always enabled in automaton
657 next_pair = new_pair(next_snapshot,next_graph_state, transition_succ->dst);
658 xbt_dynar_push(successors, &next_pair);
666 if(xbt_dynar_length(successors) == 0){
669 next_pair = new_pair(next_snapshot, next_graph_state, current_pair->automaton_state);
670 xbt_dynar_push(successors, &next_pair);
675 //XBT_DEBUG("Successors in automaton %lu", xbt_dynar_length(successors));
678 xbt_dynar_foreach(successors, cursor, pair_succ){
680 //XBT_DEBUG("Search visited pair : graph=%p, automaton=%p", pair_succ->graph_state, pair_succ->automaton_state);
682 if((search_cycle == 1) && (reached(a, next_snapshot) == 1)){
683 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
684 XBT_INFO("| ACCEPTANCE CYCLE |");
685 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
686 XBT_INFO("Counter-example that violates formula :");
687 MC_show_stack_liveness_stateful(mc_stack_liveness_stateful);
688 MC_dump_stack_liveness_stateful(mc_stack_liveness_stateful);
689 MC_print_statistics_pairs(mc_stats_pair);
693 //mc_stats_pair->executed_transitions++;
696 xbt_fifo_unshift(mc_stack_liveness_stateful, pair_succ);
699 MC_ddfs_stateful(a, search_cycle, 0);
702 if((search_cycle == 0) && ((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2))){
704 int res = set_pair_reached(a, next_snapshot);
705 XBT_DEBUG("Acceptance pair : graph=%p, automaton=%p(%s)", pair_succ->graph_state, pair_succ->automaton_state, pair_succ->automaton_state->id);
708 xbt_fifo_unshift(mc_stack_liveness_stateful, pair_succ);
711 MC_ddfs_stateful(a, 1, 1);
715 xbt_dynar_pop(reached_pairs, NULL);
722 if(MC_state_interleave_size(current_pair->graph_state) > 0){
723 XBT_DEBUG("Backtracking to depth %u", xbt_fifo_size(mc_stack_liveness_stateful));
724 MC_restore_snapshot(current_snapshot);
732 xbt_fifo_shift(mc_stack_liveness_stateful);
733 XBT_DEBUG("Pair (graph=%p, automaton =%p) shifted in stack", current_pair->graph_state, current_pair->automaton_state);