1 /* Copyright (c) 2008-2019. 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/mc_smx.hpp"
13 using simgrid::mc::remote;
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_request, mc,
16 "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
60 // because those objects are copies:
61 simgrid::kernel::activity::CommImpl* synchro1 = MC_get_comm(r1);
62 simgrid::kernel::activity::CommImpl* synchro2 = MC_get_comm(r2);
64 if ((r1->call == SIMCALL_COMM_ISEND || r1->call == SIMCALL_COMM_IRECV)
65 && r2->call == SIMCALL_COMM_WAIT) {
67 smx_mailbox_t mbox = MC_get_mbox(r1);
69 if (mbox != synchro2->mbox_cpy
70 && simcall_comm_wait__get__timeout(r2) <= 0)
73 if ((r1->issuer != synchro2->src_actor_.get()) && (r1->issuer != synchro2->dst_actor_.get()) &&
74 simcall_comm_wait__get__timeout(r2) <= 0)
77 if ((r1->call == SIMCALL_COMM_ISEND) && (synchro2->type_ == kernel::activity::CommImpl::Type::SEND) &&
78 (synchro2->src_buff_ != simcall_comm_isend__get__src_buff(r1)) && simcall_comm_wait__get__timeout(r2) <= 0)
81 if ((r1->call == SIMCALL_COMM_IRECV) && (synchro2->type_ == kernel::activity::CommImpl::Type::RECEIVE) &&
82 (synchro2->dst_buff_ != simcall_comm_irecv__get__dst_buff(r1)) && simcall_comm_wait__get__timeout(r2) <= 0)
86 /* FIXME: the following rule assumes that the result of the
87 * isend/irecv call is not stored in a buffer used in the
90 if((r1->call == SIMCALL_COMM_ISEND || r1->call == SIMCALL_COMM_IRECV)
91 && r2->call == SIMCALL_COMM_TEST)
95 if (r1->call == SIMCALL_COMM_WAIT && (r2->call == SIMCALL_COMM_WAIT || r2->call == SIMCALL_COMM_TEST) &&
96 (synchro1->src_actor_.get() == nullptr || synchro1->dst_actor_.get() == nullptr))
99 if (r1->call == SIMCALL_COMM_TEST &&
100 (simcall_comm_test__get__comm(r1) == nullptr || synchro1->src_buff_ == nullptr || synchro1->dst_buff_ == nullptr))
103 if (r1->call == SIMCALL_COMM_TEST && r2->call == SIMCALL_COMM_WAIT && synchro1->src_buff_ == synchro2->src_buff_ &&
104 synchro1->dst_buff_ == synchro2->dst_buff_)
107 if (r1->call == SIMCALL_COMM_WAIT && r2->call == SIMCALL_COMM_TEST && synchro1->src_buff_ != nullptr &&
108 synchro1->dst_buff_ != nullptr && synchro2->src_buff_ != nullptr && synchro2->dst_buff_ != nullptr &&
109 synchro1->dst_buff_ != synchro2->src_buff_ && synchro1->dst_buff_ != synchro2->dst_buff_ &&
110 synchro2->dst_buff_ != synchro1->src_buff_)
116 // Those are internal_req
117 bool request_depend(smx_simcall_t req1, smx_simcall_t req2)
119 if (req1->issuer == req2->issuer)
122 /* Wait with timeout transitions are not considered by the independence theorem, thus we consider them as dependent with all other transitions */
123 if ((req1->call == SIMCALL_COMM_WAIT && simcall_comm_wait__get__timeout(req1) > 0) ||
124 (req2->call == SIMCALL_COMM_WAIT && simcall_comm_wait__get__timeout(req2) > 0))
127 if (req1->call != req2->call)
128 return request_depend_asymmetric(req1, req2) && request_depend_asymmetric(req2, req1);
130 // Those are internal requests, we do not need indirection
131 // because those objects are copies:
132 simgrid::kernel::activity::CommImpl* synchro1 = MC_get_comm(req1);
133 simgrid::kernel::activity::CommImpl* synchro2 = MC_get_comm(req2);
135 switch (req1->call) {
136 case SIMCALL_COMM_ISEND:
137 return simcall_comm_isend__get__mbox(req1) == simcall_comm_isend__get__mbox(req2);
138 case SIMCALL_COMM_IRECV:
139 return simcall_comm_irecv__get__mbox(req1) == simcall_comm_irecv__get__mbox(req2);
140 case SIMCALL_COMM_WAIT:
141 if (synchro1->src_buff_ == synchro2->src_buff_ && synchro1->dst_buff_ == synchro2->dst_buff_)
143 if (synchro1->src_buff_ != nullptr && synchro1->dst_buff_ != nullptr && synchro2->src_buff_ != nullptr &&
144 synchro2->dst_buff_ != nullptr && synchro1->dst_buff_ != synchro2->src_buff_ &&
145 synchro1->dst_buff_ != synchro2->dst_buff_ && synchro2->dst_buff_ != synchro1->src_buff_)
156 static char *pointer_to_string(void *pointer)
159 if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
160 return bprintf("%p", pointer);
162 return xbt_strdup("(verbose only)");
165 static char *buff_size_to_string(size_t buff_size)
168 if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
169 return bprintf("%zu", buff_size);
171 return xbt_strdup("(verbose only)");
175 std::string simgrid::mc::request_to_string(smx_simcall_t req, int value, simgrid::mc::RequestType request_type)
177 xbt_assert(mc_model_checker != nullptr, "Must be called from MCer");
179 if (req->transition != nullptr)
180 return req->transition->to_string();
182 bool use_remote_comm = true;
183 switch(request_type) {
184 case simgrid::mc::RequestType::simix:
185 use_remote_comm = true;
187 case simgrid::mc::RequestType::executed:
188 case simgrid::mc::RequestType::internal:
189 use_remote_comm = false;
195 const char* type = nullptr;
196 char *args = nullptr;
198 smx_actor_t issuer = MC_smx_simcall_get_issuer(req);
202 case SIMCALL_COMM_ISEND: {
204 char* p = pointer_to_string(simcall_comm_isend__get__src_buff(req));
205 char* bs = buff_size_to_string(simcall_comm_isend__get__src_buff_size(req));
206 if (issuer->get_host())
207 args = bprintf("src=(%ld)%s (%s), buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
208 MC_smx_actor_get_name(issuer), p, bs);
210 args = bprintf("src=(%ld)%s, buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_name(issuer), p, bs);
216 case SIMCALL_COMM_IRECV: {
217 size_t* remote_size = simcall_comm_irecv__get__dst_buff_size(req);
220 mc_model_checker->process().read_bytes(&size, sizeof(size),
221 remote(remote_size));
224 char* p = pointer_to_string(simcall_comm_irecv__get__dst_buff(req));
225 char* bs = buff_size_to_string(size);
226 if (issuer->get_host())
227 args = bprintf("dst=(%ld)%s (%s), buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
228 MC_smx_actor_get_name(issuer), p, bs);
230 args = bprintf("dst=(%ld)%s, buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_name(issuer), p, bs);
236 case SIMCALL_COMM_WAIT: {
237 simgrid::kernel::activity::CommImpl* remote_act =
238 static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_wait__getraw__comm(req));
241 type = "WaitTimeout";
242 p = pointer_to_string(remote_act);
243 args = bprintf("comm=%s", p);
246 p = pointer_to_string(remote_act);
248 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
249 simgrid::kernel::activity::CommImpl* act;
250 if (use_remote_comm) {
251 mc_model_checker->process().read(temp_synchro,
252 remote(static_cast<simgrid::kernel::activity::CommImpl*>(remote_act)));
253 act = temp_synchro.get_buffer();
257 smx_actor_t src_proc = mc_model_checker->process().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
258 smx_actor_t dst_proc = mc_model_checker->process().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
260 bprintf("comm=%s [(%ld)%s (%s)-> (%ld)%s (%s)]", p, src_proc ? src_proc->get_pid() : 0,
261 src_proc ? MC_smx_actor_get_host_name(src_proc) : "", src_proc ? MC_smx_actor_get_name(src_proc) : "",
262 dst_proc ? dst_proc->get_pid() : 0, dst_proc ? MC_smx_actor_get_host_name(dst_proc) : "",
263 dst_proc ? MC_smx_actor_get_name(dst_proc) : "");
269 case SIMCALL_COMM_TEST: {
270 simgrid::kernel::activity::CommImpl* remote_act =
271 static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_test__getraw__comm(req));
272 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
273 simgrid::kernel::activity::CommImpl* act;
274 if (use_remote_comm) {
275 mc_model_checker->process().read(temp_synchro,
276 remote(static_cast<simgrid::kernel::activity::CommImpl*>(remote_act)));
277 act = temp_synchro.get_buffer();
282 if (act->src_actor_.get() == nullptr || act->dst_actor_.get() == nullptr) {
284 p = pointer_to_string(remote_act);
285 args = bprintf("comm=%s", p);
288 p = pointer_to_string(remote_act);
290 smx_actor_t src_proc = mc_model_checker->process().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
291 smx_actor_t dst_proc = mc_model_checker->process().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
292 args = bprintf("comm=%s [(%ld)%s (%s) -> (%ld)%s (%s)]", p, src_proc->get_pid(), MC_smx_actor_get_name(src_proc),
293 MC_smx_actor_get_host_name(src_proc), dst_proc->get_pid(), MC_smx_actor_get_name(dst_proc),
294 MC_smx_actor_get_host_name(dst_proc));
300 case SIMCALL_COMM_WAITANY: {
302 size_t count = simcall_comm_waitany__get__count(req);
304 simgrid::kernel::activity::CommImpl* remote_sync;
305 remote_sync = mc_model_checker->process().read(remote(simcall_comm_waitany__get__comms(req) + value));
306 char* p = pointer_to_string(remote_sync);
307 args = bprintf("comm=%s (%d of %zu)", p, value + 1, count);
310 args = bprintf("comm at idx %d", value);
314 case SIMCALL_COMM_TESTANY:
316 type = "TestAny FALSE";
317 args = xbt_strdup("-");
321 bprintf("(%d of %zu)", value + 1,
322 simcall_comm_testany__get__count(req));
326 case SIMCALL_MUTEX_TRYLOCK:
327 case SIMCALL_MUTEX_LOCK: {
328 if (req->call == SIMCALL_MUTEX_LOCK)
331 type = "Mutex TRYLOCK";
333 simgrid::mc::Remote<simgrid::kernel::activity::MutexImpl> mutex;
334 mc_model_checker->process().read_bytes(mutex.get_buffer(), sizeof(mutex),
335 remote(req->call == SIMCALL_MUTEX_LOCK
336 ? simcall_mutex_lock__get__mutex(req)
337 : simcall_mutex_trylock__get__mutex(req)));
339 "locked = %d, owner = %d, sleeping = n/a", mutex.get_buffer()->is_locked(),
340 mutex.get_buffer()->owner_ != nullptr
341 ? (int)mc_model_checker->process().resolve_actor(simgrid::mc::remote(mutex.get_buffer()->owner_))->get_pid()
346 case SIMCALL_MC_RANDOM:
348 args = bprintf("%d", value);
352 type = SIMIX_simcall_name(req->call);
353 args = bprintf("??");
359 str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s(%s)", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
360 MC_smx_actor_get_name(issuer), type, args);
362 str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s ", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
363 MC_smx_actor_get_name(issuer), type);
371 bool request_is_enabled_by_idx(smx_simcall_t req, unsigned int idx)
373 simgrid::kernel::activity::CommImpl* remote_act = nullptr;
376 case SIMCALL_COMM_WAIT:
377 /* FIXME: check also that src and dst processes are not suspended */
378 remote_act = simcall_comm_wait__getraw__comm(req);
381 case SIMCALL_COMM_WAITANY:
382 remote_act = mc_model_checker->process().read(remote(simcall_comm_testany__get__comms(req) + idx));
385 case SIMCALL_COMM_TESTANY:
386 remote_act = mc_model_checker->process().read(remote(simcall_comm_testany__get__comms(req) + idx));
393 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
394 mc_model_checker->process().read(temp_comm, remote(remote_act));
395 simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
396 return comm->src_actor_.get() && comm->dst_actor_.get();
400 static const char* colors[] = {
416 static inline const char* get_color(int id)
418 return colors[id % (sizeof(colors) / sizeof(colors[0])) ];
421 std::string request_get_dot_output(smx_simcall_t req, int value)
423 const smx_actor_t issuer = MC_smx_simcall_get_issuer(req);
424 const char* color = get_color(issuer->get_pid() - 1);
426 if (req->transition != nullptr)
427 return simgrid::xbt::string_printf("label = \"%s\", color = %s, fontcolor = %s",
428 req->transition->dot_label().c_str(), color, color);
434 case SIMCALL_COMM_ISEND:
435 if (issuer->get_host())
436 label = simgrid::xbt::string_printf("[(%ld)%s] iSend", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
438 label = bprintf("[(%ld)] iSend", issuer->get_pid());
441 case SIMCALL_COMM_IRECV:
442 if (issuer->get_host())
443 label = simgrid::xbt::string_printf("[(%ld)%s] iRecv", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
445 label = simgrid::xbt::string_printf("[(%ld)] iRecv", issuer->get_pid());
448 case SIMCALL_COMM_WAIT:
450 if (issuer->get_host())
452 simgrid::xbt::string_printf("[(%ld)%s] WaitTimeout", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
454 label = simgrid::xbt::string_printf("[(%ld)] WaitTimeout", issuer->get_pid());
456 simgrid::kernel::activity::ActivityImpl* remote_act = simcall_comm_wait__getraw__comm(req);
457 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
458 mc_model_checker->process().read(temp_comm,
459 remote(static_cast<simgrid::kernel::activity::CommImpl*>(remote_act)));
460 simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
462 smx_actor_t src_proc = mc_model_checker->process().resolve_actor(simgrid::mc::remote(comm->src_actor_.get()));
463 smx_actor_t dst_proc = mc_model_checker->process().resolve_actor(simgrid::mc::remote(comm->dst_actor_.get()));
464 if (issuer->get_host())
465 label = simgrid::xbt::string_printf("[(%ld)%s] Wait [(%ld)->(%ld)]", issuer->get_pid(),
466 MC_smx_actor_get_host_name(issuer), src_proc ? src_proc->get_pid() : 0,
467 dst_proc ? dst_proc->get_pid() : 0);
469 label = simgrid::xbt::string_printf("[(%ld)] Wait [(%ld)->(%ld)]", issuer->get_pid(),
470 src_proc ? src_proc->get_pid() : 0, dst_proc ? dst_proc->get_pid() : 0);
474 case SIMCALL_COMM_TEST: {
475 simgrid::kernel::activity::ActivityImpl* remote_act = simcall_comm_test__getraw__comm(req);
476 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
477 mc_model_checker->process().read(temp_comm, remote(static_cast<simgrid::kernel::activity::CommImpl*>(remote_act)));
478 simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
479 if (comm->src_actor_.get() == nullptr || comm->dst_actor_.get() == nullptr) {
480 if (issuer->get_host())
482 simgrid::xbt::string_printf("[(%ld)%s] Test FALSE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
484 label = bprintf("[(%ld)] Test FALSE", issuer->get_pid());
486 if (issuer->get_host())
488 simgrid::xbt::string_printf("[(%ld)%s] Test TRUE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
490 label = simgrid::xbt::string_printf("[(%ld)] Test TRUE", issuer->get_pid());
495 case SIMCALL_COMM_WAITANY: {
496 size_t comms_size = simcall_comm_waitany__get__count(req);
497 if (issuer->get_host())
498 label = simgrid::xbt::string_printf("[(%ld)%s] WaitAny [%d of %zu]", issuer->get_pid(),
499 MC_smx_actor_get_host_name(issuer), value + 1, comms_size);
501 label = simgrid::xbt::string_printf("[(%ld)] WaitAny [%d of %zu]", issuer->get_pid(), value + 1, comms_size);
505 case SIMCALL_COMM_TESTANY:
507 if (issuer->get_host())
508 label = simgrid::xbt::string_printf("[(%ld)%s] TestAny FALSE", issuer->get_pid(),
509 MC_smx_actor_get_host_name(issuer));
511 label = simgrid::xbt::string_printf("[(%ld)] TestAny FALSE", issuer->get_pid());
513 if (issuer->get_host())
514 label = simgrid::xbt::string_printf("[(%ld)%s] TestAny TRUE [%d of %lu]", issuer->get_pid(),
515 MC_smx_actor_get_host_name(issuer), value + 1,
516 simcall_comm_testany__get__count(req));
518 label = simgrid::xbt::string_printf("[(%ld)] TestAny TRUE [%d of %lu]", issuer->get_pid(), value + 1,
519 simcall_comm_testany__get__count(req));
523 case SIMCALL_MUTEX_TRYLOCK:
524 label = simgrid::xbt::string_printf("[(%ld)] Mutex TRYLOCK", issuer->get_pid());
527 case SIMCALL_MUTEX_LOCK:
528 label = simgrid::xbt::string_printf("[(%ld)] Mutex LOCK", issuer->get_pid());
531 case SIMCALL_MC_RANDOM:
532 if (issuer->get_host())
533 label = simgrid::xbt::string_printf("[(%ld)%s] MC_RANDOM (%d)", issuer->get_pid(),
534 MC_smx_actor_get_host_name(issuer), value);
536 label = simgrid::xbt::string_printf("[(%ld)] MC_RANDOM (%d)", issuer->get_pid(), value);
543 return simgrid::xbt::string_printf(
544 "label = \"%s\", color = %s, fontcolor = %s", label.c_str(),