Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement reforks by forking the application, to save the app exec time
[simgrid.git] / src / mc / api / RemoteApp.cpp
1 /* Copyright (c) 2015-2023. 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/mc/explo/Exploration.hpp"
8 #include "src/mc/mc_config.hpp"
9 #include "xbt/asserts.h"
10 #include "src/mc/api/State.hpp"
11 #include "src/mc/mc_config.hpp"
12 #include "src/mc/mc_exit.hpp"
13 #include "src/mc/mc_private.hpp"
14 #include "xbt/log.h"
15 #include "xbt/system_error.hpp"
16 #include <signal.h>
17
18 #include <algorithm>
19 #include <array>
20 #include <limits.h>
21 #include <memory>
22 #include <numeric>
23 #include <string>
24 #include <sys/ptrace.h>
25 #include <sys/un.h>
26 #include <sys/wait.h>
27
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_Session, mc, "Model-checker session");
29 XBT_LOG_EXTERNAL_CATEGORY(mc_global);
30
31 namespace simgrid::mc {
32
33 static char master_socket_name[65] = {};
34 static void cleanup_master_socket()
35 {
36   if (master_socket_name[0] != '\0')
37     unlink(master_socket_name);
38   master_socket_name[0] = '\0';
39 }
40
41 RemoteApp::RemoteApp(const std::vector<char*>& args, bool need_memory_introspection) : app_args_(args)
42 {
43   if (need_memory_introspection) {
44     checker_side_     = std::make_unique<simgrid::mc::CheckerSide>(app_args_, need_memory_introspection);
45     initial_snapshot_ = std::make_shared<simgrid::mc::Snapshot>(0, page_store_, *checker_side_->get_remote_memory());
46   } else {
47     master_socket_ = socket(AF_LOCAL, SOCK_SEQPACKET | SOCK_CLOEXEC, 0);
48     xbt_assert(master_socket_ != -1, "Cannot create the master socket: %s", strerror(errno));
49
50     struct sockaddr_un serv_addr = {};
51     serv_addr.sun_family         = AF_LOCAL;
52     snprintf(serv_addr.sun_path, 64, "/tmp/simgrid-mc-%d", getpid());
53     strcpy(master_socket_name, serv_addr.sun_path);
54     auto addr_size = offsetof(struct sockaddr_un, sun_path) + strlen(serv_addr.sun_path);
55
56     xbt_assert(bind(master_socket_, (struct sockaddr*)&serv_addr, addr_size) >= 0,
57                "Cannot bind the master socket to %s: %s.", serv_addr.sun_path, strerror(errno));
58     atexit(cleanup_master_socket);
59
60     xbt_assert(listen(master_socket_, SOMAXCONN) >= 0, "Cannot listen to the master socket: %s.", strerror(errno));
61
62     application_factory_ = std::make_unique<simgrid::mc::CheckerSide>(app_args_, need_memory_introspection);
63     checker_side_        = application_factory_->clone(master_socket_);
64   }
65 }
66
67 RemoteApp::~RemoteApp()
68 {
69   initial_snapshot_ = nullptr;
70   checker_side_     = nullptr;
71 }
72
73 void RemoteApp::restore_initial_state()
74 {
75   if (initial_snapshot_ == nullptr) { // No memory introspection
76     checker_side_ = application_factory_->clone(master_socket_);
77   } else
78     initial_snapshot_->restore(*checker_side_->get_remote_memory());
79 }
80
81 unsigned long RemoteApp::get_maxpid() const
82 {
83   // note: we could maybe cache it and count the actor creation on checker side too.
84   // But counting correctly accross state checkpoint/restore would be annoying.
85
86   checker_side_->get_channel().send(MessageType::ACTORS_MAXPID);
87   s_mc_message_int_t answer;
88   ssize_t answer_size = checker_side_->get_channel().receive(answer);
89   xbt_assert(answer_size != -1, "Could not receive message");
90   xbt_assert(answer_size == sizeof answer, "Broken message (size=%zd; expected %zu)", answer_size, sizeof answer);
91   xbt_assert(answer.type == MessageType::ACTORS_MAXPID_REPLY,
92              "Received unexpected message %s (%i); expected MessageType::ACTORS_MAXPID_REPLY (%i)",
93              to_c_str(answer.type), (int)answer.type, (int)MessageType::ACTORS_MAXPID_REPLY);
94
95   return answer.value;
96 }
97
98 void RemoteApp::get_actors_status(std::map<aid_t, ActorState>& whereto) const
99 {
100   // The messaging happens as follows:
101   //
102   // CheckerSide                  AppSide
103   // send ACTORS_STATUS ---->
104   //                    <----- send ACTORS_STATUS_REPLY_COUNT
105   //                    <----- send `N` ACTORS_STATUS_REPLY_TRANSITION (s_mc_message_actors_status_one_t)
106   //                    <----- send `M` ACTORS_STATUS_REPLY_SIMCALL (s_mc_message_simcall_probe_one_t)
107   checker_side_->get_channel().send(MessageType::ACTORS_STATUS);
108
109   s_mc_message_actors_status_answer_t answer;
110   ssize_t answer_size = checker_side_->get_channel().receive(answer);
111   xbt_assert(answer_size != -1, "Could not receive message");
112   xbt_assert(answer_size == sizeof answer, "Broken message (size=%zd; expected %zu)", answer_size, sizeof answer);
113   xbt_assert(answer.type == MessageType::ACTORS_STATUS_REPLY_COUNT,
114              "Received unexpected message %s (%i); expected MessageType::ACTORS_STATUS_REPLY_COUNT (%i)",
115              to_c_str(answer.type), (int)answer.type, (int)MessageType::ACTORS_STATUS_REPLY_COUNT);
116
117   // Message sanity checks
118   xbt_assert(answer.count >= 0, "Received an ACTORS_STATUS_REPLY_COUNT message with an actor count of '%d' < 0",
119              answer.count);
120
121   std::vector<s_mc_message_actors_status_one_t> status(answer.count);
122   if (answer.count > 0) {
123     size_t size      = status.size() * sizeof(s_mc_message_actors_status_one_t);
124     ssize_t received = checker_side_->get_channel().receive(status.data(), size);
125     xbt_assert(static_cast<size_t>(received) == size);
126   }
127
128   whereto.clear();
129
130   for (const auto& actor : status) {
131     std::vector<std::shared_ptr<Transition>> actor_transitions;
132     int n_transitions = actor.enabled ? actor.max_considered : 0;
133     for (int times_considered = 0; times_considered < n_transitions; times_considered++) {
134       s_mc_message_simcall_probe_one_t probe;
135       ssize_t received = checker_side_->get_channel().receive(probe);
136       xbt_assert(received >= 0, "Could not receive response to ACTORS_PROBE message (%s)", strerror(errno));
137       xbt_assert(static_cast<size_t>(received) == sizeof probe,
138                  "Could not receive response to ACTORS_PROBE message (%zd bytes received != %zu bytes expected",
139                  received, sizeof probe);
140
141       std::stringstream stream(probe.buffer.data());
142       actor_transitions.emplace_back(deserialize_transition(actor.aid, times_considered, stream));
143     }
144
145     XBT_DEBUG("Received %zu transitions for actor %ld", actor_transitions.size(), actor.aid);
146     whereto.try_emplace(actor.aid, actor.aid, actor.enabled, actor.max_considered, std::move(actor_transitions));
147   }
148 }
149
150 void RemoteApp::check_deadlock() const
151 {
152   xbt_assert(checker_side_->get_channel().send(MessageType::DEADLOCK_CHECK) == 0, "Could not check deadlock state");
153   s_mc_message_int_t message;
154   ssize_t received = checker_side_->get_channel().receive(message);
155   xbt_assert(received != -1, "Could not receive message");
156   xbt_assert(received == sizeof message, "Broken message (size=%zd; expected %zu)", received, sizeof message);
157   xbt_assert(message.type == MessageType::DEADLOCK_CHECK_REPLY,
158              "Received unexpected message %s (%i); expected MessageType::DEADLOCK_CHECK_REPLY (%i)",
159              to_c_str(message.type), (int)message.type, (int)MessageType::DEADLOCK_CHECK_REPLY);
160
161   if (message.value != 0) {
162     auto* explo = Exploration::get_instance();
163     XBT_CINFO(mc_global, "Counter-example execution trace:");
164     for (auto const& frame : explo->get_textual_trace())
165       XBT_CINFO(mc_global, "  %s", frame.c_str());
166     XBT_INFO("You can debug the problem (and see the whole details) by rerunning out of simgrid-mc with "
167              "--cfg=model-check/replay:'%s'",
168              explo->get_record_trace().to_string().c_str());
169     explo->log_state();
170     throw DeadlockError();
171   }
172 }
173
174 void RemoteApp::wait_for_requests()
175 {
176   checker_side_->wait_for_requests();
177 }
178
179 Transition* RemoteApp::handle_simcall(aid_t aid, int times_considered, bool new_transition)
180 {
181   s_mc_message_simcall_execute_t m = {};
182   m.type                           = MessageType::SIMCALL_EXECUTE;
183   m.aid_                           = aid;
184   m.times_considered_              = times_considered;
185   checker_side_->get_channel().send(m);
186
187   if (auto* memory = get_remote_process_memory(); memory != nullptr)
188     memory->clear_cache();
189   if (checker_side_->running())
190     checker_side_->dispatch_events(); // The app may send messages while processing the transition
191
192   s_mc_message_simcall_execute_answer_t answer;
193   ssize_t s = checker_side_->get_channel().receive(answer);
194   xbt_assert(s != -1, "Could not receive message");
195   xbt_assert(s > 0 && answer.type == MessageType::SIMCALL_EXECUTE_REPLY,
196              "%d Received unexpected message %s (%i); expected MessageType::SIMCALL_EXECUTE_REPLY (%i)", getpid(),
197              to_c_str(answer.type), (int)answer.type, (int)MessageType::SIMCALL_EXECUTE_REPLY);
198   xbt_assert(s == sizeof answer, "Broken message (size=%zd; expected %zu)", s, sizeof answer);
199
200   if (new_transition) {
201     std::stringstream stream(answer.buffer.data());
202     return deserialize_transition(aid, times_considered, stream);
203   } else
204     return nullptr;
205 }
206
207 void RemoteApp::finalize_app(bool terminate_asap)
208 {
209   checker_side_->finalize(terminate_asap);
210 }
211
212 } // namespace simgrid::mc