Logo AND Algorithmique Numérique Distribuée

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