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 xbt_assert(stream >> timeout_ >> comm_ >> sender_ >> receiver_ >> mbox_ >> sbuff_ >> rbuff_ >> size_);
27 XBT_DEBUG("CommWaitTransition %s comm:%" PRIxPTR ", sender:%ld receiver:%ld mbox:%u sbuff:%" PRIxPTR
28 " rbuff:%" PRIxPTR " size:%zu",
29 (timeout_ ? "timeout" : "no-timeout"), comm_, sender_, receiver_, mbox_, sbuff_, rbuff_, size_);
31 std::string CommWaitTransition::to_string(bool verbose) const
33 auto res = xbt::string_printf("%ld: WaitComm(from %ld to %ld, mbox=%u, %s", aid_, sender_, receiver_, mbox_,
34 (timeout_ ? "timeout" : "no timeout"));
36 res += ", sbuff=" + xbt::string_printf("%" PRIxPTR, sbuff_) + ", size=" + std::to_string(size_);
37 res += ", rbuff=" + xbt::string_printf("%" PRIxPTR, rbuff_);
42 bool CommWaitTransition::depends(const Transition* other) const
44 if (aid_ == other->aid_)
47 if (other->type_ < type_)
48 return other->depends(this);
50 if (const auto* wait = dynamic_cast<const CommWaitTransition*>(other)) {
51 if (timeout_ || wait->timeout_)
52 return true; // Timeouts are not considered by the independence theorem, thus assumed dependent
54 if (sbuff_ == wait->sbuff_ && rbuff_ == wait->rbuff_)
56 if (sbuff_ != 0 && rbuff_ != 0 && wait->sbuff_ != 0 && wait->rbuff_ != 0 && rbuff_ != wait->sbuff_ &&
57 rbuff_ != wait->rbuff_ && rbuff_ != sbuff_)
63 CommTestTransition::CommTestTransition(aid_t issuer, int times_considered, std::stringstream& stream)
64 : Transition(Type::COMM_TEST, issuer, times_considered)
66 xbt_assert(stream >> comm_ >> sender_ >> receiver_ >> mbox_ >> sbuff_ >> rbuff_ >> size_);
67 XBT_DEBUG("CommTestTransition comm:%" PRIxPTR ", sender:%ld receiver:%ld mbox:%u sbuff:%" PRIxPTR " rbuff:%" PRIxPTR
69 comm_, sender_, receiver_, mbox_, sbuff_, rbuff_, size_);
71 std::string CommTestTransition::to_string(bool verbose) const
73 auto res = xbt::string_printf("%ld: TestComm(from %ld to %ld, mbox=%u", aid_, sender_, receiver_, mbox_);
75 res += ", sbuff=" + xbt::string_printf("%" PRIxPTR, sbuff_) + ", size=" + std::to_string(size_);
76 res += ", rbuff=" + xbt::string_printf("%" PRIxPTR, rbuff_);
81 bool CommTestTransition::depends(const Transition* other) const
83 if (aid_ == other->aid_)
86 if (other->type_ < type_)
87 return other->depends(this);
89 if (dynamic_cast<const CommTestTransition*>(other) != nullptr)
90 return false; // Test & Test are independent
92 if (const auto* wait = dynamic_cast<const CommWaitTransition*>(other)) {
94 return true; // Timeouts are not considered by the independence theorem, thus assumed dependent
96 /* Wait & Test are independent */
103 CommRecvTransition::CommRecvTransition(aid_t issuer, int times_considered, std::stringstream& stream)
104 : Transition(Type::COMM_RECV, issuer, times_considered)
106 xbt_assert(stream >> comm_ >> mbox_ >> rbuff_ >> tag_);
108 std::string CommRecvTransition::to_string(bool verbose) const
110 auto res = xbt::string_printf("%ld: iRecv(mbox=%u", aid_, mbox_);
112 res += ", rbuff=" + xbt::string_printf("%" PRIxPTR, rbuff_);
116 bool CommRecvTransition::depends(const Transition* other) const
118 if (aid_ == other->aid_)
121 if (other->type_ < type_)
122 return other->depends(this);
124 if (const auto* recv = dynamic_cast<const CommRecvTransition*>(other))
125 return mbox_ == recv->mbox_;
127 if (dynamic_cast<const CommSendTransition*>(other) != nullptr)
130 if (const auto* test = dynamic_cast<const CommTestTransition*>(other)) {
131 if (mbox_ != test->mbox_)
134 if ((aid_ != test->sender_) && (aid_ != test->receiver_) && (test->rbuff_ != rbuff_))
138 if (auto* wait = dynamic_cast<const CommWaitTransition*>(other)) {
142 if (mbox_ != wait->mbox_)
145 if ((aid_ != wait->sender_) && (aid_ != wait->receiver_) && (wait->rbuff_ != rbuff_))
152 CommSendTransition::CommSendTransition(aid_t issuer, int times_considered, std::stringstream& stream)
153 : Transition(Type::COMM_SEND, issuer, times_considered)
155 xbt_assert(stream >> comm_ >> mbox_ >> sbuff_ >> size_ >> tag_);
156 XBT_DEBUG("SendTransition comm:%" PRIxPTR " mbox:%u sbuff:%" PRIxPTR " size:%zu", comm_, mbox_, sbuff_, size_);
158 std::string CommSendTransition::to_string(bool verbose = false) const
160 auto res = xbt::string_printf("%ld: iSend(mbox=%u", aid_, mbox_);
162 res += ", sbuff=" + xbt::string_printf("%" PRIxPTR, sbuff_) + ", size=" + std::to_string(size_);
166 TestAnyTransition::TestAnyTransition(aid_t issuer, int times_considered, std::stringstream& stream)
167 : Transition(Type::TESTANY, issuer, times_considered)
170 xbt_assert(stream >> size);
171 for (int i = 0; i < size; i++) {
172 Transition* t = deserialize_transition(issuer, 0, stream);
173 XBT_DEBUG("TestAny received a transition %s", t->to_string(true).c_str());
174 transitions_.push_back(t);
177 std::string TestAnyTransition::to_string(bool verbose) const
179 auto res = xbt::string_printf("%ld: TestAny{ ", aid_);
180 for (auto const* t : transitions_)
181 res += t->to_string(verbose);
185 bool TestAnyTransition::depends(const Transition* other) const
187 return transitions_[times_considered_]->depends(other);
189 WaitAnyTransition::WaitAnyTransition(aid_t issuer, int times_considered, std::stringstream& stream)
190 : Transition(Type::WAITANY, issuer, times_considered)
193 xbt_assert(stream >> size);
194 for (int i = 0; i < size; i++) {
195 Transition* t = deserialize_transition(issuer, 0, stream);
196 transitions_.push_back(t);
199 std::string WaitAnyTransition::to_string(bool verbose) const
201 auto res = xbt::string_printf("%ld: WaitAny{ ", aid_);
202 for (auto const* t : transitions_)
203 res += t->to_string(verbose);
207 bool WaitAnyTransition::depends(const Transition* other) const
209 return transitions_[times_considered_]->depends(other);
212 bool CommSendTransition::depends(const Transition* other) const
214 if (aid_ == other->aid_)
217 if (other->type_ < type_)
218 return other->depends(this);
220 if (const auto* other_isend = dynamic_cast<const CommSendTransition*>(other))
221 return mbox_ == other_isend->mbox_;
223 if (dynamic_cast<const CommRecvTransition*>(other) != nullptr)
226 if (const auto* test = dynamic_cast<const CommTestTransition*>(other)) {
227 if (mbox_ != test->mbox_)
230 if ((aid_ != test->sender_) && (aid_ != test->receiver_) && (test->sbuff_ != sbuff_))
234 if (const auto* wait = dynamic_cast<const CommWaitTransition*>(other)) {
238 if (mbox_ != wait->mbox_)
241 if ((aid_ != wait->sender_) && (aid_ != wait->receiver_) && (wait->sbuff_ != sbuff_))
248 Transition* deserialize_transition(aid_t issuer, int times_considered, std::stringstream& stream)
251 xbt_assert(stream >> type);
252 xbt_assert(type >= 0 && type <= static_cast<short>(Transition::Type::UNKNOWN), "Invalid transition type %d received",
255 auto simcall = static_cast<Transition::Type>(type);
258 case Transition::Type::COMM_RECV:
259 return new CommRecvTransition(issuer, times_considered, stream);
260 case Transition::Type::COMM_SEND:
261 return new CommSendTransition(issuer, times_considered, stream);
262 case Transition::Type::COMM_TEST:
263 return new CommTestTransition(issuer, times_considered, stream);
264 case Transition::Type::COMM_WAIT:
265 return new CommWaitTransition(issuer, times_considered, stream);
267 case Transition::Type::TESTANY:
268 return new TestAnyTransition(issuer, times_considered, stream);
269 case Transition::Type::WAITANY:
270 return new WaitAnyTransition(issuer, times_considered, stream);
272 case Transition::Type::RANDOM:
273 return new RandomTransition(issuer, times_considered, stream);
275 case Transition::Type::UNKNOWN:
276 return new Transition(Transition::Type::UNKNOWN, issuer, times_considered);
278 THROW_IMPOSSIBLE; // Some compilers don't detect that each branch of the above switch has a return
282 } // namespace simgrid