1 /* Copyright (c) 2008-2012 Da SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "mc_private.h"
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_liveness, mc,
11 "Logging specific to algorithms for liveness properties verification");
13 xbt_dynar_t reached_pairs;
14 xbt_dynar_t successors;
16 int create_dump(int pair)
18 // Try to enable core dumps
19 struct rlimit core_limit;
20 core_limit.rlim_cur = RLIM_INFINITY;
21 core_limit.rlim_max = RLIM_INFINITY;
23 if(setrlimit(RLIMIT_CORE, &core_limit) < 0)
24 fprintf(stderr, "setrlimit: %s\nWarning: core dumps may be truncated or non-existant\n", strerror(errno));
29 // We are the child process -- run the actual program
34 // An error occurred, shouldn't happen
39 // We are the parent process -- wait for the child process to exit
42 if(WIFSIGNALED(status) && WCOREDUMP(status)){
43 char *core_name = malloc(20);
44 sprintf(core_name,"core_%d", pair);
45 rename("core", core_name);
53 int reached(xbt_state_t st){
55 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
59 mc_pair_reached_t new_pair = NULL;
60 new_pair = xbt_new0(s_mc_pair_reached_t, 1);
61 new_pair->nb = xbt_dynar_length(reached_pairs) + 1;
62 new_pair->automaton_state = st;
63 new_pair->prop_ato = xbt_dynar_new(sizeof(int), NULL);
64 new_pair->system_state = xbt_new0(s_mc_snapshot_t, 1);
65 MC_take_snapshot_liveness(new_pair->system_state);
67 /* Get values of propositional symbols */
70 unsigned int cursor = 0;
71 xbt_propositional_symbol_t ps = NULL;
72 xbt_dynar_foreach(_mc_property_automaton->propositional_symbols, cursor, ps){
73 f = (int_f_void_t)ps->function;
75 xbt_dynar_push_as(new_pair->prop_ato, int, res);
80 if(xbt_dynar_is_empty(reached_pairs) || !compare){
83 /* New pair reached */
84 xbt_dynar_push(reached_pairs, &new_pair);
87 create_dump(xbt_dynar_length(reached_pairs));
96 mc_pair_reached_t pair_test = NULL;
98 xbt_dynar_foreach(reached_pairs, cursor, pair_test){
99 XBT_INFO("Pair reached #%d", pair_test->nb);
100 if(automaton_state_compare(pair_test->automaton_state, st) == 0){
101 if(propositional_symbols_compare_value(pair_test->prop_ato, new_pair->prop_ato) == 0){
102 if(snapshot_compare(new_pair->system_state, pair_test->system_state) == 0){
112 XBT_INFO("Different values of propositional symbols");
115 XBT_INFO("Different automaton state");
119 create_dump(xbt_dynar_length(reached_pairs));
121 /* New pair reached */
122 xbt_dynar_push(reached_pairs, &new_pair);
139 void set_pair_reached(xbt_state_t st){
141 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
145 mc_pair_reached_t pair = NULL;
146 pair = xbt_new0(s_mc_pair_reached_t, 1);
147 pair->nb = xbt_dynar_length(reached_pairs) + 1;
148 pair->automaton_state = st;
149 pair->prop_ato = xbt_dynar_new(sizeof(int), NULL);
150 pair->system_state = xbt_new0(s_mc_snapshot_t, 1);
151 MC_take_snapshot_liveness(pair->system_state);
153 /* Get values of propositional symbols */
154 unsigned int cursor = 0;
155 xbt_propositional_symbol_t ps = NULL;
159 xbt_dynar_foreach(_mc_property_automaton->propositional_symbols, cursor, ps){
160 f = (int_f_void_t)ps->function;
162 xbt_dynar_push_as(pair->prop_ato, int, res);
165 xbt_dynar_push(reached_pairs, &pair);
169 create_dump(xbt_dynar_length(reached_pairs));
178 void MC_pair_delete(mc_pair_t pair){
179 xbt_free(pair->graph_state->proc_status);
180 xbt_free(pair->graph_state);
186 int MC_automaton_evaluate_label(xbt_exp_label_t l){
190 int left_res = MC_automaton_evaluate_label(l->u.or_and.left_exp);
191 int right_res = MC_automaton_evaluate_label(l->u.or_and.right_exp);
192 return (left_res || right_res);
195 int left_res = MC_automaton_evaluate_label(l->u.or_and.left_exp);
196 int right_res = MC_automaton_evaluate_label(l->u.or_and.right_exp);
197 return (left_res && right_res);
200 int res = MC_automaton_evaluate_label(l->u.exp_not);
204 unsigned int cursor = 0;
205 xbt_propositional_symbol_t p = NULL;
207 xbt_dynar_foreach(_mc_property_automaton->propositional_symbols, cursor, p){
208 if(strcmp(p->pred, l->u.predicat) == 0){
209 f = (int_f_void_t)p->function;
224 /********************* Double-DFS stateless *******************/
226 void MC_pair_stateless_delete(mc_pair_stateless_t pair){
227 xbt_free(pair->graph_state->proc_status);
228 xbt_free(pair->graph_state);
232 mc_pair_stateless_t new_pair_stateless(mc_state_t sg, xbt_state_t st, int r){
233 mc_pair_stateless_t p = NULL;
234 p = xbt_new0(s_mc_pair_stateless_t, 1);
235 p->automaton_state = st;
238 mc_stats_pair->expanded_pairs++;
242 void MC_ddfs_init(void){
244 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
246 XBT_INFO("**************************************************");
247 XBT_INFO("Double-DFS init");
248 XBT_INFO("**************************************************");
250 mc_pair_stateless_t mc_initial_pair = NULL;
251 mc_state_t initial_graph_state = NULL;
252 smx_process_t process;
255 MC_wait_for_requests();
259 initial_graph_state = MC_state_pair_new();
260 xbt_swag_foreach(process, simix_global->process_list){
261 if(MC_process_is_enabled(process)){
262 MC_state_interleave_process(initial_graph_state, process);
266 reached_pairs = xbt_dynar_new(sizeof(mc_pair_reached_t), NULL);
267 successors = xbt_dynar_new(sizeof(mc_pair_stateless_t), NULL);
269 /* Save the initial state */
270 initial_snapshot_liveness = xbt_new0(s_mc_snapshot_t, 1);
271 MC_take_snapshot_liveness(initial_snapshot_liveness);
275 unsigned int cursor = 0;
278 xbt_dynar_foreach(_mc_property_automaton->states, cursor, state){
279 if(state->type == -1){
282 mc_initial_pair = new_pair_stateless(initial_graph_state, state, MC_state_interleave_size(initial_graph_state));
283 xbt_fifo_unshift(mc_stack_liveness, mc_initial_pair);
287 MC_restore_snapshot(initial_snapshot_liveness);
294 if(state->type == 2){
297 mc_initial_pair = new_pair_stateless(initial_graph_state, state, MC_state_interleave_size(initial_graph_state));
298 xbt_fifo_unshift(mc_stack_liveness, mc_initial_pair);
301 set_pair_reached(state);
304 MC_restore_snapshot(initial_snapshot_liveness);
323 void MC_ddfs(int search_cycle){
325 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
327 smx_process_t process;
328 mc_pair_stateless_t current_pair = NULL;
330 if(xbt_fifo_size(mc_stack_liveness) == 0)
334 /* Get current pair */
335 current_pair = (mc_pair_stateless_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack_liveness));
337 /* Update current state in buchi automaton */
338 _mc_property_automaton->current_state = current_pair->automaton_state;
341 XBT_INFO("********************* ( Depth = %d, search_cycle = %d )", xbt_fifo_size(mc_stack_liveness), search_cycle);
343 mc_stats_pair->visited_pairs++;
348 mc_state_t next_graph_state = NULL;
349 smx_simcall_t req = NULL;
352 xbt_transition_t transition_succ;
353 unsigned int cursor = 0;
356 mc_pair_stateless_t next_pair = NULL;
357 mc_pair_stateless_t pair_succ;
359 if(xbt_fifo_size(mc_stack_liveness) < MAX_DEPTH_LIVENESS){
361 if(current_pair->requests > 0){
363 while((req = MC_state_get_request(current_pair->graph_state, &value)) != NULL){
365 /* Debug information */
367 req_str = MC_request_to_string(req, value);
368 XBT_INFO("Execute: %s", req_str);
371 MC_state_set_executed_request(current_pair->graph_state, req, value);
373 /* Answer the request */
374 SIMIX_simcall_pre(req, value);
376 /* Wait for requests (schedules processes) */
377 MC_wait_for_requests();
381 /* Create the new expanded graph_state */
382 next_graph_state = MC_state_pair_new();
384 /* Get enabled process and insert it in the interleave set of the next graph_state */
385 xbt_swag_foreach(process, simix_global->process_list){
386 if(MC_process_is_enabled(process)){
387 MC_state_interleave_process(next_graph_state, process);
391 xbt_dynar_reset(successors);
397 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
399 res = MC_automaton_evaluate_label(transition_succ->label);
401 if(res == 1){ // enabled transition in automaton
403 next_pair = new_pair_stateless(next_graph_state, transition_succ->dst, MC_state_interleave_size(next_graph_state));
404 xbt_dynar_push(successors, &next_pair);
412 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
414 res = MC_automaton_evaluate_label(transition_succ->label);
416 if(res == 2){ // true transition in automaton
418 next_pair = new_pair_stateless(next_graph_state, transition_succ->dst, MC_state_interleave_size(next_graph_state));
419 xbt_dynar_push(successors, &next_pair);
427 xbt_dynar_foreach(successors, cursor, pair_succ){
429 if(search_cycle == 1){
431 if((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2)){
433 if(reached(pair_succ->automaton_state)){
435 XBT_INFO("Next pair (depth = %d, %u interleave) already reached !", xbt_fifo_size(mc_stack_liveness) + 1, MC_state_interleave_size(pair_succ->graph_state));
437 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
438 XBT_INFO("| ACCEPTANCE CYCLE |");
439 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
440 XBT_INFO("Counter-example that violates formula :");
441 MC_show_stack_liveness(mc_stack_liveness);
442 MC_dump_stack_liveness(mc_stack_liveness);
443 MC_print_statistics_pairs(mc_stats_pair);
448 XBT_INFO("Next pair (depth =%d) -> Acceptance pair : graph=%p, automaton=%p(%s)", xbt_fifo_size(mc_stack_liveness) + 1, pair_succ->graph_state, pair_succ->automaton_state, pair_succ->automaton_state->id);
450 XBT_INFO("Reached pairs : %lu", xbt_dynar_length(reached_pairs));
453 xbt_fifo_unshift(mc_stack_liveness, pair_succ);
456 MC_ddfs(search_cycle);
463 xbt_fifo_unshift(mc_stack_liveness, pair_succ);
466 MC_ddfs(search_cycle);
472 if(((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2))){
474 XBT_INFO("Next pair (depth =%d) -> Acceptance pair : graph=%p, automaton=%p(%s)", xbt_fifo_size(mc_stack_liveness) + 1, pair_succ->graph_state, pair_succ->automaton_state, pair_succ->automaton_state->id);
476 set_pair_reached(pair_succ->automaton_state);
480 XBT_INFO("Reached pairs : %lu", xbt_dynar_length(reached_pairs));
485 xbt_fifo_unshift(mc_stack_liveness, pair_succ);
488 MC_ddfs(search_cycle);
493 /* Restore system before checking others successors */
494 if(cursor != (xbt_dynar_length(successors) - 1))
495 MC_replay_liveness(mc_stack_liveness, 1);
500 if(MC_state_interleave_size(current_pair->graph_state) > 0){
501 XBT_INFO("Backtracking to depth %d", xbt_fifo_size(mc_stack_liveness));
502 MC_replay_liveness(mc_stack_liveness, 0);
507 }else{ /*No request to execute, search evolution in Büchi automaton */
511 /* Create the new expanded graph_state */
512 next_graph_state = MC_state_pair_new();
514 xbt_dynar_reset(successors);
520 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
522 res = MC_automaton_evaluate_label(transition_succ->label);
524 if(res == 1){ // enabled transition in automaton
526 next_pair = new_pair_stateless(next_graph_state, transition_succ->dst, MC_state_interleave_size(next_graph_state));
527 xbt_dynar_push(successors, &next_pair);
535 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
537 res = MC_automaton_evaluate_label(transition_succ->label);
539 if(res == 2){ // true transition in automaton
541 next_pair = new_pair_stateless(next_graph_state, transition_succ->dst, MC_state_interleave_size(next_graph_state));
542 xbt_dynar_push(successors, &next_pair);
550 xbt_dynar_foreach(successors, cursor, pair_succ){
552 if(search_cycle == 1){
554 if((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2)){
556 if(reached(pair_succ->automaton_state)){
558 XBT_INFO("Next pair (depth = %d) already reached !", xbt_fifo_size(mc_stack_liveness) + 1);
560 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
561 XBT_INFO("| ACCEPTANCE CYCLE |");
562 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
563 XBT_INFO("Counter-example that violates formula :");
564 MC_show_stack_liveness(mc_stack_liveness);
565 MC_dump_stack_liveness(mc_stack_liveness);
566 MC_print_statistics_pairs(mc_stats_pair);
571 XBT_INFO("Next pair (depth = %d) -> Acceptance pair : graph=%p, automaton=%p(%s)", xbt_fifo_size(mc_stack_liveness) + 1, pair_succ->graph_state, pair_succ->automaton_state, pair_succ->automaton_state->id);
573 XBT_INFO("Reached pairs : %lu", xbt_dynar_length(reached_pairs));
576 xbt_fifo_unshift(mc_stack_liveness, pair_succ);
579 MC_ddfs(search_cycle);
586 xbt_fifo_unshift(mc_stack_liveness, pair_succ);
589 MC_ddfs(search_cycle);
596 if(((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2))){
598 set_pair_reached(pair_succ->automaton_state);
602 XBT_INFO("Reached pairs : %lu", xbt_dynar_length(reached_pairs));
607 xbt_fifo_unshift(mc_stack_liveness, pair_succ);
610 MC_ddfs(search_cycle);
614 /* Restore system before checking others successors */
615 if(cursor != xbt_dynar_length(successors) - 1)
616 MC_replay_liveness(mc_stack_liveness, 1);
625 XBT_INFO("Max depth reached");
629 if(xbt_fifo_size(mc_stack_liveness) == MAX_DEPTH_LIVENESS ){
630 XBT_INFO("Pair (graph=%p, automaton =%p, search_cycle = %d, depth = %d) shifted in stack, maximum depth reached", current_pair->graph_state, current_pair->automaton_state, search_cycle, xbt_fifo_size(mc_stack_liveness) );
632 XBT_INFO("Pair (graph=%p, automaton =%p, search_cycle = %d, depth = %d) shifted in stack", current_pair->graph_state, current_pair->automaton_state, search_cycle, xbt_fifo_size(mc_stack_liveness) );
637 xbt_fifo_shift(mc_stack_liveness);
638 if((current_pair->automaton_state->type == 1) || (current_pair->automaton_state->type == 2)){
639 xbt_dynar_pop(reached_pairs, NULL);