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. */
12 #include <xbt/automaton.h>
13 #include <xbt/dynar.h>
16 #include <xbt/sysdep.h>
18 #include "src/mc/mc_request.h"
19 #include "src/mc/mc_liveness.h"
20 #include "src/mc/mc_private.h"
21 #include "src/mc/mc_record.h"
22 #include "src/mc/mc_smx.h"
23 #include "src/mc/Client.hpp"
24 #include "src/mc/mc_replay.h"
25 #include "src/mc/mc_safety.h"
26 #include "src/mc/mc_exit.h"
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_liveness, mc,
29 "Logging specific to algorithms for liveness properties verification");
31 /********* Global variables *********/
33 xbt_dynar_t acceptance_pairs;
35 /********* Static functions *********/
40 Pair::Pair() : num(++mc_stats->expanded_pairs),
41 visited_pair_removed(_sg_mc_visited > 0 ? 0 : 1)
45 if (this->visited_pair_removed)
46 MC_state_delete(this->graph_state, 1);
49 static simgrid::xbt::unique_ptr<s_xbt_dynar_t> get_atomic_propositions_values()
51 unsigned int cursor = 0;
52 xbt_automaton_propositional_symbol_t ps = nullptr;
53 simgrid::xbt::unique_ptr<s_xbt_dynar_t> values = simgrid::xbt::unique_ptr<s_xbt_dynar_t>(xbt_dynar_new(sizeof(int), nullptr));
54 xbt_dynar_foreach(simgrid::mc::property_automaton->propositional_symbols, cursor, ps) {
55 int res = xbt_automaton_propositional_symbol_evaluate(ps);
56 xbt_dynar_push_as(values.get(), int, res);
58 return std::move(values);
61 static simgrid::mc::VisitedPair* is_reached_acceptance_pair(simgrid::mc::Pair* pair)
63 simgrid::mc::VisitedPair* new_pair = new VisitedPair(
64 pair->num, pair->automaton_state, pair->atomic_propositions.get(),
66 new_pair->acceptance_pair = 1;
68 if (xbt_dynar_is_empty(acceptance_pairs))
69 xbt_dynar_push(acceptance_pairs, &new_pair);
72 int min = -1, max = -1, index;
74 simgrid::mc::VisitedPair* pair_test;
77 index = get_search_interval(acceptance_pairs, new_pair, &min, &max);
79 if (min != -1 && max != -1) { // Acceptance pair with same number of processes and same heap bytes used exists
82 if(pair->search_cycle == 1){
83 while (cursor <= max) {
84 pair_test = (simgrid::mc::VisitedPair*) xbt_dynar_get_as(acceptance_pairs, cursor, simgrid::mc::VisitedPair*);
85 if (xbt_automaton_state_compare(pair_test->automaton_state, new_pair->automaton_state) == 0) {
86 if (xbt_automaton_propositional_symbols_compare_value(
87 pair_test->atomic_propositions.get(),
88 new_pair->atomic_propositions.get()) == 0) {
89 if (snapshot_compare(pair_test, new_pair) == 0) {
90 XBT_INFO("Pair %d already reached (equal to pair %d) !", new_pair->num, pair_test->num);
91 xbt_fifo_shift(mc_stack);
92 if (dot_output != nullptr)
93 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, pair_test->num, initial_global_state->prev_req);
101 xbt_dynar_insert_at(acceptance_pairs, min, &new_pair);
103 pair_test = (simgrid::mc::VisitedPair*) xbt_dynar_get_as(acceptance_pairs, index, simgrid::mc::VisitedPair*);
104 if (pair_test->nb_processes < new_pair->nb_processes)
105 xbt_dynar_insert_at(acceptance_pairs, index + 1, &new_pair);
106 else if (pair_test->heap_bytes_used < new_pair->heap_bytes_used)
107 xbt_dynar_insert_at(acceptance_pairs, index + 1, &new_pair);
109 xbt_dynar_insert_at(acceptance_pairs, index, &new_pair);
115 static void remove_acceptance_pair(int pair_num)
117 unsigned int cursor = 0;
118 simgrid::mc::VisitedPair* pair_test = nullptr;
121 xbt_dynar_foreach(acceptance_pairs, cursor, pair_test)
122 if (pair_test->num == pair_num) {
127 if(pair_found == 1) {
128 xbt_dynar_remove_at(acceptance_pairs, cursor, &pair_test);
130 pair_test->acceptance_removed = 1;
132 if (_sg_mc_visited == 0 || pair_test->visited_removed == 1)
139 static int MC_automaton_evaluate_label(xbt_automaton_exp_label_t l,
140 xbt_dynar_t atomic_propositions_values)
145 int left_res = MC_automaton_evaluate_label(l->u.or_and.left_exp, atomic_propositions_values);
146 int right_res = MC_automaton_evaluate_label(l->u.or_and.right_exp, atomic_propositions_values);
147 return (left_res || right_res);
150 int left_res = MC_automaton_evaluate_label(l->u.or_and.left_exp, atomic_propositions_values);
151 int right_res = MC_automaton_evaluate_label(l->u.or_and.right_exp, atomic_propositions_values);
152 return (left_res && right_res);
155 int res = MC_automaton_evaluate_label(l->u.exp_not, atomic_propositions_values);
159 unsigned int cursor = 0;
160 xbt_automaton_propositional_symbol_t p = nullptr;
161 xbt_dynar_foreach(simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
162 if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
163 return (int) xbt_dynar_get_as(atomic_propositions_values, cursor, int);
174 static void MC_pre_modelcheck_liveness(void)
176 simgrid::mc::Pair* initial_pair = nullptr;
177 mc_model_checker->wait_for_requests();
178 acceptance_pairs = xbt_dynar_new(sizeof(simgrid::mc::VisitedPair*), nullptr);
179 if(_sg_mc_visited > 0)
180 simgrid::mc::visited_pairs = xbt_dynar_new(sizeof(simgrid::mc::VisitedPair*), nullptr);
182 initial_global_state->snapshot = simgrid::mc::take_snapshot(0);
183 initial_global_state->prev_pair = 0;
185 unsigned int cursor = 0;
186 xbt_automaton_state_t automaton_state;
188 xbt_dynar_foreach(simgrid::mc::property_automaton->states, cursor, automaton_state) {
189 if (automaton_state->type == -1) { /* Initial automaton state */
191 initial_pair = new Pair();
192 initial_pair->automaton_state = automaton_state;
193 initial_pair->graph_state = MC_state_new();
194 initial_pair->atomic_propositions = get_atomic_propositions_values();
195 initial_pair->depth = 1;
197 /* Get enabled processes and insert them in the interleave set of the graph_state */
198 for (auto& p : mc_model_checker->process().simix_processes())
199 if (simgrid::mc::process_is_enabled(&p.copy))
200 MC_state_interleave_process(initial_pair->graph_state, &p.copy);
202 initial_pair->requests = MC_state_interleave_size(initial_pair->graph_state);
203 initial_pair->search_cycle = 0;
205 xbt_fifo_unshift(mc_stack, initial_pair);
210 static int MC_modelcheck_liveness_main(void)
212 simgrid::mc::Pair* current_pair = nullptr;
213 int value, res, visited_num = -1;
214 smx_simcall_t req = nullptr;
215 xbt_automaton_transition_t transition_succ = nullptr;
217 simgrid::mc::Pair* next_pair = nullptr;
218 simgrid::xbt::unique_ptr<s_xbt_dynar_t> prop_values;
219 simgrid::mc::VisitedPair* reached_pair = nullptr;
221 while(xbt_fifo_size(mc_stack) > 0){
223 /* Get current pair */
224 current_pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack));
226 /* Update current state in buchi automaton */
227 simgrid::mc::property_automaton->current_state = current_pair->automaton_state;
229 XBT_DEBUG("********************* ( Depth = %d, search_cycle = %d, interleave size = %d, pair_num = %d, requests = %d)",
230 current_pair->depth, current_pair->search_cycle,
231 MC_state_interleave_size(current_pair->graph_state), current_pair->num,
232 current_pair->requests);
234 if (current_pair->requests > 0) {
236 if (current_pair->automaton_state->type == 1 && current_pair->exploration_started == 0) {
237 /* If new acceptance pair, return new pair */
238 if ((reached_pair = is_reached_acceptance_pair(current_pair)) == nullptr) {
239 int counter_example_depth = current_pair->depth;
240 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
241 XBT_INFO("| ACCEPTANCE CYCLE |");
242 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
243 XBT_INFO("Counter-example that violates formula :");
244 MC_record_dump_path(mc_stack);
245 simgrid::mc::show_stack_liveness(mc_stack);
246 simgrid::mc::dump_stack_liveness(mc_stack);
247 MC_print_statistics(mc_stats);
248 XBT_INFO("Counter-example depth : %d", counter_example_depth);
249 return SIMGRID_MC_EXIT_LIVENESS;
253 /* Pair already visited ? stop the exploration on the current path */
254 if ((current_pair->exploration_started == 0)
255 && (visited_num = simgrid::mc::is_visited_pair(
256 reached_pair, current_pair)) != -1) {
258 if (dot_output != nullptr){
259 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, visited_num, initial_global_state->prev_req);
263 XBT_DEBUG("Pair already visited (equal to pair %d), exploration on the current path stopped.", visited_num);
264 current_pair->requests = 0;
269 req = MC_state_get_request(current_pair->graph_state, &value);
271 if (dot_output != nullptr) {
272 if (initial_global_state->prev_pair != 0 && initial_global_state->prev_pair != current_pair->num) {
273 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, current_pair->num, initial_global_state->prev_req);
274 xbt_free(initial_global_state->prev_req);
276 initial_global_state->prev_pair = current_pair->num;
277 initial_global_state->prev_req = simgrid::mc::request_get_dot_output(req, value);
278 if (current_pair->search_cycle)
279 fprintf(dot_output, "%d [shape=doublecircle];\n", current_pair->num);
283 char* req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
284 XBT_DEBUG("Execute: %s", req_str);
287 /* Set request as executed */
288 MC_state_set_executed_request(current_pair->graph_state, req, value);
290 /* Update mc_stats */
291 mc_stats->executed_transitions++;
292 if(current_pair->exploration_started == 0)
293 mc_stats->visited_pairs++;
295 /* Answer the request */
296 simgrid::mc::handle_simcall(req, value);
298 /* Wait for requests (schedules processes) */
299 mc_model_checker->wait_for_requests();
301 current_pair->requests--;
302 current_pair->exploration_started = 1;
304 /* Get values of atomic propositions (variables used in the property formula) */
305 prop_values = get_atomic_propositions_values();
307 /* Evaluate enabled/true transitions in automaton according to atomic propositions values and create new pairs */
308 cursor = xbt_dynar_length(current_pair->automaton_state->out) - 1;
309 while (cursor >= 0) {
310 transition_succ = (xbt_automaton_transition_t)xbt_dynar_get_as(current_pair->automaton_state->out, cursor, xbt_automaton_transition_t);
311 res = MC_automaton_evaluate_label(transition_succ->label, prop_values.get());
312 if (res == 1 || res == 2) { /* 1 = True transition (always enabled), 2 = enabled transition according to atomic prop values */
313 next_pair = new Pair();
314 next_pair->graph_state = MC_state_new();
315 next_pair->automaton_state = transition_succ->dst;
316 next_pair->atomic_propositions = get_atomic_propositions_values();
317 next_pair->depth = current_pair->depth + 1;
318 /* Get enabled processes and insert them in the interleave set of the next graph_state */
319 for (auto& p : mc_model_checker->process().simix_processes())
320 if (simgrid::mc::process_is_enabled(&p.copy))
321 MC_state_interleave_process(next_pair->graph_state, &p.copy);
323 next_pair->requests = MC_state_interleave_size(next_pair->graph_state);
325 /* FIXME : get search_cycle value for each acceptant state */
326 if (next_pair->automaton_state->type == 1 || current_pair->search_cycle)
327 next_pair->search_cycle = 1;
329 /* Add new pair to the exploration stack */
330 xbt_fifo_unshift(mc_stack, next_pair);
336 } /* End of visited_pair test */
341 if(visited_num == -1)
342 XBT_DEBUG("No more request to execute. Looking for backtracking point.");
346 /* Traverse the stack backwards until a pair with a non empty interleave
347 set is found, deleting all the pairs that have it empty in the way. */
348 while ((current_pair = (simgrid::mc::Pair*) xbt_fifo_shift(mc_stack)) != nullptr) {
349 if (current_pair->requests > 0) {
350 /* We found a backtracking point */
351 XBT_DEBUG("Backtracking to depth %d", current_pair->depth);
352 xbt_fifo_unshift(mc_stack, current_pair);
353 MC_replay_liveness(mc_stack);
354 XBT_DEBUG("Backtracking done");
358 XBT_DEBUG("Delete pair %d at depth %d", current_pair->num, current_pair->depth);
359 if (current_pair->automaton_state->type == 1)
360 remove_acceptance_pair(current_pair->num);
365 } /* End of if (current_pair->requests > 0) else ... */
367 } /* End of while(xbt_fifo_size(mc_stack) > 0) */
369 XBT_INFO("No property violation found.");
370 MC_print_statistics(mc_stats);
371 return SIMGRID_MC_EXIT_SUCCESS;
374 int modelcheck_liveness(void)
376 if (simgrid::mc::reduction_mode == simgrid::mc::ReductionMode::unset)
377 simgrid::mc::reduction_mode = simgrid::mc::ReductionMode::none;
378 XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
379 MC_automaton_load(_sg_mc_property_file);
380 mc_model_checker->wait_for_requests();
382 XBT_DEBUG("Starting the liveness algorithm");
385 /* Create exploration stack */
386 mc_stack = xbt_fifo_new();
388 /* Create the initial state */
389 initial_global_state = xbt_new0(s_mc_global_t, 1);
391 MC_pre_modelcheck_liveness();
392 int res = MC_modelcheck_liveness_main();
395 simgrid::mc::processes_time.clear();