Logo AND Algorithmique Numérique Distribuée

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