1 /* Copyright (c) 2008-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. */
15 #include "simgrid/sg_config.h"
16 #include "../surf/surf_private.h"
17 #include "../simix/smx_private.h"
19 #include "xbt/automaton.h"
23 #include <libunwind.h>
25 #include "../xbt/mmalloc/mmprivate.h"
26 #include "mc_object_info.h"
27 #include "mc_comm_pattern.h"
28 #include "mc_request.h"
29 #include "mc_safety.h"
30 #include "mc_memory_map.h"
31 #include "mc_snapshot.h"
32 #include "mc_liveness.h"
33 #include "mc_private.h"
36 #include "mc_record.h"
39 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
40 "Logging specific to MC (global)");
42 double *mc_time = NULL;
45 int user_max_depth_reached = 0;
47 /* MC global data structures */
48 mc_state_t mc_current_state = NULL;
49 char mc_replay_mode = FALSE;
51 __thread mc_comparison_times_t mc_comp_times = NULL;
52 __thread double mc_snapshot_comparison_time;
53 mc_stats_t mc_stats = NULL;
54 mc_global_t initial_global_state = NULL;
55 xbt_fifo_t mc_stack = NULL;
58 xbt_automaton_t _mc_property_automaton = NULL;
61 FILE *dot_output = NULL;
62 const char *colors[13];
65 /******************************* Initialisation of MC *******************************/
66 /*********************************************************************************/
68 static void MC_init_dot_output()
69 { /* FIXME : more colors */
74 colors[3] = "goldenrod";
77 colors[6] = "magenta";
78 colors[7] = "turquoise4";
80 colors[9] = "forestgreen";
81 colors[10] = "hotpink";
82 colors[11] = "lightblue";
85 dot_output = fopen(_sg_mc_dot_output_file, "w");
87 if (dot_output == NULL) {
88 perror("Error open dot output file");
93 "digraph graphname{\n fixedsize=true; rankdir=TB; ranksep=.25; edge [fontsize=12]; node [fontsize=10, shape=circle,width=.5 ]; graph [resolution=20, fontsize=10];\n");
97 mc_model_checker_t mc_model_checker = NULL;
99 mc_model_checker_t MC_model_checker_new()
101 mc_model_checker_t mc = xbt_new0(s_mc_model_checker_t, 1);
102 mc->pages = mc_pages_store_new();
103 mc->fd_clear_refs = -1;
105 MC_process_init(&mc->process, getpid());
109 void MC_model_checker_delete(mc_model_checker_t mc) {
110 mc_pages_store_delete(mc->pages);
112 xbt_dynar_free(&mc->record);
113 MC_process_clear(&mc->process);
118 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
120 mc_time = xbt_new0(double, simix_process_maxpid);
122 /* Initialize the data structures that must be persistent across every
123 iteration of the model-checker (in RAW memory) */
127 mc_model_checker = MC_model_checker_new();
129 mc_comp_times = xbt_new0(s_mc_comparison_times_t, 1);
131 /* Initialize statistics */
132 mc_stats = xbt_new0(s_mc_stats_t, 1);
133 mc_stats->state_size = 1;
135 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0'))
136 MC_init_dot_output();
139 //parmap = xbt_parmap_mc_new(xbt_os_get_numcores(), XBT_PARMAP_DEFAULT);
143 if (_sg_mc_visited > 0 || _sg_mc_liveness) {
144 /* Ignore some variables from xbt/ex.h used by exception e for stacks comparison */
145 MC_ignore_local_variable("e", "*");
146 MC_ignore_local_variable("__ex_cleanup", "*");
147 MC_ignore_local_variable("__ex_mctx_en", "*");
148 MC_ignore_local_variable("__ex_mctx_me", "*");
149 MC_ignore_local_variable("__xbt_ex_ctx_ptr", "*");
150 MC_ignore_local_variable("_log_ev", "*");
151 MC_ignore_local_variable("_throw_ctx", "*");
152 MC_ignore_local_variable("ctx", "*");
154 MC_ignore_local_variable("self", "simcall_BODY_mc_snapshot");
155 MC_ignore_local_variable("next_cont"
156 "ext", "smx_ctx_sysv_suspend_serial");
157 MC_ignore_local_variable("i", "smx_ctx_sysv_suspend_serial");
159 /* Ignore local variable about time used for tracing */
160 MC_ignore_local_variable("start_time", "*");
163 MC_ignore_global_variable("mc_model_checker");
164 MC_ignore_global_variable("communications_pattern");
165 MC_ignore_global_variable("initial_communications_pattern");
166 MC_ignore_global_variable("incomplete_communications_pattern");
168 /* MC __thread variables: */
169 MC_ignore_global_variable("mc_diff_info");
170 MC_ignore_global_variable("mc_comp_times");
171 MC_ignore_global_variable("mc_snapshot_comparison_time");
173 /* This MC state is used in MC replay as well: */
174 MC_ignore_global_variable("mc_time");
176 /* Static variable used for tracing */
177 MC_ignore_global_variable("counter");
180 MC_ignore_global_variable("smx_total_comms");
182 MC_ignore_heap(mc_time, simix_process_maxpid * sizeof(double));
184 smx_process_t process;
185 // FIXME, cross-process support (simix_global->process_list)
186 xbt_swag_foreach(process, simix_global->process_list) {
187 MC_ignore_heap(&(process->process_hookup),
188 sizeof(process->process_hookup));
197 /******************************* Core of MC *******************************/
198 /**************************************************************************/
200 static void MC_modelcheck_comm_determinism_init(void)
203 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
210 /* Create exploration stack */
211 mc_stack = xbt_fifo_new();
215 MC_pre_modelcheck_comm_determinism();
218 initial_global_state = xbt_new0(s_mc_global_t, 1);
219 initial_global_state->snapshot = MC_take_snapshot(0);
220 initial_global_state->initial_communications_pattern_done = 0;
221 initial_global_state->comm_deterministic = 1;
222 initial_global_state->send_deterministic = 1;
225 MC_modelcheck_comm_determinism();
232 static void MC_modelcheck_safety_init(void)
234 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
243 /* Create exploration stack */
244 mc_stack = xbt_fifo_new();
248 MC_pre_modelcheck_safety();
251 /* Save the initial state */
252 initial_global_state = xbt_new0(s_mc_global_t, 1);
253 initial_global_state->snapshot = MC_take_snapshot(0);
256 MC_modelcheck_safety();
265 static void MC_modelcheck_liveness_init()
268 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
277 /* Create exploration stack */
278 mc_stack = xbt_fifo_new();
280 /* Create the initial state */
281 initial_global_state = xbt_new0(s_mc_global_t, 1);
285 MC_pre_modelcheck_liveness();
288 MC_print_statistics(mc_stats);
296 void MC_do_the_modelcheck_for_real()
299 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
300 XBT_INFO("Check communication determinism");
301 MC_modelcheck_comm_determinism_init();
302 } else if (!_sg_mc_property_file || _sg_mc_property_file[0] == '\0') {
303 if (mc_reduce_kind == e_mc_reduce_unset)
304 mc_reduce_kind = e_mc_reduce_dpor;
305 XBT_INFO("Check a safety property");
306 MC_modelcheck_safety_init();
308 if (mc_reduce_kind == e_mc_reduce_unset)
309 mc_reduce_kind = e_mc_reduce_none;
310 XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
311 MC_automaton_load(_sg_mc_property_file);
312 MC_modelcheck_liveness_init();
325 int MC_deadlock_check()
327 int deadlock = FALSE;
328 smx_process_t process;
329 if (xbt_swag_size(simix_global->process_list)) {
331 xbt_swag_foreach(process, simix_global->process_list) {
332 if (MC_request_is_enabled(&process->simcall)) {
341 void mc_update_comm_pattern(mc_call_type call_type, smx_simcall_t req, int value, xbt_dynar_t pattern) {
343 case MC_CALL_TYPE_NONE:
345 case MC_CALL_TYPE_SEND:
346 case MC_CALL_TYPE_RECV:
347 get_comm_pattern(pattern, req, call_type);
349 case MC_CALL_TYPE_WAIT:
351 smx_synchro_t current_comm = NULL;
352 if (call_type == MC_CALL_TYPE_WAIT)
353 current_comm = simcall_comm_wait__get__comm(req);
355 current_comm = xbt_dynar_get_as(simcall_comm_waitany__get__comms(req), value, smx_synchro_t);
356 // First wait only must be considered:
357 if (current_comm->comm.refcount == 1)
358 complete_comm_pattern(pattern, current_comm);
362 xbt_die("Unexpected call type %i", (int)call_type);
367 * \brief Re-executes from the state at position start all the transitions indicated by
368 * a given model-checker stack.
369 * \param stack The stack with the transitions to execute.
370 * \param start Start index to begin the re-execution.
372 * If start==-1, restore the initial state and replay the actions the
373 * the transitions in the stack.
375 * Otherwise, we only replay a part of the transitions of the stacks
376 * without restoring the state: it is assumed that the current state
377 * match with the transitions to execute.
379 void MC_replay(xbt_fifo_t stack, int start)
381 int raw_mem = (mmalloc_get_current_heap() == mc_heap);
383 int value, i = 1, count = 1, j;
385 smx_simcall_t req = NULL, saved_req = NULL;
386 xbt_fifo_item_t item, start_item;
388 smx_process_t process = NULL;
390 XBT_DEBUG("**** Begin Replay ****");
393 /* Restore the initial state */
394 MC_restore_snapshot(initial_global_state->snapshot);
395 /* At the moment of taking the snapshot the raw heap was set, so restoring
396 * it will set it back again, we have to unset it to continue */
400 start_item = xbt_fifo_get_last_item(stack);
403 start_item = xbt_fifo_get_prev_item(start_item);
410 if (mc_reduce_kind == e_mc_reduce_dpor) {
411 xbt_dict_reset(first_enabled_state);
412 xbt_swag_foreach(process, simix_global->process_list) {
413 if (MC_process_is_enabled(process)) {
414 char *key = bprintf("%lu", process->pid);
415 char *data = bprintf("%d", count);
416 xbt_dict_set(first_enabled_state, key, data, NULL);
422 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
423 for (j=0; j<simix_process_maxpid; j++) {
424 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(communications_pattern, j, xbt_dynar_t));
426 for (j=0; j<simix_process_maxpid; j++) {
427 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
433 /* Traverse the stack from the state at position start and re-execute the transitions */
434 for (item = start_item;
435 item != xbt_fifo_get_first_item(stack);
436 item = xbt_fifo_get_prev_item(item)) {
438 state = (mc_state_t) xbt_fifo_get_item_content(item);
439 saved_req = MC_state_get_executed_request(state, &value);
441 if (mc_reduce_kind == e_mc_reduce_dpor) {
443 char *key = bprintf("%lu", saved_req->issuer->pid);
444 if(xbt_dict_get_or_null(first_enabled_state, key))
445 xbt_dict_remove(first_enabled_state, key);
451 /* because we got a copy of the executed request, we have to fetch the
452 real one, pointed by the request field of the issuer process */
453 req = &saved_req->issuer->simcall;
455 /* Debug information */
456 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
457 req_str = MC_request_to_string(req, value);
458 XBT_DEBUG("Replay: %s (%p)", req_str, state);
462 /* TODO : handle test and testany simcalls */
463 mc_call_type call = MC_CALL_TYPE_NONE;
464 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
465 call = mc_get_call_type(req);
468 SIMIX_simcall_handle(req, value);
470 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
472 mc_update_comm_pattern(call, req, value, communications_pattern);
476 MC_wait_for_requests();
480 if (mc_reduce_kind == e_mc_reduce_dpor) {
482 /* Insert in dict all enabled processes */
483 xbt_swag_foreach(process, simix_global->process_list) {
484 if (MC_process_is_enabled(process) ) {
485 char *key = bprintf("%lu", process->pid);
486 if (xbt_dict_get_or_null(first_enabled_state, key) == NULL) {
487 char *data = bprintf("%d", count);
488 xbt_dict_set(first_enabled_state, key, data, NULL);
497 /* Update statistics */
498 mc_stats->visited_states++;
499 mc_stats->executed_transitions++;
503 XBT_DEBUG("**** End Replay ****");
513 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
516 initial_global_state->raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
518 xbt_fifo_item_t item;
521 XBT_DEBUG("**** Begin Replay ****");
523 /* Restore the initial state */
524 MC_restore_snapshot(initial_global_state->snapshot);
526 /* At the moment of taking the snapshot the raw heap was set, so restoring
527 * it will set it back again, we have to unset it to continue */
528 if (!initial_global_state->raw_mem_set)
531 /* Traverse the stack from the initial state and re-execute the transitions */
532 for (item = xbt_fifo_get_last_item(stack);
533 all_stack ? depth <= xbt_fifo_size(stack) : item != xbt_fifo_get_first_item(stack);
534 item = xbt_fifo_get_prev_item(item)) {
536 mc_pair_t pair = (mc_pair_t) xbt_fifo_get_item_content(item);
538 mc_state_t state = (mc_state_t) pair->graph_state;
539 smx_simcall_t req = NULL, saved_req = NULL;
543 if (pair->requests > 0) {
545 saved_req = MC_state_get_executed_request(state, &value);
546 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
548 if (saved_req != NULL) {
549 /* because we got a copy of the executed request, we have to fetch the
550 real one, pointed by the request field of the issuer process */
551 req = &saved_req->issuer->simcall;
552 //XBT_DEBUG("Req->call %u", req->call);
554 /* Debug information */
555 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
556 req_str = MC_request_to_string(req, value);
557 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
563 SIMIX_simcall_handle(req, value);
564 MC_wait_for_requests();
567 /* Update statistics */
568 mc_stats->visited_pairs++;
569 mc_stats->executed_transitions++;
575 XBT_DEBUG("**** End Replay ****");
577 if (initial_global_state->raw_mem_set)
585 * \brief Dumps the contents of a model-checker's stack and shows the actual
587 * \param stack The stack to dump
589 void MC_dump_stack_safety(xbt_fifo_t stack)
592 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
594 MC_show_stack_safety(stack);
596 if (!_sg_mc_checkpoint) {
601 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
602 MC_state_delete(state);
615 void MC_show_stack_safety(xbt_fifo_t stack)
618 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
624 xbt_fifo_item_t item;
626 char *req_str = NULL;
628 for (item = xbt_fifo_get_last_item(stack);
629 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
630 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
631 req = MC_state_get_executed_request(state, &value);
633 req_str = MC_request_to_string(req, value);
634 XBT_INFO("%s", req_str);
643 void MC_show_deadlock(smx_simcall_t req)
645 /*char *req_str = NULL; */
646 XBT_INFO("**************************");
647 XBT_INFO("*** DEAD-LOCK DETECTED ***");
648 XBT_INFO("**************************");
649 XBT_INFO("Locked request:");
650 /*req_str = MC_request_to_string(req);
651 XBT_INFO("%s", req_str);
652 xbt_free(req_str); */
653 XBT_INFO("Counter-example execution trace:");
654 MC_dump_stack_safety(mc_stack);
655 MC_print_statistics(mc_stats);
659 void MC_show_stack_liveness(xbt_fifo_t stack)
663 xbt_fifo_item_t item;
665 char *req_str = NULL;
667 for (item = xbt_fifo_get_last_item(stack);
668 (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item)))
669 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
670 req = MC_state_get_executed_request(pair->graph_state, &value);
672 if (pair->requests > 0) {
673 req_str = MC_request_to_string(req, value);
674 XBT_INFO("%s", req_str);
677 XBT_INFO("End of system requests but evolution in Büchi automaton");
683 void MC_dump_stack_liveness(xbt_fifo_t stack)
686 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
691 while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
692 MC_pair_delete(pair);
701 void MC_print_statistics(mc_stats_t stats)
703 xbt_mheap_t previous_heap = mmalloc_get_current_heap();
705 if (stats->expanded_pairs == 0) {
706 XBT_INFO("Expanded states = %lu", stats->expanded_states);
707 XBT_INFO("Visited states = %lu", stats->visited_states);
709 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
710 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
712 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
714 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0')) {
715 fprintf(dot_output, "}\n");
718 if (initial_global_state != NULL) {
719 if (_sg_mc_comms_determinism)
720 XBT_INFO("Communication-deterministic : %s",
721 !initial_global_state->comm_deterministic ? "No" : "Yes");
722 if (_sg_mc_send_determinism)
723 XBT_INFO("Send-deterministic : %s",
724 !initial_global_state->send_deterministic ? "No" : "Yes");
726 mmalloc_set_current_heap(previous_heap);
729 void MC_assert(int prop)
731 if (MC_is_active() && !prop) {
732 XBT_INFO("**************************");
733 XBT_INFO("*** PROPERTY NOT VALID ***");
734 XBT_INFO("**************************");
735 XBT_INFO("Counter-example execution trace:");
736 MC_record_dump_path(mc_stack);
737 MC_dump_stack_safety(mc_stack);
738 MC_print_statistics(mc_stats);
745 user_max_depth_reached = 1;
748 void MC_automaton_load(const char *file)
751 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
755 if (_mc_property_automaton == NULL)
756 _mc_property_automaton = xbt_automaton_new();
758 xbt_automaton_load(_mc_property_automaton, file);
767 void MC_automaton_new_propositional_symbol(const char *id, void *fct)
770 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
774 if (_mc_property_automaton == NULL)
775 _mc_property_automaton = xbt_automaton_new();
777 xbt_automaton_propositional_symbol_new(_mc_property_automaton, id, fct);
786 void MC_dump_stacks(FILE* file)
788 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
792 stack_region_t current_stack;
794 xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
795 unw_context_t * context = (unw_context_t *)current_stack->context;
796 fprintf(file, "Stack %i:\n", nstack);
801 unw_init_local (&c, context);
804 const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
805 fprintf(file, " %i: %s\n", nframe, name);
807 } while(unw_step(&c));
817 double MC_process_clock_get(smx_process_t process)
821 return mc_time[process->pid];
829 void MC_process_clock_add(smx_process_t process, double amount)
831 mc_time[process->pid] += amount;