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. */
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 <libunwind.h>
27 #include "../xbt/mmalloc/mmprivate.h"
28 #include "mc_object_info.h"
29 #include "mc_comm_pattern.h"
30 #include "mc_request.h"
31 #include "mc_safety.h"
32 #include "mc_memory_map.h"
33 #include "mc_snapshot.h"
34 #include "mc_liveness.h"
35 #include "mc_private.h"
38 #include "mc_record.h"
39 #include "mc_protocol.h"
40 #include "mc_client.h"
42 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
43 "Logging specific to MC (global)");
47 double *mc_time = NULL;
50 int user_max_depth_reached = 0;
52 /* MC global data structures */
53 mc_state_t mc_current_state = NULL;
54 char mc_replay_mode = FALSE;
56 __thread mc_comparison_times_t mc_comp_times = NULL;
57 __thread double mc_snapshot_comparison_time;
58 mc_stats_t mc_stats = NULL;
59 mc_global_t initial_global_state = NULL;
60 xbt_fifo_t mc_stack = NULL;
63 xbt_automaton_t _mc_property_automaton = NULL;
66 FILE *dot_output = NULL;
67 const char *colors[13];
70 /******************************* Initialisation of MC *******************************/
71 /*********************************************************************************/
73 static void MC_init_dot_output()
74 { /* FIXME : more colors */
79 colors[3] = "goldenrod";
82 colors[6] = "magenta";
83 colors[7] = "turquoise4";
85 colors[9] = "forestgreen";
86 colors[10] = "hotpink";
87 colors[11] = "lightblue";
90 dot_output = fopen(_sg_mc_dot_output_file, "w");
92 if (dot_output == NULL) {
93 perror("Error open dot output file");
98 "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");
102 mc_model_checker_t mc_model_checker = NULL;
104 mc_model_checker_t MC_model_checker_new(pid_t pid, int socket)
106 mc_model_checker_t mc = xbt_new0(s_mc_model_checker_t, 1);
107 mc->pages = mc_pages_store_new();
108 mc->fd_clear_refs = -1;
110 MC_process_init(&mc->process, pid, socket);
114 void MC_model_checker_delete(mc_model_checker_t mc)
116 mc_pages_store_delete(mc->pages);
118 xbt_dynar_free(&mc->record);
119 MC_process_clear(&mc->process);
124 MC_init_pid(getpid(), -1);
127 void MC_init_pid(pid_t pid, int socket)
129 if (mc_mode == MC_MODE_NONE) {
130 if (getenv(MC_ENV_SOCKET_FD)) {
131 mc_mode = MC_MODE_CLIENT;
133 mc_mode = MC_MODE_STANDALONE;
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(pid, socket);
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 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0'))
155 MC_init_dot_output();
158 //parmap = xbt_parmap_mc_new(xbt_os_get_numcores(), XBT_PARMAP_DEFAULT);
162 if (_sg_mc_visited > 0 || _sg_mc_liveness) {
163 /* Ignore some variables from xbt/ex.h used by exception e for stacks comparison */
164 MC_ignore_local_variable("e", "*");
165 MC_ignore_local_variable("__ex_cleanup", "*");
166 MC_ignore_local_variable("__ex_mctx_en", "*");
167 MC_ignore_local_variable("__ex_mctx_me", "*");
168 MC_ignore_local_variable("__xbt_ex_ctx_ptr", "*");
169 MC_ignore_local_variable("_log_ev", "*");
170 MC_ignore_local_variable("_throw_ctx", "*");
171 MC_ignore_local_variable("ctx", "*");
173 MC_ignore_local_variable("self", "simcall_BODY_mc_snapshot");
174 MC_ignore_local_variable("next_cont"
175 "ext", "smx_ctx_sysv_suspend_serial");
176 MC_ignore_local_variable("i", "smx_ctx_sysv_suspend_serial");
178 /* Ignore local variable about time used for tracing */
179 MC_ignore_local_variable("start_time", "*");
182 MC_ignore_global_variable("mc_model_checker");
183 MC_ignore_global_variable("communications_pattern");
184 MC_ignore_global_variable("initial_communications_pattern");
185 MC_ignore_global_variable("incomplete_communications_pattern");
187 /* MC __thread variables: */
188 MC_ignore_global_variable("mc_diff_info");
189 MC_ignore_global_variable("mc_comp_times");
190 MC_ignore_global_variable("mc_snapshot_comparison_time");
192 /* This MC state is used in MC replay as well: */
193 MC_ignore_global_variable("mc_time");
195 /* Static variable used for tracing */
196 MC_ignore_global_variable("counter");
199 MC_ignore_global_variable("smx_total_comms");
201 MC_ignore_heap(mc_time, simix_process_maxpid * sizeof(double));
203 smx_process_t process;
204 // FIXME, cross-process support (simix_global->process_list)
206 if (mc_mode == MC_MODE_STANDALONE || mc_mode == MC_MODE_CLIENT) {
207 xbt_swag_foreach(process, simix_global->process_list) {
208 MC_ignore_heap(&(process->process_hookup),
209 sizeof(process->process_hookup));
219 if (mc_mode == MC_MODE_CLIENT) {
220 // This will move somehwere else:
221 MC_client_handle_messages();
226 /******************************* Core of MC *******************************/
227 /**************************************************************************/
229 static void MC_modelcheck_comm_determinism_init(void)
232 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
239 /* Create exploration stack */
240 mc_stack = xbt_fifo_new();
244 MC_pre_modelcheck_comm_determinism();
247 initial_global_state = xbt_new0(s_mc_global_t, 1);
248 initial_global_state->snapshot = MC_take_snapshot(0);
249 initial_global_state->initial_communications_pattern_done = 0;
250 initial_global_state->comm_deterministic = 1;
251 initial_global_state->send_deterministic = 1;
254 MC_modelcheck_comm_determinism();
261 static void MC_modelcheck_safety_init(void)
263 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
272 /* Create exploration stack */
273 mc_stack = xbt_fifo_new();
277 MC_pre_modelcheck_safety();
280 /* Save the initial state */
281 initial_global_state = xbt_new0(s_mc_global_t, 1);
282 initial_global_state->snapshot = MC_take_snapshot(0);
285 MC_modelcheck_safety();
294 static void MC_modelcheck_liveness_init()
297 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
306 /* Create exploration stack */
307 mc_stack = xbt_fifo_new();
309 /* Create the initial state */
310 initial_global_state = xbt_new0(s_mc_global_t, 1);
314 MC_pre_modelcheck_liveness();
317 MC_print_statistics(mc_stats);
325 void MC_do_the_modelcheck_for_real()
328 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
329 XBT_INFO("Check communication determinism");
330 MC_modelcheck_comm_determinism_init();
331 } else if (!_sg_mc_property_file || _sg_mc_property_file[0] == '\0') {
332 if (mc_reduce_kind == e_mc_reduce_unset)
333 mc_reduce_kind = e_mc_reduce_dpor;
334 XBT_INFO("Check a safety property");
335 MC_modelcheck_safety_init();
337 if (mc_reduce_kind == e_mc_reduce_unset)
338 mc_reduce_kind = e_mc_reduce_none;
339 XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
340 MC_automaton_load(_sg_mc_property_file);
341 MC_modelcheck_liveness_init();
354 int MC_deadlock_check()
356 int deadlock = FALSE;
357 smx_process_t process;
358 if (xbt_swag_size(simix_global->process_list)) {
360 xbt_swag_foreach(process, simix_global->process_list) {
361 if (MC_request_is_enabled(&process->simcall)) {
370 void mc_update_comm_pattern(mc_call_type call_type, smx_simcall_t req, int value, xbt_dynar_t pattern) {
372 case MC_CALL_TYPE_NONE:
374 case MC_CALL_TYPE_SEND:
375 case MC_CALL_TYPE_RECV:
376 get_comm_pattern(pattern, req, call_type);
378 case MC_CALL_TYPE_WAIT:
380 smx_synchro_t current_comm = NULL;
381 if (call_type == MC_CALL_TYPE_WAIT)
382 current_comm = simcall_comm_wait__get__comm(req);
384 current_comm = xbt_dynar_get_as(simcall_comm_waitany__get__comms(req), value, smx_synchro_t);
385 // First wait only must be considered:
386 if (current_comm->comm.refcount == 1)
387 complete_comm_pattern(pattern, current_comm);
391 xbt_die("Unexpected call type %i", (int)call_type);
396 * \brief Re-executes from the state at position start all the transitions indicated by
397 * a given model-checker stack.
398 * \param stack The stack with the transitions to execute.
399 * \param start Start index to begin the re-execution.
401 * If start==-1, restore the initial state and replay the actions the
402 * the transitions in the stack.
404 * Otherwise, we only replay a part of the transitions of the stacks
405 * without restoring the state: it is assumed that the current state
406 * match with the transitions to execute.
408 void MC_replay(xbt_fifo_t stack, int start)
410 int raw_mem = (mmalloc_get_current_heap() == mc_heap);
412 int value, i = 1, count = 1, j;
414 smx_simcall_t req = NULL, saved_req = NULL;
415 xbt_fifo_item_t item, start_item;
417 smx_process_t process = NULL;
419 XBT_DEBUG("**** Begin Replay ****");
422 /* Restore the initial state */
423 MC_restore_snapshot(initial_global_state->snapshot);
424 /* At the moment of taking the snapshot the raw heap was set, so restoring
425 * it will set it back again, we have to unset it to continue */
429 start_item = xbt_fifo_get_last_item(stack);
432 start_item = xbt_fifo_get_prev_item(start_item);
439 if (mc_reduce_kind == e_mc_reduce_dpor) {
440 xbt_dict_reset(first_enabled_state);
441 xbt_swag_foreach(process, simix_global->process_list) {
442 if (MC_process_is_enabled(process)) {
443 char *key = bprintf("%lu", process->pid);
444 char *data = bprintf("%d", count);
445 xbt_dict_set(first_enabled_state, key, data, NULL);
451 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
452 for (j=0; j<simix_process_maxpid; j++) {
453 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(communications_pattern, j, xbt_dynar_t));
455 for (j=0; j<simix_process_maxpid; j++) {
456 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
462 /* Traverse the stack from the state at position start and re-execute the transitions */
463 for (item = start_item;
464 item != xbt_fifo_get_first_item(stack);
465 item = xbt_fifo_get_prev_item(item)) {
467 state = (mc_state_t) xbt_fifo_get_item_content(item);
468 saved_req = MC_state_get_executed_request(state, &value);
470 if (mc_reduce_kind == e_mc_reduce_dpor) {
472 char *key = bprintf("%lu", saved_req->issuer->pid);
473 if(xbt_dict_get_or_null(first_enabled_state, key))
474 xbt_dict_remove(first_enabled_state, key);
480 /* because we got a copy of the executed request, we have to fetch the
481 real one, pointed by the request field of the issuer process */
482 req = &saved_req->issuer->simcall;
484 /* Debug information */
485 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
486 req_str = MC_request_to_string(req, value);
487 XBT_DEBUG("Replay: %s (%p)", req_str, state);
491 /* TODO : handle test and testany simcalls */
492 mc_call_type call = MC_CALL_TYPE_NONE;
493 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
494 call = mc_get_call_type(req);
497 SIMIX_simcall_handle(req, value);
499 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
501 mc_update_comm_pattern(call, req, value, communications_pattern);
505 MC_wait_for_requests();
509 if (mc_reduce_kind == e_mc_reduce_dpor) {
511 /* Insert in dict all enabled processes */
512 xbt_swag_foreach(process, simix_global->process_list) {
513 if (MC_process_is_enabled(process) ) {
514 char *key = bprintf("%lu", process->pid);
515 if (xbt_dict_get_or_null(first_enabled_state, key) == NULL) {
516 char *data = bprintf("%d", count);
517 xbt_dict_set(first_enabled_state, key, data, NULL);
526 /* Update statistics */
527 mc_stats->visited_states++;
528 mc_stats->executed_transitions++;
532 XBT_DEBUG("**** End Replay ****");
542 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
545 initial_global_state->raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
547 xbt_fifo_item_t item;
550 XBT_DEBUG("**** Begin Replay ****");
552 /* Restore the initial state */
553 MC_restore_snapshot(initial_global_state->snapshot);
555 /* At the moment of taking the snapshot the raw heap was set, so restoring
556 * it will set it back again, we have to unset it to continue */
557 if (!initial_global_state->raw_mem_set)
560 /* Traverse the stack from the initial state and re-execute the transitions */
561 for (item = xbt_fifo_get_last_item(stack);
562 all_stack ? depth <= xbt_fifo_size(stack) : item != xbt_fifo_get_first_item(stack);
563 item = xbt_fifo_get_prev_item(item)) {
565 mc_pair_t pair = (mc_pair_t) xbt_fifo_get_item_content(item);
567 mc_state_t state = (mc_state_t) pair->graph_state;
568 smx_simcall_t req = NULL, saved_req = NULL;
572 if (pair->requests > 0) {
574 saved_req = MC_state_get_executed_request(state, &value);
575 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
577 if (saved_req != NULL) {
578 /* because we got a copy of the executed request, we have to fetch the
579 real one, pointed by the request field of the issuer process */
580 req = &saved_req->issuer->simcall;
581 //XBT_DEBUG("Req->call %u", req->call);
583 /* Debug information */
584 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
585 req_str = MC_request_to_string(req, value);
586 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
592 SIMIX_simcall_handle(req, value);
593 MC_wait_for_requests();
596 /* Update statistics */
597 mc_stats->visited_pairs++;
598 mc_stats->executed_transitions++;
604 XBT_DEBUG("**** End Replay ****");
606 if (initial_global_state->raw_mem_set)
614 * \brief Dumps the contents of a model-checker's stack and shows the actual
616 * \param stack The stack to dump
618 void MC_dump_stack_safety(xbt_fifo_t stack)
621 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
623 MC_show_stack_safety(stack);
625 if (!_sg_mc_checkpoint) {
630 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
631 MC_state_delete(state);
644 void MC_show_stack_safety(xbt_fifo_t stack)
647 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
653 xbt_fifo_item_t item;
655 char *req_str = NULL;
657 for (item = xbt_fifo_get_last_item(stack);
658 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
659 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
660 req = MC_state_get_executed_request(state, &value);
662 req_str = MC_request_to_string(req, value);
663 XBT_INFO("%s", req_str);
672 void MC_show_deadlock(smx_simcall_t req)
674 /*char *req_str = NULL; */
675 XBT_INFO("**************************");
676 XBT_INFO("*** DEAD-LOCK DETECTED ***");
677 XBT_INFO("**************************");
678 XBT_INFO("Locked request:");
679 /*req_str = MC_request_to_string(req);
680 XBT_INFO("%s", req_str);
681 xbt_free(req_str); */
682 XBT_INFO("Counter-example execution trace:");
683 MC_dump_stack_safety(mc_stack);
684 MC_print_statistics(mc_stats);
688 void MC_show_stack_liveness(xbt_fifo_t stack)
692 xbt_fifo_item_t item;
694 char *req_str = NULL;
696 for (item = xbt_fifo_get_last_item(stack);
697 (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item)))
698 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
699 req = MC_state_get_executed_request(pair->graph_state, &value);
701 if (pair->requests > 0) {
702 req_str = MC_request_to_string(req, value);
703 XBT_INFO("%s", req_str);
706 XBT_INFO("End of system requests but evolution in Büchi automaton");
712 void MC_dump_stack_liveness(xbt_fifo_t stack)
715 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
720 while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
721 MC_pair_delete(pair);
730 void MC_print_statistics(mc_stats_t stats)
732 xbt_mheap_t previous_heap = mmalloc_get_current_heap();
734 if (stats->expanded_pairs == 0) {
735 XBT_INFO("Expanded states = %lu", stats->expanded_states);
736 XBT_INFO("Visited states = %lu", stats->visited_states);
738 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
739 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
741 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
743 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0')) {
744 fprintf(dot_output, "}\n");
747 if (initial_global_state != NULL) {
748 if (_sg_mc_comms_determinism)
749 XBT_INFO("Communication-deterministic : %s",
750 !initial_global_state->comm_deterministic ? "No" : "Yes");
751 if (_sg_mc_send_determinism)
752 XBT_INFO("Send-deterministic : %s",
753 !initial_global_state->send_deterministic ? "No" : "Yes");
755 mmalloc_set_current_heap(previous_heap);
758 void MC_assert(int prop)
760 if (MC_is_active() && !prop) {
761 XBT_INFO("**************************");
762 XBT_INFO("*** PROPERTY NOT VALID ***");
763 XBT_INFO("**************************");
764 XBT_INFO("Counter-example execution trace:");
765 MC_record_dump_path(mc_stack);
766 MC_dump_stack_safety(mc_stack);
767 MC_print_statistics(mc_stats);
774 user_max_depth_reached = 1;
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));
846 double MC_process_clock_get(smx_process_t process)
850 return mc_time[process->pid];
858 void MC_process_clock_add(smx_process_t process, double amount)
860 mc_time[process->pid] += amount;