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;
9 xbt_dynar_t visited_pairs;
10 xbt_dynar_t reached_pairs;
12 mc_pairs_t new_pair(mc_snapshot_t sn, mc_state_t sg, xbt_state_t st){
14 p = xbt_new0(s_mc_pairs_t, 1);
16 p->automaton_state = st;
20 mc_stats_pair->expanded_pairs++;
25 void set_pair_visited(mc_state_t gs, xbt_state_t as, int sc){
27 //XBT_DEBUG("New visited pair : graph=%p, automaton=%p", gs, as);
29 mc_visited_pairs_t p = NULL;
30 p = xbt_new0(s_mc_visited_pairs_t, 1);
32 p->automaton_state = as;
34 xbt_dynar_push(visited_pairs, &p);
35 //XBT_DEBUG("New visited pair , length of visited pairs : %lu", xbt_dynar_length(visited_pairs));
38 //XBT_DEBUG("New visited pair , length of visited pairs : %lu", xbt_dynar_length(visited_pairs));
41 int reached(mc_state_t gs, xbt_state_t as ){
43 mc_reached_pairs_t rp = NULL;
46 int different_process;
49 xbt_dynar_foreach(reached_pairs, c, rp){
50 if(rp->automaton_state == as){
52 for(i=0; i < gs->max_pid; i++){
53 if(gs->proc_status[i].state != rp->graph_state->proc_status[i].state){
57 if(different_process==0){
59 XBT_DEBUG("Pair (graph=%p, automaton=%p(%s)) already reached (graph=%p)!", gs, as, as->id, rp->graph_state);
62 /* XBT_DEBUG("Pair (graph=%p, automaton=%p(%s)) already reached !", gs, as, as->id); */
70 void set_pair_reached(mc_state_t gs, xbt_state_t as){
72 //XBT_DEBUG("Set pair (graph=%p, automaton=%p) reached ", gs, as);
73 //if(reached(gs, as) == 0){
75 mc_reached_pairs_t p = NULL;
76 p = xbt_new0(s_mc_reached_pairs_t, 1);
78 p->automaton_state = as;
79 xbt_dynar_push(reached_pairs, &p);
80 XBT_DEBUG("New reached pair : graph=%p, automaton=%p(%s)", gs, as, as->id);
86 int visited(mc_state_t gs, xbt_state_t as, int sc){
88 mc_visited_pairs_t p = NULL;
90 int different_process;
92 //XBT_DEBUG("Visited pair length : %lu", xbt_dynar_length(visited_pairs));
95 xbt_dynar_foreach(visited_pairs, c, p){
96 //XBT_DEBUG("Test pair visited");
98 if((p->automaton_state == as) && (p->search_cycle == sc)){
100 for(i=0; i < gs->max_pid; i++){
101 if(gs->proc_status[i].state != p->graph_state->proc_status[i].state){
105 if(different_process==0){
116 void MC_dfs_init(xbt_automaton_t a){
118 XBT_DEBUG("**************************************************");
119 XBT_DEBUG("DFS init");
120 XBT_DEBUG("**************************************************");
122 mc_pairs_t mc_initial_pair;
123 mc_state_t initial_graph_state;
124 smx_process_t process;
125 mc_snapshot_t init_snapshot;
127 MC_wait_for_requests();
131 init_snapshot = xbt_new0(s_mc_snapshot_t, 1);
133 initial_graph_state = MC_state_pair_new();
134 xbt_swag_foreach(process, simix_global->process_list){
135 if(MC_process_is_enabled(process)){
136 MC_state_interleave_process(initial_graph_state, process);
140 visited_pairs = xbt_dynar_new(sizeof(mc_visited_pairs_t), NULL);
141 reached_pairs = xbt_dynar_new(sizeof(mc_reached_pairs_t), NULL);
143 MC_take_snapshot(init_snapshot);
147 /* unsigned int cursor = 0; */
148 /* xbt_state_t state = NULL; */
149 /* int nb_init_state = 0; */
151 /* xbt_dynar_foreach(a->states, cursor, state){ */
152 /* if(state->type == -1){ */
154 /* MC_SET_RAW_MEM; */
155 /* mc_initial_pair = new_pair(init_snapshot, initial_graph_state, state); */
157 /* xbt_fifo_unshift(mc_snapshot_stack, mc_initial_pair); */
159 /* XBT_DEBUG("**************************************************"); */
160 /* XBT_DEBUG("Initial state=%p ", mc_initial_pair); */
162 /* MC_UNSET_RAW_MEM; */
164 /* set_pair_visited(mc_initial_pair->graph_state, mc_initial_pair->automaton_state, 0); */
166 /* if(nb_init_state == 0) */
167 /* MC_dfs(a, 0, 0); */
169 /* MC_dfs(a, 0, 1); */
171 /* nb_init_state++; */
175 /* regarder dans l'automate toutes les transitions activables grâce à l'état initial du système
176 -> donnera les états initiaux de la propriété consistants avec l'état initial du système */
178 unsigned int cursor = 0;
179 unsigned int cursor2 = 0;
180 xbt_state_t state = NULL;
182 xbt_transition_t transition_succ;
183 xbt_dynar_t successors = xbt_dynar_new(sizeof(mc_pairs_t), NULL);
184 xbt_dynar_t elses = xbt_dynar_new(sizeof(mc_pairs_t), NULL);
185 mc_pairs_t pair_succ;
187 xbt_dynar_foreach(a->states, cursor, state){
188 if(state->type == -1){
189 xbt_dynar_foreach(state->out, cursor2, transition_succ){
190 res = MC_automaton_evaluate_label(a, transition_succ->label);
196 mc_initial_pair = new_pair(init_snapshot, initial_graph_state, transition_succ->dst);
197 xbt_dynar_push(successors, &mc_initial_pair);
206 mc_initial_pair = new_pair(init_snapshot, initial_graph_state, transition_succ->dst);
207 xbt_dynar_push(elses, &mc_initial_pair);
218 xbt_dynar_foreach(successors, cursor, pair_succ){
221 xbt_fifo_unshift(mc_snapshot_stack, pair_succ);
223 XBT_DEBUG("**************************************************");
224 XBT_DEBUG("Initial pair=%p (graph=%p, automaton=%p(%s)) ", pair_succ, pair_succ->graph_state, pair_succ->automaton_state,pair_succ->automaton_state->id );
228 set_pair_visited(pair_succ->graph_state, pair_succ->automaton_state, 0);
238 xbt_dynar_foreach(elses, cursor, pair_succ){
241 xbt_fifo_unshift(mc_snapshot_stack, pair_succ);
243 XBT_DEBUG("**************************************************");
244 XBT_DEBUG("Initial pair=%p (graph=%p, automaton=%p(%s)) ", pair_succ, pair_succ->graph_state, pair_succ->automaton_state,pair_succ->automaton_state->id );
248 set_pair_visited(pair_succ->graph_state, pair_succ->automaton_state, 0);
250 if((xbt_dynar_length(successors) == 0) && (cursor == 0)){
260 void MC_dfs(xbt_automaton_t a, int search_cycle, int restore){
262 smx_process_t process = NULL;
265 if(xbt_fifo_size(mc_snapshot_stack) == 0)
269 MC_restore_snapshot(((mc_pairs_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_snapshot_stack)))->system_state);
273 //XBT_DEBUG("Lpv length after restore snapshot: %lu", xbt_dynar_length(visited_pairs));
275 /* Get current state */
276 mc_pairs_t current_pair = (mc_pairs_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_snapshot_stack));
279 xbt_swag_foreach(process, simix_global->process_list){
280 if(MC_process_is_enabled(process)){
281 //XBT_DEBUG("Pid : %lu", process->pid);
282 MC_state_interleave_process(current_pair->graph_state, process);
287 XBT_DEBUG("************************************************** ( search_cycle = %d )", search_cycle);
288 XBT_DEBUG("State : 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));
289 //XBT_DEBUG("Restore : %d", restore);
291 //XBT_DEBUG("Reached pairs : %lu", xbt_dynar_length(reached_pairs));
295 mc_stats_pair->visited_pairs++;
298 mc_state_t next_graph_state = NULL;
299 smx_req_t req = NULL;
302 mc_pairs_t pair_succ;
303 xbt_transition_t transition_succ;
306 //int enabled_transition = 0;
308 xbt_dynar_t successors = xbt_dynar_new(sizeof(mc_pairs_t), NULL);
309 xbt_dynar_t elses = xbt_dynar_new(sizeof(mc_pairs_t), NULL);
311 mc_pairs_t next_pair;
312 mc_snapshot_t next_snapshot;
313 mc_snapshot_t current_snapshot;
316 while((req = MC_state_get_request(current_pair->graph_state, &value)) != NULL){
319 current_snapshot = xbt_new0(s_mc_snapshot_t, 1);
320 MC_take_snapshot(current_snapshot);
324 /* Debug information */
325 if(XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)){
326 req_str = MC_request_to_string(req, value);
327 XBT_DEBUG("Execute: %s", req_str);
331 MC_state_set_executed_request(current_pair->graph_state, req, value);
333 /* Answer the request */
334 SIMIX_request_pre(req, value);
336 /* Wait for requests (schedules processes) */
337 MC_wait_for_requests();
340 /* Create the new expanded graph_state */
343 next_graph_state = MC_state_pair_new();
345 /* Get enabled process and insert it in the interleave set of the next graph_state */
346 xbt_swag_foreach(process, simix_global->process_list){
347 if(MC_process_is_enabled(process)){
348 MC_state_interleave_process(next_graph_state, process);
352 next_snapshot = xbt_new0(s_mc_snapshot_t, 1);
353 MC_take_snapshot(next_snapshot);
357 xbt_dynar_reset(successors);
358 xbt_dynar_reset(elses);
361 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
363 res = MC_automaton_evaluate_label(a, transition_succ->label);
366 next_pair = new_pair(next_snapshot,next_graph_state, transition_succ->dst);
368 //XBT_DEBUG("Next pair : %p", next_pair);
370 if(res == 1){ // enabled transition in automaton
371 xbt_dynar_push(successors, &next_pair);
373 if(res == 2){ // enabled transition in automaton
374 xbt_dynar_push(elses, &next_pair);
382 if(xbt_dynar_length(successors) == 0){
385 next_pair = new_pair(next_snapshot, next_graph_state, current_pair->automaton_state);
386 xbt_dynar_push(successors, &next_pair);
393 xbt_dynar_foreach(successors, cursor, pair_succ){
395 //XBT_DEBUG("Search visited pair : graph=%p, automaton=%p", pair_succ->graph_state, pair_succ->automaton_state);
397 if((search_cycle == 1) && (reached(pair_succ->graph_state, pair_succ->automaton_state) == 1)){
398 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
399 XBT_INFO("| ACCEPTANCE CYCLE |");
400 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
401 XBT_INFO("Counter-example that violates formula :");
402 MC_show_snapshot_stack(mc_snapshot_stack);
403 MC_dump_snapshot_stack(mc_snapshot_stack);
404 MC_print_statistics_pairs(mc_stats_pair);
408 if(visited(pair_succ->graph_state, pair_succ->automaton_state, search_cycle) == 0){
410 //XBT_DEBUG("Unvisited pair !");
412 mc_stats_pair->executed_transitions++;
415 xbt_fifo_unshift(mc_snapshot_stack, pair_succ);
418 set_pair_visited(pair_succ->graph_state, pair_succ->automaton_state, search_cycle);
419 MC_dfs(a, search_cycle, 0);
421 if((search_cycle == 0) && (current_pair->automaton_state->type == 1)){
423 MC_restore_snapshot(current_pair->system_state);
426 xbt_swag_foreach(process, simix_global->process_list){
427 if(MC_process_is_enabled(process)){
428 //XBT_DEBUG("Pid : %lu", process->pid);
429 MC_state_interleave_process(current_pair->graph_state, process);
433 set_pair_reached(current_pair->graph_state, current_pair->automaton_state);
434 XBT_DEBUG("Acceptance pair : graph=%p, automaton=%p(%s)", current_pair->graph_state, current_pair->automaton_state, current_pair->automaton_state->id);
442 xbt_dynar_foreach(elses, cursor, pair_succ){
444 //XBT_DEBUG("Search visited pair : graph=%p, automaton=%p", pair_succ->graph_state, pair_succ->automaton_state);
446 if((search_cycle == 1) && (reached(pair_succ->graph_state, pair_succ->automaton_state) == 1)){
447 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
448 XBT_INFO("| ACCEPTANCE CYCLE |");
449 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
450 XBT_INFO("Counter-example that violates formula :");
451 MC_show_snapshot_stack(mc_snapshot_stack);
452 MC_dump_snapshot_stack(mc_snapshot_stack);
453 MC_print_statistics_pairs(mc_stats_pair);
457 if(visited(pair_succ->graph_state, pair_succ->automaton_state, search_cycle) == 0){
459 //XBT_DEBUG("Unvisited pair !");
461 mc_stats_pair->executed_transitions++;
464 xbt_fifo_unshift(mc_snapshot_stack, pair_succ);
467 set_pair_visited(pair_succ->graph_state, pair_succ->automaton_state, search_cycle);
468 MC_dfs(a, search_cycle, 0);
470 if((search_cycle == 0) && (current_pair->automaton_state->type == 1)){
472 MC_restore_snapshot(current_pair->system_state);
475 xbt_swag_foreach(process, simix_global->process_list){
476 if(MC_process_is_enabled(process)){
477 //XBT_DEBUG("Pid : %lu", process->pid);
478 MC_state_interleave_process(current_pair->graph_state, process);
482 set_pair_reached(current_pair->graph_state, current_pair->automaton_state);
483 XBT_DEBUG("Acceptance pair : graph=%p, automaton=%p(%s)", current_pair->graph_state, current_pair->automaton_state, current_pair->automaton_state->id);
491 if(MC_state_interleave_size(current_pair->graph_state) > 0){
492 MC_restore_snapshot(current_snapshot);
494 //XBT_DEBUG("Snapshot restored");
500 //remove_pair_reached(current_pair->graph_state);
501 xbt_fifo_shift(mc_snapshot_stack);
502 XBT_DEBUG("State (graph=%p, automaton =%p) shifted in snapshot_stack", current_pair->graph_state, current_pair->automaton_state);
507 void MC_show_snapshot_stack(xbt_fifo_t stack){
510 xbt_fifo_item_t item;
512 char *req_str = NULL;
514 for (item = xbt_fifo_get_last_item(stack);
515 (item ? (pair = (mc_pairs_t) (xbt_fifo_get_item_content(item)))
516 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
517 req = MC_state_get_executed_request(pair->graph_state, &value);
519 req_str = MC_request_to_string(req, value);
520 XBT_INFO("%s", req_str);
526 void MC_dump_snapshot_stack(xbt_fifo_t stack){
530 while ((pair = (mc_pairs_t) xbt_fifo_pop(stack)) != NULL)
531 MC_pair_delete(pair);
535 void MC_pair_delete(mc_pairs_t pair){
536 xbt_free(pair->graph_state->proc_status);
537 xbt_free(pair->graph_state);
538 //xbt_free(pair->automaton_state); -> FIXME : à implémenter
543 int MC_automaton_evaluate_label(xbt_automaton_t a, xbt_exp_label_t l){
547 int left_res = MC_automaton_evaluate_label(a, l->u.or_and.left_exp);
548 int right_res = MC_automaton_evaluate_label(a, l->u.or_and.right_exp);
549 return (left_res || right_res);
553 int left_res = MC_automaton_evaluate_label(a, l->u.or_and.left_exp);
554 int right_res = MC_automaton_evaluate_label(a, l->u.or_and.right_exp);
555 return (left_res && right_res);
559 int res = MC_automaton_evaluate_label(a, l->u.exp_not);
564 unsigned int cursor = 0;
565 xbt_propositional_symbol_t p = NULL;
566 xbt_dynar_foreach(a->propositional_symbols, cursor, p){
567 if(strcmp(p->pred, l->u.predicat) == 0){
568 int (*f)() = p->function;
584 void MC_stateful_dpor_init(xbt_automaton_t a){
586 XBT_DEBUG("**************************************************");
587 XBT_DEBUG("DPOR stateful init");
588 XBT_DEBUG("**************************************************");
590 mc_pairs_t initial_pair;
591 mc_state_t initial_graph_state;
592 smx_process_t process;
593 mc_snapshot_t initial_snapshot;
595 MC_wait_for_requests();
599 initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
601 initial_graph_state = MC_state_pair_new();
602 xbt_swag_foreach(process, simix_global->process_list){
603 if(MC_process_is_enabled(process)){
604 MC_state_interleave_process(initial_graph_state, process);
609 visited_pairs = xbt_dynar_new(sizeof(mc_visited_pairs_t), NULL);
610 reached_pairs = xbt_dynar_new(sizeof(mc_reached_pairs_t), NULL);
612 MC_take_snapshot(initial_snapshot);
616 unsigned int cursor = 0;
617 unsigned int cursor2 = 0;
618 xbt_state_t state = NULL;
620 xbt_transition_t transition_succ;
622 xbt_dynar_foreach(a->states, cursor, state){
623 if(state->type == -1){
624 xbt_dynar_foreach(state->out, cursor2, transition_succ){
625 res = MC_automaton_evaluate_label(a, transition_succ->label);
627 if((res == 1) || (res == 2)){
631 initial_pair = new_pair(initial_snapshot, initial_graph_state, transition_succ->dst);
632 xbt_fifo_unshift(mc_snapshot_stack, initial_pair);
636 set_pair_visited(initial_pair->graph_state, initial_pair->automaton_state, 0);
638 MC_stateful_dpor(a, 0, 0);
646 if(xbt_fifo_size(mc_snapshot_stack)>0)
650 if(xbt_fifo_size(mc_snapshot_stack) == 0){
654 xbt_dynar_foreach(a->states, cursor, state){
655 if(state->type == -1){
659 initial_pair = new_pair(initial_snapshot, initial_graph_state, state);
660 xbt_fifo_unshift(mc_snapshot_stack, initial_pair);
664 set_pair_visited(initial_pair->graph_state, initial_pair->automaton_state, 0);
666 MC_stateful_dpor(a, 0, 0);
676 void MC_stateful_dpor(xbt_automaton_t a, int search_cycle, int restore){
678 smx_process_t process = NULL;
680 if(xbt_fifo_size(mc_snapshot_stack) == 0)
684 mc_state_t next_graph_state = NULL;
685 smx_req_t req = NULL, prev_req = NULL;
687 xbt_fifo_item_t item = NULL;
689 //mc_pairs_t pair_succ;
690 xbt_transition_t transition_succ;
694 mc_pairs_t next_pair;
695 mc_snapshot_t next_snapshot;
696 mc_snapshot_t current_snapshot;
697 mc_pairs_t current_pair;
698 mc_pairs_t prev_pair;
699 int new_pair_created;
701 while(xbt_fifo_size(mc_snapshot_stack) > 0){
703 current_pair = (mc_pairs_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_snapshot_stack));
706 XBT_DEBUG("**************************************************");
707 XBT_DEBUG("Depth : %d, State : g=%p, a=%p(%s), %u interleave", xbt_fifo_size(mc_snapshot_stack),current_pair->graph_state, current_pair->automaton_state, current_pair->automaton_state->id,MC_state_interleave_size(current_pair->graph_state));
709 mc_stats_pair->visited_pairs++;
711 if((xbt_fifo_size(mc_snapshot_stack) < MAX_DEPTH) && (req = MC_state_get_request(current_pair->graph_state, &value))){
714 current_snapshot = xbt_new0(s_mc_snapshot_t, 1);
715 MC_take_snapshot(current_snapshot);
718 /* Debug information */
719 if(XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)){
720 req_str = MC_request_to_string(req, value);
721 XBT_DEBUG("Execute: %s", req_str);
725 MC_state_set_executed_request(current_pair->graph_state, req, value);
726 mc_stats_pair->executed_transitions++;
728 /* Answer the request */
729 SIMIX_request_pre(req, value);
731 /* Wait for requests (schedules processes) */
732 MC_wait_for_requests();
734 /* Create the new expanded graph_state */
737 next_graph_state = MC_state_pair_new();
739 /* Get an enabled process and insert it in the interleave set of the next graph_state */
740 xbt_swag_foreach(process, simix_global->process_list){
741 if(MC_process_is_enabled(process)){
742 MC_state_interleave_process(next_graph_state, process);
747 next_snapshot = xbt_new0(s_mc_snapshot_t, 1);
748 MC_take_snapshot(next_snapshot);
752 new_pair_created = 0;
755 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
756 res = MC_automaton_evaluate_label(a, transition_succ->label);
758 if((res == 1) || (res == 2)){
762 next_pair = new_pair(next_snapshot,next_graph_state, transition_succ->dst);
763 xbt_fifo_unshift(mc_snapshot_stack, next_pair);
766 new_pair_created = 1;
773 /* Pas d'avancement possible dans l'automate */
774 if(new_pair_created == 0){
777 next_pair = new_pair(next_snapshot,next_graph_state,current_pair->automaton_state);
778 xbt_fifo_unshift(mc_snapshot_stack, next_pair);
784 XBT_DEBUG("There are no more processes to interleave.");
786 /* Trash the current state, no longer needed */
788 xbt_fifo_shift(mc_snapshot_stack);
791 while((current_pair = xbt_fifo_shift(mc_snapshot_stack)) != NULL){
792 req = MC_state_get_internal_request(current_pair->graph_state);
793 xbt_fifo_foreach(mc_snapshot_stack, item, prev_pair, mc_pairs_t) {
794 if(MC_request_depend(req, MC_state_get_internal_request(prev_pair->graph_state))){
795 if(XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)){
796 XBT_DEBUG("Dependent Transitions:");
797 prev_req = MC_state_get_executed_request(prev_pair->graph_state, &value);
798 req_str = MC_request_to_string(prev_req, value);
799 XBT_DEBUG("%s (state=%p)", req_str, prev_pair->graph_state);
801 prev_req = MC_state_get_executed_request(current_pair->graph_state, &value);
802 req_str = MC_request_to_string(prev_req, value);
803 XBT_DEBUG("%s (state=%p)", req_str, current_pair->graph_state);
807 if(!MC_state_process_is_done(prev_pair->graph_state, req->issuer))
808 MC_state_interleave_process(prev_pair->graph_state, req->issuer);
810 XBT_DEBUG("Process %p is in done set", req->issuer);
816 if(MC_state_interleave_size(current_pair->graph_state)){
817 MC_restore_snapshot(current_pair->system_state);
818 xbt_fifo_unshift(mc_snapshot_stack, current_pair);
819 XBT_DEBUG("Back-tracking to depth %d", xbt_fifo_size(mc_snapshot_stack));