Logo AND Algorithmique Numérique Distribuée

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