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"
12 #include "src/mc/mc_xbt.hpp"
14 using simgrid::mc::remote;
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_request, mc,
17 "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 static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_wait__getraw__comm(r));
27 case SIMCALL_COMM_TEST:
28 return static_cast<simgrid::kernel::activity::CommImpl*>(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
61 // because those objects are copies:
62 simgrid::kernel::activity::CommImpl* synchro1 = MC_get_comm(r1);
63 simgrid::kernel::activity::CommImpl* synchro2 = MC_get_comm(r2);
65 if ((r1->call == SIMCALL_COMM_ISEND || r1->call == SIMCALL_COMM_IRECV)
66 && r2->call == SIMCALL_COMM_WAIT) {
68 smx_mailbox_t mbox = MC_get_mbox(r1);
70 if (mbox != synchro2->mbox_cpy
71 && simcall_comm_wait__get__timeout(r2) <= 0)
74 if ((r1->issuer != synchro2->src_actor_.get()) && (r1->issuer != synchro2->dst_actor_.get()) &&
75 simcall_comm_wait__get__timeout(r2) <= 0)
78 if ((r1->call == SIMCALL_COMM_ISEND) && (synchro2->type == kernel::activity::CommImpl::Type::SEND) &&
79 (synchro2->src_buff_ != simcall_comm_isend__get__src_buff(r1)) && simcall_comm_wait__get__timeout(r2) <= 0)
82 if ((r1->call == SIMCALL_COMM_IRECV) && (synchro2->type == kernel::activity::CommImpl::Type::RECEIVE) &&
83 (synchro2->dst_buff_ != simcall_comm_irecv__get__dst_buff(r1)) && simcall_comm_wait__get__timeout(r2) <= 0)
87 /* FIXME: the following rule assumes that the result of the
88 * isend/irecv call is not stored in a buffer used in the
91 if((r1->call == SIMCALL_COMM_ISEND || r1->call == SIMCALL_COMM_IRECV)
92 && r2->call == SIMCALL_COMM_TEST)
96 if (r1->call == SIMCALL_COMM_WAIT && (r2->call == SIMCALL_COMM_WAIT || r2->call == SIMCALL_COMM_TEST) &&
97 (synchro1->src_actor_.get() == nullptr || synchro1->dst_actor_.get() == nullptr))
100 if (r1->call == SIMCALL_COMM_TEST &&
101 (simcall_comm_test__get__comm(r1) == nullptr || synchro1->src_buff_ == nullptr || synchro1->dst_buff_ == nullptr))
104 if (r1->call == SIMCALL_COMM_TEST && r2->call == SIMCALL_COMM_WAIT && synchro1->src_buff_ == synchro2->src_buff_ &&
105 synchro1->dst_buff_ == synchro2->dst_buff_)
108 if (r1->call == SIMCALL_COMM_WAIT && r2->call == SIMCALL_COMM_TEST && synchro1->src_buff_ != nullptr &&
109 synchro1->dst_buff_ != nullptr && synchro2->src_buff_ != nullptr && synchro2->dst_buff_ != nullptr &&
110 synchro1->dst_buff_ != synchro2->src_buff_ && synchro1->dst_buff_ != synchro2->dst_buff_ &&
111 synchro2->dst_buff_ != synchro1->src_buff_)
117 // Those are internal_req
118 bool request_depend(smx_simcall_t r1, smx_simcall_t r2)
120 if (r1->issuer == r2->issuer)
123 /* Wait with timeout transitions are not considered by the independence theorem, thus we consider them as dependent with all other transitions */
124 if ((r1->call == SIMCALL_COMM_WAIT && simcall_comm_wait__get__timeout(r1) > 0)
125 || (r2->call == SIMCALL_COMM_WAIT
126 && simcall_comm_wait__get__timeout(r2) > 0))
129 if (r1->call != r2->call)
130 return request_depend_asymmetric(r1, r2)
131 && request_depend_asymmetric(r2, r1);
133 // Those are internal requests, we do not need indirection
134 // because those objects are copies:
135 simgrid::kernel::activity::CommImpl* synchro1 = MC_get_comm(r1);
136 simgrid::kernel::activity::CommImpl* synchro2 = MC_get_comm(r2);
139 case SIMCALL_COMM_ISEND:
140 return simcall_comm_isend__get__mbox(r1)
141 == simcall_comm_isend__get__mbox(r2);
142 case SIMCALL_COMM_IRECV:
143 return simcall_comm_irecv__get__mbox(r1)
144 == simcall_comm_irecv__get__mbox(r2);
145 case SIMCALL_COMM_WAIT:
146 if (synchro1->src_buff_ == synchro2->src_buff_ && synchro1->dst_buff_ == synchro2->dst_buff_)
148 if (synchro1->src_buff_ != nullptr && synchro1->dst_buff_ != nullptr && synchro2->src_buff_ != nullptr &&
149 synchro2->dst_buff_ != nullptr && synchro1->dst_buff_ != synchro2->src_buff_ &&
150 synchro1->dst_buff_ != synchro2->dst_buff_ && synchro2->dst_buff_ != synchro1->src_buff_)
161 static char *pointer_to_string(void *pointer)
164 if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
165 return bprintf("%p", pointer);
167 return xbt_strdup("(verbose only)");
170 static char *buff_size_to_string(size_t buff_size)
173 if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
174 return bprintf("%zu", buff_size);
176 return xbt_strdup("(verbose only)");
180 std::string simgrid::mc::request_to_string(smx_simcall_t req, int value, simgrid::mc::RequestType request_type)
182 xbt_assert(mc_model_checker != nullptr, "Must be called from MCer");
184 bool use_remote_comm = true;
185 switch(request_type) {
186 case simgrid::mc::RequestType::simix:
187 use_remote_comm = true;
189 case simgrid::mc::RequestType::executed:
190 case simgrid::mc::RequestType::internal:
191 use_remote_comm = false;
197 const char* type = nullptr;
198 char *args = nullptr;
200 smx_actor_t issuer = MC_smx_simcall_get_issuer(req);
204 case SIMCALL_COMM_ISEND: {
206 char* p = pointer_to_string(simcall_comm_isend__get__src_buff(req));
207 char* bs = buff_size_to_string(simcall_comm_isend__get__src_buff_size(req));
208 if (issuer->get_host())
209 args = bprintf("src=(%ld)%s (%s), buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
210 MC_smx_actor_get_name(issuer), p, bs);
212 args = bprintf("src=(%ld)%s, buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_name(issuer), p, bs);
218 case SIMCALL_COMM_IRECV: {
219 size_t* remote_size = simcall_comm_irecv__get__dst_buff_size(req);
222 mc_model_checker->process().read_bytes(&size, sizeof(size),
223 remote(remote_size));
226 char* p = pointer_to_string(simcall_comm_irecv__get__dst_buff(req));
227 char* bs = buff_size_to_string(size);
228 if (issuer->get_host())
229 args = bprintf("dst=(%ld)%s (%s), buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
230 MC_smx_actor_get_name(issuer), p, bs);
232 args = bprintf("dst=(%ld)%s, buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_name(issuer), p, bs);
238 case SIMCALL_COMM_WAIT: {
239 simgrid::kernel::activity::CommImpl* remote_act =
240 static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_wait__getraw__comm(req));
243 type = "WaitTimeout";
244 p = pointer_to_string(remote_act);
245 args = bprintf("comm=%s", p);
248 p = pointer_to_string(remote_act);
250 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
251 simgrid::kernel::activity::CommImpl* act;
252 if (use_remote_comm) {
253 mc_model_checker->process().read(temp_synchro,
254 remote(static_cast<simgrid::kernel::activity::CommImpl*>(remote_act)));
255 act = temp_synchro.getBuffer();
259 smx_actor_t src_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(act->src_actor_.get()));
260 smx_actor_t dst_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(act->dst_actor_.get()));
262 bprintf("comm=%s [(%ld)%s (%s)-> (%ld)%s (%s)]", p, src_proc ? src_proc->get_pid() : 0,
263 src_proc ? MC_smx_actor_get_host_name(src_proc) : "", src_proc ? MC_smx_actor_get_name(src_proc) : "",
264 dst_proc ? dst_proc->get_pid() : 0, dst_proc ? MC_smx_actor_get_host_name(dst_proc) : "",
265 dst_proc ? MC_smx_actor_get_name(dst_proc) : "");
271 case SIMCALL_COMM_TEST: {
272 simgrid::kernel::activity::CommImpl* remote_act =
273 static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_test__getraw__comm(req));
274 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
275 simgrid::kernel::activity::CommImpl* act;
276 if (use_remote_comm) {
277 mc_model_checker->process().read(temp_synchro,
278 remote(static_cast<simgrid::kernel::activity::CommImpl*>(remote_act)));
279 act = temp_synchro.getBuffer();
284 if (act->src_actor_.get() == nullptr || act->dst_actor_.get() == nullptr) {
286 p = pointer_to_string(remote_act);
287 args = bprintf("comm=%s", p);
290 p = pointer_to_string(remote_act);
292 smx_actor_t src_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(act->src_actor_.get()));
293 smx_actor_t dst_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(act->dst_actor_.get()));
294 args = bprintf("comm=%s [(%ld)%s (%s) -> (%ld)%s (%s)]", p, src_proc->get_pid(), MC_smx_actor_get_name(src_proc),
295 MC_smx_actor_get_host_name(src_proc), dst_proc->get_pid(), MC_smx_actor_get_name(dst_proc),
296 MC_smx_actor_get_host_name(dst_proc));
302 case SIMCALL_COMM_WAITANY: {
305 mc_model_checker->process().read_bytes(
306 &comms, sizeof(comms), remote(simcall_comm_waitany__get__comms(req)));
307 if (not xbt_dynar_is_empty(&comms)) {
308 smx_activity_t remote_sync;
309 read_element(mc_model_checker->process(),
310 &remote_sync, remote(simcall_comm_waitany__get__comms(req)), value,
311 sizeof(remote_sync));
312 char* p = pointer_to_string(remote_sync.get());
313 args = bprintf("comm=%s (%d of %lu)",
314 p, value + 1, xbt_dynar_length(&comms));
317 args = bprintf("comm at idx %d", value);
321 case SIMCALL_COMM_TESTANY:
323 type = "TestAny FALSE";
324 args = xbt_strdup("-");
328 bprintf("(%d of %zu)", value + 1,
329 simcall_comm_testany__get__count(req));
333 case SIMCALL_MUTEX_TRYLOCK:
334 case SIMCALL_MUTEX_LOCK: {
335 if (req->call == SIMCALL_MUTEX_LOCK)
338 type = "Mutex TRYLOCK";
340 simgrid::mc::Remote<simgrid::kernel::activity::MutexImpl> mutex;
341 mc_model_checker->process().read_bytes(mutex.getBuffer(), sizeof(mutex),
343 req->call == SIMCALL_MUTEX_LOCK
344 ? simcall_mutex_lock__get__mutex(req)
345 : simcall_mutex_trylock__get__mutex(req)
348 "locked = %d, owner = %d, sleeping = n/a", mutex.getBuffer()->locked,
349 mutex.getBuffer()->owner != nullptr
350 ? (int)mc_model_checker->process().resolveActor(simgrid::mc::remote(mutex.getBuffer()->owner))->get_pid()
355 case SIMCALL_MC_RANDOM:
357 args = bprintf("%d", value);
361 type = SIMIX_simcall_name(req->call);
362 args = bprintf("??");
368 str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s(%s)", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
369 MC_smx_actor_get_name(issuer), type, args);
371 str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s ", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
372 MC_smx_actor_get_name(issuer), type);
380 bool request_is_enabled_by_idx(smx_simcall_t req, unsigned int idx)
382 simgrid::kernel::activity::ActivityImpl* remote_act = nullptr;
385 case SIMCALL_COMM_WAIT:
386 /* FIXME: check also that src and dst processes are not suspended */
387 remote_act = simcall_comm_wait__getraw__comm(req);
390 case SIMCALL_COMM_WAITANY:
391 read_element(mc_model_checker->process(), &remote_act, remote(simcall_comm_waitany__getraw__comms(req)), idx,
395 case SIMCALL_COMM_TESTANY:
396 remote_act = mc_model_checker->process().read(remote(simcall_comm_testany__getraw__comms(req) + idx));
403 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
404 mc_model_checker->process().read(temp_comm, remote(static_cast<simgrid::kernel::activity::CommImpl*>(remote_act)));
405 simgrid::kernel::activity::CommImpl* comm = temp_comm.getBuffer();
406 return comm->src_actor_.get() && comm->dst_actor_.get();
410 static const char* colors[] = {
426 static inline const char* get_color(int id)
428 return colors[id % (sizeof(colors) / sizeof(colors[0])) ];
431 std::string request_get_dot_output(smx_simcall_t req, int value)
435 const smx_actor_t issuer = MC_smx_simcall_get_issuer(req);
438 case SIMCALL_COMM_ISEND:
439 if (issuer->get_host())
440 label = simgrid::xbt::string_printf("[(%ld)%s] iSend", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
442 label = bprintf("[(%ld)] iSend", issuer->get_pid());
445 case SIMCALL_COMM_IRECV:
446 if (issuer->get_host())
447 label = simgrid::xbt::string_printf("[(%ld)%s] iRecv", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
449 label = simgrid::xbt::string_printf("[(%ld)] iRecv", issuer->get_pid());
452 case SIMCALL_COMM_WAIT:
454 if (issuer->get_host())
456 simgrid::xbt::string_printf("[(%ld)%s] WaitTimeout", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
458 label = simgrid::xbt::string_printf("[(%ld)] WaitTimeout", issuer->get_pid());
460 simgrid::kernel::activity::ActivityImpl* remote_act = simcall_comm_wait__getraw__comm(req);
461 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
462 mc_model_checker->process().read(temp_comm,
463 remote(static_cast<simgrid::kernel::activity::CommImpl*>(remote_act)));
464 simgrid::kernel::activity::CommImpl* comm = temp_comm.getBuffer();
466 smx_actor_t src_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(comm->src_actor_.get()));
467 smx_actor_t dst_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(comm->dst_actor_.get()));
468 if (issuer->get_host())
469 label = simgrid::xbt::string_printf("[(%ld)%s] Wait [(%ld)->(%ld)]", issuer->get_pid(),
470 MC_smx_actor_get_host_name(issuer), src_proc ? src_proc->get_pid() : 0,
471 dst_proc ? dst_proc->get_pid() : 0);
473 label = simgrid::xbt::string_printf("[(%ld)] Wait [(%ld)->(%ld)]", issuer->get_pid(),
474 src_proc ? src_proc->get_pid() : 0, dst_proc ? dst_proc->get_pid() : 0);
478 case SIMCALL_COMM_TEST: {
479 simgrid::kernel::activity::ActivityImpl* remote_act = simcall_comm_test__getraw__comm(req);
480 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
481 mc_model_checker->process().read(temp_comm, remote(static_cast<simgrid::kernel::activity::CommImpl*>(remote_act)));
482 simgrid::kernel::activity::CommImpl* comm = temp_comm.getBuffer();
483 if (comm->src_actor_.get() == nullptr || comm->dst_actor_.get() == nullptr) {
484 if (issuer->get_host())
486 simgrid::xbt::string_printf("[(%ld)%s] Test FALSE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
488 label = bprintf("[(%ld)] Test FALSE", issuer->get_pid());
490 if (issuer->get_host())
492 simgrid::xbt::string_printf("[(%ld)%s] Test TRUE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
494 label = simgrid::xbt::string_printf("[(%ld)] Test TRUE", issuer->get_pid());
499 case SIMCALL_COMM_WAITANY: {
500 unsigned long comms_size = read_length(
501 mc_model_checker->process(), remote(simcall_comm_waitany__get__comms(req)));
502 if (issuer->get_host())
503 label = simgrid::xbt::string_printf("[(%ld)%s] WaitAny [%d of %lu]", issuer->get_pid(),
504 MC_smx_actor_get_host_name(issuer), value + 1, comms_size);
506 label = simgrid::xbt::string_printf("[(%ld)] WaitAny [%d of %lu]", issuer->get_pid(), value + 1, comms_size);
510 case SIMCALL_COMM_TESTANY:
512 if (issuer->get_host())
513 label = simgrid::xbt::string_printf("[(%ld)%s] TestAny FALSE", issuer->get_pid(),
514 MC_smx_actor_get_host_name(issuer));
516 label = simgrid::xbt::string_printf("[(%ld)] TestAny FALSE", issuer->get_pid());
518 if (issuer->get_host())
519 label = simgrid::xbt::string_printf("[(%ld)%s] TestAny TRUE [%d of %lu]", issuer->get_pid(),
520 MC_smx_actor_get_host_name(issuer), value + 1,
521 simcall_comm_testany__get__count(req));
523 label = simgrid::xbt::string_printf("[(%ld)] TestAny TRUE [%d of %lu]", issuer->get_pid(), value + 1,
524 simcall_comm_testany__get__count(req));
528 case SIMCALL_MUTEX_TRYLOCK:
529 label = simgrid::xbt::string_printf("[(%ld)] Mutex TRYLOCK", issuer->get_pid());
532 case SIMCALL_MUTEX_LOCK:
533 label = simgrid::xbt::string_printf("[(%ld)] Mutex LOCK", issuer->get_pid());
536 case SIMCALL_MC_RANDOM:
537 if (issuer->get_host())
538 label = simgrid::xbt::string_printf("[(%ld)%s] MC_RANDOM (%d)", issuer->get_pid(),
539 MC_smx_actor_get_host_name(issuer), value);
541 label = simgrid::xbt::string_printf("[(%ld)] MC_RANDOM (%d)", issuer->get_pid(), value);
548 const char* color = get_color(issuer->get_pid() - 1);
549 return simgrid::xbt::string_printf(
550 "label = \"%s\", color = %s, fontcolor = %s", label.c_str(),