Logo AND Algorithmique Numérique Distribuée

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