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);
56 if (timeout_detector_)
57 timeout_detector_->unref();
58 XBT_DEBUG("Destroy exec %p", this);
61 ExecImpl& ExecImpl::set_host(s4u::Host* host)
63 if (not hosts_.empty())
65 hosts_.push_back(host);
69 ExecImpl& ExecImpl::set_hosts(const std::vector<s4u::Host*>& hosts)
75 ExecImpl& ExecImpl::set_timeout(double timeout)
77 if (timeout > 0 && not MC_is_active() && not MC_record_replay_is_active()) {
78 timeout_detector_ = hosts_.front()->pimpl_cpu->sleep(timeout);
79 timeout_detector_->set_activity(this);
84 ExecImpl& ExecImpl::set_flops_amount(double flops_amount)
86 if (not flops_amounts_.empty())
87 flops_amounts_.clear();
88 flops_amounts_.push_back(flops_amount);
92 ExecImpl& ExecImpl::set_flops_amounts(const std::vector<double>& flops_amounts)
94 flops_amounts_ = flops_amounts;
98 ExecImpl& ExecImpl::set_bytes_amounts(const std::vector<double>& bytes_amounts)
100 bytes_amounts_ = bytes_amounts;
105 ExecImpl* ExecImpl::start()
107 state_ = SIMIX_RUNNING;
108 if (not MC_is_active() && not MC_record_replay_is_active()) {
109 if (hosts_.size() == 1) {
110 surf_action_ = hosts_.front()->pimpl_cpu->execution_start(flops_amounts_.front());
111 surf_action_->set_priority(priority_);
112 surf_action_->set_category(get_tracing_category());
115 surf_action_->set_bound(bound_);
117 surf_action_ = surf_host_model->execute_parallel(hosts_, flops_amounts_.data(), bytes_amounts_.data(), -1);
119 surf_action_->set_activity(this);
122 XBT_DEBUG("Create execute synchro %p: %s", this, get_cname());
123 ExecImpl::on_creation(*this);
127 double ExecImpl::get_seq_remaining_ratio()
129 return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains() / surf_action_->get_cost();
132 double ExecImpl::get_par_remaining_ratio()
134 // parallel task: their remain is already between 0 and 1
135 return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains();
138 ExecImpl& ExecImpl::set_bound(double bound)
144 ExecImpl& ExecImpl::set_priority(double priority)
146 priority_ = priority;
150 void ExecImpl::post()
152 if (hosts_.size() == 1 && not hosts_.front()->is_on()) { /* FIXME: handle resource failure for parallel tasks too */
153 /* If the host running the synchro failed, notice it. This way, the asking
154 * process can be killed if it runs on that host itself */
155 state_ = SIMIX_FAILED;
156 } else if (surf_action_ && surf_action_->get_state() == resource::Action::State::FAILED) {
157 /* If the host running the synchro didn't fail, then the synchro was canceled */
158 state_ = SIMIX_CANCELED;
159 } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
160 state_ = SIMIX_TIMEOUT;
165 on_completion(*this);
168 surf_action_->unref();
169 surf_action_ = nullptr;
171 if (timeout_detector_) {
172 timeout_detector_->unref();
173 timeout_detector_ = nullptr;
176 /* If there are simcalls associated with the synchro, then answer them */
177 if (not simcalls_.empty())
181 void ExecImpl::finish()
183 while (not simcalls_.empty()) {
184 smx_simcall_t simcall = simcalls_.front();
185 simcalls_.pop_front();
189 /* do nothing, synchro done */
190 XBT_DEBUG("ExecImpl::finish(): execution successful");
194 XBT_DEBUG("ExecImpl::finish(): host '%s' failed", simcall->issuer->get_host()->get_cname());
195 simcall->issuer->context_->iwannadie = true;
196 if (simcall->issuer->get_host()->is_on())
197 simcall->issuer->exception_ =
198 std::make_exception_ptr(simgrid::HostFailureException(XBT_THROW_POINT, "Host failed"));
199 /* else, the actor will be killed with no possibility to survive */
203 XBT_DEBUG("ExecImpl::finish(): execution canceled");
204 simcall->issuer->exception_ =
205 std::make_exception_ptr(simgrid::CancelException(XBT_THROW_POINT, "Execution Canceled"));
209 XBT_DEBUG("ExecImpl::finish(): execution timeouted");
210 simcall->issuer->exception_ = std::make_exception_ptr(simgrid::TimeoutError(XBT_THROW_POINT, "Timeouted"));
214 xbt_die("Internal error in ExecImpl::finish(): unexpected synchro state %d", static_cast<int>(state_));
217 simcall->issuer->waiting_synchro = nullptr;
218 simcall_execution_wait__set__result(simcall, state_);
220 /* Fail the process if the host is down */
221 if (simcall->issuer->get_host()->is_on())
222 SIMIX_simcall_answer(simcall);
224 simcall->issuer->context_->iwannadie = true;
228 ActivityImpl* ExecImpl::migrate(s4u::Host* to)
230 if (not MC_is_active() && not MC_record_replay_is_active()) {
231 resource::Action* old_action = this->surf_action_;
232 resource::Action* new_action = to->pimpl_cpu->execution_start(old_action->get_cost());
233 new_action->set_remains(old_action->get_remains());
234 new_action->set_activity(this);
235 new_action->set_priority(old_action->get_priority());
237 // FIXME: the user-defined bound seem to not be kept by LMM, that seem to overwrite it for the multi-core modeling.
238 // I hope that the user did not provide any.
240 old_action->set_activity(nullptr);
241 old_action->cancel();
243 this->surf_action_ = new_action;
246 on_migration(*this, to);
253 xbt::signal<void(ExecImpl&)> ExecImpl::on_creation;
254 xbt::signal<void(ExecImpl const&)> ExecImpl::on_completion;
255 xbt::signal<void(ExecImpl const&, s4u::Host*)> ExecImpl::on_migration;
257 } // namespace activity
258 } // namespace kernel
259 } // namespace simgrid