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. */
15 #include <xbt/automaton.h>
16 #include <xbt/dynar.h>
17 #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 xbt_dynar_t acceptance_pairs;
40 /********* Static functions *********/
45 static int MC_automaton_evaluate_label(xbt_automaton_exp_label_t l,
46 xbt_dynar_t atomic_propositions_values)
51 int left_res = MC_automaton_evaluate_label(l->u.or_and.left_exp, atomic_propositions_values);
52 int right_res = MC_automaton_evaluate_label(l->u.or_and.right_exp, atomic_propositions_values);
53 return (left_res || right_res);
56 int left_res = MC_automaton_evaluate_label(l->u.or_and.left_exp, atomic_propositions_values);
57 int right_res = MC_automaton_evaluate_label(l->u.or_and.right_exp, atomic_propositions_values);
58 return (left_res && right_res);
61 int res = MC_automaton_evaluate_label(l->u.exp_not, atomic_propositions_values);
65 unsigned int cursor = 0;
66 xbt_automaton_propositional_symbol_t p = nullptr;
67 xbt_dynar_foreach(simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
68 if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
69 return (int) xbt_dynar_get_as(atomic_propositions_values, cursor, int);
80 static xbt_dynar_t visited_pairs;
82 Pair::Pair() : num(++mc_stats->expanded_pairs),
83 visited_pair_removed(_sg_mc_visited > 0 ? 0 : 1)
87 if (this->visited_pair_removed)
88 MC_state_delete(this->graph_state, 1);
91 void LivenessChecker::showStack(xbt_fifo_t stack)
94 simgrid::mc::Pair* pair;
97 char *req_str = nullptr;
99 for (item = xbt_fifo_get_last_item(stack);
100 item; item = xbt_fifo_get_prev_item(item)) {
101 pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(item);
102 req = MC_state_get_executed_request(pair->graph_state, &value);
103 if (req && req->call != SIMCALL_NONE) {
104 req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::executed);
105 XBT_INFO("%s", req_str);
111 void LivenessChecker::dumpStack(xbt_fifo_t stack)
113 simgrid::mc::Pair* pair;
114 while ((pair = (simgrid::mc::Pair*) xbt_fifo_pop(stack)) != nullptr)
118 simgrid::xbt::unique_ptr<s_xbt_dynar_t> LivenessChecker::getPropositionValues()
120 unsigned int cursor = 0;
121 xbt_automaton_propositional_symbol_t ps = nullptr;
122 simgrid::xbt::unique_ptr<s_xbt_dynar_t> values = simgrid::xbt::unique_ptr<s_xbt_dynar_t>(xbt_dynar_new(sizeof(int), nullptr));
123 xbt_dynar_foreach(simgrid::mc::property_automaton->propositional_symbols, cursor, ps) {
124 int res = xbt_automaton_propositional_symbol_evaluate(ps);
125 xbt_dynar_push_as(values.get(), int, res);
127 return std::move(values);
130 int LivenessChecker::compare(simgrid::mc::VisitedPair* state1, simgrid::mc::VisitedPair* state2)
132 simgrid::mc::Snapshot* s1 = state1->graph_state->system_state;
133 simgrid::mc::Snapshot* s2 = state2->graph_state->system_state;
134 int num1 = state1->num;
135 int num2 = state2->num;
136 return simgrid::mc::snapshot_compare(num1, s1, num2, s2);
139 simgrid::mc::VisitedPair* LivenessChecker::insertAcceptancePair(simgrid::mc::Pair* pair)
141 auto acceptance_pairs = simgrid::xbt::range<simgrid::mc::VisitedPair*>(::acceptance_pairs);
143 std::unique_ptr<simgrid::mc::VisitedPair>(new VisitedPair(
144 pair->num, pair->automaton_state, pair->atomic_propositions.get(),
146 new_pair->acceptance_pair = 1;
148 auto res = std::equal_range(acceptance_pairs.begin(), acceptance_pairs.end(),
149 new_pair, simgrid::mc::DerefAndCompareByNbProcessesAndUsedHeap());
151 if (pair->search_cycle == 1)
152 for (auto i = res.first; i != res.second; ++i) {
153 simgrid::mc::VisitedPair* pair_test = *i;
154 if (xbt_automaton_state_compare(pair_test->automaton_state, new_pair->automaton_state) == 0) {
155 if (xbt_automaton_propositional_symbols_compare_value(
156 pair_test->atomic_propositions.get(),
157 new_pair->atomic_propositions.get()) == 0) {
158 if (this->compare(pair_test, new_pair.get()) == 0) {
159 XBT_INFO("Pair %d already reached (equal to pair %d) !", new_pair->num, pair_test->num);
160 xbt_fifo_shift(mc_stack);
161 if (dot_output != nullptr)
162 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, pair_test->num, initial_global_state->prev_req);
169 auto new_raw_pair = new_pair.release();
171 ::acceptance_pairs, res.first - acceptance_pairs.begin(), &new_raw_pair);
175 void LivenessChecker::removeAcceptancePair(int pair_num)
177 unsigned int cursor = 0;
178 simgrid::mc::VisitedPair* pair_test = nullptr;
181 xbt_dynar_foreach(acceptance_pairs, cursor, pair_test)
182 if (pair_test->num == pair_num) {
187 if(pair_found == 1) {
188 xbt_dynar_remove_at(acceptance_pairs, cursor, &pair_test);
190 pair_test->acceptance_removed = 1;
192 if (_sg_mc_visited == 0 || pair_test->visited_removed == 1)
198 void LivenessChecker::prepare(void)
200 simgrid::mc::Pair* initial_pair = nullptr;
201 mc_model_checker->wait_for_requests();
202 acceptance_pairs = xbt_dynar_new(sizeof(simgrid::mc::VisitedPair*), nullptr);
203 if(_sg_mc_visited > 0)
204 simgrid::mc::visited_pairs = xbt_dynar_new(sizeof(simgrid::mc::VisitedPair*), nullptr);
206 initial_global_state->snapshot = simgrid::mc::take_snapshot(0);
207 initial_global_state->prev_pair = 0;
209 unsigned int cursor = 0;
210 xbt_automaton_state_t automaton_state;
212 xbt_dynar_foreach(simgrid::mc::property_automaton->states, cursor, automaton_state) {
213 if (automaton_state->type == -1) { /* Initial automaton state */
215 initial_pair = new Pair();
216 initial_pair->automaton_state = automaton_state;
217 initial_pair->graph_state = MC_state_new();
218 initial_pair->atomic_propositions = this->getPropositionValues();
219 initial_pair->depth = 1;
221 /* Get enabled processes and insert them in the interleave set of the graph_state */
222 for (auto& p : mc_model_checker->process().simix_processes())
223 if (simgrid::mc::process_is_enabled(&p.copy))
224 MC_state_interleave_process(initial_pair->graph_state, &p.copy);
226 initial_pair->requests = MC_state_interleave_size(initial_pair->graph_state);
227 initial_pair->search_cycle = 0;
229 xbt_fifo_unshift(mc_stack, initial_pair);
235 void LivenessChecker::replay(xbt_fifo_t stack)
237 xbt_fifo_item_t item;
238 simgrid::mc::Pair* pair = nullptr;
239 mc_state_t state = nullptr;
240 smx_simcall_t req = nullptr, saved_req = NULL;
241 int value, depth = 1;
244 XBT_DEBUG("**** Begin Replay ****");
246 /* Intermediate backtracking */
247 if(_sg_mc_checkpoint > 0) {
248 item = xbt_fifo_get_first_item(stack);
249 pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(item);
250 if(pair->graph_state->system_state){
251 simgrid::mc::restore_snapshot(pair->graph_state->system_state);
256 /* Restore the initial state */
257 simgrid::mc::restore_snapshot(initial_global_state->snapshot);
259 /* Traverse the stack from the initial state and re-execute the transitions */
260 for (item = xbt_fifo_get_last_item(stack);
261 item != xbt_fifo_get_first_item(stack);
262 item = xbt_fifo_get_prev_item(item)) {
264 pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(item);
266 state = (mc_state_t) pair->graph_state;
268 if (pair->exploration_started) {
270 saved_req = MC_state_get_executed_request(state, &value);
272 if (saved_req != nullptr) {
273 /* because we got a copy of the executed request, we have to fetch the
274 real one, pointed by the request field of the issuer process */
275 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
276 req = &issuer->simcall;
278 /* Debug information */
279 if (XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)) {
280 req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
281 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
287 simgrid::mc::handle_simcall(req, value);
288 mc_model_checker->wait_for_requests();
291 /* Update statistics */
292 mc_stats->visited_pairs++;
293 mc_stats->executed_transitions++;
299 XBT_DEBUG("**** End Replay ****");
303 * \brief Checks whether a given pair has already been visited by the algorithm.
305 int LivenessChecker::insertVisitedPair(simgrid::mc::VisitedPair* visited_pair, simgrid::mc::Pair* pair)
307 if (_sg_mc_visited == 0)
310 simgrid::mc::VisitedPair* new_visited_pair = nullptr;
311 if (visited_pair == nullptr)
312 new_visited_pair = new simgrid::mc::VisitedPair(
313 pair->num, pair->automaton_state, pair->atomic_propositions.get(),
316 new_visited_pair = visited_pair;
318 auto visited_pairs = simgrid::xbt::range<simgrid::mc::VisitedPair*>(simgrid::mc::visited_pairs);
320 auto range = std::equal_range(visited_pairs.begin(), visited_pairs.end(),
321 new_visited_pair, simgrid::mc::DerefAndCompareByNbProcessesAndUsedHeap());
323 for (auto i = range.first; i != range.second; ++i) {
324 simgrid::mc::VisitedPair* pair_test = *i;
325 std::size_t cursor = i - visited_pairs.begin();
326 if (xbt_automaton_state_compare(pair_test->automaton_state, new_visited_pair->automaton_state) == 0) {
327 if (xbt_automaton_propositional_symbols_compare_value(
328 pair_test->atomic_propositions.get(),
329 new_visited_pair->atomic_propositions.get()) == 0) {
330 if (this->compare(pair_test, new_visited_pair) == 0) {
331 if (pair_test->other_num == -1)
332 new_visited_pair->other_num = pair_test->num;
334 new_visited_pair->other_num = pair_test->other_num;
335 if (dot_output == nullptr)
336 XBT_DEBUG("Pair %d already visited ! (equal to pair %d)", new_visited_pair->num, pair_test->num);
338 XBT_DEBUG("Pair %d already visited ! (equal to pair %d (pair %d in dot_output))", new_visited_pair->num, pair_test->num, new_visited_pair->other_num);
339 xbt_dynar_remove_at(simgrid::mc::visited_pairs, cursor, &pair_test);
340 xbt_dynar_insert_at(simgrid::mc::visited_pairs, cursor, &new_visited_pair);
341 pair_test->visited_removed = 1;
342 if (!pair_test->acceptance_pair
343 || pair_test->acceptance_removed == 1)
345 return new_visited_pair->other_num;
351 xbt_dynar_insert_at(simgrid::mc::visited_pairs, range.first - visited_pairs.begin(), &new_visited_pair);
353 if ((ssize_t) visited_pairs.size() > _sg_mc_visited) {
354 int min2 = mc_stats->expanded_pairs;
355 unsigned int index2 = 0;
356 for (std::size_t i = 0; i != (std::size_t) visited_pairs.size(); ++i) {
357 simgrid::mc::VisitedPair* pair_test = visited_pairs[i];
358 if (!mc_model_checker->is_important_snapshot(*pair_test->graph_state->system_state)
359 && pair_test->num < min2) {
361 min2 = pair_test->num;
364 simgrid::mc::VisitedPair* pair_test = nullptr;
365 xbt_dynar_remove_at(simgrid::mc::visited_pairs, index2, &pair_test);
366 pair_test->visited_removed = 1;
367 if (!pair_test->acceptance_pair || pair_test->acceptance_removed)
374 LivenessChecker::LivenessChecker(Session& session) : Checker(session)
378 LivenessChecker::~LivenessChecker()
382 void LivenessChecker::showAcceptanceCycle(std::size_t depth)
384 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
385 XBT_INFO("| ACCEPTANCE CYCLE |");
386 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
387 XBT_INFO("Counter-example that violates formula :");
388 MC_record_dump_path(mc_stack);
389 this->showStack(mc_stack);
390 this->dumpStack(mc_stack);
391 MC_print_statistics(mc_stats);
392 XBT_INFO("Counter-example depth : %zd", depth);
395 int LivenessChecker::main(void)
397 simgrid::mc::Pair* current_pair = nullptr;
398 int value, res, visited_num = -1;
399 smx_simcall_t req = nullptr;
400 xbt_automaton_transition_t transition_succ = nullptr;
402 simgrid::mc::Pair* next_pair = nullptr;
403 simgrid::xbt::unique_ptr<s_xbt_dynar_t> prop_values;
404 simgrid::mc::VisitedPair* reached_pair = nullptr;
406 while(xbt_fifo_size(mc_stack) > 0){
408 /* Get current pair */
409 current_pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack));
411 /* Update current state in buchi automaton */
412 simgrid::mc::property_automaton->current_state = current_pair->automaton_state;
414 XBT_DEBUG("********************* ( Depth = %d, search_cycle = %d, interleave size = %d, pair_num = %d, requests = %d)",
415 current_pair->depth, current_pair->search_cycle,
416 MC_state_interleave_size(current_pair->graph_state), current_pair->num,
417 current_pair->requests);
419 if (current_pair->requests > 0) {
421 if (current_pair->automaton_state->type == 1 && current_pair->exploration_started == 0) {
422 /* If new acceptance pair, return new pair */
423 if ((reached_pair = this->insertAcceptancePair(current_pair)) == nullptr) {
424 this->showAcceptanceCycle(current_pair->depth);
425 return SIMGRID_MC_EXIT_LIVENESS;
429 /* Pair already visited ? stop the exploration on the current path */
430 if ((current_pair->exploration_started == 0)
431 && (visited_num = this->insertVisitedPair(
432 reached_pair, current_pair)) != -1) {
434 if (dot_output != nullptr){
435 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, visited_num, initial_global_state->prev_req);
439 XBT_DEBUG("Pair already visited (equal to pair %d), exploration on the current path stopped.", visited_num);
440 current_pair->requests = 0;
445 req = MC_state_get_request(current_pair->graph_state, &value);
447 if (dot_output != nullptr) {
448 if (initial_global_state->prev_pair != 0 && initial_global_state->prev_pair != current_pair->num) {
449 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, current_pair->num, initial_global_state->prev_req);
450 xbt_free(initial_global_state->prev_req);
452 initial_global_state->prev_pair = current_pair->num;
453 initial_global_state->prev_req = simgrid::mc::request_get_dot_output(req, value);
454 if (current_pair->search_cycle)
455 fprintf(dot_output, "%d [shape=doublecircle];\n", current_pair->num);
459 char* req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
460 XBT_DEBUG("Execute: %s", req_str);
463 /* Set request as executed */
464 MC_state_set_executed_request(current_pair->graph_state, req, value);
466 /* Update mc_stats */
467 mc_stats->executed_transitions++;
468 if(current_pair->exploration_started == 0)
469 mc_stats->visited_pairs++;
471 /* Answer the request */
472 simgrid::mc::handle_simcall(req, value);
474 /* Wait for requests (schedules processes) */
475 mc_model_checker->wait_for_requests();
477 current_pair->requests--;
478 current_pair->exploration_started = 1;
480 /* Get values of atomic propositions (variables used in the property formula) */
481 prop_values = this->getPropositionValues();
483 /* Evaluate enabled/true transitions in automaton according to atomic propositions values and create new pairs */
484 cursor = xbt_dynar_length(current_pair->automaton_state->out) - 1;
485 while (cursor >= 0) {
486 transition_succ = (xbt_automaton_transition_t)xbt_dynar_get_as(current_pair->automaton_state->out, cursor, xbt_automaton_transition_t);
487 res = MC_automaton_evaluate_label(transition_succ->label, prop_values.get());
488 if (res == 1 || res == 2) { /* 1 = True transition (always enabled), 2 = enabled transition according to atomic prop values */
489 next_pair = new Pair();
490 next_pair->graph_state = MC_state_new();
491 next_pair->automaton_state = transition_succ->dst;
492 next_pair->atomic_propositions = this->getPropositionValues();
493 next_pair->depth = current_pair->depth + 1;
494 /* Get enabled processes and insert them in the interleave set of the next graph_state */
495 for (auto& p : mc_model_checker->process().simix_processes())
496 if (simgrid::mc::process_is_enabled(&p.copy))
497 MC_state_interleave_process(next_pair->graph_state, &p.copy);
499 next_pair->requests = MC_state_interleave_size(next_pair->graph_state);
501 /* FIXME : get search_cycle value for each acceptant state */
502 if (next_pair->automaton_state->type == 1 || current_pair->search_cycle)
503 next_pair->search_cycle = 1;
505 /* Add new pair to the exploration stack */
506 xbt_fifo_unshift(mc_stack, next_pair);
512 } /* End of visited_pair test */
517 if(visited_num == -1)
518 XBT_DEBUG("No more request to execute. Looking for backtracking point.");
522 /* Traverse the stack backwards until a pair with a non empty interleave
523 set is found, deleting all the pairs that have it empty in the way. */
524 while ((current_pair = (simgrid::mc::Pair*) xbt_fifo_shift(mc_stack)) != nullptr) {
525 if (current_pair->requests > 0) {
526 /* We found a backtracking point */
527 XBT_DEBUG("Backtracking to depth %d", current_pair->depth);
528 xbt_fifo_unshift(mc_stack, current_pair);
529 this->replay(mc_stack);
530 XBT_DEBUG("Backtracking done");
534 XBT_DEBUG("Delete pair %d at depth %d", current_pair->num, current_pair->depth);
535 if (current_pair->automaton_state->type == 1)
536 this->removeAcceptancePair(current_pair->num);
541 } /* End of if (current_pair->requests > 0) else ... */
543 } /* End of while(xbt_fifo_size(mc_stack) > 0) */
545 XBT_INFO("No property violation found.");
546 MC_print_statistics(mc_stats);
547 return SIMGRID_MC_EXIT_SUCCESS;
550 int LivenessChecker::run()
552 XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
553 MC_automaton_load(_sg_mc_property_file);
554 mc_model_checker->wait_for_requests();
556 XBT_DEBUG("Starting the liveness algorithm");
559 /* Create exploration stack */
560 mc_stack = xbt_fifo_new();
562 /* Create the initial state */
563 initial_global_state = xbt_new0(s_mc_global_t, 1);