Logo AND Algorithmique Numérique Distribuée

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