Logo AND Algorithmique Numérique Distribuée

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