Logo AND Algorithmique Numérique Distribuée

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