Logo AND Algorithmique Numérique Distribuée

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