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. */
7 #include "mc_private.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_liveness, mc,
12 "Logging specific to algorithms for liveness properties verification");
14 /********* Global variables *********/
16 xbt_dynar_t acceptance_pairs;
17 xbt_dynar_t successors;
20 /********* Static functions *********/
22 static xbt_dynar_t get_atomic_propositions_values()
26 unsigned int cursor = 0;
27 xbt_automaton_propositional_symbol_t ps = NULL;
28 xbt_dynar_t values = xbt_dynar_new(sizeof(int), NULL);
30 xbt_dynar_foreach(_mc_property_automaton->propositional_symbols, cursor, ps) {
31 f = (int_f_void_t) ps->function;
33 xbt_dynar_push_as(values, int, res);
40 static mc_visited_pair_t is_reached_acceptance_pair(int pair_num,
47 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
51 mc_visited_pair_t pair = NULL;
52 pair = MC_visited_pair_new(pair_num, automaton_state, atomic_propositions);
53 pair->acceptance_pair = 1;
55 if (xbt_dynar_is_empty(acceptance_pairs)) {
57 xbt_dynar_push(acceptance_pairs, &pair);
61 int min = -1, max = -1, index;
63 mc_visited_pair_t pair_test;
66 index = get_search_interval(acceptance_pairs, pair, &min, &max);
68 if (min != -1 && max != -1) { // Acceptance pair with same number of processes and same heap bytes used exists
70 // Parallell implementation
71 /*res = xbt_parmap_mc_apply(parmap, snapshot_compare, xbt_dynar_get_ptr(acceptance_pairs, min), (max-min)+1, pair);
75 return ((mc_pair_t)xbt_dynar_get_as(acceptance_pairs, (min+res)-1, mc_pair_t))->num;
79 while (cursor <= max) {
81 (mc_visited_pair_t) xbt_dynar_get_as(acceptance_pairs, cursor,
83 if (xbt_automaton_state_compare
84 (pair_test->automaton_state, pair->automaton_state) == 0) {
85 if (xbt_automaton_propositional_symbols_compare_value
86 (pair_test->atomic_propositions,
87 pair->atomic_propositions) == 0) {
88 if (snapshot_compare(pair_test, pair) == 0) {
89 XBT_INFO("Pair %d already reached (equal to pair %d) !",
90 pair->num, pair_test->num);
92 xbt_fifo_shift(mc_stack);
93 if (dot_output != NULL)
94 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n",
95 initial_global_state->prev_pair, pair_test->num,
96 initial_global_state->prev_req);
107 xbt_dynar_insert_at(acceptance_pairs, min, &pair);
110 (mc_visited_pair_t) xbt_dynar_get_as(acceptance_pairs, index,
112 if (pair_test->nb_processes < pair->nb_processes) {
113 xbt_dynar_insert_at(acceptance_pairs, index + 1, &pair);
115 if (pair_test->heap_bytes_used < pair->heap_bytes_used)
116 xbt_dynar_insert_at(acceptance_pairs, index + 1, &pair);
118 xbt_dynar_insert_at(acceptance_pairs, index, &pair);
131 static void remove_acceptance_pair(int pair_num)
134 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
138 unsigned int cursor = 0;
139 mc_visited_pair_t pair_test = NULL;
141 xbt_dynar_foreach(acceptance_pairs, cursor, pair_test) {
142 if (pair_test->num == pair_num) {
147 xbt_dynar_remove_at(acceptance_pairs, cursor, &pair_test);
149 pair_test->acceptance_removed = 1;
151 if (_sg_mc_visited == 0) {
152 MC_visited_pair_delete(pair_test);
153 } else if (pair_test->visited_removed == 1) {
154 MC_visited_pair_delete(pair_test);
162 static int MC_automaton_evaluate_label(xbt_automaton_exp_label_t l,
163 xbt_dynar_t atomic_propositions_values)
169 MC_automaton_evaluate_label(l->u.or_and.left_exp,
170 atomic_propositions_values);
172 MC_automaton_evaluate_label(l->u.or_and.right_exp,
173 atomic_propositions_values);
174 return (left_res || right_res);
178 MC_automaton_evaluate_label(l->u.or_and.left_exp,
179 atomic_propositions_values);
181 MC_automaton_evaluate_label(l->u.or_and.right_exp,
182 atomic_propositions_values);
183 return (left_res && right_res);
187 MC_automaton_evaluate_label(l->u.exp_not, atomic_propositions_values);
191 unsigned int cursor = 0;
192 xbt_automaton_propositional_symbol_t p = NULL;
193 xbt_dynar_foreach(_mc_property_automaton->propositional_symbols, cursor,
195 if (strcmp(p->pred, l->u.predicat) == 0)
196 return (int) xbt_dynar_get_as(atomic_propositions_values, cursor,
209 void MC_pre_modelcheck_liveness(void)
212 initial_global_state->raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
214 mc_pair_t initial_pair = NULL;
215 smx_process_t process;
217 MC_wait_for_requests();
221 acceptance_pairs = xbt_dynar_new(sizeof(mc_visited_pair_t), NULL);
222 visited_pairs = xbt_dynar_new(sizeof(mc_visited_pair_t), NULL);
223 successors = xbt_dynar_new(sizeof(mc_pair_t), NULL);
225 initial_global_state->snapshot = MC_take_snapshot(0);
226 initial_global_state->prev_pair = 0;
230 unsigned int cursor = 0;
231 xbt_automaton_state_t automaton_state;
233 xbt_dynar_foreach(_mc_property_automaton->states, cursor, automaton_state) {
234 if (automaton_state->type == -1) { /* Initial automaton state */
238 initial_pair = MC_pair_new();
239 initial_pair->automaton_state = automaton_state;
240 initial_pair->graph_state = MC_state_new();
241 initial_pair->atomic_propositions = get_atomic_propositions_values();
243 /* Get enabled process and insert it in the interleave set of the graph_state */
244 xbt_swag_foreach(process, simix_global->process_list) {
245 if (MC_process_is_enabled(process)) {
246 MC_state_interleave_process(initial_pair->graph_state, process);
250 initial_pair->requests =
251 MC_state_interleave_size(initial_pair->graph_state);
252 initial_pair->search_cycle = 0;
254 xbt_fifo_unshift(mc_stack, initial_pair);
258 MC_modelcheck_liveness();
261 MC_restore_snapshot(initial_global_state->snapshot);
267 if (initial_global_state->raw_mem_set)
276 void MC_modelcheck_liveness()
279 smx_process_t process;
280 mc_pair_t current_pair = NULL;
282 if (xbt_fifo_size(mc_stack) == 0)
285 /* Get current pair */
287 (mc_pair_t) xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack));
289 /* Update current state in buchi automaton */
290 _mc_property_automaton->current_state = current_pair->automaton_state;
293 ("********************* ( Depth = %d, search_cycle = %d, interleave size %d, pair_num %d)",
294 xbt_fifo_size(mc_stack), current_pair->search_cycle,
295 MC_state_interleave_size(current_pair->graph_state), current_pair->num);
297 mc_stats->visited_pairs++;
300 smx_simcall_t req = NULL;
303 xbt_automaton_transition_t transition_succ;
304 unsigned int cursor = 0;
308 mc_pair_t next_pair = NULL;
309 xbt_dynar_t prop_values = NULL;
310 mc_visited_pair_t reached_pair = NULL;
311 int counter_example_depth = 0;
313 if (xbt_fifo_size(mc_stack) < _sg_mc_max_depth) {
315 if (current_pair->requests > 0) {
317 if (current_pair->search_cycle) {
319 if ((current_pair->automaton_state->type == 1)
320 || (current_pair->automaton_state->type == 2)) {
322 is_reached_acceptance_pair(current_pair->num,
323 current_pair->automaton_state,
324 current_pair->atomic_propositions)) ==
327 counter_example_depth = xbt_fifo_size(mc_stack);
328 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
329 XBT_INFO("| ACCEPTANCE CYCLE |");
330 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
331 XBT_INFO("Counter-example that violates formula :");
332 MC_show_stack_liveness(mc_stack);
333 MC_dump_stack_liveness(mc_stack);
334 MC_print_statistics(mc_stats);
335 XBT_INFO("Counter-example depth : %d", counter_example_depth);
343 is_visited_pair(reached_pair, current_pair->num,
344 current_pair->automaton_state,
345 current_pair->atomic_propositions)) != -1) {
348 if (dot_output != NULL)
349 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n",
350 initial_global_state->prev_pair, visited_num,
351 initial_global_state->prev_req);
357 MC_state_get_request(current_pair->graph_state,
361 if (dot_output != NULL) {
362 if (initial_global_state->prev_pair != 0
363 && initial_global_state->prev_pair != current_pair->num) {
364 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n",
365 initial_global_state->prev_pair, current_pair->num,
366 initial_global_state->prev_req);
367 xbt_free(initial_global_state->prev_req);
369 initial_global_state->prev_pair = current_pair->num;
373 /* Debug information */
374 if (XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)) {
375 req_str = MC_request_to_string(req, value);
376 XBT_DEBUG("Execute: %s", req_str);
381 if (dot_output != NULL) {
382 initial_global_state->prev_req =
383 MC_request_get_dot_output(req, value);
384 if (current_pair->search_cycle)
385 fprintf(dot_output, "%d [shape=doublecircle];\n",
390 MC_state_set_executed_request(current_pair->graph_state, req, value);
391 mc_stats->executed_transitions++;
393 /* Answer the request */
394 SIMIX_simcall_pre(req, value);
396 /* Wait for requests (schedules processes) */
397 MC_wait_for_requests();
400 prop_values = get_atomic_propositions_values();
405 /* Evaluate enabled transition according to atomic propositions values */
407 xbt_dynar_foreach(current_pair->automaton_state->out, cursor,
411 MC_automaton_evaluate_label(transition_succ->label,
414 if (res == 1) { // enabled transition in automaton
417 MC_replay_liveness(mc_stack, 1);
421 next_pair = MC_pair_new();
422 next_pair->graph_state = MC_state_new();
423 next_pair->automaton_state = transition_succ->dst;
424 next_pair->atomic_propositions = get_atomic_propositions_values();
426 /* Get enabled process and insert it in the interleave set of the next graph_state */
427 xbt_swag_foreach(process, simix_global->process_list) {
428 if (MC_process_is_enabled(process)) {
429 MC_state_interleave_process(next_pair->graph_state, process);
433 next_pair->requests =
434 MC_state_interleave_size(next_pair->graph_state);
436 if (next_pair->automaton_state->type == 1
437 || next_pair->automaton_state->type == 2
438 || current_pair->search_cycle)
439 next_pair->search_cycle = 1;
441 xbt_fifo_unshift(mc_stack, next_pair);
443 if (mc_stats->expanded_pairs % 1000000 == 0)
444 XBT_INFO("Expanded pairs : %lu", mc_stats->expanded_pairs);
450 MC_modelcheck_liveness();
456 /* Then, evaluate true transitions (always true, whatever atomic propositions values) */
458 xbt_dynar_foreach(current_pair->automaton_state->out, cursor,
462 MC_automaton_evaluate_label(transition_succ->label,
465 if (res == 2) { // true transition in automaton
468 MC_replay_liveness(mc_stack, 1);
472 next_pair = MC_pair_new();
473 next_pair->graph_state = MC_state_new();
474 next_pair->automaton_state = transition_succ->dst;
475 next_pair->atomic_propositions = get_atomic_propositions_values();
477 /* Get enabled process and insert it in the interleave set of the next graph_state */
478 xbt_swag_foreach(process, simix_global->process_list) {
479 if (MC_process_is_enabled(process)) {
480 MC_state_interleave_process(next_pair->graph_state, process);
484 next_pair->requests =
485 MC_state_interleave_size(next_pair->graph_state);
487 if (next_pair->automaton_state->type == 1
488 || next_pair->automaton_state->type == 2
489 || current_pair->search_cycle)
490 next_pair->search_cycle = 1;
492 xbt_fifo_unshift(mc_stack, next_pair);
494 if (mc_stats->expanded_pairs % 1000000 == 0)
495 XBT_INFO("Expanded pairs : %lu", mc_stats->expanded_pairs);
501 MC_modelcheck_liveness();
507 if (MC_state_interleave_size(current_pair->graph_state) > 0) {
508 XBT_DEBUG("Backtracking to depth %d", xbt_fifo_size(mc_stack));
509 MC_replay_liveness(mc_stack, 0);
520 XBT_WARN("/!\\ Max depth reached ! /!\\ ");
521 if (MC_state_interleave_size(current_pair->graph_state) > 0) {
523 ("/!\\ But, there are still processes to interleave. Model-checker will not be able to ensure the soundness of the verification from now. /!\\ ");
524 if (_sg_mc_max_depth == 1000)
526 ("Notice : the default value of max depth is 1000 but you can change it with cfg=model-check/max_depth:value.");
531 if (xbt_fifo_size(mc_stack) == _sg_mc_max_depth) {
532 XBT_DEBUG("Pair %d (depth = %d) shifted in stack, maximum depth reached",
533 current_pair->num, xbt_fifo_size(mc_stack));
535 XBT_DEBUG("Pair %d (depth = %d) shifted in stack", current_pair->num,
536 xbt_fifo_size(mc_stack));
541 xbt_dynar_free(&prop_values);
542 current_pair = xbt_fifo_shift(mc_stack);
543 if (xbt_fifo_size(mc_stack) != _sg_mc_max_depth - 1
544 && current_pair->requests > 0 && current_pair->search_cycle) {
545 remove_acceptance_pair(current_pair->num);
547 MC_pair_delete(current_pair);