Logo AND Algorithmique Numérique Distribuée

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