Logo AND Algorithmique Numérique Distribuée

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