Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
log_state has nothing to do in RemoteApp, it belongs to the exploration
[simgrid.git] / src / mc / api / RemoteApp.cpp
1 /* Copyright (c) 2015-2022. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/mc/api/RemoteApp.hpp"
7 #include "src/internal_config.h" // HAVE_SMPI
8 #include "src/mc/explo/Exploration.hpp"
9 #include "src/mc/mc_config.hpp"
10 #if HAVE_SMPI
11 #include "smpi/smpi.h"
12 #include "src/smpi/include/private.hpp"
13 #endif
14 #include "src/mc/api/State.hpp"
15 #include "src/mc/mc_exit.hpp"
16 #include "src/mc/mc_private.hpp"
17 #include "xbt/log.h"
18 #include "xbt/system_error.hpp"
19
20 #include "signal.h"
21 #include <array>
22 #include <memory>
23 #include <string>
24
25 #include <fcntl.h>
26 #ifdef __linux__
27 #include <sys/prctl.h>
28 #endif
29
30 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_Session, mc, "Model-checker session");
31 XBT_LOG_EXTERNAL_CATEGORY(mc_global);
32
33 namespace simgrid::mc {
34
35 template <class Code> void run_child_process(int socket, Code code)
36 {
37   /* On startup, simix_global_init() calls simgrid::mc::Client::initialize(), which checks whether the MC_ENV_SOCKET_FD
38    * env variable is set. If so, MC mode is assumed, and the client is setup from its side
39    */
40
41 #ifdef __linux__
42   // Make sure we do not outlive our parent
43   sigset_t mask;
44   sigemptyset(&mask);
45   xbt_assert(sigprocmask(SIG_SETMASK, &mask, nullptr) >= 0, "Could not unblock signals");
46   xbt_assert(prctl(PR_SET_PDEATHSIG, SIGHUP) == 0, "Could not PR_SET_PDEATHSIG");
47 #endif
48
49   // Remove CLOEXEC to pass the socket to the application
50   int fdflags = fcntl(socket, F_GETFD, 0);
51   xbt_assert(fdflags != -1 && fcntl(socket, F_SETFD, fdflags & ~FD_CLOEXEC) != -1,
52              "Could not remove CLOEXEC for socket");
53
54   setenv(MC_ENV_SOCKET_FD, std::to_string(socket).c_str(), 1);
55
56   code();
57 }
58
59 RemoteApp::RemoteApp(const std::function<void()>& code)
60 {
61 #if HAVE_SMPI
62   smpi_init_options(); // only performed once
63   xbt_assert(smpi_cfg_privatization() != SmpiPrivStrategies::MMAP,
64              "Please use the dlopen privatization schema when model-checking SMPI code");
65 #endif
66
67   // Create an AF_LOCAL socketpair used for exchanging messages
68   // between the model-checker process (ourselves) and the model-checked
69   // process:
70   int sockets[2];
71   xbt_assert(socketpair(AF_LOCAL, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sockets) != -1, "Could not create socketpair");
72
73   pid_t pid = fork();
74   xbt_assert(pid >= 0, "Could not fork model-checked process");
75
76   if (pid == 0) { // Child
77     ::close(sockets[1]);
78     run_child_process(sockets[0], code);
79     DIE_IMPOSSIBLE;
80   }
81
82   // Parent (model-checker):
83   ::close(sockets[0]);
84
85   xbt_assert(mc_model_checker == nullptr, "Did you manage to start the MC twice in this process?");
86
87   auto process   = std::make_unique<simgrid::mc::RemoteProcess>(pid);
88   model_checker_ = std::make_unique<simgrid::mc::ModelChecker>(std::move(process), sockets[1]);
89
90   mc_model_checker = model_checker_.get();
91   model_checker_->start();
92
93   /* Take the initial snapshot */
94   model_checker_->wait_for_requests();
95   initial_snapshot_ = std::make_shared<simgrid::mc::Snapshot>(0);
96 }
97
98 RemoteApp::~RemoteApp()
99 {
100   initial_snapshot_ = nullptr;
101   if (model_checker_) {
102     model_checker_->shutdown();
103     model_checker_   = nullptr;
104     mc_model_checker = nullptr;
105   }
106 }
107
108 void RemoteApp::restore_initial_state() const
109 {
110   this->initial_snapshot_->restore(&model_checker_->get_remote_process());
111 }
112
113 unsigned long RemoteApp::get_maxpid() const
114 {
115   return model_checker_->get_remote_process().get_maxpid();
116 }
117
118 void RemoteApp::get_actors_status(std::map<aid_t, ActorState>& whereto)
119 {
120   s_mc_message_t msg;
121   memset(&msg, 0, sizeof msg);
122   msg.type = simgrid::mc::MessageType::ACTORS_STATUS;
123   model_checker_->channel().send(msg);
124
125   s_mc_message_actors_status_answer_t answer;
126   ssize_t received = model_checker_->channel().receive(answer);
127   xbt_assert(received != -1, "Could not receive message");
128   xbt_assert(received == sizeof(answer) && answer.type == MessageType::ACTORS_STATUS_REPLY,
129              "Received unexpected message %s (%i, size=%i) "
130              "expected MessageType::ACTORS_STATUS_REPLY (%i, size=%i)",
131              to_c_str(answer.type), (int)answer.type, (int)received, (int)MessageType::ACTORS_STATUS_REPLY,
132              (int)sizeof(answer));
133
134   s_mc_message_actors_status_one_t status[answer.count];
135   received = model_checker_->channel().receive(&status, sizeof(status));
136   xbt_assert(static_cast<size_t>(received) == sizeof(status));
137
138   whereto.clear();
139   for (auto const& actor : status)
140     whereto.insert(std::make_pair(actor.aid, ActorState(actor.aid, actor.enabled, actor.max_considered)));
141 }
142
143 void RemoteApp::check_deadlock() const
144 {
145   xbt_assert(model_checker_->channel().send(MessageType::DEADLOCK_CHECK) == 0, "Could not check deadlock state");
146   s_mc_message_int_t message;
147   ssize_t s = model_checker_->channel().receive(message);
148   xbt_assert(s != -1, "Could not receive message");
149   xbt_assert(s == sizeof(message) && message.type == MessageType::DEADLOCK_CHECK_REPLY,
150              "Received unexpected message %s (%i, size=%i) "
151              "expected MessageType::DEADLOCK_CHECK_REPLY (%i, size=%i)",
152              to_c_str(message.type), (int)message.type, (int)s, (int)MessageType::DEADLOCK_CHECK_REPLY,
153              (int)sizeof(message));
154
155   if (message.value != 0) {
156     XBT_CINFO(mc_global, "**************************");
157     XBT_CINFO(mc_global, "*** DEADLOCK DETECTED ***");
158     XBT_CINFO(mc_global, "**************************");
159     XBT_CINFO(mc_global, "Counter-example execution trace:");
160     for (auto const& frame : model_checker_->get_exploration()->get_textual_trace())
161       XBT_CINFO(mc_global, "  %s", frame.c_str());
162     XBT_CINFO(mc_global, "Path = %s", model_checker_->get_exploration()->get_record_trace().to_string().c_str());
163     model_checker_->get_exploration()->log_state();
164     throw DeadlockError();
165   }
166 }
167 } // namespace simgrid::mc