1 /* Copyright (c) 2019-2022. 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)
20 : ResultingSimcall(actor, -1), activities_(activities)
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);
29 int ActivityTestanySimcall::get_max_consider() const
31 return indexes_.size() + 1;
34 void ActivityTestanySimcall::prepare(int times_considered)
36 if (times_considered < static_cast<int>(indexes_.size()))
37 next_value_ = indexes_.at(times_considered);
41 static void serialize_activity_test(const activity::ActivityImpl* act, std::stringstream& stream)
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_;
51 stream << (short)mc::Transition::Type::UNKNOWN;
54 template <typename A> static std::string ptr_to_id(A* ptr)
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)));
61 static std::string to_string_activity_test(const activity::ActivityImpl* act)
63 if (auto* comm = dynamic_cast<activity::CommImpl const*>(act)) {
64 return std::string("CommTest(comm_id:") + ptr_to_id<activity::CommImpl const>(comm) +
65 " src:" + std::to_string(comm->src_actor_ != nullptr ? comm->src_actor_->get_pid() : -1) +
66 " dst:" + std::to_string(comm->dst_actor_ != nullptr ? comm->dst_actor_->get_pid() : -1) +
67 " mbox:" + std::to_string(comm->get_mailbox_id()) + " srcbuf:" + ptr_to_id<unsigned char>(comm->src_buff_) +
68 " dstbuf:" + ptr_to_id<unsigned char>(comm->dst_buff_) + " bufsize:" + std::to_string(comm->src_buff_size_);
70 return "TestUnknownType()";
73 void ActivityTestanySimcall::serialize(std::stringstream& stream) const
75 stream << (short)mc::Transition::Type::TESTANY << ' ' << activities_.size() << ' ';
76 for (auto const& act : activities_) {
77 serialize_activity_test(act, stream);
81 std::string ActivityTestanySimcall::to_string() const
83 std::stringstream buffer("TestAny(");
84 for (auto const& act : activities_) {
85 buffer << to_string_activity_test(act);
90 void ActivityTestSimcall::serialize(std::stringstream& stream) const
92 serialize_activity_test(activity_, stream);
94 std::string ActivityTestSimcall::to_string() const
96 return to_string_activity_test(activity_);
98 static void serialize_activity_wait(const activity::ActivityImpl* act, bool timeout, std::stringstream& stream)
100 if (auto* comm = dynamic_cast<activity::CommImpl const*>(act)) {
101 stream << (short)mc::Transition::Type::COMM_WAIT << ' ';
102 stream << timeout << ' ' << (uintptr_t)comm;
104 stream << ' ' << (comm->src_actor_ != nullptr ? comm->src_actor_->get_pid() : -1);
105 stream << ' ' << (comm->dst_actor_ != nullptr ? comm->dst_actor_->get_pid() : -1);
106 stream << ' ' << comm->get_mailbox_id();
107 stream << ' ' << (uintptr_t)comm->src_buff_ << ' ' << (uintptr_t)comm->dst_buff_ << ' ' << comm->src_buff_size_;
109 stream << (short)mc::Transition::Type::UNKNOWN;
112 static std::string to_string_activity_wait(const activity::ActivityImpl* act)
114 if (auto* comm = dynamic_cast<activity::CommImpl const*>(act)) {
115 return std::string("CommWait(comm_id:") + ptr_to_id<activity::CommImpl const>(comm) +
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:" + std::to_string(comm->get_mailbox_id()) + " srcbuf:" + ptr_to_id<unsigned char>(comm->src_buff_) +
119 " dstbuf:" + ptr_to_id<unsigned char>(comm->dst_buff_) + " 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);
130 void ActivityWaitanySimcall::serialize(std::stringstream& stream) const
132 stream << (short)mc::Transition::Type::WAITANY << ' ' << activities_.size() << ' ';
133 for (auto const& act : activities_) {
134 serialize_activity_wait(act, timeout_ > 0, stream);
138 std::string ActivityWaitSimcall::to_string() const
140 return to_string_activity_wait(activity_);
142 std::string ActivityWaitanySimcall::to_string() const
144 std::stringstream buffer("WaitAny(");
145 for (auto const& act : activities_) {
146 buffer << to_string_activity_wait(act);
150 ActivityWaitanySimcall::ActivityWaitanySimcall(ActorImpl* actor, const std::vector<activity::ActivityImpl*>& activities,
152 : ResultingSimcall(actor, -1), activities_(activities), timeout_(timeout)
154 // list all the activities that are ready
156 for (unsigned i = 0; i < activities_.size(); i++)
157 if (activities_[i]->test(get_issuer()))
158 indexes_.push_back(i);
161 bool ActivityWaitSimcall::is_enabled()
163 // FIXME: if _sg_mc_timeout == 1 and if we have either a sender or receiver timeout, the transition is enabled
164 // because even if the communication is not ready, it can timeout and won't block.
166 return activity_->test(get_issuer());
169 bool ActivityWaitanySimcall::is_enabled()
171 // list all the activities that are ready
173 for (unsigned i = 0; i < activities_.size(); i++)
174 if (activities_[i]->test(get_issuer()))
175 indexes_.push_back(i);
177 // if (_sg_mc_timeout && timeout_) FIXME: deal with the potential timeout of the WaitAny
179 // FIXME: even if the WaitAny has no timeout, some of the activities may still have one.
180 // we should iterate over the vector searching for them
181 return not indexes_.empty();
184 int ActivityWaitanySimcall::get_max_consider() const
186 int res = indexes_.size();
187 // if (_sg_mc_timeout && timeout_)
193 void ActivityWaitanySimcall::prepare(int times_considered)
195 if (times_considered < static_cast<int>(indexes_.size()))
196 next_value_ = indexes_.at(times_considered);
201 void CommIsendSimcall::serialize(std::stringstream& stream) const
203 stream << (short)mc::Transition::Type::COMM_ASYNC_SEND << ' ';
204 stream << (uintptr_t)comm_ << ' ' << mbox_->get_id() << ' ' << (uintptr_t)src_buff_ << ' ' << src_buff_size_ << ' '
206 XBT_DEBUG("SendObserver comm:%p mbox:%u buff:%p size:%zu tag:%d", comm_, mbox_->get_id(), src_buff_, src_buff_size_,
209 std::string CommIsendSimcall::to_string() const
211 return std::string("CommAsyncSend(comm_id: ") + std::to_string((uintptr_t)comm_) +
212 " mbox:" + std::to_string(mbox_->get_id()) + " srcbuf:" + ptr_to_id<unsigned char>(src_buff_) +
213 " bufsize:" + std::to_string(src_buff_size_) + " tag: " + std::to_string(tag_) + ")";
216 void CommIrecvSimcall::serialize(std::stringstream& stream) const
218 stream << (short)mc::Transition::Type::COMM_ASYNC_RECV << ' ';
219 stream << (uintptr_t)comm_ << ' ' << mbox_->get_id() << ' ' << (uintptr_t)dst_buff_ << ' ' << tag_;
220 XBT_DEBUG("RecvObserver comm:%p mbox:%u buff:%p tag:%d", comm_, mbox_->get_id(), dst_buff_, tag_);
222 std::string CommIrecvSimcall::to_string() const
224 return std::string("CommAsyncRecv(comm_id: ") + ptr_to_id<activity::CommImpl const>(comm_) +
225 " mbox:" + std::to_string(mbox_->get_id()) + " dstbuf:" + ptr_to_id<unsigned char>(dst_buff_) +
226 " tag: " + std::to_string(tag_) + ")";
229 } // namespace simgrid::kernel::actor