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_ >> 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 xbt_assert(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 xbt_assert(stream >> comm_ >> 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 xbt_assert(stream >> comm_ >> mbox_ >> src_buff_ >> size_);
154 XBT_DEBUG("SendTransition comm:%p mbox:%u buff:%p size:%zu", comm_, 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)
168 xbt_assert(stream >> size);
169 for (int i = 0; i < size; i++) {
170 Transition* t = deserialize_transition(issuer, 0, stream);
171 XBT_DEBUG("TestAny received a transition %s", t->to_string(true).c_str());
172 transitions_.push_back(t);
175 std::string TestAnyTransition::to_string(bool verbose) const
177 auto res = xbt::string_printf("%ld: TestAny{ ", aid_);
178 for (auto const* t : transitions_)
179 res += t->to_string(verbose);
183 bool TestAnyTransition::depends(const Transition* other) const
185 return transitions_[times_considered_]->depends(other);
187 WaitAnyTransition::WaitAnyTransition(aid_t issuer, int times_considered, std::stringstream& stream)
188 : Transition(Type::WAITANY, issuer, times_considered)
191 xbt_assert(stream >> size);
192 for (int i = 0; i < size; i++) {
193 Transition* t = deserialize_transition(issuer, 0, stream);
194 transitions_.push_back(t);
197 std::string WaitAnyTransition::to_string(bool verbose) const
199 auto res = xbt::string_printf("%ld: WaitAny{ ", aid_);
200 for (auto const* t : transitions_)
201 res += t->to_string(verbose);
205 bool WaitAnyTransition::depends(const Transition* other) const
207 return transitions_[times_considered_]->depends(other);
210 bool CommSendTransition::depends(const Transition* other) const
212 if (aid_ == other->aid_)
215 if (other->type_ < type_)
216 return other->depends(this);
218 if (const auto* other_isend = dynamic_cast<const CommSendTransition*>(other))
219 return mbox_ == other_isend->mbox_;
221 if (dynamic_cast<const CommRecvTransition*>(other) != nullptr)
224 if (const auto* test = dynamic_cast<const CommTestTransition*>(other)) {
225 if (mbox_ != test->mbox_)
228 if ((aid_ != test->sender_) && (aid_ != test->receiver_) && (test->src_buff_ != src_buff_))
232 if (const auto* wait = dynamic_cast<const CommWaitTransition*>(other)) {
236 if (mbox_ != wait->mbox_)
239 if ((aid_ != wait->sender_) && (aid_ != wait->receiver_) && (wait->src_buff_ != src_buff_))
246 Transition* deserialize_transition(aid_t issuer, int times_considered, std::stringstream& stream)
249 xbt_assert(stream >> type);
250 xbt_assert(type >= 0 && type <= static_cast<short>(Transition::Type::UNKNOWN), "Invalid transition type %d received",
253 auto simcall = static_cast<Transition::Type>(type);
256 case Transition::Type::COMM_RECV:
257 return new CommRecvTransition(issuer, times_considered, stream);
258 case Transition::Type::COMM_SEND:
259 return new CommSendTransition(issuer, times_considered, stream);
260 case Transition::Type::COMM_TEST:
261 return new CommTestTransition(issuer, times_considered, stream);
262 case Transition::Type::COMM_WAIT:
263 return new CommWaitTransition(issuer, times_considered, stream);
265 case Transition::Type::TESTANY:
266 return new TestAnyTransition(issuer, times_considered, stream);
267 case Transition::Type::WAITANY:
268 return new WaitAnyTransition(issuer, times_considered, stream);
270 case Transition::Type::RANDOM:
271 return new RandomTransition(issuer, times_considered, stream);
273 case Transition::Type::UNKNOWN:
274 return new Transition(Transition::Type::UNKNOWN, issuer, times_considered);
276 THROW_IMPOSSIBLE; // Some compilers don't detect that each branch of the above switch has a return
280 } // namespace simgrid