Logo AND Algorithmique Numérique Distribuée

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