Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Avoid to send/receive zero-size messages.
[simgrid.git] / src / mc / api / RemoteApp.cpp
index 6037b6a..6cf19b0 100644 (file)
 #include "smpi/smpi.h"
 #include "src/smpi/include/private.hpp"
 #endif
+#include "signal.h"
 #include "src/mc/api/State.hpp"
+#include "src/mc/mc_config.hpp"
 #include "src/mc/mc_exit.hpp"
 #include "src/mc/mc_private.hpp"
 #include "xbt/log.h"
 #include "xbt/system_error.hpp"
 
-#include "signal.h"
 #include <array>
+#include <boost/tokenizer.hpp>
 #include <memory>
 #include <string>
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_Session, mc, "Model-checker session");
 XBT_LOG_EXTERNAL_CATEGORY(mc_global);
 
+static simgrid::config::Flag<std::string> _sg_mc_setenv{
+    "model-check/setenv", "Extra environment variables to pass to the child process (ex: 'AZE=aze;QWE=qwe').", "",
+    [](std::string_view value) {
+      xbt_assert(value.empty() || value.find('=', 0) != std::string_view::npos,
+                 "The 'model-check/setenv' parameter must be like 'AZE=aze', but it does not contain an equal sign.");
+    }};
+
 namespace simgrid::mc {
 
-template <class Code> void run_child_process(int socket, Code code)
+static void run_child_process(int socket, const std::vector<char*>& args)
 {
   /* On startup, simix_global_init() calls simgrid::mc::Client::initialize(), which checks whether the MC_ENV_SOCKET_FD
    * env variable is set. If so, MC mode is assumed, and the client is setup from its side
@@ -53,10 +62,35 @@ template <class Code> void run_child_process(int socket, Code code)
 
   setenv(MC_ENV_SOCKET_FD, std::to_string(socket).c_str(), 1);
 
-  code();
+  /* Setup the tokenizer that parses the cfg:model-check/setenv parameter */
+  using Tokenizer = boost::tokenizer<boost::char_separator<char>>;
+  boost::char_separator<char> semicol_sep(";");
+  boost::char_separator<char> equal_sep("=");
+  Tokenizer token_vars(_sg_mc_setenv.get(), semicol_sep); /* Iterate over all FOO=foo parts */
+  for (const auto& token : token_vars) {
+    std::vector<std::string> kv;
+    Tokenizer token_kv(token, equal_sep);
+    for (const auto& t : token_kv) /* Iterate over 'FOO' and then 'foo' in that 'FOO=foo' */
+      kv.push_back(t);
+    xbt_assert(kv.size() == 2, "Parse error on 'model-check/setenv' value %s. Does it contain an equal sign?",
+               token.c_str());
+    XBT_INFO("setenv '%s'='%s'", kv[0].c_str(), kv[1].c_str());
+    setenv(kv[0].c_str(), kv[1].c_str(), 1);
+  }
+
+  /* And now, exec the child process */
+  int i = 1;
+  while (args[i] != nullptr && args[i][0] == '-')
+    i++;
+
+  xbt_assert(args[i] != nullptr,
+             "Unable to find a binary to exec on the command line. Did you only pass config flags?");
+
+  execvp(args[i], args.data() + i);
+  xbt_die("The model-checked process failed to exec(%s): %s", args[i], strerror(errno));
 }
 
-RemoteApp::RemoteApp(const std::function<void()>& code)
+RemoteApp::RemoteApp(const std::vector<char*>& args)
 {
 #if HAVE_SMPI
   smpi_init_options(); // only performed once
@@ -75,7 +109,7 @@ RemoteApp::RemoteApp(const std::function<void()>& code)
 
   if (pid == 0) { // Child
     ::close(sockets[1]);
-    run_child_process(sockets[0], code);
+    run_child_process(sockets[0], args);
     DIE_IMPOSSIBLE;
   }
 
@@ -89,42 +123,13 @@ RemoteApp::RemoteApp(const std::function<void()>& code)
 
   mc_model_checker = model_checker_.get();
   model_checker_->start();
-}
 
-RemoteApp::~RemoteApp()
-{
-  this->close();
-}
-
-/** The application must be stopped. */
-void RemoteApp::take_initial_snapshot()
-{
-  xbt_assert(initial_snapshot_ == nullptr);
+  /* Take the initial snapshot */
   model_checker_->wait_for_requests();
   initial_snapshot_ = std::make_shared<simgrid::mc::Snapshot>(0);
 }
 
-void RemoteApp::restore_initial_state() const
-{
-  this->initial_snapshot_->restore(&model_checker_->get_remote_process());
-}
-
-void RemoteApp::log_state() const
-{
-  model_checker_->get_exploration()->log_state();
-
-  if (not _sg_mc_dot_output_file.get().empty()) {
-    fprintf(dot_output, "}\n");
-    fclose(dot_output);
-  }
-  if (getenv("SIMGRID_MC_SYSTEM_STATISTICS")) {
-    int ret = system("free");
-    if (ret != 0)
-      XBT_WARN("Call to system(free) did not return 0, but %d", ret);
-  }
-}
-
-void RemoteApp::close()
+RemoteApp::~RemoteApp()
 {
   initial_snapshot_ = nullptr;
   if (model_checker_) {
@@ -134,7 +139,17 @@ void RemoteApp::close()
   }
 }
 
-void RemoteApp::get_actors_status(std::map<aid_t, ActorState>& whereto)
+void RemoteApp::restore_initial_state() const
+{
+  this->initial_snapshot_->restore(&model_checker_->get_remote_process());
+}
+
+unsigned long RemoteApp::get_maxpid() const
+{
+  return model_checker_->get_remote_process().get_maxpid();
+}
+
+void RemoteApp::get_actors_status(std::map<aid_t, ActorState>& whereto) const
 {
   s_mc_message_t msg;
   memset(&msg, 0, sizeof msg);
@@ -151,12 +166,14 @@ void RemoteApp::get_actors_status(std::map<aid_t, ActorState>& whereto)
              (int)sizeof(answer));
 
   s_mc_message_actors_status_one_t status[answer.count];
-  received = model_checker_->channel().receive(&status, sizeof(status));
-  xbt_assert(static_cast<size_t>(received) == sizeof(status));
+  if (answer.count > 0) {
+    received = model_checker_->channel().receive(&status, sizeof(status));
+    xbt_assert(static_cast<size_t>(received) == sizeof(status));
+  }
 
   whereto.clear();
   for (auto const& actor : status)
-    whereto.insert(std::make_pair(actor.aid, ActorState(actor.aid, actor.enabled, actor.max_considered)));
+    whereto.try_emplace(actor.aid, actor.aid, actor.enabled, actor.max_considered);
 }
 
 void RemoteApp::check_deadlock() const
@@ -179,7 +196,7 @@ void RemoteApp::check_deadlock() const
     for (auto const& frame : model_checker_->get_exploration()->get_textual_trace())
       XBT_CINFO(mc_global, "  %s", frame.c_str());
     XBT_CINFO(mc_global, "Path = %s", model_checker_->get_exploration()->get_record_trace().to_string().c_str());
-    log_state();
+    model_checker_->get_exploration()->log_state();
     throw DeadlockError();
   }
 }