1 /* Copyright (c) 2007-2021. 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 "src/kernel/activity/ExecImpl.hpp"
7 #include "simgrid/Exception.hpp"
8 #include "simgrid/kernel/routing/NetPoint.hpp"
9 #include "simgrid/modelchecker.h"
10 #include "simgrid/s4u/Engine.hpp"
11 #include "simgrid/s4u/Exec.hpp"
12 #include "src/kernel/actor/SimcallObserver.hpp"
13 #include "src/mc/mc_replay.hpp"
14 #include "src/surf/HostImpl.hpp"
15 #include "src/surf/cpu_interface.hpp"
16 #include "src/surf/surf_interface.hpp"
18 #include "simgrid/s4u/Host.hpp"
20 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_process);
28 piface_ = new s4u::Exec(this);
29 actor::ActorImpl* self = actor::ActorImpl::self();
32 self->activities_.emplace_back(this);
36 ExecImpl& ExecImpl::set_host(s4u::Host* host)
38 hosts_.assign(1, host);
42 ExecImpl& ExecImpl::set_hosts(const std::vector<s4u::Host*>& hosts)
48 ExecImpl& ExecImpl::set_timeout(double timeout)
50 if (timeout >= 0 && not MC_is_active() && not MC_record_replay_is_active()) {
51 timeout_detector_.reset(hosts_.front()->get_cpu()->sleep(timeout));
52 timeout_detector_->set_activity(this);
57 ExecImpl& ExecImpl::set_flops_amount(double flops_amount)
59 flops_amounts_.assign(1, flops_amount);
63 ExecImpl& ExecImpl::set_flops_amounts(const std::vector<double>& flops_amounts)
65 flops_amounts_ = flops_amounts;
69 ExecImpl& ExecImpl::set_bytes_amounts(const std::vector<double>& bytes_amounts)
71 bytes_amounts_ = bytes_amounts;
76 ExecImpl* ExecImpl::start()
78 state_ = State::RUNNING;
79 if (not MC_is_active() && not MC_record_replay_is_active()) {
80 if (hosts_.size() == 1) {
81 surf_action_ = hosts_.front()->get_cpu()->execution_start(flops_amounts_.front());
82 surf_action_->set_sharing_penalty(sharing_penalty_);
83 surf_action_->set_category(get_tracing_category());
86 surf_action_->set_bound(bound_);
87 surf_action_->set_user_bound(bound_);
90 // FIXME[donassolo]: verify if all hosts belongs to the same netZone?
91 auto host_model = hosts_.front()->get_netpoint()->get_englobing_zone()->get_host_model();
92 surf_action_ = host_model->execute_parallel(hosts_, flops_amounts_.data(), bytes_amounts_.data(), -1);
94 surf_action_->set_activity(this);
95 start_time_ = surf_action_->get_start_time();
98 XBT_DEBUG("Create execute synchro %p: %s", this, get_cname());
102 double ExecImpl::get_seq_remaining_ratio()
104 return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains() / surf_action_->get_cost();
107 double ExecImpl::get_par_remaining_ratio()
109 // parallel task: their remain is already between 0 and 1
110 return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains();
113 ExecImpl& ExecImpl::set_bound(double bound)
119 ExecImpl& ExecImpl::set_sharing_penalty(double sharing_penalty)
121 sharing_penalty_ = sharing_penalty;
125 void ExecImpl::post()
127 xbt_assert(surf_action_ != nullptr);
128 if (std::any_of(hosts_.begin(), hosts_.end(), [](const s4u::Host* host) { return not host->is_on(); })) {
129 /* If one of the hosts running the synchro failed, notice it. This way, the asking
130 * process can be killed if it runs on that host itself */
131 state_ = State::FAILED;
132 } else if (surf_action_->get_state() == resource::Action::State::FAILED) {
133 /* If all the hosts are running the synchro didn't fail, then the synchro was canceled */
134 state_ = State::CANCELED;
135 } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
136 if (surf_action_->get_remains() > 0.0) {
137 surf_action_->set_state(resource::Action::State::FAILED);
138 state_ = State::TIMEOUT;
140 state_ = State::DONE;
143 state_ = State::DONE;
146 finish_time_ = surf_action_->get_finish_time();
149 timeout_detector_.reset();
151 actor_->activities_.remove(this);
154 /* Answer all simcalls associated with the synchro */
158 void ExecImpl::finish()
160 XBT_DEBUG("ExecImpl::finish() in state %s", to_c_str(state_));
161 while (not simcalls_.empty()) {
162 smx_simcall_t simcall = simcalls_.front();
163 simcalls_.pop_front();
165 /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
166 * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
169 if (simcall->call_ == simix::Simcall::NONE) // FIXME: maybe a better way to handle this case
170 continue; // if process handling comm is killed
172 dynamic_cast<kernel::actor::ExecutionWaitanySimcall*>(simcall->observer_)) { // simcall is a wait_any?
173 const auto& execs = observer->get_execs();
175 for (auto* exec : execs) {
176 exec->unregister_simcall(simcall);
178 if (simcall->timeout_cb_) {
179 simcall->timeout_cb_->remove();
180 simcall->timeout_cb_ = nullptr;
184 if (not MC_is_active() && not MC_record_replay_is_active()) {
185 auto element = std::find(execs.begin(), execs.end(), this);
186 int rank = element != execs.end() ? static_cast<int>(std::distance(execs.begin(), element)) : -1;
187 observer->set_result(rank);
192 simcall->issuer_->context_->set_wannadie();
193 if (simcall->issuer_->get_host()->is_on())
194 simcall->issuer_->exception_ = std::make_exception_ptr(HostFailureException(XBT_THROW_POINT, "Host failed"));
195 /* else, the actor will be killed with no possibility to survive */
198 case State::CANCELED:
199 simcall->issuer_->exception_ = std::make_exception_ptr(CancelException(XBT_THROW_POINT, "Execution Canceled"));
203 simcall->issuer_->exception_ = std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Timeouted"));
207 xbt_assert(state_ == State::DONE, "Internal error in ExecImpl::finish(): unexpected synchro state %s",
211 simcall->issuer_->waiting_synchro_ = nullptr;
212 /* Fail the process if the host is down */
213 if (simcall->issuer_->get_host()->is_on())
214 simcall->issuer_->simcall_answer();
216 simcall->issuer_->context_->set_wannadie();
220 ActivityImpl* ExecImpl::migrate(s4u::Host* to)
222 if (not MC_is_active() && not MC_record_replay_is_active()) {
223 resource::Action* old_action = this->surf_action_;
224 resource::Action* new_action = to->get_cpu()->execution_start(old_action->get_cost());
225 new_action->set_remains(old_action->get_remains());
226 new_action->set_activity(this);
227 new_action->set_sharing_penalty(old_action->get_sharing_penalty());
228 new_action->set_user_bound(old_action->get_user_bound());
230 old_action->set_activity(nullptr);
231 old_action->cancel();
233 this->surf_action_ = new_action;
236 on_migration(*this, to);
240 void ExecImpl::wait_any_for(actor::ActorImpl* issuer, const std::vector<ExecImpl*>& execs, double timeout)
243 issuer->simcall_.timeout_cb_ = nullptr;
245 issuer->simcall_.timeout_cb_ = timer::Timer::set(s4u::Engine::get_clock() + timeout, [issuer, &execs]() {
246 issuer->simcall_.timeout_cb_ = nullptr;
247 for (auto* exec : execs)
248 exec->unregister_simcall(&issuer->simcall_);
249 // default result (-1) is set in actor::ExecutionWaitanySimcall
250 issuer->simcall_answer();
254 for (auto* exec : execs) {
255 /* associate this simcall to the the synchro */
256 exec->simcalls_.push_back(&issuer->simcall_);
258 /* see if the synchro is already finished */
259 if (exec->state_ != State::WAITING && exec->state_ != State::RUNNING) {
269 xbt::signal<void(ExecImpl const&, s4u::Host*)> ExecImpl::on_migration;
271 } // namespace activity
272 } // namespace kernel
273 } // namespace simgrid