]> AND Public Git Repository - simgrid.git/blob - src/mc/api.cpp
Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Pylint docs/source/tuto_s4u/*.py.
[simgrid.git] / src / mc / api.cpp
1 /* Copyright (c) 2020-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 "api.hpp"
7
8 #include "src/kernel/activity/MailboxImpl.hpp"
9 #include "src/kernel/activity/MutexImpl.hpp"
10 #include "src/kernel/actor/SimcallObserver.hpp"
11 #include "src/mc/Session.hpp"
12 #include "src/mc/checker/Checker.hpp"
13 #include "src/mc/mc_base.hpp"
14 #include "src/mc/mc_exit.hpp"
15 #include "src/mc/mc_pattern.hpp"
16 #include "src/mc/mc_private.hpp"
17 #include "src/mc/remote/RemoteProcess.hpp"
18 #include "src/surf/HostImpl.hpp"
19
20 #include <xbt/asserts.h>
21 #include <xbt/log.h>
22 #include "simgrid/s4u/Host.hpp"
23 #include "xbt/string.hpp"
24 #if HAVE_SMPI
25 #include "src/smpi/include/smpi_request.hpp"
26 #endif
27
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(Api, mc, "Logging specific to MC Facade APIs ");
29 XBT_LOG_EXTERNAL_CATEGORY(mc_global);
30
31 using Simcall = simgrid::simix::Simcall;
32
33 namespace simgrid {
34 namespace mc {
35
36 /** Statically "upcast" a s_smx_actor_t into an ActorInformation
37  *
38  *  This gets 'actorInfo' from '&actorInfo->copy'. It upcasts in the
39  *  sense that we could achieve the same thing by having ActorInformation
40  *  inherit from s_smx_actor_t but we don't really want to do that.
41  */
42 simgrid::mc::ActorInformation* Api::actor_info_cast(smx_actor_t actor) const
43 {
44   simgrid::mc::ActorInformation temp;
45   std::size_t offset = (char*)temp.copy.get_buffer() - (char*)&temp;
46
47   auto* process_info = reinterpret_cast<simgrid::mc::ActorInformation*>((char*)actor - offset);
48   return process_info;
49 }
50
51 xbt::string const& Api::get_actor_host_name(smx_actor_t actor) const
52 {
53   if (mc_model_checker == nullptr)
54     return actor->get_host()->get_name();
55
56   const simgrid::mc::RemoteProcess* process = &mc_model_checker->get_remote_process();
57
58   // Read the simgrid::xbt::string in the MCed process:
59   simgrid::mc::ActorInformation* info = actor_info_cast(actor);
60
61   if (not info->hostname) {
62     Remote<s4u::Host> temp_host = process->read(remote(actor->get_host()));
63     auto remote_string_address  = remote(&xbt::string::to_string_data(temp_host.get_buffer()->get_impl()->get_name()));
64     simgrid::xbt::string_data remote_string = process->read(remote_string_address);
65     std::vector<char> hostname(remote_string.len + 1);
66     // no need to read the terminating null byte, and thus hostname[remote_string.len] is guaranteed to be '\0'
67     process->read_bytes(hostname.data(), remote_string.len, remote(remote_string.data));
68     info->hostname = &mc_model_checker->get_host_name(hostname.data());
69   }
70   return *info->hostname;
71 }
72
73 xbt::string const& Api::get_actor_name(smx_actor_t actor) const
74 {
75   if (mc_model_checker == nullptr)
76     return actor->get_name();
77
78   simgrid::mc::ActorInformation* info = actor_info_cast(actor);
79   if (info->name.empty()) {
80     const simgrid::mc::RemoteProcess* process = &mc_model_checker->get_remote_process();
81
82     simgrid::xbt::string_data string_data = simgrid::xbt::string::to_string_data(actor->name_);
83     info->name = process->read_string(remote(string_data.data), string_data.len);
84   }
85   return info->name;
86 }
87
88 std::string Api::get_actor_string(smx_actor_t actor) const
89 {
90   std::string res;
91   if (actor) {
92     res = "(" + std::to_string(actor->get_pid()) + ")";
93     if (actor->get_host())
94       res += std::string(get_actor_host_name(actor)) + " (" + std::string(get_actor_name(actor)) + ")";
95     else
96       res += get_actor_name(actor);
97   } else
98     res = "(0) ()";
99   return res;
100 }
101
102 std::string Api::get_actor_dot_label(smx_actor_t actor) const
103 {
104   std::string res = "(" + std::to_string(actor->get_pid()) + ")";
105   if (actor->get_host())
106     res += get_actor_host_name(actor);
107   return res;
108 }
109
110 simgrid::mc::Checker* Api::initialize(char** argv, simgrid::mc::CheckerAlgorithm algo) const
111 {
112   auto session = new simgrid::mc::Session([argv] {
113     int i = 1;
114     while (argv[i] != nullptr && argv[i][0] == '-')
115       i++;
116     xbt_assert(argv[i] != nullptr,
117                "Unable to find a binary to exec on the command line. Did you only pass config flags?");
118     execvp(argv[i], argv + i);
119     xbt_die("The model-checked process failed to exec(%s): %s", argv[i], strerror(errno));
120   });
121
122   simgrid::mc::Checker* checker;
123   switch (algo) {
124     case CheckerAlgorithm::CommDeterminism:
125       checker = simgrid::mc::create_communication_determinism_checker(session);
126       break;
127
128     case CheckerAlgorithm::UDPOR:
129       checker = simgrid::mc::create_udpor_checker(session);
130       break;
131
132     case CheckerAlgorithm::Safety:
133       checker = simgrid::mc::create_safety_checker(session);
134       break;
135
136     case CheckerAlgorithm::Liveness:
137       checker = simgrid::mc::create_liveness_checker(session);
138       break;
139
140     default:
141       THROW_IMPOSSIBLE;
142   }
143
144   // FIXME: session and checker are never deleted
145   simgrid::mc::session_singleton = session;
146   mc_model_checker->setChecker(checker);
147   return checker;
148 }
149
150 std::vector<simgrid::mc::ActorInformation>& Api::get_actors() const
151 {
152   return mc_model_checker->get_remote_process().actors();
153 }
154
155 unsigned long Api::get_maxpid() const
156 {
157   return mc_model_checker->get_remote_process().get_maxpid();
158 }
159
160 int Api::get_actors_size() const
161 {
162   return mc_model_checker->get_remote_process().actors().size();
163 }
164
165 RemotePtr<kernel::activity::CommImpl> Api::get_comm_isend_raw_addr(smx_simcall_t request) const
166 {
167   return remote(static_cast<kernel::activity::CommImpl*>(simcall_comm_isend__getraw__result(request)));
168 }
169
170 RemotePtr<kernel::activity::CommImpl> Api::get_comm_waitany_raw_addr(smx_simcall_t request, int value) const
171 {
172   auto addr      = simcall_comm_waitany__getraw__comms(request) + value;
173   auto comm_addr = mc_model_checker->get_remote_process().read(remote(addr));
174   return RemotePtr<kernel::activity::CommImpl>(static_cast<kernel::activity::CommImpl*>(comm_addr));
175 }
176
177 std::string Api::get_pattern_comm_rdv(RemotePtr<kernel::activity::CommImpl> const& addr) const
178 {
179   Remote<kernel::activity::CommImpl> temp_activity;
180   mc_model_checker->get_remote_process().read(temp_activity, addr);
181   const kernel::activity::CommImpl* activity = temp_activity.get_buffer();
182
183   char* remote_name = mc_model_checker->get_remote_process().read<char*>(RemotePtr<char*>(
184       (uint64_t)(activity->get_mailbox() ? &activity->get_mailbox()->get_name() : &activity->mbox_cpy->get_name())));
185   auto rdv          = mc_model_checker->get_remote_process().read_string(RemotePtr<char>(remote_name));
186   return rdv;
187 }
188
189 unsigned long Api::get_pattern_comm_src_proc(RemotePtr<kernel::activity::CommImpl> const& addr) const
190 {
191   Remote<kernel::activity::CommImpl> temp_activity;
192   mc_model_checker->get_remote_process().read(temp_activity, addr);
193   const kernel::activity::CommImpl* activity = temp_activity.get_buffer();
194   auto src_proc =
195       mc_model_checker->get_remote_process().resolve_actor(mc::remote(activity->src_actor_.get()))->get_pid();
196   return src_proc;
197 }
198
199 unsigned long Api::get_pattern_comm_dst_proc(RemotePtr<kernel::activity::CommImpl> const& addr) const
200 {
201   Remote<kernel::activity::CommImpl> temp_activity;
202   mc_model_checker->get_remote_process().read(temp_activity, addr);
203   const kernel::activity::CommImpl* activity = temp_activity.get_buffer();
204   auto src_proc =
205       mc_model_checker->get_remote_process().resolve_actor(mc::remote(activity->dst_actor_.get()))->get_pid();
206   return src_proc;
207 }
208
209 std::vector<char> Api::get_pattern_comm_data(RemotePtr<kernel::activity::CommImpl> const& addr) const
210 {
211   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
212   mc_model_checker->get_remote_process().read(temp_comm, addr);
213   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
214
215   std::vector<char> buffer{};
216   if (comm->src_buff_ != nullptr) {
217     buffer.resize(comm->src_buff_size_);
218     mc_model_checker->get_remote_process().read_bytes(buffer.data(), buffer.size(), remote(comm->src_buff_));
219   }
220   return buffer;
221 }
222
223 #if HAVE_SMPI
224 bool Api::check_send_request_detached(smx_simcall_t const& simcall) const
225 {
226   Remote<simgrid::smpi::Request> mpi_request;
227   mc_model_checker->get_remote_process().read(
228       mpi_request, remote(static_cast<smpi::Request*>(simcall_comm_isend__get__data(simcall))));
229   return mpi_request.get_buffer()->detached();
230 }
231 #endif
232
233 smx_actor_t Api::get_src_actor(RemotePtr<kernel::activity::CommImpl> const& comm_addr) const
234 {
235   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
236   mc_model_checker->get_remote_process().read(temp_comm, comm_addr);
237   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
238
239   auto src_proc = mc_model_checker->get_remote_process().resolve_actor(simgrid::mc::remote(comm->src_actor_.get()));
240   return src_proc;
241 }
242
243 smx_actor_t Api::get_dst_actor(RemotePtr<kernel::activity::CommImpl> const& comm_addr) const
244 {
245   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
246   mc_model_checker->get_remote_process().read(temp_comm, comm_addr);
247   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
248
249   auto dst_proc = mc_model_checker->get_remote_process().resolve_actor(simgrid::mc::remote(comm->dst_actor_.get()));
250   return dst_proc;
251 }
252
253 std::size_t Api::get_remote_heap_bytes() const
254 {
255   RemoteProcess& process    = mc_model_checker->get_remote_process();
256   auto heap_bytes_used      = mmalloc_get_bytes_used_remote(process.get_heap()->heaplimit, process.get_malloc_info());
257   return heap_bytes_used;
258 }
259
260 void Api::mc_inc_visited_states() const
261 {
262   mc_model_checker->visited_states++;
263 }
264
265 unsigned long Api::mc_get_visited_states() const
266 {
267   return mc_model_checker->visited_states;
268 }
269
270 void Api::mc_check_deadlock() const
271 {
272   if (mc_model_checker->checkDeadlock()) {
273     XBT_CINFO(mc_global, "**************************");
274     XBT_CINFO(mc_global, "*** DEADLOCK DETECTED ***");
275     XBT_CINFO(mc_global, "**************************");
276     XBT_CINFO(mc_global, "Counter-example execution trace:");
277     for (auto const& s : mc_model_checker->getChecker()->get_textual_trace())
278       XBT_CINFO(mc_global, "  %s", s.c_str());
279     XBT_INFO("Path = %s", mc_model_checker->getChecker()->get_record_trace().to_string().c_str());
280     simgrid::mc::session_singleton->log_state();
281     throw DeadlockError();
282   }
283 }
284
285 /** Get the issuer of a simcall (`req->issuer`)
286  *
287  *  In split-process mode, it does the black magic necessary to get an address
288  *  of a (shallow) copy of the data structure the issuer SIMIX actor in the local
289  *  address space.
290  *
291  *  @param process the MCed process
292  *  @param req     the simcall (copied in the local process)
293  */
294 smx_actor_t Api::simcall_get_issuer(s_smx_simcall const* req) const
295 {
296   xbt_assert(mc_model_checker != nullptr);
297
298   // This is the address of the smx_actor in the MCed process:
299   auto address = simgrid::mc::remote(req->issuer_);
300
301   // Lookup by address:
302   for (auto& actor : mc_model_checker->get_remote_process().actors())
303     if (actor.address == address)
304       return actor.copy.get_buffer();
305   for (auto& actor : mc_model_checker->get_remote_process().dead_actors())
306     if (actor.address == address)
307       return actor.copy.get_buffer();
308
309   xbt_die("Issuer not found");
310 }
311
312 RemotePtr<kernel::activity::MailboxImpl> Api::get_mbox_remote_addr(smx_simcall_t const req) const
313 {
314   if (req->call_ == Simcall::COMM_ISEND)
315     return remote(simcall_comm_isend__get__mbox(req));
316   if (req->call_ == Simcall::COMM_IRECV)
317     return remote(simcall_comm_irecv__get__mbox(req));
318   THROW_IMPOSSIBLE;
319 }
320
321 RemotePtr<kernel::activity::ActivityImpl> Api::get_comm_remote_addr(smx_simcall_t const req) const
322 {
323   if (req->call_ == Simcall::COMM_ISEND)
324     return remote(simcall_comm_isend__getraw__result(req));
325   if (req->call_ == Simcall::COMM_IRECV)
326     return remote(simcall_comm_irecv__getraw__result(req));
327   THROW_IMPOSSIBLE;
328 }
329
330 void Api::mc_exit(int status) const
331 {
332   mc_model_checker->exit(status);
333 }
334
335 std::string Api::request_get_dot_output(const Transition* t) const
336 {
337   static constexpr std::array<const char*, 13> colors{{"blue", "red", "green3", "goldenrod", "brown", "purple",
338                                                        "magenta", "turquoise4", "gray25", "forestgreen", "hotpink",
339                                                        "lightblue", "tan"}};
340   const char* color = colors[(t->aid_ - 1) % colors.size()];
341
342   return "label = \"" + t->dot_label() + "\", color = " + color + ", fontcolor = " + color;
343 }
344
345 #if HAVE_SMPI
346 int Api::get_smpi_request_tag(smx_simcall_t const& simcall, simgrid::simix::Simcall type) const
347 {
348   void* simcall_data = nullptr;
349   if (type == Simcall::COMM_ISEND)
350     simcall_data = simcall_comm_isend__get__data(simcall);
351   else if (type == Simcall::COMM_IRECV)
352     simcall_data = simcall_comm_irecv__get__data(simcall);
353   Remote<simgrid::smpi::Request> mpi_request;
354   mc_model_checker->get_remote_process().read(mpi_request, remote(static_cast<smpi::Request*>(simcall_data)));
355   return mpi_request.get_buffer()->tag();
356 }
357 #endif
358
359 void Api::restore_state(std::shared_ptr<simgrid::mc::Snapshot> system_state) const
360 {
361   system_state->restore(&mc_model_checker->get_remote_process());
362 }
363
364 void Api::log_state() const
365 {
366   session_singleton->log_state();
367 }
368
369 bool Api::snapshot_equal(const Snapshot* s1, const Snapshot* s2) const
370 {
371   return simgrid::mc::snapshot_equal(s1, s2);
372 }
373
374 simgrid::mc::Snapshot* Api::take_snapshot(long num_state) const
375 {
376   auto snapshot = new simgrid::mc::Snapshot(num_state);
377   return snapshot;
378 }
379
380 void Api::s_close() const
381 {
382   session_singleton->close();
383 }
384
385 void Api::automaton_load(const char* file) const
386 {
387   if (simgrid::mc::property_automaton == nullptr)
388     simgrid::mc::property_automaton = xbt_automaton_new();
389
390   xbt_automaton_load(simgrid::mc::property_automaton, file);
391 }
392
393 std::vector<int> Api::automaton_propositional_symbol_evaluate() const
394 {
395   unsigned int cursor = 0;
396   std::vector<int> values;
397   xbt_automaton_propositional_symbol_t ps = nullptr;
398   xbt_dynar_foreach (mc::property_automaton->propositional_symbols, cursor, ps)
399     values.push_back(xbt_automaton_propositional_symbol_evaluate(ps));
400   return values;
401 }
402
403 std::vector<xbt_automaton_state_t> Api::get_automaton_state() const
404 {
405   std::vector<xbt_automaton_state_t> automaton_stack;
406   unsigned int cursor = 0;
407   xbt_automaton_state_t automaton_state;
408   xbt_dynar_foreach (mc::property_automaton->states, cursor, automaton_state)
409     if (automaton_state->type == -1)
410       automaton_stack.push_back(automaton_state);
411   return automaton_stack;
412 }
413
414 int Api::compare_automaton_exp_label(const xbt_automaton_exp_label* l) const
415 {
416   unsigned int cursor                    = 0;
417   xbt_automaton_propositional_symbol_t p = nullptr;
418   xbt_dynar_foreach (simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
419     if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
420       return cursor;
421   }
422   return -1;
423 }
424
425 void Api::set_property_automaton(xbt_automaton_state_t const& automaton_state) const
426 {
427   mc::property_automaton->current_state = automaton_state;
428 }
429
430 xbt_automaton_exp_label_t Api::get_automaton_transition_label(xbt_dynar_t const& dynar, int index) const
431 {
432   const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
433   return transition->label;
434 }
435
436 xbt_automaton_state_t Api::get_automaton_transition_dst(xbt_dynar_t const& dynar, int index) const
437 {
438   const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
439   return transition->dst;
440 }
441
442 } // namespace mc
443 } // namespace simgrid