Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
dc14fe9226899ce480c0a3cce8af16e8cdad5ea6
[simgrid.git] / src / mc / api / TransitionComm.cpp
1 /* Copyright (c) 2015-2022. 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/api/TransitionComm.hpp"
7 #include "xbt/asserts.h"
8 #include <simgrid/config.h>
9 #if SIMGRID_HAVE_MC
10 #include "src/mc/ModelChecker.hpp"
11 #include "src/mc/Session.hpp"
12 #include "src/mc/api/State.hpp"
13 #endif
14
15 #include <sstream>
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_trans_comm, mc_transition,
18                                 "Logging specific to MC transitions about communications");
19
20 namespace simgrid {
21 namespace mc {
22
23 CommWaitTransition::CommWaitTransition(aid_t issuer, int times_considered, char* buffer)
24     : Transition(Type::COMM_WAIT, issuer, times_considered)
25 {
26   std::stringstream stream(buffer);
27   stream >> timeout_ >> comm_ >> sender_ >> receiver_ >> mbox_ >> src_buff_ >> dst_buff_ >> size_;
28   XBT_DEBUG("CommWaitTransition %s comm:%p, sender:%ld receiver:%ld mbox:%u sbuff:%p rbuff:%p size:%zu",
29             (timeout_ ? "timeout" : "no-timeout"), comm_, sender_, receiver_, mbox_, src_buff_, dst_buff_, size_);
30 }
31 std::string CommWaitTransition::to_string(bool verbose) const
32 {
33   auto res = xbt::string_printf("%ld: WaitComm(from %ld to %ld, mbox=%u, %s", aid_, sender_, receiver_, mbox_,
34                                 (timeout_ ? "timeout" : "no timeout"));
35   if (verbose) {
36     res += ", src_buff=" + xbt::string_printf("%p", src_buff_) + ", size=" + std::to_string(size_);
37     res += ", dst_buff=" + xbt::string_printf("%p", dst_buff_);
38   }
39   res += ")";
40   return res;
41 }
42 bool CommWaitTransition::depends(const Transition* other) const
43 {
44   if (aid_ == other->aid_)
45     return false;
46
47   if (dynamic_cast<const RandomTransition*>(other) != nullptr)
48     return false; // Random is indep with any transition
49
50   if (auto* recv = dynamic_cast<const CommRecvTransition*>(other))
51     return recv->depends(this);
52
53   if (auto* send = dynamic_cast<const CommSendTransition*>(other))
54     return send->depends(this);
55
56   if (auto* test = dynamic_cast<const CommTestTransition*>(other))
57     return test->depends(this);
58
59   if (const auto* wait = dynamic_cast<const CommWaitTransition*>(other)) {
60     if (timeout_ || wait->timeout_)
61       return true; // Timeouts are not considered by the independence theorem, thus assumed dependent
62
63     if (src_buff_ == wait->src_buff_ && dst_buff_ == wait->dst_buff_)
64       return false;
65     if (src_buff_ != nullptr && dst_buff_ != nullptr && wait->src_buff_ != nullptr && wait->dst_buff_ != nullptr &&
66         dst_buff_ != wait->src_buff_ && dst_buff_ != wait->dst_buff_ && dst_buff_ != src_buff_)
67       return false;
68   }
69
70   return true;
71 }
72 CommTestTransition::CommTestTransition(aid_t issuer, int times_considered, char* buffer)
73     : Transition(Type::COMM_TEST, issuer, times_considered)
74 {
75   std::stringstream stream(buffer);
76   stream >> comm_ >> sender_ >> receiver_ >> mbox_ >> src_buff_ >> dst_buff_ >> size_;
77   XBT_DEBUG("CommTestTransition comm:%p, sender:%ld receiver:%ld mbox:%u sbuff:%p rbuff:%p size:%zu", comm_, sender_,
78             receiver_, mbox_, src_buff_, dst_buff_, size_);
79 }
80 std::string CommTestTransition::to_string(bool verbose) const
81 {
82   auto res = xbt::string_printf("%ld: TestComm(from %ld to %ld, mbox=%u", aid_, sender_, receiver_, mbox_);
83   if (verbose) {
84     res += ", src_buff=" + xbt::string_printf("%p", src_buff_) + ", size=" + std::to_string(size_);
85     res += ", dst_buff=" + xbt::string_printf("%p", dst_buff_);
86   }
87   res += ")";
88   return res;
89 }
90 bool CommTestTransition::depends(const Transition* other) const
91 {
92   if (aid_ == other->aid_)
93     return false;
94
95   if (dynamic_cast<const RandomTransition*>(other) != nullptr)
96     return false; // Test & Random are independent (Random is independent with anything)
97
98   if (auto* recv = dynamic_cast<const CommRecvTransition*>(other))
99     return recv->depends(this); // Recv < Test (alphabetical ordering)
100
101   if (auto* send = dynamic_cast<const CommSendTransition*>(other))
102     return send->depends(this); // Send < Test (alphabetical ordering)
103
104   if (dynamic_cast<const CommTestTransition*>(other) != nullptr)
105     return false; // Test & Test are independent
106
107   if (const auto* wait = dynamic_cast<const CommWaitTransition*>(other)) {
108     if (wait->timeout_)
109       return true; // Timeouts are not considered by the independence theorem, thus assumed dependent
110
111     /* Wait & Test are independent */
112     return false;
113   }
114
115   return true;
116 }
117
118 CommRecvTransition::CommRecvTransition(aid_t issuer, int times_considered, char* buffer)
119     : Transition(Type::COMM_RECV, issuer, times_considered)
120 {
121   std::stringstream stream(buffer);
122   stream >> mbox_ >> dst_buff_;
123 }
124 std::string CommRecvTransition::to_string(bool verbose) const
125 {
126   auto res = xbt::string_printf("%ld: iRecv(mbox=%u", aid_, mbox_);
127   if (verbose)
128     res += ", buff=" + xbt::string_printf("%p", dst_buff_);
129   res += ")";
130   return res;
131 }
132 bool CommRecvTransition::depends(const Transition* other) const
133 {
134   if (aid_ == other->aid_)
135     return false;
136
137   if (dynamic_cast<const RandomTransition*>(other) != nullptr)
138     return false; // Random is indep with any transition
139
140   if (const auto* recv = dynamic_cast<const CommRecvTransition*>(other))
141     return mbox_ == recv->mbox_;
142
143   if (dynamic_cast<const CommSendTransition*>(other) != nullptr)
144     return false;
145
146   if (const auto* test = dynamic_cast<const CommTestTransition*>(other)) {
147     if (mbox_ != test->mbox_)
148       return false;
149
150     if ((aid_ != test->sender_) && (aid_ != test->receiver_) && (test->dst_buff_ != dst_buff_))
151       return false;
152   }
153
154   if (auto* wait = dynamic_cast<const CommWaitTransition*>(other)) {
155     if (wait->timeout_)
156       return true;
157
158     if (mbox_ != wait->mbox_)
159       return false;
160
161     if ((aid_ != wait->sender_) && (aid_ != wait->receiver_) && (wait->dst_buff_ != dst_buff_))
162       return false;
163   }
164
165   return true;
166 }
167
168 CommSendTransition::CommSendTransition(aid_t issuer, int times_considered, char* buffer)
169     : Transition(Type::COMM_SEND, issuer, times_considered)
170 {
171   std::stringstream stream(buffer);
172   stream >> mbox_ >> src_buff_ >> size_;
173   XBT_DEBUG("SendTransition mbox:%u buff:%p size:%zu", mbox_, src_buff_, size_);
174 }
175 std::string CommSendTransition::to_string(bool verbose = false) const
176 {
177   auto res = xbt::string_printf("%ld: iSend(mbox=%u", aid_, mbox_);
178   if (verbose)
179     res += ", buff=" + xbt::string_printf("%p", src_buff_) + ", size=" + std::to_string(size_);
180   res += ")";
181   return res;
182 }
183
184 bool CommSendTransition::depends(const Transition* other) const
185 {
186   if (aid_ == other->aid_)
187     return false;
188
189   if (dynamic_cast<const RandomTransition*>(other) != nullptr)
190     return false; // Random is indep with any transition
191
192   if (const auto* other_isend = dynamic_cast<const CommSendTransition*>(other))
193     return mbox_ == other_isend->mbox_;
194
195   if (dynamic_cast<const CommRecvTransition*>(other) != nullptr)
196     return false;
197
198   if (const auto* test = dynamic_cast<const CommTestTransition*>(other)) {
199     if (mbox_ != test->mbox_)
200       return false;
201
202     if ((aid_ != test->sender_) && (aid_ != test->receiver_) && (test->src_buff_ != src_buff_))
203       return false;
204   }
205
206   if (const auto* wait = dynamic_cast<const CommWaitTransition*>(other)) {
207     if (wait->timeout_)
208       return true;
209
210     if (mbox_ != wait->mbox_)
211       return false;
212
213     if ((aid_ != wait->sender_) && (aid_ != wait->receiver_) && (wait->src_buff_ != src_buff_))
214       return false;
215   }
216
217   return true;
218 }
219
220 Transition* recv_transition(aid_t issuer, int times_considered, Transition::Type simcall, char* buffer)
221 {
222   switch (simcall) {
223     case Transition::Type::COMM_RECV:
224       return new CommRecvTransition(issuer, times_considered, buffer);
225     case Transition::Type::COMM_SEND:
226       return new CommSendTransition(issuer, times_considered, buffer);
227     case Transition::Type::COMM_TEST:
228       return new CommTestTransition(issuer, times_considered, buffer);
229     case Transition::Type::COMM_WAIT:
230       return new CommWaitTransition(issuer, times_considered, buffer);
231
232     case Transition::Type::RANDOM:
233       return new RandomTransition(issuer, times_considered, buffer);
234
235     case Transition::Type::UNKNOWN:
236       return new Transition(Transition::Type::UNKNOWN, issuer, times_considered);
237     default:
238       xbt_die("recv_transition of type %s unimplemented", Transition::to_c_str(simcall));
239   }
240 }
241
242 } // namespace mc
243 } // namespace simgrid