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();
51 stream << (short)mc::Transition::Type::UNKNOWN;
52 XBT_CRITICAL("Unknown transition in a test any. Bad things may happen");
55 static std::string to_string_activity_test(const activity::ActivityImpl* act)
57 if (const auto* comm = dynamic_cast<activity::CommImpl const*>(act)) {
58 return "CommTest(comm_id:" + std::to_string(comm->get_id()) +
59 " src:" + std::to_string(comm->src_actor_ != nullptr ? comm->src_actor_->get_pid() : -1) +
60 " dst:" + std::to_string(comm->dst_actor_ != nullptr ? comm->dst_actor_->get_pid() : -1) +
61 " mbox:" + std::to_string(comm->get_mailbox_id());
63 return "TestUnknownType()";
66 void ActivityTestanySimcall::serialize(std::stringstream& stream) const
68 stream << (short)mc::Transition::Type::TESTANY << ' ' << activities_.size() << ' ';
69 for (auto const* act : activities_) {
70 serialize_activity_test(act, stream);
75 std::string ActivityTestanySimcall::to_string() const
77 std::stringstream buffer("TestAny(");
78 for (auto const* act : activities_) {
79 buffer << to_string_activity_test(act);
84 void ActivityTestSimcall::serialize(std::stringstream& stream) const
86 serialize_activity_test(activity_, stream);
87 stream << ' ' << fun_call_;
89 std::string ActivityTestSimcall::to_string() const
91 return to_string_activity_test(activity_);
93 static void serialize_activity_wait(const activity::ActivityImpl* act, bool timeout, std::stringstream& stream)
95 if (const auto* comm = dynamic_cast<activity::CommImpl const*>(act)) {
96 stream << (short)mc::Transition::Type::COMM_WAIT << ' ';
97 stream << timeout << ' ' << comm->get_id();
99 stream << ' ' << (comm->src_actor_ != nullptr ? comm->src_actor_->get_pid() : -1);
100 stream << ' ' << (comm->dst_actor_ != nullptr ? comm->dst_actor_->get_pid() : -1);
101 stream << ' ' << comm->get_mailbox_id();
103 stream << (short)mc::Transition::Type::UNKNOWN;
106 static std::string to_string_activity_wait(const activity::ActivityImpl* act)
108 if (const auto* comm = dynamic_cast<activity::CommImpl const*>(act)) {
109 return "CommWait(comm_id:" + std::to_string(comm->get_id()) +
110 " src:" + std::to_string(comm->src_actor_ != nullptr ? comm->src_actor_->get_pid() : -1) +
111 " dst:" + std::to_string(comm->dst_actor_ != nullptr ? comm->dst_actor_->get_pid() : -1) +
112 " mbox:" + (comm->get_mailbox() == nullptr ? "-" : comm->get_mailbox()->get_name()) +
113 "(id:" + std::to_string(comm->get_mailbox_id()) + "))";
115 return "WaitUnknownType()";
119 void ActivityWaitSimcall::serialize(std::stringstream& stream) const
121 serialize_activity_wait(activity_, timeout_ > 0, stream);
122 stream << ' ' << fun_call_;
124 void ActivityWaitanySimcall::serialize(std::stringstream& stream) const
126 stream << (short)mc::Transition::Type::WAITANY << ' ' << activities_.size() << ' ';
127 for (auto const* act : activities_) {
128 serialize_activity_wait(act, timeout_ > 0, stream);
133 std::string ActivityWaitSimcall::to_string() const
135 return to_string_activity_wait(activity_);
137 std::string ActivityWaitanySimcall::to_string() const
139 std::stringstream buffer("WaitAny(");
140 for (auto const* act : activities_) {
141 buffer << to_string_activity_wait(act);
145 ActivityWaitanySimcall::ActivityWaitanySimcall(ActorImpl* actor, const std::vector<activity::ActivityImpl*>& activities,
146 double timeout, std::string fun_call)
147 : ResultingSimcall(actor, -1), activities_(activities), timeout_(timeout), fun_call_(fun_call)
149 // list all the activities that are ready
151 for (unsigned i = 0; i < activities_.size(); i++)
152 if (activities_[i]->test(get_issuer()))
153 indexes_.push_back(i);
156 bool ActivityWaitSimcall::is_enabled()
158 // FIXME: if _sg_mc_timeout == 1 and if we have either a sender or receiver timeout, the transition is enabled
159 // because even if the communication is not ready, it can timeout and won't block.
161 return activity_->test(get_issuer());
164 bool ActivityWaitanySimcall::is_enabled()
166 // list all the activities that are ready
168 for (unsigned i = 0; i < activities_.size(); i++)
169 if (activities_[i]->test(get_issuer()))
170 indexes_.push_back(i);
172 // if (_sg_mc_timeout && timeout_) FIXME: deal with the potential timeout of the WaitAny
174 // FIXME: even if the WaitAny has no timeout, some of the activities may still have one.
175 // we should iterate over the vector searching for them
176 return not indexes_.empty();
179 int ActivityWaitanySimcall::get_max_consider() const
181 int res = indexes_.size();
182 // if (_sg_mc_timeout && timeout_)
188 void ActivityWaitanySimcall::prepare(int times_considered)
190 if (times_considered < static_cast<int>(indexes_.size()))
191 next_value_ = indexes_.at(times_considered);
196 void CommIsendSimcall::serialize(std::stringstream& stream) const
198 /* Note that the comm_ is 0 until after the execution of the simcall */
199 stream << (short)mc::Transition::Type::COMM_ASYNC_SEND << ' ';
200 stream << (comm_ ? comm_->get_id() : 0) << ' ' << mbox_->get_id() << ' ' << tag_;
201 XBT_DEBUG("SendObserver comm:%p mbox:%u tag:%d", comm_, mbox_->get_id(), tag_);
202 stream << ' ' << fun_call_;
204 std::string CommIsendSimcall::to_string() const
206 return "CommAsyncSend(comm_id: " + std::to_string(comm_->get_id()) + " mbox:" + std::to_string(mbox_->get_id()) +
207 " tag: " + std::to_string(tag_) + ")";
210 void CommIrecvSimcall::serialize(std::stringstream& stream) const
212 /* Note that the comm_ is 0 until after the execution of the simcall */
213 stream << (short)mc::Transition::Type::COMM_ASYNC_RECV << ' ';
214 stream << (comm_ ? comm_->get_id() : 0) << ' ' << mbox_->get_id() << ' ' << tag_;
215 XBT_DEBUG("RecvObserver comm:%p mbox:%u tag:%d", comm_, mbox_->get_id(), tag_);
216 stream << ' ' << fun_call_;
218 std::string CommIrecvSimcall::to_string() const
220 return "CommAsyncRecv(comm_id: " + std::to_string(comm_->get_id()) + " mbox:" + std::to_string(mbox_->get_id()) +
221 " tag: " + std::to_string(tag_) + ")";
224 } // namespace simgrid::kernel::actor