Logo AND Algorithmique Numérique Distribuée

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