Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move 2 functions from mc::ModelChecker to mc::RemoteApp
[simgrid.git] / src / mc / api / RemoteApp.cpp
index ad68366e9af54098e2380b89a103fa66c2e93dbe..842961a11ccc4e573caf939b857634ca45f7886f 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 <algorithm>
 #include <array>
@@ -128,7 +128,7 @@ RemoteApp::RemoteApp(const std::vector<char*>& args)
 
   xbt_assert(mc_model_checker == nullptr, "Did you manage to start the MC twice in this process?");
 
-  auto process   = std::make_unique<simgrid::mc::RemoteProcess>(pid);
+  auto process   = std::make_unique<simgrid::mc::RemoteProcessMemory>(pid);
   model_checker_ = std::make_unique<simgrid::mc::ModelChecker>(std::move(process), sockets[1]);
 
   mc_model_checker = model_checker_.get();
@@ -136,14 +136,14 @@ RemoteApp::RemoteApp(const std::vector<char*>& args)
 
   /* Take the initial snapshot */
   model_checker_->wait_for_requests();
-  initial_snapshot_ = std::make_shared<simgrid::mc::Snapshot>(0);
+  initial_snapshot_ = std::make_shared<simgrid::mc::Snapshot>(0, page_store_);
 }
 
 RemoteApp::~RemoteApp()
 {
   initial_snapshot_ = nullptr;
   if (model_checker_) {
-    model_checker_->shutdown();
+    shutdown();
     model_checker_   = nullptr;
     mc_model_checker = nullptr;
   }
@@ -151,12 +151,24 @@ RemoteApp::~RemoteApp()
 
 void RemoteApp::restore_initial_state() const
 {
-  this->initial_snapshot_->restore(&model_checker_->get_remote_process());
+  this->initial_snapshot_->restore(&model_checker_->get_remote_process_memory());
 }
 
 unsigned long RemoteApp::get_maxpid() const
 {
-  return model_checker_->get_remote_process().get_maxpid();
+  // note: we could maybe cache it and count the actor creation on checker side too.
+  // But counting correctly accross state checkpoint/restore would be annoying.
+
+  model_checker_->channel().send(MessageType::ACTORS_MAXPID);
+  s_mc_message_int_t answer;
+  ssize_t answer_size = model_checker_->channel().receive(answer);
+  xbt_assert(answer_size != -1, "Could not receive message");
+  xbt_assert(answer_size == sizeof answer, "Broken message (size=%zd; expected %zu)", answer_size, sizeof answer);
+  xbt_assert(answer.type == MessageType::ACTORS_MAXPID_REPLY,
+             "Received unexpected message %s (%i); expected MessageType::ACTORS_MAXPID_REPLY (%i)",
+             to_c_str(answer.type), (int)answer.type, (int)MessageType::ACTORS_MAXPID_REPLY);
+
+  return answer.value;
 }
 
 void RemoteApp::get_actors_status(std::map<aid_t, ActorState>& whereto) const
@@ -171,13 +183,12 @@ void RemoteApp::get_actors_status(std::map<aid_t, ActorState>& whereto) const
   model_checker_->channel().send(MessageType::ACTORS_STATUS);
 
   s_mc_message_actors_status_answer_t answer;
-  ssize_t received = model_checker_->channel().receive(answer);
-  xbt_assert(received != -1, "Could not receive message");
-  xbt_assert(received == sizeof(answer) && answer.type == MessageType::ACTORS_STATUS_REPLY,
-             "Received unexpected message %s (%i, size=%i) "
-             "expected MessageType::ACTORS_STATUS_REPLY (%i, size=%i)",
-             to_c_str(answer.type), (int)answer.type, (int)received, (int)MessageType::ACTORS_STATUS_REPLY,
-             (int)sizeof(answer));
+  ssize_t answer_size = model_checker_->channel().receive(answer);
+  xbt_assert(answer_size != -1, "Could not receive message");
+  xbt_assert(answer_size == sizeof answer, "Broken message (size=%zd; expected %zu)", answer_size, sizeof answer);
+  xbt_assert(answer.type == MessageType::ACTORS_STATUS_REPLY,
+             "Received unexpected message %s (%i); expected MessageType::ACTORS_STATUS_REPLY (%i)",
+             to_c_str(answer.type), (int)answer.type, (int)MessageType::ACTORS_STATUS_REPLY);
 
   // Message sanity checks
   xbt_assert(answer.count >= 0, "Received an ACTOR_STATUS_REPLY message with an actor count of '%d' < 0", answer.count);
@@ -206,17 +217,16 @@ void RemoteApp::get_actors_status(std::map<aid_t, ActorState>& whereto) const
   std::vector<s_mc_message_simcall_probe_one_t> probes(answer.transition_count);
   if (answer.transition_count > 0) {
     for (auto& probe : probes) {
-      size_t size      = sizeof(s_mc_message_simcall_probe_one_t);
-      ssize_t received = model_checker_->channel().receive(&probe, size);
+      ssize_t received = model_checker_->channel().receive(probe);
       xbt_assert(received >= 0, "Could not receive response to ACTORS_PROBE message (%s)", strerror(errno));
-      xbt_assert(static_cast<size_t>(received) == size,
+      xbt_assert(static_cast<size_t>(received) == sizeof probe,
                  "Could not receive response to ACTORS_PROBE message (%zd bytes received != %zu bytes expected",
-                 received, size);
+                 received, sizeof probe);
     }
   }
 
   whereto.clear();
-  auto probes_iter = std::move_iterator(probes.begin());
+  std::move_iterator probes_iter(probes.begin());
 
   for (const auto& actor : status) {
     xbt_assert(actor.n_transitions == 0 || actor.n_transitions == actor.max_considered,
@@ -225,14 +235,13 @@ void RemoteApp::get_actors_status(std::map<aid_t, ActorState>& whereto) const
                "(currently %d), but only %d transition(s) was/were said to be encoded",
                actor.max_considered, actor.n_transitions);
 
-    auto actor_transitions = std::vector<std::unique_ptr<Transition>>(actor.n_transitions);
+    std::vector<std::shared_ptr<Transition>> actor_transitions;
     for (int times_considered = 0; times_considered < actor.n_transitions; times_considered++, probes_iter++) {
       std::stringstream stream((*probes_iter).buffer.data());
-      auto transition = std::unique_ptr<Transition>(deserialize_transition(actor.aid, times_considered, stream));
-      actor_transitions[times_considered] = std::move(transition);
+      actor_transitions.emplace_back(deserialize_transition(actor.aid, times_considered, stream));
     }
 
-    XBT_DEBUG("Received %d transitions for actor %ld", actor.n_transitions, actor.aid);
+    XBT_DEBUG("Received %zu transitions for actor %ld", actor_transitions.size(), actor.aid);
     whereto.try_emplace(actor.aid, actor.aid, actor.enabled, actor.max_considered, std::move(actor_transitions));
   }
 }
@@ -241,13 +250,12 @@ void RemoteApp::check_deadlock() const
 {
   xbt_assert(model_checker_->channel().send(MessageType::DEADLOCK_CHECK) == 0, "Could not check deadlock state");
   s_mc_message_int_t message;
-  ssize_t s = model_checker_->channel().receive(message);
-  xbt_assert(s != -1, "Could not receive message");
-  xbt_assert(s == sizeof(message) && message.type == MessageType::DEADLOCK_CHECK_REPLY,
-             "Received unexpected message %s (%i, size=%i) "
-             "expected MessageType::DEADLOCK_CHECK_REPLY (%i, size=%i)",
-             to_c_str(message.type), (int)message.type, (int)s, (int)MessageType::DEADLOCK_CHECK_REPLY,
-             (int)sizeof(message));
+  ssize_t received = model_checker_->channel().receive(message);
+  xbt_assert(received != -1, "Could not receive message");
+  xbt_assert(received == sizeof message, "Broken message (size=%zd; expected %zu)", received, sizeof message);
+  xbt_assert(message.type == MessageType::DEADLOCK_CHECK_REPLY,
+             "Received unexpected message %s (%i); expected MessageType::DEADLOCK_CHECK_REPLY (%i)",
+             to_c_str(message.type), (int)message.type, (int)MessageType::DEADLOCK_CHECK_REPLY);
 
   if (message.value != 0) {
     XBT_CINFO(mc_global, "Counter-example execution trace:");
@@ -260,4 +268,34 @@ void RemoteApp::check_deadlock() const
     throw DeadlockError();
   }
 }
+
+void RemoteApp::shutdown()
+{
+  XBT_DEBUG("Shutting down model-checker");
+
+  RemoteProcessMemory& process = get_remote_process_memory();
+  if (process.running()) {
+    XBT_DEBUG("Killing process");
+    finalize_app(true);
+    kill(process.pid(), SIGKILL);
+    process.terminate();
+  }
+}
+
+void RemoteApp::finalize_app(bool terminate_asap)
+{
+  s_mc_message_int_t m = {};
+  m.type               = MessageType::FINALIZE;
+  m.value              = terminate_asap;
+  xbt_assert(model_checker_->channel().send(m) == 0, "Could not ask the app to finalize on need");
+
+  s_mc_message_t answer;
+  ssize_t s = model_checker_->channel().receive(answer);
+  xbt_assert(s != -1, "Could not receive answer to FINALIZE");
+  xbt_assert(s == sizeof answer, "Broken message (size=%zd; expected %zu)", s, sizeof answer);
+  xbt_assert(answer.type == MessageType::FINALIZE_REPLY,
+             "Received unexpected message %s (%i); expected MessageType::FINALIZE_REPLY (%i)", to_c_str(answer.type),
+             (int)answer.type, (int)MessageType::FINALIZE_REPLY);
+}
+
 } // namespace simgrid::mc