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 simcall_comm_wait__getraw__comm(r);
26 case SIMCALL_COMM_TEST:
27 return 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 = simcall_comm_wait__getraw__comm(req);
231 type = "WaitTimeout";
232 p = pointer_to_string(remote_act);
233 args = bprintf("comm=%s", p);
236 p = pointer_to_string(remote_act);
238 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
239 const simgrid::kernel::activity::CommImpl* act;
240 if (use_remote_comm) {
241 mc_model_checker->get_remote_simulation().read(temp_synchro, remote(remote_act));
242 act = temp_synchro.get_buffer();
246 smx_actor_t src_proc =
247 mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
248 smx_actor_t dst_proc =
249 mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
250 args = bprintf("comm=%s [(%ld)%s (%s)-> (%ld)%s (%s)]", p, src_proc ? src_proc->get_pid() : 0,
251 src_proc ? MC_smx_actor_get_host_name(src_proc) : "",
252 src_proc ? MC_smx_actor_get_name(src_proc) : "", dst_proc ? dst_proc->get_pid() : 0,
253 dst_proc ? MC_smx_actor_get_host_name(dst_proc) : "",
254 dst_proc ? MC_smx_actor_get_name(dst_proc) : "");
260 case SIMCALL_COMM_TEST: {
261 simgrid::kernel::activity::CommImpl* remote_act = simcall_comm_test__getraw__comm(req);
262 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
263 const simgrid::kernel::activity::CommImpl* act;
264 if (use_remote_comm) {
265 mc_model_checker->get_remote_simulation().read(temp_synchro, remote(remote_act));
266 act = temp_synchro.get_buffer();
271 if (act->src_actor_.get() == nullptr || act->dst_actor_.get() == nullptr) {
273 p = pointer_to_string(remote_act);
274 args = bprintf("comm=%s", p);
277 p = pointer_to_string(remote_act);
279 smx_actor_t src_proc =
280 mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
281 smx_actor_t dst_proc =
282 mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
283 args = bprintf("comm=%s [(%ld)%s (%s) -> (%ld)%s (%s)]", p, src_proc->get_pid(),
284 MC_smx_actor_get_name(src_proc), MC_smx_actor_get_host_name(src_proc), dst_proc->get_pid(),
285 MC_smx_actor_get_name(dst_proc), MC_smx_actor_get_host_name(dst_proc));
291 case SIMCALL_COMM_WAITANY: {
293 size_t count = simcall_comm_waitany__get__count(req);
295 simgrid::kernel::activity::CommImpl* remote_sync;
297 mc_model_checker->get_remote_simulation().read(remote(simcall_comm_waitany__get__comms(req) + value));
298 char* p = pointer_to_string(remote_sync);
299 args = bprintf("comm=%s (%d of %zu)", p, value + 1, count);
302 args = bprintf("comm at idx %d", value);
306 case SIMCALL_COMM_TESTANY:
308 type = "TestAny FALSE";
309 args = xbt_strdup("-");
312 args = bprintf("(%d of %zu)", value + 1, simcall_comm_testany__get__count(req));
316 case SIMCALL_MUTEX_TRYLOCK:
317 case SIMCALL_MUTEX_LOCK: {
318 if (req->call_ == SIMCALL_MUTEX_LOCK)
321 type = "Mutex TRYLOCK";
323 simgrid::mc::Remote<simgrid::kernel::activity::MutexImpl> mutex;
324 mc_model_checker->get_remote_simulation().read_bytes(mutex.get_buffer(), sizeof(mutex),
325 remote(req->call_ == SIMCALL_MUTEX_LOCK
326 ? simcall_mutex_lock__get__mutex(req)
327 : simcall_mutex_trylock__get__mutex(req)));
328 args = bprintf("locked = %d, owner = %d, sleeping = n/a", mutex.get_buffer()->is_locked(),
329 mutex.get_buffer()->get_owner() != nullptr
330 ? (int)mc_model_checker->get_remote_simulation()
331 .resolve_actor(simgrid::mc::remote(mutex.get_buffer()->get_owner()))
337 case SIMCALL_MC_RANDOM:
339 args = bprintf("%d", value);
343 type = SIMIX_simcall_name(req->call_);
344 args = bprintf("??");
350 str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s(%s)", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
351 MC_smx_actor_get_name(issuer), type, args);
353 str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s ", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
354 MC_smx_actor_get_name(issuer), type);
362 bool request_is_enabled_by_idx(smx_simcall_t req, unsigned int idx)
364 kernel::activity::CommImpl* remote_act = nullptr;
365 switch (req->call_) {
366 case SIMCALL_COMM_WAIT:
367 /* FIXME: check also that src and dst processes are not suspended */
368 remote_act = simcall_comm_wait__getraw__comm(req);
371 case SIMCALL_COMM_WAITANY:
372 remote_act = mc_model_checker->get_remote_simulation().read(remote(simcall_comm_testany__get__comms(req) + idx));
375 case SIMCALL_COMM_TESTANY:
376 remote_act = mc_model_checker->get_remote_simulation().read(remote(simcall_comm_testany__get__comms(req) + idx));
383 Remote<kernel::activity::CommImpl> temp_comm;
384 mc_model_checker->get_remote_simulation().read(temp_comm, remote(remote_act));
385 const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
386 return comm->src_actor_.get() && comm->dst_actor_.get();
389 static const char* colors[] = {
390 "blue", "red", "green3", "goldenrod", "brown", "purple", "magenta",
391 "turquoise4", "gray25", "forestgreen", "hotpink", "lightblue", "tan",
394 static inline const char* get_color(int id)
396 return colors[id % (sizeof(colors) / sizeof(colors[0])) ];
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