1 /* Copyright (c) 2008-2020. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "src/mc/mc_request.hpp"
7 #include "src/include/mc/mc.h"
8 #include "src/kernel/activity/CommImpl.hpp"
9 #include "src/kernel/activity/MutexImpl.hpp"
10 #include "src/mc/ModelChecker.hpp"
11 #include "src/mc/checker/SimcallInspector.hpp"
12 #include "src/mc/mc_smx.hpp"
15 using simgrid::mc::remote;
16 using simgrid::simix::Simcall;
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_request, mc, "Logging specific to MC (request)");
20 static char *pointer_to_string(void *pointer);
21 static char *buff_size_to_string(size_t size);
23 static inline simgrid::kernel::activity::CommImpl* MC_get_comm(smx_simcall_t r)
26 case Simcall::COMM_WAIT:
27 return simcall_comm_wait__getraw__comm(r);
28 case Simcall::COMM_TEST:
29 return simcall_comm_test__getraw__comm(r);
36 smx_mailbox_t MC_get_mbox(smx_simcall_t r)
39 case Simcall::COMM_ISEND:
40 return simcall_comm_isend__get__mbox(r);
41 case Simcall::COMM_IRECV:
42 return simcall_comm_irecv__get__mbox(r);
53 bool request_depend_asymmetric(smx_simcall_t r1, smx_simcall_t r2)
55 if (r1->call_ == Simcall::COMM_ISEND && r2->call_ == Simcall::COMM_IRECV)
58 if (r1->call_ == Simcall::COMM_IRECV && r2->call_ == Simcall::COMM_ISEND)
61 // Those are internal requests, we do not need indirection because those objects are copies:
62 const kernel::activity::CommImpl* synchro1 = MC_get_comm(r1);
63 const kernel::activity::CommImpl* synchro2 = MC_get_comm(r2);
65 if ((r1->call_ == Simcall::COMM_ISEND || r1->call_ == Simcall::COMM_IRECV) && r2->call_ == Simcall::COMM_WAIT) {
66 const kernel::activity::MailboxImpl* mbox = MC_get_mbox(r1);
68 if (mbox != synchro2->mbox_cpy
69 && simcall_comm_wait__get__timeout(r2) <= 0)
72 if ((r1->issuer_ != synchro2->src_actor_.get()) && (r1->issuer_ != synchro2->dst_actor_.get()) &&
73 simcall_comm_wait__get__timeout(r2) <= 0)
76 if ((r1->call_ == Simcall::COMM_ISEND) && (synchro2->type_ == kernel::activity::CommImpl::Type::SEND) &&
77 (synchro2->src_buff_ != simcall_comm_isend__get__src_buff(r1)) && simcall_comm_wait__get__timeout(r2) <= 0)
80 if ((r1->call_ == Simcall::COMM_IRECV) && (synchro2->type_ == kernel::activity::CommImpl::Type::RECEIVE) &&
81 (synchro2->dst_buff_ != simcall_comm_irecv__get__dst_buff(r1)) && simcall_comm_wait__get__timeout(r2) <= 0)
85 /* FIXME: the following rule assumes that the result of the isend/irecv call is not stored in a buffer used in the
88 if((r1->call == Simcall::COMM_ISEND || r1->call == Simcall::COMM_IRECV)
89 && r2->call == Simcall::COMM_TEST)
93 if (r1->call_ == Simcall::COMM_WAIT && (r2->call_ == Simcall::COMM_WAIT || r2->call_ == Simcall::COMM_TEST) &&
94 (synchro1->src_actor_.get() == nullptr || synchro1->dst_actor_.get() == nullptr))
97 if (r1->call_ == Simcall::COMM_TEST &&
98 (simcall_comm_test__get__comm(r1) == nullptr || synchro1->src_buff_ == nullptr || synchro1->dst_buff_ == nullptr))
101 if (r1->call_ == Simcall::COMM_TEST && r2->call_ == Simcall::COMM_WAIT &&
102 synchro1->src_buff_ == synchro2->src_buff_ && synchro1->dst_buff_ == synchro2->dst_buff_)
105 if (r1->call_ == Simcall::COMM_WAIT && r2->call_ == Simcall::COMM_TEST && synchro1->src_buff_ != nullptr &&
106 synchro1->dst_buff_ != nullptr && synchro2->src_buff_ != nullptr && synchro2->dst_buff_ != nullptr &&
107 synchro1->dst_buff_ != synchro2->src_buff_ && synchro1->dst_buff_ != synchro2->dst_buff_ &&
108 synchro2->dst_buff_ != synchro1->src_buff_)
114 // Those are internal_req
115 bool request_depend(smx_simcall_t req1, smx_simcall_t req2)
117 if (req1->issuer_ == req2->issuer_)
120 /* Wait with timeout transitions are not considered by the independence theorem, thus we consider them as dependent with all other transitions */
121 if ((req1->call_ == Simcall::COMM_WAIT && simcall_comm_wait__get__timeout(req1) > 0) ||
122 (req2->call_ == Simcall::COMM_WAIT && simcall_comm_wait__get__timeout(req2) > 0))
125 if (req1->call_ != req2->call_)
126 return request_depend_asymmetric(req1, req2) && request_depend_asymmetric(req2, req1);
128 // Those are internal requests, we do not need indirection because those objects are copies:
129 const kernel::activity::CommImpl* synchro1 = MC_get_comm(req1);
130 const kernel::activity::CommImpl* synchro2 = MC_get_comm(req2);
132 switch (req1->call_) {
133 case Simcall::COMM_ISEND:
134 return simcall_comm_isend__get__mbox(req1) == simcall_comm_isend__get__mbox(req2);
135 case Simcall::COMM_IRECV:
136 return simcall_comm_irecv__get__mbox(req1) == simcall_comm_irecv__get__mbox(req2);
137 case Simcall::COMM_WAIT:
138 if (synchro1->src_buff_ == synchro2->src_buff_ && synchro1->dst_buff_ == synchro2->dst_buff_)
140 if (synchro1->src_buff_ != nullptr && synchro1->dst_buff_ != nullptr && synchro2->src_buff_ != nullptr &&
141 synchro2->dst_buff_ != nullptr && synchro1->dst_buff_ != synchro2->src_buff_ &&
142 synchro1->dst_buff_ != synchro2->dst_buff_ && synchro2->dst_buff_ != synchro1->src_buff_)
151 } // namespace simgrid
153 static char *pointer_to_string(void *pointer)
155 if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
156 return bprintf("%p", pointer);
158 return xbt_strdup("(verbose only)");
161 static char *buff_size_to_string(size_t buff_size)
163 if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
164 return bprintf("%zu", buff_size);
166 return xbt_strdup("(verbose only)");
170 std::string simgrid::mc::request_to_string(smx_simcall_t req, int value, simgrid::mc::RequestType request_type)
172 xbt_assert(mc_model_checker != nullptr, "Must be called from MCer");
174 if (req->inspector_ != nullptr)
175 return req->inspector_->to_string();
177 bool use_remote_comm = true;
178 switch(request_type) {
179 case simgrid::mc::RequestType::simix:
180 use_remote_comm = true;
182 case simgrid::mc::RequestType::executed:
183 case simgrid::mc::RequestType::internal:
184 use_remote_comm = false;
190 const char* type = nullptr;
191 char *args = nullptr;
193 smx_actor_t issuer = MC_smx_simcall_get_issuer(req);
195 switch (req->call_) {
196 case Simcall::COMM_ISEND: {
198 char* p = pointer_to_string(simcall_comm_isend__get__src_buff(req));
199 char* bs = buff_size_to_string(simcall_comm_isend__get__src_buff_size(req));
200 if (issuer->get_host())
201 args = bprintf("src=(%ld)%s (%s), buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
202 MC_smx_actor_get_name(issuer), p, bs);
204 args = bprintf("src=(%ld)%s, buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_name(issuer), p, bs);
210 case Simcall::COMM_IRECV: {
211 size_t* remote_size = simcall_comm_irecv__get__dst_buff_size(req);
214 mc_model_checker->get_remote_simulation().read_bytes(&size, sizeof(size), remote(remote_size));
217 char* p = pointer_to_string(simcall_comm_irecv__get__dst_buff(req));
218 char* bs = buff_size_to_string(size);
219 if (issuer->get_host())
220 args = bprintf("dst=(%ld)%s (%s), buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
221 MC_smx_actor_get_name(issuer), p, bs);
223 args = bprintf("dst=(%ld)%s, buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_name(issuer), p, bs);
229 case Simcall::COMM_WAIT: {
230 simgrid::kernel::activity::CommImpl* remote_act = simcall_comm_wait__getraw__comm(req);
233 type = "WaitTimeout";
234 p = pointer_to_string(remote_act);
235 args = bprintf("comm=%s", p);
238 p = pointer_to_string(remote_act);
240 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
241 const simgrid::kernel::activity::CommImpl* act;
242 if (use_remote_comm) {
243 mc_model_checker->get_remote_simulation().read(temp_synchro, remote(remote_act));
244 act = temp_synchro.get_buffer();
248 smx_actor_t src_proc =
249 mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
250 smx_actor_t dst_proc =
251 mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
252 args = bprintf("comm=%s [(%ld)%s (%s)-> (%ld)%s (%s)]", p, src_proc ? src_proc->get_pid() : 0,
253 src_proc ? MC_smx_actor_get_host_name(src_proc) : "",
254 src_proc ? MC_smx_actor_get_name(src_proc) : "", dst_proc ? dst_proc->get_pid() : 0,
255 dst_proc ? MC_smx_actor_get_host_name(dst_proc) : "",
256 dst_proc ? MC_smx_actor_get_name(dst_proc) : "");
262 case Simcall::COMM_TEST: {
263 simgrid::kernel::activity::CommImpl* remote_act = simcall_comm_test__getraw__comm(req);
264 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
265 const simgrid::kernel::activity::CommImpl* act;
266 if (use_remote_comm) {
267 mc_model_checker->get_remote_simulation().read(temp_synchro, remote(remote_act));
268 act = temp_synchro.get_buffer();
273 if (act->src_actor_.get() == nullptr || act->dst_actor_.get() == nullptr) {
275 p = pointer_to_string(remote_act);
276 args = bprintf("comm=%s", p);
279 p = pointer_to_string(remote_act);
281 smx_actor_t src_proc =
282 mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
283 smx_actor_t dst_proc =
284 mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
285 args = bprintf("comm=%s [(%ld)%s (%s) -> (%ld)%s (%s)]", p, src_proc->get_pid(),
286 MC_smx_actor_get_name(src_proc), MC_smx_actor_get_host_name(src_proc), dst_proc->get_pid(),
287 MC_smx_actor_get_name(dst_proc), MC_smx_actor_get_host_name(dst_proc));
293 case Simcall::COMM_WAITANY: {
295 size_t count = simcall_comm_waitany__get__count(req);
297 simgrid::kernel::activity::CommImpl* remote_sync;
299 mc_model_checker->get_remote_simulation().read(remote(simcall_comm_waitany__get__comms(req) + value));
300 char* p = pointer_to_string(remote_sync);
301 args = bprintf("comm=%s (%d of %zu)", p, value + 1, count);
304 args = bprintf("comm at idx %d", value);
308 case Simcall::COMM_TESTANY:
310 type = "TestAny FALSE";
311 args = xbt_strdup("-");
314 args = bprintf("(%d of %zu)", value + 1, simcall_comm_testany__get__count(req));
318 case Simcall::MUTEX_TRYLOCK:
319 case Simcall::MUTEX_LOCK: {
320 if (req->call_ == Simcall::MUTEX_LOCK)
323 type = "Mutex TRYLOCK";
325 simgrid::mc::Remote<simgrid::kernel::activity::MutexImpl> mutex;
326 mc_model_checker->get_remote_simulation().read_bytes(mutex.get_buffer(), sizeof(mutex),
327 remote(req->call_ == Simcall::MUTEX_LOCK
328 ? simcall_mutex_lock__get__mutex(req)
329 : simcall_mutex_trylock__get__mutex(req)));
330 args = bprintf("locked = %d, owner = %d, sleeping = n/a", mutex.get_buffer()->is_locked(),
331 mutex.get_buffer()->get_owner() != nullptr
332 ? (int)mc_model_checker->get_remote_simulation()
333 .resolve_actor(simgrid::mc::remote(mutex.get_buffer()->get_owner()))
339 case Simcall::MC_RANDOM:
341 args = bprintf("%d", value);
345 type = SIMIX_simcall_name(req->call_);
346 args = bprintf("??");
352 str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s(%s)", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
353 MC_smx_actor_get_name(issuer), type, args);
355 str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s ", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
356 MC_smx_actor_get_name(issuer), type);
364 bool request_is_enabled_by_idx(smx_simcall_t req, unsigned int idx)
366 kernel::activity::CommImpl* remote_act = nullptr;
367 switch (req->call_) {
368 case Simcall::COMM_WAIT:
369 /* FIXME: check also that src and dst processes are not suspended */
370 remote_act = simcall_comm_wait__getraw__comm(req);
373 case Simcall::COMM_WAITANY:
374 remote_act = mc_model_checker->get_remote_simulation().read(remote(simcall_comm_waitany__get__comms(req) + idx));
377 case Simcall::COMM_TESTANY:
378 remote_act = mc_model_checker->get_remote_simulation().read(remote(simcall_comm_testany__get__comms(req) + idx));
385 Remote<kernel::activity::CommImpl> temp_comm;
386 mc_model_checker->get_remote_simulation().read(temp_comm, remote(remote_act));
387 const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
388 return comm->src_actor_.get() && comm->dst_actor_.get();
391 static inline const char* get_color(int id)
393 static constexpr std::array<const char*, 13> colors{{"blue", "red", "green3", "goldenrod", "brown", "purple",
394 "magenta", "turquoise4", "gray25", "forestgreen", "hotpink",
395 "lightblue", "tan"}};
396 return colors[id % colors.size()];
399 std::string request_get_dot_output(smx_simcall_t req, int value)
401 const smx_actor_t issuer = MC_smx_simcall_get_issuer(req);
402 const char* color = get_color(issuer->get_pid() - 1);
404 if (req->inspector_ != nullptr)
405 return simgrid::xbt::string_printf("label = \"%s\", color = %s, fontcolor = %s",
406 req->inspector_->dot_label().c_str(), color, color);
410 switch (req->call_) {
411 case Simcall::COMM_ISEND:
412 if (issuer->get_host())
413 label = xbt::string_printf("[(%ld)%s] iSend", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
415 label = bprintf("[(%ld)] iSend", issuer->get_pid());
418 case Simcall::COMM_IRECV:
419 if (issuer->get_host())
420 label = xbt::string_printf("[(%ld)%s] iRecv", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
422 label = xbt::string_printf("[(%ld)] iRecv", issuer->get_pid());
425 case Simcall::COMM_WAIT:
427 if (issuer->get_host())
428 label = xbt::string_printf("[(%ld)%s] WaitTimeout", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
430 label = xbt::string_printf("[(%ld)] WaitTimeout", issuer->get_pid());
432 kernel::activity::ActivityImpl* remote_act = simcall_comm_wait__getraw__comm(req);
433 Remote<kernel::activity::CommImpl> temp_comm;
434 mc_model_checker->get_remote_simulation().read(temp_comm,
435 remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
436 const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
438 const kernel::actor::ActorImpl* src_proc =
439 mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(comm->src_actor_.get()));
440 const kernel::actor::ActorImpl* dst_proc =
441 mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(comm->dst_actor_.get()));
442 if (issuer->get_host())
444 xbt::string_printf("[(%ld)%s] Wait [(%ld)->(%ld)]", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
445 src_proc ? src_proc->get_pid() : 0, dst_proc ? dst_proc->get_pid() : 0);
447 label = xbt::string_printf("[(%ld)] Wait [(%ld)->(%ld)]", issuer->get_pid(),
448 src_proc ? src_proc->get_pid() : 0, dst_proc ? dst_proc->get_pid() : 0);
452 case Simcall::COMM_TEST: {
453 kernel::activity::ActivityImpl* remote_act = simcall_comm_test__getraw__comm(req);
454 Remote<simgrid::kernel::activity::CommImpl> temp_comm;
455 mc_model_checker->get_remote_simulation().read(temp_comm,
456 remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
457 const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
458 if (comm->src_actor_.get() == nullptr || comm->dst_actor_.get() == nullptr) {
459 if (issuer->get_host())
460 label = xbt::string_printf("[(%ld)%s] Test FALSE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
462 label = bprintf("[(%ld)] Test FALSE", issuer->get_pid());
464 if (issuer->get_host())
465 label = xbt::string_printf("[(%ld)%s] Test TRUE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
467 label = xbt::string_printf("[(%ld)] Test TRUE", issuer->get_pid());
472 case Simcall::COMM_WAITANY: {
473 size_t comms_size = simcall_comm_waitany__get__count(req);
474 if (issuer->get_host())
475 label = xbt::string_printf("[(%ld)%s] WaitAny [%d of %zu]", issuer->get_pid(),
476 MC_smx_actor_get_host_name(issuer), value + 1, comms_size);
478 label = xbt::string_printf("[(%ld)] WaitAny [%d of %zu]", issuer->get_pid(), value + 1, comms_size);
482 case Simcall::COMM_TESTANY:
484 if (issuer->get_host())
485 label = xbt::string_printf("[(%ld)%s] TestAny FALSE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
487 label = xbt::string_printf("[(%ld)] TestAny FALSE", issuer->get_pid());
489 if (issuer->get_host())
491 xbt::string_printf("[(%ld)%s] TestAny TRUE [%d of %lu]", issuer->get_pid(),
492 MC_smx_actor_get_host_name(issuer), value + 1, simcall_comm_testany__get__count(req));
494 label = xbt::string_printf("[(%ld)] TestAny TRUE [%d of %lu]", issuer->get_pid(), value + 1,
495 simcall_comm_testany__get__count(req));
499 case Simcall::MUTEX_TRYLOCK:
500 label = xbt::string_printf("[(%ld)] Mutex TRYLOCK", issuer->get_pid());
503 case Simcall::MUTEX_LOCK:
504 label = xbt::string_printf("[(%ld)] Mutex LOCK", issuer->get_pid());
507 case Simcall::MC_RANDOM:
508 if (issuer->get_host())
509 label = xbt::string_printf("[(%ld)%s] MC_RANDOM (%d)", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
512 label = xbt::string_printf("[(%ld)] MC_RANDOM (%d)", issuer->get_pid(), value);
519 return xbt::string_printf("label = \"%s\", color = %s, fontcolor = %s", label.c_str(), color, color);
523 } // namespace simgrid