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 /********* Global variables *********/
38 /********* Static functions *********/
43 VisitedPair::VisitedPair(
44 int pair_num, xbt_automaton_state_t automaton_state,
45 std::vector<int> const& atomic_propositions, std::shared_ptr<simgrid::mc::State> graph_state)
47 simgrid::mc::Process* process = &(mc_model_checker->process());
49 this->graph_state = std::move(graph_state);
50 if(this->graph_state->system_state == nullptr)
51 this->graph_state->system_state = simgrid::mc::take_snapshot(pair_num);
52 this->heap_bytes_used = mmalloc_get_bytes_used_remote(
53 process->get_heap()->heaplimit,
54 process->get_malloc_info());
57 mc_model_checker->process().simix_processes().size();
59 this->automaton_state = automaton_state;
62 this->acceptance_pair = 0;
63 this->atomic_propositions = atomic_propositions;
66 VisitedPair::~VisitedPair()
70 static int MC_automaton_evaluate_label(
71 xbt_automaton_exp_label_t l,
72 std::vector<int> const& atomic_propositions_values)
77 int left_res = MC_automaton_evaluate_label(
78 l->u.or_and.left_exp, atomic_propositions_values);
79 int right_res = MC_automaton_evaluate_label(
80 l->u.or_and.right_exp, atomic_propositions_values);
81 return (left_res || right_res);
84 int left_res = MC_automaton_evaluate_label(
85 l->u.or_and.left_exp, atomic_propositions_values);
86 int right_res = MC_automaton_evaluate_label(
87 l->u.or_and.right_exp, atomic_propositions_values);
88 return (left_res && right_res);
91 int res = MC_automaton_evaluate_label(
92 l->u.exp_not, atomic_propositions_values);
96 unsigned int cursor = 0;
97 xbt_automaton_propositional_symbol_t p = nullptr;
98 xbt_dynar_foreach(simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
99 if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
100 return atomic_propositions_values[cursor];
111 Pair::Pair() : num(++mc_stats->expanded_pairs)
116 std::vector<int> LivenessChecker::getPropositionValues()
118 std::vector<int> values;
119 unsigned int cursor = 0;
120 xbt_automaton_propositional_symbol_t ps = nullptr;
121 xbt_dynar_foreach(simgrid::mc::property_automaton->propositional_symbols, cursor, ps)
122 values.push_back(xbt_automaton_propositional_symbol_evaluate(ps));
126 int LivenessChecker::compare(simgrid::mc::VisitedPair* state1, simgrid::mc::VisitedPair* state2)
128 simgrid::mc::Snapshot* s1 = state1->graph_state->system_state.get();
129 simgrid::mc::Snapshot* s2 = state2->graph_state->system_state.get();
130 int num1 = state1->num;
131 int num2 = state2->num;
132 return simgrid::mc::snapshot_compare(num1, s1, num2, s2);
135 std::shared_ptr<VisitedPair> LivenessChecker::insertAcceptancePair(simgrid::mc::Pair* pair)
137 std::shared_ptr<VisitedPair> new_pair = std::make_shared<VisitedPair>(
138 pair->num, pair->automaton_state, pair->atomic_propositions,
140 new_pair->acceptance_pair = 1;
142 auto res = std::equal_range(acceptancePairs_.begin(), acceptancePairs_.end(),
143 new_pair.get(), simgrid::mc::DerefAndCompareByNbProcessesAndUsedHeap());
145 if (pair->search_cycle == 1)
146 for (auto i = res.first; i != res.second; ++i) {
147 std::shared_ptr<simgrid::mc::VisitedPair> const& pair_test = *i;
148 if (xbt_automaton_state_compare(pair_test->automaton_state, new_pair->automaton_state) == 0) {
149 if (pair_test->atomic_propositions == new_pair->atomic_propositions) {
150 if (this->compare(pair_test.get(), new_pair.get()) == 0) {
151 XBT_INFO("Pair %d already reached (equal to pair %d) !", new_pair->num, pair_test->num);
152 livenessStack_.pop_back();
153 if (dot_output != nullptr)
154 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, pair_test->num, initial_global_state->prev_req);
161 acceptancePairs_.insert(res.first, new_pair);
165 void LivenessChecker::removeAcceptancePair(int pair_num)
167 for (auto i = acceptancePairs_.begin(); i != acceptancePairs_.end(); ++i)
168 if ((*i)->num == pair_num) {
169 acceptancePairs_.erase(i);
174 void LivenessChecker::prepare(void)
176 simgrid::mc::Pair* initial_pair = nullptr;
177 mc_model_checker->wait_for_requests();
179 initial_global_state->snapshot = simgrid::mc::take_snapshot(0);
180 initial_global_state->prev_pair = 0;
182 unsigned int cursor = 0;
183 xbt_automaton_state_t automaton_state;
185 xbt_dynar_foreach(simgrid::mc::property_automaton->states, cursor, automaton_state) {
186 if (automaton_state->type == -1) { /* Initial automaton state */
188 initial_pair = new Pair();
189 initial_pair->automaton_state = automaton_state;
190 initial_pair->graph_state = std::shared_ptr<simgrid::mc::State>(MC_state_new());
191 initial_pair->atomic_propositions = this->getPropositionValues();
192 initial_pair->depth = 1;
194 /* Get enabled processes and insert them in the interleave set of the graph_state */
195 for (auto& p : mc_model_checker->process().simix_processes())
196 if (simgrid::mc::process_is_enabled(&p.copy))
197 MC_state_interleave_process(initial_pair->graph_state.get(), &p.copy);
199 initial_pair->requests = MC_state_interleave_size(initial_pair->graph_state.get());
200 initial_pair->search_cycle = 0;
202 livenessStack_.push_back(initial_pair);
208 void LivenessChecker::replay()
210 smx_simcall_t req = nullptr, saved_req = NULL;
211 int value, depth = 1;
214 XBT_DEBUG("**** Begin Replay ****");
216 /* Intermediate backtracking */
217 if(_sg_mc_checkpoint > 0) {
218 simgrid::mc::Pair* pair = livenessStack_.back();
219 if(pair->graph_state->system_state){
220 simgrid::mc::restore_snapshot(pair->graph_state->system_state);
225 /* Restore the initial state */
226 simgrid::mc::restore_snapshot(initial_global_state->snapshot);
228 /* Traverse the stack from the initial state and re-execute the transitions */
229 for (simgrid::mc::Pair* pair : livenessStack_) {
230 if (pair == livenessStack_.back())
233 std::shared_ptr<State> state = pair->graph_state;
235 if (pair->exploration_started) {
237 saved_req = MC_state_get_executed_request(state.get(), &value);
239 if (saved_req != nullptr) {
240 /* because we got a copy of the executed request, we have to fetch the
241 real one, pointed by the request field of the issuer process */
242 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
243 req = &issuer->simcall;
245 /* Debug information */
246 if (XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)) {
247 req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
248 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state.get());
254 simgrid::mc::handle_simcall(req, value);
255 mc_model_checker->wait_for_requests();
258 /* Update statistics */
259 mc_stats->visited_pairs++;
260 mc_stats->executed_transitions++;
266 XBT_DEBUG("**** End Replay ****");
270 * \brief Checks whether a given pair has already been visited by the algorithm.
272 int LivenessChecker::insertVisitedPair(std::shared_ptr<VisitedPair> visited_pair, simgrid::mc::Pair* pair)
274 if (_sg_mc_visited == 0)
277 if (visited_pair == nullptr)
278 visited_pair = std::make_shared<VisitedPair>(
279 pair->num, pair->automaton_state, pair->atomic_propositions,
282 auto range = std::equal_range(visitedPairs_.begin(), visitedPairs_.end(),
283 visited_pair.get(), simgrid::mc::DerefAndCompareByNbProcessesAndUsedHeap());
285 for (auto i = range.first; i != range.second; ++i) {
286 VisitedPair* pair_test = i->get();
287 if (xbt_automaton_state_compare(pair_test->automaton_state, visited_pair->automaton_state) == 0) {
288 if (pair_test->atomic_propositions == visited_pair->atomic_propositions) {
289 if (this->compare(pair_test, visited_pair.get()) == 0) {
290 if (pair_test->other_num == -1)
291 visited_pair->other_num = pair_test->num;
293 visited_pair->other_num = pair_test->other_num;
294 if (dot_output == nullptr)
295 XBT_DEBUG("Pair %d already visited ! (equal to pair %d)",
296 visited_pair->num, pair_test->num);
298 XBT_DEBUG("Pair %d already visited ! (equal to pair %d (pair %d in dot_output))",
299 visited_pair->num, pair_test->num, visited_pair->other_num);
300 (*i) = std::move(visited_pair);
301 return (*i)->other_num;
307 visitedPairs_.insert(range.first, std::move(visited_pair));
309 if (visitedPairs_.size() > (std::size_t) _sg_mc_visited) {
310 int min2 = mc_stats->expanded_pairs;
312 std::list<std::shared_ptr<VisitedPair>>::iterator index2;
313 for (auto i = visitedPairs_.begin(); i != visitedPairs_.end(); ++i) {
314 if ((*i)->num < min2) {
320 visitedPairs_.erase(index2);
326 LivenessChecker::LivenessChecker(Session& session) : Checker(session)
330 LivenessChecker::~LivenessChecker()
334 RecordTrace LivenessChecker::getRecordTrace() // override
337 for (simgrid::mc::Pair* pair : livenessStack_) {
339 smx_simcall_t req = MC_state_get_executed_request(pair->graph_state.get(), &value);
340 if (req && req->call != SIMCALL_NONE) {
341 smx_process_t issuer = MC_smx_simcall_get_issuer(req);
342 const int pid = issuer->pid;
344 // Serialization the (pid, value) pair:
345 res.push_back(RecordTraceElement(pid, value));
351 void LivenessChecker::showAcceptanceCycle(std::size_t depth)
353 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
354 XBT_INFO("| ACCEPTANCE CYCLE |");
355 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
356 XBT_INFO("Counter-example that violates formula :");
357 simgrid::mc::dumpRecordPath();
358 for (auto& s : this->getTextualTrace())
359 XBT_INFO("%s", s.c_str());
360 MC_print_statistics(mc_stats);
361 XBT_INFO("Counter-example depth : %zd", depth);
364 std::vector<std::string> LivenessChecker::getTextualTrace() // override
366 std::vector<std::string> trace;
367 for (simgrid::mc::Pair* pair : livenessStack_) {
369 smx_simcall_t req = MC_state_get_executed_request(pair->graph_state.get(), &value);
370 if (req && req->call != SIMCALL_NONE) {
371 char* req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::executed);
372 trace.push_back(std::string(req_str));
379 int LivenessChecker::main(void)
381 simgrid::mc::Pair* current_pair = nullptr;
382 int value, res, visited_num = -1;
383 smx_simcall_t req = nullptr;
384 xbt_automaton_transition_t transition_succ = nullptr;
386 std::shared_ptr<VisitedPair> reached_pair = nullptr;
388 while (!livenessStack_.empty()){
390 /* Get current pair */
391 current_pair = livenessStack_.back();
393 /* Update current state in buchi automaton */
394 simgrid::mc::property_automaton->current_state = current_pair->automaton_state;
396 XBT_DEBUG("********************* ( Depth = %d, search_cycle = %d, interleave size = %d, pair_num = %d, requests = %d)",
397 current_pair->depth, current_pair->search_cycle,
398 MC_state_interleave_size(current_pair->graph_state.get()), current_pair->num,
399 current_pair->requests);
401 if (current_pair->requests > 0) {
403 if (current_pair->automaton_state->type == 1 && current_pair->exploration_started == 0) {
404 /* If new acceptance pair, return new pair */
405 if ((reached_pair = this->insertAcceptancePair(current_pair)) == nullptr) {
406 this->showAcceptanceCycle(current_pair->depth);
407 return SIMGRID_MC_EXIT_LIVENESS;
411 /* Pair already visited ? stop the exploration on the current path */
412 if ((current_pair->exploration_started == 0)
413 && (visited_num = this->insertVisitedPair(
414 reached_pair, current_pair)) != -1) {
416 if (dot_output != nullptr){
417 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, visited_num, initial_global_state->prev_req);
421 XBT_DEBUG("Pair already visited (equal to pair %d), exploration on the current path stopped.", visited_num);
422 current_pair->requests = 0;
427 req = MC_state_get_request(current_pair->graph_state.get(), &value);
429 if (dot_output != nullptr) {
430 if (initial_global_state->prev_pair != 0 && initial_global_state->prev_pair != current_pair->num) {
431 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, current_pair->num, initial_global_state->prev_req);
432 xbt_free(initial_global_state->prev_req);
434 initial_global_state->prev_pair = current_pair->num;
435 initial_global_state->prev_req = simgrid::mc::request_get_dot_output(req, value);
436 if (current_pair->search_cycle)
437 fprintf(dot_output, "%d [shape=doublecircle];\n", current_pair->num);
441 char* req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
442 XBT_DEBUG("Execute: %s", req_str);
445 /* Set request as executed */
446 MC_state_set_executed_request(current_pair->graph_state.get(), req, value);
448 /* Update mc_stats */
449 mc_stats->executed_transitions++;
450 if(current_pair->exploration_started == 0)
451 mc_stats->visited_pairs++;
453 /* Answer the request */
454 simgrid::mc::handle_simcall(req, value);
456 /* Wait for requests (schedules processes) */
457 mc_model_checker->wait_for_requests();
459 current_pair->requests--;
460 current_pair->exploration_started = 1;
462 /* Get values of atomic propositions (variables used in the property formula) */
463 std::vector<int> prop_values = this->getPropositionValues();
465 /* Evaluate enabled/true transitions in automaton according to atomic propositions values and create new pairs */
466 cursor = xbt_dynar_length(current_pair->automaton_state->out) - 1;
467 while (cursor >= 0) {
468 transition_succ = (xbt_automaton_transition_t)xbt_dynar_get_as(current_pair->automaton_state->out, cursor, xbt_automaton_transition_t);
469 res = MC_automaton_evaluate_label(transition_succ->label, prop_values);
470 if (res == 1 || res == 2) { /* 1 = True transition (always enabled), 2 = enabled transition according to atomic prop values */
471 Pair* next_pair = new Pair();
472 next_pair->graph_state = std::shared_ptr<simgrid::mc::State>(MC_state_new());
473 next_pair->automaton_state = transition_succ->dst;
474 next_pair->atomic_propositions = this->getPropositionValues();
475 next_pair->depth = current_pair->depth + 1;
476 /* Get enabled processes and insert them in the interleave set of the next graph_state */
477 for (auto& p : mc_model_checker->process().simix_processes())
478 if (simgrid::mc::process_is_enabled(&p.copy))
479 MC_state_interleave_process(next_pair->graph_state.get(), &p.copy);
481 next_pair->requests = MC_state_interleave_size(next_pair->graph_state.get());
483 /* FIXME : get search_cycle value for each acceptant state */
484 if (next_pair->automaton_state->type == 1 || current_pair->search_cycle)
485 next_pair->search_cycle = 1;
487 /* Add new pair to the exploration stack */
488 livenessStack_.push_back(next_pair);
494 } /* End of visited_pair test */
499 if(visited_num == -1)
500 XBT_DEBUG("No more request to execute. Looking for backtracking point.");
502 /* Traverse the stack backwards until a pair with a non empty interleave
503 set is found, deleting all the pairs that have it empty in the way. */
504 while (!livenessStack_.empty()) {
505 current_pair = livenessStack_.back();
506 livenessStack_.pop_back();
507 if (current_pair->requests > 0) {
508 /* We found a backtracking point */
509 XBT_DEBUG("Backtracking to depth %d", current_pair->depth);
510 livenessStack_.push_back(current_pair);
512 XBT_DEBUG("Backtracking done");
516 XBT_DEBUG("Delete pair %d at depth %d", current_pair->num, current_pair->depth);
517 if (current_pair->automaton_state->type == 1)
518 this->removeAcceptancePair(current_pair->num);
523 } /* End of if (current_pair->requests > 0) else ... */
527 XBT_INFO("No property violation found.");
528 MC_print_statistics(mc_stats);
529 return SIMGRID_MC_EXIT_SUCCESS;
532 int LivenessChecker::run()
534 XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
535 MC_automaton_load(_sg_mc_property_file);
536 mc_model_checker->wait_for_requests();
538 XBT_DEBUG("Starting the liveness algorithm");
541 /* Create the initial state */
542 simgrid::mc::initial_global_state = std::unique_ptr<s_mc_global_t>(new s_mc_global_t());
545 int res = this->main();
546 simgrid::mc::initial_global_state = nullptr;
551 Checker* createLivenessChecker(Session& session)
553 return new LivenessChecker(session);