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. */
16 #include <xbt/automaton.h>
17 #include <xbt/dynar.h>
18 #include <xbt/dynar.hpp>
20 #include <xbt/sysdep.h>
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 #include "src/mc/Transition.hpp"
33 #include "src/mc/Session.hpp"
35 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_liveness, mc,
36 "Logging specific to algorithms for liveness properties verification");
38 /********* Static functions *********/
43 VisitedPair::VisitedPair(
44 int pair_num, xbt_automaton_state_t automaton_state,
45 std::shared_ptr<const std::vector<int>> atomic_propositions,
46 std::shared_ptr<simgrid::mc::State> graph_state)
48 simgrid::mc::Process* process = &(mc_model_checker->process());
50 this->graph_state = std::move(graph_state);
51 if(this->graph_state->system_state == nullptr)
52 this->graph_state->system_state = simgrid::mc::take_snapshot(pair_num);
53 this->heap_bytes_used = mmalloc_get_bytes_used_remote(
54 process->get_heap()->heaplimit,
55 process->get_malloc_info());
57 this->nb_processes = mc_model_checker->process().simix_processes().size();
59 this->automaton_state = automaton_state;
62 this->atomic_propositions = std::move(atomic_propositions);
65 VisitedPair::~VisitedPair()
69 static bool evaluate_label(
70 xbt_automaton_exp_label_t l, std::vector<int> const& values)
73 case xbt_automaton_exp_label::AUT_OR:
74 return evaluate_label(l->u.or_and.left_exp, values)
75 || evaluate_label(l->u.or_and.right_exp, values);
76 case xbt_automaton_exp_label::AUT_AND:
77 return evaluate_label(l->u.or_and.left_exp, values)
78 && evaluate_label(l->u.or_and.right_exp, values);
79 case xbt_automaton_exp_label::AUT_NOT:
80 return !evaluate_label(l->u.exp_not, values);
81 case xbt_automaton_exp_label::AUT_PREDICAT:{
82 unsigned int cursor = 0;
83 xbt_automaton_propositional_symbol_t p = nullptr;
84 xbt_dynar_foreach(simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
85 if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
86 return values[cursor] != 0;
88 xbt_die("Missing predicate");
90 case xbt_automaton_exp_label::AUT_ONE:
93 xbt_die("Unexpected vaue for automaton");
97 Pair::Pair(unsigned long expanded_pairs) : num(expanded_pairs)
102 std::shared_ptr<const std::vector<int>> LivenessChecker::getPropositionValues()
104 std::vector<int> values;
105 unsigned int cursor = 0;
106 xbt_automaton_propositional_symbol_t ps = nullptr;
107 xbt_dynar_foreach(simgrid::mc::property_automaton->propositional_symbols, cursor, ps)
108 values.push_back(xbt_automaton_propositional_symbol_evaluate(ps));
109 return std::make_shared<const std::vector<int>>(std::move(values));
112 int LivenessChecker::compare(simgrid::mc::VisitedPair* state1, simgrid::mc::VisitedPair* state2)
114 simgrid::mc::Snapshot* s1 = state1->graph_state->system_state.get();
115 simgrid::mc::Snapshot* s2 = state2->graph_state->system_state.get();
116 int num1 = state1->num;
117 int num2 = state2->num;
118 return simgrid::mc::snapshot_compare(num1, s1, num2, s2);
121 std::shared_ptr<VisitedPair> LivenessChecker::insertAcceptancePair(simgrid::mc::Pair* pair)
123 std::shared_ptr<VisitedPair> new_pair = std::make_shared<VisitedPair>(
124 pair->num, pair->automaton_state, pair->atomic_propositions,
127 auto res = std::equal_range(acceptancePairs_.begin(), acceptancePairs_.end(),
128 new_pair.get(), simgrid::mc::DerefAndCompareByNbProcessesAndUsedHeap());
130 if (pair->search_cycle) for (auto i = res.first; i != res.second; ++i) {
131 std::shared_ptr<simgrid::mc::VisitedPair> const& pair_test = *i;
132 if (xbt_automaton_state_compare(
133 pair_test->automaton_state, new_pair->automaton_state) != 0
134 || *(pair_test->atomic_propositions) != *(new_pair->atomic_propositions)
135 || this->compare(pair_test.get(), new_pair.get()) != 0)
137 XBT_INFO("Pair %d already reached (equal to pair %d) !",
138 new_pair->num, pair_test->num);
139 explorationStack_.pop_back();
140 if (dot_output != nullptr)
141 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n",
142 initial_global_state->prev_pair, pair_test->num,
143 initial_global_state->prev_req.c_str());
147 acceptancePairs_.insert(res.first, new_pair);
151 void LivenessChecker::removeAcceptancePair(int pair_num)
153 for (auto i = acceptancePairs_.begin(); i != acceptancePairs_.end(); ++i)
154 if ((*i)->num == pair_num) {
155 acceptancePairs_.erase(i);
160 void LivenessChecker::prepare(void)
162 initial_global_state->snapshot = simgrid::mc::take_snapshot(0);
163 initial_global_state->prev_pair = 0;
165 std::shared_ptr<const std::vector<int>> propos = this->getPropositionValues();
167 // For each initial state of the property automaton, push a
168 // (application_state, automaton_state) pair to the exploration stack:
169 unsigned int cursor = 0;
170 xbt_automaton_state_t automaton_state;
171 xbt_dynar_foreach(simgrid::mc::property_automaton->states, cursor, automaton_state)
172 if (automaton_state->type == -1)
173 explorationStack_.push_back(this->newPair(nullptr, automaton_state, propos));
177 void LivenessChecker::replay()
179 XBT_DEBUG("**** Begin Replay ****");
181 /* Intermediate backtracking */
182 if(_sg_mc_checkpoint > 0) {
183 simgrid::mc::Pair* pair = explorationStack_.back().get();
184 if(pair->graph_state->system_state){
185 simgrid::mc::restore_snapshot(pair->graph_state->system_state);
190 /* Restore the initial state */
191 simgrid::mc::restore_snapshot(initial_global_state->snapshot);
193 /* Traverse the stack from the initial state and re-execute the transitions */
195 for (std::shared_ptr<Pair> const& pair : explorationStack_) {
196 if (pair == explorationStack_.back())
199 std::shared_ptr<State> state = pair->graph_state;
201 if (pair->exploration_started) {
203 int req_num = state->transition.argument;
204 smx_simcall_t saved_req = &state->executed_req;
206 smx_simcall_t req = nullptr;
208 if (saved_req != nullptr) {
209 /* because we got a copy of the executed request, we have to fetch the
210 real one, pointed by the request field of the issuer process */
211 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
212 req = &issuer->simcall;
214 /* Debug information */
215 XBT_DEBUG("Replay (depth = %d) : %s (%p)",
217 simgrid::mc::request_to_string(
218 req, req_num, simgrid::mc::RequestType::simix).c_str(),
222 this->getSession().execute(state->transition);
225 /* Update statistics */
226 visitedPairsCount_++;
227 mc_model_checker->executed_transitions++;
233 XBT_DEBUG("**** End Replay ****");
237 * \brief Checks whether a given pair has already been visited by the algorithm.
239 int LivenessChecker::insertVisitedPair(std::shared_ptr<VisitedPair> visited_pair, simgrid::mc::Pair* pair)
241 if (_sg_mc_visited == 0)
244 if (visited_pair == nullptr)
245 visited_pair = std::make_shared<VisitedPair>(
246 pair->num, pair->automaton_state, pair->atomic_propositions,
249 auto range = std::equal_range(visitedPairs_.begin(), visitedPairs_.end(),
250 visited_pair.get(), simgrid::mc::DerefAndCompareByNbProcessesAndUsedHeap());
252 for (auto i = range.first; i != range.second; ++i) {
253 VisitedPair* pair_test = i->get();
254 if (xbt_automaton_state_compare(
255 pair_test->automaton_state, visited_pair->automaton_state) != 0
256 || *(pair_test->atomic_propositions) != *(visited_pair->atomic_propositions)
257 || this->compare(pair_test, visited_pair.get()) != 0)
259 if (pair_test->other_num == -1)
260 visited_pair->other_num = pair_test->num;
262 visited_pair->other_num = pair_test->other_num;
263 if (dot_output == nullptr)
264 XBT_DEBUG("Pair %d already visited ! (equal to pair %d)", visited_pair->num, pair_test->num);
266 XBT_DEBUG("Pair %d already visited ! (equal to pair %d (pair %d in dot_output))",
267 visited_pair->num, pair_test->num, visited_pair->other_num);
268 (*i) = std::move(visited_pair);
269 return (*i)->other_num;
272 visitedPairs_.insert(range.first, std::move(visited_pair));
273 this->purgeVisitedPairs();
277 void LivenessChecker::purgeVisitedPairs()
279 if (_sg_mc_visited != 0 && visitedPairs_.size() > (std::size_t) _sg_mc_visited) {
280 // Remove the oldest entry with a linear search:
281 visitedPairs_.erase(std::min_element(
282 visitedPairs_.begin(), visitedPairs_.end(),
283 [](std::shared_ptr<VisitedPair> const a, std::shared_ptr<VisitedPair> const& b) {
284 return a->num < b->num; } ));
288 LivenessChecker::LivenessChecker(Session& session) : Checker(session)
292 LivenessChecker::~LivenessChecker()
296 RecordTrace LivenessChecker::getRecordTrace() // override
299 for (std::shared_ptr<Pair> const& pair : explorationStack_)
300 res.push_back(pair->graph_state->getTransition());
304 void LivenessChecker::logState() // override
307 XBT_INFO("Expanded pairs = %lu", expandedPairsCount_);
308 XBT_INFO("Visited pairs = %lu", visitedPairsCount_);
309 XBT_INFO("Executed transitions = %lu", mc_model_checker->executed_transitions);
312 void LivenessChecker::showAcceptanceCycle(std::size_t depth)
314 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
315 XBT_INFO("| ACCEPTANCE CYCLE |");
316 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
317 XBT_INFO("Counter-example that violates formula :");
318 simgrid::mc::dumpRecordPath();
319 for (auto& s : this->getTextualTrace())
320 XBT_INFO("%s", s.c_str());
321 simgrid::mc::session->logState();
322 XBT_INFO("Counter-example depth : %zd", depth);
325 std::vector<std::string> LivenessChecker::getTextualTrace() // override
327 std::vector<std::string> trace;
328 for (std::shared_ptr<Pair> const& pair : explorationStack_) {
329 int req_num = pair->graph_state->transition.argument;
330 smx_simcall_t req = &pair->graph_state->executed_req;
331 if (req && req->call != SIMCALL_NONE)
332 trace.push_back(simgrid::mc::request_to_string(
333 req, req_num, simgrid::mc::RequestType::executed));
338 int LivenessChecker::main(void)
340 while (!explorationStack_.empty()){
341 std::shared_ptr<Pair> current_pair = explorationStack_.back();
343 /* Update current state in buchi automaton */
344 simgrid::mc::property_automaton->current_state = current_pair->automaton_state;
346 XBT_DEBUG("********************* ( Depth = %d, search_cycle = %d, interleave size = %zd, pair_num = %d, requests = %d)",
347 current_pair->depth, current_pair->search_cycle,
348 current_pair->graph_state->interleaveSize(),
350 current_pair->requests);
352 if (current_pair->requests == 0) {
357 std::shared_ptr<VisitedPair> reached_pair;
358 if (current_pair->automaton_state->type == 1 && !current_pair->exploration_started
359 && (reached_pair = this->insertAcceptancePair(current_pair.get())) == nullptr) {
360 this->showAcceptanceCycle(current_pair->depth);
361 return SIMGRID_MC_EXIT_LIVENESS;
364 /* Pair already visited ? stop the exploration on the current path */
365 int visited_num = -1;
366 if ((!current_pair->exploration_started)
367 && (visited_num = this->insertVisitedPair(
368 reached_pair, current_pair.get())) != -1) {
369 if (dot_output != nullptr){
370 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n",
371 initial_global_state->prev_pair, visited_num,
372 initial_global_state->prev_req.c_str());
375 XBT_DEBUG("Pair already visited (equal to pair %d), exploration on the current path stopped.", visited_num);
376 current_pair->requests = 0;
381 smx_simcall_t req = MC_state_get_request(current_pair->graph_state.get());
382 int req_num = current_pair->graph_state->transition.argument;
384 if (dot_output != nullptr) {
385 if (initial_global_state->prev_pair != 0 && initial_global_state->prev_pair != current_pair->num) {
386 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n",
387 initial_global_state->prev_pair, current_pair->num,
388 initial_global_state->prev_req.c_str());
389 initial_global_state->prev_req.clear();
391 initial_global_state->prev_pair = current_pair->num;
392 initial_global_state->prev_req = simgrid::mc::request_get_dot_output(req, req_num);
393 if (current_pair->search_cycle)
394 fprintf(dot_output, "%d [shape=doublecircle];\n", current_pair->num);
398 XBT_DEBUG("Execute: %s",
399 simgrid::mc::request_to_string(
400 req, req_num, simgrid::mc::RequestType::simix).c_str());
403 mc_model_checker->executed_transitions++;
404 if (!current_pair->exploration_started)
405 visitedPairsCount_++;
407 /* Answer the request */
408 mc_model_checker->handle_simcall(current_pair->graph_state->transition);
410 /* Wait for requests (schedules processes) */
411 mc_model_checker->wait_for_requests();
413 current_pair->requests--;
414 current_pair->exploration_started = true;
416 /* Get values of atomic propositions (variables used in the property formula) */
417 std::shared_ptr<const std::vector<int>> prop_values = this->getPropositionValues();
419 // For each enabled transition in the property automaton, push a
420 // (application_state, automaton_state) pair to the exploration stack:
421 int cursor = xbt_dynar_length(current_pair->automaton_state->out) - 1;
422 while (cursor >= 0) {
423 xbt_automaton_transition_t transition_succ = (xbt_automaton_transition_t)xbt_dynar_get_as(current_pair->automaton_state->out, cursor, xbt_automaton_transition_t);
424 if (evaluate_label(transition_succ->label, *prop_values))
425 explorationStack_.push_back(this->newPair(
426 current_pair.get(), transition_succ->dst, prop_values));
432 XBT_INFO("No property violation found.");
433 simgrid::mc::session->logState();
434 return SIMGRID_MC_EXIT_SUCCESS;
437 std::shared_ptr<Pair> LivenessChecker::newPair(Pair* current_pair, xbt_automaton_state_t state, std::shared_ptr<const std::vector<int>> propositions)
439 std::shared_ptr<Pair> next_pair = std::make_shared<Pair>(++expandedPairsCount_);
440 next_pair->automaton_state = state;
441 next_pair->graph_state = std::shared_ptr<simgrid::mc::State>(MC_state_new(++expandedStatesCount_));
442 next_pair->atomic_propositions = std::move(propositions);
444 next_pair->depth = current_pair->depth + 1;
446 next_pair->depth = 1;
447 /* Get enabled processes and insert them in the interleave set of the next graph_state */
448 for (auto& p : mc_model_checker->process().simix_processes())
449 if (simgrid::mc::process_is_enabled(&p.copy))
450 next_pair->graph_state->interleave(&p.copy);
451 next_pair->requests = next_pair->graph_state->interleaveSize();
452 /* FIXME : get search_cycle value for each acceptant state */
453 if (next_pair->automaton_state->type == 1 ||
454 (current_pair && current_pair->search_cycle))
455 next_pair->search_cycle = true;
457 next_pair->search_cycle = false;
458 return std::move(next_pair);
461 void LivenessChecker::backtrack()
463 /* Traverse the stack backwards until a pair with a non empty interleave
464 set is found, deleting all the pairs that have it empty in the way. */
465 while (!explorationStack_.empty()) {
466 std::shared_ptr<simgrid::mc::Pair> current_pair = explorationStack_.back();
467 explorationStack_.pop_back();
468 if (current_pair->requests > 0) {
469 /* We found a backtracking point */
470 XBT_DEBUG("Backtracking to depth %d", current_pair->depth);
471 explorationStack_.push_back(std::move(current_pair));
473 XBT_DEBUG("Backtracking done");
476 XBT_DEBUG("Delete pair %d at depth %d", current_pair->num, current_pair->depth);
477 if (current_pair->automaton_state->type == 1)
478 this->removeAcceptancePair(current_pair->num);
483 int LivenessChecker::run()
485 XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
486 MC_automaton_load(_sg_mc_property_file);
487 mc_model_checker->wait_for_requests();
489 XBT_DEBUG("Starting the liveness algorithm");
491 /* Create the initial state */
492 simgrid::mc::initial_global_state = std::unique_ptr<s_mc_global_t>(new s_mc_global_t());
495 int res = this->main();
496 simgrid::mc::initial_global_state = nullptr;
501 Checker* createLivenessChecker(Session& session)
503 return new LivenessChecker(session);