1 /* Copyright (c) 2011-2014. 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 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_liveness, mc,
19 "Logging specific to algorithms for liveness properties verification");
21 /********* Global variables *********/
23 xbt_dynar_t acceptance_pairs;
24 xbt_dynar_t successors;
27 /********* Static functions *********/
29 static xbt_dynar_t get_atomic_propositions_values()
33 unsigned int cursor = 0;
34 xbt_automaton_propositional_symbol_t ps = NULL;
35 xbt_dynar_t values = xbt_dynar_new(sizeof(int), NULL);
36 // FIXME, cross-process support
37 xbt_dynar_foreach(_mc_property_automaton->propositional_symbols, cursor, ps) {
38 f = (int_f_void_t) ps->function;
40 xbt_dynar_push_as(values, int, res);
47 static mc_visited_pair_t is_reached_acceptance_pair(int pair_num,
54 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
58 mc_visited_pair_t pair = NULL;
59 pair = MC_visited_pair_new(pair_num, automaton_state, atomic_propositions);
60 pair->acceptance_pair = 1;
62 if (xbt_dynar_is_empty(acceptance_pairs)) {
64 xbt_dynar_push(acceptance_pairs, &pair);
68 int min = -1, max = -1, index;
70 mc_visited_pair_t pair_test;
73 index = get_search_interval(acceptance_pairs, pair, &min, &max);
75 if (min != -1 && max != -1) { // Acceptance pair with same number of processes and same heap bytes used exists
77 // Parallell implementation
78 /*res = xbt_parmap_mc_apply(parmap, snapshot_compare, xbt_dynar_get_ptr(acceptance_pairs, min), (max-min)+1, pair);
82 return ((mc_pair_t)xbt_dynar_get_as(acceptance_pairs, (min+res)-1, mc_pair_t))->num;
86 while (cursor <= max) {
88 (mc_visited_pair_t) xbt_dynar_get_as(acceptance_pairs, cursor,
90 if (xbt_automaton_state_compare
91 (pair_test->automaton_state, pair->automaton_state) == 0) {
92 if (xbt_automaton_propositional_symbols_compare_value
93 (pair_test->atomic_propositions,
94 pair->atomic_propositions) == 0) {
95 if (snapshot_compare(pair_test, pair) == 0) {
96 XBT_INFO("Pair %d already reached (equal to pair %d) !",
97 pair->num, pair_test->num);
99 xbt_fifo_shift(mc_stack);
100 if (dot_output != NULL)
101 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n",
102 initial_global_state->prev_pair, pair_test->num,
103 initial_global_state->prev_req);
114 xbt_dynar_insert_at(acceptance_pairs, min, &pair);
117 (mc_visited_pair_t) xbt_dynar_get_as(acceptance_pairs, index,
119 if (pair_test->nb_processes < pair->nb_processes) {
120 xbt_dynar_insert_at(acceptance_pairs, index + 1, &pair);
122 if (pair_test->heap_bytes_used < pair->heap_bytes_used)
123 xbt_dynar_insert_at(acceptance_pairs, index + 1, &pair);
125 xbt_dynar_insert_at(acceptance_pairs, index, &pair);
138 static void remove_acceptance_pair(int pair_num)
141 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
145 unsigned int cursor = 0;
146 mc_visited_pair_t pair_test = NULL;
148 xbt_dynar_foreach(acceptance_pairs, cursor, pair_test) {
149 if (pair_test->num == pair_num) {
154 xbt_dynar_remove_at(acceptance_pairs, cursor, &pair_test);
156 pair_test->acceptance_removed = 1;
158 if (_sg_mc_visited == 0) {
159 MC_visited_pair_delete(pair_test);
160 } else if (pair_test->visited_removed == 1) {
161 MC_visited_pair_delete(pair_test);
169 static int MC_automaton_evaluate_label(xbt_automaton_exp_label_t l,
170 xbt_dynar_t atomic_propositions_values)
176 MC_automaton_evaluate_label(l->u.or_and.left_exp,
177 atomic_propositions_values);
179 MC_automaton_evaluate_label(l->u.or_and.right_exp,
180 atomic_propositions_values);
181 return (left_res || right_res);
185 MC_automaton_evaluate_label(l->u.or_and.left_exp,
186 atomic_propositions_values);
188 MC_automaton_evaluate_label(l->u.or_and.right_exp,
189 atomic_propositions_values);
190 return (left_res && right_res);
194 MC_automaton_evaluate_label(l->u.exp_not, atomic_propositions_values);
198 unsigned int cursor = 0;
199 xbt_automaton_propositional_symbol_t p = NULL;
200 xbt_dynar_foreach(_mc_property_automaton->propositional_symbols, cursor,
202 if (strcmp(p->pred, l->u.predicat) == 0)
203 return (int) xbt_dynar_get_as(atomic_propositions_values, cursor,
216 void MC_pre_modelcheck_liveness(void)
219 initial_global_state->raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
221 mc_pair_t initial_pair = NULL;
222 smx_process_t process;
224 MC_wait_for_requests();
228 acceptance_pairs = xbt_dynar_new(sizeof(mc_visited_pair_t), NULL);
229 visited_pairs = xbt_dynar_new(sizeof(mc_visited_pair_t), NULL);
230 successors = xbt_dynar_new(sizeof(mc_pair_t), NULL);
232 initial_global_state->snapshot = MC_take_snapshot(0);
233 initial_global_state->prev_pair = 0;
237 unsigned int cursor = 0;
238 xbt_automaton_state_t automaton_state;
240 xbt_dynar_foreach(_mc_property_automaton->states, cursor, automaton_state) {
241 if (automaton_state->type == -1) { /* Initial automaton state */
245 initial_pair = MC_pair_new();
246 initial_pair->automaton_state = automaton_state;
247 initial_pair->graph_state = MC_state_new();
248 initial_pair->atomic_propositions = get_atomic_propositions_values();
250 /* Get enabled processes and insert them in the interleave set of the graph_state */
251 xbt_swag_foreach(process, simix_global->process_list) {
252 if (MC_process_is_enabled(process)) {
253 MC_state_interleave_process(initial_pair->graph_state, process);
257 initial_pair->requests =
258 MC_state_interleave_size(initial_pair->graph_state);
259 initial_pair->search_cycle = 0;
261 xbt_fifo_unshift(mc_stack, initial_pair);
265 MC_modelcheck_liveness();
268 MC_restore_snapshot(initial_global_state->snapshot);
274 if (initial_global_state->raw_mem_set)
283 void MC_modelcheck_liveness()
286 smx_process_t process;
287 mc_pair_t current_pair = NULL;
289 if (xbt_fifo_size(mc_stack) == 0)
292 /* Get current pair */
294 (mc_pair_t) xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack));
296 /* Update current state in buchi automaton */
297 _mc_property_automaton->current_state = current_pair->automaton_state;
300 ("********************* ( Depth = %d, search_cycle = %d, interleave size %d, pair_num %d)",
301 xbt_fifo_size(mc_stack), current_pair->search_cycle,
302 MC_state_interleave_size(current_pair->graph_state), current_pair->num);
304 mc_stats->visited_pairs++;
307 smx_simcall_t req = NULL;
309 xbt_automaton_transition_t transition_succ;
310 unsigned int cursor = 0;
314 mc_pair_t next_pair = NULL;
315 xbt_dynar_t prop_values = NULL;
316 mc_visited_pair_t reached_pair = NULL;
317 int counter_example_depth = 0;
319 if (xbt_fifo_size(mc_stack) < _sg_mc_max_depth) {
321 if (current_pair->requests > 0) {
323 if (current_pair->search_cycle) {
325 if ((current_pair->automaton_state->type == 1)
326 || (current_pair->automaton_state->type == 2)) {
328 is_reached_acceptance_pair(current_pair->num,
329 current_pair->automaton_state,
330 current_pair->atomic_propositions)) ==
333 counter_example_depth = xbt_fifo_size(mc_stack);
334 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
335 XBT_INFO("| ACCEPTANCE CYCLE |");
336 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
337 XBT_INFO("Counter-example that violates formula :");
338 MC_show_stack_liveness(mc_stack);
339 MC_dump_stack_liveness(mc_stack);
340 MC_print_statistics(mc_stats);
341 XBT_INFO("Counter-example depth : %d", counter_example_depth);
349 is_visited_pair(reached_pair, current_pair->num,
350 current_pair->automaton_state,
351 current_pair->atomic_propositions)) != -1) {
354 if (dot_output != NULL)
355 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n",
356 initial_global_state->prev_pair, visited_num,
357 initial_global_state->prev_req);
363 MC_state_get_request(current_pair->graph_state,
367 if (dot_output != NULL) {
368 if (initial_global_state->prev_pair != 0
369 && initial_global_state->prev_pair != current_pair->num) {
370 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n",
371 initial_global_state->prev_pair, current_pair->num,
372 initial_global_state->prev_req);
373 xbt_free(initial_global_state->prev_req);
375 initial_global_state->prev_pair = current_pair->num;
379 MC_LOG_REQUEST(mc_liveness, req, value);
382 if (dot_output != NULL) {
383 initial_global_state->prev_req =
384 MC_request_get_dot_output(req, value);
385 if (current_pair->search_cycle)
386 fprintf(dot_output, "%d [shape=doublecircle];\n",
391 MC_state_set_executed_request(current_pair->graph_state, req, value);
392 mc_stats->executed_transitions++;
394 /* Answer the request */
395 SIMIX_simcall_handle(req, value);
397 /* Wait for requests (schedules processes) */
398 MC_wait_for_requests();
401 prop_values = get_atomic_propositions_values();
406 /* Evaluate enabled transition according to atomic propositions values */
408 xbt_dynar_foreach(current_pair->automaton_state->out, cursor,
412 MC_automaton_evaluate_label(transition_succ->label,
415 if (res == 1) { // enabled transition in automaton
418 MC_replay_liveness(mc_stack, 1);
422 next_pair = MC_pair_new();
423 next_pair->graph_state = MC_state_new();
424 next_pair->automaton_state = transition_succ->dst;
425 next_pair->atomic_propositions = get_atomic_propositions_values();
427 /* Get enabled processes and insert them in the interleave set of the next graph_state */
428 xbt_swag_foreach(process, simix_global->process_list) {
429 if (MC_process_is_enabled(process)) {
430 MC_state_interleave_process(next_pair->graph_state, process);
434 next_pair->requests =
435 MC_state_interleave_size(next_pair->graph_state);
437 if (next_pair->automaton_state->type == 1
438 || next_pair->automaton_state->type == 2
439 || current_pair->search_cycle)
440 next_pair->search_cycle = 1;
442 xbt_fifo_unshift(mc_stack, next_pair);
444 if (mc_stats->expanded_pairs % 1000000 == 0)
445 XBT_INFO("Expanded pairs : %lu", mc_stats->expanded_pairs);
451 MC_modelcheck_liveness();
457 /* Then, evaluate true transitions (always true, whatever atomic propositions values) */
459 xbt_dynar_foreach(current_pair->automaton_state->out, cursor,
463 MC_automaton_evaluate_label(transition_succ->label,
466 if (res == 2) { // true transition in automaton
469 MC_replay_liveness(mc_stack, 1);
473 next_pair = MC_pair_new();
474 next_pair->graph_state = MC_state_new();
475 next_pair->automaton_state = transition_succ->dst;
476 next_pair->atomic_propositions = get_atomic_propositions_values();
478 /* Get enabled process and insert it in the interleave set of the next graph_state */
479 xbt_swag_foreach(process, simix_global->process_list) {
480 if (MC_process_is_enabled(process)) {
481 MC_state_interleave_process(next_pair->graph_state, process);
485 next_pair->requests =
486 MC_state_interleave_size(next_pair->graph_state);
488 if (next_pair->automaton_state->type == 1
489 || next_pair->automaton_state->type == 2
490 || current_pair->search_cycle)
491 next_pair->search_cycle = 1;
493 xbt_fifo_unshift(mc_stack, next_pair);
495 if (mc_stats->expanded_pairs % 1000000 == 0)
496 XBT_INFO("Expanded pairs : %lu", mc_stats->expanded_pairs);
502 MC_modelcheck_liveness();
508 if (MC_state_interleave_size(current_pair->graph_state) > 0) {
509 XBT_DEBUG("Backtracking to depth %d", xbt_fifo_size(mc_stack));
510 MC_replay_liveness(mc_stack, 0);
521 XBT_WARN("/!\\ Max depth reached ! /!\\ ");
522 if (MC_state_interleave_size(current_pair->graph_state) > 0) {
524 ("/!\\ But, there are still processes to interleave. Model-checker will not be able to ensure the soundness of the verification from now. /!\\ ");
525 if (_sg_mc_max_depth == 1000)
527 ("Notice : the default value of max depth is 1000 but you can change it with cfg=model-check/max_depth:value.");
532 if (xbt_fifo_size(mc_stack) == _sg_mc_max_depth) {
533 XBT_DEBUG("Pair %d (depth = %d) shifted in stack, maximum depth reached",
534 current_pair->num, xbt_fifo_size(mc_stack));
536 XBT_DEBUG("Pair %d (depth = %d) shifted in stack", current_pair->num,
537 xbt_fifo_size(mc_stack));
542 xbt_dynar_free(&prop_values);
543 current_pair = xbt_fifo_shift(mc_stack);
544 if (xbt_fifo_size(mc_stack) != _sg_mc_max_depth - 1
545 && current_pair->requests > 0 && current_pair->search_cycle) {
546 remove_acceptance_pair(current_pair->num);
548 MC_pair_delete(current_pair);