Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
request_to_string() implementation in mc_api class
[simgrid.git] / src / mc / mc_request.cpp
1 /* Copyright (c) 2008-2020. The SimGrid Team. All rights reserved.          */
2
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. */
5
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"
13 #include <array>
14
15 using simgrid::mc::remote;
16 using simgrid::simix::Simcall;
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_request, mc, "Logging specific to MC (request)");
19
20 static char *pointer_to_string(void *pointer);
21 static char *buff_size_to_string(size_t size);
22
23 static inline simgrid::kernel::activity::CommImpl* MC_get_comm(smx_simcall_t r)
24 {
25   switch (r->call_) {
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);
30     default:
31       return nullptr;
32   }
33 }
34
35 static inline
36 smx_mailbox_t MC_get_mbox(smx_simcall_t r)
37 {
38   switch (r->call_) {
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);
43     default:
44       return nullptr;
45   }
46 }
47
48 namespace simgrid {
49 namespace mc {
50
51 // Does half the job
52 static inline
53 bool request_depend_asymmetric(smx_simcall_t r1, smx_simcall_t r2)
54 {
55   if (r1->call_ == Simcall::COMM_ISEND && r2->call_ == Simcall::COMM_IRECV)
56     return false;
57
58   if (r1->call_ == Simcall::COMM_IRECV && r2->call_ == Simcall::COMM_ISEND)
59     return false;
60
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);
64
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);
67
68     if (mbox != synchro2->mbox_cpy
69         && simcall_comm_wait__get__timeout(r2) <= 0)
70       return false;
71
72     if ((r1->issuer_ != synchro2->src_actor_.get()) && (r1->issuer_ != synchro2->dst_actor_.get()) &&
73         simcall_comm_wait__get__timeout(r2) <= 0)
74       return false;
75
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)
78       return false;
79
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)
82       return false;
83   }
84
85   /* FIXME: the following rule assumes that the result of the isend/irecv call is not stored in a buffer used in the
86    * test call. */
87 #if 0
88   if((r1->call == Simcall::COMM_ISEND || r1->call == Simcall::COMM_IRECV)
89       &&  r2->call == Simcall::COMM_TEST)
90     return false;
91 #endif
92
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))
95     return false;
96
97   if (r1->call_ == Simcall::COMM_TEST &&
98       (simcall_comm_test__get__comm(r1) == nullptr || synchro1->src_buff_ == nullptr || synchro1->dst_buff_ == nullptr))
99     return false;
100
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_)
103     return false;
104
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_)
109     return false;
110
111   return true;
112 }
113
114 // Those are internal_req
115 bool request_depend(smx_simcall_t req1, smx_simcall_t req2)
116 {
117   if (req1->issuer_ == req2->issuer_)
118     return false;
119
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))
123     return true;
124
125   if (req1->call_ != req2->call_)
126     return request_depend_asymmetric(req1, req2) && request_depend_asymmetric(req2, req1);
127
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);
131
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_)
139         return false;
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_)
143         return false;
144       return true;
145     default:
146       return true;
147   }
148 }
149
150 } // namespace mc
151 } // namespace simgrid
152
153 static char *pointer_to_string(void *pointer)
154 {
155   if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
156     return bprintf("%p", pointer);
157
158   return xbt_strdup("(verbose only)");
159 }
160
161 static char *buff_size_to_string(size_t buff_size)
162 {
163   if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
164     return bprintf("%zu", buff_size);
165
166   return xbt_strdup("(verbose only)");
167 }
168
169 namespace simgrid {
170 namespace mc {
171
172 bool request_is_enabled_by_idx(smx_simcall_t req, unsigned int idx)
173 {
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);
179       break;
180
181     case Simcall::COMM_WAITANY:
182       remote_act = mc_model_checker->get_remote_simulation().read(remote(simcall_comm_waitany__get__comms(req) + idx));
183       break;
184
185     case Simcall::COMM_TESTANY:
186       remote_act = mc_model_checker->get_remote_simulation().read(remote(simcall_comm_testany__get__comms(req) + idx));
187       break;
188
189     default:
190       return true;
191   }
192
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();
197 }
198
199 static inline const char* get_color(int id)
200 {
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()];
205 }
206
207 } // namespace mc
208 } // namespace simgrid