1 /* Copyright (c) 2007-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/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/mc/mc_replay.hpp"
16 #include "src/surf/HostImpl.hpp"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_cpu, kernel, "Kernel cpu-related synchronization");
26 piface_ = new s4u::Exec(this);
27 actor::ActorImpl* self = actor::ActorImpl::self();
30 self->activities_.emplace_back(this);
34 ExecImpl& ExecImpl::set_host(s4u::Host* host)
36 hosts_.assign(1, host);
40 ExecImpl& ExecImpl::set_hosts(const std::vector<s4u::Host*>& hosts)
46 ExecImpl& ExecImpl::set_timeout(double timeout)
48 if (timeout >= 0 && not MC_is_active() && not MC_record_replay_is_active()) {
49 timeout_detector_.reset(hosts_.front()->get_cpu()->sleep(timeout));
50 timeout_detector_->set_activity(this);
55 ExecImpl& ExecImpl::set_flops_amount(double flops_amount)
57 flops_amounts_.assign(1, flops_amount);
61 ExecImpl& ExecImpl::set_flops_amounts(const std::vector<double>& flops_amounts)
63 flops_amounts_ = flops_amounts;
67 ExecImpl& ExecImpl::set_bytes_amounts(const std::vector<double>& bytes_amounts)
69 bytes_amounts_ = bytes_amounts;
74 ExecImpl* ExecImpl::start()
76 set_state(State::RUNNING);
77 if (not MC_is_active() && not MC_record_replay_is_active()) {
78 if (hosts_.size() == 1) {
79 surf_action_ = hosts_.front()->get_cpu()->execution_start(flops_amounts_.front(), bound_);
80 surf_action_->set_sharing_penalty(sharing_penalty_);
81 surf_action_->set_category(get_tracing_category());
83 // get the model from first host since we have only 1 by now
84 auto host_model = hosts_.front()->get_netpoint()->get_englobing_zone()->get_host_model();
85 surf_action_ = host_model->execute_parallel(hosts_, flops_amounts_.data(), bytes_amounts_.data(), -1);
87 surf_action_->set_activity(this);
88 set_start_time(surf_action_->get_start_time());
91 XBT_DEBUG("Create execute synchro %p: %s", this, get_cname());
95 double ExecImpl::get_remaining() const
97 if (get_state() == State::WAITING || get_state() == State::FAILED)
98 return flops_amounts_.front();
99 return ActivityImpl::get_remaining();
102 double ExecImpl::get_seq_remaining_ratio()
104 if (get_state() == State::WAITING)
106 return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains() / surf_action_->get_cost();
109 double ExecImpl::get_par_remaining_ratio()
111 // parallel task: their remain is already between 0 and 1
112 if (get_state() == State::WAITING)
114 return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains();
117 ExecImpl& ExecImpl::set_bound(double bound)
123 ExecImpl& ExecImpl::set_sharing_penalty(double sharing_penalty)
125 sharing_penalty_ = sharing_penalty;
129 ExecImpl& ExecImpl::update_sharing_penalty(double sharing_penalty)
131 sharing_penalty_ = sharing_penalty;
132 surf_action_->set_sharing_penalty(sharing_penalty);
136 void ExecImpl::post()
138 xbt_assert(surf_action_ != nullptr);
139 if (std::any_of(hosts_.begin(), hosts_.end(), [](const s4u::Host* host) { return not host->is_on(); })) {
140 /* If one of the hosts running the synchro failed, notice it. This way, the asking
141 * process can be killed if it runs on that host itself */
142 set_state(State::FAILED);
143 } else if (surf_action_->get_state() == resource::Action::State::FAILED) {
144 /* If all the hosts are running the synchro didn't fail, then the synchro was canceled */
145 set_state(State::CANCELED);
146 } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
147 if (surf_action_->get_remains() > 0.0) {
148 surf_action_->set_state(resource::Action::State::FAILED);
149 set_state(State::TIMEOUT);
151 set_state(State::DONE);
154 set_state(State::DONE);
158 timeout_detector_.reset();
159 if (get_actor() != nullptr) {
160 get_actor()->activities_.remove(this);
162 if (get_state() != State::FAILED && cb_id_ >= 0)
163 s4u::Host::on_state_change.disconnect(cb_id_);
164 /* Answer all simcalls associated with the synchro */
168 void ExecImpl::set_exception(actor::ActorImpl* issuer)
170 switch (get_state()) {
172 static_cast<s4u::Exec*>(get_iface())->complete(s4u::Activity::State::FAILED);
173 if (issuer->get_host()->is_on())
174 issuer->exception_ = std::make_exception_ptr(HostFailureException(XBT_THROW_POINT, "Host failed"));
175 else /* else, the actor will be killed with no possibility to survive */
176 issuer->context_->set_wannadie();
179 case State::CANCELED:
180 issuer->exception_ = std::make_exception_ptr(CancelException(XBT_THROW_POINT, "Execution Canceled"));
184 issuer->exception_ = std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Timeouted"));
188 xbt_assert(get_state() == State::DONE, "Internal error in ExecImpl::finish(): unexpected synchro state %s",
192 void ExecImpl::finish()
194 XBT_DEBUG("ExecImpl::finish() in state %s", get_state_str());
195 while (not simcalls_.empty()) {
196 smx_simcall_t simcall = simcalls_.front();
197 simcalls_.pop_front();
199 if (simcall->call_ == simix::Simcall::NONE) // FIXME: maybe a better way to handle this case
200 continue; // if process handling comm is killed
202 handle_activity_waitany(simcall);
204 set_exception(simcall->issuer_);
206 simcall->issuer_->waiting_synchro_ = nullptr;
207 /* Fail the process if the host is down */
208 if (simcall->issuer_->get_host()->is_on())
209 simcall->issuer_->simcall_answer();
211 simcall->issuer_->context_->set_wannadie();
215 void ExecImpl::reset()
218 bytes_amounts_.clear();
219 flops_amounts_.clear();
220 set_start_time(-1.0);
223 ActivityImpl* ExecImpl::migrate(s4u::Host* to)
225 if (not MC_is_active() && not MC_record_replay_is_active()) {
226 resource::Action* old_action = this->surf_action_;
227 resource::Action* new_action = to->get_cpu()->execution_start(old_action->get_cost(), old_action->get_user_bound());
228 new_action->set_remains(old_action->get_remains());
229 new_action->set_activity(this);
230 new_action->set_sharing_penalty(old_action->get_sharing_penalty());
231 new_action->set_user_bound(old_action->get_user_bound());
233 old_action->set_activity(nullptr);
234 old_action->cancel();
236 this->surf_action_ = new_action;
239 on_migration(*this, to);
246 xbt::signal<void(ExecImpl const&, s4u::Host*)> ExecImpl::on_migration;
248 } // namespace activity
249 } // namespace kernel
250 } // namespace simgrid