Logo AND Algorithmique Numérique Distribuée

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