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;
44 unsigned int cursor = 0;
47 xbt_dynar_foreach(reached_pairs, cursor, rp){
48 if((rp->graph_state == gs) &&(rp->automaton_state == as)){
57 void set_pair_reached(mc_state_t gs, xbt_state_t as){
59 if(reached(gs, as) == 0){
61 mc_reached_pairs_t p = NULL;
62 p = xbt_new0(s_mc_reached_pairs_t, 1);
64 p->automaton_state = as;
65 xbt_dynar_push(reached_pairs, &p);
66 XBT_DEBUG("New reached pair : graph=%p, automaton=%p", gs, as);
72 int visited(mc_state_t gs, xbt_state_t as, int sc){
74 mc_visited_pairs_t p = NULL;
76 int different_process;
79 xbt_dynar_foreach(visited_pairs, c, p){
80 if((p->automaton_state == as) && (p->search_cycle == sc)){
82 for(i=0; i < gs->max_pid; i++){
83 if(gs->proc_status[i].state != p->graph_state->proc_status[i].state){
87 if(different_process==0){
98 void MC_dfs_init(xbt_automaton_t a){
100 XBT_DEBUG("**************************************************");
101 XBT_DEBUG("DFS init");
102 XBT_DEBUG("**************************************************");
104 mc_pairs_t mc_initial_pair;
105 mc_state_t initial_graph_state;
106 smx_process_t process;
107 mc_snapshot_t init_snapshot;
109 MC_wait_for_requests();
113 init_snapshot = xbt_new0(s_mc_snapshot_t, 1);
115 initial_graph_state = MC_state_new();
116 xbt_swag_foreach(process, simix_global->process_list){
117 if(MC_process_is_enabled(process)){
118 MC_state_interleave_process(initial_graph_state, process);
122 visited_pairs = xbt_dynar_new(sizeof(mc_visited_pairs_t), NULL);
123 reached_pairs = xbt_dynar_new(sizeof(mc_reached_pairs_t), NULL);
125 MC_take_snapshot(init_snapshot);
130 /* regarder dans l'automate toutes les transitions activables grâce à l'état initial du système
131 -> donnera les états initiaux de la propriété consistants avec l'état initial du système */
133 unsigned int cursor = 0;
134 unsigned int cursor2 = 0;
135 xbt_state_t state = NULL;
137 xbt_transition_t transition_succ;
138 xbt_dynar_t elses = xbt_dynar_new(sizeof(mc_pairs_t), NULL);
139 xbt_dynar_t successors = xbt_dynar_new(sizeof(mc_pairs_t), NULL);
140 // int enabled_transition = 0;
141 mc_pairs_t pair_succ;
143 xbt_dynar_foreach(a->states, cursor, state){
144 if(state->type == -1){
145 xbt_dynar_foreach(state->out, cursor2, transition_succ){
146 res = MC_automaton_evaluate_label(a, transition_succ->label);
152 mc_initial_pair = new_pair(init_snapshot, initial_graph_state, transition_succ->dst);
153 xbt_dynar_push(successors, &mc_initial_pair);
155 //XBT_DEBUG("New initial successor");
164 mc_initial_pair = new_pair(init_snapshot, initial_graph_state, transition_succ->dst);
165 xbt_dynar_push(elses, &mc_initial_pair);
167 //XBT_DEBUG("New initial successor");
176 if(xbt_dynar_length(successors) > 0){
179 xbt_dynar_foreach(successors, cursor, pair_succ){
182 xbt_fifo_unshift(mc_snapshot_stack, pair_succ);
184 XBT_DEBUG("**************************************************");
185 XBT_DEBUG("Initial state=%p ", pair_succ);
189 set_pair_visited(pair_succ->graph_state, pair_succ->automaton_state, 0);
200 if(xbt_dynar_length(elses) > 0){
202 xbt_dynar_foreach(elses, cursor, pair_succ){
205 xbt_fifo_unshift(mc_snapshot_stack, pair_succ);
207 XBT_DEBUG("**************************************************");
208 XBT_DEBUG("Initial state=%p ", pair_succ);
212 set_pair_visited(pair_succ->graph_state, pair_succ->automaton_state, 0);
221 XBT_DEBUG("No initial state !");
231 void MC_dfs(xbt_automaton_t a, int search_cycle, int restore){
234 if(xbt_fifo_size(mc_snapshot_stack) == 0)
238 MC_restore_snapshot(((mc_pairs_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_snapshot_stack)))->system_state);
243 //XBT_DEBUG("Lpv length after restore snapshot: %lu", xbt_dynar_length(visited_pairs));
245 /* Get current state */
246 mc_pairs_t current_pair = (mc_pairs_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_snapshot_stack));
248 XBT_DEBUG("**************************************************");
249 XBT_DEBUG("State=%p, %u interleave, mc_snapshot_stack size=%d", current_pair, MC_state_interleave_size(current_pair->graph_state), xbt_fifo_size(mc_snapshot_stack));
250 //XBT_DEBUG("Restore : %d", restore);
253 /* Update statistics */
254 //mc_stats->visited_states++;
255 //set_pair_visited(current_pair->graph_state, current_pair->automaton_state, search_cycle);
259 smx_process_t process = NULL;
261 mc_state_t next_graph_state = NULL;
262 smx_req_t req = NULL;
265 mc_pairs_t pair_succ;
266 xbt_transition_t transition_succ;
269 //int enabled_transition = 0;
271 xbt_dynar_t elses = xbt_dynar_new(sizeof(mc_pairs_t), NULL);
272 xbt_dynar_t successors = xbt_dynar_new(sizeof(mc_pairs_t), NULL);
274 mc_pairs_t next_pair;
275 mc_snapshot_t next_snapshot;
276 mc_snapshot_t current_snapshot;
278 while((req = MC_state_get_request(current_pair->graph_state, &value)) != NULL){
280 XBT_DEBUG("Current pair : %p (%u interleave)", current_pair, MC_state_interleave_size(current_pair->graph_state)+1);
281 //XBT_DEBUG("Visited pairs : %lu", xbt_dynar_length(visited_pairs));
284 current_snapshot = xbt_new0(s_mc_snapshot_t, 1);
285 MC_take_snapshot(current_snapshot);
289 /* Debug information */
290 if(XBT_LOG_ISENABLED(mc_dfs, xbt_log_priority_debug)){
291 req_str = MC_request_to_string(req, value);
292 XBT_DEBUG("Execute: %s", req_str);
296 MC_state_set_executed_request(current_pair->graph_state, req, value);
297 //mc_stats->executed_transitions++;
299 /* Answer the request */
300 SIMIX_request_pre(req, value);
302 /* Wait for requests (schedules processes) */
303 MC_wait_for_requests();
306 /* Create the new expanded graph_state */
309 next_graph_state = MC_state_new();
311 /* Get enabled process and insert it in the interleave set of the next graph_state */
312 xbt_swag_foreach(process, simix_global->process_list){
313 if(MC_process_is_enabled(process)){
314 MC_state_interleave_process(next_graph_state, process);
318 next_snapshot = xbt_new0(s_mc_snapshot_t, 1);
319 MC_take_snapshot(next_snapshot);
323 xbt_dynar_reset(elses);
324 xbt_dynar_reset(successors);
327 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
329 res = MC_automaton_evaluate_label(a, transition_succ->label);
332 next_pair = new_pair(next_snapshot,next_graph_state, transition_succ->dst);
334 //XBT_DEBUG("Next pair : %p", next_pair);
336 if(res == 1){ // enabled transition in automaton
337 xbt_dynar_push(successors, &next_pair);
338 XBT_DEBUG("New Successors length : %lu", xbt_dynar_length(successors));
341 xbt_dynar_push(elses, &next_pair);
342 XBT_DEBUG("New Elses length : %lu", xbt_dynar_length(elses));
350 if((xbt_dynar_length(successors) == 0) && (xbt_dynar_length(elses) == 0) ){
353 next_pair = new_pair(next_snapshot, next_graph_state, current_pair->automaton_state);
354 xbt_dynar_push(successors, &next_pair);
359 //XBT_DEBUG("Successors length : %lu", xbt_dynar_length(successors));
360 //XBT_DEBUG("Elses length : %lu", xbt_dynar_length(elses));
362 if(xbt_dynar_length(successors) >0){
365 xbt_dynar_foreach(successors, cursor, pair_succ){
367 //XBT_DEBUG("Search visited pair : graph=%p, automaton=%p", pair_succ->graph_state, pair_succ->automaton_state);
369 if(visited(pair_succ->graph_state, pair_succ->automaton_state, search_cycle) == 0){
372 xbt_fifo_unshift(mc_snapshot_stack, pair_succ);
375 set_pair_visited(pair_succ->graph_state, pair_succ->automaton_state, search_cycle);
377 MC_dfs(a, search_cycle, 0);
379 if((search_cycle == 0) && (pair_succ->automaton_state->type == 1)){
381 mc_pairs_t first_item = (mc_pairs_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_snapshot_stack));
382 if((first_item->graph_state != pair_succ->graph_state) || (first_item->automaton_state != pair_succ->automaton_state) ){
384 xbt_fifo_unshift(mc_snapshot_stack, pair_succ);
388 set_pair_reached(pair_succ->graph_state, pair_succ->automaton_state);
389 XBT_DEBUG("Acceptance state : graph=%p, automaton=%p", pair_succ->graph_state, pair_succ->automaton_state);
393 XBT_DEBUG("Pair (graph=%p, automaton=%p) already visited !", pair_succ->graph_state, pair_succ->automaton_state );
394 set_pair_visited(pair_succ->graph_state, pair_succ->automaton_state, search_cycle);
402 xbt_dynar_foreach(elses, cursor, pair_succ){
404 //XBT_DEBUG("Search visited pair : graph=%p, automaton=%p", pair_succ->graph_state, pair_succ->automaton_state);
406 if(visited(pair_succ->graph_state, pair_succ->automaton_state, search_cycle) == 0){
409 xbt_fifo_unshift(mc_snapshot_stack, pair_succ);
412 set_pair_visited(pair_succ->graph_state, pair_succ->automaton_state, search_cycle);
413 MC_dfs(a, search_cycle, 0);
415 if((search_cycle == 0) && (pair_succ->automaton_state->type == 1)){
417 mc_pairs_t first_item = (mc_pairs_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_snapshot_stack));
418 if((first_item->graph_state != pair_succ->graph_state) || (first_item->automaton_state != pair_succ->automaton_state)){
420 xbt_fifo_unshift(mc_snapshot_stack, pair_succ);
424 set_pair_reached(pair_succ->graph_state, pair_succ->automaton_state);
425 XBT_DEBUG("Acceptance state : graph state=%p, automaton state=%p",pair_succ->graph_state, pair_succ->automaton_state);
429 XBT_DEBUG("Pair (graph=%p, automaton=%p) already visited !", pair_succ->graph_state, pair_succ->automaton_state );
430 set_pair_visited(pair_succ->graph_state, pair_succ->automaton_state, search_cycle);
437 if(MC_state_interleave_size(current_pair->graph_state) > 0){
438 MC_restore_snapshot(current_snapshot);
440 XBT_DEBUG("Snapshot restored");
444 if((search_cycle == 1) && (reached(current_pair->graph_state, current_pair->automaton_state) == 1)){
445 XBT_DEBUG("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
446 XBT_DEBUG("| ACCEPTANCE CYCLE |");
447 XBT_DEBUG("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
448 // afficher chemin menant au cycle d'acceptation
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 int MC_automaton_evaluate_label(xbt_automaton_t a, xbt_exp_label_t l){
464 int left_res = MC_automaton_evaluate_label(a, l->u.or_and.left_exp);
465 int right_res = MC_automaton_evaluate_label(a, l->u.or_and.right_exp);
466 return (left_res || right_res);
470 int left_res = MC_automaton_evaluate_label(a, l->u.or_and.left_exp);
471 int right_res = MC_automaton_evaluate_label(a, l->u.or_and.right_exp);
472 return (left_res && right_res);
476 int res = MC_automaton_evaluate_label(a, l->u.exp_not);
481 unsigned int cursor = 0;
482 xbt_propositional_symbol_t p = NULL;
483 xbt_dynar_foreach(a->propositional_symbols, cursor, p){
484 if(strcmp(p->pred, l->u.predicat) == 0){
485 int (*f)() = p->function;
501 /*void MC_dfs(xbt_automaton_t a, int search_cycle){
503 xbt_state_t current_state = (xbt_state_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(stack_automaton_dfs));
504 xbt_transition_t transition_succ = NULL;
505 xbt_state_t successor = NULL;
506 unsigned int cursor = 0;
507 xbt_dynar_t elses = xbt_dynar_new(sizeof(xbt_transition_t), NULL);
510 //printf("++ current state : %s\n", current_state->id);
512 xbt_dynar_foreach(current_state->out, cursor, transition_succ){
513 successor = transition_succ->dst;
514 res = MC_automaton_evaluate_label(a, transition_succ->label);
515 //printf("-- state : %s, transition_label : %d, res = %d\n", successor->id, transition_succ->label->type, res);
517 if(search_cycle == 1 && reached_dfs != NULL){
518 if(strcmp(reached_dfs->id, successor->id) == 0){
519 xbt_dynar_push(state_automaton_visited_dfs, &successor);
520 printf("\n-*-*-*-*-*-*- ACCEPTANCE CYCLE -*-*-*-*-*-*-\n");
521 printf("Visited states : \n");
522 unsigned int cursor2 = 0;
523 xbt_state_t visited_state = NULL;
524 xbt_dynar_foreach(state_automaton_visited_dfs, cursor2, visited_state){
525 printf("State : %s\n", visited_state->id);
530 if(successor->visited == 0){
531 successor->visited = 1;
532 xbt_fifo_unshift(stack_automaton_dfs, successor);
533 xbt_dynar_push(state_automaton_visited_dfs, &successor);
534 MC_dfs(a, search_cycle);
535 if(search_cycle == 0 && successor->type == 1){
536 reached_dfs = successor;
542 xbt_dynar_push(elses,&transition_succ);
548 xbt_dynar_foreach(elses, cursor, transition_succ){
549 successor = transition_succ->dst;
550 if(search_cycle == 1 && reached_dfs != NULL){
551 if(strcmp(reached_dfs->id, successor->id) == 0){
552 xbt_dynar_push(state_automaton_visited_dfs, &successor);
553 printf("\n-*-*-*-*-*-*- ACCEPTANCE CYCLE -*-*-*-*-*-*-\n");
554 printf("Visited states : \n");
555 unsigned int cursor2 = 0;
556 xbt_state_t visited_state = NULL;
557 xbt_dynar_foreach(state_automaton_visited_dfs, cursor2, visited_state){
558 printf("State : %s\n", visited_state->id);
563 if(successor->visited == 0){
564 successor->visited = 1;
565 xbt_fifo_unshift(stack_automaton_dfs, successor);
566 xbt_dynar_push(state_automaton_visited_dfs, &successor);
567 MC_dfs(a, search_cycle);
568 if(search_cycle == 0 && successor->type == 1){
569 reached_dfs = successor;
575 xbt_fifo_shift(stack_automaton_dfs);