Logo AND Algorithmique Numérique Distribuée

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