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>
26 #include <xbt/mmalloc.h>
28 #include "../xbt/mmalloc/mmprivate.h"
29 #include "mc_object_info.h"
30 #include "mc_comm_pattern.h"
31 #include "mc_request.h"
32 #include "mc_safety.h"
33 #include "mc_memory_map.h"
34 #include "mc_snapshot.h"
35 #include "mc_liveness.h"
36 #include "mc_private.h"
39 #include "mc_record.h"
40 #include "mc_protocol.h"
41 #include "mc_client.h"
43 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
44 "Logging specific to MC (global)");
48 double *mc_time = NULL;
51 int user_max_depth_reached = 0;
53 /* MC global data structures */
54 mc_state_t mc_current_state = NULL;
55 char mc_replay_mode = FALSE;
57 __thread mc_comparison_times_t mc_comp_times = NULL;
58 __thread double mc_snapshot_comparison_time;
59 mc_stats_t mc_stats = NULL;
60 mc_global_t initial_global_state = NULL;
61 xbt_fifo_t mc_stack = NULL;
64 xbt_automaton_t _mc_property_automaton = NULL;
67 FILE *dot_output = NULL;
68 const char *colors[13];
71 /******************************* Initialisation of MC *******************************/
72 /*********************************************************************************/
74 static void MC_init_dot_output()
75 { /* FIXME : more colors */
80 colors[3] = "goldenrod";
83 colors[6] = "magenta";
84 colors[7] = "turquoise4";
86 colors[9] = "forestgreen";
87 colors[10] = "hotpink";
88 colors[11] = "lightblue";
91 dot_output = fopen(_sg_mc_dot_output_file, "w");
93 if (dot_output == NULL) {
94 perror("Error open dot output file");
99 "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");
105 MC_init_pid(getpid(), -1);
108 void MC_init_pid(pid_t pid, int socket)
110 if (mc_mode == MC_MODE_NONE) {
111 if (getenv(MC_ENV_SOCKET_FD)) {
112 mc_mode = MC_MODE_CLIENT;
114 mc_mode = MC_MODE_STANDALONE;
118 mc_time = xbt_new0(double, simix_process_maxpid);
120 /* Initialize the data structures that must be persistent across every
121 iteration of the model-checker (in RAW memory) */
123 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
125 mc_model_checker = MC_model_checker_new(pid, socket);
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 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0'))
134 MC_init_dot_output();
137 //parmap = xbt_parmap_mc_new(xbt_os_get_numcores(), XBT_PARMAP_DEFAULT);
141 if (_sg_mc_visited > 0 || _sg_mc_liveness) {
142 /* Ignore some variables from xbt/ex.h used by exception e for stacks comparison */
143 MC_ignore_local_variable("e", "*");
144 MC_ignore_local_variable("__ex_cleanup", "*");
145 MC_ignore_local_variable("__ex_mctx_en", "*");
146 MC_ignore_local_variable("__ex_mctx_me", "*");
147 MC_ignore_local_variable("__xbt_ex_ctx_ptr", "*");
148 MC_ignore_local_variable("_log_ev", "*");
149 MC_ignore_local_variable("_throw_ctx", "*");
150 MC_ignore_local_variable("ctx", "*");
152 MC_ignore_local_variable("self", "simcall_BODY_mc_snapshot");
153 MC_ignore_local_variable("next_cont"
154 "ext", "smx_ctx_sysv_suspend_serial");
155 MC_ignore_local_variable("i", "smx_ctx_sysv_suspend_serial");
157 /* Ignore local variable about time used for tracing */
158 MC_ignore_local_variable("start_time", "*");
161 MC_ignore_global_variable("mc_model_checker");
162 MC_ignore_global_variable("communications_pattern");
163 MC_ignore_global_variable("initial_communications_pattern");
164 MC_ignore_global_variable("incomplete_communications_pattern");
166 /* MC __thread variables: */
167 MC_ignore_global_variable("mc_diff_info");
168 MC_ignore_global_variable("mc_comp_times");
169 MC_ignore_global_variable("mc_snapshot_comparison_time");
171 /* This MC state is used in MC replay as well: */
172 MC_ignore_global_variable("mc_time");
174 /* Static variable used for tracing */
175 MC_ignore_global_variable("counter");
178 MC_ignore_global_variable("smx_total_comms");
180 if (mc_mode == MC_MODE_STANDALONE || mc_mode == MC_MODE_CLIENT) {
181 MC_ignore_heap(mc_time, simix_process_maxpid * sizeof(double));
182 smx_process_t process;
183 xbt_swag_foreach(process, simix_global->process_list) {
184 MC_ignore_heap(&(process->process_hookup),
185 sizeof(process->process_hookup));
190 mmalloc_set_current_heap(heap);
192 if (mc_mode == MC_MODE_CLIENT) {
193 // This will move somehwere else:
194 MC_client_handle_messages();
199 /******************************* Core of MC *******************************/
200 /**************************************************************************/
202 static void MC_modelcheck_comm_determinism_init(void)
206 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
208 /* Create exploration stack */
209 mc_stack = xbt_fifo_new();
213 MC_pre_modelcheck_comm_determinism();
216 initial_global_state = xbt_new0(s_mc_global_t, 1);
217 initial_global_state->snapshot = MC_take_snapshot(0);
218 initial_global_state->initial_communications_pattern_done = 0;
219 initial_global_state->comm_deterministic = 1;
220 initial_global_state->send_deterministic = 1;
223 MC_modelcheck_comm_determinism();
225 mmalloc_set_current_heap(heap);
228 static void MC_modelcheck_safety_init(void)
234 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
236 /* Create exploration stack */
237 mc_stack = xbt_fifo_new();
241 MC_pre_modelcheck_safety();
244 /* Save the initial state */
245 initial_global_state = xbt_new0(s_mc_global_t, 1);
246 initial_global_state->snapshot = MC_take_snapshot(0);
249 MC_modelcheck_safety();
251 mmalloc_set_current_heap(heap);
257 static void MC_modelcheck_liveness_init()
263 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
265 /* Create exploration stack */
266 mc_stack = xbt_fifo_new();
268 /* Create the initial state */
269 initial_global_state = xbt_new0(s_mc_global_t, 1);
273 MC_pre_modelcheck_liveness();
276 MC_print_statistics(mc_stats);
279 mmalloc_set_current_heap(heap);
283 void MC_do_the_modelcheck_for_real()
286 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
287 XBT_INFO("Check communication determinism");
288 MC_modelcheck_comm_determinism_init();
289 } else if (!_sg_mc_property_file || _sg_mc_property_file[0] == '\0') {
290 if (mc_reduce_kind == e_mc_reduce_unset)
291 mc_reduce_kind = e_mc_reduce_dpor;
292 XBT_INFO("Check a safety property");
293 MC_modelcheck_safety_init();
295 if (mc_reduce_kind == e_mc_reduce_unset)
296 mc_reduce_kind = e_mc_reduce_none;
297 XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
298 MC_automaton_load(_sg_mc_property_file);
299 MC_modelcheck_liveness_init();
312 int MC_deadlock_check()
314 int deadlock = FALSE;
315 smx_process_t process;
316 if (xbt_swag_size(simix_global->process_list)) {
318 xbt_swag_foreach(process, simix_global->process_list) {
319 if (MC_request_is_enabled(&process->simcall)) {
328 void mc_update_comm_pattern(mc_call_type call_type, smx_simcall_t req, int value, xbt_dynar_t pattern) {
330 case MC_CALL_TYPE_NONE:
332 case MC_CALL_TYPE_SEND:
333 case MC_CALL_TYPE_RECV:
334 get_comm_pattern(pattern, req, call_type);
336 case MC_CALL_TYPE_WAIT:
338 smx_synchro_t current_comm = NULL;
339 if (call_type == MC_CALL_TYPE_WAIT)
340 current_comm = simcall_comm_wait__get__comm(req);
342 current_comm = xbt_dynar_get_as(simcall_comm_waitany__get__comms(req), value, smx_synchro_t);
343 // First wait only must be considered:
344 if (current_comm->comm.refcount == 1)
345 complete_comm_pattern(pattern, current_comm);
349 xbt_die("Unexpected call type %i", (int)call_type);
354 * \brief Re-executes from the state at position start all the transitions indicated by
355 * a given model-checker stack.
356 * \param stack The stack with the transitions to execute.
357 * \param start Start index to begin the re-execution.
359 * If start==-1, restore the initial state and replay the actions the
360 * the transitions in the stack.
362 * Otherwise, we only replay a part of the transitions of the stacks
363 * without restoring the state: it is assumed that the current state
364 * match with the transitions to execute.
366 void MC_replay(xbt_fifo_t stack, int start)
368 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
370 int value, i = 1, count = 1, j;
372 smx_simcall_t req = NULL, saved_req = NULL;
373 xbt_fifo_item_t item, start_item;
375 smx_process_t process = NULL;
377 XBT_DEBUG("**** Begin Replay ****");
380 /* Restore the initial state */
381 MC_restore_snapshot(initial_global_state->snapshot);
382 /* At the moment of taking the snapshot the raw heap was set, so restoring
383 * it will set it back again, we have to unset it to continue */
387 start_item = xbt_fifo_get_last_item(stack);
390 start_item = xbt_fifo_get_prev_item(start_item);
397 if (mc_reduce_kind == e_mc_reduce_dpor) {
398 xbt_dict_reset(first_enabled_state);
399 xbt_swag_foreach(process, simix_global->process_list) {
400 if (MC_process_is_enabled(process)) {
401 char *key = bprintf("%lu", process->pid);
402 char *data = bprintf("%d", count);
403 xbt_dict_set(first_enabled_state, key, data, NULL);
409 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
410 for (j=0; j<simix_process_maxpid; j++) {
411 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(communications_pattern, j, xbt_dynar_t));
413 for (j=0; j<simix_process_maxpid; j++) {
414 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
420 /* Traverse the stack from the state at position start and re-execute the transitions */
421 for (item = start_item;
422 item != xbt_fifo_get_first_item(stack);
423 item = xbt_fifo_get_prev_item(item)) {
425 state = (mc_state_t) xbt_fifo_get_item_content(item);
426 saved_req = MC_state_get_executed_request(state, &value);
428 if (mc_reduce_kind == e_mc_reduce_dpor) {
430 char *key = bprintf("%lu", saved_req->issuer->pid);
431 if(xbt_dict_get_or_null(first_enabled_state, key))
432 xbt_dict_remove(first_enabled_state, key);
438 /* because we got a copy of the executed request, we have to fetch the
439 real one, pointed by the request field of the issuer process */
440 req = &saved_req->issuer->simcall;
442 /* Debug information */
443 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
444 req_str = MC_request_to_string(req, value);
445 XBT_DEBUG("Replay: %s (%p)", req_str, state);
449 /* TODO : handle test and testany simcalls */
450 mc_call_type call = MC_CALL_TYPE_NONE;
451 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
452 call = mc_get_call_type(req);
455 SIMIX_simcall_handle(req, value);
457 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
459 mc_update_comm_pattern(call, req, value, communications_pattern);
463 MC_wait_for_requests();
467 if (mc_reduce_kind == e_mc_reduce_dpor) {
469 /* Insert in dict all enabled processes */
470 xbt_swag_foreach(process, simix_global->process_list) {
471 if (MC_process_is_enabled(process) ) {
472 char *key = bprintf("%lu", process->pid);
473 if (xbt_dict_get_or_null(first_enabled_state, key) == NULL) {
474 char *data = bprintf("%d", count);
475 xbt_dict_set(first_enabled_state, key, data, NULL);
484 /* Update statistics */
485 mc_stats->visited_states++;
486 mc_stats->executed_transitions++;
490 XBT_DEBUG("**** End Replay ****");
491 mmalloc_set_current_heap(heap);
494 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
497 initial_global_state->raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
499 xbt_fifo_item_t item;
502 XBT_DEBUG("**** Begin Replay ****");
504 /* Restore the initial state */
505 MC_restore_snapshot(initial_global_state->snapshot);
507 /* At the moment of taking the snapshot the raw heap was set, so restoring
508 * it will set it back again, we have to unset it to continue */
509 if (!initial_global_state->raw_mem_set)
512 /* Traverse the stack from the initial state and re-execute the transitions */
513 for (item = xbt_fifo_get_last_item(stack);
514 all_stack ? depth <= xbt_fifo_size(stack) : item != xbt_fifo_get_first_item(stack);
515 item = xbt_fifo_get_prev_item(item)) {
517 mc_pair_t pair = (mc_pair_t) xbt_fifo_get_item_content(item);
519 mc_state_t state = (mc_state_t) pair->graph_state;
520 smx_simcall_t req = NULL, saved_req = NULL;
524 if (pair->requests > 0) {
526 saved_req = MC_state_get_executed_request(state, &value);
527 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
529 if (saved_req != NULL) {
530 /* because we got a copy of the executed request, we have to fetch the
531 real one, pointed by the request field of the issuer process */
532 req = &saved_req->issuer->simcall;
533 //XBT_DEBUG("Req->call %u", req->call);
535 /* Debug information */
536 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
537 req_str = MC_request_to_string(req, value);
538 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
544 SIMIX_simcall_handle(req, value);
545 MC_wait_for_requests();
548 /* Update statistics */
549 mc_stats->visited_pairs++;
550 mc_stats->executed_transitions++;
556 XBT_DEBUG("**** End Replay ****");
558 if (initial_global_state->raw_mem_set)
566 * \brief Dumps the contents of a model-checker's stack and shows the actual
568 * \param stack The stack to dump
570 void MC_dump_stack_safety(xbt_fifo_t stack)
572 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
574 MC_show_stack_safety(stack);
576 if (!_sg_mc_checkpoint) {
581 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
582 MC_state_delete(state);
587 mmalloc_set_current_heap(heap);
591 void MC_show_stack_safety(xbt_fifo_t stack)
593 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
597 xbt_fifo_item_t item;
599 char *req_str = NULL;
601 for (item = xbt_fifo_get_last_item(stack);
602 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
603 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
604 req = MC_state_get_executed_request(state, &value);
606 req_str = MC_request_to_string(req, value);
607 XBT_INFO("%s", req_str);
612 mmalloc_set_current_heap(heap);
615 void MC_show_deadlock(smx_simcall_t req)
617 /*char *req_str = NULL; */
618 XBT_INFO("**************************");
619 XBT_INFO("*** DEAD-LOCK DETECTED ***");
620 XBT_INFO("**************************");
621 XBT_INFO("Locked request:");
622 /*req_str = MC_request_to_string(req);
623 XBT_INFO("%s", req_str);
624 xbt_free(req_str); */
625 XBT_INFO("Counter-example execution trace:");
626 MC_dump_stack_safety(mc_stack);
627 MC_print_statistics(mc_stats);
631 void MC_show_stack_liveness(xbt_fifo_t stack)
635 xbt_fifo_item_t item;
637 char *req_str = NULL;
639 for (item = xbt_fifo_get_last_item(stack);
640 (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item)))
641 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
642 req = MC_state_get_executed_request(pair->graph_state, &value);
644 if (pair->requests > 0) {
645 req_str = MC_request_to_string(req, value);
646 XBT_INFO("%s", req_str);
649 XBT_INFO("End of system requests but evolution in Büchi automaton");
655 void MC_dump_stack_liveness(xbt_fifo_t stack)
657 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
659 while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
660 MC_pair_delete(pair);
661 mmalloc_set_current_heap(heap);
665 void MC_print_statistics(mc_stats_t stats)
667 if (stats->expanded_pairs == 0) {
668 XBT_INFO("Expanded states = %lu", stats->expanded_states);
669 XBT_INFO("Visited states = %lu", stats->visited_states);
671 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
672 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
674 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
675 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
676 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0')) {
677 fprintf(dot_output, "}\n");
680 if (initial_global_state != NULL) {
681 if (_sg_mc_comms_determinism)
682 XBT_INFO("Communication-deterministic : %s",
683 !initial_global_state->comm_deterministic ? "No" : "Yes");
684 if (_sg_mc_send_determinism)
685 XBT_INFO("Send-deterministic : %s",
686 !initial_global_state->send_deterministic ? "No" : "Yes");
688 mmalloc_set_current_heap(heap);
691 void MC_automaton_load(const char *file)
693 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
695 if (_mc_property_automaton == NULL)
696 _mc_property_automaton = xbt_automaton_new();
698 xbt_automaton_load(_mc_property_automaton, file);
699 mmalloc_set_current_heap(heap);
702 static void register_symbol(xbt_automaton_propositional_symbol_t symbol)
704 if (mc_mode != MC_MODE_CLIENT)
706 s_mc_register_symbol_message_t message;
707 message.type = MC_MESSAGE_REGISTER_SYMBOL;
708 const char* name = xbt_automaton_propositional_symbol_get_name(symbol);
709 if (strlen(name) + 1 > sizeof(message.name))
710 xbt_die("Symbol is too long");
711 strncpy(message.name, name, sizeof(message.name));
712 message.callback = xbt_automaton_propositional_symbol_get_callback(symbol);
713 message.data = xbt_automaton_propositional_symbol_get_data(symbol);
714 MC_client_send_message(&message, sizeof(message));
717 void MC_automaton_new_propositional_symbol(const char *id, int(*fct)(void))
719 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
721 if (_mc_property_automaton == NULL)
722 _mc_property_automaton = xbt_automaton_new();
724 xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new(_mc_property_automaton, id, fct);
725 register_symbol(symbol);
726 mmalloc_set_current_heap(heap);
729 void MC_automaton_new_propositional_symbol_pointer(const char *id, int* value)
731 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
732 if (_mc_property_automaton == NULL)
733 _mc_property_automaton = xbt_automaton_new();
734 xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new_pointer(_mc_property_automaton, id, value);
735 register_symbol(symbol);
736 mmalloc_set_current_heap(heap);
739 void MC_automaton_new_propositional_symbol_callback(const char* id,
740 xbt_automaton_propositional_symbol_callback_type callback,
741 void* data, xbt_automaton_propositional_symbol_free_function_type free_function)
743 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
744 if (_mc_property_automaton == NULL)
745 _mc_property_automaton = xbt_automaton_new();
746 xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new_callback(
747 _mc_property_automaton, id, callback, data, free_function);
748 register_symbol(symbol);
749 mmalloc_set_current_heap(heap);
752 void MC_dump_stacks(FILE* file)
754 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
757 stack_region_t current_stack;
759 xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
760 unw_context_t * context = (unw_context_t *)current_stack->context;
761 fprintf(file, "Stack %i:\n", nstack);
766 unw_init_local (&c, context);
769 const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
770 fprintf(file, " %i: %s\n", nframe, name);
772 } while(unw_step(&c));
776 mmalloc_set_current_heap(heap);
780 double MC_process_clock_get(smx_process_t process)
784 return mc_time[process->pid];
792 void MC_process_clock_add(smx_process_t process, double amount)
794 mc_time[process->pid] += amount;