1 /* Copyright (c) 2011-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
12 #include <boost/range/algorithm.hpp>
17 #include <xbt/automaton.h>
18 #include <xbt/dynar.h>
19 #include <xbt/dynar.hpp>
21 #include <xbt/sysdep.h>
23 #include "src/mc/mc_request.h"
24 #include "src/mc/checker/LivenessChecker.hpp"
25 #include "src/mc/mc_private.h"
26 #include "src/mc/mc_record.h"
27 #include "src/mc/mc_smx.h"
28 #include "src/mc/Client.hpp"
29 #include "src/mc/mc_private.h"
30 #include "src/mc/mc_replay.h"
31 #include "src/mc/mc_safety.h"
32 #include "src/mc/mc_exit.h"
33 #include "src/mc/Transition.hpp"
34 #include "src/mc/Session.hpp"
36 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_liveness, mc,
37 "Logging specific to algorithms for liveness properties verification");
39 /********* Static functions *********/
44 VisitedPair::VisitedPair(
45 int pair_num, xbt_automaton_state_t automaton_state,
46 std::shared_ptr<const std::vector<int>> atomic_propositions,
47 std::shared_ptr<simgrid::mc::State> graph_state)
49 simgrid::mc::Process* process = &(mc_model_checker->process());
51 this->graph_state = std::move(graph_state);
52 if(this->graph_state->system_state == nullptr)
53 this->graph_state->system_state = simgrid::mc::take_snapshot(pair_num);
54 this->heap_bytes_used = mmalloc_get_bytes_used_remote(
55 process->get_heap()->heaplimit,
56 process->get_malloc_info());
58 this->actors_count = mc_model_checker->process().actors().size();
60 this->automaton_state = automaton_state;
63 this->atomic_propositions = std::move(atomic_propositions);
66 VisitedPair::~VisitedPair() = default;
68 static bool evaluate_label(
69 xbt_automaton_exp_label_t l, std::vector<int> const& values)
72 case xbt_automaton_exp_label::AUT_OR:
73 return evaluate_label(l->u.or_and.left_exp, values)
74 || evaluate_label(l->u.or_and.right_exp, values);
75 case xbt_automaton_exp_label::AUT_AND:
76 return evaluate_label(l->u.or_and.left_exp, values)
77 && evaluate_label(l->u.or_and.right_exp, values);
78 case xbt_automaton_exp_label::AUT_NOT:
79 return !evaluate_label(l->u.exp_not, values);
80 case xbt_automaton_exp_label::AUT_PREDICAT:{
81 unsigned int cursor = 0;
82 xbt_automaton_propositional_symbol_t p = nullptr;
83 xbt_dynar_foreach(simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
84 if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
85 return values[cursor] != 0;
87 xbt_die("Missing predicate");
89 case xbt_automaton_exp_label::AUT_ONE:
92 xbt_die("Unexpected vaue for automaton");
96 Pair::Pair(unsigned long expanded_pairs) : num(expanded_pairs)
101 std::shared_ptr<const std::vector<int>> LivenessChecker::getPropositionValues()
103 std::vector<int> values;
104 unsigned int cursor = 0;
105 xbt_automaton_propositional_symbol_t ps = nullptr;
106 xbt_dynar_foreach(simgrid::mc::property_automaton->propositional_symbols, cursor, ps)
107 values.push_back(xbt_automaton_propositional_symbol_evaluate(ps));
108 return std::make_shared<const std::vector<int>>(std::move(values));
111 int LivenessChecker::compare(simgrid::mc::VisitedPair* state1, simgrid::mc::VisitedPair* state2)
113 simgrid::mc::Snapshot* s1 = state1->graph_state->system_state.get();
114 simgrid::mc::Snapshot* s2 = state2->graph_state->system_state.get();
115 int num1 = state1->num;
116 int num2 = state2->num;
117 return simgrid::mc::snapshot_compare(num1, s1, num2, s2);
120 std::shared_ptr<VisitedPair> LivenessChecker::insertAcceptancePair(simgrid::mc::Pair* pair)
122 std::shared_ptr<VisitedPair> new_pair = std::make_shared<VisitedPair>(
123 pair->num, pair->automaton_state, pair->atomic_propositions,
126 auto res = boost::range::equal_range(acceptancePairs_, new_pair.get(),
127 simgrid::mc::DerefAndCompareByActorsCountAndUsedHeap());
129 if (pair->search_cycle) for (auto i = res.first; i != res.second; ++i) {
130 std::shared_ptr<simgrid::mc::VisitedPair> const& pair_test = *i;
131 if (xbt_automaton_state_compare(
132 pair_test->automaton_state, new_pair->automaton_state) != 0
133 || *(pair_test->atomic_propositions) != *(new_pair->atomic_propositions)
134 || this->compare(pair_test.get(), new_pair.get()) != 0)
136 XBT_INFO("Pair %d already reached (equal to pair %d) !",
137 new_pair->num, pair_test->num);
138 explorationStack_.pop_back();
139 if (dot_output != nullptr)
140 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n",
141 this->previousPair_, pair_test->num,
142 this->previousRequest_.c_str());
146 acceptancePairs_.insert(res.first, new_pair);
150 void LivenessChecker::removeAcceptancePair(int pair_num)
152 for (auto i = acceptancePairs_.begin(); i != acceptancePairs_.end(); ++i)
153 if ((*i)->num == pair_num) {
154 acceptancePairs_.erase(i);
159 void LivenessChecker::prepare(void)
161 this->previousPair_ = 0;
163 std::shared_ptr<const std::vector<int>> propos = this->getPropositionValues();
165 // For each initial state of the property automaton, push a
166 // (application_state, automaton_state) pair to the exploration stack:
167 unsigned int cursor = 0;
168 xbt_automaton_state_t automaton_state;
169 xbt_dynar_foreach(simgrid::mc::property_automaton->states, cursor, automaton_state)
170 if (automaton_state->type == -1)
171 explorationStack_.push_back(this->newPair(nullptr, automaton_state, propos));
175 void LivenessChecker::replay()
177 XBT_DEBUG("**** Begin Replay ****");
179 /* Intermediate backtracking */
180 if(_sg_mc_checkpoint > 0) {
181 simgrid::mc::Pair* pair = explorationStack_.back().get();
182 if(pair->graph_state->system_state){
183 simgrid::mc::restore_snapshot(pair->graph_state->system_state);
188 /* Restore the initial state */
189 simgrid::mc::session->restoreInitialState();
191 /* Traverse the stack from the initial state and re-execute the transitions */
193 for (std::shared_ptr<Pair> const& pair : explorationStack_) {
194 if (pair == explorationStack_.back())
197 std::shared_ptr<State> state = pair->graph_state;
199 if (pair->exploration_started) {
201 int req_num = state->transition.argument;
202 smx_simcall_t saved_req = &state->executed_req;
204 smx_simcall_t req = nullptr;
206 if (saved_req != nullptr) {
207 /* because we got a copy of the executed request, we have to fetch the
208 real one, pointed by the request field of the issuer process */
209 const smx_actor_t issuer = MC_smx_simcall_get_issuer(saved_req);
210 req = &issuer->simcall;
212 /* Debug information */
213 XBT_DEBUG("Replay (depth = %d) : %s (%p)",
215 simgrid::mc::request_to_string(
216 req, req_num, simgrid::mc::RequestType::simix).c_str(),
220 this->getSession().execute(state->transition);
223 /* Update statistics */
224 visitedPairsCount_++;
225 mc_model_checker->executed_transitions++;
231 XBT_DEBUG("**** End Replay ****");
235 * \brief Checks whether a given pair has already been visited by the algorithm.
237 int LivenessChecker::insertVisitedPair(std::shared_ptr<VisitedPair> visited_pair, simgrid::mc::Pair* pair)
239 if (_sg_mc_visited == 0)
242 if (visited_pair == nullptr)
243 visited_pair = std::make_shared<VisitedPair>(
244 pair->num, pair->automaton_state, pair->atomic_propositions,
247 auto range = boost::range::equal_range(visitedPairs_, visited_pair.get(),
248 simgrid::mc::DerefAndCompareByActorsCountAndUsedHeap());
250 for (auto i = range.first; i != range.second; ++i) {
251 VisitedPair* pair_test = i->get();
252 if (xbt_automaton_state_compare(
253 pair_test->automaton_state, visited_pair->automaton_state) != 0
254 || *(pair_test->atomic_propositions) != *(visited_pair->atomic_propositions)
255 || this->compare(pair_test, visited_pair.get()) != 0)
257 if (pair_test->other_num == -1)
258 visited_pair->other_num = pair_test->num;
260 visited_pair->other_num = pair_test->other_num;
261 if (dot_output == nullptr)
262 XBT_DEBUG("Pair %d already visited ! (equal to pair %d)", visited_pair->num, pair_test->num);
264 XBT_DEBUG("Pair %d already visited ! (equal to pair %d (pair %d in dot_output))",
265 visited_pair->num, pair_test->num, visited_pair->other_num);
266 (*i) = std::move(visited_pair);
267 return (*i)->other_num;
270 visitedPairs_.insert(range.first, std::move(visited_pair));
271 this->purgeVisitedPairs();
275 void LivenessChecker::purgeVisitedPairs()
277 if (_sg_mc_visited != 0 && visitedPairs_.size() > (std::size_t) _sg_mc_visited) {
278 // Remove the oldest entry with a linear search:
279 visitedPairs_.erase(boost::min_element(visitedPairs_,
280 [](std::shared_ptr<VisitedPair> const a, std::shared_ptr<VisitedPair> const& b) {
281 return a->num < b->num; } ));
285 LivenessChecker::LivenessChecker(Session& session) : Checker(session)
289 LivenessChecker::~LivenessChecker()
293 RecordTrace LivenessChecker::getRecordTrace() // override
296 for (std::shared_ptr<Pair> const& pair : explorationStack_)
297 res.push_back(pair->graph_state->getTransition());
301 void LivenessChecker::logState() // override
304 XBT_INFO("Expanded pairs = %lu", expandedPairsCount_);
305 XBT_INFO("Visited pairs = %lu", visitedPairsCount_);
306 XBT_INFO("Executed transitions = %lu", mc_model_checker->executed_transitions);
309 void LivenessChecker::showAcceptanceCycle(std::size_t depth)
311 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
312 XBT_INFO("| ACCEPTANCE CYCLE |");
313 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
314 XBT_INFO("Counter-example that violates formula :");
315 simgrid::mc::dumpRecordPath();
316 for (auto& s : this->getTextualTrace())
317 XBT_INFO("%s", s.c_str());
318 simgrid::mc::session->logState();
319 XBT_INFO("Counter-example depth : %zd", depth);
322 std::vector<std::string> LivenessChecker::getTextualTrace() // override
324 std::vector<std::string> trace;
325 for (std::shared_ptr<Pair> const& pair : explorationStack_) {
326 int req_num = pair->graph_state->transition.argument;
327 smx_simcall_t req = &pair->graph_state->executed_req;
328 if (req && req->call != SIMCALL_NONE)
329 trace.push_back(simgrid::mc::request_to_string(
330 req, req_num, simgrid::mc::RequestType::executed));
335 void LivenessChecker::main(void)
337 while (!explorationStack_.empty()){
338 std::shared_ptr<Pair> current_pair = explorationStack_.back();
340 /* Update current state in buchi automaton */
341 simgrid::mc::property_automaton->current_state = current_pair->automaton_state;
343 XBT_DEBUG("********************* ( Depth = %d, search_cycle = %d, interleave size = %zd, pair_num = %d, requests = %d)",
344 current_pair->depth, current_pair->search_cycle,
345 current_pair->graph_state->interleaveSize(),
347 current_pair->requests);
349 if (current_pair->requests == 0) {
354 std::shared_ptr<VisitedPair> reached_pair;
355 if (current_pair->automaton_state->type == 1 && !current_pair->exploration_started
356 && (reached_pair = this->insertAcceptancePair(current_pair.get())) == nullptr) {
357 this->showAcceptanceCycle(current_pair->depth);
358 throw simgrid::mc::LivenessError();
361 /* Pair already visited ? stop the exploration on the current path */
362 int visited_num = -1;
363 if ((!current_pair->exploration_started)
364 && (visited_num = this->insertVisitedPair(
365 reached_pair, current_pair.get())) != -1) {
366 if (dot_output != nullptr){
367 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n",
368 this->previousPair_, visited_num,
369 this->previousRequest_.c_str());
372 XBT_DEBUG("Pair already visited (equal to pair %d), exploration on the current path stopped.", visited_num);
373 current_pair->requests = 0;
378 smx_simcall_t req = MC_state_get_request(current_pair->graph_state.get());
379 int req_num = current_pair->graph_state->transition.argument;
381 if (dot_output != nullptr) {
382 if (this->previousPair_ != 0 && this->previousPair_ != current_pair->num) {
383 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n",
384 this->previousPair_, current_pair->num,
385 this->previousRequest_.c_str());
386 this->previousRequest_.clear();
388 this->previousPair_ = current_pair->num;
389 this->previousRequest_ = simgrid::mc::request_get_dot_output(req, req_num);
390 if (current_pair->search_cycle)
391 fprintf(dot_output, "%d [shape=doublecircle];\n", current_pair->num);
395 XBT_DEBUG("Execute: %s",
396 simgrid::mc::request_to_string(
397 req, req_num, simgrid::mc::RequestType::simix).c_str());
400 mc_model_checker->executed_transitions++;
401 if (!current_pair->exploration_started)
402 visitedPairsCount_++;
404 /* Answer the request */
405 mc_model_checker->handle_simcall(current_pair->graph_state->transition);
407 /* Wait for requests (schedules processes) */
408 mc_model_checker->wait_for_requests();
410 current_pair->requests--;
411 current_pair->exploration_started = true;
413 /* Get values of atomic propositions (variables used in the property formula) */
414 std::shared_ptr<const std::vector<int>> prop_values = this->getPropositionValues();
416 // For each enabled transition in the property automaton, push a
417 // (application_state, automaton_state) pair to the exploration stack:
418 int cursor = xbt_dynar_length(current_pair->automaton_state->out) - 1;
419 while (cursor >= 0) {
420 xbt_automaton_transition_t transition_succ = (xbt_automaton_transition_t)xbt_dynar_get_as(current_pair->automaton_state->out, cursor, xbt_automaton_transition_t);
421 if (evaluate_label(transition_succ->label, *prop_values))
422 explorationStack_.push_back(this->newPair(
423 current_pair.get(), transition_succ->dst, prop_values));
429 XBT_INFO("No property violation found.");
430 simgrid::mc::session->logState();
433 std::shared_ptr<Pair> LivenessChecker::newPair(Pair* current_pair, xbt_automaton_state_t state, std::shared_ptr<const std::vector<int>> propositions)
435 std::shared_ptr<Pair> next_pair = std::make_shared<Pair>(++expandedPairsCount_);
436 next_pair->automaton_state = state;
437 next_pair->graph_state = std::shared_ptr<simgrid::mc::State>(new simgrid::mc::State(++expandedStatesCount_));
438 next_pair->atomic_propositions = std::move(propositions);
440 next_pair->depth = current_pair->depth + 1;
442 next_pair->depth = 1;
443 /* Get enabled actors and insert them in the interleave set of the next graph_state */
444 for (auto& actor : mc_model_checker->process().actors())
445 if (simgrid::mc::actor_is_enabled(actor.copy.getBuffer()))
446 next_pair->graph_state->interleave(actor.copy.getBuffer());
447 next_pair->requests = next_pair->graph_state->interleaveSize();
448 /* FIXME : get search_cycle value for each accepting state */
449 if (next_pair->automaton_state->type == 1 ||
450 (current_pair && current_pair->search_cycle))
451 next_pair->search_cycle = true;
453 next_pair->search_cycle = false;
457 void LivenessChecker::backtrack()
459 /* Traverse the stack backwards until a pair with a non empty interleave
460 set is found, deleting all the pairs that have it empty in the way. */
461 while (!explorationStack_.empty()) {
462 std::shared_ptr<simgrid::mc::Pair> current_pair = explorationStack_.back();
463 explorationStack_.pop_back();
464 if (current_pair->requests > 0) {
465 /* We found a backtracking point */
466 XBT_DEBUG("Backtracking to depth %d", current_pair->depth);
467 explorationStack_.push_back(std::move(current_pair));
469 XBT_DEBUG("Backtracking done");
472 XBT_DEBUG("Delete pair %d at depth %d", current_pair->num, current_pair->depth);
473 if (current_pair->automaton_state->type == 1)
474 this->removeAcceptancePair(current_pair->num);
479 void LivenessChecker::run()
481 XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
482 MC_automaton_load(_sg_mc_property_file);
484 XBT_DEBUG("Starting the liveness algorithm");
485 simgrid::mc::session->initialize();
491 Checker* createLivenessChecker(Session& session)
493 return new LivenessChecker(session);