Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Don't send sbuff and rbuff to the MC checker
[simgrid.git] / src / kernel / actor / CommObserver.cpp
1 /* Copyright (c) 2019-2023. 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 "simgrid/s4u/Host.hpp"
7 #include "src/kernel/activity/CommImpl.hpp"
8 #include "src/kernel/activity/MailboxImpl.hpp"
9 #include "src/kernel/actor/ActorImpl.hpp"
10 #include "src/kernel/actor/SimcallObserver.hpp"
11 #include "src/mc/mc_config.hpp"
12
13 #include <sstream>
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(obs_comm, mc_observer, "Logging specific to the Communication simcalls observation");
16
17 namespace simgrid::kernel::actor {
18
19 ActivityTestanySimcall::ActivityTestanySimcall(ActorImpl* actor, const std::vector<activity::ActivityImpl*>& activities,
20                                                std::string fun_call)
21     : ResultingSimcall(actor, -1), activities_(activities), fun_call_(fun_call)
22 {
23   indexes_.clear();
24   // list all the activities that are ready
25   for (unsigned i = 0; i < activities_.size(); i++)
26     if (activities_[i]->test(get_issuer()))
27       indexes_.push_back(i);
28 }
29
30 int ActivityTestanySimcall::get_max_consider() const
31 {
32   return indexes_.size() + 1;
33 }
34
35 void ActivityTestanySimcall::prepare(int times_considered)
36 {
37   if (times_considered < static_cast<int>(indexes_.size()))
38     next_value_ = indexes_.at(times_considered);
39   else
40     next_value_ = -1;
41 }
42 static void serialize_activity_test(const activity::ActivityImpl* act, std::stringstream& stream)
43 {
44   if (const auto* comm = dynamic_cast<activity::CommImpl const*>(act)) {
45     stream << "  " << (short)mc::Transition::Type::COMM_TEST;
46     stream << ' ' << comm->get_id();
47     stream << ' ' << (comm->src_actor_ != nullptr ? comm->src_actor_->get_pid() : -1);
48     stream << ' ' << (comm->dst_actor_ != nullptr ? comm->dst_actor_->get_pid() : -1);
49     stream << ' ' << comm->get_mailbox_id();
50   } else {
51     stream << (short)mc::Transition::Type::UNKNOWN;
52     XBT_CRITICAL("Unknown transition in a test any. Bad things may happen");
53   }
54 }
55 static std::string to_string_activity_test(const activity::ActivityImpl* act)
56 {
57   if (const auto* comm = dynamic_cast<activity::CommImpl const*>(act)) {
58     return "CommTest(comm_id:" + std::to_string(comm->get_id()) +
59            " src:" + std::to_string(comm->src_actor_ != nullptr ? comm->src_actor_->get_pid() : -1) +
60            " dst:" + std::to_string(comm->dst_actor_ != nullptr ? comm->dst_actor_->get_pid() : -1) +
61            " mbox:" + std::to_string(comm->get_mailbox_id());
62   } else {
63     return "TestUnknownType()";
64   }
65 }
66 void ActivityTestanySimcall::serialize(std::stringstream& stream) const
67 {
68   stream << (short)mc::Transition::Type::TESTANY << ' ' << activities_.size() << ' ';
69   for (auto const* act : activities_) {
70     serialize_activity_test(act, stream);
71     stream << ' ';
72   }
73   stream << fun_call_;
74 }
75 std::string ActivityTestanySimcall::to_string() const
76 {
77   std::stringstream buffer("TestAny(");
78   for (auto const* act : activities_) {
79     buffer << to_string_activity_test(act);
80   }
81   return buffer.str();
82 }
83
84 void ActivityTestSimcall::serialize(std::stringstream& stream) const
85 {
86   serialize_activity_test(activity_, stream);
87   stream << ' ' << fun_call_;
88 }
89 std::string ActivityTestSimcall::to_string() const
90 {
91   return to_string_activity_test(activity_);
92 }
93 static void serialize_activity_wait(const activity::ActivityImpl* act, bool timeout, std::stringstream& stream)
94 {
95   if (const auto* comm = dynamic_cast<activity::CommImpl const*>(act)) {
96     stream << (short)mc::Transition::Type::COMM_WAIT << ' ';
97     stream << timeout << ' ' << comm->get_id();
98
99     stream << ' ' << (comm->src_actor_ != nullptr ? comm->src_actor_->get_pid() : -1);
100     stream << ' ' << (comm->dst_actor_ != nullptr ? comm->dst_actor_->get_pid() : -1);
101     stream << ' ' << comm->get_mailbox_id();
102   } else {
103     stream << (short)mc::Transition::Type::UNKNOWN;
104   }
105 }
106 static std::string to_string_activity_wait(const activity::ActivityImpl* act)
107 {
108   if (const auto* comm = dynamic_cast<activity::CommImpl const*>(act)) {
109     return "CommWait(comm_id:" + std::to_string(comm->get_id()) +
110            " src:" + std::to_string(comm->src_actor_ != nullptr ? comm->src_actor_->get_pid() : -1) +
111            " dst:" + std::to_string(comm->dst_actor_ != nullptr ? comm->dst_actor_->get_pid() : -1) +
112            " mbox:" + (comm->get_mailbox() == nullptr ? "-" : comm->get_mailbox()->get_name()) +
113            "(id:" + std::to_string(comm->get_mailbox_id()) + "))";
114   } else {
115     return "WaitUnknownType()";
116   }
117 }
118
119 void ActivityWaitSimcall::serialize(std::stringstream& stream) const
120 {
121   serialize_activity_wait(activity_, timeout_ > 0, stream);
122   stream << ' ' << fun_call_;
123 }
124 void ActivityWaitanySimcall::serialize(std::stringstream& stream) const
125 {
126   stream << (short)mc::Transition::Type::WAITANY << ' ' << activities_.size() << ' ';
127   for (auto const* act : activities_) {
128     serialize_activity_wait(act, timeout_ > 0, stream);
129     stream << ' ';
130   }
131   stream << fun_call_;
132 }
133 std::string ActivityWaitSimcall::to_string() const
134 {
135   return to_string_activity_wait(activity_);
136 }
137 std::string ActivityWaitanySimcall::to_string() const
138 {
139   std::stringstream buffer("WaitAny(");
140   for (auto const* act : activities_) {
141     buffer << to_string_activity_wait(act);
142   }
143   return buffer.str();
144 }
145 ActivityWaitanySimcall::ActivityWaitanySimcall(ActorImpl* actor, const std::vector<activity::ActivityImpl*>& activities,
146                                                double timeout, std::string fun_call)
147     : ResultingSimcall(actor, -1), activities_(activities), timeout_(timeout), fun_call_(fun_call)
148 {
149   // list all the activities that are ready
150   indexes_.clear();
151   for (unsigned i = 0; i < activities_.size(); i++)
152     if (activities_[i]->test(get_issuer()))
153       indexes_.push_back(i);
154 }
155
156 bool ActivityWaitSimcall::is_enabled()
157 {
158   // FIXME: if _sg_mc_timeout == 1 and if we have either a sender or receiver timeout, the transition is enabled
159   // because even if the communication is not ready, it can timeout and won't block.
160
161   return activity_->test(get_issuer());
162 }
163
164 bool ActivityWaitanySimcall::is_enabled()
165 {
166   // list all the activities that are ready
167   indexes_.clear();
168   for (unsigned i = 0; i < activities_.size(); i++)
169     if (activities_[i]->test(get_issuer()))
170       indexes_.push_back(i);
171
172   //  if (_sg_mc_timeout && timeout_)  FIXME: deal with the potential timeout of the WaitAny
173
174   // FIXME: even if the WaitAny has no timeout, some of the activities may still have one.
175   // we should iterate over the vector searching for them
176   return not indexes_.empty();
177 }
178
179 int ActivityWaitanySimcall::get_max_consider() const
180 {
181   int res = indexes_.size();
182   //  if (_sg_mc_timeout && timeout_)
183   //    res++;
184
185   return res;
186 }
187
188 void ActivityWaitanySimcall::prepare(int times_considered)
189 {
190   if (times_considered < static_cast<int>(indexes_.size()))
191     next_value_ = indexes_.at(times_considered);
192   else
193     next_value_ = -1;
194 }
195
196 void CommIsendSimcall::serialize(std::stringstream& stream) const
197 {
198   /* Note that the comm_ is 0 until after the execution of the simcall */
199   stream << (short)mc::Transition::Type::COMM_ASYNC_SEND << ' ';
200   stream << (comm_ ? comm_->get_id() : 0) << ' ' << mbox_->get_id() << ' ' << tag_;
201   XBT_DEBUG("SendObserver comm:%p mbox:%u tag:%d", comm_, mbox_->get_id(), tag_);
202   stream << ' ' << fun_call_;
203 }
204 std::string CommIsendSimcall::to_string() const
205 {
206   return "CommAsyncSend(comm_id: " + std::to_string(comm_->get_id()) + " mbox:" + std::to_string(mbox_->get_id()) +
207          " tag: " + std::to_string(tag_) + ")";
208 }
209
210 void CommIrecvSimcall::serialize(std::stringstream& stream) const
211 {
212   /* Note that the comm_ is 0 until after the execution of the simcall */
213   stream << (short)mc::Transition::Type::COMM_ASYNC_RECV << ' ';
214   stream << (comm_ ? comm_->get_id() : 0) << ' ' << mbox_->get_id() << ' ' << tag_;
215   XBT_DEBUG("RecvObserver comm:%p mbox:%u tag:%d", comm_, mbox_->get_id(), tag_);
216   stream << ' ' << fun_call_;
217 }
218 std::string CommIrecvSimcall::to_string() const
219 {
220   return "CommAsyncRecv(comm_id: " + std::to_string(comm_->get_id()) + " mbox:" + std::to_string(mbox_->get_id()) +
221          " tag: " + std::to_string(tag_) + ")";
222 }
223
224 } // namespace simgrid::kernel::actor