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. */
10 #include <sys/types.h>
16 #define UNW_LOCAL_ONLY
17 #include <libunwind.h>
20 #include "simgrid/sg_config.h"
21 #include "../surf/surf_private.h"
22 #include "../simix/smx_private.h"
24 #include "xbt/automaton.h"
28 #include "../xbt/mmalloc/mmprivate.h"
29 #include "mc_private.h"
31 #include "mc_record.h"
33 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
34 "Logging specific to MC (global)");
36 double *mc_time = NULL;
39 int user_max_depth_reached = 0;
41 /* MC global data structures */
42 mc_state_t mc_current_state = NULL;
43 char mc_replay_mode = FALSE;
45 __thread mc_comparison_times_t mc_comp_times = NULL;
46 __thread double mc_snapshot_comparison_time;
47 mc_stats_t mc_stats = NULL;
48 mc_global_t initial_global_state = NULL;
49 xbt_fifo_t mc_stack = NULL;
52 xbt_automaton_t _mc_property_automaton = NULL;
55 mc_object_info_t mc_libsimgrid_info = NULL;
56 mc_object_info_t mc_binary_info = NULL;
58 mc_object_info_t mc_object_infos[2] = { NULL, NULL };
60 size_t mc_object_infos_size = 2;
63 FILE *dot_output = NULL;
64 const char *colors[13];
67 /******************************* Initialisation of MC *******************************/
68 /*********************************************************************************/
70 static void MC_init_dot_output()
71 { /* FIXME : more colors */
76 colors[3] = "goldenrod";
79 colors[6] = "magenta";
80 colors[7] = "turquoise4";
82 colors[9] = "forestgreen";
83 colors[10] = "hotpink";
84 colors[11] = "lightblue";
87 dot_output = fopen(_sg_mc_dot_output_file, "w");
89 if (dot_output == NULL) {
90 perror("Error open dot output file");
95 "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");
99 static void MC_init_debug_info(void)
101 XBT_INFO("Get debug information ...");
103 memory_map_t maps = MC_get_memory_map();
105 /* Get local variables for state equality detection */
106 mc_binary_info = MC_find_object_info(maps, xbt_binary_name, 1);
107 mc_object_infos[0] = mc_binary_info;
109 mc_libsimgrid_info = MC_find_object_info(maps, libsimgrid_path, 0);
110 mc_object_infos[1] = mc_libsimgrid_info;
112 // Use information of the other objects:
113 MC_post_process_object_info(mc_binary_info);
114 MC_post_process_object_info(mc_libsimgrid_info);
116 MC_free_memory_map(maps);
117 XBT_INFO("Get debug information done !");
121 mc_model_checker_t mc_model_checker = NULL;
123 mc_model_checker_t MC_model_checker_new()
125 mc_model_checker_t mc = xbt_new0(s_mc_model_checker_t, 1);
126 mc->pages = mc_pages_store_new();
127 mc->fd_clear_refs = -1;
132 void MC_model_checker_delete(mc_model_checker_t mc) {
133 mc_pages_store_delete(mc->pages);
135 xbt_dynar_free(&mc->record);
140 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
142 mc_time = xbt_new0(double, simix_process_maxpid);
144 /* Initialize the data structures that must be persistent across every
145 iteration of the model-checker (in RAW memory) */
149 mc_model_checker = MC_model_checker_new();
151 mc_comp_times = xbt_new0(s_mc_comparison_times_t, 1);
153 /* Initialize statistics */
154 mc_stats = xbt_new0(s_mc_stats_t, 1);
155 mc_stats->state_size = 1;
157 MC_init_memory_map_info();
158 MC_init_debug_info(); /* FIXME : get debug information only if liveness verification or visited state reduction */
160 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0'))
161 MC_init_dot_output();
164 //parmap = xbt_parmap_mc_new(xbt_os_get_numcores(), XBT_PARMAP_DEFAULT);
168 if (_sg_mc_visited > 0 || _sg_mc_liveness) {
169 /* Ignore some variables from xbt/ex.h used by exception e for stacks comparison */
170 MC_ignore_local_variable("e", "*");
171 MC_ignore_local_variable("__ex_cleanup", "*");
172 MC_ignore_local_variable("__ex_mctx_en", "*");
173 MC_ignore_local_variable("__ex_mctx_me", "*");
174 MC_ignore_local_variable("__xbt_ex_ctx_ptr", "*");
175 MC_ignore_local_variable("_log_ev", "*");
176 MC_ignore_local_variable("_throw_ctx", "*");
177 MC_ignore_local_variable("ctx", "*");
179 MC_ignore_local_variable("self", "simcall_BODY_mc_snapshot");
180 MC_ignore_local_variable("next_cont"
181 "ext", "smx_ctx_sysv_suspend_serial");
182 MC_ignore_local_variable("i", "smx_ctx_sysv_suspend_serial");
184 /* Ignore local variable about time used for tracing */
185 MC_ignore_local_variable("start_time", "*");
188 MC_ignore_global_variable("mc_model_checker");
189 MC_ignore_global_variable("communications_pattern");
190 MC_ignore_global_variable("initial_communications_pattern");
191 MC_ignore_global_variable("incomplete_communications_pattern");
193 /* MC __thread variables: */
194 MC_ignore_global_variable("mc_diff_info");
195 MC_ignore_global_variable("mc_comp_times");
196 MC_ignore_global_variable("mc_snapshot_comparison_time");
198 /* This MC state is used in MC replay as well: */
199 MC_ignore_global_variable("mc_time");
201 /* Static variable used for tracing */
202 MC_ignore_global_variable("counter");
205 MC_ignore_global_variable("smx_total_comms");
207 MC_ignore_heap(mc_time, simix_process_maxpid * sizeof(double));
209 smx_process_t process;
210 xbt_swag_foreach(process, simix_global->process_list) {
211 MC_ignore_heap(&(process->process_hookup),
212 sizeof(process->process_hookup));
221 /******************************* Core of MC *******************************/
222 /**************************************************************************/
224 static void MC_modelcheck_comm_determinism_init(void)
227 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
234 /* Create exploration stack */
235 mc_stack = xbt_fifo_new();
239 MC_pre_modelcheck_comm_determinism();
242 initial_global_state = xbt_new0(s_mc_global_t, 1);
243 initial_global_state->snapshot = MC_take_snapshot(0);
244 initial_global_state->initial_communications_pattern_done = 0;
245 initial_global_state->comm_deterministic = 1;
246 initial_global_state->send_deterministic = 1;
249 MC_modelcheck_comm_determinism();
256 static void MC_modelcheck_safety_init(void)
258 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
267 /* Create exploration stack */
268 mc_stack = xbt_fifo_new();
272 MC_pre_modelcheck_safety();
275 /* Save the initial state */
276 initial_global_state = xbt_new0(s_mc_global_t, 1);
277 initial_global_state->snapshot = MC_take_snapshot(0);
280 MC_modelcheck_safety();
289 static void MC_modelcheck_liveness_init()
292 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
301 /* Create exploration stack */
302 mc_stack = xbt_fifo_new();
304 /* Create the initial state */
305 initial_global_state = xbt_new0(s_mc_global_t, 1);
309 MC_pre_modelcheck_liveness();
312 MC_print_statistics(mc_stats);
320 void MC_do_the_modelcheck_for_real()
323 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
324 XBT_INFO("Check communication determinism");
325 MC_modelcheck_comm_determinism_init();
326 } else if (!_sg_mc_property_file || _sg_mc_property_file[0] == '\0') {
327 if (mc_reduce_kind == e_mc_reduce_unset)
328 mc_reduce_kind = e_mc_reduce_dpor;
329 XBT_INFO("Check a safety property");
330 MC_modelcheck_safety_init();
332 if (mc_reduce_kind == e_mc_reduce_unset)
333 mc_reduce_kind = e_mc_reduce_none;
334 XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
335 MC_automaton_load(_sg_mc_property_file);
336 MC_modelcheck_liveness_init();
349 int MC_deadlock_check()
351 int deadlock = FALSE;
352 smx_process_t process;
353 if (xbt_swag_size(simix_global->process_list)) {
355 xbt_swag_foreach(process, simix_global->process_list) {
356 if (MC_request_is_enabled(&process->simcall)) {
365 void mc_update_comm_pattern(mc_call_type call_type, smx_simcall_t req, int value, xbt_dynar_t pattern) {
367 case MC_CALL_TYPE_NONE:
369 case MC_CALL_TYPE_SEND:
370 case MC_CALL_TYPE_RECV:
371 get_comm_pattern(pattern, req, call_type);
373 case MC_CALL_TYPE_WAIT:
375 smx_synchro_t current_comm = NULL;
376 if (call_type == MC_CALL_TYPE_WAIT)
377 current_comm = simcall_comm_wait__get__comm(req);
379 current_comm = xbt_dynar_get_as(simcall_comm_waitany__get__comms(req), value, smx_synchro_t);
380 // First wait only must be considered:
381 if (current_comm->comm.refcount == 1)
382 complete_comm_pattern(pattern, current_comm);
386 xbt_die("Unexpected call type %i", (int)call_type);
391 * \brief Re-executes from the state at position start all the transitions indicated by
392 * a given model-checker stack.
393 * \param stack The stack with the transitions to execute.
394 * \param start Start index to begin the re-execution.
396 * If start==-1, restore the initial state and replay the actions the
397 * the transitions in the stack.
399 * Otherwise, we only replay a part of the transitions of the stacks
400 * without restoring the state: it is assumed that the current state
401 * match with the transitions to execute.
403 void MC_replay(xbt_fifo_t stack, int start)
405 int raw_mem = (mmalloc_get_current_heap() == mc_heap);
407 int value, i = 1, count = 1, j;
409 smx_simcall_t req = NULL, saved_req = NULL;
410 xbt_fifo_item_t item, start_item;
412 smx_process_t process = NULL;
414 XBT_DEBUG("**** Begin Replay ****");
417 /* Restore the initial state */
418 MC_restore_snapshot(initial_global_state->snapshot);
419 /* At the moment of taking the snapshot the raw heap was set, so restoring
420 * it will set it back again, we have to unset it to continue */
424 start_item = xbt_fifo_get_last_item(stack);
427 start_item = xbt_fifo_get_prev_item(start_item);
434 if (mc_reduce_kind == e_mc_reduce_dpor) {
435 xbt_dict_reset(first_enabled_state);
436 xbt_swag_foreach(process, simix_global->process_list) {
437 if (MC_process_is_enabled(process)) {
438 char *key = bprintf("%lu", process->pid);
439 char *data = bprintf("%d", count);
440 xbt_dict_set(first_enabled_state, key, data, NULL);
446 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
447 for (j=0; j<simix_process_maxpid; j++) {
448 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(communications_pattern, j, xbt_dynar_t));
450 for (j=0; j<simix_process_maxpid; j++) {
451 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
457 /* Traverse the stack from the state at position start and re-execute the transitions */
458 for (item = start_item;
459 item != xbt_fifo_get_first_item(stack);
460 item = xbt_fifo_get_prev_item(item)) {
462 state = (mc_state_t) xbt_fifo_get_item_content(item);
463 saved_req = MC_state_get_executed_request(state, &value);
465 if (mc_reduce_kind == e_mc_reduce_dpor) {
467 char *key = bprintf("%lu", saved_req->issuer->pid);
468 if(xbt_dict_get_or_null(first_enabled_state, key))
469 xbt_dict_remove(first_enabled_state, key);
475 /* because we got a copy of the executed request, we have to fetch the
476 real one, pointed by the request field of the issuer process */
477 req = &saved_req->issuer->simcall;
479 /* Debug information */
480 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
481 req_str = MC_request_to_string(req, value);
482 XBT_DEBUG("Replay: %s (%p)", req_str, state);
486 /* TODO : handle test and testany simcalls */
487 mc_call_type call = MC_CALL_TYPE_NONE;
488 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
489 call = mc_get_call_type(req);
492 SIMIX_simcall_handle(req, value);
494 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
496 mc_update_comm_pattern(call, req, value, communications_pattern);
500 MC_wait_for_requests();
504 if (mc_reduce_kind == e_mc_reduce_dpor) {
506 /* Insert in dict all enabled processes */
507 xbt_swag_foreach(process, simix_global->process_list) {
508 if (MC_process_is_enabled(process) ) {
509 char *key = bprintf("%lu", process->pid);
510 if (xbt_dict_get_or_null(first_enabled_state, key) == NULL) {
511 char *data = bprintf("%d", count);
512 xbt_dict_set(first_enabled_state, key, data, NULL);
521 /* Update statistics */
522 mc_stats->visited_states++;
523 mc_stats->executed_transitions++;
527 XBT_DEBUG("**** End Replay ****");
537 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
540 initial_global_state->raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
542 xbt_fifo_item_t item;
545 XBT_DEBUG("**** Begin Replay ****");
547 /* Restore the initial state */
548 MC_restore_snapshot(initial_global_state->snapshot);
550 /* At the moment of taking the snapshot the raw heap was set, so restoring
551 * it will set it back again, we have to unset it to continue */
552 if (!initial_global_state->raw_mem_set)
555 /* Traverse the stack from the initial state and re-execute the transitions */
556 for (item = xbt_fifo_get_last_item(stack);
557 all_stack ? depth <= xbt_fifo_size(stack) : item != xbt_fifo_get_first_item(stack);
558 item = xbt_fifo_get_prev_item(item)) {
560 mc_pair_t pair = (mc_pair_t) xbt_fifo_get_item_content(item);
562 mc_state_t state = (mc_state_t) pair->graph_state;
563 smx_simcall_t req = NULL, saved_req = NULL;
567 if (pair->requests > 0) {
569 saved_req = MC_state_get_executed_request(state, &value);
570 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
572 if (saved_req != NULL) {
573 /* because we got a copy of the executed request, we have to fetch the
574 real one, pointed by the request field of the issuer process */
575 req = &saved_req->issuer->simcall;
576 //XBT_DEBUG("Req->call %u", req->call);
578 /* Debug information */
579 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
580 req_str = MC_request_to_string(req, value);
581 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
587 SIMIX_simcall_handle(req, value);
588 MC_wait_for_requests();
591 /* Update statistics */
592 mc_stats->visited_pairs++;
593 mc_stats->executed_transitions++;
599 XBT_DEBUG("**** End Replay ****");
601 if (initial_global_state->raw_mem_set)
609 * \brief Dumps the contents of a model-checker's stack and shows the actual
611 * \param stack The stack to dump
613 void MC_dump_stack_safety(xbt_fifo_t stack)
616 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
618 MC_show_stack_safety(stack);
620 if (!_sg_mc_checkpoint) {
625 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
626 MC_state_delete(state);
639 void MC_show_stack_safety(xbt_fifo_t stack)
642 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
648 xbt_fifo_item_t item;
650 char *req_str = NULL;
652 for (item = xbt_fifo_get_last_item(stack);
653 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
654 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
655 req = MC_state_get_executed_request(state, &value);
657 req_str = MC_request_to_string(req, value);
658 XBT_INFO("%s", req_str);
667 void MC_show_deadlock(smx_simcall_t req)
669 /*char *req_str = NULL; */
670 XBT_INFO("**************************");
671 XBT_INFO("*** DEAD-LOCK DETECTED ***");
672 XBT_INFO("**************************");
673 XBT_INFO("Locked request:");
674 /*req_str = MC_request_to_string(req);
675 XBT_INFO("%s", req_str);
676 xbt_free(req_str); */
677 XBT_INFO("Counter-example execution trace:");
678 MC_dump_stack_safety(mc_stack);
679 MC_print_statistics(mc_stats);
683 void MC_show_stack_liveness(xbt_fifo_t stack)
687 xbt_fifo_item_t item;
689 char *req_str = NULL;
691 for (item = xbt_fifo_get_last_item(stack);
692 (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item)))
693 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
694 req = MC_state_get_executed_request(pair->graph_state, &value);
696 if (pair->requests > 0) {
697 req_str = MC_request_to_string(req, value);
698 XBT_INFO("%s", req_str);
701 XBT_INFO("End of system requests but evolution in Büchi automaton");
707 void MC_dump_stack_liveness(xbt_fifo_t stack)
710 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
715 while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
716 MC_pair_delete(pair);
725 void MC_print_statistics(mc_stats_t stats)
727 xbt_mheap_t previous_heap = mmalloc_get_current_heap();
729 if (stats->expanded_pairs == 0) {
730 XBT_INFO("Expanded states = %lu", stats->expanded_states);
731 XBT_INFO("Visited states = %lu", stats->visited_states);
733 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
734 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
736 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
738 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0')) {
739 fprintf(dot_output, "}\n");
742 if (initial_global_state != NULL) {
743 if (_sg_mc_comms_determinism)
744 XBT_INFO("Communication-deterministic : %s",
745 !initial_global_state->comm_deterministic ? "No" : "Yes");
746 if (_sg_mc_send_determinism)
747 XBT_INFO("Send-deterministic : %s",
748 !initial_global_state->send_deterministic ? "No" : "Yes");
750 mmalloc_set_current_heap(previous_heap);
753 void MC_assert(int prop)
755 if (MC_is_active() && !prop) {
756 XBT_INFO("**************************");
757 XBT_INFO("*** PROPERTY NOT VALID ***");
758 XBT_INFO("**************************");
759 XBT_INFO("Counter-example execution trace:");
760 MC_record_dump_path(mc_stack);
761 MC_dump_stack_safety(mc_stack);
762 MC_print_statistics(mc_stats);
769 user_max_depth_reached = 1;
772 void MC_automaton_load(const char *file)
775 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
779 if (_mc_property_automaton == NULL)
780 _mc_property_automaton = xbt_automaton_new();
782 xbt_automaton_load(_mc_property_automaton, file);
791 void MC_automaton_new_propositional_symbol(const char *id, void *fct)
794 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
798 if (_mc_property_automaton == NULL)
799 _mc_property_automaton = xbt_automaton_new();
801 xbt_automaton_propositional_symbol_new(_mc_property_automaton, id, fct);
810 void MC_dump_stacks(FILE* file)
812 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
816 stack_region_t current_stack;
818 xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
819 unw_context_t * context = (unw_context_t *)current_stack->context;
820 fprintf(file, "Stack %i:\n", nstack);
825 unw_init_local (&c, context);
828 const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
829 fprintf(file, " %i: %s\n", nframe, name);
831 } while(unw_step(&c));
841 double MC_process_clock_get(smx_process_t process)
845 return mc_time[process->pid];
853 void MC_process_clock_add(smx_process_t process, double amount)
855 mc_time[process->pid] += amount;