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;
86 xbt_dynar_foreach(reached_pairs, cursor, pair_test){
87 if(automaton_state_compare(pair_test->automaton_state, pair->automaton_state) == 0){
88 if(xbt_dynar_compare(pair_test->prop_ato, pair->prop_ato, propositional_symbols_compare_value) == 0){
89 if(snapshot_compare(pair_test->system_state, pair->system_state) == 0){
103 int set_pair_reached(xbt_automaton_t a, mc_snapshot_t s){
105 if(reached(a, s) == 0){
109 mc_pair_reached_t pair = NULL;
110 pair = xbt_new0(s_mc_pair_reached_t, 1);
111 pair->automaton_state = a->current_state;
112 pair->prop_ato = xbt_dynar_new(sizeof(int), NULL);
113 pair->system_state = s;
115 /* Get values of propositional symbols */
116 unsigned int cursor = 0;
117 xbt_propositional_symbol_t ps = NULL;
118 xbt_dynar_foreach(a->propositional_symbols, cursor, ps){
119 int (*f)() = ps->function;
121 xbt_dynar_push(pair->prop_ato, &res);
124 xbt_dynar_push(reached_pairs, &pair);
135 void MC_pair_delete(mc_pair_t pair){
136 xbt_free(pair->graph_state->proc_status);
137 xbt_free(pair->graph_state);
138 //xbt_free(pair->automaton_state); -> FIXME : à implémenter
143 int MC_automaton_evaluate_label(xbt_automaton_t a, xbt_exp_label_t l){
147 int left_res = MC_automaton_evaluate_label(a, l->u.or_and.left_exp);
148 int right_res = MC_automaton_evaluate_label(a, l->u.or_and.right_exp);
149 return (left_res || right_res);
153 int left_res = MC_automaton_evaluate_label(a, l->u.or_and.left_exp);
154 int right_res = MC_automaton_evaluate_label(a, l->u.or_and.right_exp);
155 return (left_res && right_res);
159 int res = MC_automaton_evaluate_label(a, l->u.exp_not);
164 unsigned int cursor = 0;
165 xbt_propositional_symbol_t p = NULL;
166 xbt_dynar_foreach(a->propositional_symbols, cursor, p){
167 if(strcmp(p->pred, l->u.predicat) == 0){
168 int (*f)() = p->function;
188 /********************* Double-DFS stateless *******************/
190 void MC_pair_stateless_delete(mc_pair_stateless_t pair){
191 xbt_free(pair->graph_state->proc_status);
192 xbt_free(pair->graph_state);
193 //xbt_free(pair->automaton_state); -> FIXME : à implémenter
197 mc_pair_stateless_t new_pair_stateless(mc_state_t sg, xbt_state_t st){
198 mc_pair_stateless_t p = NULL;
199 p = xbt_new0(s_mc_pair_stateless_t, 1);
200 p->automaton_state = st;
202 mc_stats_pair->expanded_pairs++;
208 void MC_ddfs_stateless_init(xbt_automaton_t a){
210 XBT_DEBUG("**************************************************");
211 XBT_DEBUG("Double-DFS stateless init");
212 XBT_DEBUG("**************************************************");
214 mc_pair_stateless_t mc_initial_pair = NULL;
215 mc_state_t initial_graph_state = NULL;
216 smx_process_t process;
218 MC_wait_for_requests();
221 initial_graph_state = MC_state_pair_new();
222 xbt_swag_foreach(process, simix_global->process_list){
223 if(MC_process_is_enabled(process)){
224 MC_state_interleave_process(initial_graph_state, process);
228 reached_pairs = xbt_dynar_new(sizeof(mc_pair_reached_t), NULL);
230 initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
231 MC_take_snapshot(initial_snapshot);
235 unsigned int cursor = 0;
238 xbt_dynar_foreach(a->states, cursor, state){
239 if(state->type == -1){
242 mc_initial_pair = new_pair_stateless(initial_graph_state, state);
243 xbt_fifo_unshift(mc_stack_liveness_stateless, mc_initial_pair);
247 MC_ddfs_stateless(a, 0, 0);
249 MC_restore_snapshot(initial_snapshot);
251 MC_ddfs_stateless(a, 0, 0);
254 if(state->type == 2){
257 mc_initial_pair = new_pair_stateless(initial_graph_state, state);
258 xbt_fifo_unshift(mc_stack_liveness_stateless, mc_initial_pair);
262 MC_ddfs_stateless(a, 1, 0);
264 MC_restore_snapshot(initial_snapshot);
266 MC_ddfs_stateless(a, 1, 0);
275 void MC_ddfs_stateless(xbt_automaton_t a, int search_cycle, int replay){
277 smx_process_t process;
278 mc_pair_stateless_t current_pair = NULL;
280 if(xbt_fifo_size(mc_stack_liveness_stateless) == 0)
284 MC_replay_liveness(mc_stack_liveness_stateless);
285 current_pair = (mc_pair_stateless_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack_liveness_stateless));
286 xbt_swag_foreach(process, simix_global->process_list){
287 if(MC_process_is_enabled(process)){
288 MC_state_interleave_process(current_pair->graph_state, process);
293 /* Get current pair */
294 current_pair = (mc_pair_stateless_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack_liveness_stateless));
296 /* Update current state in buchi automaton */
297 a->current_state = current_pair->automaton_state;
299 XBT_DEBUG("********************* ( Depth = %d, search_cycle = %d )", xbt_fifo_size(mc_stack_liveness_stateless), search_cycle);
300 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));
303 mc_stats_pair->visited_pairs++;
306 mc_state_t next_graph_state = NULL;
307 smx_req_t req = NULL;
310 xbt_transition_t transition_succ;
311 unsigned int cursor = 0;
314 mc_pair_stateless_t next_pair = NULL;
315 mc_pair_stateless_t pair_succ;
316 mc_snapshot_t next_snapshot = NULL;
317 mc_snapshot_t current_snapshot = NULL;
319 xbt_dynar_t successors = NULL;
322 successors = xbt_dynar_new(sizeof(mc_pair_stateless_t), NULL);
325 while((req = MC_state_get_request(current_pair->graph_state, &value)) != NULL){
328 current_snapshot = xbt_new0(s_mc_snapshot_t, 1);
329 MC_take_snapshot(current_snapshot);
332 /* Debug information */
333 if(XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)){
334 req_str = MC_request_to_string(req, value);
335 XBT_DEBUG("Execute: %s", req_str);
341 MC_state_set_executed_request(current_pair->graph_state, req, value);
343 /* Answer the request */
344 SIMIX_request_pre(req, value);
346 /* Wait for requests (schedules processes) */
347 MC_wait_for_requests();
352 /* Create the new expanded graph_state */
353 next_graph_state = MC_state_pair_new();
355 /* Get enabled process and insert it in the interleave set of the next graph_state */
356 xbt_swag_foreach(process, simix_global->process_list){
357 if(MC_process_is_enabled(process)){
358 MC_state_interleave_process(next_graph_state, process);
362 next_snapshot = xbt_new0(s_mc_snapshot_t, 1);
363 MC_take_snapshot(next_snapshot);
365 xbt_dynar_reset(successors);
367 if(snapshot_compare(current_snapshot,next_snapshot)){
368 XBT_DEBUG("Different snapshot");
377 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
379 res = MC_automaton_evaluate_label(a, transition_succ->label);
381 if(res == 1){ // enabled transition in automaton
383 next_pair = new_pair_stateless(next_graph_state, transition_succ->dst);
384 xbt_dynar_push(successors, &next_pair);
392 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
394 res = MC_automaton_evaluate_label(a, transition_succ->label);
396 if(res == 2){ // true transition in automaton
398 next_pair = new_pair_stateless(next_graph_state, transition_succ->dst);
399 xbt_dynar_push(successors, &next_pair);
406 if(xbt_dynar_length(successors) == 0){
408 next_pair = new_pair_stateless(next_graph_state, current_pair->automaton_state);
409 xbt_dynar_push(successors, &next_pair);
415 xbt_dynar_foreach(successors, cursor, pair_succ){
417 if((search_cycle == 1) && (reached(a, next_snapshot) == 1)){
418 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
419 XBT_INFO("| ACCEPTANCE CYCLE |");
420 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
421 XBT_INFO("Counter-example that violates formula :");
422 MC_show_stack_liveness_stateless(mc_stack_liveness_stateless);
423 MC_dump_stack_liveness_stateless(mc_stack_liveness_stateless);
424 MC_print_statistics_pairs(mc_stats_pair);
429 xbt_fifo_unshift(mc_stack_liveness_stateless, pair_succ);
432 MC_ddfs_stateless(a, search_cycle, 0);
435 if((search_cycle == 0) && ((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2))){
437 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);
438 int res = set_pair_reached(a, next_snapshot);
441 xbt_fifo_unshift(mc_stack_liveness_stateless, pair_succ);
444 MC_ddfs_stateless(a, 1, 1);
448 xbt_dynar_pop(reached_pairs, NULL);
454 if(MC_state_interleave_size(current_pair->graph_state) > 0){
455 XBT_DEBUG("Backtracking to depth %u", xbt_fifo_size(mc_stack_liveness_stateless));
456 MC_replay_liveness(mc_stack_liveness_stateless);
462 xbt_fifo_shift(mc_stack_liveness_stateless);
463 XBT_DEBUG("Pair (graph=%p, automaton =%p, search_cycle = %u) shifted in stack", current_pair->graph_state, current_pair->automaton_state, search_cycle);
468 /********************* Double-DFS stateful without visited state *******************/
471 void MC_ddfs_stateful_init(xbt_automaton_t a){
473 XBT_DEBUG("**************************************************");
474 XBT_DEBUG("Double-DFS stateful without visited state init");
475 XBT_DEBUG("**************************************************");
477 mc_pair_t mc_initial_pair;
478 mc_state_t initial_graph_state;
479 smx_process_t process;
480 mc_snapshot_t init_snapshot;
482 MC_wait_for_requests();
486 init_snapshot = xbt_new0(s_mc_snapshot_t, 1);
488 initial_graph_state = MC_state_pair_new();
489 xbt_swag_foreach(process, simix_global->process_list){
490 if(MC_process_is_enabled(process)){
491 MC_state_interleave_process(initial_graph_state, process);
495 reached_pairs = xbt_dynar_new(sizeof(mc_pair_reached_t), NULL);
497 MC_take_snapshot(init_snapshot);
501 unsigned int cursor = 0;
502 xbt_state_t state = NULL;
504 xbt_dynar_foreach(a->states, cursor, state){
505 if(state->type == -1){
508 mc_initial_pair = new_pair(init_snapshot, initial_graph_state, state);
509 xbt_fifo_unshift(mc_stack_liveness_stateful, mc_initial_pair);
513 MC_ddfs_stateful(a, 0, 0);
515 MC_restore_snapshot(init_snapshot);
517 MC_ddfs_stateful(a, 0, 0);
520 if(state->type == 2){
523 mc_initial_pair = new_pair(init_snapshot, initial_graph_state, state);
524 xbt_fifo_unshift(mc_stack_liveness_stateful, mc_initial_pair);
528 MC_ddfs_stateful(a, 1, 0);
530 MC_restore_snapshot(init_snapshot);
532 MC_ddfs_stateful(a, 1, 0);
540 void MC_ddfs_stateful(xbt_automaton_t a, int search_cycle, int restore){
542 smx_process_t process = NULL;
543 mc_pair_t current_pair = NULL;
545 if(xbt_fifo_size(mc_stack_liveness_stateful) == 0)
549 current_pair = (mc_pair_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack_liveness_stateful));
550 MC_restore_snapshot(current_pair->system_state);
551 xbt_swag_foreach(process, simix_global->process_list){
552 if(MC_process_is_enabled(process)){
553 MC_state_interleave_process(current_pair->graph_state, process);
559 /* Get current state */
560 current_pair = (mc_pair_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack_liveness_stateful));
563 XBT_DEBUG("********************* ( Depth = %d, search_cycle = %d )", xbt_fifo_size(mc_stack_liveness_stateful), search_cycle);
564 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));
566 a->current_state = current_pair->automaton_state;
568 mc_stats_pair->visited_pairs++;
571 mc_state_t next_graph_state = NULL;
572 smx_req_t req = NULL;
576 xbt_transition_t transition_succ;
580 xbt_dynar_t successors = NULL;
582 mc_pair_t next_pair = NULL;
583 mc_snapshot_t next_snapshot = NULL;
584 mc_snapshot_t current_snapshot = NULL;
588 successors = xbt_dynar_new(sizeof(mc_pair_t), NULL);
591 while((req = MC_state_get_request(current_pair->graph_state, &value)) != NULL){
594 current_snapshot = xbt_new0(s_mc_snapshot_t, 1);
595 MC_take_snapshot(current_snapshot);
599 /* Debug information */
600 if(XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)){
601 req_str = MC_request_to_string(req, value);
602 XBT_DEBUG("Execute: %s", req_str);
606 MC_state_set_executed_request(current_pair->graph_state, req, value);
608 /* Answer the request */
609 SIMIX_request_pre(req, value);
611 /* Wait for requests (schedules processes) */
612 MC_wait_for_requests();
615 /* Create the new expanded graph_state */
618 next_graph_state = MC_state_pair_new();
620 /* Get enabled process and insert it in the interleave set of the next graph_state */
621 xbt_swag_foreach(process, simix_global->process_list){
622 if(MC_process_is_enabled(process)){
623 MC_state_interleave_process(next_graph_state, process);
627 next_snapshot = xbt_new0(s_mc_snapshot_t, 1);
628 MC_take_snapshot(next_snapshot);
630 xbt_dynar_reset(successors);
636 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
638 res = MC_automaton_evaluate_label(a, transition_succ->label);
640 if(res == 1){ // enabled transition in automaton
642 next_pair = new_pair(next_snapshot,next_graph_state, transition_succ->dst);
643 xbt_dynar_push(successors, &next_pair);
650 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
652 res = MC_automaton_evaluate_label(a, transition_succ->label);
654 if(res == 2){ // transition always enabled in automaton
656 next_pair = new_pair(next_snapshot,next_graph_state, transition_succ->dst);
657 xbt_dynar_push(successors, &next_pair);
665 if(xbt_dynar_length(successors) == 0){
668 next_pair = new_pair(next_snapshot, next_graph_state, current_pair->automaton_state);
669 xbt_dynar_push(successors, &next_pair);
674 //XBT_DEBUG("Successors in automaton %lu", xbt_dynar_length(successors));
677 xbt_dynar_foreach(successors, cursor, pair_succ){
679 //XBT_DEBUG("Search visited pair : graph=%p, automaton=%p", pair_succ->graph_state, pair_succ->automaton_state);
681 if((search_cycle == 1) && (reached(a, next_snapshot) == 1)){
682 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
683 XBT_INFO("| ACCEPTANCE CYCLE |");
684 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
685 XBT_INFO("Counter-example that violates formula :");
686 MC_show_stack_liveness_stateful(mc_stack_liveness_stateful);
687 MC_dump_stack_liveness_stateful(mc_stack_liveness_stateful);
688 MC_print_statistics_pairs(mc_stats_pair);
692 //mc_stats_pair->executed_transitions++;
695 xbt_fifo_unshift(mc_stack_liveness_stateful, pair_succ);
698 MC_ddfs_stateful(a, search_cycle, 0);
701 if((search_cycle == 0) && ((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2))){
703 int res = set_pair_reached(a, next_snapshot);
704 XBT_DEBUG("Acceptance pair : graph=%p, automaton=%p(%s)", pair_succ->graph_state, pair_succ->automaton_state, pair_succ->automaton_state->id);
707 xbt_fifo_unshift(mc_stack_liveness_stateful, pair_succ);
710 MC_ddfs_stateful(a, 1, 1);
714 xbt_dynar_pop(reached_pairs, NULL);
721 if(MC_state_interleave_size(current_pair->graph_state) > 0){
722 XBT_DEBUG("Backtracking to depth %u", xbt_fifo_size(mc_stack_liveness_stateful));
723 MC_restore_snapshot(current_snapshot);
731 xbt_fifo_shift(mc_stack_liveness_stateful);
732 XBT_DEBUG("Pair (graph=%p, automaton =%p) shifted in stack", current_pair->graph_state, current_pair->automaton_state);