1 /* Copyright (c) 2019-2023. The SimGrid Team. All rights reserved. */
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. */
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"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(obs_comm, mc_observer, "Logging specific to the Communication simcalls observation");
17 namespace simgrid::kernel::actor {
19 ActivityTestanySimcall::ActivityTestanySimcall(ActorImpl* actor, const std::vector<activity::ActivityImpl*>& activities,
21 : ResultingSimcall(actor, -1), activities_(activities), fun_call_(fun_call)
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);
30 int ActivityTestanySimcall::get_max_consider() const
32 return indexes_.size() + 1;
35 void ActivityTestanySimcall::prepare(int times_considered)
37 if (times_considered < static_cast<int>(indexes_.size()))
38 next_value_ = indexes_.at(times_considered);
42 static void serialize_activity_test(const activity::ActivityImpl* act, std::stringstream& stream)
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_;
52 stream << (short)mc::Transition::Type::UNKNOWN;
55 static std::string to_string_activity_test(const activity::ActivityImpl* act)
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_);
66 return "TestUnknownType()";
69 void ActivityTestanySimcall::serialize(std::stringstream& stream) const
71 stream << (short)mc::Transition::Type::TESTANY << ' ' << activities_.size() << ' ';
72 for (auto const* act : activities_) {
73 serialize_activity_test(act, stream);
78 std::string ActivityTestanySimcall::to_string() const
80 std::stringstream buffer("TestAny(");
81 for (auto const* act : activities_) {
82 buffer << to_string_activity_test(act);
87 void ActivityTestSimcall::serialize(std::stringstream& stream) const
89 serialize_activity_test(activity_, stream);
90 stream << ' ' << fun_call_;
92 std::string ActivityTestSimcall::to_string() const
94 return to_string_activity_test(activity_);
96 static void serialize_activity_wait(const activity::ActivityImpl* act, bool timeout, std::stringstream& stream)
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();
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_;
107 stream << (short)mc::Transition::Type::UNKNOWN;
110 static std::string to_string_activity_wait(const activity::ActivityImpl* act)
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_) + ")";
122 return "WaitUnknownType()";
126 void ActivityWaitSimcall::serialize(std::stringstream& stream) const
128 serialize_activity_wait(activity_, timeout_ > 0, stream);
129 stream << ' ' << fun_call_;
131 void ActivityWaitanySimcall::serialize(std::stringstream& stream) const
133 stream << (short)mc::Transition::Type::WAITANY << ' ' << activities_.size() << ' ';
134 for (auto const* act : activities_) {
135 serialize_activity_wait(act, timeout_ > 0, stream);
140 std::string ActivityWaitSimcall::to_string() const
142 return to_string_activity_wait(activity_);
144 std::string ActivityWaitanySimcall::to_string() const
146 std::stringstream buffer("WaitAny(");
147 for (auto const* act : activities_) {
148 buffer << to_string_activity_wait(act);
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)
156 // list all the activities that are ready
158 for (unsigned i = 0; i < activities_.size(); i++)
159 if (activities_[i]->test(get_issuer()))
160 indexes_.push_back(i);
163 bool ActivityWaitSimcall::is_enabled()
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.
168 return activity_->test(get_issuer());
171 bool ActivityWaitanySimcall::is_enabled()
173 // list all the activities that are ready
175 for (unsigned i = 0; i < activities_.size(); i++)
176 if (activities_[i]->test(get_issuer()))
177 indexes_.push_back(i);
179 // if (_sg_mc_timeout && timeout_) FIXME: deal with the potential timeout of the WaitAny
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();
186 int ActivityWaitanySimcall::get_max_consider() const
188 int res = indexes_.size();
189 // if (_sg_mc_timeout && timeout_)
195 void ActivityWaitanySimcall::prepare(int times_considered)
197 if (times_considered < static_cast<int>(indexes_.size()))
198 next_value_ = indexes_.at(times_considered);
203 void CommIsendSimcall::serialize(std::stringstream& stream) const
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_,
211 stream << ' ' << fun_call_;
213 std::string CommIsendSimcall::to_string() const
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_) + ")";
220 void CommIrecvSimcall::serialize(std::stringstream& stream) const
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_;
228 std::string CommIrecvSimcall::to_string() const
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_) + ")";
234 } // namespace simgrid::kernel::actor