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 / LivenessChecker.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 <algorithm>
10 #include <memory>
11
12 #include <unistd.h>
13 #include <sys/wait.h>
14
15 #include <xbt/automaton.h>
16 #include <xbt/dynar.h>
17 #include <xbt/dynar.hpp>
18 #include <xbt/fifo.h>
19 #include <xbt/log.h>
20 #include <xbt/sysdep.h>
21
22 #include "src/mc/mc_request.h"
23 #include "src/mc/LivenessChecker.hpp"
24 #include "src/mc/mc_private.h"
25 #include "src/mc/mc_record.h"
26 #include "src/mc/mc_smx.h"
27 #include "src/mc/Client.hpp"
28 #include "src/mc/mc_private.h"
29 #include "src/mc/mc_replay.h"
30 #include "src/mc/mc_safety.h"
31 #include "src/mc/mc_exit.h"
32
33 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_liveness, mc,
34                                 "Logging specific to algorithms for liveness properties verification");
35
36 /********* Global variables *********/
37
38 xbt_dynar_t acceptance_pairs;
39
40 /********* Static functions *********/
41
42 namespace simgrid {
43 namespace mc {
44
45 static int MC_automaton_evaluate_label(xbt_automaton_exp_label_t l,
46                                        xbt_dynar_t atomic_propositions_values)
47 {
48
49   switch (l->type) {
50   case 0:{
51       int left_res = MC_automaton_evaluate_label(l->u.or_and.left_exp, atomic_propositions_values);
52       int right_res = MC_automaton_evaluate_label(l->u.or_and.right_exp, atomic_propositions_values);
53       return (left_res || right_res);
54     }
55   case 1:{
56       int left_res = MC_automaton_evaluate_label(l->u.or_and.left_exp, atomic_propositions_values);
57       int right_res = MC_automaton_evaluate_label(l->u.or_and.right_exp, atomic_propositions_values);
58       return (left_res && right_res);
59     }
60   case 2:{
61       int res = MC_automaton_evaluate_label(l->u.exp_not, atomic_propositions_values);
62       return (!res);
63     }
64   case 3:{
65       unsigned int cursor = 0;
66       xbt_automaton_propositional_symbol_t p = nullptr;
67       xbt_dynar_foreach(simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
68         if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
69           return (int) xbt_dynar_get_as(atomic_propositions_values, cursor, int);
70       }
71       return -1;
72     }
73   case 4:
74     return 2;
75   default:
76     return -1;
77   }
78 }
79
80 static xbt_dynar_t visited_pairs;
81
82 Pair::Pair() : num(++mc_stats->expanded_pairs),
83   visited_pair_removed(_sg_mc_visited > 0 ? 0 : 1)
84 {}
85
86 Pair::~Pair() {
87   if (this->visited_pair_removed)
88     MC_state_delete(this->graph_state, 1);
89 }
90
91 void LivenessChecker::showStack(xbt_fifo_t stack)
92 {
93   int value;
94   simgrid::mc::Pair* pair;
95   xbt_fifo_item_t item;
96   smx_simcall_t req;
97   char *req_str = nullptr;
98
99   for (item = xbt_fifo_get_last_item(stack);
100        item; item = xbt_fifo_get_prev_item(item)) {
101     pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(item);
102     req = MC_state_get_executed_request(pair->graph_state, &value);
103     if (req && req->call != SIMCALL_NONE) {
104       req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::executed);
105       XBT_INFO("%s", req_str);
106       xbt_free(req_str);
107     }
108   }
109 }
110
111 void LivenessChecker::dumpStack(xbt_fifo_t stack)
112 {
113   simgrid::mc::Pair* pair;
114   while ((pair = (simgrid::mc::Pair*) xbt_fifo_pop(stack)) != nullptr)
115     delete pair;
116 }
117
118 simgrid::xbt::unique_ptr<s_xbt_dynar_t> LivenessChecker::getPropositionValues()
119 {
120   unsigned int cursor = 0;
121   xbt_automaton_propositional_symbol_t ps = nullptr;
122   simgrid::xbt::unique_ptr<s_xbt_dynar_t> values = simgrid::xbt::unique_ptr<s_xbt_dynar_t>(xbt_dynar_new(sizeof(int), nullptr));
123   xbt_dynar_foreach(simgrid::mc::property_automaton->propositional_symbols, cursor, ps) {
124     int res = xbt_automaton_propositional_symbol_evaluate(ps);
125     xbt_dynar_push_as(values.get(), int, res);
126   }
127   return std::move(values);
128 }
129
130 int LivenessChecker::compare(simgrid::mc::VisitedPair* state1, simgrid::mc::VisitedPair* state2)
131 {
132   simgrid::mc::Snapshot* s1 = state1->graph_state->system_state;
133   simgrid::mc::Snapshot* s2 = state2->graph_state->system_state;
134   int num1 = state1->num;
135   int num2 = state2->num;
136   return simgrid::mc::snapshot_compare(num1, s1, num2, s2);
137 }
138
139 simgrid::mc::VisitedPair* LivenessChecker::insertAcceptancePair(simgrid::mc::Pair* pair)
140 {
141   auto acceptance_pairs = simgrid::xbt::range<simgrid::mc::VisitedPair*>(::acceptance_pairs);
142   auto new_pair =
143     std::unique_ptr<simgrid::mc::VisitedPair>(new VisitedPair(
144       pair->num, pair->automaton_state, pair->atomic_propositions.get(),
145       pair->graph_state));
146   new_pair->acceptance_pair = 1;
147
148   auto res = std::equal_range(acceptance_pairs.begin(), acceptance_pairs.end(),
149     new_pair, simgrid::mc::DerefAndCompareByNbProcessesAndUsedHeap());
150
151   if (pair->search_cycle == 1)
152     for (auto i = res.first; i != res.second; ++i) {
153       simgrid::mc::VisitedPair* pair_test = *i;
154       if (xbt_automaton_state_compare(pair_test->automaton_state, new_pair->automaton_state) == 0) {
155         if (xbt_automaton_propositional_symbols_compare_value(
156             pair_test->atomic_propositions.get(),
157             new_pair->atomic_propositions.get()) == 0) {
158           if (this->compare(pair_test, new_pair.get()) == 0) {
159             XBT_INFO("Pair %d already reached (equal to pair %d) !", new_pair->num, pair_test->num);
160             xbt_fifo_shift(mc_stack);
161             if (dot_output != nullptr)
162               fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, pair_test->num, initial_global_state->prev_req);
163             return nullptr;
164           }
165         }
166       }
167     }
168
169   auto new_raw_pair = new_pair.release();
170   xbt_dynar_insert_at(
171     ::acceptance_pairs, res.first - acceptance_pairs.begin(), &new_raw_pair);
172   return new_raw_pair;
173 }
174
175 void LivenessChecker::removeAcceptancePair(int pair_num)
176 {
177   unsigned int cursor = 0;
178   simgrid::mc::VisitedPair* pair_test = nullptr;
179   int pair_found = 0;
180
181   xbt_dynar_foreach(acceptance_pairs, cursor, pair_test)
182     if (pair_test->num == pair_num) {
183        pair_found = 1;
184       break;
185     }
186
187   if(pair_found == 1) {
188     xbt_dynar_remove_at(acceptance_pairs, cursor, &pair_test);
189
190     pair_test->acceptance_removed = 1;
191
192     if (_sg_mc_visited == 0 || pair_test->visited_removed == 1) 
193       delete pair_test;
194
195   }
196 }
197
198 void LivenessChecker::prepare(void)
199 {
200   simgrid::mc::Pair* initial_pair = nullptr;
201   mc_model_checker->wait_for_requests();
202   acceptance_pairs = xbt_dynar_new(sizeof(simgrid::mc::VisitedPair*), nullptr);
203   if(_sg_mc_visited > 0)
204     simgrid::mc::visited_pairs = xbt_dynar_new(sizeof(simgrid::mc::VisitedPair*), nullptr);
205
206   initial_global_state->snapshot = simgrid::mc::take_snapshot(0);
207   initial_global_state->prev_pair = 0;
208
209   unsigned int cursor = 0;
210   xbt_automaton_state_t automaton_state;
211
212   xbt_dynar_foreach(simgrid::mc::property_automaton->states, cursor, automaton_state) {
213     if (automaton_state->type == -1) {  /* Initial automaton state */
214
215       initial_pair = new Pair();
216       initial_pair->automaton_state = automaton_state;
217       initial_pair->graph_state = MC_state_new();
218       initial_pair->atomic_propositions = this->getPropositionValues();
219       initial_pair->depth = 1;
220
221       /* Get enabled processes and insert them in the interleave set of the graph_state */
222       for (auto& p : mc_model_checker->process().simix_processes())
223         if (simgrid::mc::process_is_enabled(&p.copy))
224           MC_state_interleave_process(initial_pair->graph_state, &p.copy);
225
226       initial_pair->requests = MC_state_interleave_size(initial_pair->graph_state);
227       initial_pair->search_cycle = 0;
228
229       xbt_fifo_unshift(mc_stack, initial_pair);
230     }
231   }
232 }
233
234
235 void LivenessChecker::replay(xbt_fifo_t stack)
236 {
237   xbt_fifo_item_t item;
238   simgrid::mc::Pair* pair = nullptr;
239   mc_state_t state = nullptr;
240   smx_simcall_t req = nullptr, saved_req = NULL;
241   int value, depth = 1;
242   char *req_str;
243
244   XBT_DEBUG("**** Begin Replay ****");
245
246   /* Intermediate backtracking */
247   if(_sg_mc_checkpoint > 0) {
248     item = xbt_fifo_get_first_item(stack);
249     pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(item);
250     if(pair->graph_state->system_state){
251       simgrid::mc::restore_snapshot(pair->graph_state->system_state);
252       return;
253     }
254   }
255
256   /* Restore the initial state */
257   simgrid::mc::restore_snapshot(initial_global_state->snapshot);
258
259     /* Traverse the stack from the initial state and re-execute the transitions */
260     for (item = xbt_fifo_get_last_item(stack);
261          item != xbt_fifo_get_first_item(stack);
262          item = xbt_fifo_get_prev_item(item)) {
263
264       pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(item);
265
266       state = (mc_state_t) pair->graph_state;
267
268       if (pair->exploration_started) {
269
270         saved_req = MC_state_get_executed_request(state, &value);
271
272         if (saved_req != nullptr) {
273           /* because we got a copy of the executed request, we have to fetch the
274              real one, pointed by the request field of the issuer process */
275           const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
276           req = &issuer->simcall;
277
278           /* Debug information */
279           if (XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)) {
280             req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
281             XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
282             xbt_free(req_str);
283           }
284
285         }
286
287         simgrid::mc::handle_simcall(req, value);
288         mc_model_checker->wait_for_requests();
289       }
290
291       /* Update statistics */
292       mc_stats->visited_pairs++;
293       mc_stats->executed_transitions++;
294
295       depth++;
296
297     }
298
299   XBT_DEBUG("**** End Replay ****");
300 }
301
302 /**
303  * \brief Checks whether a given pair has already been visited by the algorithm.
304  */
305 int LivenessChecker::insertVisitedPair(simgrid::mc::VisitedPair* visited_pair, simgrid::mc::Pair* pair)
306 {
307   if (_sg_mc_visited == 0)
308     return -1;
309
310   simgrid::mc::VisitedPair* new_visited_pair = nullptr;
311   if (visited_pair == nullptr)
312     new_visited_pair = new simgrid::mc::VisitedPair(
313       pair->num, pair->automaton_state, pair->atomic_propositions.get(),
314       pair->graph_state);
315   else
316     new_visited_pair = visited_pair;
317
318   auto visited_pairs = simgrid::xbt::range<simgrid::mc::VisitedPair*>(simgrid::mc::visited_pairs);
319
320   auto range = std::equal_range(visited_pairs.begin(), visited_pairs.end(),
321     new_visited_pair, simgrid::mc::DerefAndCompareByNbProcessesAndUsedHeap());
322
323   for (auto i = range.first; i != range.second; ++i) {
324     simgrid::mc::VisitedPair* pair_test = *i;
325     std::size_t cursor = i - visited_pairs.begin();
326     if (xbt_automaton_state_compare(pair_test->automaton_state, new_visited_pair->automaton_state) == 0) {
327       if (xbt_automaton_propositional_symbols_compare_value(
328           pair_test->atomic_propositions.get(),
329           new_visited_pair->atomic_propositions.get()) == 0) {
330         if (this->compare(pair_test, new_visited_pair) == 0) {
331           if (pair_test->other_num == -1)
332             new_visited_pair->other_num = pair_test->num;
333           else
334             new_visited_pair->other_num = pair_test->other_num;
335           if (dot_output == nullptr)
336             XBT_DEBUG("Pair %d already visited ! (equal to pair %d)", new_visited_pair->num, pair_test->num);
337           else
338             XBT_DEBUG("Pair %d already visited ! (equal to pair %d (pair %d in dot_output))", new_visited_pair->num, pair_test->num, new_visited_pair->other_num);
339           xbt_dynar_remove_at(simgrid::mc::visited_pairs, cursor, &pair_test);
340           xbt_dynar_insert_at(simgrid::mc::visited_pairs, cursor, &new_visited_pair);
341           pair_test->visited_removed = 1;
342           if (!pair_test->acceptance_pair
343               || pair_test->acceptance_removed == 1)
344             delete pair_test;
345           return new_visited_pair->other_num;
346         }
347       }
348     }
349   }
350
351   xbt_dynar_insert_at(simgrid::mc::visited_pairs, range.first - visited_pairs.begin(), &new_visited_pair);
352
353   if ((ssize_t) visited_pairs.size() > _sg_mc_visited) {
354     int min2 = mc_stats->expanded_pairs;
355     unsigned int index2 = 0;
356     for (std::size_t i = 0; i != (std::size_t) visited_pairs.size(); ++i) {
357       simgrid::mc::VisitedPair* pair_test = visited_pairs[i];
358       if (!mc_model_checker->is_important_snapshot(*pair_test->graph_state->system_state)
359           && pair_test->num < min2) {
360         index2 = i;
361         min2 = pair_test->num;
362       }
363     }
364     simgrid::mc::VisitedPair* pair_test = nullptr;
365     xbt_dynar_remove_at(simgrid::mc::visited_pairs, index2, &pair_test);
366     pair_test->visited_removed = 1;
367     if (!pair_test->acceptance_pair || pair_test->acceptance_removed)
368       delete pair_test;
369   }
370
371   return -1;
372 }
373
374 LivenessChecker::LivenessChecker(Session& session) : Checker(session)
375 {
376 }
377
378 LivenessChecker::~LivenessChecker()
379 {
380 }
381
382 void LivenessChecker::showAcceptanceCycle(std::size_t depth)
383 {
384   XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
385   XBT_INFO("|             ACCEPTANCE CYCLE            |");
386   XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
387   XBT_INFO("Counter-example that violates formula :");
388   MC_record_dump_path(mc_stack);
389   this->showStack(mc_stack);
390   this->dumpStack(mc_stack);
391   MC_print_statistics(mc_stats);
392   XBT_INFO("Counter-example depth : %zd", depth);
393 }
394
395 int LivenessChecker::main(void)
396 {
397   simgrid::mc::Pair* current_pair = nullptr;
398   int value, res, visited_num = -1;
399   smx_simcall_t req = nullptr;
400   xbt_automaton_transition_t transition_succ = nullptr;
401   int cursor = 0;
402   simgrid::mc::Pair* next_pair = nullptr;
403   simgrid::xbt::unique_ptr<s_xbt_dynar_t> prop_values;
404   simgrid::mc::VisitedPair* reached_pair = nullptr;
405
406   while(xbt_fifo_size(mc_stack) > 0){
407
408     /* Get current pair */
409     current_pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack));
410
411     /* Update current state in buchi automaton */
412     simgrid::mc::property_automaton->current_state = current_pair->automaton_state;
413
414     XBT_DEBUG("********************* ( Depth = %d, search_cycle = %d, interleave size = %d, pair_num = %d, requests = %d)",
415        current_pair->depth, current_pair->search_cycle,
416        MC_state_interleave_size(current_pair->graph_state), current_pair->num,
417        current_pair->requests);
418
419     if (current_pair->requests > 0) {
420
421       if (current_pair->automaton_state->type == 1 && current_pair->exploration_started == 0) {
422         /* If new acceptance pair, return new pair */
423         if ((reached_pair = this->insertAcceptancePair(current_pair)) == nullptr) {
424           this->showAcceptanceCycle(current_pair->depth);
425           return SIMGRID_MC_EXIT_LIVENESS;
426         }
427       }
428
429       /* Pair already visited ? stop the exploration on the current path */
430       if ((current_pair->exploration_started == 0)
431         && (visited_num = this->insertVisitedPair(
432           reached_pair, current_pair)) != -1) {
433
434         if (dot_output != nullptr){
435           fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, visited_num, initial_global_state->prev_req);
436           fflush(dot_output);
437         }
438
439         XBT_DEBUG("Pair already visited (equal to pair %d), exploration on the current path stopped.", visited_num);
440         current_pair->requests = 0;
441         goto backtracking;
442
443       }else{
444
445         req = MC_state_get_request(current_pair->graph_state, &value);
446
447          if (dot_output != nullptr) {
448            if (initial_global_state->prev_pair != 0 && initial_global_state->prev_pair != current_pair->num) {
449              fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, current_pair->num, initial_global_state->prev_req);
450              xbt_free(initial_global_state->prev_req);
451            }
452            initial_global_state->prev_pair = current_pair->num;
453            initial_global_state->prev_req = simgrid::mc::request_get_dot_output(req, value);
454            if (current_pair->search_cycle)
455              fprintf(dot_output, "%d [shape=doublecircle];\n", current_pair->num);
456            fflush(dot_output);
457          }
458
459          char* req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
460          XBT_DEBUG("Execute: %s", req_str);
461          xbt_free(req_str);
462
463          /* Set request as executed */
464          MC_state_set_executed_request(current_pair->graph_state, req, value);
465
466          /* Update mc_stats */
467          mc_stats->executed_transitions++;
468          if(current_pair->exploration_started == 0)
469            mc_stats->visited_pairs++;
470
471          /* Answer the request */
472          simgrid::mc::handle_simcall(req, value);
473
474          /* Wait for requests (schedules processes) */
475          mc_model_checker->wait_for_requests();
476
477          current_pair->requests--;
478          current_pair->exploration_started = 1;
479
480          /* Get values of atomic propositions (variables used in the property formula) */
481          prop_values = this->getPropositionValues();
482
483          /* Evaluate enabled/true transitions in automaton according to atomic propositions values and create new pairs */
484          cursor = xbt_dynar_length(current_pair->automaton_state->out) - 1;
485          while (cursor >= 0) {
486            transition_succ = (xbt_automaton_transition_t)xbt_dynar_get_as(current_pair->automaton_state->out, cursor, xbt_automaton_transition_t);
487            res = MC_automaton_evaluate_label(transition_succ->label, prop_values.get());
488            if (res == 1 || res == 2) { /* 1 = True transition (always enabled), 2 = enabled transition according to atomic prop values */
489               next_pair = new Pair();
490               next_pair->graph_state = MC_state_new();
491               next_pair->automaton_state = transition_succ->dst;
492               next_pair->atomic_propositions = this->getPropositionValues();
493               next_pair->depth = current_pair->depth + 1;
494               /* Get enabled processes and insert them in the interleave set of the next graph_state */
495               for (auto& p : mc_model_checker->process().simix_processes())
496                 if (simgrid::mc::process_is_enabled(&p.copy))
497                   MC_state_interleave_process(next_pair->graph_state, &p.copy);
498
499               next_pair->requests = MC_state_interleave_size(next_pair->graph_state);
500
501               /* FIXME : get search_cycle value for each acceptant state */
502               if (next_pair->automaton_state->type == 1 || current_pair->search_cycle)
503                 next_pair->search_cycle = 1;
504
505               /* Add new pair to the exploration stack */
506               xbt_fifo_unshift(mc_stack, next_pair);
507
508            }
509            cursor--;
510          }
511
512       } /* End of visited_pair test */
513
514     } else {
515
516     backtracking:
517       if(visited_num == -1)
518         XBT_DEBUG("No more request to execute. Looking for backtracking point.");
519
520       prop_values.reset();
521
522       /* Traverse the stack backwards until a pair with a non empty interleave
523          set is found, deleting all the pairs that have it empty in the way. */
524       while ((current_pair = (simgrid::mc::Pair*) xbt_fifo_shift(mc_stack)) != nullptr) {
525         if (current_pair->requests > 0) {
526           /* We found a backtracking point */
527           XBT_DEBUG("Backtracking to depth %d", current_pair->depth);
528           xbt_fifo_unshift(mc_stack, current_pair);
529           this->replay(mc_stack);
530           XBT_DEBUG("Backtracking done");
531           break;
532         }else{
533           /* Delete pair */
534           XBT_DEBUG("Delete pair %d at depth %d", current_pair->num, current_pair->depth);
535           if (current_pair->automaton_state->type == 1)
536             this->removeAcceptancePair(current_pair->num);
537           delete current_pair;
538         }
539       }
540
541     } /* End of if (current_pair->requests > 0) else ... */
542
543   } /* End of while(xbt_fifo_size(mc_stack) > 0) */
544
545   XBT_INFO("No property violation found.");
546   MC_print_statistics(mc_stats);
547   return SIMGRID_MC_EXIT_SUCCESS;
548 }
549
550 int LivenessChecker::run()
551 {
552   XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
553   MC_automaton_load(_sg_mc_property_file);
554   mc_model_checker->wait_for_requests();
555
556   XBT_DEBUG("Starting the liveness algorithm");
557   _sg_mc_liveness = 1;
558
559   /* Create exploration stack */
560   mc_stack = xbt_fifo_new();
561
562   /* Create the initial state */
563   initial_global_state = xbt_new0(s_mc_global_t, 1);
564
565   this->prepare();
566   return this->main();
567 }
568
569 }
570 }