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;
13 mc_pairs_t new_pair(mc_snapshot_t sn, mc_state_t sg, xbt_state_t st){
15 p = xbt_new0(s_mc_pairs_t, 1);
17 p->automaton_state = st;
21 mc_stats_pair->expanded_pairs++;
26 void set_pair_visited(mc_state_t gs, xbt_state_t as, int sc){
29 mc_visited_pairs_t p = NULL;
30 p = xbt_new0(s_mc_visited_pairs_t, 1);
32 p->automaton_state = as;
34 char *hash = malloc(sizeof(char)*160);
35 xbt_sha((char *)&p, hash);
36 xbt_dynar_push(visited_pairs, &hash);
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);
66 //XBT_DEBUG("Search acceptance pair already reach !");
68 char* hash_reached = malloc(sizeof(char)*160);
72 mc_reached_pairs_t p = NULL;
73 p = xbt_new0(s_mc_reached_pairs_t, 1);
75 p->automaton_state = as;
76 char *hash = malloc(sizeof(char)*160);
77 xbt_sha((char *)&p, hash);
78 //XBT_DEBUG("Hash : %s", hash);
79 xbt_dynar_foreach(reached_pairs, c, hash_reached){
80 if(strcmp(hash, hash_reached) == 0){
82 //XBT_DEBUG("Pair (graph=%p, automaton=%p(%s)) already reached !", gs, as, as->id);
91 void set_pair_reached(mc_state_t gs, xbt_state_t as){
93 //XBT_DEBUG("Set pair (graph=%p, automaton=%p) reached ", gs, as);
94 if(reached(gs, as) == 0){
96 mc_reached_pairs_t p = NULL;
97 p = xbt_new0(s_mc_reached_pairs_t, 1);
99 p->automaton_state = as;
100 char *hash = malloc(sizeof(char)*160) ;
101 xbt_sha((char *)&p, hash);
102 xbt_dynar_push(reached_pairs, &hash);
103 //XBT_DEBUG("New reached pair : graph=%p, automaton=%p(%s)", gs, as, as->id);
109 int visited(mc_state_t gs, xbt_state_t as, int sc){
110 /*unsigned int c = 0;
111 mc_visited_pairs_t p = NULL;
113 int different_process;
115 //XBT_DEBUG("Visited pair length : %lu", xbt_dynar_length(visited_pairs));
118 xbt_dynar_foreach(visited_pairs, c, p){
119 //XBT_DEBUG("Test pair visited");
121 if((p->automaton_state == as) && (p->search_cycle == sc)){
123 for(i=0; i < gs->max_pid; i++){
124 if(gs->proc_status[i].state != p->graph_state->proc_status[i].state){
128 if(different_process==0){
137 //XBT_DEBUG("Test visited pair");
139 char* hash_visited = malloc(sizeof(char)*160);
143 mc_visited_pairs_t p = NULL;
144 p = xbt_new0(s_mc_visited_pairs_t, 1);
146 p->automaton_state = as;
147 char *hash = malloc(sizeof(char)*160);
148 xbt_sha((char *)&p, hash);
149 //XBT_DEBUG("Hash : %s", hash);
150 xbt_dynar_foreach(visited_pairs, c, hash_visited){
151 if(strcmp(hash, hash_visited) == 0){
158 //XBT_DEBUG("End test visited pair");
163 void MC_dfs_init(xbt_automaton_t a){
165 XBT_DEBUG("**************************************************");
166 XBT_DEBUG("DFS init");
167 XBT_DEBUG("**************************************************");
169 mc_pairs_t mc_initial_pair;
170 mc_state_t initial_graph_state;
171 smx_process_t process;
172 mc_snapshot_t init_snapshot;
174 MC_wait_for_requests();
178 init_snapshot = xbt_new0(s_mc_snapshot_t, 1);
180 initial_graph_state = MC_state_pair_new();
181 xbt_swag_foreach(process, simix_global->process_list){
182 if(MC_process_is_enabled(process)){
183 MC_state_interleave_process(initial_graph_state, process);
187 visited_pairs = xbt_dynar_new(sizeof(char *), NULL);
188 reached_pairs = xbt_dynar_new(sizeof(char *), NULL);
190 MC_take_snapshot(init_snapshot);
194 /* unsigned int cursor = 0; */
195 /* xbt_state_t state = NULL; */
196 /* int nb_init_state = 0; */
198 /* xbt_dynar_foreach(a->states, cursor, state){ */
199 /* if(state->type == -1){ */
201 /* MC_SET_RAW_MEM; */
202 /* mc_initial_pair = new_pair(init_snapshot, initial_graph_state, state); */
204 /* xbt_fifo_unshift(mc_snapshot_stack, mc_initial_pair); */
206 /* XBT_DEBUG("**************************************************"); */
207 /* XBT_DEBUG("Initial state=%p ", mc_initial_pair); */
209 /* MC_UNSET_RAW_MEM; */
211 /* set_pair_visited(mc_initial_pair->graph_state, mc_initial_pair->automaton_state, 0); */
213 /* if(nb_init_state == 0) */
214 /* MC_dfs(a, 0, 0); */
216 /* MC_dfs(a, 0, 1); */
218 /* nb_init_state++; */
222 /* regarder dans l'automate toutes les transitions activables grâce à l'état initial du système
223 -> donnera les états initiaux de la propriété consistants avec l'état initial du système */
225 unsigned int cursor = 0;
226 unsigned int cursor2 = 0;
227 xbt_state_t state = NULL;
229 xbt_transition_t transition_succ;
230 xbt_dynar_t successors = xbt_dynar_new(sizeof(mc_pairs_t), NULL);
231 mc_pairs_t pair_succ;
233 xbt_dynar_foreach(a->states, cursor, state){
234 if(state->type == -1){
235 xbt_dynar_foreach(state->out, cursor2, transition_succ){
236 res = MC_automaton_evaluate_label(a, transition_succ->label);
238 if((res == 1) || (res == 2)){
242 mc_initial_pair = new_pair(init_snapshot, initial_graph_state, transition_succ->dst);
243 xbt_dynar_push(successors, &mc_initial_pair);
254 if(xbt_dynar_length(successors) == 0){
255 xbt_dynar_foreach(a->states, cursor, state){
256 if(state->type == -1){
259 mc_initial_pair = new_pair(init_snapshot, initial_graph_state, transition_succ->dst);
260 xbt_dynar_push(successors, &mc_initial_pair);
268 xbt_dynar_foreach(successors, cursor, pair_succ){
271 xbt_fifo_unshift(mc_snapshot_stack, pair_succ);
272 set_pair_visited(pair_succ->graph_state, pair_succ->automaton_state, 0);
290 void MC_dfs(xbt_automaton_t a, int search_cycle, int restore){
292 smx_process_t process = NULL;
295 if(xbt_fifo_size(mc_snapshot_stack) == 0)
299 MC_restore_snapshot(((mc_pairs_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_snapshot_stack)))->system_state);
304 /* Get current state */
305 mc_pairs_t current_pair = (mc_pairs_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_snapshot_stack));
308 xbt_swag_foreach(process, simix_global->process_list){
309 if(MC_process_is_enabled(process)){
310 //XBT_DEBUG("Pid : %lu", process->pid);
311 MC_state_interleave_process(current_pair->graph_state, process);
316 XBT_DEBUG("************************************************** ( search_cycle = %d )", search_cycle);
317 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));
320 mc_stats_pair->visited_pairs++;
323 mc_state_t next_graph_state = NULL;
324 smx_req_t req = NULL;
327 mc_pairs_t pair_succ;
328 xbt_transition_t transition_succ;
331 //int enabled_transition = 0;
333 xbt_dynar_t successors = xbt_dynar_new(sizeof(mc_pairs_t), NULL);
335 mc_pairs_t next_pair;
336 mc_snapshot_t next_snapshot;
337 mc_snapshot_t current_snapshot;
340 while((req = MC_state_get_request(current_pair->graph_state, &value)) != NULL){
343 current_snapshot = xbt_new0(s_mc_snapshot_t, 1);
344 MC_take_snapshot(current_snapshot);
348 /* Debug information */
349 if(XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)){
350 req_str = MC_request_to_string(req, value);
351 XBT_DEBUG("Execute: %s", req_str);
355 MC_state_set_executed_request(current_pair->graph_state, req, value);
357 /* Answer the request */
358 SIMIX_request_pre(req, value);
360 /* Wait for requests (schedules processes) */
361 MC_wait_for_requests();
364 /* Create the new expanded graph_state */
367 next_graph_state = MC_state_pair_new();
369 /* Get enabled process and insert it in the interleave set of the next graph_state */
370 xbt_swag_foreach(process, simix_global->process_list){
371 if(MC_process_is_enabled(process)){
372 MC_state_interleave_process(next_graph_state, process);
376 next_snapshot = xbt_new0(s_mc_snapshot_t, 1);
377 MC_take_snapshot(next_snapshot);
381 xbt_dynar_reset(successors);
384 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
386 res = MC_automaton_evaluate_label(a, transition_succ->label);
390 if((res == 1) || (res == 2)){ // enabled transition in automaton
391 next_pair = new_pair(next_snapshot,next_graph_state, transition_succ->dst);
392 xbt_dynar_push(successors, &next_pair);
399 if(xbt_dynar_length(successors) == 0){
402 next_pair = new_pair(next_snapshot, next_graph_state, current_pair->automaton_state);
403 xbt_dynar_push(successors, &next_pair);
410 xbt_dynar_foreach(successors, cursor, pair_succ){
412 //XBT_DEBUG("Search visited pair : graph=%p, automaton=%p", pair_succ->graph_state, pair_succ->automaton_state);
414 if((search_cycle == 1) && (reached(pair_succ->graph_state, pair_succ->automaton_state) == 1)){
415 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
416 XBT_INFO("| ACCEPTANCE CYCLE |");
417 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
418 XBT_INFO("Counter-example that violates formula :");
419 MC_show_snapshot_stack(mc_snapshot_stack);
420 MC_dump_snapshot_stack(mc_snapshot_stack);
421 MC_print_statistics_pairs(mc_stats_pair);
425 if(visited(pair_succ->graph_state, pair_succ->automaton_state, search_cycle) == 0){
427 //XBT_DEBUG("Unvisited pair !");
429 mc_stats_pair->executed_transitions++;
432 xbt_fifo_unshift(mc_snapshot_stack, pair_succ);
433 set_pair_visited(pair_succ->graph_state, pair_succ->automaton_state, search_cycle);
436 MC_dfs(a, search_cycle, 0);
438 if((search_cycle == 0) && (current_pair->automaton_state->type == 1)){
440 MC_restore_snapshot(current_pair->system_state);
443 xbt_swag_foreach(process, simix_global->process_list){
444 if(MC_process_is_enabled(process)){
445 //XBT_DEBUG("Pid : %lu", process->pid);
446 MC_state_interleave_process(current_pair->graph_state, process);
450 set_pair_reached(current_pair->graph_state, current_pair->automaton_state);
451 XBT_DEBUG("Acceptance pair : graph=%p, automaton=%p(%s)", current_pair->graph_state, current_pair->automaton_state, current_pair->automaton_state->id);
457 XBT_DEBUG("Pair already visited !");
463 if(MC_state_interleave_size(current_pair->graph_state) > 0){
464 MC_restore_snapshot(current_snapshot);
466 //XBT_DEBUG("Snapshot restored");
472 //remove_pair_reached(current_pair->graph_state);
473 xbt_fifo_shift(mc_snapshot_stack);
474 XBT_DEBUG("State (graph=%p, automaton =%p) shifted in snapshot_stack", current_pair->graph_state, current_pair->automaton_state);
479 void MC_show_snapshot_stack(xbt_fifo_t stack){
482 xbt_fifo_item_t item;
484 char *req_str = NULL;
486 for (item = xbt_fifo_get_last_item(stack);
487 (item ? (pair = (mc_pairs_t) (xbt_fifo_get_item_content(item)))
488 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
489 req = MC_state_get_executed_request(pair->graph_state, &value);
491 req_str = MC_request_to_string(req, value);
492 XBT_INFO("%s", req_str);
498 void MC_dump_snapshot_stack(xbt_fifo_t stack){
502 while ((pair = (mc_pairs_t) xbt_fifo_pop(stack)) != NULL)
503 MC_pair_delete(pair);
507 void MC_pair_delete(mc_pairs_t pair){
508 xbt_free(pair->graph_state->proc_status);
509 xbt_free(pair->graph_state);
510 //xbt_free(pair->automaton_state); -> FIXME : à implémenter
515 int MC_automaton_evaluate_label(xbt_automaton_t a, xbt_exp_label_t l){
519 int left_res = MC_automaton_evaluate_label(a, l->u.or_and.left_exp);
520 int right_res = MC_automaton_evaluate_label(a, l->u.or_and.right_exp);
521 return (left_res || right_res);
525 int left_res = MC_automaton_evaluate_label(a, l->u.or_and.left_exp);
526 int right_res = MC_automaton_evaluate_label(a, l->u.or_and.right_exp);
527 return (left_res && right_res);
531 int res = MC_automaton_evaluate_label(a, l->u.exp_not);
536 unsigned int cursor = 0;
537 xbt_propositional_symbol_t p = NULL;
538 xbt_dynar_foreach(a->propositional_symbols, cursor, p){
539 if(strcmp(p->pred, l->u.predicat) == 0){
540 int (*f)() = p->function;
557 /********************* Double DFS using color pair *********************/
564 /********************* DPOR without replay *********************/
568 void MC_dpor_with_restore_snapshot_init(xbt_automaton_t a){
570 XBT_DEBUG("**************************************************");
571 XBT_DEBUG("DPOR (with restore snapshot) init");
572 XBT_DEBUG("**************************************************");
574 mc_pairs_t initial_pair;
575 mc_state_t initial_graph_state;
576 smx_process_t process;
577 mc_snapshot_t initial_snapshot;
579 MC_wait_for_requests();
583 initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
585 initial_graph_state = MC_state_pair_new();
586 xbt_swag_foreach(process, simix_global->process_list){
587 if(MC_process_is_enabled(process)){
588 MC_state_interleave_process(initial_graph_state, process);
593 visited_pairs = xbt_dynar_new(sizeof(mc_visited_pairs_t), NULL);
594 reached_pairs = xbt_dynar_new(sizeof(mc_reached_pairs_t), NULL);
596 MC_take_snapshot(initial_snapshot);
600 unsigned int cursor = 0;
601 unsigned int cursor2 = 0;
602 xbt_state_t state = NULL;
604 xbt_transition_t transition_succ;
606 xbt_dynar_foreach(a->states, cursor, state){
607 if(state->type == -1){
608 xbt_dynar_foreach(state->out, cursor2, transition_succ){
609 res = MC_automaton_evaluate_label(a, transition_succ->label);
611 if((res == 1) || (res == 2)){
615 initial_pair = new_pair(initial_snapshot, initial_graph_state, transition_succ->dst);
616 xbt_fifo_unshift(mc_snapshot_stack, initial_pair);
620 /* Add pair to list of visited pairs*/
621 set_pair_visited(initial_pair->graph_state, initial_pair->automaton_state, 0);
623 MC_dpor_with_restore_snapshot(a, 0, 0);
631 if(xbt_fifo_size(mc_snapshot_stack)>0)
635 /* Initial state of automaton is initial state for initial pair*/
636 if(xbt_fifo_size(mc_snapshot_stack) == 0){
640 xbt_dynar_foreach(a->states, cursor, state){
641 if(state->type == -1){
645 initial_pair = new_pair(initial_snapshot, initial_graph_state, state);
646 xbt_fifo_unshift(mc_snapshot_stack, initial_pair);
650 /* Add pair to list of visited pairs*/
651 set_pair_visited(initial_pair->graph_state, initial_pair->automaton_state, 0);
653 MC_dpor_with_restore_snapshot(a, 0, 0);
664 /* Test du dpor sans rejeu des états de la pile à partir de l'état initial du système mais avec restauration au milieu de l'arbre -> s'applique aux safety properties mais pas d'intégration pour les liveness */
665 void MC_dpor_with_restore_snapshot(xbt_automaton_t a, int search_cycle, int restore){
667 smx_process_t process = NULL;
669 if(xbt_fifo_size(mc_snapshot_stack) == 0)
673 mc_state_t next_graph_state = NULL;
674 smx_req_t req = NULL, prev_req = NULL;
676 xbt_fifo_item_t item = NULL;
678 xbt_transition_t transition_succ;
682 mc_pairs_t next_pair;
683 mc_snapshot_t next_snapshot;
684 mc_snapshot_t current_snapshot;
685 mc_pairs_t current_pair;
686 mc_pairs_t prev_pair;
687 int new_pair_created;
689 while(xbt_fifo_size(mc_snapshot_stack) > 0){
691 current_pair = (mc_pairs_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_snapshot_stack));
694 XBT_DEBUG("**************************************************");
695 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));
697 mc_stats_pair->visited_pairs++;
699 /* Add pair to list of visited pairs*/
700 set_pair_visited(current_pair->graph_state, current_pair->automaton_state, search_cycle);
702 if((xbt_fifo_size(mc_snapshot_stack) < MAX_DEPTH) && (req = MC_state_get_request(current_pair->graph_state, &value))){
705 current_snapshot = xbt_new0(s_mc_snapshot_t, 1);
706 MC_take_snapshot(current_snapshot);
709 /* Debug information */
710 if(XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)){
711 req_str = MC_request_to_string(req, value);
712 XBT_DEBUG("Execute: %s", req_str);
716 MC_state_set_executed_request(current_pair->graph_state, req, value);
718 /* Answer the request */
719 SIMIX_request_pre(req, value);
721 /* Wait for requests (schedules processes) */
722 MC_wait_for_requests();
724 /* Create the new expanded graph_state */
727 next_graph_state = MC_state_pair_new();
729 /* Get an enabled process and insert it in the interleave set of the next graph_state */
730 xbt_swag_foreach(process, simix_global->process_list){
731 if(MC_process_is_enabled(process)){
732 MC_state_interleave_process(next_graph_state, process);
737 next_snapshot = xbt_new0(s_mc_snapshot_t, 1);
738 MC_take_snapshot(next_snapshot);
742 new_pair_created = 0;
745 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
746 res = MC_automaton_evaluate_label(a, transition_succ->label);
748 if((res == 1) || (res == 2)){
751 next_pair = new_pair(next_snapshot,next_graph_state, transition_succ->dst);
752 xbt_fifo_unshift(mc_snapshot_stack, next_pair);
755 new_pair_created = 1;
762 /* Pas d'avancement possible dans l'automate */
763 if(new_pair_created == 0){
766 next_pair = new_pair(next_snapshot,next_graph_state,current_pair->automaton_state);
767 xbt_fifo_unshift(mc_snapshot_stack, next_pair);
773 XBT_DEBUG("There are no more processes to interleave.");
775 /* Trash the current state, no longer needed */
777 xbt_fifo_shift(mc_snapshot_stack);
780 while((current_pair = xbt_fifo_shift(mc_snapshot_stack)) != NULL){
781 req = MC_state_get_internal_request(current_pair->graph_state);
782 xbt_fifo_foreach(mc_snapshot_stack, item, prev_pair, mc_pairs_t) {
783 if(MC_request_depend(req, MC_state_get_internal_request(prev_pair->graph_state))){
784 if(XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)){
785 XBT_DEBUG("Dependent Transitions:");
786 prev_req = MC_state_get_executed_request(prev_pair->graph_state, &value);
787 req_str = MC_request_to_string(prev_req, value);
788 XBT_DEBUG("%s (state=%p)", req_str, prev_pair->graph_state);
790 prev_req = MC_state_get_executed_request(current_pair->graph_state, &value);
791 req_str = MC_request_to_string(prev_req, value);
792 XBT_DEBUG("%s (state=%p)", req_str, current_pair->graph_state);
796 if(!MC_state_process_is_done(prev_pair->graph_state, req->issuer)){
797 MC_state_interleave_process(prev_pair->graph_state, req->issuer);
800 XBT_DEBUG("Process %p is in done set", req->issuer);
807 if(MC_state_interleave_size(current_pair->graph_state)){
808 MC_restore_snapshot(current_pair->system_state);
809 xbt_fifo_unshift(mc_snapshot_stack, current_pair);
810 XBT_DEBUG("Back-tracking to depth %d", xbt_fifo_size(mc_snapshot_stack));