Logo AND Algorithmique Numérique Distribuée

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