Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Use std::string in simgrid::mc::request_to_string
authorGabriel Corona <gabriel.corona@loria.fr>
Tue, 5 Apr 2016 12:48:33 +0000 (14:48 +0200)
committerGabriel Corona <gabriel.corona@loria.fr>
Tue, 5 Apr 2016 14:32:44 +0000 (16:32 +0200)
include/xbt/string.hpp
src/mc/CommunicationDeterminismChecker.cpp
src/mc/LivenessChecker.cpp
src/mc/SafetyChecker.cpp
src/mc/mc_global.cpp
src/mc/mc_request.cpp
src/mc/mc_request.h
src/xbt/string.cpp

index 9840855..be542c0 100644 (file)
@@ -9,20 +9,26 @@
 
 #include <simgrid_config.h>
 
+#include <string>
+#include <cstdarg>
+
 #if HAVE_MC
 
 #include <stdexcept>
 #include <cstddef>
 #include <cstdlib>
 #include <cstring>
-#include <string>
 #include <iterator>
 
 #include <xbt/sysdep.h>
 
+#endif
+
 namespace simgrid {
 namespace xbt {
 
+#if HAVE_MC
+
 /** POD structure representation of a string
  */
 struct string_data {
@@ -281,21 +287,16 @@ bool operator>=(std::string const& a, string const& b)
   return b <= a;
 }
 
-}
-}
-
 #else
 
-#include <string>
+typedef std::string string;
 
-namespace simgrid {
-namespace xbt {
+#endif
 
-typedef std::string string;
+std::string string_vprintf(const char *fmt, va_list ap);
+std::string string_printf(const char *fmt, ...);
 
 }
 }
 
 #endif
-
-#endif
index 4c51b1b..15f19f0 100644 (file)
@@ -323,12 +323,9 @@ std::vector<std::string> CommunicationDeterminismChecker::getTextualTrace() // o
   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);
-    }
+    if (req)
+      trace.push_back(simgrid::mc::request_to_string(
+        req, value, simgrid::mc::RequestType::executed));
   }
   return trace;
 }
@@ -387,8 +384,6 @@ bool all_communications_are_finished()
 
 int CommunicationDeterminismChecker::main(void)
 {
-
-  char *req_str = nullptr;
   int value;
   std::unique_ptr<simgrid::mc::VisitedState> visited_state = nullptr;
   smx_simcall_t req = nullptr;
@@ -410,10 +405,11 @@ int CommunicationDeterminismChecker::main(void)
         && (req = MC_state_get_request(state, &value))
         && (visited_state == nullptr)) {
 
-      req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
-      XBT_DEBUG("Execute: %s", req_str);
-      xbt_free(req_str);
+      XBT_DEBUG("Execute: %s",
+        simgrid::mc::request_to_string(
+          req, value, simgrid::mc::RequestType::simix).c_str());
 
+      char* req_str = nullptr;
       if (dot_output != nullptr)
         req_str = simgrid::mc::request_get_dot_output(req, value);
 
index 086d122..5ded308 100644 (file)
@@ -211,12 +211,11 @@ void LivenessChecker::replay()
         req = &issuer->simcall;
 
         /* Debug information */
-        if (XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)) {
-          char* req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
-          XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state.get());
-          xbt_free(req_str);
-        }
-
+        XBT_DEBUG("Replay (depth = %d) : %s (%p)",
+          depth,
+          simgrid::mc::request_to_string(
+            req, value, simgrid::mc::RequestType::simix).c_str(),
+          state.get());
       }
 
       simgrid::mc::handle_simcall(req, value);
@@ -328,11 +327,9 @@ std::vector<std::string> LivenessChecker::getTextualTrace() // override
   for (std::shared_ptr<Pair> const& pair : explorationStack_) {
     int value;
     smx_simcall_t req = MC_state_get_executed_request(pair->graph_state.get(), &value);
-    if (req && req->call != SIMCALL_NONE) {
-      char* req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::executed);
-      trace.push_back(std::string(req_str));
-      xbt_free(req_str);
-    }
+    if (req && req->call != SIMCALL_NONE)
+      trace.push_back(simgrid::mc::request_to_string(
+        req, value, simgrid::mc::RequestType::executed));
   }
   return trace;
 }
@@ -393,9 +390,9 @@ int LivenessChecker::main(void)
       fflush(dot_output);
     }
 
-    char* req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
-    XBT_DEBUG("Execute: %s", req_str);
-    xbt_free(req_str);
+    XBT_DEBUG("Execute: %s",
+      simgrid::mc::request_to_string(
+        req, value, simgrid::mc::RequestType::simix).c_str());
 
     /* Set request as executed */
     MC_state_set_executed_request(current_pair->graph_state.get(), req, value);
index 0f20188..4b8c59f 100644 (file)
@@ -81,12 +81,9 @@ std::vector<std::string> SafetyChecker::getTextualTrace() // override
   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);
-    }
+    if (req)
+      trace.push_back(simgrid::mc::request_to_string(
+        req, value, simgrid::mc::RequestType::executed));
   }
   return trace;
 }
@@ -125,12 +122,9 @@ int SafetyChecker::run()
 
     // If there are processes to interleave and the maximum depth has not been
     // reached then perform one step of the exploration algorithm.
-
-    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);
-    }
+    XBT_DEBUG("Execute: %s",
+      simgrid::mc::request_to_string(
+        req, value, simgrid::mc::RequestType::simix).c_str());
 
     char* req_str = nullptr;
     if (dot_output != nullptr)
@@ -232,13 +226,15 @@ int SafetyChecker::backtrack()
             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);
+            XBT_DEBUG("%s (state=%d)",
+              simgrid::mc::request_to_string(
+                prev_req, value, simgrid::mc::RequestType::internal).c_str(),
+              prev_state->num);
             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);
+            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())
index ad03c3f..079c2aa 100644 (file)
@@ -167,11 +167,10 @@ void replay(std::list<std::unique_ptr<simgrid::mc::State>> const& stack)
       smx_simcall_t req = &issuer->simcall;
 
       /* Debug information */
-      if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
-        char* req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
-        XBT_DEBUG("Replay: %s (%p)", req_str, state.get());
-        xbt_free(req_str);
-      }
+      XBT_DEBUG("Replay: %s (%p)",
+        simgrid::mc::request_to_string(
+          req, value, simgrid::mc::RequestType::simix).c_str(),
+        state.get());
 
       /* TODO : handle test and testany simcalls */
       e_mc_call_type_t call = MC_CALL_TYPE_NONE;
index 1404c21..556e335 100644 (file)
@@ -200,7 +200,7 @@ static char *buff_size_to_string(size_t buff_size)
 }
 
 
-char *simgrid::mc::request_to_string(smx_simcall_t req, int value, simgrid::mc::RequestType request_type)
+std::string simgrid::mc::request_to_string(smx_simcall_t req, int value, simgrid::mc::RequestType request_type)
 {
   bool use_remote_comm = true;
   switch(request_type) {
@@ -401,16 +401,14 @@ char *simgrid::mc::request_to_string(smx_simcall_t req, int value, simgrid::mc::
     THROW_UNIMPLEMENTED;
   }
 
-  char* str;
+  std::string str;
   if (args != nullptr)
-    str =
-        bprintf("[(%lu)%s (%s)] %s(%s)", issuer->pid,
+    str = simgrid::xbt::string_printf("[(%lu)%s (%s)] %s(%s)", issuer->pid,
                 MC_smx_process_get_host_name(issuer),
                 MC_smx_process_get_name(issuer),
                 type, args);
   else
-    str =
-        bprintf("[(%lu)%s (%s)] %s ", issuer->pid,
+    str = simgrid::xbt::string_printf("[(%lu)%s (%s)] %s ", issuer->pid,
                 MC_smx_process_get_host_name(issuer),
                 MC_smx_process_get_name(issuer),
                 type);
index 2fa0896..97c153a 100644 (file)
@@ -7,6 +7,8 @@
 #ifndef SIMGRID_MC_REQUEST_H
 #define SIMGRID_MC_REQUEST_H
 
+#include <string>
+
 #include <xbt/base.h>
 
 #include "src/simix/smx_private.h"
@@ -22,7 +24,7 @@ enum class RequestType {
 
 XBT_PRIVATE bool request_depend(smx_simcall_t req1, smx_simcall_t req2);
 
-XBT_PRIVATE char* request_to_string(smx_simcall_t req, int value, simgrid::mc::RequestType type);
+XBT_PRIVATE std::string request_to_string(smx_simcall_t req, int value, simgrid::mc::RequestType type);
 
 XBT_PRIVATE bool request_is_enabled_by_idx(smx_simcall_t req, unsigned int idx);
 
index f378cec..f23e429 100644 (file)
@@ -4,8 +4,13 @@
 /* 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 <cstdarg>
+#include <cstdio>
+
 #include <simgrid_config.h>
+
 #include <xbt/string.hpp>
+#include <xbt/sysdep.h>
 
 namespace simgrid {
 namespace xbt {
@@ -16,5 +21,39 @@ const char string::NUL = '\0';
 
 #endif
 
+std::string string_vprintf(const char *fmt, va_list ap)
+{
+  // Get the size:
+  va_list ap2;
+  va_copy(ap2, ap);
+  int size = std::vsnprintf(nullptr, 0, fmt, ap2);
+  va_end(ap2);
+  if (size < 0)
+    xbt_die("string_vprintf error");
+
+  // Allocate the string and format:
+  std::string res;
+  res.resize(size);
+  if (size != 0 && std::vsnprintf(&res[0], size + 1, fmt, ap) != size)
+    xbt_die("string_vprintf error");
+  return res;
+}
+
+std::string string_printf(const char *fmt, ...)
+{
+  va_list ap;
+  va_start(ap, fmt);
+  std::string res;
+  try {
+    res = string_vprintf(fmt, ap);
+  }
+  catch(...) {
+    va_end(ap);
+    throw;
+  }
+  va_end(ap);
+  return res;
+}
+
 }
 }