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", "*");
185 MC_ignore_global_variable("mc_model_checker");
186 MC_ignore_global_variable("communications_pattern");
187 MC_ignore_global_variable("initial_communications_pattern");
188 MC_ignore_global_variable("incomplete_communications_pattern");
190 /* MC __thread variables: */
191 MC_ignore_global_variable("mc_diff_info");
192 MC_ignore_global_variable("mc_comp_times");
193 MC_ignore_global_variable("mc_snapshot_comparison_time");
195 /* This MC state is used in MC replay as well: */
196 MC_ignore_global_variable("mc_time");
198 /* Static variable used for tracing */
199 MC_ignore_global_variable("counter");
202 MC_ignore_global_variable("smx_total_comms");
204 MC_ignore_heap(mc_time, simix_process_maxpid * sizeof(double));
206 smx_process_t process;
207 xbt_swag_foreach(process, simix_global->process_list) {
208 MC_ignore_heap(&(process->process_hookup),
209 sizeof(process->process_hookup));
218 /******************************* Core of MC *******************************/
219 /**************************************************************************/
221 static void MC_modelcheck_comm_determinism_init(void)
224 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
231 /* Create exploration stack */
232 mc_stack = xbt_fifo_new();
236 MC_pre_modelcheck_comm_determinism();
239 initial_global_state = xbt_new0(s_mc_global_t, 1);
240 initial_global_state->snapshot = MC_take_snapshot(0);
241 initial_global_state->initial_communications_pattern_done = 0;
242 initial_global_state->comm_deterministic = 1;
243 initial_global_state->send_deterministic = 1;
246 MC_modelcheck_comm_determinism();
253 static void MC_modelcheck_safety_init(void)
255 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
264 /* Create exploration stack */
265 mc_stack = xbt_fifo_new();
269 MC_pre_modelcheck_safety();
272 /* Save the initial state */
273 initial_global_state = xbt_new0(s_mc_global_t, 1);
274 initial_global_state->snapshot = MC_take_snapshot(0);
277 MC_modelcheck_safety();
286 static void MC_modelcheck_liveness_init()
289 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
298 /* Create exploration stack */
299 mc_stack = xbt_fifo_new();
301 /* Create the initial state */
302 initial_global_state = xbt_new0(s_mc_global_t, 1);
306 MC_pre_modelcheck_liveness();
309 MC_print_statistics(mc_stats);
317 void MC_do_the_modelcheck_for_real()
320 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
321 XBT_INFO("Check communication determinism");
322 MC_modelcheck_comm_determinism_init();
323 } else if (!_sg_mc_property_file || _sg_mc_property_file[0] == '\0') {
324 if (mc_reduce_kind == e_mc_reduce_unset)
325 mc_reduce_kind = e_mc_reduce_dpor;
326 XBT_INFO("Check a safety property");
327 MC_modelcheck_safety_init();
329 if (mc_reduce_kind == e_mc_reduce_unset)
330 mc_reduce_kind = e_mc_reduce_none;
331 XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
332 MC_automaton_load(_sg_mc_property_file);
333 MC_modelcheck_liveness_init();
346 int MC_deadlock_check()
348 int deadlock = FALSE;
349 smx_process_t process;
350 if (xbt_swag_size(simix_global->process_list)) {
352 xbt_swag_foreach(process, simix_global->process_list) {
353 if (MC_request_is_enabled(&process->simcall)) {
362 void mc_update_comm_pattern(mc_call_type call_type, smx_simcall_t req, int value, xbt_dynar_t pattern) {
364 case MC_CALL_TYPE_NONE:
366 case MC_CALL_TYPE_SEND:
367 case MC_CALL_TYPE_RECV:
368 get_comm_pattern(pattern, req, call_type);
370 case MC_CALL_TYPE_WAIT:
372 smx_synchro_t current_comm = NULL;
373 if (call_type == MC_CALL_TYPE_WAIT)
374 current_comm = simcall_comm_wait__get__comm(req);
376 current_comm = xbt_dynar_get_as(simcall_comm_waitany__get__comms(req), value, smx_synchro_t);
377 // First wait only must be considered:
378 if (current_comm->comm.refcount == 1)
379 complete_comm_pattern(pattern, current_comm);
383 xbt_die("Unexpected call type %i", (int)call_type);
388 * \brief Re-executes from the state at position start all the transitions indicated by
389 * a given model-checker stack.
390 * \param stack The stack with the transitions to execute.
391 * \param start Start index to begin the re-execution.
393 * If start==-1, restore the initial state and replay the actions the
394 * the transitions in the stack.
396 * Otherwise, we only replay a part of the transitions of the stacks
397 * without restoring the state: it is assumed that the current state
398 * match with the transitions to execute.
400 void MC_replay(xbt_fifo_t stack, int start)
402 int raw_mem = (mmalloc_get_current_heap() == mc_heap);
404 int value, i = 1, count = 1, j;
406 smx_simcall_t req = NULL, saved_req = NULL;
407 xbt_fifo_item_t item, start_item;
409 smx_process_t process = NULL;
411 XBT_DEBUG("**** Begin Replay ****");
414 /* Restore the initial state */
415 MC_restore_snapshot(initial_global_state->snapshot);
416 /* At the moment of taking the snapshot the raw heap was set, so restoring
417 * it will set it back again, we have to unset it to continue */
421 start_item = xbt_fifo_get_last_item(stack);
424 start_item = xbt_fifo_get_prev_item(start_item);
431 if (mc_reduce_kind == e_mc_reduce_dpor) {
432 xbt_dict_reset(first_enabled_state);
433 xbt_swag_foreach(process, simix_global->process_list) {
434 if (MC_process_is_enabled(process)) {
435 char *key = bprintf("%lu", process->pid);
436 char *data = bprintf("%d", count);
437 xbt_dict_set(first_enabled_state, key, data, NULL);
443 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
444 for (j=0; j<simix_process_maxpid; j++) {
445 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(communications_pattern, j, xbt_dynar_t));
447 for (j=0; j<simix_process_maxpid; j++) {
448 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
454 /* Traverse the stack from the state at position start and re-execute the transitions */
455 for (item = start_item;
456 item != xbt_fifo_get_first_item(stack);
457 item = xbt_fifo_get_prev_item(item)) {
459 state = (mc_state_t) xbt_fifo_get_item_content(item);
460 saved_req = MC_state_get_executed_request(state, &value);
462 if (mc_reduce_kind == e_mc_reduce_dpor) {
464 char *key = bprintf("%lu", saved_req->issuer->pid);
465 if(xbt_dict_get_or_null(first_enabled_state, key))
466 xbt_dict_remove(first_enabled_state, key);
472 /* because we got a copy of the executed request, we have to fetch the
473 real one, pointed by the request field of the issuer process */
474 req = &saved_req->issuer->simcall;
476 /* Debug information */
477 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
478 req_str = MC_request_to_string(req, value);
479 XBT_DEBUG("Replay: %s (%p)", req_str, state);
483 /* TODO : handle test and testany simcalls */
484 mc_call_type call = MC_CALL_TYPE_NONE;
485 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
486 call = mc_get_call_type(req);
489 SIMIX_simcall_handle(req, value);
491 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
493 mc_update_comm_pattern(call, req, value, communications_pattern);
497 MC_wait_for_requests();
501 if (mc_reduce_kind == e_mc_reduce_dpor) {
503 /* Insert in dict all enabled processes */
504 xbt_swag_foreach(process, simix_global->process_list) {
505 if (MC_process_is_enabled(process) ) {
506 char *key = bprintf("%lu", process->pid);
507 if (xbt_dict_get_or_null(first_enabled_state, key) == NULL) {
508 char *data = bprintf("%d", count);
509 xbt_dict_set(first_enabled_state, key, data, NULL);
518 /* Update statistics */
519 mc_stats->visited_states++;
520 mc_stats->executed_transitions++;
524 XBT_DEBUG("**** End Replay ****");
534 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
537 initial_global_state->raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
539 xbt_fifo_item_t item;
542 XBT_DEBUG("**** Begin Replay ****");
544 /* Restore the initial state */
545 MC_restore_snapshot(initial_global_state->snapshot);
547 /* At the moment of taking the snapshot the raw heap was set, so restoring
548 * it will set it back again, we have to unset it to continue */
549 if (!initial_global_state->raw_mem_set)
552 /* Traverse the stack from the initial state and re-execute the transitions */
553 for (item = xbt_fifo_get_last_item(stack);
554 all_stack ? depth <= xbt_fifo_size(stack) : item != xbt_fifo_get_first_item(stack);
555 item = xbt_fifo_get_prev_item(item)) {
557 mc_pair_t pair = (mc_pair_t) xbt_fifo_get_item_content(item);
559 mc_state_t state = (mc_state_t) pair->graph_state;
560 smx_simcall_t req = NULL, saved_req = NULL;
564 if (pair->requests > 0) {
566 saved_req = MC_state_get_executed_request(state, &value);
567 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
569 if (saved_req != NULL) {
570 /* because we got a copy of the executed request, we have to fetch the
571 real one, pointed by the request field of the issuer process */
572 req = &saved_req->issuer->simcall;
573 //XBT_DEBUG("Req->call %u", req->call);
575 /* Debug information */
576 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
577 req_str = MC_request_to_string(req, value);
578 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
584 SIMIX_simcall_handle(req, value);
585 MC_wait_for_requests();
588 /* Update statistics */
589 mc_stats->visited_pairs++;
590 mc_stats->executed_transitions++;
596 XBT_DEBUG("**** End Replay ****");
598 if (initial_global_state->raw_mem_set)
606 * \brief Dumps the contents of a model-checker's stack and shows the actual
608 * \param stack The stack to dump
610 void MC_dump_stack_safety(xbt_fifo_t stack)
613 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
615 MC_show_stack_safety(stack);
617 if (!_sg_mc_checkpoint) {
622 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
623 MC_state_delete(state);
636 void MC_show_stack_safety(xbt_fifo_t stack)
639 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
645 xbt_fifo_item_t item;
647 char *req_str = NULL;
649 for (item = xbt_fifo_get_last_item(stack);
650 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
651 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
652 req = MC_state_get_executed_request(state, &value);
654 req_str = MC_request_to_string(req, value);
655 XBT_INFO("%s", req_str);
664 void MC_show_deadlock(smx_simcall_t req)
666 /*char *req_str = NULL; */
667 XBT_INFO("**************************");
668 XBT_INFO("*** DEAD-LOCK DETECTED ***");
669 XBT_INFO("**************************");
670 XBT_INFO("Locked request:");
671 /*req_str = MC_request_to_string(req);
672 XBT_INFO("%s", req_str);
673 xbt_free(req_str); */
674 XBT_INFO("Counter-example execution trace:");
675 MC_dump_stack_safety(mc_stack);
676 MC_print_statistics(mc_stats);
680 void MC_show_stack_liveness(xbt_fifo_t stack)
684 xbt_fifo_item_t item;
686 char *req_str = NULL;
688 for (item = xbt_fifo_get_last_item(stack);
689 (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item)))
690 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
691 req = MC_state_get_executed_request(pair->graph_state, &value);
693 if (pair->requests > 0) {
694 req_str = MC_request_to_string(req, value);
695 XBT_INFO("%s", req_str);
698 XBT_INFO("End of system requests but evolution in Büchi automaton");
704 void MC_dump_stack_liveness(xbt_fifo_t stack)
707 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
712 while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
713 MC_pair_delete(pair);
722 void MC_print_statistics(mc_stats_t stats)
724 xbt_mheap_t previous_heap = mmalloc_get_current_heap();
726 if (stats->expanded_pairs == 0) {
727 XBT_INFO("Expanded states = %lu", stats->expanded_states);
728 XBT_INFO("Visited states = %lu", stats->visited_states);
730 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
731 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
733 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
735 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0')) {
736 fprintf(dot_output, "}\n");
739 if (initial_global_state != NULL) {
740 if (_sg_mc_comms_determinism)
741 XBT_INFO("Communication-deterministic : %s",
742 !initial_global_state->comm_deterministic ? "No" : "Yes");
743 if (_sg_mc_send_determinism)
744 XBT_INFO("Send-deterministic : %s",
745 !initial_global_state->send_deterministic ? "No" : "Yes");
747 mmalloc_set_current_heap(previous_heap);
750 void MC_assert(int prop)
752 if (MC_is_active() && !prop) {
753 XBT_INFO("**************************");
754 XBT_INFO("*** PROPERTY NOT VALID ***");
755 XBT_INFO("**************************");
756 XBT_INFO("Counter-example execution trace:");
757 MC_record_dump_path(mc_stack);
758 MC_dump_stack_safety(mc_stack);
759 MC_print_statistics(mc_stats);
766 user_max_depth_reached = 1;
769 void MC_automaton_load(const char *file)
772 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
776 if (_mc_property_automaton == NULL)
777 _mc_property_automaton = xbt_automaton_new();
779 xbt_automaton_load(_mc_property_automaton, file);
788 void MC_automaton_new_propositional_symbol(const char *id, void *fct)
791 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
795 if (_mc_property_automaton == NULL)
796 _mc_property_automaton = xbt_automaton_new();
798 xbt_automaton_propositional_symbol_new(_mc_property_automaton, id, fct);
807 void MC_dump_stacks(FILE* file)
809 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
813 stack_region_t current_stack;
815 xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
816 unw_context_t * context = (unw_context_t *)current_stack->context;
817 fprintf(file, "Stack %i:\n", nstack);
822 unw_init_local (&c, context);
825 const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
826 fprintf(file, " %i: %s\n", nframe, name);
828 } while(unw_step(&c));
838 double MC_process_clock_get(smx_process_t process)
842 return mc_time[process->pid];
850 void MC_process_clock_add(smx_process_t process, double amount)
852 mc_time[process->pid] += amount;