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 void MC_replay_liveness(xbt_fifo_t stack)
212 xbt_fifo_item_t item;
213 simgrid::mc::Pair* pair = nullptr;
214 mc_state_t state = nullptr;
215 smx_simcall_t req = nullptr, saved_req = NULL;
216 int value, depth = 1;
219 XBT_DEBUG("**** Begin Replay ****");
221 /* Intermediate backtracking */
222 if(_sg_mc_checkpoint > 0) {
223 item = xbt_fifo_get_first_item(stack);
224 pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(item);
225 if(pair->graph_state->system_state){
226 simgrid::mc::restore_snapshot(pair->graph_state->system_state);
231 /* Restore the initial state */
232 simgrid::mc::restore_snapshot(initial_global_state->snapshot);
234 /* Traverse the stack from the initial state and re-execute the transitions */
235 for (item = xbt_fifo_get_last_item(stack);
236 item != xbt_fifo_get_first_item(stack);
237 item = xbt_fifo_get_prev_item(item)) {
239 pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(item);
241 state = (mc_state_t) pair->graph_state;
243 if (pair->exploration_started) {
245 saved_req = MC_state_get_executed_request(state, &value);
247 if (saved_req != nullptr) {
248 /* because we got a copy of the executed request, we have to fetch the
249 real one, pointed by the request field of the issuer process */
250 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
251 req = &issuer->simcall;
253 /* Debug information */
254 if (XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)) {
255 req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
256 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
262 simgrid::mc::handle_simcall(req, value);
263 mc_model_checker->wait_for_requests();
266 /* Update statistics */
267 mc_stats->visited_pairs++;
268 mc_stats->executed_transitions++;
274 XBT_DEBUG("**** End Replay ****");
277 static int MC_modelcheck_liveness_main(void)
279 simgrid::mc::Pair* current_pair = nullptr;
280 int value, res, visited_num = -1;
281 smx_simcall_t req = nullptr;
282 xbt_automaton_transition_t transition_succ = nullptr;
284 simgrid::mc::Pair* next_pair = nullptr;
285 simgrid::xbt::unique_ptr<s_xbt_dynar_t> prop_values;
286 simgrid::mc::VisitedPair* reached_pair = nullptr;
288 while(xbt_fifo_size(mc_stack) > 0){
290 /* Get current pair */
291 current_pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack));
293 /* Update current state in buchi automaton */
294 simgrid::mc::property_automaton->current_state = current_pair->automaton_state;
296 XBT_DEBUG("********************* ( Depth = %d, search_cycle = %d, interleave size = %d, pair_num = %d, requests = %d)",
297 current_pair->depth, current_pair->search_cycle,
298 MC_state_interleave_size(current_pair->graph_state), current_pair->num,
299 current_pair->requests);
301 if (current_pair->requests > 0) {
303 if (current_pair->automaton_state->type == 1 && current_pair->exploration_started == 0) {
304 /* If new acceptance pair, return new pair */
305 if ((reached_pair = is_reached_acceptance_pair(current_pair)) == nullptr) {
306 int counter_example_depth = current_pair->depth;
307 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
308 XBT_INFO("| ACCEPTANCE CYCLE |");
309 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
310 XBT_INFO("Counter-example that violates formula :");
311 MC_record_dump_path(mc_stack);
312 simgrid::mc::show_stack_liveness(mc_stack);
313 simgrid::mc::dump_stack_liveness(mc_stack);
314 MC_print_statistics(mc_stats);
315 XBT_INFO("Counter-example depth : %d", counter_example_depth);
316 return SIMGRID_MC_EXIT_LIVENESS;
320 /* Pair already visited ? stop the exploration on the current path */
321 if ((current_pair->exploration_started == 0)
322 && (visited_num = simgrid::mc::is_visited_pair(
323 reached_pair, current_pair)) != -1) {
325 if (dot_output != nullptr){
326 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, visited_num, initial_global_state->prev_req);
330 XBT_DEBUG("Pair already visited (equal to pair %d), exploration on the current path stopped.", visited_num);
331 current_pair->requests = 0;
336 req = MC_state_get_request(current_pair->graph_state, &value);
338 if (dot_output != nullptr) {
339 if (initial_global_state->prev_pair != 0 && initial_global_state->prev_pair != current_pair->num) {
340 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, current_pair->num, initial_global_state->prev_req);
341 xbt_free(initial_global_state->prev_req);
343 initial_global_state->prev_pair = current_pair->num;
344 initial_global_state->prev_req = simgrid::mc::request_get_dot_output(req, value);
345 if (current_pair->search_cycle)
346 fprintf(dot_output, "%d [shape=doublecircle];\n", current_pair->num);
350 char* req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
351 XBT_DEBUG("Execute: %s", req_str);
354 /* Set request as executed */
355 MC_state_set_executed_request(current_pair->graph_state, req, value);
357 /* Update mc_stats */
358 mc_stats->executed_transitions++;
359 if(current_pair->exploration_started == 0)
360 mc_stats->visited_pairs++;
362 /* Answer the request */
363 simgrid::mc::handle_simcall(req, value);
365 /* Wait for requests (schedules processes) */
366 mc_model_checker->wait_for_requests();
368 current_pair->requests--;
369 current_pair->exploration_started = 1;
371 /* Get values of atomic propositions (variables used in the property formula) */
372 prop_values = get_atomic_propositions_values();
374 /* Evaluate enabled/true transitions in automaton according to atomic propositions values and create new pairs */
375 cursor = xbt_dynar_length(current_pair->automaton_state->out) - 1;
376 while (cursor >= 0) {
377 transition_succ = (xbt_automaton_transition_t)xbt_dynar_get_as(current_pair->automaton_state->out, cursor, xbt_automaton_transition_t);
378 res = MC_automaton_evaluate_label(transition_succ->label, prop_values.get());
379 if (res == 1 || res == 2) { /* 1 = True transition (always enabled), 2 = enabled transition according to atomic prop values */
380 next_pair = new Pair();
381 next_pair->graph_state = MC_state_new();
382 next_pair->automaton_state = transition_succ->dst;
383 next_pair->atomic_propositions = get_atomic_propositions_values();
384 next_pair->depth = current_pair->depth + 1;
385 /* Get enabled processes and insert them in the interleave set of the next graph_state */
386 for (auto& p : mc_model_checker->process().simix_processes())
387 if (simgrid::mc::process_is_enabled(&p.copy))
388 MC_state_interleave_process(next_pair->graph_state, &p.copy);
390 next_pair->requests = MC_state_interleave_size(next_pair->graph_state);
392 /* FIXME : get search_cycle value for each acceptant state */
393 if (next_pair->automaton_state->type == 1 || current_pair->search_cycle)
394 next_pair->search_cycle = 1;
396 /* Add new pair to the exploration stack */
397 xbt_fifo_unshift(mc_stack, next_pair);
403 } /* End of visited_pair test */
408 if(visited_num == -1)
409 XBT_DEBUG("No more request to execute. Looking for backtracking point.");
413 /* Traverse the stack backwards until a pair with a non empty interleave
414 set is found, deleting all the pairs that have it empty in the way. */
415 while ((current_pair = (simgrid::mc::Pair*) xbt_fifo_shift(mc_stack)) != nullptr) {
416 if (current_pair->requests > 0) {
417 /* We found a backtracking point */
418 XBT_DEBUG("Backtracking to depth %d", current_pair->depth);
419 xbt_fifo_unshift(mc_stack, current_pair);
420 MC_replay_liveness(mc_stack);
421 XBT_DEBUG("Backtracking done");
425 XBT_DEBUG("Delete pair %d at depth %d", current_pair->num, current_pair->depth);
426 if (current_pair->automaton_state->type == 1)
427 remove_acceptance_pair(current_pair->num);
432 } /* End of if (current_pair->requests > 0) else ... */
434 } /* End of while(xbt_fifo_size(mc_stack) > 0) */
436 XBT_INFO("No property violation found.");
437 MC_print_statistics(mc_stats);
438 return SIMGRID_MC_EXIT_SUCCESS;
441 int modelcheck_liveness(void)
443 XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
444 MC_automaton_load(_sg_mc_property_file);
445 mc_model_checker->wait_for_requests();
447 XBT_DEBUG("Starting the liveness algorithm");
450 /* Create exploration stack */
451 mc_stack = xbt_fifo_new();
453 /* Create the initial state */
454 initial_global_state = xbt_new0(s_mc_global_t, 1);
456 MC_pre_modelcheck_liveness();
457 int res = MC_modelcheck_liveness_main();
460 simgrid::mc::processes_time.clear();