1 /* Copyright (c) 2015-2022. 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/api/TransitionComm.hpp"
7 #include "xbt/asserts.h"
8 #include <simgrid/config.h>
10 #include "src/mc/ModelChecker.hpp"
11 #include "src/mc/Session.hpp"
12 #include "src/mc/api/State.hpp"
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_trans_comm, mc_transition,
18 "Logging specific to MC transitions about communications");
23 CommWaitTransition::CommWaitTransition(aid_t issuer, int times_considered, std::stringstream& stream)
24 : Transition(Type::COMM_WAIT, issuer, times_considered)
26 stream >> timeout_ >> comm_ >> sender_ >> receiver_ >> mbox_ >> src_buff_ >> dst_buff_ >> size_;
27 XBT_DEBUG("CommWaitTransition %s comm:%p, sender:%ld receiver:%ld mbox:%u sbuff:%p rbuff:%p size:%zu",
28 (timeout_ ? "timeout" : "no-timeout"), comm_, sender_, receiver_, mbox_, src_buff_, dst_buff_, size_);
30 std::string CommWaitTransition::to_string(bool verbose) const
32 auto res = xbt::string_printf("%ld: WaitComm(from %ld to %ld, mbox=%u, %s", aid_, sender_, receiver_, mbox_,
33 (timeout_ ? "timeout" : "no timeout"));
35 res += ", src_buff=" + xbt::string_printf("%p", src_buff_) + ", size=" + std::to_string(size_);
36 res += ", dst_buff=" + xbt::string_printf("%p", dst_buff_);
41 bool CommWaitTransition::depends(const Transition* other) const
43 if (aid_ == other->aid_)
46 if (dynamic_cast<const RandomTransition*>(other) != nullptr)
47 return false; // Random is indep with any transition
49 if (auto* any = dynamic_cast<const WaitAnyTransition*>(other))
50 return any->depends(this);
51 if (auto* any = dynamic_cast<const TestAnyTransition*>(other))
52 return any->depends(this);
54 if (auto* recv = dynamic_cast<const CommRecvTransition*>(other))
55 return recv->depends(this);
57 if (auto* send = dynamic_cast<const CommSendTransition*>(other))
58 return send->depends(this);
60 if (auto* test = dynamic_cast<const CommTestTransition*>(other))
61 return test->depends(this);
63 if (const auto* wait = dynamic_cast<const CommWaitTransition*>(other)) {
64 if (timeout_ || wait->timeout_)
65 return true; // Timeouts are not considered by the independence theorem, thus assumed dependent
67 if (src_buff_ == wait->src_buff_ && dst_buff_ == wait->dst_buff_)
69 if (src_buff_ != nullptr && dst_buff_ != nullptr && wait->src_buff_ != nullptr && wait->dst_buff_ != nullptr &&
70 dst_buff_ != wait->src_buff_ && dst_buff_ != wait->dst_buff_ && dst_buff_ != src_buff_)
76 CommTestTransition::CommTestTransition(aid_t issuer, int times_considered, std::stringstream& stream)
77 : Transition(Type::COMM_TEST, issuer, times_considered)
79 stream >> comm_ >> sender_ >> receiver_ >> mbox_ >> src_buff_ >> dst_buff_ >> size_;
80 XBT_DEBUG("CommTestTransition comm:%p, sender:%ld receiver:%ld mbox:%u sbuff:%p rbuff:%p size:%zu", comm_, sender_,
81 receiver_, mbox_, src_buff_, dst_buff_, size_);
83 std::string CommTestTransition::to_string(bool verbose) const
85 auto res = xbt::string_printf("%ld: TestComm(from %ld to %ld, mbox=%u", aid_, sender_, receiver_, mbox_);
87 res += ", src_buff=" + xbt::string_printf("%p", src_buff_) + ", size=" + std::to_string(size_);
88 res += ", dst_buff=" + xbt::string_printf("%p", dst_buff_);
93 bool CommTestTransition::depends(const Transition* other) const
95 if (aid_ == other->aid_)
98 if (dynamic_cast<const RandomTransition*>(other) != nullptr)
99 return false; // Test & Random are independent (Random is independent with anything)
101 if (auto* any = dynamic_cast<const WaitAnyTransition*>(other))
102 return any->depends(this);
103 if (auto* any = dynamic_cast<const TestAnyTransition*>(other))
104 return any->depends(this);
106 if (auto* recv = dynamic_cast<const CommRecvTransition*>(other))
107 return recv->depends(this); // Recv < Test (alphabetical ordering)
109 if (auto* send = dynamic_cast<const CommSendTransition*>(other))
110 return send->depends(this); // Send < Test (alphabetical ordering)
112 if (dynamic_cast<const CommTestTransition*>(other) != nullptr)
113 return false; // Test & Test are independent
115 if (const auto* wait = dynamic_cast<const CommWaitTransition*>(other)) {
117 return true; // Timeouts are not considered by the independence theorem, thus assumed dependent
119 /* Wait & Test are independent */
126 CommRecvTransition::CommRecvTransition(aid_t issuer, int times_considered, std::stringstream& stream)
127 : Transition(Type::COMM_RECV, issuer, times_considered)
129 stream >> mbox_ >> dst_buff_;
131 std::string CommRecvTransition::to_string(bool verbose) const
133 auto res = xbt::string_printf("%ld: iRecv(mbox=%u", aid_, mbox_);
135 res += ", buff=" + xbt::string_printf("%p", dst_buff_);
139 bool CommRecvTransition::depends(const Transition* other) const
141 if (aid_ == other->aid_)
144 if (dynamic_cast<const RandomTransition*>(other) != nullptr)
145 return false; // Random is indep with any transition
146 if (auto* any = dynamic_cast<const WaitAnyTransition*>(other))
147 return any->depends(this);
148 if (auto* any = dynamic_cast<const TestAnyTransition*>(other))
149 return any->depends(this);
151 if (const auto* recv = dynamic_cast<const CommRecvTransition*>(other))
152 return mbox_ == recv->mbox_;
154 if (dynamic_cast<const CommSendTransition*>(other) != nullptr)
157 if (const auto* test = dynamic_cast<const CommTestTransition*>(other)) {
158 if (mbox_ != test->mbox_)
161 if ((aid_ != test->sender_) && (aid_ != test->receiver_) && (test->dst_buff_ != dst_buff_))
165 if (auto* wait = dynamic_cast<const CommWaitTransition*>(other)) {
169 if (mbox_ != wait->mbox_)
172 if ((aid_ != wait->sender_) && (aid_ != wait->receiver_) && (wait->dst_buff_ != dst_buff_))
179 CommSendTransition::CommSendTransition(aid_t issuer, int times_considered, std::stringstream& stream)
180 : Transition(Type::COMM_SEND, issuer, times_considered)
182 stream >> mbox_ >> src_buff_ >> size_;
183 XBT_DEBUG("SendTransition mbox:%u buff:%p size:%zu", mbox_, src_buff_, size_);
185 std::string CommSendTransition::to_string(bool verbose = false) const
187 auto res = xbt::string_printf("%ld: iSend(mbox=%u", aid_, mbox_);
189 res += ", buff=" + xbt::string_printf("%p", src_buff_) + ", size=" + std::to_string(size_);
193 TestAnyTransition::TestAnyTransition(aid_t issuer, int times_considered, std::stringstream& stream)
194 : Transition(Type::TESTANY, issuer, times_considered)
198 for (int i = 0; i < size; i++) {
200 Transition* t = deserialize_transition(issuer, 0, stream);
201 XBT_DEBUG("TestAny received a transition %s", t->to_string(true).c_str());
202 transitions_.push_back(t);
205 std::string TestAnyTransition::to_string(bool verbose) const
207 auto res = xbt::string_printf("%ld: TestAny{ ", aid_);
208 for (auto const* t : transitions_)
209 res += t->to_string(verbose);
213 bool TestAnyTransition::depends(const Transition* other) const
215 return transitions_[times_considered_]->depends(other);
217 WaitAnyTransition::WaitAnyTransition(aid_t issuer, int times_considered, std::stringstream& stream)
218 : Transition(Type::WAITANY, issuer, times_considered)
222 for (int i = 0; i < size; i++) {
223 Transition* t = deserialize_transition(issuer, 0, stream);
224 transitions_.push_back(t);
227 std::string WaitAnyTransition::to_string(bool verbose) const
229 auto res = xbt::string_printf("%ld: WaitAny{ ", aid_);
230 for (auto const* t : transitions_)
231 res += t->to_string(verbose);
235 bool WaitAnyTransition::depends(const Transition* other) const
237 return transitions_[times_considered_]->depends(other);
240 bool CommSendTransition::depends(const Transition* other) const
242 if (aid_ == other->aid_)
245 if (dynamic_cast<const RandomTransition*>(other) != nullptr)
246 return false; // Random is indep with any transition
248 if (auto* any = dynamic_cast<const WaitAnyTransition*>(other))
249 return any->depends(this);
250 if (auto* any = dynamic_cast<const TestAnyTransition*>(other))
251 return any->depends(this);
253 if (const auto* other_isend = dynamic_cast<const CommSendTransition*>(other))
254 return mbox_ == other_isend->mbox_;
256 if (dynamic_cast<const CommRecvTransition*>(other) != nullptr)
259 if (const auto* test = dynamic_cast<const CommTestTransition*>(other)) {
260 if (mbox_ != test->mbox_)
263 if ((aid_ != test->sender_) && (aid_ != test->receiver_) && (test->src_buff_ != src_buff_))
267 if (const auto* wait = dynamic_cast<const CommWaitTransition*>(other)) {
271 if (mbox_ != wait->mbox_)
274 if ((aid_ != wait->sender_) && (aid_ != wait->receiver_) && (wait->src_buff_ != src_buff_))
281 Transition* deserialize_transition(aid_t issuer, int times_considered, std::stringstream& stream)
285 xbt_assert(type >= 0 && type <= static_cast<short>(Transition::Type::UNKNOWN), "Invalid transition type %d received",
288 auto simcall = static_cast<Transition::Type>(type);
291 case Transition::Type::COMM_RECV:
292 return new CommRecvTransition(issuer, times_considered, stream);
293 case Transition::Type::COMM_SEND:
294 return new CommSendTransition(issuer, times_considered, stream);
295 case Transition::Type::COMM_TEST:
296 return new CommTestTransition(issuer, times_considered, stream);
297 case Transition::Type::COMM_WAIT:
298 return new CommWaitTransition(issuer, times_considered, stream);
300 case Transition::Type::TESTANY:
301 return new TestAnyTransition(issuer, times_considered, stream);
302 case Transition::Type::WAITANY:
303 return new WaitAnyTransition(issuer, times_considered, stream);
305 case Transition::Type::RANDOM:
306 return new RandomTransition(issuer, times_considered, stream);
308 case Transition::Type::UNKNOWN:
309 return new Transition(Transition::Type::UNKNOWN, issuer, times_considered);
311 THROW_IMPOSSIBLE; // Some compilers don't detect that each branch of the above switch has a return
315 } // namespace simgrid