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) {
115 mc_pages_store_delete(mc->pages);
117 xbt_dynar_free(&mc->record);
118 MC_process_clear(&mc->process);
123 if (mc_mode == MC_MODE_NONE) {
124 if (getenv(MC_ENV_SOCKET_FD)) {
125 mc_mode = MC_MODE_CLIENT;
127 mc_mode = MC_MODE_STANDALONE;
131 MC_init_pid(getpid(), -1);
133 if (mc_mode == MC_MODE_CLIENT) {
136 // This will move somehwere else:
137 MC_client_handle_messages();
141 void MC_init_pid(pid_t pid, int socket)
143 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
145 mc_time = xbt_new0(double, simix_process_maxpid);
147 /* Initialize the data structures that must be persistent across every
148 iteration of the model-checker (in RAW memory) */
152 mc_model_checker = MC_model_checker_new(pid, socket);
154 mc_comp_times = xbt_new0(s_mc_comparison_times_t, 1);
156 /* Initialize statistics */
157 mc_stats = xbt_new0(s_mc_stats_t, 1);
158 mc_stats->state_size = 1;
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 // FIXME, cross-process support (simix_global->process_list)
212 if (mc_mode == MC_MODE_STANDALONE || mc_mode == MC_MODE_CLIENT) {
213 xbt_swag_foreach(process, simix_global->process_list) {
214 MC_ignore_heap(&(process->process_hookup),
215 sizeof(process->process_hookup));
227 /******************************* Core of MC *******************************/
228 /**************************************************************************/
230 static void MC_modelcheck_comm_determinism_init(void)
233 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
240 /* Create exploration stack */
241 mc_stack = xbt_fifo_new();
245 MC_pre_modelcheck_comm_determinism();
248 initial_global_state = xbt_new0(s_mc_global_t, 1);
249 initial_global_state->snapshot = MC_take_snapshot(0);
250 initial_global_state->initial_communications_pattern_done = 0;
251 initial_global_state->comm_deterministic = 1;
252 initial_global_state->send_deterministic = 1;
255 MC_modelcheck_comm_determinism();
262 static void MC_modelcheck_safety_init(void)
264 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
273 /* Create exploration stack */
274 mc_stack = xbt_fifo_new();
278 MC_pre_modelcheck_safety();
281 /* Save the initial state */
282 initial_global_state = xbt_new0(s_mc_global_t, 1);
283 initial_global_state->snapshot = MC_take_snapshot(0);
286 MC_modelcheck_safety();
295 static void MC_modelcheck_liveness_init()
298 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
307 /* Create exploration stack */
308 mc_stack = xbt_fifo_new();
310 /* Create the initial state */
311 initial_global_state = xbt_new0(s_mc_global_t, 1);
315 MC_pre_modelcheck_liveness();
318 MC_print_statistics(mc_stats);
326 void MC_do_the_modelcheck_for_real()
329 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
330 XBT_INFO("Check communication determinism");
331 MC_modelcheck_comm_determinism_init();
332 } else if (!_sg_mc_property_file || _sg_mc_property_file[0] == '\0') {
333 if (mc_reduce_kind == e_mc_reduce_unset)
334 mc_reduce_kind = e_mc_reduce_dpor;
335 XBT_INFO("Check a safety property");
336 MC_modelcheck_safety_init();
338 if (mc_reduce_kind == e_mc_reduce_unset)
339 mc_reduce_kind = e_mc_reduce_none;
340 XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
341 MC_automaton_load(_sg_mc_property_file);
342 MC_modelcheck_liveness_init();
355 int MC_deadlock_check()
357 int deadlock = FALSE;
358 smx_process_t process;
359 if (xbt_swag_size(simix_global->process_list)) {
361 xbt_swag_foreach(process, simix_global->process_list) {
362 if (MC_request_is_enabled(&process->simcall)) {
371 void mc_update_comm_pattern(mc_call_type call_type, smx_simcall_t req, int value, xbt_dynar_t pattern) {
373 case MC_CALL_TYPE_NONE:
375 case MC_CALL_TYPE_SEND:
376 case MC_CALL_TYPE_RECV:
377 get_comm_pattern(pattern, req, call_type);
379 case MC_CALL_TYPE_WAIT:
381 smx_synchro_t current_comm = NULL;
382 if (call_type == MC_CALL_TYPE_WAIT)
383 current_comm = simcall_comm_wait__get__comm(req);
385 current_comm = xbt_dynar_get_as(simcall_comm_waitany__get__comms(req), value, smx_synchro_t);
386 // First wait only must be considered:
387 if (current_comm->comm.refcount == 1)
388 complete_comm_pattern(pattern, current_comm);
392 xbt_die("Unexpected call type %i", (int)call_type);
397 * \brief Re-executes from the state at position start all the transitions indicated by
398 * a given model-checker stack.
399 * \param stack The stack with the transitions to execute.
400 * \param start Start index to begin the re-execution.
402 * If start==-1, restore the initial state and replay the actions the
403 * the transitions in the stack.
405 * Otherwise, we only replay a part of the transitions of the stacks
406 * without restoring the state: it is assumed that the current state
407 * match with the transitions to execute.
409 void MC_replay(xbt_fifo_t stack, int start)
411 int raw_mem = (mmalloc_get_current_heap() == mc_heap);
413 int value, i = 1, count = 1, j;
415 smx_simcall_t req = NULL, saved_req = NULL;
416 xbt_fifo_item_t item, start_item;
418 smx_process_t process = NULL;
420 XBT_DEBUG("**** Begin Replay ****");
423 /* Restore the initial state */
424 MC_restore_snapshot(initial_global_state->snapshot);
425 /* At the moment of taking the snapshot the raw heap was set, so restoring
426 * it will set it back again, we have to unset it to continue */
430 start_item = xbt_fifo_get_last_item(stack);
433 start_item = xbt_fifo_get_prev_item(start_item);
440 if (mc_reduce_kind == e_mc_reduce_dpor) {
441 xbt_dict_reset(first_enabled_state);
442 xbt_swag_foreach(process, simix_global->process_list) {
443 if (MC_process_is_enabled(process)) {
444 char *key = bprintf("%lu", process->pid);
445 char *data = bprintf("%d", count);
446 xbt_dict_set(first_enabled_state, key, data, NULL);
452 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
453 for (j=0; j<simix_process_maxpid; j++) {
454 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(communications_pattern, j, xbt_dynar_t));
456 for (j=0; j<simix_process_maxpid; j++) {
457 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
463 /* Traverse the stack from the state at position start and re-execute the transitions */
464 for (item = start_item;
465 item != xbt_fifo_get_first_item(stack);
466 item = xbt_fifo_get_prev_item(item)) {
468 state = (mc_state_t) xbt_fifo_get_item_content(item);
469 saved_req = MC_state_get_executed_request(state, &value);
471 if (mc_reduce_kind == e_mc_reduce_dpor) {
473 char *key = bprintf("%lu", saved_req->issuer->pid);
474 if(xbt_dict_get_or_null(first_enabled_state, key))
475 xbt_dict_remove(first_enabled_state, key);
481 /* because we got a copy of the executed request, we have to fetch the
482 real one, pointed by the request field of the issuer process */
483 req = &saved_req->issuer->simcall;
485 /* Debug information */
486 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
487 req_str = MC_request_to_string(req, value);
488 XBT_DEBUG("Replay: %s (%p)", req_str, state);
492 /* TODO : handle test and testany simcalls */
493 mc_call_type call = MC_CALL_TYPE_NONE;
494 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
495 call = mc_get_call_type(req);
498 SIMIX_simcall_handle(req, value);
500 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
502 mc_update_comm_pattern(call, req, value, communications_pattern);
506 MC_wait_for_requests();
510 if (mc_reduce_kind == e_mc_reduce_dpor) {
512 /* Insert in dict all enabled processes */
513 xbt_swag_foreach(process, simix_global->process_list) {
514 if (MC_process_is_enabled(process) ) {
515 char *key = bprintf("%lu", process->pid);
516 if (xbt_dict_get_or_null(first_enabled_state, key) == NULL) {
517 char *data = bprintf("%d", count);
518 xbt_dict_set(first_enabled_state, key, data, NULL);
527 /* Update statistics */
528 mc_stats->visited_states++;
529 mc_stats->executed_transitions++;
533 XBT_DEBUG("**** End Replay ****");
543 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
546 initial_global_state->raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
548 xbt_fifo_item_t item;
551 XBT_DEBUG("**** Begin Replay ****");
553 /* Restore the initial state */
554 MC_restore_snapshot(initial_global_state->snapshot);
556 /* At the moment of taking the snapshot the raw heap was set, so restoring
557 * it will set it back again, we have to unset it to continue */
558 if (!initial_global_state->raw_mem_set)
561 /* Traverse the stack from the initial state and re-execute the transitions */
562 for (item = xbt_fifo_get_last_item(stack);
563 all_stack ? depth <= xbt_fifo_size(stack) : item != xbt_fifo_get_first_item(stack);
564 item = xbt_fifo_get_prev_item(item)) {
566 mc_pair_t pair = (mc_pair_t) xbt_fifo_get_item_content(item);
568 mc_state_t state = (mc_state_t) pair->graph_state;
569 smx_simcall_t req = NULL, saved_req = NULL;
573 if (pair->requests > 0) {
575 saved_req = MC_state_get_executed_request(state, &value);
576 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
578 if (saved_req != NULL) {
579 /* because we got a copy of the executed request, we have to fetch the
580 real one, pointed by the request field of the issuer process */
581 req = &saved_req->issuer->simcall;
582 //XBT_DEBUG("Req->call %u", req->call);
584 /* Debug information */
585 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
586 req_str = MC_request_to_string(req, value);
587 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
593 SIMIX_simcall_handle(req, value);
594 MC_wait_for_requests();
597 /* Update statistics */
598 mc_stats->visited_pairs++;
599 mc_stats->executed_transitions++;
605 XBT_DEBUG("**** End Replay ****");
607 if (initial_global_state->raw_mem_set)
615 * \brief Dumps the contents of a model-checker's stack and shows the actual
617 * \param stack The stack to dump
619 void MC_dump_stack_safety(xbt_fifo_t stack)
622 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
624 MC_show_stack_safety(stack);
626 if (!_sg_mc_checkpoint) {
631 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
632 MC_state_delete(state);
645 void MC_show_stack_safety(xbt_fifo_t stack)
648 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
654 xbt_fifo_item_t item;
656 char *req_str = NULL;
658 for (item = xbt_fifo_get_last_item(stack);
659 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
660 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
661 req = MC_state_get_executed_request(state, &value);
663 req_str = MC_request_to_string(req, value);
664 XBT_INFO("%s", req_str);
673 void MC_show_deadlock(smx_simcall_t req)
675 /*char *req_str = NULL; */
676 XBT_INFO("**************************");
677 XBT_INFO("*** DEAD-LOCK DETECTED ***");
678 XBT_INFO("**************************");
679 XBT_INFO("Locked request:");
680 /*req_str = MC_request_to_string(req);
681 XBT_INFO("%s", req_str);
682 xbt_free(req_str); */
683 XBT_INFO("Counter-example execution trace:");
684 MC_dump_stack_safety(mc_stack);
685 MC_print_statistics(mc_stats);
689 void MC_show_stack_liveness(xbt_fifo_t stack)
693 xbt_fifo_item_t item;
695 char *req_str = NULL;
697 for (item = xbt_fifo_get_last_item(stack);
698 (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item)))
699 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
700 req = MC_state_get_executed_request(pair->graph_state, &value);
702 if (pair->requests > 0) {
703 req_str = MC_request_to_string(req, value);
704 XBT_INFO("%s", req_str);
707 XBT_INFO("End of system requests but evolution in Büchi automaton");
713 void MC_dump_stack_liveness(xbt_fifo_t stack)
716 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
721 while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
722 MC_pair_delete(pair);
731 void MC_print_statistics(mc_stats_t stats)
733 xbt_mheap_t previous_heap = mmalloc_get_current_heap();
735 if (stats->expanded_pairs == 0) {
736 XBT_INFO("Expanded states = %lu", stats->expanded_states);
737 XBT_INFO("Visited states = %lu", stats->visited_states);
739 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
740 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
742 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
744 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0')) {
745 fprintf(dot_output, "}\n");
748 if (initial_global_state != NULL) {
749 if (_sg_mc_comms_determinism)
750 XBT_INFO("Communication-deterministic : %s",
751 !initial_global_state->comm_deterministic ? "No" : "Yes");
752 if (_sg_mc_send_determinism)
753 XBT_INFO("Send-deterministic : %s",
754 !initial_global_state->send_deterministic ? "No" : "Yes");
756 mmalloc_set_current_heap(previous_heap);
759 void MC_assert(int prop)
761 if (MC_is_active() && !prop) {
762 XBT_INFO("**************************");
763 XBT_INFO("*** PROPERTY NOT VALID ***");
764 XBT_INFO("**************************");
765 XBT_INFO("Counter-example execution trace:");
766 MC_record_dump_path(mc_stack);
767 MC_dump_stack_safety(mc_stack);
768 MC_print_statistics(mc_stats);
775 user_max_depth_reached = 1;
778 void MC_automaton_load(const char *file)
781 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
785 if (_mc_property_automaton == NULL)
786 _mc_property_automaton = xbt_automaton_new();
788 xbt_automaton_load(_mc_property_automaton, file);
797 void MC_automaton_new_propositional_symbol(const char *id, void *fct)
800 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
804 if (_mc_property_automaton == NULL)
805 _mc_property_automaton = xbt_automaton_new();
807 xbt_automaton_propositional_symbol_new(_mc_property_automaton, id, fct);
816 void MC_dump_stacks(FILE* file)
818 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
822 stack_region_t current_stack;
824 xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
825 unw_context_t * context = (unw_context_t *)current_stack->context;
826 fprintf(file, "Stack %i:\n", nstack);
831 unw_init_local (&c, context);
834 const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
835 fprintf(file, " %i: %s\n", nframe, name);
837 } while(unw_step(&c));
847 double MC_process_clock_get(smx_process_t process)
851 return mc_time[process->pid];
859 void MC_process_clock_add(smx_process_t process, double amount)
861 mc_time[process->pid] += amount;