Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
07fbd74847ffa09d8c5fafe4f65e0e7a66a74e4b
[simgrid.git] / src / mc / mc_liveness.c
1 /* Copyright (c) 2011-2014. 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 <unistd.h>
8 #include <sys/wait.h>
9
10 #include <xbt/dynar.h>
11 #include <xbt/automaton.h>
12
13 #include "mc_request.h"
14 #include "mc_liveness.h"
15 #include "mc_private.h"
16 #include "mc_record.h"
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_liveness, mc,
19                                 "Logging specific to algorithms for liveness properties verification");
20
21 /********* Global variables *********/
22
23 xbt_dynar_t acceptance_pairs;
24 xbt_parmap_t parmap;
25
26 /********* Static functions *********/
27
28 static xbt_dynar_t get_atomic_propositions_values()
29 {
30   unsigned int cursor = 0;
31   xbt_automaton_propositional_symbol_t ps = NULL;
32   xbt_dynar_t values = xbt_dynar_new(sizeof(int), NULL);
33   xbt_dynar_foreach(_mc_property_automaton->propositional_symbols, cursor, ps) {
34     int res = xbt_automaton_propositional_symbol_evaluate(ps);
35     xbt_dynar_push_as(values, int, res);
36   }
37
38   return values;
39 }
40
41 static mc_visited_pair_t is_reached_acceptance_pair(mc_pair_t pair) {
42   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
43
44   mc_visited_pair_t new_pair = NULL;
45   new_pair = MC_visited_pair_new(pair->num, pair->automaton_state, pair->atomic_propositions, pair->graph_state);
46   new_pair->acceptance_pair = 1;
47
48   if (xbt_dynar_is_empty(acceptance_pairs)) {
49
50     xbt_dynar_push(acceptance_pairs, &new_pair);
51
52   } else {
53
54     int min = -1, max = -1, index;
55     //int res;
56     mc_visited_pair_t pair_test;
57     int cursor;
58
59     index = get_search_interval(acceptance_pairs, new_pair, &min, &max);
60
61     if (min != -1 && max != -1) {       // Acceptance pair with same number of processes and same heap bytes used exists
62
63       // Parallell implementation
64       /*res = xbt_parmap_mc_apply(parmap, snapshot_compare, xbt_dynar_get_ptr(acceptance_pairs, min), (max-min)+1, pair);
65          if(res != -1){
66          if(!raw_mem_set)
67          MC_SET_STD_HEAP;
68          return ((mc_pair_t)xbt_dynar_get_as(acceptance_pairs, (min+res)-1, mc_pair_t))->num;
69          } */
70
71       cursor = min;
72       if(pair->search_cycle == 1){
73         while (cursor <= max) {
74           pair_test = (mc_visited_pair_t) xbt_dynar_get_as(acceptance_pairs, cursor, mc_visited_pair_t);
75           if (xbt_automaton_state_compare(pair_test->automaton_state, new_pair->automaton_state) == 0) {
76             if (xbt_automaton_propositional_symbols_compare_value(pair_test->atomic_propositions, new_pair->atomic_propositions) == 0) {
77               if (snapshot_compare(pair_test, new_pair) == 0) {
78                 XBT_INFO("Pair %d already reached (equal to pair %d) !", new_pair->num, pair_test->num);
79                 xbt_fifo_shift(mc_stack);
80                 if (dot_output != NULL)
81                   fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, pair_test->num, initial_global_state->prev_req);
82                 mmalloc_set_current_heap(heap);
83                 return NULL;
84               }
85             }
86           }
87           cursor++;
88         }
89       }
90       xbt_dynar_insert_at(acceptance_pairs, min, &new_pair);
91     } else {
92       pair_test = (mc_visited_pair_t) xbt_dynar_get_as(acceptance_pairs, index, mc_visited_pair_t);
93       if (pair_test->nb_processes < new_pair->nb_processes) {
94         xbt_dynar_insert_at(acceptance_pairs, index + 1, &new_pair);
95       } else {
96         if (pair_test->heap_bytes_used < new_pair->heap_bytes_used)
97           xbt_dynar_insert_at(acceptance_pairs, index + 1, &new_pair);
98         else
99           xbt_dynar_insert_at(acceptance_pairs, index, &new_pair);
100       }
101     }
102
103   }
104   mmalloc_set_current_heap(heap);
105   return new_pair;
106 }
107
108 static void remove_acceptance_pair(int pair_num)
109 {
110   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
111
112   unsigned int cursor = 0;
113   mc_visited_pair_t pair_test = NULL;
114   int pair_found = 0;
115
116   xbt_dynar_foreach(acceptance_pairs, cursor, pair_test) {
117     if (pair_test->num == pair_num) {
118        pair_found = 1;
119       break;
120     }
121   }
122
123   if(pair_found == 1) {
124     xbt_dynar_remove_at(acceptance_pairs, cursor, &pair_test);
125
126     pair_test->acceptance_removed = 1;
127
128     if (_sg_mc_visited == 0 || pair_test->visited_removed == 1) 
129       MC_visited_pair_delete(pair_test);
130
131   }
132
133   mmalloc_set_current_heap(heap);
134 }
135
136
137 static int MC_automaton_evaluate_label(xbt_automaton_exp_label_t l,
138                                        xbt_dynar_t atomic_propositions_values)
139 {
140
141   switch (l->type) {
142   case 0:{
143       int left_res = MC_automaton_evaluate_label(l->u.or_and.left_exp, atomic_propositions_values);
144       int right_res = MC_automaton_evaluate_label(l->u.or_and.right_exp, atomic_propositions_values);
145       return (left_res || right_res);
146     }
147   case 1:{
148       int left_res = MC_automaton_evaluate_label(l->u.or_and.left_exp, atomic_propositions_values);
149       int right_res = MC_automaton_evaluate_label(l->u.or_and.right_exp, atomic_propositions_values);
150       return (left_res && right_res);
151     }
152   case 2:{
153       int res = MC_automaton_evaluate_label(l->u.exp_not, atomic_propositions_values);
154       return (!res);
155     }
156   case 3:{
157       unsigned int cursor = 0;
158       xbt_automaton_propositional_symbol_t p = NULL;
159       xbt_dynar_foreach(_mc_property_automaton->propositional_symbols, cursor, p) {
160         if (strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
161           return (int) xbt_dynar_get_as(atomic_propositions_values, cursor, int);
162       }
163       return -1;
164     }
165   case 4:{
166       return 2;
167     }
168   default:
169     return -1;
170   }
171 }
172
173 void MC_pre_modelcheck_liveness(void) {
174
175   initial_global_state->raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
176
177   mc_pair_t initial_pair = NULL;
178   smx_process_t process;
179
180   MC_wait_for_requests();
181
182   MC_SET_MC_HEAP;
183
184   acceptance_pairs = xbt_dynar_new(sizeof(mc_visited_pair_t), NULL);
185   if(_sg_mc_visited > 0)
186     visited_pairs = xbt_dynar_new(sizeof(mc_visited_pair_t), NULL);
187
188   initial_global_state->snapshot = MC_take_snapshot(0);
189   initial_global_state->prev_pair = 0;
190
191   unsigned int cursor = 0;
192   xbt_automaton_state_t automaton_state;
193   
194   xbt_dynar_foreach(_mc_property_automaton->states, cursor, automaton_state) {
195     if (automaton_state->type == -1) {  /* Initial automaton state */
196
197       initial_pair = MC_pair_new();
198       initial_pair->automaton_state = automaton_state;
199       initial_pair->graph_state = MC_state_new();
200       initial_pair->atomic_propositions = get_atomic_propositions_values();
201       initial_pair->depth = 1;
202
203       /* Get enabled processes and insert them in the interleave set of the graph_state */
204       MC_EACH_SIMIX_PROCESS(process,
205         if (MC_process_is_enabled(process)) {
206           MC_state_interleave_process(initial_pair->graph_state, process);
207         }
208       );
209
210       initial_pair->requests = MC_state_interleave_size(initial_pair->graph_state);
211       initial_pair->search_cycle = 0;
212
213       xbt_fifo_unshift(mc_stack, initial_pair);
214     }
215   }
216
217   MC_SET_STD_HEAP;
218   
219   MC_modelcheck_liveness();
220
221   if (initial_global_state->raw_mem_set)
222     MC_SET_MC_HEAP;
223 }
224
225 void MC_modelcheck_liveness()
226 {
227   smx_process_t process = NULL;
228   mc_pair_t current_pair = NULL;
229   int value, res, visited_num = -1;
230   smx_simcall_t req = NULL;
231   xbt_automaton_transition_t transition_succ = NULL;
232   int cursor = 0;
233   mc_pair_t next_pair = NULL;
234   xbt_dynar_t prop_values = NULL;
235   mc_visited_pair_t reached_pair = NULL;
236   
237   while(xbt_fifo_size(mc_stack) > 0){
238
239     /* Get current pair */
240     current_pair = (mc_pair_t) xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack));
241
242     /* Update current state in buchi automaton */
243     _mc_property_automaton->current_state = current_pair->automaton_state;
244
245     XBT_DEBUG("********************* ( Depth = %d, search_cycle = %d, interleave size %d, pair_num %d)",
246        current_pair->depth, current_pair->search_cycle,
247        MC_state_interleave_size(current_pair->graph_state), current_pair->num);
248
249     if (current_pair->requests > 0) {
250
251       if (current_pair->automaton_state->type == 1 && current_pair->exploration_started == 0) {
252         /* If new acceptance pair, return new pair */
253         if ((reached_pair = is_reached_acceptance_pair(current_pair)) == NULL) {
254           int counter_example_depth = current_pair->depth;
255           XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
256           XBT_INFO("|             ACCEPTANCE CYCLE            |");
257           XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
258           XBT_INFO("Counter-example that violates formula :");
259           MC_show_stack_liveness(mc_stack);
260           MC_dump_stack_liveness(mc_stack);
261           MC_print_statistics(mc_stats);
262           XBT_INFO("Counter-example depth : %d", counter_example_depth);
263           xbt_abort();
264         }
265       }
266
267       /* Pair already visited ? stop the exploration on the current path */
268       if ((current_pair->exploration_started == 0) && (visited_num = is_visited_pair(reached_pair, current_pair)) != -1) {
269
270         if (dot_output != NULL){
271           MC_SET_MC_HEAP;
272           fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, visited_num, initial_global_state->prev_req);
273           fflush(dot_output);
274           MC_SET_STD_HEAP;
275         }
276
277         XBT_DEBUG("Pair already visited (equal to pair %d), exploration on the current path stopped.", visited_num);
278         MC_SET_MC_HEAP;
279         current_pair->requests = 0;
280         MC_SET_STD_HEAP;
281         goto backtracking;
282         
283       }else{
284
285         req = MC_state_get_request(current_pair->graph_state, &value);
286
287          if (dot_output != NULL) {
288            MC_SET_MC_HEAP;
289            if (initial_global_state->prev_pair != 0 && initial_global_state->prev_pair != current_pair->num) {
290              fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, current_pair->num, initial_global_state->prev_req);
291              xbt_free(initial_global_state->prev_req);
292            }
293            initial_global_state->prev_pair = current_pair->num;
294            initial_global_state->prev_req = MC_request_get_dot_output(req, value);
295            if (current_pair->search_cycle)
296              fprintf(dot_output, "%d [shape=doublecircle];\n", current_pair->num);
297            fflush(dot_output);
298            MC_SET_STD_HEAP;
299          }
300
301          char* req_str = MC_request_to_string(req, value);  
302          XBT_DEBUG("Execute: %s", req_str);                 
303          xbt_free(req_str);
304
305          /* Set request as executed */
306          MC_state_set_executed_request(current_pair->graph_state, req, value);
307
308          /* Update mc_stats */
309          mc_stats->executed_transitions++;
310          if(current_pair->exploration_started == 0)
311            mc_stats->visited_pairs++;
312
313          /* Answer the request */
314          MC_simcall_handle(req, value);
315          
316          /* Wait for requests (schedules processes) */
317          MC_wait_for_requests();
318
319          MC_SET_MC_HEAP;
320
321          current_pair->requests--;
322          current_pair->exploration_started = 1;
323
324          /* Get values of atomic propositions (variables used in the property formula) */ 
325          prop_values = get_atomic_propositions_values();
326
327          /* Evaluate enabled/true transitions in automaton according to atomic propositions values and create new pairs */
328          cursor = xbt_dynar_length(current_pair->automaton_state->out) - 1;
329          while (cursor >= 0) {
330            transition_succ = (xbt_automaton_transition_t)xbt_dynar_get_as(current_pair->automaton_state->out, cursor, xbt_automaton_transition_t);
331            res = MC_automaton_evaluate_label(transition_succ->label, prop_values);
332            if (res == 1 || res == 2) { /* 1 = True transition (always enabled), 2 = enabled transition according to atomic prop values */
333               next_pair = MC_pair_new();
334               next_pair->graph_state = MC_state_new();
335               next_pair->automaton_state = transition_succ->dst;
336               next_pair->atomic_propositions = get_atomic_propositions_values();
337               next_pair->depth = current_pair->depth + 1;
338               /* Get enabled processes and insert them in the interleave set of the next graph_state */
339               MC_EACH_SIMIX_PROCESS(process,
340                 if (MC_process_is_enabled(process)) {
341                   MC_state_interleave_process(next_pair->graph_state, process);
342                 }
343               );
344
345               next_pair->requests = MC_state_interleave_size(next_pair->graph_state);
346
347               /* FIXME : get search_cycle value for each acceptant state */
348               if (next_pair->automaton_state->type == 1 || current_pair->search_cycle)
349                 next_pair->search_cycle = 1;
350
351               /* Add new pair to the exploration stack */
352               xbt_fifo_unshift(mc_stack, next_pair);
353
354            }
355            cursor--;
356          }
357          
358          MC_SET_STD_HEAP;
359         
360       } /* End of visited_pair test */
361       
362     } else {
363
364     backtracking:
365       if(visited_num == -1)
366         XBT_DEBUG("No more request to execute. Looking for backtracking point.");
367     
368       MC_SET_MC_HEAP;
369     
370       xbt_dynar_free(&prop_values);
371     
372       /* Traverse the stack backwards until a pair with a non empty interleave
373          set is found, deleting all the pairs that have it empty in the way. */
374       while ((current_pair = xbt_fifo_shift(mc_stack)) != NULL) {
375         if (current_pair->requests > 0) {
376           /* We found a backtracking point */
377           XBT_DEBUG("Backtracking to depth %d", current_pair->depth);
378           xbt_fifo_unshift(mc_stack, current_pair);
379           MC_SET_STD_HEAP;
380           MC_replay_liveness(mc_stack);
381           XBT_DEBUG("Backtracking done");
382           break;
383         }else{
384           /* Delete pair */
385           XBT_DEBUG("Delete pair %d at depth %d", current_pair->num, current_pair->depth);
386           if (current_pair->automaton_state->type == 1) 
387             remove_acceptance_pair(current_pair->num);
388           MC_pair_delete(current_pair);
389         }
390       }
391     
392       MC_SET_STD_HEAP;
393     } /* End of if (current_pair->requests > 0) else ... */
394     
395   } /* End of while(xbt_fifo_size(mc_stack) > 0) */
396   
397 }