Logo AND Algorithmique Numérique Distribuée

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