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;
16 using simgrid::simix::Simcall;
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_request, mc, "Logging specific to MC (request)");
20 static char *pointer_to_string(void *pointer);
21 static char *buff_size_to_string(size_t size);
23 static inline simgrid::kernel::activity::CommImpl* MC_get_comm(smx_simcall_t r)
26 case Simcall::COMM_WAIT:
27 return simcall_comm_wait__getraw__comm(r);
28 case Simcall::COMM_TEST:
29 return simcall_comm_test__getraw__comm(r);
36 smx_mailbox_t MC_get_mbox(smx_simcall_t r)
39 case Simcall::COMM_ISEND:
40 return simcall_comm_isend__get__mbox(r);
41 case Simcall::COMM_IRECV:
42 return simcall_comm_irecv__get__mbox(r);
53 bool request_depend_asymmetric(smx_simcall_t r1, smx_simcall_t r2)
55 if (r1->call_ == Simcall::COMM_ISEND && r2->call_ == Simcall::COMM_IRECV)
58 if (r1->call_ == Simcall::COMM_IRECV && r2->call_ == Simcall::COMM_ISEND)
61 // Those are internal requests, we do not need indirection because those objects are copies:
62 const kernel::activity::CommImpl* synchro1 = MC_get_comm(r1);
63 const kernel::activity::CommImpl* synchro2 = MC_get_comm(r2);
65 if ((r1->call_ == Simcall::COMM_ISEND || r1->call_ == Simcall::COMM_IRECV) && r2->call_ == Simcall::COMM_WAIT) {
66 const kernel::activity::MailboxImpl* mbox = MC_get_mbox(r1);
68 if (mbox != synchro2->mbox_cpy
69 && simcall_comm_wait__get__timeout(r2) <= 0)
72 if ((r1->issuer_ != synchro2->src_actor_.get()) && (r1->issuer_ != synchro2->dst_actor_.get()) &&
73 simcall_comm_wait__get__timeout(r2) <= 0)
76 if ((r1->call_ == Simcall::COMM_ISEND) && (synchro2->type_ == kernel::activity::CommImpl::Type::SEND) &&
77 (synchro2->src_buff_ != simcall_comm_isend__get__src_buff(r1)) && simcall_comm_wait__get__timeout(r2) <= 0)
80 if ((r1->call_ == Simcall::COMM_IRECV) && (synchro2->type_ == kernel::activity::CommImpl::Type::RECEIVE) &&
81 (synchro2->dst_buff_ != simcall_comm_irecv__get__dst_buff(r1)) && simcall_comm_wait__get__timeout(r2) <= 0)
85 /* FIXME: the following rule assumes that the result of the isend/irecv call is not stored in a buffer used in the
88 if((r1->call == Simcall::COMM_ISEND || r1->call == Simcall::COMM_IRECV)
89 && r2->call == Simcall::COMM_TEST)
93 if (r1->call_ == Simcall::COMM_WAIT && (r2->call_ == Simcall::COMM_WAIT || r2->call_ == Simcall::COMM_TEST) &&
94 (synchro1->src_actor_.get() == nullptr || synchro1->dst_actor_.get() == nullptr))
97 if (r1->call_ == Simcall::COMM_TEST &&
98 (simcall_comm_test__get__comm(r1) == nullptr || synchro1->src_buff_ == nullptr || synchro1->dst_buff_ == nullptr))
101 if (r1->call_ == Simcall::COMM_TEST && r2->call_ == Simcall::COMM_WAIT &&
102 synchro1->src_buff_ == synchro2->src_buff_ && synchro1->dst_buff_ == synchro2->dst_buff_)
105 if (r1->call_ == Simcall::COMM_WAIT && r2->call_ == Simcall::COMM_TEST && synchro1->src_buff_ != nullptr &&
106 synchro1->dst_buff_ != nullptr && synchro2->src_buff_ != nullptr && synchro2->dst_buff_ != nullptr &&
107 synchro1->dst_buff_ != synchro2->src_buff_ && synchro1->dst_buff_ != synchro2->dst_buff_ &&
108 synchro2->dst_buff_ != synchro1->src_buff_)
114 // Those are internal_req
115 bool request_depend(smx_simcall_t req1, smx_simcall_t req2)
117 if (req1->issuer_ == req2->issuer_)
120 /* Wait with timeout transitions are not considered by the independence theorem, thus we consider them as dependent with all other transitions */
121 if ((req1->call_ == Simcall::COMM_WAIT && simcall_comm_wait__get__timeout(req1) > 0) ||
122 (req2->call_ == Simcall::COMM_WAIT && simcall_comm_wait__get__timeout(req2) > 0))
125 if (req1->call_ != req2->call_)
126 return request_depend_asymmetric(req1, req2) && request_depend_asymmetric(req2, req1);
128 // Those are internal requests, we do not need indirection because those objects are copies:
129 const kernel::activity::CommImpl* synchro1 = MC_get_comm(req1);
130 const kernel::activity::CommImpl* synchro2 = MC_get_comm(req2);
132 switch (req1->call_) {
133 case Simcall::COMM_ISEND:
134 return simcall_comm_isend__get__mbox(req1) == simcall_comm_isend__get__mbox(req2);
135 case Simcall::COMM_IRECV:
136 return simcall_comm_irecv__get__mbox(req1) == simcall_comm_irecv__get__mbox(req2);
137 case Simcall::COMM_WAIT:
138 if (synchro1->src_buff_ == synchro2->src_buff_ && synchro1->dst_buff_ == synchro2->dst_buff_)
140 if (synchro1->src_buff_ != nullptr && synchro1->dst_buff_ != nullptr && synchro2->src_buff_ != nullptr &&
141 synchro2->dst_buff_ != nullptr && synchro1->dst_buff_ != synchro2->src_buff_ &&
142 synchro1->dst_buff_ != synchro2->dst_buff_ && synchro2->dst_buff_ != synchro1->src_buff_)
151 } // namespace simgrid
153 static char *pointer_to_string(void *pointer)
155 if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
156 return bprintf("%p", pointer);
158 return xbt_strdup("(verbose only)");
161 static char *buff_size_to_string(size_t buff_size)
163 if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
164 return bprintf("%zu", buff_size);
166 return xbt_strdup("(verbose only)");
172 bool request_is_enabled_by_idx(smx_simcall_t req, unsigned int idx)
174 kernel::activity::CommImpl* remote_act = nullptr;
175 switch (req->call_) {
176 case Simcall::COMM_WAIT:
177 /* FIXME: check also that src and dst processes are not suspended */
178 remote_act = simcall_comm_wait__getraw__comm(req);
181 case Simcall::COMM_WAITANY:
182 remote_act = mc_model_checker->get_remote_simulation().read(remote(simcall_comm_waitany__get__comms(req) + idx));
185 case Simcall::COMM_TESTANY:
186 remote_act = mc_model_checker->get_remote_simulation().read(remote(simcall_comm_testany__get__comms(req) + idx));
193 Remote<kernel::activity::CommImpl> temp_comm;
194 mc_model_checker->get_remote_simulation().read(temp_comm, remote(remote_act));
195 const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
196 return comm->src_actor_.get() && comm->dst_actor_.get();
199 static inline const char* get_color(int id)
201 static constexpr std::array<const char*, 13> colors{{"blue", "red", "green3", "goldenrod", "brown", "purple",
202 "magenta", "turquoise4", "gray25", "forestgreen", "hotpink",
203 "lightblue", "tan"}};
204 return colors[id % colors.size()];
208 } // namespace simgrid