Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
bcb4f6663d00a7259a81ff8b4ad5736540701dcd
[simgrid.git] / src / mc / mc_api.cpp
1 #include "mc_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/mc_comm_pattern.hpp"
7 #include "src/mc/mc_private.hpp"
8 #include "src/mc/mc_smx.hpp"
9 #include "src/mc/remote/RemoteSimulation.hpp"
10 #include "src/mc/mc_pattern.hpp"
11 #include "src/mc/checker/SimcallInspector.hpp"
12 #include <xbt/asserts.h>
13 #include <xbt/log.h>
14 // #include <xbt/dynar.h>
15
16 #if HAVE_SMPI
17 #include "src/smpi/include/smpi_request.hpp"
18 #endif
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_api, mc, "Logging specific to MC Fasade APIs ");
21
22 using Simcall = simgrid::simix::Simcall;
23
24 namespace simgrid {
25 namespace mc {
26
27 static inline const char* get_color(int id)
28 {
29   static constexpr std::array<const char*, 13> colors{{"blue", "red", "green3", "goldenrod", "brown", "purple",
30                                                        "magenta", "turquoise4", "gray25", "forestgreen", "hotpink",
31                                                        "lightblue", "tan"}};
32   return colors[id % colors.size()];
33 }
34
35 static char *pointer_to_string(void *pointer)
36 {
37   if (XBT_LOG_ISENABLED(mc_api, xbt_log_priority_verbose))
38     return bprintf("%p", pointer);
39
40   return xbt_strdup("(verbose only)");
41 }
42
43 static char *buff_size_to_string(size_t buff_size)
44 {
45   if (XBT_LOG_ISENABLED(mc_api, xbt_log_priority_verbose))
46     return bprintf("%zu", buff_size);
47
48   return xbt_strdup("(verbose only)");
49 }
50
51 /* Search an enabled transition for the given process.
52  *
53  * This can be seen as an iterator returning the next transition of the process.
54  *
55  * We only consider the processes that are both
56  *  - marked "to be interleaved" in their ActorState (controlled by the checker algorithm).
57  *  - which simcall can currently be executed (like a comm where the other partner is already known)
58  * Once we returned the last enabled transition of a process, it is marked done.
59  *
60  * Things can get muddled with the WAITANY and TESTANY simcalls, that are rewritten on the fly to a bunch of WAIT
61  * (resp TEST) transitions using the transition.argument field to remember what was the last returned sub-transition.
62  */
63 static inline smx_simcall_t MC_state_choose_request_for_process(simgrid::mc::State* state, smx_actor_t actor)
64 {
65   /* reset the outgoing transition */
66   simgrid::mc::ActorState* procstate = &state->actor_states_[actor->get_pid()];
67   state->transition_.pid_            = -1;
68   state->transition_.argument_       = -1;
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   switch (actor->simcall_.call_) {
76     case Simcall::COMM_WAITANY:
77       state->transition_.argument_ = -1;
78       while (procstate->times_considered < simcall_comm_waitany__get__count(&actor->simcall_)) {
79         if (simgrid::mc::request_is_enabled_by_idx(&actor->simcall_, procstate->times_considered)) {
80           state->transition_.argument_ = procstate->times_considered;
81           ++procstate->times_considered;
82           break;
83         }
84         ++procstate->times_considered;
85       }
86
87       if (procstate->times_considered >= simcall_comm_waitany__get__count(&actor->simcall_))
88         procstate->set_done();
89       if (state->transition_.argument_ != -1)
90         req = &actor->simcall_;
91       break;
92
93     case Simcall::COMM_TESTANY: {
94       unsigned start_count         = procstate->times_considered;
95       state->transition_.argument_ = -1;
96       while (procstate->times_considered < simcall_comm_testany__get__count(&actor->simcall_)) {
97         if (simgrid::mc::request_is_enabled_by_idx(&actor->simcall_, procstate->times_considered)) {
98           state->transition_.argument_ = procstate->times_considered;
99           ++procstate->times_considered;
100           break;
101         }
102         ++procstate->times_considered;
103       }
104
105       if (procstate->times_considered >= simcall_comm_testany__get__count(&actor->simcall_))
106         procstate->set_done();
107
108       if (state->transition_.argument_ != -1 || start_count == 0)
109         req = &actor->simcall_;
110
111       break;
112     }
113
114     case Simcall::COMM_WAIT: {
115       simgrid::mc::RemotePtr<simgrid::kernel::activity::CommImpl> remote_act =
116           remote(simcall_comm_wait__getraw__comm(&actor->simcall_));
117       simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_act;
118       mc_model_checker->get_remote_simulation().read(temp_act, remote_act);
119       const simgrid::kernel::activity::CommImpl* act = temp_act.get_buffer();
120       if (act->src_actor_.get() && act->dst_actor_.get())
121         state->transition_.argument_ = 0; // OK
122       else if (act->src_actor_.get() == nullptr && act->type_ == simgrid::kernel::activity::CommImpl::Type::READY &&
123                act->detached())
124         state->transition_.argument_ = 0; // OK
125       else
126         state->transition_.argument_ = -1; // timeout
127       procstate->set_done();
128       req = &actor->simcall_;
129       break;
130     }
131
132     case Simcall::MC_RANDOM: {
133       int min_value                = simcall_mc_random__get__min(&actor->simcall_);
134       state->transition_.argument_ = procstate->times_considered + min_value;
135       procstate->times_considered++;
136       if (state->transition_.argument_ == simcall_mc_random__get__max(&actor->simcall_))
137         procstate->set_done();
138       req = &actor->simcall_;
139       break;
140     }
141
142     default:
143       procstate->set_done();
144       state->transition_.argument_ = 0;
145       req                          = &actor->simcall_;
146       break;
147   }
148   if (not req)
149     return nullptr;
150
151   state->transition_.pid_ = actor->get_pid();
152   state->executed_req_    = *req;
153   // Fetch the data of the request and translate it:
154   state->internal_req_ = *req;
155
156   /* The waitany and testany request are transformed into a wait or test request over the corresponding communication
157    * action so it can be treated later by the dependence function. */
158   switch (req->call_) {
159     case Simcall::COMM_WAITANY: {
160       state->internal_req_.call_ = Simcall::COMM_WAIT;
161       simgrid::kernel::activity::CommImpl* remote_comm;
162       remote_comm = mc_model_checker->get_remote_simulation().read(
163           remote(simcall_comm_waitany__get__comms(req) + state->transition_.argument_));
164       mc_model_checker->get_remote_simulation().read(state->internal_comm_, remote(remote_comm));
165       simcall_comm_wait__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
166       simcall_comm_wait__set__timeout(&state->internal_req_, 0);
167       break;
168     }
169
170     case Simcall::COMM_TESTANY:
171       state->internal_req_.call_ = Simcall::COMM_TEST;
172
173       if (state->transition_.argument_ > 0) {
174         simgrid::kernel::activity::CommImpl* remote_comm = mc_model_checker->get_remote_simulation().read(
175             remote(simcall_comm_testany__get__comms(req) + state->transition_.argument_));
176         mc_model_checker->get_remote_simulation().read(state->internal_comm_, remote(remote_comm));
177       }
178
179       simcall_comm_test__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
180       simcall_comm_test__set__result(&state->internal_req_, state->transition_.argument_);
181       break;
182
183     case Simcall::COMM_WAIT:
184       mc_model_checker->get_remote_simulation().read_bytes(&state->internal_comm_, sizeof(state->internal_comm_),
185                                                            remote(simcall_comm_wait__getraw__comm(req)));
186       simcall_comm_wait__set__comm(&state->executed_req_, state->internal_comm_.get_buffer());
187       simcall_comm_wait__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
188       break;
189
190     case Simcall::COMM_TEST:
191       mc_model_checker->get_remote_simulation().read_bytes(&state->internal_comm_, sizeof(state->internal_comm_),
192                                                            remote(simcall_comm_test__getraw__comm(req)));
193       simcall_comm_test__set__comm(&state->executed_req_, state->internal_comm_.get_buffer());
194       simcall_comm_test__set__comm(&state->internal_req_, state->internal_comm_.get_buffer());
195       break;
196
197     default:
198       /* No translation needed */
199       break;
200   }
201
202   return req;
203 }
204
205 void mc_api::initialize(char** argv) const
206 {
207   simgrid::mc::session = new simgrid::mc::Session([argv] {
208     int i = 1;
209     while (argv[i] != nullptr && argv[i][0] == '-')
210       i++;
211     xbt_assert(argv[i] != nullptr,
212                "Unable to find a binary to exec on the command line. Did you only pass config flags?");
213     execvp(argv[i], argv + i);
214     xbt_die("The model-checked process failed to exec(): %s", strerror(errno));
215   });
216 }
217
218 std::vector<simgrid::mc::ActorInformation>& mc_api::get_actors() const
219 {
220   return mc_model_checker->get_remote_simulation().actors();
221 }
222
223 bool mc_api::actor_is_enabled(aid_t pid) const
224 {
225   return session->actor_is_enabled(pid);
226 }
227
228 unsigned long mc_api::get_maxpid() const
229 {
230   return MC_smx_get_maxpid();
231 }
232
233 int mc_api::get_actors_size() const
234 {
235   return mc_model_checker->get_remote_simulation().actors().size();
236 }
237
238 bool mc_api::comm_addr_equal(const kernel::activity::CommImpl* comm_addr1, const kernel::activity::CommImpl* comm_addr2) const
239 {
240   return remote(comm_addr1) == remote(comm_addr2);
241 }
242
243 kernel::activity::CommImpl* mc_api::get_comm_isend_raw_addr(smx_simcall_t request) const
244 {
245   auto comm_addr = simcall_comm_isend__getraw__result(request);
246   return static_cast<kernel::activity::CommImpl*>(comm_addr);
247 }
248
249 kernel::activity::CommImpl* mc_api::get_comm_wait_raw_addr(smx_simcall_t request) const
250 {
251   return simcall_comm_wait__getraw__comm(request);
252 }
253
254 kernel::activity::CommImpl* mc_api::get_comm_waitany_raw_addr(smx_simcall_t request, int value) const
255 {
256   auto addr = mc_model_checker->get_remote_simulation().read(remote(simcall_comm_waitany__getraw__comms(request) + value));
257   return static_cast<simgrid::kernel::activity::CommImpl*>(addr);
258 }
259
260 std::string mc_api::get_pattern_comm_rdv(void* addr) const
261 {
262   Remote<kernel::activity::CommImpl> temp_synchro;
263   mc_model_checker->get_remote_simulation().read(temp_synchro, remote((simgrid::kernel::activity::CommImpl*)addr));
264   const kernel::activity::CommImpl* synchro = temp_synchro.get_buffer();
265
266   char* remote_name = mc_model_checker->get_remote_simulation().read<char*>(RemotePtr<char*>(
267       (uint64_t)(synchro->get_mailbox() ? &synchro->get_mailbox()->get_name() : &synchro->mbox_cpy->get_name())));
268   auto rdv = mc_model_checker->get_remote_simulation().read_string(RemotePtr<char>(remote_name));
269   return rdv;
270 }
271
272 unsigned long mc_api::get_pattern_comm_src_proc(void* addr) const
273 {
274   Remote<kernel::activity::CommImpl> temp_synchro;
275   mc_model_checker->get_remote_simulation().read(temp_synchro, remote((simgrid::kernel::activity::CommImpl*)addr));
276   const kernel::activity::CommImpl* synchro = temp_synchro.get_buffer();
277   auto src_proc = mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(synchro->src_actor_.get()))->get_pid();
278   return src_proc;
279 }
280
281 unsigned long mc_api::get_pattern_comm_dst_proc(void* addr) const
282 {
283   Remote<kernel::activity::CommImpl> temp_synchro;
284   mc_model_checker->get_remote_simulation().read(temp_synchro, remote((simgrid::kernel::activity::CommImpl*)addr));
285   const kernel::activity::CommImpl* synchro = temp_synchro.get_buffer();
286   auto src_proc = mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(synchro->dst_actor_.get()))->get_pid();
287   return src_proc;
288 }
289
290 std::vector<char> mc_api::get_pattern_comm_data(void* addr) const
291 {
292   Remote<kernel::activity::CommImpl> temp_synchro;
293   mc_model_checker->get_remote_simulation().read(temp_synchro, remote((simgrid::kernel::activity::CommImpl*)addr));
294   const kernel::activity::CommImpl* synchro = temp_synchro.get_buffer();
295
296   std::vector<char> buffer {};
297   if (synchro->src_buff_ != nullptr) {
298     buffer.resize(synchro->src_buff_size_);
299     mc_model_checker->get_remote_simulation().read_bytes(buffer.data(), buffer.size(),
300                                                          remote(synchro->src_buff_));
301   }
302   return buffer;
303 }
304
305 std::vector<char> mc_api::get_pattern_comm_data(const kernel::activity::CommImpl* comm_addr) const
306 {
307   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
308   mc_model_checker->get_remote_simulation().read(temp_comm, remote((kernel::activity::CommImpl*)comm_addr));
309   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
310   
311   std::vector<char> buffer {};
312   if (comm->src_buff_ != nullptr) {
313     buffer.resize(comm->src_buff_size_);
314     mc_model_checker->get_remote_simulation().read_bytes(buffer.data(), buffer.size(),
315                                                          remote(comm->src_buff_));
316   }
317   return buffer;
318 }
319
320 const char* mc_api::get_actor_host_name(smx_actor_t actor) const
321 {
322   const char* host_name = MC_smx_actor_get_host_name(actor);
323   return host_name;
324 }
325
326 #if HAVE_SMPI
327 bool mc_api::check_send_request_detached(smx_simcall_t const& simcall) const
328 {
329   simgrid::smpi::Request mpi_request;
330   mc_model_checker->get_remote_simulation().read(
331       &mpi_request, remote(static_cast<smpi::Request*>(simcall_comm_isend__get__data(simcall))));
332   return mpi_request.detached();
333 }
334 #endif
335
336 smx_actor_t mc_api::get_src_actor(const kernel::activity::CommImpl* comm_addr) const
337 {
338   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
339   mc_model_checker->get_remote_simulation().read(temp_comm, remote((kernel::activity::CommImpl*)comm_addr));
340   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
341
342   auto src_proc = mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(comm->src_actor_.get()));
343   return src_proc;
344 }
345
346 smx_actor_t mc_api::get_dst_actor(const kernel::activity::CommImpl* comm_addr) const
347 {
348   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
349   mc_model_checker->get_remote_simulation().read(temp_comm, remote((kernel::activity::CommImpl*)comm_addr));
350   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
351
352   auto dst_proc = mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(comm->dst_actor_.get()));
353   return dst_proc;
354 }
355
356 std::size_t mc_api::get_remote_heap_bytes() const
357 {
358   RemoteSimulation& process = mc_model_checker->get_remote_simulation();
359   auto heap_bytes_used      = mmalloc_get_bytes_used_remote(process.get_heap()->heaplimit, process.get_malloc_info());
360   return heap_bytes_used;
361 }
362
363 void mc_api::session_initialize() const
364 {
365   session->initialize();
366 }
367
368 ModelChecker* mc_api::get_model_checker() const
369 {
370   return mc_model_checker;
371 }
372
373 void mc_api::mc_inc_visited_states() const
374 {
375   mc_model_checker->visited_states++;
376 }
377
378 void mc_api::mc_inc_executed_trans() const
379 {
380   mc_model_checker->executed_transitions++;
381 }
382
383 unsigned long mc_api::mc_get_visited_states() const
384 {
385   return mc_model_checker->visited_states;
386 }
387
388 unsigned long mc_api::mc_get_executed_trans() const
389 {
390   return mc_model_checker->executed_transitions;
391 }
392
393 bool mc_api::mc_check_deadlock() const
394 {
395   return mc_model_checker->checkDeadlock();
396 }
397
398 void mc_api::mc_show_deadlock() const
399 {
400   MC_show_deadlock();
401 }
402
403 smx_actor_t mc_api::simcall_get_issuer(s_smx_simcall const* req) const
404 {
405   return MC_smx_simcall_get_issuer(req);
406 }
407
408 bool mc_api::mc_is_null() const
409 {
410   auto is_null = (mc_model_checker == nullptr) ? true : false;
411   return is_null;
412 }
413
414 Checker* mc_api::mc_get_checker() const
415 {
416   return mc_model_checker->getChecker();
417 }
418
419 void mc_api::set_checker(Checker* const checker) const
420 {
421   xbt_assert(mc_model_checker);
422   xbt_assert(mc_model_checker->getChecker() == nullptr);
423   mc_model_checker->setChecker(checker);
424 }
425
426 RemoteSimulation& mc_api::mc_get_remote_simulation() const
427 {
428   return mc_model_checker->get_remote_simulation();
429 }
430
431 void mc_api::handle_simcall(Transition const& transition) const
432 {
433   mc_model_checker->handle_simcall(transition);
434 }
435
436 void mc_api::mc_wait_for_requests() const
437 {
438   mc_model_checker->wait_for_requests();
439 }
440
441 void mc_api::mc_exit(int status) const
442 {
443   mc_model_checker->exit(status);
444 }
445
446 std::string const& mc_api::mc_get_host_name(std::string const& hostname) const
447 {
448   return mc_model_checker->get_host_name(hostname);
449 }
450
451 void mc_api::dump_record_path() const
452 {
453   simgrid::mc::dumpRecordPath();
454 }
455
456 smx_simcall_t mc_api::mc_state_choose_request(simgrid::mc::State* state) const
457 {
458   for (auto& actor : mc_model_checker->get_remote_simulation().actors()) {
459     /* Only consider the actors that were marked as interleaving by the checker algorithm */
460     if (not state->actor_states_[actor.copy.get_buffer()->get_pid()].is_todo())
461       continue;
462
463     smx_simcall_t res = MC_state_choose_request_for_process(state, actor.copy.get_buffer());
464     if (res)
465       return res;
466   }
467   return nullptr;
468 }
469
470 bool mc_api::request_depend(smx_simcall_t req1, smx_simcall_t req2) const
471 {
472   return simgrid::mc::request_depend(req1, req2);
473 }
474
475 std::string mc_api::request_to_string(smx_simcall_t req, int value, RequestType request_type) const
476 {
477   xbt_assert(mc_model_checker != nullptr, "Must be called from MCer");
478
479   if (req->inspector_ != nullptr)
480     return req->inspector_->to_string();
481
482   bool use_remote_comm = true;
483   switch(request_type) {
484   case simgrid::mc::RequestType::simix:
485     use_remote_comm = true;
486     break;
487   case simgrid::mc::RequestType::executed:
488   case simgrid::mc::RequestType::internal:
489     use_remote_comm = false;
490     break;
491   default:
492     THROW_IMPOSSIBLE;
493   }
494
495   const char* type = nullptr;
496   char *args = nullptr;
497
498   smx_actor_t issuer = MC_smx_simcall_get_issuer(req);
499
500   switch (req->call_) {
501     case Simcall::COMM_ISEND: {
502       type     = "iSend";
503       char* p  = pointer_to_string(simcall_comm_isend__get__src_buff(req));
504       char* bs = buff_size_to_string(simcall_comm_isend__get__src_buff_size(req));
505       if (issuer->get_host())
506         args = bprintf("src=(%ld)%s (%s), buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
507                        MC_smx_actor_get_name(issuer), p, bs);
508       else
509         args = bprintf("src=(%ld)%s, buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_name(issuer), p, bs);
510       xbt_free(bs);
511       xbt_free(p);
512       break;
513     }
514
515     case Simcall::COMM_IRECV: {
516       size_t* remote_size = simcall_comm_irecv__get__dst_buff_size(req);
517       size_t size         = 0;
518       if (remote_size)
519         mc_model_checker->get_remote_simulation().read_bytes(&size, sizeof(size), remote(remote_size));
520
521       type     = "iRecv";
522       char* p  = pointer_to_string(simcall_comm_irecv__get__dst_buff(req));
523       char* bs = buff_size_to_string(size);
524       if (issuer->get_host())
525         args = bprintf("dst=(%ld)%s (%s), buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
526                        MC_smx_actor_get_name(issuer), p, bs);
527       else
528         args = bprintf("dst=(%ld)%s, buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_name(issuer), p, bs);
529       xbt_free(bs);
530       xbt_free(p);
531       break;
532     }
533
534     case Simcall::COMM_WAIT: {
535       simgrid::kernel::activity::CommImpl* remote_act = simcall_comm_wait__getraw__comm(req);
536       char* p;
537       if (value == -1) {
538         type = "WaitTimeout";
539         p    = pointer_to_string(remote_act);
540         args = bprintf("comm=%s", p);
541       } else {
542         type = "Wait";
543         p    = pointer_to_string(remote_act);
544
545         simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
546         const simgrid::kernel::activity::CommImpl* act;
547         if (use_remote_comm) {
548           mc_model_checker->get_remote_simulation().read(temp_synchro, remote(remote_act));
549           act = temp_synchro.get_buffer();
550         } else
551           act = remote_act;
552
553         smx_actor_t src_proc =
554             mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
555         smx_actor_t dst_proc =
556             mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
557         args                 = bprintf("comm=%s [(%ld)%s (%s)-> (%ld)%s (%s)]", p, src_proc ? src_proc->get_pid() : 0,
558                        src_proc ? MC_smx_actor_get_host_name(src_proc) : "",
559                        src_proc ? MC_smx_actor_get_name(src_proc) : "", dst_proc ? dst_proc->get_pid() : 0,
560                        dst_proc ? MC_smx_actor_get_host_name(dst_proc) : "",
561                        dst_proc ? MC_smx_actor_get_name(dst_proc) : "");
562       }
563       xbt_free(p);
564       break;
565     }
566
567     case Simcall::COMM_TEST: {
568       simgrid::kernel::activity::CommImpl* remote_act = simcall_comm_test__getraw__comm(req);
569       simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
570       const simgrid::kernel::activity::CommImpl* act;
571       if (use_remote_comm) {
572         mc_model_checker->get_remote_simulation().read(temp_synchro, remote(remote_act));
573         act = temp_synchro.get_buffer();
574       } else
575         act = remote_act;
576
577       char* p;
578       if (act->src_actor_.get() == nullptr || act->dst_actor_.get() == nullptr) {
579         type = "Test FALSE";
580         p    = pointer_to_string(remote_act);
581         args = bprintf("comm=%s", p);
582       } else {
583         type = "Test TRUE";
584         p    = pointer_to_string(remote_act);
585
586         smx_actor_t src_proc =
587             mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
588         smx_actor_t dst_proc =
589             mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
590         args                 = bprintf("comm=%s [(%ld)%s (%s) -> (%ld)%s (%s)]", p, src_proc->get_pid(),
591                        MC_smx_actor_get_name(src_proc), MC_smx_actor_get_host_name(src_proc), dst_proc->get_pid(),
592                        MC_smx_actor_get_name(dst_proc), MC_smx_actor_get_host_name(dst_proc));
593       }
594       xbt_free(p);
595       break;
596     }
597
598     case Simcall::COMM_WAITANY: {
599       type         = "WaitAny";
600       size_t count = simcall_comm_waitany__get__count(req);
601       if (count > 0) {
602         simgrid::kernel::activity::CommImpl* remote_sync;
603         remote_sync =
604             mc_model_checker->get_remote_simulation().read(remote(simcall_comm_waitany__get__comms(req) + value));
605         char* p     = pointer_to_string(remote_sync);
606         args        = bprintf("comm=%s (%d of %zu)", p, value + 1, count);
607         xbt_free(p);
608       } else
609         args = bprintf("comm at idx %d", value);
610       break;
611     }
612
613     case Simcall::COMM_TESTANY:
614       if (value == -1) {
615         type = "TestAny FALSE";
616         args = xbt_strdup("-");
617       } else {
618         type = "TestAny";
619         args = bprintf("(%d of %zu)", value + 1, simcall_comm_testany__get__count(req));
620       }
621       break;
622
623     case Simcall::MUTEX_TRYLOCK:
624     case Simcall::MUTEX_LOCK: {
625       if (req->call_ == Simcall::MUTEX_LOCK)
626         type = "Mutex LOCK";
627       else
628         type = "Mutex TRYLOCK";
629
630       simgrid::mc::Remote<simgrid::kernel::activity::MutexImpl> mutex;
631       mc_model_checker->get_remote_simulation().read_bytes(mutex.get_buffer(), sizeof(mutex),
632                                                            remote(req->call_ == Simcall::MUTEX_LOCK
633                                                                       ? simcall_mutex_lock__get__mutex(req)
634                                                                       : simcall_mutex_trylock__get__mutex(req)));
635       args = bprintf("locked = %d, owner = %d, sleeping = n/a", mutex.get_buffer()->is_locked(),
636                      mutex.get_buffer()->get_owner() != nullptr
637                          ? (int)mc_model_checker->get_remote_simulation()
638                                .resolve_actor(simgrid::mc::remote(mutex.get_buffer()->get_owner()))
639                                ->get_pid()
640                          : -1);
641       break;
642     }
643
644     case Simcall::MC_RANDOM:
645       type = "MC_RANDOM";
646       args = bprintf("%d", value);
647       break;
648
649     default:
650       type = SIMIX_simcall_name(req->call_);
651       args = bprintf("??");
652       break;
653   }
654
655   std::string str;
656   if (args != nullptr)
657     str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s(%s)", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
658                                       MC_smx_actor_get_name(issuer), type, args);
659   else
660     str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s ", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
661                                       MC_smx_actor_get_name(issuer), type);
662   xbt_free(args);
663   return str;  
664 }
665
666 std::string mc_api::request_get_dot_output(smx_simcall_t req, int value) const
667 {
668   const smx_actor_t issuer = MC_smx_simcall_get_issuer(req);
669   const char* color        = get_color(issuer->get_pid() - 1);
670
671   if (req->inspector_ != nullptr)
672     return simgrid::xbt::string_printf("label = \"%s\", color = %s, fontcolor = %s",
673                                        req->inspector_->dot_label().c_str(), color, color);
674
675   std::string label;
676
677   switch (req->call_) {
678     case Simcall::COMM_ISEND:
679       if (issuer->get_host())
680         label = xbt::string_printf("[(%ld)%s] iSend", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
681       else
682         label = bprintf("[(%ld)] iSend", issuer->get_pid());
683       break;
684
685     case Simcall::COMM_IRECV:
686       if (issuer->get_host())
687         label = xbt::string_printf("[(%ld)%s] iRecv", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
688       else
689         label = xbt::string_printf("[(%ld)] iRecv", issuer->get_pid());
690       break;
691
692     case Simcall::COMM_WAIT:
693       if (value == -1) {
694         if (issuer->get_host())
695           label = xbt::string_printf("[(%ld)%s] WaitTimeout", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
696         else
697           label = xbt::string_printf("[(%ld)] WaitTimeout", issuer->get_pid());
698       } else {
699         kernel::activity::ActivityImpl* remote_act = simcall_comm_wait__getraw__comm(req);
700         Remote<kernel::activity::CommImpl> temp_comm;
701         mc_model_checker->get_remote_simulation().read(temp_comm,
702                                                        remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
703         const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
704
705         const kernel::actor::ActorImpl* src_proc =
706             mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(comm->src_actor_.get()));
707         const kernel::actor::ActorImpl* dst_proc =
708             mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(comm->dst_actor_.get()));
709         if (issuer->get_host())
710           label =
711               xbt::string_printf("[(%ld)%s] Wait [(%ld)->(%ld)]", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
712                                  src_proc ? src_proc->get_pid() : 0, dst_proc ? dst_proc->get_pid() : 0);
713         else
714           label = xbt::string_printf("[(%ld)] Wait [(%ld)->(%ld)]", issuer->get_pid(),
715                                      src_proc ? src_proc->get_pid() : 0, dst_proc ? dst_proc->get_pid() : 0);
716       }
717       break;
718
719     case Simcall::COMM_TEST: {
720       kernel::activity::ActivityImpl* remote_act = simcall_comm_test__getraw__comm(req);
721       Remote<simgrid::kernel::activity::CommImpl> temp_comm;
722       mc_model_checker->get_remote_simulation().read(temp_comm,
723                                                      remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
724       const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
725       if (comm->src_actor_.get() == nullptr || comm->dst_actor_.get() == nullptr) {
726         if (issuer->get_host())
727           label = xbt::string_printf("[(%ld)%s] Test FALSE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
728         else
729           label = bprintf("[(%ld)] Test FALSE", issuer->get_pid());
730       } else {
731         if (issuer->get_host())
732           label = xbt::string_printf("[(%ld)%s] Test TRUE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
733         else
734           label = xbt::string_printf("[(%ld)] Test TRUE", issuer->get_pid());
735       }
736       break;
737     }
738
739     case Simcall::COMM_WAITANY: {
740       size_t comms_size = simcall_comm_waitany__get__count(req);
741       if (issuer->get_host())
742         label = xbt::string_printf("[(%ld)%s] WaitAny [%d of %zu]", issuer->get_pid(),
743                                    MC_smx_actor_get_host_name(issuer), value + 1, comms_size);
744       else
745         label = xbt::string_printf("[(%ld)] WaitAny [%d of %zu]", issuer->get_pid(), value + 1, comms_size);
746       break;
747     }
748
749     case Simcall::COMM_TESTANY:
750       if (value == -1) {
751         if (issuer->get_host())
752           label = xbt::string_printf("[(%ld)%s] TestAny FALSE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
753         else
754           label = xbt::string_printf("[(%ld)] TestAny FALSE", issuer->get_pid());
755       } else {
756         if (issuer->get_host())
757           label =
758               xbt::string_printf("[(%ld)%s] TestAny TRUE [%d of %lu]", issuer->get_pid(),
759                                  MC_smx_actor_get_host_name(issuer), value + 1, simcall_comm_testany__get__count(req));
760         else
761           label = xbt::string_printf("[(%ld)] TestAny TRUE [%d of %lu]", issuer->get_pid(), value + 1,
762                                      simcall_comm_testany__get__count(req));
763       }
764       break;
765
766     case Simcall::MUTEX_TRYLOCK:
767       label = xbt::string_printf("[(%ld)] Mutex TRYLOCK", issuer->get_pid());
768       break;
769
770     case Simcall::MUTEX_LOCK:
771       label = xbt::string_printf("[(%ld)] Mutex LOCK", issuer->get_pid());
772       break;
773
774     case Simcall::MC_RANDOM:
775       if (issuer->get_host())
776         label = xbt::string_printf("[(%ld)%s] MC_RANDOM (%d)", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
777                                    value);
778       else
779         label = xbt::string_printf("[(%ld)] MC_RANDOM (%d)", issuer->get_pid(), value);
780       break;
781
782     default:
783       THROW_UNIMPLEMENTED;
784   }
785
786   return xbt::string_printf("label = \"%s\", color = %s, fontcolor = %s", label.c_str(), color, color);  
787 }
788
789 const char* mc_api::simcall_get_name(simgrid::simix::Simcall kind) const
790 {
791   return SIMIX_simcall_name(kind);
792 }
793
794 #if HAVE_SMPI
795 int mc_api::get_smpi_request_tag(smx_simcall_t const& simcall, simgrid::simix::Simcall type) const
796 {
797   simgrid::smpi::Request mpi_request;
798   void* simcall_data = nullptr;
799   if (type == Simcall::COMM_ISEND)
800     simcall_data = simcall_comm_isend__get__data(simcall);
801   else if (type == Simcall::COMM_IRECV)
802     simcall_data = simcall_comm_irecv__get__data(simcall);
803   mc_model_checker->get_remote_simulation().read(&mpi_request, remote(static_cast<smpi::Request*>(simcall_data)));
804   return mpi_request.tag();
805 }
806 #endif
807
808 void mc_api::restore_state(std::shared_ptr<simgrid::mc::Snapshot> system_state) const
809 {
810   system_state->restore(&mc_model_checker->get_remote_simulation());
811 }
812
813 void mc_api::log_state() const
814 {
815   session->log_state();
816 }
817
818 bool mc_api::snapshot_equal(const Snapshot* s1, const Snapshot* s2) const
819 {
820   return simgrid::mc::snapshot_equal(s1, s2);
821 }
822
823 simgrid::mc::Snapshot* mc_api::take_snapshot(int num_state) const
824 {
825   auto snapshot = new simgrid::mc::Snapshot(num_state);
826   return snapshot;
827 }
828
829 void mc_api::s_close() const
830 {
831   session->close();
832 }
833
834 void mc_api::restore_initial_state() const
835 {
836   session->restore_initial_state();
837 }
838
839 void mc_api::execute(Transition const& transition) const
840 {
841   session->execute(transition);
842 }
843
844 #if SIMGRID_HAVE_MC
845 void mc_api::automaton_load(const char *file) const
846
847   MC_automaton_load(file); 
848 }
849 #endif
850
851 std::vector<int> mc_api::automaton_propositional_symbol_evaluate() const  
852 {
853   unsigned int cursor = 0;
854   std::vector<int> values;    
855   xbt_automaton_propositional_symbol_t ps = nullptr;
856   xbt_dynar_foreach (mc::property_automaton->propositional_symbols, cursor, ps)
857     values.push_back(xbt_automaton_propositional_symbol_evaluate(ps));
858   return values;
859 }
860
861 std::vector<xbt_automaton_state_t> mc_api::get_automaton_state() const
862 {
863   std::vector<xbt_automaton_state_t> automaton_stack;
864   unsigned int cursor = 0;
865   xbt_automaton_state_t automaton_state;
866   xbt_dynar_foreach (mc::property_automaton->states, cursor, automaton_state)
867     if (automaton_state->type == -1)
868       automaton_stack.push_back(automaton_state);
869   return automaton_stack;
870 }
871
872 int mc_api::compare_automaton_exp_label(const xbt_automaton_exp_label* l) const
873 {
874   unsigned int cursor = 0;
875   xbt_automaton_propositional_symbol_t p = nullptr;
876   xbt_dynar_foreach (simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
877     if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
878       return cursor;
879   }
880   return -1;
881 }
882
883 void mc_api::set_property_automaton(xbt_automaton_state_t const& automaton_state) const
884 {
885   mc::property_automaton->current_state = automaton_state;
886 }
887
888 xbt_automaton_exp_label_t mc_api::get_automaton_transition_label(xbt_dynar_t const& dynar, int index) const
889 {
890   const xbt_automaton_transition* transition =
891       xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
892   return transition->label;
893 }
894
895 xbt_automaton_state_t mc_api::get_automaton_transition_dst(xbt_dynar_t const& dynar, int index) const
896 {
897   const xbt_automaton_transition* transition =
898       xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
899   return transition->dst;
900 }
901
902 } // namespace mc
903 } // namespace simgrid