Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Start to modernize the remaining old simcalls related to comms
[simgrid.git] / src / mc / api.cpp
1 /* Copyright (c) 2020-2022. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "api.hpp"
7
8 #include "src/kernel/activity/MailboxImpl.hpp"
9 #include "src/kernel/activity/MutexImpl.hpp"
10 #include "src/kernel/actor/SimcallObserver.hpp"
11 #include "src/mc/Session.hpp"
12 #include "src/mc/checker/Checker.hpp"
13 #include "src/mc/mc_base.hpp"
14 #include "src/mc/mc_comm_pattern.hpp"
15 #include "src/mc/mc_exit.hpp"
16 #include "src/mc/mc_pattern.hpp"
17 #include "src/mc/mc_private.hpp"
18 #include "src/mc/remote/RemoteProcess.hpp"
19 #include "src/surf/HostImpl.hpp"
20
21 #include <xbt/asserts.h>
22 #include <xbt/log.h>
23 #include "simgrid/s4u/Host.hpp"
24 #include "xbt/string.hpp"
25 #if HAVE_SMPI
26 #include "src/smpi/include/smpi_request.hpp"
27 #endif
28
29 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(Api, mc, "Logging specific to MC Facade APIs ");
30 XBT_LOG_EXTERNAL_CATEGORY(mc_global);
31
32 using Simcall = simgrid::simix::Simcall;
33
34 namespace simgrid {
35 namespace mc {
36
37 static inline const char* get_color(int id)
38 {
39   static constexpr std::array<const char*, 13> colors{{"blue", "red", "green3", "goldenrod", "brown", "purple",
40                                                        "magenta", "turquoise4", "gray25", "forestgreen", "hotpink",
41                                                        "lightblue", "tan"}};
42   return colors[id % colors.size()];
43 }
44
45 static std::string pointer_to_string(void* pointer)
46 {
47   return XBT_LOG_ISENABLED(Api, xbt_log_priority_verbose) ? xbt::string_printf("%p", pointer) : "(verbose only)";
48 }
49
50 static std::string buff_size_to_string(size_t buff_size)
51 {
52   return XBT_LOG_ISENABLED(Api, xbt_log_priority_verbose) ? std::to_string(buff_size) : "(verbose only)";
53 }
54
55 static void simcall_translate(smx_simcall_t req, Remote<kernel::activity::CommImpl>& buffered_comm);
56
57 static bool request_is_enabled_by_idx(const RemoteProcess& process, smx_simcall_t req, unsigned int idx)
58 {
59   kernel::activity::ActivityImpl* remote_act = nullptr;
60   if (auto wait = dynamic_cast<kernel::actor::ActivityWaitSimcall*>(req->observer_))
61     /* FIXME: check also that src and dst processes are not suspended */
62     remote_act = wait->get_activity();
63   else if (auto waitany = dynamic_cast<kernel::actor::ActivityWaitanySimcall*>(req->observer_))
64     remote_act = waitany->get_activities().at(idx);
65   else if (auto testany = dynamic_cast<kernel::actor::ActivityTestanySimcall*>(req->observer_))
66     remote_act = testany->get_activities().at(idx);
67
68   switch (req->call_) {
69     case Simcall::COMM_WAIT:
70     case Simcall::COMM_WAITANY:
71     case Simcall::COMM_TESTANY:
72       break;
73     default:
74       return true;
75   }
76
77   Remote<kernel::activity::CommImpl> temp_comm;
78   process.read(temp_comm, remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
79   const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
80   return comm->src_actor_.get() && comm->dst_actor_.get();
81 }
82
83 /* Search an enabled transition for the given process.
84  *
85  * This can be seen as an iterator returning the next transition of the process.
86  *
87  * We only consider the processes that are both
88  *  - marked "to be interleaved" in their ActorState (controlled by the checker algorithm).
89  *  - which simcall can currently be executed (like a comm where the other partner is already known)
90  * Once we returned the last enabled transition of a process, it is marked done.
91  *
92  * Things can get muddled with the WAITANY and TESTANY simcalls, that are rewritten on the fly to a bunch of WAIT
93  * (resp TEST) transitions using the transition.argument field to remember what was the last returned sub-transition.
94  */
95 static inline smx_simcall_t MC_state_choose_request_for_process(const RemoteProcess& process, simgrid::mc::State* state,
96                                                                 smx_actor_t actor)
97 {
98   /* reset the outgoing transition */
99   simgrid::mc::ActorState* procstate = &state->actor_states_[actor->get_pid()];
100   state->transition_.aid_              = -1;
101   state->transition_.times_considered_ = -1;
102   state->transition_.textual[0]        = '\0';
103   state->executed_req_.call_         = Simcall::NONE;
104
105   if (not simgrid::mc::actor_is_enabled(actor))
106     return nullptr; // Not executable in the application
107
108   smx_simcall_t req = nullptr;
109   if (actor->simcall_.observer_ != nullptr) {
110     state->transition_.times_considered_ = procstate->get_times_considered_and_inc();
111     if (actor->simcall_.mc_max_consider_ <= procstate->get_times_considered())
112       procstate->set_done();
113     req = &actor->simcall_;
114   } else
115     switch (actor->simcall_.call_) {
116       case Simcall::COMM_WAITANY:
117         while (procstate->get_times_considered() < simcall_comm_waitany__get__count(&actor->simcall_)) {
118           if (simgrid::mc::request_is_enabled_by_idx(process, &actor->simcall_, procstate->get_times_considered())) {
119             state->transition_.times_considered_ = procstate->get_times_considered_and_inc();
120             break;
121           }
122           procstate->get_times_considered_and_inc();
123         }
124
125         if (procstate->get_times_considered() >= simcall_comm_waitany__get__count(&actor->simcall_))
126           procstate->set_done();
127         if (state->transition_.times_considered_ != -1)
128           req = &actor->simcall_;
129         break;
130
131       case Simcall::COMM_TESTANY:
132         while (procstate->get_times_considered() < simcall_comm_testany__get__count(&actor->simcall_)) {
133           if (simgrid::mc::request_is_enabled_by_idx(process, &actor->simcall_, procstate->get_times_considered())) {
134             state->transition_.times_considered_ = procstate->get_times_considered_and_inc();
135             break;
136           }
137           procstate->get_times_considered_and_inc();
138         }
139
140         if (procstate->get_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->get_state() == simgrid::kernel::activity::State::READY &&
155                  act->detached())
156           state->transition_.times_considered_ = 0; // OK
157         procstate->set_done();
158         req = &actor->simcall_;
159         break;
160       }
161
162       default:
163         procstate->set_done();
164         state->transition_.times_considered_ = 0;
165         req                                  = &actor->simcall_;
166         break;
167     }
168   if (not req)
169     return nullptr;
170
171   state->transition_.aid_ = actor->get_pid();
172   state->executed_req_    = *req;
173
174   // Fetch the data of the request and translate it:
175   state->internal_req_ = *req;
176   state->internal_req_.mc_value_ = state->transition_.times_considered_;
177   simcall_translate(&state->internal_req_, state->internal_comm_);
178
179   return req;
180 }
181
182 static void simcall_translate(smx_simcall_t req,
183                               simgrid::mc::Remote<simgrid::kernel::activity::CommImpl>& buffered_comm)
184 {
185   simgrid::kernel::activity::CommImpl* chosen_comm;
186
187   /* The waitany and testany request are transformed into a wait or test request over the corresponding communication
188    * action so it can be treated later by the dependence function. */
189   switch (req->call_) {
190     case Simcall::COMM_WAITANY:
191       req->call_  = Simcall::COMM_WAIT;
192       chosen_comm =
193           mc_model_checker->get_remote_process().read(remote(simcall_comm_waitany__get__comms(req) + req->mc_value_));
194
195       mc_model_checker->get_remote_process().read(buffered_comm, remote(chosen_comm));
196       simcall_comm_wait__set__comm(req, buffered_comm.get_buffer());
197       simcall_comm_wait__set__timeout(req, 0);
198       break;
199
200     case Simcall::COMM_TESTANY:
201       req->call_  = Simcall::COMM_TEST;
202       chosen_comm =
203           mc_model_checker->get_remote_process().read(remote(simcall_comm_testany__get__comms(req) + req->mc_value_));
204
205       mc_model_checker->get_remote_process().read(buffered_comm, remote(chosen_comm));
206       simcall_comm_test__set__comm(req, buffered_comm.get_buffer());
207       simcall_comm_test__set__result(req, req->mc_value_);
208       break;
209
210     case Simcall::COMM_WAIT:
211       chosen_comm = simcall_comm_wait__get__comm(req);
212       mc_model_checker->get_remote_process().read(buffered_comm, remote(chosen_comm));
213       simcall_comm_wait__set__comm(req, buffered_comm.get_buffer());
214       break;
215
216     case Simcall::COMM_TEST:
217       chosen_comm = simcall_comm_test__get__comm(req);
218       mc_model_checker->get_remote_process().read(buffered_comm, remote(chosen_comm));
219       simcall_comm_test__set__comm(req, buffered_comm.get_buffer());
220       break;
221
222     default:
223       /* No translation needed */
224       break;
225   }
226 }
227
228 kernel::activity::CommImpl* Api::get_comm_or_nullptr(smx_simcall_t const r) const
229 {
230   if (auto wait = dynamic_cast<kernel::actor::ActivityWaitSimcall*>(r->observer_))
231     return static_cast<kernel::activity::CommImpl*>(wait->get_activity());
232   if (auto test = dynamic_cast<kernel::actor::ActivityTestSimcall*>(r->observer_))
233     return static_cast<kernel::activity::CommImpl*>(test->get_activity());
234   return nullptr;
235 }
236
237 /** Statically "upcast" a s_smx_actor_t into an ActorInformation
238  *
239  *  This gets 'actorInfo' from '&actorInfo->copy'. It upcasts in the
240  *  sense that we could achieve the same thing by having ActorInformation
241  *  inherit from s_smx_actor_t but we don't really want to do that.
242  */
243 simgrid::mc::ActorInformation* Api::actor_info_cast(smx_actor_t actor) const
244 {
245   simgrid::mc::ActorInformation temp;
246   std::size_t offset = (char*)temp.copy.get_buffer() - (char*)&temp;
247
248   auto* process_info = reinterpret_cast<simgrid::mc::ActorInformation*>((char*)actor - offset);
249   return process_info;
250 }
251
252 bool Api::simcall_check_dependency(smx_simcall_t req1, smx_simcall_t req2) const
253 {
254   const auto IRECV = Simcall::COMM_IRECV;
255   const auto ISEND = Simcall::COMM_ISEND;
256   const auto TEST  = Simcall::COMM_TEST;
257   const auto WAIT  = Simcall::COMM_WAIT;
258
259   if (req1->issuer_ == req2->issuer_)
260     return false;
261
262   /* The independence theorem only consider 4 simcalls. All others are dependent with anything. */
263   if (req1->call_ != ISEND && req1->call_ != IRECV && req1->call_ != TEST && req1->call_ != WAIT)
264     return true;
265   if (req2->call_ != ISEND && req2->call_ != IRECV && req2->call_ != TEST && req2->call_ != WAIT)
266     return true;
267
268   /* Timeouts in wait transitions are not considered by the independence theorem, thus assumed dependent */
269   if ((req1->call_ == WAIT && simcall_comm_wait__get__timeout(req1) > 0) ||
270       (req2->call_ == WAIT && simcall_comm_wait__get__timeout(req2) > 0))
271     return true;
272
273   /* Make sure that req1 and req2 are in alphabetic order */
274   if (req1->call_ > req2->call_) {
275     auto temp = req1;
276     req1      = req2;
277     req2      = temp;
278   }
279
280   auto comm1 = get_comm_or_nullptr(req1);
281   auto comm2 = get_comm_or_nullptr(req2);
282
283   /* First case: that's not the same kind of request (we also know that req1 < req2 alphabetically) */
284   if (req1->call_ != req2->call_) {
285     if (req1->call_ == IRECV && req2->call_ == ISEND)
286       return false;
287
288     if ((req1->call_ == IRECV || req1->call_ == ISEND) && req2->call_ == WAIT) {
289       auto mbox1 = get_mbox_remote_addr(req1);
290       auto mbox2 = remote(comm2->mbox_cpy);
291
292       if (mbox1 != mbox2 && simcall_comm_wait__get__timeout(req2) <= 0)
293         return false;
294
295       if ((req1->issuer_ != comm2->src_actor_.get()) && (req1->issuer_ != comm2->dst_actor_.get()) &&
296           simcall_comm_wait__get__timeout(req2) <= 0)
297         return false;
298
299       if ((req1->call_ == ISEND) && (comm2->type_ == kernel::activity::CommImpl::Type::SEND) &&
300           (comm2->src_buff_ != simcall_comm_isend__get__src_buff(req1)) && simcall_comm_wait__get__timeout(req2) <= 0)
301         return false;
302
303       if ((req1->call_ == IRECV) && (comm2->type_ == kernel::activity::CommImpl::Type::RECEIVE) &&
304           (comm2->dst_buff_ != simcall_comm_irecv__get__dst_buff(req1)) && simcall_comm_wait__get__timeout(req2) <= 0)
305         return false;
306     }
307
308     /* FIXME: the following rule assumes that the result of the isend/irecv call is not stored in a buffer used in the
309      * test call. */
310 #if 0
311   if((req1->call == ISEND || req1->call == IRECV)
312       &&  req2->call == TEST)
313     return false;
314 #endif
315
316     if (req1->call_ == TEST && req2->call_ == WAIT &&
317         (comm1->src_actor_.get() == nullptr || comm1->dst_actor_.get() == nullptr))
318       return false;
319
320     if (req1->call_ == TEST &&
321         (simcall_comm_test__get__comm(req1) == nullptr || comm1->src_buff_ == nullptr || comm1->dst_buff_ == nullptr))
322       return false;
323     if (req2->call_ == TEST &&
324         (simcall_comm_test__get__comm(req2) == nullptr || comm2->src_buff_ == nullptr || comm2->dst_buff_ == nullptr))
325       return false;
326
327     if (req1->call_ == TEST && req2->call_ == WAIT && comm1->src_buff_ == comm2->src_buff_ &&
328         comm1->dst_buff_ == comm2->dst_buff_)
329       return false;
330
331     if (req1->call_ == TEST && req2->call_ == WAIT && comm1->src_buff_ != nullptr && comm1->dst_buff_ != nullptr &&
332         comm2->src_buff_ != nullptr && comm2->dst_buff_ != nullptr && comm1->dst_buff_ != comm2->src_buff_ &&
333         comm1->dst_buff_ != comm2->dst_buff_ && comm2->dst_buff_ != comm1->src_buff_)
334       return false;
335
336     return true;
337   }
338
339   /* Second case: req1 and req2 are of the same call type */
340   switch (req1->call_) {
341     case ISEND:
342       return simcall_comm_isend__get__mbox(req1) == simcall_comm_isend__get__mbox(req2);
343     case IRECV:
344       return simcall_comm_irecv__get__mbox(req1) == simcall_comm_irecv__get__mbox(req2);
345     case WAIT:
346       if (comm1->src_buff_ == comm2->src_buff_ && comm1->dst_buff_ == comm2->dst_buff_)
347         return false;
348       if (comm1->src_buff_ != nullptr && comm1->dst_buff_ != nullptr && comm2->src_buff_ != nullptr &&
349           comm2->dst_buff_ != nullptr && comm1->dst_buff_ != comm2->src_buff_ && comm1->dst_buff_ != comm2->dst_buff_ &&
350           comm2->dst_buff_ != comm1->src_buff_)
351         return false;
352       return true;
353     default:
354       return true;
355   }
356 }
357
358 xbt::string const& Api::get_actor_host_name(smx_actor_t actor) const
359 {
360   if (mc_model_checker == nullptr)
361     return actor->get_host()->get_name();
362
363   const simgrid::mc::RemoteProcess* process = &mc_model_checker->get_remote_process();
364
365   // Read the simgrid::xbt::string in the MCed process:
366   simgrid::mc::ActorInformation* info = actor_info_cast(actor);
367
368   if (not info->hostname) {
369     Remote<s4u::Host> temp_host = process->read(remote(actor->get_host()));
370     auto remote_string_address  = remote(&xbt::string::to_string_data(temp_host.get_buffer()->get_impl()->get_name()));
371     simgrid::xbt::string_data remote_string = process->read(remote_string_address);
372     std::vector<char> hostname(remote_string.len + 1);
373     // no need to read the terminating null byte, and thus hostname[remote_string.len] is guaranteed to be '\0'
374     process->read_bytes(hostname.data(), remote_string.len, remote(remote_string.data));
375     info->hostname = &mc_model_checker->get_host_name(hostname.data());
376   }
377   return *info->hostname;
378 }
379
380 xbt::string const& Api::get_actor_name(smx_actor_t actor) const
381 {
382   if (mc_model_checker == nullptr)
383     return actor->get_name();
384
385   simgrid::mc::ActorInformation* info = actor_info_cast(actor);
386   if (info->name.empty()) {
387     const simgrid::mc::RemoteProcess* process = &mc_model_checker->get_remote_process();
388
389     simgrid::xbt::string_data string_data = simgrid::xbt::string::to_string_data(actor->name_);
390     info->name = process->read_string(remote(string_data.data), string_data.len);
391   }
392   return info->name;
393 }
394
395 std::string Api::get_actor_string(smx_actor_t actor) const
396 {
397   std::string res;
398   if (actor) {
399     res = "(" + std::to_string(actor->get_pid()) + ")";
400     if (actor->get_host())
401       res += std::string(get_actor_host_name(actor)) + " (" + std::string(get_actor_name(actor)) + ")";
402     else
403       res += get_actor_name(actor);
404   } else
405     res = "(0) ()";
406   return res;
407 }
408
409 std::string Api::get_actor_dot_label(smx_actor_t actor) const
410 {
411   std::string res = "(" + std::to_string(actor->get_pid()) + ")";
412   if (actor->get_host())
413     res += get_actor_host_name(actor);
414   return res;
415 }
416
417 simgrid::mc::Checker* Api::initialize(char** argv, simgrid::mc::CheckerAlgorithm algo) const
418 {
419   auto session = new simgrid::mc::Session([argv] {
420     int i = 1;
421     while (argv[i] != nullptr && argv[i][0] == '-')
422       i++;
423     xbt_assert(argv[i] != nullptr,
424                "Unable to find a binary to exec on the command line. Did you only pass config flags?");
425     execvp(argv[i], argv + i);
426     xbt_die("The model-checked process failed to exec(%s): %s", argv[i], strerror(errno));
427   });
428
429   simgrid::mc::Checker* checker;
430   switch (algo) {
431     case CheckerAlgorithm::CommDeterminism:
432       checker = simgrid::mc::create_communication_determinism_checker(session);
433       break;
434
435     case CheckerAlgorithm::UDPOR:
436       checker = simgrid::mc::create_udpor_checker(session);
437       break;
438
439     case CheckerAlgorithm::Safety:
440       checker = simgrid::mc::create_safety_checker(session);
441       break;
442
443     case CheckerAlgorithm::Liveness:
444       checker = simgrid::mc::create_liveness_checker(session);
445       break;
446
447     default:
448       THROW_IMPOSSIBLE;
449   }
450
451   // FIXME: session and checker are never deleted
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   Remote<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.get_buffer()->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     XBT_CINFO(mc_global, "**************************");
591     XBT_CINFO(mc_global, "*** DEADLOCK DETECTED ***");
592     XBT_CINFO(mc_global, "**************************");
593     XBT_CINFO(mc_global, "Counter-example execution trace:");
594     for (auto const& s : mc_model_checker->getChecker()->get_textual_trace())
595       XBT_CINFO(mc_global, "  %s", s.c_str());
596     simgrid::mc::dumpRecordPath();
597     simgrid::mc::session_singleton->log_state();
598     throw DeadlockError();
599   }
600 }
601
602 /** Get the issuer of a simcall (`req->issuer`)
603  *
604  *  In split-process mode, it does the black magic necessary to get an address
605  *  of a (shallow) copy of the data structure the issuer SIMIX actor in the local
606  *  address space.
607  *
608  *  @param process the MCed process
609  *  @param req     the simcall (copied in the local process)
610  */
611 smx_actor_t Api::simcall_get_issuer(s_smx_simcall const* req) const
612 {
613   xbt_assert(mc_model_checker != nullptr);
614
615   // This is the address of the smx_actor in the MCed process:
616   auto address = simgrid::mc::remote(req->issuer_);
617
618   // Lookup by address:
619   for (auto& actor : mc_model_checker->get_remote_process().actors())
620     if (actor.address == address)
621       return actor.copy.get_buffer();
622   for (auto& actor : mc_model_checker->get_remote_process().dead_actors())
623     if (actor.address == address)
624       return actor.copy.get_buffer();
625
626   xbt_die("Issuer not found");
627 }
628
629 RemotePtr<kernel::activity::MailboxImpl> Api::get_mbox_remote_addr(smx_simcall_t const req) const
630 {
631   if (req->call_ == Simcall::COMM_ISEND)
632     return remote(simcall_comm_isend__get__mbox(req));
633   if (req->call_ == Simcall::COMM_IRECV)
634     return remote(simcall_comm_irecv__get__mbox(req));
635   THROW_IMPOSSIBLE;
636 }
637
638 RemotePtr<kernel::activity::ActivityImpl> Api::get_comm_remote_addr(smx_simcall_t const req) const
639 {
640   if (req->call_ == Simcall::COMM_ISEND)
641     return remote(simcall_comm_isend__getraw__result(req));
642   if (req->call_ == Simcall::COMM_IRECV)
643     return remote(simcall_comm_irecv__getraw__result(req));
644   THROW_IMPOSSIBLE;
645 }
646
647 void Api::handle_simcall(Transition const& transition) const
648 {
649   mc_model_checker->handle_simcall(transition);
650 }
651
652 void Api::mc_wait_for_requests() const
653 {
654   mc_model_checker->wait_for_requests();
655 }
656
657 void Api::mc_exit(int status) const
658 {
659   mc_model_checker->exit(status);
660 }
661
662 void Api::dump_record_path() const
663 {
664   simgrid::mc::dumpRecordPath();
665 }
666
667 smx_simcall_t Api::mc_state_choose_request(simgrid::mc::State* state) const
668 {
669   RemoteProcess& process = mc_model_checker->get_remote_process();
670   for (auto& actor : process.actors()) {
671     /* Only consider the actors that were marked as interleaving by the checker algorithm */
672     if (not state->actor_states_[actor.copy.get_buffer()->get_pid()].is_todo())
673       continue;
674
675     smx_simcall_t res = MC_state_choose_request_for_process(process, state, actor.copy.get_buffer());
676     if (res)
677       return res;
678   }
679   return nullptr;
680 }
681
682 std::list<transition_detail_t> Api::get_enabled_transitions(simgrid::mc::State* state) const
683 {
684   std::list<transition_detail_t> tr_list{};
685
686   for (auto& actor : mc_model_checker->get_remote_process().actors()) {
687     auto actor_pid  = actor.copy.get_buffer()->get_pid();
688     auto actor_impl = actor.copy.get_buffer();
689
690     // Only consider the actors that were marked as interleaving by the checker algorithm
691     if (not state->actor_states_[actor_pid].is_todo())
692       continue;
693     // Not executable in the application
694     if (not simgrid::mc::actor_is_enabled(actor_impl))
695       continue;
696
697     auto transition       = std::make_unique<s_transition_detail>();
698     Simcall simcall_call  = actor_impl->simcall_.call_;
699     smx_simcall_t simcall = &actor_impl->simcall_;
700     transition->call_     = simcall_call;
701     switch (simcall_call) {
702       case Simcall::COMM_ISEND:
703       case Simcall::COMM_IRECV:
704         transition->mbox_remote_addr = get_mbox_remote_addr(simcall);
705         transition->comm_remote_addr = get_comm_remote_addr(simcall);
706         break;
707
708       default:
709         break;
710     }
711     tr_list.emplace_back(std::move(transition));
712   }
713
714   return tr_list;
715 }
716
717 std::string Api::request_to_string(smx_simcall_t req, int value) const
718 {
719   xbt_assert(mc_model_checker != nullptr, "Must be called from MCer");
720
721   std::string type;
722   std::string args;
723
724   smx_actor_t issuer = simcall_get_issuer(req);
725
726   if (issuer->simcall_.observer_ != nullptr)
727     return mc_model_checker->simcall_to_string(issuer->get_pid(), value);
728
729   switch (req->call_) {
730     case Simcall::COMM_ISEND:
731       type = "iSend";
732       args = "src=" + get_actor_string(issuer);
733       args += ", buff=" + pointer_to_string(simcall_comm_isend__get__src_buff(req));
734       args += ", size=" + buff_size_to_string(simcall_comm_isend__get__src_buff_size(req));
735       break;
736
737     case Simcall::COMM_IRECV: {
738       size_t* remote_size = simcall_comm_irecv__get__dst_buff_size(req);
739       size_t size         = 0;
740       if (remote_size)
741         mc_model_checker->get_remote_process().read_bytes(&size, sizeof(size), remote(remote_size));
742
743       type = "iRecv";
744       args = "dst=" + get_actor_string(issuer);
745       args += ", buff=" + pointer_to_string(simcall_comm_irecv__get__dst_buff(req));
746       args += ", size=" + buff_size_to_string(size);
747       break;
748     }
749
750     case Simcall::COMM_WAIT:
751       // See ActivityWaitSimcall::to_string(int times_considered)
752     case Simcall::COMM_TEST:
753       // See ActivityTestSimcall::to_string(int times_considered)
754     case Simcall::COMM_WAITANY:
755       // See ActivityWaitanySimcall::to_string(int times_considered)
756     case Simcall::COMM_TESTANY:
757       // See ActivityTestanySimcall::to_string(int times_considered)
758       break;
759
760     default:
761       type = SIMIX_simcall_name(*req);
762       args = "??";
763       break;
764   }
765
766   return "[" + get_actor_string(issuer) + "] " + type + "(" + args + ")";
767 }
768
769 std::string Api::request_get_dot_output(smx_simcall_t req, int value) const
770 {
771   const smx_actor_t issuer = simcall_get_issuer(req);
772   const char* color        = get_color(issuer->get_pid() - 1);
773
774   std::string label;
775
776   if (req->observer_ != nullptr) {
777     label = mc_model_checker->simcall_dot_label(issuer->get_pid(), value);
778   } else
779     switch (req->call_) {
780       case Simcall::COMM_ISEND:
781         label = "[" + get_actor_dot_label(issuer) + "] iSend";
782         break;
783
784       case Simcall::COMM_IRECV:
785         label = "[" + get_actor_dot_label(issuer) + "] iRecv";
786         break;
787
788       case Simcall::COMM_WAIT:
789         // See ActivityWaitSimcall::dot_label(int times_considered)
790       case Simcall::COMM_TEST:
791         // See ActivityTestSimcall::dot_label(int times_considered)
792       case Simcall::COMM_WAITANY:
793         // See ActivityWaittanySimcall::dot_label(int times_considered)
794       case Simcall::COMM_TESTANY:
795         // See ActivityTestanySimcall::dot_label(int times_considered)
796         break;
797
798       default:
799         THROW_UNIMPLEMENTED;
800     }
801
802   return "label = \"" + label + "\", color = " + color + ", fontcolor = " + color;
803 }
804
805 #if HAVE_SMPI
806 int Api::get_smpi_request_tag(smx_simcall_t const& simcall, simgrid::simix::Simcall type) const
807 {
808   void* simcall_data = nullptr;
809   if (type == Simcall::COMM_ISEND)
810     simcall_data = simcall_comm_isend__get__data(simcall);
811   else if (type == Simcall::COMM_IRECV)
812     simcall_data = simcall_comm_irecv__get__data(simcall);
813   Remote<simgrid::smpi::Request> mpi_request;
814   mc_model_checker->get_remote_process().read(mpi_request, remote(static_cast<smpi::Request*>(simcall_data)));
815   return mpi_request.get_buffer()->tag();
816 }
817 #endif
818
819 void Api::restore_state(std::shared_ptr<simgrid::mc::Snapshot> system_state) const
820 {
821   system_state->restore(&mc_model_checker->get_remote_process());
822 }
823
824 void Api::log_state() const
825 {
826   session_singleton->log_state();
827 }
828
829 bool Api::snapshot_equal(const Snapshot* s1, const Snapshot* s2) const
830 {
831   return simgrid::mc::snapshot_equal(s1, s2);
832 }
833
834 simgrid::mc::Snapshot* Api::take_snapshot(int num_state) const
835 {
836   auto snapshot = new simgrid::mc::Snapshot(num_state);
837   return snapshot;
838 }
839
840 void Api::s_close() const
841 {
842   session_singleton->close();
843 }
844
845 void Api::execute(Transition& transition, smx_simcall_t simcall) const
846 {
847   /* FIXME: once all simcalls have observers, kill the simcall parameter and use mc_model_checker->simcall_to_string() */
848   transition.textual = request_to_string(simcall, transition.times_considered_);
849   session_singleton->execute(transition);
850 }
851
852 void Api::automaton_load(const char* file) const
853 {
854   MC_automaton_load(file);
855 }
856
857 std::vector<int> Api::automaton_propositional_symbol_evaluate() const
858 {
859   unsigned int cursor = 0;
860   std::vector<int> values;
861   xbt_automaton_propositional_symbol_t ps = nullptr;
862   xbt_dynar_foreach (mc::property_automaton->propositional_symbols, cursor, ps)
863     values.push_back(xbt_automaton_propositional_symbol_evaluate(ps));
864   return values;
865 }
866
867 std::vector<xbt_automaton_state_t> Api::get_automaton_state() const
868 {
869   std::vector<xbt_automaton_state_t> automaton_stack;
870   unsigned int cursor = 0;
871   xbt_automaton_state_t automaton_state;
872   xbt_dynar_foreach (mc::property_automaton->states, cursor, automaton_state)
873     if (automaton_state->type == -1)
874       automaton_stack.push_back(automaton_state);
875   return automaton_stack;
876 }
877
878 int Api::compare_automaton_exp_label(const xbt_automaton_exp_label* l) const
879 {
880   unsigned int cursor                    = 0;
881   xbt_automaton_propositional_symbol_t p = nullptr;
882   xbt_dynar_foreach (simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
883     if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
884       return cursor;
885   }
886   return -1;
887 }
888
889 void Api::set_property_automaton(xbt_automaton_state_t const& automaton_state) const
890 {
891   mc::property_automaton->current_state = automaton_state;
892 }
893
894 xbt_automaton_exp_label_t Api::get_automaton_transition_label(xbt_dynar_t const& dynar, int index) const
895 {
896   const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
897   return transition->label;
898 }
899
900 xbt_automaton_state_t Api::get_automaton_transition_dst(xbt_dynar_t const& dynar, int index) const
901 {
902   const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
903   return transition->dst;
904 }
905
906 } // namespace mc
907 } // namespace simgrid