Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
further cleanups to the SafetyChecker
[simgrid.git] / src / mc / checker / SafetyChecker.cpp
index 68e667f..2d766e2 100644 (file)
@@ -34,17 +34,6 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_safety, mc,
 namespace simgrid {
 namespace mc {
 
-static void MC_show_non_termination(void)
-{
-  XBT_INFO("******************************************");
-  XBT_INFO("*** NON-PROGRESSIVE CYCLE DETECTED ***");
-  XBT_INFO("******************************************");
-  XBT_INFO("Counter-example execution trace:");
-  for (auto& s : mc_model_checker->getChecker()->getTextualTrace())
-    XBT_INFO("%s", s.c_str());
-  simgrid::mc::session->logState();
-}
-
 static int snapshot_compare(simgrid::mc::State* state1, simgrid::mc::State* state2)
 {
   simgrid::mc::Snapshot* s1 = state1->system_state.get();
@@ -54,14 +43,21 @@ static int snapshot_compare(simgrid::mc::State* state1, simgrid::mc::State* stat
   return snapshot_compare(num1, s1, num2, s2);
 }
 
-bool SafetyChecker::checkNonTermination(simgrid::mc::State* current_state)
+void SafetyChecker::checkNonTermination(simgrid::mc::State* current_state)
 {
   for (auto state = stack_.rbegin(); state != stack_.rend(); ++state)
     if (snapshot_compare(state->get(), current_state) == 0) {
       XBT_INFO("Non-progressive cycle: state %d -> state %d", (*state)->num, current_state->num);
-      return true;
+      XBT_INFO("******************************************");
+      XBT_INFO("*** NON-PROGRESSIVE CYCLE DETECTED ***");
+      XBT_INFO("******************************************");
+      XBT_INFO("Counter-example execution trace:");
+      for (auto& s : mc_model_checker->getChecker()->getTextualTrace())
+        XBT_INFO("%s", s.c_str());
+      simgrid::mc::session->logState();
+
+      throw simgrid::mc::TerminationError();
     }
-  return false;
 }
 
 RecordTrace SafetyChecker::getRecordTrace() // override
@@ -93,7 +89,7 @@ void SafetyChecker::logState() // override
   XBT_INFO("Executed transitions = %lu", mc_model_checker->executed_transitions);
 }
 
-int SafetyChecker::run()
+void SafetyChecker::run()
 {
   /* This function runs the DFS algorithm the state space.
    * We do so iteratively instead of recursively, dealing with the call stack manually.
@@ -113,11 +109,8 @@ int SafetyChecker::run()
     // Backtrack if we reached the maximum depth
     if (stack_.size() > (std::size_t)_sg_mc_max_depth) {
       XBT_WARN("/!\\ Max depth reached ! /!\\ ");
-      int res = this->backtrack();
-      if (res)
-        return res;
-      else
-        continue;
+      this->backtrack();
+      continue;
     }
 
     // Backtrack if we are revisiting a state we saw previously
@@ -126,23 +119,17 @@ int SafetyChecker::run()
                 visitedState_->other_num == -1 ? visitedState_->num : visitedState_->other_num);
 
       visitedState_ = nullptr;
-      int res       = this->backtrack();
-      if (res)
-        return res;
-      else
-        continue;
+      this->backtrack();
+      continue;
     }
 
+    // Search an enabled transition in the current state; backtrack if the interleave set is empty
     smx_simcall_t req = MC_state_get_request(state);
-    // Backtrack if the interleave set is empty
     if (req == nullptr) {
       XBT_DEBUG("There are no more processes to interleave. (depth %zi)", stack_.size() + 1);
 
-      int res = this->backtrack();
-      if (res)
-        return res;
-      else
-        continue;
+      this->backtrack();
+      continue;
     }
 
     // If there are processes to interleave and the maximum depth has not been
@@ -164,17 +151,15 @@ int SafetyChecker::run()
     std::unique_ptr<simgrid::mc::State> next_state =
         std::unique_ptr<simgrid::mc::State>(new simgrid::mc::State(++expandedStatesCount_));
 
-    if (_sg_mc_termination && this->checkNonTermination(next_state.get())) {
-        MC_show_non_termination();
-        return SIMGRID_MC_EXIT_NON_TERMINATION;
-    }
+    if (_sg_mc_termination)
+      this->checkNonTermination(next_state.get());
 
     /* Check whether we already explored next_state in the past (but only if interested in state-equality reduction) */
     if (_sg_mc_visited == true)
       visitedState_ = visitedStates_.addVisitedState(expandedStatesCount_, next_state.get(), true);
 
     /* If this is a new state (or if we don't care about state-equality reduction) */
-    if (_sg_mc_visited == 0 || visitedState_ == nullptr) {
+    if (visitedState_ == nullptr) {
 
       /* Get an enabled process and insert it in the interleave set of the next state */
       for (auto& actor : mc_model_checker->process().actors())
@@ -198,17 +183,16 @@ int SafetyChecker::run()
 
   XBT_INFO("No property violation found.");
   simgrid::mc::session->logState();
-  return SIMGRID_MC_EXIT_SUCCESS;
 }
 
-int SafetyChecker::backtrack()
+void SafetyChecker::backtrack()
 {
   stack_.pop_back();
 
   /* Check for deadlocks */
   if (mc_model_checker->checkDeadlock()) {
     MC_show_deadlock();
-    return SIMGRID_MC_EXIT_DEADLOCK;
+    throw simgrid::mc::DeadlockError();
   }
 
   /* Traverse the stack backwards until a state with a non empty interleave
@@ -286,7 +270,6 @@ int SafetyChecker::backtrack()
         state->num, stack_.size() + 1);
     }
   }
-  return SIMGRID_MC_EXIT_SUCCESS;
 }
 
 void SafetyChecker::restoreState()