4 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_dfs, mc,
5 "Logging specific to MC DFS algorithm");
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;
24 void set_pair_visited(mc_state_t gs, xbt_state_t as, int sc){
26 if(visited(gs, as, sc) == 0){
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);
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){
67 void set_pair_reached(mc_state_t gs, xbt_state_t as){
69 if(reached(gs, as) == 0){
71 mc_reached_pairs_t p = NULL;
72 p = xbt_new0(s_mc_reached_pairs_t, 1);
74 p->automaton_state = as;
75 xbt_dynar_push(reached_pairs, &p);
76 //XBT_DEBUG("New reached pair : graph=%p, automaton=%p", gs, as);
82 int visited(mc_state_t gs, xbt_state_t as, int sc){
84 mc_visited_pairs_t p = NULL;
86 int different_process;
89 xbt_dynar_foreach(visited_pairs, c, p){
90 if((p->automaton_state == as) && (p->search_cycle == sc)){
92 for(i=0; i < gs->max_pid; i++){
93 if(gs->proc_status[i].state != p->graph_state->proc_status[i].state){
97 if(different_process==0){
108 void MC_dfs_init(xbt_automaton_t a){
110 XBT_DEBUG("**************************************************");
111 XBT_DEBUG("DFS init");
112 XBT_DEBUG("**************************************************");
114 mc_pairs_t mc_initial_pair;
115 mc_state_t initial_graph_state;
116 smx_process_t process;
117 mc_snapshot_t init_snapshot;
119 MC_wait_for_requests();
123 init_snapshot = xbt_new0(s_mc_snapshot_t, 1);
125 initial_graph_state = MC_state_new();
126 xbt_swag_foreach(process, simix_global->process_list){
127 if(MC_process_is_enabled(process)){
128 MC_state_interleave_process(initial_graph_state, process);
132 visited_pairs = xbt_dynar_new(sizeof(mc_visited_pairs_t), NULL);
133 reached_pairs = xbt_dynar_new(sizeof(mc_reached_pairs_t), NULL);
135 MC_take_snapshot(init_snapshot);
140 /* regarder dans l'automate toutes les transitions activables grâce à l'état initial du système
141 -> donnera les états initiaux de la propriété consistants avec l'état initial du système */
143 unsigned int cursor = 0;
144 unsigned int cursor2 = 0;
145 xbt_state_t state = NULL;
147 xbt_transition_t transition_succ;
148 xbt_dynar_t elses = xbt_dynar_new(sizeof(mc_pairs_t), NULL);
149 xbt_dynar_t successors = xbt_dynar_new(sizeof(mc_pairs_t), NULL);
150 // int enabled_transition = 0;
151 mc_pairs_t pair_succ;
153 xbt_dynar_foreach(a->states, cursor, state){
154 if(state->type == -1){
155 xbt_dynar_foreach(state->out, cursor2, transition_succ){
156 res = MC_automaton_evaluate_label(a, transition_succ->label);
162 mc_initial_pair = new_pair(init_snapshot, initial_graph_state, transition_succ->dst);
163 xbt_dynar_push(successors, &mc_initial_pair);
165 //XBT_DEBUG("New initial successor");
174 mc_initial_pair = new_pair(init_snapshot, initial_graph_state, transition_succ->dst);
175 xbt_dynar_push(elses, &mc_initial_pair);
177 //XBT_DEBUG("New initial successor");
186 if(xbt_dynar_length(successors) > 0){
189 xbt_dynar_foreach(successors, cursor, pair_succ){
192 xbt_fifo_unshift(mc_snapshot_stack, pair_succ);
194 XBT_DEBUG("**************************************************");
195 XBT_DEBUG("Initial state=%p ", pair_succ);
199 set_pair_visited(pair_succ->graph_state, pair_succ->automaton_state, 0);
210 if(xbt_dynar_length(elses) > 0){
212 xbt_dynar_foreach(elses, cursor, pair_succ){
215 xbt_fifo_unshift(mc_snapshot_stack, pair_succ);
217 XBT_DEBUG("**************************************************");
218 XBT_DEBUG("Initial state=%p ", pair_succ);
222 set_pair_visited(pair_succ->graph_state, pair_succ->automaton_state, 0);
231 XBT_DEBUG("No initial state !");
241 void MC_dfs(xbt_automaton_t a, int search_cycle, int restore){
244 if(xbt_fifo_size(mc_snapshot_stack) == 0)
248 MC_restore_snapshot(((mc_pairs_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_snapshot_stack)))->system_state);
253 //XBT_DEBUG("Lpv length after restore snapshot: %lu", xbt_dynar_length(visited_pairs));
255 /* Get current state */
256 mc_pairs_t current_pair = (mc_pairs_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_snapshot_stack));
258 XBT_DEBUG("**************************************************");
259 XBT_DEBUG("State : graph=%p, automaton=%p, %u interleave, stack size=%d", current_pair->graph_state, current_pair->automaton_state, MC_state_interleave_size(current_pair->graph_state), xbt_fifo_size(mc_snapshot_stack));
260 //XBT_DEBUG("Restore : %d", restore);
263 /* Update statistics */
264 //mc_stats->visited_states++;
265 //set_pair_visited(current_pair->graph_state, current_pair->automaton_state, search_cycle);
269 smx_process_t process = NULL;
271 mc_state_t next_graph_state = NULL;
272 smx_req_t req = NULL;
275 mc_pairs_t pair_succ;
276 xbt_transition_t transition_succ;
279 //int enabled_transition = 0;
281 xbt_dynar_t elses = xbt_dynar_new(sizeof(mc_pairs_t), NULL);
282 xbt_dynar_t successors = xbt_dynar_new(sizeof(mc_pairs_t), NULL);
284 mc_pairs_t next_pair;
285 mc_snapshot_t next_snapshot;
286 mc_snapshot_t current_snapshot;
288 while((req = MC_state_get_request(current_pair->graph_state, &value)) != NULL){
290 //XBT_DEBUG("Current pair : %p (%u interleave)", current_pair, MC_state_interleave_size(current_pair->graph_state)+1);
291 //XBT_DEBUG("Visited pairs : %lu", xbt_dynar_length(visited_pairs));
294 current_snapshot = xbt_new0(s_mc_snapshot_t, 1);
295 MC_take_snapshot(current_snapshot);
299 /* Debug information */
300 if(XBT_LOG_ISENABLED(mc_dfs, xbt_log_priority_debug)){
301 req_str = MC_request_to_string(req, value);
302 XBT_DEBUG("Execute: %s", req_str);
306 MC_state_set_executed_request(current_pair->graph_state, req, value);
307 //mc_stats->executed_transitions++;
309 /* Answer the request */
310 SIMIX_request_pre(req, value);
312 /* Wait for requests (schedules processes) */
313 MC_wait_for_requests();
316 /* Create the new expanded graph_state */
319 next_graph_state = MC_state_new();
321 /* Get enabled process and insert it in the interleave set of the next graph_state */
322 xbt_swag_foreach(process, simix_global->process_list){
323 if(MC_process_is_enabled(process)){
324 MC_state_interleave_process(next_graph_state, process);
328 next_snapshot = xbt_new0(s_mc_snapshot_t, 1);
329 MC_take_snapshot(next_snapshot);
333 xbt_dynar_reset(elses);
334 xbt_dynar_reset(successors);
337 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
339 res = MC_automaton_evaluate_label(a, transition_succ->label);
342 next_pair = new_pair(next_snapshot,next_graph_state, transition_succ->dst);
344 //XBT_DEBUG("Next pair : %p", next_pair);
346 if(res == 1){ // enabled transition in automaton
347 xbt_dynar_push(successors, &next_pair);
348 //XBT_DEBUG("New Successors length : %lu", xbt_dynar_length(successors));
351 xbt_dynar_push(elses, &next_pair);
352 //XBT_DEBUG("New Elses length : %lu", xbt_dynar_length(elses));
360 if((xbt_dynar_length(successors) == 0) && (xbt_dynar_length(elses) == 0) ){
363 next_pair = new_pair(next_snapshot, next_graph_state, current_pair->automaton_state);
364 xbt_dynar_push(successors, &next_pair);
369 //XBT_DEBUG("Successors length : %lu", xbt_dynar_length(successors));
370 //XBT_DEBUG("Elses length : %lu", xbt_dynar_length(elses));
372 if(xbt_dynar_length(successors) >0){
375 xbt_dynar_foreach(successors, cursor, pair_succ){
377 //XBT_DEBUG("Search visited pair : graph=%p, automaton=%p", pair_succ->graph_state, pair_succ->automaton_state);
379 if(visited(pair_succ->graph_state, pair_succ->automaton_state, search_cycle) == 0){
382 xbt_fifo_unshift(mc_snapshot_stack, pair_succ);
385 set_pair_visited(pair_succ->graph_state, pair_succ->automaton_state, search_cycle);
386 MC_dfs(a, search_cycle, 0);
388 if((search_cycle == 0) && (pair_succ->automaton_state->type == 1)){
390 set_pair_reached(pair_succ->graph_state, pair_succ->automaton_state);
391 XBT_DEBUG("Acceptance state : graph=%p, automaton=%p", pair_succ->graph_state, pair_succ->automaton_state);
396 //XBT_DEBUG("Pair (graph=%p, automaton=%p) already visited !", pair_succ->graph_state, pair_succ->automaton_state );
397 set_pair_visited(pair_succ->graph_state, pair_succ->automaton_state, search_cycle);
405 xbt_dynar_foreach(elses, cursor, pair_succ){
407 //XBT_DEBUG("Search visited pair : graph=%p, automaton=%p", pair_succ->graph_state, pair_succ->automaton_state);
409 if(visited(pair_succ->graph_state, pair_succ->automaton_state, search_cycle) == 0){
412 xbt_fifo_unshift(mc_snapshot_stack, pair_succ);
415 set_pair_visited(pair_succ->graph_state, pair_succ->automaton_state, search_cycle);
416 MC_dfs(a, search_cycle, 0);
418 if((search_cycle == 0) && (pair_succ->automaton_state->type == 1)){
420 set_pair_reached(pair_succ->graph_state, pair_succ->automaton_state);
421 XBT_DEBUG("Acceptance state : graph state=%p, automaton state=%p",pair_succ->graph_state, pair_succ->automaton_state);
426 //XBT_DEBUG("Pair (graph=%p, automaton=%p) already visited !", pair_succ->graph_state, pair_succ->automaton_state );
427 /* Different graph_state */
428 set_pair_visited(pair_succ->graph_state, pair_succ->automaton_state, search_cycle);
435 if(MC_state_interleave_size(current_pair->graph_state) > 0){
436 MC_restore_snapshot(current_snapshot);
438 //XBT_DEBUG("Snapshot restored");
442 if((search_cycle == 1) && (reached(current_pair->graph_state, current_pair->automaton_state) == 1)){
443 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
444 XBT_INFO("| ACCEPTANCE CYCLE |");
445 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
446 XBT_INFO("Counter-example that violates formula :");
447 MC_show_snapshot_stack(mc_snapshot_stack);
448 MC_dump_snapshot_stack(mc_snapshot_stack);
453 xbt_fifo_shift(mc_snapshot_stack);
454 //XBT_DEBUG("State shifted in snapshot_stack, mc_snapshot_stack size=%d", xbt_fifo_size(mc_snapshot_stack));
460 void MC_show_snapshot_stack(xbt_fifo_t stack){
463 xbt_fifo_item_t item;
465 char *req_str = NULL;
467 for (item = xbt_fifo_get_last_item(stack);
468 (item ? (pair = (mc_pairs_t) (xbt_fifo_get_item_content(item)))
469 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
470 req = MC_state_get_executed_request(pair->graph_state, &value);
472 req_str = MC_request_to_string(req, value);
473 XBT_INFO("%s", req_str);
479 void MC_dump_snapshot_stack(xbt_fifo_t stack){
483 while ((pair = (mc_pairs_t) xbt_fifo_pop(stack)) != NULL)
484 MC_pair_delete(pair);
488 void MC_pair_delete(mc_pairs_t pair){
489 xbt_free(pair->graph_state->proc_status);
490 xbt_free(pair->graph_state);
491 //xbt_free(pair->automaton_state); -> FIXME : à implémenter
496 int MC_automaton_evaluate_label(xbt_automaton_t a, xbt_exp_label_t l){
500 int left_res = MC_automaton_evaluate_label(a, l->u.or_and.left_exp);
501 int right_res = MC_automaton_evaluate_label(a, l->u.or_and.right_exp);
502 return (left_res || right_res);
506 int left_res = MC_automaton_evaluate_label(a, l->u.or_and.left_exp);
507 int right_res = MC_automaton_evaluate_label(a, l->u.or_and.right_exp);
508 return (left_res && right_res);
512 int res = MC_automaton_evaluate_label(a, l->u.exp_not);
517 unsigned int cursor = 0;
518 xbt_propositional_symbol_t p = NULL;
519 xbt_dynar_foreach(a->propositional_symbols, cursor, p){
520 if(strcmp(p->pred, l->u.predicat) == 0){
521 int (*f)() = p->function;
537 /*void MC_dfs(xbt_automaton_t a, int search_cycle){
539 xbt_state_t current_state = (xbt_state_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(stack_automaton_dfs));
540 xbt_transition_t transition_succ = NULL;
541 xbt_state_t successor = NULL;
542 unsigned int cursor = 0;
543 xbt_dynar_t elses = xbt_dynar_new(sizeof(xbt_transition_t), NULL);
546 //printf("++ current state : %s\n", current_state->id);
548 xbt_dynar_foreach(current_state->out, cursor, transition_succ){
549 successor = transition_succ->dst;
550 res = MC_automaton_evaluate_label(a, transition_succ->label);
551 //printf("-- state : %s, transition_label : %d, res = %d\n", successor->id, transition_succ->label->type, res);
553 if(search_cycle == 1 && reached_dfs != NULL){
554 if(strcmp(reached_dfs->id, successor->id) == 0){
555 xbt_dynar_push(state_automaton_visited_dfs, &successor);
556 printf("\n-*-*-*-*-*-*- ACCEPTANCE CYCLE -*-*-*-*-*-*-\n");
557 printf("Visited states : \n");
558 unsigned int cursor2 = 0;
559 xbt_state_t visited_state = NULL;
560 xbt_dynar_foreach(state_automaton_visited_dfs, cursor2, visited_state){
561 printf("State : %s\n", visited_state->id);
566 if(successor->visited == 0){
567 successor->visited = 1;
568 xbt_fifo_unshift(stack_automaton_dfs, successor);
569 xbt_dynar_push(state_automaton_visited_dfs, &successor);
570 MC_dfs(a, search_cycle);
571 if(search_cycle == 0 && successor->type == 1){
572 reached_dfs = successor;
578 xbt_dynar_push(elses,&transition_succ);
584 xbt_dynar_foreach(elses, cursor, transition_succ){
585 successor = transition_succ->dst;
586 if(search_cycle == 1 && reached_dfs != NULL){
587 if(strcmp(reached_dfs->id, successor->id) == 0){
588 xbt_dynar_push(state_automaton_visited_dfs, &successor);
589 printf("\n-*-*-*-*-*-*- ACCEPTANCE CYCLE -*-*-*-*-*-*-\n");
590 printf("Visited states : \n");
591 unsigned int cursor2 = 0;
592 xbt_state_t visited_state = NULL;
593 xbt_dynar_foreach(state_automaton_visited_dfs, cursor2, visited_state){
594 printf("State : %s\n", visited_state->id);
599 if(successor->visited == 0){
600 successor->visited = 1;
601 xbt_fifo_unshift(stack_automaton_dfs, successor);
602 xbt_dynar_push(state_automaton_visited_dfs, &successor);
603 MC_dfs(a, search_cycle);
604 if(search_cycle == 0 && successor->type == 1){
605 reached_dfs = successor;
611 xbt_fifo_shift(stack_automaton_dfs);