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. */
14 #define UNW_LOCAL_ONLY
15 #include <libunwind.h>
17 #include "simgrid/sg_config.h"
18 #include "../surf/surf_private.h"
19 #include "../simix/smx_private.h"
21 #include "xbt/automaton.h"
25 #include "../xbt/mmalloc/mmprivate.h"
26 #include "mc_private.h"
28 #include "mc_record.h"
30 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
31 "Logging specific to MC (global)");
33 double *mc_time = NULL;
36 int user_max_depth_reached = 0;
38 /* MC global data structures */
39 mc_state_t mc_current_state = NULL;
40 char mc_replay_mode = FALSE;
42 __thread mc_comparison_times_t mc_comp_times = NULL;
43 __thread double mc_snapshot_comparison_time;
44 mc_stats_t mc_stats = NULL;
45 mc_global_t initial_global_state = NULL;
46 xbt_fifo_t mc_stack = NULL;
49 xbt_automaton_t _mc_property_automaton = NULL;
52 mc_object_info_t mc_libsimgrid_info = NULL;
53 mc_object_info_t mc_binary_info = NULL;
55 mc_object_info_t mc_object_infos[2] = { NULL, NULL };
57 size_t mc_object_infos_size = 2;
60 FILE *dot_output = NULL;
61 const char *colors[13];
64 /******************************* Initialisation of MC *******************************/
65 /*********************************************************************************/
67 static void MC_init_dot_output()
68 { /* FIXME : more colors */
73 colors[3] = "goldenrod";
76 colors[6] = "magenta";
77 colors[7] = "turquoise4";
79 colors[9] = "forestgreen";
80 colors[10] = "hotpink";
81 colors[11] = "lightblue";
84 dot_output = fopen(_sg_mc_dot_output_file, "w");
86 if (dot_output == NULL) {
87 perror("Error open dot output file");
92 "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");
96 static void MC_init_debug_info(void)
98 XBT_INFO("Get debug information ...");
100 memory_map_t maps = MC_get_memory_map();
102 /* Get local variables for state equality detection */
103 mc_binary_info = MC_find_object_info(maps, xbt_binary_name, 1);
104 mc_object_infos[0] = mc_binary_info;
106 mc_libsimgrid_info = MC_find_object_info(maps, libsimgrid_path, 0);
107 mc_object_infos[1] = mc_libsimgrid_info;
109 // Use information of the other objects:
110 MC_post_process_object_info(mc_binary_info);
111 MC_post_process_object_info(mc_libsimgrid_info);
113 MC_free_memory_map(maps);
114 XBT_INFO("Get debug information done !");
118 mc_model_checker_t mc_model_checker = NULL;
120 mc_model_checker_t MC_model_checker_new()
122 mc_model_checker_t mc = xbt_new0(s_mc_model_checker_t, 1);
123 mc->pages = mc_pages_store_new();
124 mc->fd_clear_refs = -1;
129 void MC_model_checker_delete(mc_model_checker_t mc) {
130 mc_pages_store_delete(mc->pages);
132 xbt_dynar_free(&mc->record);
137 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
139 mc_time = xbt_new0(double, simix_process_maxpid);
141 /* Initialize the data structures that must be persistent across every
142 iteration of the model-checker (in RAW memory) */
146 mc_model_checker = MC_model_checker_new();
148 mc_comp_times = xbt_new0(s_mc_comparison_times_t, 1);
150 /* Initialize statistics */
151 mc_stats = xbt_new0(s_mc_stats_t, 1);
152 mc_stats->state_size = 1;
154 MC_init_memory_map_info();
155 MC_init_debug_info(); /* FIXME : get debug information only if liveness verification or visited state reduction */
157 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0'))
158 MC_init_dot_output();
161 //parmap = xbt_parmap_mc_new(xbt_os_get_numcores(), XBT_PARMAP_DEFAULT);
165 if (_sg_mc_visited > 0 || _sg_mc_liveness) {
166 /* Ignore some variables from xbt/ex.h used by exception e for stacks comparison */
167 MC_ignore_local_variable("e", "*");
168 MC_ignore_local_variable("__ex_cleanup", "*");
169 MC_ignore_local_variable("__ex_mctx_en", "*");
170 MC_ignore_local_variable("__ex_mctx_me", "*");
171 MC_ignore_local_variable("__xbt_ex_ctx_ptr", "*");
172 MC_ignore_local_variable("_log_ev", "*");
173 MC_ignore_local_variable("_throw_ctx", "*");
174 MC_ignore_local_variable("ctx", "*");
176 MC_ignore_local_variable("self", "simcall_BODY_mc_snapshot");
177 MC_ignore_local_variable("next_cont"
178 "ext", "smx_ctx_sysv_suspend_serial");
179 MC_ignore_local_variable("i", "smx_ctx_sysv_suspend_serial");
181 /* Ignore local variable about time used for tracing */
182 MC_ignore_local_variable("start_time", "*");
184 MC_ignore_global_variable("mc_model_checker");
186 // Mot of those things could be moved into mc_model_checker:
187 MC_ignore_global_variable("compared_pointers");
188 MC_ignore_global_variable("mc_comp_times");
189 MC_ignore_global_variable("mc_snapshot_comparison_time");
190 MC_ignore_global_variable("mc_time");
191 MC_ignore_global_variable("smpi_current_rank");
192 MC_ignore_global_variable("counter"); /* Static variable used for tracing */
193 MC_ignore_global_variable("maestro_stack_start");
194 MC_ignore_global_variable("maestro_stack_end");
195 MC_ignore_global_variable("smx_total_comms");
196 MC_ignore_global_variable("communications_pattern");
197 MC_ignore_global_variable("initial_communications_pattern");
198 MC_ignore_global_variable("incomplete_communications_pattern");
199 MC_ignore_global_variable("mc_diff_info");
201 MC_ignore_heap(mc_time, simix_process_maxpid * sizeof(double));
203 smx_process_t process;
204 xbt_swag_foreach(process, simix_global->process_list) {
205 MC_ignore_heap(&(process->process_hookup),
206 sizeof(process->process_hookup));
215 /******************************* Core of MC *******************************/
216 /**************************************************************************/
218 static void MC_modelcheck_comm_determinism_init(void)
221 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
228 /* Create exploration stack */
229 mc_stack = xbt_fifo_new();
233 MC_pre_modelcheck_comm_determinism();
236 initial_global_state = xbt_new0(s_mc_global_t, 1);
237 initial_global_state->snapshot = MC_take_snapshot(0);
238 initial_global_state->initial_communications_pattern_done = 0;
239 initial_global_state->comm_deterministic = 1;
240 initial_global_state->send_deterministic = 1;
243 MC_modelcheck_comm_determinism();
250 static void MC_modelcheck_safety_init(void)
252 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
261 /* Create exploration stack */
262 mc_stack = xbt_fifo_new();
266 MC_pre_modelcheck_safety();
269 /* Save the initial state */
270 initial_global_state = xbt_new0(s_mc_global_t, 1);
271 initial_global_state->snapshot = MC_take_snapshot(0);
274 MC_modelcheck_safety();
283 static void MC_modelcheck_liveness_init()
286 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
295 /* Create exploration stack */
296 mc_stack = xbt_fifo_new();
298 /* Create the initial state */
299 initial_global_state = xbt_new0(s_mc_global_t, 1);
303 MC_pre_modelcheck_liveness();
306 MC_print_statistics(mc_stats);
314 void MC_do_the_modelcheck_for_real()
317 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
318 XBT_INFO("Check communication determinism");
319 MC_modelcheck_comm_determinism_init();
320 } else if (!_sg_mc_property_file || _sg_mc_property_file[0] == '\0') {
321 if (mc_reduce_kind == e_mc_reduce_unset)
322 mc_reduce_kind = e_mc_reduce_dpor;
323 XBT_INFO("Check a safety property");
324 MC_modelcheck_safety_init();
326 if (mc_reduce_kind == e_mc_reduce_unset)
327 mc_reduce_kind = e_mc_reduce_none;
328 XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
329 MC_automaton_load(_sg_mc_property_file);
330 MC_modelcheck_liveness_init();
343 int MC_deadlock_check()
345 int deadlock = FALSE;
346 smx_process_t process;
347 if (xbt_swag_size(simix_global->process_list)) {
349 xbt_swag_foreach(process, simix_global->process_list) {
350 if (MC_request_is_enabled(&process->simcall)) {
359 void mc_update_comm_pattern(mc_call_type call_type, smx_simcall_t req, int value, xbt_dynar_t pattern) {
361 case MC_CALL_TYPE_NONE:
363 case MC_CALL_TYPE_SEND:
364 case MC_CALL_TYPE_RECV:
365 get_comm_pattern(pattern, req, call_type);
367 case MC_CALL_TYPE_WAIT:
369 smx_synchro_t current_comm = NULL;
370 if (call_type == MC_CALL_TYPE_WAIT)
371 current_comm = simcall_comm_wait__get__comm(req);
373 current_comm = xbt_dynar_get_as(simcall_comm_waitany__get__comms(req), value, smx_synchro_t);
374 // First wait only must be considered:
375 if (current_comm->comm.refcount == 1)
376 complete_comm_pattern(pattern, current_comm);
380 xbt_die("Unexpected call type %i", (int)call_type);
385 * \brief Re-executes from the state at position start all the transitions indicated by
386 * a given model-checker stack.
387 * \param stack The stack with the transitions to execute.
388 * \param start Start index to begin the re-execution.
390 * If start==-1, restore the initial state and replay the actions the
391 * the transitions in the stack.
393 * Otherwise, we only replay a part of the transitions of the stacks
394 * without restoring the state: it is assumed that the current state
395 * match with the transitions to execute.
397 void MC_replay(xbt_fifo_t stack, int start)
399 int raw_mem = (mmalloc_get_current_heap() == mc_heap);
401 int value, i = 1, count = 1, j;
403 smx_simcall_t req = NULL, saved_req = NULL;
404 xbt_fifo_item_t item, start_item;
406 smx_process_t process = NULL;
408 XBT_DEBUG("**** Begin Replay ****");
411 /* Restore the initial state */
412 MC_restore_snapshot(initial_global_state->snapshot);
413 /* At the moment of taking the snapshot the raw heap was set, so restoring
414 * it will set it back again, we have to unset it to continue */
418 start_item = xbt_fifo_get_last_item(stack);
421 start_item = xbt_fifo_get_prev_item(start_item);
428 if (mc_reduce_kind == e_mc_reduce_dpor) {
429 xbt_dict_reset(first_enabled_state);
430 xbt_swag_foreach(process, simix_global->process_list) {
431 if (MC_process_is_enabled(process)) {
432 char *key = bprintf("%lu", process->pid);
433 char *data = bprintf("%d", count);
434 xbt_dict_set(first_enabled_state, key, data, NULL);
440 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
441 for (j=0; j<simix_process_maxpid; j++) {
442 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(communications_pattern, j, xbt_dynar_t));
444 for (j=0; j<simix_process_maxpid; j++) {
445 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
451 /* Traverse the stack from the state at position start and re-execute the transitions */
452 for (item = start_item;
453 item != xbt_fifo_get_first_item(stack);
454 item = xbt_fifo_get_prev_item(item)) {
456 state = (mc_state_t) xbt_fifo_get_item_content(item);
457 saved_req = MC_state_get_executed_request(state, &value);
459 if (mc_reduce_kind == e_mc_reduce_dpor) {
461 char *key = bprintf("%lu", saved_req->issuer->pid);
462 if(xbt_dict_get_or_null(first_enabled_state, key))
463 xbt_dict_remove(first_enabled_state, key);
469 /* because we got a copy of the executed request, we have to fetch the
470 real one, pointed by the request field of the issuer process */
471 req = &saved_req->issuer->simcall;
473 /* Debug information */
474 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
475 req_str = MC_request_to_string(req, value);
476 XBT_DEBUG("Replay: %s (%p)", req_str, state);
480 /* TODO : handle test and testany simcalls */
481 mc_call_type call = MC_CALL_TYPE_NONE;
482 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
483 call = mc_get_call_type(req);
486 SIMIX_simcall_handle(req, value);
488 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
490 mc_update_comm_pattern(call, req, value, communications_pattern);
494 MC_wait_for_requests();
498 if (mc_reduce_kind == e_mc_reduce_dpor) {
500 /* Insert in dict all enabled processes */
501 xbt_swag_foreach(process, simix_global->process_list) {
502 if (MC_process_is_enabled(process) ) {
503 char *key = bprintf("%lu", process->pid);
504 if (xbt_dict_get_or_null(first_enabled_state, key) == NULL) {
505 char *data = bprintf("%d", count);
506 xbt_dict_set(first_enabled_state, key, data, NULL);
515 /* Update statistics */
516 mc_stats->visited_states++;
517 mc_stats->executed_transitions++;
521 XBT_DEBUG("**** End Replay ****");
531 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
534 initial_global_state->raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
536 xbt_fifo_item_t item;
539 XBT_DEBUG("**** Begin Replay ****");
541 /* Restore the initial state */
542 MC_restore_snapshot(initial_global_state->snapshot);
544 /* At the moment of taking the snapshot the raw heap was set, so restoring
545 * it will set it back again, we have to unset it to continue */
546 if (!initial_global_state->raw_mem_set)
549 /* Traverse the stack from the initial state and re-execute the transitions */
550 for (item = xbt_fifo_get_last_item(stack);
551 all_stack ? depth <= xbt_fifo_size(stack) : item != xbt_fifo_get_first_item(stack);
552 item = xbt_fifo_get_prev_item(item)) {
554 mc_pair_t pair = (mc_pair_t) xbt_fifo_get_item_content(item);
556 mc_state_t state = (mc_state_t) pair->graph_state;
557 smx_simcall_t req = NULL, saved_req = NULL;
561 if (pair->requests > 0) {
563 saved_req = MC_state_get_executed_request(state, &value);
564 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
566 if (saved_req != NULL) {
567 /* because we got a copy of the executed request, we have to fetch the
568 real one, pointed by the request field of the issuer process */
569 req = &saved_req->issuer->simcall;
570 //XBT_DEBUG("Req->call %u", req->call);
572 /* Debug information */
573 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
574 req_str = MC_request_to_string(req, value);
575 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
581 SIMIX_simcall_handle(req, value);
582 MC_wait_for_requests();
585 /* Update statistics */
586 mc_stats->visited_pairs++;
587 mc_stats->executed_transitions++;
593 XBT_DEBUG("**** End Replay ****");
595 if (initial_global_state->raw_mem_set)
603 * \brief Dumps the contents of a model-checker's stack and shows the actual
605 * \param stack The stack to dump
607 void MC_dump_stack_safety(xbt_fifo_t stack)
610 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
612 MC_show_stack_safety(stack);
614 if (!_sg_mc_checkpoint) {
619 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
620 MC_state_delete(state);
633 void MC_show_stack_safety(xbt_fifo_t stack)
636 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
642 xbt_fifo_item_t item;
644 char *req_str = NULL;
646 for (item = xbt_fifo_get_last_item(stack);
647 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
648 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
649 req = MC_state_get_executed_request(state, &value);
651 req_str = MC_request_to_string(req, value);
652 XBT_INFO("%s", req_str);
661 void MC_show_deadlock(smx_simcall_t req)
663 /*char *req_str = NULL; */
664 XBT_INFO("**************************");
665 XBT_INFO("*** DEAD-LOCK DETECTED ***");
666 XBT_INFO("**************************");
667 XBT_INFO("Locked request:");
668 /*req_str = MC_request_to_string(req);
669 XBT_INFO("%s", req_str);
670 xbt_free(req_str); */
671 XBT_INFO("Counter-example execution trace:");
672 MC_dump_stack_safety(mc_stack);
673 MC_print_statistics(mc_stats);
677 void MC_show_stack_liveness(xbt_fifo_t stack)
681 xbt_fifo_item_t item;
683 char *req_str = NULL;
685 for (item = xbt_fifo_get_last_item(stack);
686 (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item)))
687 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
688 req = MC_state_get_executed_request(pair->graph_state, &value);
690 if (pair->requests > 0) {
691 req_str = MC_request_to_string(req, value);
692 XBT_INFO("%s", req_str);
695 XBT_INFO("End of system requests but evolution in Büchi automaton");
701 void MC_dump_stack_liveness(xbt_fifo_t stack)
704 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
709 while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
710 MC_pair_delete(pair);
719 void MC_print_statistics(mc_stats_t stats)
721 xbt_mheap_t previous_heap = mmalloc_get_current_heap();
723 if (stats->expanded_pairs == 0) {
724 XBT_INFO("Expanded states = %lu", stats->expanded_states);
725 XBT_INFO("Visited states = %lu", stats->visited_states);
727 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
728 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
730 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
732 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0')) {
733 fprintf(dot_output, "}\n");
736 if (initial_global_state != NULL) {
737 if (_sg_mc_comms_determinism)
738 XBT_INFO("Communication-deterministic : %s",
739 !initial_global_state->comm_deterministic ? "No" : "Yes");
740 if (_sg_mc_send_determinism)
741 XBT_INFO("Send-deterministic : %s",
742 !initial_global_state->send_deterministic ? "No" : "Yes");
744 mmalloc_set_current_heap(previous_heap);
747 void MC_assert(int prop)
749 if (MC_is_active() && !prop) {
750 XBT_INFO("**************************");
751 XBT_INFO("*** PROPERTY NOT VALID ***");
752 XBT_INFO("**************************");
753 XBT_INFO("Counter-example execution trace:");
754 MC_record_dump_path(mc_stack);
755 MC_dump_stack_safety(mc_stack);
756 MC_print_statistics(mc_stats);
763 user_max_depth_reached = 1;
766 void MC_automaton_load(const char *file)
769 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
773 if (_mc_property_automaton == NULL)
774 _mc_property_automaton = xbt_automaton_new();
776 xbt_automaton_load(_mc_property_automaton, file);
785 void MC_automaton_new_propositional_symbol(const char *id, void *fct)
788 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
792 if (_mc_property_automaton == NULL)
793 _mc_property_automaton = xbt_automaton_new();
795 xbt_automaton_propositional_symbol_new(_mc_property_automaton, id, fct);
804 void MC_dump_stacks(FILE* file)
806 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
810 stack_region_t current_stack;
812 xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
813 unw_context_t * context = (unw_context_t *)current_stack->context;
814 fprintf(file, "Stack %i:\n", nstack);
819 unw_init_local (&c, context);
822 const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
823 fprintf(file, " %i: %s\n", nframe, name);
825 } while(unw_step(&c));
835 double MC_process_clock_get(smx_process_t process)
839 return mc_time[process->pid];
847 void MC_process_clock_add(smx_process_t process, double amount)
849 mc_time[process->pid] += amount;