Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
More verbose error and debug messages
[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     stream << ' ' << (uintptr_t)comm->src_buff_ << ' ' << (uintptr_t)comm->dst_buff_ << ' ' << comm->src_buff_size_;
51   } else {
52     stream << (short)mc::Transition::Type::UNKNOWN;
53     XBT_CRITICAL("Unknown transition in a test any. Bad things may happen");
54   }
55 }
56 static std::string to_string_activity_test(const activity::ActivityImpl* act)
57 {
58   if (const auto* comm = dynamic_cast<activity::CommImpl const*>(act)) {
59     const std::string src_buff_id = ptr_to_id<unsigned char>(comm->src_buff_);
60     const std::string dst_buff_id = ptr_to_id<unsigned char>(comm->dst_buff_);
61     return "CommTest(comm_id:" + std::to_string(comm->get_id()) +
62            " src:" + std::to_string(comm->src_actor_ != nullptr ? comm->src_actor_->get_pid() : -1) +
63            " dst:" + std::to_string(comm->dst_actor_ != nullptr ? comm->dst_actor_->get_pid() : -1) +
64            " mbox:" + std::to_string(comm->get_mailbox_id()) + " srcbuf:" + src_buff_id + " dstbuf:" + dst_buff_id +
65            " bufsize:" + std::to_string(comm->src_buff_size_);
66   } else {
67     return "TestUnknownType()";
68   }
69 }
70 void ActivityTestanySimcall::serialize(std::stringstream& stream) const
71 {
72   stream << (short)mc::Transition::Type::TESTANY << ' ' << activities_.size() << ' ';
73   for (auto const* act : activities_) {
74     serialize_activity_test(act, stream);
75     stream << ' ';
76   }
77   stream << fun_call_;
78 }
79 std::string ActivityTestanySimcall::to_string() const
80 {
81   std::stringstream buffer("TestAny(");
82   for (auto const* act : activities_) {
83     buffer << to_string_activity_test(act);
84   }
85   return buffer.str();
86 }
87
88 void ActivityTestSimcall::serialize(std::stringstream& stream) const
89 {
90   serialize_activity_test(activity_, stream);
91   stream << ' ' << fun_call_;
92 }
93 std::string ActivityTestSimcall::to_string() const
94 {
95   return to_string_activity_test(activity_);
96 }
97 static void serialize_activity_wait(const activity::ActivityImpl* act, bool timeout, std::stringstream& stream)
98 {
99   if (const auto* comm = dynamic_cast<activity::CommImpl const*>(act)) {
100     stream << (short)mc::Transition::Type::COMM_WAIT << ' ';
101     stream << timeout << ' ' << comm->get_id();
102
103     stream << ' ' << (comm->src_actor_ != nullptr ? comm->src_actor_->get_pid() : -1);
104     stream << ' ' << (comm->dst_actor_ != nullptr ? comm->dst_actor_->get_pid() : -1);
105     stream << ' ' << comm->get_mailbox_id();
106     stream << ' ' << (uintptr_t)comm->src_buff_ << ' ' << (uintptr_t)comm->dst_buff_ << ' ' << comm->src_buff_size_;
107   } else {
108     stream << (short)mc::Transition::Type::UNKNOWN;
109   }
110 }
111 static std::string to_string_activity_wait(const activity::ActivityImpl* act)
112 {
113   if (const auto* comm = dynamic_cast<activity::CommImpl const*>(act)) {
114     const std::string src_buff_id = ptr_to_id<unsigned char>(comm->src_buff_);
115     const std::string dst_buff_id = ptr_to_id<unsigned char>(comm->dst_buff_);
116     return "CommWait(comm_id:" + std::to_string(comm->get_id()) +
117            " src:" + std::to_string(comm->src_actor_ != nullptr ? comm->src_actor_->get_pid() : -1) +
118            " dst:" + std::to_string(comm->dst_actor_ != nullptr ? comm->dst_actor_->get_pid() : -1) +
119            " mbox:" + (comm->get_mailbox() == nullptr ? "-" : comm->get_mailbox()->get_name()) +
120            "(id:" + std::to_string(comm->get_mailbox_id()) + ") srcbuf:" + src_buff_id + " dstbuf:" + dst_buff_id +
121            " bufsize:" + std::to_string(comm->src_buff_size_) + ")";
122   } else {
123     return "WaitUnknownType()";
124   }
125 }
126
127 void ActivityWaitSimcall::serialize(std::stringstream& stream) const
128 {
129   serialize_activity_wait(activity_, timeout_ > 0, stream);
130   stream << ' ' << fun_call_;
131 }
132 void ActivityWaitanySimcall::serialize(std::stringstream& stream) const
133 {
134   stream << (short)mc::Transition::Type::WAITANY << ' ' << activities_.size() << ' ';
135   for (auto const* act : activities_) {
136     serialize_activity_wait(act, timeout_ > 0, stream);
137     stream << ' ';
138   }
139   stream << fun_call_;
140 }
141 std::string ActivityWaitSimcall::to_string() const
142 {
143   return to_string_activity_wait(activity_);
144 }
145 std::string ActivityWaitanySimcall::to_string() const
146 {
147   std::stringstream buffer("WaitAny(");
148   for (auto const* act : activities_) {
149     buffer << to_string_activity_wait(act);
150   }
151   return buffer.str();
152 }
153 ActivityWaitanySimcall::ActivityWaitanySimcall(ActorImpl* actor, const std::vector<activity::ActivityImpl*>& activities,
154                                                double timeout, std::string fun_call)
155     : ResultingSimcall(actor, -1), activities_(activities), timeout_(timeout), fun_call_(fun_call)
156 {
157   // list all the activities that are ready
158   indexes_.clear();
159   for (unsigned i = 0; i < activities_.size(); i++)
160     if (activities_[i]->test(get_issuer()))
161       indexes_.push_back(i);
162 }
163
164 bool ActivityWaitSimcall::is_enabled()
165 {
166   // FIXME: if _sg_mc_timeout == 1 and if we have either a sender or receiver timeout, the transition is enabled
167   // because even if the communication is not ready, it can timeout and won't block.
168
169   return activity_->test(get_issuer());
170 }
171
172 bool ActivityWaitanySimcall::is_enabled()
173 {
174   // list all the activities that are ready
175   indexes_.clear();
176   for (unsigned i = 0; i < activities_.size(); i++)
177     if (activities_[i]->test(get_issuer()))
178       indexes_.push_back(i);
179
180   //  if (_sg_mc_timeout && timeout_)  FIXME: deal with the potential timeout of the WaitAny
181
182   // FIXME: even if the WaitAny has no timeout, some of the activities may still have one.
183   // we should iterate over the vector searching for them
184   return not indexes_.empty();
185 }
186
187 int ActivityWaitanySimcall::get_max_consider() const
188 {
189   int res = indexes_.size();
190   //  if (_sg_mc_timeout && timeout_)
191   //    res++;
192
193   return res;
194 }
195
196 void ActivityWaitanySimcall::prepare(int times_considered)
197 {
198   if (times_considered < static_cast<int>(indexes_.size()))
199     next_value_ = indexes_.at(times_considered);
200   else
201     next_value_ = -1;
202 }
203
204 void CommIsendSimcall::serialize(std::stringstream& stream) const
205 {
206   /* Note that the comm_ is 0 until after the execution of the simcall */
207   stream << (short)mc::Transition::Type::COMM_ASYNC_SEND << ' ';
208   stream << (comm_ ? comm_->get_id() : 0) << ' ' << mbox_->get_id() << ' ' << (uintptr_t)src_buff_ << ' '
209          << src_buff_size_ << ' ' << tag_;
210   XBT_DEBUG("SendObserver comm:%p mbox:%u buff:%p size:%zu tag:%d", comm_, mbox_->get_id(), src_buff_, src_buff_size_,
211             tag_);
212   stream << ' ' << fun_call_;
213 }
214 std::string CommIsendSimcall::to_string() const
215 {
216   return "CommAsyncSend(comm_id: " + std::to_string(comm_->get_id()) + " mbox:" + std::to_string(mbox_->get_id()) +
217          " srcbuf:" + ptr_to_id<unsigned char>(src_buff_) + " bufsize:" + std::to_string(src_buff_size_) +
218          " tag: " + std::to_string(tag_) + ")";
219 }
220
221 void CommIrecvSimcall::serialize(std::stringstream& stream) const
222 {
223   /* Note that the comm_ is 0 until after the execution of the simcall */
224   stream << (short)mc::Transition::Type::COMM_ASYNC_RECV << ' ';
225   stream << (comm_ ? comm_->get_id() : 0) << ' ' << mbox_->get_id() << ' ' << (uintptr_t)dst_buff_ << ' ' << tag_;
226   XBT_DEBUG("RecvObserver comm:%p mbox:%u buff:%p tag:%d", comm_, mbox_->get_id(), dst_buff_, tag_);
227   stream << ' ' << fun_call_;
228 }
229 std::string CommIrecvSimcall::to_string() const
230 {
231   return "CommAsyncRecv(comm_id: " + std::to_string(comm_->get_id()) + " mbox:" + std::to_string(mbox_->get_id()) +
232          " dstbuf:" + ptr_to_id<unsigned char>(dst_buff_) + " tag: " + std::to_string(tag_) + ")";
233 }
234
235 } // namespace simgrid::kernel::actor