1 /* Copyright (c) 2011-2019. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include <boost/range/algorithm.hpp>
16 #include <xbt/automaton.h>
17 #include <xbt/dynar.h>
19 #include <xbt/sysdep.h>
21 #include "src/mc/Session.hpp"
22 #include "src/mc/Transition.hpp"
23 #include "src/mc/checker/LivenessChecker.hpp"
24 #include "src/mc/mc_exit.hpp"
25 #include "src/mc/mc_private.hpp"
26 #include "src/mc/mc_private.hpp"
27 #include "src/mc/mc_record.hpp"
28 #include "src/mc/mc_replay.hpp"
29 #include "src/mc/mc_request.hpp"
30 #include "src/mc/mc_smx.hpp"
31 #include "src/mc/remote/Client.hpp"
33 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_liveness, mc, "Logging specific to algorithms for liveness properties verification");
35 /********* Static functions *********/
40 VisitedPair::VisitedPair(int pair_num, xbt_automaton_state_t automaton_state,
41 std::shared_ptr<const std::vector<int>> atomic_propositions,
42 std::shared_ptr<simgrid::mc::State> graph_state)
43 : num(pair_num), automaton_state(automaton_state)
45 simgrid::mc::RemoteClient* process = &(mc_model_checker->process());
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(process->get_heap()->heaplimit, process->get_malloc_info());
52 this->actors_count = mc_model_checker->process().actors().size();
55 this->atomic_propositions = std::move(atomic_propositions);
58 static bool evaluate_label(xbt_automaton_exp_label_t l, std::vector<int> const& values)
61 case xbt_automaton_exp_label::AUT_OR:
62 return evaluate_label(l->u.or_and.left_exp, values)
63 || evaluate_label(l->u.or_and.right_exp, values);
64 case xbt_automaton_exp_label::AUT_AND:
65 return evaluate_label(l->u.or_and.left_exp, values)
66 && evaluate_label(l->u.or_and.right_exp, values);
67 case xbt_automaton_exp_label::AUT_NOT:
68 return not evaluate_label(l->u.exp_not, values);
69 case xbt_automaton_exp_label::AUT_PREDICAT:{
70 unsigned int cursor = 0;
71 xbt_automaton_propositional_symbol_t p = nullptr;
72 xbt_dynar_foreach(simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
73 if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
74 return values[cursor] != 0;
76 xbt_die("Missing predicate");
79 case xbt_automaton_exp_label::AUT_ONE:
82 xbt_die("Unexpected vaue for automaton");
86 Pair::Pair(unsigned long expanded_pairs) : num(expanded_pairs)
89 std::shared_ptr<const std::vector<int>> LivenessChecker::getPropositionValues()
91 std::vector<int> values;
92 unsigned int cursor = 0;
93 xbt_automaton_propositional_symbol_t ps = nullptr;
94 xbt_dynar_foreach(simgrid::mc::property_automaton->propositional_symbols, cursor, ps)
95 values.push_back(xbt_automaton_propositional_symbol_evaluate(ps));
96 return std::make_shared<const std::vector<int>>(std::move(values));
99 int LivenessChecker::compare(simgrid::mc::VisitedPair* state1, simgrid::mc::VisitedPair* state2)
101 simgrid::mc::Snapshot* s1 = state1->graph_state->system_state.get();
102 simgrid::mc::Snapshot* s2 = state2->graph_state->system_state.get();
103 return simgrid::mc::snapshot_compare(s1, s2);
106 std::shared_ptr<VisitedPair> LivenessChecker::insertAcceptancePair(simgrid::mc::Pair* pair)
108 std::shared_ptr<VisitedPair> new_pair = std::make_shared<VisitedPair>(
109 pair->num, pair->automaton_state, pair->atomic_propositions,
112 auto res = boost::range::equal_range(acceptancePairs_, new_pair.get(),
113 simgrid::mc::DerefAndCompareByActorsCountAndUsedHeap());
115 if (pair->search_cycle) for (auto i = res.first; i != res.second; ++i) {
116 std::shared_ptr<simgrid::mc::VisitedPair> const& pair_test = *i;
117 if (xbt_automaton_state_compare(
118 pair_test->automaton_state, new_pair->automaton_state) != 0
119 || *(pair_test->atomic_propositions) != *(new_pair->atomic_propositions)
120 || this->compare(pair_test.get(), new_pair.get()) != 0)
122 XBT_INFO("Pair %d already reached (equal to pair %d) !", new_pair->num, pair_test->num);
123 explorationStack_.pop_back();
124 if (dot_output != nullptr)
125 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", this->previousPair_, pair_test->num,
126 this->previousRequest_.c_str());
130 acceptancePairs_.insert(res.first, new_pair);
134 void LivenessChecker::removeAcceptancePair(int pair_num)
136 for (auto i = acceptancePairs_.begin(); i != acceptancePairs_.end(); ++i)
137 if ((*i)->num == pair_num) {
138 acceptancePairs_.erase(i);
143 void LivenessChecker::replay()
145 XBT_DEBUG("**** Begin Replay ****");
147 /* Intermediate backtracking */
148 if(_sg_mc_checkpoint > 0) {
149 simgrid::mc::Pair* pair = explorationStack_.back().get();
150 if(pair->graph_state->system_state){
151 simgrid::mc::restore_snapshot(pair->graph_state->system_state);
156 /* Restore the initial state */
157 simgrid::mc::session->restoreInitialState();
159 /* Traverse the stack from the initial state and re-execute the transitions */
161 for (std::shared_ptr<Pair> const& pair : explorationStack_) {
162 if (pair == explorationStack_.back())
165 std::shared_ptr<State> state = pair->graph_state;
167 if (pair->exploration_started) {
169 int req_num = state->transition.argument;
170 smx_simcall_t saved_req = &state->executed_req;
172 smx_simcall_t req = nullptr;
174 if (saved_req != nullptr) {
175 /* because we got a copy of the executed request, we have to fetch the
176 real one, pointed by the request field of the issuer process */
177 const smx_actor_t issuer = MC_smx_simcall_get_issuer(saved_req);
178 req = &issuer->simcall;
180 /* Debug information */
181 XBT_DEBUG("Replay (depth = %d) : %s (%p)",
183 simgrid::mc::request_to_string(
184 req, req_num, simgrid::mc::RequestType::simix).c_str(),
188 this->getSession().execute(state->transition);
191 /* Update statistics */
192 visitedPairsCount_++;
193 mc_model_checker->executed_transitions++;
199 XBT_DEBUG("**** End Replay ****");
203 * @brief Checks whether a given pair has already been visited by the algorithm.
205 int LivenessChecker::insertVisitedPair(std::shared_ptr<VisitedPair> visited_pair, simgrid::mc::Pair* pair)
207 if (_sg_mc_max_visited_states == 0)
210 if (visited_pair == nullptr)
212 std::make_shared<VisitedPair>(pair->num, pair->automaton_state, pair->atomic_propositions, pair->graph_state);
214 auto range = boost::range::equal_range(visitedPairs_, visited_pair.get(),
215 simgrid::mc::DerefAndCompareByActorsCountAndUsedHeap());
217 for (auto i = range.first; i != range.second; ++i) {
218 VisitedPair* pair_test = i->get();
219 if (xbt_automaton_state_compare(
220 pair_test->automaton_state, visited_pair->automaton_state) != 0
221 || *(pair_test->atomic_propositions) != *(visited_pair->atomic_propositions)
222 || this->compare(pair_test, visited_pair.get()) != 0)
224 if (pair_test->other_num == -1)
225 visited_pair->other_num = pair_test->num;
227 visited_pair->other_num = pair_test->other_num;
228 if (dot_output == nullptr)
229 XBT_DEBUG("Pair %d already visited ! (equal to pair %d)", visited_pair->num, pair_test->num);
231 XBT_DEBUG("Pair %d already visited ! (equal to pair %d (pair %d in dot_output))",
232 visited_pair->num, pair_test->num, visited_pair->other_num);
233 (*i) = std::move(visited_pair);
234 return (*i)->other_num;
237 visitedPairs_.insert(range.first, std::move(visited_pair));
238 this->purgeVisitedPairs();
242 void LivenessChecker::purgeVisitedPairs()
244 if (_sg_mc_max_visited_states != 0 && visitedPairs_.size() > (std::size_t)_sg_mc_max_visited_states) {
245 // Remove the oldest entry with a linear search:
246 visitedPairs_.erase(boost::min_element(visitedPairs_,
247 [](std::shared_ptr<VisitedPair> const a, std::shared_ptr<VisitedPair> const& b) {
248 return a->num < b->num; } ));
252 LivenessChecker::LivenessChecker(Session& s) : Checker(s)
256 RecordTrace LivenessChecker::getRecordTrace() // override
259 for (std::shared_ptr<Pair> const& pair : explorationStack_)
260 res.push_back(pair->graph_state->getTransition());
264 void LivenessChecker::logState() // override
266 XBT_INFO("Expanded pairs = %lu", expandedPairsCount_);
267 XBT_INFO("Visited pairs = %lu", visitedPairsCount_);
268 XBT_INFO("Executed transitions = %lu", mc_model_checker->executed_transitions);
271 void LivenessChecker::showAcceptanceCycle(std::size_t depth)
273 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
274 XBT_INFO("| ACCEPTANCE CYCLE |");
275 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
276 XBT_INFO("Counter-example that violates formula:");
277 for (auto const& s : this->getTextualTrace())
278 XBT_INFO(" %s", s.c_str());
279 simgrid::mc::dumpRecordPath();
280 simgrid::mc::session->logState();
281 XBT_INFO("Counter-example depth: %zu", depth);
284 std::vector<std::string> LivenessChecker::getTextualTrace() // override
286 std::vector<std::string> trace;
287 for (std::shared_ptr<Pair> const& pair : explorationStack_) {
288 int req_num = pair->graph_state->transition.argument;
289 smx_simcall_t req = &pair->graph_state->executed_req;
290 if (req && req->call != SIMCALL_NONE)
291 trace.push_back(simgrid::mc::request_to_string(
292 req, req_num, simgrid::mc::RequestType::executed));
297 std::shared_ptr<Pair> LivenessChecker::newPair(Pair* current_pair, xbt_automaton_state_t state,
298 std::shared_ptr<const std::vector<int>> propositions)
300 expandedPairsCount_++;
301 std::shared_ptr<Pair> next_pair = std::make_shared<Pair>(expandedPairsCount_);
302 next_pair->automaton_state = state;
303 next_pair->graph_state = std::shared_ptr<simgrid::mc::State>(new simgrid::mc::State(++expandedStatesCount_));
304 next_pair->atomic_propositions = std::move(propositions);
306 next_pair->depth = current_pair->depth + 1;
308 next_pair->depth = 1;
309 /* Get enabled actors and insert them in the interleave set of the next graph_state */
310 for (auto& actor : mc_model_checker->process().actors())
311 if (simgrid::mc::actor_is_enabled(actor.copy.getBuffer()))
312 next_pair->graph_state->addInterleavingSet(actor.copy.getBuffer());
313 next_pair->requests = next_pair->graph_state->interleaveSize();
314 /* FIXME : get search_cycle value for each accepting state */
315 if (next_pair->automaton_state->type == 1 || (current_pair && current_pair->search_cycle))
316 next_pair->search_cycle = true;
318 next_pair->search_cycle = false;
322 void LivenessChecker::backtrack()
324 /* Traverse the stack backwards until a pair with a non empty interleave
325 set is found, deleting all the pairs that have it empty in the way. */
326 while (not explorationStack_.empty()) {
327 std::shared_ptr<simgrid::mc::Pair> current_pair = explorationStack_.back();
328 explorationStack_.pop_back();
329 if (current_pair->requests > 0) {
330 /* We found a backtracking point */
331 XBT_DEBUG("Backtracking to depth %d", current_pair->depth);
332 explorationStack_.push_back(std::move(current_pair));
334 XBT_DEBUG("Backtracking done");
337 XBT_DEBUG("Delete pair %d at depth %d", current_pair->num, current_pair->depth);
338 if (current_pair->automaton_state->type == 1)
339 this->removeAcceptancePair(current_pair->num);
344 void LivenessChecker::run()
346 XBT_INFO("Check the liveness property %s", _sg_mc_property_file.get().c_str());
347 MC_automaton_load(_sg_mc_property_file.get().c_str());
349 XBT_DEBUG("Starting the liveness algorithm");
350 simgrid::mc::session->initialize();
353 this->previousPair_ = 0;
355 std::shared_ptr<const std::vector<int>> propos = this->getPropositionValues();
357 // For each initial state of the property automaton, push a
358 // (application_state, automaton_state) pair to the exploration stack:
359 unsigned int cursor = 0;
360 xbt_automaton_state_t automaton_state;
361 xbt_dynar_foreach (simgrid::mc::property_automaton->states, cursor, automaton_state)
362 if (automaton_state->type == -1)
363 explorationStack_.push_back(this->newPair(nullptr, automaton_state, propos));
365 /* Actually run the double DFS search for counter-examples */
366 while (not explorationStack_.empty()) {
367 std::shared_ptr<Pair> current_pair = explorationStack_.back();
369 /* Update current state in buchi automaton */
370 simgrid::mc::property_automaton->current_state = current_pair->automaton_state;
373 "********************* ( Depth = %d, search_cycle = %d, interleave size = %zu, pair_num = %d, requests = %d)",
374 current_pair->depth, current_pair->search_cycle, current_pair->graph_state->interleaveSize(), current_pair->num,
375 current_pair->requests);
377 if (current_pair->requests == 0) {
382 std::shared_ptr<VisitedPair> reached_pair;
383 if (current_pair->automaton_state->type == 1 && not current_pair->exploration_started) {
384 reached_pair = this->insertAcceptancePair(current_pair.get());
385 if (reached_pair == nullptr) {
386 this->showAcceptanceCycle(current_pair->depth);
387 throw simgrid::mc::LivenessError();
391 /* Pair already visited ? stop the exploration on the current path */
392 if (not current_pair->exploration_started) {
393 int visited_num = this->insertVisitedPair(reached_pair, current_pair.get());
394 if (visited_num != -1) {
395 if (dot_output != nullptr) {
396 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", this->previousPair_, visited_num,
397 this->previousRequest_.c_str());
400 XBT_DEBUG("Pair already visited (equal to pair %d), exploration on the current path stopped.", visited_num);
401 current_pair->requests = 0;
407 smx_simcall_t req = MC_state_get_request(current_pair->graph_state.get());
408 int req_num = current_pair->graph_state->transition.argument;
410 if (dot_output != nullptr) {
411 if (this->previousPair_ != 0 && this->previousPair_ != current_pair->num) {
412 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n",
413 this->previousPair_, current_pair->num,
414 this->previousRequest_.c_str());
415 this->previousRequest_.clear();
417 this->previousPair_ = current_pair->num;
418 this->previousRequest_ = simgrid::mc::request_get_dot_output(req, req_num);
419 if (current_pair->search_cycle)
420 fprintf(dot_output, "%d [shape=doublecircle];\n", current_pair->num);
424 XBT_DEBUG("Execute: %s",
425 simgrid::mc::request_to_string(
426 req, req_num, simgrid::mc::RequestType::simix).c_str());
429 mc_model_checker->executed_transitions++;
430 if (not current_pair->exploration_started)
431 visitedPairsCount_++;
433 /* Answer the request */
434 mc_model_checker->handle_simcall(current_pair->graph_state->transition);
436 /* Wait for requests (schedules processes) */
437 mc_model_checker->wait_for_requests();
439 current_pair->requests--;
440 current_pair->exploration_started = true;
442 /* Get values of atomic propositions (variables used in the property formula) */
443 std::shared_ptr<const std::vector<int>> prop_values = this->getPropositionValues();
445 // For each enabled transition in the property automaton, push a
446 // (application_state, automaton_state) pair to the exploration stack:
447 for (int i = xbt_dynar_length(current_pair->automaton_state->out) - 1; i >= 0; i--) {
448 xbt_automaton_transition_t transition_succ = (xbt_automaton_transition_t)xbt_dynar_get_as(
449 current_pair->automaton_state->out, i, xbt_automaton_transition_t);
450 if (evaluate_label(transition_succ->label, *prop_values))
451 explorationStack_.push_back(this->newPair(
452 current_pair.get(), transition_succ->dst, prop_values));
457 XBT_INFO("No property violation found.");
458 simgrid::mc::session->logState();
461 Checker* createLivenessChecker(Session& s)
463 return new LivenessChecker(s);