Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement the RandomTransition
[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(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)
32 {
33   textual_ = 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     textual_ += ", src_buff=" + xbt::string_printf("%p", src_buff_) + ", size=" + std::to_string(size_);
37     textual_ += ", dst_buff=" + xbt::string_printf("%p", dst_buff_);
38   }
39   textual_ += ")";
40   return textual_;
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* send = dynamic_cast<const CommSendTransition*>(other))
51     return send->depends(this);
52
53   if (auto* recv = dynamic_cast<const CommRecvTransition*>(other))
54     return recv->depends(this);
55
56   /* Timeouts in wait transitions are not considered by the independence theorem, thus assumed dependent */
57   if (const auto* wait = dynamic_cast<const CommWaitTransition*>(other)) {
58     if (timeout_ || wait->timeout_)
59       return true;
60
61     if (src_buff_ == wait->src_buff_ && dst_buff_ == wait->dst_buff_)
62       return false;
63     if (src_buff_ != nullptr && dst_buff_ != nullptr && wait->src_buff_ != nullptr && wait->dst_buff_ != nullptr &&
64         dst_buff_ != wait->src_buff_ && dst_buff_ != wait->dst_buff_ && dst_buff_ != src_buff_)
65       return false;
66   }
67
68   return true;
69 }
70
71 CommRecvTransition::CommRecvTransition(aid_t issuer, int times_considered, char* buffer)
72     : Transition(issuer, times_considered)
73 {
74   std::stringstream stream(buffer);
75   stream >> mbox_ >> dst_buff_;
76 }
77 std::string CommRecvTransition::to_string(bool verbose)
78 {
79   textual_ = xbt::string_printf("%ld: iRecv(mbox=%u", aid_, mbox_);
80   if (verbose)
81     textual_ += ", buff=" + xbt::string_printf("%p", dst_buff_);
82   textual_ += ")";
83   return textual_;
84 }
85 bool CommRecvTransition::depends(const Transition* other) const
86 {
87   if (aid_ == other->aid_)
88     return false;
89
90   if (dynamic_cast<const RandomTransition*>(other) != nullptr)
91     return false; // Random is indep with any transition
92
93   if (const auto* other_irecv = dynamic_cast<const CommRecvTransition*>(other))
94     return mbox_ == other_irecv->mbox_;
95
96   if (auto* isend = dynamic_cast<const CommSendTransition*>(other))
97     return isend->depends(this);
98
99   if (auto* wait = dynamic_cast<const CommWaitTransition*>(other)) {
100     if (wait->timeout_)
101       return true;
102
103     if (mbox_ != wait->mbox_)
104       return false;
105
106     if ((aid_ != wait->sender_) && (aid_ != wait->receiver_))
107       return false;
108
109     if (wait->dst_buff_ != dst_buff_)
110       return false;
111   }
112
113   /* FIXME: the following rule assumes that the result of the isend/irecv call is not stored in a buffer used in the
114    * test call. */
115 #if 0
116   if (dynamic_cast<ActivityTestSimcall*>(other))
117     return false;
118 #endif
119
120   return true;
121 }
122
123 CommSendTransition::CommSendTransition(aid_t issuer, int times_considered, char* buffer)
124     : Transition(issuer, times_considered)
125 {
126   std::stringstream stream(buffer);
127   stream >> mbox_ >> src_buff_ >> size_;
128   XBT_DEBUG("SendTransition mbox:%u buff:%p size:%zu", mbox_, src_buff_, size_);
129 }
130 std::string CommSendTransition::to_string(bool verbose = false)
131 {
132   textual_ = xbt::string_printf("%ld: iSend(mbox=%u", aid_, mbox_);
133   if (verbose)
134     textual_ += ", buff=" + xbt::string_printf("%p", src_buff_) + ", size=" + std::to_string(size_);
135   textual_ += ")";
136   return textual_;
137 }
138 bool CommSendTransition::depends(const Transition* other) const
139 {
140   if (aid_ == other->aid_)
141     return false;
142
143   if (dynamic_cast<const RandomTransition*>(other) != nullptr)
144     return false; // Random is indep with any transition
145
146   if (const auto* other_isend = dynamic_cast<const CommSendTransition*>(other))
147     return mbox_ == other_isend->mbox_;
148
149   // FIXME: Not in the former dependency check because of the ordering but seems logical to add it
150   if (dynamic_cast<const CommRecvTransition*>(other) != nullptr)
151     return false;
152
153   if (const auto* wait = dynamic_cast<const CommWaitTransition*>(other)) {
154     if (wait->timeout_)
155       return true;
156
157     if (mbox_ != wait->mbox_)
158       return false;
159
160     if ((aid_ != wait->sender_) && (aid_ != wait->receiver_))
161       return false;
162
163     if (wait->src_buff_ != src_buff_)
164       return false;
165   }
166
167   /* FIXME: the following rule assumes that the result of the isend/irecv call is not stored in a buffer used in the
168    * test call. */
169 #if 0
170   if (dynamic_cast<ActivityTestSimcall*>(other))
171     return false;
172 #endif
173
174   return true;
175 }
176
177 Transition* recv_transition(aid_t issuer, int times_considered, kernel::actor::SimcallObserver::Simcall simcall,
178                             char* buffer)
179 {
180   switch (simcall) {
181     case kernel::actor::SimcallObserver::Simcall::COMM_WAIT:
182       return new CommWaitTransition(issuer, times_considered, buffer);
183     case kernel::actor::SimcallObserver::Simcall::IRECV:
184       return new CommRecvTransition(issuer, times_considered, buffer);
185     case kernel::actor::SimcallObserver::Simcall::ISEND:
186       return new CommSendTransition(issuer, times_considered, buffer);
187
188     case kernel::actor::SimcallObserver::Simcall::RANDOM:
189       return new RandomTransition(issuer, times_considered, buffer);
190
191     case kernel::actor::SimcallObserver::Simcall::UNKNOWN:
192       return new Transition(issuer, times_considered);
193     default:
194       xbt_die("recv_transition of type %s unimplemented", kernel::actor::SimcallObserver::to_c_str(simcall));
195   }
196 }
197
198 } // namespace mc
199 } // namespace simgrid