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 #include "simgrid/sg_config.h"
15 #include "../surf/surf_private.h"
16 #include "../simix/smx_private.h"
17 #include "../xbt/mmalloc/mmprivate.h"
19 #include "mc_private.h"
20 #include "xbt/automaton.h"
23 XBT_LOG_NEW_CATEGORY(mc, "All MC categories");
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
25 "Logging specific to MC (global)");
27 int user_max_depth_reached = 0;
29 /* MC global data structures */
30 mc_state_t mc_current_state = NULL;
31 char mc_replay_mode = FALSE;
32 double *mc_time = NULL;
33 __thread mc_comparison_times_t mc_comp_times = NULL;
34 __thread double mc_snapshot_comparison_time;
35 mc_stats_t mc_stats = NULL;
36 mc_global_t initial_global_state = NULL;
37 xbt_fifo_t mc_stack = NULL;
40 xbt_automaton_t _mc_property_automaton = NULL;
43 mc_object_info_t mc_libsimgrid_info = NULL;
44 mc_object_info_t mc_binary_info = NULL;
46 mc_object_info_t mc_object_infos[2] = { NULL, NULL };
48 size_t mc_object_infos_size = 2;
51 FILE *dot_output = NULL;
52 const char *colors[13];
55 /******************************* Initialisation of MC *******************************/
56 /*********************************************************************************/
58 static void MC_init_dot_output()
59 { /* FIXME : more colors */
64 colors[3] = "goldenrod";
67 colors[6] = "magenta";
68 colors[7] = "turquoise4";
70 colors[9] = "forestgreen";
71 colors[10] = "hotpink";
72 colors[11] = "lightblue";
75 dot_output = fopen(_sg_mc_dot_output_file, "w");
77 if (dot_output == NULL) {
78 perror("Error open dot output file");
83 "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");
87 static void MC_init_debug_info(void)
89 XBT_INFO("Get debug information ...");
91 memory_map_t maps = MC_get_memory_map();
93 /* Get local variables for state equality detection */
94 mc_binary_info = MC_find_object_info(maps, xbt_binary_name, 1);
95 mc_object_infos[0] = mc_binary_info;
97 mc_libsimgrid_info = MC_find_object_info(maps, libsimgrid_path, 0);
98 mc_object_infos[1] = mc_libsimgrid_info;
100 // Use information of the other objects:
101 MC_post_process_object_info(mc_binary_info);
102 MC_post_process_object_info(mc_libsimgrid_info);
104 MC_free_memory_map(maps);
105 XBT_INFO("Get debug information done !");
109 mc_model_checker_t mc_model_checker = NULL;
113 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
115 mc_time = xbt_new0(double, simix_process_maxpid);
117 /* Initialize the data structures that must be persistent across every
118 iteration of the model-checker (in RAW memory) */
122 mc_model_checker = xbt_new0(s_mc_model_checker_t, 1);
123 mc_model_checker->pages = mc_pages_store_new();
124 mc_model_checker->fd_clear_refs = -1;
125 mc_model_checker->fd_pagemap = -1;
127 mc_comp_times = xbt_new0(s_mc_comparison_times_t, 1);
129 /* Initialize statistics */
130 mc_stats = xbt_new0(s_mc_stats_t, 1);
131 mc_stats->state_size = 1;
133 MC_init_memory_map_info();
134 MC_init_debug_info(); /* FIXME : get debug information only if liveness verification or visited state reduction */
136 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0'))
137 MC_init_dot_output();
140 //parmap = xbt_parmap_mc_new(xbt_os_get_numcores(), XBT_PARMAP_DEFAULT);
144 if (_sg_mc_visited > 0 || _sg_mc_liveness) {
145 /* Ignore some variables from xbt/ex.h used by exception e for stacks comparison */
146 MC_ignore_local_variable("e", "*");
147 MC_ignore_local_variable("__ex_cleanup", "*");
148 MC_ignore_local_variable("__ex_mctx_en", "*");
149 MC_ignore_local_variable("__ex_mctx_me", "*");
150 MC_ignore_local_variable("__xbt_ex_ctx_ptr", "*");
151 MC_ignore_local_variable("_log_ev", "*");
152 MC_ignore_local_variable("_throw_ctx", "*");
153 MC_ignore_local_variable("ctx", "*");
155 MC_ignore_local_variable("self", "simcall_BODY_mc_snapshot");
156 MC_ignore_local_variable("next_cont"
157 "ext", "smx_ctx_sysv_suspend_serial");
158 MC_ignore_local_variable("i", "smx_ctx_sysv_suspend_serial");
160 /* Ignore local variable about time used for tracing */
161 MC_ignore_local_variable("start_time", "*");
163 MC_ignore_global_variable("mc_model_checker");
165 // Mot of those things could be moved into mc_model_checker:
166 MC_ignore_global_variable("compared_pointers");
167 MC_ignore_global_variable("mc_comp_times");
168 MC_ignore_global_variable("mc_snapshot_comparison_time");
169 MC_ignore_global_variable("mc_time");
170 MC_ignore_global_variable("smpi_current_rank");
171 MC_ignore_global_variable("counter"); /* Static variable used for tracing */
172 MC_ignore_global_variable("maestro_stack_start");
173 MC_ignore_global_variable("maestro_stack_end");
174 MC_ignore_global_variable("smx_total_comms");
175 MC_ignore_global_variable("communications_pattern");
176 MC_ignore_global_variable("initial_communications_pattern");
177 MC_ignore_global_variable("incomplete_communications_pattern");
179 if (MC_is_active()) {
180 MC_ignore_global_variable("mc_diff_info");
183 MC_ignore_heap(mc_time, simix_process_maxpid * sizeof(double));
185 smx_process_t process;
186 xbt_swag_foreach(process, simix_global->process_list) {
187 MC_ignore_heap(&(process->process_hookup),
188 sizeof(process->process_hookup));
197 /******************************* Core of MC *******************************/
198 /**************************************************************************/
200 static void MC_modelcheck_comm_determinism_init(void)
203 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
210 /* Create exploration stack */
211 mc_stack = xbt_fifo_new();
215 MC_pre_modelcheck_comm_determinism();
218 initial_global_state = xbt_new0(s_mc_global_t, 1);
219 initial_global_state->snapshot = MC_take_snapshot(0);
220 initial_global_state->initial_communications_pattern_done = 0;
221 initial_global_state->comm_deterministic = 1;
222 initial_global_state->send_deterministic = 1;
225 MC_modelcheck_comm_determinism();
232 static void MC_modelcheck_safety_init(void)
234 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
243 /* Create exploration stack */
244 mc_stack = xbt_fifo_new();
248 MC_pre_modelcheck_safety();
251 /* Save the initial state */
252 initial_global_state = xbt_new0(s_mc_global_t, 1);
253 initial_global_state->snapshot = MC_take_snapshot(0);
256 MC_modelcheck_safety();
265 static void MC_modelcheck_liveness_init()
268 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
277 /* Create exploration stack */
278 mc_stack = xbt_fifo_new();
280 /* Create the initial state */
281 initial_global_state = xbt_new0(s_mc_global_t, 1);
285 MC_pre_modelcheck_liveness();
288 MC_print_statistics(mc_stats);
296 void MC_do_the_modelcheck_for_real()
299 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
300 XBT_INFO("Check communication determinism");
301 MC_modelcheck_comm_determinism_init();
302 } else if (!_sg_mc_property_file || _sg_mc_property_file[0] == '\0') {
303 if (mc_reduce_kind == e_mc_reduce_unset)
304 mc_reduce_kind = e_mc_reduce_dpor;
305 XBT_INFO("Check a safety property");
306 MC_modelcheck_safety_init();
308 if (mc_reduce_kind == e_mc_reduce_unset)
309 mc_reduce_kind = e_mc_reduce_none;
310 XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
311 MC_automaton_load(_sg_mc_property_file);
312 MC_modelcheck_liveness_init();
325 int SIMIX_pre_mc_random(smx_simcall_t simcall, int min, int max)
328 return simcall->mc_value;
332 int MC_random(int min, int max)
334 /*FIXME: return mc_current_state->executed_transition->random.value; */
335 return simcall_mc_random(min, max);
339 * \brief Schedules all the process that are ready to run
341 void MC_wait_for_requests(void)
343 smx_process_t process;
347 while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
348 SIMIX_process_runall();
349 xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
350 req = &process->simcall;
351 if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
352 SIMIX_simcall_handle(req, 0);
357 int MC_deadlock_check()
359 int deadlock = FALSE;
360 smx_process_t process;
361 if (xbt_swag_size(simix_global->process_list)) {
363 xbt_swag_foreach(process, simix_global->process_list) {
364 if (MC_request_is_enabled(&process->simcall)) {
373 void mc_update_comm_pattern(mc_call_type call_type, smx_simcall_t req, int value, xbt_dynar_t pattern) {
375 case MC_CALL_TYPE_NONE:
377 case MC_CALL_TYPE_SEND:
378 case MC_CALL_TYPE_RECV:
379 get_comm_pattern(pattern, req, call_type);
381 case MC_CALL_TYPE_WAIT:
383 smx_action_t current_comm = NULL;
384 if (call_type == MC_CALL_TYPE_WAIT)
385 current_comm = simcall_comm_wait__get__comm(req);
387 current_comm = xbt_dynar_get_as(simcall_comm_waitany__get__comms(req), value, smx_action_t);
388 // First wait only must be considered:
389 if (current_comm->comm.refcount == 1)
390 complete_comm_pattern(pattern, current_comm);
394 xbt_die("Unexpected call type %i", (int)call_type);
399 * \brief Re-executes from the state at position start all the transitions indicated by
400 * a given model-checker stack.
401 * \param stack The stack with the transitions to execute.
402 * \param start Start index to begin the re-execution.
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));
459 /* Traverse the stack from the state at position start and re-execute the transitions */
460 for (item = start_item;
461 item != xbt_fifo_get_first_item(stack);
462 item = xbt_fifo_get_prev_item(item)) {
464 state = (mc_state_t) xbt_fifo_get_item_content(item);
465 saved_req = MC_state_get_executed_request(state, &value);
467 if (mc_reduce_kind == e_mc_reduce_dpor) {
469 char *key = bprintf("%lu", saved_req->issuer->pid);
470 if(xbt_dict_get_or_null(first_enabled_state, key))
471 xbt_dict_remove(first_enabled_state, key);
477 /* because we got a copy of the executed request, we have to fetch the
478 real one, pointed by the request field of the issuer process */
479 req = &saved_req->issuer->simcall;
481 /* Debug information */
482 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
483 req_str = MC_request_to_string(req, value);
484 XBT_DEBUG("Replay: %s (%p)", req_str, state);
488 /* TODO : handle test and testany simcalls */
489 mc_call_type call = MC_CALL_TYPE_NONE;
490 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
491 call = mc_get_call_type(req);
494 SIMIX_simcall_handle(req, value);
496 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
498 mc_update_comm_pattern(call, req, value, communications_pattern);
502 MC_wait_for_requests();
506 if (mc_reduce_kind == e_mc_reduce_dpor) {
508 /* Insert in dict all enabled processes */
509 xbt_swag_foreach(process, simix_global->process_list) {
510 if (MC_process_is_enabled(process) ) {
511 char *key = bprintf("%lu", process->pid);
512 if (xbt_dict_get_or_null(first_enabled_state, key) == NULL) {
513 char *data = bprintf("%d", count);
514 xbt_dict_set(first_enabled_state, key, data, NULL);
523 /* Update statistics */
524 mc_stats->visited_states++;
525 mc_stats->executed_transitions++;
529 XBT_DEBUG("**** End Replay ****");
539 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
542 initial_global_state->raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
544 xbt_fifo_item_t item;
547 XBT_DEBUG("**** Begin Replay ****");
549 /* Restore the initial state */
550 MC_restore_snapshot(initial_global_state->snapshot);
552 /* At the moment of taking the snapshot the raw heap was set, so restoring
553 * it will set it back again, we have to unset it to continue */
554 if (!initial_global_state->raw_mem_set)
557 /* Traverse the stack from the initial state and re-execute the transitions */
558 for (item = xbt_fifo_get_last_item(stack);
559 all_stack ? depth <= xbt_fifo_size(stack) : item != xbt_fifo_get_first_item(stack);
560 item = xbt_fifo_get_prev_item(item)) {
562 mc_pair_t pair = (mc_pair_t) xbt_fifo_get_item_content(item);
564 mc_state_t state = (mc_state_t) pair->graph_state;
565 smx_simcall_t req = NULL, saved_req = NULL;
569 if (pair->requests > 0) {
571 saved_req = MC_state_get_executed_request(state, &value);
572 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
574 if (saved_req != NULL) {
575 /* because we got a copy of the executed request, we have to fetch the
576 real one, pointed by the request field of the issuer process */
577 req = &saved_req->issuer->simcall;
578 //XBT_DEBUG("Req->call %u", req->call);
580 /* Debug information */
581 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
582 req_str = MC_request_to_string(req, value);
583 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
589 SIMIX_simcall_handle(req, value);
590 MC_wait_for_requests();
593 /* Update statistics */
594 mc_stats->visited_pairs++;
595 mc_stats->executed_transitions++;
601 XBT_DEBUG("**** End Replay ****");
603 if (initial_global_state->raw_mem_set)
611 * \brief Dumps the contents of a model-checker's stack and shows the actual
613 * \param stack The stack to dump
615 void MC_dump_stack_safety(xbt_fifo_t stack)
618 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
620 MC_show_stack_safety(stack);
622 if (!_sg_mc_checkpoint) {
627 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
628 MC_state_delete(state);
641 void MC_show_stack_safety(xbt_fifo_t stack)
644 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
650 xbt_fifo_item_t item;
652 char *req_str = NULL;
654 for (item = xbt_fifo_get_last_item(stack);
655 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
656 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
657 req = MC_state_get_executed_request(state, &value);
659 req_str = MC_request_to_string(req, value);
660 XBT_INFO("%s", req_str);
669 void MC_show_deadlock(smx_simcall_t req)
671 /*char *req_str = NULL; */
672 XBT_INFO("**************************");
673 XBT_INFO("*** DEAD-LOCK DETECTED ***");
674 XBT_INFO("**************************");
675 XBT_INFO("Locked request:");
676 /*req_str = MC_request_to_string(req);
677 XBT_INFO("%s", req_str);
678 xbt_free(req_str); */
679 XBT_INFO("Counter-example execution trace:");
680 MC_dump_stack_safety(mc_stack);
681 MC_print_statistics(mc_stats);
685 void MC_show_stack_liveness(xbt_fifo_t stack)
689 xbt_fifo_item_t item;
691 char *req_str = NULL;
693 for (item = xbt_fifo_get_last_item(stack);
694 (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item)))
695 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
696 req = MC_state_get_executed_request(pair->graph_state, &value);
698 if (pair->requests > 0) {
699 req_str = MC_request_to_string(req, value);
700 XBT_INFO("%s", req_str);
703 XBT_INFO("End of system requests but evolution in Büchi automaton");
709 void MC_dump_stack_liveness(xbt_fifo_t stack)
712 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
717 while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
718 MC_pair_delete(pair);
727 void MC_print_statistics(mc_stats_t stats)
729 xbt_mheap_t previous_heap = mmalloc_get_current_heap();
731 if (stats->expanded_pairs == 0) {
732 XBT_INFO("Expanded states = %lu", stats->expanded_states);
733 XBT_INFO("Visited states = %lu", stats->visited_states);
735 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
736 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
738 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
740 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0')) {
741 fprintf(dot_output, "}\n");
744 if (initial_global_state != NULL) {
745 if (_sg_mc_comms_determinism)
746 XBT_INFO("Communication-deterministic : %s",
747 !initial_global_state->comm_deterministic ? "No" : "Yes");
748 if (_sg_mc_send_determinism)
749 XBT_INFO("Send-deterministic : %s",
750 !initial_global_state->send_deterministic ? "No" : "Yes");
752 mmalloc_set_current_heap(previous_heap);
755 void MC_assert(int prop)
757 if (MC_is_active() && !prop) {
758 XBT_INFO("**************************");
759 XBT_INFO("*** PROPERTY NOT VALID ***");
760 XBT_INFO("**************************");
761 XBT_INFO("Counter-example execution trace:");
762 MC_dump_stack_safety(mc_stack);
763 MC_print_statistics(mc_stats);
770 user_max_depth_reached = 1;
773 void MC_process_clock_add(smx_process_t process, double amount)
775 mc_time[process->pid] += amount;
778 double MC_process_clock_get(smx_process_t process)
782 return mc_time[process->pid];
790 void MC_automaton_load(const char *file)
793 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
797 if (_mc_property_automaton == NULL)
798 _mc_property_automaton = xbt_automaton_new();
800 xbt_automaton_load(_mc_property_automaton, file);
809 void MC_automaton_new_propositional_symbol(const char *id, void *fct)
812 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
816 if (_mc_property_automaton == NULL)
817 _mc_property_automaton = xbt_automaton_new();
819 xbt_automaton_propositional_symbol_new(_mc_property_automaton, id, fct);