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. */
11 #include <sys/types.h>
17 #define UNW_LOCAL_ONLY
18 #include <libunwind.h>
21 #include "simgrid/sg_config.h"
22 #include "../surf/surf_private.h"
23 #include "../simix/smx_private.h"
25 #include "xbt/automaton.h"
29 #include "../xbt/mmalloc/mmprivate.h"
30 #include "mc_private.h"
32 #include "mc_record.h"
34 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
35 "Logging specific to MC (global)");
37 double *mc_time = NULL;
40 int user_max_depth_reached = 0;
42 /* MC global data structures */
43 mc_state_t mc_current_state = NULL;
44 char mc_replay_mode = FALSE;
46 __thread mc_comparison_times_t mc_comp_times = NULL;
47 __thread double mc_snapshot_comparison_time;
48 mc_stats_t mc_stats = NULL;
49 mc_global_t initial_global_state = NULL;
50 xbt_fifo_t mc_stack = NULL;
53 xbt_automaton_t _mc_property_automaton = NULL;
56 mc_object_info_t mc_libsimgrid_info = NULL;
57 mc_object_info_t mc_binary_info = NULL;
59 mc_object_info_t mc_object_infos[2] = { NULL, NULL };
61 size_t mc_object_infos_size = 2;
64 FILE *dot_output = NULL;
65 const char *colors[13];
68 /******************************* Initialisation of MC *******************************/
69 /*********************************************************************************/
71 static void MC_init_dot_output()
72 { /* FIXME : more colors */
77 colors[3] = "goldenrod";
80 colors[6] = "magenta";
81 colors[7] = "turquoise4";
83 colors[9] = "forestgreen";
84 colors[10] = "hotpink";
85 colors[11] = "lightblue";
88 dot_output = fopen(_sg_mc_dot_output_file, "w");
90 if (dot_output == NULL) {
91 perror("Error open dot output file");
96 "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");
100 static void MC_init_debug_info(void)
102 XBT_INFO("Get debug information ...");
104 memory_map_t maps = MC_get_memory_map();
106 /* Get local variables for state equality detection */
107 mc_binary_info = MC_find_object_info(maps, xbt_binary_name, 1);
108 mc_object_infos[0] = mc_binary_info;
110 mc_libsimgrid_info = MC_find_object_info(maps, libsimgrid_path, 0);
111 mc_object_infos[1] = mc_libsimgrid_info;
113 // Use information of the other objects:
114 MC_post_process_object_info(mc_binary_info);
115 MC_post_process_object_info(mc_libsimgrid_info);
117 MC_free_memory_map(maps);
118 XBT_INFO("Get debug information done !");
122 mc_model_checker_t mc_model_checker = NULL;
124 mc_model_checker_t MC_model_checker_new()
126 mc_model_checker_t mc = xbt_new0(s_mc_model_checker_t, 1);
127 mc->pages = mc_pages_store_new();
128 mc->fd_clear_refs = -1;
133 void MC_model_checker_delete(mc_model_checker_t mc) {
134 mc_pages_store_delete(mc->pages);
136 xbt_dynar_free(&mc->record);
141 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
143 mc_time = xbt_new0(double, simix_process_maxpid);
145 /* Initialize the data structures that must be persistent across every
146 iteration of the model-checker (in RAW memory) */
150 mc_model_checker = MC_model_checker_new();
152 mc_comp_times = xbt_new0(s_mc_comparison_times_t, 1);
154 /* Initialize statistics */
155 mc_stats = xbt_new0(s_mc_stats_t, 1);
156 mc_stats->state_size = 1;
158 MC_init_memory_map_info();
159 MC_init_debug_info(); /* FIXME : get debug information only if liveness verification or visited state reduction */
161 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0'))
162 MC_init_dot_output();
165 //parmap = xbt_parmap_mc_new(xbt_os_get_numcores(), XBT_PARMAP_DEFAULT);
169 if (_sg_mc_visited > 0 || _sg_mc_liveness) {
170 /* Ignore some variables from xbt/ex.h used by exception e for stacks comparison */
171 MC_ignore_local_variable("e", "*");
172 MC_ignore_local_variable("__ex_cleanup", "*");
173 MC_ignore_local_variable("__ex_mctx_en", "*");
174 MC_ignore_local_variable("__ex_mctx_me", "*");
175 MC_ignore_local_variable("__xbt_ex_ctx_ptr", "*");
176 MC_ignore_local_variable("_log_ev", "*");
177 MC_ignore_local_variable("_throw_ctx", "*");
178 MC_ignore_local_variable("ctx", "*");
180 MC_ignore_local_variable("self", "simcall_BODY_mc_snapshot");
181 MC_ignore_local_variable("next_cont"
182 "ext", "smx_ctx_sysv_suspend_serial");
183 MC_ignore_local_variable("i", "smx_ctx_sysv_suspend_serial");
185 /* Ignore local variable about time used for tracing */
186 MC_ignore_local_variable("start_time", "*");
189 MC_ignore_global_variable("mc_model_checker");
190 MC_ignore_global_variable("communications_pattern");
191 MC_ignore_global_variable("initial_communications_pattern");
192 MC_ignore_global_variable("incomplete_communications_pattern");
194 /* MC __thread variables: */
195 MC_ignore_global_variable("mc_diff_info");
196 MC_ignore_global_variable("mc_comp_times");
197 MC_ignore_global_variable("mc_snapshot_comparison_time");
199 /* This MC state is used in MC replay as well: */
200 MC_ignore_global_variable("mc_time");
202 /* Static variable used for tracing */
203 MC_ignore_global_variable("counter");
206 MC_ignore_global_variable("smx_total_comms");
208 MC_ignore_heap(mc_time, simix_process_maxpid * sizeof(double));
210 smx_process_t process;
211 xbt_swag_foreach(process, simix_global->process_list) {
212 MC_ignore_heap(&(process->process_hookup),
213 sizeof(process->process_hookup));
222 /******************************* Core of MC *******************************/
223 /**************************************************************************/
225 static void MC_modelcheck_comm_determinism_init(void)
228 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
235 /* Create exploration stack */
236 mc_stack = xbt_fifo_new();
240 MC_pre_modelcheck_comm_determinism();
243 initial_global_state = xbt_new0(s_mc_global_t, 1);
244 initial_global_state->snapshot = MC_take_snapshot(0);
245 initial_global_state->initial_communications_pattern_done = 0;
246 initial_global_state->comm_deterministic = 1;
247 initial_global_state->send_deterministic = 1;
250 MC_modelcheck_comm_determinism();
257 static void MC_modelcheck_safety_init(void)
259 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
268 /* Create exploration stack */
269 mc_stack = xbt_fifo_new();
273 MC_pre_modelcheck_safety();
276 /* Save the initial state */
277 initial_global_state = xbt_new0(s_mc_global_t, 1);
278 initial_global_state->snapshot = MC_take_snapshot(0);
281 MC_modelcheck_safety();
290 static void MC_modelcheck_liveness_init()
293 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
302 /* Create exploration stack */
303 mc_stack = xbt_fifo_new();
305 /* Create the initial state */
306 initial_global_state = xbt_new0(s_mc_global_t, 1);
310 MC_pre_modelcheck_liveness();
313 MC_print_statistics(mc_stats);
321 void MC_do_the_modelcheck_for_real()
324 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
325 XBT_INFO("Check communication determinism");
326 MC_modelcheck_comm_determinism_init();
327 } else if (!_sg_mc_property_file || _sg_mc_property_file[0] == '\0') {
328 if (mc_reduce_kind == e_mc_reduce_unset)
329 mc_reduce_kind = e_mc_reduce_dpor;
330 XBT_INFO("Check a safety property");
331 MC_modelcheck_safety_init();
333 if (mc_reduce_kind == e_mc_reduce_unset)
334 mc_reduce_kind = e_mc_reduce_none;
335 XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
336 MC_automaton_load(_sg_mc_property_file);
337 MC_modelcheck_liveness_init();
350 int MC_deadlock_check()
352 int deadlock = FALSE;
353 smx_process_t process;
354 if (xbt_swag_size(simix_global->process_list)) {
356 xbt_swag_foreach(process, simix_global->process_list) {
357 if (MC_request_is_enabled(&process->simcall)) {
366 void mc_update_comm_pattern(mc_call_type call_type, smx_simcall_t req, int value, xbt_dynar_t pattern) {
368 case MC_CALL_TYPE_NONE:
370 case MC_CALL_TYPE_SEND:
371 case MC_CALL_TYPE_RECV:
372 get_comm_pattern(pattern, req, call_type);
374 case MC_CALL_TYPE_WAIT:
376 smx_synchro_t current_comm = NULL;
377 if (call_type == MC_CALL_TYPE_WAIT)
378 current_comm = simcall_comm_wait__get__comm(req);
380 current_comm = xbt_dynar_get_as(simcall_comm_waitany__get__comms(req), value, smx_synchro_t);
381 // First wait only must be considered:
382 if (current_comm->comm.refcount == 1)
383 complete_comm_pattern(pattern, current_comm);
387 xbt_die("Unexpected call type %i", (int)call_type);
392 * \brief Re-executes from the state at position start all the transitions indicated by
393 * a given model-checker stack.
394 * \param stack The stack with the transitions to execute.
395 * \param start Start index to begin the re-execution.
397 * If start==-1, restore the initial state and replay the actions the
398 * the transitions in the stack.
400 * Otherwise, we only replay a part of the transitions of the stacks
401 * without restoring the state: it is assumed that the current state
402 * match with the transitions to execute.
404 void MC_replay(xbt_fifo_t stack, int start)
406 int raw_mem = (mmalloc_get_current_heap() == mc_heap);
408 int value, i = 1, count = 1, j;
410 smx_simcall_t req = NULL, saved_req = NULL;
411 xbt_fifo_item_t item, start_item;
413 smx_process_t process = NULL;
415 XBT_DEBUG("**** Begin Replay ****");
418 /* Restore the initial state */
419 MC_restore_snapshot(initial_global_state->snapshot);
420 /* At the moment of taking the snapshot the raw heap was set, so restoring
421 * it will set it back again, we have to unset it to continue */
425 start_item = xbt_fifo_get_last_item(stack);
428 start_item = xbt_fifo_get_prev_item(start_item);
435 if (mc_reduce_kind == e_mc_reduce_dpor) {
436 xbt_dict_reset(first_enabled_state);
437 xbt_swag_foreach(process, simix_global->process_list) {
438 if (MC_process_is_enabled(process)) {
439 char *key = bprintf("%lu", process->pid);
440 char *data = bprintf("%d", count);
441 xbt_dict_set(first_enabled_state, key, data, NULL);
447 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
448 for (j=0; j<simix_process_maxpid; j++) {
449 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(communications_pattern, j, xbt_dynar_t));
451 for (j=0; j<simix_process_maxpid; j++) {
452 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
458 /* Traverse the stack from the state at position start and re-execute the transitions */
459 for (item = start_item;
460 item != xbt_fifo_get_first_item(stack);
461 item = xbt_fifo_get_prev_item(item)) {
463 state = (mc_state_t) xbt_fifo_get_item_content(item);
464 saved_req = MC_state_get_executed_request(state, &value);
466 if (mc_reduce_kind == e_mc_reduce_dpor) {
468 char *key = bprintf("%lu", saved_req->issuer->pid);
469 if(xbt_dict_get_or_null(first_enabled_state, key))
470 xbt_dict_remove(first_enabled_state, key);
476 /* because we got a copy of the executed request, we have to fetch the
477 real one, pointed by the request field of the issuer process */
478 req = &saved_req->issuer->simcall;
480 /* Debug information */
481 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
482 req_str = MC_request_to_string(req, value);
483 XBT_DEBUG("Replay: %s (%p)", req_str, state);
487 /* TODO : handle test and testany simcalls */
488 mc_call_type call = MC_CALL_TYPE_NONE;
489 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
490 call = mc_get_call_type(req);
493 SIMIX_simcall_handle(req, value);
495 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
497 mc_update_comm_pattern(call, req, value, communications_pattern);
501 MC_wait_for_requests();
505 if (mc_reduce_kind == e_mc_reduce_dpor) {
507 /* Insert in dict all enabled processes */
508 xbt_swag_foreach(process, simix_global->process_list) {
509 if (MC_process_is_enabled(process) ) {
510 char *key = bprintf("%lu", process->pid);
511 if (xbt_dict_get_or_null(first_enabled_state, key) == NULL) {
512 char *data = bprintf("%d", count);
513 xbt_dict_set(first_enabled_state, key, data, NULL);
522 /* Update statistics */
523 mc_stats->visited_states++;
524 mc_stats->executed_transitions++;
528 XBT_DEBUG("**** End Replay ****");
538 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
541 initial_global_state->raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
543 xbt_fifo_item_t item;
546 XBT_DEBUG("**** Begin Replay ****");
548 /* Restore the initial state */
549 MC_restore_snapshot(initial_global_state->snapshot);
551 /* At the moment of taking the snapshot the raw heap was set, so restoring
552 * it will set it back again, we have to unset it to continue */
553 if (!initial_global_state->raw_mem_set)
556 /* Traverse the stack from the initial state and re-execute the transitions */
557 for (item = xbt_fifo_get_last_item(stack);
558 all_stack ? depth <= xbt_fifo_size(stack) : item != xbt_fifo_get_first_item(stack);
559 item = xbt_fifo_get_prev_item(item)) {
561 mc_pair_t pair = (mc_pair_t) xbt_fifo_get_item_content(item);
563 mc_state_t state = (mc_state_t) pair->graph_state;
564 smx_simcall_t req = NULL, saved_req = NULL;
568 if (pair->requests > 0) {
570 saved_req = MC_state_get_executed_request(state, &value);
571 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
573 if (saved_req != NULL) {
574 /* because we got a copy of the executed request, we have to fetch the
575 real one, pointed by the request field of the issuer process */
576 req = &saved_req->issuer->simcall;
577 //XBT_DEBUG("Req->call %u", req->call);
579 /* Debug information */
580 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
581 req_str = MC_request_to_string(req, value);
582 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
588 SIMIX_simcall_handle(req, value);
589 MC_wait_for_requests();
592 /* Update statistics */
593 mc_stats->visited_pairs++;
594 mc_stats->executed_transitions++;
600 XBT_DEBUG("**** End Replay ****");
602 if (initial_global_state->raw_mem_set)
610 * \brief Dumps the contents of a model-checker's stack and shows the actual
612 * \param stack The stack to dump
614 void MC_dump_stack_safety(xbt_fifo_t stack)
617 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
619 MC_show_stack_safety(stack);
621 if (!_sg_mc_checkpoint) {
626 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
627 MC_state_delete(state);
640 void MC_show_stack_safety(xbt_fifo_t stack)
643 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
649 xbt_fifo_item_t item;
651 char *req_str = NULL;
653 for (item = xbt_fifo_get_last_item(stack);
654 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
655 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
656 req = MC_state_get_executed_request(state, &value);
658 req_str = MC_request_to_string(req, value);
659 XBT_INFO("%s", req_str);
668 void MC_show_deadlock(smx_simcall_t req)
670 /*char *req_str = NULL; */
671 XBT_INFO("**************************");
672 XBT_INFO("*** DEAD-LOCK DETECTED ***");
673 XBT_INFO("**************************");
674 XBT_INFO("Locked request:");
675 /*req_str = MC_request_to_string(req);
676 XBT_INFO("%s", req_str);
677 xbt_free(req_str); */
678 XBT_INFO("Counter-example execution trace:");
679 MC_dump_stack_safety(mc_stack);
680 MC_print_statistics(mc_stats);
684 void MC_show_stack_liveness(xbt_fifo_t stack)
688 xbt_fifo_item_t item;
690 char *req_str = NULL;
692 for (item = xbt_fifo_get_last_item(stack);
693 (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item)))
694 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
695 req = MC_state_get_executed_request(pair->graph_state, &value);
697 if (pair->requests > 0) {
698 req_str = MC_request_to_string(req, value);
699 XBT_INFO("%s", req_str);
702 XBT_INFO("End of system requests but evolution in Büchi automaton");
708 void MC_dump_stack_liveness(xbt_fifo_t stack)
711 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
716 while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
717 MC_pair_delete(pair);
726 void MC_print_statistics(mc_stats_t stats)
728 xbt_mheap_t previous_heap = mmalloc_get_current_heap();
730 if (stats->expanded_pairs == 0) {
731 XBT_INFO("Expanded states = %lu", stats->expanded_states);
732 XBT_INFO("Visited states = %lu", stats->visited_states);
734 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
735 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
737 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
739 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0')) {
740 fprintf(dot_output, "}\n");
743 if (initial_global_state != NULL) {
744 if (_sg_mc_comms_determinism)
745 XBT_INFO("Communication-deterministic : %s",
746 !initial_global_state->comm_deterministic ? "No" : "Yes");
747 if (_sg_mc_send_determinism)
748 XBT_INFO("Send-deterministic : %s",
749 !initial_global_state->send_deterministic ? "No" : "Yes");
751 mmalloc_set_current_heap(previous_heap);
754 void MC_assert(int prop)
756 if (MC_is_active() && !prop) {
757 XBT_INFO("**************************");
758 XBT_INFO("*** PROPERTY NOT VALID ***");
759 XBT_INFO("**************************");
760 XBT_INFO("Counter-example execution trace:");
761 MC_record_dump_path(mc_stack);
762 MC_dump_stack_safety(mc_stack);
763 MC_print_statistics(mc_stats);
770 user_max_depth_reached = 1;
773 void MC_automaton_load(const char *file)
776 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
780 if (_mc_property_automaton == NULL)
781 _mc_property_automaton = xbt_automaton_new();
783 xbt_automaton_load(_mc_property_automaton, file);
792 void MC_automaton_new_propositional_symbol(const char *id, void *fct)
795 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
799 if (_mc_property_automaton == NULL)
800 _mc_property_automaton = xbt_automaton_new();
802 xbt_automaton_propositional_symbol_new(_mc_property_automaton, id, fct);
811 void MC_dump_stacks(FILE* file)
813 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
817 stack_region_t current_stack;
819 xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
820 unw_context_t * context = (unw_context_t *)current_stack->context;
821 fprintf(file, "Stack %i:\n", nstack);
826 unw_init_local (&c, context);
829 const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
830 fprintf(file, " %i: %s\n", nframe, name);
832 } while(unw_step(&c));
842 double MC_process_clock_get(smx_process_t process)
846 return mc_time[process->pid];
854 void MC_process_clock_add(smx_process_t process, double amount)
856 mc_time[process->pid] += amount;