Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Move mc_model_checker in its own .c file
[simgrid.git] / src / mc / mc_global.c
index cdbec20..52848e6 100644 (file)
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
+#include <string.h>
+
+#include "mc_base.h"
+
+#ifndef _XBT_WIN32
 #include <unistd.h>
-#include <sys/types.h>
 #include <sys/wait.h>
 #include <sys/time.h>
-#include <sys/mman.h>
-#include <libgen.h>
+#endif
 
 #include "simgrid/sg_config.h"
 #include "../surf/surf_private.h"
 #include "../simix/smx_private.h"
-#include "../xbt/mmalloc/mmprivate.h"
 #include "xbt/fifo.h"
-#include "mc_private.h"
 #include "xbt/automaton.h"
 #include "xbt/dict.h"
 
-XBT_LOG_NEW_CATEGORY(mc, "All MC categories");
-XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
-                                "Logging specific to MC (global)");
-
-/* Configuration support */
-e_mc_reduce_t mc_reduce_kind = e_mc_reduce_unset;
-
-int _sg_do_model_check = 0;
-int _sg_mc_checkpoint = 0;
-int _sg_mc_sparse_checkpoint = 0;
-int _sg_mc_soft_dirty = 0;
-char *_sg_mc_property_file = NULL;
-int _sg_mc_timeout = 0;
-int _sg_mc_hash = 0;
-int _sg_mc_max_depth = 1000;
-int _sg_mc_visited = 0;
-char *_sg_mc_dot_output_file = NULL;
-int _sg_mc_comms_determinism = 0;
-int _sg_mc_send_determinism = 0;
-int _sg_mc_safety = 0;
-int _sg_mc_liveness = 0;
-
-int user_max_depth_reached = 0;
-
-void _mc_cfg_cb_reduce(const char *name, int pos)
-{
-  if (_sg_cfg_init_status && !_sg_do_model_check) {
-    xbt_die
-        ("You are specifying a reduction strategy after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
-  }
-  char *val = xbt_cfg_get_string(_sg_cfg_set, name);
-  if (!strcasecmp(val, "none")) {
-    mc_reduce_kind = e_mc_reduce_none;
-  } else if (!strcasecmp(val, "dpor")) {
-    mc_reduce_kind = e_mc_reduce_dpor;
-  } else {
-    xbt_die("configuration option %s can only take 'none' or 'dpor' as a value",
-            name);
-  }
-}
-
-void _mc_cfg_cb_checkpoint(const char *name, int pos)
-{
-  if (_sg_cfg_init_status && !_sg_do_model_check) {
-    xbt_die
-        ("You are specifying a checkpointing value after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
-  }
-  _sg_mc_checkpoint = xbt_cfg_get_int(_sg_cfg_set, name);
-}
-
-void _mc_cfg_cb_sparse_checkpoint(const char *name, int pos) {
-  if (_sg_cfg_init_status && !_sg_do_model_check) {
-    xbt_die("You are specifying a checkpointing value after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
-  }
-  _sg_mc_sparse_checkpoint = xbt_cfg_get_boolean(_sg_cfg_set, name);
-}
-
-void _mc_cfg_cb_soft_dirty(const char *name, int pos) {
-  if (_sg_cfg_init_status && !_sg_do_model_check) {
-    xbt_die("You are specifying a soft dirty value after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
-  }
-  _sg_mc_soft_dirty = xbt_cfg_get_boolean(_sg_cfg_set, name);
-}
-
-void _mc_cfg_cb_property(const char *name, int pos)
-{
-  if (_sg_cfg_init_status && !_sg_do_model_check) {
-    xbt_die
-        ("You are specifying a property after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
-  }
-  _sg_mc_property_file = xbt_cfg_get_string(_sg_cfg_set, name);
-}
-
-void _mc_cfg_cb_timeout(const char *name, int pos)
-{
-  if (_sg_cfg_init_status && !_sg_do_model_check) {
-    xbt_die
-        ("You are specifying a value to enable/disable timeout for wait requests after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
-  }
-  _sg_mc_timeout = xbt_cfg_get_boolean(_sg_cfg_set, name);
-}
-
-void _mc_cfg_cb_hash(const char *name, int pos)
-{
-  if (_sg_cfg_init_status && !_sg_do_model_check) {
-    xbt_die
-        ("You are specifying a value to enable/disable the use of global hash to speedup state comparaison, but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
-  }
-  _sg_mc_hash = xbt_cfg_get_boolean(_sg_cfg_set, name);
-}
+#ifdef HAVE_MC
+#include <libunwind.h>
 
-void _mc_cfg_cb_max_depth(const char *name, int pos)
-{
-  if (_sg_cfg_init_status && !_sg_do_model_check) {
-    xbt_die
-        ("You are specifying a max depth value after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
-  }
-  _sg_mc_max_depth = xbt_cfg_get_int(_sg_cfg_set, name);
-}
+#include "../xbt/mmalloc/mmprivate.h"
+#include "mc_object_info.h"
+#include "mc_comm_pattern.h"
+#include "mc_request.h"
+#include "mc_safety.h"
+#include "mc_memory_map.h"
+#include "mc_snapshot.h"
+#include "mc_liveness.h"
+#include "mc_private.h"
+#include "mc_unw.h"
+#endif
+#include "mc_record.h"
+#include "mc_protocol.h"
+#include "mc_client.h"
 
-void _mc_cfg_cb_visited(const char *name, int pos)
-{
-  if (_sg_cfg_init_status && !_sg_do_model_check) {
-    xbt_die
-        ("You are specifying a number of stored visited states after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
-  }
-  _sg_mc_visited = xbt_cfg_get_int(_sg_cfg_set, name);
-}
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
+                                "Logging specific to MC (global)");
 
-void _mc_cfg_cb_dot_output(const char *name, int pos)
-{
-  if (_sg_cfg_init_status && !_sg_do_model_check) {
-    xbt_die
-        ("You are specifying a file name for a dot output of graph state after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
-  }
-  _sg_mc_dot_output_file = xbt_cfg_get_string(_sg_cfg_set, name);
-}
+e_mc_mode_t mc_mode;
 
-void _mc_cfg_cb_comms_determinism(const char *name, int pos)
-{
-  if (_sg_cfg_init_status && !_sg_do_model_check) {
-    xbt_die
-        ("You are specifying a value to enable/disable the detection of determinism in the communications schemes after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
-  }
-  _sg_mc_comms_determinism = xbt_cfg_get_boolean(_sg_cfg_set, name);
-  mc_reduce_kind = e_mc_reduce_none;
-}
+double *mc_time = NULL;
 
-void _mc_cfg_cb_send_determinism(const char *name, int pos)
-{
-  if (_sg_cfg_init_status && !_sg_do_model_check) {
-    xbt_die
-        ("You are specifying a value to enable/disable the detection of send-determinism in the communications schemes after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
-  }
-  _sg_mc_send_determinism = xbt_cfg_get_boolean(_sg_cfg_set, name);
-  mc_reduce_kind = e_mc_reduce_none;
-}
+#ifdef HAVE_MC
+int user_max_depth_reached = 0;
 
 /* MC global data structures */
 mc_state_t mc_current_state = NULL;
 char mc_replay_mode = FALSE;
-double *mc_time = NULL;
+
 __thread mc_comparison_times_t mc_comp_times = NULL;
 __thread double mc_snapshot_comparison_time;
 mc_stats_t mc_stats = NULL;
@@ -171,14 +62,6 @@ xbt_fifo_t mc_stack = NULL;
 /* Liveness */
 xbt_automaton_t _mc_property_automaton = NULL;
 
-/* Variables */
-mc_object_info_t mc_libsimgrid_info = NULL;
-mc_object_info_t mc_binary_info = NULL;
-
-mc_object_info_t mc_object_infos[2] = { NULL, NULL };
-
-size_t mc_object_infos_size = 2;
-
 /* Dot output */
 FILE *dot_output = NULL;
 const char *colors[13];
@@ -216,32 +99,21 @@ static void MC_init_dot_output()
 
 }
 
-static void MC_init_debug_info(void)
+void MC_init()
 {
-  XBT_INFO("Get debug information ...");
-
-  memory_map_t maps = MC_get_memory_map();
-
-  /* Get local variables for state equality detection */
-  mc_binary_info = MC_find_object_info(maps, xbt_binary_name, 1);
-  mc_object_infos[0] = mc_binary_info;
-
-  mc_libsimgrid_info = MC_find_object_info(maps, libsimgrid_path, 0);
-  mc_object_infos[1] = mc_libsimgrid_info;
-
-  // Use information of the other objects:
-  MC_post_process_object_info(mc_binary_info);
-  MC_post_process_object_info(mc_libsimgrid_info);
-
-  MC_free_memory_map(maps);
-  XBT_INFO("Get debug information done !");
+  MC_init_pid(getpid(), -1);
 }
 
-
-mc_model_checker_t mc_model_checker = NULL;
-
-void MC_init()
+void MC_init_pid(pid_t pid, int socket)
 {
+  if (mc_mode == MC_MODE_NONE) {
+    if (getenv(MC_ENV_SOCKET_FD)) {
+      mc_mode = MC_MODE_CLIENT;
+    } else {
+      mc_mode = MC_MODE_STANDALONE;
+    }
+  }
+
   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
 
   mc_time = xbt_new0(double, simix_process_maxpid);
@@ -251,10 +123,7 @@ void MC_init()
 
   MC_SET_MC_HEAP;
 
-  mc_model_checker = xbt_new0(s_mc_model_checker_t, 1);
-  mc_model_checker->pages = mc_pages_store_new();
-  mc_model_checker->fd_clear_refs = -1;
-  mc_model_checker->fd_pagemap = -1;
+  mc_model_checker = MC_model_checker_new(pid, socket);
 
   mc_comp_times = xbt_new0(s_mc_comparison_times_t, 1);
 
@@ -262,9 +131,6 @@ void MC_init()
   mc_stats = xbt_new0(s_mc_stats_t, 1);
   mc_stats->state_size = 1;
 
-  MC_init_memory_map_info();
-  MC_init_debug_info();         /* FIXME : get debug information only if liveness verification or visited state reduction */
-
   if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0'))
     MC_init_dot_output();
 
@@ -292,38 +158,49 @@ void MC_init()
     /* Ignore local variable about time used for tracing */
     MC_ignore_local_variable("start_time", "*");
 
+    /* Main MC state: */
     MC_ignore_global_variable("mc_model_checker");
+    MC_ignore_global_variable("communications_pattern");
+    MC_ignore_global_variable("initial_communications_pattern");
+    MC_ignore_global_variable("incomplete_communications_pattern");
 
-    // Mot of those things could be moved into mc_model_checker:
-    MC_ignore_global_variable("compared_pointers");
+    /* MC __thread variables: */
+    MC_ignore_global_variable("mc_diff_info");
     MC_ignore_global_variable("mc_comp_times");
     MC_ignore_global_variable("mc_snapshot_comparison_time");
+
+    /* This MC state is used in MC replay as well: */
     MC_ignore_global_variable("mc_time");
-    MC_ignore_global_variable("smpi_current_rank");
-    MC_ignore_global_variable("counter");       /* Static variable used for tracing */
-    MC_ignore_global_variable("maestro_stack_start");
-    MC_ignore_global_variable("maestro_stack_end");
-    MC_ignore_global_variable("smx_total_comms");
-    MC_ignore_global_variable("communications_pattern");
-    MC_ignore_global_variable("initial_communications_pattern");
-    MC_ignore_global_variable("incomplete_communications_pattern");
 
-    if (MC_is_active()) {
-      MC_ignore_global_variable("mc_diff_info");
-    }
+    /* Static variable used for tracing */
+    MC_ignore_global_variable("counter");
+
+    /* SIMIX */
+    MC_ignore_global_variable("smx_total_comms");
 
     MC_ignore_heap(mc_time, simix_process_maxpid * sizeof(double));
 
     smx_process_t process;
-    xbt_swag_foreach(process, simix_global->process_list) {
-      MC_ignore_heap(&(process->process_hookup),
-                     sizeof(process->process_hookup));
-                     }
+    // FIXME, cross-process support (simix_global->process_list)
+
+    if (mc_mode == MC_MODE_STANDALONE || mc_mode == MC_MODE_CLIENT) {
+      xbt_swag_foreach(process, simix_global->process_list) {
+        MC_ignore_heap(&(process->process_hookup),
+                       sizeof(process->process_hookup));
+                       }
+    } else {
+      // TODO
+    }
   }
 
   if (raw_mem_set)
     MC_SET_MC_HEAP;
 
+  if (mc_mode == MC_MODE_CLIENT) {
+    // This will move somehwere else:
+    MC_client_handle_messages();
+  }
+
 }
 
 /*******************************  Core of MC *******************************/
@@ -454,38 +331,6 @@ void MC_exit(void)
   //xbt_abort();
 }
 
-int SIMIX_pre_mc_random(smx_simcall_t simcall, int min, int max)
-{
-
-  return simcall->mc_value;
-}
-
-
-int MC_random(int min, int max)
-{
-  /*FIXME: return mc_current_state->executed_transition->random.value; */
-  return simcall_mc_random(min, max);
-}
-
-/**
- * \brief Schedules all the process that are ready to run
- */
-void MC_wait_for_requests(void)
-{
-  smx_process_t process;
-  smx_simcall_t req;
-  unsigned int iter;
-
-  while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
-    SIMIX_process_runall();
-    xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
-      req = &process->simcall;
-      if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
-        SIMIX_simcall_enter(req, 0);
-    }
-  }
-}
-
 int MC_deadlock_check()
 {
   int deadlock = FALSE;
@@ -493,8 +338,7 @@ int MC_deadlock_check()
   if (xbt_swag_size(simix_global->process_list)) {
     deadlock = TRUE;
     xbt_swag_foreach(process, simix_global->process_list) {
-      if (process->simcall.call != SIMCALL_NONE
-          && MC_request_is_enabled(&process->simcall)) {
+      if (MC_request_is_enabled(&process->simcall)) {
         deadlock = FALSE;
         break;
       }
@@ -503,23 +347,54 @@ int MC_deadlock_check()
   return deadlock;
 }
 
+void mc_update_comm_pattern(mc_call_type call_type, smx_simcall_t req, int value, xbt_dynar_t pattern) {
+  switch(call_type) {
+  case MC_CALL_TYPE_NONE:
+    break;
+  case MC_CALL_TYPE_SEND:
+  case MC_CALL_TYPE_RECV:
+    get_comm_pattern(pattern, req, call_type);
+    break;
+  case MC_CALL_TYPE_WAIT:
+    {
+      smx_synchro_t current_comm = NULL;
+      if (call_type == MC_CALL_TYPE_WAIT)
+        current_comm = simcall_comm_wait__get__comm(req);
+      else
+        current_comm = xbt_dynar_get_as(simcall_comm_waitany__get__comms(req), value, smx_synchro_t);
+      // First wait only must be considered:
+      if (current_comm->comm.refcount == 1)
+        complete_comm_pattern(pattern, current_comm);
+      break;
+    }
+  default:
+    xbt_die("Unexpected call type %i", (int)call_type);
+  }
+}
+
 /**
  * \brief Re-executes from the state at position start all the transitions indicated by
  *        a given model-checker stack.
  * \param stack The stack with the transitions to execute.
  * \param start Start index to begin the re-execution.
+ *
+ *  If start==-1, restore the initial state and replay the actions the
+ *  the transitions in the stack.
+ *
+ *  Otherwise, we only replay a part of the transitions of the stacks
+ *  without restoring the state: it is assumed that the current state
+ *  match with the transitions to execute.
  */
 void MC_replay(xbt_fifo_t stack, int start)
 {
   int raw_mem = (mmalloc_get_current_heap() == mc_heap);
 
-  int value, i = 1, count = 1, call = 0, j;
+  int value, i = 1, count = 1, j;
   char *req_str;
   smx_simcall_t req = NULL, saved_req = NULL;
   xbt_fifo_item_t item, start_item;
   mc_state_t state;
   smx_process_t process = NULL;
-  smx_action_t current_comm;
 
   XBT_DEBUG("**** Begin Replay ****");
 
@@ -564,7 +439,6 @@ void MC_replay(xbt_fifo_t stack, int start)
 
   MC_SET_STD_HEAP;
 
-
   /* Traverse the stack from the state at position start and re-execute the transitions */
   for (item = start_item;
        item != xbt_fifo_get_first_item(stack);
@@ -595,39 +469,19 @@ void MC_replay(xbt_fifo_t stack, int start)
       }
 
       /* TODO : handle test and testany simcalls */
+      mc_call_type call = MC_CALL_TYPE_NONE;
       if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
-        if (req->call == SIMCALL_COMM_ISEND)
-          call = 1;
-        else if (req->call == SIMCALL_COMM_IRECV)
-          call = 2;
-        else if (req->call == SIMCALL_COMM_WAIT)
-          call = 3;
-        else if (req->call == SIMCALL_COMM_WAITANY)
-          call = 4;
+        call = mc_get_call_type(req);
       }
 
-      SIMIX_simcall_enter(req, value);
+      SIMIX_simcall_handle(req, value);
 
       if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
         MC_SET_MC_HEAP;
-        if (call == 1) { /* Send */
-          get_comm_pattern(communications_pattern, req, call);
-        } else if (call == 2) { /* Recv */
-          get_comm_pattern(communications_pattern, req, call);
-        } else if (call == 3) { /* Wait */
-          current_comm = simcall_comm_wait__get__comm(req);
-          if (current_comm->comm.refcount == 1)  /* First wait only must be considered */
-            complete_comm_pattern(communications_pattern, current_comm);
-        } else if (call == 4) { /* WaitAny */
-          current_comm = xbt_dynar_get_as(simcall_comm_waitany__get__comms(req), value, smx_action_t);
-          if (current_comm->comm.refcount == 1) /* First wait only must be considered */
-            complete_comm_pattern(communications_pattern, current_comm);
-        }
+        mc_update_comm_pattern(call, req, value, communications_pattern);
         MC_SET_STD_HEAP;
-        call = 0;
       }
 
-
       MC_wait_for_requests();
 
       count++;
@@ -715,7 +569,7 @@ void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
 
         }
 
-        SIMIX_simcall_enter(req, value);
+        SIMIX_simcall_handle(req, value);
         MC_wait_for_requests();
       }
 
@@ -888,6 +742,7 @@ void MC_assert(int prop)
     XBT_INFO("*** PROPERTY NOT VALID ***");
     XBT_INFO("**************************");
     XBT_INFO("Counter-example execution trace:");
+    MC_record_dump_path(mc_stack);
     MC_dump_stack_safety(mc_stack);
     MC_print_statistics(mc_stats);
     xbt_abort();
@@ -899,23 +754,6 @@ void MC_cut(void)
   user_max_depth_reached = 1;
 }
 
-void MC_process_clock_add(smx_process_t process, double amount)
-{
-  mc_time[process->pid] += amount;
-}
-
-double MC_process_clock_get(smx_process_t process)
-{
-  if (mc_time) {
-    if (process != NULL)
-      return mc_time[process->pid];
-    else
-      return -1;
-  } else {
-    return 0;
-  }
-}
-
 void MC_automaton_load(const char *file)
 {
 
@@ -953,3 +791,51 @@ void MC_automaton_new_propositional_symbol(const char *id, void *fct)
     MC_SET_MC_HEAP;
 
 }
+
+void MC_dump_stacks(FILE* file)
+{
+  int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
+  MC_SET_MC_HEAP;
+
+  int nstack = 0;
+  stack_region_t current_stack;
+  unsigned cursor;
+  xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
+    unw_context_t * context = (unw_context_t *)current_stack->context;
+    fprintf(file, "Stack %i:\n", nstack);
+
+    int nframe = 0;
+    char buffer[100];
+    unw_cursor_t c;
+    unw_init_local (&c, context);
+    unw_word_t off;
+    do {
+      const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
+      fprintf(file, "  %i: %s\n", nframe, name);
+      ++nframe;
+    } while(unw_step(&c));
+
+    ++nstack;
+  }
+
+  if (raw_mem_set)
+    MC_SET_MC_HEAP;
+}
+#endif
+
+double MC_process_clock_get(smx_process_t process)
+{
+  if (mc_time) {
+    if (process != NULL)
+      return mc_time[process->pid];
+    else
+      return -1;
+  } else {
+    return 0;
+  }
+}
+
+void MC_process_clock_add(smx_process_t process, double amount)
+{
+  mc_time[process->pid] += amount;
+}