Logo AND Algorithmique Numérique Distribuée

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