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/parmap.h>
17 #include <xbt/sysdep.h>
19 #include "src/mc/mc_request.h"
20 #include "src/mc/mc_liveness.h"
21 #include "src/mc/mc_private.h"
22 #include "src/mc/mc_record.h"
23 #include "src/mc/mc_smx.h"
24 #include "src/mc/mc_client.h"
25 #include "src/mc/mc_replay.h"
26 #include "src/mc/mc_safety.h"
27 #include "src/mc/mc_exit.h"
31 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_liveness, mc,
32 "Logging specific to algorithms for liveness properties verification");
36 /********* Global variables *********/
38 xbt_dynar_t acceptance_pairs;
41 /********* Static functions *********/
46 Pair::Pair() : num(++mc_stats->expanded_pairs),
47 visited_pair_removed(_sg_mc_visited > 0 ? 0 : 1)
51 this->automaton_state = nullptr;
52 if (this->visited_pair_removed)
53 MC_state_delete(this->graph_state, 1);
54 xbt_dynar_free(&(this->atomic_propositions));
57 static xbt_dynar_t get_atomic_propositions_values()
59 unsigned int cursor = 0;
60 xbt_automaton_propositional_symbol_t ps = nullptr;
61 xbt_dynar_t values = xbt_dynar_new(sizeof(int), nullptr);
62 xbt_dynar_foreach(simgrid::mc::property_automaton->propositional_symbols, cursor, ps) {
63 int res = xbt_automaton_propositional_symbol_evaluate(ps);
64 xbt_dynar_push_as(values, int, res);
70 static simgrid::mc::VisitedPair* is_reached_acceptance_pair(simgrid::mc::Pair* pair)
72 simgrid::mc::VisitedPair* new_pair = nullptr;
73 new_pair = simgrid::mc::visited_pair_new(pair->num, pair->automaton_state, pair->atomic_propositions, pair->graph_state);
74 new_pair->acceptance_pair = 1;
76 if (xbt_dynar_is_empty(acceptance_pairs))
77 xbt_dynar_push(acceptance_pairs, &new_pair);
80 int min = -1, max = -1, index;
82 simgrid::mc::VisitedPair* pair_test;
85 index = get_search_interval(acceptance_pairs, new_pair, &min, &max);
87 if (min != -1 && max != -1) { // Acceptance pair with same number of processes and same heap bytes used exists
90 if(pair->search_cycle == 1){
91 while (cursor <= max) {
92 pair_test = (simgrid::mc::VisitedPair*) xbt_dynar_get_as(acceptance_pairs, cursor, simgrid::mc::VisitedPair*);
93 if (xbt_automaton_state_compare(pair_test->automaton_state, new_pair->automaton_state) == 0) {
94 if (xbt_automaton_propositional_symbols_compare_value(pair_test->atomic_propositions, new_pair->atomic_propositions) == 0) {
95 if (snapshot_compare(pair_test, new_pair) == 0) {
96 XBT_INFO("Pair %d already reached (equal to pair %d) !", new_pair->num, pair_test->num);
97 xbt_fifo_shift(mc_stack);
98 if (dot_output != nullptr)
99 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, pair_test->num, initial_global_state->prev_req);
107 xbt_dynar_insert_at(acceptance_pairs, min, &new_pair);
109 pair_test = (simgrid::mc::VisitedPair*) xbt_dynar_get_as(acceptance_pairs, index, simgrid::mc::VisitedPair*);
110 if (pair_test->nb_processes < new_pair->nb_processes)
111 xbt_dynar_insert_at(acceptance_pairs, index + 1, &new_pair);
112 else if (pair_test->heap_bytes_used < new_pair->heap_bytes_used)
113 xbt_dynar_insert_at(acceptance_pairs, index + 1, &new_pair);
115 xbt_dynar_insert_at(acceptance_pairs, index, &new_pair);
121 static void remove_acceptance_pair(int pair_num)
123 unsigned int cursor = 0;
124 simgrid::mc::VisitedPair* pair_test = nullptr;
127 xbt_dynar_foreach(acceptance_pairs, cursor, pair_test)
128 if (pair_test->num == pair_num) {
133 if(pair_found == 1) {
134 xbt_dynar_remove_at(acceptance_pairs, cursor, &pair_test);
136 pair_test->acceptance_removed = 1;
138 if (_sg_mc_visited == 0 || pair_test->visited_removed == 1)
139 simgrid::mc::visited_pair_delete(pair_test);
145 static int MC_automaton_evaluate_label(xbt_automaton_exp_label_t l,
146 xbt_dynar_t atomic_propositions_values)
151 int left_res = MC_automaton_evaluate_label(l->u.or_and.left_exp, atomic_propositions_values);
152 int right_res = MC_automaton_evaluate_label(l->u.or_and.right_exp, atomic_propositions_values);
153 return (left_res || right_res);
156 int left_res = MC_automaton_evaluate_label(l->u.or_and.left_exp, atomic_propositions_values);
157 int right_res = MC_automaton_evaluate_label(l->u.or_and.right_exp, atomic_propositions_values);
158 return (left_res && right_res);
161 int res = MC_automaton_evaluate_label(l->u.exp_not, atomic_propositions_values);
165 unsigned int cursor = 0;
166 xbt_automaton_propositional_symbol_t p = nullptr;
167 xbt_dynar_foreach(simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
168 if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
169 return (int) xbt_dynar_get_as(atomic_propositions_values, cursor, int);
180 static void MC_pre_modelcheck_liveness(void)
182 simgrid::mc::Pair* initial_pair = nullptr;
183 mc_model_checker->wait_for_requests();
184 acceptance_pairs = xbt_dynar_new(sizeof(simgrid::mc::VisitedPair*), nullptr);
185 if(_sg_mc_visited > 0)
186 simgrid::mc::visited_pairs = xbt_dynar_new(sizeof(simgrid::mc::VisitedPair*), nullptr);
188 initial_global_state->snapshot = simgrid::mc::take_snapshot(0);
189 initial_global_state->prev_pair = 0;
191 unsigned int cursor = 0;
192 xbt_automaton_state_t automaton_state;
194 xbt_dynar_foreach(simgrid::mc::property_automaton->states, cursor, automaton_state) {
195 if (automaton_state->type == -1) { /* Initial automaton state */
197 initial_pair = new Pair();
198 initial_pair->automaton_state = automaton_state;
199 initial_pair->graph_state = MC_state_new();
200 initial_pair->atomic_propositions = get_atomic_propositions_values();
201 initial_pair->depth = 1;
203 /* Get enabled processes and insert them in the interleave set of the graph_state */
204 for (auto& p : mc_model_checker->process().simix_processes())
205 if (simgrid::mc::process_is_enabled(&p.copy))
206 MC_state_interleave_process(initial_pair->graph_state, &p.copy);
208 initial_pair->requests = MC_state_interleave_size(initial_pair->graph_state);
209 initial_pair->search_cycle = 0;
211 xbt_fifo_unshift(mc_stack, initial_pair);
216 static int MC_modelcheck_liveness_main(void)
218 simgrid::mc::Pair* current_pair = nullptr;
219 int value, res, visited_num = -1;
220 smx_simcall_t req = nullptr;
221 xbt_automaton_transition_t transition_succ = nullptr;
223 simgrid::mc::Pair* next_pair = nullptr;
224 xbt_dynar_t prop_values = nullptr;
225 simgrid::mc::VisitedPair* reached_pair = nullptr;
227 while(xbt_fifo_size(mc_stack) > 0){
229 /* Get current pair */
230 current_pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack));
232 /* Update current state in buchi automaton */
233 simgrid::mc::property_automaton->current_state = current_pair->automaton_state;
235 XBT_DEBUG("********************* ( Depth = %d, search_cycle = %d, interleave size = %d, pair_num = %d, requests = %d)",
236 current_pair->depth, current_pair->search_cycle,
237 MC_state_interleave_size(current_pair->graph_state), current_pair->num,
238 current_pair->requests);
240 if (current_pair->requests > 0) {
242 if (current_pair->automaton_state->type == 1 && current_pair->exploration_started == 0) {
243 /* If new acceptance pair, return new pair */
244 if ((reached_pair = is_reached_acceptance_pair(current_pair)) == nullptr) {
245 int counter_example_depth = current_pair->depth;
246 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
247 XBT_INFO("| ACCEPTANCE CYCLE |");
248 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
249 XBT_INFO("Counter-example that violates formula :");
250 MC_record_dump_path(mc_stack);
251 simgrid::mc::show_stack_liveness(mc_stack);
252 simgrid::mc::dump_stack_liveness(mc_stack);
253 MC_print_statistics(mc_stats);
254 XBT_INFO("Counter-example depth : %d", counter_example_depth);
255 return SIMGRID_MC_EXIT_LIVENESS;
259 /* Pair already visited ? stop the exploration on the current path */
260 if ((current_pair->exploration_started == 0)
261 && (visited_num = simgrid::mc::is_visited_pair(
262 reached_pair, current_pair)) != -1) {
264 if (dot_output != nullptr){
265 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, visited_num, initial_global_state->prev_req);
269 XBT_DEBUG("Pair already visited (equal to pair %d), exploration on the current path stopped.", visited_num);
270 current_pair->requests = 0;
275 req = MC_state_get_request(current_pair->graph_state, &value);
277 if (dot_output != nullptr) {
278 if (initial_global_state->prev_pair != 0 && initial_global_state->prev_pair != current_pair->num) {
279 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, current_pair->num, initial_global_state->prev_req);
280 xbt_free(initial_global_state->prev_req);
282 initial_global_state->prev_pair = current_pair->num;
283 initial_global_state->prev_req = simgrid::mc::request_get_dot_output(req, value);
284 if (current_pair->search_cycle)
285 fprintf(dot_output, "%d [shape=doublecircle];\n", current_pair->num);
289 char* req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
290 XBT_DEBUG("Execute: %s", req_str);
293 /* Set request as executed */
294 MC_state_set_executed_request(current_pair->graph_state, req, value);
296 /* Update mc_stats */
297 mc_stats->executed_transitions++;
298 if(current_pair->exploration_started == 0)
299 mc_stats->visited_pairs++;
301 /* Answer the request */
302 simgrid::mc::handle_simcall(req, value);
304 /* Wait for requests (schedules processes) */
305 mc_model_checker->wait_for_requests();
307 current_pair->requests--;
308 current_pair->exploration_started = 1;
310 /* Get values of atomic propositions (variables used in the property formula) */
311 prop_values = get_atomic_propositions_values();
313 /* Evaluate enabled/true transitions in automaton according to atomic propositions values and create new pairs */
314 cursor = xbt_dynar_length(current_pair->automaton_state->out) - 1;
315 while (cursor >= 0) {
316 transition_succ = (xbt_automaton_transition_t)xbt_dynar_get_as(current_pair->automaton_state->out, cursor, xbt_automaton_transition_t);
317 res = MC_automaton_evaluate_label(transition_succ->label, prop_values);
318 if (res == 1 || res == 2) { /* 1 = True transition (always enabled), 2 = enabled transition according to atomic prop values */
319 next_pair = new Pair();
320 next_pair->graph_state = MC_state_new();
321 next_pair->automaton_state = transition_succ->dst;
322 next_pair->atomic_propositions = get_atomic_propositions_values();
323 next_pair->depth = current_pair->depth + 1;
324 /* Get enabled processes and insert them in the interleave set of the next graph_state */
325 for (auto& p : mc_model_checker->process().simix_processes())
326 if (simgrid::mc::process_is_enabled(&p.copy))
327 MC_state_interleave_process(next_pair->graph_state, &p.copy);
329 next_pair->requests = MC_state_interleave_size(next_pair->graph_state);
331 /* FIXME : get search_cycle value for each acceptant state */
332 if (next_pair->automaton_state->type == 1 || current_pair->search_cycle)
333 next_pair->search_cycle = 1;
335 /* Add new pair to the exploration stack */
336 xbt_fifo_unshift(mc_stack, next_pair);
342 } /* End of visited_pair test */
347 if(visited_num == -1)
348 XBT_DEBUG("No more request to execute. Looking for backtracking point.");
350 xbt_dynar_free(&prop_values);
352 /* Traverse the stack backwards until a pair with a non empty interleave
353 set is found, deleting all the pairs that have it empty in the way. */
354 while ((current_pair = (simgrid::mc::Pair*) xbt_fifo_shift(mc_stack)) != nullptr) {
355 if (current_pair->requests > 0) {
356 /* We found a backtracking point */
357 XBT_DEBUG("Backtracking to depth %d", current_pair->depth);
358 xbt_fifo_unshift(mc_stack, current_pair);
359 MC_replay_liveness(mc_stack);
360 XBT_DEBUG("Backtracking done");
364 XBT_DEBUG("Delete pair %d at depth %d", current_pair->num, current_pair->depth);
365 if (current_pair->automaton_state->type == 1)
366 remove_acceptance_pair(current_pair->num);
371 } /* End of if (current_pair->requests > 0) else ... */
373 } /* End of while(xbt_fifo_size(mc_stack) > 0) */
375 XBT_INFO("No property violation found.");
376 MC_print_statistics(mc_stats);
377 return SIMGRID_MC_EXIT_SUCCESS;
380 int modelcheck_liveness(void)
382 if (mc_reduce_kind == e_mc_reduce_unset)
383 mc_reduce_kind = e_mc_reduce_none;
384 XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
385 MC_automaton_load(_sg_mc_property_file);
386 mc_model_checker->wait_for_requests();
388 XBT_DEBUG("Starting the liveness algorithm");
391 /* Create exploration stack */
392 mc_stack = xbt_fifo_new();
394 /* Create the initial state */
395 initial_global_state = xbt_new0(s_mc_global_t, 1);
397 MC_pre_modelcheck_liveness();
398 int res = MC_modelcheck_liveness_main();
401 simgrid::mc::processes_time.clear();