Logo AND Algorithmique Numérique Distribuée

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