Logo AND Algorithmique Numérique Distribuée

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