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>
18 #include "simgrid/sg_config.h"
19 #include "../surf/surf_private.h"
20 #include "../simix/smx_private.h"
22 #include "xbt/automaton.h"
26 #define UNW_LOCAL_ONLY
27 #include <libunwind.h>
29 #include "../xbt/mmalloc/mmprivate.h"
30 #include "mc_object_info.h"
31 #include "mc_comm_pattern.h"
32 #include "mc_request.h"
33 #include "mc_safety.h"
34 #include "mc_memory_map.h"
35 #include "mc_snapshot.h"
36 #include "mc_liveness.h"
37 #include "mc_private.h"
39 #include "mc_record.h"
41 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
42 "Logging specific to MC (global)");
44 double *mc_time = NULL;
47 int user_max_depth_reached = 0;
49 /* MC global data structures */
50 mc_state_t mc_current_state = NULL;
51 char mc_replay_mode = FALSE;
53 __thread mc_comparison_times_t mc_comp_times = NULL;
54 __thread double mc_snapshot_comparison_time;
55 mc_stats_t mc_stats = NULL;
56 mc_global_t initial_global_state = NULL;
57 xbt_fifo_t mc_stack = NULL;
60 xbt_automaton_t _mc_property_automaton = NULL;
63 mc_object_info_t mc_libsimgrid_info = NULL;
64 mc_object_info_t mc_binary_info = NULL;
66 mc_object_info_t mc_object_infos[2] = { NULL, NULL };
68 size_t mc_object_infos_size = 2;
71 FILE *dot_output = NULL;
72 const char *colors[13];
75 /******************************* Initialisation of MC *******************************/
76 /*********************************************************************************/
78 static void MC_init_dot_output()
79 { /* FIXME : more colors */
84 colors[3] = "goldenrod";
87 colors[6] = "magenta";
88 colors[7] = "turquoise4";
90 colors[9] = "forestgreen";
91 colors[10] = "hotpink";
92 colors[11] = "lightblue";
95 dot_output = fopen(_sg_mc_dot_output_file, "w");
97 if (dot_output == NULL) {
98 perror("Error open dot output file");
103 "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");
107 static void MC_init_debug_info(void)
109 XBT_INFO("Get debug information ...");
111 memory_map_t maps = MC_get_memory_map();
113 /* Get local variables for state equality detection */
114 mc_binary_info = MC_find_object_info(maps, xbt_binary_name, 1);
115 mc_object_infos[0] = mc_binary_info;
117 mc_libsimgrid_info = MC_find_object_info(maps, libsimgrid_path, 0);
118 mc_object_infos[1] = mc_libsimgrid_info;
120 // Use information of the other objects:
121 MC_post_process_object_info(mc_binary_info);
122 MC_post_process_object_info(mc_libsimgrid_info);
124 MC_free_memory_map(maps);
125 XBT_INFO("Get debug information done !");
129 mc_model_checker_t mc_model_checker = NULL;
131 mc_model_checker_t MC_model_checker_new()
133 mc_model_checker_t mc = xbt_new0(s_mc_model_checker_t, 1);
134 mc->pages = mc_pages_store_new();
135 mc->fd_clear_refs = -1;
140 void MC_model_checker_delete(mc_model_checker_t mc) {
141 mc_pages_store_delete(mc->pages);
143 xbt_dynar_free(&mc->record);
148 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
150 mc_time = xbt_new0(double, simix_process_maxpid);
152 /* Initialize the data structures that must be persistent across every
153 iteration of the model-checker (in RAW memory) */
157 mc_model_checker = MC_model_checker_new();
159 mc_comp_times = xbt_new0(s_mc_comparison_times_t, 1);
161 /* Initialize statistics */
162 mc_stats = xbt_new0(s_mc_stats_t, 1);
163 mc_stats->state_size = 1;
165 MC_init_memory_map_info();
166 MC_init_debug_info(); /* FIXME : get debug information only if liveness verification or visited state reduction */
168 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0'))
169 MC_init_dot_output();
172 //parmap = xbt_parmap_mc_new(xbt_os_get_numcores(), XBT_PARMAP_DEFAULT);
176 if (_sg_mc_visited > 0 || _sg_mc_liveness) {
177 /* Ignore some variables from xbt/ex.h used by exception e for stacks comparison */
178 MC_ignore_local_variable("e", "*");
179 MC_ignore_local_variable("__ex_cleanup", "*");
180 MC_ignore_local_variable("__ex_mctx_en", "*");
181 MC_ignore_local_variable("__ex_mctx_me", "*");
182 MC_ignore_local_variable("__xbt_ex_ctx_ptr", "*");
183 MC_ignore_local_variable("_log_ev", "*");
184 MC_ignore_local_variable("_throw_ctx", "*");
185 MC_ignore_local_variable("ctx", "*");
187 MC_ignore_local_variable("self", "simcall_BODY_mc_snapshot");
188 MC_ignore_local_variable("next_cont"
189 "ext", "smx_ctx_sysv_suspend_serial");
190 MC_ignore_local_variable("i", "smx_ctx_sysv_suspend_serial");
192 /* Ignore local variable about time used for tracing */
193 MC_ignore_local_variable("start_time", "*");
196 MC_ignore_global_variable("mc_model_checker");
197 MC_ignore_global_variable("initial_communications_pattern");
198 MC_ignore_global_variable("incomplete_communications_pattern");
199 MC_ignore_global_variable("nb_comm_pattern");
201 /* MC __thread variables: */
202 MC_ignore_global_variable("mc_diff_info");
203 MC_ignore_global_variable("mc_comp_times");
204 MC_ignore_global_variable("mc_snapshot_comparison_time");
206 /* This MC state is used in MC replay as well: */
207 MC_ignore_global_variable("mc_time");
209 /* Static variable used for tracing */
210 MC_ignore_global_variable("counter");
213 MC_ignore_global_variable("smx_total_comms");
215 MC_ignore_heap(mc_time, simix_process_maxpid * sizeof(double));
217 smx_process_t process;
218 xbt_swag_foreach(process, simix_global->process_list) {
219 MC_ignore_heap(&(process->process_hookup),
220 sizeof(process->process_hookup));
229 /******************************* Core of MC *******************************/
230 /**************************************************************************/
232 static void MC_modelcheck_comm_determinism_init(void)
235 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
242 /* Create exploration stack */
243 mc_stack = xbt_fifo_new();
247 MC_pre_modelcheck_comm_determinism();
250 initial_global_state = xbt_new0(s_mc_global_t, 1);
251 initial_global_state->snapshot = MC_take_snapshot(0);
252 initial_global_state->initial_communications_pattern_done = 0;
253 initial_global_state->comm_deterministic = 1;
254 initial_global_state->send_deterministic = 1;
257 MC_modelcheck_comm_determinism();
264 static void MC_modelcheck_safety_init(void)
266 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
275 /* Create exploration stack */
276 mc_stack = xbt_fifo_new();
280 MC_pre_modelcheck_safety();
283 /* Save the initial state */
284 initial_global_state = xbt_new0(s_mc_global_t, 1);
285 initial_global_state->snapshot = MC_take_snapshot(0);
288 MC_modelcheck_safety();
297 static void MC_modelcheck_liveness_init()
300 int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
309 /* Create exploration stack */
310 mc_stack = xbt_fifo_new();
312 /* Create the initial state */
313 initial_global_state = xbt_new0(s_mc_global_t, 1);
317 MC_pre_modelcheck_liveness();
320 MC_print_statistics(mc_stats);
328 void MC_do_the_modelcheck_for_real()
331 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
332 XBT_INFO("Check communication determinism");
333 MC_modelcheck_comm_determinism_init();
334 } else if (!_sg_mc_property_file || _sg_mc_property_file[0] == '\0') {
335 if (mc_reduce_kind == e_mc_reduce_unset)
336 mc_reduce_kind = e_mc_reduce_dpor;
337 XBT_INFO("Check a safety property");
338 MC_modelcheck_safety_init();
340 if (mc_reduce_kind == e_mc_reduce_unset)
341 mc_reduce_kind = e_mc_reduce_none;
342 XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
343 MC_automaton_load(_sg_mc_property_file);
344 MC_modelcheck_liveness_init();
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 handle_comm_pattern(e_mc_call_type_t call_type, smx_simcall_t req, int value, xbt_dynar_t pattern, int backtracking) {
376 case MC_CALL_TYPE_NONE:
378 case MC_CALL_TYPE_SEND:
379 case MC_CALL_TYPE_RECV:
380 get_comm_pattern(pattern, req, call_type);
382 case MC_CALL_TYPE_WAIT:
383 case MC_CALL_TYPE_WAITANY:
385 smx_synchro_t current_comm = NULL;
386 if (call_type == MC_CALL_TYPE_WAIT)
387 current_comm = simcall_comm_wait__get__comm(req);
389 current_comm = xbt_dynar_get_as(simcall_comm_waitany__get__comms(req), value, smx_synchro_t);
390 // First wait only must be considered:
391 if (current_comm->comm.refcount == 1)
392 complete_comm_pattern(pattern, current_comm, backtracking);
396 xbt_die("Unexpected call type %i", (int)call_type);
401 static void MC_restore_communications_pattern(mc_state_t state) {
402 mc_list_comm_pattern_t list_process_comm;
403 unsigned int cursor, cursor2;
404 xbt_dynar_foreach(initial_communications_pattern, cursor, list_process_comm){
405 list_process_comm->index_comm = (int)xbt_dynar_get_as(state->index_comm, cursor, int);
407 mc_comm_pattern_t comm;
409 xbt_dynar_t initial_incomplete_process_comms, incomplete_process_comms;
410 for(int i=0; i<simix_process_maxpid; i++){
411 initial_incomplete_process_comms = xbt_dynar_get_as(incomplete_communications_pattern, i, xbt_dynar_t);
412 xbt_dynar_reset(initial_incomplete_process_comms);
413 incomplete_process_comms = xbt_dynar_get_as(state->incomplete_comm_pattern, i, xbt_dynar_t);
414 xbt_dynar_foreach(incomplete_process_comms, cursor2, comm) {
415 mc_comm_pattern_t copy_comm = xbt_new0(s_mc_comm_pattern_t, 1);
416 copy_comm->index = comm->index;
417 copy_comm->type = comm->type;
418 copy_comm->comm = comm->comm;
419 copy_comm->rdv = strdup(comm->rdv);
420 copy_comm->data_size = -1;
421 copy_comm->data = NULL;
422 if(comm->type == SIMIX_COMM_SEND){
423 copy_comm->src_proc = comm->src_proc;
424 copy_comm->src_host = comm->src_host;
425 if(comm->data != NULL){
426 copy_comm->data_size = comm->data_size;
427 copy_comm->data = xbt_malloc0(comm->data_size);
428 memcpy(copy_comm->data, comm->data, comm->data_size);
431 copy_comm->dst_proc = comm->dst_proc;
432 copy_comm->dst_host = comm->dst_host;
434 xbt_dynar_push(initial_incomplete_process_comms, ©_comm);
440 * \brief Re-executes from the state at position start all the transitions indicated by
441 * a given model-checker stack.
442 * \param stack The stack with the transitions to execute.
443 * \param start Start index to begin the re-execution.
445 * If start==-1, restore the initial state and replay the actions the
446 * the transitions in the stack.
448 * Otherwise, we only replay a part of the transitions of the stacks
449 * without restoring the state: it is assumed that the current state
450 * match with the transitions to execute.
452 void MC_replay(xbt_fifo_t stack, int start)
454 int raw_mem = (mmalloc_get_current_heap() == mc_heap);
456 int value, i = 1, count = 1, j;
458 smx_simcall_t req = NULL, saved_req = NULL;
459 xbt_fifo_item_t item, start_item;
461 smx_process_t process = NULL;
463 XBT_DEBUG("**** Begin Replay ****");
466 /* Restore the initial state */
467 MC_restore_snapshot(initial_global_state->snapshot);
468 /* At the moment of taking the snapshot the raw heap was set, so restoring
469 * it will set it back again, we have to unset it to continue */
473 start_item = xbt_fifo_get_last_item(stack);
476 start_item = xbt_fifo_get_prev_item(start_item);
483 if (mc_reduce_kind == e_mc_reduce_dpor) {
484 xbt_dict_reset(first_enabled_state);
485 xbt_swag_foreach(process, simix_global->process_list) {
486 if (MC_process_is_enabled(process)) {
487 char *key = bprintf("%lu", process->pid);
488 char *data = bprintf("%d", count);
489 xbt_dict_set(first_enabled_state, key, data, NULL);
495 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
496 for (j=0; j<simix_process_maxpid; j++) {
497 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(communications_pattern, j, xbt_dynar_t));
499 for (j=0; j<simix_process_maxpid; j++) {
500 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
506 /* Traverse the stack from the state at position start and re-execute the transitions */
507 for (item = start_item;
508 item != xbt_fifo_get_first_item(stack);
509 item = xbt_fifo_get_prev_item(item)) {
511 state = (mc_state_t) xbt_fifo_get_item_content(item);
512 saved_req = MC_state_get_executed_request(state, &value);
514 if (mc_reduce_kind == e_mc_reduce_dpor) {
516 char *key = bprintf("%lu", saved_req->issuer->pid);
517 if(xbt_dict_get_or_null(first_enabled_state, key))
518 xbt_dict_remove(first_enabled_state, key);
524 /* because we got a copy of the executed request, we have to fetch the
525 real one, pointed by the request field of the issuer process */
526 req = &saved_req->issuer->simcall;
528 /* Debug information */
529 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
530 req_str = MC_request_to_string(req, value);
531 XBT_DEBUG("Replay: %s (%p)", req_str, state);
535 /* TODO : handle test and testany simcalls */
536 mc_call_type call = MC_CALL_TYPE_NONE;
537 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
538 call = mc_get_call_type(req);
541 SIMIX_simcall_handle(req, value);
543 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
545 mc_update_comm_pattern(call, req, value, communications_pattern);
549 MC_wait_for_requests();
553 if (mc_reduce_kind == e_mc_reduce_dpor) {
555 /* Insert in dict all enabled processes */
556 xbt_swag_foreach(process, simix_global->process_list) {
557 if (MC_process_is_enabled(process) ) {
558 char *key = bprintf("%lu", process->pid);
559 if (xbt_dict_get_or_null(first_enabled_state, key) == NULL) {
560 char *data = bprintf("%d", count);
561 xbt_dict_set(first_enabled_state, key, data, NULL);
570 /* Update statistics */
571 mc_stats->visited_states++;
572 mc_stats->executed_transitions++;
576 XBT_DEBUG("**** End Replay ****");
586 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
589 initial_global_state->raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
591 xbt_fifo_item_t item;
594 XBT_DEBUG("**** Begin Replay ****");
596 /* Restore the initial state */
597 MC_restore_snapshot(initial_global_state->snapshot);
599 /* At the moment of taking the snapshot the raw heap was set, so restoring
600 * it will set it back again, we have to unset it to continue */
601 if (!initial_global_state->raw_mem_set)
604 /* Traverse the stack from the initial state and re-execute the transitions */
605 for (item = xbt_fifo_get_last_item(stack);
606 all_stack ? depth <= xbt_fifo_size(stack) : item != xbt_fifo_get_first_item(stack);
607 item = xbt_fifo_get_prev_item(item)) {
609 mc_pair_t pair = (mc_pair_t) xbt_fifo_get_item_content(item);
611 mc_state_t state = (mc_state_t) pair->graph_state;
612 smx_simcall_t req = NULL, saved_req = NULL;
616 if (pair->requests > 0) {
618 saved_req = MC_state_get_executed_request(state, &value);
619 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
621 if (saved_req != NULL) {
622 /* because we got a copy of the executed request, we have to fetch the
623 real one, pointed by the request field of the issuer process */
624 req = &saved_req->issuer->simcall;
625 //XBT_DEBUG("Req->call %u", req->call);
627 /* Debug information */
628 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
629 req_str = MC_request_to_string(req, value);
630 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
636 SIMIX_simcall_handle(req, value);
637 MC_wait_for_requests();
640 /* Update statistics */
641 mc_stats->visited_pairs++;
642 mc_stats->executed_transitions++;
648 XBT_DEBUG("**** End Replay ****");
650 if (initial_global_state->raw_mem_set)
658 * \brief Dumps the contents of a model-checker's stack and shows the actual
660 * \param stack The stack to dump
662 void MC_dump_stack_safety(xbt_fifo_t stack)
665 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
667 MC_show_stack_safety(stack);
669 if (!_sg_mc_checkpoint) {
674 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
675 MC_state_delete(state);
688 void MC_show_stack_safety(xbt_fifo_t stack)
691 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
697 xbt_fifo_item_t item;
699 char *req_str = NULL;
701 for (item = xbt_fifo_get_last_item(stack);
702 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
703 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
704 req = MC_state_get_executed_request(state, &value);
706 req_str = MC_request_to_string(req, value);
707 XBT_INFO("%s", req_str);
716 void MC_show_deadlock(smx_simcall_t req)
718 /*char *req_str = NULL; */
719 XBT_INFO("**************************");
720 XBT_INFO("*** DEAD-LOCK DETECTED ***");
721 XBT_INFO("**************************");
722 XBT_INFO("Locked request:");
723 /*req_str = MC_request_to_string(req);
724 XBT_INFO("%s", req_str);
725 xbt_free(req_str); */
726 XBT_INFO("Counter-example execution trace:");
727 MC_dump_stack_safety(mc_stack);
728 MC_print_statistics(mc_stats);
732 void MC_show_stack_liveness(xbt_fifo_t stack)
736 xbt_fifo_item_t item;
738 char *req_str = NULL;
740 for (item = xbt_fifo_get_last_item(stack);
741 (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item)))
742 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
743 req = MC_state_get_executed_request(pair->graph_state, &value);
745 if (pair->requests > 0) {
746 req_str = MC_request_to_string(req, value);
747 XBT_INFO("%s", req_str);
750 XBT_INFO("End of system requests but evolution in Büchi automaton");
756 void MC_dump_stack_liveness(xbt_fifo_t stack)
759 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
764 while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
765 MC_pair_delete(pair);
774 void MC_print_statistics(mc_stats_t stats)
776 xbt_mheap_t previous_heap = mmalloc_get_current_heap();
778 if (stats->expanded_pairs == 0) {
779 XBT_INFO("Expanded states = %lu", stats->expanded_states);
780 XBT_INFO("Visited states = %lu", stats->visited_states);
782 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
783 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
785 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
787 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0')) {
788 fprintf(dot_output, "}\n");
791 if (initial_global_state != NULL) {
792 if (_sg_mc_comms_determinism)
793 XBT_INFO("Communication-deterministic : %s", !initial_global_state->comm_deterministic ? "No" : "Yes");
794 if (_sg_mc_send_determinism)
795 XBT_INFO("Send-deterministic : %s", !initial_global_state->send_deterministic ? "No" : "Yes");
797 mmalloc_set_current_heap(previous_heap);
800 void MC_assert(int prop)
802 if (MC_is_active() && !prop) {
803 XBT_INFO("**************************");
804 XBT_INFO("*** PROPERTY NOT VALID ***");
805 XBT_INFO("**************************");
806 XBT_INFO("Counter-example execution trace:");
807 MC_record_dump_path(mc_stack);
808 MC_dump_stack_safety(mc_stack);
809 MC_print_statistics(mc_stats);
816 user_max_depth_reached = 1;
819 void MC_automaton_load(const char *file)
822 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
826 if (_mc_property_automaton == NULL)
827 _mc_property_automaton = xbt_automaton_new();
829 xbt_automaton_load(_mc_property_automaton, file);
838 void MC_automaton_new_propositional_symbol(const char *id, void *fct)
841 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
845 if (_mc_property_automaton == NULL)
846 _mc_property_automaton = xbt_automaton_new();
848 xbt_automaton_propositional_symbol_new(_mc_property_automaton, id, fct);
857 void MC_dump_stacks(FILE* file)
859 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
863 stack_region_t current_stack;
865 xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
866 unw_context_t * context = (unw_context_t *)current_stack->context;
867 fprintf(file, "Stack %i:\n", nstack);
872 unw_init_local (&c, context);
875 const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
876 fprintf(file, " %i: %s\n", nframe, name);
878 } while(unw_step(&c));
888 double MC_process_clock_get(smx_process_t process)
892 return mc_time[process->pid];
900 void MC_process_clock_add(smx_process_t process, double amount)
902 mc_time[process->pid] += amount;