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;
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_request, mc, "Logging specific to MC (request)");
19 static char *pointer_to_string(void *pointer);
20 static char *buff_size_to_string(size_t size);
22 static inline simgrid::kernel::activity::CommImpl* MC_get_comm(smx_simcall_t r)
25 case SIMCALL_COMM_WAIT:
26 return simcall_comm_wait__getraw__comm(r);
27 case SIMCALL_COMM_TEST:
28 return simcall_comm_test__getraw__comm(r);
35 smx_mailbox_t MC_get_mbox(smx_simcall_t r)
38 case SIMCALL_COMM_ISEND:
39 return simcall_comm_isend__get__mbox(r);
40 case SIMCALL_COMM_IRECV:
41 return simcall_comm_irecv__get__mbox(r);
52 bool request_depend_asymmetric(smx_simcall_t r1, smx_simcall_t r2)
54 if (r1->call_ == SIMCALL_COMM_ISEND && r2->call_ == SIMCALL_COMM_IRECV)
57 if (r1->call_ == SIMCALL_COMM_IRECV && r2->call_ == SIMCALL_COMM_ISEND)
60 // Those are internal requests, we do not need indirection because those objects are copies:
61 const kernel::activity::CommImpl* synchro1 = MC_get_comm(r1);
62 const kernel::activity::CommImpl* synchro2 = MC_get_comm(r2);
64 if ((r1->call_ == SIMCALL_COMM_ISEND || r1->call_ == SIMCALL_COMM_IRECV) && r2->call_ == SIMCALL_COMM_WAIT) {
65 const kernel::activity::MailboxImpl* mbox = MC_get_mbox(r1);
67 if (mbox != synchro2->mbox_cpy
68 && simcall_comm_wait__get__timeout(r2) <= 0)
71 if ((r1->issuer_ != synchro2->src_actor_.get()) && (r1->issuer_ != synchro2->dst_actor_.get()) &&
72 simcall_comm_wait__get__timeout(r2) <= 0)
75 if ((r1->call_ == SIMCALL_COMM_ISEND) && (synchro2->type_ == kernel::activity::CommImpl::Type::SEND) &&
76 (synchro2->src_buff_ != simcall_comm_isend__get__src_buff(r1)) && simcall_comm_wait__get__timeout(r2) <= 0)
79 if ((r1->call_ == SIMCALL_COMM_IRECV) && (synchro2->type_ == kernel::activity::CommImpl::Type::RECEIVE) &&
80 (synchro2->dst_buff_ != simcall_comm_irecv__get__dst_buff(r1)) && simcall_comm_wait__get__timeout(r2) <= 0)
84 /* FIXME: the following rule assumes that the result of the isend/irecv call is not stored in a buffer used in the
87 if((r1->call == SIMCALL_COMM_ISEND || r1->call == SIMCALL_COMM_IRECV)
88 && r2->call == SIMCALL_COMM_TEST)
92 if (r1->call_ == SIMCALL_COMM_WAIT && (r2->call_ == SIMCALL_COMM_WAIT || r2->call_ == SIMCALL_COMM_TEST) &&
93 (synchro1->src_actor_.get() == nullptr || synchro1->dst_actor_.get() == nullptr))
96 if (r1->call_ == SIMCALL_COMM_TEST &&
97 (simcall_comm_test__get__comm(r1) == nullptr || synchro1->src_buff_ == nullptr || synchro1->dst_buff_ == nullptr))
100 if (r1->call_ == SIMCALL_COMM_TEST && r2->call_ == SIMCALL_COMM_WAIT && synchro1->src_buff_ == synchro2->src_buff_ &&
101 synchro1->dst_buff_ == synchro2->dst_buff_)
104 if (r1->call_ == SIMCALL_COMM_WAIT && r2->call_ == SIMCALL_COMM_TEST && synchro1->src_buff_ != nullptr &&
105 synchro1->dst_buff_ != nullptr && synchro2->src_buff_ != nullptr && synchro2->dst_buff_ != nullptr &&
106 synchro1->dst_buff_ != synchro2->src_buff_ && synchro1->dst_buff_ != synchro2->dst_buff_ &&
107 synchro2->dst_buff_ != synchro1->src_buff_)
113 // Those are internal_req
114 bool request_depend(smx_simcall_t req1, smx_simcall_t req2)
116 if (req1->issuer_ == req2->issuer_)
119 /* Wait with timeout transitions are not considered by the independence theorem, thus we consider them as dependent with all other transitions */
120 if ((req1->call_ == SIMCALL_COMM_WAIT && simcall_comm_wait__get__timeout(req1) > 0) ||
121 (req2->call_ == SIMCALL_COMM_WAIT && simcall_comm_wait__get__timeout(req2) > 0))
124 if (req1->call_ != req2->call_)
125 return request_depend_asymmetric(req1, req2) && request_depend_asymmetric(req2, req1);
127 // Those are internal requests, we do not need indirection because those objects are copies:
128 const kernel::activity::CommImpl* synchro1 = MC_get_comm(req1);
129 const kernel::activity::CommImpl* synchro2 = MC_get_comm(req2);
131 switch (req1->call_) {
132 case SIMCALL_COMM_ISEND:
133 return simcall_comm_isend__get__mbox(req1) == simcall_comm_isend__get__mbox(req2);
134 case SIMCALL_COMM_IRECV:
135 return simcall_comm_irecv__get__mbox(req1) == simcall_comm_irecv__get__mbox(req2);
136 case SIMCALL_COMM_WAIT:
137 if (synchro1->src_buff_ == synchro2->src_buff_ && synchro1->dst_buff_ == synchro2->dst_buff_)
139 if (synchro1->src_buff_ != nullptr && synchro1->dst_buff_ != nullptr && synchro2->src_buff_ != nullptr &&
140 synchro2->dst_buff_ != nullptr && synchro1->dst_buff_ != synchro2->src_buff_ &&
141 synchro1->dst_buff_ != synchro2->dst_buff_ && synchro2->dst_buff_ != synchro1->src_buff_)
150 } // namespace simgrid
152 static char *pointer_to_string(void *pointer)
154 if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
155 return bprintf("%p", pointer);
157 return xbt_strdup("(verbose only)");
160 static char *buff_size_to_string(size_t buff_size)
162 if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
163 return bprintf("%zu", buff_size);
165 return xbt_strdup("(verbose only)");
169 std::string simgrid::mc::request_to_string(smx_simcall_t req, int value, simgrid::mc::RequestType request_type)
171 xbt_assert(mc_model_checker != nullptr, "Must be called from MCer");
173 if (req->inspector_ != nullptr)
174 return req->inspector_->to_string();
176 bool use_remote_comm = true;
177 switch(request_type) {
178 case simgrid::mc::RequestType::simix:
179 use_remote_comm = true;
181 case simgrid::mc::RequestType::executed:
182 case simgrid::mc::RequestType::internal:
183 use_remote_comm = false;
189 const char* type = nullptr;
190 char *args = nullptr;
192 smx_actor_t issuer = MC_smx_simcall_get_issuer(req);
194 switch (req->call_) {
195 case SIMCALL_COMM_ISEND: {
197 char* p = pointer_to_string(simcall_comm_isend__get__src_buff(req));
198 char* bs = buff_size_to_string(simcall_comm_isend__get__src_buff_size(req));
199 if (issuer->get_host())
200 args = bprintf("src=(%ld)%s (%s), buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
201 MC_smx_actor_get_name(issuer), p, bs);
203 args = bprintf("src=(%ld)%s, buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_name(issuer), p, bs);
209 case SIMCALL_COMM_IRECV: {
210 size_t* remote_size = simcall_comm_irecv__get__dst_buff_size(req);
213 mc_model_checker->get_remote_simulation().read_bytes(&size, sizeof(size), remote(remote_size));
216 char* p = pointer_to_string(simcall_comm_irecv__get__dst_buff(req));
217 char* bs = buff_size_to_string(size);
218 if (issuer->get_host())
219 args = bprintf("dst=(%ld)%s (%s), buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
220 MC_smx_actor_get_name(issuer), p, bs);
222 args = bprintf("dst=(%ld)%s, buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_name(issuer), p, bs);
228 case SIMCALL_COMM_WAIT: {
229 simgrid::kernel::activity::CommImpl* remote_act = 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(temp_synchro, remote(remote_act));
243 act = temp_synchro.get_buffer();
247 smx_actor_t src_proc =
248 mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
249 smx_actor_t dst_proc =
250 mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
251 args = bprintf("comm=%s [(%ld)%s (%s)-> (%ld)%s (%s)]", p, src_proc ? src_proc->get_pid() : 0,
252 src_proc ? MC_smx_actor_get_host_name(src_proc) : "",
253 src_proc ? MC_smx_actor_get_name(src_proc) : "", dst_proc ? dst_proc->get_pid() : 0,
254 dst_proc ? MC_smx_actor_get_host_name(dst_proc) : "",
255 dst_proc ? MC_smx_actor_get_name(dst_proc) : "");
261 case SIMCALL_COMM_TEST: {
262 simgrid::kernel::activity::CommImpl* remote_act = simcall_comm_test__getraw__comm(req);
263 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
264 const simgrid::kernel::activity::CommImpl* act;
265 if (use_remote_comm) {
266 mc_model_checker->get_remote_simulation().read(temp_synchro, remote(remote_act));
267 act = temp_synchro.get_buffer();
272 if (act->src_actor_.get() == nullptr || act->dst_actor_.get() == nullptr) {
274 p = pointer_to_string(remote_act);
275 args = bprintf("comm=%s", p);
278 p = pointer_to_string(remote_act);
280 smx_actor_t src_proc =
281 mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
282 smx_actor_t dst_proc =
283 mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
284 args = bprintf("comm=%s [(%ld)%s (%s) -> (%ld)%s (%s)]", p, src_proc->get_pid(),
285 MC_smx_actor_get_name(src_proc), MC_smx_actor_get_host_name(src_proc), dst_proc->get_pid(),
286 MC_smx_actor_get_name(dst_proc), MC_smx_actor_get_host_name(dst_proc));
292 case SIMCALL_COMM_WAITANY: {
294 size_t count = simcall_comm_waitany__get__count(req);
296 simgrid::kernel::activity::CommImpl* remote_sync;
298 mc_model_checker->get_remote_simulation().read(remote(simcall_comm_waitany__get__comms(req) + value));
299 char* p = pointer_to_string(remote_sync);
300 args = bprintf("comm=%s (%d of %zu)", p, value + 1, count);
303 args = bprintf("comm at idx %d", value);
307 case SIMCALL_COMM_TESTANY:
309 type = "TestAny FALSE";
310 args = xbt_strdup("-");
313 args = bprintf("(%d of %zu)", value + 1, simcall_comm_testany__get__count(req));
317 case SIMCALL_MUTEX_TRYLOCK:
318 case SIMCALL_MUTEX_LOCK: {
319 if (req->call_ == SIMCALL_MUTEX_LOCK)
322 type = "Mutex TRYLOCK";
324 simgrid::mc::Remote<simgrid::kernel::activity::MutexImpl> mutex;
325 mc_model_checker->get_remote_simulation().read_bytes(mutex.get_buffer(), sizeof(mutex),
326 remote(req->call_ == SIMCALL_MUTEX_LOCK
327 ? simcall_mutex_lock__get__mutex(req)
328 : simcall_mutex_trylock__get__mutex(req)));
329 args = bprintf("locked = %d, owner = %d, sleeping = n/a", mutex.get_buffer()->is_locked(),
330 mutex.get_buffer()->get_owner() != nullptr
331 ? (int)mc_model_checker->get_remote_simulation()
332 .resolve_actor(simgrid::mc::remote(mutex.get_buffer()->get_owner()))
338 case SIMCALL_MC_RANDOM:
340 args = bprintf("%d", value);
344 type = SIMIX_simcall_name(req->call_);
345 args = bprintf("??");
351 str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s(%s)", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
352 MC_smx_actor_get_name(issuer), type, args);
354 str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s ", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
355 MC_smx_actor_get_name(issuer), type);
363 bool request_is_enabled_by_idx(smx_simcall_t req, unsigned int idx)
365 kernel::activity::CommImpl* remote_act = nullptr;
366 switch (req->call_) {
367 case SIMCALL_COMM_WAIT:
368 /* FIXME: check also that src and dst processes are not suspended */
369 remote_act = simcall_comm_wait__getraw__comm(req);
372 case SIMCALL_COMM_WAITANY:
373 remote_act = mc_model_checker->get_remote_simulation().read(remote(simcall_comm_testany__get__comms(req) + idx));
376 case SIMCALL_COMM_TESTANY:
377 remote_act = mc_model_checker->get_remote_simulation().read(remote(simcall_comm_testany__get__comms(req) + idx));
384 Remote<kernel::activity::CommImpl> temp_comm;
385 mc_model_checker->get_remote_simulation().read(temp_comm, remote(remote_act));
386 const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
387 return comm->src_actor_.get() && comm->dst_actor_.get();
390 static inline const char* get_color(int id)
392 static constexpr std::array<const char*, 13> colors{{"blue", "red", "green3", "goldenrod", "brown", "purple",
393 "magenta", "turquoise4", "gray25", "forestgreen", "hotpink",
394 "lightblue", "tan"}};
395 return colors[id % colors.size()];
398 std::string request_get_dot_output(smx_simcall_t req, int value)
400 const smx_actor_t issuer = MC_smx_simcall_get_issuer(req);
401 const char* color = get_color(issuer->get_pid() - 1);
403 if (req->inspector_ != nullptr)
404 return simgrid::xbt::string_printf("label = \"%s\", color = %s, fontcolor = %s",
405 req->inspector_->dot_label().c_str(), color, color);
409 switch (req->call_) {
410 case SIMCALL_COMM_ISEND:
411 if (issuer->get_host())
412 label = xbt::string_printf("[(%ld)%s] iSend", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
414 label = bprintf("[(%ld)] iSend", issuer->get_pid());
417 case SIMCALL_COMM_IRECV:
418 if (issuer->get_host())
419 label = xbt::string_printf("[(%ld)%s] iRecv", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
421 label = xbt::string_printf("[(%ld)] iRecv", issuer->get_pid());
424 case SIMCALL_COMM_WAIT:
426 if (issuer->get_host())
427 label = xbt::string_printf("[(%ld)%s] WaitTimeout", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
429 label = xbt::string_printf("[(%ld)] WaitTimeout", issuer->get_pid());
431 kernel::activity::ActivityImpl* remote_act = simcall_comm_wait__getraw__comm(req);
432 Remote<kernel::activity::CommImpl> temp_comm;
433 mc_model_checker->get_remote_simulation().read(temp_comm,
434 remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
435 const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
437 const kernel::actor::ActorImpl* src_proc =
438 mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(comm->src_actor_.get()));
439 const kernel::actor::ActorImpl* dst_proc =
440 mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(comm->dst_actor_.get()));
441 if (issuer->get_host())
443 xbt::string_printf("[(%ld)%s] Wait [(%ld)->(%ld)]", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
444 src_proc ? src_proc->get_pid() : 0, dst_proc ? dst_proc->get_pid() : 0);
446 label = xbt::string_printf("[(%ld)] Wait [(%ld)->(%ld)]", issuer->get_pid(),
447 src_proc ? src_proc->get_pid() : 0, dst_proc ? dst_proc->get_pid() : 0);
451 case SIMCALL_COMM_TEST: {
452 kernel::activity::ActivityImpl* remote_act = simcall_comm_test__getraw__comm(req);
453 Remote<simgrid::kernel::activity::CommImpl> temp_comm;
454 mc_model_checker->get_remote_simulation().read(temp_comm,
455 remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
456 const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
457 if (comm->src_actor_.get() == nullptr || comm->dst_actor_.get() == nullptr) {
458 if (issuer->get_host())
459 label = xbt::string_printf("[(%ld)%s] Test FALSE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
461 label = bprintf("[(%ld)] Test FALSE", issuer->get_pid());
463 if (issuer->get_host())
464 label = xbt::string_printf("[(%ld)%s] Test TRUE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
466 label = xbt::string_printf("[(%ld)] Test TRUE", issuer->get_pid());
471 case SIMCALL_COMM_WAITANY: {
472 size_t comms_size = simcall_comm_waitany__get__count(req);
473 if (issuer->get_host())
474 label = xbt::string_printf("[(%ld)%s] WaitAny [%d of %zu]", issuer->get_pid(),
475 MC_smx_actor_get_host_name(issuer), value + 1, comms_size);
477 label = xbt::string_printf("[(%ld)] WaitAny [%d of %zu]", issuer->get_pid(), value + 1, comms_size);
481 case SIMCALL_COMM_TESTANY:
483 if (issuer->get_host())
484 label = xbt::string_printf("[(%ld)%s] TestAny FALSE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
486 label = xbt::string_printf("[(%ld)] TestAny FALSE", issuer->get_pid());
488 if (issuer->get_host())
490 xbt::string_printf("[(%ld)%s] TestAny TRUE [%d of %lu]", issuer->get_pid(),
491 MC_smx_actor_get_host_name(issuer), value + 1, simcall_comm_testany__get__count(req));
493 label = xbt::string_printf("[(%ld)] TestAny TRUE [%d of %lu]", issuer->get_pid(), value + 1,
494 simcall_comm_testany__get__count(req));
498 case SIMCALL_MUTEX_TRYLOCK:
499 label = xbt::string_printf("[(%ld)] Mutex TRYLOCK", issuer->get_pid());
502 case SIMCALL_MUTEX_LOCK:
503 label = xbt::string_printf("[(%ld)] Mutex LOCK", issuer->get_pid());
506 case SIMCALL_MC_RANDOM:
507 if (issuer->get_host())
508 label = xbt::string_printf("[(%ld)%s] MC_RANDOM (%d)", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
511 label = xbt::string_printf("[(%ld)] MC_RANDOM (%d)", issuer->get_pid(), value);
518 return xbt::string_printf("label = \"%s\", color = %s, fontcolor = %s", label.c_str(), color, color);
522 } // namespace simgrid