Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
simcall_get_comm() deleted
[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); // r1->get_mboxx')
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 bool Api::mc_is_null() const
503 {
504   auto is_null = (mc_model_checker == nullptr) ? true : false;
505   return is_null;
506 }
507
508 Checker* Api::mc_get_checker() const
509 {
510   return mc_model_checker->getChecker();
511 }
512
513 void Api::set_checker(Checker* const checker) const
514 {
515   xbt_assert(mc_model_checker);
516   xbt_assert(mc_model_checker->getChecker() == nullptr);
517   mc_model_checker->setChecker(checker);
518 }
519
520 void Api::handle_simcall(Transition const& transition) const
521 {
522   mc_model_checker->handle_simcall(transition);
523 }
524
525 void Api::mc_wait_for_requests() const
526 {
527   mc_model_checker->wait_for_requests();
528 }
529
530 void Api::mc_exit(int status) const
531 {
532   mc_model_checker->exit(status);
533 }
534
535 std::string const& Api::mc_get_host_name(std::string const& hostname) const
536 {
537   return mc_model_checker->get_host_name(hostname);
538 }
539
540 void Api::dump_record_path() const
541 {
542   simgrid::mc::dumpRecordPath();
543 }
544
545 smx_simcall_t Api::mc_state_choose_request(simgrid::mc::State* state) const
546 {
547   for (auto& actor : mc_model_checker->get_remote_simulation().actors()) {
548     /* Only consider the actors that were marked as interleaving by the checker algorithm */
549     if (not state->actor_states_[actor.copy.get_buffer()->get_pid()].is_todo())
550       continue;
551
552     smx_simcall_t res = MC_state_choose_request_for_process(state, actor.copy.get_buffer());
553     if (res)
554       return res;
555   }
556   return nullptr;
557 }
558
559 bool Api::simcall_check_dependency(smx_simcall_t const req1, smx_simcall_t const req2) const
560 {
561   if (req1->issuer_ == req2->issuer_)
562     return false;
563
564   /* Wait with timeout transitions are not considered by the independence theorem, thus we consider them as dependent with all other transitions */
565   if ((req1->call_ == Simcall::COMM_WAIT && simcall_comm_wait__get__timeout(req1) > 0) ||
566       (req2->call_ == Simcall::COMM_WAIT && simcall_comm_wait__get__timeout(req2) > 0))
567     return true;
568
569   if (req1->call_ != req2->call_)
570     return request_depend_asymmetric(req1, req2) && request_depend_asymmetric(req2, req1);
571
572   // Those are internal requests, we do not need indirection because those objects are copies:
573   const kernel::activity::CommImpl* synchro1 = get_comm(req1);
574   const kernel::activity::CommImpl* synchro2 = get_comm(req2);
575
576   switch (req1->call_) {
577     case Simcall::COMM_ISEND:
578       return simcall_comm_isend__get__mbox(req1) == simcall_comm_isend__get__mbox(req2);
579     case Simcall::COMM_IRECV:
580       return simcall_comm_irecv__get__mbox(req1) == simcall_comm_irecv__get__mbox(req2);
581     case Simcall::COMM_WAIT:
582       if (synchro1->src_buff_ == synchro2->src_buff_ && synchro1->dst_buff_ == synchro2->dst_buff_)
583         return false;
584       if (synchro1->src_buff_ != nullptr && synchro1->dst_buff_ != nullptr && synchro2->src_buff_ != nullptr &&
585           synchro2->dst_buff_ != nullptr && synchro1->dst_buff_ != synchro2->src_buff_ &&
586           synchro1->dst_buff_ != synchro2->dst_buff_ && synchro2->dst_buff_ != synchro1->src_buff_)
587         return false;
588       return true;
589     default:
590       return true;
591   }
592 }
593
594 std::string Api::request_to_string(smx_simcall_t req, int value, RequestType request_type) const
595 {
596   xbt_assert(mc_model_checker != nullptr, "Must be called from MCer");
597
598   if (req->inspector_ != nullptr)
599     return req->inspector_->to_string();
600
601   bool use_remote_comm = true;
602   switch (request_type) {
603     case simgrid::mc::RequestType::simix:
604       use_remote_comm = true;
605       break;
606     case simgrid::mc::RequestType::executed:
607     case simgrid::mc::RequestType::internal:
608       use_remote_comm = false;
609       break;
610     default:
611       THROW_IMPOSSIBLE;
612   }
613
614   const char* type = nullptr;
615   char* args       = nullptr;
616
617   smx_actor_t issuer = MC_smx_simcall_get_issuer(req);
618
619   switch (req->call_) {
620     case Simcall::COMM_ISEND: {
621       type     = "iSend";
622       char* p  = pointer_to_string(simcall_comm_isend__get__src_buff(req));
623       char* bs = buff_size_to_string(simcall_comm_isend__get__src_buff_size(req));
624       if (issuer->get_host())
625         args = bprintf("src=(%ld)%s (%s), buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
626                        MC_smx_actor_get_name(issuer), p, bs);
627       else
628         args = bprintf("src=(%ld)%s, buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_name(issuer), p, bs);
629       xbt_free(bs);
630       xbt_free(p);
631       break;
632     }
633
634     case Simcall::COMM_IRECV: {
635       size_t* remote_size = simcall_comm_irecv__get__dst_buff_size(req);
636       size_t size         = 0;
637       if (remote_size)
638         mc_model_checker->get_remote_simulation().read_bytes(&size, sizeof(size), remote(remote_size));
639
640       type     = "iRecv";
641       char* p  = pointer_to_string(simcall_comm_irecv__get__dst_buff(req));
642       char* bs = buff_size_to_string(size);
643       if (issuer->get_host())
644         args = bprintf("dst=(%ld)%s (%s), buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
645                        MC_smx_actor_get_name(issuer), p, bs);
646       else
647         args = bprintf("dst=(%ld)%s, buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_name(issuer), p, bs);
648       xbt_free(bs);
649       xbt_free(p);
650       break;
651     }
652
653     case Simcall::COMM_WAIT: {
654       simgrid::kernel::activity::CommImpl* remote_act = simcall_comm_wait__getraw__comm(req);
655       char* p;
656       if (value == -1) {
657         type = "WaitTimeout";
658         p    = pointer_to_string(remote_act);
659         args = bprintf("comm=%s", p);
660       } else {
661         type = "Wait";
662         p    = pointer_to_string(remote_act);
663
664         simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
665         const simgrid::kernel::activity::CommImpl* act;
666         if (use_remote_comm) {
667           mc_model_checker->get_remote_simulation().read(temp_synchro, remote(remote_act));
668           act = temp_synchro.get_buffer();
669         } else
670           act = remote_act;
671
672         smx_actor_t src_proc =
673             mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
674         smx_actor_t dst_proc =
675             mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
676         args = bprintf("comm=%s [(%ld)%s (%s)-> (%ld)%s (%s)]", p, src_proc ? src_proc->get_pid() : 0,
677                        src_proc ? MC_smx_actor_get_host_name(src_proc) : "",
678                        src_proc ? MC_smx_actor_get_name(src_proc) : "", dst_proc ? dst_proc->get_pid() : 0,
679                        dst_proc ? MC_smx_actor_get_host_name(dst_proc) : "",
680                        dst_proc ? MC_smx_actor_get_name(dst_proc) : "");
681       }
682       xbt_free(p);
683       break;
684     }
685
686     case Simcall::COMM_TEST: {
687       simgrid::kernel::activity::CommImpl* remote_act = simcall_comm_test__getraw__comm(req);
688       simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
689       const simgrid::kernel::activity::CommImpl* act;
690       if (use_remote_comm) {
691         mc_model_checker->get_remote_simulation().read(temp_synchro, remote(remote_act));
692         act = temp_synchro.get_buffer();
693       } else
694         act = remote_act;
695
696       char* p;
697       if (act->src_actor_.get() == nullptr || act->dst_actor_.get() == nullptr) {
698         type = "Test FALSE";
699         p    = pointer_to_string(remote_act);
700         args = bprintf("comm=%s", p);
701       } else {
702         type = "Test TRUE";
703         p    = pointer_to_string(remote_act);
704
705         smx_actor_t src_proc =
706             mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
707         smx_actor_t dst_proc =
708             mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
709         args = bprintf("comm=%s [(%ld)%s (%s) -> (%ld)%s (%s)]", p, src_proc->get_pid(),
710                        MC_smx_actor_get_name(src_proc), MC_smx_actor_get_host_name(src_proc), dst_proc->get_pid(),
711                        MC_smx_actor_get_name(dst_proc), MC_smx_actor_get_host_name(dst_proc));
712       }
713       xbt_free(p);
714       break;
715     }
716
717     case Simcall::COMM_WAITANY: {
718       type         = "WaitAny";
719       size_t count = simcall_comm_waitany__get__count(req);
720       if (count > 0) {
721         simgrid::kernel::activity::CommImpl* remote_sync;
722         remote_sync =
723             mc_model_checker->get_remote_simulation().read(remote(simcall_comm_waitany__get__comms(req) + value));
724         char* p = pointer_to_string(remote_sync);
725         args    = bprintf("comm=%s (%d of %zu)", p, value + 1, count);
726         xbt_free(p);
727       } else
728         args = bprintf("comm at idx %d", value);
729       break;
730     }
731
732     case Simcall::COMM_TESTANY:
733       if (value == -1) {
734         type = "TestAny FALSE";
735         args = xbt_strdup("-");
736       } else {
737         type = "TestAny";
738         args = bprintf("(%d of %zu)", value + 1, simcall_comm_testany__get__count(req));
739       }
740       break;
741
742     case Simcall::MUTEX_TRYLOCK:
743     case Simcall::MUTEX_LOCK: {
744       if (req->call_ == Simcall::MUTEX_LOCK)
745         type = "Mutex LOCK";
746       else
747         type = "Mutex TRYLOCK";
748
749       simgrid::mc::Remote<simgrid::kernel::activity::MutexImpl> mutex;
750       mc_model_checker->get_remote_simulation().read_bytes(mutex.get_buffer(), sizeof(mutex),
751                                                            remote(req->call_ == Simcall::MUTEX_LOCK
752                                                                       ? simcall_mutex_lock__get__mutex(req)
753                                                                       : simcall_mutex_trylock__get__mutex(req)));
754       args = bprintf("locked = %d, owner = %d, sleeping = n/a", mutex.get_buffer()->is_locked(),
755                      mutex.get_buffer()->get_owner() != nullptr
756                          ? (int)mc_model_checker->get_remote_simulation()
757                                .resolve_actor(simgrid::mc::remote(mutex.get_buffer()->get_owner()))
758                                ->get_pid()
759                          : -1);
760       break;
761     }
762
763     case Simcall::MC_RANDOM:
764       type = "MC_RANDOM";
765       args = bprintf("%d", value);
766       break;
767
768     default:
769       type = SIMIX_simcall_name(req->call_);
770       args = bprintf("??");
771       break;
772   }
773
774   std::string str;
775   if (args != nullptr)
776     str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s(%s)", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
777                                       MC_smx_actor_get_name(issuer), type, args);
778   else
779     str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s ", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
780                                       MC_smx_actor_get_name(issuer), type);
781   xbt_free(args);
782   return str;
783 }
784
785 std::string Api::request_get_dot_output(smx_simcall_t req, int value) const
786 {
787   const smx_actor_t issuer = MC_smx_simcall_get_issuer(req);
788   const char* color        = get_color(issuer->get_pid() - 1);
789
790   if (req->inspector_ != nullptr)
791     return simgrid::xbt::string_printf("label = \"%s\", color = %s, fontcolor = %s",
792                                        req->inspector_->dot_label().c_str(), color, color);
793
794   std::string label;
795
796   switch (req->call_) {
797     case Simcall::COMM_ISEND:
798       if (issuer->get_host())
799         label = xbt::string_printf("[(%ld)%s] iSend", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
800       else
801         label = bprintf("[(%ld)] iSend", issuer->get_pid());
802       break;
803
804     case Simcall::COMM_IRECV:
805       if (issuer->get_host())
806         label = xbt::string_printf("[(%ld)%s] iRecv", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
807       else
808         label = xbt::string_printf("[(%ld)] iRecv", issuer->get_pid());
809       break;
810
811     case Simcall::COMM_WAIT:
812       if (value == -1) {
813         if (issuer->get_host())
814           label = xbt::string_printf("[(%ld)%s] WaitTimeout", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
815         else
816           label = xbt::string_printf("[(%ld)] WaitTimeout", issuer->get_pid());
817       } else {
818         kernel::activity::ActivityImpl* remote_act = simcall_comm_wait__getraw__comm(req);
819         Remote<kernel::activity::CommImpl> temp_comm;
820         mc_model_checker->get_remote_simulation().read(temp_comm,
821                                                        remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
822         const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
823
824         const kernel::actor::ActorImpl* src_proc =
825             mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(comm->src_actor_.get()));
826         const kernel::actor::ActorImpl* dst_proc =
827             mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(comm->dst_actor_.get()));
828         if (issuer->get_host())
829           label =
830               xbt::string_printf("[(%ld)%s] Wait [(%ld)->(%ld)]", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
831                                  src_proc ? src_proc->get_pid() : 0, dst_proc ? dst_proc->get_pid() : 0);
832         else
833           label = xbt::string_printf("[(%ld)] Wait [(%ld)->(%ld)]", issuer->get_pid(),
834                                      src_proc ? src_proc->get_pid() : 0, dst_proc ? dst_proc->get_pid() : 0);
835       }
836       break;
837
838     case Simcall::COMM_TEST: {
839       kernel::activity::ActivityImpl* remote_act = simcall_comm_test__getraw__comm(req);
840       Remote<simgrid::kernel::activity::CommImpl> temp_comm;
841       mc_model_checker->get_remote_simulation().read(temp_comm,
842                                                      remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
843       const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
844       if (comm->src_actor_.get() == nullptr || comm->dst_actor_.get() == nullptr) {
845         if (issuer->get_host())
846           label = xbt::string_printf("[(%ld)%s] Test FALSE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
847         else
848           label = bprintf("[(%ld)] Test FALSE", issuer->get_pid());
849       } else {
850         if (issuer->get_host())
851           label = xbt::string_printf("[(%ld)%s] Test TRUE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
852         else
853           label = xbt::string_printf("[(%ld)] Test TRUE", issuer->get_pid());
854       }
855       break;
856     }
857
858     case Simcall::COMM_WAITANY: {
859       size_t comms_size = simcall_comm_waitany__get__count(req);
860       if (issuer->get_host())
861         label = xbt::string_printf("[(%ld)%s] WaitAny [%d of %zu]", issuer->get_pid(),
862                                    MC_smx_actor_get_host_name(issuer), value + 1, comms_size);
863       else
864         label = xbt::string_printf("[(%ld)] WaitAny [%d of %zu]", issuer->get_pid(), value + 1, comms_size);
865       break;
866     }
867
868     case Simcall::COMM_TESTANY:
869       if (value == -1) {
870         if (issuer->get_host())
871           label = xbt::string_printf("[(%ld)%s] TestAny FALSE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
872         else
873           label = xbt::string_printf("[(%ld)] TestAny FALSE", issuer->get_pid());
874       } else {
875         if (issuer->get_host())
876           label =
877               xbt::string_printf("[(%ld)%s] TestAny TRUE [%d of %lu]", issuer->get_pid(),
878                                  MC_smx_actor_get_host_name(issuer), value + 1, simcall_comm_testany__get__count(req));
879         else
880           label = xbt::string_printf("[(%ld)] TestAny TRUE [%d of %lu]", issuer->get_pid(), value + 1,
881                                      simcall_comm_testany__get__count(req));
882       }
883       break;
884
885     case Simcall::MUTEX_TRYLOCK:
886       label = xbt::string_printf("[(%ld)] Mutex TRYLOCK", issuer->get_pid());
887       break;
888
889     case Simcall::MUTEX_LOCK:
890       label = xbt::string_printf("[(%ld)] Mutex LOCK", issuer->get_pid());
891       break;
892
893     case Simcall::MC_RANDOM:
894       if (issuer->get_host())
895         label = xbt::string_printf("[(%ld)%s] MC_RANDOM (%d)", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
896                                    value);
897       else
898         label = xbt::string_printf("[(%ld)] MC_RANDOM (%d)", issuer->get_pid(), value);
899       break;
900
901     default:
902       THROW_UNIMPLEMENTED;
903   }
904
905   return xbt::string_printf("label = \"%s\", color = %s, fontcolor = %s", label.c_str(), color, color);
906 }
907
908 const char* Api::simcall_get_name(simgrid::simix::Simcall kind) const
909 {
910   return SIMIX_simcall_name(kind);
911 }
912
913 #if HAVE_SMPI
914 int Api::get_smpi_request_tag(smx_simcall_t const& simcall, simgrid::simix::Simcall type) const
915 {
916   simgrid::smpi::Request mpi_request;
917   void* simcall_data = nullptr;
918   if (type == Simcall::COMM_ISEND)
919     simcall_data = simcall_comm_isend__get__data(simcall);
920   else if (type == Simcall::COMM_IRECV)
921     simcall_data = simcall_comm_irecv__get__data(simcall);
922   mc_model_checker->get_remote_simulation().read(&mpi_request, remote(static_cast<smpi::Request*>(simcall_data)));
923   return mpi_request.tag();
924 }
925 #endif
926
927 void Api::restore_state(std::shared_ptr<simgrid::mc::Snapshot> system_state) const
928 {
929   system_state->restore(&mc_model_checker->get_remote_simulation());
930 }
931
932 void Api::log_state() const
933 {
934   session->log_state();
935 }
936
937 bool Api::snapshot_equal(const Snapshot* s1, const Snapshot* s2) const
938 {
939   return simgrid::mc::snapshot_equal(s1, s2);
940 }
941
942 simgrid::mc::Snapshot* Api::take_snapshot(int num_state) const
943 {
944   auto snapshot = new simgrid::mc::Snapshot(num_state);
945   return snapshot;
946 }
947
948 void Api::s_close() const
949 {
950   session->close();
951 }
952
953 void Api::restore_initial_state() const
954 {
955   session->restore_initial_state();
956 }
957
958 void Api::execute(Transition const& transition) const
959 {
960   session->execute(transition);
961 }
962
963 #if SIMGRID_HAVE_MC
964 void Api::automaton_load(const char* file) const
965 {
966   MC_automaton_load(file);
967 }
968 #endif
969
970 std::vector<int> Api::automaton_propositional_symbol_evaluate() const
971 {
972   unsigned int cursor = 0;
973   std::vector<int> values;
974   xbt_automaton_propositional_symbol_t ps = nullptr;
975   xbt_dynar_foreach (mc::property_automaton->propositional_symbols, cursor, ps)
976     values.push_back(xbt_automaton_propositional_symbol_evaluate(ps));
977   return values;
978 }
979
980 std::vector<xbt_automaton_state_t> Api::get_automaton_state() const
981 {
982   std::vector<xbt_automaton_state_t> automaton_stack;
983   unsigned int cursor = 0;
984   xbt_automaton_state_t automaton_state;
985   xbt_dynar_foreach (mc::property_automaton->states, cursor, automaton_state)
986     if (automaton_state->type == -1)
987       automaton_stack.push_back(automaton_state);
988   return automaton_stack;
989 }
990
991 int Api::compare_automaton_exp_label(const xbt_automaton_exp_label* l) const
992 {
993   unsigned int cursor                    = 0;
994   xbt_automaton_propositional_symbol_t p = nullptr;
995   xbt_dynar_foreach (simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
996     if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
997       return cursor;
998   }
999   return -1;
1000 }
1001
1002 void Api::set_property_automaton(xbt_automaton_state_t const& automaton_state) const
1003 {
1004   mc::property_automaton->current_state = automaton_state;
1005 }
1006
1007 xbt_automaton_exp_label_t Api::get_automaton_transition_label(xbt_dynar_t const& dynar, int index) const
1008 {
1009   const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
1010   return transition->label;
1011 }
1012
1013 xbt_automaton_state_t Api::get_automaton_transition_dst(xbt_dynar_t const& dynar, int index) const
1014 {
1015   const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
1016   return transition->dst;
1017 }
1018
1019 } // namespace mc
1020 } // namespace simgrid