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->acceptance_pair = 0;
61 this->atomic_propositions = atomic_propositions;
64 VisitedPair::~VisitedPair()
68 static int MC_automaton_evaluate_label(
69 xbt_automaton_exp_label_t l,
70 std::vector<int> const& atomic_propositions_values)
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);
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);
88 case xbt_automaton_exp_label::AUT_NOT:{
89 int res = MC_automaton_evaluate_label(
90 l->u.exp_not, atomic_propositions_values);
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];
102 case xbt_automaton_exp_label::AUT_ONE:
109 Pair::Pair() : num(++mc_stats->expanded_pairs)
114 std::vector<int> LivenessChecker::getPropositionValues()
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));
124 int LivenessChecker::compare(simgrid::mc::VisitedPair* state1, simgrid::mc::VisitedPair* state2)
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);
133 std::shared_ptr<VisitedPair> LivenessChecker::insertAcceptancePair(simgrid::mc::Pair* pair)
135 std::shared_ptr<VisitedPair> new_pair = std::make_shared<VisitedPair>(
136 pair->num, pair->automaton_state, pair->atomic_propositions,
138 new_pair->acceptance_pair = 1;
140 auto res = std::equal_range(acceptancePairs_.begin(), acceptancePairs_.end(),
141 new_pair.get(), simgrid::mc::DerefAndCompareByNbProcessesAndUsedHeap());
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);
159 acceptancePairs_.insert(res.first, new_pair);
163 void LivenessChecker::removeAcceptancePair(int pair_num)
165 for (auto i = acceptancePairs_.begin(); i != acceptancePairs_.end(); ++i)
166 if ((*i)->num == pair_num) {
167 acceptancePairs_.erase(i);
172 void LivenessChecker::prepare(void)
174 simgrid::mc::Pair* initial_pair = nullptr;
175 mc_model_checker->wait_for_requests();
177 initial_global_state->snapshot = simgrid::mc::take_snapshot(0);
178 initial_global_state->prev_pair = 0;
180 unsigned int cursor = 0;
181 xbt_automaton_state_t automaton_state;
183 xbt_dynar_foreach(simgrid::mc::property_automaton->states, cursor, automaton_state) {
184 if (automaton_state->type == -1) { /* Initial automaton state */
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;
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);
197 initial_pair->requests = MC_state_interleave_size(initial_pair->graph_state.get());
198 initial_pair->search_cycle = 0;
200 livenessStack_.push_back(initial_pair);
206 void LivenessChecker::replay()
208 smx_simcall_t req = nullptr, saved_req = NULL;
209 int value, depth = 1;
212 XBT_DEBUG("**** Begin Replay ****");
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);
223 /* Restore the initial state */
224 simgrid::mc::restore_snapshot(initial_global_state->snapshot);
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())
231 std::shared_ptr<State> state = pair->graph_state;
233 if (pair->exploration_started) {
235 saved_req = MC_state_get_executed_request(state.get(), &value);
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;
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());
252 simgrid::mc::handle_simcall(req, value);
253 mc_model_checker->wait_for_requests();
256 /* Update statistics */
257 mc_stats->visited_pairs++;
258 mc_stats->executed_transitions++;
264 XBT_DEBUG("**** End Replay ****");
268 * \brief Checks whether a given pair has already been visited by the algorithm.
270 int LivenessChecker::insertVisitedPair(std::shared_ptr<VisitedPair> visited_pair, simgrid::mc::Pair* pair)
272 if (_sg_mc_visited == 0)
275 if (visited_pair == nullptr)
276 visited_pair = std::make_shared<VisitedPair>(
277 pair->num, pair->automaton_state, pair->atomic_propositions,
280 auto range = std::equal_range(visitedPairs_.begin(), visitedPairs_.end(),
281 visited_pair.get(), simgrid::mc::DerefAndCompareByNbProcessesAndUsedHeap());
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;
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);
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;
305 visitedPairs_.insert(range.first, std::move(visited_pair));
307 if (visitedPairs_.size() > (std::size_t) _sg_mc_visited) {
308 int min2 = mc_stats->expanded_pairs;
310 std::list<std::shared_ptr<VisitedPair>>::iterator index2;
311 for (auto i = visitedPairs_.begin(); i != visitedPairs_.end(); ++i) {
312 if ((*i)->num < min2) {
318 visitedPairs_.erase(index2);
324 LivenessChecker::LivenessChecker(Session& session) : Checker(session)
328 LivenessChecker::~LivenessChecker()
332 RecordTrace LivenessChecker::getRecordTrace() // override
335 for (simgrid::mc::Pair* pair : livenessStack_) {
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;
342 // Serialization the (pid, value) pair:
343 res.push_back(RecordTraceElement(pid, value));
349 void LivenessChecker::showAcceptanceCycle(std::size_t depth)
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);
362 std::vector<std::string> LivenessChecker::getTextualTrace() // override
364 std::vector<std::string> trace;
365 for (simgrid::mc::Pair* pair : livenessStack_) {
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));
377 int LivenessChecker::main(void)
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;
384 std::shared_ptr<VisitedPair> reached_pair = nullptr;
386 while (!livenessStack_.empty()){
388 /* Get current pair */
389 current_pair = livenessStack_.back();
391 /* Update current state in buchi automaton */
392 simgrid::mc::property_automaton->current_state = current_pair->automaton_state;
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);
399 if (current_pair->requests > 0) {
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;
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) {
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);
419 XBT_DEBUG("Pair already visited (equal to pair %d), exploration on the current path stopped.", visited_num);
420 current_pair->requests = 0;
425 req = MC_state_get_request(current_pair->graph_state.get(), &value);
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);
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);
439 char* req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
440 XBT_DEBUG("Execute: %s", req_str);
443 /* Set request as executed */
444 MC_state_set_executed_request(current_pair->graph_state.get(), req, value);
446 /* Update mc_stats */
447 mc_stats->executed_transitions++;
448 if(current_pair->exploration_started == 0)
449 mc_stats->visited_pairs++;
451 /* Answer the request */
452 simgrid::mc::handle_simcall(req, value);
454 /* Wait for requests (schedules processes) */
455 mc_model_checker->wait_for_requests();
457 current_pair->requests--;
458 current_pair->exploration_started = 1;
460 /* Get values of atomic propositions (variables used in the property formula) */
461 std::vector<int> prop_values = this->getPropositionValues();
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);
479 next_pair->requests = MC_state_interleave_size(next_pair->graph_state.get());
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;
485 /* Add new pair to the exploration stack */
486 livenessStack_.push_back(next_pair);
492 } /* End of visited_pair test */
497 if(visited_num == -1)
498 XBT_DEBUG("No more request to execute. Looking for backtracking point.");
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);
510 XBT_DEBUG("Backtracking done");
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);
521 } /* End of if (current_pair->requests > 0) else ... */
525 XBT_INFO("No property violation found.");
526 MC_print_statistics(mc_stats);
527 return SIMGRID_MC_EXIT_SUCCESS;
530 int LivenessChecker::run()
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();
536 XBT_DEBUG("Starting the liveness algorithm");
539 /* Create the initial state */
540 simgrid::mc::initial_global_state = std::unique_ptr<s_mc_global_t>(new s_mc_global_t());
543 int res = this->main();
544 simgrid::mc::initial_global_state = nullptr;
549 Checker* createLivenessChecker(Session& session)
551 return new LivenessChecker(session);