1 /* Copyright (c) 2007-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/Exception.hpp>
7 #include <simgrid/kernel/routing/NetPoint.hpp>
8 #include <simgrid/modelchecker.h>
9 #include <simgrid/s4u/Engine.hpp>
11 #include "src/kernel/activity/ExecImpl.hpp"
12 #include "src/kernel/actor/ActorImpl.hpp"
13 #include "src/kernel/actor/SimcallObserver.hpp"
14 #include "src/kernel/resource/CpuImpl.hpp"
15 #include "src/kernel/resource/HostImpl.hpp"
16 #include "src/mc/mc_replay.hpp"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_cpu, kernel, "Kernel cpu-related synchronization");
20 namespace simgrid::kernel::activity {
24 piface_ = new s4u::Exec(this);
25 actor::ActorImpl* self = actor::ActorImpl::self();
28 self->activities_.insert(this);
32 ExecImpl& ExecImpl::set_host(s4u::Host* host)
34 ActivityImpl::set_hosts({host});
38 ExecImpl& ExecImpl::set_hosts(const std::vector<s4u::Host*>& hosts)
40 ActivityImpl::set_hosts(hosts);
44 ExecImpl& ExecImpl::set_flops_amount(double flops_amount)
46 flops_amounts_.assign(1, flops_amount);
50 ExecImpl& ExecImpl::set_flops_amounts(const std::vector<double>& flops_amounts)
52 flops_amounts_ = flops_amounts;
56 ExecImpl& ExecImpl::set_bytes_amounts(const std::vector<double>& bytes_amounts)
58 bytes_amounts_ = bytes_amounts;
62 ExecImpl& ExecImpl::set_thread_count(int thread_count)
64 thread_count_ = thread_count;
69 ExecImpl* ExecImpl::start()
71 set_state(State::RUNNING);
72 if (not MC_is_active() && not MC_record_replay_is_active()) {
73 if (get_hosts().size() == 1) {
74 xbt_assert(not flops_amounts_.empty(), "Cannot start Exec: no flops_amount defined.");
75 if (thread_count_ == 1) {
76 model_action_ = get_host()->get_cpu()->execution_start(flops_amounts_.front(), bound_);
77 model_action_->set_sharing_penalty(sharing_penalty_);
79 auto host_model = get_host()->get_netpoint()->get_englobing_zone()->get_host_model();
80 model_action_ = host_model->execute_thread(get_host(), flops_amounts_.front(), thread_count_);
82 model_action_->set_category(get_tracing_category());
84 // get the model from first host since we have only 1 by now
85 auto host_model = get_host()->get_netpoint()->get_englobing_zone()->get_host_model();
86 model_action_ = host_model->execute_parallel(get_hosts(), flops_amounts_.data(), bytes_amounts_.data(), -1);
88 model_action_->set_activity(this);
89 set_start_time(model_action_->get_start_time());
92 XBT_DEBUG("Create execute synchro %p: %s", this, get_cname());
96 double ExecImpl::get_remaining() const
98 if (get_state() == State::WAITING || get_state() == State::FAILED)
99 return flops_amounts_.front();
100 return ActivityImpl::get_remaining();
103 double ExecImpl::get_seq_remaining_ratio()
105 if (get_state() == State::WAITING)
107 return (model_action_ == nullptr) ? 0 : model_action_->get_remains() / model_action_->get_cost();
110 double ExecImpl::get_par_remaining_ratio()
112 // parallel task: their remain is already between 0 and 1
113 if (get_state() == State::WAITING)
115 return (model_action_ == nullptr) ? 0 : model_action_->get_remains();
118 ExecImpl& ExecImpl::set_bound(double bound)
124 ExecImpl& ExecImpl::set_sharing_penalty(double sharing_penalty)
126 sharing_penalty_ = sharing_penalty;
130 ExecImpl& ExecImpl::update_sharing_penalty(double sharing_penalty)
132 sharing_penalty_ = sharing_penalty;
133 model_action_->set_sharing_penalty(sharing_penalty);
137 void ExecImpl::set_exception(actor::ActorImpl* issuer)
139 switch (get_state()) {
141 static_cast<s4u::Exec*>(get_iface())->complete(s4u::Activity::State::FAILED);
142 if (issuer->get_host()->is_on())
143 issuer->exception_ = std::make_exception_ptr(HostFailureException(XBT_THROW_POINT, "Host failed"));
144 else /* else, the actor will be killed with no possibility to survive */
145 issuer->set_wannadie();
148 case State::CANCELED:
149 issuer->exception_ = std::make_exception_ptr(CancelException(XBT_THROW_POINT, "Execution Canceled"));
153 issuer->exception_ = std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Timeouted"));
157 xbt_assert(get_state() == State::DONE, "Internal error in ExecImpl::finish(): unexpected synchro state %s",
161 void ExecImpl::finish()
163 XBT_DEBUG("ExecImpl::finish() in state %s", get_state_str());
164 if (model_action_ != nullptr) {
165 if (auto const& hosts = get_hosts();
166 std::any_of(hosts.begin(), hosts.end(), [](const s4u::Host* host) { return not host->is_on(); })) {
167 /* If one of the hosts running the synchro failed, notice it. This way, the asking
168 * process can be killed if it runs on that host itself */
169 set_state(State::FAILED);
170 } else if (model_action_->get_state() == resource::Action::State::FAILED) {
171 /* If all the hosts are running the synchro didn't fail, then the synchro was canceled */
172 set_state(State::CANCELED);
174 set_state(State::DONE);
180 if (get_actor() != nullptr)
181 get_actor()->activities_.erase(this);
183 while (not simcalls_.empty()) {
184 actor::Simcall* simcall = simcalls_.front();
185 simcalls_.pop_front();
187 if (simcall->call_ == actor::Simcall::Type::NONE) // FIXME: maybe a better way to handle this case
188 continue; // if process handling comm is killed
190 handle_activity_waitany(simcall);
192 set_exception(simcall->issuer_);
194 simcall->issuer_->waiting_synchro_ = nullptr;
195 /* Fail the process if the host is down */
196 if (simcall->issuer_->get_host()->is_on())
197 simcall->issuer_->simcall_answer();
199 simcall->issuer_->set_wannadie();
203 void ExecImpl::reset()
206 set_start_time(-1.0);
209 ActivityImpl* ExecImpl::migrate(s4u::Host* to)
211 if (not MC_is_active() && not MC_record_replay_is_active()) {
212 resource::Action* old_action = this->model_action_;
213 resource::Action* new_action = to->get_cpu()->execution_start(old_action->get_cost(), old_action->get_user_bound());
214 new_action->set_remains(old_action->get_remains());
215 new_action->set_activity(this);
216 new_action->set_sharing_penalty(old_action->get_sharing_penalty());
217 new_action->set_user_bound(old_action->get_user_bound());
219 old_action->set_activity(nullptr);
220 old_action->cancel();
222 this->model_action_ = new_action;
225 on_migration(*this, to);
232 xbt::signal<void(ExecImpl const&, s4u::Host*)> ExecImpl::on_migration;
234 } // namespace simgrid::kernel::activity