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 VisitedPair::VisitedPair(int pair_num, xbt_automaton_state_t automaton_state, xbt_dynar_t atomic_propositions, mc_state_t graph_state)
47 simgrid::mc::Process* process = &(mc_model_checker->process());
49 this->graph_state = 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_removed = 0;
63 this->visited_removed = 0;
64 this->acceptance_pair = 0;
65 this->atomic_propositions = simgrid::xbt::unique_ptr<s_xbt_dynar_t>(
66 xbt_dynar_new(sizeof(int), nullptr));
68 unsigned int cursor = 0;
70 xbt_dynar_foreach(atomic_propositions, cursor, value)
71 xbt_dynar_push_as(this->atomic_propositions.get(), int, value);
74 static int is_exploration_stack_pair(simgrid::mc::VisitedPair* pair){
75 xbt_fifo_item_t item = xbt_fifo_get_first_item(mc_stack);
77 if (((simgrid::mc::Pair*)xbt_fifo_get_item_content(item))->num == pair->num){
78 ((simgrid::mc::Pair*)xbt_fifo_get_item_content(item))->visited_pair_removed = 1;
81 item = xbt_fifo_get_next_item(item);
86 VisitedPair::~VisitedPair()
88 if( !is_exploration_stack_pair(this))
89 MC_state_delete(this->graph_state, 1);
92 static int MC_automaton_evaluate_label(xbt_automaton_exp_label_t l,
93 xbt_dynar_t atomic_propositions_values)
98 int left_res = MC_automaton_evaluate_label(l->u.or_and.left_exp, atomic_propositions_values);
99 int right_res = MC_automaton_evaluate_label(l->u.or_and.right_exp, atomic_propositions_values);
100 return (left_res || right_res);
103 int left_res = MC_automaton_evaluate_label(l->u.or_and.left_exp, atomic_propositions_values);
104 int right_res = MC_automaton_evaluate_label(l->u.or_and.right_exp, atomic_propositions_values);
105 return (left_res && right_res);
108 int res = MC_automaton_evaluate_label(l->u.exp_not, atomic_propositions_values);
112 unsigned int cursor = 0;
113 xbt_automaton_propositional_symbol_t p = nullptr;
114 xbt_dynar_foreach(simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
115 if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
116 return (int) xbt_dynar_get_as(atomic_propositions_values, cursor, int);
127 static xbt_dynar_t visited_pairs;
129 Pair::Pair() : num(++mc_stats->expanded_pairs),
130 visited_pair_removed(_sg_mc_visited > 0 ? 0 : 1)
134 if (this->visited_pair_removed)
135 MC_state_delete(this->graph_state, 1);
138 void LivenessChecker::showStack(xbt_fifo_t stack)
141 simgrid::mc::Pair* pair;
142 xbt_fifo_item_t item;
144 char *req_str = nullptr;
146 for (item = xbt_fifo_get_last_item(stack);
147 item; item = xbt_fifo_get_prev_item(item)) {
148 pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(item);
149 req = MC_state_get_executed_request(pair->graph_state, &value);
150 if (req && req->call != SIMCALL_NONE) {
151 req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::executed);
152 XBT_INFO("%s", req_str);
158 void LivenessChecker::dumpStack(xbt_fifo_t stack)
160 simgrid::mc::Pair* pair;
161 while ((pair = (simgrid::mc::Pair*) xbt_fifo_pop(stack)) != nullptr)
165 simgrid::xbt::unique_ptr<s_xbt_dynar_t> LivenessChecker::getPropositionValues()
167 unsigned int cursor = 0;
168 xbt_automaton_propositional_symbol_t ps = nullptr;
169 simgrid::xbt::unique_ptr<s_xbt_dynar_t> values = simgrid::xbt::unique_ptr<s_xbt_dynar_t>(xbt_dynar_new(sizeof(int), nullptr));
170 xbt_dynar_foreach(simgrid::mc::property_automaton->propositional_symbols, cursor, ps) {
171 int res = xbt_automaton_propositional_symbol_evaluate(ps);
172 xbt_dynar_push_as(values.get(), int, res);
174 return std::move(values);
177 int LivenessChecker::compare(simgrid::mc::VisitedPair* state1, simgrid::mc::VisitedPair* state2)
179 simgrid::mc::Snapshot* s1 = state1->graph_state->system_state;
180 simgrid::mc::Snapshot* s2 = state2->graph_state->system_state;
181 int num1 = state1->num;
182 int num2 = state2->num;
183 return simgrid::mc::snapshot_compare(num1, s1, num2, s2);
186 simgrid::mc::VisitedPair* LivenessChecker::insertAcceptancePair(simgrid::mc::Pair* pair)
188 auto acceptance_pairs = simgrid::xbt::range<simgrid::mc::VisitedPair*>(::acceptance_pairs);
190 std::unique_ptr<simgrid::mc::VisitedPair>(new VisitedPair(
191 pair->num, pair->automaton_state, pair->atomic_propositions.get(),
193 new_pair->acceptance_pair = 1;
195 auto res = std::equal_range(acceptance_pairs.begin(), acceptance_pairs.end(),
196 new_pair, simgrid::mc::DerefAndCompareByNbProcessesAndUsedHeap());
198 if (pair->search_cycle == 1)
199 for (auto i = res.first; i != res.second; ++i) {
200 simgrid::mc::VisitedPair* pair_test = *i;
201 if (xbt_automaton_state_compare(pair_test->automaton_state, new_pair->automaton_state) == 0) {
202 if (xbt_automaton_propositional_symbols_compare_value(
203 pair_test->atomic_propositions.get(),
204 new_pair->atomic_propositions.get()) == 0) {
205 if (this->compare(pair_test, new_pair.get()) == 0) {
206 XBT_INFO("Pair %d already reached (equal to pair %d) !", new_pair->num, pair_test->num);
207 xbt_fifo_shift(mc_stack);
208 if (dot_output != nullptr)
209 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, pair_test->num, initial_global_state->prev_req);
216 auto new_raw_pair = new_pair.release();
218 ::acceptance_pairs, res.first - acceptance_pairs.begin(), &new_raw_pair);
222 void LivenessChecker::removeAcceptancePair(int pair_num)
224 unsigned int cursor = 0;
225 simgrid::mc::VisitedPair* pair_test = nullptr;
228 xbt_dynar_foreach(acceptance_pairs, cursor, pair_test)
229 if (pair_test->num == pair_num) {
234 if(pair_found == 1) {
235 xbt_dynar_remove_at(acceptance_pairs, cursor, &pair_test);
237 pair_test->acceptance_removed = 1;
239 if (_sg_mc_visited == 0 || pair_test->visited_removed == 1)
245 void LivenessChecker::prepare(void)
247 simgrid::mc::Pair* initial_pair = nullptr;
248 mc_model_checker->wait_for_requests();
249 acceptance_pairs = xbt_dynar_new(sizeof(simgrid::mc::VisitedPair*), nullptr);
250 if(_sg_mc_visited > 0)
251 simgrid::mc::visited_pairs = xbt_dynar_new(sizeof(simgrid::mc::VisitedPair*), nullptr);
253 initial_global_state->snapshot = simgrid::mc::take_snapshot(0);
254 initial_global_state->prev_pair = 0;
256 unsigned int cursor = 0;
257 xbt_automaton_state_t automaton_state;
259 xbt_dynar_foreach(simgrid::mc::property_automaton->states, cursor, automaton_state) {
260 if (automaton_state->type == -1) { /* Initial automaton state */
262 initial_pair = new Pair();
263 initial_pair->automaton_state = automaton_state;
264 initial_pair->graph_state = MC_state_new();
265 initial_pair->atomic_propositions = this->getPropositionValues();
266 initial_pair->depth = 1;
268 /* Get enabled processes and insert them in the interleave set of the graph_state */
269 for (auto& p : mc_model_checker->process().simix_processes())
270 if (simgrid::mc::process_is_enabled(&p.copy))
271 MC_state_interleave_process(initial_pair->graph_state, &p.copy);
273 initial_pair->requests = MC_state_interleave_size(initial_pair->graph_state);
274 initial_pair->search_cycle = 0;
276 xbt_fifo_unshift(mc_stack, initial_pair);
282 void LivenessChecker::replay(xbt_fifo_t stack)
284 xbt_fifo_item_t item;
285 simgrid::mc::Pair* pair = nullptr;
286 mc_state_t state = nullptr;
287 smx_simcall_t req = nullptr, saved_req = NULL;
288 int value, depth = 1;
291 XBT_DEBUG("**** Begin Replay ****");
293 /* Intermediate backtracking */
294 if(_sg_mc_checkpoint > 0) {
295 item = xbt_fifo_get_first_item(stack);
296 pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(item);
297 if(pair->graph_state->system_state){
298 simgrid::mc::restore_snapshot(pair->graph_state->system_state);
303 /* Restore the initial state */
304 simgrid::mc::restore_snapshot(initial_global_state->snapshot);
306 /* Traverse the stack from the initial state and re-execute the transitions */
307 for (item = xbt_fifo_get_last_item(stack);
308 item != xbt_fifo_get_first_item(stack);
309 item = xbt_fifo_get_prev_item(item)) {
311 pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(item);
313 state = (mc_state_t) pair->graph_state;
315 if (pair->exploration_started) {
317 saved_req = MC_state_get_executed_request(state, &value);
319 if (saved_req != nullptr) {
320 /* because we got a copy of the executed request, we have to fetch the
321 real one, pointed by the request field of the issuer process */
322 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
323 req = &issuer->simcall;
325 /* Debug information */
326 if (XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)) {
327 req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
328 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
334 simgrid::mc::handle_simcall(req, value);
335 mc_model_checker->wait_for_requests();
338 /* Update statistics */
339 mc_stats->visited_pairs++;
340 mc_stats->executed_transitions++;
346 XBT_DEBUG("**** End Replay ****");
350 * \brief Checks whether a given pair has already been visited by the algorithm.
352 int LivenessChecker::insertVisitedPair(simgrid::mc::VisitedPair* visited_pair, simgrid::mc::Pair* pair)
354 if (_sg_mc_visited == 0)
357 simgrid::mc::VisitedPair* new_visited_pair = nullptr;
358 if (visited_pair == nullptr)
359 new_visited_pair = new simgrid::mc::VisitedPair(
360 pair->num, pair->automaton_state, pair->atomic_propositions.get(),
363 new_visited_pair = visited_pair;
365 auto visited_pairs = simgrid::xbt::range<simgrid::mc::VisitedPair*>(simgrid::mc::visited_pairs);
367 auto range = std::equal_range(visited_pairs.begin(), visited_pairs.end(),
368 new_visited_pair, simgrid::mc::DerefAndCompareByNbProcessesAndUsedHeap());
370 for (auto i = range.first; i != range.second; ++i) {
371 simgrid::mc::VisitedPair* pair_test = *i;
372 std::size_t cursor = i - visited_pairs.begin();
373 if (xbt_automaton_state_compare(pair_test->automaton_state, new_visited_pair->automaton_state) == 0) {
374 if (xbt_automaton_propositional_symbols_compare_value(
375 pair_test->atomic_propositions.get(),
376 new_visited_pair->atomic_propositions.get()) == 0) {
377 if (this->compare(pair_test, new_visited_pair) == 0) {
378 if (pair_test->other_num == -1)
379 new_visited_pair->other_num = pair_test->num;
381 new_visited_pair->other_num = pair_test->other_num;
382 if (dot_output == nullptr)
383 XBT_DEBUG("Pair %d already visited ! (equal to pair %d)", new_visited_pair->num, pair_test->num);
385 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);
386 xbt_dynar_remove_at(simgrid::mc::visited_pairs, cursor, &pair_test);
387 xbt_dynar_insert_at(simgrid::mc::visited_pairs, cursor, &new_visited_pair);
388 pair_test->visited_removed = 1;
389 if (!pair_test->acceptance_pair
390 || pair_test->acceptance_removed == 1)
392 return new_visited_pair->other_num;
398 xbt_dynar_insert_at(simgrid::mc::visited_pairs, range.first - visited_pairs.begin(), &new_visited_pair);
400 if ((ssize_t) visited_pairs.size() > _sg_mc_visited) {
401 int min2 = mc_stats->expanded_pairs;
402 unsigned int index2 = 0;
403 for (std::size_t i = 0; i != (std::size_t) visited_pairs.size(); ++i) {
404 simgrid::mc::VisitedPair* pair_test = visited_pairs[i];
405 if (!mc_model_checker->is_important_snapshot(*pair_test->graph_state->system_state)
406 && pair_test->num < min2) {
408 min2 = pair_test->num;
411 simgrid::mc::VisitedPair* pair_test = nullptr;
412 xbt_dynar_remove_at(simgrid::mc::visited_pairs, index2, &pair_test);
413 pair_test->visited_removed = 1;
414 if (!pair_test->acceptance_pair || pair_test->acceptance_removed)
421 LivenessChecker::LivenessChecker(Session& session) : Checker(session)
425 LivenessChecker::~LivenessChecker()
429 RecordTrace LivenessChecker::getRecordTrace() // override
433 xbt_fifo_item_t start = xbt_fifo_get_last_item(mc_stack);
434 for (xbt_fifo_item_t item = start; item; item = xbt_fifo_get_prev_item(item)) {
435 simgrid::mc::Pair* pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(item);
437 smx_simcall_t req = MC_state_get_executed_request(pair->graph_state, &value);
438 if (req && req->call != SIMCALL_NONE) {
439 smx_process_t issuer = MC_smx_simcall_get_issuer(req);
440 const int pid = issuer->pid;
442 // Serialization the (pid, value) pair:
443 res.push_back(RecordTraceElement(pid, value));
447 return std::move(res);
450 void LivenessChecker::showAcceptanceCycle(std::size_t depth)
452 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
453 XBT_INFO("| ACCEPTANCE CYCLE |");
454 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
455 XBT_INFO("Counter-example that violates formula :");
456 simgrid::mc::dumpRecordPath();
457 this->showStack(mc_stack);
458 this->dumpStack(mc_stack);
459 MC_print_statistics(mc_stats);
460 XBT_INFO("Counter-example depth : %zd", depth);
463 int LivenessChecker::main(void)
465 simgrid::mc::Pair* current_pair = nullptr;
466 int value, res, visited_num = -1;
467 smx_simcall_t req = nullptr;
468 xbt_automaton_transition_t transition_succ = nullptr;
470 simgrid::mc::Pair* next_pair = nullptr;
471 simgrid::xbt::unique_ptr<s_xbt_dynar_t> prop_values;
472 simgrid::mc::VisitedPair* reached_pair = nullptr;
474 while(xbt_fifo_size(mc_stack) > 0){
476 /* Get current pair */
477 current_pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack));
479 /* Update current state in buchi automaton */
480 simgrid::mc::property_automaton->current_state = current_pair->automaton_state;
482 XBT_DEBUG("********************* ( Depth = %d, search_cycle = %d, interleave size = %d, pair_num = %d, requests = %d)",
483 current_pair->depth, current_pair->search_cycle,
484 MC_state_interleave_size(current_pair->graph_state), current_pair->num,
485 current_pair->requests);
487 if (current_pair->requests > 0) {
489 if (current_pair->automaton_state->type == 1 && current_pair->exploration_started == 0) {
490 /* If new acceptance pair, return new pair */
491 if ((reached_pair = this->insertAcceptancePair(current_pair)) == nullptr) {
492 this->showAcceptanceCycle(current_pair->depth);
493 return SIMGRID_MC_EXIT_LIVENESS;
497 /* Pair already visited ? stop the exploration on the current path */
498 if ((current_pair->exploration_started == 0)
499 && (visited_num = this->insertVisitedPair(
500 reached_pair, current_pair)) != -1) {
502 if (dot_output != nullptr){
503 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, visited_num, initial_global_state->prev_req);
507 XBT_DEBUG("Pair already visited (equal to pair %d), exploration on the current path stopped.", visited_num);
508 current_pair->requests = 0;
513 req = MC_state_get_request(current_pair->graph_state, &value);
515 if (dot_output != nullptr) {
516 if (initial_global_state->prev_pair != 0 && initial_global_state->prev_pair != current_pair->num) {
517 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, current_pair->num, initial_global_state->prev_req);
518 xbt_free(initial_global_state->prev_req);
520 initial_global_state->prev_pair = current_pair->num;
521 initial_global_state->prev_req = simgrid::mc::request_get_dot_output(req, value);
522 if (current_pair->search_cycle)
523 fprintf(dot_output, "%d [shape=doublecircle];\n", current_pair->num);
527 char* req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
528 XBT_DEBUG("Execute: %s", req_str);
531 /* Set request as executed */
532 MC_state_set_executed_request(current_pair->graph_state, req, value);
534 /* Update mc_stats */
535 mc_stats->executed_transitions++;
536 if(current_pair->exploration_started == 0)
537 mc_stats->visited_pairs++;
539 /* Answer the request */
540 simgrid::mc::handle_simcall(req, value);
542 /* Wait for requests (schedules processes) */
543 mc_model_checker->wait_for_requests();
545 current_pair->requests--;
546 current_pair->exploration_started = 1;
548 /* Get values of atomic propositions (variables used in the property formula) */
549 prop_values = this->getPropositionValues();
551 /* Evaluate enabled/true transitions in automaton according to atomic propositions values and create new pairs */
552 cursor = xbt_dynar_length(current_pair->automaton_state->out) - 1;
553 while (cursor >= 0) {
554 transition_succ = (xbt_automaton_transition_t)xbt_dynar_get_as(current_pair->automaton_state->out, cursor, xbt_automaton_transition_t);
555 res = MC_automaton_evaluate_label(transition_succ->label, prop_values.get());
556 if (res == 1 || res == 2) { /* 1 = True transition (always enabled), 2 = enabled transition according to atomic prop values */
557 next_pair = new Pair();
558 next_pair->graph_state = MC_state_new();
559 next_pair->automaton_state = transition_succ->dst;
560 next_pair->atomic_propositions = this->getPropositionValues();
561 next_pair->depth = current_pair->depth + 1;
562 /* Get enabled processes and insert them in the interleave set of the next graph_state */
563 for (auto& p : mc_model_checker->process().simix_processes())
564 if (simgrid::mc::process_is_enabled(&p.copy))
565 MC_state_interleave_process(next_pair->graph_state, &p.copy);
567 next_pair->requests = MC_state_interleave_size(next_pair->graph_state);
569 /* FIXME : get search_cycle value for each acceptant state */
570 if (next_pair->automaton_state->type == 1 || current_pair->search_cycle)
571 next_pair->search_cycle = 1;
573 /* Add new pair to the exploration stack */
574 xbt_fifo_unshift(mc_stack, next_pair);
580 } /* End of visited_pair test */
585 if(visited_num == -1)
586 XBT_DEBUG("No more request to execute. Looking for backtracking point.");
590 /* Traverse the stack backwards until a pair with a non empty interleave
591 set is found, deleting all the pairs that have it empty in the way. */
592 while ((current_pair = (simgrid::mc::Pair*) xbt_fifo_shift(mc_stack)) != nullptr) {
593 if (current_pair->requests > 0) {
594 /* We found a backtracking point */
595 XBT_DEBUG("Backtracking to depth %d", current_pair->depth);
596 xbt_fifo_unshift(mc_stack, current_pair);
597 this->replay(mc_stack);
598 XBT_DEBUG("Backtracking done");
602 XBT_DEBUG("Delete pair %d at depth %d", current_pair->num, current_pair->depth);
603 if (current_pair->automaton_state->type == 1)
604 this->removeAcceptancePair(current_pair->num);
609 } /* End of if (current_pair->requests > 0) else ... */
611 } /* End of while(xbt_fifo_size(mc_stack) > 0) */
613 XBT_INFO("No property violation found.");
614 MC_print_statistics(mc_stats);
615 return SIMGRID_MC_EXIT_SUCCESS;
618 int LivenessChecker::run()
620 XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
621 MC_automaton_load(_sg_mc_property_file);
622 mc_model_checker->wait_for_requests();
624 XBT_DEBUG("Starting the liveness algorithm");
627 /* Create exploration stack */
628 mc_stack = xbt_fifo_new();
630 /* Create the initial state */
631 initial_global_state = xbt_new0(s_mc_global_t, 1);