Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Get rid of the ugly "value" out parameter in MC_state_get_request()
[simgrid.git] / src / mc / SafetyChecker.cpp
index 0f20188..8055c65 100644 (file)
@@ -7,7 +7,9 @@
 #include <cassert>
 #include <cstdio>
 
-#include <list>
+#include <memory>
+#include <string>
+#include <vector>
 
 #include <xbt/log.h>
 #include <xbt/sysdep.h>
@@ -65,13 +67,8 @@ bool SafetyChecker::checkNonTermination(simgrid::mc::State* current_state)
 RecordTrace SafetyChecker::getRecordTrace() // override
 {
   RecordTrace res;
-  for (auto const& state : stack_) {
-    int value = 0;
-    smx_simcall_t saved_req = MC_state_get_executed_request(state.get(), &value);
-    const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
-    const int pid = issuer->pid;
-    res.push_back(RecordTraceElement(pid, value));
-  }
+  for (auto const& state : stack_)
+    res.push_back(state->getRecordElement());
   return res;
 }
 
@@ -79,14 +76,11 @@ std::vector<std::string> SafetyChecker::getTextualTrace() // override
 {
   std::vector<std::string> trace;
   for (auto const& state : stack_) {
-    int value;
-    smx_simcall_t req = MC_state_get_executed_request(state.get(), &value);
-    if (req) {
-      char* req_str = simgrid::mc::request_to_string(
-        req, value, simgrid::mc::RequestType::executed);
-      trace.push_back(req_str);
-      xbt_free(req_str);
-    }
+    int value = state->req_num;
+    smx_simcall_t req = &state->executed_req;
+    if (req)
+      trace.push_back(simgrid::mc::request_to_string(
+        req, value, simgrid::mc::RequestType::executed));
   }
   return trace;
 }
@@ -95,8 +89,6 @@ int SafetyChecker::run()
 {
   this->init();
 
-  int value;
-
   while (!stack_.empty()) {
 
     /* Get current state */
@@ -114,7 +106,7 @@ int SafetyChecker::run()
     // let's back-track.
     smx_simcall_t req = nullptr;
     if (stack_.size() > (std::size_t) _sg_mc_max_depth
-        || (req = MC_state_get_request(state, &value)) == nullptr
+        || (req = MC_state_get_request(state)) == nullptr
         || visitedState_ != nullptr) {
       int res = this->backtrack();
       if (res)
@@ -123,27 +115,25 @@ int SafetyChecker::run()
         continue;
     }
 
+    int req_num = state->req_num;
+
     // If there are processes to interleave and the maximum depth has not been
     // reached then perform one step of the exploration algorithm.
+    XBT_DEBUG("Execute: %s",
+      simgrid::mc::request_to_string(
+        req, req_num, simgrid::mc::RequestType::simix).c_str());
 
-    if (XBT_LOG_ISENABLED(mc_safety, xbt_log_priority_debug)) {
-      char* req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
-      XBT_DEBUG("Execute: %s", req_str);
-      xbt_free(req_str);
-    }
-
-    char* req_str = nullptr;
+    std::string req_str;
     if (dot_output != nullptr)
-      req_str = simgrid::mc::request_get_dot_output(req, value);
+      req_str = simgrid::mc::request_get_dot_output(req, req_num);
 
-    MC_state_set_executed_request(state, req, value);
     mc_stats->executed_transitions++;
 
     // TODO, bundle both operations in a single message
-    //   MC_execute_transition(req, value)
+    //   MC_execute_transition(req, req_num)
 
     /* Answer the request */
-    simgrid::mc::handle_simcall(req, value);
+    simgrid::mc::handle_simcall(req, req_num);
     mc_model_checker->wait_for_requests();
 
     /* Create the new expanded state */
@@ -161,21 +151,21 @@ int SafetyChecker::run()
       /* Get an enabled process and insert it in the interleave set of the next state */
       for (auto& p : mc_model_checker->process().simix_processes())
         if (simgrid::mc::process_is_enabled(&p.copy)) {
-          MC_state_interleave_process(next_state.get(), &p.copy);
+          next_state->interleave(&p.copy);
           if (reductionMode_ != simgrid::mc::ReductionMode::none)
             break;
         }
 
       if (dot_output != nullptr)
-        std::fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num, next_state->num, req_str);
+        std::fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n",
+          state->num, next_state->num, req_str.c_str());
 
     } else if (dot_output != nullptr)
-      std::fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", state->num, visitedState_->other_num == -1 ? visitedState_->num : visitedState_->other_num, req_str);
+      std::fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n",
+        state->num,
+        visitedState_->other_num == -1 ? visitedState_->num : visitedState_->other_num, req_str.c_str());
 
     stack_.push_back(std::move(next_state));
-
-    if (dot_output != nullptr)
-      xbt_free(req_str);
   }
 
   XBT_INFO("No property violation found.");
@@ -219,7 +209,7 @@ int SafetyChecker::backtrack()
     std::unique_ptr<simgrid::mc::State> state = std::move(stack_.back());
     stack_.pop_back();
     if (reductionMode_ == simgrid::mc::ReductionMode::dpor) {
-      smx_simcall_t req = MC_state_get_internal_request(state.get());
+      smx_simcall_t req = &state->internal_req;
       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");
@@ -227,38 +217,41 @@ int SafetyChecker::backtrack()
       for (auto i = stack_.rbegin(); i != stack_.rend(); ++i) {
         simgrid::mc::State* prev_state = i->get();
         if (reductionMode_ != simgrid::mc::ReductionMode::none
-            && simgrid::mc::request_depend(req, MC_state_get_internal_request(prev_state))) {
+            && simgrid::mc::request_depend(req, &prev_state->internal_req)) {
           if (XBT_LOG_ISENABLED(mc_safety, xbt_log_priority_debug)) {
             XBT_DEBUG("Dependent Transitions:");
-            int value;
-            smx_simcall_t prev_req = MC_state_get_executed_request(prev_state, &value);
-            char* 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.get(), &value);
-            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);
+            int value = prev_state->req_num;
+            smx_simcall_t prev_req = &prev_state->executed_req;
+            XBT_DEBUG("%s (state=%d)",
+              simgrid::mc::request_to_string(
+                prev_req, value, simgrid::mc::RequestType::internal).c_str(),
+              prev_state->num);
+            value = state->req_num;
+            prev_req = &state->executed_req;
+            XBT_DEBUG("%s (state=%d)",
+              simgrid::mc::request_to_string(
+                prev_req, value, simgrid::mc::RequestType::executed).c_str(),
+              state->num);
           }
 
-          if (!prev_state->processStates[issuer->pid].done())
-            MC_state_interleave_process(prev_state, issuer);
+          if (!prev_state->processStates[issuer->pid].isDone())
+            prev_state->interleave(issuer);
           else
             XBT_DEBUG("Process %p is in done set", req->issuer);
 
           break;
 
-        } else if (req->issuer == MC_state_get_internal_request(prev_state)->issuer) {
+        } else if (req->issuer == prev_state->internal_req.issuer) {
 
-          XBT_DEBUG("Simcall %d and %d with same issuer", req->call, MC_state_get_internal_request(prev_state)->call);
+          XBT_DEBUG("Simcall %d and %d with same issuer", req->call, prev_state->internal_req.call);
           break;
 
         } else {
 
-          const smx_process_t previous_issuer = MC_smx_simcall_get_issuer(MC_state_get_internal_request(prev_state));
+          const smx_process_t previous_issuer = MC_smx_simcall_get_issuer(&prev_state->internal_req);
           XBT_DEBUG("Simcall %d, process %lu (state %d) and simcall %d, process %lu (state %d) are independant",
                     req->call, issuer->pid, state->num,
-                    MC_state_get_internal_request(prev_state)->call,
+                    prev_state->internal_req.call,
                     previous_issuer->pid,
                     prev_state->num);
 
@@ -312,7 +305,7 @@ void SafetyChecker::init()
   /* Get an enabled process and insert it in the interleave set of the initial state */
   for (auto& p : mc_model_checker->process().simix_processes())
     if (simgrid::mc::process_is_enabled(&p.copy)) {
-      MC_state_interleave_process(initial_state.get(), &p.copy);
+      initial_state->interleave(&p.copy);
       if (reductionMode_ != simgrid::mc::ReductionMode::none)
         break;
     }