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)
28 for(i=0 ; i< s1->num_reg ; i++){
30 if(s1->regions[i]->size != s2->regions[i]->size)
33 if(s1->regions[i]->start_addr != s2->regions[i]->start_addr)
36 if(memcmp(s1->regions[i]->data, s2->regions[i]->data, s1->regions[i]->size) != 0)
45 int reached(xbt_automaton_t a){
47 if(xbt_dynar_is_empty(reached_pairs)){
52 mc_pair_reached_t pair = NULL;
53 pair = xbt_new0(s_mc_pair_reached_t, 1);
54 pair->automaton_state = a->current_state;
55 pair->prop_ato = xbt_dynar_new(sizeof(int), NULL);
56 pair->system_state = xbt_new0(s_mc_snapshot_t, 1);
57 MC_take_snapshot(pair->system_state);
59 /* Get values of propositional symbols */
60 unsigned int cursor = 0;
61 xbt_propositional_symbol_t ps = NULL;
62 xbt_dynar_foreach(a->propositional_symbols, cursor, ps){
63 int (*f)() = ps->function;
65 xbt_dynar_push(pair->prop_ato, &res);
69 mc_pair_reached_t pair_test;
71 xbt_dynar_foreach(reached_pairs, cursor, pair_test){
72 if(automaton_state_compare(pair_test->automaton_state, pair->automaton_state) == 0){
73 if(xbt_dynar_compare(pair_test->prop_ato, pair->prop_ato, propositional_symbols_compare_value) == 0){
74 if(snapshot_compare(pair_test->system_state, pair->system_state) == 0){
88 int set_pair_reached(xbt_automaton_t a){
94 mc_pair_reached_t pair = NULL;
95 pair = xbt_new0(s_mc_pair_reached_t, 1);
96 pair->automaton_state = a->current_state;
97 pair->prop_ato = xbt_dynar_new(sizeof(int), NULL);
98 pair->system_state = xbt_new0(s_mc_snapshot_t, 1);
99 MC_take_snapshot(pair->system_state);
101 /* Get values of propositional symbols */
102 unsigned int cursor = 0;
103 xbt_propositional_symbol_t ps = NULL;
104 xbt_dynar_foreach(a->propositional_symbols, cursor, ps){
105 int (*f)() = ps->function;
107 xbt_dynar_push(pair->prop_ato, &res);
110 xbt_dynar_push(reached_pairs, &pair);
121 void MC_pair_delete(mc_pair_t pair){
122 xbt_free(pair->graph_state->proc_status);
123 xbt_free(pair->graph_state);
124 //xbt_free(pair->automaton_state); -> FIXME : à implémenter
129 int MC_automaton_evaluate_label(xbt_automaton_t a, xbt_exp_label_t l){
133 int left_res = MC_automaton_evaluate_label(a, l->u.or_and.left_exp);
134 int right_res = MC_automaton_evaluate_label(a, l->u.or_and.right_exp);
135 return (left_res || right_res);
139 int left_res = MC_automaton_evaluate_label(a, l->u.or_and.left_exp);
140 int right_res = MC_automaton_evaluate_label(a, l->u.or_and.right_exp);
141 return (left_res && right_res);
145 int res = MC_automaton_evaluate_label(a, l->u.exp_not);
150 unsigned int cursor = 0;
151 xbt_propositional_symbol_t p = NULL;
152 xbt_dynar_foreach(a->propositional_symbols, cursor, p){
153 if(strcmp(p->pred, l->u.predicat) == 0){
154 int (*f)() = p->function;
171 /********************* Double-DFS stateful without visited state *******************/
174 void MC_ddfs_stateful_init(xbt_automaton_t a){
176 XBT_DEBUG("**************************************************");
177 XBT_DEBUG("Double-DFS stateful without visited state init");
178 XBT_DEBUG("**************************************************");
180 mc_pair_t mc_initial_pair;
181 mc_state_t initial_graph_state;
182 smx_process_t process;
183 mc_snapshot_t init_snapshot;
185 MC_wait_for_requests();
189 init_snapshot = xbt_new0(s_mc_snapshot_t, 1);
191 initial_graph_state = MC_state_pair_new();
192 xbt_swag_foreach(process, simix_global->process_list){
193 if(MC_process_is_enabled(process)){
194 MC_state_interleave_process(initial_graph_state, process);
198 reached_pairs = xbt_dynar_new(sizeof(mc_pair_reached_t), NULL);
200 MC_take_snapshot(init_snapshot);
204 unsigned int cursor = 0;
205 xbt_state_t state = NULL;
207 xbt_dynar_foreach(a->states, cursor, state){
208 if(state->type == -1){
211 mc_initial_pair = new_pair(init_snapshot, initial_graph_state, state);
212 xbt_fifo_unshift(mc_stack_liveness_stateful, mc_initial_pair);
216 MC_ddfs_stateful(a, 0, 0);
218 MC_restore_snapshot(init_snapshot);
220 MC_ddfs_stateful(a, 0, 0);
223 if(state->type == 2){
226 mc_initial_pair = new_pair(init_snapshot, initial_graph_state, state);
227 xbt_fifo_unshift(mc_stack_liveness_stateful, mc_initial_pair);
231 MC_ddfs_stateful(a, 1, 0);
233 MC_restore_snapshot(init_snapshot);
235 MC_ddfs_stateful(a, 1, 0);
243 void MC_ddfs_stateful(xbt_automaton_t a, int search_cycle, int restore){
245 smx_process_t process = NULL;
246 mc_pair_t current_pair = NULL;
248 if(xbt_fifo_size(mc_stack_liveness_stateful) == 0)
252 current_pair = (mc_pair_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack_liveness_stateful));
253 MC_restore_snapshot(current_pair->system_state);
254 xbt_swag_foreach(process, simix_global->process_list){
255 if(MC_process_is_enabled(process)){
256 MC_state_interleave_process(current_pair->graph_state, process);
262 /* Get current state */
263 current_pair = (mc_pair_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack_liveness_stateful));
266 XBT_DEBUG("********************* ( Depth = %d, search_cycle = %d )", xbt_fifo_size(mc_stack_liveness_stateful), search_cycle);
267 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));
269 a->current_state = current_pair->automaton_state;
271 mc_stats_pair->visited_pairs++;
274 mc_state_t next_graph_state = NULL;
275 smx_req_t req = NULL;
279 xbt_transition_t transition_succ;
283 xbt_dynar_t successors = NULL;
285 mc_pair_t next_pair = NULL;
286 mc_snapshot_t next_snapshot = NULL;
287 mc_snapshot_t current_snapshot = NULL;
291 successors = xbt_dynar_new(sizeof(mc_pair_t), NULL);
294 while((req = MC_state_get_request(current_pair->graph_state, &value)) != NULL){
297 current_snapshot = xbt_new0(s_mc_snapshot_t, 1);
298 MC_take_snapshot(current_snapshot);
302 /* Debug information */
303 if(XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)){
304 req_str = MC_request_to_string(req, value);
305 XBT_DEBUG("Execute: %s", req_str);
309 MC_state_set_executed_request(current_pair->graph_state, req, value);
311 /* Answer the request */
312 SIMIX_request_pre(req, value);
314 /* Wait for requests (schedules processes) */
315 MC_wait_for_requests();
318 /* Create the new expanded graph_state */
321 next_graph_state = MC_state_pair_new();
323 /* Get enabled process and insert it in the interleave set of the next graph_state */
324 xbt_swag_foreach(process, simix_global->process_list){
325 if(MC_process_is_enabled(process)){
326 MC_state_interleave_process(next_graph_state, process);
330 next_snapshot = xbt_new0(s_mc_snapshot_t, 1);
331 MC_take_snapshot(next_snapshot);
333 xbt_dynar_reset(successors);
339 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
341 res = MC_automaton_evaluate_label(a, transition_succ->label);
343 if(res == 1){ // enabled transition in automaton
345 next_pair = new_pair(next_snapshot,next_graph_state, transition_succ->dst);
346 xbt_dynar_push(successors, &next_pair);
353 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
355 res = MC_automaton_evaluate_label(a, transition_succ->label);
357 if(res == 2){ // transition always enabled in automaton
359 next_pair = new_pair(next_snapshot,next_graph_state, transition_succ->dst);
360 xbt_dynar_push(successors, &next_pair);
368 if(xbt_dynar_length(successors) == 0){
371 next_pair = new_pair(next_snapshot, next_graph_state, current_pair->automaton_state);
372 xbt_dynar_push(successors, &next_pair);
377 //XBT_DEBUG("Successors in automaton %lu", xbt_dynar_length(successors));
380 xbt_dynar_foreach(successors, cursor, pair_succ){
382 //XBT_DEBUG("Search visited pair : graph=%p, automaton=%p", pair_succ->graph_state, pair_succ->automaton_state);
384 if((search_cycle == 1) && (reached(a) == 1)){
385 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
386 XBT_INFO("| ACCEPTANCE CYCLE |");
387 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
388 XBT_INFO("Counter-example that violates formula :");
389 MC_show_stack_liveness_stateful(mc_stack_liveness_stateful);
390 MC_dump_stack_liveness_stateful(mc_stack_liveness_stateful);
391 MC_print_statistics_pairs(mc_stats_pair);
395 //mc_stats_pair->executed_transitions++;
398 xbt_fifo_unshift(mc_stack_liveness_stateful, pair_succ);
401 MC_ddfs_stateful(a, search_cycle, 0);
404 if((search_cycle == 0) && ((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2))){
406 int res = set_pair_reached(a);
407 XBT_DEBUG("Acceptance pair : graph=%p, automaton=%p(%s)", pair_succ->graph_state, pair_succ->automaton_state, pair_succ->automaton_state->id);
410 xbt_fifo_unshift(mc_stack_liveness_stateful, pair_succ);
413 MC_ddfs_stateful(a, 1, 1);
417 xbt_dynar_pop(reached_pairs, NULL);
424 if(MC_state_interleave_size(current_pair->graph_state) > 0){
425 XBT_DEBUG("Backtracking to depth %u", xbt_fifo_size(mc_stack_liveness_stateful));
426 MC_restore_snapshot(current_snapshot);
434 xbt_fifo_shift(mc_stack_liveness_stateful);
435 XBT_DEBUG("Pair (graph=%p, automaton =%p) shifted in stack", current_pair->graph_state, current_pair->automaton_state);
442 /********************* Double-DFS stateless *******************/
444 void MC_pair_stateless_delete(mc_pair_stateless_t pair){
445 xbt_free(pair->graph_state->proc_status);
446 xbt_free(pair->graph_state);
447 //xbt_free(pair->automaton_state); -> FIXME : à implémenter
451 mc_pair_stateless_t new_pair_stateless(mc_state_t sg, xbt_state_t st){
452 mc_pair_stateless_t p = NULL;
453 p = xbt_new0(s_mc_pair_stateless_t, 1);
454 p->automaton_state = st;
456 mc_stats_pair->expanded_pairs++;
462 void MC_ddfs_stateless_init(xbt_automaton_t a){
464 XBT_DEBUG("**************************************************");
465 XBT_DEBUG("Double-DFS stateless init");
466 XBT_DEBUG("**************************************************");
468 mc_pair_stateless_t mc_initial_pair = NULL;
469 mc_state_t initial_graph_state = NULL;
470 smx_process_t process;
472 MC_wait_for_requests();
475 initial_graph_state = MC_state_pair_new();
476 xbt_swag_foreach(process, simix_global->process_list){
477 if(MC_process_is_enabled(process)){
478 MC_state_interleave_process(initial_graph_state, process);
482 reached_pairs = xbt_dynar_new(sizeof(mc_pair_reached_t), NULL);
484 initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
485 MC_take_snapshot(initial_snapshot);
489 unsigned int cursor = 0;
492 xbt_dynar_foreach(a->states, cursor, state){
493 if(state->type == -1){
496 mc_initial_pair = new_pair_stateless(initial_graph_state, state);
497 xbt_fifo_unshift(mc_stack_liveness_stateless, mc_initial_pair);
501 MC_ddfs_stateless(a, 0, 0);
503 MC_restore_snapshot(initial_snapshot);
505 MC_ddfs_stateless(a, 0, 0);
508 if(state->type == 2){
511 mc_initial_pair = new_pair_stateless(initial_graph_state, state);
512 xbt_fifo_unshift(mc_stack_liveness_stateless, mc_initial_pair);
516 MC_ddfs_stateless(a, 1, 0);
518 MC_restore_snapshot(initial_snapshot);
520 MC_ddfs_stateless(a, 1, 0);
529 void MC_ddfs_stateless(xbt_automaton_t a, int search_cycle, int replay){
531 smx_process_t process;
532 mc_pair_stateless_t current_pair = NULL;
534 if(xbt_fifo_size(mc_stack_liveness_stateless) == 0)
538 MC_replay_liveness(mc_stack_liveness_stateless);
539 current_pair = (mc_pair_stateless_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack_liveness_stateless));
540 xbt_swag_foreach(process, simix_global->process_list){
541 if(MC_process_is_enabled(process)){
542 MC_state_interleave_process(current_pair->graph_state, process);
547 /* Get current pair */
548 current_pair = (mc_pair_stateless_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack_liveness_stateless));
550 /* Update current state in buchi automaton */
551 a->current_state = current_pair->automaton_state;
553 XBT_DEBUG("********************* ( Depth = %d, search_cycle = %d )", xbt_fifo_size(mc_stack_liveness_stateless), search_cycle);
554 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));
557 mc_stats_pair->visited_pairs++;
560 mc_state_t next_graph_state = NULL;
561 smx_req_t req = NULL;
564 xbt_transition_t transition_succ;
565 unsigned int cursor = 0;
568 mc_pair_stateless_t next_pair = NULL;
569 mc_pair_stateless_t pair_succ;
571 xbt_dynar_t successors = NULL;
574 successors = xbt_dynar_new(sizeof(mc_pair_stateless_t), NULL);
577 while((req = MC_state_get_request(current_pair->graph_state, &value)) != NULL){
580 /* Debug information */
581 if(XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)){
582 req_str = MC_request_to_string(req, value);
583 XBT_DEBUG("Execute: %s", req_str);
589 MC_state_set_executed_request(current_pair->graph_state, req, value);
591 /* Answer the request */
592 SIMIX_request_pre(req, value);
594 /* Wait for requests (schedules processes) */
595 MC_wait_for_requests();
600 /* Create the new expanded graph_state */
601 next_graph_state = MC_state_pair_new();
603 /* Get enabled process and insert it in the interleave set of the next graph_state */
604 xbt_swag_foreach(process, simix_global->process_list){
605 if(MC_process_is_enabled(process)){
606 MC_state_interleave_process(next_graph_state, process);
610 xbt_dynar_reset(successors);
616 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
618 res = MC_automaton_evaluate_label(a, transition_succ->label);
620 if(res == 1){ // enabled transition in automaton
622 next_pair = new_pair_stateless(next_graph_state, transition_succ->dst);
623 xbt_dynar_push(successors, &next_pair);
631 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
633 res = MC_automaton_evaluate_label(a, transition_succ->label);
635 if(res == 2){ // enabled transition in automaton
637 next_pair = new_pair_stateless(next_graph_state, transition_succ->dst);
638 xbt_dynar_push(successors, &next_pair);
645 if(xbt_dynar_length(successors) == 0){
647 next_pair = new_pair_stateless(next_graph_state, current_pair->automaton_state);
648 xbt_dynar_push(successors, &next_pair);
654 xbt_dynar_foreach(successors, cursor, pair_succ){
656 if((search_cycle == 1) && (reached(a) == 1)){
657 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
658 XBT_INFO("| ACCEPTANCE CYCLE |");
659 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
660 XBT_INFO("Counter-example that violates formula :");
661 MC_show_stack_liveness_stateless(mc_stack_liveness_stateless);
662 MC_dump_stack_liveness_stateless(mc_stack_liveness_stateless);
663 MC_print_statistics_pairs(mc_stats_pair);
668 xbt_fifo_unshift(mc_stack_liveness_stateless, pair_succ);
671 MC_ddfs_stateless(a, search_cycle, 0);
674 if((search_cycle == 0) && ((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2))){
676 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);
677 int res = set_pair_reached(a);
680 xbt_fifo_unshift(mc_stack_liveness_stateless, pair_succ);
683 MC_ddfs_stateless(a, 1, 1);
687 xbt_dynar_pop(reached_pairs, NULL);
693 if(MC_state_interleave_size(current_pair->graph_state) > 0){
694 XBT_DEBUG("Backtracking to depth %u", xbt_fifo_size(mc_stack_liveness_stateless));
695 MC_replay_liveness(mc_stack_liveness_stateless);
701 xbt_fifo_shift(mc_stack_liveness_stateless);
702 XBT_DEBUG("Pair (graph=%p, automaton =%p, search_cycle = %u) shifted in stack", current_pair->graph_state, current_pair->automaton_state, search_cycle);