Logo AND Algorithmique Numérique Distribuée

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