Logo AND Algorithmique Numérique Distribuée

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