Logo AND Algorithmique Numérique Distribuée

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