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"
20 #include "../xbt/mmalloc/mmprivate.h"
22 #include "mc_private.h"
23 #include "mc_record.h"
24 #include "xbt/automaton.h"
27 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
28 "Logging specific to MC (global)");
30 int user_max_depth_reached = 0;
32 /* MC global data structures */
33 mc_state_t mc_current_state = NULL;
34 char mc_replay_mode = FALSE;
35 double *mc_time = NULL;
36 __thread mc_comparison_times_t mc_comp_times = NULL;
37 __thread double mc_snapshot_comparison_time;
38 mc_stats_t mc_stats = NULL;
39 mc_global_t initial_global_state = NULL;
40 xbt_fifo_t mc_stack = NULL;
43 xbt_automaton_t _mc_property_automaton = NULL;
46 mc_object_info_t mc_libsimgrid_info = NULL;
47 mc_object_info_t mc_binary_info = NULL;
49 mc_object_info_t mc_object_infos[2] = { NULL, NULL };
51 size_t mc_object_infos_size = 2;
54 FILE *dot_output = NULL;
55 const char *colors[13];
58 /******************************* Initialisation of MC *******************************/
59 /*********************************************************************************/
61 static void MC_init_dot_output()
62 { /* FIXME : more colors */
67 colors[3] = "goldenrod";
70 colors[6] = "magenta";
71 colors[7] = "turquoise4";
73 colors[9] = "forestgreen";
74 colors[10] = "hotpink";
75 colors[11] = "lightblue";
78 dot_output = fopen(_sg_mc_dot_output_file, "w");
80 if (dot_output == NULL) {
81 perror("Error open dot output file");
86 "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");
90 static void MC_init_debug_info(void)
92 XBT_INFO("Get debug information ...");
94 memory_map_t maps = MC_get_memory_map();
96 /* Get local variables for state equality detection */
97 mc_binary_info = MC_find_object_info(maps, xbt_binary_name, 1);
98 mc_object_infos[0] = mc_binary_info;
100 mc_libsimgrid_info = MC_find_object_info(maps, libsimgrid_path, 0);
101 mc_object_infos[1] = mc_libsimgrid_info;
103 // Use information of the other objects:
104 MC_post_process_object_info(mc_binary_info);
105 MC_post_process_object_info(mc_libsimgrid_info);
107 MC_free_memory_map(maps);
108 XBT_INFO("Get debug information done !");
112 mc_model_checker_t mc_model_checker = NULL;
114 mc_model_checker_t MC_model_checker_new()
116 mc_model_checker_t mc = xbt_new0(s_mc_model_checker_t, 1);
117 mc->pages = mc_pages_store_new();
118 mc->fd_clear_refs = -1;
123 void MC_model_checker_delete(mc_model_checker_t mc) {
124 mc_pages_store_delete(mc->pages);
126 xbt_dynar_free(&mc->record);
131 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
133 mc_time = xbt_new0(double, simix_process_maxpid);
135 /* Initialize the data structures that must be persistent across every
136 iteration of the model-checker (in RAW memory) */
140 mc_model_checker = MC_model_checker_new();
142 mc_comp_times = xbt_new0(s_mc_comparison_times_t, 1);
144 /* Initialize statistics */
145 mc_stats = xbt_new0(s_mc_stats_t, 1);
146 mc_stats->state_size = 1;
148 MC_init_memory_map_info();
149 MC_init_debug_info(); /* FIXME : get debug information only if liveness verification or visited state reduction */
151 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0'))
152 MC_init_dot_output();
155 //parmap = xbt_parmap_mc_new(xbt_os_get_numcores(), XBT_PARMAP_DEFAULT);
159 if (_sg_mc_visited > 0 || _sg_mc_liveness) {
160 /* Ignore some variables from xbt/ex.h used by exception e for stacks comparison */
161 MC_ignore_local_variable("e", "*");
162 MC_ignore_local_variable("__ex_cleanup", "*");
163 MC_ignore_local_variable("__ex_mctx_en", "*");
164 MC_ignore_local_variable("__ex_mctx_me", "*");
165 MC_ignore_local_variable("__xbt_ex_ctx_ptr", "*");
166 MC_ignore_local_variable("_log_ev", "*");
167 MC_ignore_local_variable("_throw_ctx", "*");
168 MC_ignore_local_variable("ctx", "*");
170 MC_ignore_local_variable("self", "simcall_BODY_mc_snapshot");
171 MC_ignore_local_variable("next_cont"
172 "ext", "smx_ctx_sysv_suspend_serial");
173 MC_ignore_local_variable("i", "smx_ctx_sysv_suspend_serial");
175 /* Ignore local variable about time used for tracing */
176 MC_ignore_local_variable("start_time", "*");
178 MC_ignore_global_variable("mc_model_checker");
180 // Mot of those things could be moved into mc_model_checker:
181 MC_ignore_global_variable("compared_pointers");
182 MC_ignore_global_variable("mc_comp_times");
183 MC_ignore_global_variable("mc_snapshot_comparison_time");
184 MC_ignore_global_variable("mc_time");
185 MC_ignore_global_variable("smpi_current_rank");
186 MC_ignore_global_variable("counter"); /* Static variable used for tracing */
187 MC_ignore_global_variable("maestro_stack_start");
188 MC_ignore_global_variable("maestro_stack_end");
189 MC_ignore_global_variable("smx_total_comms");
190 MC_ignore_global_variable("communications_pattern");
191 MC_ignore_global_variable("initial_communications_pattern");
192 MC_ignore_global_variable("incomplete_communications_pattern");
193 MC_ignore_global_variable("mc_diff_info");
195 MC_ignore_heap(mc_time, simix_process_maxpid * sizeof(double));
197 smx_process_t process;
198 xbt_swag_foreach(process, simix_global->process_list) {
199 MC_ignore_heap(&(process->process_hookup),
200 sizeof(process->process_hookup));
209 /******************************* Core of MC *******************************/
210 /**************************************************************************/
212 static void MC_modelcheck_comm_determinism_init(void)
215 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
222 /* Create exploration stack */
223 mc_stack = xbt_fifo_new();
227 MC_pre_modelcheck_comm_determinism();
230 initial_global_state = xbt_new0(s_mc_global_t, 1);
231 initial_global_state->snapshot = MC_take_snapshot(0);
232 initial_global_state->initial_communications_pattern_done = 0;
233 initial_global_state->comm_deterministic = 1;
234 initial_global_state->send_deterministic = 1;
237 MC_modelcheck_comm_determinism();
244 static void MC_modelcheck_safety_init(void)
246 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
255 /* Create exploration stack */
256 mc_stack = xbt_fifo_new();
260 MC_pre_modelcheck_safety();
263 /* Save the initial state */
264 initial_global_state = xbt_new0(s_mc_global_t, 1);
265 initial_global_state->snapshot = MC_take_snapshot(0);
268 MC_modelcheck_safety();
277 static void MC_modelcheck_liveness_init()
280 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
289 /* Create exploration stack */
290 mc_stack = xbt_fifo_new();
292 /* Create the initial state */
293 initial_global_state = xbt_new0(s_mc_global_t, 1);
297 MC_pre_modelcheck_liveness();
300 MC_print_statistics(mc_stats);
308 void MC_do_the_modelcheck_for_real()
311 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
312 XBT_INFO("Check communication determinism");
313 MC_modelcheck_comm_determinism_init();
314 } else if (!_sg_mc_property_file || _sg_mc_property_file[0] == '\0') {
315 if (mc_reduce_kind == e_mc_reduce_unset)
316 mc_reduce_kind = e_mc_reduce_dpor;
317 XBT_INFO("Check a safety property");
318 MC_modelcheck_safety_init();
320 if (mc_reduce_kind == e_mc_reduce_unset)
321 mc_reduce_kind = e_mc_reduce_none;
322 XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
323 MC_automaton_load(_sg_mc_property_file);
324 MC_modelcheck_liveness_init();
337 int MC_deadlock_check()
339 int deadlock = FALSE;
340 smx_process_t process;
341 if (xbt_swag_size(simix_global->process_list)) {
343 xbt_swag_foreach(process, simix_global->process_list) {
344 if (MC_request_is_enabled(&process->simcall)) {
353 void mc_update_comm_pattern(mc_call_type call_type, smx_simcall_t req, int value, xbt_dynar_t pattern) {
355 case MC_CALL_TYPE_NONE:
357 case MC_CALL_TYPE_SEND:
358 case MC_CALL_TYPE_RECV:
359 get_comm_pattern(pattern, req, call_type);
361 case MC_CALL_TYPE_WAIT:
363 smx_synchro_t current_comm = NULL;
364 if (call_type == MC_CALL_TYPE_WAIT)
365 current_comm = simcall_comm_wait__get__comm(req);
367 current_comm = xbt_dynar_get_as(simcall_comm_waitany__get__comms(req), value, smx_synchro_t);
368 // First wait only must be considered:
369 if (current_comm->comm.refcount == 1)
370 complete_comm_pattern(pattern, current_comm);
374 xbt_die("Unexpected call type %i", (int)call_type);
379 * \brief Re-executes from the state at position start all the transitions indicated by
380 * a given model-checker stack.
381 * \param stack The stack with the transitions to execute.
382 * \param start Start index to begin the re-execution.
384 * If start==-1, restore the initial state and replay the actions the
385 * the transitions in the stack.
387 * Otherwise, we only replay a part of the transitions of the stacks
388 * without restoring the state: it is assumed that the current state
389 * match with the transitions to execute.
391 void MC_replay(xbt_fifo_t stack, int start)
393 int raw_mem = (mmalloc_get_current_heap() == mc_heap);
395 int value, i = 1, count = 1, j;
397 smx_simcall_t req = NULL, saved_req = NULL;
398 xbt_fifo_item_t item, start_item;
400 smx_process_t process = NULL;
402 XBT_DEBUG("**** Begin Replay ****");
405 /* Restore the initial state */
406 MC_restore_snapshot(initial_global_state->snapshot);
407 /* At the moment of taking the snapshot the raw heap was set, so restoring
408 * it will set it back again, we have to unset it to continue */
412 start_item = xbt_fifo_get_last_item(stack);
415 start_item = xbt_fifo_get_prev_item(start_item);
422 if (mc_reduce_kind == e_mc_reduce_dpor) {
423 xbt_dict_reset(first_enabled_state);
424 xbt_swag_foreach(process, simix_global->process_list) {
425 if (MC_process_is_enabled(process)) {
426 char *key = bprintf("%lu", process->pid);
427 char *data = bprintf("%d", count);
428 xbt_dict_set(first_enabled_state, key, data, NULL);
434 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
435 for (j=0; j<simix_process_maxpid; j++) {
436 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(communications_pattern, j, xbt_dynar_t));
438 for (j=0; j<simix_process_maxpid; j++) {
439 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
445 /* Traverse the stack from the state at position start and re-execute the transitions */
446 for (item = start_item;
447 item != xbt_fifo_get_first_item(stack);
448 item = xbt_fifo_get_prev_item(item)) {
450 state = (mc_state_t) xbt_fifo_get_item_content(item);
451 saved_req = MC_state_get_executed_request(state, &value);
453 if (mc_reduce_kind == e_mc_reduce_dpor) {
455 char *key = bprintf("%lu", saved_req->issuer->pid);
456 if(xbt_dict_get_or_null(first_enabled_state, key))
457 xbt_dict_remove(first_enabled_state, key);
463 /* because we got a copy of the executed request, we have to fetch the
464 real one, pointed by the request field of the issuer process */
465 req = &saved_req->issuer->simcall;
467 /* Debug information */
468 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
469 req_str = MC_request_to_string(req, value);
470 XBT_DEBUG("Replay: %s (%p)", req_str, state);
474 /* TODO : handle test and testany simcalls */
475 mc_call_type call = MC_CALL_TYPE_NONE;
476 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
477 call = mc_get_call_type(req);
480 SIMIX_simcall_handle(req, value);
482 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
484 mc_update_comm_pattern(call, req, value, communications_pattern);
488 MC_wait_for_requests();
492 if (mc_reduce_kind == e_mc_reduce_dpor) {
494 /* Insert in dict all enabled processes */
495 xbt_swag_foreach(process, simix_global->process_list) {
496 if (MC_process_is_enabled(process) ) {
497 char *key = bprintf("%lu", process->pid);
498 if (xbt_dict_get_or_null(first_enabled_state, key) == NULL) {
499 char *data = bprintf("%d", count);
500 xbt_dict_set(first_enabled_state, key, data, NULL);
509 /* Update statistics */
510 mc_stats->visited_states++;
511 mc_stats->executed_transitions++;
515 XBT_DEBUG("**** End Replay ****");
525 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
528 initial_global_state->raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
530 xbt_fifo_item_t item;
533 XBT_DEBUG("**** Begin Replay ****");
535 /* Restore the initial state */
536 MC_restore_snapshot(initial_global_state->snapshot);
538 /* At the moment of taking the snapshot the raw heap was set, so restoring
539 * it will set it back again, we have to unset it to continue */
540 if (!initial_global_state->raw_mem_set)
543 /* Traverse the stack from the initial state and re-execute the transitions */
544 for (item = xbt_fifo_get_last_item(stack);
545 all_stack ? depth <= xbt_fifo_size(stack) : item != xbt_fifo_get_first_item(stack);
546 item = xbt_fifo_get_prev_item(item)) {
548 mc_pair_t pair = (mc_pair_t) xbt_fifo_get_item_content(item);
550 mc_state_t state = (mc_state_t) pair->graph_state;
551 smx_simcall_t req = NULL, saved_req = NULL;
555 if (pair->requests > 0) {
557 saved_req = MC_state_get_executed_request(state, &value);
558 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
560 if (saved_req != NULL) {
561 /* because we got a copy of the executed request, we have to fetch the
562 real one, pointed by the request field of the issuer process */
563 req = &saved_req->issuer->simcall;
564 //XBT_DEBUG("Req->call %u", req->call);
566 /* Debug information */
567 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
568 req_str = MC_request_to_string(req, value);
569 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
575 SIMIX_simcall_handle(req, value);
576 MC_wait_for_requests();
579 /* Update statistics */
580 mc_stats->visited_pairs++;
581 mc_stats->executed_transitions++;
587 XBT_DEBUG("**** End Replay ****");
589 if (initial_global_state->raw_mem_set)
597 * \brief Dumps the contents of a model-checker's stack and shows the actual
599 * \param stack The stack to dump
601 void MC_dump_stack_safety(xbt_fifo_t stack)
604 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
606 MC_show_stack_safety(stack);
608 if (!_sg_mc_checkpoint) {
613 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
614 MC_state_delete(state);
627 void MC_show_stack_safety(xbt_fifo_t stack)
630 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
636 xbt_fifo_item_t item;
638 char *req_str = NULL;
640 for (item = xbt_fifo_get_last_item(stack);
641 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
642 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
643 req = MC_state_get_executed_request(state, &value);
645 req_str = MC_request_to_string(req, value);
646 XBT_INFO("%s", req_str);
655 void MC_show_deadlock(smx_simcall_t req)
657 /*char *req_str = NULL; */
658 XBT_INFO("**************************");
659 XBT_INFO("*** DEAD-LOCK DETECTED ***");
660 XBT_INFO("**************************");
661 XBT_INFO("Locked request:");
662 /*req_str = MC_request_to_string(req);
663 XBT_INFO("%s", req_str);
664 xbt_free(req_str); */
665 XBT_INFO("Counter-example execution trace:");
666 MC_dump_stack_safety(mc_stack);
667 MC_print_statistics(mc_stats);
671 void MC_show_stack_liveness(xbt_fifo_t stack)
675 xbt_fifo_item_t item;
677 char *req_str = NULL;
679 for (item = xbt_fifo_get_last_item(stack);
680 (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item)))
681 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
682 req = MC_state_get_executed_request(pair->graph_state, &value);
684 if (pair->requests > 0) {
685 req_str = MC_request_to_string(req, value);
686 XBT_INFO("%s", req_str);
689 XBT_INFO("End of system requests but evolution in Büchi automaton");
695 void MC_dump_stack_liveness(xbt_fifo_t stack)
698 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
703 while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
704 MC_pair_delete(pair);
713 void MC_print_statistics(mc_stats_t stats)
715 xbt_mheap_t previous_heap = mmalloc_get_current_heap();
717 if (stats->expanded_pairs == 0) {
718 XBT_INFO("Expanded states = %lu", stats->expanded_states);
719 XBT_INFO("Visited states = %lu", stats->visited_states);
721 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
722 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
724 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
726 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0')) {
727 fprintf(dot_output, "}\n");
730 if (initial_global_state != NULL) {
731 if (_sg_mc_comms_determinism)
732 XBT_INFO("Communication-deterministic : %s",
733 !initial_global_state->comm_deterministic ? "No" : "Yes");
734 if (_sg_mc_send_determinism)
735 XBT_INFO("Send-deterministic : %s",
736 !initial_global_state->send_deterministic ? "No" : "Yes");
738 mmalloc_set_current_heap(previous_heap);
741 void MC_assert(int prop)
743 if (MC_is_active() && !prop) {
744 XBT_INFO("**************************");
745 XBT_INFO("*** PROPERTY NOT VALID ***");
746 XBT_INFO("**************************");
747 XBT_INFO("Counter-example execution trace:");
748 MC_record_dump_path(mc_stack);
749 MC_dump_stack_safety(mc_stack);
750 MC_print_statistics(mc_stats);
757 user_max_depth_reached = 1;
760 void MC_process_clock_add(smx_process_t process, double amount)
762 mc_time[process->pid] += amount;
765 double MC_process_clock_get(smx_process_t process)
769 return mc_time[process->pid];
777 void MC_automaton_load(const char *file)
780 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
784 if (_mc_property_automaton == NULL)
785 _mc_property_automaton = xbt_automaton_new();
787 xbt_automaton_load(_mc_property_automaton, file);
796 void MC_automaton_new_propositional_symbol(const char *id, void *fct)
799 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
803 if (_mc_property_automaton == NULL)
804 _mc_property_automaton = xbt_automaton_new();
806 xbt_automaton_propositional_symbol_new(_mc_property_automaton, id, fct);
815 void MC_dump_stacks(FILE* file)
817 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
821 stack_region_t current_stack;
823 xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
824 unw_context_t * context = (unw_context_t *)current_stack->context;
825 fprintf(file, "Stack %i:\n", nstack);
830 unw_init_local (&c, context);
833 const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
834 fprintf(file, " %i: %s\n", nframe, name);
836 } while(unw_step(&c));