Logo AND Algorithmique Numérique Distribuée

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