Logo AND Algorithmique Numérique Distribuée

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