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"
14 using simgrid::mc::remote;
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_request, mc, "Logging specific to MC (request)");
18 static char *pointer_to_string(void *pointer);
19 static char *buff_size_to_string(size_t size);
21 static inline simgrid::kernel::activity::CommImpl* MC_get_comm(smx_simcall_t r)
24 case SIMCALL_COMM_WAIT:
25 return static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_wait__getraw__comm(r));
26 case SIMCALL_COMM_TEST:
27 return static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_test__getraw__comm(r));
34 smx_mailbox_t MC_get_mbox(smx_simcall_t r)
37 case SIMCALL_COMM_ISEND:
38 return simcall_comm_isend__get__mbox(r);
39 case SIMCALL_COMM_IRECV:
40 return simcall_comm_irecv__get__mbox(r);
51 bool request_depend_asymmetric(smx_simcall_t r1, smx_simcall_t r2)
53 if (r1->call_ == SIMCALL_COMM_ISEND && r2->call_ == SIMCALL_COMM_IRECV)
56 if (r1->call_ == SIMCALL_COMM_IRECV && r2->call_ == SIMCALL_COMM_ISEND)
59 // Those are internal requests, we do not need indirection because those objects are copies:
60 const kernel::activity::CommImpl* synchro1 = MC_get_comm(r1);
61 const kernel::activity::CommImpl* synchro2 = MC_get_comm(r2);
63 if ((r1->call_ == SIMCALL_COMM_ISEND || r1->call_ == SIMCALL_COMM_IRECV) && r2->call_ == SIMCALL_COMM_WAIT) {
64 const kernel::activity::MailboxImpl* mbox = MC_get_mbox(r1);
66 if (mbox != synchro2->mbox_cpy
67 && simcall_comm_wait__get__timeout(r2) <= 0)
70 if ((r1->issuer_ != synchro2->src_actor_.get()) && (r1->issuer_ != synchro2->dst_actor_.get()) &&
71 simcall_comm_wait__get__timeout(r2) <= 0)
74 if ((r1->call_ == SIMCALL_COMM_ISEND) && (synchro2->type_ == kernel::activity::CommImpl::Type::SEND) &&
75 (synchro2->src_buff_ != simcall_comm_isend__get__src_buff(r1)) && simcall_comm_wait__get__timeout(r2) <= 0)
78 if ((r1->call_ == SIMCALL_COMM_IRECV) && (synchro2->type_ == kernel::activity::CommImpl::Type::RECEIVE) &&
79 (synchro2->dst_buff_ != simcall_comm_irecv__get__dst_buff(r1)) && simcall_comm_wait__get__timeout(r2) <= 0)
83 /* FIXME: the following rule assumes that the result of the isend/irecv call is not stored in a buffer used in the
86 if((r1->call == SIMCALL_COMM_ISEND || r1->call == SIMCALL_COMM_IRECV)
87 && r2->call == SIMCALL_COMM_TEST)
91 if (r1->call_ == SIMCALL_COMM_WAIT && (r2->call_ == SIMCALL_COMM_WAIT || r2->call_ == SIMCALL_COMM_TEST) &&
92 (synchro1->src_actor_.get() == nullptr || synchro1->dst_actor_.get() == nullptr))
95 if (r1->call_ == SIMCALL_COMM_TEST &&
96 (simcall_comm_test__get__comm(r1) == nullptr || synchro1->src_buff_ == nullptr || synchro1->dst_buff_ == nullptr))
99 if (r1->call_ == SIMCALL_COMM_TEST && r2->call_ == SIMCALL_COMM_WAIT && synchro1->src_buff_ == synchro2->src_buff_ &&
100 synchro1->dst_buff_ == synchro2->dst_buff_)
103 if (r1->call_ == SIMCALL_COMM_WAIT && r2->call_ == SIMCALL_COMM_TEST && synchro1->src_buff_ != nullptr &&
104 synchro1->dst_buff_ != nullptr && synchro2->src_buff_ != nullptr && synchro2->dst_buff_ != nullptr &&
105 synchro1->dst_buff_ != synchro2->src_buff_ && synchro1->dst_buff_ != synchro2->dst_buff_ &&
106 synchro2->dst_buff_ != synchro1->src_buff_)
112 // Those are internal_req
113 bool request_depend(smx_simcall_t req1, smx_simcall_t req2)
115 if (req1->issuer_ == req2->issuer_)
118 /* Wait with timeout transitions are not considered by the independence theorem, thus we consider them as dependent with all other transitions */
119 if ((req1->call_ == SIMCALL_COMM_WAIT && simcall_comm_wait__get__timeout(req1) > 0) ||
120 (req2->call_ == SIMCALL_COMM_WAIT && simcall_comm_wait__get__timeout(req2) > 0))
123 if (req1->call_ != req2->call_)
124 return request_depend_asymmetric(req1, req2) && request_depend_asymmetric(req2, req1);
126 // Those are internal requests, we do not need indirection because those objects are copies:
127 const kernel::activity::CommImpl* synchro1 = MC_get_comm(req1);
128 const kernel::activity::CommImpl* synchro2 = MC_get_comm(req2);
130 switch (req1->call_) {
131 case SIMCALL_COMM_ISEND:
132 return simcall_comm_isend__get__mbox(req1) == simcall_comm_isend__get__mbox(req2);
133 case SIMCALL_COMM_IRECV:
134 return simcall_comm_irecv__get__mbox(req1) == simcall_comm_irecv__get__mbox(req2);
135 case SIMCALL_COMM_WAIT:
136 if (synchro1->src_buff_ == synchro2->src_buff_ && synchro1->dst_buff_ == synchro2->dst_buff_)
138 if (synchro1->src_buff_ != nullptr && synchro1->dst_buff_ != nullptr && synchro2->src_buff_ != nullptr &&
139 synchro2->dst_buff_ != nullptr && synchro1->dst_buff_ != synchro2->src_buff_ &&
140 synchro1->dst_buff_ != synchro2->dst_buff_ && synchro2->dst_buff_ != synchro1->src_buff_)
149 } // namespace simgrid
151 static char *pointer_to_string(void *pointer)
153 if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
154 return bprintf("%p", pointer);
156 return xbt_strdup("(verbose only)");
159 static char *buff_size_to_string(size_t buff_size)
161 if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
162 return bprintf("%zu", buff_size);
164 return xbt_strdup("(verbose only)");
168 std::string simgrid::mc::request_to_string(smx_simcall_t req, int value, simgrid::mc::RequestType request_type)
170 xbt_assert(mc_model_checker != nullptr, "Must be called from MCer");
172 if (req->inspector_ != nullptr)
173 return req->inspector_->to_string();
175 bool use_remote_comm = true;
176 switch(request_type) {
177 case simgrid::mc::RequestType::simix:
178 use_remote_comm = true;
180 case simgrid::mc::RequestType::executed:
181 case simgrid::mc::RequestType::internal:
182 use_remote_comm = false;
188 const char* type = nullptr;
189 char *args = nullptr;
191 smx_actor_t issuer = MC_smx_simcall_get_issuer(req);
193 switch (req->call_) {
194 case SIMCALL_COMM_ISEND: {
196 char* p = pointer_to_string(simcall_comm_isend__get__src_buff(req));
197 char* bs = buff_size_to_string(simcall_comm_isend__get__src_buff_size(req));
198 if (issuer->get_host())
199 args = bprintf("src=(%ld)%s (%s), buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
200 MC_smx_actor_get_name(issuer), p, bs);
202 args = bprintf("src=(%ld)%s, buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_name(issuer), p, bs);
208 case SIMCALL_COMM_IRECV: {
209 size_t* remote_size = simcall_comm_irecv__get__dst_buff_size(req);
212 mc_model_checker->get_remote_simulation().read_bytes(&size, sizeof(size), remote(remote_size));
215 char* p = pointer_to_string(simcall_comm_irecv__get__dst_buff(req));
216 char* bs = buff_size_to_string(size);
217 if (issuer->get_host())
218 args = bprintf("dst=(%ld)%s (%s), buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
219 MC_smx_actor_get_name(issuer), p, bs);
221 args = bprintf("dst=(%ld)%s, buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_name(issuer), p, bs);
227 case SIMCALL_COMM_WAIT: {
228 simgrid::kernel::activity::CommImpl* remote_act =
229 static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_wait__getraw__comm(req));
232 type = "WaitTimeout";
233 p = pointer_to_string(remote_act);
234 args = bprintf("comm=%s", p);
237 p = pointer_to_string(remote_act);
239 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
240 const simgrid::kernel::activity::CommImpl* act;
241 if (use_remote_comm) {
242 mc_model_checker->get_remote_simulation().read(
243 temp_synchro, remote(static_cast<simgrid::kernel::activity::CommImpl*>(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 =
264 static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_test__getraw__comm(req));
265 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
266 const simgrid::kernel::activity::CommImpl* act;
267 if (use_remote_comm) {
268 mc_model_checker->get_remote_simulation().read(
269 temp_synchro, remote(static_cast<simgrid::kernel::activity::CommImpl*>(remote_act)));
270 act = temp_synchro.get_buffer();
275 if (act->src_actor_.get() == nullptr || act->dst_actor_.get() == nullptr) {
277 p = pointer_to_string(remote_act);
278 args = bprintf("comm=%s", p);
281 p = pointer_to_string(remote_act);
283 smx_actor_t src_proc =
284 mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
285 smx_actor_t dst_proc =
286 mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
287 args = bprintf("comm=%s [(%ld)%s (%s) -> (%ld)%s (%s)]", p, src_proc->get_pid(),
288 MC_smx_actor_get_name(src_proc), MC_smx_actor_get_host_name(src_proc), dst_proc->get_pid(),
289 MC_smx_actor_get_name(dst_proc), MC_smx_actor_get_host_name(dst_proc));
295 case SIMCALL_COMM_WAITANY: {
297 size_t count = simcall_comm_waitany__get__count(req);
299 simgrid::kernel::activity::CommImpl* remote_sync;
301 mc_model_checker->get_remote_simulation().read(remote(simcall_comm_waitany__get__comms(req) + value));
302 char* p = pointer_to_string(remote_sync);
303 args = bprintf("comm=%s (%d of %zu)", p, value + 1, count);
306 args = bprintf("comm at idx %d", value);
310 case SIMCALL_COMM_TESTANY:
312 type = "TestAny FALSE";
313 args = xbt_strdup("-");
316 args = bprintf("(%d of %zu)", value + 1, simcall_comm_testany__get__count(req));
320 case SIMCALL_MUTEX_TRYLOCK:
321 case SIMCALL_MUTEX_LOCK: {
322 if (req->call_ == SIMCALL_MUTEX_LOCK)
325 type = "Mutex TRYLOCK";
327 simgrid::mc::Remote<simgrid::kernel::activity::MutexImpl> mutex;
328 mc_model_checker->get_remote_simulation().read_bytes(mutex.get_buffer(), sizeof(mutex),
329 remote(req->call_ == SIMCALL_MUTEX_LOCK
330 ? simcall_mutex_lock__get__mutex(req)
331 : simcall_mutex_trylock__get__mutex(req)));
332 args = bprintf("locked = %d, owner = %d, sleeping = n/a", mutex.get_buffer()->is_locked(),
333 mutex.get_buffer()->get_owner() != nullptr
334 ? (int)mc_model_checker->get_remote_simulation()
335 .resolve_actor(simgrid::mc::remote(mutex.get_buffer()->get_owner()))
341 case SIMCALL_MC_RANDOM:
343 args = bprintf("%d", value);
347 type = SIMIX_simcall_name(req->call_);
348 args = bprintf("??");
354 str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s(%s)", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
355 MC_smx_actor_get_name(issuer), type, args);
357 str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s ", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
358 MC_smx_actor_get_name(issuer), type);
366 bool request_is_enabled_by_idx(smx_simcall_t req, unsigned int idx)
368 kernel::activity::CommImpl* remote_act = nullptr;
369 switch (req->call_) {
370 case SIMCALL_COMM_WAIT:
371 /* FIXME: check also that src and dst processes are not suspended */
372 remote_act = simcall_comm_wait__getraw__comm(req);
375 case SIMCALL_COMM_WAITANY:
376 remote_act = mc_model_checker->get_remote_simulation().read(remote(simcall_comm_testany__get__comms(req) + idx));
379 case SIMCALL_COMM_TESTANY:
380 remote_act = mc_model_checker->get_remote_simulation().read(remote(simcall_comm_testany__get__comms(req) + idx));
387 Remote<kernel::activity::CommImpl> temp_comm;
388 mc_model_checker->get_remote_simulation().read(temp_comm, remote(remote_act));
389 const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
390 return comm->src_actor_.get() && comm->dst_actor_.get();
393 static const char* colors[] = {
394 "blue", "red", "green3", "goldenrod", "brown", "purple", "magenta",
395 "turquoise4", "gray25", "forestgreen", "hotpink", "lightblue", "tan",
398 static inline const char* get_color(int id)
400 return colors[id % (sizeof(colors) / sizeof(colors[0])) ];
403 std::string request_get_dot_output(smx_simcall_t req, int value)
405 const smx_actor_t issuer = MC_smx_simcall_get_issuer(req);
406 const char* color = get_color(issuer->get_pid() - 1);
408 if (req->inspector_ != nullptr)
409 return simgrid::xbt::string_printf("label = \"%s\", color = %s, fontcolor = %s",
410 req->inspector_->dot_label().c_str(), color, color);
414 switch (req->call_) {
415 case SIMCALL_COMM_ISEND:
416 if (issuer->get_host())
417 label = xbt::string_printf("[(%ld)%s] iSend", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
419 label = bprintf("[(%ld)] iSend", issuer->get_pid());
422 case SIMCALL_COMM_IRECV:
423 if (issuer->get_host())
424 label = xbt::string_printf("[(%ld)%s] iRecv", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
426 label = xbt::string_printf("[(%ld)] iRecv", issuer->get_pid());
429 case SIMCALL_COMM_WAIT:
431 if (issuer->get_host())
432 label = xbt::string_printf("[(%ld)%s] WaitTimeout", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
434 label = xbt::string_printf("[(%ld)] WaitTimeout", issuer->get_pid());
436 kernel::activity::ActivityImpl* remote_act = simcall_comm_wait__getraw__comm(req);
437 Remote<kernel::activity::CommImpl> temp_comm;
438 mc_model_checker->get_remote_simulation().read(temp_comm,
439 remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
440 const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
442 const kernel::actor::ActorImpl* src_proc =
443 mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(comm->src_actor_.get()));
444 const kernel::actor::ActorImpl* dst_proc =
445 mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(comm->dst_actor_.get()));
446 if (issuer->get_host())
448 xbt::string_printf("[(%ld)%s] Wait [(%ld)->(%ld)]", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
449 src_proc ? src_proc->get_pid() : 0, dst_proc ? dst_proc->get_pid() : 0);
451 label = xbt::string_printf("[(%ld)] Wait [(%ld)->(%ld)]", issuer->get_pid(),
452 src_proc ? src_proc->get_pid() : 0, dst_proc ? dst_proc->get_pid() : 0);
456 case SIMCALL_COMM_TEST: {
457 kernel::activity::ActivityImpl* remote_act = simcall_comm_test__getraw__comm(req);
458 Remote<simgrid::kernel::activity::CommImpl> temp_comm;
459 mc_model_checker->get_remote_simulation().read(temp_comm,
460 remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
461 const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
462 if (comm->src_actor_.get() == nullptr || comm->dst_actor_.get() == nullptr) {
463 if (issuer->get_host())
464 label = xbt::string_printf("[(%ld)%s] Test FALSE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
466 label = bprintf("[(%ld)] Test FALSE", issuer->get_pid());
468 if (issuer->get_host())
469 label = xbt::string_printf("[(%ld)%s] Test TRUE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
471 label = xbt::string_printf("[(%ld)] Test TRUE", issuer->get_pid());
476 case SIMCALL_COMM_WAITANY: {
477 size_t comms_size = simcall_comm_waitany__get__count(req);
478 if (issuer->get_host())
479 label = xbt::string_printf("[(%ld)%s] WaitAny [%d of %zu]", issuer->get_pid(),
480 MC_smx_actor_get_host_name(issuer), value + 1, comms_size);
482 label = xbt::string_printf("[(%ld)] WaitAny [%d of %zu]", issuer->get_pid(), value + 1, comms_size);
486 case SIMCALL_COMM_TESTANY:
488 if (issuer->get_host())
489 label = xbt::string_printf("[(%ld)%s] TestAny FALSE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
491 label = xbt::string_printf("[(%ld)] TestAny FALSE", issuer->get_pid());
493 if (issuer->get_host())
495 xbt::string_printf("[(%ld)%s] TestAny TRUE [%d of %lu]", issuer->get_pid(),
496 MC_smx_actor_get_host_name(issuer), value + 1, simcall_comm_testany__get__count(req));
498 label = xbt::string_printf("[(%ld)] TestAny TRUE [%d of %lu]", issuer->get_pid(), value + 1,
499 simcall_comm_testany__get__count(req));
503 case SIMCALL_MUTEX_TRYLOCK:
504 label = xbt::string_printf("[(%ld)] Mutex TRYLOCK", issuer->get_pid());
507 case SIMCALL_MUTEX_LOCK:
508 label = xbt::string_printf("[(%ld)] Mutex LOCK", issuer->get_pid());
511 case SIMCALL_MC_RANDOM:
512 if (issuer->get_host())
513 label = xbt::string_printf("[(%ld)%s] MC_RANDOM (%d)", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
516 label = xbt::string_printf("[(%ld)] MC_RANDOM (%d)", issuer->get_pid(), value);
523 return xbt::string_printf("label = \"%s\", color = %s, fontcolor = %s", label.c_str(), color, color);
527 } // namespace simgrid