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. */
10 #include <xbt/dynar.h>
11 #include <xbt/automaton.h>
13 #include "mc_request.h"
14 #include "mc_liveness.h"
15 #include "mc_private.h"
16 #include "mc_record.h"
18 #include "mc_client.h"
19 #include "mc_replay.h"
20 #include "mc_safety.h"
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_liveness, mc,
26 "Logging specific to algorithms for liveness properties verification");
28 /********* Global variables *********/
30 xbt_dynar_t acceptance_pairs;
33 /********* Static functions *********/
35 static xbt_dynar_t get_atomic_propositions_values()
37 unsigned int cursor = 0;
38 xbt_automaton_propositional_symbol_t ps = NULL;
39 xbt_dynar_t values = xbt_dynar_new(sizeof(int), NULL);
40 xbt_dynar_foreach(_mc_property_automaton->propositional_symbols, cursor, ps) {
41 int res = xbt_automaton_propositional_symbol_evaluate(ps);
42 xbt_dynar_push_as(values, int, res);
48 static mc_visited_pair_t is_reached_acceptance_pair(mc_pair_t pair)
50 mc_visited_pair_t new_pair = NULL;
51 new_pair = MC_visited_pair_new(pair->num, pair->automaton_state, pair->atomic_propositions, pair->graph_state);
52 new_pair->acceptance_pair = 1;
54 if (xbt_dynar_is_empty(acceptance_pairs)) {
56 xbt_dynar_push(acceptance_pairs, &new_pair);
60 int min = -1, max = -1, index;
62 mc_visited_pair_t pair_test;
65 index = get_search_interval(acceptance_pairs, new_pair, &min, &max);
67 if (min != -1 && max != -1) { // Acceptance pair with same number of processes and same heap bytes used exists
69 // Parallell implementation
70 /*res = xbt_parmap_mc_apply(parmap, snapshot_compare, xbt_dynar_get_ptr(acceptance_pairs, min), (max-min)+1, pair);
72 return ((mc_pair_t)xbt_dynar_get_as(acceptance_pairs, (min+res)-1, mc_pair_t))->num;
76 if(pair->search_cycle == 1){
77 while (cursor <= max) {
78 pair_test = (mc_visited_pair_t) xbt_dynar_get_as(acceptance_pairs, cursor, mc_visited_pair_t);
79 if (xbt_automaton_state_compare(pair_test->automaton_state, new_pair->automaton_state) == 0) {
80 if (xbt_automaton_propositional_symbols_compare_value(pair_test->atomic_propositions, new_pair->atomic_propositions) == 0) {
81 if (snapshot_compare(pair_test, new_pair) == 0) {
82 XBT_INFO("Pair %d already reached (equal to pair %d) !", new_pair->num, pair_test->num);
83 xbt_fifo_shift(mc_stack);
84 if (dot_output != NULL)
85 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, pair_test->num, initial_global_state->prev_req);
93 xbt_dynar_insert_at(acceptance_pairs, min, &new_pair);
95 pair_test = (mc_visited_pair_t) xbt_dynar_get_as(acceptance_pairs, index, mc_visited_pair_t);
96 if (pair_test->nb_processes < new_pair->nb_processes) {
97 xbt_dynar_insert_at(acceptance_pairs, index + 1, &new_pair);
99 if (pair_test->heap_bytes_used < new_pair->heap_bytes_used)
100 xbt_dynar_insert_at(acceptance_pairs, index + 1, &new_pair);
102 xbt_dynar_insert_at(acceptance_pairs, index, &new_pair);
109 static void remove_acceptance_pair(int pair_num)
111 unsigned int cursor = 0;
112 mc_visited_pair_t pair_test = NULL;
115 xbt_dynar_foreach(acceptance_pairs, cursor, pair_test) {
116 if (pair_test->num == pair_num) {
122 if(pair_found == 1) {
123 xbt_dynar_remove_at(acceptance_pairs, cursor, &pair_test);
125 pair_test->acceptance_removed = 1;
127 if (_sg_mc_visited == 0 || pair_test->visited_removed == 1)
128 MC_visited_pair_delete(pair_test);
134 static int MC_automaton_evaluate_label(xbt_automaton_exp_label_t l,
135 xbt_dynar_t atomic_propositions_values)
140 int left_res = MC_automaton_evaluate_label(l->u.or_and.left_exp, atomic_propositions_values);
141 int right_res = MC_automaton_evaluate_label(l->u.or_and.right_exp, atomic_propositions_values);
142 return (left_res || right_res);
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 res = MC_automaton_evaluate_label(l->u.exp_not, atomic_propositions_values);
154 unsigned int cursor = 0;
155 xbt_automaton_propositional_symbol_t p = NULL;
156 xbt_dynar_foreach(_mc_property_automaton->propositional_symbols, cursor, p) {
157 if (strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
158 return (int) xbt_dynar_get_as(atomic_propositions_values, cursor, int);
170 static void MC_modelcheck_liveness_main(void);
172 static void MC_pre_modelcheck_liveness(void)
174 mc_pair_t initial_pair = NULL;
175 smx_process_t process;
177 MC_wait_for_requests();
179 acceptance_pairs = xbt_dynar_new(sizeof(mc_visited_pair_t), NULL);
180 if(_sg_mc_visited > 0)
181 visited_pairs = xbt_dynar_new(sizeof(mc_visited_pair_t), NULL);
183 initial_global_state->snapshot = MC_take_snapshot(0);
184 initial_global_state->prev_pair = 0;
186 unsigned int cursor = 0;
187 xbt_automaton_state_t automaton_state;
189 xbt_dynar_foreach(_mc_property_automaton->states, cursor, automaton_state) {
190 if (automaton_state->type == -1) { /* Initial automaton state */
192 initial_pair = MC_pair_new();
193 initial_pair->automaton_state = automaton_state;
194 initial_pair->graph_state = MC_state_new();
195 initial_pair->atomic_propositions = get_atomic_propositions_values();
196 initial_pair->depth = 1;
198 /* Get enabled processes and insert them in the interleave set of the graph_state */
199 MC_EACH_SIMIX_PROCESS(process,
200 if (MC_process_is_enabled(process)) {
201 MC_state_interleave_process(initial_pair->graph_state, process);
205 initial_pair->requests = MC_state_interleave_size(initial_pair->graph_state);
206 initial_pair->search_cycle = 0;
208 xbt_fifo_unshift(mc_stack, initial_pair);
212 MC_modelcheck_liveness_main();
215 static void MC_modelcheck_liveness_main(void)
217 smx_process_t process = NULL;
218 mc_pair_t current_pair = NULL;
219 int value, res, visited_num = -1;
220 smx_simcall_t req = NULL;
221 xbt_automaton_transition_t transition_succ = NULL;
223 mc_pair_t next_pair = NULL;
224 xbt_dynar_t prop_values = NULL;
225 mc_visited_pair_t reached_pair = NULL;
227 while(xbt_fifo_size(mc_stack) > 0){
229 /* Get current pair */
230 current_pair = (mc_pair_t) xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack));
232 /* Update current state in buchi automaton */
233 _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)) == NULL) {
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 MC_show_stack_liveness(mc_stack);
252 MC_dump_stack_liveness(mc_stack);
253 MC_print_statistics(mc_stats);
254 XBT_INFO("Counter-example depth : %d", counter_example_depth);
255 exit(SIMGRID_EXIT_LIVENESS);
259 /* Pair already visited ? stop the exploration on the current path */
260 if ((current_pair->exploration_started == 0) && (visited_num = is_visited_pair(reached_pair, current_pair)) != -1) {
262 if (dot_output != NULL){
263 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, visited_num, initial_global_state->prev_req);
267 XBT_DEBUG("Pair already visited (equal to pair %d), exploration on the current path stopped.", visited_num);
268 current_pair->requests = 0;
273 req = MC_state_get_request(current_pair->graph_state, &value);
275 if (dot_output != NULL) {
276 if (initial_global_state->prev_pair != 0 && initial_global_state->prev_pair != current_pair->num) {
277 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, current_pair->num, initial_global_state->prev_req);
278 xbt_free(initial_global_state->prev_req);
280 initial_global_state->prev_pair = current_pair->num;
281 initial_global_state->prev_req = MC_request_get_dot_output(req, value);
282 if (current_pair->search_cycle)
283 fprintf(dot_output, "%d [shape=doublecircle];\n", current_pair->num);
287 char* req_str = MC_request_to_string(req, value, MC_REQUEST_SIMIX);
288 XBT_DEBUG("Execute: %s", req_str);
291 /* Set request as executed */
292 MC_state_set_executed_request(current_pair->graph_state, req, value);
294 /* Update mc_stats */
295 mc_stats->executed_transitions++;
296 if(current_pair->exploration_started == 0)
297 mc_stats->visited_pairs++;
299 /* Answer the request */
300 MC_simcall_handle(req, value);
302 /* Wait for requests (schedules processes) */
303 MC_wait_for_requests();
305 current_pair->requests--;
306 current_pair->exploration_started = 1;
308 /* Get values of atomic propositions (variables used in the property formula) */
309 prop_values = get_atomic_propositions_values();
311 /* Evaluate enabled/true transitions in automaton according to atomic propositions values and create new pairs */
312 cursor = xbt_dynar_length(current_pair->automaton_state->out) - 1;
313 while (cursor >= 0) {
314 transition_succ = (xbt_automaton_transition_t)xbt_dynar_get_as(current_pair->automaton_state->out, cursor, xbt_automaton_transition_t);
315 res = MC_automaton_evaluate_label(transition_succ->label, prop_values);
316 if (res == 1 || res == 2) { /* 1 = True transition (always enabled), 2 = enabled transition according to atomic prop values */
317 next_pair = MC_pair_new();
318 next_pair->graph_state = MC_state_new();
319 next_pair->automaton_state = transition_succ->dst;
320 next_pair->atomic_propositions = get_atomic_propositions_values();
321 next_pair->depth = current_pair->depth + 1;
322 /* Get enabled processes and insert them in the interleave set of the next graph_state */
323 MC_EACH_SIMIX_PROCESS(process,
324 if (MC_process_is_enabled(process)) {
325 MC_state_interleave_process(next_pair->graph_state, process);
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 = (mc_pair_t) xbt_fifo_shift(mc_stack)) != NULL) {
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);
367 MC_pair_delete(current_pair);
371 } /* End of if (current_pair->requests > 0) else ... */
373 } /* End of while(xbt_fifo_size(mc_stack) > 0) */
377 void MC_modelcheck_liveness(void)
379 if (mc_reduce_kind == e_mc_reduce_unset)
380 mc_reduce_kind = e_mc_reduce_none;
381 XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
382 MC_automaton_load(_sg_mc_property_file);
383 MC_wait_for_requests();
385 XBT_DEBUG("Starting the liveness algorithm");
388 /* Create exploration stack */
389 mc_stack = xbt_fifo_new();
391 /* Create the initial state */
392 initial_global_state = xbt_new0(s_mc_global_t, 1);
394 MC_pre_modelcheck_liveness();
397 XBT_INFO("No property violation found.");
398 MC_print_statistics(mc_stats);