Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use std::unique_ptr and remove explicit destructor.
[simgrid.git] / src / mc / api.cpp
1 /* Copyright (c) 2020-2021. 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_comm_pattern.hpp"
15 #include "src/mc/mc_exit.hpp"
16 #include "src/mc/mc_pattern.hpp"
17 #include "src/mc/mc_private.hpp"
18 #include "src/mc/remote/RemoteProcess.hpp"
19 #include "src/surf/HostImpl.hpp"
20
21 #include <xbt/asserts.h>
22 #include <xbt/log.h>
23 #include "simgrid/s4u/Host.hpp"
24 #include "xbt/string.hpp"
25 #if HAVE_SMPI
26 #include "src/smpi/include/smpi_request.hpp"
27 #endif
28
29 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(Api, mc, "Logging specific to MC Facade APIs ");
30
31 using Simcall = simgrid::simix::Simcall;
32
33 namespace simgrid {
34 namespace mc {
35
36 static inline const char* get_color(int id)
37 {
38   static constexpr std::array<const char*, 13> colors{{"blue", "red", "green3", "goldenrod", "brown", "purple",
39                                                        "magenta", "turquoise4", "gray25", "forestgreen", "hotpink",
40                                                        "lightblue", "tan"}};
41   return colors[id % colors.size()];
42 }
43
44 static std::string pointer_to_string(void* pointer)
45 {
46   return XBT_LOG_ISENABLED(Api, xbt_log_priority_verbose) ? xbt::string_printf("%p", pointer) : "(verbose only)";
47 }
48
49 static std::string buff_size_to_string(size_t buff_size)
50 {
51   return XBT_LOG_ISENABLED(Api, xbt_log_priority_verbose) ? std::to_string(buff_size) : "(verbose only)";
52 }
53
54 static void simcall_translate(smx_simcall_t req,
55                               simgrid::mc::Remote<simgrid::kernel::activity::CommImpl>& buffered_comm);
56
57 static bool request_is_enabled_by_idx(const RemoteProcess& process, smx_simcall_t req, unsigned int idx)
58 {
59   kernel::activity::CommImpl* remote_act = nullptr;
60   switch (req->call_) {
61     case Simcall::COMM_WAIT:
62       /* FIXME: check also that src and dst processes are not suspended */
63       remote_act = simcall_comm_wait__getraw__comm(req);
64       break;
65
66     case Simcall::COMM_WAITANY:
67       remote_act = process.read(remote(simcall_comm_waitany__get__comms(req) + idx));
68       break;
69
70     case Simcall::COMM_TESTANY:
71       remote_act = process.read(remote(simcall_comm_testany__get__comms(req) + idx));
72       break;
73
74     default:
75       return true;
76   }
77
78   Remote<kernel::activity::CommImpl> temp_comm;
79   process.read(temp_comm, remote(remote_act));
80   const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
81   return comm->src_actor_.get() && comm->dst_actor_.get();
82 }
83
84 /* Search an enabled transition for the given process.
85  *
86  * This can be seen as an iterator returning the next transition of the process.
87  *
88  * We only consider the processes that are both
89  *  - marked "to be interleaved" in their ActorState (controlled by the checker algorithm).
90  *  - which simcall can currently be executed (like a comm where the other partner is already known)
91  * Once we returned the last enabled transition of a process, it is marked done.
92  *
93  * Things can get muddled with the WAITANY and TESTANY simcalls, that are rewritten on the fly to a bunch of WAIT
94  * (resp TEST) transitions using the transition.argument field to remember what was the last returned sub-transition.
95  */
96 static inline smx_simcall_t MC_state_choose_request_for_process(const RemoteProcess& process, simgrid::mc::State* state,
97                                                                 smx_actor_t actor)
98 {
99   /* reset the outgoing transition */
100   simgrid::mc::ActorState* procstate = &state->actor_states_[actor->get_pid()];
101   state->transition_.aid_              = -1;
102   state->transition_.times_considered_ = -1;
103   state->transition_.textual[0]        = '\0';
104   state->executed_req_.call_         = Simcall::NONE;
105
106   if (not simgrid::mc::actor_is_enabled(actor))
107     return nullptr; // Not executable in the application
108
109   smx_simcall_t req = nullptr;
110   if (actor->simcall_.observer_ != nullptr) {
111     state->transition_.times_considered_ = procstate->get_times_considered_and_inc();
112     if (actor->simcall_.mc_max_consider_ <= procstate->get_times_considered())
113       procstate->set_done();
114     req = &actor->simcall_;
115   } else
116     switch (actor->simcall_.call_) {
117       case Simcall::COMM_WAITANY:
118         state->transition_.times_considered_ = -1;
119         while (procstate->get_times_considered() < simcall_comm_waitany__get__count(&actor->simcall_)) {
120           if (simgrid::mc::request_is_enabled_by_idx(process, &actor->simcall_, procstate->get_times_considered())) {
121             state->transition_.times_considered_ = procstate->get_times_considered_and_inc();
122             break;
123           }
124           procstate->get_times_considered_and_inc();
125         }
126
127         if (procstate->get_times_considered() >= simcall_comm_waitany__get__count(&actor->simcall_))
128           procstate->set_done();
129         if (state->transition_.times_considered_ != -1)
130           req = &actor->simcall_;
131         break;
132
133       case Simcall::COMM_TESTANY:
134         state->transition_.times_considered_ = -1;
135         while (procstate->get_times_considered() < simcall_comm_testany__get__count(&actor->simcall_)) {
136           if (simgrid::mc::request_is_enabled_by_idx(process, &actor->simcall_, procstate->get_times_considered())) {
137             state->transition_.times_considered_ = procstate->get_times_considered_and_inc();
138             break;
139           }
140           procstate->get_times_considered_and_inc();
141         }
142
143         if (procstate->get_times_considered() >= simcall_comm_testany__get__count(&actor->simcall_))
144           procstate->set_done();
145         if (state->transition_.times_considered_ != -1)
146           req = &actor->simcall_;
147         break;
148
149       case Simcall::COMM_WAIT: {
150         simgrid::mc::RemotePtr<simgrid::kernel::activity::CommImpl> remote_act =
151             remote(simcall_comm_wait__get__comm(&actor->simcall_));
152         simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_act;
153         process.read(temp_act, remote_act);
154         const simgrid::kernel::activity::CommImpl* act = temp_act.get_buffer();
155         if (act->src_actor_.get() && act->dst_actor_.get())
156           state->transition_.times_considered_ = 0; // OK
157         else if (act->src_actor_.get() == nullptr && act->state_ == simgrid::kernel::activity::State::READY &&
158                  act->detached())
159           state->transition_.times_considered_ = 0; // OK
160         else
161           state->transition_.times_considered_ = -1; // timeout
162         procstate->set_done();
163         req = &actor->simcall_;
164         break;
165       }
166
167       default:
168         procstate->set_done();
169         state->transition_.times_considered_ = 0;
170         req                                  = &actor->simcall_;
171         break;
172     }
173   if (not req)
174     return nullptr;
175
176   state->transition_.aid_ = actor->get_pid();
177   state->executed_req_    = *req;
178
179   // Fetch the data of the request and translate it:
180   state->internal_req_ = *req;
181   state->internal_req_.mc_value_ = state->transition_.times_considered_;
182   simcall_translate(&state->internal_req_, state->internal_comm_);
183
184   return req;
185 }
186
187 static void simcall_translate(smx_simcall_t req,
188                               simgrid::mc::Remote<simgrid::kernel::activity::CommImpl>& buffered_comm)
189 {
190   simgrid::kernel::activity::CommImpl* chosen_comm;
191
192   /* The waitany and testany request are transformed into a wait or test request over the corresponding communication
193    * action so it can be treated later by the dependence function. */
194   switch (req->call_) {
195     case Simcall::COMM_WAITANY:
196       req->call_  = Simcall::COMM_WAIT;
197       chosen_comm =
198           mc_model_checker->get_remote_process().read(remote(simcall_comm_waitany__get__comms(req) + req->mc_value_));
199
200       mc_model_checker->get_remote_process().read(buffered_comm, remote(chosen_comm));
201       simcall_comm_wait__set__comm(req, buffered_comm.get_buffer());
202       simcall_comm_wait__set__timeout(req, 0);
203       break;
204
205     case Simcall::COMM_TESTANY:
206       req->call_  = Simcall::COMM_TEST;
207       chosen_comm =
208           mc_model_checker->get_remote_process().read(remote(simcall_comm_testany__get__comms(req) + req->mc_value_));
209
210       mc_model_checker->get_remote_process().read(buffered_comm, remote(chosen_comm));
211       simcall_comm_test__set__comm(req, buffered_comm.get_buffer());
212       simcall_comm_test__set__result(req, req->mc_value_);
213       break;
214
215     case Simcall::COMM_WAIT:
216       chosen_comm = simcall_comm_wait__get__comm(req);
217       mc_model_checker->get_remote_process().read(buffered_comm, remote(chosen_comm));
218       simcall_comm_wait__set__comm(req, buffered_comm.get_buffer());
219       break;
220
221     case Simcall::COMM_TEST:
222       chosen_comm = simcall_comm_test__get__comm(req);
223       mc_model_checker->get_remote_process().read(buffered_comm, remote(chosen_comm));
224       simcall_comm_test__set__comm(req, buffered_comm.get_buffer());
225       break;
226
227     default:
228       /* No translation needed */
229       break;
230   }
231 }
232
233 simgrid::kernel::activity::CommImpl* Api::get_comm_or_nullptr(smx_simcall_t const r) const
234 {
235   if (r->call_ == Simcall::COMM_WAIT)
236     return simcall_comm_wait__get__comm(r);
237   if (r->call_ == Simcall::COMM_TEST)
238     return simcall_comm_test__get__comm(r);
239   return nullptr;
240 }
241
242 /** Statically "upcast" a s_smx_actor_t into an ActorInformation
243  *
244  *  This gets 'actorInfo' from '&actorInfo->copy'. It upcasts in the
245  *  sense that we could achieve the same thing by having ActorInformation
246  *  inherit from s_smx_actor_t but we don't really want to do that.
247  */
248 simgrid::mc::ActorInformation* Api::actor_info_cast(smx_actor_t actor) const
249 {
250   simgrid::mc::ActorInformation temp;
251   std::size_t offset = (char*)temp.copy.get_buffer() - (char*)&temp;
252
253   auto* process_info = reinterpret_cast<simgrid::mc::ActorInformation*>((char*)actor - offset);
254   return process_info;
255 }
256
257 bool Api::simcall_check_dependency(smx_simcall_t req1, smx_simcall_t req2) const
258 {
259   const auto IRECV = Simcall::COMM_IRECV;
260   const auto ISEND = Simcall::COMM_ISEND;
261   const auto TEST  = Simcall::COMM_TEST;
262   const auto WAIT  = Simcall::COMM_WAIT;
263
264   if (req1->issuer_ == req2->issuer_)
265     return false;
266
267   /* The independence theorem only consider 4 simcalls. All others are dependent with anything. */
268   if (req1->call_ != ISEND && req1->call_ != IRECV && req1->call_ != TEST && req1->call_ != WAIT)
269     return true;
270   if (req2->call_ != ISEND && req2->call_ != IRECV && req2->call_ != TEST && req2->call_ != WAIT)
271     return true;
272
273   /* Timeouts in wait transitions are not considered by the independence theorem, thus assumed dependent */
274   if ((req1->call_ == WAIT && simcall_comm_wait__get__timeout(req1) > 0) ||
275       (req2->call_ == WAIT && simcall_comm_wait__get__timeout(req2) > 0))
276     return true;
277
278   /* Make sure that req1 and req2 are in alphabetic order */
279   if (req1->call_ > req2->call_) {
280     auto temp = req1;
281     req1      = req2;
282     req2      = temp;
283   }
284
285   auto comm1 = get_comm_or_nullptr(req1);
286   auto comm2 = get_comm_or_nullptr(req2);
287
288   /* First case: that's not the same kind of request (we also know that req1 < req2 alphabetically) */
289   if (req1->call_ != req2->call_) {
290     if (req1->call_ == IRECV && req2->call_ == ISEND)
291       return false;
292
293     if ((req1->call_ == IRECV || req1->call_ == ISEND) && req2->call_ == WAIT) {
294       auto mbox1 = get_mbox_remote_addr(req1);
295       auto mbox2 = remote(comm2->mbox_cpy);
296
297       if (mbox1 != mbox2 && simcall_comm_wait__get__timeout(req2) <= 0)
298         return false;
299
300       if ((req1->issuer_ != comm2->src_actor_.get()) && (req1->issuer_ != comm2->dst_actor_.get()) &&
301           simcall_comm_wait__get__timeout(req2) <= 0)
302         return false;
303
304       if ((req1->call_ == ISEND) && (comm2->type_ == kernel::activity::CommImpl::Type::SEND) &&
305           (comm2->src_buff_ != simcall_comm_isend__get__src_buff(req1)) && simcall_comm_wait__get__timeout(req2) <= 0)
306         return false;
307
308       if ((req1->call_ == IRECV) && (comm2->type_ == kernel::activity::CommImpl::Type::RECEIVE) &&
309           (comm2->dst_buff_ != simcall_comm_irecv__get__dst_buff(req1)) && simcall_comm_wait__get__timeout(req2) <= 0)
310         return false;
311     }
312
313     /* FIXME: the following rule assumes that the result of the isend/irecv call is not stored in a buffer used in the
314      * test call. */
315 #if 0
316   if((req1->call == ISEND || req1->call == IRECV)
317       &&  req2->call == TEST)
318     return false;
319 #endif
320
321     if (req1->call_ == TEST && req2->call_ == WAIT &&
322         (comm1->src_actor_.get() == nullptr || comm1->dst_actor_.get() == nullptr))
323       return false;
324
325     if (req1->call_ == TEST &&
326         (simcall_comm_test__get__comm(req1) == nullptr || comm1->src_buff_ == nullptr || comm1->dst_buff_ == nullptr))
327       return false;
328     if (req2->call_ == TEST &&
329         (simcall_comm_test__get__comm(req2) == nullptr || comm2->src_buff_ == nullptr || comm2->dst_buff_ == nullptr))
330       return false;
331
332     if (req1->call_ == TEST && req2->call_ == WAIT && comm1->src_buff_ == comm2->src_buff_ &&
333         comm1->dst_buff_ == comm2->dst_buff_)
334       return false;
335
336     if (req1->call_ == TEST && req2->call_ == WAIT && comm1->src_buff_ != nullptr && comm1->dst_buff_ != nullptr &&
337         comm2->src_buff_ != nullptr && comm2->dst_buff_ != nullptr && comm1->dst_buff_ != comm2->src_buff_ &&
338         comm1->dst_buff_ != comm2->dst_buff_ && comm2->dst_buff_ != comm1->src_buff_)
339       return false;
340
341     return true;
342   }
343
344   /* Second case: req1 and req2 are of the same call type */
345   switch (req1->call_) {
346     case ISEND:
347       return simcall_comm_isend__get__mbox(req1) == simcall_comm_isend__get__mbox(req2);
348     case IRECV:
349       return simcall_comm_irecv__get__mbox(req1) == simcall_comm_irecv__get__mbox(req2);
350     case WAIT:
351       if (comm1->src_buff_ == comm2->src_buff_ && comm1->dst_buff_ == comm2->dst_buff_)
352         return false;
353       if (comm1->src_buff_ != nullptr && comm1->dst_buff_ != nullptr && comm2->src_buff_ != nullptr &&
354           comm2->dst_buff_ != nullptr && comm1->dst_buff_ != comm2->src_buff_ && comm1->dst_buff_ != comm2->dst_buff_ &&
355           comm2->dst_buff_ != comm1->src_buff_)
356         return false;
357       return true;
358     default:
359       return true;
360   }
361 }
362
363 xbt::string const& Api::get_actor_host_name(smx_actor_t actor) const
364 {
365   if (mc_model_checker == nullptr)
366     return actor->get_host()->get_name();
367
368   const simgrid::mc::RemoteProcess* process = &mc_model_checker->get_remote_process();
369
370   // Read the simgrid::xbt::string in the MCed process:
371   simgrid::mc::ActorInformation* info = actor_info_cast(actor);
372
373   if (not info->hostname) {
374     Remote<s4u::Host> temp_host = process->read(remote(actor->get_host()));
375     auto remote_string_address  = remote(&xbt::string::to_string_data(temp_host.get_buffer()->get_impl()->get_name()));
376     simgrid::xbt::string_data remote_string = process->read(remote_string_address);
377     std::vector<char> hostname(remote_string.len + 1);
378     // no need to read the terminating null byte, and thus hostname[remote_string.len] is guaranteed to be '\0'
379     process->read_bytes(hostname.data(), remote_string.len, remote(remote_string.data));
380     info->hostname = &mc_model_checker->get_host_name(hostname.data());
381   }
382   return *info->hostname;
383 }
384
385 xbt::string const& Api::get_actor_name(smx_actor_t actor) const
386 {
387   if (mc_model_checker == nullptr)
388     return actor->get_name();
389
390   simgrid::mc::ActorInformation* info = actor_info_cast(actor);
391   if (info->name.empty()) {
392     const simgrid::mc::RemoteProcess* process = &mc_model_checker->get_remote_process();
393
394     simgrid::xbt::string_data string_data = simgrid::xbt::string::to_string_data(actor->name_);
395     info->name = process->read_string(remote(string_data.data), string_data.len);
396   }
397   return info->name;
398 }
399
400 std::string Api::get_actor_string(smx_actor_t actor) const
401 {
402   std::string res;
403   if (actor) {
404     res = "(" + std::to_string(actor->get_pid()) + ")";
405     if (actor->get_host())
406       res += std::string(get_actor_host_name(actor)) + " (" + std::string(get_actor_name(actor)) + ")";
407     else
408       res += get_actor_name(actor);
409   } else
410     res = "(0) ()";
411   return res;
412 }
413
414 std::string Api::get_actor_dot_label(smx_actor_t actor) const
415 {
416   std::string res = "(" + std::to_string(actor->get_pid()) + ")";
417   if (actor->get_host())
418     res += get_actor_host_name(actor);
419   return res;
420 }
421
422 simgrid::mc::Checker* Api::initialize(char** argv, simgrid::mc::CheckerAlgorithm algo) const
423 {
424   auto session = new simgrid::mc::Session([argv] {
425     int i = 1;
426     while (argv[i] != nullptr && argv[i][0] == '-')
427       i++;
428     xbt_assert(argv[i] != nullptr,
429                "Unable to find a binary to exec on the command line. Did you only pass config flags?");
430     execvp(argv[i], argv + i);
431     xbt_die("The model-checked process failed to exec(%s): %s", argv[i], strerror(errno));
432   });
433
434   simgrid::mc::Checker* checker;
435   switch (algo) {
436     case CheckerAlgorithm::CommDeterminism:
437       checker = simgrid::mc::create_communication_determinism_checker(session);
438       break;
439
440     case CheckerAlgorithm::UDPOR:
441       checker = simgrid::mc::create_udpor_checker(session);
442       break;
443
444     case CheckerAlgorithm::Safety:
445       checker = simgrid::mc::create_safety_checker(session);
446       break;
447
448     case CheckerAlgorithm::Liveness:
449       checker = simgrid::mc::create_liveness_checker(session);
450       break;
451
452     default:
453       THROW_IMPOSSIBLE;
454   }
455
456   // FIXME: session and checker are never deleted
457   simgrid::mc::session_singleton = session;
458   mc_model_checker->setChecker(checker);
459   return checker;
460 }
461
462 std::vector<simgrid::mc::ActorInformation>& Api::get_actors() const
463 {
464   return mc_model_checker->get_remote_process().actors();
465 }
466
467 unsigned long Api::get_maxpid() const
468 {
469   return mc_model_checker->get_remote_process().get_maxpid();
470 }
471
472 int Api::get_actors_size() const
473 {
474   return mc_model_checker->get_remote_process().actors().size();
475 }
476
477 RemotePtr<kernel::activity::CommImpl> Api::get_comm_isend_raw_addr(smx_simcall_t request) const
478 {
479   return remote(static_cast<kernel::activity::CommImpl*>(simcall_comm_isend__getraw__result(request)));
480 }
481
482 RemotePtr<kernel::activity::CommImpl> Api::get_comm_waitany_raw_addr(smx_simcall_t request, int value) const
483 {
484   auto addr      = simcall_comm_waitany__getraw__comms(request) + value;
485   auto comm_addr = mc_model_checker->get_remote_process().read(remote(addr));
486   return RemotePtr<kernel::activity::CommImpl>(static_cast<kernel::activity::CommImpl*>(comm_addr));
487 }
488
489 std::string Api::get_pattern_comm_rdv(RemotePtr<kernel::activity::CommImpl> const& addr) const
490 {
491   Remote<kernel::activity::CommImpl> temp_activity;
492   mc_model_checker->get_remote_process().read(temp_activity, addr);
493   const kernel::activity::CommImpl* activity = temp_activity.get_buffer();
494
495   char* remote_name = mc_model_checker->get_remote_process().read<char*>(RemotePtr<char*>(
496       (uint64_t)(activity->get_mailbox() ? &activity->get_mailbox()->get_name() : &activity->mbox_cpy->get_name())));
497   auto rdv          = mc_model_checker->get_remote_process().read_string(RemotePtr<char>(remote_name));
498   return rdv;
499 }
500
501 unsigned long Api::get_pattern_comm_src_proc(RemotePtr<kernel::activity::CommImpl> const& addr) const
502 {
503   Remote<kernel::activity::CommImpl> temp_activity;
504   mc_model_checker->get_remote_process().read(temp_activity, addr);
505   const kernel::activity::CommImpl* activity = temp_activity.get_buffer();
506   auto src_proc =
507       mc_model_checker->get_remote_process().resolve_actor(mc::remote(activity->src_actor_.get()))->get_pid();
508   return src_proc;
509 }
510
511 unsigned long Api::get_pattern_comm_dst_proc(RemotePtr<kernel::activity::CommImpl> const& addr) const
512 {
513   Remote<kernel::activity::CommImpl> temp_activity;
514   mc_model_checker->get_remote_process().read(temp_activity, addr);
515   const kernel::activity::CommImpl* activity = temp_activity.get_buffer();
516   auto src_proc =
517       mc_model_checker->get_remote_process().resolve_actor(mc::remote(activity->dst_actor_.get()))->get_pid();
518   return src_proc;
519 }
520
521 std::vector<char> Api::get_pattern_comm_data(RemotePtr<kernel::activity::CommImpl> const& addr) const
522 {
523   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
524   mc_model_checker->get_remote_process().read(temp_comm, addr);
525   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
526
527   std::vector<char> buffer{};
528   if (comm->src_buff_ != nullptr) {
529     buffer.resize(comm->src_buff_size_);
530     mc_model_checker->get_remote_process().read_bytes(buffer.data(), buffer.size(), remote(comm->src_buff_));
531   }
532   return buffer;
533 }
534
535 #if HAVE_SMPI
536 bool Api::check_send_request_detached(smx_simcall_t const& simcall) const
537 {
538   Remote<simgrid::smpi::Request> mpi_request;
539   mc_model_checker->get_remote_process().read(
540       mpi_request, remote(static_cast<smpi::Request*>(simcall_comm_isend__get__data(simcall))));
541   return mpi_request.get_buffer()->detached();
542 }
543 #endif
544
545 smx_actor_t Api::get_src_actor(RemotePtr<kernel::activity::CommImpl> const& comm_addr) const
546 {
547   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
548   mc_model_checker->get_remote_process().read(temp_comm, comm_addr);
549   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
550
551   auto src_proc = mc_model_checker->get_remote_process().resolve_actor(simgrid::mc::remote(comm->src_actor_.get()));
552   return src_proc;
553 }
554
555 smx_actor_t Api::get_dst_actor(RemotePtr<kernel::activity::CommImpl> const& comm_addr) const
556 {
557   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
558   mc_model_checker->get_remote_process().read(temp_comm, comm_addr);
559   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
560
561   auto dst_proc = mc_model_checker->get_remote_process().resolve_actor(simgrid::mc::remote(comm->dst_actor_.get()));
562   return dst_proc;
563 }
564
565 std::size_t Api::get_remote_heap_bytes() const
566 {
567   RemoteProcess& process    = mc_model_checker->get_remote_process();
568   auto heap_bytes_used      = mmalloc_get_bytes_used_remote(process.get_heap()->heaplimit, process.get_malloc_info());
569   return heap_bytes_used;
570 }
571
572 void Api::mc_inc_visited_states() const
573 {
574   mc_model_checker->visited_states++;
575 }
576
577 void Api::mc_inc_executed_trans() const
578 {
579   mc_model_checker->executed_transitions++;
580 }
581
582 unsigned long Api::mc_get_visited_states() const
583 {
584   return mc_model_checker->visited_states;
585 }
586
587 unsigned long Api::mc_get_executed_trans() const
588 {
589   return mc_model_checker->executed_transitions;
590 }
591
592 void Api::mc_check_deadlock() const
593 {
594   if (mc_model_checker->checkDeadlock()) {
595     MC_show_deadlock();
596     throw DeadlockError();
597   }
598 }
599
600 /** Get the issuer of a simcall (`req->issuer`)
601  *
602  *  In split-process mode, it does the black magic necessary to get an address
603  *  of a (shallow) copy of the data structure the issuer SIMIX actor in the local
604  *  address space.
605  *
606  *  @param process the MCed process
607  *  @param req     the simcall (copied in the local process)
608  */
609 smx_actor_t Api::simcall_get_issuer(s_smx_simcall const* req) const
610 {
611   xbt_assert(mc_model_checker != nullptr);
612
613   // This is the address of the smx_actor in the MCed process:
614   auto address = simgrid::mc::remote(req->issuer_);
615
616   // Lookup by address:
617   for (auto& actor : mc_model_checker->get_remote_process().actors())
618     if (actor.address == address)
619       return actor.copy.get_buffer();
620   for (auto& actor : mc_model_checker->get_remote_process().dead_actors())
621     if (actor.address == address)
622       return actor.copy.get_buffer();
623
624   xbt_die("Issuer not found");
625 }
626
627 long Api::simcall_get_actor_id(s_smx_simcall const* req) const
628 {
629   return simcall_get_issuer(req)->get_pid();
630 }
631
632 RemotePtr<kernel::activity::MailboxImpl> Api::get_mbox_remote_addr(smx_simcall_t const req) const
633 {
634   if (req->call_ == Simcall::COMM_ISEND)
635     return remote(simcall_comm_isend__get__mbox(req));
636   if (req->call_ == Simcall::COMM_IRECV)
637     return remote(simcall_comm_irecv__get__mbox(req));
638   THROW_IMPOSSIBLE;
639 }
640
641 RemotePtr<kernel::activity::ActivityImpl> Api::get_comm_remote_addr(smx_simcall_t const req) const
642 {
643   if (req->call_ == Simcall::COMM_ISEND)
644     return remote(simcall_comm_isend__getraw__result(req));
645   if (req->call_ == Simcall::COMM_IRECV)
646     return remote(simcall_comm_irecv__getraw__result(req));
647   THROW_IMPOSSIBLE;
648 }
649
650 bool Api::mc_is_null() const
651 {
652   auto is_null = (mc_model_checker == nullptr) ? true : false;
653   return is_null;
654 }
655
656 Checker* Api::mc_get_checker() const
657 {
658   return mc_model_checker->getChecker();
659 }
660
661 void Api::handle_simcall(Transition const& transition) const
662 {
663   mc_model_checker->handle_simcall(transition);
664 }
665
666 void Api::mc_wait_for_requests() const
667 {
668   mc_model_checker->wait_for_requests();
669 }
670
671 void Api::mc_exit(int status) const
672 {
673   mc_model_checker->exit(status);
674 }
675
676 void Api::dump_record_path() const
677 {
678   simgrid::mc::dumpRecordPath();
679 }
680
681 smx_simcall_t Api::mc_state_choose_request(simgrid::mc::State* state) const
682 {
683   RemoteProcess& process = mc_model_checker->get_remote_process();
684   for (auto& actor : process.actors()) {
685     /* Only consider the actors that were marked as interleaving by the checker algorithm */
686     if (not state->actor_states_[actor.copy.get_buffer()->get_pid()].is_todo())
687       continue;
688
689     smx_simcall_t res = MC_state_choose_request_for_process(process, state, actor.copy.get_buffer());
690     if (res)
691       return res;
692   }
693   return nullptr;
694 }
695
696 std::list<transition_detail_t> Api::get_enabled_transitions(simgrid::mc::State* state) const
697 {
698   std::list<transition_detail_t> tr_list{};
699
700   for (auto& actor : mc_model_checker->get_remote_process().actors()) {
701     auto actor_pid  = actor.copy.get_buffer()->get_pid();
702     auto actor_impl = actor.copy.get_buffer();
703
704     // Only consider the actors that were marked as interleaving by the checker algorithm
705     if (not state->actor_states_[actor_pid].is_todo())
706       continue;
707     // Not executable in the application
708     if (not simgrid::mc::actor_is_enabled(actor_impl))
709       continue;
710
711     auto transition       = std::make_unique<s_transition_detail>();
712     Simcall simcall_call  = actor_impl->simcall_.call_;
713     smx_simcall_t simcall = &actor_impl->simcall_;
714     transition->call_     = simcall_call;
715     switch (simcall_call) {
716       case Simcall::COMM_ISEND:
717       case Simcall::COMM_IRECV:
718         transition->mbox_remote_addr = get_mbox_remote_addr(simcall);
719         transition->comm_remote_addr = get_comm_remote_addr(simcall);
720         break;
721
722       default:
723         break;
724     }
725     tr_list.emplace_back(std::move(transition));
726   }
727
728   return tr_list;
729 }
730
731 std::string Api::request_to_string(smx_simcall_t req, int value) const
732 {
733   xbt_assert(mc_model_checker != nullptr, "Must be called from MCer");
734
735   std::string type;
736   std::string args;
737
738   smx_actor_t issuer = simcall_get_issuer(req);
739
740   if (issuer->simcall_.observer_ != nullptr)
741     return mc_model_checker->simcall_to_string(issuer->get_pid(), value);
742
743   switch (req->call_) {
744     case Simcall::COMM_ISEND:
745       type = "iSend";
746       args = "src=" + get_actor_string(issuer);
747       args += ", buff=" + pointer_to_string(simcall_comm_isend__get__src_buff(req));
748       args += ", size=" + buff_size_to_string(simcall_comm_isend__get__src_buff_size(req));
749       break;
750
751     case Simcall::COMM_IRECV: {
752       size_t* remote_size = simcall_comm_irecv__get__dst_buff_size(req);
753       size_t size         = 0;
754       if (remote_size)
755         mc_model_checker->get_remote_process().read_bytes(&size, sizeof(size), remote(remote_size));
756
757       type = "iRecv";
758       args = "dst=" + get_actor_string(issuer);
759       args += ", buff=" + pointer_to_string(simcall_comm_irecv__get__dst_buff(req));
760       args += ", size=" + buff_size_to_string(size);
761       break;
762     }
763
764     case Simcall::COMM_WAIT: {
765       simgrid::kernel::activity::CommImpl* remote_act = simcall_comm_wait__get__comm(req);
766       if (value == -1) {
767         type = "WaitTimeout";
768         args = "comm=" + pointer_to_string(remote_act);
769       } else {
770         type = "Wait";
771
772         simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_activity;
773         const simgrid::kernel::activity::CommImpl* act;
774         mc_model_checker->get_remote_process().read(temp_activity, remote(remote_act));
775         act = temp_activity.get_buffer();
776
777         smx_actor_t src_proc =
778             mc_model_checker->get_remote_process().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
779         smx_actor_t dst_proc =
780             mc_model_checker->get_remote_process().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
781         args = "comm=" + pointer_to_string(remote_act);
782         args += " [" + get_actor_string(src_proc) + "-> " + get_actor_string(dst_proc) + "]";
783       }
784       break;
785     }
786
787     case Simcall::COMM_TEST: {
788       simgrid::kernel::activity::CommImpl* remote_act = simcall_comm_test__get__comm(req);
789       simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_activity;
790       const simgrid::kernel::activity::CommImpl* act;
791       mc_model_checker->get_remote_process().read(temp_activity, remote(remote_act));
792       act = temp_activity.get_buffer();
793
794       if (act->src_actor_.get() == nullptr || act->dst_actor_.get() == nullptr) {
795         type = "Test FALSE";
796         args = "comm=" + pointer_to_string(remote_act);
797       } else {
798         type = "Test TRUE";
799
800         smx_actor_t src_proc =
801             mc_model_checker->get_remote_process().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
802         smx_actor_t dst_proc =
803             mc_model_checker->get_remote_process().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
804         args = "comm=" + pointer_to_string(remote_act);
805         args += " [" + get_actor_string(src_proc) + " -> " + get_actor_string(dst_proc) + "]";
806       }
807       break;
808     }
809
810     case Simcall::COMM_WAITANY: {
811       type         = "WaitAny";
812       size_t count = simcall_comm_waitany__get__count(req);
813       if (count > 0) {
814         simgrid::kernel::activity::CommImpl* remote_sync;
815         remote_sync =
816             mc_model_checker->get_remote_process().read(remote(simcall_comm_waitany__get__comms(req) + value));
817         args = "comm=" + pointer_to_string(remote_sync) + xbt::string_printf("(%d of %zu)", value + 1, count);
818       } else
819         args = "comm at idx " + std::to_string(value);
820       break;
821     }
822
823     case Simcall::COMM_TESTANY:
824       if (value == -1) {
825         type = "TestAny FALSE";
826         args = "-";
827       } else {
828         type = "TestAny";
829         args = xbt::string_printf("(%d of %zu)", value + 1, simcall_comm_testany__get__count(req));
830       }
831       break;
832
833     default:
834       type = SIMIX_simcall_name(*req);
835       args = "??";
836       break;
837   }
838
839   return "[" + get_actor_string(issuer) + "] " + type + "(" + args + ")";
840 }
841
842 std::string Api::request_get_dot_output(smx_simcall_t req, int value) const
843 {
844   const smx_actor_t issuer = simcall_get_issuer(req);
845   const char* color        = get_color(issuer->get_pid() - 1);
846
847   std::string label;
848
849   if (req->observer_ != nullptr) {
850     label = mc_model_checker->simcall_dot_label(issuer->get_pid(), value);
851   } else
852     switch (req->call_) {
853       case Simcall::COMM_ISEND:
854         label = "[" + get_actor_dot_label(issuer) + "] iSend";
855         break;
856
857       case Simcall::COMM_IRECV:
858         label = "[" + get_actor_dot_label(issuer) + "] iRecv";
859         break;
860
861       case Simcall::COMM_WAIT:
862         if (value == -1) {
863           label = "[" + get_actor_dot_label(issuer) + "] WaitTimeout";
864         } else {
865           kernel::activity::ActivityImpl* remote_act = simcall_comm_wait__get__comm(req);
866           Remote<kernel::activity::CommImpl> temp_comm;
867           mc_model_checker->get_remote_process().read(temp_comm,
868                                                       remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
869           const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
870
871           const kernel::actor::ActorImpl* src_proc =
872               mc_model_checker->get_remote_process().resolve_actor(mc::remote(comm->src_actor_.get()));
873           const kernel::actor::ActorImpl* dst_proc =
874               mc_model_checker->get_remote_process().resolve_actor(mc::remote(comm->dst_actor_.get()));
875           label = "[" + get_actor_dot_label(issuer) + "] Wait";
876           label += " [(" + std::to_string(src_proc ? src_proc->get_pid() : 0) + ")";
877           label += "->(" + std::to_string(dst_proc ? dst_proc->get_pid() : 0) + ")]";
878         }
879         break;
880
881       case Simcall::COMM_TEST: {
882         kernel::activity::ActivityImpl* remote_act = simcall_comm_test__get__comm(req);
883         Remote<simgrid::kernel::activity::CommImpl> temp_comm;
884         mc_model_checker->get_remote_process().read(temp_comm,
885                                                     remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
886         const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
887         if (comm->src_actor_.get() == nullptr || comm->dst_actor_.get() == nullptr) {
888           label = "[" + get_actor_dot_label(issuer) + "] Test FALSE";
889         } else {
890           label = "[" + get_actor_dot_label(issuer) + "] Test TRUE";
891         }
892         break;
893       }
894
895       case Simcall::COMM_WAITANY:
896         label = "[" + get_actor_dot_label(issuer) + "] WaitAny";
897         label += xbt::string_printf(" [%d of %zu]", value + 1, simcall_comm_waitany__get__count(req));
898         break;
899
900       case Simcall::COMM_TESTANY:
901         if (value == -1) {
902           label = "[" + get_actor_dot_label(issuer) + "] TestAny FALSE";
903         } else {
904           label = "[" + get_actor_dot_label(issuer) + "] TestAny TRUE";
905           label += xbt::string_printf(" [%d of %zu]", value + 1, simcall_comm_testany__get__count(req));
906         }
907         break;
908
909       default:
910         THROW_UNIMPLEMENTED;
911     }
912
913   return "label = \"" + label + "\", color = " + color + ", fontcolor = " + color;
914 }
915
916 #if HAVE_SMPI
917 int Api::get_smpi_request_tag(smx_simcall_t const& simcall, simgrid::simix::Simcall type) const
918 {
919   void* simcall_data = nullptr;
920   if (type == Simcall::COMM_ISEND)
921     simcall_data = simcall_comm_isend__get__data(simcall);
922   else if (type == Simcall::COMM_IRECV)
923     simcall_data = simcall_comm_irecv__get__data(simcall);
924   Remote<simgrid::smpi::Request> mpi_request;
925   mc_model_checker->get_remote_process().read(mpi_request, remote(static_cast<smpi::Request*>(simcall_data)));
926   return mpi_request.get_buffer()->tag();
927 }
928 #endif
929
930 void Api::restore_state(std::shared_ptr<simgrid::mc::Snapshot> system_state) const
931 {
932   system_state->restore(&mc_model_checker->get_remote_process());
933 }
934
935 void Api::log_state() const
936 {
937   session_singleton->log_state();
938 }
939
940 bool Api::snapshot_equal(const Snapshot* s1, const Snapshot* s2) const
941 {
942   return simgrid::mc::snapshot_equal(s1, s2);
943 }
944
945 simgrid::mc::Snapshot* Api::take_snapshot(int num_state) const
946 {
947   auto snapshot = new simgrid::mc::Snapshot(num_state);
948   return snapshot;
949 }
950
951 void Api::s_close() const
952 {
953   session_singleton->close();
954 }
955
956 void Api::execute(Transition& transition, smx_simcall_t simcall) const
957 {
958   /* FIXME: once all simcalls have observers, kill the simcall parameter and use mc_model_checker->simcall_to_string() */
959   transition.textual = request_to_string(simcall, transition.times_considered_);
960   session_singleton->execute(transition);
961 }
962
963 void Api::automaton_load(const char* file) const
964 {
965   MC_automaton_load(file);
966 }
967
968 std::vector<int> Api::automaton_propositional_symbol_evaluate() const
969 {
970   unsigned int cursor = 0;
971   std::vector<int> values;
972   xbt_automaton_propositional_symbol_t ps = nullptr;
973   xbt_dynar_foreach (mc::property_automaton->propositional_symbols, cursor, ps)
974     values.push_back(xbt_automaton_propositional_symbol_evaluate(ps));
975   return values;
976 }
977
978 std::vector<xbt_automaton_state_t> Api::get_automaton_state() const
979 {
980   std::vector<xbt_automaton_state_t> automaton_stack;
981   unsigned int cursor = 0;
982   xbt_automaton_state_t automaton_state;
983   xbt_dynar_foreach (mc::property_automaton->states, cursor, automaton_state)
984     if (automaton_state->type == -1)
985       automaton_stack.push_back(automaton_state);
986   return automaton_stack;
987 }
988
989 int Api::compare_automaton_exp_label(const xbt_automaton_exp_label* l) const
990 {
991   unsigned int cursor                    = 0;
992   xbt_automaton_propositional_symbol_t p = nullptr;
993   xbt_dynar_foreach (simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
994     if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
995       return cursor;
996   }
997   return -1;
998 }
999
1000 void Api::set_property_automaton(xbt_automaton_state_t const& automaton_state) const
1001 {
1002   mc::property_automaton->current_state = automaton_state;
1003 }
1004
1005 xbt_automaton_exp_label_t Api::get_automaton_transition_label(xbt_dynar_t const& dynar, int index) const
1006 {
1007   const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
1008   return transition->label;
1009 }
1010
1011 xbt_automaton_state_t Api::get_automaton_transition_dst(xbt_dynar_t const& dynar, int index) const
1012 {
1013   const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
1014   return transition->dst;
1015 }
1016
1017 } // namespace mc
1018 } // namespace simgrid