Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid
[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 /* 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 smx_mailbox_t Api::get_mbox(smx_simcall_t const r) const
206 {
207   switch (r->call_) {
208     case Simcall::COMM_ISEND:
209       return simcall_comm_isend__get__mbox(r);
210     case Simcall::COMM_IRECV:
211       return simcall_comm_irecv__get__mbox(r);
212     default:
213       return nullptr;
214   }
215 }
216
217 simgrid::kernel::activity::CommImpl* Api::get_comm(smx_simcall_t const r) const
218 {
219   switch (r->call_) {
220     case Simcall::COMM_WAIT:
221       return simcall_comm_wait__getraw__comm(r);
222     case Simcall::COMM_TEST:
223       return simcall_comm_test__getraw__comm(r);
224     default:
225       return nullptr;
226   }
227 }
228
229 // Does half the job
230 bool Api::request_depend_asymmetric(smx_simcall_t r1, smx_simcall_t r2) const 
231 {
232   if (r1->call_ == Simcall::COMM_ISEND && r2->call_ == Simcall::COMM_IRECV)
233     return false;
234
235   if (r1->call_ == Simcall::COMM_IRECV && r2->call_ == Simcall::COMM_ISEND)
236     return false;
237
238   // Those are internal requests, we do not need indirection because those objects are copies:
239   const kernel::activity::CommImpl* synchro1 = get_comm(r1);
240   const kernel::activity::CommImpl* synchro2 = get_comm(r2);
241
242   if ((r1->call_ == Simcall::COMM_ISEND || r1->call_ == Simcall::COMM_IRECV) && r2->call_ == Simcall::COMM_WAIT) {
243     const kernel::activity::MailboxImpl* mbox = get_mbox(r1); // r1->get_mboxx')
244
245     if (mbox != synchro2->mbox_cpy
246         && simcall_comm_wait__get__timeout(r2) <= 0)
247       return false;
248
249     if ((r1->issuer_ != synchro2->src_actor_.get()) && (r1->issuer_ != synchro2->dst_actor_.get()) &&
250         simcall_comm_wait__get__timeout(r2) <= 0)
251       return false;
252
253     if ((r1->call_ == Simcall::COMM_ISEND) && (synchro2->type_ == kernel::activity::CommImpl::Type::SEND) &&
254         (synchro2->src_buff_ != simcall_comm_isend__get__src_buff(r1)) && simcall_comm_wait__get__timeout(r2) <= 0)
255       return false;
256
257     if ((r1->call_ == Simcall::COMM_IRECV) && (synchro2->type_ == kernel::activity::CommImpl::Type::RECEIVE) &&
258         (synchro2->dst_buff_ != simcall_comm_irecv__get__dst_buff(r1)) && simcall_comm_wait__get__timeout(r2) <= 0)
259       return false;
260   }
261
262   /* FIXME: the following rule assumes that the result of the isend/irecv call is not stored in a buffer used in the
263    * test call. */
264 #if 0
265   if((r1->call == Simcall::COMM_ISEND || r1->call == Simcall::COMM_IRECV)
266       &&  r2->call == Simcall::COMM_TEST)
267     return false;
268 #endif
269
270   if (r1->call_ == Simcall::COMM_WAIT && (r2->call_ == Simcall::COMM_WAIT || r2->call_ == Simcall::COMM_TEST) &&
271       (synchro1->src_actor_.get() == nullptr || synchro1->dst_actor_.get() == nullptr))
272     return false;
273
274   if (r1->call_ == Simcall::COMM_TEST &&
275       (simcall_comm_test__get__comm(r1) == nullptr || synchro1->src_buff_ == nullptr || synchro1->dst_buff_ == nullptr))
276     return false;
277
278   if (r1->call_ == Simcall::COMM_TEST && r2->call_ == Simcall::COMM_WAIT &&
279       synchro1->src_buff_ == synchro2->src_buff_ && synchro1->dst_buff_ == synchro2->dst_buff_)
280     return false;
281
282   if (r1->call_ == Simcall::COMM_WAIT && r2->call_ == Simcall::COMM_TEST && synchro1->src_buff_ != nullptr &&
283       synchro1->dst_buff_ != nullptr && synchro2->src_buff_ != nullptr && synchro2->dst_buff_ != nullptr &&
284       synchro1->dst_buff_ != synchro2->src_buff_ && synchro1->dst_buff_ != synchro2->dst_buff_ &&
285       synchro2->dst_buff_ != synchro1->src_buff_)
286     return false;
287
288   return true;
289 }
290
291 void Api::initialize(char** argv) const
292 {
293   simgrid::mc::session = new simgrid::mc::Session([argv] {
294     int i = 1;
295     while (argv[i] != nullptr && argv[i][0] == '-')
296       i++;
297     xbt_assert(argv[i] != nullptr,
298                "Unable to find a binary to exec on the command line. Did you only pass config flags?");
299     execvp(argv[i], argv + i);
300     xbt_die("The model-checked process failed to exec(): %s", strerror(errno));
301   });
302 }
303
304 std::vector<simgrid::mc::ActorInformation>& Api::get_actors() const
305 {
306   return mc_model_checker->get_remote_simulation().actors();
307 }
308
309 bool Api::actor_is_enabled(aid_t pid) const
310 {
311   return session->actor_is_enabled(pid);
312 }
313
314 unsigned long Api::get_maxpid() const
315 {
316   return MC_smx_get_maxpid();
317 }
318
319 int Api::get_actors_size() const
320 {
321   return mc_model_checker->get_remote_simulation().actors().size();
322 }
323
324 RemotePtr<kernel::activity::CommImpl> Api::get_comm_isend_raw_addr(smx_simcall_t request) const
325 {
326   auto comm_addr = simgrid::simix::unmarshal_raw<simgrid::kernel::activity::ActivityImpl*>(request->result_);
327   return RemotePtr<kernel::activity::CommImpl>(static_cast<kernel::activity::CommImpl*>(comm_addr));
328 }
329
330 RemotePtr<kernel::activity::CommImpl> Api::get_comm_irecv_raw_addr(smx_simcall_t request) const
331 {
332   auto comm_addr = simgrid::simix::unmarshal_raw<simgrid::kernel::activity::ActivityImpl*>(request->result_);
333   return RemotePtr<kernel::activity::CommImpl>(static_cast<kernel::activity::CommImpl*>(comm_addr));
334 }
335
336 RemotePtr<kernel::activity::CommImpl> Api::get_comm_wait_raw_addr(smx_simcall_t request) const
337 {
338   auto comm_addr = simgrid::simix::unmarshal_raw<simgrid::kernel::activity::CommImpl*>(request->args_[0]);
339   return RemotePtr<kernel::activity::CommImpl>(static_cast<kernel::activity::CommImpl*>(comm_addr));
340 }
341
342 RemotePtr<kernel::activity::CommImpl> Api::get_comm_waitany_raw_addr(smx_simcall_t request, int value) const
343 {
344   auto addr = simgrid::simix::unmarshal_raw<simgrid::kernel::activity::CommImpl**>(request->args_[0]) + value;
345   auto comm_addr = mc_model_checker->get_remote_simulation().read(remote(addr));
346   return RemotePtr<kernel::activity::CommImpl>(static_cast<kernel::activity::CommImpl*>(comm_addr));
347 }
348
349 std::string Api::get_pattern_comm_rdv(RemotePtr<kernel::activity::CommImpl> const& addr) const
350 {
351   Remote<kernel::activity::CommImpl> temp_synchro;
352   mc_model_checker->get_remote_simulation().read(temp_synchro, 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(RemotePtr<kernel::activity::CommImpl> const& addr) const
362 {
363   Remote<kernel::activity::CommImpl> temp_synchro;
364   mc_model_checker->get_remote_simulation().read(temp_synchro, 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(RemotePtr<kernel::activity::CommImpl> const& addr) const
372 {
373   Remote<kernel::activity::CommImpl> temp_synchro;
374   mc_model_checker->get_remote_simulation().read(temp_synchro, 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(RemotePtr<kernel::activity::CommImpl> const& addr) const
382 {
383   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
384   mc_model_checker->get_remote_simulation().read(temp_comm, addr);
385   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
386
387   std::vector<char> buffer{};
388   if (comm->src_buff_ != nullptr) {
389     buffer.resize(comm->src_buff_size_);
390     mc_model_checker->get_remote_simulation().read_bytes(buffer.data(), buffer.size(), remote(comm->src_buff_));
391   }
392   return buffer;
393 }
394
395 const char* Api::get_actor_host_name(smx_actor_t actor) const
396 {
397   const char* host_name = MC_smx_actor_get_host_name(actor);
398   return host_name;
399 }
400
401 #if HAVE_SMPI
402 bool Api::check_send_request_detached(smx_simcall_t const& simcall) const
403 {
404   simgrid::smpi::Request mpi_request;
405   mc_model_checker->get_remote_simulation().read(
406       &mpi_request, remote(static_cast<smpi::Request*>(simcall_comm_isend__get__data(simcall))));
407   return mpi_request.detached();
408 }
409 #endif
410
411 smx_actor_t Api::get_src_actor(RemotePtr<kernel::activity::CommImpl> const& comm_addr) const
412 {
413   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
414   mc_model_checker->get_remote_simulation().read(temp_comm, comm_addr);
415   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
416
417   auto src_proc = mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(comm->src_actor_.get()));
418   return src_proc;
419 }
420
421 smx_actor_t Api::get_dst_actor(RemotePtr<kernel::activity::CommImpl> const& comm_addr) const
422 {
423   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
424   mc_model_checker->get_remote_simulation().read(temp_comm, comm_addr);
425   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
426
427   auto dst_proc = mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(comm->dst_actor_.get()));
428   return dst_proc;
429 }
430
431 std::size_t Api::get_remote_heap_bytes() const
432 {
433   RemoteSimulation& process = mc_model_checker->get_remote_simulation();
434   auto heap_bytes_used      = mmalloc_get_bytes_used_remote(process.get_heap()->heaplimit, process.get_malloc_info());
435   return heap_bytes_used;
436 }
437
438 void Api::session_initialize() const
439 {
440   session->initialize();
441 }
442
443 void Api::mc_inc_visited_states() const
444 {
445   mc_model_checker->visited_states++;
446 }
447
448 void Api::mc_inc_executed_trans() const
449 {
450   mc_model_checker->executed_transitions++;
451 }
452
453 unsigned long Api::mc_get_visited_states() const
454 {
455   return mc_model_checker->visited_states;
456 }
457
458 unsigned long Api::mc_get_executed_trans() const
459 {
460   return mc_model_checker->executed_transitions;
461 }
462
463 bool Api::mc_check_deadlock() const
464 {
465   return mc_model_checker->checkDeadlock();
466 }
467
468 void Api::mc_show_deadlock() const
469 {
470   MC_show_deadlock();
471 }
472
473 /** Get the issuer of a simcall (`req->issuer`)
474  *
475  *  In split-process mode, it does the black magic necessary to get an address
476  *  of a (shallow) copy of the data structure the issuer SIMIX actor in the local
477  *  address space.
478  *
479  *  @param process the MCed process
480  *  @param req     the simcall (copied in the local process)
481  */
482 smx_actor_t Api::simcall_get_issuer(s_smx_simcall const* req) const
483 {
484   xbt_assert(mc_model_checker != nullptr);
485
486   // This is the address of the smx_actor in the MCed process:
487   auto address = simgrid::mc::remote(req->issuer_);
488
489   // Lookup by address:
490   for (auto& actor : mc_model_checker->get_remote_simulation().actors())
491     if (actor.address == address)
492       return actor.copy.get_buffer();
493   for (auto& actor : mc_model_checker->get_remote_simulation().dead_actors())
494     if (actor.address == address)
495       return actor.copy.get_buffer();
496
497   xbt_die("Issuer not found");
498 }
499
500 long Api::simcall_get_actor_id(s_smx_simcall const* req) const
501 {
502   return simcall_get_issuer(req)->get_pid();
503 }
504
505 smx_mailbox_t Api::simcall_get_mbox(smx_simcall_t const req) const
506 {
507   return get_mbox(req);
508 }
509
510 bool Api::mc_is_null() const
511 {
512   auto is_null = (mc_model_checker == nullptr) ? true : false;
513   return is_null;
514 }
515
516 Checker* Api::mc_get_checker() const
517 {
518   return mc_model_checker->getChecker();
519 }
520
521 void Api::set_checker(Checker* const checker) const
522 {
523   xbt_assert(mc_model_checker);
524   xbt_assert(mc_model_checker->getChecker() == nullptr);
525   mc_model_checker->setChecker(checker);
526 }
527
528 void Api::handle_simcall(Transition const& transition) const
529 {
530   mc_model_checker->handle_simcall(transition);
531 }
532
533 void Api::mc_wait_for_requests() const
534 {
535   mc_model_checker->wait_for_requests();
536 }
537
538 void Api::mc_exit(int status) const
539 {
540   mc_model_checker->exit(status);
541 }
542
543 std::string const& Api::mc_get_host_name(std::string const& hostname) const
544 {
545   return mc_model_checker->get_host_name(hostname);
546 }
547
548 void Api::dump_record_path() const
549 {
550   simgrid::mc::dumpRecordPath();
551 }
552
553 smx_simcall_t Api::mc_state_choose_request(simgrid::mc::State* state) const
554 {
555   for (auto& actor : mc_model_checker->get_remote_simulation().actors()) {
556     /* Only consider the actors that were marked as interleaving by the checker algorithm */
557     if (not state->actor_states_[actor.copy.get_buffer()->get_pid()].is_todo())
558       continue;
559
560     smx_simcall_t res = MC_state_choose_request_for_process(state, actor.copy.get_buffer());
561     if (res)
562       return res;
563   }
564   return nullptr;
565 }
566
567 bool Api::simcall_check_dependency(smx_simcall_t const req1, smx_simcall_t const req2) const
568 {
569   if (req1->issuer_ == req2->issuer_)
570     return false;
571
572   /* Wait with timeout transitions are not considered by the independence theorem, thus we consider them as dependent with all other transitions */
573   if ((req1->call_ == Simcall::COMM_WAIT && simcall_comm_wait__get__timeout(req1) > 0) ||
574       (req2->call_ == Simcall::COMM_WAIT && simcall_comm_wait__get__timeout(req2) > 0))
575     return true;
576
577   if (req1->call_ != req2->call_)
578     return request_depend_asymmetric(req1, req2) && request_depend_asymmetric(req2, req1);
579
580   // Those are internal requests, we do not need indirection because those objects are copies:
581   const kernel::activity::CommImpl* synchro1 = get_comm(req1);
582   const kernel::activity::CommImpl* synchro2 = get_comm(req2);
583
584   switch (req1->call_) {
585     case Simcall::COMM_ISEND:
586       return simcall_comm_isend__get__mbox(req1) == simcall_comm_isend__get__mbox(req2);
587     case Simcall::COMM_IRECV:
588       return simcall_comm_irecv__get__mbox(req1) == simcall_comm_irecv__get__mbox(req2);
589     case Simcall::COMM_WAIT:
590       if (synchro1->src_buff_ == synchro2->src_buff_ && synchro1->dst_buff_ == synchro2->dst_buff_)
591         return false;
592       if (synchro1->src_buff_ != nullptr && synchro1->dst_buff_ != nullptr && synchro2->src_buff_ != nullptr &&
593           synchro2->dst_buff_ != nullptr && synchro1->dst_buff_ != synchro2->src_buff_ &&
594           synchro1->dst_buff_ != synchro2->dst_buff_ && synchro2->dst_buff_ != synchro1->src_buff_)
595         return false;
596       return true;
597     default:
598       return true;
599   }
600 }
601
602 std::string Api::request_to_string(smx_simcall_t req, int value, RequestType request_type) const
603 {
604   xbt_assert(mc_model_checker != nullptr, "Must be called from MCer");
605
606   if (req->inspector_ != nullptr)
607     return req->inspector_->to_string();
608
609   bool use_remote_comm = true;
610   switch (request_type) {
611     case simgrid::mc::RequestType::simix:
612       use_remote_comm = true;
613       break;
614     case simgrid::mc::RequestType::executed:
615     case simgrid::mc::RequestType::internal:
616       use_remote_comm = false;
617       break;
618     default:
619       THROW_IMPOSSIBLE;
620   }
621
622   const char* type = nullptr;
623   char* args       = nullptr;
624
625   smx_actor_t issuer = simcall_get_issuer(req);
626
627   switch (req->call_) {
628     case Simcall::COMM_ISEND: {
629       type     = "iSend";
630       char* p  = pointer_to_string(simcall_comm_isend__get__src_buff(req));
631       char* bs = buff_size_to_string(simcall_comm_isend__get__src_buff_size(req));
632       if (issuer->get_host())
633         args = bprintf("src=(%ld)%s (%s), buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
634                        MC_smx_actor_get_name(issuer), p, bs);
635       else
636         args = bprintf("src=(%ld)%s, buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_name(issuer), p, bs);
637       xbt_free(bs);
638       xbt_free(p);
639       break;
640     }
641
642     case Simcall::COMM_IRECV: {
643       size_t* remote_size = simcall_comm_irecv__get__dst_buff_size(req);
644       size_t size         = 0;
645       if (remote_size)
646         mc_model_checker->get_remote_simulation().read_bytes(&size, sizeof(size), remote(remote_size));
647
648       type     = "iRecv";
649       char* p  = pointer_to_string(simcall_comm_irecv__get__dst_buff(req));
650       char* bs = buff_size_to_string(size);
651       if (issuer->get_host())
652         args = bprintf("dst=(%ld)%s (%s), buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
653                        MC_smx_actor_get_name(issuer), p, bs);
654       else
655         args = bprintf("dst=(%ld)%s, buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_name(issuer), p, bs);
656       xbt_free(bs);
657       xbt_free(p);
658       break;
659     }
660
661     case Simcall::COMM_WAIT: {
662       simgrid::kernel::activity::CommImpl* remote_act = simcall_comm_wait__getraw__comm(req);
663       char* p;
664       if (value == -1) {
665         type = "WaitTimeout";
666         p    = pointer_to_string(remote_act);
667         args = bprintf("comm=%s", p);
668       } else {
669         type = "Wait";
670         p    = pointer_to_string(remote_act);
671
672         simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
673         const simgrid::kernel::activity::CommImpl* act;
674         if (use_remote_comm) {
675           mc_model_checker->get_remote_simulation().read(temp_synchro, remote(remote_act));
676           act = temp_synchro.get_buffer();
677         } else
678           act = remote_act;
679
680         smx_actor_t src_proc =
681             mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
682         smx_actor_t dst_proc =
683             mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
684         args = bprintf("comm=%s [(%ld)%s (%s)-> (%ld)%s (%s)]", p, src_proc ? src_proc->get_pid() : 0,
685                        src_proc ? MC_smx_actor_get_host_name(src_proc) : "",
686                        src_proc ? MC_smx_actor_get_name(src_proc) : "", dst_proc ? dst_proc->get_pid() : 0,
687                        dst_proc ? MC_smx_actor_get_host_name(dst_proc) : "",
688                        dst_proc ? MC_smx_actor_get_name(dst_proc) : "");
689       }
690       xbt_free(p);
691       break;
692     }
693
694     case Simcall::COMM_TEST: {
695       simgrid::kernel::activity::CommImpl* remote_act = simcall_comm_test__getraw__comm(req);
696       simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
697       const simgrid::kernel::activity::CommImpl* act;
698       if (use_remote_comm) {
699         mc_model_checker->get_remote_simulation().read(temp_synchro, remote(remote_act));
700         act = temp_synchro.get_buffer();
701       } else
702         act = remote_act;
703
704       char* p;
705       if (act->src_actor_.get() == nullptr || act->dst_actor_.get() == nullptr) {
706         type = "Test FALSE";
707         p    = pointer_to_string(remote_act);
708         args = bprintf("comm=%s", p);
709       } else {
710         type = "Test TRUE";
711         p    = pointer_to_string(remote_act);
712
713         smx_actor_t src_proc =
714             mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
715         smx_actor_t dst_proc =
716             mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
717         args = bprintf("comm=%s [(%ld)%s (%s) -> (%ld)%s (%s)]", p, src_proc->get_pid(),
718                        MC_smx_actor_get_name(src_proc), MC_smx_actor_get_host_name(src_proc), dst_proc->get_pid(),
719                        MC_smx_actor_get_name(dst_proc), MC_smx_actor_get_host_name(dst_proc));
720       }
721       xbt_free(p);
722       break;
723     }
724
725     case Simcall::COMM_WAITANY: {
726       type         = "WaitAny";
727       size_t count = simcall_comm_waitany__get__count(req);
728       if (count > 0) {
729         simgrid::kernel::activity::CommImpl* remote_sync;
730         remote_sync =
731             mc_model_checker->get_remote_simulation().read(remote(simcall_comm_waitany__get__comms(req) + value));
732         char* p = pointer_to_string(remote_sync);
733         args    = bprintf("comm=%s (%d of %zu)", p, value + 1, count);
734         xbt_free(p);
735       } else
736         args = bprintf("comm at idx %d", value);
737       break;
738     }
739
740     case Simcall::COMM_TESTANY:
741       if (value == -1) {
742         type = "TestAny FALSE";
743         args = xbt_strdup("-");
744       } else {
745         type = "TestAny";
746         args = bprintf("(%d of %zu)", value + 1, simcall_comm_testany__get__count(req));
747       }
748       break;
749
750     case Simcall::MUTEX_TRYLOCK:
751     case Simcall::MUTEX_LOCK: {
752       if (req->call_ == Simcall::MUTEX_LOCK)
753         type = "Mutex LOCK";
754       else
755         type = "Mutex TRYLOCK";
756
757       simgrid::mc::Remote<simgrid::kernel::activity::MutexImpl> mutex;
758       mc_model_checker->get_remote_simulation().read_bytes(mutex.get_buffer(), sizeof(mutex),
759                                                            remote(req->call_ == Simcall::MUTEX_LOCK
760                                                                       ? simcall_mutex_lock__get__mutex(req)
761                                                                       : simcall_mutex_trylock__get__mutex(req)));
762       args = bprintf("locked = %d, owner = %d, sleeping = n/a", mutex.get_buffer()->is_locked(),
763                      mutex.get_buffer()->get_owner() != nullptr
764                          ? (int)mc_model_checker->get_remote_simulation()
765                                .resolve_actor(simgrid::mc::remote(mutex.get_buffer()->get_owner()))
766                                ->get_pid()
767                          : -1);
768       break;
769     }
770
771     case Simcall::MC_RANDOM:
772       type = "MC_RANDOM";
773       args = bprintf("%d", value);
774       break;
775
776     default:
777       type = SIMIX_simcall_name(req->call_);
778       args = bprintf("??");
779       break;
780   }
781
782   std::string str;
783   if (args != nullptr)
784     str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s(%s)", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
785                                       MC_smx_actor_get_name(issuer), type, args);
786   else
787     str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s ", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
788                                       MC_smx_actor_get_name(issuer), type);
789   xbt_free(args);
790   return str;
791 }
792
793 std::string Api::request_get_dot_output(smx_simcall_t req, int value) const
794 {
795   const smx_actor_t issuer = simcall_get_issuer(req);
796   const char* color        = get_color(issuer->get_pid() - 1);
797
798   if (req->inspector_ != nullptr)
799     return simgrid::xbt::string_printf("label = \"%s\", color = %s, fontcolor = %s",
800                                        req->inspector_->dot_label().c_str(), color, color);
801
802   std::string label;
803
804   switch (req->call_) {
805     case Simcall::COMM_ISEND:
806       if (issuer->get_host())
807         label = xbt::string_printf("[(%ld)%s] iSend", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
808       else
809         label = bprintf("[(%ld)] iSend", issuer->get_pid());
810       break;
811
812     case Simcall::COMM_IRECV:
813       if (issuer->get_host())
814         label = xbt::string_printf("[(%ld)%s] iRecv", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
815       else
816         label = xbt::string_printf("[(%ld)] iRecv", issuer->get_pid());
817       break;
818
819     case Simcall::COMM_WAIT:
820       if (value == -1) {
821         if (issuer->get_host())
822           label = xbt::string_printf("[(%ld)%s] WaitTimeout", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
823         else
824           label = xbt::string_printf("[(%ld)] WaitTimeout", issuer->get_pid());
825       } else {
826         kernel::activity::ActivityImpl* remote_act = simcall_comm_wait__getraw__comm(req);
827         Remote<kernel::activity::CommImpl> temp_comm;
828         mc_model_checker->get_remote_simulation().read(temp_comm,
829                                                        remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
830         const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
831
832         const kernel::actor::ActorImpl* src_proc =
833             mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(comm->src_actor_.get()));
834         const kernel::actor::ActorImpl* dst_proc =
835             mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(comm->dst_actor_.get()));
836         if (issuer->get_host())
837           label =
838               xbt::string_printf("[(%ld)%s] Wait [(%ld)->(%ld)]", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
839                                  src_proc ? src_proc->get_pid() : 0, dst_proc ? dst_proc->get_pid() : 0);
840         else
841           label = xbt::string_printf("[(%ld)] Wait [(%ld)->(%ld)]", issuer->get_pid(),
842                                      src_proc ? src_proc->get_pid() : 0, dst_proc ? dst_proc->get_pid() : 0);
843       }
844       break;
845
846     case Simcall::COMM_TEST: {
847       kernel::activity::ActivityImpl* remote_act = simcall_comm_test__getraw__comm(req);
848       Remote<simgrid::kernel::activity::CommImpl> temp_comm;
849       mc_model_checker->get_remote_simulation().read(temp_comm,
850                                                      remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
851       const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
852       if (comm->src_actor_.get() == nullptr || comm->dst_actor_.get() == nullptr) {
853         if (issuer->get_host())
854           label = xbt::string_printf("[(%ld)%s] Test FALSE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
855         else
856           label = bprintf("[(%ld)] Test FALSE", issuer->get_pid());
857       } else {
858         if (issuer->get_host())
859           label = xbt::string_printf("[(%ld)%s] Test TRUE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
860         else
861           label = xbt::string_printf("[(%ld)] Test TRUE", issuer->get_pid());
862       }
863       break;
864     }
865
866     case Simcall::COMM_WAITANY: {
867       size_t comms_size = simcall_comm_waitany__get__count(req);
868       if (issuer->get_host())
869         label = xbt::string_printf("[(%ld)%s] WaitAny [%d of %zu]", issuer->get_pid(),
870                                    MC_smx_actor_get_host_name(issuer), value + 1, comms_size);
871       else
872         label = xbt::string_printf("[(%ld)] WaitAny [%d of %zu]", issuer->get_pid(), value + 1, comms_size);
873       break;
874     }
875
876     case Simcall::COMM_TESTANY:
877       if (value == -1) {
878         if (issuer->get_host())
879           label = xbt::string_printf("[(%ld)%s] TestAny FALSE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
880         else
881           label = xbt::string_printf("[(%ld)] TestAny FALSE", issuer->get_pid());
882       } else {
883         if (issuer->get_host())
884           label =
885               xbt::string_printf("[(%ld)%s] TestAny TRUE [%d of %lu]", issuer->get_pid(),
886                                  MC_smx_actor_get_host_name(issuer), value + 1, simcall_comm_testany__get__count(req));
887         else
888           label = xbt::string_printf("[(%ld)] TestAny TRUE [%d of %lu]", issuer->get_pid(), value + 1,
889                                      simcall_comm_testany__get__count(req));
890       }
891       break;
892
893     case Simcall::MUTEX_TRYLOCK:
894       label = xbt::string_printf("[(%ld)] Mutex TRYLOCK", issuer->get_pid());
895       break;
896
897     case Simcall::MUTEX_LOCK:
898       label = xbt::string_printf("[(%ld)] Mutex LOCK", issuer->get_pid());
899       break;
900
901     case Simcall::MC_RANDOM:
902       if (issuer->get_host())
903         label = xbt::string_printf("[(%ld)%s] MC_RANDOM (%d)", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
904                                    value);
905       else
906         label = xbt::string_printf("[(%ld)] MC_RANDOM (%d)", issuer->get_pid(), value);
907       break;
908
909     default:
910       THROW_UNIMPLEMENTED;
911   }
912
913   return xbt::string_printf("label = \"%s\", color = %s, fontcolor = %s", label.c_str(), color, color);
914 }
915
916 const char* Api::simcall_get_name(simgrid::simix::Simcall kind) const
917 {
918   return SIMIX_simcall_name(kind);
919 }
920
921 #if HAVE_SMPI
922 int Api::get_smpi_request_tag(smx_simcall_t const& simcall, simgrid::simix::Simcall type) const
923 {
924   simgrid::smpi::Request mpi_request;
925   void* simcall_data = nullptr;
926   if (type == Simcall::COMM_ISEND)
927     simcall_data = simcall_comm_isend__get__data(simcall);
928   else if (type == Simcall::COMM_IRECV)
929     simcall_data = simcall_comm_irecv__get__data(simcall);
930   mc_model_checker->get_remote_simulation().read(&mpi_request, remote(static_cast<smpi::Request*>(simcall_data)));
931   return mpi_request.tag();
932 }
933 #endif
934
935 void Api::restore_state(std::shared_ptr<simgrid::mc::Snapshot> system_state) const
936 {
937   system_state->restore(&mc_model_checker->get_remote_simulation());
938 }
939
940 void Api::log_state() const
941 {
942   session->log_state();
943 }
944
945 bool Api::snapshot_equal(const Snapshot* s1, const Snapshot* s2) const
946 {
947   return simgrid::mc::snapshot_equal(s1, s2);
948 }
949
950 simgrid::mc::Snapshot* Api::take_snapshot(int num_state) const
951 {
952   auto snapshot = new simgrid::mc::Snapshot(num_state);
953   return snapshot;
954 }
955
956 void Api::s_close() const
957 {
958   session->close();
959 }
960
961 void Api::restore_initial_state() const
962 {
963   session->restore_initial_state();
964 }
965
966 void Api::execute(Transition const& transition) const
967 {
968   session->execute(transition);
969 }
970
971 #if SIMGRID_HAVE_MC
972 void Api::automaton_load(const char* file) const
973 {
974   MC_automaton_load(file);
975 }
976 #endif
977
978 std::vector<int> Api::automaton_propositional_symbol_evaluate() const
979 {
980   unsigned int cursor = 0;
981   std::vector<int> values;
982   xbt_automaton_propositional_symbol_t ps = nullptr;
983   xbt_dynar_foreach (mc::property_automaton->propositional_symbols, cursor, ps)
984     values.push_back(xbt_automaton_propositional_symbol_evaluate(ps));
985   return values;
986 }
987
988 std::vector<xbt_automaton_state_t> Api::get_automaton_state() const
989 {
990   std::vector<xbt_automaton_state_t> automaton_stack;
991   unsigned int cursor = 0;
992   xbt_automaton_state_t automaton_state;
993   xbt_dynar_foreach (mc::property_automaton->states, cursor, automaton_state)
994     if (automaton_state->type == -1)
995       automaton_stack.push_back(automaton_state);
996   return automaton_stack;
997 }
998
999 int Api::compare_automaton_exp_label(const xbt_automaton_exp_label* l) const
1000 {
1001   unsigned int cursor                    = 0;
1002   xbt_automaton_propositional_symbol_t p = nullptr;
1003   xbt_dynar_foreach (simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
1004     if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
1005       return cursor;
1006   }
1007   return -1;
1008 }
1009
1010 void Api::set_property_automaton(xbt_automaton_state_t const& automaton_state) const
1011 {
1012   mc::property_automaton->current_state = automaton_state;
1013 }
1014
1015 xbt_automaton_exp_label_t Api::get_automaton_transition_label(xbt_dynar_t const& dynar, int index) const
1016 {
1017   const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
1018   return transition->label;
1019 }
1020
1021 xbt_automaton_state_t Api::get_automaton_transition_dst(xbt_dynar_t const& dynar, int index) const
1022 {
1023   const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
1024   return transition->dst;
1025 }
1026
1027 } // namespace mc
1028 } // namespace simgrid