Logo AND Algorithmique Numérique Distribuée

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