1 /* Copyright (c) 2007-2019. 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/modelchecker.h"
9 #include "src/mc/mc_replay.hpp"
10 #include "src/surf/HostImpl.hpp"
11 #include "src/surf/cpu_interface.hpp"
12 #include "src/surf/surf_interface.hpp"
14 #include "simgrid/s4u/Host.hpp"
16 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_process);
18 void simcall_HANDLER_execution_wait(smx_simcall_t simcall, simgrid::kernel::activity::ExecImpl* synchro)
20 XBT_DEBUG("Wait for execution of synchro %p, state %d", synchro, (int)synchro->state_);
22 /* Associate this simcall to the synchro */
23 synchro->simcalls_.push_back(simcall);
24 simcall->issuer->waiting_synchro = synchro;
26 /* set surf's synchro */
27 if (MC_is_active() || MC_record_replay_is_active()) {
28 synchro->state_ = SIMIX_DONE;
33 /* If the synchro is already finished then perform the error handling */
34 if (synchro->state_ != SIMIX_RUNNING)
38 void simcall_HANDLER_execution_test(smx_simcall_t simcall, simgrid::kernel::activity::ExecImpl* synchro)
40 int res = (synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING);
42 synchro->simcalls_.push_back(simcall);
45 SIMIX_simcall_answer(simcall);
47 simcall_execution_test__set__result(simcall, res);
54 ExecImpl::ExecImpl(const std::string& name, const std::string& tracing_category) : ActivityImpl(name)
56 this->state_ = SIMIX_RUNNING;
57 this->set_category(tracing_category);
59 XBT_DEBUG("Create exec %p", this);
64 if (timeout_detector_)
65 timeout_detector_->unref();
66 XBT_DEBUG("Destroy exec %p", this);
69 ExecImpl* ExecImpl::set_host(s4u::Host* host)
75 ExecImpl* ExecImpl::set_timeout(double timeout)
77 if (timeout > 0 && not MC_is_active() && not MC_record_replay_is_active()) {
78 timeout_detector_ = host_->pimpl_cpu->sleep(timeout);
79 timeout_detector_->set_data(this);
84 ExecImpl* ExecImpl::start(double flops_amount, double priority, double bound)
86 if (not MC_is_active() && not MC_record_replay_is_active()) {
87 surf_action_ = host_->pimpl_cpu->execution_start(flops_amount);
88 surf_action_->set_data(this);
89 surf_action_->set_priority(priority);
91 surf_action_->set_bound(bound);
94 XBT_DEBUG("Create execute synchro %p: %s", this, get_cname());
95 ExecImpl::on_creation(this);
99 ExecImpl* ExecImpl::start(const std::vector<s4u::Host*>& hosts, const std::vector<double>& flops_amounts,
100 const std::vector<double>& bytes_amounts)
102 /* set surf's synchro */
103 if (not MC_is_active() && not MC_record_replay_is_active()) {
104 surf_action_ = surf_host_model->execute_parallel(hosts, flops_amounts.data(), bytes_amounts.data(), -1);
105 if (surf_action_ != nullptr) {
106 surf_action_->set_data(this);
109 XBT_DEBUG("Create parallel execute synchro %p", this);
110 ExecImpl::on_creation(this);
113 void ExecImpl::cancel()
115 XBT_VERB("This exec %p is canceled", this);
116 if (surf_action_ != nullptr)
117 surf_action_->cancel();
120 double ExecImpl::get_remaining()
122 return surf_action_ ? surf_action_->get_remains() : 0;
125 double ExecImpl::get_seq_remaining_ratio()
127 return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains() / surf_action_->get_cost();
130 double ExecImpl::get_par_remaining_ratio()
132 // parallel task: their remain is already between 0 and 1
133 return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains();
136 void ExecImpl::set_bound(double bound)
139 surf_action_->set_bound(bound);
141 void ExecImpl::set_priority(double priority)
144 surf_action_->set_priority(priority);
147 void ExecImpl::post()
149 if (host_ && not host_->is_on()) { /* FIXME: handle resource failure for parallel tasks too */
150 /* If the host running the synchro failed, notice it. This way, the asking
151 * process can be killed if it runs on that host itself */
152 state_ = SIMIX_FAILED;
153 } else if (surf_action_ && surf_action_->get_state() == resource::Action::State::FAILED) {
154 /* If the host running the synchro didn't fail, then the synchro was canceled */
155 state_ = SIMIX_CANCELED;
156 } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
157 state_ = SIMIX_TIMEOUT;
165 surf_action_->unref();
166 surf_action_ = nullptr;
168 if (timeout_detector_) {
169 timeout_detector_->unref();
170 timeout_detector_ = nullptr;
173 /* If there are simcalls associated with the synchro, then answer them */
174 if (not simcalls_.empty())
178 void ExecImpl::finish()
180 while (not simcalls_.empty()) {
181 smx_simcall_t simcall = simcalls_.front();
182 simcalls_.pop_front();
186 /* do nothing, synchro done */
187 XBT_DEBUG("ExecImpl::finish(): execution successful");
191 XBT_DEBUG("ExecImpl::finish(): host '%s' failed", simcall->issuer->get_host()->get_cname());
192 simcall->issuer->context_->iwannadie = true;
193 if (simcall->issuer->get_host()->is_on())
194 simcall->issuer->exception_ =
195 std::make_exception_ptr(simgrid::HostFailureException(XBT_THROW_POINT, "Host failed"));
196 /* else, the actor will be killed with no possibility to survive */
200 XBT_DEBUG("ExecImpl::finish(): execution canceled");
201 simcall->issuer->exception_ =
202 std::make_exception_ptr(simgrid::CancelException(XBT_THROW_POINT, "Execution Canceled"));
206 XBT_DEBUG("ExecImpl::finish(): execution timeouted");
207 simcall->issuer->exception_ = std::make_exception_ptr(simgrid::TimeoutError(XBT_THROW_POINT, "Timeouted"));
211 xbt_die("Internal error in ExecImpl::finish(): unexpected synchro state %d", static_cast<int>(state_));
214 simcall->issuer->waiting_synchro = nullptr;
215 simcall_execution_wait__set__result(simcall, state_);
217 /* Fail the process if the host is down */
218 if (simcall->issuer->get_host()->is_on())
219 SIMIX_simcall_answer(simcall);
221 simcall->issuer->context_->iwannadie = true;
225 ActivityImpl* ExecImpl::migrate(s4u::Host* to)
227 if (not MC_is_active() && not MC_record_replay_is_active()) {
228 resource::Action* old_action = this->surf_action_;
229 resource::Action* new_action = to->pimpl_cpu->execution_start(old_action->get_cost());
230 new_action->set_remains(old_action->get_remains());
231 new_action->set_data(this);
232 new_action->set_priority(old_action->get_priority());
234 // FIXME: the user-defined bound seem to not be kept by LMM, that seem to overwrite it for the multi-core modeling.
235 // I hope that the user did not provide any.
237 old_action->set_data(nullptr);
238 old_action->cancel();
240 this->surf_action_ = new_action;
243 on_migration(this, to);
250 xbt::signal<void(ExecImplPtr)> ExecImpl::on_creation;
251 xbt::signal<void(ExecImplPtr)> ExecImpl::on_completion;
252 xbt::signal<void(ExecImplPtr, s4u::Host*)> ExecImpl::on_migration;
254 } // namespace activity
255 } // namespace kernel
256 } // namespace simgrid