Logo AND Algorithmique Numérique Distribuée

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