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 (other->type_ < type_)
47 return other->depends(this);
49 if (const auto* wait = dynamic_cast<const CommWaitTransition*>(other)) {
50 if (timeout_ || wait->timeout_)
51 return true; // Timeouts are not considered by the independence theorem, thus assumed dependent
53 if (src_buff_ == wait->src_buff_ && dst_buff_ == wait->dst_buff_)
55 if (src_buff_ != nullptr && dst_buff_ != nullptr && wait->src_buff_ != nullptr && wait->dst_buff_ != nullptr &&
56 dst_buff_ != wait->src_buff_ && dst_buff_ != wait->dst_buff_ && dst_buff_ != src_buff_)
62 CommTestTransition::CommTestTransition(aid_t issuer, int times_considered, std::stringstream& stream)
63 : Transition(Type::COMM_TEST, issuer, times_considered)
65 stream >> comm_ >> sender_ >> receiver_ >> mbox_ >> src_buff_ >> dst_buff_ >> size_;
66 XBT_DEBUG("CommTestTransition comm:%p, sender:%ld receiver:%ld mbox:%u sbuff:%p rbuff:%p size:%zu", comm_, sender_,
67 receiver_, mbox_, src_buff_, dst_buff_, size_);
69 std::string CommTestTransition::to_string(bool verbose) const
71 auto res = xbt::string_printf("%ld: TestComm(from %ld to %ld, mbox=%u", aid_, sender_, receiver_, mbox_);
73 res += ", src_buff=" + xbt::string_printf("%p", src_buff_) + ", size=" + std::to_string(size_);
74 res += ", dst_buff=" + xbt::string_printf("%p", dst_buff_);
79 bool CommTestTransition::depends(const Transition* other) const
81 if (aid_ == other->aid_)
84 if (other->type_ < type_)
85 return other->depends(this);
87 if (dynamic_cast<const CommTestTransition*>(other) != nullptr)
88 return false; // Test & Test are independent
90 if (const auto* wait = dynamic_cast<const CommWaitTransition*>(other)) {
92 return true; // Timeouts are not considered by the independence theorem, thus assumed dependent
94 /* Wait & Test are independent */
101 CommRecvTransition::CommRecvTransition(aid_t issuer, int times_considered, std::stringstream& stream)
102 : Transition(Type::COMM_RECV, issuer, times_considered)
104 stream >> mbox_ >> dst_buff_;
106 std::string CommRecvTransition::to_string(bool verbose) const
108 auto res = xbt::string_printf("%ld: iRecv(mbox=%u", aid_, mbox_);
110 res += ", buff=" + xbt::string_printf("%p", dst_buff_);
114 bool CommRecvTransition::depends(const Transition* other) const
116 if (aid_ == other->aid_)
119 if (other->type_ < type_)
120 return other->depends(this);
122 if (const auto* recv = dynamic_cast<const CommRecvTransition*>(other))
123 return mbox_ == recv->mbox_;
125 if (dynamic_cast<const CommSendTransition*>(other) != nullptr)
128 if (const auto* test = dynamic_cast<const CommTestTransition*>(other)) {
129 if (mbox_ != test->mbox_)
132 if ((aid_ != test->sender_) && (aid_ != test->receiver_) && (test->dst_buff_ != dst_buff_))
136 if (auto* wait = dynamic_cast<const CommWaitTransition*>(other)) {
140 if (mbox_ != wait->mbox_)
143 if ((aid_ != wait->sender_) && (aid_ != wait->receiver_) && (wait->dst_buff_ != dst_buff_))
150 CommSendTransition::CommSendTransition(aid_t issuer, int times_considered, std::stringstream& stream)
151 : Transition(Type::COMM_SEND, issuer, times_considered)
153 stream >> mbox_ >> src_buff_ >> size_;
154 XBT_DEBUG("SendTransition mbox:%u buff:%p size:%zu", mbox_, src_buff_, size_);
156 std::string CommSendTransition::to_string(bool verbose = false) const
158 auto res = xbt::string_printf("%ld: iSend(mbox=%u", aid_, mbox_);
160 res += ", buff=" + xbt::string_printf("%p", src_buff_) + ", size=" + std::to_string(size_);
164 TestAnyTransition::TestAnyTransition(aid_t issuer, int times_considered, std::stringstream& stream)
165 : Transition(Type::TESTANY, issuer, times_considered)
169 for (int i = 0; i < size; i++) {
171 Transition* t = deserialize_transition(issuer, 0, stream);
172 XBT_DEBUG("TestAny received a transition %s", t->to_string(true).c_str());
173 transitions_.push_back(t);
176 std::string TestAnyTransition::to_string(bool verbose) const
178 auto res = xbt::string_printf("%ld: TestAny{ ", aid_);
179 for (auto const* t : transitions_)
180 res += t->to_string(verbose);
184 bool TestAnyTransition::depends(const Transition* other) const
186 return transitions_[times_considered_]->depends(other);
188 WaitAnyTransition::WaitAnyTransition(aid_t issuer, int times_considered, std::stringstream& stream)
189 : Transition(Type::WAITANY, issuer, times_considered)
193 for (int i = 0; i < size; i++) {
194 Transition* t = deserialize_transition(issuer, 0, stream);
195 transitions_.push_back(t);
198 std::string WaitAnyTransition::to_string(bool verbose) const
200 auto res = xbt::string_printf("%ld: WaitAny{ ", aid_);
201 for (auto const* t : transitions_)
202 res += t->to_string(verbose);
206 bool WaitAnyTransition::depends(const Transition* other) const
208 return transitions_[times_considered_]->depends(other);
211 bool CommSendTransition::depends(const Transition* other) const
213 if (aid_ == other->aid_)
216 if (other->type_ < type_)
217 return other->depends(this);
219 if (const auto* other_isend = dynamic_cast<const CommSendTransition*>(other))
220 return mbox_ == other_isend->mbox_;
222 if (dynamic_cast<const CommRecvTransition*>(other) != nullptr)
225 if (const auto* test = dynamic_cast<const CommTestTransition*>(other)) {
226 if (mbox_ != test->mbox_)
229 if ((aid_ != test->sender_) && (aid_ != test->receiver_) && (test->src_buff_ != src_buff_))
233 if (const auto* wait = dynamic_cast<const CommWaitTransition*>(other)) {
237 if (mbox_ != wait->mbox_)
240 if ((aid_ != wait->sender_) && (aid_ != wait->receiver_) && (wait->src_buff_ != src_buff_))
247 Transition* deserialize_transition(aid_t issuer, int times_considered, std::stringstream& stream)
251 xbt_assert(type >= 0 && type <= static_cast<short>(Transition::Type::UNKNOWN), "Invalid transition type %d received",
254 auto simcall = static_cast<Transition::Type>(type);
257 case Transition::Type::COMM_RECV:
258 return new CommRecvTransition(issuer, times_considered, stream);
259 case Transition::Type::COMM_SEND:
260 return new CommSendTransition(issuer, times_considered, stream);
261 case Transition::Type::COMM_TEST:
262 return new CommTestTransition(issuer, times_considered, stream);
263 case Transition::Type::COMM_WAIT:
264 return new CommWaitTransition(issuer, times_considered, stream);
266 case Transition::Type::TESTANY:
267 return new TestAnyTransition(issuer, times_considered, stream);
268 case Transition::Type::WAITANY:
269 return new WaitAnyTransition(issuer, times_considered, stream);
271 case Transition::Type::RANDOM:
272 return new RandomTransition(issuer, times_considered, stream);
274 case Transition::Type::UNKNOWN:
275 return new Transition(Transition::Type::UNKNOWN, issuer, times_considered);
277 THROW_IMPOSSIBLE; // Some compilers don't detect that each branch of the above switch has a return
281 } // namespace simgrid