Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Move mc_safety code in simgrid::mc
[simgrid.git] / src / mc / mc_safety.cpp
index 160fea3..74fde12 100644 (file)
@@ -4,7 +4,15 @@
 /* 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 <assert.h>
+#include <cassert>
+
+#include <cstdio>
+
+#include <xbt/log.h>
+#include <xbt/dynar.h>
+#include <xbt/dynar.hpp>
+#include <xbt/fifo.h>
+#include <xbt/sysdep.h>
 
 #include "src/mc/mc_state.h"
 #include "src/mc/mc_request.h"
@@ -24,11 +32,14 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_safety, mc,
 
 }
 
+namespace simgrid {
+namespace mc {
+
 static int is_exploration_stack_state(mc_state_t current_state){
 
   xbt_fifo_item_t item;
   mc_state_t stack_state;
-  for(item = xbt_fifo_get_first_item(mc_stack); item != NULL; item = xbt_fifo_get_next_item(item)) {
+  for(item = xbt_fifo_get_first_item(mc_stack); item != nullptr; item = xbt_fifo_get_next_item(item)) {
     stack_state = (mc_state_t) xbt_fifo_get_item_content(item);
     if(snapshot_compare(stack_state, current_state) == 0){
       XBT_INFO("Non-progressive cycle : state %d -> state %d", stack_state->num, current_state->num);
@@ -38,15 +49,15 @@ static int is_exploration_stack_state(mc_state_t current_state){
   return 0;
 }
 
-static void MC_modelcheck_safety_init(void);
+static void modelcheck_safety_init(void);
 
 /**
  *  \brief Initialize the DPOR exploration algorithm
  */
-static void MC_pre_modelcheck_safety()
+static void pre_modelcheck_safety()
 {
   if (_sg_mc_visited > 0)
-    visited_states = xbt_dynar_new(sizeof(mc_visited_state_t), visited_state_free_voidp);
+    simgrid::mc::visited_states = simgrid::xbt::newDeleteDynar<simgrid::mc::VisitedState>();
 
   mc_state_t initial_state = MC_state_new();
 
@@ -57,14 +68,12 @@ static void MC_pre_modelcheck_safety()
   mc_model_checker->wait_for_requests();
 
   /* Get an enabled process and insert it in the interleave set of the initial state */
-  smx_process_t process;
-  MC_EACH_SIMIX_PROCESS(process,
-    if (MC_process_is_enabled(process)) {
-      MC_state_interleave_process(initial_state, process);
-      if (mc_reduce_kind != e_mc_reduce_none)
+  for (auto& p : mc_model_checker->process().simix_processes())
+    if (simgrid::mc::process_is_enabled(&p.copy)) {
+      MC_state_interleave_process(initial_state, &p.copy);
+      if (simgrid::mc::reduction_mode != simgrid::mc::ReductionMode::none)
         break;
     }
-  );
 
   xbt_fifo_unshift(mc_stack, initial_state);
 }
@@ -73,16 +82,16 @@ static void MC_pre_modelcheck_safety()
 /** \brief Model-check the application using a DFS exploration
  *         with DPOR (Dynamic Partial Order Reductions)
  */
-int MC_modelcheck_safety(void)
+int modelcheck_safety(void)
 {
-  MC_modelcheck_safety_init();
+  modelcheck_safety_init();
 
-  char *req_str = NULL;
+  char *req_str = nullptr;
   int value;
-  smx_simcall_t req = NULL;
-  mc_state_t state = NULL, prev_state = NULL, next_state = NULL;
-  xbt_fifo_item_t item = NULL;
-  mc_visited_state_t visited_state = NULL;
+  smx_simcall_t req = nullptr;
+  mc_state_t state = nullptr, prev_state = NULL, next_state = NULL;
+  xbt_fifo_item_t item = nullptr;
+  simgrid::mc::VisitedState* visited_state = nullptr;
 
   while (xbt_fifo_size(mc_stack) > 0) {
 
@@ -101,15 +110,14 @@ int MC_modelcheck_safety(void)
     /* If there are processes to interleave and the maximum depth has not been reached
        then perform one step of the exploration algorithm */
     if (xbt_fifo_size(mc_stack) <= _sg_mc_max_depth && !user_max_depth_reached
-        && (req = MC_state_get_request(state, &value)) && visited_state == NULL) {
+        && (req = MC_state_get_request(state, &value)) && visited_state == nullptr) {
 
-      req_str = MC_request_to_string(req, value, MC_REQUEST_SIMIX);
+      req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
       XBT_DEBUG("Execute: %s", req_str);
       xbt_free(req_str);
 
-      if (dot_output != NULL) {
-        req_str = MC_request_get_dot_output(req, value);
-      }
+      if (dot_output != nullptr)
+        req_str = simgrid::mc::request_get_dot_output(req, value);
 
       MC_state_set_executed_request(state, req, value);
       mc_stats->executed_transitions++;
@@ -118,7 +126,7 @@ int MC_modelcheck_safety(void)
       //   MC_execute_transition(req, value)
 
       /* Answer the request */
-      MC_simcall_handle(req, value);
+      simgrid::mc::handle_simcall(req, value);
       mc_model_checker->wait_for_requests();
 
       /* Create the new expanded state */
@@ -129,31 +137,26 @@ int MC_modelcheck_safety(void)
           return SIMGRID_MC_EXIT_NON_TERMINATION;
       }
 
-      if ((visited_state = is_visited_state(next_state)) == NULL) {
+      if ((visited_state = simgrid::mc::is_visited_state(next_state)) == nullptr) {
 
         /* Get an enabled process and insert it in the interleave set of the next state */
-        smx_process_t process = NULL;
-        MC_EACH_SIMIX_PROCESS(process,
-          if (MC_process_is_enabled(process)) {
-            MC_state_interleave_process(next_state, process);
-            if (mc_reduce_kind != e_mc_reduce_none)
+        for (auto& p : mc_model_checker->process().simix_processes())
+          if (simgrid::mc::process_is_enabled(&p.copy)) {
+            MC_state_interleave_process(next_state, &p.copy);
+            if (simgrid::mc::reduction_mode != simgrid::mc::ReductionMode::none)
               break;
           }
-        );
-
-        if (dot_output != NULL)
-          fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num, next_state->num, req_str);
 
-      } else {
+        if (dot_output != nullptr)
+          std::fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num, next_state->num, req_str);
 
-        if (dot_output != NULL)
-          fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num, visited_state->other_num == -1 ? visited_state->num : visited_state->other_num, req_str);
+      } else if (dot_output != nullptr)
+        std::fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num, visited_state->other_num == -1 ? visited_state->num : visited_state->other_num, req_str);
 
-      }
 
       xbt_fifo_unshift(mc_stack, next_state);
 
-      if (dot_output != NULL)
+      if (dot_output != nullptr)
         xbt_free(req_str);
 
       /* Let's loop again */
@@ -161,31 +164,28 @@ int MC_modelcheck_safety(void)
       /* The interleave set is empty or the maximum depth is reached, let's back-track */
     } else {
 
-      if ((xbt_fifo_size(mc_stack) > _sg_mc_max_depth) || user_max_depth_reached || visited_state != NULL) {
+      if ((xbt_fifo_size(mc_stack) > _sg_mc_max_depth) || user_max_depth_reached || visited_state != nullptr) {
 
-        if (user_max_depth_reached && visited_state == NULL)
+        if (user_max_depth_reached && visited_state == nullptr)
           XBT_DEBUG("User max depth reached !");
-        else if (visited_state == NULL)
+        else if (visited_state == nullptr)
           XBT_WARN("/!\\ Max depth reached ! /!\\ ");
         else
           XBT_DEBUG("State already visited (equal to state %d), exploration stopped on this path.", visited_state->other_num == -1 ? visited_state->num : visited_state->other_num);
 
-      } else {
-
+      } else
         XBT_DEBUG("There are no more processes to interleave. (depth %d)", xbt_fifo_size(mc_stack) + 1);
 
-      }
-
       /* Trash the current state, no longer needed */
       xbt_fifo_shift(mc_stack);
       XBT_DEBUG("Delete state %d at depth %d", state->num, xbt_fifo_size(mc_stack) + 1);
       MC_state_delete(state, !state->in_visited_states ? 1 : 0);
 
-      visited_state = NULL;
+      visited_state = nullptr;
 
       /* Check for deadlocks */
       if (MC_deadlock_check()) {
-        MC_show_deadlock(NULL);
+        MC_show_deadlock(nullptr);
         return SIMGRID_MC_EXIT_DEADLOCK;
       }
 
@@ -197,22 +197,22 @@ int MC_modelcheck_safety(void)
          state that executed that previous request. */
 
       while ((state = (mc_state_t) xbt_fifo_shift(mc_stack))) {
-        if (mc_reduce_kind == e_mc_reduce_dpor) {
+        if (simgrid::mc::reduction_mode == simgrid::mc::ReductionMode::dpor) {
           req = MC_state_get_internal_request(state);
           if (req->call == SIMCALL_MUTEX_LOCK || req->call == SIMCALL_MUTEX_TRYLOCK)
             xbt_die("Mutex is currently not supported with DPOR, "
               "use --cfg=model-check/reduction:none");
           const smx_process_t issuer = MC_smx_simcall_get_issuer(req);
           xbt_fifo_foreach(mc_stack, item, prev_state, mc_state_t) {
-            if (MC_request_depend(req, MC_state_get_internal_request(prev_state))) {
+            if (simgrid::mc::request_depend(req, MC_state_get_internal_request(prev_state))) {
               if (XBT_LOG_ISENABLED(mc_safety, xbt_log_priority_debug)) {
                 XBT_DEBUG("Dependent Transitions:");
                 smx_simcall_t prev_req = MC_state_get_executed_request(prev_state, &value);
-                req_str = MC_request_to_string(prev_req, value, MC_REQUEST_INTERNAL);
+                req_str = simgrid::mc::request_to_string(prev_req, value, simgrid::mc::RequestType::internal);
                 XBT_DEBUG("%s (state=%d)", req_str, prev_state->num);
                 xbt_free(req_str);
                 prev_req = MC_state_get_executed_request(state, &value);
-                req_str = MC_request_to_string(prev_req, value, MC_REQUEST_EXECUTED);
+                req_str = simgrid::mc::request_to_string(prev_req, value, simgrid::mc::RequestType::executed);
                 XBT_DEBUG("%s (state=%d)", req_str, state->num);
                 xbt_free(req_str);
               }
@@ -262,12 +262,12 @@ int MC_modelcheck_safety(void)
   return SIMGRID_MC_EXIT_SUCCESS;
 }
 
-static void MC_modelcheck_safety_init(void)
+static void modelcheck_safety_init(void)
 {
   if(_sg_mc_termination)
-    mc_reduce_kind = e_mc_reduce_none;
-  else if (mc_reduce_kind == e_mc_reduce_unset)
-    mc_reduce_kind = e_mc_reduce_dpor;
+    simgrid::mc::reduction_mode = simgrid::mc::ReductionMode::none;
+  else if (simgrid::mc::reduction_mode == simgrid::mc::ReductionMode::unset)
+    simgrid::mc::reduction_mode = simgrid::mc::ReductionMode::dpor;
   _sg_mc_safety = 1;
   if (_sg_mc_termination)
     XBT_INFO("Check non progressive cycles");
@@ -282,9 +282,12 @@ static void MC_modelcheck_safety_init(void)
   /* Create exploration stack */
   mc_stack = xbt_fifo_new();
 
-  MC_pre_modelcheck_safety();
+  pre_modelcheck_safety();
 
   /* Save the initial state */
   initial_global_state = xbt_new0(s_mc_global_t, 1);
   initial_global_state->snapshot = simgrid::mc::take_snapshot(0);
 }
+
+}
+}