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_proc)
75 && (r1->issuer != synchro2->dst_proc)
76 && simcall_comm_wait__get__timeout(r2) <= 0)
79 if ((r1->call == SIMCALL_COMM_ISEND)
80 && (synchro2->type == SIMIX_COMM_SEND)
81 && (synchro2->src_buff !=
82 simcall_comm_isend__get__src_buff(r1))
83 && simcall_comm_wait__get__timeout(r2) <= 0)
86 if ((r1->call == SIMCALL_COMM_IRECV)
87 && (synchro2->type == SIMIX_COMM_RECEIVE)
88 && (synchro2->dst_buff != simcall_comm_irecv__get__dst_buff(r1))
89 && simcall_comm_wait__get__timeout(r2) <= 0)
93 /* FIXME: the following rule assumes that the result of the
94 * isend/irecv call is not stored in a buffer used in the
97 if((r1->call == SIMCALL_COMM_ISEND || r1->call == SIMCALL_COMM_IRECV)
98 && r2->call == SIMCALL_COMM_TEST)
102 if (r1->call == SIMCALL_COMM_WAIT
103 && (r2->call == SIMCALL_COMM_WAIT || r2->call == SIMCALL_COMM_TEST)
104 && (synchro1->src_proc == nullptr || synchro1->dst_proc == nullptr))
107 if (r1->call == SIMCALL_COMM_TEST &&
108 (simcall_comm_test__get__comm(r1) == nullptr
109 || synchro1->src_buff == nullptr
110 || synchro1->dst_buff == nullptr))
113 if (r1->call == SIMCALL_COMM_TEST && r2->call == SIMCALL_COMM_WAIT
114 && synchro1->src_buff == synchro2->src_buff
115 && synchro1->dst_buff == synchro2->dst_buff)
118 if (r1->call == SIMCALL_COMM_WAIT && r2->call == SIMCALL_COMM_TEST
119 && synchro1->src_buff != nullptr
120 && synchro1->dst_buff != nullptr
121 && synchro2->src_buff != nullptr
122 && synchro2->dst_buff != nullptr
123 && synchro1->dst_buff != synchro2->src_buff
124 && synchro1->dst_buff != synchro2->dst_buff
125 && synchro2->dst_buff != synchro1->src_buff)
131 // Those are internal_req
132 bool request_depend(smx_simcall_t r1, smx_simcall_t r2)
134 if (r1->issuer == r2->issuer)
137 /* Wait with timeout transitions are not considered by the independence theorem, thus we consider them as dependent with all other transitions */
138 if ((r1->call == SIMCALL_COMM_WAIT && simcall_comm_wait__get__timeout(r1) > 0)
139 || (r2->call == SIMCALL_COMM_WAIT
140 && simcall_comm_wait__get__timeout(r2) > 0))
143 if (r1->call != r2->call)
144 return request_depend_asymmetric(r1, r2)
145 && request_depend_asymmetric(r2, r1);
147 // Those are internal requests, we do not need indirection
148 // because those objects are copies:
149 simgrid::kernel::activity::CommImpl* synchro1 = MC_get_comm(r1);
150 simgrid::kernel::activity::CommImpl* synchro2 = MC_get_comm(r2);
153 case SIMCALL_COMM_ISEND:
154 return simcall_comm_isend__get__mbox(r1)
155 == simcall_comm_isend__get__mbox(r2);
156 case SIMCALL_COMM_IRECV:
157 return simcall_comm_irecv__get__mbox(r1)
158 == simcall_comm_irecv__get__mbox(r2);
159 case SIMCALL_COMM_WAIT:
160 if (synchro1->src_buff == synchro2->src_buff
161 && synchro1->dst_buff == synchro2->dst_buff)
163 if (synchro1->src_buff != nullptr && synchro1->dst_buff != nullptr && synchro2->src_buff != nullptr &&
164 synchro2->dst_buff != nullptr && synchro1->dst_buff != synchro2->src_buff &&
165 synchro1->dst_buff != synchro2->dst_buff && synchro2->dst_buff != synchro1->src_buff)
176 static char *pointer_to_string(void *pointer)
179 if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
180 return bprintf("%p", pointer);
182 return xbt_strdup("(verbose only)");
185 static char *buff_size_to_string(size_t buff_size)
188 if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
189 return bprintf("%zu", buff_size);
191 return xbt_strdup("(verbose only)");
195 std::string simgrid::mc::request_to_string(smx_simcall_t req, int value, simgrid::mc::RequestType request_type)
197 xbt_assert(mc_model_checker != nullptr, "Must be called from MCer");
199 bool use_remote_comm = true;
200 switch(request_type) {
201 case simgrid::mc::RequestType::simix:
202 use_remote_comm = true;
204 case simgrid::mc::RequestType::executed:
205 case simgrid::mc::RequestType::internal:
206 use_remote_comm = false;
212 const char* type = nullptr;
213 char *args = nullptr;
215 smx_actor_t issuer = MC_smx_simcall_get_issuer(req);
219 case SIMCALL_COMM_ISEND: {
221 char* p = pointer_to_string(simcall_comm_isend__get__src_buff(req));
222 char* bs = buff_size_to_string(simcall_comm_isend__get__src_buff_size(req));
224 args = bprintf("src=(%ld)%s (%s), buff=%s, size=%s", issuer->pid_, MC_smx_actor_get_host_name(issuer),
225 MC_smx_actor_get_name(issuer), p, bs);
227 args = bprintf("src=(%ld)%s, buff=%s, size=%s", issuer->pid_, MC_smx_actor_get_name(issuer), p, bs);
233 case SIMCALL_COMM_IRECV: {
234 size_t* remote_size = simcall_comm_irecv__get__dst_buff_size(req);
237 mc_model_checker->process().read_bytes(&size, sizeof(size),
238 remote(remote_size));
241 char* p = pointer_to_string(simcall_comm_irecv__get__dst_buff(req));
242 char* bs = buff_size_to_string(size);
244 args = bprintf("dst=(%ld)%s (%s), buff=%s, size=%s", issuer->pid_, MC_smx_actor_get_host_name(issuer),
245 MC_smx_actor_get_name(issuer), p, bs);
247 args = bprintf("dst=(%ld)%s, buff=%s, size=%s", issuer->pid_, MC_smx_actor_get_name(issuer), p, bs);
253 case SIMCALL_COMM_WAIT: {
254 simgrid::kernel::activity::CommImpl* remote_act =
255 static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_wait__getraw__comm(req));
258 type = "WaitTimeout";
259 p = pointer_to_string(remote_act);
260 args = bprintf("comm=%s", p);
263 p = pointer_to_string(remote_act);
265 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
266 simgrid::kernel::activity::CommImpl* act;
267 if (use_remote_comm) {
268 mc_model_checker->process().read(temp_synchro,
269 remote(static_cast<simgrid::kernel::activity::CommImpl*>(remote_act)));
270 act = temp_synchro.getBuffer();
274 smx_actor_t src_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(act->src_proc));
275 smx_actor_t dst_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(act->dst_proc));
277 bprintf("comm=%s [(%ld)%s (%s)-> (%ld)%s (%s)]", p, src_proc ? src_proc->pid_ : 0,
278 src_proc ? MC_smx_actor_get_host_name(src_proc) : "", src_proc ? MC_smx_actor_get_name(src_proc) : "",
279 dst_proc ? dst_proc->pid_ : 0, dst_proc ? MC_smx_actor_get_host_name(dst_proc) : "",
280 dst_proc ? MC_smx_actor_get_name(dst_proc) : "");
286 case SIMCALL_COMM_TEST: {
287 simgrid::kernel::activity::CommImpl* remote_act =
288 static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_test__getraw__comm(req));
289 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
290 simgrid::kernel::activity::CommImpl* act;
291 if (use_remote_comm) {
292 mc_model_checker->process().read(temp_synchro,
293 remote(static_cast<simgrid::kernel::activity::CommImpl*>(remote_act)));
294 act = temp_synchro.getBuffer();
299 if (act->src_proc == nullptr || act->dst_proc == nullptr) {
301 p = pointer_to_string(remote_act);
302 args = bprintf("comm=%s", p);
305 p = pointer_to_string(remote_act);
307 smx_actor_t src_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(act->src_proc));
308 smx_actor_t dst_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(act->dst_proc));
309 args = bprintf("comm=%s [(%ld)%s (%s) -> (%ld)%s (%s)]", p, src_proc->pid_, MC_smx_actor_get_name(src_proc),
310 MC_smx_actor_get_host_name(src_proc), dst_proc->pid_, MC_smx_actor_get_name(dst_proc),
311 MC_smx_actor_get_host_name(dst_proc));
317 case SIMCALL_COMM_WAITANY: {
320 mc_model_checker->process().read_bytes(
321 &comms, sizeof(comms), remote(simcall_comm_waitany__get__comms(req)));
322 if (not xbt_dynar_is_empty(&comms)) {
323 smx_activity_t remote_sync;
324 read_element(mc_model_checker->process(),
325 &remote_sync, remote(simcall_comm_waitany__get__comms(req)), value,
326 sizeof(remote_sync));
327 char* p = pointer_to_string(remote_sync.get());
328 args = bprintf("comm=%s (%d of %lu)",
329 p, value + 1, xbt_dynar_length(&comms));
332 args = bprintf("comm at idx %d", value);
336 case SIMCALL_COMM_TESTANY:
338 type = "TestAny FALSE";
339 args = xbt_strdup("-");
343 bprintf("(%d of %zu)", value + 1,
344 simcall_comm_testany__get__count(req));
348 case SIMCALL_MUTEX_TRYLOCK:
349 case SIMCALL_MUTEX_LOCK: {
350 if (req->call == SIMCALL_MUTEX_LOCK)
353 type = "Mutex TRYLOCK";
355 simgrid::mc::Remote<simgrid::kernel::activity::MutexImpl> mutex;
356 mc_model_checker->process().read_bytes(mutex.getBuffer(), sizeof(mutex),
358 req->call == SIMCALL_MUTEX_LOCK
359 ? simcall_mutex_lock__get__mutex(req)
360 : simcall_mutex_trylock__get__mutex(req)
363 bprintf("locked = %d, owner = %d, sleeping = n/a", mutex.getBuffer()->locked,
364 mutex.getBuffer()->owner != nullptr
365 ? (int)mc_model_checker->process().resolveActor(simgrid::mc::remote(mutex.getBuffer()->owner))->pid_
370 case SIMCALL_MC_RANDOM:
372 args = bprintf("%d", value);
376 type = SIMIX_simcall_name(req->call);
377 args = bprintf("??");
383 str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s(%s)", issuer->pid_, MC_smx_actor_get_host_name(issuer),
384 MC_smx_actor_get_name(issuer), type, args);
386 str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s ", issuer->pid_, MC_smx_actor_get_host_name(issuer),
387 MC_smx_actor_get_name(issuer), type);
395 bool request_is_enabled_by_idx(smx_simcall_t req, unsigned int idx)
397 simgrid::kernel::activity::ActivityImpl* remote_act = nullptr;
400 case SIMCALL_COMM_WAIT:
401 /* FIXME: check also that src and dst processes are not suspended */
402 remote_act = simcall_comm_wait__getraw__comm(req);
405 case SIMCALL_COMM_WAITANY:
406 read_element(mc_model_checker->process(), &remote_act, remote(simcall_comm_waitany__getraw__comms(req)), idx,
410 case SIMCALL_COMM_TESTANY:
411 remote_act = mc_model_checker->process().read(remote(simcall_comm_testany__getraw__comms(req) + idx));
418 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
419 mc_model_checker->process().read(temp_comm, remote(static_cast<simgrid::kernel::activity::CommImpl*>(remote_act)));
420 simgrid::kernel::activity::CommImpl* comm = temp_comm.getBuffer();
421 return comm->src_proc && comm->dst_proc;
425 static const char* colors[] = {
441 static inline const char* get_color(int id)
443 return colors[id % (sizeof(colors) / sizeof(colors[0])) ];
446 std::string request_get_dot_output(smx_simcall_t req, int value)
450 const smx_actor_t issuer = MC_smx_simcall_get_issuer(req);
453 case SIMCALL_COMM_ISEND:
455 label = simgrid::xbt::string_printf("[(%ld)%s] iSend", issuer->pid_, MC_smx_actor_get_host_name(issuer));
457 label = bprintf("[(%ld)] iSend", issuer->pid_);
460 case SIMCALL_COMM_IRECV:
462 label = simgrid::xbt::string_printf("[(%ld)%s] iRecv", issuer->pid_, MC_smx_actor_get_host_name(issuer));
464 label = simgrid::xbt::string_printf("[(%ld)] iRecv", issuer->pid_);
467 case SIMCALL_COMM_WAIT:
470 label = simgrid::xbt::string_printf("[(%ld)%s] WaitTimeout", issuer->pid_, MC_smx_actor_get_host_name(issuer));
472 label = simgrid::xbt::string_printf("[(%ld)] WaitTimeout", issuer->pid_);
474 simgrid::kernel::activity::ActivityImpl* remote_act = simcall_comm_wait__getraw__comm(req);
475 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
476 mc_model_checker->process().read(temp_comm,
477 remote(static_cast<simgrid::kernel::activity::CommImpl*>(remote_act)));
478 simgrid::kernel::activity::CommImpl* comm = temp_comm.getBuffer();
480 smx_actor_t src_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(comm->src_proc));
481 smx_actor_t dst_proc = mc_model_checker->process().resolveActor(simgrid::mc::remote(comm->dst_proc));
483 label = simgrid::xbt::string_printf("[(%ld)%s] Wait [(%ld)->(%ld)]", issuer->pid_,
484 MC_smx_actor_get_host_name(issuer), src_proc ? src_proc->pid_ : 0,
485 dst_proc ? dst_proc->pid_ : 0);
487 label = simgrid::xbt::string_printf("[(%ld)] Wait [(%ld)->(%ld)]", issuer->pid_, src_proc ? src_proc->pid_ : 0,
488 dst_proc ? dst_proc->pid_ : 0);
492 case SIMCALL_COMM_TEST: {
493 simgrid::kernel::activity::ActivityImpl* remote_act = simcall_comm_test__getraw__comm(req);
494 simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
495 mc_model_checker->process().read(temp_comm, remote(static_cast<simgrid::kernel::activity::CommImpl*>(remote_act)));
496 simgrid::kernel::activity::CommImpl* comm = temp_comm.getBuffer();
497 if (comm->src_proc == nullptr || comm->dst_proc == nullptr) {
499 label = simgrid::xbt::string_printf("[(%ld)%s] Test FALSE", issuer->pid_, MC_smx_actor_get_host_name(issuer));
501 label = bprintf("[(%ld)] Test FALSE", issuer->pid_);
504 label = simgrid::xbt::string_printf("[(%ld)%s] Test TRUE", issuer->pid_, MC_smx_actor_get_host_name(issuer));
506 label = simgrid::xbt::string_printf("[(%ld)] Test TRUE", issuer->pid_);
511 case SIMCALL_COMM_WAITANY: {
512 unsigned long comms_size = read_length(
513 mc_model_checker->process(), remote(simcall_comm_waitany__get__comms(req)));
515 label = simgrid::xbt::string_printf("[(%ld)%s] WaitAny [%d of %lu]", issuer->pid_,
516 MC_smx_actor_get_host_name(issuer), value + 1, comms_size);
518 label = simgrid::xbt::string_printf("[(%ld)] WaitAny [%d of %lu]", issuer->pid_, value + 1, comms_size);
522 case SIMCALL_COMM_TESTANY:
526 simgrid::xbt::string_printf("[(%ld)%s] TestAny FALSE", issuer->pid_, MC_smx_actor_get_host_name(issuer));
528 label = simgrid::xbt::string_printf("[(%ld)] TestAny FALSE", issuer->pid_);
531 label = simgrid::xbt::string_printf("[(%ld)%s] TestAny TRUE [%d of %lu]", issuer->pid_,
532 MC_smx_actor_get_host_name(issuer), value + 1,
533 simcall_comm_testany__get__count(req));
535 label = simgrid::xbt::string_printf("[(%ld)] TestAny TRUE [%d of %lu]", issuer->pid_, value + 1,
536 simcall_comm_testany__get__count(req));
540 case SIMCALL_MUTEX_TRYLOCK:
541 label = simgrid::xbt::string_printf("[(%ld)] Mutex TRYLOCK", issuer->pid_);
544 case SIMCALL_MUTEX_LOCK:
545 label = simgrid::xbt::string_printf("[(%ld)] Mutex LOCK", issuer->pid_);
548 case SIMCALL_MC_RANDOM:
550 label = simgrid::xbt::string_printf("[(%ld)%s] MC_RANDOM (%d)", issuer->pid_, MC_smx_actor_get_host_name(issuer),
553 label = simgrid::xbt::string_printf("[(%ld)] MC_RANDOM (%d)", issuer->pid_, value);
560 const char* color = get_color(issuer->pid_ - 1);
561 return simgrid::xbt::string_printf(
562 "label = \"%s\", color = %s, fontcolor = %s", label.c_str(),