Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
117c99f7c2e384bdbaee1a886e47f55cb25da094
[simgrid.git] / src / mc / mc_liveness.c
1 /* Copyright (c) 2011-2013. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "mc_private.h"
8 #include <unistd.h>
9 #include <sys/wait.h>
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_liveness, mc,
12                                 "Logging specific to algorithms for liveness properties verification");
13
14 /********* Global variables *********/
15
16 xbt_dynar_t acceptance_pairs;
17 xbt_dynar_t visited_pairs;
18 xbt_dynar_t successors;
19 xbt_parmap_t parmap;
20
21 /********* Static functions *********/
22
23 static xbt_dynar_t get_atomic_propositions_values(){
24   int res;
25   int_f_void_t f;
26   unsigned int cursor = 0;
27   xbt_automaton_propositional_symbol_t ps = NULL;
28   xbt_dynar_t values = xbt_dynar_new(sizeof(int), NULL);
29
30   xbt_dynar_foreach(_mc_property_automaton->propositional_symbols, cursor, ps){
31     f = (int_f_void_t)ps->function;
32     res = f();
33     xbt_dynar_push_as(values, int, res);
34   }
35
36   return values;
37 }
38
39 static int get_search_interval(xbt_dynar_t all_pairs, mc_pair_t pair, int *min, int *max){
40
41   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
42
43   MC_SET_RAW_MEM;
44
45   int cursor = 0, previous_cursor, next_cursor;
46   mc_pair_t pair_test;
47   int start = 0;
48   int end = xbt_dynar_length(all_pairs) - 1;
49   
50   while(start <= end){
51     cursor = (start + end) / 2;
52     pair_test = (mc_pair_t)xbt_dynar_get_as(all_pairs, cursor, mc_pair_t);
53     if(pair_test->nb_processes < pair->nb_processes){
54       start = cursor + 1;
55     }else if(pair_test->nb_processes > pair->nb_processes){
56       end = cursor - 1;
57     }else{
58       if(pair_test->heap_bytes_used < pair->heap_bytes_used){
59         start = cursor +1;
60       }else if(pair_test->heap_bytes_used > pair->heap_bytes_used){
61         end = cursor - 1;
62       }else{
63         *min = *max = cursor;
64         previous_cursor = cursor - 1;
65         while(previous_cursor >= 0){
66           pair_test = (mc_pair_t)xbt_dynar_get_as(all_pairs, previous_cursor, mc_pair_t);
67           if(pair_test->nb_processes != pair->nb_processes || pair_test->heap_bytes_used != pair->heap_bytes_used)
68             break;
69           *min = previous_cursor;
70           previous_cursor--;
71         }
72         next_cursor = cursor + 1;
73         while(next_cursor < xbt_dynar_length(all_pairs)){
74           pair_test = (mc_pair_t)xbt_dynar_get_as(all_pairs, next_cursor, mc_pair_t);
75           if(pair_test->nb_processes != pair->nb_processes || pair_test->heap_bytes_used != pair->heap_bytes_used)
76             break;
77           *max = next_cursor;
78           next_cursor++;
79         }
80         if(!raw_mem_set)
81           MC_UNSET_RAW_MEM;
82         return -1;
83       }
84      }
85   }
86
87   if(!raw_mem_set)
88     MC_UNSET_RAW_MEM;
89
90   return cursor;
91 }
92
93 static int is_reached_acceptance_pair(mc_pair_t pair){
94
95   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
96
97   MC_SET_RAW_MEM;
98  
99   if(xbt_dynar_is_empty(acceptance_pairs)){
100
101     if(pair->graph_state->system_state == NULL){
102       pair->graph_state->system_state = MC_take_snapshot();
103       pair->heap_bytes_used = mmalloc_get_bytes_used(std_heap);
104     }
105     xbt_dynar_push(acceptance_pairs, &pair); 
106
107     if(!raw_mem_set)
108       MC_UNSET_RAW_MEM;
109
110     return -1;
111
112   }else{
113
114     if(pair->graph_state->system_state == NULL){
115       pair->graph_state->system_state = MC_take_snapshot();
116       pair->heap_bytes_used = mmalloc_get_bytes_used(std_heap);
117     }
118
119     int min = -1, max = -1, index;
120     //int res;
121     mc_pair_t pair_test;
122     int cursor;
123
124     index = get_search_interval(acceptance_pairs, pair, &min, &max);
125
126     if(min != -1 && max != -1){ // Acceptance pair with same number of processes and same heap bytes used exists
127       /*res = xbt_parmap_mc_apply(parmap, snapshot_compare, xbt_dynar_get_ptr(acceptance_pairs, min), (max-min)+1, pair);
128       if(res != -1){
129         if(!raw_mem_set)
130           MC_UNSET_RAW_MEM;
131         return ((mc_pair_t)xbt_dynar_get_as(acceptance_pairs, (min+res)-1, mc_pair_t))->num;
132         }*/
133       cursor = min;
134       while(cursor <= max){
135         pair_test = (mc_pair_t)xbt_dynar_get_as(acceptance_pairs, cursor, mc_pair_t);
136         if(xbt_automaton_state_compare(pair_test->automaton_state, pair->automaton_state) == 0){
137           if(xbt_automaton_propositional_symbols_compare_value(pair_test->atomic_propositions, pair->atomic_propositions) == 0){
138             if(snapshot_compare(pair_test, pair) == 0){
139               if(!raw_mem_set)
140                 MC_UNSET_RAW_MEM;
141               return pair_test->num;
142             }
143           }
144         }
145         cursor++;
146       }
147       xbt_dynar_insert_at(acceptance_pairs, min, &pair);
148     }else{
149       pair_test = (mc_pair_t)xbt_dynar_get_as(acceptance_pairs, index, mc_pair_t);
150       if(pair_test->nb_processes < pair->nb_processes){
151         xbt_dynar_insert_at(acceptance_pairs, index+1, &pair);
152       }else{
153         if(pair_test->heap_bytes_used < pair->heap_bytes_used)
154           xbt_dynar_insert_at(acceptance_pairs, index + 1, &pair);
155         else
156           xbt_dynar_insert_at(acceptance_pairs, index, &pair);
157       }
158     }
159
160     if(!raw_mem_set)
161       MC_UNSET_RAW_MEM;
162     
163     return -1;
164     
165   }
166  
167 }
168
169
170 static void set_acceptance_pair_reached(mc_pair_t pair){
171
172   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
173
174   MC_SET_RAW_MEM;
175
176   if(xbt_dynar_is_empty(acceptance_pairs)){
177
178      if(pair->graph_state->system_state == NULL){
179        pair->graph_state->system_state = MC_take_snapshot();
180        pair->heap_bytes_used = mmalloc_get_bytes_used(std_heap);
181      }
182      xbt_dynar_push(acceptance_pairs, &pair); 
183
184   }else{
185
186     if(pair->graph_state->system_state == NULL){
187       pair->graph_state->system_state = MC_take_snapshot();
188       pair->heap_bytes_used = mmalloc_get_bytes_used(std_heap);
189     }
190     
191     size_t current_bytes_used = pair->heap_bytes_used;
192     int current_nb_processes = pair->nb_processes;
193
194     int cursor = 0;
195     int start = 0;
196     int end = xbt_dynar_length(acceptance_pairs) - 1;
197
198     mc_pair_t pair_test = NULL;
199     size_t bytes_used_test = 0;
200     int nb_processes_test;
201
202     while(start <= end){
203       cursor = (start + end) / 2;
204       pair_test = (mc_pair_t)xbt_dynar_get_as(acceptance_pairs, cursor, mc_pair_t);
205       bytes_used_test = pair_test->heap_bytes_used;
206       nb_processes_test = pair_test->nb_processes;
207       if(nb_processes_test < current_nb_processes)
208         start = cursor + 1;
209       if(nb_processes_test > current_nb_processes)
210         end = cursor - 1; 
211       if(nb_processes_test == current_nb_processes){
212         if(bytes_used_test < current_bytes_used)
213           start = cursor + 1;
214         if(bytes_used_test > current_bytes_used)
215           end = cursor - 1;
216         if(bytes_used_test == current_bytes_used)
217           break;
218       }
219     }
220
221     if(pair_test->nb_processes < pair->nb_processes){
222       xbt_dynar_insert_at(acceptance_pairs, cursor+1, &pair);
223     }else{
224       if(pair_test->heap_bytes_used < pair->heap_bytes_used)
225         xbt_dynar_insert_at(acceptance_pairs, cursor + 1, &pair);
226       else
227         xbt_dynar_insert_at(acceptance_pairs, cursor, &pair);
228     }    
229
230   }
231
232   if(!raw_mem_set)
233     MC_UNSET_RAW_MEM;
234     
235 }
236
237 static void remove_acceptance_pair(mc_pair_t pair){
238
239   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
240
241   MC_SET_RAW_MEM;
242
243   unsigned int cursor = 0;
244   mc_pair_t pair_test;
245   int pair_found = 0;
246
247   xbt_dynar_foreach(acceptance_pairs, cursor, pair_test){
248     if(pair_test->num == pair->num){
249       pair_found = 1;
250       break;
251     }
252   }
253
254   if(pair_found)
255     xbt_dynar_remove_at(acceptance_pairs, cursor, NULL);
256   
257   pair->acceptance_removed = 1;
258
259   if(pair->stack_removed && pair->acceptance_removed){
260     if(_sg_mc_visited == 0){
261       MC_pair_delete(pair);
262     }else if(pair->visited_removed){
263       MC_pair_delete(pair);
264     }
265   }
266
267   if(!raw_mem_set)
268     MC_UNSET_RAW_MEM;
269 }
270
271 static int is_visited_pair(mc_pair_t pair){
272
273   if(_sg_mc_visited == 0)
274     return -1;
275
276   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
277
278   MC_SET_RAW_MEM;
279
280   if(xbt_dynar_is_empty(visited_pairs)){
281
282     if(pair->graph_state->system_state == NULL)
283       pair->graph_state->system_state = MC_take_snapshot();
284     xbt_dynar_push(visited_pairs, &pair); 
285
286     if(!raw_mem_set)
287       MC_UNSET_RAW_MEM;
288
289     return -1;
290
291   }else{
292
293     if(pair->graph_state->system_state == NULL)
294       pair->graph_state->system_state = MC_take_snapshot();
295
296     int min = -1, max = -1, index;
297     //int res;
298     mc_pair_t pair_test;
299     int cursor;
300
301     index = get_search_interval(visited_pairs, pair, &min, &max);
302
303     if(min != -1 && max != -1){ // Visited pair with same number of processes and same heap bytes used exists
304       /*res = xbt_parmap_mc_apply(parmap, snapshot_compare, xbt_dynar_get_ptr(visited_pairs, min), (max-min)+1, pair);
305       if(res != -1){
306         pair_test = (mc_pair_t)xbt_dynar_get_as(visited_pairs, (min+res)-1, mc_pair_t);
307         if(pair_test->other_num == -1)
308           pair->other_num = pair_test->num;
309         else
310           pair->other_num = pair_test->other_num;
311         if(dot_output == NULL)
312           XBT_DEBUG("Pair %d already visited ! (equal to pair %d)", pair->num, pair_test->num);
313         else
314           XBT_DEBUG("Pair %d already visited ! (equal to pair %d (pair %d in dot_output))", pair->num, pair_test->num, pair->other_num);
315         xbt_dynar_remove_at(visited_pairs, (min + res) - 1, NULL);
316         xbt_dynar_insert_at(visited_pairs, (min+res) - 1, &pair);
317         pair_test->visited_removed = 1;
318         if(pair_test->stack_removed && pair_test->visited_removed){
319           if((pair_test->automaton_state->type == 1) || (pair_test->automaton_state->type == 2)){
320             if(pair_test->acceptance_removed){
321               MC_pair_delete(pair_test);
322             }
323           }else{
324             MC_pair_delete(pair_test);
325           }
326         }
327         if(!raw_mem_set)
328           MC_UNSET_RAW_MEM;
329         return pair->other_num;
330         }*/
331       cursor = min;
332       while(cursor <= max){
333         pair_test = (mc_pair_t)xbt_dynar_get_as(visited_pairs, cursor, mc_pair_t);
334         if(xbt_automaton_state_compare(pair_test->automaton_state, pair->automaton_state) == 0){
335           if(xbt_automaton_propositional_symbols_compare_value(pair_test->atomic_propositions, pair->atomic_propositions) == 0){
336             if(snapshot_compare(pair_test, pair) == 0){
337               if(pair_test->other_num == -1)
338                 pair->other_num = pair_test->num;
339               else
340                 pair->other_num = pair_test->other_num;
341               if(dot_output == NULL)
342                 XBT_DEBUG("Pair %d already visited ! (equal to pair %d)", pair->num, pair_test->num);
343               else
344                 XBT_DEBUG("Pair %d already visited ! (equal to pair %d (pair %d in dot_output))", pair->num, pair_test->num, pair->other_num);
345               xbt_dynar_remove_at(visited_pairs, cursor, NULL);
346               xbt_dynar_insert_at(visited_pairs, cursor, &pair);
347               pair_test->visited_removed = 1;
348               if(pair_test->stack_removed && pair_test->visited_removed){
349                 if((pair_test->automaton_state->type == 1) || (pair_test->automaton_state->type == 2)){
350                   if(pair_test->acceptance_removed){
351                     MC_pair_delete(pair_test);
352                   }
353                 }else{
354                   MC_pair_delete(pair_test);
355                 }
356               }
357               if(!raw_mem_set)
358                 MC_UNSET_RAW_MEM;
359               return pair->other_num;
360             }
361           }
362         }
363         cursor++;
364       }
365       xbt_dynar_insert_at(visited_pairs, min, &pair);
366     }else{
367       pair_test = (mc_pair_t)xbt_dynar_get_as(visited_pairs, index, mc_pair_t);
368       if(pair_test->nb_processes < pair->nb_processes){
369         xbt_dynar_insert_at(visited_pairs, index+1, &pair);
370       }else{
371         if(pair_test->heap_bytes_used < pair->heap_bytes_used)
372           xbt_dynar_insert_at(visited_pairs, index + 1, &pair);
373         else
374           xbt_dynar_insert_at(visited_pairs, index, &pair);
375       }
376     }
377
378     if(xbt_dynar_length(visited_pairs) > _sg_mc_visited){
379       int min2 = mc_stats->expanded_pairs;
380       unsigned int cursor2 = 0;
381       unsigned int index2 = 0;
382       xbt_dynar_foreach(visited_pairs, cursor2, pair_test){
383         if(pair_test->num < min2){
384           index2 = cursor2;
385           min2 = pair_test->num;
386         }
387       }
388       xbt_dynar_remove_at(visited_pairs, index2, &pair_test);
389       pair_test->visited_removed = 1;
390       if(pair_test->stack_removed && pair_test->acceptance_removed && pair_test->visited_removed)
391         MC_pair_delete(pair_test);
392       
393     }
394
395     if(!raw_mem_set)
396       MC_UNSET_RAW_MEM;
397     
398     return -1;
399
400   }
401 }
402
403 static int MC_automaton_evaluate_label(xbt_automaton_exp_label_t l, xbt_dynar_t atomic_propositions_values){
404   
405   switch(l->type){
406   case 0 : {
407     int left_res = MC_automaton_evaluate_label(l->u.or_and.left_exp, atomic_propositions_values);
408     int right_res = MC_automaton_evaluate_label(l->u.or_and.right_exp, atomic_propositions_values);
409     return (left_res || right_res);
410   }
411   case 1 : {
412     int left_res = MC_automaton_evaluate_label(l->u.or_and.left_exp, atomic_propositions_values);
413     int right_res = MC_automaton_evaluate_label(l->u.or_and.right_exp, atomic_propositions_values);
414     return (left_res && right_res);
415   }
416   case 2 : {
417     int res = MC_automaton_evaluate_label(l->u.exp_not, atomic_propositions_values);
418     return (!res);
419   }
420   case 3 : { 
421     unsigned int cursor = 0;
422     xbt_automaton_propositional_symbol_t p = NULL;
423     xbt_dynar_foreach(_mc_property_automaton->propositional_symbols, cursor, p){
424       if(strcmp(p->pred, l->u.predicat) == 0)
425         return (int)xbt_dynar_get_as(atomic_propositions_values, cursor, int);
426     }
427     return -1;
428   }
429   case 4 : {
430     return 2;
431   }
432   default : 
433     return -1;
434   }
435 }
436
437
438 /********* DDFS Algorithm *********/
439
440
441 void MC_ddfs_init(void){
442
443   initial_state_liveness->raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
444
445   XBT_DEBUG("**************************************************");
446   XBT_DEBUG("Double-DFS init");
447   XBT_DEBUG("**************************************************");
448
449   mc_pair_t initial_pair = NULL;
450   smx_process_t process; 
451
452   MC_wait_for_requests();
453
454   MC_SET_RAW_MEM;
455
456   acceptance_pairs = xbt_dynar_new(sizeof(mc_pair_t), NULL); 
457   visited_pairs = xbt_dynar_new(sizeof(mc_pair_t), NULL); 
458   successors = xbt_dynar_new(sizeof(mc_pair_t), NULL);
459
460   initial_state_liveness->snapshot = MC_take_snapshot();
461   initial_state_liveness->prev_pair = 0;
462
463   MC_UNSET_RAW_MEM; 
464   
465   unsigned int cursor = 0;
466   xbt_automaton_state_t automaton_state;
467
468   xbt_dynar_foreach(_mc_property_automaton->states, cursor, automaton_state){
469     if(automaton_state->type == -1){ /* Initial automaton state */
470       
471       MC_SET_RAW_MEM;
472
473       initial_pair = MC_pair_new();
474       initial_pair->automaton_state = automaton_state;
475       initial_pair->graph_state = MC_state_new();
476       initial_pair->atomic_propositions = get_atomic_propositions_values();
477
478       /* Get enabled process and insert it in the interleave set of the graph_state */
479       xbt_swag_foreach(process, simix_global->process_list){
480         if(MC_process_is_enabled(process)){
481           MC_state_interleave_process(initial_pair->graph_state, process);
482         }
483       }
484
485       initial_pair->requests = MC_state_interleave_size(initial_pair->graph_state);
486       initial_pair->search_cycle = 0;
487
488       xbt_fifo_unshift(mc_stack_liveness, initial_pair);
489
490       MC_UNSET_RAW_MEM;
491       
492       MC_ddfs();
493       
494       if(cursor != 0){
495         MC_restore_snapshot(initial_state_liveness->snapshot);
496         MC_UNSET_RAW_MEM;
497       }
498       
499
500     }else if(automaton_state->type == 2){ /* Acceptance automaton state */
501       
502       MC_SET_RAW_MEM;
503
504       initial_pair = MC_pair_new();
505       initial_pair->automaton_state = automaton_state;
506       initial_pair->graph_state = MC_state_new();
507       initial_pair->atomic_propositions = get_atomic_propositions_values();
508         
509       /* Get enabled process and insert it in the interleave set of the graph_state */
510       xbt_swag_foreach(process, simix_global->process_list){
511         if(MC_process_is_enabled(process)){
512           MC_state_interleave_process(initial_pair->graph_state, process);
513         }
514       }
515
516       initial_pair->requests = MC_state_interleave_size(initial_pair->graph_state);
517       initial_pair->search_cycle = 1;
518         
519       xbt_fifo_unshift(mc_stack_liveness, initial_pair);
520         
521       MC_UNSET_RAW_MEM;
522
523       set_acceptance_pair_reached(initial_pair);
524   
525       MC_ddfs();
526
527       if(cursor != 0){
528         MC_restore_snapshot(initial_state_liveness->snapshot);
529         MC_UNSET_RAW_MEM;
530       }
531     }
532   }
533
534   if(initial_state_liveness->raw_mem_set)
535     MC_SET_RAW_MEM;
536   else
537     MC_UNSET_RAW_MEM;
538   
539
540 }
541
542
543 void MC_ddfs(){
544
545   smx_process_t process;
546   mc_pair_t current_pair = NULL;
547
548   if(xbt_fifo_size(mc_stack_liveness) == 0)
549     return;
550
551   /* Get current pair */
552   current_pair = (mc_pair_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack_liveness));
553
554   /* Update current state in buchi automaton */
555   _mc_property_automaton->current_state = current_pair->automaton_state;
556
557   XBT_DEBUG("********************* ( Depth = %d, search_cycle = %d, interleave size %d)", xbt_fifo_size(mc_stack_liveness), current_pair->search_cycle, MC_state_interleave_size(current_pair->graph_state));
558  
559   mc_stats->visited_pairs++;
560
561   int value;
562   smx_simcall_t req = NULL;
563   char *req_str;
564
565   xbt_automaton_transition_t transition_succ;
566   unsigned int cursor = 0;
567   int res;
568   int reached_num, visited_num;
569
570   mc_pair_t next_pair = NULL;
571   xbt_dynar_t prop_values = NULL;
572   
573   if(xbt_fifo_size(mc_stack_liveness) < _sg_mc_max_depth){
574
575     if(current_pair->requests > 0){
576
577       if(current_pair->search_cycle){
578
579         if((current_pair->automaton_state->type == 1) || (current_pair->automaton_state->type == 2)){ 
580           
581           if((reached_num = is_reached_acceptance_pair(current_pair)) != -1){
582         
583             XBT_INFO("Pair %d already reached (equal to pair %d) !", current_pair->num, reached_num);
584             
585             MC_SET_RAW_MEM;
586             xbt_fifo_shift(mc_stack_liveness);
587             if(dot_output != NULL)
588               fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_state_liveness->prev_pair, reached_num, initial_state_liveness->prev_req);
589             MC_UNSET_RAW_MEM;
590
591             XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
592             XBT_INFO("|             ACCEPTANCE CYCLE            |");
593             XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
594             XBT_INFO("Counter-example that violates formula :");
595             MC_show_stack_liveness(mc_stack_liveness);
596             MC_dump_stack_liveness(mc_stack_liveness);
597             MC_print_statistics(mc_stats);
598             xbt_abort();
599
600           }
601         }
602       }
603
604       if((visited_num = is_visited_pair(current_pair)) != -1){
605
606         MC_SET_RAW_MEM;
607         if(dot_output != NULL)
608           fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_state_liveness->prev_pair, visited_num, initial_state_liveness->prev_req);
609         MC_UNSET_RAW_MEM;
610         
611       
612       }else{  
613
614         while((req = MC_state_get_request(current_pair->graph_state, &value)) != NULL){
615
616           MC_SET_RAW_MEM;
617           if(dot_output != NULL){
618             if(initial_state_liveness->prev_pair != 0 && initial_state_liveness->prev_pair != current_pair->num){
619               fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_state_liveness->prev_pair, current_pair->num, initial_state_liveness->prev_req);
620               xbt_free(initial_state_liveness->prev_req);
621             }
622             initial_state_liveness->prev_pair = current_pair->num;
623           }
624           MC_UNSET_RAW_MEM;
625
626           /* Debug information */
627           if(XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)){
628             req_str = MC_request_to_string(req, value);
629             XBT_DEBUG("Execute: %s", req_str);
630             xbt_free(req_str);
631           }
632
633           MC_SET_RAW_MEM;
634           if(dot_output != NULL){
635             initial_state_liveness->prev_req = MC_request_get_dot_output(req, value);
636             if(current_pair->search_cycle)
637               fprintf(dot_output, "%d [shape=doublecircle];\n", current_pair->num);
638           }
639           MC_UNSET_RAW_MEM; 
640           
641           MC_state_set_executed_request(current_pair->graph_state, req, value);  
642           mc_stats->executed_transitions++;
643
644           /* Answer the request */
645           SIMIX_simcall_pre(req, value);
646           
647           /* Wait for requests (schedules processes) */
648           MC_wait_for_requests();
649
650           MC_SET_RAW_MEM;
651           prop_values = get_atomic_propositions_values();
652           MC_UNSET_RAW_MEM;
653          
654           /* Evaluate enabled transition according to atomic propositions values */
655           cursor= 0;
656           xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
657
658             res = MC_automaton_evaluate_label(transition_succ->label, prop_values);
659
660             if(res == 1){ // enabled transition in automaton
661
662               MC_SET_RAW_MEM;
663
664               next_pair = MC_pair_new();
665               next_pair->graph_state = MC_state_new();
666               next_pair->automaton_state = transition_succ->dst;
667               next_pair->atomic_propositions = get_atomic_propositions_values();
668
669               /* Get enabled process and insert it in the interleave set of the next graph_state */
670               xbt_swag_foreach(process, simix_global->process_list){
671                 if(MC_process_is_enabled(process)){
672                   MC_state_interleave_process(next_pair->graph_state, process);
673                 }
674               }
675
676               next_pair->requests = MC_state_interleave_size(next_pair->graph_state);
677               
678               if(next_pair->automaton_state->type == 1 || next_pair->automaton_state->type == 2 || current_pair->search_cycle)
679                 next_pair->search_cycle = 1;
680             
681               xbt_fifo_unshift(mc_stack_liveness, next_pair);
682
683               if(mc_stats->expanded_pairs%1000000 == 0)
684                 XBT_INFO("Expanded pairs : %lu", mc_stats->expanded_pairs);
685
686               MC_UNSET_RAW_MEM;
687
688               MC_ddfs();
689
690             }
691
692           }
693
694           /* Then, evaluate true transitions (always true, whatever atomic propositions values) */
695           cursor = 0;   
696           xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
697       
698             res = MC_automaton_evaluate_label(transition_succ->label, prop_values);
699   
700             if(res == 2){ // true transition in automaton
701             
702               MC_SET_RAW_MEM;
703             
704               next_pair = MC_pair_new();
705               next_pair->graph_state = MC_state_new();
706               next_pair->automaton_state = transition_succ->dst;
707               next_pair->atomic_propositions = get_atomic_propositions_values();
708
709               /* Get enabled process and insert it in the interleave set of the next graph_state */
710               xbt_swag_foreach(process, simix_global->process_list){
711                 if(MC_process_is_enabled(process)){
712                   MC_state_interleave_process(next_pair->graph_state, process);
713                 }
714               }
715
716               next_pair->requests = MC_state_interleave_size(next_pair->graph_state);
717             
718               if(next_pair->automaton_state->type == 1 || next_pair->automaton_state->type == 2 || current_pair->search_cycle)
719                 next_pair->search_cycle = 1;
720
721               xbt_fifo_unshift(mc_stack_liveness, next_pair);
722
723               if(mc_stats->expanded_pairs%1000000 == 0)
724                 XBT_INFO("Expanded pairs : %lu", mc_stats->expanded_pairs);
725             
726               MC_UNSET_RAW_MEM;
727
728               MC_ddfs();
729
730             }
731
732           }
733
734           if(MC_state_interleave_size(current_pair->graph_state) > 0){
735             XBT_DEBUG("Backtracking to depth %d", xbt_fifo_size(mc_stack_liveness));
736             MC_replay_liveness(mc_stack_liveness, 0);
737           }
738         
739         }
740
741       }
742  
743     }else{
744
745       mc_stats->executed_transitions++;
746       
747       XBT_DEBUG("No request to execute in this state, search evolution in Büchi Automaton.");
748
749       if(current_pair->search_cycle){
750
751         if((current_pair->automaton_state->type == 1) || (current_pair->automaton_state->type == 2)){ 
752           
753           if((reached_num = is_reached_acceptance_pair(current_pair)) != -1){
754         
755             XBT_INFO("Pair %d already reached (equal to pair %d) !", current_pair->num, reached_num);
756             
757             MC_SET_RAW_MEM;
758             xbt_fifo_shift(mc_stack_liveness);
759             MC_UNSET_RAW_MEM;
760
761             XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
762             XBT_INFO("|             ACCEPTANCE CYCLE            |");
763             XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
764             XBT_INFO("Counter-example that violates formula :");
765             MC_show_stack_liveness(mc_stack_liveness);
766             MC_dump_stack_liveness(mc_stack_liveness);
767             MC_print_statistics(mc_stats);
768             xbt_abort();
769
770           }
771         }
772       }
773
774       if((visited_num = is_visited_pair(current_pair)) != -1){
775
776         XBT_DEBUG("Pair %d already visited ! (equal to pair %d)", current_pair->num, visited_num);
777       
778       }else{            
779         
780         MC_SET_RAW_MEM;
781         prop_values = get_atomic_propositions_values();
782         MC_UNSET_RAW_MEM;
783
784         /* Evaluate enabled transition according to atomic propositions values */
785         cursor= 0;
786         xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
787
788           res = MC_automaton_evaluate_label(transition_succ->label, prop_values);
789
790           if(res == 1){ // enabled transition in automaton
791
792             MC_SET_RAW_MEM;
793
794             next_pair = MC_pair_new();
795             next_pair->graph_state = MC_state_new();
796             next_pair->automaton_state = transition_succ->dst;
797             next_pair->atomic_propositions = get_atomic_propositions_values();
798             next_pair->requests = MC_state_interleave_size(next_pair->graph_state);
799               
800             if(next_pair->automaton_state->type == 1 || next_pair->automaton_state->type == 2 || current_pair->search_cycle)
801               next_pair->search_cycle = 1;
802             
803             xbt_fifo_unshift(mc_stack_liveness, next_pair);
804
805             if(mc_stats->expanded_pairs%1000 == 0)
806               XBT_INFO("Expanded pairs : %lu", mc_stats->expanded_pairs);
807
808             if(dot_output != NULL)
809               fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", current_pair->num, next_pair->num, "");
810
811             MC_UNSET_RAW_MEM;
812
813             MC_ddfs();
814
815           }
816
817         }
818
819         /* Then, evaluate true transitions (always true, whatever atomic propositions values) */
820         cursor = 0;   
821         xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
822       
823           res = MC_automaton_evaluate_label(transition_succ->label, prop_values);
824   
825           if(res == 2){ // true transition in automaton
826
827             MC_SET_RAW_MEM;
828             
829             next_pair = MC_pair_new();
830             next_pair->graph_state = MC_state_new();
831             next_pair->automaton_state = transition_succ->dst;
832             next_pair->atomic_propositions = get_atomic_propositions_values();
833             next_pair->requests = MC_state_interleave_size(next_pair->graph_state);
834             
835             if(next_pair->automaton_state->type == 1 || next_pair->automaton_state->type == 2 || current_pair->search_cycle)
836               next_pair->search_cycle = 1;
837
838             xbt_fifo_unshift(mc_stack_liveness, next_pair);
839
840             if(mc_stats->expanded_pairs%1000 == 0)
841               XBT_INFO("Expanded pairs : %lu", mc_stats->expanded_pairs);
842             
843             if(dot_output != NULL)
844               fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", current_pair->num, next_pair->num, "");
845
846             MC_UNSET_RAW_MEM;
847
848             MC_ddfs();
849
850           }
851
852         }
853       }
854     }
855     
856   }else{
857     
858     XBT_WARN("/!\\ Max depth reached ! /!\\ ");
859     if(MC_state_interleave_size(current_pair->graph_state) > 0){
860       XBT_WARN("/!\\ But, there are still processes to interleave. Model-checker will not be able to ensure the soundness of the verification from now. /!\\ "); 
861       if(_sg_mc_max_depth == 1000)
862         XBT_WARN("Notice : the default value of max depth is 1000 but you can change it with cfg=model-check/max_depth:value.");
863     }
864     
865   }
866
867   if(xbt_fifo_size(mc_stack_liveness) == _sg_mc_max_depth ){
868     XBT_DEBUG("Pair %d (depth = %d) shifted in stack, maximum depth reached", current_pair->num, xbt_fifo_size(mc_stack_liveness) );
869   }else{
870     XBT_DEBUG("Pair %d (depth = %d) shifted in stack", current_pair->num, xbt_fifo_size(mc_stack_liveness) );
871   }
872
873   
874   MC_SET_RAW_MEM;
875   xbt_dynar_free(&prop_values);
876   current_pair = xbt_fifo_shift(mc_stack_liveness);
877   current_pair->stack_removed = 1;
878   if(current_pair->search_cycle){
879     remove_acceptance_pair(current_pair);
880   }else{
881     if(_sg_mc_visited == 0)
882       MC_pair_delete(current_pair);
883     else if(current_pair->visited_removed)
884       MC_pair_delete(current_pair);
885   }
886
887   MC_UNSET_RAW_MEM;
888
889 }