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"
33 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_liveness, mc,
34 "Logging specific to algorithms for liveness properties verification");
36 /********* Static functions *********/
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)
45 simgrid::mc::Process* 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(
51 process->get_heap()->heaplimit,
52 process->get_malloc_info());
55 mc_model_checker->process().simix_processes().size();
57 this->automaton_state = automaton_state;
60 this->atomic_propositions = atomic_propositions;
63 VisitedPair::~VisitedPair()
67 static int MC_automaton_evaluate_label(
68 xbt_automaton_exp_label_t l,
69 std::vector<int> const& atomic_propositions_values)
73 case xbt_automaton_exp_label::AUT_OR:{
74 int left_res = MC_automaton_evaluate_label(
75 l->u.or_and.left_exp, atomic_propositions_values);
76 int right_res = MC_automaton_evaluate_label(
77 l->u.or_and.right_exp, atomic_propositions_values);
78 return (left_res || right_res);
80 case xbt_automaton_exp_label::AUT_AND:{
81 int left_res = MC_automaton_evaluate_label(
82 l->u.or_and.left_exp, atomic_propositions_values);
83 int right_res = MC_automaton_evaluate_label(
84 l->u.or_and.right_exp, atomic_propositions_values);
85 return (left_res && right_res);
87 case xbt_automaton_exp_label::AUT_NOT:{
88 int res = MC_automaton_evaluate_label(
89 l->u.exp_not, atomic_propositions_values);
92 case xbt_automaton_exp_label::AUT_PREDICAT:{
93 unsigned int cursor = 0;
94 xbt_automaton_propositional_symbol_t p = nullptr;
95 xbt_dynar_foreach(simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
96 if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
97 return atomic_propositions_values[cursor];
101 case xbt_automaton_exp_label::AUT_ONE:
108 Pair::Pair() : num(++mc_stats->expanded_pairs)
113 std::vector<int> LivenessChecker::getPropositionValues()
115 std::vector<int> values;
116 unsigned int cursor = 0;
117 xbt_automaton_propositional_symbol_t ps = nullptr;
118 xbt_dynar_foreach(simgrid::mc::property_automaton->propositional_symbols, cursor, ps)
119 values.push_back(xbt_automaton_propositional_symbol_evaluate(ps));
123 int LivenessChecker::compare(simgrid::mc::VisitedPair* state1, simgrid::mc::VisitedPair* state2)
125 simgrid::mc::Snapshot* s1 = state1->graph_state->system_state.get();
126 simgrid::mc::Snapshot* s2 = state2->graph_state->system_state.get();
127 int num1 = state1->num;
128 int num2 = state2->num;
129 return simgrid::mc::snapshot_compare(num1, s1, num2, s2);
132 std::shared_ptr<VisitedPair> LivenessChecker::insertAcceptancePair(simgrid::mc::Pair* pair)
134 std::shared_ptr<VisitedPair> new_pair = std::make_shared<VisitedPair>(
135 pair->num, pair->automaton_state, pair->atomic_propositions,
138 auto res = std::equal_range(acceptancePairs_.begin(), acceptancePairs_.end(),
139 new_pair.get(), simgrid::mc::DerefAndCompareByNbProcessesAndUsedHeap());
141 if (pair->search_cycle) for (auto i = res.first; i != res.second; ++i) {
142 std::shared_ptr<simgrid::mc::VisitedPair> const& pair_test = *i;
143 if (xbt_automaton_state_compare(
144 pair_test->automaton_state, new_pair->automaton_state) != 0
145 || pair_test->atomic_propositions != new_pair->atomic_propositions
146 || this->compare(pair_test.get(), new_pair.get()) != 0)
148 XBT_INFO("Pair %d already reached (equal to pair %d) !",
149 new_pair->num, pair_test->num);
150 livenessStack_.pop_back();
151 if (dot_output != nullptr)
152 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n",
153 initial_global_state->prev_pair, pair_test->num,
154 initial_global_state->prev_req);
158 acceptancePairs_.insert(res.first, new_pair);
162 void LivenessChecker::removeAcceptancePair(int pair_num)
164 for (auto i = acceptancePairs_.begin(); i != acceptancePairs_.end(); ++i)
165 if ((*i)->num == pair_num) {
166 acceptancePairs_.erase(i);
171 void LivenessChecker::prepare(void)
173 simgrid::mc::Pair* initial_pair = nullptr;
174 mc_model_checker->wait_for_requests();
176 initial_global_state->snapshot = simgrid::mc::take_snapshot(0);
177 initial_global_state->prev_pair = 0;
179 unsigned int cursor = 0;
180 xbt_automaton_state_t automaton_state;
182 xbt_dynar_foreach(simgrid::mc::property_automaton->states, cursor, automaton_state) {
183 if (automaton_state->type == -1) { /* Initial automaton state */
185 initial_pair = new Pair();
186 initial_pair->automaton_state = automaton_state;
187 initial_pair->graph_state = std::shared_ptr<simgrid::mc::State>(MC_state_new());
188 initial_pair->atomic_propositions = this->getPropositionValues();
189 initial_pair->depth = 1;
191 /* Get enabled processes and insert them in the interleave set of the graph_state */
192 for (auto& p : mc_model_checker->process().simix_processes())
193 if (simgrid::mc::process_is_enabled(&p.copy))
194 MC_state_interleave_process(initial_pair->graph_state.get(), &p.copy);
196 initial_pair->requests = MC_state_interleave_size(initial_pair->graph_state.get());
197 initial_pair->search_cycle = false;
199 livenessStack_.push_back(initial_pair);
205 void LivenessChecker::replay()
207 smx_simcall_t req = nullptr, saved_req = NULL;
208 int value, depth = 1;
211 XBT_DEBUG("**** Begin Replay ****");
213 /* Intermediate backtracking */
214 if(_sg_mc_checkpoint > 0) {
215 simgrid::mc::Pair* pair = livenessStack_.back();
216 if(pair->graph_state->system_state){
217 simgrid::mc::restore_snapshot(pair->graph_state->system_state);
222 /* Restore the initial state */
223 simgrid::mc::restore_snapshot(initial_global_state->snapshot);
225 /* Traverse the stack from the initial state and re-execute the transitions */
226 for (simgrid::mc::Pair* pair : livenessStack_) {
227 if (pair == livenessStack_.back())
230 std::shared_ptr<State> state = pair->graph_state;
232 if (pair->exploration_started) {
234 saved_req = MC_state_get_executed_request(state.get(), &value);
236 if (saved_req != nullptr) {
237 /* because we got a copy of the executed request, we have to fetch the
238 real one, pointed by the request field of the issuer process */
239 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
240 req = &issuer->simcall;
242 /* Debug information */
243 if (XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)) {
244 req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
245 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state.get());
251 simgrid::mc::handle_simcall(req, value);
252 mc_model_checker->wait_for_requests();
255 /* Update statistics */
256 mc_stats->visited_pairs++;
257 mc_stats->executed_transitions++;
263 XBT_DEBUG("**** End Replay ****");
267 * \brief Checks whether a given pair has already been visited by the algorithm.
269 int LivenessChecker::insertVisitedPair(std::shared_ptr<VisitedPair> visited_pair, simgrid::mc::Pair* pair)
271 if (_sg_mc_visited == 0)
274 if (visited_pair == nullptr)
275 visited_pair = std::make_shared<VisitedPair>(
276 pair->num, pair->automaton_state, pair->atomic_propositions,
279 auto range = std::equal_range(visitedPairs_.begin(), visitedPairs_.end(),
280 visited_pair.get(), simgrid::mc::DerefAndCompareByNbProcessesAndUsedHeap());
282 for (auto i = range.first; i != range.second; ++i) {
283 VisitedPair* pair_test = i->get();
284 if (xbt_automaton_state_compare(
285 pair_test->automaton_state, visited_pair->automaton_state) != 0
286 || pair_test->atomic_propositions != visited_pair->atomic_propositions
287 || this->compare(pair_test, visited_pair.get()) != 0)
289 if (pair_test->other_num == -1)
290 visited_pair->other_num = pair_test->num;
292 visited_pair->other_num = pair_test->other_num;
293 if (dot_output == nullptr)
294 XBT_DEBUG("Pair %d already visited ! (equal to pair %d)",
295 visited_pair->num, pair_test->num);
297 XBT_DEBUG("Pair %d already visited ! (equal to pair %d (pair %d in dot_output))",
298 visited_pair->num, pair_test->num, visited_pair->other_num);
299 (*i) = std::move(visited_pair);
300 return (*i)->other_num;
303 visitedPairs_.insert(range.first, std::move(visited_pair));
305 if (visitedPairs_.size() > (std::size_t) _sg_mc_visited) {
306 int min2 = mc_stats->expanded_pairs;
308 std::list<std::shared_ptr<VisitedPair>>::iterator index2;
309 for (auto i = visitedPairs_.begin(); i != visitedPairs_.end(); ++i) {
310 if ((*i)->num < min2) {
316 visitedPairs_.erase(index2);
322 LivenessChecker::LivenessChecker(Session& session) : Checker(session)
326 LivenessChecker::~LivenessChecker()
330 RecordTrace LivenessChecker::getRecordTrace() // override
333 for (simgrid::mc::Pair* pair : livenessStack_) {
335 smx_simcall_t req = MC_state_get_executed_request(pair->graph_state.get(), &value);
336 if (req && req->call != SIMCALL_NONE) {
337 smx_process_t issuer = MC_smx_simcall_get_issuer(req);
338 const int pid = issuer->pid;
340 // Serialization the (pid, value) pair:
341 res.push_back(RecordTraceElement(pid, value));
347 void LivenessChecker::showAcceptanceCycle(std::size_t depth)
349 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
350 XBT_INFO("| ACCEPTANCE CYCLE |");
351 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
352 XBT_INFO("Counter-example that violates formula :");
353 simgrid::mc::dumpRecordPath();
354 for (auto& s : this->getTextualTrace())
355 XBT_INFO("%s", s.c_str());
356 MC_print_statistics(mc_stats);
357 XBT_INFO("Counter-example depth : %zd", depth);
360 std::vector<std::string> LivenessChecker::getTextualTrace() // override
362 std::vector<std::string> trace;
363 for (simgrid::mc::Pair* pair : livenessStack_) {
365 smx_simcall_t req = MC_state_get_executed_request(pair->graph_state.get(), &value);
366 if (req && req->call != SIMCALL_NONE) {
367 char* req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::executed);
368 trace.push_back(std::string(req_str));
375 int LivenessChecker::main(void)
377 simgrid::mc::Pair* current_pair = nullptr;
378 int value, res, visited_num = -1;
379 smx_simcall_t req = nullptr;
380 xbt_automaton_transition_t transition_succ = nullptr;
382 std::shared_ptr<VisitedPair> reached_pair = nullptr;
384 while (!livenessStack_.empty()){
386 /* Get current pair */
387 current_pair = livenessStack_.back();
389 /* Update current state in buchi automaton */
390 simgrid::mc::property_automaton->current_state = current_pair->automaton_state;
392 XBT_DEBUG("********************* ( Depth = %d, search_cycle = %d, interleave size = %d, pair_num = %d, requests = %d)",
393 current_pair->depth, current_pair->search_cycle,
394 MC_state_interleave_size(current_pair->graph_state.get()), current_pair->num,
395 current_pair->requests);
397 if (current_pair->requests > 0) {
399 if (current_pair->automaton_state->type == 1 && !current_pair->exploration_started
400 && (reached_pair = this->insertAcceptancePair(current_pair)) == nullptr) {
401 this->showAcceptanceCycle(current_pair->depth);
402 return SIMGRID_MC_EXIT_LIVENESS;
405 /* Pair already visited ? stop the exploration on the current path */
406 if ((!current_pair->exploration_started)
407 && (visited_num = this->insertVisitedPair(
408 reached_pair, current_pair)) != -1) {
410 if (dot_output != nullptr){
411 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, visited_num, initial_global_state->prev_req);
415 XBT_DEBUG("Pair already visited (equal to pair %d), exploration on the current path stopped.", visited_num);
416 current_pair->requests = 0;
421 req = MC_state_get_request(current_pair->graph_state.get(), &value);
423 if (dot_output != nullptr) {
424 if (initial_global_state->prev_pair != 0 && initial_global_state->prev_pair != current_pair->num) {
425 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, current_pair->num, initial_global_state->prev_req);
426 xbt_free(initial_global_state->prev_req);
428 initial_global_state->prev_pair = current_pair->num;
429 initial_global_state->prev_req = simgrid::mc::request_get_dot_output(req, value);
430 if (current_pair->search_cycle)
431 fprintf(dot_output, "%d [shape=doublecircle];\n", current_pair->num);
435 char* req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
436 XBT_DEBUG("Execute: %s", req_str);
439 /* Set request as executed */
440 MC_state_set_executed_request(current_pair->graph_state.get(), req, value);
442 /* Update mc_stats */
443 mc_stats->executed_transitions++;
444 if (!current_pair->exploration_started)
445 mc_stats->visited_pairs++;
447 /* Answer the request */
448 simgrid::mc::handle_simcall(req, value);
450 /* Wait for requests (schedules processes) */
451 mc_model_checker->wait_for_requests();
453 current_pair->requests--;
454 current_pair->exploration_started = true;
456 /* Get values of atomic propositions (variables used in the property formula) */
457 std::vector<int> prop_values = this->getPropositionValues();
459 /* Evaluate enabled/true transitions in automaton according to atomic propositions values and create new pairs */
460 cursor = xbt_dynar_length(current_pair->automaton_state->out) - 1;
461 while (cursor >= 0) {
462 transition_succ = (xbt_automaton_transition_t)xbt_dynar_get_as(current_pair->automaton_state->out, cursor, xbt_automaton_transition_t);
463 res = MC_automaton_evaluate_label(transition_succ->label, prop_values);
464 if (res == 1 || res == 2) { /* 1 = True transition (always enabled), 2 = enabled transition according to atomic prop values */
465 Pair* next_pair = new Pair();
466 next_pair->graph_state = std::shared_ptr<simgrid::mc::State>(MC_state_new());
467 next_pair->automaton_state = transition_succ->dst;
468 next_pair->atomic_propositions = this->getPropositionValues();
469 next_pair->depth = current_pair->depth + 1;
470 /* Get enabled processes and insert them in the interleave set of the next graph_state */
471 for (auto& p : mc_model_checker->process().simix_processes())
472 if (simgrid::mc::process_is_enabled(&p.copy))
473 MC_state_interleave_process(next_pair->graph_state.get(), &p.copy);
475 next_pair->requests = MC_state_interleave_size(next_pair->graph_state.get());
477 /* FIXME : get search_cycle value for each acceptant state */
478 if (next_pair->automaton_state->type == 1 || current_pair->search_cycle)
479 next_pair->search_cycle = true;
481 /* Add new pair to the exploration stack */
482 livenessStack_.push_back(next_pair);
488 } /* End of visited_pair test */
493 if(visited_num == -1)
494 XBT_DEBUG("No more request to execute. Looking for backtracking point.");
496 /* Traverse the stack backwards until a pair with a non empty interleave
497 set is found, deleting all the pairs that have it empty in the way. */
498 while (!livenessStack_.empty()) {
499 current_pair = livenessStack_.back();
500 livenessStack_.pop_back();
501 if (current_pair->requests > 0) {
502 /* We found a backtracking point */
503 XBT_DEBUG("Backtracking to depth %d", current_pair->depth);
504 livenessStack_.push_back(current_pair);
506 XBT_DEBUG("Backtracking done");
510 XBT_DEBUG("Delete pair %d at depth %d", current_pair->num, current_pair->depth);
511 if (current_pair->automaton_state->type == 1)
512 this->removeAcceptancePair(current_pair->num);
517 } /* End of if (current_pair->requests > 0) else ... */
521 XBT_INFO("No property violation found.");
522 MC_print_statistics(mc_stats);
523 return SIMGRID_MC_EXIT_SUCCESS;
526 int LivenessChecker::run()
528 XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
529 MC_automaton_load(_sg_mc_property_file);
530 mc_model_checker->wait_for_requests();
532 XBT_DEBUG("Starting the liveness algorithm");
534 /* Create the initial state */
535 simgrid::mc::initial_global_state = std::unique_ptr<s_mc_global_t>(new s_mc_global_t());
538 int res = this->main();
539 simgrid::mc::initial_global_state = nullptr;
544 Checker* createLivenessChecker(Session& session)
546 return new LivenessChecker(session);